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). The other eptdev->ept users get this right: rpmsg_eptdev_write_iter() and rpmsg_eptdev_poll() take ept_lock and re-check eptdev->ept before use. Do the same in the ioctl. Lock only this case: RPMSG_DESTROY_EPT_IOCTL calls rpmsg_chrdev_eptdev_destroy(), which takes ept_lock itself, so the switch as a whole must not hold it. Fixes: 5550201c0fe2 ("rpmsg: char: Add RPMSG GET/SET FLOWCONTROL IOCTL support") Cc: stable@vger.kernel.org Signed-off-by: Yu Zhang --- drivers/rpmsg/rpmsg_char.c | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/drivers/rpmsg/rpmsg_char.c b/drivers/rpmsg/rpmsg_char.c index 550fda2..49c0784 100644 --- a/drivers/rpmsg/rpmsg_char.c +++ b/drivers/rpmsg/rpmsg_char.c @@ -343,7 +343,15 @@ 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) + ret = rpmsg_set_flow_control(eptdev->ept, set, + eptdev->chinfo.dst); + else + ret = -EPIPE; + mutex_unlock(&eptdev->ept_lock); break; case RPMSG_DESTROY_EPT_IOCTL: /* Don't allow to destroy a default endpoint. */ -- 2.43.0