linux-block.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] block: Fix potential NULL pointer dereference in __bio_crypt_clone()
@ 2020-09-01 11:59 Miaohe Lin
  2020-09-01 16:07 ` Eric Biggers
  0 siblings, 1 reply; 4+ messages in thread
From: Miaohe Lin @ 2020-09-01 11:59 UTC (permalink / raw)
  To: axboe, ebiggers, satyat; +Cc: linux-block, linux-kernel, linmiaohe

mempool_alloc() may return NULL if __GFP_DIRECT_RECLAIM is not set in
gfp_mask under memory pressure. So we should check the return value of
mempool_alloc() against NULL before dereference.

Fixes: a892c8d52c02 ("block: Inline encryption support for blk-mq")
Signed-off-by: Miaohe Lin <linmiaohe@huawei.com>
---
 block/blk-crypto.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/block/blk-crypto.c b/block/blk-crypto.c
index 2d5e60023b08..046aff85cfa3 100644
--- a/block/blk-crypto.c
+++ b/block/blk-crypto.c
@@ -98,7 +98,8 @@ void __bio_crypt_free_ctx(struct bio *bio)
 void __bio_crypt_clone(struct bio *dst, struct bio *src, gfp_t gfp_mask)
 {
 	dst->bi_crypt_context = mempool_alloc(bio_crypt_ctx_pool, gfp_mask);
-	*dst->bi_crypt_context = *src->bi_crypt_context;
+	if (likely(dst->bi_crypt_context))
+		*dst->bi_crypt_context = *src->bi_crypt_context;
 }
 EXPORT_SYMBOL_GPL(__bio_crypt_clone);
 
-- 
2.19.1


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

* Re: [PATCH] block: Fix potential NULL pointer dereference in __bio_crypt_clone()
  2020-09-01 11:59 [PATCH] block: Fix potential NULL pointer dereference in __bio_crypt_clone() Miaohe Lin
@ 2020-09-01 16:07 ` Eric Biggers
  0 siblings, 0 replies; 4+ messages in thread
From: Eric Biggers @ 2020-09-01 16:07 UTC (permalink / raw)
  To: Miaohe Lin; +Cc: axboe, satyat, linux-block, linux-kernel

On Tue, Sep 01, 2020 at 07:59:21AM -0400, Miaohe Lin wrote:
> mempool_alloc() may return NULL if __GFP_DIRECT_RECLAIM is not set in
> gfp_mask under memory pressure. So we should check the return value of
> mempool_alloc() against NULL before dereference.
> 
> Fixes: a892c8d52c02 ("block: Inline encryption support for blk-mq")
> Signed-off-by: Miaohe Lin <linmiaohe@huawei.com>
> ---
>  block/blk-crypto.c | 3 ++-
>  1 file changed, 2 insertions(+), 1 deletion(-)
> 
> diff --git a/block/blk-crypto.c b/block/blk-crypto.c
> index 2d5e60023b08..046aff85cfa3 100644
> --- a/block/blk-crypto.c
> +++ b/block/blk-crypto.c
> @@ -98,7 +98,8 @@ void __bio_crypt_free_ctx(struct bio *bio)
>  void __bio_crypt_clone(struct bio *dst, struct bio *src, gfp_t gfp_mask)
>  {
>  	dst->bi_crypt_context = mempool_alloc(bio_crypt_ctx_pool, gfp_mask);
> -	*dst->bi_crypt_context = *src->bi_crypt_context;
> +	if (likely(dst->bi_crypt_context))
> +		*dst->bi_crypt_context = *src->bi_crypt_context;
>  }
>  EXPORT_SYMBOL_GPL(__bio_crypt_clone);

It's intended that __GFP_DIRECT_RECLAIM always be set here.
Do you have an example where it isn't set here?

Also, if this can indeed happen, then we need to make __bio_crypt_clone()
(and bio_crypt_clone()) return a bool (or an error code) to indicate whether it
succeeded or failed.  We can't just ignore the allocation failure.

- Eric

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

* Re: [PATCH] block: Fix potential NULL pointer dereference in __bio_crypt_clone()
@ 2020-09-02  1:56 linmiaohe
  2020-09-02  5:22 ` Eric Biggers
  0 siblings, 1 reply; 4+ messages in thread
From: linmiaohe @ 2020-09-02  1:56 UTC (permalink / raw)
  To: Eric Biggers
  Cc: axboe@kernel.dk, satyat@google.com, linux-block@vger.kernel.org,
	linux-kernel@vger.kernel.org

Eric Biggers <ebiggers@kernel.org> wrote:
>On Tue, Sep 01, 2020 at 07:59:21AM -0400, Miaohe Lin wrote:
>> mempool_alloc() may return NULL if __GFP_DIRECT_RECLAIM is not set in 
>> gfp_mask under memory pressure. So we should check the return value of
>> mempool_alloc() against NULL before dereference.
>> 
>> Fixes: a892c8d52c02 ("block: Inline encryption support for blk-mq")
>> Signed-off-by: Miaohe Lin <linmiaohe@huawei.com>
>
>It's intended that __GFP_DIRECT_RECLAIM always be set here.
>Do you have an example where it isn't set here?

map_request() only pass GFP_ATOMIC to gfp_mask, though bio crypt is not used yet.

>Also, if this can indeed happen, then we need to make __bio_crypt_clone() (and bio_crypt_clone()) return a bool (or an error code) to indicate whether it succeeded or failed.  We can't just ignore the allocation failure.
>
>- Eric

IMO, just the allocation failure is ok or we would break KABI.
Many thanks.


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

* Re: [PATCH] block: Fix potential NULL pointer dereference in __bio_crypt_clone()
  2020-09-02  1:56 linmiaohe
@ 2020-09-02  5:22 ` Eric Biggers
  0 siblings, 0 replies; 4+ messages in thread
From: Eric Biggers @ 2020-09-02  5:22 UTC (permalink / raw)
  To: linmiaohe
  Cc: axboe@kernel.dk, satyat@google.com, linux-block@vger.kernel.org,
	linux-kernel@vger.kernel.org

On Wed, Sep 02, 2020 at 01:56:53AM +0000, linmiaohe wrote:
> Eric Biggers <ebiggers@kernel.org> wrote:
> >On Tue, Sep 01, 2020 at 07:59:21AM -0400, Miaohe Lin wrote:
> >> mempool_alloc() may return NULL if __GFP_DIRECT_RECLAIM is not set in 
> >> gfp_mask under memory pressure. So we should check the return value of
> >> mempool_alloc() against NULL before dereference.
> >> 
> >> Fixes: a892c8d52c02 ("block: Inline encryption support for blk-mq")
> >> Signed-off-by: Miaohe Lin <linmiaohe@huawei.com>
> >
> >It's intended that __GFP_DIRECT_RECLAIM always be set here.
> >Do you have an example where it isn't set here?
> 
> map_request() only pass GFP_ATOMIC to gfp_mask, though bio crypt is not used yet.
> 
> >Also, if this can indeed happen, then we need to make __bio_crypt_clone() (and bio_crypt_clone()) return a bool (or an error code) to indicate whether it succeeded or failed.  We can't just ignore the allocation failure.
> >
> >- Eric
> 
> IMO, just the allocation failure is ok or we would break KABI.
> Many thanks.
> 

Ignoring the allocation failure isn't okay, since it would cause encrypted I/O
to fall back to unencrypted I/O, which would cause data corruption.

Also, upstream doesn't have a stable KABI.

I sent out a patch with what I have in mind; can you take a look?
https://lkml.kernel.org/r/20200902051511.79821-1-ebiggers@kernel.org

- Eric

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

end of thread, other threads:[~2020-09-02  5:22 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2020-09-01 11:59 [PATCH] block: Fix potential NULL pointer dereference in __bio_crypt_clone() Miaohe Lin
2020-09-01 16:07 ` Eric Biggers
  -- strict thread matches above, loose matches on Subject: below --
2020-09-02  1:56 linmiaohe
2020-09-02  5:22 ` Eric Biggers

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).