Re: [PATCH] block/genhd: Fix memory leak in error path of __alloc_disk_node()

From: Naohiro Aota
Date: Wed Dec 04 2019 - 04:56:20 EST


Looks good,
Reviewed-by: Naohiro Aota <naohiro.aota@xxxxxxx>

I think you can also add:

Fixes: 6c71013ecb7e ("block: partition: convert percpu ref")

On Wed, Nov 27, 2019 at 02:40:57AM +0000, Keita Suzuki wrote:
'disk->part_tbl' is malloced in disk_expand_part_tbl() and should be
freed before leaving from the error handling cases. However, current code
does not free this, causing a memory leak. Add disk_replace_part_tbl()
before freeing 'disk'.

I have tested this by randomly causing failures to the target code,
and verified on kmemleak that this memory leak does occur.

unreferenced object 0xffff888006dad500 (size 64):
comm "systemd-udevd", pid 116, jiffies 4294895558 (age 121.716s)
hex dump (first 32 bytes):
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
backtrace:
[<00000000eec79bf3>] disk_expand_part_tbl+0xab/0x170
[<00000000624e7d03>] __alloc_disk_node+0xb1/0x1c0
[<00000000ca3f4185>] 0xffffffffc01b8584
[<000000006f88a6ee>] do_one_initcall+0x8b/0x2a4
[<0000000016058199>] do_init_module+0xfd/0x380
[<00000000b6fde336>] load_module+0x3fae/0x4240
[<00000000c523d013>] __do_sys_finit_module+0x11a/0x1b0
[<00000000f07bba26>] do_syscall_64+0x6d/0x1e0
[<00000000979467fd>] entry_SYSCALL_64_after_hwframe+0x44/0xa9

Signed-off-by: Keita Suzuki <keitasuzuki.park@xxxxxxxxxxxxxxxxxxxx>
---
block/genhd.c | 1 +
1 file changed, 1 insertion(+)

diff --git a/block/genhd.c b/block/genhd.c
index ff6268970ddc..8c4b63d7f507 100644
--- a/block/genhd.c
+++ b/block/genhd.c
@@ -1504,6 +1504,7 @@ struct gendisk *__alloc_disk_node(int minors, int node_id)
*/
seqcount_init(&disk->part0.nr_sects_seq);
if (hd_ref_init(&disk->part0)) {
+ disk_replace_part_tbl(disk, NULL);
hd_free_part(&disk->part0);
kfree(disk);
return NULL;
--
2.17.1