Skip to contentSkip to conversation

dmaengine: idxd: Fix use-after-free of idxd_wq

PATCH In review Guixin Liu 2 participants 13 messages Apr 15 – Nov 3

Subsystems (2)
lore
Share & tools
lore

Guixin Liu

author
#0 lore raw

PATCH v2 1/1 dmaengine: idxd: Fix use-after-free of idxd_wq

We found an idxd_wq use-after-free issue with kasan when remove the idxd PCI device:

BUG: KASAN: slab-use-after-free in idxd_device_drv_remove+0x1f8/0x240 [idxd]
Call Trace:
  <TASK>
  dump_stack_lvl+0x32/0x50
  print_address_description.constprop.0+0x2c/0x390
  ? idxd_device_drv_remove+0x1f8/0x240 [idxd]
  print_report+0xba/0x280
  ? kasan_addr_to_slab+0x9/0xa0
  ? idxd_device_drv_remove+0x1f8/0x240 [idxd]
  kasan_report+0xab/0xe0
  ? idxd_device_drv_remove+0x1f8/0x240 [idxd]
  idxd_device_drv_remove+0x1f8/0x240 [idxd]
  device_release_driver_internal+0x391/0x560
  bus_remove_device+0x1f5/0x3f0
  device_del+0x392/0x990
  ? __pfx_device_del+0x10/0x10
  ? kobject_cleanup+0x117/0x360
  ? idxd_unregister_devices+0x229/0x320 [idxd]
  device_unregister+0x13/0xa0
  idxd_remove+0x4f/0x1b0 [idxd]
  pci_device_remove+0xa7/0x1d0
  device_release_driver_internal+0x391/0x560
  ? pci_pme_active+0x1e/0x450
  pci_stop_bus_device+0x10a/0x150
  pci_stop_and_remove_bus_device_locked+0x16/0x30
  remove_store+0xcf/0xe0

Freed by task 15535:
  kasan_save_stack+0x1c/0x40
  kasan_set_track+0x21/0x30
  kasan_save_free_info+0x27/0x40
  ____kasan_slab_free+0x171/0x240
  slab_free_freelist_hook+0xde/0x190
  __kmem_cache_free+0x19e/0x310
  device_release+0x98/0x210
  kobject_cleanup+0x102/0x360
  idxd_unregister_devices+0xb3/0x320 [idxd]
  dxd_remove+0x3f/0x1b0 [idxd]
  pci_device_remove+0xa7/0x1d0
  device_release_driver_internal+0x391/0x560
  pci_stop_bus_device+0x10a/0x150
  pci_stop_and_remove_bus_device_locked+0x16/0x30
  remove_store+0xcf/0xe0

In the idxd_remove() flow, when execution reaches idxd_unregister_devices(), all idxd_wq instances have already been freed. Subsequently, when device_unregister(idxd_confdev(idxd)) is executed, it calls into idxd_device_drv_remove() which accesses the already-freed idxd_wq. This fix resolves the issue by calling device_release_driver() before idxd_unregister_devices().

Fixes 98da0106aac0d ("dmanegine: idxd: fix resource free ordering on driver removal")

---
v1 -> v2:
1. Call device_release_driver() in advance instead of swapping the order of device_unregister() and idxd_unregister_devices().
2. Add Co-developed-by: Shuai Xue <[email protected]>.

 drivers/dma/idxd/init.c | 27 ++++++++++++++++++++++-----
 1 file changed, 22 insertions(+), 5 deletions(-)
drivers/dma/idxd/init.c +22 -5
index f1cfc7790d95..3b0a0363ca65 100644
@@ -1293,13 +1293,30 @@ static void idxd_remove(struct pci_dev *pdev)
 {
 	struct idxd_device *idxd = pci_get_drvdata(pdev);
 
+	/*
+	 * The idxd sub-driver's remove callback (idxd_device_drv_remove())
+	 * iterates idxd->wqs[] and accesses wq objects. We must unbind the
+	 * sub-driver before idxd_unregister_devices() frees these objects,
+	 * otherwise a use-after-free occurs.
+	 *
+	 * We cannot simply reorder device_unregister(idxd_confdev) before
+	 * idxd_unregister_devices() because device_del() -> kobject_del()
+	 * recursively removes the parent's sysfs directory, which destroys
+	 * children's sysfs entries. Subsequent device_unregister() on the
+	 * children then fails with "sysfs group 'power' not found".
+	 *
+	 * Use device_release_driver() to only unbind the driver (triggering
+	 * idxd_device_drv_remove()) without touching sysfs. Then safely
+	 * unregister children before the parent.
+	 */
+	device_release_driver(idxd_confdev(idxd));
 	idxd_unregister_devices(idxd);
+
 	/*
-	 * When ->release() is called for the idxd->conf_dev, it frees all the memory related
-	 * to the idxd context. The driver still needs those bits in order to do the rest of
-	 * the cleanup. However, we do need to unbound the idxd sub-driver. So take a ref
-	 * on the device here to hold off the freeing while allowing the idxd sub-driver
-	 * to unbind.
+	 * When ->release() is called for the idxd->conf_dev, it frees all the
+	 * memory related to the idxd context. The driver still needs those bits
+	 * in order to do the rest of the cleanup. So take a ref on the device
+	 * here to hold off the freeing.
 	 */
 	get_device(idxd_confdev(idxd));
 	device_unregister(idxd_confdev(idxd));
Signature · 1 line
2.32.0.3.g01195cf9f
#1 lore raw

drivers/dma/idxd/init.c static void idxd_remove(struct pci_dev *pdev) · line 1312

Quoted 12 lines · Guixin Liu
drivers/dma/idxd/init.c +0 -0
index fb80803d5b57..c3cfd96074c9 100644
@@ -1293,13 +1293,30 @@ static void idxd_remove(struct pci_dev *pdev)

[ ... skip 14 lines ... ]

drivers/dma/idxd/init.c +6 -0
+	 *
+	 * Use device_release_driver() to only unbind the driver (triggering
+	 * idxd_device_drv_remove()) without touching sysfs. Then safely
+	 * unregister children before the parent.
+	 */
+	device_release_driver(idxd_confdev(idxd));

Below are sashiko comments verbatim, some notes:

- I do believe the race condition that it points out is real (but very narrow), but not sure that we don't have a similar one in current code (i.e. it wasn't introduced by your patch);
- The possible wq leak was definitely not introduced by this patch;
- The question about what happens with the file references after a device is removed is a separate issue;

The patch looks good, not opposed to it at all, the only one I am a bit concerned, about this patch, to be sure, is the race condition, and the two "paths" to reach it. (the others we can fix separately)

sashiko.dev <[email protected]>:

Does this introduce a race condition with sysfs?

Since the driver's sysfs nodes remain active after device_release_driver() is called, a user with sysfs privileges could write to the bind attribute to re-probe the driver before the device unregistration completes.

If idxd_device_drv_probe()->idxd_wqs_setup() runs concurrently with idxd_unregister_devices(), does it cause a use-after-free when it iterates over the idxd->wqs[] objects being freed, since suppress_bind_attrs is not set?

Also, does this code leak the workqueue?

The workqueue idxd->wq is dynamically allocated using create_workqueue() during device initialization, but destroy_workqueue(idxd->wq) does not appear to be called in idxd_remove() or the sysfs device release callback.

Quoted 1 line

idxd_unregister_devices(idxd);

sashiko.dev <[email protected]>:

If idxd_unregister_devices() drops the last reference to the child wq objects and frees them, can idxd_shutdown() trigger a use-after-free when called shortly after?

For example, if a hardware error interrupt fired during teardown, idxd_shutdown()->flush_workqueue() could execute an error handler that iterates over the freed idxd->wqs[] memory.

Additionally, if the hardware is wedged, idxd_shutdown()->idxd_device_disable()->idxd_device_clear_state() iterates over idxd->wqs[] and attempts to acquire mutex_lock(&wq->wq_lock) on the freed memory.

Is there also a missing reference count for the cdev open path?

Since idxd_unregister_devices() frees the wq structure, if user space holds an open file descriptor, cdev_device_del() won't revoke it. Can file operations like mmap or poll subsequently access the freed ctx->wq pointer? Should idxd_cdev_open() take a kobject reference on the parent wq device?

via: https://sashiko.dev/#/message/[email protected]
via: https://sashiko.dev/#/message/[email protected]

via: https://sashiko.dev/#/message/[email protected]
via: https://sashiko.dev/#/message/[email protected]

Signature · 1 line
Vinicius
author
#2 lore raw
Quoted 13 lines · Vinicius Costa Gomes

drivers/dma/idxd/init.c static void idxd_remove(struct pci_dev *pdev) · line 1312

Quoted 12 lines · Guixin Liu
drivers/dma/idxd/init.c +0 -0
index fb80803d5b57..c3cfd96074c9 100644
@@ -1293,13 +1293,30 @@ static void idxd_remove(struct pci_dev *pdev)

[ ... skip 14 lines ... ]

drivers/dma/idxd/init.c +6 -0
+	 *
+	 * Use device_release_driver() to only unbind the driver (triggering
+	 * idxd_device_drv_remove()) without touching sysfs. Then safely
+	 * unregister children before the parent.
+	 */
+	device_release_driver(idxd_confdev(idxd));

Thanks for the reply.

Quoted 17 lines

Below are sashiko comments verbatim, some notes:

- I do believe the race condition that it points out is real (but very narrow), but not sure that we don't have a similar one in current code (i.e. it wasn't introduced by your patch);
- The possible wq leak was definitely not introduced by this patch;
- The question about what happens with the file references after a

    device is removed is a separate issue;

The patch looks good, not opposed to it at all, the only one I am a bit concerned, about this patch, to be sure, is the race condition, and the two "paths" to reach it. (the others we can fix separately)

sashiko.dev <[email protected]>:

Does this introduce a race condition with sysfs?

Quoted 8 lines

Since the driver's sysfs nodes remain active after device_release_driver() is called, a user with sysfs privileges could write to the bind attribute to re-probe the driver before the device unregistration completes.

If idxd_device_drv_probe()->idxd_wqs_setup() runs concurrently with idxd_unregister_devices(), does it cause a use-after-free when it iterates over the idxd->wqs[] objects being freed, since suppress_bind_attrs is not set?

I think re-probe will call idxd_pci_probe() to allocate an new idxd device, this avoids accessing stale data.

Quoted 5 lines

Also, does this code leak the workqueue?

The workqueue idxd->wq is dynamically allocated using create_workqueue() during device initialization, but destroy_workqueue(idxd->wq) does not appear to be called in idxd_remove() or the sysfs device release callback.

Looks like it's b7cb9a034305 ("dmaengine: idxd: Fix refcount underflow on module unload") overlooked this, destory_workqueue(idxd->wq) is called in idxd_cleanup() -> idxd_cleanup_internals().

If so, I can send another patch to fix this.

Quoted 7 lines
Quoted 1 line
  	idxd_unregister_devices(idxd);

sashiko.dev <[email protected]>:

If idxd_unregister_devices() drops the last reference to the child wq objects and frees them, can idxd_shutdown() trigger a use-after-free when called shortly after?

Quoted 8 lines

For example, if a hardware error interrupt fired during teardown, idxd_shutdown()->flush_workqueue() could execute an error handler that iterates over the freed idxd->wqs[] memory.

Additionally, if the hardware is wedged, idxd_shutdown()->idxd_device_disable()->idxd_device_clear_state() iterates over idxd->wqs[] and attempts to acquire mutex_lock(&wq->wq_lock) on the freed memory.

I took a look — idxd_shutdown() is only invoked by idxd_remove() and during system shutdown/reboot. Is idxd_shutdown() ever reached from the idxd hardware error interrupt path?

Quoted 8 lines

Is there also a missing reference count for the cdev open path?

Since idxd_unregister_devices() frees the wq structure, if user space holds an open file descriptor, cdev_device_del() won't revoke it. Can file operations like mmap or poll subsequently access the freed ctx->wq pointer? Should idxd_cdev_open() take a kobject reference on the parent wq device?

To be honest, I'm not very familiar with the idxd driver. At this point, it looks like the idxd driver needs a state machine to ensure mutual exclusion across the various concurrent paths.

Best Regards,
Guixin Liu

Quoted 7 lines

via: https://sashiko.dev/#/message/[email protected]
via: https://sashiko.dev/#/message/[email protected]

via: https://sashiko.dev/#/message/[email protected]
via: https://sashiko.dev/#/message/[email protected]

#3 lore raw
Quoted 36 lines · Guixin Liu
Quoted 13 lines · Vinicius Costa Gomes

drivers/dma/idxd/init.c static void idxd_remove(struct pci_dev *pdev) · line 1312

Quoted 12 lines · Guixin Liu
drivers/dma/idxd/init.c +0 -0
index fb80803d5b57..c3cfd96074c9 100644
@@ -1293,13 +1293,30 @@ static void idxd_remove(struct pci_dev *pdev)

[ ... skip 14 lines ... ]

drivers/dma/idxd/init.c +6 -0
+	 *
+	 * Use device_release_driver() to only unbind the driver (triggering
+	 * idxd_device_drv_remove()) without touching sysfs. Then safely
+	 * unregister children before the parent.
+	 */
+	device_release_driver(idxd_confdev(idxd));

Thanks for the reply.

Quoted 21 lines

Below are sashiko comments verbatim, some notes:

- I do believe the race condition that it points out is real (but very narrow), but not sure that we don't have a similar one in current code (i.e. it wasn't introduced by your patch);
- The possible wq leak was definitely not introduced by this patch;
- The question about what happens with the file references after a

    device is removed is a separate issue;

The patch looks good, not opposed to it at all, the only one I am a bit concerned, about this patch, to be sure, is the race condition, and the two "paths" to reach it. (the others we can fix separately)

sashiko.dev <[email protected]>:

Does this introduce a race condition with sysfs?

Since the driver's sysfs nodes remain active after device_release_driver() is called, a user with sysfs privileges could write to the bind attribute to re-probe the driver before the device unregistration completes.

Quoted 6 lines
Quoted 4 lines

If idxd_device_drv_probe()->idxd_wqs_setup() runs concurrently with idxd_unregister_devices(), does it cause a use-after-free when it iterates over the idxd->wqs[] objects being freed, since suppress_bind_attrs is not set?

I think re-probe will call idxd_pci_probe() to allocate an new idxd device, this avoids accessing stale data.

Yeah, I missed the (now obvious) that a new 'probe' also means a new device. So this "path" is not valid. Thank you.

Quoted 11 lines
Quoted 5 lines

Also, does this code leak the workqueue?

The workqueue idxd->wq is dynamically allocated using create_workqueue() during device initialization, but destroy_workqueue(idxd->wq) does not appear to be called in idxd_remove() or the sysfs device release callback.

Looks like it's b7cb9a034305 ("dmaengine: idxd: Fix refcount underflow on module unload")
overlooked this, destory_workqueue(idxd->wq) is called in idxd_cleanup() -> idxd_cleanup_internals().

If so, I can send another patch to fix this.

Yes, please do.

Quoted 11 lines
Quoted 11 lines
Quoted 1 line
  	idxd_unregister_devices(idxd);

sashiko.dev <[email protected]>:

If idxd_unregister_devices() drops the last reference to the child wq objects and frees them, can idxd_shutdown() trigger a use-after-free when called shortly after?

For example, if a hardware error interrupt fired during teardown, idxd_shutdown()->flush_workqueue() could execute an error handler that iterates over the freed idxd->wqs[] memory.

Quoted 8 lines
Quoted 4 lines

Additionally, if the hardware is wedged, idxd_shutdown()->idxd_device_disable()->idxd_device_clear_state() iterates over idxd->wqs[] and attempts to acquire mutex_lock(&wq->wq_lock) on the freed memory.

I took a look — idxd_shutdown() is only invoked by idxd_remove() and during system shutdown/reboot.
Is idxd_shutdown() ever reached from the idxd hardware error interrupt path?

The bot's suggestion that this code path could be hit by a hardware error is false, but the shutdown path (like the user pressing a button) could be valid.

A crash during that could cause the machine to not poweroff, which is not good. Worth thinking about this, if there's something we could do while we are here.

Quoted 8 lines
Quoted 8 lines

Is there also a missing reference count for the cdev open path?

Since idxd_unregister_devices() frees the wq structure, if user space holds an open file descriptor, cdev_device_del() won't revoke it. Can file operations like mmap or poll subsequently access the freed ctx->wq pointer? Should idxd_cdev_open() take a kobject reference on the parent wq device?

Quoted 4 lines

To be honest, I'm not very familiar with the idxd driver. At this point, it looks like the idxd driver needs a state machine to ensure mutual exclusion
across the various concurrent paths.

Don't worry about this one, it's a separate issue.

Quoted 11 lines

Best Regards,
Guixin Liu

Quoted 7 lines

via: https://sashiko.dev/#/message/[email protected]
via: https://sashiko.dev/#/message/[email protected]

via: https://sashiko.dev/#/message/[email protected]
via: https://sashiko.dev/#/message/[email protected]

Cheers,

Signature · 1 line
Vinicius
author
#4 lore raw
Quoted 52 lines · Vinicius Costa Gomes
Quoted 36 lines · Guixin Liu
Quoted 13 lines · Vinicius Costa Gomes

drivers/dma/idxd/init.c static void idxd_remove(struct pci_dev *pdev) · line 1312

Quoted 12 lines · Guixin Liu
drivers/dma/idxd/init.c +0 -0
index fb80803d5b57..c3cfd96074c9 100644
@@ -1293,13 +1293,30 @@ static void idxd_remove(struct pci_dev *pdev)

[ ... skip 14 lines ... ]

drivers/dma/idxd/init.c +6 -0
+	 *
+	 * Use device_release_driver() to only unbind the driver (triggering
+	 * idxd_device_drv_remove()) without touching sysfs. Then safely
+	 * unregister children before the parent.
+	 */
+	device_release_driver(idxd_confdev(idxd));

Thanks for the reply.

Quoted 21 lines

Below are sashiko comments verbatim, some notes:

- I do believe the race condition that it points out is real (but very narrow), but not sure that we don't have a similar one in current code (i.e. it wasn't introduced by your patch);
- The possible wq leak was definitely not introduced by this patch;
- The question about what happens with the file references after a

     device is removed is a separate issue;

The patch looks good, not opposed to it at all, the only one I am a bit concerned, about this patch, to be sure, is the race condition, and the two "paths" to reach it. (the others we can fix separately)

sashiko.dev <[email protected]>:

Does this introduce a race condition with sysfs?

Since the driver's sysfs nodes remain active after device_release_driver() is called, a user with sysfs privileges could write to the bind attribute to re-probe the driver before the device unregistration completes.

Quoted 6 lines
Quoted 4 lines

If idxd_device_drv_probe()->idxd_wqs_setup() runs concurrently with idxd_unregister_devices(), does it cause a use-after-free when it iterates over the idxd->wqs[] objects being freed, since suppress_bind_attrs is not set?

I think re-probe will call idxd_pci_probe() to allocate an new idxd device, this avoids accessing stale data.

Yeah, I missed the (now obvious) that a new 'probe' also means a new device. So this "path" is not valid. Thank you.

Quoted 5 lines
Quoted 5 lines

Also, does this code leak the workqueue?

The workqueue idxd->wq is dynamically allocated using create_workqueue() during device initialization, but destroy_workqueue(idxd->wq) does not appear to be called in idxd_remove() or the sysfs device release callback.

Quoted 7 lines
Quoted 6 lines

Looks like it's b7cb9a034305 ("dmaengine: idxd: Fix refcount underflow on module unload")
overlooked this, destory_workqueue(idxd->wq) is called in idxd_cleanup() -> idxd_cleanup_internals().

If so, I can send another patch to fix this.

Yes, please do.

Will be added in v3.

Quoted 19 lines
Quoted 11 lines
Quoted 11 lines
Quoted 1 line
   	idxd_unregister_devices(idxd);

sashiko.dev <[email protected]>:

If idxd_unregister_devices() drops the last reference to the child wq objects and frees them, can idxd_shutdown() trigger a use-after-free when called shortly after?

For example, if a hardware error interrupt fired during teardown, idxd_shutdown()->flush_workqueue() could execute an error handler that iterates over the freed idxd->wqs[] memory.

Quoted 8 lines
Quoted 4 lines

Additionally, if the hardware is wedged, idxd_shutdown()->idxd_device_disable()->idxd_device_clear_state() iterates over idxd->wqs[] and attempts to acquire mutex_lock(&wq->wq_lock) on the freed memory.

I took a look — idxd_shutdown() is only invoked by idxd_remove() and during system shutdown/reboot.
Is idxd_shutdown() ever reached from the idxd hardware error interrupt path?

Quoted 7 lines

The bot's suggestion that this code path could be hit by a hardware error is false, but the shutdown path (like the user pressing a button) could be valid.

A crash during that could cause the machine to not poweroff, which is not good. Worth thinking about this, if there's something we could do while we are here.

Both idxd_remove() and idxd_shutdown() are called under device_lock(dev) — the lock is acquired by the driver core before invoking any bus callback:

1.

Remove path: device_release_driver_internal() → __device_driver_lock(dev, parent) → device_lock(dev) → ... → pci_device_remove() → idxd_remove()

2.

Shutdown path: device_shutdown() → device_lock(dev) → dev->bus->shutdown(dev) → pci_device_shutdown() → idxd_shutdown()

Since both paths acquire the same dev->mutex on the same PCI device,

idxd_remove() and idxd_shutdown() are mutually exclusive.

Best Regards,

Guixin Liu

Quoted 23 lines
Quoted 11 lines
Quoted 7 lines

Is there also a missing reference count for the cdev open path?

Since idxd_unregister_devices() frees the wq structure, if user space holds an open file descriptor, cdev_device_del() won't revoke it. Can file operations like mmap or poll subsequently access the freed ctx->wq pointer? Should idxd_cdev_open() take a kobject reference on the parent wq device?

To be honest, I'm not very familiar with the idxd driver. At this point, it looks like the idxd driver needs a state machine to ensure mutual exclusion
across the various concurrent paths.

Don't worry about this one, it's a separate issue.

Quoted 8 lines

Best Regards,
Guixin Liu

Quoted 6 lines

via: https://sashiko.dev/#/message/[email protected]
via: https://sashiko.dev/#/message/[email protected]

via: https://sashiko.dev/#/message/[email protected]
via: https://sashiko.dev/#/message/[email protected]

Cheers,

author
#5 lore raw
Quoted 56 lines · Guixin Liu
Quoted 53 lines · Vinicius Costa Gomes
Quoted 37 lines · Guixin Liu
Quoted 14 lines · Vinicius Costa Gomes
Quoted 5 lines · Guixin Liu
drivers/dma/idxd/init.c +0 -0
index fb80803d5b57..c3cfd96074c9 100644
@@ -1293,13 +1293,30 @@ static void idxd_remove(struct pci_dev *pdev)
Quoted 8 lines

[ ... skip 14 lines ... ]
+     *
+     * Use device_release_driver() to only unbind the driver (triggering
+     * idxd_device_drv_remove()) without touching sysfs. Then safely +     * unregister children before the parent.
+     */
+    device_release_driver(idxd_confdev(idxd));

Thanks for the reply.

Quoted 21 lines

Below are sashiko comments verbatim, some notes:

   - I do believe the race condition that it points out is real (but very narrow), but not sure that we don't have a similar one in current code (i.e. it wasn't introduced by your patch);
   - The possible wq leak was definitely not introduced by this patch;
   - The question about what happens with the file references after a device is removed is a separate issue;

The patch looks good, not opposed to it at all, the only one I am a bit concerned, about this patch, to be sure, is the race condition, and the two "paths" to reach it. (the others we can fix separately)

sashiko.dev <[email protected]>:

Does this introduce a race condition with sysfs?

Since the driver's sysfs nodes remain active after device_release_driver() is called, a user with sysfs privileges could write to the bind attribute to re-probe the driver before the device unregistration completes.

Quoted 6 lines
Quoted 4 lines

If idxd_device_drv_probe()->idxd_wqs_setup() runs concurrently with idxd_unregister_devices(), does it cause a use-after-free when it iterates over the idxd->wqs[] objects being freed, since suppress_bind_attrs is not set?

I think re-probe will call idxd_pci_probe() to allocate an new idxd device, this avoids accessing stale data.

Yeah, I missed the (now obvious) that a new 'probe' also means a new device. So this "path" is not valid. Thank you.

Quoted 5 lines
Quoted 5 lines

Also, does this code leak the workqueue?

The workqueue idxd->wq is dynamically allocated using create_workqueue() during device initialization, but destroy_workqueue(idxd->wq) does not appear to be called in idxd_remove() or the sysfs device release callback.

Quoted 8 lines
Quoted 7 lines
Quoted 6 lines

Looks like it's b7cb9a034305 ("dmaengine: idxd: Fix refcount underflow on module unload")
overlooked this, destory_workqueue(idxd->wq) is called in idxd_cleanup() -> idxd_cleanup_internals().

If so, I can send another patch to fix this.

Yes, please do.

Will be added in v3.

Well, the idxd->wq is freed in idxd_conf_device_release(), so there is no problem, no need to fix.

Quoted 73 lines
Quoted 19 lines
Quoted 11 lines
Quoted 11 lines
Quoted 1 line

idxd_unregister_devices(idxd);

sashiko.dev <[email protected]>:

If idxd_unregister_devices() drops the last reference to the child wq objects and frees them, can idxd_shutdown() trigger a use-after-free when called shortly after?

For example, if a hardware error interrupt fired during teardown, idxd_shutdown()->flush_workqueue() could execute an error handler that iterates over the freed idxd->wqs[] memory.

Quoted 8 lines
Quoted 4 lines

Additionally, if the hardware is wedged, idxd_shutdown()->idxd_device_disable()->idxd_device_clear_state() iterates over idxd->wqs[] and attempts to acquire mutex_lock(&wq->wq_lock) on the freed memory.

I took a look — idxd_shutdown() is only invoked by idxd_remove() and during system shutdown/reboot.
Is idxd_shutdown() ever reached from the idxd hardware error interrupt path?

Quoted 7 lines

The bot's suggestion that this code path could be hit by a hardware error is false, but the shutdown path (like the user pressing a button) could be valid.

A crash during that could cause the machine to not poweroff, which is not good. Worth thinking about this, if there's something we could do while we are here.

Both idxd_remove() and idxd_shutdown() are called under device_lock(dev) — the lock is acquired by the driver core before invoking any bus callback:

1.

   Remove path: device_release_driver_internal() → __device_driver_lock(dev, parent) → device_lock(dev) → ... → pci_device_remove() → idxd_remove()

2.

   Shutdown path: device_shutdown() → device_lock(dev) → dev->bus->shutdown(dev) → pci_device_shutdown() → idxd_shutdown()

Since both paths acquire the same dev->mutex on the same PCI device,

idxd_remove() and idxd_shutdown() are mutually exclusive.

Best Regards,

Guixin Liu

Quoted 23 lines
Quoted 11 lines
Quoted 7 lines

Is there also a missing reference count for the cdev open path?

Since idxd_unregister_devices() frees the wq structure, if user space holds an open file descriptor, cdev_device_del() won't revoke it. Can file operations like mmap or poll subsequently access the freed ctx->wq pointer? Should idxd_cdev_open() take a kobject reference on the parent wq device?

To be honest, I'm not very familiar with the idxd driver. At this point, it looks like the idxd driver needs a state machine to ensure mutual exclusion
across the various concurrent paths.

Don't worry about this one, it's a separate issue.

Quoted 8 lines

Best Regards,
Guixin Liu

Quoted 6 lines

via: https://sashiko.dev/#/message/[email protected]
via: https://sashiko.dev/#/message/[email protected]

via: https://sashiko.dev/#/message/[email protected]
via: https://sashiko.dev/#/message/[email protected]

Cheers,

author
#6 lore raw

Hi Vinicius,

It looks like all the issues from the AI Review have been clarified, Can this patch be accepted?

Best Regards,
Guixin Liu

Quoted 144 lines · Guixin Liu
Quoted 58 lines · Guixin Liu
Quoted 55 lines · Vinicius Costa Gomes
Quoted 39 lines · Guixin Liu
Quoted 16 lines · Vinicius Costa Gomes
Quoted 7 lines · Guixin Liu
drivers/dma/idxd/init.c +0 -0
index fb80803d5b57..c3cfd96074c9 100644
@@ -1293,13 +1293,30 @@ static void idxd_remove(struct pci_dev 
 *pdev)

[ ... skip 14 lines ... ]

Quoted 8 lines

+     *
+     * Use device_release_driver() to only unbind the driver (triggering
+     * idxd_device_drv_remove()) without touching sysfs. Then safely
+     * unregister children before the parent.
+     */
+    device_release_driver(idxd_confdev(idxd));

Thanks for the reply.

Quoted 21 lines

Below are sashiko comments verbatim, some notes:

   - I do believe the race condition that it points out is real (but very narrow), but not sure that we don't have a similar one in current code (i.e. it wasn't introduced by your patch);
   - The possible wq leak was definitely not introduced by this patch;
   - The question about what happens with the file references after a device is removed is a separate issue;

The patch looks good, not opposed to it at all, the only one I am a bit concerned, about this patch, to be sure, is the race condition, and the two "paths" to reach it. (the others we can fix separately)

sashiko.dev <[email protected]>:

Does this introduce a race condition with sysfs?

Since the driver's sysfs nodes remain active after device_release_driver() is called, a user with sysfs privileges could write to the bind attribute to re-probe the driver before the device unregistration completes.

Quoted 6 lines
Quoted 4 lines

If idxd_device_drv_probe()->idxd_wqs_setup() runs concurrently with idxd_unregister_devices(), does it cause a use-after-free when it iterates over the idxd->wqs[] objects being freed, since suppress_bind_attrs is not set?

I think re-probe will call idxd_pci_probe() to allocate an new idxd device, this avoids accessing stale data.

Yeah, I missed the (now obvious) that a new 'probe' also means a new device. So this "path" is not valid. Thank you.

Quoted 5 lines
Quoted 5 lines

Also, does this code leak the workqueue?

The workqueue idxd->wq is dynamically allocated using create_workqueue() during device initialization, but destroy_workqueue(idxd->wq) does not appear to be called in idxd_remove() or the sysfs device release callback.

Quoted 8 lines
Quoted 7 lines
Quoted 6 lines

Looks like it's b7cb9a034305 ("dmaengine: idxd: Fix refcount underflow on module unload")
overlooked this, destory_workqueue(idxd->wq) is called in idxd_cleanup() -> idxd_cleanup_internals().

If so, I can send another patch to fix this.

Yes, please do.

Will be added in v3.

Well, the idxd->wq is freed in idxd_conf_device_release(), so there is no problem, no need to fix.

Quoted 73 lines
Quoted 19 lines
Quoted 11 lines
Quoted 11 lines
Quoted 1 line

idxd_unregister_devices(idxd);

sashiko.dev <[email protected]>:

If idxd_unregister_devices() drops the last reference to the child wq objects and frees them, can idxd_shutdown() trigger a use-after-free when called shortly after?

For example, if a hardware error interrupt fired during teardown, idxd_shutdown()->flush_workqueue() could execute an error handler that iterates over the freed idxd->wqs[] memory.

Quoted 8 lines
Quoted 4 lines

Additionally, if the hardware is wedged, idxd_shutdown()->idxd_device_disable()->idxd_device_clear_state() iterates over idxd->wqs[] and attempts to acquire mutex_lock(&wq->wq_lock) on the freed memory.

I took a look — idxd_shutdown() is only invoked by idxd_remove() and during system shutdown/reboot.
Is idxd_shutdown() ever reached from the idxd hardware error interrupt path?

Quoted 7 lines

The bot's suggestion that this code path could be hit by a hardware error is false, but the shutdown path (like the user pressing a button) could be valid.

A crash during that could cause the machine to not poweroff, which is not good. Worth thinking about this, if there's something we could do while we are here.

Both idxd_remove() and idxd_shutdown() are called under device_lock(dev) — the lock is acquired by the driver core before invoking any bus callback:

1.

   Remove path: device_release_driver_internal() → __device_driver_lock(dev, parent) → device_lock(dev) → ... → pci_device_remove() → idxd_remove()

2.

   Shutdown path: device_shutdown() → device_lock(dev) → dev->bus->shutdown(dev) → pci_device_shutdown() → idxd_shutdown()

Since both paths acquire the same dev->mutex on the same PCI device,

idxd_remove() and idxd_shutdown() are mutually exclusive.

Best Regards,

Guixin Liu

Quoted 23 lines
Quoted 11 lines
Quoted 7 lines

Is there also a missing reference count for the cdev open path?

Since idxd_unregister_devices() frees the wq structure, if user space holds an open file descriptor, cdev_device_del() won't revoke it. Can file operations like mmap or poll subsequently access the freed ctx->wq pointer? Should idxd_cdev_open() take a kobject reference on the parent wq device?

To be honest, I'm not very familiar with the idxd driver. At this point, it looks like the idxd driver needs a state machine to ensure mutual exclusion
across the various concurrent paths.

Don't worry about this one, it's a separate issue.

Quoted 8 lines

Best Regards,
Guixin Liu

Quoted 6 lines

via: https://sashiko.dev/#/message/[email protected]
via: https://sashiko.dev/#/message/[email protected]

via: https://sashiko.dev/#/message/[email protected]
via: https://sashiko.dev/#/message/[email protected]

Cheers,

maintainer Acked-by on v2 1/1
#7 lore raw
In reply to Guixin Liu
Quoted 54 lines · Guixin Liu

We found an idxd_wq use-after-free issue with kasan when remove the idxd PCI device:

BUG: KASAN: slab-use-after-free in idxd_device_drv_remove+0x1f8/0x240 [idxd]
Call Trace:
  <TASK>
  dump_stack_lvl+0x32/0x50
  print_address_description.constprop.0+0x2c/0x390
  ? idxd_device_drv_remove+0x1f8/0x240 [idxd]
  print_report+0xba/0x280
  ? kasan_addr_to_slab+0x9/0xa0
  ? idxd_device_drv_remove+0x1f8/0x240 [idxd]
  kasan_report+0xab/0xe0
  ? idxd_device_drv_remove+0x1f8/0x240 [idxd]
  idxd_device_drv_remove+0x1f8/0x240 [idxd]
  device_release_driver_internal+0x391/0x560
  bus_remove_device+0x1f5/0x3f0
  device_del+0x392/0x990
  ? __pfx_device_del+0x10/0x10
  ? kobject_cleanup+0x117/0x360
  ? idxd_unregister_devices+0x229/0x320 [idxd]
  device_unregister+0x13/0xa0
  idxd_remove+0x4f/0x1b0 [idxd]
  pci_device_remove+0xa7/0x1d0
  device_release_driver_internal+0x391/0x560
  ? pci_pme_active+0x1e/0x450
  pci_stop_bus_device+0x10a/0x150
  pci_stop_and_remove_bus_device_locked+0x16/0x30
  remove_store+0xcf/0xe0

Freed by task 15535:
  kasan_save_stack+0x1c/0x40
  kasan_set_track+0x21/0x30
  kasan_save_free_info+0x27/0x40
  ____kasan_slab_free+0x171/0x240
  slab_free_freelist_hook+0xde/0x190
  __kmem_cache_free+0x19e/0x310
  device_release+0x98/0x210
  kobject_cleanup+0x102/0x360
  idxd_unregister_devices+0xb3/0x320 [idxd]
  dxd_remove+0x3f/0x1b0 [idxd]
  pci_device_remove+0xa7/0x1d0
  device_release_driver_internal+0x391/0x560
  pci_stop_bus_device+0x10a/0x150
  pci_stop_and_remove_bus_device_locked+0x16/0x30
  remove_store+0xcf/0xe0

In the idxd_remove() flow, when execution reaches idxd_unregister_devices(), all idxd_wq instances have already been freed. Subsequently, when device_unregister(idxd_confdev(idxd)) is executed, it calls into idxd_device_drv_remove() which accesses the already-freed idxd_wq. This fix resolves the issue by calling device_release_driver() before idxd_unregister_devices().

Quoted 5 lines
Fixes 98da0106aac0d ("dmanegine: idxd: fix resource free ordering on driver removal")

---

All questions that I had after the AI review are handled:

Cheers,

Signature · 1 line
Vinicius
author
#8 lore raw

Hi Vinicius,
    I notice that this patch is still not merged
into mainline, could you plz merge this patch?

Best Regards,
Guixin Liu

Quoted 67 lines · Vinicius Costa Gomes
Quoted 54 lines · Guixin Liu

We found an idxd_wq use-after-free issue with kasan when remove the idxd PCI device:

BUG: KASAN: slab-use-after-free in idxd_device_drv_remove+0x1f8/0x240 [idxd]
Call Trace:
   <TASK>
   dump_stack_lvl+0x32/0x50
   print_address_description.constprop.0+0x2c/0x390
   ? idxd_device_drv_remove+0x1f8/0x240 [idxd]
   print_report+0xba/0x280
   ? kasan_addr_to_slab+0x9/0xa0
   ? idxd_device_drv_remove+0x1f8/0x240 [idxd]
   kasan_report+0xab/0xe0
   ? idxd_device_drv_remove+0x1f8/0x240 [idxd]
   idxd_device_drv_remove+0x1f8/0x240 [idxd]
   device_release_driver_internal+0x391/0x560
   bus_remove_device+0x1f5/0x3f0
   device_del+0x392/0x990
   ? __pfx_device_del+0x10/0x10
   ? kobject_cleanup+0x117/0x360
   ? idxd_unregister_devices+0x229/0x320 [idxd]
   device_unregister+0x13/0xa0
   idxd_remove+0x4f/0x1b0 [idxd]
   pci_device_remove+0xa7/0x1d0
   device_release_driver_internal+0x391/0x560
   ? pci_pme_active+0x1e/0x450
   pci_stop_bus_device+0x10a/0x150
   pci_stop_and_remove_bus_device_locked+0x16/0x30
   remove_store+0xcf/0xe0

Freed by task 15535:
   kasan_save_stack+0x1c/0x40
   kasan_set_track+0x21/0x30
   kasan_save_free_info+0x27/0x40
   ____kasan_slab_free+0x171/0x240
   slab_free_freelist_hook+0xde/0x190
   __kmem_cache_free+0x19e/0x310
   device_release+0x98/0x210
   kobject_cleanup+0x102/0x360
   idxd_unregister_devices+0xb3/0x320 [idxd]
   dxd_remove+0x3f/0x1b0 [idxd]
   pci_device_remove+0xa7/0x1d0
   device_release_driver_internal+0x391/0x560
   pci_stop_bus_device+0x10a/0x150
   pci_stop_and_remove_bus_device_locked+0x16/0x30
   remove_store+0xcf/0xe0

In the idxd_remove() flow, when execution reaches idxd_unregister_devices(), all idxd_wq instances have already been freed. Subsequently, when device_unregister(idxd_confdev(idxd)) is executed, it calls into idxd_device_drv_remove() which accesses the already-freed idxd_wq. This fix resolves the issue by calling device_release_driver() before idxd_unregister_devices().

Quoted 5 lines
Fixes 98da0106aac0d ("dmanegine: idxd: fix resource free ordering on driver removal")

---

All questions that I had after the AI review are handled:

Cheers,

maintainer Tested-by
#9 lore raw
In reply to Vinicius Costa Gomes
Quoted 62 lines · Vinicius Costa Gomes
Quoted 54 lines · Guixin Liu

We found an idxd_wq use-after-free issue with kasan when remove the idxd PCI device:

BUG: KASAN: slab-use-after-free in idxd_device_drv_remove+0x1f8/0x240 [idxd]
Call Trace:
  <TASK>
  dump_stack_lvl+0x32/0x50
  print_address_description.constprop.0+0x2c/0x390
  ? idxd_device_drv_remove+0x1f8/0x240 [idxd]
  print_report+0xba/0x280
  ? kasan_addr_to_slab+0x9/0xa0
  ? idxd_device_drv_remove+0x1f8/0x240 [idxd]
  kasan_report+0xab/0xe0
  ? idxd_device_drv_remove+0x1f8/0x240 [idxd]
  idxd_device_drv_remove+0x1f8/0x240 [idxd]
  device_release_driver_internal+0x391/0x560
  bus_remove_device+0x1f5/0x3f0
  device_del+0x392/0x990
  ? __pfx_device_del+0x10/0x10
  ? kobject_cleanup+0x117/0x360
  ? idxd_unregister_devices+0x229/0x320 [idxd]
  device_unregister+0x13/0xa0
  idxd_remove+0x4f/0x1b0 [idxd]
  pci_device_remove+0xa7/0x1d0
  device_release_driver_internal+0x391/0x560
  ? pci_pme_active+0x1e/0x450
  pci_stop_bus_device+0x10a/0x150
  pci_stop_and_remove_bus_device_locked+0x16/0x30
  remove_store+0xcf/0xe0

Freed by task 15535:
  kasan_save_stack+0x1c/0x40
  kasan_set_track+0x21/0x30
  kasan_save_free_info+0x27/0x40
  ____kasan_slab_free+0x171/0x240
  slab_free_freelist_hook+0xde/0x190
  __kmem_cache_free+0x19e/0x310
  device_release+0x98/0x210
  kobject_cleanup+0x102/0x360
  idxd_unregister_devices+0xb3/0x320 [idxd]
  dxd_remove+0x3f/0x1b0 [idxd]
  pci_device_remove+0xa7/0x1d0
  device_release_driver_internal+0x391/0x560
  pci_stop_bus_device+0x10a/0x150
  pci_stop_and_remove_bus_device_locked+0x16/0x30
  remove_store+0xcf/0xe0

In the idxd_remove() flow, when execution reaches idxd_unregister_devices(), all idxd_wq instances have already been freed. Subsequently, when device_unregister(idxd_confdev(idxd)) is executed, it calls into idxd_device_drv_remove() which accesses the already-freed idxd_wq. This fix resolves the issue by calling device_release_driver() before idxd_unregister_devices().

Quoted 5 lines
Fixes 98da0106aac0d ("dmanegine: idxd: fix resource free ordering on driver removal")

---

Quoted 4 lines

All questions that I had after the AI review are handled:

Messing with /sys/bus/pci/drivers/idxd/unbind I was able to reproduce this issue without KASAN (KASAN makes it 100% reproducible), so:

Cheers,

Signature · 1 line
Vinicius
author
#10 lore raw
Quoted 67 lines · Vinicius Costa Gomes
Quoted 56 lines · Vinicius Costa Gomes
Quoted 54 lines · Guixin Liu

We found an idxd_wq use-after-free issue with kasan when remove the idxd PCI device:

BUG: KASAN: slab-use-after-free in idxd_device_drv_remove+0x1f8/0x240 [idxd]
Call Trace:
  <TASK>
  dump_stack_lvl+0x32/0x50
  print_address_description.constprop.0+0x2c/0x390
  ? idxd_device_drv_remove+0x1f8/0x240 [idxd]
  print_report+0xba/0x280
  ? kasan_addr_to_slab+0x9/0xa0
  ? idxd_device_drv_remove+0x1f8/0x240 [idxd]
  kasan_report+0xab/0xe0
  ? idxd_device_drv_remove+0x1f8/0x240 [idxd]
  idxd_device_drv_remove+0x1f8/0x240 [idxd]
  device_release_driver_internal+0x391/0x560
  bus_remove_device+0x1f5/0x3f0
  device_del+0x392/0x990
  ? __pfx_device_del+0x10/0x10
  ? kobject_cleanup+0x117/0x360
  ? idxd_unregister_devices+0x229/0x320 [idxd]
  device_unregister+0x13/0xa0
  idxd_remove+0x4f/0x1b0 [idxd]
  pci_device_remove+0xa7/0x1d0
  device_release_driver_internal+0x391/0x560
  ? pci_pme_active+0x1e/0x450
  pci_stop_bus_device+0x10a/0x150
  pci_stop_and_remove_bus_device_locked+0x16/0x30
  remove_store+0xcf/0xe0

Freed by task 15535:
  kasan_save_stack+0x1c/0x40
  kasan_set_track+0x21/0x30
  kasan_save_free_info+0x27/0x40
  ____kasan_slab_free+0x171/0x240
  slab_free_freelist_hook+0xde/0x190
  __kmem_cache_free+0x19e/0x310
  device_release+0x98/0x210
  kobject_cleanup+0x102/0x360
  idxd_unregister_devices+0xb3/0x320 [idxd]
  dxd_remove+0x3f/0x1b0 [idxd]
  pci_device_remove+0xa7/0x1d0
  device_release_driver_internal+0x391/0x560
  pci_stop_bus_device+0x10a/0x150
  pci_stop_and_remove_bus_device_locked+0x16/0x30
  remove_store+0xcf/0xe0

In the idxd_remove() flow, when execution reaches idxd_unregister_devices(), all idxd_wq instances have already been freed. Subsequently, when device_unregister(idxd_confdev(idxd)) is executed, it calls into idxd_device_drv_remove() which accesses the already-freed idxd_wq. This fix resolves the issue by calling device_release_driver() before idxd_unregister_devices().

Quoted 9 lines
Quoted 5 lines
Fixes 98da0106aac0d ("dmanegine: idxd: fix resource free ordering on driver removal")

---

All questions that I had after the AI review are handled:

Quoted 7 lines

Messing with /sys/bus/pci/drivers/idxd/unbind I was able to reproduce this issue without KASAN (KASAN makes it 100% reproducible), so:

Cheers,

Thanks for the testing, could this patch be applied in 7.4?

Best Regards,
Guixin Liu

author
#11 lore raw
In reply to Guixin Liu

Hi idxd maintainers, could anyone merge this patch?

Best Regards,
Guixin Liu

Quoted 106 lines · Guixin Liu

We found an idxd_wq use-after-free issue with kasan when remove the idxd PCI device:

BUG: KASAN: slab-use-after-free in idxd_device_drv_remove+0x1f8/0x240 [idxd]
Call Trace:
  <TASK>
  dump_stack_lvl+0x32/0x50
  print_address_description.constprop.0+0x2c/0x390
  ? idxd_device_drv_remove+0x1f8/0x240 [idxd]
  print_report+0xba/0x280
  ? kasan_addr_to_slab+0x9/0xa0
  ? idxd_device_drv_remove+0x1f8/0x240 [idxd]
  kasan_report+0xab/0xe0
  ? idxd_device_drv_remove+0x1f8/0x240 [idxd]
  idxd_device_drv_remove+0x1f8/0x240 [idxd]
  device_release_driver_internal+0x391/0x560
  bus_remove_device+0x1f5/0x3f0
  device_del+0x392/0x990
  ? __pfx_device_del+0x10/0x10
  ? kobject_cleanup+0x117/0x360
  ? idxd_unregister_devices+0x229/0x320 [idxd]
  device_unregister+0x13/0xa0
  idxd_remove+0x4f/0x1b0 [idxd]
  pci_device_remove+0xa7/0x1d0
  device_release_driver_internal+0x391/0x560
  ? pci_pme_active+0x1e/0x450
  pci_stop_bus_device+0x10a/0x150
  pci_stop_and_remove_bus_device_locked+0x16/0x30
  remove_store+0xcf/0xe0

Freed by task 15535:
  kasan_save_stack+0x1c/0x40
  kasan_set_track+0x21/0x30
  kasan_save_free_info+0x27/0x40
  ____kasan_slab_free+0x171/0x240
  slab_free_freelist_hook+0xde/0x190
  __kmem_cache_free+0x19e/0x310
  device_release+0x98/0x210
  kobject_cleanup+0x102/0x360
  idxd_unregister_devices+0xb3/0x320 [idxd]
  dxd_remove+0x3f/0x1b0 [idxd]
  pci_device_remove+0xa7/0x1d0
  device_release_driver_internal+0x391/0x560
  pci_stop_bus_device+0x10a/0x150
  pci_stop_and_remove_bus_device_locked+0x16/0x30
  remove_store+0xcf/0xe0

In the idxd_remove() flow, when execution reaches idxd_unregister_devices(), all idxd_wq instances have already been freed. Subsequently, when device_unregister(idxd_confdev(idxd)) is executed, it calls into idxd_device_drv_remove() which accesses the already-freed idxd_wq. This fix resolves the issue by calling device_release_driver() before idxd_unregister_devices().

Fixes 98da0106aac0d ("dmanegine: idxd: fix resource free ordering on driver removal")

---
v1 -> v2:
1. Call device_release_driver() in advance instead of swapping the order of device_unregister() and idxd_unregister_devices().
2. Add Co-developed-by: Shuai Xue <[email protected]>.

 drivers/dma/idxd/init.c | 27 ++++++++++++++++++++++-----
 1 file changed, 22 insertions(+), 5 deletions(-)
drivers/dma/idxd/init.c +22 -5
index f1cfc7790d95..3b0a0363ca65 100644
@@ -1293,13 +1293,30 @@ static void idxd_remove(struct pci_dev *pdev)
 {
 	struct idxd_device *idxd = pci_get_drvdata(pdev);
 
+	/*
+	 * The idxd sub-driver's remove callback (idxd_device_drv_remove())
+	 * iterates idxd->wqs[] and accesses wq objects. We must unbind the
+	 * sub-driver before idxd_unregister_devices() frees these objects,
+	 * otherwise a use-after-free occurs.
+	 *
+	 * We cannot simply reorder device_unregister(idxd_confdev) before
+	 * idxd_unregister_devices() because device_del() -> kobject_del()
+	 * recursively removes the parent's sysfs directory, which destroys
+	 * children's sysfs entries. Subsequent device_unregister() on the
+	 * children then fails with "sysfs group 'power' not found".
+	 *
+	 * Use device_release_driver() to only unbind the driver (triggering
+	 * idxd_device_drv_remove()) without touching sysfs. Then safely
+	 * unregister children before the parent.
+	 */
+	device_release_driver(idxd_confdev(idxd));
 	idxd_unregister_devices(idxd);
+
 	/*
-	 * When ->release() is called for the idxd->conf_dev, it frees all the memory related
-	 * to the idxd context. The driver still needs those bits in order to do the rest of
-	 * the cleanup. However, we do need to unbound the idxd sub-driver. So take a ref
-	 * on the device here to hold off the freeing while allowing the idxd sub-driver
-	 * to unbind.
+	 * When ->release() is called for the idxd->conf_dev, it frees all the
+	 * memory related to the idxd context. The driver still needs those bits
+	 * in order to do the rest of the cleanup. So take a ref on the device
+	 * here to hold off the freeing.
 	 */
 	get_device(idxd_confdev(idxd));
 	device_unregister(idxd_confdev(idxd));
author
#12 lore raw
In reply to Guixin Liu

Hi Vinod, could you please apply this patch?
This patch was from a long time ago.

Best Regards,
Guixin Liu

Quoted 106 lines · Guixin Liu

We found an idxd_wq use-after-free issue with kasan when remove the idxd PCI device:

BUG: KASAN: slab-use-after-free in idxd_device_drv_remove+0x1f8/0x240 [idxd]
Call Trace:
  <TASK>
  dump_stack_lvl+0x32/0x50
  print_address_description.constprop.0+0x2c/0x390
  ? idxd_device_drv_remove+0x1f8/0x240 [idxd]
  print_report+0xba/0x280
  ? kasan_addr_to_slab+0x9/0xa0
  ? idxd_device_drv_remove+0x1f8/0x240 [idxd]
  kasan_report+0xab/0xe0
  ? idxd_device_drv_remove+0x1f8/0x240 [idxd]
  idxd_device_drv_remove+0x1f8/0x240 [idxd]
  device_release_driver_internal+0x391/0x560
  bus_remove_device+0x1f5/0x3f0
  device_del+0x392/0x990
  ? __pfx_device_del+0x10/0x10
  ? kobject_cleanup+0x117/0x360
  ? idxd_unregister_devices+0x229/0x320 [idxd]
  device_unregister+0x13/0xa0
  idxd_remove+0x4f/0x1b0 [idxd]
  pci_device_remove+0xa7/0x1d0
  device_release_driver_internal+0x391/0x560
  ? pci_pme_active+0x1e/0x450
  pci_stop_bus_device+0x10a/0x150
  pci_stop_and_remove_bus_device_locked+0x16/0x30
  remove_store+0xcf/0xe0

Freed by task 15535:
  kasan_save_stack+0x1c/0x40
  kasan_set_track+0x21/0x30
  kasan_save_free_info+0x27/0x40
  ____kasan_slab_free+0x171/0x240
  slab_free_freelist_hook+0xde/0x190
  __kmem_cache_free+0x19e/0x310
  device_release+0x98/0x210
  kobject_cleanup+0x102/0x360
  idxd_unregister_devices+0xb3/0x320 [idxd]
  dxd_remove+0x3f/0x1b0 [idxd]
  pci_device_remove+0xa7/0x1d0
  device_release_driver_internal+0x391/0x560
  pci_stop_bus_device+0x10a/0x150
  pci_stop_and_remove_bus_device_locked+0x16/0x30
  remove_store+0xcf/0xe0

In the idxd_remove() flow, when execution reaches idxd_unregister_devices(), all idxd_wq instances have already been freed. Subsequently, when device_unregister(idxd_confdev(idxd)) is executed, it calls into idxd_device_drv_remove() which accesses the already-freed idxd_wq. This fix resolves the issue by calling device_release_driver() before idxd_unregister_devices().

Fixes 98da0106aac0d ("dmanegine: idxd: fix resource free ordering on driver removal")

---
v1 -> v2:
1. Call device_release_driver() in advance instead of swapping the order of device_unregister() and idxd_unregister_devices().
2. Add Co-developed-by: Shuai Xue <[email protected]>.

 drivers/dma/idxd/init.c | 27 ++++++++++++++++++++++-----
 1 file changed, 22 insertions(+), 5 deletions(-)
drivers/dma/idxd/init.c +22 -5
index f1cfc7790d95..3b0a0363ca65 100644
@@ -1293,13 +1293,30 @@ static void idxd_remove(struct pci_dev *pdev)
 {
 	struct idxd_device *idxd = pci_get_drvdata(pdev);
 
+	/*
+	 * The idxd sub-driver's remove callback (idxd_device_drv_remove())
+	 * iterates idxd->wqs[] and accesses wq objects. We must unbind the
+	 * sub-driver before idxd_unregister_devices() frees these objects,
+	 * otherwise a use-after-free occurs.
+	 *
+	 * We cannot simply reorder device_unregister(idxd_confdev) before
+	 * idxd_unregister_devices() because device_del() -> kobject_del()
+	 * recursively removes the parent's sysfs directory, which destroys
+	 * children's sysfs entries. Subsequent device_unregister() on the
+	 * children then fails with "sysfs group 'power' not found".
+	 *
+	 * Use device_release_driver() to only unbind the driver (triggering
+	 * idxd_device_drv_remove()) without touching sysfs. Then safely
+	 * unregister children before the parent.
+	 */
+	device_release_driver(idxd_confdev(idxd));
 	idxd_unregister_devices(idxd);
+
 	/*
-	 * When ->release() is called for the idxd->conf_dev, it frees all the memory related
-	 * to the idxd context. The driver still needs those bits in order to do the rest of
-	 * the cleanup. However, we do need to unbound the idxd sub-driver. So take a ref
-	 * on the device here to hold off the freeing while allowing the idxd sub-driver
-	 * to unbind.
+	 * When ->release() is called for the idxd->conf_dev, it frees all the
+	 * memory related to the idxd context. The driver still needs those bits
+	 * in order to do the rest of the cleanup. So take a ref on the device
+	 * here to hold off the freeing.
 	 */
 	get_device(idxd_confdev(idxd));
 	device_unregister(idxd_confdev(idxd));