Re: [PATCH v2] rpmsg: char: take ept_lock in the SET_INCOMING_FLOWCONTROL ioctl
From: Mathieu Poirier
Date: Thu Nov 05 2026 - 12:03:17 EST
On Sat, Oct 31, 2026 at 07:06:50PM +1000, Yu Zhang wrote:
> rpmsg_eptdev_ioctl() handles RPMSG_SET_INCOMING_FLOWCONTROL by calling
> rpmsg_set_flow_control(eptdev->ept, ...) without holding eptdev->ept_lock
> and without checking eptdev->ept for NULL. rpmsg_set_flow_control()
> dereferences ept->ops, so this is the only eptdev->ept user that does not
> honour the ept_lock discipline.
>
> eptdev->ept is torn down under ept_lock: rpmsg_chrdev_eptdev_destroy()
> (reached via RPMSG_DESTROY_EPT_IOCTL on a second fd, or when the rpmsg
> device is removed) and rpmsg_eptdev_release() both do
>
> mutex_lock(&eptdev->ept_lock);
> rpmsg_destroy_ept(eptdev->ept);
> eptdev->ept = NULL;
> mutex_unlock(&eptdev->ept_lock);
>
> and rpmsg_destroy_ept() drops the endpoint's last reference and frees it.
> A SET_INCOMING_FLOWCONTROL ioctl running concurrently can therefore read
> eptdev->ept just as it is freed and dereference ept->ops -> use-after-free
> (or a NULL dereference once ept has been cleared).
>
> Fixes: 5550201c0fe2 ("rpmsg: char: Add RPMSG GET/SET FLOWCONTROL IOCTL support")
> Cc: stable@xxxxxxxxxxxxxxx
> Signed-off-by: Yu Zhang <yuz08559@xxxxxxxxx>
> ---
> v2:
> - Drop the paragraph about the other ept_lock users from the commit
> message (Mathieu).
> - Bail out early when the endpoint is already gone instead of an
> if/else around the call (Mathieu).
> v1: https://lore.kernel.org/all/20261026100306.68019-1-yuz08559@xxxxxxxxx/
> ---
> drivers/rpmsg/rpmsg_char.c | 15 ++++++++++++++-
> 1 file changed, 14 insertions(+), 1 deletion(-)
>
Applied.
Thanks,
Mathieu
> diff --git a/drivers/rpmsg/rpmsg_char.c b/drivers/rpmsg/rpmsg_char.c
> index 550fda2..d8868fa 100644
> --- a/drivers/rpmsg/rpmsg_char.c
> +++ b/drivers/rpmsg/rpmsg_char.c
> @@ -343,7 +343,20 @@ static long rpmsg_eptdev_ioctl(struct file *fp, unsigned int cmd,
> break;
> }
> set = !!arg;
> - ret = rpmsg_set_flow_control(eptdev->ept, set, eptdev->chinfo.dst);
> +
> + ret = mutex_lock_interruptible(&eptdev->ept_lock);
> + if (ret)
> + break;
> +
> + if (!eptdev->ept) {
> + mutex_unlock(&eptdev->ept_lock);
> + ret = -EPIPE;
> + break;
> + }
> +
> + ret = rpmsg_set_flow_control(eptdev->ept, set,
> + eptdev->chinfo.dst);
> + mutex_unlock(&eptdev->ept_lock);
> break;
> case RPMSG_DESTROY_EPT_IOCTL:
> /* Don't allow to destroy a default endpoint. */
> --
> 2.43.0
>