[PATCH] scsi: scsi_debug: Check mode page header length in resp_mode_select()

From: Weidong Wang via B4 Relay

Date: Fri Dec 04 2026 - 03:35:55 EST


From: Weidong Wang <weidongw@xxxxxxxxxxxx>

resp_mode_select() only checks "off >= res", which covers arr[off].
The page length is then read from arr[off + 1], or from arr[off + 2]
and arr[off + 3] with SPF set. For MODE SELECT(10) the block descriptor
length can place off at 509 to 511, so the read goes 1 to 3 bytes past
the 512-byte stack array. The command result is not affected: the
following length check fails in any case.

Check that the 2-byte page header, or 4-byte sub-page header, fits in
the parameter list before reading the page length.

Found by a static analysis tool.

Fixes: 231839102b54 ("[SCSI] scsi_debug version 1.80")
Signed-off-by: Weidong Wang <weidongw@xxxxxxxxxxxx>
---
The fix and the changelog were drafted with AI assistance (Claude Code,
with a second review by OpenAI Codex), then reviewed by the submitter.

Testing:
- Compile-tested on x86_64 (allmodconfig, W=1):
drivers/scsi/scsi_debug.o; no new warnings.
- checkpatch.pl --strict: no errors or warnings.
---
drivers/scsi/scsi_debug.c | 6 ++++++
1 file changed, 6 insertions(+)

diff --git a/drivers/scsi/scsi_debug.c b/drivers/scsi/scsi_debug.c
index c5c4312e23aa..a3af80524ae5 100644
--- a/drivers/scsi/scsi_debug.c
+++ b/drivers/scsi/scsi_debug.c
@@ -3257,6 +3257,12 @@ static int resp_mode_select(struct scsi_cmnd *scp,
return check_condition_result;
}
spf = !!(arr[off] & 0x40);
+ /* The page header must fit before the page length is read from it */
+ if ((off + (spf ? 4 : 2)) > param_len) {
+ mk_sense_buffer(scp, ILLEGAL_REQUEST,
+ PARAMETER_LIST_LENGTH_ERROR);
+ return check_condition_result;
+ }
pg_len = spf ? (get_unaligned_be16(arr + off + 2) + 4) :
(arr[off + 1] + 2);
if ((pg_len + off) > param_len) {

---
base-commit: efd68f17acca65f581b9f2fa9ba50c8643ab544d
change-id: 20261128-kbug-n025-f1bc2cbe2520

Best regards,
--
Weidong Wang <weidongw@xxxxxxxxxxxx>