Skip to contentSkip to conversation

cobalt: always check userspace read/write validity

PATCH In review Jan Kiszka 2 participants 7 messages Jan 12, 2025 – Jan 13, 2025

lore
Share & tools
lore
author
#1 lore raw

PATCH 1/3 testsuite/smokey/y2038: Always present valid u_ret to monitor_wait64

In reply to Jan Kiszka

From: Jan Kiszka <[email protected]>

So far, this parameter was not check for validity, but this will change, and checking that is not in scope for this test case.

---
 testsuite/smokey/y2038/syscall-tests.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
testsuite/smokey/y2038/syscall-tests.c +3 -3
index b452b642bc..29e9c66c0a 100644
@@ -831,7 +831,7 @@ static int test_sc_cobalt_monitor_wait64(void)
 		return -errno;
 
 	/* Make sure we don't crash because of NULL pointers */
-	ret = XENOMAI_SYSCALL4(sc_nr, NULL, NULL, NULL, NULL);
+	ret = XENOMAI_SYSCALL4(sc_nr, NULL, NULL, NULL, &opret);
 	if (ret == -ENOSYS) {
 		smokey_note("monitor_wait64: skipped. (no kernel support)");
 		return 0; // Not implemented, nothing to test, success
@@ -841,14 +841,14 @@ static int test_sc_cobalt_monitor_wait64(void)
 
 	/* Providing an invalid address has to deliver EFAULT */
 	ret = XENOMAI_SYSCALL4(sc_nr, &mon, COBALT_MONITOR_WAITGRANT,
-			       (void *)0xdeadbeefUL, NULL);
+			       (void *)0xdeadbeefUL, &opret);
 	if (!smokey_assert(ret == -EFAULT))
 		return ret ? ret : -EINVAL;
 
 	/* providing an invalid timeout has to deliver EINVAL */
 	t1.tv_sec = -1;
 	ret = XENOMAI_SYSCALL4(sc_nr, &mon, COBALT_MONITOR_WAITGRANT, &t1,
-			       NULL);
+			       &opret);
 	if (!smokey_assert(ret == -EINVAL))
 		return ret ? ret : -EINVAL;
 
Signature · 1 line
2.43.0

Jan Kiszka

author
#0 lore raw

cover cobalt: always check userspace read/write validity

This adds a few missing access_ok and get/put_user return code evaluations. Along that, the register corruption seen since kernel 6.11 is avoided.

Jan

Jan Kiszka (3):
testsuite/smokey/y2038: Always present valid u_ret to monitor_wait64
cobalt: Add missing access_ok checks
cobalt: Add missing evaluations of __xn_get/put_user return codes

 kernel/cobalt/posix/cond.c             | 9 ++++++---
 kernel/cobalt/posix/io.c               | 2 ++
 kernel/cobalt/posix/monitor.c          | 3 ++-
 kernel/cobalt/posix/mqueue.c           | 3 ++-
 kernel/cobalt/posix/process.c          | 5 ++++-
 kernel/cobalt/posix/syscall32.c        | 2 ++
 testsuite/smokey/y2038/syscall-tests.c | 6 +++---
 7 files changed, 21 insertions(+), 9 deletions(-)
Signature · 1 line
2.43.0
author
#2 lore raw

PATCH 2/3 cobalt: Add missing access_ok checks

From: Jan Kiszka <[email protected]>

These ensure that we are not using an invalid address range, possibly writing to kernel space.

---
 kernel/cobalt/posix/io.c        | 2 ++
 kernel/cobalt/posix/mqueue.c    | 3 ++-
 kernel/cobalt/posix/syscall32.c | 2 ++
 3 files changed, 6 insertions(+), 1 deletion(-)
kernel/cobalt/posix/io.c +2 -0
index e581ec6db8..722f1ce20c 100644
@@ -146,6 +146,8 @@ static int put_mmsglen(void __user **u_mmsg_p, const struct mmsghdr *mmsg)
 	struct mmsghdr __user **p = (struct mmsghdr **)u_mmsg_p,
 		*q __user = (*p)++;
 
+	if (!access_ok(&q->msg_len, sizeof(q->msg_len)))
+		return -EFAULT;
 	return __xn_put_user(mmsg->msg_len, &q->msg_len);
 }
 
kernel/cobalt/posix/mqueue.c +2 -1
index 853aa6cad8..34776fe6cd 100644
@@ -1031,7 +1031,8 @@ int __cobalt_mq_timedreceive(mqd_t uqd, void __user *u_buf,
 
 	cobalt_mqd_put(mqd);
 
-	if (u_prio && __xn_put_user(prio, u_prio))
+	if (u_prio && (!access_ok(u_prio, sizeof(*u_prio)) ||
+		       __xn_put_user(prio, u_prio)))
 		return -EFAULT;
 
 	return 0;
kernel/cobalt/posix/syscall32.c +2 -0
index 793bde1388..484f059844 100644
@@ -605,6 +605,8 @@ static int put_mmsglen32(void __user **u_mmsg_p, const struct mmsghdr *mmsg)
 	struct compat_mmsghdr __user **p = (struct compat_mmsghdr **)u_mmsg_p,
 		*q __user = (*p)++;
 
+	if (!access_ok(&q->msg_len, sizeof(q->msg_len)))
+		return -EFAULT;
 	return __xn_put_user(mmsg->msg_len, &q->msg_len);
 }
 
Signature · 1 line
2.43.0
author
#6 lore raw

PATCH 3/3 cobalt: Add missing evaluations of __xn_get/put_user return codes

In reply to Jan Kiszka

From: Jan Kiszka <[email protected]>

We should always give userspace feedback on the validity of a provided pointer, rather than silently swallowing errors. Except for cobalt_map_user, this includes adding an access_ok check as well.

As a side-effect, this resolves a compiler issue caused by [1] in __cobalt_monitor_wait: At least gcc 12 on arm64 manages to skip an important instruction during fixup of _[_xn]_put_user that causes a register corruption down the road [2]. That is triggered by us ignoring the return value, thus making the compiler drop the asm-goto block for it which would have contained that critical instruction as well (as duplicate who knows why). While this may very well be a compiler bug, the triggering pattern of us remains invalid.

[1] https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=7fd298d4b39d8d5fe99d56811a7ed78c7a5377d5
[2] https://lore.kernel.org/xenomai/[email protected]

---
 kernel/cobalt/posix/cond.c    | 9 ++++++---
 kernel/cobalt/posix/monitor.c | 3 ++-
 kernel/cobalt/posix/process.c | 5 ++++-
 3 files changed, 12 insertions(+), 5 deletions(-)
kernel/cobalt/posix/cond.c +6 -3
index cd9cf0cb98..2a1904c2c5 100644
@@ -309,7 +309,9 @@ int __cobalt_cond_wait_prologue(struct cobalt_cond_shadow __user *u_cnd,
 		return -EINVAL;
 
 	if (cond->mutex == NULL) {
-		__xn_get_user(offset, &u_mx->state_offset);
+		if (!access_ok(&u_mx->state_offset, sizeof(u_mx->state_offset))
+		    || __xn_get_user(offset, &u_mx->state_offset))
+			return -EFAULT;
 		cond->state->mutex_state_offset = offset;
 	}
 
@@ -348,8 +350,9 @@ int __cobalt_cond_wait_prologue(struct cobalt_cond_shadow __user *u_cnd,
 	if (cond->mutex == NULL)
 		cond->state->mutex_state_offset = ~0U;
 
-	if (err == -EINTR)
-		__xn_put_user(d.err, u_err);
+	if (err == -EINTR &&
+	    (!access_ok(u_err, sizeof(*u_err)) || __xn_put_user(d.err, u_err)))
+		err = -EFAULT;
 
 	return err == 0 ? perr : err;
 }
kernel/cobalt/posix/monitor.c +2 -1
index fb7fb27e94..9ce75f4bce 100644
@@ -289,7 +289,8 @@ int __cobalt_monitor_wait(struct cobalt_monitor_shadow __user *u_mon,
 out:
 	xnlock_put_irqrestore(&nklock, s);
 
-	__xn_put_user(opret, u_ret);
+	if (!access_ok(u_ret, sizeof(*u_ret)) || __xn_put_user(opret, u_ret))
+		ret = -EFAULT;
 
 	return ret;
 }
kernel/cobalt/posix/process.c +4 -1
index 8b4efcab40..f7cf97c398 100644
@@ -617,8 +617,11 @@ int cobalt_map_user(struct xnthread *thread, __u32 __user *u_winoff)
 	if (u_window == NULL)
 		return -ENOMEM;
 
+	if (__xn_put_user(cobalt_umm_offset(umm, u_window), u_winoff)) {
+		cobalt_umm_free(umm, u_window);
+		return -EFAULT;
+	}
 	thread->u_window = u_window;
-	__xn_put_user(cobalt_umm_offset(umm, u_window), u_winoff);
 	xnthread_pin_initial(thread);
 
 	/*
Signature · 1 line
2.43.0
#3 lore raw

Re: [PATCH 2/3] cobalt: Add missing access_ok checks

In reply to Jan Kiszka
Quoted 43 lines · Jan Kiszka

From: Jan Kiszka <[email protected]>

These ensure that we are not using an invalid address range, possibly writing to kernel space.

---
 kernel/cobalt/posix/io.c        | 2 ++
 kernel/cobalt/posix/mqueue.c    | 3 ++-
 kernel/cobalt/posix/syscall32.c | 2 ++
 3 files changed, 6 insertions(+), 1 deletion(-)
kernel/cobalt/posix/io.c +2 -0
index e581ec6db8..722f1ce20c 100644
@@ -146,6 +146,8 @@ static int put_mmsglen(void __user **u_mmsg_p, const struct mmsghdr *mmsg)
 	struct mmsghdr __user **p = (struct mmsghdr **)u_mmsg_p,
 		*q __user = (*p)++;
 
+	if (!access_ok(&q->msg_len, sizeof(q->msg_len)))
+		return -EFAULT;
 	return __xn_put_user(mmsg->msg_len, &q->msg_len);
 }
 
kernel/cobalt/posix/mqueue.c +2 -1
index 853aa6cad8..34776fe6cd 100644
@@ -1031,7 +1031,8 @@ int __cobalt_mq_timedreceive(mqd_t uqd, void __user *u_buf,
 
 	cobalt_mqd_put(mqd);
 
-	if (u_prio && __xn_put_user(prio, u_prio))
+	if (u_prio && (!access_ok(u_prio, sizeof(*u_prio)) ||
+		       __xn_put_user(prio, u_prio)))
 		return -EFAULT;
 
 	return 0;
kernel/cobalt/posix/syscall32.c +0 -0
index 793bde1388..484f059844 100644

kernel/cobalt/posix/syscall32.c static int put_mmsglen32(void __user **u_mmsg_p, const struct mmsghdr *mmsg) · line 611

Quoted 7 lines
kernel/cobalt/posix/syscall32.c +2 -0
@@ -605,6 +605,8 @@ static int put_mmsglen32(void __user **u_mmsg_p, const struct mmsghdr *mmsg)
 	struct compat_mmsghdr __user **p = (struct compat_mmsghdr **)u_mmsg_p,
 		*q __user = (*p)++;
 
+	if (!access_ok(&q->msg_len, sizeof(q->msg_len)))
+		return -EFAULT;
 	return __xn_put_user(mmsg->msg_len, &q->msg_len);
 }

That all looks OK, but I think we have two more missing access_ok() checks:

posix/cond.c: __cobalt_cond_wait_prologue()
posix/monitor.c: __cobalt_monitor_wait()

Quoted 3 lines
Signature · 2 lines
-- 
2.43.0
#4 lore raw

Re: [PATCH 2/3] cobalt: Add missing access_ok checks

Quoted 55 lines · Florian Bezdeka
Quoted 43 lines · Jan Kiszka

From: Jan Kiszka <[email protected]>

These ensure that we are not using an invalid address range, possibly writing to kernel space.

---
 kernel/cobalt/posix/io.c        | 2 ++
 kernel/cobalt/posix/mqueue.c    | 3 ++-
 kernel/cobalt/posix/syscall32.c | 2 ++
 3 files changed, 6 insertions(+), 1 deletion(-)
kernel/cobalt/posix/io.c +2 -0
index e581ec6db8..722f1ce20c 100644
@@ -146,6 +146,8 @@ static int put_mmsglen(void __user **u_mmsg_p, const struct mmsghdr *mmsg)
 	struct mmsghdr __user **p = (struct mmsghdr **)u_mmsg_p,
 		*q __user = (*p)++;
 
+	if (!access_ok(&q->msg_len, sizeof(q->msg_len)))
+		return -EFAULT;
 	return __xn_put_user(mmsg->msg_len, &q->msg_len);
 }
 
kernel/cobalt/posix/mqueue.c +2 -1
index 853aa6cad8..34776fe6cd 100644
@@ -1031,7 +1031,8 @@ int __cobalt_mq_timedreceive(mqd_t uqd, void __user *u_buf,
 
 	cobalt_mqd_put(mqd);
 
-	if (u_prio && __xn_put_user(prio, u_prio))
+	if (u_prio && (!access_ok(u_prio, sizeof(*u_prio)) ||
+		       __xn_put_user(prio, u_prio)))
 		return -EFAULT;
 
 	return 0;
kernel/cobalt/posix/syscall32.c +0 -0
index 793bde1388..484f059844 100644

kernel/cobalt/posix/syscall32.c static int put_mmsglen32(void __user **u_mmsg_p, const struct mmsghdr *mmsg) · line 611

Quoted 7 lines
kernel/cobalt/posix/syscall32.c +2 -0
@@ -605,6 +605,8 @@ static int put_mmsglen32(void __user **u_mmsg_p, const struct mmsghdr *mmsg)
 	struct compat_mmsghdr __user **p = (struct compat_mmsghdr **)u_mmsg_p,
 		*q __user = (*p)++;
 
+	if (!access_ok(&q->msg_len, sizeof(q->msg_len)))
+		return -EFAULT;
 	return __xn_put_user(mmsg->msg_len, &q->msg_len);
 }

That all looks OK, but I think we have two more missing access_ok() checks:

Quoted 2 lines

posix/cond.c: __cobalt_cond_wait_prologue()
posix/monitor.c: __cobalt_monitor_wait()

Just realized that you folded that into 3/3...

Fine but slightly mixed, but as it touches the same code...

Quoted 4 lines
Quoted 4 lines
Signature · 2 lines
-- 
2.43.0
author
#5 lore raw

Re: [PATCH 2/3] cobalt: Add missing access_ok checks

Quoted 56 lines · Florian Bezdeka
Quoted 55 lines · Florian Bezdeka
Quoted 43 lines · Jan Kiszka

From: Jan Kiszka <[email protected]>

These ensure that we are not using an invalid address range, possibly writing to kernel space.

---
 kernel/cobalt/posix/io.c        | 2 ++
 kernel/cobalt/posix/mqueue.c    | 3 ++-
 kernel/cobalt/posix/syscall32.c | 2 ++
 3 files changed, 6 insertions(+), 1 deletion(-)
kernel/cobalt/posix/io.c +2 -0
index e581ec6db8..722f1ce20c 100644
@@ -146,6 +146,8 @@ static int put_mmsglen(void __user **u_mmsg_p, const struct mmsghdr *mmsg)
 	struct mmsghdr __user **p = (struct mmsghdr **)u_mmsg_p,
 		*q __user = (*p)++;
 
+	if (!access_ok(&q->msg_len, sizeof(q->msg_len)))
+		return -EFAULT;
 	return __xn_put_user(mmsg->msg_len, &q->msg_len);
 }
 
kernel/cobalt/posix/mqueue.c +2 -1
index 853aa6cad8..34776fe6cd 100644
@@ -1031,7 +1031,8 @@ int __cobalt_mq_timedreceive(mqd_t uqd, void __user *u_buf,
 
 	cobalt_mqd_put(mqd);
 
-	if (u_prio && __xn_put_user(prio, u_prio))
+	if (u_prio && (!access_ok(u_prio, sizeof(*u_prio)) ||
+		       __xn_put_user(prio, u_prio)))
 		return -EFAULT;
 
 	return 0;
kernel/cobalt/posix/syscall32.c +0 -0
index 793bde1388..484f059844 100644

kernel/cobalt/posix/syscall32.c static int put_mmsglen32(void __user **u_mmsg_p, const struct mmsghdr *mmsg) · line 611

Quoted 7 lines
kernel/cobalt/posix/syscall32.c +2 -0
@@ -605,6 +605,8 @@ static int put_mmsglen32(void __user **u_mmsg_p, const struct mmsghdr *mmsg)
 	struct compat_mmsghdr __user **p = (struct compat_mmsghdr **)u_mmsg_p,
 		*q __user = (*p)++;
 
+	if (!access_ok(&q->msg_len, sizeof(q->msg_len)))
+		return -EFAULT;
 	return __xn_put_user(mmsg->msg_len, &q->msg_len);
 }

That all looks OK, but I think we have two more missing access_ok() checks:

Quoted 7 lines
Quoted 2 lines

posix/cond.c: __cobalt_cond_wait_prologue()
posix/monitor.c: __cobalt_monitor_wait()

Just realized that you folded that into 3/3...

Fine but slightly mixed, but as it touches the same code...

The spots in patch 3 were missing any return code check, not just access_ok. Therefore the separation.

Jan

Signature · 2 lines
Siemens AG, Foundational Technologies
Linux Expert Center