Avoid that scsi_exit_rq() triggers a use-after-free
Subsystems (2)
PATCH v2 1/1 Avoid that scsi_exit_rq() triggers a use-after-free
Dereferencing shost from scsi_exit_rq() is not safe because the SCSI host may already have been freed when scsi_exit_rq() is called. Increasing the shost reference count in scsi_init_rq() and dropping that reference in scsi_exit_rq() is nontrivial since scsi_host_dev_release() may sleep and since scsi_exit_rq() may be called from interrupt context. Since scsi_exit_rq() only needs a single bit from shost, copy that bit into struct scsi_cmnd.
--- drivers/scsi/scsi_lib.c | 39 +++++++++++++++++++++------------------ include/scsi/scsi_cmnd.h | 1 + 2 files changed, 22 insertions(+), 18 deletions(-)
drivers/scsi/scsi_lib.c +21 -18
@@ -43,23 +43,23 @@ static struct kmem_cache *scsi_sense_isadma_cache;static DEFINE_MUTEX (scsi_sense_cache_mutex);static inline struct kmem_cache * -scsi_select_sense_cache (struct Scsi_Host *shost) +scsi_select_sense_cache (bool unchecked_isa_dma) { -return shost->unchecked_isa_dma ? - scsi_sense_isadma_cache : scsi_sense_cache; +return unchecked_isa_dma ? scsi_sense_isadma_cache : scsi_sense_cache; } -static void scsi_free_sense_buffer (struct Scsi_Host *shost, -unsigned char *sense_buffer) +static void scsi_free_sense_buffer (bool unchecked_isa_dma, +unsigned char *sense_buffer) { -kmem_cache_free (scsi_select_sense_cache (shost), sense_buffer); +kmem_cache_free (scsi_select_sense_cache (unchecked_isa_dma), + sense_buffer); } -static unsigned char *scsi_alloc_sense_buffer (struct Scsi_Host *shost, +static unsigned char *scsi_alloc_sense_buffer (bool unchecked_isa_dma,gfp_t gfp_mask,int numa_node) { -return kmem_cache_alloc_node (scsi_select_sense_cache (shost), gfp_mask, - numa_node); +return kmem_cache_alloc_node (scsi_select_sense_cache (unchecked_isa_dma), + gfp_mask, numa_node); }int scsi_init_sense_cache (struct Scsi_Host *shost)
@@ -67,7 +67,7 @@ int scsi_init_sense_cache(struct Scsi_Host *shost)struct kmem_cache *cache;int ret =0 ; - cache =scsi_select_sense_cache (shost); + cache =scsi_select_sense_cache (shost->unchecked_isa_dma);if (cache)return 0 ;
@@ -2004,10 +2004,12 @@ static int scsi_init_request(void *data, struct request *rq, unsigned int numa_node) {struct Scsi_Host *shost = data; +const bool unchecked_isa_dma = shost->unchecked_isa_dma;struct scsi_cmnd *cmd =blk_mq_rq_to_pdu (rq); - cmd->sense_buffer = -scsi_alloc_sense_buffer (shost,GFP_KERNEL , numa_node); + cmd->unchecked_isa_dma = unchecked_isa_dma; + cmd->sense_buffer =scsi_alloc_sense_buffer (unchecked_isa_dma, +GFP_KERNEL , numa_node);if (!cmd->sense_buffer)return -ENOMEM ; cmd->req.sense = cmd->sense_buffer;
@@ -2017,10 +2019,9 @@ static int scsi_init_request(void *data, struct request *rq,static void scsi_exit_request (void *data,struct request *rq,unsigned int hctx_idx,unsigned int request_idx) { -struct Scsi_Host *shost = data;struct scsi_cmnd *cmd =blk_mq_rq_to_pdu (rq); -scsi_free_sense_buffer (shost, cmd->sense_buffer); +scsi_free_sense_buffer (cmd->unchecked_isa_dma, cmd->sense_buffer); }static int scsi_map_queues (struct blk_mq_tag_set *set)
@@ -2093,11 +2094,14 @@ EXPORT_SYMBOL_GPL(__scsi_init_queue);static int scsi_init_rq (struct request_queue *q,struct request *rq,gfp_t gfp) {struct Scsi_Host *shost = q->rq_alloc_data; +const bool unchecked_isa_dma = shost->unchecked_isa_dma;struct scsi_cmnd *cmd =blk_mq_rq_to_pdu (rq);memset (cmd,0 ,sizeof (*cmd)); - cmd->sense_buffer =scsi_alloc_sense_buffer (shost, gfp,NUMA_NO_NODE ); + cmd->unchecked_isa_dma = unchecked_isa_dma; + cmd->sense_buffer =scsi_alloc_sense_buffer (unchecked_isa_dma, gfp, +NUMA_NO_NODE );if (!cmd->sense_buffer) goto fail; cmd->req.sense = cmd->sense_buffer;
@@ -2111,19 +2115,18 @@ static int scsi_init_rq(struct request_queue *q, struct request *rq, gfp_t gfp)return 0 ; fail_free_sense: -scsi_free_sense_buffer (shost, cmd->sense_buffer); +scsi_free_sense_buffer (unchecked_isa_dma, cmd->sense_buffer); fail:return -ENOMEM ; }static void scsi_exit_rq (struct request_queue *q,struct request *rq) { -struct Scsi_Host *shost = q->rq_alloc_data;struct scsi_cmnd *cmd =blk_mq_rq_to_pdu (rq);if (cmd->prot_sdb)kmem_cache_free (scsi_sdb_cache, cmd->prot_sdb); -scsi_free_sense_buffer (shost, cmd->sense_buffer); +scsi_free_sense_buffer (cmd->unchecked_isa_dma, cmd->sense_buffer); }struct request_queue *scsi_alloc_queue (struct scsi_device *sdev)
include/scsi/scsi_cmnd.h +1 -0
@@ -139,6 +139,7 @@ struct scsi_cmnd {int result;/* Status code from lower level driver */ int flags;/* Command flags */ +bool unchecked_isa_dma;unsigned char tag;/* SCSI-II queued command tag */ };
Signature · 1 line
2.12.2
Please just add a flag to ->flags instead of adding a whole new field.
Otherwise this looks good to me.
Quoted 3 lines · Christoph Hellwig
Please just add a flag to ->flags instead of adding a whole new field.
Otherwise this looks good to me.
Hello Christoph,
Thanks for the feedback. I will make the proposed change and post a second version.
Bart.
Quoted 11 lines · Bart Van Assche
Quoted 3 lines · Christoph Hellwig
Please just add a flag to ->flags instead of adding a whole new field.
Otherwise this looks good to me.
Hello Christoph,
Thanks for the feedback. I will make the proposed change and post a second version.
Bart.
BTW, what branch was your last patch based off? I had some hunk errors while applying it.
Quoted 6 lines · Scott Bauer
Quoted 5 lines · Bart Van Assche
Quoted 3 lines · Christoph Hellwig
Please just add a flag to ->flags instead of adding a whole new field.
Otherwise this looks good to me.
Quoted 7 lines
Quoted 4 lines
Hello Christoph,
Thanks for the feedback. I will make the proposed change and post a second version.
BTW, what branch was your last patch based off? I had some hunk errors while applying it.
Hello Scott,
That patch was based off kernel v4.11. Did you perhaps use a different kernel version for your tests?
Bart.
Quoted 15 lines · Bart Van Assche
Quoted 6 lines · Scott Bauer
Quoted 5 lines · Bart Van Assche
Quoted 3 lines · Christoph Hellwig
Please just add a flag to ->flags instead of adding a whole new field.
Otherwise this looks good to me.
Quoted 7 lines
Quoted 4 lines
Hello Christoph,
Thanks for the feedback. I will make the proposed change and post a second version.
BTW, what branch was your last patch based off? I had some hunk errors while applying it.
Quoted 6 lines
Hello Scott,
That patch was based off kernel v4.11. Did you perhaps use a different kernel version for your tests?
Bart.
I've been working on top of Jens' for-linus
https://git.kernel.org/pub/scm/linux/kernel/git/axboe/linux-block.git/log/?h=for-linus