Re: [PATCH] EDAC/device: Fix double free on sysfs registration failure
From: Wong Steve
Date: Thu Nov 19 2026 - 08:10:21 EST
Hi Borislav,
On Wed, Aug 12, 2026 at 11:16:45PM -0700, Borislav Petkov wrote:
> This is where you lost me.
I ran into the same problem independently, so I tried to reproduce it.
It's easy to hit: calling edac_device_alloc_ctl_info() twice with the
same dev_name makes kobject_init_and_add() fail with -EEXIST in the
second call. I did that from a late_initcall on next-20261109 with
KASAN:
a = edac_device_alloc_ctl_info(0, "duptest", 1, "blk", 1, 0, 0);
b = edac_device_alloc_ctl_info(0, "duptest", 1, "blk", 1, 0, 1);
Without the patch, the second call gives:
BUG: KASAN: slab-use-after-free in
edac_device_register_sysfs_main_kobj+0x14b/0x150
Read of size 8 at addr ffff8880044eec10 by task swapper/0/1
...
Freed by task 1:
kfree+0x176/0x530
kobject_put+0x101/0x1e0
edac_device_register_sysfs_main_kobj+0xd2/0x150
edac_device_alloc_ctl_info+0x3c2/0x570
and with kasan_multi_shot it goes on with three more use-after-free
reads and three double-frees in edac_device_alloc_ctl_info(), from
__edac_device_free_ctl_info() running on the dev_ctl that the kobject
release already freed.
The if (ci) check doesn't catch it because ci isn't NULL there: the
release callback kfree()s the object, but the caller still holds the
old pointer and frees through it again. I think it went unnoticed
because the path needs kobject_init_and_add() to fail, which doesn't
happen unless there is a duplicate name or an allocation failure.
With Guangshuo's patch applied (it still applies to next-20261109), the
same test only prints the expected -EEXIST message from kobject, the
second call returns NULL, and KASAN reports nothing.
The patch also turns the if (dev_root) block into an early return, which
I think is the cleanup you mentioned.
Tested-by: Suyuan Wang <stevewong@xxxxxxxxxxx>
Reviewed-by: Suyuan Wang <stevewong@xxxxxxxxxxx>
Thanks,
Suyuan