dmaengine: idxd: fix fdev setup failure cleanup in idxd_cdev_open()
Subsystems (2)
PATCH v3 1/1 dmaengine: idxd: fix fdev setup failure cleanup in idxd_cdev_open()
The failed_dev_add and failed_dev_name paths drop the file-device reference while wq->wq_lock is still held. If put_device(fdev) drops the last reference, idxd_file_dev_release() runs synchronously and tries to take wq->wq_lock again, deadlocking.
Those paths also fall through into the later ctx cleanup labels even though idxd_file_dev_release() owns that cleanup and frees ctx. This can make idxd_xa_pasid_remove(ctx) and kfree(ctx) operate on a freed context.
Move idxd_wq_get() before file-device setup can fail, since the release callback always calls idxd_wq_put(). Then unlock wq->wq_lock before put_device(fdev) and return directly from the file-device setup failure path, leaving ctx cleanup to the release callback.
---
Changes in v3:
- Drop scoped __free(put_device) cleanup and use explicit cleanup, as suggested by Dave Jiang.
- Keep idxd_wq_get() before file-device setup can fail so the release callback always balances a matching WQ reference.
Changes in v2:
- Use __free(put_device) for the file-device reference.
- Take the WQ reference before fdev can be released so the release callback's idxd_wq_put() has a matching idxd_wq_get().
drivers/dma/idxd/cdev.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/dma/idxd/cdev.c +3 -1
@@ -288,6 +288,7 @@ static int idxd_cdev_open(struct inode *inode, struct file *filp) fdev->parent =cdev_dev (idxd_cdev); fdev->bus = &dsa_bus_type; fdev->type = &idxd_cdev_file_type; +idxd_wq_get (wq); rc =dev_set_name (fdev,"file%d" , ctx->id);if (rc <0 ) {
@@ -301,13 +302,14 @@ static int idxd_cdev_open(struct inode *inode, struct file *filp) goto failed_dev_add; } -idxd_wq_get (wq);mutex_unlock (&wq->wq_lock);return 0 ; failed_dev_add: failed_dev_name: +mutex_unlock (&wq->wq_lock);put_device (fdev); +return rc; failed_ida: failed_set_pasid:if (device_user_pasid_enabled (idxd))
Signature · 1 line
2.43.0
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Pre-existing issues:
- [Critical] If ida_alloc() fails in idxd_cdev_open(), the error code is not assigned to rc, causing the function to return 0 (success) while leaving filp->private_data pointing to a freed context. --
commit 51406f4c692bf6e1cbab1fe293fc2fda433beb08
Author: Yuho Choi <[email protected]>
dmaengine: idxd: fix fdev setup failure cleanup in idxd_cdev_open()
This commit fixes a deadlock and use-after-free during file-device setup failures in idxd_cdev_open(). It moves idxd_wq_get() to occur before potential failures and adjusts the cleanup path to return directly, leaving context cleanup to the release callback.
Quoted 3 lines
drivers/dma/idxd/cdev.c +0
-0
[ ... ]
Quoted 4 lines
drivers/dma/idxd/cdev.c +0
-0@@ -301,13 +302,14 @@ static int idxd_cdev_open(struct inode *inode, struct file *filp) goto failed_dev_add; }
drivers/dma/idxd/cdev.c static int idxd_cdev_open(struct inode *inode, struct file *filp) · line 313
Quoted 10 lines
drivers/dma/idxd/cdev.c +2
-1-idxd_wq_get (wq);mutex_unlock (&wq->wq_lock);return 0 ; failed_dev_add: failed_dev_name: +mutex_unlock (&wq->wq_lock);put_device (fdev); +return rc; failed_ida:
[Severity: Critical]
This isn't a bug introduced by this patch, but is it possible for rc to be 0 when reaching this label?
Looking at the ida_alloc() call earlier in this function:
ctx->id = ida_alloc(&file_ida, GFP_KERNEL);
if (ctx->id < 0) {
dev_warn(dev, "ida alloc failure\n");
goto failed_ida;
}If ida_alloc() fails, it returns a negative error code to ctx->id, but rc is not updated and remains 0. The function then jumps to failed_ida, eventually frees the context, and returns 0.
Since open() returns 0, the VFS layer treats the system call as successful and returns a valid file descriptor to user space. However, filp->private_data was already set to the freed ctx earlier in the function.
Could this lead to a use-after-free if subsequent file operations (like write() or close()) dereference the dangling filp->private_data pointer?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Hi,
Quoted 9 lines · Yuho Choi
The
failed_dev_addandfailed_dev_namepaths drop the file-device reference whilewq->wq_lockis still held. Ifput_device(fdev)drops the last reference,idxd_file_dev_release()runs synchronously and tries to takewq->wq_lockagain, deadlocking.Those paths also fall through into the later ctx cleanup labels even though
idxd_file_dev_release()owns that cleanup and frees ctx. This can makeidxd_xa_pasid_remove(ctx)and kfree(ctx) operate on a freed context.
Quoted 8 lines
Move
idxd_wq_get()before file-device setup can fail, since the release callback always callsidxd_wq_put(). Then unlockwq->wq_lockbeforeput_device(fdev)and return directly from the file-device setup failure path, leaving ctx cleanup to the release callback.Fixes e6fd6d7e5f0fe ("dmaengine: idxd: add a device to represent the file opened")Signed-off-by Yuho Choi <[email protected]>---
Cheers,
Signature · 1 line
Vinicius
Quoted 9 lines · Yuho Choi
The
failed_dev_addandfailed_dev_namepaths drop the file-device reference whilewq->wq_lockis still held. Ifput_device(fdev)drops the last reference,idxd_file_dev_release()runs synchronously and tries to takewq->wq_lockagain, deadlocking.Those paths also fall through into the later ctx cleanup labels even though
idxd_file_dev_release()owns that cleanup and frees ctx. This can makeidxd_xa_pasid_remove(ctx)and kfree(ctx) operate on a freed context.
Quoted 7 lines
Move
idxd_wq_get()before file-device setup can fail, since the release callback always callsidxd_wq_put(). Then unlockwq->wq_lockbeforeput_device(fdev)and return directly from the file-device setup failure path, leaving ctx cleanup to the release callback.Fixes e6fd6d7e5f0fe ("dmaengine: idxd: add a device to represent the file opened")Signed-off-by Yuho Choi <[email protected]>
Quoted 42 lines
---
Changes in v3:
- Drop scoped__free(put_device)cleanup and use explicit cleanup, as suggested by Dave Jiang.
- Keepidxd_wq_get()before file-device setup can fail so the release callback always balances a matching WQ reference.
Changes in v2:
- Use__free(put_device)for the file-device reference.
- Take the WQ reference before fdev can be released so the release callback'sidxd_wq_put()has a matchingidxd_wq_get().drivers/dma/idxd/cdev.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)drivers/dma/idxd/cdev.c +3
-1@@ -288,6 +288,7 @@ static int idxd_cdev_open(struct inode *inode, struct file *filp) fdev->parent =cdev_dev (idxd_cdev); fdev->bus = &dsa_bus_type; fdev->type = &idxd_cdev_file_type; +idxd_wq_get (wq); rc =dev_set_name (fdev,"file%d" , ctx->id);if (rc <0 ) {@@ -301,13 +302,14 @@ static int idxd_cdev_open(struct inode *inode, struct file *filp) goto failed_dev_add; } -idxd_wq_get (wq);mutex_unlock (&wq->wq_lock);return 0 ; failed_dev_add: failed_dev_name: +mutex_unlock (&wq->wq_lock);put_device (fdev); +return rc; failed_ida: failed_set_pasid:if (device_user_pasid_enabled (idxd))
Quoted 10 lines · Yuho Choi
The
failed_dev_addandfailed_dev_namepaths drop the file-device reference whilewq->wq_lockis still held. Ifput_device(fdev)drops the last reference,idxd_file_dev_release()runs synchronously and tries to takewq->wq_lockagain, deadlocking.Those paths also fall through into the later ctx cleanup labels even though
idxd_file_dev_release()owns that cleanup and frees ctx. This can makeidxd_xa_pasid_remove(ctx)and kfree(ctx) operate on a freed context.[...]
Applied, thanks!
[1/1] dmaengine: idxd: fix fdev setup failure cleanup in idxd_cdev_open()
commit: ee1d7274102285d78a53161fc705a8d8cd40b066
Best regards,
Signature · 1 line
~Vinod