Skip to contentSkip to conversation

ipc/sem: check nsops overflow in do_semtimedop()

PATCH In review Yi Xie 2 participants 2 messages Jun 29

lore
Share & tools
lore

Yi Xie

author
#0 lore raw

PATCH 1/1 ipc/sem: check nsops overflow in do_semtimedop()

do_semtimedop() caps nsops against sc_semopm, but still multiplies it by sizeof(struct sembuf) when copying from userspace. That product can overflow, so copy_from_user() may not match what kvmalloc_objs() allocated.

Use check_mul_overflow() and return -E2BIG on overflow.

---
 ipc/sem.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
ipc/sem.c +5 -1
index 5ec41de7e85b..791a0505923b 100644
@@ -86,6 +86,7 @@
 #include <linux/ipc_namespace.h>
 #include <linux/sched/wake_q.h>
 #include <linux/nospec.h>
+#include <linux/overflow.h>
 #include <linux/rhashtable.h>
 
 #include <linux/uaccess.h>
@@ -2225,6 +2226,7 @@ static long do_semtimedop(int semid, struct sembuf __user *tsops,
 	struct sembuf fast_sops[SEMOPM_FAST];
 	struct sembuf *sops = fast_sops;
 	struct ipc_namespace *ns;
+	size_t sops_bytes;
 	int ret;
 
 	ns = current->nsproxy->ipc_ns;
@@ -2232,6 +2234,8 @@ static long do_semtimedop(int semid, struct sembuf __user *tsops,
 		return -E2BIG;
 	if (nsops < 1)
 		return -EINVAL;
+	if (check_mul_overflow(nsops, sizeof(*sops), &sops_bytes))
+		return -E2BIG;
 
 	if (nsops > SEMOPM_FAST) {
 		sops = kvmalloc_objs(*sops, nsops);
@@ -2239,7 +2243,7 @@ static long do_semtimedop(int semid, struct sembuf __user *tsops,
 			return -ENOMEM;
 	}
 
-	if (copy_from_user(sops, tsops, nsops * sizeof(*tsops))) {
+	if (copy_from_user(sops, tsops, sops_bytes)) {
 		ret =  -EFAULT;
 		goto out_free;
 	}
Signature · 1 line
2.25.1
#1 lore raw
Quoted 3 lines · Yi Xie

do_semtimedop() caps nsops against sc_semopm, but still multiplies it by sizeof(struct sembuf) when copying from userspace. That product can overflow, so copy_from_user() may not match what kvmalloc_objs() allocated.

This is just incorrect, becasue for copy_from_user() to happen kvmalloc_objs() would be called -> __alloc_objs() -> size_mul() (saturates) -> -ENOMEM.

Quoted 21 lines

Use check_mul_overflow() and return -E2BIG on overflow.

---
 ipc/sem.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
ipc/sem.c +1 -0
index 5ec41de7e85b..791a0505923b 100644
@@ -86,6 +86,7 @@
 #include <linux/ipc_namespace.h>
 #include <linux/sched/wake_q.h>
 #include <linux/nospec.h>
+#include <linux/overflow.h>
 #include <linux/rhashtable.h>
 
 #include <linux/uaccess.h>

ipc/sem.c static long do_semtimedop(int semid, struct sembuf __user *tsops, · line 2229

Quoted 4 lines
ipc/sem.c +1 -0
@@ -2225,6 +2226,7 @@ static long do_semtimedop(int semid, struct sembuf __user *tsops,
 	struct sembuf fast_sops[SEMOPM_FAST];
 	struct sembuf *sops = fast_sops;
 	struct ipc_namespace *ns;
+	size_t sops_bytes;

Maybe just len or size?

ipc/sem.c static long do_semtimedop(int semid, struct sembuf __user *tsops, · line 2238

Quoted 9 lines
ipc/sem.c +2 -0
 	int ret;
 
 	ns = current->nsproxy->ipc_ns;
@@ -2232,6 +2234,8 @@ static long do_semtimedop(int semid, struct sembuf __user *tsops,
 		return -E2BIG;
 	if (nsops < 1)
 		return -EINVAL;
+	if (check_mul_overflow(nsops, sizeof(*sops), &sops_bytes))
+		return -E2BIG;

struct sembuf is 6 bytes is size, so you'd need some truly enormous ns->sc_semopm (defaults to SEMOPM == 500) and nsops to get an overflow here, and it'd only be the case on 32-bit systems also.

But in any case as per above,

kvmalloc_objs() -> __alloc_objs() -> size_mul() (saturates) -> -ENOMEM.

So this check is redundant.

Quoted 13 lines
ipc/sem.c +1 -2
 	if (nsops > SEMOPM_FAST) {
 		sops = kvmalloc_objs(*sops, nsops);
@@ -2239,7 +2243,7 @@ static long do_semtimedop(int semid, struct sembuf __user *tsops,
 			return -ENOMEM;
 	}
 
-	if (copy_from_user(sops, tsops, nsops * sizeof(*tsops))) {
+	if (copy_from_user(sops, tsops, sops_bytes)) {
 		ret =  -EFAULT;
 		goto out_free;
 	}
--
Quoted 2 lines

2.25.1

Thanks, Lorenzo