2019-08-18 11:13:39

by Pan Bian

[permalink] [raw]
Subject: [PATCH] block: fix a mismatched alloc free in bio_alloc_bioset

The function kmalloc is called to allocate memory if bs is NULL.
However, mempool_free is used to release the memory chunk even if bs is
NULL in the error hanlding code. This patch checks bs and use the
correct function to release memory.

Signed-off-by: Pan Bian <[email protected]>
---
block/bio.c | 5 ++++-
1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/block/bio.c b/block/bio.c
index 299a0e7..c5f5238 100644
--- a/block/bio.c
+++ b/block/bio.c
@@ -515,7 +515,10 @@ struct bio *bio_alloc_bioset(gfp_t gfp_mask, unsigned int nr_iovecs,
return bio;

err_free:
- mempool_free(p, &bs->bio_pool);
+ if (!bs)
+ kfree(p);
+ else
+ mempool_free(p, &bs->bio_pool);
return NULL;
}
EXPORT_SYMBOL(bio_alloc_bioset);
--
2.7.4


2019-08-18 16:27:53

by Bart Van Assche

[permalink] [raw]
Subject: Re: [PATCH] block: fix a mismatched alloc free in bio_alloc_bioset

On 8/18/19 3:57 AM, Pan Bian wrote:
> The function kmalloc is called to allocate memory if bs is NULL.
> However, mempool_free is used to release the memory chunk even if bs is
> NULL in the error hanlding code. This patch checks bs and use the
> correct function to release memory.
>
> Signed-off-by: Pan Bian <[email protected]>
> ---
> block/bio.c | 5 ++++-
> 1 file changed, 4 insertions(+), 1 deletion(-)
>
> diff --git a/block/bio.c b/block/bio.c
> index 299a0e7..c5f5238 100644
> --- a/block/bio.c
> +++ b/block/bio.c
> @@ -515,7 +515,10 @@ struct bio *bio_alloc_bioset(gfp_t gfp_mask, unsigned int nr_iovecs,
> return bio;
>
> err_free:
> - mempool_free(p, &bs->bio_pool);
> + if (!bs)
> + kfree(p);
> + else
> + mempool_free(p, &bs->bio_pool);
> return NULL;
> }
> EXPORT_SYMBOL(bio_alloc_bioset);
>

Please add "Fixes:" and "Cc: stable" tags. See also
Documentation/process/submitting-patches.rst.

Thanks,

Bart.