linux-scsi.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Hannes Reinecke <hare@suse.de>
To: Christoph Hellwig <hch@lst.de>,
	linux-scsi@vger.kernel.org, linux-block@vger.kernel.org
Cc: Johannes Thumshirn <jthumshirn@suse.de>,
	Benjamin Block <bblock@linux.vnet.ibm.com>,
	stable@vger.kernel.org
Subject: Re: [PATCH 1/9] bsg-lib: fix use-after-free under memory-pressure
Date: Wed, 4 Oct 2017 08:16:11 +0200	[thread overview]
Message-ID: <c87d75ff-9b6c-df5a-e2b3-b6e747a15679@suse.de> (raw)
In-Reply-To: <20171003104845.10417-2-hch@lst.de>

On 10/03/2017 12:48 PM, Christoph Hellwig wrote:
> From: Benjamin Block <bblock@linux.vnet.ibm.com>
> 
> 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: 50b4d485528d ("bsg-lib: fix kernel panic resulting from missing allocation of reply-buffer")
> Cc: <stable@vger.kernel.org> # 4.11+
> Reviewed-by: Christoph Hellwig <hch@lst.de>
> Signed-off-by: Benjamin Block <bblock@linux.vnet.ibm.com>
> ---
>  block/bsg-lib.c | 27 +++++++++++++++++++++------
>  1 file changed, 21 insertions(+), 6 deletions(-)
> 
Reviewed-by: Hannes Reinecke <hare@suse.com>

Cheers,

Hannes
-- 
Dr. Hannes Reinecke		   Teamlead Storage & Networking
hare@suse.de			               +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)

  reply	other threads:[~2017-10-04  6:16 UTC|newest]

Thread overview: 45+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-10-03 10:48 [RFC] bsg-lib interface cleanup Christoph Hellwig
2017-10-03 10:48 ` [PATCH 1/9] bsg-lib: fix use-after-free under memory-pressure Christoph Hellwig
2017-10-04  6:16   ` Hannes Reinecke [this message]
2017-10-04  8:54   ` Johannes Thumshirn
2017-10-03 10:48 ` [PATCH 2/9] bfa: don't reset max_segments for every bsg request Christoph Hellwig
2017-10-04  6:16   ` Hannes Reinecke
2017-10-04  8:54   ` Johannes Thumshirn
2017-10-03 10:48 ` [PATCH 3/9] libfc: don't assign resid_len in fc_lport_bsg_request Christoph Hellwig
2017-10-04  6:17   ` Hannes Reinecke
2017-10-04  8:54   ` Johannes Thumshirn
2017-10-03 10:48 ` [PATCH 4/9] qla2xxx: don't break the bsg-lib abstractions Christoph Hellwig
2017-10-04  6:20   ` Hannes Reinecke
2017-10-04  8:54   ` Johannes Thumshirn
2017-10-05 16:58   ` Madhani, Himanshu
2017-10-03 10:48 ` [PATCH 5/9] scsi_transport_sas: check reply payload length instead of bidi request Christoph Hellwig
2017-10-04  6:21   ` Hannes Reinecke
2017-10-04  8:53   ` Johannes Thumshirn
2017-10-03 10:48 ` [PATCH 6/9] bsg-lib: introduce a timeout field in struct bsg_job Christoph Hellwig
2017-10-04  6:21   ` Hannes Reinecke
2017-10-04  8:53   ` Johannes Thumshirn
2017-10-16 16:30   ` Benjamin Block
2017-10-03 10:48 ` [PATCH 7/9] bsg-lib: remove bsg_job.req Christoph Hellwig
2017-10-04  6:22   ` Hannes Reinecke
2017-10-04  8:52   ` Johannes Thumshirn
2017-10-16 16:36   ` Benjamin Block
2017-10-03 10:48 ` [PATCH 8/9] block: pass full fmode_t to blk_verify_command Christoph Hellwig
2017-10-04  6:23   ` Hannes Reinecke
2017-10-04  8:52   ` Johannes Thumshirn
2017-10-16 16:50   ` Benjamin Block
2017-10-03 10:48 ` [PATCH 9/9] bsg: split handling of SCSI CDBs vs transport requeues Christoph Hellwig
2017-10-04  6:26   ` Hannes Reinecke
2017-10-04  7:18   ` Johannes Thumshirn
2017-10-04  7:20     ` Christoph Hellwig
2017-10-04  8:52       ` Johannes Thumshirn
2017-10-04  7:18   ` Johannes Thumshirn
2017-10-19 15:59   ` Benjamin Block
2017-10-20 16:26     ` Christoph Hellwig
2017-10-20 16:47       ` Benjamin Block
2017-10-23  6:16         ` Martin K. Petersen
2017-10-23  6:29           ` Christoph Hellwig
2017-10-23  7:17             ` Martin K. Petersen
2017-10-24 17:46               ` Jens Axboe
2017-10-24 16:58   ` Benjamin Block
2017-10-04 14:35 ` [RFC] bsg-lib interface cleanup Jens Axboe
2017-10-17  3:50 ` Martin K. Petersen

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=c87d75ff-9b6c-df5a-e2b3-b6e747a15679@suse.de \
    --to=hare@suse.de \
    --cc=bblock@linux.vnet.ibm.com \
    --cc=hch@lst.de \
    --cc=jthumshirn@suse.de \
    --cc=linux-block@vger.kernel.org \
    --cc=linux-scsi@vger.kernel.org \
    --cc=stable@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).