rpmsg: char: take ept_lock in the SET_INCOMING_FLOWCONTROL ioctl
Subsystems (2)
PATCH 1/1 rpmsg: char: take ept_lock in the SET_INCOMING_FLOWCONTROL ioctl
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.
--- drivers/rpmsg/rpmsg_char.c | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-)
drivers/rpmsg/rpmsg_char.c +9 -1
@@ -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. */
Signature · 1 line
2.43.0
Mathieu Poirier
maintainerHi,
Quoted 20 lines · Yu Zhang
rpmsg_eptdev_ioctl()handlesRPMSG_SET_INCOMING_FLOWCONTROLby callingrpmsg_set_flow_control(eptdev->ept, ...)without holdingeptdev->ept_lockand without checkingeptdev->eptfor NULL.rpmsg_set_flow_control()dereferencesept->ops, so this is the onlyeptdev->eptuser that does not honour theept_lockdiscipline.
eptdev->eptis torn down underept_lock:rpmsg_chrdev_eptdev_destroy()(reached viaRPMSG_DESTROY_EPT_IOCTLon a second fd, or when the rpmsg device is removed) andrpmsg_eptdev_release()both domutex_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. ASET_INCOMING_FLOWCONTROLioctl running concurrently can therefore readeptdev->eptjust as it is freed and dereferenceept->ops-> use-after-free (or a NULL dereference once ept has been cleared).
Quoted 5 lines
The other
eptdev->eptusers get this right:rpmsg_eptdev_write_iter()andrpmsg_eptdev_poll()takeept_lockand re-checkeptdev->eptbefore use. Do the same in the ioctl. Lock only this case:RPMSG_DESTROY_EPT_IOCTLcallsrpmsg_chrdev_eptdev_destroy(), which takesept_lockitself, so the switch as a whole must not hold it.
Please get rid of the above paragraph.
Quoted 16 lines
Fixes 5550201c0fe2 ("rpmsg: char: Add RPMSG GET/SET FLOWCONTROL IOCTL support")Signed-off-by Yu Zhang <[email protected]>--- drivers/rpmsg/rpmsg_char.c | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-)drivers/rpmsg/rpmsg_char.c +0
-0@@ -343,7 +343,15 @@ static long rpmsg_eptdev_ioctl(struct file *fp, unsigned int cmd,break ; } set = !!arg;
drivers/rpmsg/rpmsg_char.c static long rpmsg_eptdev_ioctl(struct file *fp, unsigned int cmd, · line 354
Quoted 10 lines
drivers/rpmsg/rpmsg_char.c +9
-1- 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);
ret = mutex_lock_interruptible(&eptdev->ept_lock); if (ret)
break;
if (!eptdev->ept) {
ret = -EPIEP;
break;
}
ret = rpmsg_set_flow_control(eptdev->ept, set,
eptdev->chinfo.dst);
mutex_unlock(&eptdev->ept_lock);Thanks,
Mathieu
Quoted 6 lines
drivers/rpmsg/rpmsg_char.c +0
-0break ;case RPMSG_DESTROY_EPT_IOCTL :/* Don't allow to destroy a default endpoint. */ Signature · 1 line
2.43.0
PATCH v2 1/1 rpmsg: char: take ept_lock in the SET_INCOMING_FLOWCONTROL ioctl
In reply to Yu Zhangrpmsg_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).
---
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/[email protected]/
--- drivers/rpmsg/rpmsg_char.c | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-)
drivers/rpmsg/rpmsg_char.c +14 -1
@@ -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. */
Signature · 1 line
2.43.0
Quoted 30 lines · Yu Zhang
rpmsg_eptdev_ioctl()handlesRPMSG_SET_INCOMING_FLOWCONTROLby callingrpmsg_set_flow_control(eptdev->ept, ...)without holdingeptdev->ept_lockand without checkingeptdev->eptfor NULL.rpmsg_set_flow_control()dereferencesept->ops, so this is the onlyeptdev->eptuser that does not honour theept_lockdiscipline.
eptdev->eptis torn down underept_lock:rpmsg_chrdev_eptdev_destroy()(reached viaRPMSG_DESTROY_EPT_IOCTLon a second fd, or when the rpmsg device is removed) andrpmsg_eptdev_release()both domutex_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. ASET_INCOMING_FLOWCONTROLioctl running concurrently can therefore readeptdev->eptjust as it is freed and dereferenceept->ops-> use-after-free (or a NULL dereference once ept has been cleared).Fixes 5550201c0fe2 ("rpmsg: char: Add RPMSG GET/SET FLOWCONTROL IOCTL support")Signed-off-by Yu Zhang <[email protected]>---
v2:
- Drop the paragraph about the otherept_lockusers 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/[email protected]/
Quoted 4 lines
--- drivers/rpmsg/rpmsg_char.c | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-)
Applied.
Thanks,
Mathieu
Quoted 29 lines
drivers/rpmsg/rpmsg_char.c +14
-1@@ -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. */ Signature · 1 line
2.43.0