mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Sean Young <sean@mess.org>
To: YuanZhe Liu <25031212351@stu.xidian.edu.cn>
Cc: "Mauro Carvalho Chehab" <mchehab@kernel.org>,
	"David H�rdeman" <david@hardeman.nu>,
	linux-media@vger.kernel.org, linux-kernel@vger.kernel.org,
	stable@vger.kernel.org, "Bin Yu" <byu@xidian.edu.cn>,
	"Cheng Wen" <wencheng@xidian.edu.cn>
Subject: Re: [PATCH v2] media: rc: fix use-after-free of rc_map.name on register error path
Date: Sat, 21 Nov 2026 21:52:04 +0000	[thread overview]
Message-ID: <awIShD1k89rEsIc5@extorris.mess.org> (raw)
In-Reply-To: <20261027014144.9144-1-25031212351@stu.xidian.edu.cn>

Hi,

Apologies for the delay in getting this reviewed. I've been very busy
lately, things should return to normal soon.

On Tue, Oct 27, 2026 at 09:41:44AM +0800, YuanZhe Liu wrote:
> 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
> 
> Fix it the same way rc_unregister_device() does: take dev->lock,
> close the input device if it is still open and clear
> dev->registered before releasing any resources. rc_dev_uevent()
> then observes the cleared flag and returns -ENODEV instead of
> reading rc_map.name. The same sequence is also needed when
> rc_setup_rx_device() fails: lirc has been registered by then, so
> an open /dev/lircX file descriptor would otherwise keep calling
> driver callbacks that access the torn down device.
> 
> Fixes: f56928abaa6d ("[media] rc-core: cleanup rc_register_device")
> Cc: stable@vger.kernel.org
> Co-developed-by: Bin Yu <byu@xidian.edu.cn>
> Signed-off-by: Bin Yu <byu@xidian.edu.cn>
> Co-developed-by: Cheng Wen <wencheng@xidian.edu.cn>
> Signed-off-by: Cheng Wen <wencheng@xidian.edu.cn>
> Signed-off-by: YuanZhe Liu <25031212351@stu.xidian.edu.cn>
> 
> ---
> 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 | 29 +++++++++++++++++++++++------
>  1 file changed, 23 insertions(+), 6 deletions(-)
> 
> diff --git a/drivers/media/rc/rc-main.c b/drivers/media/rc/rc-main.c
> index dda3479ea3ad..3ac5d532c6dc 100644
> --- a/drivers/media/rc/rc-main.c
> +++ b/drivers/media/rc/rc-main.c
> @@ -1888,6 +1888,24 @@ static void rc_free_rx_device(struct rc_dev *dev)
>  	ir_free_table(&dev->rc_map);
>  }
>  
> +/*
> + * Prevent userspace from accessing a device that is being taken down.
> + *
> + * The uevent attribute, the lirc character device and the input device
> + * all check dev->registered under dev->lock before using driver data.
> + * Clear it while still holding that lock, and call dev->close() for an
> + * input device that is still open, exactly as rc_unregister_device()
> + * does, before the resources they use are freed.
> + */

This comment is not really useful, might was as well delete it. It's just
part of the teardown.

> +static void rc_unregister_device_state(struct rc_dev *dev)

I am not sure rc_unregister_device_state() is a good name. How about
something like set_unregistered()? Since it's a static function it does
not need the rc_ prefix.

> +{
> +	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;
> @@ -1954,8 +1972,10 @@ int rc_register_device(struct rc_dev *dev)
>  
>  	if (dev->driver_type != RC_DRIVER_IR_RAW_TX) {
>  		rc = rc_setup_rx_device(dev);
> -		if (rc)
> +		if (rc) {
> +			rc_unregister_device_state(dev);
>  			goto out_lirc;
> +		}
>  	}

Above this code there is a call to lirc_register() which also has a
goto out_lirc; error path. Unless I'm missing something, this should
also have a rc_unregister_device_state() in its error path.

>  
>  	if (dev->driver_type == RC_DRIVER_IR_RAW) {
> @@ -1970,6 +1990,7 @@ int rc_register_device(struct rc_dev *dev)
>  	return 0;
>  
>  out_rx:
> +	rc_unregister_device_state(dev);
>  	rc_free_rx_device(dev);
>  out_lirc:
>  	if (dev->allowed_protocols != RC_PROTO_BIT_CEC)
> @@ -2024,11 +2045,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);
> +	rc_unregister_device_state(dev);
>  
>  	rc_free_rx_device(dev);
>  
> -- 
> 2.34.1
> 

Thanks,

Sean

      parent reply	other threads:[~2026-11-21 21:52 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-10-27  1:41 YuanZhe Liu
2026-10-27  2:17 ` YuanZhe Liu
2026-10-30  6:00   ` YuanZhe Liu
2026-10-30 10:06     ` Sean Young
2026-10-30 13:05       ` YuanZhe Liu
2026-11-21 21:52 ` Sean Young [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=awIShD1k89rEsIc5@extorris.mess.org \
    --to=sean@mess.org \
    --cc=25031212351@stu.xidian.edu.cn \
    --cc=byu@xidian.edu.cn \
    --cc=david@hardeman.nu \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-media@vger.kernel.org \
    --cc=mchehab@kernel.org \
    --cc=stable@vger.kernel.org \
    --cc=wencheng@xidian.edu.cn \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®