Skip to content

Commit eab40cf

Browse files
Benjamin-Blockaxboe
authored andcommitted
bsg-lib: fix use-after-free under memory-pressure
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_rq() is changed to only do 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_initialize_rq() is a new function that makes 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. Fixes: 50b4d48 ("bsg-lib: fix kernel panic resulting from missing allocation of reply-buffer") Cc: <[email protected]> # 4.11+ Reviewed-by: Hannes Reinecke <[email protected]> Reviewed-by: Johannes Thumshirn <[email protected]> Reviewed-by: Christoph Hellwig <[email protected]> Signed-off-by: Benjamin Block <[email protected]> Signed-off-by: Jens Axboe <[email protected]>
1 parent 70e62f4 commit eab40cf

File tree

1 file changed

+21
-6
lines changed

1 file changed

+21
-6
lines changed

block/bsg-lib.c

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -207,20 +207,34 @@ static int bsg_init_rq(struct request_queue *q, struct request *req, gfp_t gfp)
207207
struct bsg_job *job = blk_mq_rq_to_pdu(req);
208208
struct scsi_request *sreq = &job->sreq;
209209

210+
/* called right after the request is allocated for the request_queue */
211+
212+
sreq->sense = kzalloc(SCSI_SENSE_BUFFERSIZE, gfp);
213+
if (!sreq->sense)
214+
return -ENOMEM;
215+
216+
return 0;
217+
}
218+
219+
static void bsg_initialize_rq(struct request *req)
220+
{
221+
struct bsg_job *job = blk_mq_rq_to_pdu(req);
222+
struct scsi_request *sreq = &job->sreq;
223+
void *sense = sreq->sense;
224+
225+
/* called right before the request is given to the request_queue user */
226+
210227
memset(job, 0, sizeof(*job));
211228

212229
scsi_req_init(sreq);
230+
231+
sreq->sense = sense;
213232
sreq->sense_len = SCSI_SENSE_BUFFERSIZE;
214-
sreq->sense = kzalloc(sreq->sense_len, gfp);
215-
if (!sreq->sense)
216-
return -ENOMEM;
217233

218234
job->req = req;
219-
job->reply = sreq->sense;
235+
job->reply = sense;
220236
job->reply_len = sreq->sense_len;
221237
job->dd_data = job + 1;
222-
223-
return 0;
224238
}
225239

226240
static void bsg_exit_rq(struct request_queue *q, struct request *req)
@@ -251,6 +265,7 @@ struct request_queue *bsg_setup_queue(struct device *dev, const char *name,
251265
q->cmd_size = sizeof(struct bsg_job) + dd_job_size;
252266
q->init_rq_fn = bsg_init_rq;
253267
q->exit_rq_fn = bsg_exit_rq;
268+
q->initialize_rq_fn = bsg_initialize_rq;
254269
q->request_fn = bsg_request_fn;
255270

256271
ret = blk_init_allocated_queue(q);

0 commit comments

Comments
 (0)