Re: [PATCH v2] Avoid that scsi_exit_rq() triggers a use-after-free

2017-05-04 Thread Scott Bauer
On Thu, May 04, 2017 at 03:32:44PM +, Bart Van Assche wrote:
> On Thu, 2017-05-04 at 09:15 -0600, Scott Bauer wrote:
> > On Thu, May 04, 2017 at 03:26:37PM +, Bart Van Assche wrote:
> > > On Thu, 2017-05-04 at 09:30 +0200, Christoph Hellwig wrote:
> > > > 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.
> > 
> > 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.

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



Re: [PATCH v2] Avoid that scsi_exit_rq() triggers a use-after-free

2017-05-04 Thread Bart Van Assche
On Thu, 2017-05-04 at 09:15 -0600, Scott Bauer wrote:
> On Thu, May 04, 2017 at 03:26:37PM +, Bart Van Assche wrote:
> > On Thu, 2017-05-04 at 09:30 +0200, Christoph Hellwig wrote:
> > > 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.
> 
> 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.

Re: [PATCH v2] Avoid that scsi_exit_rq() triggers a use-after-free

2017-05-04 Thread Scott Bauer
On Thu, May 04, 2017 at 03:26:37PM +, Bart Van Assche wrote:
> On Thu, 2017-05-04 at 09:30 +0200, Christoph Hellwig wrote:
> > 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.


Re: [PATCH v2] Avoid that scsi_exit_rq() triggers a use-after-free

2017-05-04 Thread Bart Van Assche
On Thu, 2017-05-04 at 09:30 +0200, Christoph Hellwig wrote:
> 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.

Re: [PATCH v2] Avoid that scsi_exit_rq() triggers a use-after-free

2017-05-04 Thread Christoph Hellwig
Please just add a flag to ->flags instead of adding a whole new field.

Otherwise this looks good to me.


[PATCH v2] Avoid that scsi_exit_rq() triggers a use-after-free

2017-05-03 Thread Bart Van Assche
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.

Reported-by: Scott Bauer 
Fixes: e9c787e65c0c ("scsi: allocate scsi_cmnd structures as part of struct 
request")
Signed-off-by: Bart Van Assche 
Cc: Scott Bauer 
Cc: Christoph Hellwig 
Cc: Jan Kara 
Cc: Hannes Reinecke 
Cc: 
---
 drivers/scsi/scsi_lib.c  | 39 +--
 include/scsi/scsi_cmnd.h |  1 +
 2 files changed, 22 insertions(+), 18 deletions(-)

diff --git a/drivers/scsi/scsi_lib.c b/drivers/scsi/scsi_lib.c
index 15c9fe766071..3a5a49e3274a 100644
--- a/drivers/scsi/scsi_lib.c
+++ b/drivers/scsi/scsi_lib.c
@@ -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: