Skip to contentSkip to conversation

block: fix a mismatched alloc free in bio_alloc_bioset

PATCH In review Pan Bian 2 participants 2 messages Aug 19, 2019

Subsystems (2)
lore
Share & tools
lore

Pan Bian

author
#0 lore raw

PATCH v2 1/1 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.

Fixes 3f86a82aeb ("block: Consolidate bio_alloc_bioset(), bio_kmalloc()")

---
V2: add Fixes and Cc tags

---
 block/bio.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
block/bio.c +4 -1
index 299a0e7..c5f5238 100644
@@ -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);
Signature · 1 line
2.7.4
#1 lore raw
Quoted 21 lines · Pan Bian

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.

Fixes 3f86a82aeb ("block: Consolidate bio_alloc_bioset(), bio_kmalloc()")

---
V2: add Fixes and Cc tags

---
 block/bio.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
block/bio.c +0 -0
index 299a0e7..c5f5238 100644
@@ -515,7 +515,10 @@ struct bio *bio_alloc_bioset(gfp_t gfp_mask, unsigned int nr_iovecs,
 	return bio;

block/bio.c struct bio *bio_alloc_bioset(gfp_t gfp_mask, unsigned int nr_iovecs, · line 524

Quoted 10 lines
block/bio.c +4 -1
 
 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);

'err_free' is only reached in case that 'bs' isn't NULL, so this patch fixes nothing.

Thanks,
Ming