2015-02-11 16:41:53

by Tony Battersby

[permalink] [raw]
Subject: [PATCH] blk-mq: fix double-free in error path

If the allocation of bt->bs fails, then bt->map can be freed twice, once
in blk_mq_init_bitmap_tags() -> bt_alloc(), and once in
blk_mq_init_bitmap_tags() -> bt_free(). Fix by setting the pointer to
NULL after the first free.

Cc: <[email protected]>
Signed-off-by: Tony Battersby <[email protected]>
---

For inclusion in kernel 3.20.

--- linux-3.19.0/block/blk-mq-tag.c.orig 2015-02-08 21:54:22.000000000 -0500
+++ linux-3.19.0/block/blk-mq-tag.c 2015-02-10 09:42:38.000000000 -0500
@@ -509,6 +509,7 @@ static int bt_alloc(struct blk_mq_bitmap
bt->bs = kzalloc(BT_WAIT_QUEUES * sizeof(*bt->bs), GFP_KERNEL);
if (!bt->bs) {
kfree(bt->map);
+ bt->map = NULL;
return -ENOMEM;
}


2015-02-11 16:43:51

by Jens Axboe

[permalink] [raw]
Subject: Re: [PATCH] blk-mq: fix double-free in error path

On 02/11/2015 09:32 AM, Tony Battersby wrote:
> If the allocation of bt->bs fails, then bt->map can be freed twice, once
> in blk_mq_init_bitmap_tags() -> bt_alloc(), and once in
> blk_mq_init_bitmap_tags() -> bt_free(). Fix by setting the pointer to
> NULL after the first free.
>
> Cc: <[email protected]>
> Signed-off-by: Tony Battersby <[email protected]>
> ---
>
> For inclusion in kernel 3.20.
>
> --- linux-3.19.0/block/blk-mq-tag.c.orig 2015-02-08 21:54:22.000000000 -0500
> +++ linux-3.19.0/block/blk-mq-tag.c 2015-02-10 09:42:38.000000000 -0500
> @@ -509,6 +509,7 @@ static int bt_alloc(struct blk_mq_bitmap
> bt->bs = kzalloc(BT_WAIT_QUEUES * sizeof(*bt->bs), GFP_KERNEL);
> if (!bt->bs) {
> kfree(bt->map);
> + bt->map = NULL;
> return -ENOMEM;
> }

Thanks Tony, applied.


--
Jens Axboe