* Re: [PATCH] PCI/sysfs: NULL res_attr slot after kfree as defence against double-remove
2026-05-20 21:13 [PATCH] PCI/sysfs: NULL res_attr slot after kfree as defence against double-remove Gianfranco Dutka
@ 2026-05-20 21:45 ` sashiko-bot
2026-05-20 22:45 ` Gianfranco Dutka
` (2 subsequent siblings)
3 siblings, 0 replies; 8+ messages in thread
From: sashiko-bot @ 2026-05-20 21:45 UTC (permalink / raw)
To: Gianfranco Dutka; +Cc: linux-pci
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
Pre-existing issues:
- [Critical] Invalid kfree() causing slab corruption in arch/alpha/kernel/pci-sysfs.c
- [High] Concurrent execution of pci_remove_resource_files() allows a data race resulting in a Double-Free/UAF
--
commit e4e0ea2861eb8f50c9e5303035a956c3614b2023
Author: Gianfranco Dutka <gianfranco.dutka@arista.com>
PCI/sysfs: NULL res_attr slot after kfree as defence against double-remove
This patch clears the pdev->res_attr[i] and pdev->res_attr_wc[i] slots after
calling kfree() in pci_remove_resource_files(). This is intended as
defense-in-depth against double-remove scenarios, such as out-of-tree hotplug
drivers re-entering the teardown path.
> diff --git a/drivers/pci/pci-sysfs.c b/drivers/pci/pci-sysfs.c
> index d37860841260c..11961bb31301e 100644
> --- a/drivers/pci/pci-sysfs.c
> +++ b/drivers/pci/pci-sysfs.c
> @@ -1195,12 +1195,14 @@ static void pci_remove_resource_files(struct pci_dev *pdev)
> if (res_attr) {
> sysfs_remove_bin_file(&pdev->dev.kobj, res_attr);
> kfree(res_attr);
[Severity: High]
This isn't a bug introduced by this patch, but does NULLing the pointer
prevent a double free if this function is executed concurrently?
If a user writes to resourceN_resize via sysfs, __resource_resize_store()
executes and acquires device_lock(). If no driver is bound
(dev->driver == NULL), it proceeds and calls pci_remove_resource_files().
Concurrently, a hotplug remove event triggers pci_stop_dev(), which calls
device_release_driver(). With no driver, this briefly acquires and releases
device_lock(), then proceeds without the lock to pci_remove_sysfs_dev_files(),
which also calls pci_remove_resource_files().
Both threads can enter pci_remove_resource_files() concurrently, read the
same non-NULL res_attr pointer before either sets it to NULL, and call
kfree(res_attr), resulting in a data race and double free.
> + pdev->res_attr[i] = NULL;
> }
>
> res_attr = pdev->res_attr_wc[i];
> if (res_attr) {
> sysfs_remove_bin_file(&pdev->dev.kobj, res_attr);
> kfree(res_attr);
[Severity: Critical]
This isn't a bug introduced by this patch, but could this kfree() corrupt
the slab allocator on the alpha architecture?
In arch/alpha/kernel/pci-sysfs.c:pci_create_attr(), when creating both
sparse and dense files, a single memory block is allocated for two
bin_attribute structures:
attr = kzalloc(sizeof(*attr) * res_count, GFP_ATOMIC);
...
pdev->res_attr[num] = attr;
...
attr++;
pdev->res_attr_wc[num] = attr;
When pci_remove_resource_files() executes, it will first kfree() the base
pointer at pdev->res_attr[i]. Then, it calls kfree() on pdev->res_attr_wc[i],
which points to the middle of the same slab allocation. Passing a mid-slab
pointer to kfree(), as well as freeing the same block twice, will cause
memory corruption.
> + pdev->res_attr_wc[i] = NULL;
> }
> }
> }
--
Sashiko AI review · https://sashiko.dev/#/patchset/20260520211306.3301893-1-gianfranco.dutka@arista.com?part=1
^ permalink raw reply [flat|nested] 8+ messages in thread* Re: [PATCH] PCI/sysfs: NULL res_attr slot after kfree as defence against double-remove
2026-05-20 21:13 [PATCH] PCI/sysfs: NULL res_attr slot after kfree as defence against double-remove Gianfranco Dutka
2026-05-20 21:45 ` sashiko-bot
@ 2026-05-20 22:45 ` Gianfranco Dutka
2026-05-21 11:54 ` Krzysztof Wilczyński
2026-05-21 10:22 ` Ilpo Järvinen
2026-05-21 11:59 ` Krzysztof Wilczyński
3 siblings, 1 reply; 8+ messages in thread
From: Gianfranco Dutka @ 2026-05-20 22:45 UTC (permalink / raw)
To: Bjorn Helgaas; +Cc: Gianfranco Dutka, linux-pci, linux-kernel
Thanks for the review.
Both findings concern code paths I have not independently
debugged or reproduced, so I do not want to confirm or refute
them as bugs from the sender side of this patch -- that is for
the PCI maintainers and, in the alpha case, the alpha
maintainers to judge.
What I can say about this patch: it is intentionally
defence-in-depth, not a fix for the concurrency window or for
the alpha allocation pattern. The read-then-NULL it adds is
not atomic, so if pci_remove_resource_files() is genuinely
reachable from two threads concurrently then this patch does
not close that window. And the patch makes no change to how
the two pointers are freed, so if those pointers do alias the
same allocation on alpha then this patch does not change the
underlying free behaviour either.
For this patch itself I am happy with any of:
- merge as-is for defence-in-depth, and let the issues raised
in review be addressed (or not) in separate work,
- hold it pending a proper fix for whichever of those issues
you consider blocking,
- NACK if you would rather not carry a NULL-after-free that
does not address the underlying concerns.
Whichever you prefer.
Thanks,
Gianfranco
^ permalink raw reply [flat|nested] 8+ messages in thread* Re: [PATCH] PCI/sysfs: NULL res_attr slot after kfree as defence against double-remove
2026-05-20 21:13 [PATCH] PCI/sysfs: NULL res_attr slot after kfree as defence against double-remove Gianfranco Dutka
2026-05-20 21:45 ` sashiko-bot
2026-05-20 22:45 ` Gianfranco Dutka
@ 2026-05-21 10:22 ` Ilpo Järvinen
2026-05-21 12:01 ` Krzysztof Wilczyński
2026-05-21 11:59 ` Krzysztof Wilczyński
3 siblings, 1 reply; 8+ messages in thread
From: Ilpo Järvinen @ 2026-05-21 10:22 UTC (permalink / raw)
To: Gianfranco Dutka; +Cc: Bjorn Helgaas, linux-pci, LKML
On Wed, 20 May 2026, Gianfranco Dutka wrote:
> pci_remove_resource_files() frees the bin_attribute pointed at by
> pdev->res_attr[i] / pdev->res_attr_wc[i] but does not clear the slot
> after kfree(). If pci_remove_sysfs_dev_files() is ever invoked twice
> against the same pdev, the second pass dereferences a freed
> bin_attribute and faults inside strlen() called from
> kernfs_remove_by_name_ns().
>
> To the best of my knowledge no in-tree caller hits this today -- the
> primary path (pci_stop_dev() -> pci_remove_sysfs_dev_files()) is
> expected to fire once per device -- so this is being submitted as
> defence-in-depth rather than as a fix for a reproducer in the upstream
> tree. The motivation is that a freed-but-not-cleared pointer in a
> device-lifetime table is a sharp edge: any future caller (in-tree or
> out-of-tree) that re-enters the teardown path turns it into a UAF
> with no warning. Other allocator-managed pointer tables in the
> kernel NULL the slot after kfree() for exactly this reason, and the
> two-line change makes pci_remove_resource_files() idempotent at
> essentially zero cost.
>
> For full disclosure: the way I encountered this was via an
> out-of-tree PCIe hotplug driver on an AMD Ryzen Embedded V3000
> platform. The driver re-enters pci_stop_and_remove_bus_device()
> against a pdev that the standard pci_destroy_dev() path has already
> torn down, and with slub_debug=FZPU the second entry faults with the
> classic POISON_FREE signature:
>
> BUG: unable to handle page fault for address: 6b6b6b6b6b6b6b6b
> RIP: strlen+0x4
> Call Trace:
> kernfs_find_ns
> kernfs_remove_by_name_ns
> sysfs_remove_bin_file
> pci_remove_resource_files
> pci_remove_sysfs_dev_files
> pci_stop_bus_device
> pci_stop_and_remove_bus_device
> <out-of-tree hotplug driver remove path>
>
> I am aware that "out-of-tree driver re-enters teardown" is not by
> itself a reason to take a change upstream, and I would understand a
> NACK on that basis. The reason I am still sending it is that the
> fix is local, mechanical, matches an idiom already used elsewhere in
> the kernel, and removes a class of bug rather than papering over the
> specific caller that surfaced it.
Hi,
To me it looks more like pci_remove_resource_files() is expected to be
entered only once.
If this change is accepted, the 2nd entry is "silently" allowed whereas
better course of action would feel to catch that in
pci_remove_resource_files() with:
if (WARN_ON_ONCE(something))
return;
That way, developers may become aware the code has some lifetime issue.
(I'm not strictly against your change but I think it would be much better
to have sane lifetimes and catch offenders.)
> Signed-off-by: Gianfranco Dutka <gianfranco.dutka@arista.com>
> ---
> drivers/pci/pci-sysfs.c | 2 ++
> 1 file changed, 2 insertions(+)
>
> diff --git a/drivers/pci/pci-sysfs.c b/drivers/pci/pci-sysfs.c
> --- a/drivers/pci/pci-sysfs.c
> +++ b/drivers/pci/pci-sysfs.c
> @@ -1228,11 +1228,13 @@ static void pci_remove_resource_files(struct pci_dev *pdev)
> if (res_attr) {
> sysfs_remove_bin_file(&pdev->dev.kobj, res_attr);
> kfree(res_attr);
> + pdev->res_attr[i] = NULL;
> }
>
> res_attr = pdev->res_attr_wc[i];
> if (res_attr) {
> sysfs_remove_bin_file(&pdev->dev.kobj, res_attr);
> kfree(res_attr);
> + pdev->res_attr_wc[i] = NULL;
> }
> }
> }
> --
> 2.43.0
>
--
i.
^ permalink raw reply [flat|nested] 8+ messages in thread* Re: [PATCH] PCI/sysfs: NULL res_attr slot after kfree as defence against double-remove
2026-05-21 10:22 ` Ilpo Järvinen
@ 2026-05-21 12:01 ` Krzysztof Wilczyński
2026-05-21 12:04 ` Ilpo Järvinen
0 siblings, 1 reply; 8+ messages in thread
From: Krzysztof Wilczyński @ 2026-05-21 12:01 UTC (permalink / raw)
To: Ilpo Järvinen; +Cc: Gianfranco Dutka, Bjorn Helgaas, linux-pci, LKML
> To me it looks more like pci_remove_resource_files() is expected to be
> entered only once.
>
> If this change is accepted, the 2nd entry is "silently" allowed whereas
> better course of action would feel to catch that in
> pci_remove_resource_files() with:
>
> if (WARN_ON_ONCE(something))
> return;
>
> That way, developers may become aware the code has some lifetime issue.
>
> (I'm not strictly against your change but I think it would be much better
> to have sane lifetimes and catch offenders.)
The upcoming sysfs changes would remove pci_remove_resource_files()
completely, so there would not be anything needed to be done here,
hopefully.
Thank you!
Krzysztof
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH] PCI/sysfs: NULL res_attr slot after kfree as defence against double-remove
2026-05-21 12:01 ` Krzysztof Wilczyński
@ 2026-05-21 12:04 ` Ilpo Järvinen
0 siblings, 0 replies; 8+ messages in thread
From: Ilpo Järvinen @ 2026-05-21 12:04 UTC (permalink / raw)
To: Krzysztof Wilczyński
Cc: Gianfranco Dutka, Bjorn Helgaas, linux-pci, LKML
[-- Attachment #1: Type: text/plain, Size: 876 bytes --]
On Thu, 21 May 2026, Krzysztof Wilczyński wrote:
> > To me it looks more like pci_remove_resource_files() is expected to be
> > entered only once.
> >
> > If this change is accepted, the 2nd entry is "silently" allowed whereas
> > better course of action would feel to catch that in
> > pci_remove_resource_files() with:
> >
> > if (WARN_ON_ONCE(something))
> > return;
> >
> > That way, developers may become aware the code has some lifetime issue.
> >
> > (I'm not strictly against your change but I think it would be much better
> > to have sane lifetimes and catch offenders.)
>
> The upcoming sysfs changes would remove pci_remove_resource_files()
> completely, so there would not be anything needed to be done here,
> hopefully.
Yeah, I should have looked at that branch first instead of checking just
code in mainline.
--
i.
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH] PCI/sysfs: NULL res_attr slot after kfree as defence against double-remove
2026-05-20 21:13 [PATCH] PCI/sysfs: NULL res_attr slot after kfree as defence against double-remove Gianfranco Dutka
` (2 preceding siblings ...)
2026-05-21 10:22 ` Ilpo Järvinen
@ 2026-05-21 11:59 ` Krzysztof Wilczyński
3 siblings, 0 replies; 8+ messages in thread
From: Krzysztof Wilczyński @ 2026-05-21 11:59 UTC (permalink / raw)
To: Gianfranco Dutka; +Cc: Bjorn Helgaas, linux-pci, linux-kernel
Hello,
> diff --git a/drivers/pci/pci-sysfs.c b/drivers/pci/pci-sysfs.c
> --- a/drivers/pci/pci-sysfs.c
> +++ b/drivers/pci/pci-sysfs.c
> @@ -1228,11 +1228,13 @@ static void pci_remove_resource_files(struct pci_dev *pdev)
> if (res_attr) {
> sysfs_remove_bin_file(&pdev->dev.kobj, res_attr);
> kfree(res_attr);
> + pdev->res_attr[i] = NULL;
> }
>
> res_attr = pdev->res_attr_wc[i];
> if (res_attr) {
> sysfs_remove_bin_file(&pdev->dev.kobj, res_attr);
> kfree(res_attr);
> + pdev->res_attr_wc[i] = NULL;
> }
> }
> }
Similar such patches (for reference):
- https://lore.kernel.org/linux-pci/20251010093023.2569-1-guojinhui.liam@bytedance.com/
- https://lore.kernel.org/linux-pci/20250629030547.1073425-1-zhangjian496@huawei.com/
- https://lore.kernel.org/linux-pci/20210507102706.7658-1-danijel.slivka@amd.com/
- https://lore.kernel.org/linux-pci/20191024172157.878735-2-s.miroshnichenko@yadro.com/
Thank you!
Krzysztof
^ permalink raw reply [flat|nested] 8+ messages in thread