firmware: imx: Add missing return in error path to prevent use-after-free.
PATCH 1/1 firmware: imx: Add missing return in error path to prevent use-after-free.
After freeing dev_ctx and setting *new_dev_ctx to NULL, the function continues execution and dereferences dev_ctx, leading to a potential use-after-free. Return immediately to avoid accessing freed memory.
This appears to have been lost during merge of the original fix.
--- drivers/firmware/imx/se_ctrl.c | 2 ++ 1 file changed, 2 insertions(+)
drivers/firmware/imx/se_ctrl.c +2 -0
@@ -486,6 +486,8 @@ static int init_device_context(struct se_if_priv *priv, int ch_id,kfree (dev_ctx->devname);kfree (dev_ctx); *new_dev_ctx =NULL ; + +return ret; }list_add_tail (&dev_ctx->link, &priv->dev_ctx_list);
Signature · 1 line
2.43.0
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 7 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 7 potential issue(s) to consider:
Pre-existing issues:
- [Critical] Improper cancellation of asynchronous hardware commands leads to Use-After-Free and kernel memory corruption upon delayed response.
- [High] Shared driver state is incorrectly protected by per-file descriptor locks, leading to list corruption and data races.
- [High] Incorrect usage of scoped_cond_guard in .release file operation causes Use-After-Free and permanent resource leaks.
- [High] Direct dereference of user-space pointers without using copy_from_user() or get_user().
- [High] Devres lifecycle mismatch with character device file descriptors causes Use-After-Free upon unbind.
- [High] Unhandled pending signals in ele_msg_rcv cause an infinite loop and livelock.
- [Medium] Masked -EFAULT error code in se_ioctl_cmd_snd_rcv_rsp_handler falsely reports success to user space.
--
commit 91ef98b22f1e9e4e106c5851a3e8c69fd60f747c
Author: Pankaj Gupta <[email protected]>
firmware: imx: Add missing return in error path to prevent use-after-free.
This commit fixes a missing return in the error path of init_device_context() to prevent a use-after-free. After setting *new_dev_ctx to NULL, the function previously continued execution and dereferenced dev_ctx.
drivers/firmware/imx/se_ctrl.c static int init_device_context(struct se_if_priv *priv, int ch_id, · line 493
Quoted 13 lines
drivers/firmware/imx/se_ctrl.c +2
-0@@ -486,6 +486,8 @@ static int init_device_context(struct se_if_priv *priv, int ch_id,kfree (dev_ctx->devname);kfree (dev_ctx); *new_dev_ctx =NULL ; + +return ret; }list_add_tail (&dev_ctx->link, &priv->dev_ctx_list);
[Severity: High]
This is a pre-existing issue, but does se_if_fops_close() race with se_if_fops_open() on the dev_ctx_list?
In se_if_fops_open(), new contexts are added to priv->dev_ctx_list under misc_dev_ctx->fops_lock. However, in se_if_fops_close(), contexts are removed using list_del(&dev_ctx->link) while holding the per-file descriptor lock dev_ctx->fops_lock.
Since concurrent open and close calls acquire entirely different locks, does this guarantee list corruption? Furthermore, does se_if_probe_cleanup() traverse this list without holding any lock?
[Severity: High]
This is a pre-existing issue, but does the usage of scoped_cond_guard in the .release file operation cause a use-after-free and resource leak?
se_if_fops_close() uses scoped_cond_guard(mutex_intr, return -EBUSY, &dev_ctx->fops_lock). VFS .release operations ignore the return value, so returning -EBUSY when interrupted permanently leaks the dev_ctx memory and leaves dangling pointers in priv->dev_ctx_list.
If the lock is successfully acquired, kfree(dev_ctx) is called inside the scope block. Upon scope exit, the guard macro automatically calls mutex_unlock(&dev_ctx->fops_lock). Since dev_ctx was just freed, does this dereference freed memory?
[Severity: High]
This is a pre-existing issue, but does se_if_fops_write() directly dereference user-space pointers?
In se_if_fops_write(), the buf argument is a const char __user *, which is cast to struct se_msg_hdr * and directly passed to se_chk_tx_msg_hdr(), which accesses its fields directly.
Similarly, in se_ioctl_cmd_snd_rcv_rsp_handler(), the tx_buf field is a user pointer directly passed to se_chk_tx_msg_hdr() before the data is safely copied with memdup_user(). Could this violate uaccess rules and trigger a kernel panic on architectures with SMAP enabled?
[Severity: Critical]
This is a pre-existing issue, but does improper cancellation of asynchronous hardware commands lead to a use-after-free?
In ele_msg_send_rcv(), the driver assigns a locally-allocated buffer rx_msg to priv->waiting_rsp_clbk_hdl.rx_msg. If ele_msg_send() fails, the function returns early without clearing the callback state. If ele_msg_rcv() times out, the rx_msg pointer is also left dangling. The local rx_msg buffer is then freed by the caller.
Later, when the firmware unexpectedly responds, se_if_rx_callback() executes asynchronously. Is it possible that it dereferences a potentially NULL dev_ctx, or directly copies the hardware payload into the dangling rx_msg pointer using memcpy(), leading to kernel heap corruption?
[Severity: Medium]
This is a pre-existing issue, but is the -EFAULT error code masked in se_ioctl_cmd_snd_rcv_rsp_handler()?
If copy_to_user() fails to copy the rx_msg payload, the function correctly sets err = -EFAULT. However, it then immediately calls err = se_ioctl_cmd_snd_rcv_cleanup(dev_ctx, uarg, &cmd_snd_rcv_rsp_info).
If the cleanup succeeds, it returns 0, which unconditionally overwrites the previously set -EFAULT error code. Does the IOCTL thus return success to user space despite the data transfer failure?
[Severity: High]
This is a pre-existing issue, but does the devres lifecycle mismatch with character device file descriptors cause a use-after-free upon unbind?
The core state struct se_if_priv is allocated using devm_kzalloc(), meaning it is automatically freed when the device is unbound. However, the driver exposes a miscdevice interface, and applications may keep file descriptors open during unbind.
When an application eventually closes an open file descriptor, VFS will call se_if_fops_close(). Will this blindly access dev_ctx->priv, which was freed by devres during the unbind operation?
[Severity: High]
This is a pre-existing issue, but do unhandled pending signals in ele_msg_rcv() cause an infinite loop and livelock?
In ele_msg_rcv(), the code uses a do { ... } while (ret < 0) loop around wait_for_completion_interruptible_timeout(). If interrupted by a signal, wait_for_completion returns -ERESTARTSYS.
The error handling block sets signal_rcvd = true and calls continue without clearing the pending signal or returning to user space. The loop restarts and immediately calls wait_for_completion again, which instantly returns -ERESTARTSYS because the signal is still pending. Can this create a tight infinite kernel loop that pins the CPU and livelocks the thread?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Quoted 9 lines · Pankaj Gupta
After freeing
dev_ctxand setting*new_dev_ctxto NULL, the function continues execution and dereferencesdev_ctx, leading to a potential use-after-free. Return immediately to avoid accessing freed memory.This appears to have been lost during merge of the original fix.
Fixes 3ae9dcce8400 ("firmware: drivers: imx: adds miscdev")Signed-off-by Dan Carpenter <[email protected]>Signed-off-by Pankaj Gupta <[email protected]>
I try not to be a whiner about patch attibution to me because I feel like I get enough credit for stuff and don't need to complain about small things. But it's puzzling to see my Signed-off-by here and normally someone would ask about it.
I did write a patch for this but you have modified the commit message and added a blank line.
https://lore.kernel.org/all/[email protected]/
Potentially, you already had written this patch by the time I sent mine because it was a static checker fix and a lot of people run static checkers.
Normally these tags are in chronoligical order and a Signed-off-by would mean I handled the patch. It doesn't make sense for me to be the first person to handle the patch if I didn't write it.
I suspect what happened is that actually I should get authorship credit but when you modified the patch, the authorship was lost. (I have done this mistake unintentionally before. More than once). To give authorship credit, the first line in the body of the email would be:
From: Dan Carpenter <[email protected]>
But if you already had a patch written then just give me a Reported-by tag instead. Either way is fine.
Sorry again, for being a bit nit picky on this...
regards,
dan carpenter
[email protected]
AppliedFrom: Frank Li <[email protected]>
Quoted 8 lines · Pankaj Gupta
After freeing
dev_ctxand setting*new_dev_ctxto NULL, the function continues execution and dereferencesdev_ctx, leading to a potential use-after-free. Return immediately to avoid accessing freed memory.This appears to have been lost during merge of the original fix.
[...]
Applied, thanks!
[1/1] firmware: imx: Add missing return in error path to prevent use-after-free.
commit: 28599a13adb5db6b4fb63ce088766c456f8f562b
Best regards,
Signature · 1 line
Frank Li <[email protected]>