Skip to contentSkip to conversation

Problems with upstream SPECTRE mitigation found in sendmsg/recvmsg syscalls

PATCH In review François Legal 3 participants 6 messages Dec 7, 2020 – Dec 11, 2020

lore
Share & tools
lore
#0 lore raw

PATCH 1/5 Problems with upstream SPECTRE mitigation found in sendmsg/recvmsg syscalls

From: François LEGAL <[email protected]>

Remove the copy of struct struct user_msghdr onto stack allocated buffer.

---
 kernel/cobalt/posix/io.c          | 20 ++------------------
 1 file changed, 2 insertions(+), 18 deletions(-)
kernel/cobalt/posix/io.c +2 -18
index f35aaf8..85272a5 100644
@@ -79,18 +79,7 @@ COBALT_SYSCALL(write, handover,
 COBALT_SYSCALL(recvmsg, handover,
 	       (int fd, struct user_msghdr __user *umsg, int flags))
 {
-	struct user_msghdr m;
-	ssize_t ret;
-
-	ret = cobalt_copy_from_user(&m, umsg, sizeof(m));
-	if (ret)
-		return ret;
-
-	ret = rtdm_fd_recvmsg(fd, &m, flags);
-	if (ret < 0)
-		return ret;
-
-	return cobalt_copy_to_user(umsg, &m, sizeof(*umsg)) ?: ret;
+	return rtdm_fd_recvmsg(fd, umsg, flags);
 }
 
 static int get_timespec(struct timespec *ts,
@@ -123,12 +112,7 @@ COBALT_SYSCALL(recvmmsg, primary,
 COBALT_SYSCALL(sendmsg, handover,
 	       (int fd, struct user_msghdr __user *umsg, int flags))
 {
-	struct user_msghdr m;
-	int ret;
-
-	ret = cobalt_copy_from_user(&m, umsg, sizeof(m));
-
-	return ret ?: rtdm_fd_sendmsg(fd, &m, flags);
+	return rtdm_fd_sendmsg(fd, umsg, flags);
 }
 
 static int put_mmsglen(void __user **u_mmsg_p, const struct mmsghdr *mmsg)
#1 lore raw
Quoted 4 lines · François Legal via Xenomai

From: François LEGAL <[email protected]>

Remove the copy of struct struct user_msghdr onto stack allocated buffer.

Reasoning is missing here: The driver callbacks are supposed to do that copy-from-user.

But the Question is: why? Is that local copy history left-over, or do only the drivers know how much to copy?

Jan

Quoted 45 lines
---
 kernel/cobalt/posix/io.c          | 20 ++------------------
 1 file changed, 2 insertions(+), 18 deletions(-)
kernel/cobalt/posix/io.c +2 -18
index f35aaf8..85272a5 100644
@@ -79,18 +79,7 @@ COBALT_SYSCALL(write, handover,
 COBALT_SYSCALL(recvmsg, handover,
 	       (int fd, struct user_msghdr __user *umsg, int flags))
 {
-	struct user_msghdr m;
-	ssize_t ret;
-
-	ret = cobalt_copy_from_user(&m, umsg, sizeof(m));
-	if (ret)
-		return ret;
-
-	ret = rtdm_fd_recvmsg(fd, &m, flags);
-	if (ret < 0)
-		return ret;
-
-	return cobalt_copy_to_user(umsg, &m, sizeof(*umsg)) ?: ret;
+	return rtdm_fd_recvmsg(fd, umsg, flags);
 }
 
 static int get_timespec(struct timespec *ts,
@@ -123,12 +112,7 @@ COBALT_SYSCALL(recvmmsg, primary,
 COBALT_SYSCALL(sendmsg, handover,
 	       (int fd, struct user_msghdr __user *umsg, int flags))
 {
-	struct user_msghdr m;
-	int ret;
-
-	ret = cobalt_copy_from_user(&m, umsg, sizeof(m));
-
-	return ret ?: rtdm_fd_sendmsg(fd, &m, flags);
+	return rtdm_fd_sendmsg(fd, umsg, flags);
 }
 
 static int put_mmsglen(void __user **u_mmsg_p, const struct mmsghdr *mmsg)
Signature · 2 lines
Siemens AG, T RDA IOT
Corporate Competence Center Embedded Linux
#2 lore raw
Quoted 6 lines · Jan Kiszka
Quoted 4 lines · François Legal via Xenomai

From: François LEGAL <[email protected]>

Remove the copy of struct struct user_msghdr onto stack allocated buffer.

Quoted 8 lines

Reasoning is missing here: The driver callbacks are supposed to do that copy-from-user.

But the Question is: why? Is that local copy history left-over, or do only the drivers know how much to copy?

Jan

Hi Jan,

The drivers are effectively doing that, hence the problem that happens with SPECTRE mitigation activated (at least on arm arch). About the length of the copy, the drivers do about the the same, sizeof (struct user_msghdr)

So now about the "WHY ?", that I can't say for sure. It might be history, as the other modified parts are IPCs and RTCAN (which I believe are older than RTNET integration). I dug this a little bit, and what I can say is that in 2.6.x, in rtdm/syscall.c, the copy_to/from_user calls were already present and in IPC, they were absent.

François

Quoted 49 lines
Quoted 35 lines
---
 kernel/cobalt/posix/io.c          | 20 ++------------------
 1 file changed, 2 insertions(+), 18 deletions(-)
kernel/cobalt/posix/io.c +1 -12
index f35aaf8..85272a5 100644
@@ -79,18 +79,7 @@ COBALT_SYSCALL(write, handover,
 COBALT_SYSCALL(recvmsg, handover,
 	       (int fd, struct user_msghdr __user *umsg, int flags))
 {
-	struct user_msghdr m;
-	ssize_t ret;
-
-	ret = cobalt_copy_from_user(&m, umsg, sizeof(m));
-	if (ret)
-		return ret;
-
-	ret = rtdm_fd_recvmsg(fd, &m, flags);
-	if (ret < 0)
-		return ret;
-
-	return cobalt_copy_to_user(umsg, &m, sizeof(*umsg)) ?: ret;
+	return rtdm_fd_recvmsg(fd, umsg, flags);
 }
 
 static int get_timespec(struct timespec *ts,
@@ -123,12 +112,7 @@ COBALT_SYSCALL(recvmmsg, primary,
 COBALT_SYSCALL(sendmsg, handover,
 	       (int fd, struct user_msghdr __user *umsg, int flags))
 {

kernel/cobalt/posix/io.c COBALT_SYSCALL(recvmmsg, primary, · line 118

Quoted 10 lines
kernel/cobalt/posix/io.c +1 -6
-	struct user_msghdr m;
-	int ret;
-
-	ret = cobalt_copy_from_user(&m, umsg, sizeof(m));
-
-	return ret ?: rtdm_fd_sendmsg(fd, &m, flags);
+	return rtdm_fd_sendmsg(fd, umsg, flags);
 }
 
 static int put_mmsglen(void __user **u_mmsg_p, const struct mmsghdr *mmsg)

--
Siemens AG, T RDA IOT
Corporate Competence Center Embedded Linux

#3 lore raw
Quoted 14 lines · François Legal
Quoted 12 lines · Jan Kiszka
Quoted 4 lines · François Legal via Xenomai

From: François LEGAL <[email protected]>

Remove the copy of struct struct user_msghdr onto stack allocated buffer.

Reasoning is missing here: The driver callbacks are supposed to do that copy-from-user.

But the Question is: why? Is that local copy history left-over, or do only the drivers know how much to copy?

Quoted 7 lines
Quoted 2 lines

Jan

Hi Jan,

The drivers are effectively doing that, hence the problem that happens with SPECTRE mitigation activated (at least on arm arch). About the length of the copy, the drivers do about the the same, sizeof (struct user_msghdr)

Then we should move the common code into the common place, i.e. drop the copies in the drivers instead.

Quoted 3 lines

So now about the "WHY ?", that I can't say for sure. It might be history, as the other modified parts are IPCs and RTCAN (which I believe are older than RTNET integration). I dug this a little bit, and what I can say is that in 2.6.x, in rtdm/syscall.c, the copy_to/from_user calls were already present and in IPC, they were absent.

Yeah, for a long time, we didn't notice such mess automatically...

Jan

Quoted 52 lines

François

Quoted 49 lines
Quoted 45 lines
---
 kernel/cobalt/posix/io.c          | 20 ++------------------
 1 file changed, 2 insertions(+), 18 deletions(-)
kernel/cobalt/posix/io.c +2 -18
index f35aaf8..85272a5 100644
@@ -79,18 +79,7 @@ COBALT_SYSCALL(write, handover,
 COBALT_SYSCALL(recvmsg, handover,
 	       (int fd, struct user_msghdr __user *umsg, int flags))
 {
-	struct user_msghdr m;
-	ssize_t ret;
-
-	ret = cobalt_copy_from_user(&m, umsg, sizeof(m));
-	if (ret)
-		return ret;
-
-	ret = rtdm_fd_recvmsg(fd, &m, flags);
-	if (ret < 0)
-		return ret;
-
-	return cobalt_copy_to_user(umsg, &m, sizeof(*umsg)) ?: ret;
+	return rtdm_fd_recvmsg(fd, umsg, flags);
 }
 
 static int get_timespec(struct timespec *ts,
@@ -123,12 +112,7 @@ COBALT_SYSCALL(recvmmsg, primary,
 COBALT_SYSCALL(sendmsg, handover,
 	       (int fd, struct user_msghdr __user *umsg, int flags))
 {
-	struct user_msghdr m;
-	int ret;
-
-	ret = cobalt_copy_from_user(&m, umsg, sizeof(m));
-
-	return ret ?: rtdm_fd_sendmsg(fd, &m, flags);
+	return rtdm_fd_sendmsg(fd, umsg, flags);
 }
 
 static int put_mmsglen(void __user **u_mmsg_p, const struct mmsghdr *mmsg)
Signature · 2 lines
Siemens AG, T RDA IOT
Corporate Competence Center Embedded Linux
Signature · 2 lines
Siemens AG, T RDA IOT
Corporate Competence Center Embedded Linux
#4 lore raw
Quoted 18 lines · Jan Kiszka
Quoted 17 lines · François Legal
Quoted 14 lines · Jan Kiszka
Quoted 4 lines · François Legal via Xenomai

From: François LEGAL <[email protected]>

Remove the copy of struct struct user_msghdr onto stack allocated buffer.

Reasoning is missing here: The driver callbacks are supposed to do that copy-from-user.

But the Question is: why? Is that local copy history left-over, or do only the drivers know how much to copy?

Jan

Quoted 8 lines
Quoted 4 lines

Hi Jan,

The drivers are effectively doing that, hence the problem that happens with SPECTRE mitigation activated (at least on arm arch). About the length of the copy, the drivers do about the the same, sizeof (struct user_msghdr)

Then we should move the common code into the common place, i.e. drop the copies in the drivers instead.

I wonder, because from what I could read on RTIPC, these driver functions seems to be callable from kernel context (see rtipc_get_arg() ). There is a risk of trigging SPECTRE mitigation in case is is called from kernel no, or maybe it does not get called through posix/io.c ?

François

Quoted 64 lines
Quoted 3 lines

So now about the "WHY ?", that I can't say for sure. It might be history, as the other modified parts are IPCs and RTCAN (which I believe are older than RTNET integration). I dug this a little bit, and what I can say is that in 2.6.x, in rtdm/syscall.c, the copy_to/from_user calls were already present and in IPC, they were absent.

Yeah, for a long time, we didn't notice such mess automatically...

Jan

Quoted 52 lines

François

Quoted 49 lines
Quoted 35 lines
---
 kernel/cobalt/posix/io.c          | 20 ++------------------
 1 file changed, 2 insertions(+), 18 deletions(-)
kernel/cobalt/posix/io.c +1 -12
index f35aaf8..85272a5 100644
@@ -79,18 +79,7 @@ COBALT_SYSCALL(write, handover,
 COBALT_SYSCALL(recvmsg, handover,
 	       (int fd, struct user_msghdr __user *umsg, int flags))
 {
-	struct user_msghdr m;
-	ssize_t ret;
-
-	ret = cobalt_copy_from_user(&m, umsg, sizeof(m));
-	if (ret)
-		return ret;
-
-	ret = rtdm_fd_recvmsg(fd, &m, flags);
-	if (ret < 0)
-		return ret;
-
-	return cobalt_copy_to_user(umsg, &m, sizeof(*umsg)) ?: ret;
+	return rtdm_fd_recvmsg(fd, umsg, flags);
 }
 
 static int get_timespec(struct timespec *ts,
@@ -123,12 +112,7 @@ COBALT_SYSCALL(recvmmsg, primary,
 COBALT_SYSCALL(sendmsg, handover,
 	       (int fd, struct user_msghdr __user *umsg, int flags))
 {

kernel/cobalt/posix/io.c COBALT_SYSCALL(recvmmsg, primary, · line 118

Quoted 10 lines
kernel/cobalt/posix/io.c +1 -6
-	struct user_msghdr m;
-	int ret;
-
-	ret = cobalt_copy_from_user(&m, umsg, sizeof(m));
-
-	return ret ?: rtdm_fd_sendmsg(fd, &m, flags);
+	return rtdm_fd_sendmsg(fd, umsg, flags);
 }
 
 static int put_mmsglen(void __user **u_mmsg_p, const struct mmsghdr *mmsg)

--
Siemens AG, T RDA IOT
Corporate Competence Center Embedded Linux

--
Siemens AG, T RDA IOT
Corporate Competence Center Embedded Linux

#5 lore raw
Quoted 25 lines · François Legal
Quoted 23 lines · Jan Kiszka
Quoted 21 lines · François Legal
Quoted 6 lines · Jan Kiszka
Quoted 4 lines · François Legal via Xenomai

From: François LEGAL <[email protected]>

Remove the copy of struct struct user_msghdr onto stack allocated buffer.

Quoted 8 lines

Reasoning is missing here: The driver callbacks are supposed to do that copy-from-user.

But the Question is: why? Is that local copy history left-over, or do only the drivers know how much to copy?

Jan

Hi Jan,

The drivers are effectively doing that, hence the problem that happens with SPECTRE mitigation activated (at least on arm arch). About the length of the copy, the drivers do about the the same, sizeof (struct user_msghdr)

Quoted 6 lines
Quoted 3 lines

Then we should move the common code into the common place, i.e. drop the copies in the drivers instead.

I wonder, because from what I could read on RTIPC, these driver functions seems to be callable from kernel context (see rtipc_get_arg() ). There is a risk of trigging SPECTRE mitigation in case is is called from kernel no, or maybe it does not get called through posix/io.c ?

RTDM drivers can call each other, and that can create a non-user entry to callbacks. rtipc_get_arg accounts for that via rtdm_fd_is_user(fd).

If we consolidate again over "callbacks get user_msghdr from kernel memory" (but nothing that this struct my point to), no driver needs to copy that struct, just the syscall entry code.

Jan

Signature · 2 lines
Siemens AG, T RDA IOT
Corporate Competence Center Embedded Linux