mvumi_handle_clob() indexes mhba->tag_cmd[] with the tag of a response frame the firmware DMAs into the outbound list. The array holds tag_pool.size entries, and the only check on that tag runs earlier, in mvumi_receive_ob_list_entry() and mvumi_check_ob_frame(), against the frame still sitting in the outbound list. mvumi_receive_ob_list_entry() then copies the frame into an ob_data buffer and mvumi_handle_clob() reads the tag out of that copy, so the value that is checked and the value that is used are two separate fetches of firmware-writable memory. A firmware that changes the tag after the check reaches tag_cmd[] with any u16, reads a pointer past the array and dereferences it, writes NULL past the array and pushes the tag onto the free stack for a later command to pick up. The check is also off by one: tag_cmd[] and tag_pool.stack[] both hold tag_pool.size entries, so a tag equal to tag_pool.size passes a > test and lands one entry past both. Check the tag that mvumi_handle_clob() actually uses, against the size of the array it indexes, and check the command it finds for NULL before dereferencing it: with two fetches the earlier NULL check no longer says anything about this one. Re-check request_id against that command too, where the controller provides one, for the same reason the earlier check does: a tag changed between the check and the copy can land on a different command that is also live, and then only request_id tells the two apart. Make the two existing bounds checks >= so a tag equal to the entry count is rejected there too, and read the tag into a local in mvumi_receive_ob_list_entry() as well, so the check there and the accesses beside it see one value instead of three fetches. Use READ_ONCE() for the tag and request_id both functions take out of the outbound list, so the single fetch is guaranteed rather than left to the compiler. Fixes: f0c568a478f0 ("[SCSI] mvumi: Add Marvell UMI driver") Cc: stable@vger.kernel.org Signed-off-by: Yehyeong Lee --- drivers/scsi/mvumi.c | 45 ++++++++++++++++++++++++++++++++++---------- 1 file changed, 35 insertions(+), 10 deletions(-) diff --git a/drivers/scsi/mvumi.c b/drivers/scsi/mvumi.c index 8b8e5545747e8..2410d2aa147d9 100644 --- a/drivers/scsi/mvumi.c +++ b/drivers/scsi/mvumi.c @@ -466,9 +466,9 @@ static char mvumi_check_ob_frame(struct mvumi_hba *mhba, udelay(1); p_outb_frame = mhba->ob_list + cur_obf * mhba->ob_max_size; - request_id = p_outb_frame->request_id; - tag = p_outb_frame->tag; - if (tag > mhba->tag_pool.size) { + request_id = READ_ONCE(p_outb_frame->request_id); + tag = READ_ONCE(p_outb_frame->tag); + if (tag >= mhba->tag_pool.size) { dev_err(&mhba->pdev->dev, "ob frame data error\n"); return -1; } @@ -530,6 +530,7 @@ static void mvumi_receive_ob_list_entry(struct mvumi_hba *mhba) struct mvumi_ob_data *ob_data; struct mvumi_rsp_frame *p_outb_frame; struct mvumi_hw_regs *regs = mhba->regs; + unsigned short tag; if (mhba->instancet->check_ob_list(mhba, &cur_obf, &assign_obf_end)) return; @@ -546,10 +547,11 @@ static void mvumi_receive_ob_list_entry(struct mvumi_hba *mhba) /* Copy pointer may point to entry in outbound list * before entry has valid data */ - if (unlikely(p_outb_frame->tag > mhba->tag_pool.size || - mhba->tag_cmd[p_outb_frame->tag] == NULL || - p_outb_frame->request_id != - mhba->tag_cmd[p_outb_frame->tag]->request_id)) + tag = READ_ONCE(p_outb_frame->tag); + if (unlikely(tag >= mhba->tag_pool.size || + !mhba->tag_cmd[tag] || + mhba->tag_cmd[tag]->request_id != + READ_ONCE(p_outb_frame->request_id))) if (mvumi_check_ob_frame(mhba, cur_obf, p_outb_frame)) continue; @@ -1878,6 +1880,7 @@ static void mvumi_handle_clob(struct mvumi_hba *mhba) struct mvumi_rsp_frame *ob_frame; struct mvumi_cmd *cmd; struct mvumi_ob_data *pool; + unsigned short tag; while (!list_empty(&mhba->free_ob_list)) { pool = list_first_entry(&mhba->free_ob_list, @@ -1886,11 +1889,33 @@ static void mvumi_handle_clob(struct mvumi_hba *mhba) list_add_tail(&pool->list, &mhba->ob_data_list); ob_frame = (struct mvumi_rsp_frame *) &pool->data[0]; - cmd = mhba->tag_cmd[ob_frame->tag]; + /* + * The tag was checked against the frame in the outbound list. + * This is the copy, so check the value that is used. + */ + tag = ob_frame->tag; + if (tag >= mhba->tag_pool.size) { + dev_err(&mhba->pdev->dev, + "ob frame tag[0x%x] out of range\n", tag); + continue; + } + cmd = mhba->tag_cmd[tag]; + if (!cmd) { + dev_err(&mhba->pdev->dev, + "ob frame tag[0x%x] with no command\n", tag); + continue; + } + if (mhba->request_id_enabled && + ob_frame->request_id != cmd->request_id) { + dev_err(&mhba->pdev->dev, + "ob frame tag[0x%x] request ID 0x%x != 0x%x\n", + tag, ob_frame->request_id, cmd->request_id); + continue; + } atomic_dec(&mhba->fw_outstanding); - mhba->tag_cmd[ob_frame->tag] = NULL; - tag_release_one(mhba, &mhba->tag_pool, ob_frame->tag); + mhba->tag_cmd[tag] = NULL; + tag_release_one(mhba, &mhba->tag_pool, tag); if (cmd->scmd) mvumi_complete_cmd(mhba, cmd, ob_frame); else -- 2.43.0