Skip to contentSkip to conversation

usb: image: microtek: don't receive REQUEST SENSE data into sense_buffer

PATCH NAK Alexandre Becholey 3 participants 7 messages Nov 19 – Nov 23

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 usb: image: microtek: don't receive REQUEST SENSE data into sense_buffer

mts_command_done() receives the data phase of a REQUEST SENSE into srb->sense_buffer (SCSI_SENSE_BUFFERSIZE, 96 bytes), but with the length of the command's own data buffer. A REQUEST SENSE sent through SG_IO, allowed on a read-only /dev/sgN without any capability, can have a much larger buffer (252 bytes with sg_requests), and a device that returns that much writes past the end of the sense buffer.

The special case is not needed: for the error handler's own REQUEST SENSE, scsi_eh_prep_cmnd() already points the data buffer at sense_buffer with at most SCSI_SENSE_BUFFERSIZE. Handle REQUEST SENSE like every other data-in command.

The bug was found, and the reproducer and this fix were written, by AI agents (Claude Mythos and Claude Opus models). The analysis and the patch were reviewed by a human.

Fixes 1da177e4c3f4 ("Linux-2.6.12-rc2")

---
An emulated ScanMaker X6 answering a 252-byte SG_IO REQUEST SENSE with 252 bytes triggers it (KASAN in dummy_timer, the HCD's copy into the URB buffer; slub_debug redzone report with xHCI), on v7.3.1 and master 9d66f26fefac. No reports with the fix. User-space REQUEST SENSE now gets the sense data in its own buffer; the error handler's REQUEST SENSE returns the same data as before. Not tested on real hardware. Reproducer available on request.

 drivers/usb/image/microtek.c | 19 ++++++-------------
 1 file changed, 6 insertions(+), 13 deletions(-)
drivers/usb/image/microtek.c +6 -13
index 1f0b3c44d388..0b8d7f71c3c9 100644
@@ -445,22 +445,15 @@ static void mts_command_done( struct urb *transfer )
 		return;
 	}
 
-	if (context->srb->cmnd[0] == REQUEST_SENSE) {
+	if (context->data) {
 		mts_int_submit_urb(transfer,
 				   context->data_pipe,
-				   context->srb->sense_buffer,
+				   context->data,
 				   context->data_length,
-				   mts_data_done);
-	} else { if ( context->data ) {
-			mts_int_submit_urb(transfer,
-					   context->data_pipe,
-					   context->data,
-					   context->data_length,
-					   scsi_sg_count(context->srb) > 1 ?
-					           mts_do_sg : mts_data_done);
-		} else {
-			mts_get_status(transfer);
-		}
+				   scsi_sg_count(context->srb) > 1 ?
+				   mts_do_sg : mts_data_done);
+	} else {
+		mts_get_status(transfer);
 	}
 }
 
Signature · 1 line
2.53.0
#1 lore raw
Sashiko has reviewed this patch and found no issues.
Quoted 12 lines

mts_command_done() receives the data phase of a REQUEST SENSE into srb->sense_buffer (SCSI_SENSE_BUFFERSIZE, 96 bytes), but with the length of the command's own data buffer. A REQUEST SENSE sent through SG_IO, allowed on a read-only /dev/sgN without any capability, can have a much larger buffer (252 bytes with sg_requests), and a device that returns that much writes past the end of the sense buffer.

The special case is not needed: for the error handler's own REQUEST SENSE, scsi_eh_prep_cmnd() already points the data buffer at sense_buffer with at most SCSI_SENSE_BUFFERSIZE. Handle REQUEST SENSE like every other data-in command.

Quoted 7 lines

The bug was found, and the reproducer and this fix were written, by AI agents (Claude Mythos and Claude Opus models). The analysis and the patch were reviewed by a human.

Fixes 1da177e4c3f4 ("Linux-2.6.12-rc2")

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
NACKED-BY on v1 1/1 NAK
#2 lore raw
In reply to Alexandre Becholey
Quoted 4 lines · Alexandre Becholey

The special case is not needed: for the error handler's own REQUEST SENSE, scsi_eh_prep_cmnd() already points the data buffer at sense_buffer with at most SCSI_SENSE_BUFFERSIZE. Handle REQUEST SENSE like every other data-in command.

Hi,

thank you for looking at this, but this fix seems to not take an approach I can approve. Where sense_buffer is pointed to is internal to the SCSI layer. We cannot use that in an LLD.

If you think that LLDs should not use sense_buffer then the clean solution is to remove it from struct scsi_cmnd.

Regards
Oliver

#3 lore raw
Quoted 12 lines · Oliver Neukum
Quoted 4 lines · Alexandre Becholey

The special case is not needed: for the error handler's own REQUEST SENSE, scsi_eh_prep_cmnd() already points the data buffer at sense_buffer with at most SCSI_SENSE_BUFFERSIZE. Handle REQUEST SENSE like every other data-in command.

Hi,

thank you for looking at this, but this fix seems to not take an approach I can approve. Where sense_buffer is pointed to is internal to the SCSI layer. We cannot use that in an LLD.

Quoted 8 lines

If you think that LLDs should not use sense_buffer then the clean solution is to remove it from struct scsi_cmnd.

Regards
Oliver

Hi Oliver,

Would you take a v2 that keeps receiving the REQUEST SENSE data into sense_buffer and only limits the transfer to SCSI_SENSE_BUFFERSIZE?

        if (context->srb->cmnd[0] == REQUEST_SENSE) {
                mts_int_submit_urb(transfer,
                                   context->data_pipe,
                                   context->srb->sense_buffer,
                                   min_t(unsigned int, context->data_length,
                                         SCSI_SENSE_BUFFERSIZE),
                                   mts_data_done);

The device can then no longer write past the 96-byte buffer, and nothing else changes.

Kind regards,
Alexandre

#4 lore raw
Quoted 20 lines · Alexandre Becholey
Quoted 18 lines · Oliver Neukum
Quoted 4 lines · Alexandre Becholey

The special case is not needed: for the error handler's own REQUEST SENSE, scsi_eh_prep_cmnd() already points the data buffer at sense_buffer with at most SCSI_SENSE_BUFFERSIZE. Handle REQUEST SENSE like every other data-in command.

Hi,

thank you for looking at this, but this fix seems to not take an approach I can approve. Where sense_buffer is pointed to is internal to the SCSI layer. We cannot use that in an LLD.

If you think that LLDs should not use sense_buffer then the clean solution is to remove it from struct scsi_cmnd.

Regards
Oliver

Quoted 6 lines
Quoted 2 lines

Hi Oliver,

Would you take a v2 that keeps receiving the REQUEST SENSE data into sense_buffer and only limits the transfer to SCSI_SENSE_BUFFERSIZE?

Hi,

sure that would be fine.

Yet, I am afraid I have to ask why you want to solve this issue at the level of the LLD? This looks like a generic issue to me. The size of sense_buffer is documented. I don't see how this issue can be limited to microtek.

Regards
Oliver

#5 lore raw
Quoted 25 lines · Oliver Neukum
Quoted 22 lines · Alexandre Becholey
Quoted 20 lines · Oliver Neukum
Quoted 4 lines · Alexandre Becholey

The special case is not needed: for the error handler's own REQUEST SENSE, scsi_eh_prep_cmnd() already points the data buffer at sense_buffer with at most SCSI_SENSE_BUFFERSIZE. Handle REQUEST SENSE like every other data-in command.

Hi,

thank you for looking at this, but this fix seems to not take an approach I can approve. Where sense_buffer is pointed to is internal to the SCSI layer. We cannot use that in an LLD.

If you think that LLDs should not use sense_buffer then the clean solution is to remove it from struct scsi_cmnd.

Regards
Oliver

Quoted 8 lines
Quoted 4 lines

Hi Oliver,

Would you take a v2 that keeps receiving the REQUEST SENSE data into sense_buffer and only limits the transfer to SCSI_SENSE_BUFFERSIZE?

Hi,

sure that would be fine.

Thanks, v2 will do that.

Quoted 10 lines

Yet, I am afraid I have to ask why you want to solve this issue at the level of the LLD? This looks like a generic issue to me. The size of sense_buffer is documented. I don't see how this issue can be limited to microtek.

Regards
Oliver

The overflow comes from microtek receiving the data phase of a REQUEST SENSE command into sense_buffer with the length of the command's own data buffer. That length is chosen by whoever sends the command (sg allows REQUEST SENSE on a read-only node) and has nothing to do with sense_buffer, so I don't think the core can see the problem.

I looked at the other drivers for that special-case REQUEST SENSE (in drivers/scsi, drivers/ata and drivers/usb). They either transfer its data into the command's data buffer like any other data-in command, or emulate it there. I found no other driver that copies a caller-sized transfer into sense_buffer, so I think the fix belongs in microtek. If you see a place where the core should enforce the size, I am happy to look at it.

Kind regards,
Alexandre

#6 lore raw
Quoted 7 lines · Alexandre Becholey

I looked at the other drivers for that special-case REQUEST SENSE (in drivers/scsi, drivers/ata and drivers/usb). They either transfer its data into the command's data buffer like any other data-in command, or emulate it there. I found no other driver that copies a caller-sized transfer into sense_buffer, so I think the fix belongs in microtek. If you see a place where the core should enforce the size, I am happy to look at it.

Thank you, that makes the situation clearer. Yet it kind of brings back one question: Why does scsi_cmnd have the member sense_buffer at all, if you simple can write the result into the generic buffer?

I have no problem with cleaning up an issue in the microtek driver, but if an issue is to be cleaned up, it should be done fully.

Regards
Oliver