All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH 1/1] bsg-lib: fix use-after-free under memory-pressure
@ 2017-09-21 18:17 Benjamin Block
  2017-09-25 15:53 ` Christoph Hellwig
  0 siblings, 1 reply; 4+ messages in thread
From: Benjamin Block @ 2017-09-21 18:17 UTC (permalink / raw)
  To: Jens Axboe; +Cc: Benjamin Block, linux-block, linux-scsi, Steffen Maier, stable

When under memory-pressure it is possible that the mempool which backs
the 'struct request_queue' will make use of up to BLKDEV_MIN_RQ count
emergency buffers - in case it can't get a regular allocation. These
buffers are preallocated and once they are also used, they are
re-supplied with old finished requests from the same request_queue (see
mempool_free()).

The bug is, when re-supplying the emergency pool, the old requests are
not again ran through the callback mempool_t->alloc(), and thus also not
through the callback bsg_init_rq(). Thus we skip initialization, and
while the sense-buffer still should be good, scsi_request->cmd might
have become to be an invalid pointer in the meantime. When the request
is initialized in bsg.c, and the user's CDB is larger than BLK_MAX_CDB,
bsg will replace it with a custom allocated buffer, which is freed when
the user's command is finished, thus it dangles afterwards. When next a
command is sent by the user that has a smaller/similar CDB as
BLK_MAX_CDB, bsg will assume that scsi_request->cmd is backed by
scsi_request->__cmd, will not make a custom allocation, and write into
undefined memory.

Fix this by splitting bsg_init_rq() into two functions:
 - bsg_init_job() directly replace bsg_init_rq() and only does the
   allocation of the sense-buffer, which is used to back the bsg job's
   reply buffer. This pointer should never change during the lifetime of
   a scsi_request, so it doesn't need re-initialization.
 - bsg_init_rq() is a new function that make use of
   'struct request_queue's initialize_rq_fn callback (which was
   introduced in v4.12). This is always called before the request is
   given out via blk_get_request(). This function does the remaining
   initialization that was previously done in bsg_init_rq(), and will
   also do it when the request is taken from the emergency-pool of the
   backing mempool.

Also rename bsg_exit_rq() into bsg_exit_job(), to make it fit the
name-scheme.

Fixes: 50b4d485528d ("bsg-lib: fix kernel panic resulting from missing allocation of reply-buffer")
Cc: <stable@vger.kernel.org> # 4.11+
Signed-off-by: Benjamin Block <bblock@linux.vnet.ibm.com>
---

Notes:
    I did test this on zFCP with FC CT commands send via the ioctl() and
    write() system-call. That did work fine. But I would very much
    appreciate if anyone could run this against an other HBA or even an
    other implementer of bsg-lib, such as now SAS, because I have no access
    to such hardware here.
    
    This should make no difference to the normal cases - where each request
    is allocated via slab - with- or without this patch; if I didn't miss
    anything. Only the order is a bit mixed up - the memset is done after
    the sense-allocation, so I have to buffer the sense-pointer for that.
    But otherwise there is no difference I am aware of, so it should behave
    the same (does for me).
    
    I could not reproduce the memory-pressure case here in the lab.. I
    don't see any reason why it should work now, but I am open to
    suggestions :)
    
                                                Beste Grüße / Best regards,
                                                  - Benjamin Block

 block/bsg-lib.c | 41 ++++++++++++++++++++++++++++-------------
 1 file changed, 28 insertions(+), 13 deletions(-)

diff --git a/block/bsg-lib.c b/block/bsg-lib.c
index c82408c7cc3c..634d1557da38 100644
--- a/block/bsg-lib.c
+++ b/block/bsg-lib.c
@@ -203,28 +203,42 @@ static void bsg_request_fn(struct request_queue *q)
 	spin_lock_irq(q->queue_lock);
 }
 
-static int bsg_init_rq(struct request_queue *q, struct request *req, gfp_t gfp)
+static int bsg_init_job(struct request_queue *q, struct request *req, gfp_t gfp)
 {
 	struct bsg_job *job = blk_mq_rq_to_pdu(req);
 	struct scsi_request *sreq = &job->sreq;
 
-	memset(job, 0, sizeof(*job));
+	/* called right after the request is allocated for the request_queue */
 
-	scsi_req_init(sreq);
-	sreq->sense_len = SCSI_SENSE_BUFFERSIZE;
-	sreq->sense = kzalloc(sreq->sense_len, gfp);
+	sreq->sense = kzalloc(SCSI_SENSE_BUFFERSIZE, gfp);
 	if (!sreq->sense)
 		return -ENOMEM;
 
-	job->req = req;
-	job->reply = sreq->sense;
-	job->reply_len = sreq->sense_len;
-	job->dd_data = job + 1;
-
 	return 0;
 }
 
-static void bsg_exit_rq(struct request_queue *q, struct request *req)
+static void bsg_init_rq(struct request *req)
+{
+	struct bsg_job *job = blk_mq_rq_to_pdu(req);
+	struct scsi_request *sreq = &job->sreq;
+	void *sense = sreq->sense;
+
+	/* called right before the request is given to the request_queue user */
+
+	memset(job, 0, sizeof(*job));
+
+	scsi_req_init(sreq);
+
+	sreq->sense = sense;
+	sreq->sense_len = SCSI_SENSE_BUFFERSIZE;
+
+	job->req = req;
+	job->reply = sense;
+	job->reply_len = sreq->sense_len;
+	job->dd_data = job + 1;
+}
+
+static void bsg_exit_job(struct request_queue *q, struct request *req)
 {
 	struct bsg_job *job = blk_mq_rq_to_pdu(req);
 	struct scsi_request *sreq = &job->sreq;
@@ -250,8 +264,9 @@ struct request_queue *bsg_setup_queue(struct device *dev, const char *name,
 	if (!q)
 		return ERR_PTR(-ENOMEM);
 	q->cmd_size = sizeof(struct bsg_job) + dd_job_size;
-	q->init_rq_fn = bsg_init_rq;
-	q->exit_rq_fn = bsg_exit_rq;
+	q->init_rq_fn = bsg_init_job;
+	q->exit_rq_fn = bsg_exit_job;
+	q->initialize_rq_fn = bsg_init_rq;
 	q->request_fn = bsg_request_fn;
 
 	ret = blk_init_allocated_queue(q);
-- 
2.14.1

^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH 1/1] bsg-lib: fix use-after-free under memory-pressure
  2017-09-21 18:17 [PATCH 1/1] bsg-lib: fix use-after-free under memory-pressure Benjamin Block
@ 2017-09-25 15:53 ` Christoph Hellwig
  2017-09-25 16:54     ` Benjamin Block
  0 siblings, 1 reply; 4+ messages in thread
From: Christoph Hellwig @ 2017-09-25 15:53 UTC (permalink / raw)
  To: Benjamin Block; +Cc: Jens Axboe, linux-block, linux-scsi, Steffen Maier, stable

>  	if (!q)
>  		return ERR_PTR(-ENOMEM);
>  	q->cmd_size = sizeof(struct bsg_job) + dd_job_size;
> -	q->init_rq_fn = bsg_init_rq;
> -	q->exit_rq_fn = bsg_exit_rq;
> +	q->init_rq_fn = bsg_init_job;
> +	q->exit_rq_fn = bsg_exit_job;
> +	q->initialize_rq_fn = bsg_init_rq;

Please use function names that match the method names, that is keep
the existing names and name the new helper bsg_initialize_rq;

Except for that the patch looks fine to me:

Reviewed-by: Christoph Hellwig <hch@lst.de>

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH 1/1] bsg-lib: fix use-after-free under memory-pressure
  2017-09-25 15:53 ` Christoph Hellwig
@ 2017-09-25 16:54     ` Benjamin Block
  0 siblings, 0 replies; 4+ messages in thread
From: Benjamin Block @ 2017-09-25 16:54 UTC (permalink / raw)
  To: Christoph Hellwig
  Cc: Jens Axboe, linux-block, linux-scsi, Steffen Maier, stable

On Mon, Sep 25, 2017 at 08:53:07AM -0700, Christoph Hellwig wrote:
> >  	if (!q)
> >  		return ERR_PTR(-ENOMEM);
> >  	q->cmd_size = sizeof(struct bsg_job) + dd_job_size;
> > -	q->init_rq_fn = bsg_init_rq;
> > -	q->exit_rq_fn = bsg_exit_rq;
> > +	q->init_rq_fn = bsg_init_job;
> > +	q->exit_rq_fn = bsg_exit_job;
> > +	q->initialize_rq_fn = bsg_init_rq;
> 
> Please use function names that match the method names, that is keep
> the existing names and name the new helper bsg_initialize_rq;
>

OK, I can change that.


                                                    Beste Gr��e / Best regards,
                                                      - Benjamin Block

> 
> Except for that the patch looks fine to me:
> 
> Reviewed-by: Christoph Hellwig <hch@lst.de>
> 

-- 
Linux on z Systems Development         /         IBM Systems & Technology Group
		  IBM Deutschland Research & Development GmbH 
Vorsitz. AufsR.: Martina Koederitz     /        Gesch�ftsf�hrung: Dirk Wittkopp
Sitz der Gesellschaft: B�blingen / Registergericht: AmtsG Stuttgart, HRB 243294

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH 1/1] bsg-lib: fix use-after-free under memory-pressure
@ 2017-09-25 16:54     ` Benjamin Block
  0 siblings, 0 replies; 4+ messages in thread
From: Benjamin Block @ 2017-09-25 16:54 UTC (permalink / raw)
  To: Christoph Hellwig
  Cc: Jens Axboe, linux-block, linux-scsi, Steffen Maier, stable

On Mon, Sep 25, 2017 at 08:53:07AM -0700, Christoph Hellwig wrote:
> >  	if (!q)
> >  		return ERR_PTR(-ENOMEM);
> >  	q->cmd_size = sizeof(struct bsg_job) + dd_job_size;
> > -	q->init_rq_fn = bsg_init_rq;
> > -	q->exit_rq_fn = bsg_exit_rq;
> > +	q->init_rq_fn = bsg_init_job;
> > +	q->exit_rq_fn = bsg_exit_job;
> > +	q->initialize_rq_fn = bsg_init_rq;
> 
> Please use function names that match the method names, that is keep
> the existing names and name the new helper bsg_initialize_rq;
>

OK, I can change that.


                                                    Beste Grüße / Best regards,
                                                      - Benjamin Block

> 
> Except for that the patch looks fine to me:
> 
> Reviewed-by: Christoph Hellwig <hch@lst.de>
> 

-- 
Linux on z Systems Development         /         IBM Systems & Technology Group
		  IBM Deutschland Research & Development GmbH 
Vorsitz. AufsR.: Martina Koederitz     /        Geschäftsführung: Dirk Wittkopp
Sitz der Gesellschaft: Böblingen / Registergericht: AmtsG Stuttgart, HRB 243294

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2017-09-25 16:54 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-09-21 18:17 [PATCH 1/1] bsg-lib: fix use-after-free under memory-pressure Benjamin Block
2017-09-25 15:53 ` Christoph Hellwig
2017-09-25 16:54   ` Benjamin Block
2017-09-25 16:54     ` Benjamin Block

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.