[PATCH v4] media: rc: fix use-after-free of rc_map.name on register error path

From: YuanZhe Liu

Date: Sun Nov 22 2026 - 05:52:34 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.

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 is not enough: it makes
rc_dev_uevent() return -ENODEV, dev_uevent() fails and kobject_uevent_env()
drops the event, so the KOBJ_REMOVE of device_del() is never delivered.
The device has already been announced with KOBJ_ADD, so the remove event
has to be sent as well, and it needs the keymap and the driver strings.

Keep the keymap alive until device_del() has been called instead: only
unregister the input device at out_rx, since it is a child of dev->dev,
and let out_free_table 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.

dev->registered is still cleared so that open lirc file descriptors
cannot reach the driver callbacks, but only after device_del() has sent
the remove uevent.

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,

this is a rebase of v3 on the media tree: media-ci rejected v3 because it
was not based on a media-committers branch. I have based it on fixes;
the function names and the structure follow what is there now, so the
helper from v2 is gone and the clear after device_del() folds into the
existing scoped_guard.

The rest of the media-ci report (coccinelle, smatch, sparse and the rc
keymap checks) comes from the tree itself, none of it touches rc-main.c.

Thanks,
YuanZhe

v4:
- Rebased on the media-committers fixes branch, as media-ci requires a
media tree ancestor (v3 was based on v7.2.10).
- The keymap is kept until device_del() has sent the remove uevent, so
the KOBJ_REMOVE is not suppressed by rc_dev_uevent() returning -ENODEV.

v3:
- Keep the keymap until the remove uevent has been sent instead of
clearing dev->registered before freeing it.
- Clear dev->registered after device_del() on the register error paths.
- Rename the helper to set_unregistered() and drop the explanatory
comment (Sean Young).
---
drivers/media/rc/rc-main.c | 15 ++++++++++-----
1 file changed, 10 insertions(+), 5 deletions(-)

diff --git a/drivers/media/rc/rc-main.c b/drivers/media/rc/rc-main.c
index a867bf9e8416..a2a6ad2a7b0c 100644
--- a/drivers/media/rc/rc-main.c
+++ b/drivers/media/rc/rc-main.c
@@ -2013,14 +2013,19 @@ int rc_register_device(struct rc_dev *dev)
return 0;

out_rx:
- rc_free_rx_device(dev);
+ /*
+ * The input device is a child of dev->dev, so it has to be
+ * unregistered before device_del(); the keymap is still needed
+ * by the uevent attribute until then.
+ */
+ if (dev->input_dev)
+ input_unregister_device(dev->input_dev);
out_lirc:
- scoped_guard(mutex, &dev->lock)
- dev->registered = false;
-
lirc_unregister(dev);
device_del(&dev->dev);
- /* registered already cleared above */
+
+ scoped_guard(mutex, &dev->lock)
+ dev->registered = false;
goto out_free_table;
out_rx_free:
scoped_guard(mutex, &dev->lock)
--
2.34.1