Skip to contentSkip to conversation

bsg: allocate response buffer if requested

PATCH In review Hannes Reinecke 3 participants 6 messages Jan 30, 2019 – Feb 4, 2019

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

PATCH 1/1 bsg: allocate response buffer if requested

The 'response' buffer from bsg is mapped onto the SCSI sense buffer, however after commit 82ed4db499b8 we need to allocate them ourselves as the bsg queue is _not_ a SCSI queue, and hence the sense buffer won't be allocated from the scsi stack.

Fixes 82ed4db499b8 ("block: split scsi_request out of struct request")
---
 block/bsg.c | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)
block/bsg.c +11 -1
index 50e5f8f666f2..7554901096c8 100644
@@ -81,6 +81,13 @@ static int bsg_scsi_fill_hdr(struct request *rq, struct sg_io_v4 *hdr,
 			return -ENOMEM;
 	}
 
+	if (hdr->response) {
+		sreq->sense = kzalloc(hdr->max_response_len, GFP_KERNEL);
+		if (!sreq->sense)
+			return -ENOMEM;
+	} else
+		sreq->sense = NULL;
+
 	if (copy_from_user(sreq->cmd, uptr64(hdr->request), sreq->cmd_len))
 		return -EFAULT;
 	if (blk_verify_command(sreq->cmd, mode))
@@ -128,7 +135,10 @@ static int bsg_scsi_complete_rq(struct request *rq, struct sg_io_v4 *hdr)
 
 static void bsg_scsi_free_rq(struct request *rq)
 {
-	scsi_req_free_cmd(scsi_req(rq));
+	struct scsi_request *sreq = scsi_req(rq);
+
+	kfree(sreq->sense);
+	scsi_req_free_cmd(sreq);
 }
 
 static const struct bsg_ops bsg_scsi_ops = {
Signature · 1 line
2.16.4
Reviewed-by on v1 1/1
#1 lore raw
Quoted 8 lines · Hannes Reinecke

The 'response' buffer from bsg is mapped onto the SCSI sense buffer, however after commit 82ed4db499b8 we need to allocate them ourselves as the bsg queue is _not_a SCSI queue, and hence the sense buffer won't be allocated from the scsi stack.

Fixes 82ed4db499b8 ("block: split scsi_request out of struct request"

Please add a "Cc: stable"tag. Anyway:

Reviewed-by on v1 1/1
#2 lore raw
In reply to Hannes Reinecke
Quoted 6 lines · Hannes Reinecke

The 'response' buffer from bsg is mapped onto the SCSI sense buffer, however after commit 82ed4db499b8 we need to allocate them ourselves as the bsg queue is _not_ a SCSI queue, and hence the sense buffer won't be allocated from the scsi stack.

Fixes 82ed4db499b8 ("block: split scsi_request out of struct request")

Since this patch fixes a bug introduced in kernel v4.11, should this patch have a "Cc: stable" tag? Anyway:

#3 lore raw
In reply to Hannes Reinecke
Quoted 4 lines · Hannes Reinecke

The 'response' buffer from bsg is mapped onto the SCSI sense buffer, however after commit 82ed4db499b8 we need to allocate them ourselves as the bsg queue is _not_ a SCSI queue, and hence the sense buffer won't be allocated from the scsi stack.

I don't think this is the full story. Plain old bsg nodes are on SCSI (or legacy IDE) request queues, so this should be initialized, and your patch creates a memory leak by overwriting the sense pointer.

bsg-lib nodes aren't on scsi request queues, but they don't use the code path your patch to start with.

What exactly is the reproducer for this problem?

#4 lore raw
Quoted 6 lines · Christoph Hellwig
Quoted 4 lines · Hannes Reinecke

The 'response' buffer from bsg is mapped onto the SCSI sense buffer, however after commit 82ed4db499b8 we need to allocate them ourselves as the bsg queue is _not_ a SCSI queue, and hence the sense buffer won't be allocated from the scsi stack.

Quoted 9 lines

I don't think this is the full story. Plain old bsg nodes are on SCSI (or legacy IDE) request queues, so this should be initialized, and your patch creates a memory leak by overwriting the sense pointer.

bsg-lib nodes aren't on scsi request queues, but they don't use the code path your patch to start with.

What exactly is the reproducer for this problem?

The problem is here:

static int bsg_scsi_complete_rq(struct request *rq, struct sg_io_v4 *hdr)

{
	struct scsi_request *sreq = scsi_req(rq);
	int ret = 0;

	/*
	 * fill in all the output members
	 */
	hdr->device_status = sreq->result & 0xff;
	hdr->transport_status = host_byte(sreq->result);
	hdr->driver_status = driver_byte(sreq->result);
	hdr->info = 0;
	if (hdr->device_status || hdr->transport_status || hdr->driver_status)
		hdr->info |= SG_INFO_CHECK;
	hdr->response_len = 0;
->	if (sreq->sense_len && hdr->response) {
		int len = min_t(unsigned int, hdr->max_response_len,
					sreq->sense_len);

		if (copy_to_user(uptr64(hdr->response), sreq->sense, len))
			ret = -EFAULT;
		else
			hdr->response_len = len;
	}

This expects the 'response' to be allocated.
Yet nowhere in the block/bsg.c we actually _do_ allocate the 'response' field.
And as the header is pretty much copied from userspace we don't really have any control about the contents of the 'response' nor the 'response_len' parameter.

These fields used to be filled by mpt3sas (to hold the sense code), but with commit 651a01364994 ("scsi: scsi_transport_sas: switch to bsg-lib for SMP passthrough") the sense code handling got removed.

Alternatively we might kill 'response' handling altogether, but then that might have an impact on userland.

Cheers,

Hannes

Signature · 5 lines
Dr. Hannes Reinecke		   Teamlead Storage & Networking
[email protected]			               +49 911 74053 688
SUSE LINUX GmbH, Maxfeldstr. 5, 90409 Nürnberg
GF: F. Imendörffer, J. Smithard, J. Guild, D. Upmanyu, G. Norton
HRB 21284 (AG Nürnberg)
#5 lore raw
Quoted 1 line · Hannes Reinecke

static int bsg_scsi_complete_rq(struct request *rq, struct sg_io_v4 *hdr)

So this is bsg_scsi_ops that you quote.

Quoted 10 lines

This expects the 'response' to be allocated.
Yet nowhere in the block/bsg.c we actually _do_ allocate the 'response' field.
And as the header is pretty much copied from userspace we don't really have any control about the contents of the 'response' nor the 'response_len' parameter.

These fields used to be filled by mpt3sas (to hold the sense code), but with commit 651a01364994 ("scsi: scsi_transport_sas: switch to bsg-lib for SMP passthrough") the sense code handling got removed.

But all the transport bsg users actually use bsg_transport_ops and thus should never end up in the above code.

Something in this bug report does not add up.