Skip to content

Commit 21681b8

Browse files
Justin Teemartinkpetersen
authored andcommitted
scsi: lpfc: Fix use-after-free KFENCE violation during sysfs firmware write
During the sysfs firmware write process, a use-after-free read warning is logged from the lpfc_wr_object() routine: BUG: KFENCE: use-after-free read in lpfc_wr_object+0x235/0x310 [lpfc] Use-after-free read at 0x0000000000cf164d (in kfence-torvalds#111): lpfc_wr_object+0x235/0x310 [lpfc] lpfc_write_firmware.cold+0x206/0x30d [lpfc] lpfc_sli4_request_firmware_update+0xa6/0x100 [lpfc] lpfc_request_firmware_upgrade_store+0x66/0xb0 [lpfc] kernfs_fop_write_iter+0x121/0x1b0 new_sync_write+0x11c/0x1b0 vfs_write+0x1ef/0x280 ksys_write+0x5f/0xe0 do_syscall_64+0x59/0x90 entry_SYSCALL_64_after_hwframe+0x63/0xcd The driver accessed wr_object pointer data, which was initialized into mailbox payload memory, after the mailbox object was released back to the mailbox pool. Fix by moving the mailbox free calls to the end of the routine ensuring that we don't reference internal mailbox memory after release. Signed-off-by: Justin Tee <[email protected]> Signed-off-by: Martin K. Petersen <[email protected]>
1 parent c051f1a commit 21681b8

File tree

1 file changed

+13
-6
lines changed

1 file changed

+13
-6
lines changed

drivers/scsi/lpfc/lpfc_sli.c

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20815,6 +20815,7 @@ lpfc_wr_object(struct lpfc_hba *phba, struct list_head *dmabuf_list,
2081520815
struct lpfc_mbx_wr_object *wr_object;
2081620816
LPFC_MBOXQ_t *mbox;
2081720817
int rc = 0, i = 0;
20818+
int mbox_status = 0;
2081820819
uint32_t shdr_status, shdr_add_status, shdr_add_status_2;
2081920820
uint32_t shdr_change_status = 0, shdr_csf = 0;
2082020821
uint32_t mbox_tmo;
@@ -20860,11 +20861,15 @@ lpfc_wr_object(struct lpfc_hba *phba, struct list_head *dmabuf_list,
2086020861
wr_object->u.request.bde_count = i;
2086120862
bf_set(lpfc_wr_object_write_length, &wr_object->u.request, written);
2086220863
if (!phba->sli4_hba.intr_enable)
20863-
rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
20864+
mbox_status = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
2086420865
else {
2086520866
mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
20866-
rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
20867+
mbox_status = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
2086720868
}
20869+
20870+
/* The mbox status needs to be maintained to detect MBOX_TIMEOUT. */
20871+
rc = mbox_status;
20872+
2086820873
/* The IOCTL status is embedded in the mailbox subheader. */
2086920874
shdr_status = bf_get(lpfc_mbox_hdr_status,
2087020875
&wr_object->header.cfg_shdr.response);
@@ -20879,10 +20884,6 @@ lpfc_wr_object(struct lpfc_hba *phba, struct list_head *dmabuf_list,
2087920884
&wr_object->u.response);
2088020885
}
2088120886

20882-
if (!phba->sli4_hba.intr_enable)
20883-
mempool_free(mbox, phba->mbox_mem_pool);
20884-
else if (rc != MBX_TIMEOUT)
20885-
mempool_free(mbox, phba->mbox_mem_pool);
2088620887
if (shdr_status || shdr_add_status || shdr_add_status_2 || rc) {
2088720888
lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
2088820889
"3025 Write Object mailbox failed with "
@@ -20900,6 +20901,12 @@ lpfc_wr_object(struct lpfc_hba *phba, struct list_head *dmabuf_list,
2090020901
lpfc_log_fw_write_cmpl(phba, shdr_status, shdr_add_status,
2090120902
shdr_add_status_2, shdr_change_status,
2090220903
shdr_csf);
20904+
20905+
if (!phba->sli4_hba.intr_enable)
20906+
mempool_free(mbox, phba->mbox_mem_pool);
20907+
else if (mbox_status != MBX_TIMEOUT)
20908+
mempool_free(mbox, phba->mbox_mem_pool);
20909+
2090320910
return rc;
2090420911
}
2090520912

0 commit comments

Comments
 (0)