scsi: ufs: Fix UIC result byte and BSG request length handling
Subsystems (3)
cover scsi: ufs: Fix UIC result byte and BSG request length handling
Two ufs-bsg fixes for issues found by fuzzing /dev/bsg/ufs-bsg* with syzkaller on QEMU's emulated UFS host controller, plus a related fix for the same UIC result byte problem in the peer DME retry path.
Patch 1 rejects requests shorter than the UFS BSG request structures. Patch 2 clears the UIC result byte supplied by userspace. Patch 3 clears the stale UIC result byte before peer DME retries.
Tested on QEMU arm64: the reproducers no longer trigger.
Stanley Jhu (3):
scsi: ufs: bsg: Validate request_len before accessing request
scsi: ufs: bsg: Clear the UIC result byte before sending UIC commands
scsi: ufs: core: Clear stale UIC result byte before peer DME retries
drivers/ufs/core/ufs_bsg.c | 9 +++++++++ drivers/ufs/core/ufshcd.c | 2 ++ 2 files changed, 11 insertions(+)
Signature · 1 line
2.56.0.385.gd3acb90ef8-goog
PATCH 1/3 scsi: ufs: bsg: Validate request_len before accessing request
ufs_bsg_request() and ufs_bsg_exec_advanced_rpmb_req() read the request buffer without checking job->request_len, which userspace controls. A short request causes an out-of-bounds read. Found by syzkaller.
Reject requests shorter than the structure each path reads.
--- drivers/ufs/core/ufs_bsg.c | 8 ++++++++ 1 file changed, 8 insertions(+)
drivers/ufs/core/ufs_bsg.c +8 -0
@@ -73,6 +73,9 @@ static int ufs_bsg_exec_advanced_rpmb_req(struct ufs_hba *hba, struct bsg_job *jif (hba->ufs_version <ufshci_version (4 ,0 ) || !hba->dev_info.b_advanced_rpmb_en)return -EINVAL ; +if (job->request_len <sizeof (*rpmb_request)) +return -EINVAL ; +if (rpmb_request->ehs_req.length !=2 || rpmb_request->ehs_req.ehs_type !=1 )return -EINVAL ;
@@ -142,6 +145,11 @@ static int ufs_bsg_request(struct bsg_job *job) bsg_reply->reply_payload_rcv_len =0 ; +if (job->request_len <sizeof (*bsg_request)) { + bsg_reply->result = -EINVAL ; +return -EINVAL ; + } +ufshcd_rpm_get_sync (hba); msgcode = bsg_request->msgcode;
Signature · 1 line
2.56.0.385.gd3acb90ef8-goog
[email protected]
AI reviewRe: [PATCH 1/3] scsi: ufs: bsg: Validate request_len before accessing request
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [High] ufs/bsg: heap buffer overflow in Advanced RPMB reply handling - [Medium] block: memory and device…
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] ufs/bsg: heap buffer overflow in Advanced RPMB reply handling
- [Medium] block: memory and device refcount leak on ufs_bsg_request() error path --
commit 6c7ab99a857f0be8f1f19eb78aa230d8ccb39b14
Author: Stanley Jhu <[email protected]>
scsi: ufs: bsg: Validate request_len before accessing request
This commit adds validation checks for job->request_len in the UFS BSG paths to prevent out-of-bounds reads when userspace provides short requests.
drivers/ufs/core/ufs_bsg.c static int ufs_bsg_exec_advanced_rpmb_req(struct ufs_hba *hba, struct bsg_job *j · line 78
Quoted 11 lines
drivers/ufs/core/ufs_bsg.c +3
-0@@ -73,6 +73,9 @@ static int ufs_bsg_exec_advanced_rpmb_req(struct ufs_hba *hba, struct bsg_job *jif (hba->ufs_version <ufshci_version (4 ,0 ) || !hba->dev_info.b_advanced_rpmb_en)return -EINVAL ; +if (job->request_len <sizeof (*rpmb_request)) +return -EINVAL ; +
[Severity: High]
While this properly validates the request size, does the reply handling contain a heap buffer overflow for advanced RPMB requests?
The BSG core allocates job->reply in bsg_init_rq() with a fixed size of SCSI_SENSE_BUFFERSIZE (96 bytes). However, ufs_bsg_exec_advanced_rpmb_req() casts job->reply to struct ufs_rpmb_reply, which has a size of 104 bytes:sizeof(struct ufs_bsg_reply) (40 bytes) + sizeof(struct ufs_ehs) (64 bytes).
When ufshcd_advanced_rpmb_req_handler() is called, it writes 64 bytes to the ehs_rsp field at offset 40, resulting in an 8-byte out-of-bounds write on the heap:
drivers/ufs/core/ufshcd.c:ufshcd_advanced_rpmb_req_handler() {
...
memcpy(rsp_ehs, ehs_data, ehs_len * 32);
...
}Later, ufs_bsg_request() sets job->reply_len to 104 for RPMB replies, which configures the block layer to copy 104 bytes to user space from this 96-byte allocation. Will this leak adjacent heap memory to userspace?
[ ... ]
drivers/ufs/core/ufs_bsg.c static int ufs_bsg_request(struct bsg_job *job) · line 152
Quoted 9 lines
drivers/ufs/core/ufs_bsg.c +5
-0@@ -142,6 +145,11 @@ static int ufs_bsg_request(struct bsg_job *job) bsg_reply->reply_payload_rcv_len =0 ; +if (job->request_len <sizeof (*bsg_request)) { + bsg_reply->result = -EINVAL ; +return -EINVAL ; + } +
[Severity: Medium]
Does returning -EINVAL directly bypass the BSG job completion mechanism and permanently leak the job's resources?
When ufs_bsg_request() returns -EINVAL directly, the block layer's bsg_queue_rq() returns BLK_STS_IOERR. This causes blk_mq_end_request() to complete the request without invoking the queue's .complete callback (bsg_complete()).
Because bsg_complete() is bypassed, bsg_job_put() is never called, meaning bsg_teardown_job() never executes. This leaves the sg_list memory and the job->dev reference count permanently leaked, which would prevent the UFS device from ever being cleanly removed.
Should this error path instead call bsg_job_done(job, -EINVAL, 0) and return 0 to ensure proper teardown?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Re: [PATCH 1/3] scsi: ufs: bsg: Validate request_len before accessing request
Quoted 3 lines · On Tue, Oct 13, 2026 at 10:44:34AM +0000, [email protected] wrote:
Later,
ufs_bsg_request()setsjob->reply_lento 104 for RPMB replies, which configures the block layer to copy 104 bytes to user space from this 96-byte allocation. Will this leak adjacent heap memory to userspace?
Pre-existing; fixed separately in bsg-lib:
https://lore.kernel.org/all/[email protected]/
Quoted 2 lines
Should this error path instead call
bsg_job_done(job, -EINVAL, 0)and return 0 to ensure proper teardown?
Yes, done in v2 (1/5 and 3/5):
https://lore.kernel.org/all/[email protected]/
Re: [PATCH 1/3] scsi: ufs: bsg: Validate request_len before accessing request
In reply to Stanley JhuQuoted 37 lines · Stanley Jhu
ufs_bsg_request()andufs_bsg_exec_advanced_rpmb_req()read the request buffer without checkingjob->request_len, which userspace controls. A short request causes an out-of-bounds read. Found by syzkaller.Reject requests shorter than the structure each path reads.
Fixes 64d4864714c2 ("scsi: ufs: bsg: Remove unnecessary length checkup")Fixes 6ff265fc5ef6 ("scsi: ufs: core: bsg: Add advanced RPMB support in
ufs_bsg")Signed-off-by Stanley Jhu <[email protected]>---
drivers/ufs/core/ufs_bsg.c | 8 ++++++++1 file changed, 8 insertions(+)drivers/ufs/core/ufs_bsg.c +0
-0@@ -73,6 +73,9 @@ static int ufs_bsg_exec_advanced_rpmb_req(struct ufs_hba *hba, struct bsg_job *jif (
hba->ufs_version<ufshci_version(4, 0)|| !hba-Quoted 1 line
dev_info.b_advanced_rpmb_en)
return
-EINVAL;
+ if (job->request_len<sizeof(*rpmb_request)) + return-EINVAL;
+
if (rpmb_request->ehs_req.length!= 2 || rpmb_request-Quoted 1 line
ehs_req.ehs_type != 1)
return
-EINVAL;drivers/ufs/core/ufs_bsg.c +0
-0@@ -142,6 +145,11 @@ static int ufs_bsg_request(struct bsg_job *job)
bsg_reply->reply_payload_rcv_len = 0;
Quoted 8 lines
+ if (
job->request_len<sizeof(*let )) {
+ bsg_reply->result =-EINVAL;
+ return-EINVAL;
+ }
+
ufshcd_rpm_get_sync(hba);
msgcode =bsg_request->msgcode;
Stanley,
The check was removed by my commit 64d4864714c2 when adding arpmb support, since msgcode check was thought to be enough, thanks for fixing it.
PATCH 2/3 scsi: ufs: bsg: Clear the UIC result byte before sending UIC commands
In reply to Stanley Jhuufs_bsg_request() passes the user-supplied UIC argument2 unchanged, including the result byte (bits 7:0) that only the host controller writes. A non-zero value triggers the WARN_ON_ONCE() in ufshcd_dispatch_uic_cmd() and corrupts the reported result. Found by syzkaller.
Clear the result byte before sending the command.
--- drivers/ufs/core/ufs_bsg.c | 1 + 1 file changed, 1 insertion(+)
drivers/ufs/core/ufs_bsg.c +1 -0
@@ -179,6 +179,7 @@ static int ufs_bsg_request(struct bsg_job *job)break ;case UPIU_TRANSACTION_UIC_CMD :memcpy (&uc, &bsg_request->upiu_req.uc,UIC_CMD_SIZE ); + uc.argument2 &= ~MASK_UIC_COMMAND_RESULT ; ret =ufshcd_send_bsg_uic_cmd (hba, &uc);if (ret)dev_err (hba->dev,"send uic cmd: error code %d\n" , ret);
Signature · 1 line
2.56.0.385.gd3acb90ef8-goog
Re: [PATCH 2/3] scsi: ufs: bsg: Clear the UIC result byte before sending UIC commands
Quoted 8 lines · Stanley Jhu
ufs_bsg_request()passes the user-supplied UIC argument2 unchanged, including the result byte (bits 7:0) that only the host controller writes. A non-zero value triggers theWARN_ON_ONCE()inufshcd_dispatch_uic_cmd()and corrupts the reported result. Found by syzkaller.Clear the result byte before sending the command.
Quoted 7 lines
Fixes e77044c5a842 ("scsi: ufs-bsg: Add support for uic commands in
ufs_bsg_request()")Fixes 9fb4c793223b ("scsi: ufs: core: Complain if UIC argument 2 isinvalid")
Signed-off-by Stanley Jhu <[email protected]>---
Re: [PATCH 2/3] scsi: ufs: bsg: Clear the UIC result byte before sending UIC commands
In reply to Stanley JhuQuoted 6 lines · Stanley Jhu
ufs_bsg_request()passes the user-supplied UIC argument2 unchanged, including the result byte (bits 7:0) that only the host controller writes. A non-zero value triggers theWARN_ON_ONCE()inufshcd_dispatch_uic_cmd()and corrupts the reported result. Found by syzkaller.
Quoted 7 lines
Clear the result byte before sending the command.
Fixes e77044c5a842 ("scsi: ufs-bsg: Add support for uic commands in
ufs_bsg_request()")Fixes 9fb4c793223b ("scsi: ufs: core: Complain if UIC argument 2 is invalid")Signed-off-by Stanley Jhu <[email protected]>
Bart Van Assche
reviewerRe: [PATCH 2/3] scsi: ufs: bsg: Clear the UIC result byte before sending UIC commands
In reply to Stanley JhuQuoted 21 lines · Stanley Jhu
ufs_bsg_request()passes the user-supplied UIC argument2 unchanged, including the result byte (bits 7:0) that only the host controller writes. A non-zero value triggers theWARN_ON_ONCE()inufshcd_dispatch_uic_cmd()and corrupts the reported result. Found by syzkaller.Clear the result byte before sending the command.
Fixes e77044c5a842 ("scsi: ufs-bsg: Add support for uic commands in ufs_bsg_request()")Fixes 9fb4c793223b ("scsi: ufs: core: Complain if UIC argument 2 is invalid")Signed-off-by Stanley Jhu <[email protected]>--- drivers/ufs/core/ufs_bsg.c | 1 + 1 file changed, 1 insertion(+)drivers/ufs/core/ufs_bsg.c +0
-0
drivers/ufs/core/ufs_bsg.c static int ufs_bsg_request(struct bsg_job *job) · line 185
Quoted 7 lines
drivers/ufs/core/ufs_bsg.c +1
-0@@ -179,6 +179,7 @@ static int ufs_bsg_request(struct bsg_job *job)break ;case UPIU_TRANSACTION_UIC_CMD :memcpy (&uc, &bsg_request->upiu_req.uc,UIC_CMD_SIZE ); + uc.argument2 &= ~MASK_UIC_COMMAND_RESULT ; ret =ufshcd_send_bsg_uic_cmd (hba, &uc);if (ret)dev_err (hba->dev,"send uic cmd: error code %d\n" , ret);
Shouldn't invalid inputs be rejected rather than trying to correct these?
Thanks,
Bart.
Re: [PATCH 2/3] scsi: ufs: bsg: Clear the UIC result byte before sending UIC commands
Quoted 1 line · Bart Van Assche
Shouldn't invalid inputs be rejected rather than trying to correct these?
Agreed. v2 rejects a non-zero result byte with -EINVAL:
https://lore.kernel.org/all/[email protected]/
PATCH 3/3 scsi: ufs: core: Clear stale UIC result byte before peer DME retries
In reply to Stanley JhuPeer DME get and set retry a failed attempt with the same struct uic_command. UIC completion ORs the ConfigResultCode into argument2[7:0], and nothing clears it before the next attempt. Once an attempt fails, every retry keeps reporting that error code, even when the device returns success, so a transient failure becomes a hard failure after three retries. Each such retry also triggers the WARN_ON_ONCE() in ufshcd_dispatch_uic_cmd().
Clear the result byte before each attempt.
--- drivers/ufs/core/ufshcd.c | 2 ++ 1 file changed, 2 insertions(+)
drivers/ufs/core/ufshcd.c +2 -0
@@ -4398,6 +4398,7 @@ int ufshcd_dme_set_attr(struct ufs_hba *hba, u32 attr_sel,int retries =UFS_UIC_COMMAND_RETRIES ; do { + uic_cmd.argument2 &= ~MASK_UIC_COMMAND_RESULT; /* for peer attributes we retry upon failure */ ret = ufshcd_send_uic_cmd(hba, &uic_cmd); if (ret)
@@ -4465,6 +4466,7 @@ int ufshcd_dme_get_attr(struct ufs_hba *hba, u32 attr_sel, } do { + uic_cmd.argument2 &= ~MASK_UIC_COMMAND_RESULT; /* for peer attributes we retry upon failure */ ret = ufshcd_send_uic_cmd(hba, &uic_cmd); if (ret)
Signature · 1 line
2.56.0.385.gd3acb90ef8-goog
[email protected]
AI reviewRe: [PATCH 3/3] scsi: ufs: core: Clear stale UIC result byte before peer DME retries
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] scsi: ufs: dme_set retry loop reuses potentially corrupted argument3 --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] scsi: ufs: dme_set retry loop reuses potentially corrupted argument3 --
commit b7bd98803047d25927dcd2349ce8724199680d23
Author: Stanley Jhu <[email protected]>
scsi: ufs: core: Clear stale UIC result byte before peer DME retries
Peer DME get and set retry a failed attempt with the same struct uic_command. This commit clears the result byte in argument2 before each attempt to prevent transient failures from becoming hard failures due to stale error codes.
drivers/ufs/core/ufshcd.c int ufshcd_dme_set_attr(struct ufs_hba *hba, u32 attr_sel, · line 4401
Quoted 9 lines
drivers/ufs/core/ufshcd.c +1
-0@@ -4398,6 +4398,7 @@ int ufshcd_dme_set_attr(struct ufs_hba *hba, u32 attr_sel,int retries =UFS_UIC_COMMAND_RETRIES ; do { + uic_cmd.argument2 &= ~MASK_UIC_COMMAND_RESULT;
[Severity: Medium]
When ufshcd_send_uic_cmd() fails, does the completion handler overwrite argument3?
Looking at ufshcd_uic_cmd_compl(), it appears argument3 is unconditionally updated with the hardware register's contents:
ufshcd_uic_cmd_compl() {
...
/* Store the DME attribute value in cmd->argument3. */
cmd->argument3 = ufshcd_readl(hba, REG_UIC_COMMAND_ARG_3);
...
}Since uic_cmd.argument3 is initialized to mib_val only once outside the retry loop in ufshcd_dme_set_attr(), can this regression cause a subsequent retry to write whatever value the hardware left in the register during the failure?
Should uic_cmd.argument3 be restored to mib_val before retrying, similar to how argument2 is cleared here?
Quoted 3 lines
drivers/ufs/core/ufshcd.c +0
-0/* for peer attributes we retry upon failure */ ret =ufshcd_send_uic_cmd (hba, &uic_cmd);if (ret)
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
Re: [PATCH 3/3] scsi: ufs: core: Clear stale UIC result byte before peer DME retries
Quoted 7 lines · On Tue, Oct 13, 2026 at 10:41:55AM +0000, [email protected] wrote:
Since uic_cmd.argument3 is initialized to
mib_valonly once outside the retry loop inufshcd_dme_set_attr(), can this regression cause a subsequent retry to write whatever value the hardware left in the register during the failure?Should uic_cmd.argument3 be restored to
mib_valbefore retrying, similar to how argument2 is cleared here?
Good point. ufshcd_uic_cmd_compl() always overwrites argument3, so the DME_SET retry should restore mib_val as well. I'll fold that into v2.
Re: [PATCH 3/3] scsi: ufs: core: Clear stale UIC result byte before peer DME retries
In reply to [email protected]Quoted 2 lines · On Tue, Oct 13, 2026 at 10:41:55AM +0000, [email protected] wrote:
Should uic_cmd.argument3 be restored to
mib_valbefore retrying, similar to how argument2 is cleared here?
Yes, done in v2:
https://lore.kernel.org/all/[email protected]/
Re: [PATCH 3/3] scsi: ufs: core: Clear stale UIC result byte before peer DME retries
In reply to Stanley JhuQuoted 34 lines · Stanley Jhu
Peer DME get and set retry a failed attempt with the same struct
uic_command. UIC completion ORs the ConfigResultCode into argument2[7:0], and nothing clears it before the next attempt. Once an attempt fails, every retry keeps reporting that error code, even when the device returns success, so a transient failure becomes a hard failure after three retries. Each such retry also triggers theWARN_ON_ONCE()inufshcd_dispatch_uic_cmd().Clear the result byte before each attempt.
Fixes 64238fbd15ea ("scsi: ufs: add retries to dme_peer get and setattribute")
Signed-off-by Stanley Jhu <[email protected]>---
drivers/ufs/core/ufshcd.c | 2 ++1 file changed, 2 insertions(+)drivers/ufs/core/ufshcd.c +0
-0@@ -4398,6 +4398,7 @@ int ufshcd_dme_set_attr(struct ufs_hba *hba, u32 attr_sel, int retries = UFS_UIC_COMMAND_RETRIES;
do {
+ uic_cmd.argument2 &= ~MASK_UIC_COMMAND_RESULT; /* for peer attributes we retry upon failure */ ret =ufshcd_send_uic_cmd(hba, &uic_cmd); if (ret)drivers/ufs/core/ufshcd.c +0
-0@@ -4465,6 +4466,7 @@ int ufshcd_dme_get_attr(struct ufs_hba *hba, u32 attr_sel, }
Quoted 6 lines
do {
+ uic_cmd.argument2 &= ~MASK_UIC_COMMAND_RESULT; /* for peer attributes we retry upon failure */ ret =ufshcd_send_uic_cmd(hba, &uic_cmd); if (ret)
Stanley,
AI Sashiko comment on arg3 should not happen on a spec compliant ufshci controller, since arg3 is an input for dme_set, arg2 indeed has an output byte for every command, but restoring arg3 in the set loop would be cheap safety enhancement, but up to you:
for me this patch is ok: