Skip to content

Commit 4cf752f

Browse files
Chanwoo Leemartinkpetersen
authored andcommitted
scsi: ufs: core: Fix NULL pointer dereference in scsi_cmd_priv() calls
ufshcd_tag_to_cmd() may return NULL if no command is associated with the given tag. However, several callers dereference the returned cmd pointer via scsi_cmd_priv() without checking for NULL first, leading to a potential NULL pointer dereference. Fix this by adding NULL checks for cmd before calling scsi_cmd_priv() and moving the lrbp initialization after the NULL check. Signed-off-by: Chanwoo Lee <cw9316.lee@samsung.com> Reviewed-by: Peter Wang <peter.wang@mediatek.com> Reviewed-by: Bart Van Assche <bvanassche@acm.org> Link: https://patch.msgid.link/20260529010739.295391-1-cw9316.lee@samsung.com Signed-off-by: Martin K. Petersen <martin.petersen@oracle.com>
1 parent c39a9a0 commit 4cf752f

2 files changed

Lines changed: 17 additions & 3 deletions

File tree

drivers/ufs/core/ufs-mcq.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -637,7 +637,7 @@ static bool ufshcd_mcq_sqe_search(struct ufs_hba *hba,
637637
struct ufs_hw_queue *hwq, int task_tag)
638638
{
639639
struct scsi_cmnd *cmd = ufshcd_tag_to_cmd(hba, task_tag);
640-
struct ufshcd_lrb *lrbp = scsi_cmd_priv(cmd);
640+
struct ufshcd_lrb *lrbp;
641641
struct utp_transfer_req_desc *utrd;
642642
__le64 cmd_desc_base_addr;
643643
bool ret = false;
@@ -647,6 +647,11 @@ static bool ufshcd_mcq_sqe_search(struct ufs_hba *hba,
647647
if (hba->quirks & UFSHCD_QUIRK_MCQ_BROKEN_RTC)
648648
return true;
649649

650+
if (!cmd)
651+
return false;
652+
653+
lrbp = scsi_cmd_priv(cmd);
654+
650655
mutex_lock(&hwq->sq_mutex);
651656

652657
ufshcd_mcq_sq_stop(hba, hwq);

drivers/ufs/core/ufshcd.c

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7903,8 +7903,12 @@ static void ufshcd_set_req_abort_skip(struct ufs_hba *hba, unsigned long bitmap)
79037903

79047904
for_each_set_bit(tag, &bitmap, hba->nutrs) {
79057905
struct scsi_cmnd *cmd = ufshcd_tag_to_cmd(hba, tag);
7906-
struct ufshcd_lrb *lrbp = scsi_cmd_priv(cmd);
7906+
struct ufshcd_lrb *lrbp;
79077907

7908+
if (!cmd)
7909+
continue;
7910+
7911+
lrbp = scsi_cmd_priv(cmd);
79087912
lrbp->req_abort_skip = true;
79097913
}
79107914
}
@@ -7925,11 +7929,16 @@ static void ufshcd_set_req_abort_skip(struct ufs_hba *hba, unsigned long bitmap)
79257929
int ufshcd_try_to_abort_task(struct ufs_hba *hba, int tag)
79267930
{
79277931
struct scsi_cmnd *cmd = ufshcd_tag_to_cmd(hba, tag);
7928-
struct ufshcd_lrb *lrbp = scsi_cmd_priv(cmd);
7932+
struct ufshcd_lrb *lrbp;
79297933
int err;
79307934
int poll_cnt;
79317935
u8 resp = 0xF;
79327936

7937+
if (!cmd)
7938+
return -EINVAL;
7939+
7940+
lrbp = scsi_cmd_priv(cmd);
7941+
79337942
for (poll_cnt = 100; poll_cnt; poll_cnt--) {
79347943
err = ufshcd_issue_tm_cmd(hba, lrbp->lun, tag, UFS_QUERY_TASK,
79357944
&resp);

0 commit comments

Comments
 (0)