[PATCH v3] media: rc: fix use-after-free of rc_map.name on register error path
From: YuanZhe Liu
Date: Sun Nov 22 2026 - 04:22:19 EST
If rc_register_device() fails after device_add() - for example when
ir_raw_event_register() cannot create its kthread - the unwind path
frees the keymap via rc_free_rx_device() while the device is still
visible in sysfs, and never clears dev->registered.
A userspace reader of the uevent attribute then enters
rc_dev_uevent(), passes the registered check under dev->lock and
dereferences the already freed dev->rc_map.name:
BUG: KASAN: slab-use-after-free in string+0x483/0x4c0
lib/vsprintf.c:737
Read of size 1 at addr ffff888007ca9d80
rc_dev_uevent+0xd7/0x1d0 drivers/media/rc/rc-main.c:1628
dev_uevent+0x28d/0x570 drivers/base/core.c:2709
uevent_show+0x1af/0x380 drivers/base/core.c:2756
Clearing dev->registered before the teardown would make
rc_dev_uevent() return -ENODEV, which makes dev_uevent() fail and
suppresses the KOBJ_REMOVE uevent of device_del() altogether. The
device has already been announced with KOBJ_ADD, so the remove event
has to be delivered as well, and it needs the keymap and the driver
strings.
Instead, keep the keymap alive until device_del() has been called:
only unregister the input device at the out_rx label, since it is a
child of dev->dev, and let out_rx_free free the keymap afterwards, as
it already does for the other error paths. device_del() drains the
sysfs readers, so rc_dev_uevent() cannot run once it has returned and
the keymap can be freed there.
The registered flag still has to be cleared so that lirc file
descriptors which are still open cannot reach the driver callbacks,
but that now happens after the remove uevent has been sent; this also
covers the lirc_register() and rc_setup_rx_device() failures. Factor
the lock/close/clear sequence into set_unregistered() and share it
with rc_unregister_device().
Fixes: f56928abaa6d ("[media] rc-core: cleanup rc_register_device")
Cc: stable@xxxxxxxxxxxxxxx
Co-developed-by: Bin Yu <byu@xxxxxxxxxxxxx>
Signed-off-by: Bin Yu <byu@xxxxxxxxxxxxx>
Co-developed-by: Cheng Wen <wencheng@xxxxxxxxxxxxx>
Signed-off-by: Cheng Wen <wencheng@xxxxxxxxxxxxx>
Signed-off-by: YuanZhe Liu <25031212351@xxxxxxxxxxxxxxxxx>
---
Hi Sean,
thanks for the review. I have dropped the comment and renamed the helper
to set_unregistered().
One correction on the lirc_register() error path: it goes to out_dev, not
out_lirc, so neither lirc nor the input device is registered when it is
taken, and the keymap is already freed after device_del() there. With
this version the flag is cleared after device_del() on every error path,
so that path is covered as well.
That also answers the point you made earlier about the remove uevent: a
cleared dev->registered makes rc_dev_uevent() return -ENODEV, dev_uevent()
fails and kobject_uevent_env() drops the event, so the KOBJ_REMOVE is
never delivered. Instead of clearing before the teardown, the keymap now
stays alive until device_del() has sent the uevent, and the flag is
cleared afterwards - device_del() has drained the sysfs readers by then,
so rc_dev_uevent() cannot run any more.
The patch below is against v7.2.10; I can rebase it on your series in the
media tree if you prefer.
v3:
- Keep the keymap until device_del() has sent the remove uevent instead
of clearing dev->registered before freeing it, so that the KOBJ_REMOVE
event is not suppressed.
- Clear dev->registered after device_del() on the register error paths,
which also covers the lirc_register() and rc_setup_rx_device()
failures.
- Rename the helper to set_unregistered() and drop the explanatory
comment (Sean Young).
v2:
- Clear dev->registered on the rc_setup_rx_device() error path too;
lirc is already registered there and open file descriptors can
still reach driver callbacks.
- Factor the lock/close/clear sequence into rc_unregister_device_state()
and share it with rc_unregister_device().
---
drivers/media/rc/rc-main.c | 26 ++++++++++++++++++++------
1 file changed, 20 insertions(+), 6 deletions(-)
diff --git a/drivers/media/rc/rc-main.c b/drivers/media/rc/rc-main.c
index dda3479ea3ad..ff3149415e45 100644
--- a/drivers/media/rc/rc-main.c
+++ b/drivers/media/rc/rc-main.c
@@ -1888,6 +1888,15 @@ static void rc_free_rx_device(struct rc_dev *dev)
ir_free_table(&dev->rc_map);
}
+static void set_unregistered(struct rc_dev *dev)
+{
+ mutex_lock(&dev->lock);
+ if (dev->users && dev->close)
+ dev->close(dev);
+ dev->registered = false;
+ mutex_unlock(&dev->lock);
+}
+
int rc_register_device(struct rc_dev *dev)
{
const char *path;
@@ -1970,12 +1979,21 @@ int rc_register_device(struct rc_dev *dev)
return 0;
out_rx:
- rc_free_rx_device(dev);
+ /*
+ * The input device has to be unregistered before its parent goes
+ * away, but the keymap is still needed by the uevent attribute
+ * until device_del() has been called.
+ */
+ if (dev->input_dev) {
+ input_unregister_device(dev->input_dev);
+ dev->input_dev = NULL;
+ }
out_lirc:
if (dev->allowed_protocols != RC_PROTO_BIT_CEC)
lirc_unregister(dev);
out_dev:
device_del(&dev->dev);
+ set_unregistered(dev);
out_rx_free:
ir_free_table(&dev->rc_map);
out_raw:
@@ -2024,11 +2042,7 @@ void rc_unregister_device(struct rc_dev *dev)
timer_delete_sync(&dev->timer_keyup);
timer_delete_sync(&dev->timer_repeat);
- mutex_lock(&dev->lock);
- if (dev->users && dev->close)
- dev->close(dev);
- dev->registered = false;
- mutex_unlock(&dev->lock);
+ set_unregistered(dev);
rc_free_rx_device(dev);
--
2.34.1