Skip to contentSkip to conversation

rpmsg: char: take ept_lock in the SET_INCOMING_FLOWCONTROL ioctl

PATCH Superseded Yu Zhang 2 participants 4 messages Oct 26 – Nov 5

Subsystems (2)
lore
Share & tools
lore

Yu Zhang

author
#0 lore raw

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.

Fixes 5550201c0fe2 ("rpmsg: char: Add RPMSG GET/SET FLOWCONTROL IOCTL support")
---
 drivers/rpmsg/rpmsg_char.c | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)
drivers/rpmsg/rpmsg_char.c +9 -1
index 550fda2..49c0784 100644
@@ -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
maintainer
#1 lore raw

Hi,

Quoted 20 lines · Yu Zhang

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).

Quoted 5 lines

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.

Please get rid of the above paragraph.

Quoted 16 lines
Fixes 5550201c0fe2 ("rpmsg: char: Add RPMSG GET/SET FLOWCONTROL IOCTL support")
---
 drivers/rpmsg/rpmsg_char.c | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)
drivers/rpmsg/rpmsg_char.c +0 -0
index 550fda2..49c0784 100644
@@ -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 -0
 		break;
 	case RPMSG_DESTROY_EPT_IOCTL:
 		/* Don't allow to destroy a default endpoint. */
Signature · 1 line
2.43.0
author
#2 lore raw

PATCH v2 1/1 rpmsg: char: take ept_lock in the SET_INCOMING_FLOWCONTROL ioctl

In reply to Yu Zhang

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")

---
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
index 550fda2..d8868fa 100644
@@ -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
maintainer Applied
#3 lore raw
Quoted 30 lines · Yu Zhang

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")

---
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]/

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
index 550fda2..d8868fa 100644
@@ -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