From nobody Tue Nov 24 16:41:50 2026 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=162.243.164.118 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795339334; b=2oqTVB6RbLZTAMZ8PQz/nYRwGkwlQVJuMeviRZfkAz77w1EmUetmklCT1/coFF0/GML6 ZY2rS31E3r8t/iP0/sT2+gijYKE2zXFl82IW5wlOl6x2kiJJHedGEf/6/Bj1QJsrIigMh te2ai2BIcRoOLGaul+U4WKkMB/Z/+VUDHehaVWxO173uijbiSM8c7xIjmPHyf5vsBbml1 lizIA04tl8LCI9Jx23c9Qr0dplbB02pX9G0tkn3Ljf2e4bWt8kmUyY1CGxYSnqcd2KxMy x8ucsAYD6YXZELK19eYbfZxAqt+RC/VnLfXotJwE1HXLgS9e3pRflbrKZ/+VwBAIyQw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795339334; h=DMARC-Filter:DKIM-Signature:Received:From:To:Cc:Subject:Date: Message-Id:X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding:X-CM-TRANSID:X-Coremail-Antispam: X-CM-SenderInfo; bh=gOkWR6H5HKpidkAAlJBE2KrMxAp78SnPQgYFiW8dD9k=; b=EZqj3FEIW0tsBBcc+EnZ/5EeNXCU/5uQUwSdWbvOJ9Uqy+R+8Gz908R2Ud9Gqr9UIfKk SL00HbY/A3/7tJb5rbGXW9f0TxzGlSr/aa39qFJ7P8i4uUcNVLt/Z0G//1xSCk6bfNf2I lCSIwYqYC/J3W6e2wc0SYMfPlYNh+yUpJQCXnKbl+8DjYolSHF/5VBXOvdtob2quOjtCu CI87TPIirkky8FAK6H2LgkZ9gah0w6sNk2Tt/Em+jex1knD3fsP3xPc0gtDqy6OfMr0+4 gLqeiWnXfc/ofZRzCpGOIkjayoAPEp4raOKuN8IAuzxLZ0eMzigm0LOjIGhiad6H/xg== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=stu.xidian.edu.cn; spf=pass smtp.mailfrom=stu.xidian.edu.cn; arc=none smtp.remote-ip=162.243.164.118 Received: from zg8tmtyylji0my4xnjqumte4.icoremail.net (zg8tmtyylji0my4xnjqumte4.icoremail.net [162.243.164.118]) by smtp.subspace.kernel.org (Postfix) with ESMTP id BDA6F606A320; Sun, 22 Nov 2026 09:22:09 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org BDA6F606A320 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=stu.xidian.edu.cn Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=stu.xidian.edu.cn DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=stu.xidian.edu.cn; s=dkim; h=Received:From:To:Cc:Subject:Date: Message-Id:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding; bh=gOkWR6H5HKpidkAAlJBE2KrMxAp78SnPQg YFiW8dD9k=; b=gaJ6sms7dR4j5XW0mj8jW1UcmoIE/4QUtyWKfLH74PoTqkDXFX CvJjg3s/9Rs1lPMzYTUNhKxagsvgXwV+NWylp5w9Q6hVU074sUkbNdq89rPS4jEu Wm0f6WgnNJUX4qYAQE8BFj2xpDh+YfakGjO9y/VssA2SZBAMezyX2Ud78= Received: from user-virtual-machine.localdomain (unknown [113.200.174.70]) by hzbj-edu-front-3.icoremail.net (Coremail) with UTF8SMTPA id BbQMCkA2PP8ztAJrzxsOAg--.17246S2; Sun, 22 Nov 2026 17:21:56 +0800 (CST) From: YuanZhe Liu <25031212351@stu.xidian.edu.cn> To: Sean Young Cc: Mauro Carvalho Chehab , Mauro Carvalho Chehab , =?UTF-8?q?David=20H=C3=A4rdeman?= , linux-media@vger.kernel.org, linux-kernel@vger.kernel.org, stable@vger.kernel.org, Bin Yu , Cheng Wen Subject: [PATCH v3] media: rc: fix use-after-free of rc_map.name on register error path Date: Sun, 22 Nov 2026 17:21:55 +0800 Message-Id: <20261122092155.11550-1-25031212351@stu.xidian.edu.cn> X-Mailer: git-send-email 2.34.1 In-Reply-To: References: <20261027014144.9144-1-25031212351@stu.xidian.edu.cn> <20261027021718.14264-1-25031212351@stu.xidian.edu.cn> <20261030.pref-04.ec83d43911ee476d@stu.xidian.edu.cn> <20261030130535.2921-1-25031212351@stu.xidian.edu.cn> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-CM-TRANSID: BbQMCkA2PP8ztAJrzxsOAg--.17246S2 X-Coremail-Antispam: 1UD129KBjvJXoWxKrWDKw4rKF13Xr4DKF17Jrb_yoW7ZF4fpa 98Gas8trWDKr4UXwsxXw47Xa45ur4xJws8Zry8G34j9398CryftFWUJFyUWa4UJFyxCFWU tr98X3y8uFWUCFJanT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUU9vb7Iv0xC_Kw4lb4IE77IF4wAFF20E14v26r4j6ryUM7CY07I2 0VC2zVCF04k26cxKx2IYs7xG6rWj6s0DM7CIcVAFz4kK6r1j6r18M28lY4IEw2IIxxk0rw A2F7IY1VAKz4vEj48ve4kI8wA2z4x0Y4vE2Ix0cI8IcVAFwI0_Jr0_JF4l84ACjcxK6xII jxv20xvEc7CjxVAFwI0_Gr0_Cr1l84ACjcxK6I8E87Iv67AKxVW8Jr0_Cr1UM28EF7xvwV C2z280aVCY1x0267AKxVW8Jr0_Cr1UM2vYz4IE04k24VAvwVAKI4IrM2AIxVAIcxkEcVAq 07x20xvEncxIr21l5I8CrVACY4xI64kE6c02F40Ex7xfMcIj6xIIjxv20xvE14v26r1j6r 18McIj6I8E87Iv67AKxVWUJVW8JwAm72CE4IkC6x0Yz7v_Jr0_Gr1lF7xvr2IYc2Ij64vI r41lc7CjxVAaw2AFwI0_Jw0_GFylc2xSY4AK67AK6r4fMxAIw28IcxkI7VAKI48JMxC20s 026xCaFVCjc4AY6r1j6r4UMI8I3I0E5I8CrVAFwI0_Jr0_Jr4lx2IqxVCjr7xvwVAFwI0_ JrI_JrWlx4CE17CEb7AF67AKxVWUtVW8ZwCIc40Y0x0EwIxGrwCI42IY6xIIjxv20xvE14 v26r1j6r1xMIIF0xvE2Ix0cI8IcVCY1x0267AKxVWUJVW8JwCI42IY6xAIw20EY4v20xva j40_Jr0_JF4lIxAIcVC2z280aVAFwI0_Jr0_Gr1lIxAIcVC2z280aVCY1x0267AKxVW8JV W8JrUvcSsGvfC2KfnxnUUI43ZEXa7IU5p5l7UUUUU== X-CM-SenderInfo: ysvqjiysrsjkur6v33wo0lvxldqovvfxof0/1tbiAQUFD2sBs9hVeAABsj Content-Type: text/plain; charset="utf-8" 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@vger.kernel.org Co-developed-by: Bin Yu Signed-off-by: Bin Yu Co-developed-by: Cheng Wen Signed-off-by: Cheng Wen Signed-off-by: YuanZhe Liu <25031212351@stu.xidian.edu.cn> --- 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); } =20 +static void set_unregistered(struct rc_dev *dev) +{ + mutex_lock(&dev->lock); + if (dev->users && dev->close) + dev->close(dev); + dev->registered =3D 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; =20 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 =3D NULL; + } out_lirc: if (dev->allowed_protocols !=3D 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); =20 - mutex_lock(&dev->lock); - if (dev->users && dev->close) - dev->close(dev); - dev->registered =3D false; - mutex_unlock(&dev->lock); + set_unregistered(dev); =20 rc_free_rx_device(dev); =20 --=20 2.34.1