Skip to contentSkip to conversation

Syscall User Dispatch

PATCH Superseded Gabriel Krisman Bertazi 8 participants 40 messages Sep 4, 2020 – Sep 25, 2020

Subsystems (13)
Documentation x86 VFS Kselftest +9 more subsystems

v6 of 8 Superseded Latest is v8 →

lore
Share & tools
lore
#0 lore raw

v6 cover Syscall User Dispatch

Hi,

The v6 of this patch series include only the type change requested by Andy on the vdso patch, but since v5 included some bigger changes, I'm documenting them in this cover letter as well.

Please note this applies on top of Linus tree, and it succeeds seccomp and syscall user dispatch selftests.

v5 cover letter
--------------

This is v5 of Syscall User Dispatch. It has some big changes in comparison to v4.

First of all, it allows the vdso trampoline code for architectures that support it. This is exposed through an arch hook. It also addresses the concern about what happens when a bad selector is provided, instead of SIGSEGV, we fail with SIGSYS, which is more debug-able.

Another major change is that it is now based on top of Gleixner's common syscall entry work, and is supposed to only be used by that code. Therefore, the entry symbol is not exported outside of kernel/entry/ code.

The biggest change in this version is the attempt to avoid using one of the final TIF flags on x86 32 bit, without increasing the size of that variable to 64 bit. My expectation is that, with this work, plus the removal of TIF_IA32, TIF_X32 and TIF_FORCE_TF, we might be able to avoid changing this field to 64 bits at all. Instead, this follows the suggestion by Andy to have a generic TIF flag for SECCOMP and this mechanism, and use another field to decide which one is enabled. The code for this is not complex, so it seems like a viable approach.

Finally, this version adds some documentation to the feature.

Kees, I dropped your reviewed-by on patch 5, given the amount of changes.

Thanks,

Previous submissions are archived at:

RFC/v1: https://lkml.org/lkml/2020/7/8/96
v2: https://lkml.org/lkml/2020/7/9/17
v3: https://lkml.org/lkml/2020/7/12/4
v4: https://www.spinics.net/lists/linux-kselftest/msg16377.html
v5: https://lkml.org/lkml/2020/8/10/1320

Gabriel Krisman Bertazi (9):
kernel: Support TIF_SYSCALL_INTERCEPT flag
kernel: entry: Support TIF_SYSCAL_INTERCEPT on common entry code
x86: vdso: Expose sigreturn address on vdso to the kernel
signal: Expose SYS_USER_DISPATCH si_code type
kernel: Implement selective syscall userspace redirection
kernel: entry: Support Syscall User Dispatch for common syscall entry
x86: Enable Syscall User Dispatch
selftests: Add kselftest for syscall user dispatch
doc: Document Syscall User Dispatch

 .../admin-guide/syscall-user-dispatch.rst     |  87 ++++++
 arch/Kconfig                                  |  21 ++
 arch/x86/Kconfig                              |   1 +
 arch/x86/entry/vdso/vdso2c.c                  |   2 +
 arch/x86/entry/vdso/vdso32/sigreturn.S        |   2 +
 arch/x86/entry/vdso/vma.c                     |  15 +
 arch/x86/include/asm/elf.h                    |   1 +
 arch/x86/include/asm/thread_info.h            |   4 +-
 arch/x86/include/asm/vdso.h                   |   2 +
 arch/x86/kernel/signal_compat.c               |   2 +-
 fs/exec.c                                     |   8 +
 include/linux/entry-common.h                  |   6 +-
 include/linux/sched.h                         |   8 +-
 include/linux/seccomp.h                       |  20 +-
 include/linux/syscall_intercept.h             |  71 +++++
 include/linux/syscall_user_dispatch.h         |  29 ++
 include/uapi/asm-generic/siginfo.h            |   3 +-
 include/uapi/linux/prctl.h                    |   5 +
 kernel/entry/Makefile                         |   1 +
 kernel/entry/common.c                         |  32 +-
 kernel/entry/common.h                         |  15 +
 kernel/entry/syscall_user_dispatch.c          | 101 ++++++
 kernel/fork.c                                 |  10 +-
 kernel/seccomp.c                              |   7 +-
 kernel/sys.c                                  |   5 +
 tools/testing/selftests/Makefile              |   1 +
 .../syscall_user_dispatch/.gitignore          |   2 +
 .../selftests/syscall_user_dispatch/Makefile  |   9 +
 .../selftests/syscall_user_dispatch/config    |   1 +
 .../syscall_user_dispatch.c                   | 292 ++++++++++++++++++
 30 files changed, 744 insertions(+), 19 deletions(-)
 create mode 100644 Documentation/admin-guide/syscall-user-dispatch.rst
 create mode 100644 include/linux/syscall_intercept.h
 create mode 100644 include/linux/syscall_user_dispatch.h
 create mode 100644 kernel/entry/common.h
 create mode 100644 kernel/entry/syscall_user_dispatch.c
 create mode 100644 tools/testing/selftests/syscall_user_dispatch/.gitignore
 create mode 100644 tools/testing/selftests/syscall_user_dispatch/Makefile
 create mode 100644 tools/testing/selftests/syscall_user_dispatch/config
 create mode 100644 tools/testing/selftests/syscall_user_dispatch/syscall_user_dispatch.c
Signature · 1 line
2.28.0
#1 lore raw

PATCH v6 1/9 kernel: Support TIF_SYSCALL_INTERCEPT flag

Convert TIF_SECCOMP into a generic TI flag for any syscall interception work being done by the kernel. The actual type of work is exposed by a new flag field outside of thread_info. This ensures that the syscall_intercept field is only accessed if struct seccomp has to be accessed already, such that it doesn't incur in a much higher cost to the seccomp path.

In order to avoid modifying every architecture at once, this patch has a transition mechanism, such that architectures that define TIF_SECCOMP continue to work by ignoring the syscall_intercept flag, as long as they don't support other syscall interception mechanisms like the future syscall user dispatch. When migrating TIF_SECCOMP to TIF_SYSCALL_INTERCEPT, they should adopt the semantics of checking the syscall_intercept flag, like it is done in the common entry syscall code, or even better, migrate to the common syscall entry code.

This was tested by running the selftests for seccomp. No regressions were observed, and all tests passed (with and without this patch).

---
 include/linux/sched.h             |  6 ++-
 include/linux/seccomp.h           | 20 ++++++++-
 include/linux/syscall_intercept.h | 70 +++++++++++++++++++++++++++++++
 kernel/fork.c                     | 10 ++++-
 kernel/seccomp.c                  |  7 ++--
 5 files changed, 106 insertions(+), 7 deletions(-)
 create mode 100644 include/linux/syscall_intercept.h
include/linux/sched.h +5 -1
index afe01e232935..3511c98a7849 100644
@@ -959,7 +959,11 @@ struct task_struct {
 	kuid_t				loginuid;
 	unsigned int			sessionid;
 #endif
-	struct seccomp			seccomp;
+
+	struct {
+		unsigned int			syscall_intercept;
+		struct seccomp			seccomp;
+	};
 
 	/* Thread group tracking: */
 	u64				parent_exec_id;
include/linux/seccomp.h +19 -1
index 02aef2844c38..027dc462cea9 100644
@@ -20,6 +20,24 @@
 #include <linux/atomic.h>
 #include <asm/seccomp.h>
 
+/*
+ * Some transitional defines to avoid migrating every architecture code
+ * at once.
+ */
+
+#if defined(TIF_SECCOMP) && defined(TIF_SYSCALL_INTERCEPT)
+# error "TIF_SYSCALL_INTERCEPT and TIF_SECCOMP can't be defined at the same time"
+#endif
+
+/*
+ * If the arch has not transitioned to TIF_SYSCALL_INTERCEPT, this let
+ * seccomp work with these architectures, as long as no other syscall
+ * intercept features are meant to be supported.
+ */
+#ifdef TIF_SECCOMP
+# define TIF_SYSCALL_INTERCEPT TIF_SECCOMP
+#endif
+
 struct seccomp_filter;
 /**
  * struct seccomp - the state of a seccomp'ed process
@@ -42,7 +60,7 @@ struct seccomp {
 extern int __secure_computing(const struct seccomp_data *sd);
 static inline int secure_computing(void)
 {
-	if (unlikely(test_thread_flag(TIF_SECCOMP)))
+	if (unlikely(test_thread_flag(TIF_SYSCALL_INTERCEPT)))
 		return  __secure_computing(NULL);
 	return 0;
 }
include/linux/syscall_intercept.h new file +70 -0

70 lines not on this page · raw message

kernel/fork.c +8 -2
index 4d32190861bd..a39177bed8ea 100644
@@ -49,7 +49,7 @@
 #include <linux/cgroup.h>
 #include <linux/security.h>
 #include <linux/hugetlb.h>
-#include <linux/seccomp.h>
+#include <linux/syscall_intercept.h>
 #include <linux/swap.h>
 #include <linux/syscalls.h>
 #include <linux/jiffies.h>
@@ -898,6 +898,7 @@ static struct task_struct *dup_task_struct(struct task_struct *orig, int node)
 	 * the usage counts on the error path calling free_task.
 	 */
 	tsk->seccomp.filter = NULL;
+	tsk->syscall_intercept = 0;
 #endif
 
 	setup_thread_stack(tsk, orig);
@@ -1620,9 +1621,14 @@ static void copy_seccomp(struct task_struct *p)
 	 * If the parent gained a seccomp mode after copying thread
 	 * flags and between before we held the sighand lock, we have
 	 * to manually enable the seccomp thread flag here.
+	 *
+	 * In addition current sighand lock is asserted, so it is safe
+	 * to use the unlocked version of set_tsk_syscall_intercept.
 	 */
 	if (p->seccomp.mode != SECCOMP_MODE_DISABLED)
-		set_tsk_thread_flag(p, TIF_SECCOMP);
+		__set_tsk_syscall_intercept(p, SYSINT_SECCOMP);
+	else
+		__clear_tsk_syscall_intercept(p, SYSINT_SECCOMP);
 #endif
 }
 
kernel/seccomp.c +4 -3
index 3ee59ce0a323..d0643b500f2e 100644
@@ -28,6 +28,7 @@
 #include <linux/slab.h>
 #include <linux/syscalls.h>
 #include <linux/sysctl.h>
+#include <linux/syscall_intercept.h>
 
 #ifdef CONFIG_HAVE_ARCH_SECCOMP_FILTER
 #include <asm/syscall.h>
@@ -352,14 +353,14 @@ static inline void seccomp_assign_mode(struct task_struct *task,
 
 	task->seccomp.mode = seccomp_mode;
 	/*
-	 * Make sure TIF_SECCOMP cannot be set before the mode (and
+	 * Make sure SYSINT_SECCOMP cannot be set before the mode (and
 	 * filter) is set.
 	 */
 	smp_mb__before_atomic();
 	/* Assume default seccomp processes want spec flaw mitigation. */
 	if ((flags & SECCOMP_FILTER_FLAG_SPEC_ALLOW) == 0)
 		arch_seccomp_spec_mitigate(task);
-	set_tsk_thread_flag(task, TIF_SECCOMP);
+	__set_tsk_syscall_intercept(task, SYSINT_SECCOMP);
 }
 
 #ifdef CONFIG_SECCOMP_FILTER
@@ -925,7 +926,7 @@ static int __seccomp_filter(int this_syscall, const struct seccomp_data *sd,
 
 	/*
 	 * Make sure that any changes to mode from another thread have
-	 * been seen after TIF_SECCOMP was seen.
+	 * been seen after SYSINT_SECCOMP was seen.
 	 */
 	rmb();
 
Signature · 1 line
2.28.0
#2 lore raw

Re: [PATCH v6 1/9] kernel: Support TIF_SYSCALL_INTERCEPT flag

Quoted 34 lines · Gabriel Krisman Bertazi

Convert TIF_SECCOMP into a generic TI flag for any syscall interception work being done by the kernel. The actual type of work is exposed by a new flag field outside of thread_info. This ensures that the syscall_intercept field is only accessed if struct seccomp has to be accessed already, such that it doesn't incur in a much higher cost to the seccomp path.

In order to avoid modifying every architecture at once, this patch has a transition mechanism, such that architectures that define TIF_SECCOMP continue to work by ignoring the syscall_intercept flag, as long as they don't support other syscall interception mechanisms like the future syscall user dispatch. When migrating TIF_SECCOMP to TIF_SYSCALL_INTERCEPT, they should adopt the semantics of checking the syscall_intercept flag, like it is done in the common entry syscall code, or even better, migrate to the common syscall entry code.

This was tested by running the selftests for seccomp. No regressions were observed, and all tests passed (with and without this patch).

---
 include/linux/sched.h             |  6 ++-
 include/linux/seccomp.h           | 20 ++++++++-
 include/linux/syscall_intercept.h | 70 +++++++++++++++++++++++++++++++
 kernel/fork.c                     | 10 ++++-
 kernel/seccomp.c                  |  7 ++--
 5 files changed, 106 insertions(+), 7 deletions(-)
 create mode 100644 include/linux/syscall_intercept.h
include/linux/sched.h +0 -0
index afe01e232935..3511c98a7849 100644

include/linux/sched.h struct task_struct { · line 966

Quoted 9 lines
include/linux/sched.h +5 -1
@@ -959,7 +959,11 @@ struct task_struct {
 	kuid_t				loginuid;
 	unsigned int			sessionid;
 #endif
-	struct seccomp			seccomp;
+
+	struct {
+		unsigned int			syscall_intercept;
+		struct seccomp			seccomp;
+	};

If there's no specific reason to do this I'd not wrap this in an anonymous struct. It doesn't really buy anything and there doesn't seem to be precedent in struct task_struct right now. Also, if this somehow adds padding it seems you might end up increasing the size of struct task_struct more than necessary by accident? (I might be wrong though.)

Quoted 49 lines
include/linux/sched.h +0 -0
 	/* Thread group tracking: */
 	u64				parent_exec_id;
include/linux/seccomp.h +19 -1
index 02aef2844c38..027dc462cea9 100644
@@ -20,6 +20,24 @@
 #include <linux/atomic.h>
 #include <asm/seccomp.h>
 
+/*
+ * Some transitional defines to avoid migrating every architecture code
+ * at once.
+ */
+
+#if defined(TIF_SECCOMP) && defined(TIF_SYSCALL_INTERCEPT)
+# error "TIF_SYSCALL_INTERCEPT and TIF_SECCOMP can't be defined at the same time"
+#endif
+
+/*
+ * If the arch has not transitioned to TIF_SYSCALL_INTERCEPT, this let
+ * seccomp work with these architectures, as long as no other syscall
+ * intercept features are meant to be supported.
+ */
+#ifdef TIF_SECCOMP
+# define TIF_SYSCALL_INTERCEPT TIF_SECCOMP
+#endif
+
 struct seccomp_filter;
 /**
  * struct seccomp - the state of a seccomp'ed process
@@ -42,7 +60,7 @@ struct seccomp {
 extern int __secure_computing(const struct seccomp_data *sd);
 static inline int secure_computing(void)
 {
-	if (unlikely(test_thread_flag(TIF_SECCOMP)))
+	if (unlikely(test_thread_flag(TIF_SYSCALL_INTERCEPT)))
 		return  __secure_computing(NULL);
 	return 0;
 }
include/linux/syscall_intercept.h new file +2 -0
new file mode 100644
index 000000000000..725d157699da
@@ -0,0 +1,70 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*

include/linux/syscall_intercept.h line 12

Quoted 10 lines
include/linux/syscall_intercept.h new file +10 -0
+ * Copyright (C) 2020 Collabora Ltd.
+ */
+#ifndef _SYSCALL_INTERCEPT_H
+#define _SYSCALL_INTERCEPT_H
+
+#include <linux/sched.h>
+#include <linux/sched/signal.h>
+#include <linux/thread_info.h>
+
+#define SYSINT_SECCOMP		0x1

<bikeshed>

Can we maybe use a better name for this? I noone minds the extra characters I'd suggest:
SYSCALL_INTERCEPT_SECCOMP
or
SYS_INTERCEPT_SECCOMP

</bikeshed>

Quoted 135 lines
include/linux/syscall_intercept.h new file +58 -0
+
+#ifdef TIF_SYSCALL_INTERCEPT
+
+/* seccomp (at least) can modify TIF_SYSCALL_INTERCEPT from a different
+ * thread, which means it can race with itself or with
+ * syscall_user_dispatch. Therefore, TIF_SYSCALL_INTERCEPT and
+ * syscall_intercept are synchronized by tsk->sighand->siglock.
+ */
+
+static inline void __set_tsk_syscall_intercept(struct task_struct *tsk,
+					   unsigned int type)
+{
+	tsk->syscall_intercept |= type;
+
+	if (tsk->syscall_intercept)
+		set_tsk_thread_flag(tsk, TIF_SYSCALL_INTERCEPT);
+}
+
+static inline void __clear_tsk_syscall_intercept(struct task_struct *tsk,
+					     unsigned int type)
+{
+	tsk->syscall_intercept &= ~type;
+
+	if (tsk->syscall_intercept == 0)
+		clear_tsk_thread_flag(tsk, TIF_SYSCALL_INTERCEPT);
+}
+
+static inline void set_tsk_syscall_intercept(struct task_struct *tsk, unsigned int type)
+{
+	spin_lock_irq(&tsk->sighand->siglock);
+	__set_tsk_syscall_intercept(tsk, type);
+	spin_unlock_irq(&tsk->sighand->siglock);
+}
+
+static inline void clear_tsk_syscall_intercept(struct task_struct *tsk, unsigned int type)
+{
+	spin_lock_irq(&tsk->sighand->siglock);
+	__clear_tsk_syscall_intercept(tsk, type);
+	spin_unlock_irq(&tsk->sighand->siglock);
+}
+
+#else
+static inline void __set_tsk_syscall_intercept(struct task_struct *tsk, unsigned int type)
+{
+}
+static inline void set_tsk_syscall_intercept(struct task_struct *tsk, unsigned int type)
+{
+}
+static inline void __clear_tsk_syscall_intercept(struct task_struct *tsk, unsigned int type)
+{
+}
+static inline void clear_tsk_syscall_intercept(struct task_struct *tsk, unsigned int type)
+{
+}
+#endif
+
+#endif
+
kernel/fork.c +8 -2
index 4d32190861bd..a39177bed8ea 100644
@@ -49,7 +49,7 @@
 #include <linux/cgroup.h>
 #include <linux/security.h>
 #include <linux/hugetlb.h>
-#include <linux/seccomp.h>
+#include <linux/syscall_intercept.h>
 #include <linux/swap.h>
 #include <linux/syscalls.h>
 #include <linux/jiffies.h>
@@ -898,6 +898,7 @@ static struct task_struct *dup_task_struct(struct task_struct *orig, int node)
 	 * the usage counts on the error path calling free_task.
 	 */
 	tsk->seccomp.filter = NULL;
+	tsk->syscall_intercept = 0;
 #endif
 
 	setup_thread_stack(tsk, orig);
@@ -1620,9 +1621,14 @@ static void copy_seccomp(struct task_struct *p)
 	 * If the parent gained a seccomp mode after copying thread
 	 * flags and between before we held the sighand lock, we have
 	 * to manually enable the seccomp thread flag here.
+	 *
+	 * In addition current sighand lock is asserted, so it is safe
+	 * to use the unlocked version of set_tsk_syscall_intercept.
 	 */
 	if (p->seccomp.mode != SECCOMP_MODE_DISABLED)
-		set_tsk_thread_flag(p, TIF_SECCOMP);
+		__set_tsk_syscall_intercept(p, SYSINT_SECCOMP);
+	else
+		__clear_tsk_syscall_intercept(p, SYSINT_SECCOMP);
 #endif
 }
 
kernel/seccomp.c +4 -3
index 3ee59ce0a323..d0643b500f2e 100644
@@ -28,6 +28,7 @@
 #include <linux/slab.h>
 #include <linux/syscalls.h>
 #include <linux/sysctl.h>
+#include <linux/syscall_intercept.h>
 
 #ifdef CONFIG_HAVE_ARCH_SECCOMP_FILTER
 #include <asm/syscall.h>
@@ -352,14 +353,14 @@ static inline void seccomp_assign_mode(struct task_struct *task,
 
 	task->seccomp.mode = seccomp_mode;
 	/*
-	 * Make sure TIF_SECCOMP cannot be set before the mode (and
+	 * Make sure SYSINT_SECCOMP cannot be set before the mode (and
 	 * filter) is set.
 	 */
 	smp_mb__before_atomic();
 	/* Assume default seccomp processes want spec flaw mitigation. */
 	if ((flags & SECCOMP_FILTER_FLAG_SPEC_ALLOW) == 0)
 		arch_seccomp_spec_mitigate(task);
-	set_tsk_thread_flag(task, TIF_SECCOMP);
+	__set_tsk_syscall_intercept(task, SYSINT_SECCOMP);
 }
 
 #ifdef CONFIG_SECCOMP_FILTER
@@ -925,7 +926,7 @@ static int __seccomp_filter(int this_syscall, const struct seccomp_data *sd,
 
 	/*
 	 * Make sure that any changes to mode from another thread have
-	 * been seen after TIF_SECCOMP was seen.
+	 * been seen after SYSINT_SECCOMP was seen.
 	 */
 	rmb();
 
Signature · 1 line
2.28.0
#3 lore raw

Re: [PATCH v6 1/9] kernel: Support TIF_SYSCALL_INTERCEPT flag

Quoted 13 lines · Christian Brauner
Quoted 12 lines · Gabriel Krisman Bertazi

index afe01e232935..3511c98a7849 100644

include/linux/sched.h +4 -1
@@ -959,7 +959,11 @@ struct task_struct {
 	kuid_t				loginuid;
 	unsigned int			sessionid;
 #endif
-	struct seccomp			seccomp;
+
+	struct {
+		unsigned int			syscall_intercept;
+		struct seccomp			seccomp;
Quoted 8 lines

include/linux/sched.h struct task_struct { · line 966

Quoted 1 line
include/linux/sched.h +1 -0
+	};

If there's no specific reason to do this I'd not wrap this in an anonymous struct. It doesn't really buy anything and there doesn't seem to be precedent in struct task_struct right now. Also, if this somehow adds padding it seems you might end up increasing the size of struct task_struct more than necessary by accident? (I might be wrong though.)

Hi Christian,

Thanks for your review on this and on the other patches of this series.

I wrapped these to prevent struct layout randomization from separating the flags field from seccomp, as they are going to be used together and I was trying to reduce overhead to seccomp entry due to two cache misses when reading this structure. Measuring it seccomp_benchmark didn't show any difference with the unwrapped version, so perhaps it was a bit of premature optimization?

Quoted 21 lines
Quoted 8 lines
include/linux/syscall_intercept.h new file +2 -0
new file mode 100644
index 000000000000..725d157699da
@@ -0,0 +1,70 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*

include/linux/syscall_intercept.h line 12

Quoted 10 lines
include/linux/syscall_intercept.h new file +10 -0
+ * Copyright (C) 2020 Collabora Ltd.
+ */
+#ifndef _SYSCALL_INTERCEPT_H
+#define _SYSCALL_INTERCEPT_H
+
+#include <linux/sched.h>
+#include <linux/sched/signal.h>
+#include <linux/thread_info.h>
+
+#define SYSINT_SECCOMP		0x1

<bikeshed>

Quoted 8 lines

Can we maybe use a better name for this? I noone minds the extra characters I'd suggest:
SYSCALL_INTERCEPT_SECCOMP
or
SYS_INTERCEPT_SECCOMP

</bikeshed>

will do.

Thanks,

Signature · 1 line
Gabriel Krisman Bertazi
#4 lore raw

Re: [PATCH v6 1/9] kernel: Support TIF_SYSCALL_INTERCEPT flag

Quoted 26 lines · Gabriel Krisman Bertazi
Quoted 13 lines · Christian Brauner
Quoted 12 lines · Gabriel Krisman Bertazi

index afe01e232935..3511c98a7849 100644

include/linux/sched.h +4 -1
@@ -959,7 +959,11 @@ struct task_struct {
 	kuid_t				loginuid;
 	unsigned int			sessionid;
 #endif
-	struct seccomp			seccomp;
+
+	struct {
+		unsigned int			syscall_intercept;
+		struct seccomp			seccomp;
Quoted 8 lines

include/linux/sched.h struct task_struct { · line 966

Quoted 1 line
include/linux/sched.h +1 -0
+	};

If there's no specific reason to do this I'd not wrap this in an anonymous struct. It doesn't really buy anything and there doesn't seem to be precedent in struct task_struct right now. Also, if this somehow adds padding it seems you might end up increasing the size of struct task_struct more than necessary by accident? (I might be wrong though.)

Hi Christian,

Quoted 8 lines

Thanks for your review on this and on the other patches of this series.

I wrapped these to prevent struct layout randomization from separating the flags field from seccomp, as they are going to be used together and I was trying to reduce overhead to seccomp entry due to two cache misses when reading this structure. Measuring it seccomp_benchmark didn't show any difference with the unwrapped version, so perhaps it was a bit of premature optimization?

That should not be a thing to think about here. Structure randomization already has a mode to protect against cache line issues. I would leave this as just a new member; no wrapping struct.

Signature · 1 line
Kees Cook
#5 lore raw

Re: [PATCH v6 1/9] kernel: Support TIF_SYSCALL_INTERCEPT flag

Quoted 36 lines · Kees Cook
Quoted 26 lines · Gabriel Krisman Bertazi
Quoted 13 lines · Christian Brauner
Quoted 12 lines · Gabriel Krisman Bertazi

index afe01e232935..3511c98a7849 100644

include/linux/sched.h +4 -1
@@ -959,7 +959,11 @@ struct task_struct {
 	kuid_t				loginuid;
 	unsigned int			sessionid;
 #endif
-	struct seccomp			seccomp;
+
+	struct {
+		unsigned int			syscall_intercept;
+		struct seccomp			seccomp;
Quoted 8 lines

include/linux/sched.h struct task_struct { · line 966

Quoted 1 line
include/linux/sched.h +1 -0
+	};

If there's no specific reason to do this I'd not wrap this in an anonymous struct. It doesn't really buy anything and there doesn't seem to be precedent in struct task_struct right now. Also, if this somehow adds padding it seems you might end up increasing the size of struct task_struct more than necessary by accident? (I might be wrong though.)

Hi Christian,

Quoted 8 lines

Thanks for your review on this and on the other patches of this series.

I wrapped these to prevent struct layout randomization from separating the flags field from seccomp, as they are going to be used together and I was trying to reduce overhead to seccomp entry due to two cache misses when reading this structure. Measuring it seccomp_benchmark didn't show any difference with the unwrapped version, so perhaps it was a bit of premature optimization?

Quoted 3 lines

That should not be a thing to think about here. Structure randomization already has a mode to protect against cache line issues. I would leave this as just a new member; no wrapping struct.

Makes sense. I will drop it for the next iteration. Thanks!

Signature · 1 line
Gabriel Krisman Bertazi
#6 lore raw

Re: [PATCH v6 1/9] kernel: Support TIF_SYSCALL_INTERCEPT flag

In reply to Gabriel Krisman Bertazi

include/linux/syscall_intercept.h line 29

Quoted 8 lines · Gabriel Krisman Bertazi
include/linux/syscall_intercept.h new file +8 -0
+static inline void __set_tsk_syscall_intercept(struct task_struct *tsk,
+					   unsigned int type)
+{
+	tsk->syscall_intercept |= type;
+
+	if (tsk->syscall_intercept)
+		set_tsk_thread_flag(tsk, TIF_SYSCALL_INTERCEPT);
+}

Did the above want to be:

	unsigned int old = tsk->syscall_intercept;
	tsk->syscall_intercept |= type;
	if (!old)
		set_tsk_thread_flag(tsk, TIF_SYSCALL_INTERCEPT)

?

Quoted 8 lines
include/linux/syscall_intercept.h new file +8 -0
+static inline void __clear_tsk_syscall_intercept(struct task_struct *tsk,
+					     unsigned int type)
+{
+	tsk->syscall_intercept &= ~type;
+
+	if (tsk->syscall_intercept == 0)
+		clear_tsk_thread_flag(tsk, TIF_SYSCALL_INTERCEPT);
+}
#7 lore raw

Re: [PATCH v6 1/9] kernel: Support TIF_SYSCALL_INTERCEPT flag

Quoted 12 lines · [email protected] writes:

include/linux/syscall_intercept.h line 29

Quoted 8 lines · Gabriel Krisman Bertazi
include/linux/syscall_intercept.h new file +8 -0
+static inline void __set_tsk_syscall_intercept(struct task_struct *tsk,
+					   unsigned int type)
+{
+	tsk->syscall_intercept |= type;
+
+	if (tsk->syscall_intercept)
+		set_tsk_thread_flag(tsk, TIF_SYSCALL_INTERCEPT);
+}

Did the above want to be:

Quoted 5 lines
	unsigned int old = tsk->syscall_intercept;
	tsk->syscall_intercept |= type;
	if (!old)
		set_tsk_thread_flag(tsk, TIF_SYSCALL_INTERCEPT)

Hi Peter,

Thanks for the review!

I'm not sure this change gains us anything. For now, __set_tsk_syscall_intercept cannot be called with !type, so both versions behave the same, but my version is safe with that scenario. This won't be called frequent enough for the extra calls to set_tsk_thread_flag matter. Am I missing something?

Thanks,

Signature · 1 line
Gabriel Krisman Bertazi
maintainer
#8 lore raw

Re: [PATCH v6 1/9] kernel: Support TIF_SYSCALL_INTERCEPT flag

Quoted 22 lines · Gabriel Krisman Bertazi
Quoted 12 lines · [email protected] writes:

include/linux/syscall_intercept.h line 29

Quoted 8 lines · Gabriel Krisman Bertazi
include/linux/syscall_intercept.h new file +8 -0
+static inline void __set_tsk_syscall_intercept(struct task_struct *tsk,
+					   unsigned int type)
+{
+	tsk->syscall_intercept |= type;
+
+	if (tsk->syscall_intercept)
+		set_tsk_thread_flag(tsk, TIF_SYSCALL_INTERCEPT);
+}

Did the above want to be:

Quoted 5 lines
	unsigned int old = tsk->syscall_intercept;
	tsk->syscall_intercept |= type;
	if (!old)
		set_tsk_thread_flag(tsk, TIF_SYSCALL_INTERCEPT)

Hi Peter,

Quoted 7 lines

Thanks for the review!

I'm not sure this change gains us anything. For now, __set_tsk_syscall_intercept cannot be called with !type, so both versions behave the same, but my version is safe with that scenario. This won't be called frequent enough for the extra calls to set_tsk_thread_flag matter. Am I missing something?

Your version will do set_tsk_thread_flag() for every invocation (assuming non-zero type). That's sub-optimal.

#9 lore raw

Re: [PATCH v6 1/9] kernel: Support TIF_SYSCALL_INTERCEPT flag

In reply to Gabriel Krisman Bertazi
Quoted 7 lines · Gabriel Krisman Bertazi

Convert TIF_SECCOMP into a generic TI flag for any syscall interception work being done by the kernel. The actual type of work is exposed by a new flag field outside of thread_info. This ensures that the syscall_intercept field is only accessed if struct seccomp has to be accessed already, such that it doesn't incur in a much higher cost to the seccomp path.

Quoted 8 lines

In order to avoid modifying every architecture at once, this patch has a transition mechanism, such that architectures that define TIF_SECCOMP continue to work by ignoring the syscall_intercept flag, as long as they don't support other syscall interception mechanisms like the future syscall user dispatch. When migrating TIF_SECCOMP to TIF_SYSCALL_INTERCEPT, they should adopt the semantics of checking the syscall_intercept flag, like it is done in the common entry syscall code, or even better, migrate to the common syscall entry code.

Can we "eat" all the other flags like ptrace, audit, etc, too? Doing this only for seccomp seems strange.

Signature · 1 line
Kees Cook
#10 lore raw

Re: [PATCH v6 1/9] kernel: Support TIF_SYSCALL_INTERCEPT flag

Quoted 17 lines · Kees Cook
Quoted 7 lines · Gabriel Krisman Bertazi

Convert TIF_SECCOMP into a generic TI flag for any syscall interception work being done by the kernel. The actual type of work is exposed by a new flag field outside of thread_info. This ensures that the syscall_intercept field is only accessed if struct seccomp has to be accessed already, such that it doesn't incur in a much higher cost to the seccomp path.

Quoted 8 lines

In order to avoid modifying every architecture at once, this patch has a transition mechanism, such that architectures that define TIF_SECCOMP continue to work by ignoring the syscall_intercept flag, as long as they don't support other syscall interception mechanisms like the future syscall user dispatch. When migrating TIF_SECCOMP to TIF_SYSCALL_INTERCEPT, they should adopt the semantics of checking the syscall_intercept flag, like it is done in the common entry syscall code, or even better, migrate to the common syscall entry code.

Quoted 2 lines

Can we "eat" all the other flags like ptrace, audit, etc, too? Doing this only for seccomp seems strange.

Hi Kees, Thanks again for the review.

Yes, we can, and I'm happy to follow up with that as part of my TIF clean up work, but can we not block the current patchset to be merged waiting for that, as this already grew a lot from the original feature submission?

Also, Thomas do you mind this approach to reduce the usage of TIF_ flags? I remember you suggested simply expanding the variable to 64 bits at some point.

Thanks,

Signature · 1 line
Gabriel Krisman Bertazi
#11 lore raw

Re: [PATCH v6 1/9] kernel: Support TIF_SYSCALL_INTERCEPT flag

Quoted 22 lines · Gabriel Krisman Bertazi
Quoted 17 lines · Kees Cook
Quoted 7 lines · Gabriel Krisman Bertazi

Convert TIF_SECCOMP into a generic TI flag for any syscall interception work being done by the kernel. The actual type of work is exposed by a new flag field outside of thread_info. This ensures that the syscall_intercept field is only accessed if struct seccomp has to be accessed already, such that it doesn't incur in a much higher cost to the seccomp path.

Quoted 8 lines

In order to avoid modifying every architecture at once, this patch has a transition mechanism, such that architectures that define TIF_SECCOMP continue to work by ignoring the syscall_intercept flag, as long as they don't support other syscall interception mechanisms like the future syscall user dispatch. When migrating TIF_SECCOMP to TIF_SYSCALL_INTERCEPT, they should adopt the semantics of checking the syscall_intercept flag, like it is done in the common entry syscall code, or even better, migrate to the common syscall entry code.

Quoted 2 lines

Can we "eat" all the other flags like ptrace, audit, etc, too? Doing this only for seccomp seems strange.

Quoted 6 lines

Hi Kees, Thanks again for the review.

Yes, we can, and I'm happy to follow up with that as part of my TIF clean up work, but can we not block the current patchset to be merged waiting for that, as this already grew a lot from the original feature submission?

In that case, I'd say just add the new TIF flag. The consolidation can come later.

Signature · 1 line
Kees Cook
#12 lore raw

Re: [PATCH v6 1/9] kernel: Support TIF_SYSCALL_INTERCEPT flag

Quoted 9 lines · Kees Cook
Quoted 5 lines · Gabriel Krisman Bertazi

Kees Cook <[email protected]> writes:
Yes, we can, and I'm happy to follow up with that as part of my TIF clean up work, but can we not block the current patchset to be merged waiting for that, as this already grew a lot from the original feature submission?

In that case, I'd say just add the new TIF flag. The consolidation can come later.

No. This is exactly the wrong order. Cleanup and consolidation have precedence over features. I'm tired of 'we'll do that later' songs, simply because in the very end I'm going to be the idiot who mops up the resulting mess.

Thanks,

tglx

#13 lore raw

Re: [PATCH v6 1/9] kernel: Support TIF_SYSCALL_INTERCEPT flag

Quoted 8 lines · Thomas Gleixner
Quoted 7 lines · Kees Cook
Quoted 5 lines · Gabriel Krisman Bertazi

Kees Cook <[email protected]> writes:
Yes, we can, and I'm happy to follow up with that as part of my TIF clean up work, but can we not block the current patchset to be merged waiting for that, as this already grew a lot from the original feature submission?

Quoted 8 lines
Quoted 2 lines

In that case, I'd say just add the new TIF flag. The consolidation can come later.

No. This is exactly the wrong order. Cleanup and consolidation have precedence over features. I'm tired of 'we'll do that later' songs, simply because in the very end I'm going to be the idiot who mops up the resulting mess.

No problem. I will follow up with a patchset consolidating those flags into this syscall_intercept interface I proposed. I assume there is no immediate concerns with the consolidation approach itself.

Signature · 1 line
Gabriel Krisman Bertazi
#14 lore raw

Re: [PATCH v6 1/9] kernel: Support TIF_SYSCALL_INTERCEPT flag

Quoted 19 lines · Gabriel Krisman Bertazi
Quoted 8 lines · Thomas Gleixner
Quoted 7 lines · Kees Cook
Quoted 5 lines · Gabriel Krisman Bertazi

Kees Cook <[email protected]> writes:
Yes, we can, and I'm happy to follow up with that as part of my TIF clean up work, but can we not block the current patchset to be merged waiting for that, as this already grew a lot from the original feature submission?

Quoted 8 lines
Quoted 2 lines

In that case, I'd say just add the new TIF flag. The consolidation can come later.

No. This is exactly the wrong order. Cleanup and consolidation have precedence over features. I'm tired of 'we'll do that later' songs, simply because in the very end I'm going to be the idiot who mops up the resulting mess.

Quoted 3 lines

No problem. I will follow up with a patchset consolidating those flags into this syscall_intercept interface I proposed. I assume there is no immediate concerns with the consolidation approach itself.

I think the only issue is just finding a clean way to set/unset the flags safely/quickly (a lock seems too heavy to me).

Should thread_info hold an entire u32 for all intercept flags (then the TIF_WORK tests is just a zero-test of the intercept u32 word)? Or should there be a TIF_INTERCEPT and a totally separate u32 (e.g. in task_struct) indicating which intercepts? (And if they're separate, how do we atomically set/unset)

i.e.:

atomic_start
toggle a per-intercept bit
set TIF_INTERCEPT = !!(intercept word)
atomic_end

Signature · 1 line
Kees Cook
#15 lore raw

PATCH v6 2/9 kernel: entry: Support TIF_SYSCAL_INTERCEPT on common entry code

In reply to Gabriel Krisman Bertazi

Syscalls that use common entry code (x86 at the moment of this writing) need to have their defines updated inside this commit. This added a measureable overhead of 1ns to seccomp_benchmark selftests on a bare-metal AMD system.

---
 arch/x86/include/asm/thread_info.h |  4 ++--
 include/linux/entry-common.h       |  6 +-----
 kernel/entry/common.c              | 24 +++++++++++++++++++++---
 3 files changed, 24 insertions(+), 10 deletions(-)
arch/x86/include/asm/thread_info.h +2 -2
index 267701ae3d86..cf723181e1f2 100644
@@ -82,7 +82,7 @@ struct thread_info {
 #define TIF_SSBD		5	/* Speculative store bypass disable */
 #define TIF_SYSCALL_EMU		6	/* syscall emulation active */
 #define TIF_SYSCALL_AUDIT	7	/* syscall auditing active */
-#define TIF_SECCOMP		8	/* secure computing */
+#define TIF_SYSCALL_INTERCEPT	8	/* Intercept system call */
 #define TIF_SPEC_IB		9	/* Indirect branch speculation mitigation */
 #define TIF_SPEC_FORCE_UPDATE	10	/* Force speculation MSR update in context switch */
 #define TIF_USER_RETURN_NOTIFY	11	/* notify kernel of userspace return */
@@ -112,7 +112,7 @@ struct thread_info {
 #define _TIF_SSBD		(1 << TIF_SSBD)
 #define _TIF_SYSCALL_EMU	(1 << TIF_SYSCALL_EMU)
 #define _TIF_SYSCALL_AUDIT	(1 << TIF_SYSCALL_AUDIT)
-#define _TIF_SECCOMP		(1 << TIF_SECCOMP)
+#define _TIF_SYSCALL_INTERCEPT	(1 << TIF_SYSCALL_INTERCEPT)
 #define _TIF_SPEC_IB		(1 << TIF_SPEC_IB)
 #define _TIF_SPEC_FORCE_UPDATE	(1 << TIF_SPEC_FORCE_UPDATE)
 #define _TIF_USER_RETURN_NOTIFY	(1 << TIF_USER_RETURN_NOTIFY)
include/linux/entry-common.h +1 -5
index efebbffcd5cc..72ce9ca860c6 100644
@@ -21,10 +21,6 @@
 # define _TIF_SYSCALL_TRACEPOINT	(0)
 #endif
 
-#ifndef _TIF_SECCOMP
-# define _TIF_SECCOMP			(0)
-#endif
-
 #ifndef _TIF_SYSCALL_AUDIT
 # define _TIF_SYSCALL_AUDIT		(0)
 #endif
@@ -45,7 +41,7 @@
 #endif
 
 #define SYSCALL_ENTER_WORK						\
-	(_TIF_SYSCALL_TRACE | _TIF_SYSCALL_AUDIT | _TIF_SECCOMP |	\
+	(_TIF_SYSCALL_TRACE | _TIF_SYSCALL_AUDIT | _TIF_SYSCALL_INTERCEPT | \
 	 _TIF_SYSCALL_TRACEPOINT | _TIF_SYSCALL_EMU |			\
 	 ARCH_SYSCALL_ENTER_WORK)
 
kernel/entry/common.c +21 -3
index fcae019158ca..44fd089d59da 100644
@@ -4,6 +4,7 @@
 #include <linux/entry-common.h>
 #include <linux/livepatch.h>
 #include <linux/audit.h>
+#include <linux/syscall_intercept.h>
 
 #define CREATE_TRACE_POINTS
 #include <trace/events/syscalls.h>
@@ -41,6 +42,20 @@ static inline void syscall_enter_audit(struct pt_regs *regs, long syscall)
 	}
 }
 
+static inline long do_syscall_intercept(struct pt_regs *regs)
+{
+	int sysint_work = READ_ONCE(current->syscall_intercept);
+	int ret;
+
+	if (sysint_work & SYSINT_SECCOMP) {
+		ret = __secure_computing(NULL);
+		if (ret == -1L)
+			return ret;
+	}
+
+	return 0;
+}
+
 static long syscall_trace_enter(struct pt_regs *regs, long syscall,
 				unsigned long ti_work)
 {
@@ -53,9 +68,12 @@ static long syscall_trace_enter(struct pt_regs *regs, long syscall,
 			return -1L;
 	}
 
-	/* Do seccomp after ptrace, to catch any tracer changes. */
-	if (ti_work & _TIF_SECCOMP) {
-		ret = __secure_computing(NULL);
+	/*
+	 * Do syscall interception like seccomp after ptrace, to catch
+	 * any tracer changes.
+	 */
+	if (ti_work & _TIF_SYSCALL_INTERCEPT) {
+		ret = do_syscall_intercept(regs);
 		if (ret == -1L)
 			return ret;
 	}
Signature · 1 line
2.28.0
Acked-by on v6 2/9
#16 lore raw

Re: [PATCH v6 2/9] kernel: entry: Support TIF_SYSCAL_INTERCEPT on common entry code

Quoted 71 lines · Gabriel Krisman Bertazi

Syscalls that use common entry code (x86 at the moment of this writing) need to have their defines updated inside this commit. This added a measureable overhead of 1ns to seccomp_benchmark selftests on a bare-metal AMD system.

---
 arch/x86/include/asm/thread_info.h |  4 ++--
 include/linux/entry-common.h       |  6 +-----
 kernel/entry/common.c              | 24 +++++++++++++++++++++---
 3 files changed, 24 insertions(+), 10 deletions(-)
arch/x86/include/asm/thread_info.h +2 -2
index 267701ae3d86..cf723181e1f2 100644
@@ -82,7 +82,7 @@ struct thread_info {
 #define TIF_SSBD		5	/* Speculative store bypass disable */
 #define TIF_SYSCALL_EMU		6	/* syscall emulation active */
 #define TIF_SYSCALL_AUDIT	7	/* syscall auditing active */
-#define TIF_SECCOMP		8	/* secure computing */
+#define TIF_SYSCALL_INTERCEPT	8	/* Intercept system call */
 #define TIF_SPEC_IB		9	/* Indirect branch speculation mitigation */
 #define TIF_SPEC_FORCE_UPDATE	10	/* Force speculation MSR update in context switch */
 #define TIF_USER_RETURN_NOTIFY	11	/* notify kernel of userspace return */
@@ -112,7 +112,7 @@ struct thread_info {
 #define _TIF_SSBD		(1 << TIF_SSBD)
 #define _TIF_SYSCALL_EMU	(1 << TIF_SYSCALL_EMU)
 #define _TIF_SYSCALL_AUDIT	(1 << TIF_SYSCALL_AUDIT)
-#define _TIF_SECCOMP		(1 << TIF_SECCOMP)
+#define _TIF_SYSCALL_INTERCEPT	(1 << TIF_SYSCALL_INTERCEPT)
 #define _TIF_SPEC_IB		(1 << TIF_SPEC_IB)
 #define _TIF_SPEC_FORCE_UPDATE	(1 << TIF_SPEC_FORCE_UPDATE)
 #define _TIF_USER_RETURN_NOTIFY	(1 << TIF_USER_RETURN_NOTIFY)
include/linux/entry-common.h +1 -5
index efebbffcd5cc..72ce9ca860c6 100644
@@ -21,10 +21,6 @@
 # define _TIF_SYSCALL_TRACEPOINT	(0)
 #endif
 
-#ifndef _TIF_SECCOMP
-# define _TIF_SECCOMP			(0)
-#endif
-
 #ifndef _TIF_SYSCALL_AUDIT
 # define _TIF_SYSCALL_AUDIT		(0)
 #endif
@@ -45,7 +41,7 @@
 #endif
 
 #define SYSCALL_ENTER_WORK						\
-	(_TIF_SYSCALL_TRACE | _TIF_SYSCALL_AUDIT | _TIF_SECCOMP |	\
+	(_TIF_SYSCALL_TRACE | _TIF_SYSCALL_AUDIT | _TIF_SYSCALL_INTERCEPT | \
 	 _TIF_SYSCALL_TRACEPOINT | _TIF_SYSCALL_EMU |			\
 	 ARCH_SYSCALL_ENTER_WORK)
 
kernel/entry/common.c +1 -0
index fcae019158ca..44fd089d59da 100644
@@ -4,6 +4,7 @@
 #include <linux/entry-common.h>
 #include <linux/livepatch.h>
 #include <linux/audit.h>
+#include <linux/syscall_intercept.h>
 
 #define CREATE_TRACE_POINTS
 #include <trace/events/syscalls.h>

kernel/entry/common.c static inline void syscall_enter_audit(struct pt_regs *regs, long syscall) · line 45

Quoted 4 lines
kernel/entry/common.c +1 -0
@@ -41,6 +42,20 @@ static inline void syscall_enter_audit(struct pt_regs *regs, long syscall)
 	}
 }
 
+static inline long do_syscall_intercept(struct pt_regs *regs)

Hey Gabriel,

I think you can drop the pt_regs argument and just have this be

static inline do_syscall_intercept(void)

otherwise

Quoted 34 lines
kernel/entry/common.c +19 -3
+{
+	int sysint_work = READ_ONCE(current->syscall_intercept);
+	int ret;
+
+	if (sysint_work & SYSINT_SECCOMP) {
+		ret = __secure_computing(NULL);
+		if (ret == -1L)
+			return ret;
+	}
+
+	return 0;
+}
+
 static long syscall_trace_enter(struct pt_regs *regs, long syscall,
 				unsigned long ti_work)
 {
@@ -53,9 +68,12 @@ static long syscall_trace_enter(struct pt_regs *regs, long syscall,
 			return -1L;
 	}
 
-	/* Do seccomp after ptrace, to catch any tracer changes. */
-	if (ti_work & _TIF_SECCOMP) {
-		ret = __secure_computing(NULL);
+	/*
+	 * Do syscall interception like seccomp after ptrace, to catch
+	 * any tracer changes.
+	 */
+	if (ti_work & _TIF_SYSCALL_INTERCEPT) {
+		ret = do_syscall_intercept(regs);
 		if (ret == -1L)
 			return ret;
 	}
Signature · 1 line
2.28.0
#17 lore raw

Re: [PATCH v6 2/9] kernel: entry: Support TIF_SYSCAL_INTERCEPT on common entry code

In reply to Gabriel Krisman Bertazi
Quoted 5 lines · Gabriel Krisman Bertazi
include/linux/entry-common.h +0 -0
index efebbffcd5cc..72ce9ca860c6 100644

include/linux/entry-common.h line 26

Quoted 10 lines
include/linux/entry-common.h +0 -4
@@ -21,10 +21,6 @@
 # define _TIF_SYSCALL_TRACEPOINT	(0)
 #endif
 
-#ifndef _TIF_SECCOMP
-# define _TIF_SECCOMP			(0)
-#endif
-
 #ifndef _TIF_SYSCALL_AUDIT
 # define _TIF_SYSCALL_AUDIT		(0)
 #endif

Why doesn't this add:

#ifndef _TIF_SYSCALL_INTERCEPT
#define _TIF_SYSCALL_INTERCEPT (0)
#endif

?

#18 lore raw

Re: [PATCH v6 2/9] kernel: entry: Support TIF_SYSCAL_INTERCEPT on common entry code

Quoted 19 lines · [email protected] writes:
Quoted 5 lines · Gabriel Krisman Bertazi
include/linux/entry-common.h +0 -0
index efebbffcd5cc..72ce9ca860c6 100644

include/linux/entry-common.h line 26

Quoted 10 lines
include/linux/entry-common.h +0 -4
@@ -21,10 +21,6 @@
 # define _TIF_SYSCALL_TRACEPOINT	(0)
 #endif
 
-#ifndef _TIF_SECCOMP
-# define _TIF_SECCOMP			(0)
-#endif
-
 #ifndef _TIF_SYSCALL_AUDIT
 # define _TIF_SYSCALL_AUDIT		(0)
 #endif

Why doesn't this add:

Quoted 4 lines

#ifndef _TIF_SYSCALL_INTERCEPT
#define _TIF_SYSCALL_INTERCEPT (0)
#endif

I will add in the next version. Thanks!

Signature · 1 line
Gabriel Krisman Bertazi
#19 lore raw

PATCH v6 3/9 x86: vdso: Expose sigreturn address on vdso to the kernel

In reply to Gabriel Krisman Bertazi

Syscall user redirection requires the signal trampoline code to not be captured, in order to support returning with a locked selector while avoiding recursion back into the signal handler. For ia-32, which has the trampoline in the vDSO, expose the entry points to the kernel, such that it can avoid dispatching syscalls from that region to userspace.

Changes since V1
- Change return address to bool (Andy)

---
 arch/x86/entry/vdso/vdso2c.c           |  2 ++
 arch/x86/entry/vdso/vdso32/sigreturn.S |  2 ++
 arch/x86/entry/vdso/vma.c              | 15 +++++++++++++++
 arch/x86/include/asm/elf.h             |  1 +
 arch/x86/include/asm/vdso.h            |  2 ++
 5 files changed, 22 insertions(+)
arch/x86/entry/vdso/vdso2c.c +2 -0
index 7380908045c7..2d0f3d8bcc25 100644
@@ -101,6 +101,8 @@ struct vdso_sym required_syms[] = {
 	{"__kernel_sigreturn", true},
 	{"__kernel_rt_sigreturn", true},
 	{"int80_landing_pad", true},
+	{"vdso32_rt_sigreturn_landing_pad", true},
+	{"vdso32_sigreturn_landing_pad", true},
 };
 
 __attribute__((format(printf, 1, 2))) __attribute__((noreturn))
arch/x86/entry/vdso/vdso32/sigreturn.S +2 -0
index c3233ee98a6b..1bd068f72d4c 100644
@@ -18,6 +18,7 @@ __kernel_sigreturn:
 	movl $__NR_sigreturn, %eax
 	SYSCALL_ENTER_KERNEL
 .LEND_sigreturn:
+SYM_INNER_LABEL(vdso32_sigreturn_landing_pad, SYM_L_GLOBAL)
 	nop
 	.size __kernel_sigreturn,.-.LSTART_sigreturn
 
@@ -29,6 +30,7 @@ __kernel_rt_sigreturn:
 	movl $__NR_rt_sigreturn, %eax
 	SYSCALL_ENTER_KERNEL
 .LEND_rt_sigreturn:
+SYM_INNER_LABEL(vdso32_rt_sigreturn_landing_pad, SYM_L_GLOBAL)
 	nop
 	.size __kernel_rt_sigreturn,.-.LSTART_rt_sigreturn
 	.previous
arch/x86/entry/vdso/vma.c +15 -0
index 9185cb1d13b9..3fc323d24824 100644
@@ -436,6 +436,21 @@ int arch_setup_additional_pages(struct linux_binprm *bprm, int uses_interp)
 }
 #endif
 
+bool arch_syscall_is_vdso_sigreturn(struct pt_regs *regs)
+{
+#if defined(CONFIG_X86_32) || defined(CONFIG_IA32_EMULATION)
+	const struct vdso_image *image = current->mm->context.vdso_image;
+	unsigned long vdso = (unsigned long) current->mm->context.vdso;
+
+	if (in_ia32_syscall() && image == &vdso_image_32) {
+		if (regs->ip == vdso + image->sym_vdso32_sigreturn_landing_pad ||
+		    regs->ip == vdso + image->sym_vdso32_rt_sigreturn_landing_pad)
+			return true;
+	}
+#endif
+	return false;
+}
+
 #ifdef CONFIG_X86_64
 static __init int vdso_setup(char *s)
 {
arch/x86/include/asm/elf.h +1 -0
index b9a5d488f1a5..eb41db289fe6 100644
@@ -383,6 +383,7 @@ extern int arch_setup_additional_pages(struct linux_binprm *bprm,
 				       int uses_interp);
 extern int compat_arch_setup_additional_pages(struct linux_binprm *bprm,
 					      int uses_interp);
+extern bool arch_syscall_is_vdso_sigreturn(struct pt_regs *regs);
 #define compat_arch_setup_additional_pages compat_arch_setup_additional_pages
 
 /* Do not change the values. See get_align_mask() */
arch/x86/include/asm/vdso.h +2 -0
index bbcdc7b8f963..589f489dd375 100644
@@ -27,6 +27,8 @@ struct vdso_image {
 	long sym___kernel_rt_sigreturn;
 	long sym___kernel_vsyscall;
 	long sym_int80_landing_pad;
+	long sym_vdso32_sigreturn_landing_pad;
+	long sym_vdso32_rt_sigreturn_landing_pad;
 };
 
 #ifdef CONFIG_X86_64
Signature · 1 line
2.28.0
Reviewed-by on v6 3/9
#20 lore raw

Re: [PATCH v6 3/9] x86: vdso: Expose sigreturn address on vdso to the kernel

Quoted 6 lines · Gabriel Krisman Bertazi

Syscall user redirection requires the signal trampoline code to not be captured, in order to support returning with a locked selector while avoiding recursion back into the signal handler. For ia-32, which has the trampoline in the vDSO, expose the entry points to the kernel, such that it can avoid dispatching syscalls from that region to userspace.

Quoted 6 lines

Changes since V1
- Change return address to bool (Andy)

Look good to me; would anything else benefit from this information?

Signature · 1 line
Kees Cook
#21 lore raw

PATCH v6 4/9 signal: Expose SYS_USER_DISPATCH si_code type

In reply to Gabriel Krisman Bertazi

SYS_USER_DISPATCH will be triggered when a syscall is sent to userspace by the Syscall User Dispatch mechanism. This adjusts eventual BUILD_BUG_ON around the tree.

---
 arch/x86/kernel/signal_compat.c    | 2 +-
 include/uapi/asm-generic/siginfo.h | 3 ++-
 2 files changed, 3 insertions(+), 2 deletions(-)
arch/x86/kernel/signal_compat.c +1 -1
index 9ccbf0576cd0..210aecc6eab9 100644
@@ -31,7 +31,7 @@ static inline void signal_compat_build_tests(void)
 	BUILD_BUG_ON(NSIGBUS  != 5);
 	BUILD_BUG_ON(NSIGTRAP != 5);
 	BUILD_BUG_ON(NSIGCHLD != 6);
-	BUILD_BUG_ON(NSIGSYS  != 1);
+	BUILD_BUG_ON(NSIGSYS  != 2);
 
 	/* This is part of the ABI and can never change in size: */
 	BUILD_BUG_ON(sizeof(compat_siginfo_t) != 128);
include/uapi/asm-generic/siginfo.h +2 -1
index cb3d6c267181..37741908b846 100644
@@ -284,7 +284,8 @@ typedef struct siginfo {
  * SIGSYS si_codes
  */
 #define SYS_SECCOMP	1	/* seccomp triggered */
-#define NSIGSYS		1
+#define SYS_USER_DISPATCH 2	/* syscall user dispatch triggered */
+#define NSIGSYS		2
 
 /*
  * SIGEMT si_codes
Signature · 1 line
2.28.0
Acked-by on v6 4/9
#23 lore raw

Re: [PATCH v6 4/9] signal: Expose SYS_USER_DISPATCH si_code type

In reply to Gabriel Krisman Bertazi
Quoted 5 lines · Gabriel Krisman Bertazi

SYS_USER_DISPATCH will be triggered when a syscall is sent to userspace by the Syscall User Dispatch mechanism. This adjusts eventual BUILD_BUG_ON around the tree.

Yup, this looks good; seccomp is glad to have a new SIGSYS friend. ;)

Signature · 1 line
Kees Cook
#24 lore raw

PATCH v6 5/9 kernel: Implement selective syscall userspace redirection

In reply to Gabriel Krisman Bertazi

Introduce a mechanism to quickly disable/enable syscall handling for a specific process and redirect to userspace via SIGSYS. This is useful for processes with parts that require syscall redirection and parts that don't, but who need to perform this boundary crossing really fast, without paying the cost of a system call to reconfigure syscall handling on each boundary transition. This is particularly important for Windows games running over Wine.

The proposed interface looks like this:

prctl(PR_SET_SYSCALL_USER_DISPATCH, <op>, <start_addr>, <end_addr>, [selector])

The range [<start_addr>,<end_addr>] is a part of the process memory map that is allowed to by-pass the redirection code and dispatch syscalls directly, such that in fast paths a process doesn't need to disable the trap nor the kernel has to check the selector. This is essential to return from SIGSYS to a blocked area without triggering another SIGSYS from rt_sigreturn.

selector is an optional pointer to a char-sized userspace memory region that has a key switch for the mechanism. This key switch is set to either PR_SYS_DISPATCH_ON, PR_SYS_DISPATCH_OFF to enable and disable the redirection without calling the kernel.

The feature is meant to be set per-thread and it is disabled on fork/clone/execv.

Internally, this doesn't add overhead to the syscall hot path, and it requires very little per-architecture support. I avoided using seccomp, even though it duplicates some functionality, due to previous feedback that maybe it shouldn't mix with seccomp since it is not a security mechanism. And obviously, this should never be considered a security mechanism, since any part of the program can by-pass it by using the syscall dispatcher.

For the sysinfo benchmark, which measures the overhead added to executing a native syscall that doesn't require interception, the overhead using only the direct dispatcher region to issue syscalls is pretty much irrelevant. The overhead of using the selector goes around 40ns for a native (unredirected) syscall in my system, and it is (as expected) dominated by the supervisor-mode user-address access. In fact, with SMAP off, the overhead is consistently less than 5ns on my test box.

An example code using this interface can be found at:
https://gitlab.collabora.com/krisman/syscall-disable-personality

Changes since v4:
(Andy Lutomirski)
- Allow sigreturn coming from vDSO
- Exit with SIGSYS instead of SIGSEGV on bad selector (Thomas Gleixner)
- Use sizeof selector in access_ok
- Document usage of __get_user
- Use constant for state value
- Split out x86 parts
- Rebase on top of Gleixner's common entry code
- Don't expose do_syscall_user_dispatch

Changes since v3:
- NTR.

Changes since v2:
(Matthew Wilcox suggestions)
- Drop __user on non-ptr type.
- Move #define closer to similar defs
- Allow a memory region that can dispatch directly (Kees Cook suggestions)
- Improve kconfig summary line
- Move flag cleanup on execve to begin_new_exec
- Hint branch predictor in the syscall path (Me)
- Convert selector to char

Changes since RFC:
(Kees Cook suggestions)
- Don't mention personality while explaining the feature
- Use syscall_get_nr
- Remove header guard on several places
- Convert WARN_ON to WARN_ON_ONCE
- Explicit check for state values
- Rename to syscall user dispatcher

Cc Matthew Wilcox <[email protected]>
Cc Andy Lutomirski <[email protected]>
Cc Paul Gofman <[email protected]>
Cc Kees Cook <[email protected]>
---
 arch/Kconfig                          |  21 ++++++
 fs/exec.c                             |   8 ++
 include/linux/sched.h                 |   2 +
 include/linux/syscall_intercept.h     |   1 +
 include/linux/syscall_user_dispatch.h |  29 ++++++++
 include/uapi/linux/prctl.h            |   5 ++
 kernel/entry/Makefile                 |   1 +
 kernel/entry/common.h                 |  15 ++++
 kernel/entry/syscall_user_dispatch.c  | 101 ++++++++++++++++++++++++++
 kernel/sys.c                          |   5 ++
 10 files changed, 188 insertions(+)
 create mode 100644 include/linux/syscall_user_dispatch.h
 create mode 100644 kernel/entry/common.h
 create mode 100644 kernel/entry/syscall_user_dispatch.c
arch/Kconfig +21 -0
index af14a567b493..6e022c5de020 100644
@@ -468,6 +468,27 @@ config SECCOMP_FILTER
 
 	  See Documentation/userspace-api/seccomp_filter.rst for details.
 
+config HAVE_ARCH_SYSCALL_USER_DISPATCH
+	bool
+	help
+	  An arch should select this symbol if it provides all of these things:
+	  - TIF_SYSCALL_INTERCEPT
+	  - syscall_get_arch
+	  - syscall_rollback
+	  - syscall_get_nr
+	  - SIGSYS siginfo_t support
+	  - arch_syscall_is_vdso_sigreturn
+
+config SYSCALL_USER_DISPATCH
+	bool "Support syscall redirection to userspace dispatcher"
+	depends on HAVE_ARCH_SYSCALL_USER_DISPATCH
+	help
+	  Enable tasks to ask the kernel to redirect syscalls not
+	  issued from a predefined dispatcher back to userspace,
+	  depending on a userspace memory selector.
+
+	  This option is useful to optimize games running over Wine.
+
 config HAVE_ARCH_STACKLEAK
 	bool
 	help
fs/exec.c +8 -0
index a91003e28eaa..9cdf827151c4 100644
@@ -62,6 +62,7 @@
 #include <linux/oom.h>
 #include <linux/compat.h>
 #include <linux/vmalloc.h>
+#include <linux/syscall_intercept.h>
 
 #include <linux/uaccess.h>
 #include <asm/mmu_context.h>
@@ -1418,6 +1419,13 @@ int begin_new_exec(struct linux_binprm * bprm)
 	flush_thread();
 	me->personality &= ~bprm->per_clear;
 
+	/*
+	 * Prevent Syscall User Dispatch from crossing application
+	 * boundaries.  sighand is already unshared, so it is safe to
+	 * use the unlocked version here.
+	 */
+	__clear_tsk_syscall_intercept(me, SYSINT_USER_DISPATCH);
+
 	/*
 	 * We have to apply CLOEXEC before we change whether the process is
 	 * dumpable (in setup_new_exec) to avoid a race with a process in userspace
include/linux/sched.h +2 -0
index 3511c98a7849..47ce09589072 100644
@@ -34,6 +34,7 @@
 #include <linux/rseq.h>
 #include <linux/seqlock.h>
 #include <linux/kcsan.h>
+#include <linux/syscall_user_dispatch.h>
 
 /* task_struct member predeclarations (sorted alphabetically): */
 struct audit_context;
@@ -963,6 +964,7 @@ struct task_struct {
 	struct {
 		unsigned int			syscall_intercept;
 		struct seccomp			seccomp;
+		struct syscall_user_dispatch	syscall_dispatch;
 	};
 
 	/* Thread group tracking: */
include/linux/syscall_intercept.h +1 -0
index 725d157699da..21bc2eb668f3 100644
@@ -10,6 +10,7 @@
 #include <linux/thread_info.h>
 
 #define SYSINT_SECCOMP		0x1
+#define SYSINT_USER_DISPATCH	0x2
 
 #ifdef TIF_SYSCALL_INTERCEPT
 
include/linux/syscall_user_dispatch.h new file +29 -0
new file mode 100644
index 000000000000..f831358bfaab
@@ -0,0 +1,29 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * Copyright (C) 2020 Collabora Ltd.
+ */
+#ifndef _SYSCALL_USER_DISPATCH_H
+#define _SYSCALL_USER_DISPATCH_H
+
+#ifdef CONFIG_SYSCALL_USER_DISPATCH
+struct syscall_user_dispatch {
+	char __user *selector;
+	unsigned long dispatcher_start;
+	unsigned long dispatcher_end;
+};
+
+int set_syscall_user_dispatch(int mode, unsigned long dispatcher_start,
+			      unsigned long dispatcher_end,
+			      char __user *selector);
+#else
+struct syscall_user_dispatch {};
+
+static inline int set_syscall_user_dispatch(int mode, unsigned long dispatcher_start,
+					    unsigned long dispatcher_end,
+					    char __user *selector)
+{
+	return -EINVAL;
+}
+#endif /* CONFIG_SYSCALL_USER_DISPATCH */
+
+#endif /* _SYSCALL_USER_DISPATCH_H */
include/uapi/linux/prctl.h +5 -0
index 07b4f8131e36..96265246383d 100644
@@ -238,4 +238,9 @@ struct prctl_mm_map {
 #define PR_SET_IO_FLUSHER		57
 #define PR_GET_IO_FLUSHER		58
 
+/* Dispatch syscalls to a userspace handler */
+#define PR_SET_SYSCALL_USER_DISPATCH	59
+# define PR_SYS_DISPATCH_OFF		0
+# define PR_SYS_DISPATCH_ON		1
+
 #endif /* _LINUX_PRCTL_H */
kernel/entry/Makefile +1 -0
index 34c8a3f1c735..81182ab5a40c 100644
@@ -11,3 +11,4 @@ CFLAGS_common.o		+= -fno-stack-protector
 
 obj-$(CONFIG_GENERIC_ENTRY) 		+= common.o
 obj-$(CONFIG_KVM_XFER_TO_GUEST_WORK)	+= kvm.o
+obj-$(CONFIG_SYSCALL_USER_DISPATCH)	+= syscall_user_dispatch.o
kernel/entry/common.h new file +15 -0
new file mode 100644
index 000000000000..557ecaa3fb31
@@ -0,0 +1,15 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef _COMMON_H
+#define _COMMON_H
+
+#ifdef CONFIG_SYSCALL_USER_DISPATCH
+int do_syscall_user_dispatch(struct pt_regs *regs);
+#else
+static inline int do_syscall_user_dispatch(struct pt_regs *regs)
+{
+	WARN_ON_ONCE(1);
+	return 0;
+}
+#endif /* CONFIG_SYSCALL_USER_DISPATCH */
+
+#endif
kernel/entry/syscall_user_dispatch.c new file +101 -0

101 lines not on this page · raw message

kernel/sys.c +5 -0
index ab6c409b1159..2786dca862b9 100644
@@ -42,6 +42,7 @@
 #include <linux/syscore_ops.h>
 #include <linux/version.h>
 #include <linux/ctype.h>
+#include <linux/syscall_user_dispatch.h>
 
 #include <linux/compat.h>
 #include <linux/syscalls.h>
@@ -2530,6 +2531,10 @@ SYSCALL_DEFINE5(prctl, int, option, unsigned long, arg2, unsigned long, arg3,
 
 		error = (current->flags & PR_IO_FLUSHER) == PR_IO_FLUSHER;
 		break;
+	case PR_SET_SYSCALL_USER_DISPATCH:
+		error = set_syscall_user_dispatch((int) arg2, arg3, arg4,
+						  (char __user *) arg5);
+		break;
 	default:
 		error = -EINVAL;
 		break;
Signature · 1 line
2.28.0
#25 lore raw

Re: [PATCH v6 5/9] kernel: Implement selective syscall userspace redirection

Quoted 5 lines · Gabriel Krisman Bertazi
kernel/entry/syscall_user_dispatch.c new file +5 -0
+int set_syscall_user_dispatch(int mode, unsigned long dispatcher_start,
+			      unsigned long dispatcher_end, char __user *selector)
+{
+	switch (mode) {
+	case PR_SYS_DISPATCH_OFF:

...

Quoted 1 line
kernel/entry/syscall_user_dispatch.c new file +1 -0
+	case PR_SYS_DISPATCH_ON:

...

Quoted 2 lines
kernel/entry/syscall_user_dispatch.c new file +2 -0
+	default:
+		return -EINVAL;

...

kernel/sys.c SYSCALL_DEFINE5(prctl, int, option, unsigned long, arg2, unsigned long, arg3, · line 2536

Quoted 3 lines
kernel/sys.c +3 -0
+	case PR_SET_SYSCALL_USER_DISPATCH:
+		error = set_syscall_user_dispatch((int) arg2, arg3, arg4,
+						  (char __user *) arg5);

This makes aliases of DISPATCH_OFF and DISPATCH_ON every 4GB throughout the 64-bit space of arg2. I don't think that was intentional (nor desirable). I'd suggest just making 'mode' a long and dropping the cast.

#26 lore raw

Re: [PATCH v6 5/9] kernel: Implement selective syscall userspace redirection

In reply to Gabriel Krisman Bertazi
Quoted 4 lines · Gabriel Krisman Bertazi
include/linux/syscall_user_dispatch.h new file +4 -0
+struct syscall_user_dispatch {
+	char __user *selector;
+	unsigned long dispatcher_start;
+	unsigned long dispatcher_end;

include/linux/syscall_user_dispatch.h

Quoted 10 lines
include/linux/syscall_user_dispatch.h new file +9 -0
+};
 
+int do_syscall_user_dispatch(struct pt_regs *regs)
+{
+	struct syscall_user_dispatch *sd = &current->syscall_dispatch;
+	unsigned long ip = instruction_pointer(regs);
+	char state;
+
+	if (likely(ip >= sd->dispatcher_start && ip <= sd->dispatcher_end))
+		return 0;

If you use {offset,size}, instead of {start,end}, you can write the above like:

	if (ip - sd->dispatcher_offset < sd->dispatcher_size)
		return 0;

which is just a single branch.

#27 lore raw

PATCH v6 6/9 kernel: entry: Support Syscall User Dispatch for common syscall entry

In reply to Gabriel Krisman Bertazi

Syscall User Dispatch (SUD) must take precedence over seccomp, since the use case is emulation (it can be invoked with a different ABI) such that seccomp filtering by syscall number doesn't make sense in the first place. In addition, either the syscall is dispatched back to userspace, in which case there is no resource for seccomp to protect, or the syscall will be executed, and seccomp will execute next.

Regarding ptrace, I experimented with before and after, and while the same ABI argument applies, I felt it was easier to debug if I let ptrace happen for syscalls that are dispatched back to userspace. In addition, doing it after ptrace makes the code in syscall_exit_work slightly simpler, since it doesn't require special handling for this feature.

---
 kernel/entry/common.c | 8 ++++++++
 1 file changed, 8 insertions(+)
kernel/entry/common.c +8 -0
index 44fd089d59da..fdb0c543539d 100644
@@ -6,6 +6,8 @@
 #include <linux/audit.h>
 #include <linux/syscall_intercept.h>
 
+#include "common.h"
+
 #define CREATE_TRACE_POINTS
 #include <trace/events/syscalls.h>
 
@@ -47,6 +49,12 @@ static inline long do_syscall_intercept(struct pt_regs *regs)
 	int sysint_work = READ_ONCE(current->syscall_intercept);
 	int ret;
 
+	if (sysint_work & SYSINT_USER_DISPATCH) {
+		ret = do_syscall_user_dispatch(regs);
+		if (ret == -1L)
+			return ret;
+	}
+
 	if (sysint_work & SYSINT_SECCOMP) {
 		ret = __secure_computing(NULL);
 		if (ret == -1L)
Signature · 1 line
2.28.0
#28 lore raw

Re: [PATCH v6 6/9] kernel: entry: Support Syscall User Dispatch for common syscall entry

Quoted 5 lines · Gabriel Krisman Bertazi

Syscall User Dispatch (SUD) must take precedence over seccomp, since the use case is emulation (it can be invoked with a different ABI) such that seccomp filtering by syscall number doesn't make sense in the first place. In addition, either the syscall is dispatched back to userspace, in which case there is no resource for seccomp to protect, or the

Tbh, I'm torn here. I'm not a super clever attacker but it feels to me that this is still at least a clever way to circumvent a seccomp sandbox.
If I'd be confined by a seccomp profile that would cause me to be SIGKILLed when I try do open() I could prctl() myself to do user dispatch to prevent that from happening, no?

Quoted 41 lines

syscall will be executed, and seccomp will execute next.

Regarding ptrace, I experimented with before and after, and while the same ABI argument applies, I felt it was easier to debug if I let ptrace happen for syscalls that are dispatched back to userspace. In addition, doing it after ptrace makes the code in syscall_exit_work slightly simpler, since it doesn't require special handling for this feature.

---
 kernel/entry/common.c | 8 ++++++++
 1 file changed, 8 insertions(+)
kernel/entry/common.c +8 -0
index 44fd089d59da..fdb0c543539d 100644
@@ -6,6 +6,8 @@
 #include <linux/audit.h>
 #include <linux/syscall_intercept.h>
 
+#include "common.h"
+
 #define CREATE_TRACE_POINTS
 #include <trace/events/syscalls.h>
 
@@ -47,6 +49,12 @@ static inline long do_syscall_intercept(struct pt_regs *regs)
 	int sysint_work = READ_ONCE(current->syscall_intercept);
 	int ret;
 
+	if (sysint_work & SYSINT_USER_DISPATCH) {
+		ret = do_syscall_user_dispatch(regs);
+		if (ret == -1L)
+			return ret;
+	}
+
 	if (sysint_work & SYSINT_SECCOMP) {
 		ret = __secure_computing(NULL);
 		if (ret == -1L)
Signature · 1 line
2.28.0
#29 lore raw

Re: [PATCH v6 6/9] kernel: entry: Support Syscall User Dispatch for common syscall entry

Quoted 11 lines · Christian Brauner
Quoted 5 lines · Gabriel Krisman Bertazi

Syscall User Dispatch (SUD) must take precedence over seccomp, since the use case is emulation (it can be invoked with a different ABI) such that seccomp filtering by syscall number doesn't make sense in the first place. In addition, either the syscall is dispatched back to userspace, in which case there is no resource for seccomp to protect, or the

Quoted 7 lines

Tbh, I'm torn here. I'm not a super clever attacker but it feels to me that this is still at least a clever way to circumvent a seccomp sandbox.
If I'd be confined by a seccomp profile that would cause me to be SIGKILLed when I try do open() I could prctl() myself to do user dispatch to prevent that from happening, no?

Not really, I think. The idea is that you didn’t actually do open(). You did a SYSCALL instruction which meant something else, and the syscall dispatch correctly prevented the kernel from misinterpreting it as open().

#30 lore raw

Re: [PATCH v6 6/9] kernel: entry: Support Syscall User Dispatch for common syscall entry

Quoted 19 lines · Andy Lutomirski
Quoted 11 lines · Christian Brauner
Quoted 5 lines · Gabriel Krisman Bertazi

Syscall User Dispatch (SUD) must take precedence over seccomp, since the use case is emulation (it can be invoked with a different ABI) such that seccomp filtering by syscall number doesn't make sense in the first place. In addition, either the syscall is dispatched back to userspace, in which case there is no resource for seccomp to protect, or the

Quoted 7 lines

Tbh, I'm torn here. I'm not a super clever attacker but it feels to me that this is still at least a clever way to circumvent a seccomp sandbox.
If I'd be confined by a seccomp profile that would cause me to be SIGKILLed when I try do open() I could prctl() myself to do user dispatch to prevent that from happening, no?

Quoted 4 lines

Not really, I think. The idea is that you didn’t actually do open(). You did a SYSCALL instruction which meant something else, and the syscall dispatch correctly prevented the kernel from misinterpreting it as open().

Right, for the case where you're e.g. emulating windows syscalls that's true. I was thinking when you're running natively on Linux: couldn't I first load a seccomp profile "kill me if someone does an open()", then I exec() the target binary and that binary is setup to do prctl(USER_DISPATCH) first thing. I guess, it's ok because as far as I had time to read it this is a nothing or all mechanism, i.e. _all_ system calls are re-routed in contrast to e.g. seccomp where I could do this per-syscall. So for user-dispatch it wouldn't make sense to use it on Linux per se. Still makes me a little uneasy. :)

Christian

maintainer
#31 lore raw

Re: [PATCH v6 6/9] kernel: entry: Support Syscall User Dispatch for common syscall entry

Quoted 26 lines · Christian Brauner
Quoted 19 lines · Andy Lutomirski
Quoted 11 lines · Christian Brauner
Quoted 5 lines · Gabriel Krisman Bertazi

Syscall User Dispatch (SUD) must take precedence over seccomp, since the use case is emulation (it can be invoked with a different ABI) such that seccomp filtering by syscall number doesn't make sense in the first place. In addition, either the syscall is dispatched back to userspace, in which case there is no resource for seccomp to protect, or the

Quoted 7 lines

Tbh, I'm torn here. I'm not a super clever attacker but it feels to me that this is still at least a clever way to circumvent a seccomp sandbox.
If I'd be confined by a seccomp profile that would cause me to be SIGKILLed when I try do open() I could prctl() myself to do user dispatch to prevent that from happening, no?

Quoted 4 lines

Not really, I think. The idea is that you didn’t actually do open(). You did a SYSCALL instruction which meant something else, and the syscall dispatch correctly prevented the kernel from misinterpreting it as open().

Quoted 9 lines

Right, for the case where you're e.g. emulating windows syscalls that's true. I was thinking when you're running natively on Linux: couldn't I first load a seccomp profile "kill me if someone does an open()", then I exec() the target binary and that binary is setup to do prctl(USER_DISPATCH) first thing. I guess, it's ok because as far as I had time to read it this is a nothing or all mechanism, i.e. _all_ system calls are re-routed in contrast to e.g. seccomp where I could do this per-syscall. So for user-dispatch it wouldn't make sense to use it on Linux per se. Still makes me a little uneasy. :)

There's an escape hatch, so processes using this can still make syscalls.

Maybe think about it another way: a process using user dispatch should definitely *not* trigger seccomp user notifiers, errno returns, or ptrace events, since they'll all do the wrong thing. IMO RET_KILL is the same.

Barring some very severe defect, there's no way a program can use user dispatch to escape seccomp -- a program could use user dispatch to allow them to do:

mov $__NR_open, %rax
syscall

without dying despite the presence of a filter that would kill the process if it tried to do open(), but this doesn't bypass the filter at all. The process could just as easily have done:

mov $__NR_open
jmp magic_stub(%rip)

without tripping the filter, since no system call actually happens here.

--Andy

#32 lore raw

Re: [PATCH v6 6/9] kernel: entry: Support Syscall User Dispatch for common syscall entry

In reply to Gabriel Krisman Bertazi
Quoted 7 lines · Gabriel Krisman Bertazi

Syscall User Dispatch (SUD) must take precedence over seccomp, since the use case is emulation (it can be invoked with a different ABI) such that seccomp filtering by syscall number doesn't make sense in the first place. In addition, either the syscall is dispatched back to userspace, in which case there is no resource for seccomp to protect, or the syscall will be executed, and seccomp will execute next.

Quoted 5 lines

Regarding ptrace, I experimented with before and after, and while the same ABI argument applies, I felt it was easier to debug if I let ptrace happen for syscalls that are dispatched back to userspace. In addition, doing it after ptrace makes the code in syscall_exit_work slightly simpler, since it doesn't require special handling for this feature.

I think I'm with Andy that this should be before ptrace(). ptrace() users will attempt to interpret things like they're regular syscalls, and that's definitely not the case.

#33 lore raw

PATCH v6 7/9 x86: Enable Syscall User Dispatch

In reply to Gabriel Krisman Bertazi

Syscall User Dispatch requirements are fully supported in x86. This patch flips the switch, marking it as supported. This was tested against Syscall User Dispatch selftest.

---
 arch/x86/Kconfig | 1 +
 1 file changed, 1 insertion(+)
arch/x86/Kconfig +1 -0
index 7101ac64bb20..56ac8de99021 100644
@@ -150,6 +150,7 @@ config X86
 	select HAVE_ARCH_COMPAT_MMAP_BASES	if MMU && COMPAT
 	select HAVE_ARCH_PREL32_RELOCATIONS
 	select HAVE_ARCH_SECCOMP_FILTER
+	select HAVE_ARCH_SYSCALL_USER_DISPATCH
 	select HAVE_ARCH_THREAD_STRUCT_WHITELIST
 	select HAVE_ARCH_STACKLEAK
 	select HAVE_ARCH_TRACEHOOK
Signature · 1 line
2.28.0
#34 lore raw

Re: [PATCH v6 7/9] x86: Enable Syscall User Dispatch

Quoted 14 lines · Gabriel Krisman Bertazi

Syscall User Dispatch requirements are fully supported in x86. This patch flips the switch, marking it as supported. This was tested against Syscall User Dispatch selftest.

---
 arch/x86/Kconfig | 1 +
 1 file changed, 1 insertion(+)
arch/x86/Kconfig +0 -0
index 7101ac64bb20..56ac8de99021 100644

arch/x86/Kconfig config X86 · line 153

Quoted 4 lines
arch/x86/Kconfig +1 -0
@@ -150,6 +150,7 @@ config X86
 	select HAVE_ARCH_COMPAT_MMAP_BASES	if MMU && COMPAT
 	select HAVE_ARCH_PREL32_RELOCATIONS
 	select HAVE_ARCH_SECCOMP_FILTER
+	select HAVE_ARCH_SYSCALL_USER_DISPATCH

Is this needed at all? I think simply "the architecture uses the generic entry code" is sufficient to enable it. (Especially since there's a top level config for SYSCALL_USER_DISPATCH, it feels like overkill).

Signature · 1 line
Kees Cook
#35 lore raw

Re: [PATCH v6 7/9] x86: Enable Syscall User Dispatch

Quoted 10 lines · Kees Cook
Quoted 9 lines · Gabriel Krisman Bertazi

Syscall User Dispatch requirements are fully supported in x86. This patch flips the switch, marking it as supported. This was tested against Syscall User Dispatch selftest.

---
 arch/x86/Kconfig | 1 +
 1 file changed, 1 insertion(+)
Quoted 13 lines

arch/x86/Kconfig config X86 · line 153

Quoted 9 lines
arch/x86/Kconfig +1 -0
index 7101ac64bb20..56ac8de99021 100644
@@ -150,6 +150,7 @@ config X86
 	select HAVE_ARCH_COMPAT_MMAP_BASES	if MMU && COMPAT
 	select HAVE_ARCH_PREL32_RELOCATIONS
 	select HAVE_ARCH_SECCOMP_FILTER
+	select HAVE_ARCH_SYSCALL_USER_DISPATCH

Is this needed at all? I think simply "the architecture uses the generic entry code" is sufficient to enable it. (Especially since there's a top level config for SYSCALL_USER_DISPATCH, it feels like overkill).

Maybe it is not necessary. The reason I have this is to prevent architectures migrating to the generic entry code from inadvertently starting to support this feature, without thinking in advance whether arch_syscall_is_vdso_sigreturn is needed. If that is not a good reason, I'm happy to drop it.

Signature · 1 line
Gabriel Krisman Bertazi
#36 lore raw

PATCH v6 8/9 selftests: Add kselftest for syscall user dispatch

In reply to Gabriel Krisman Bertazi

Implement functionality tests for syscall user dispatch. In order to make the test portable, refrain from open coding syscall dispatchers and calculating glibc memory ranges.

Changes since v4:
- Update bad selector test to reflect change in API

Changes since v3:
- Sort entry in Makefile
- Add SPDX header
- Use __NR_syscalls if available

---
 tools/testing/selftests/Makefile              |   1 +
 .../syscall_user_dispatch/.gitignore          |   2 +
 .../selftests/syscall_user_dispatch/Makefile  |   9 +
 .../selftests/syscall_user_dispatch/config    |   1 +
 .../syscall_user_dispatch.c                   | 292 ++++++++++++++++++
 5 files changed, 305 insertions(+)
 create mode 100644 tools/testing/selftests/syscall_user_dispatch/.gitignore
 create mode 100644 tools/testing/selftests/syscall_user_dispatch/Makefile
 create mode 100644 tools/testing/selftests/syscall_user_dispatch/config
 create mode 100644 tools/testing/selftests/syscall_user_dispatch/syscall_user_dispatch.c
tools/testing/selftests/Makefile +1 -0
index 9018f45d631d..34ab8579e22f 100644
@@ -56,6 +56,7 @@ TARGETS += sparc64
 TARGETS += splice
 TARGETS += static_keys
 TARGETS += sync
+TARGETS += syscall_user_dispatch
 TARGETS += sysctl
 TARGETS += tc-testing
 TARGETS += timens
tools/testing/selftests/syscall_user_dispatch/.gitignore new file +2 -0
new file mode 100644
index 000000000000..637f08107add
@@ -0,0 +1,2 @@
+# SPDX-License-Identifier: GPL-2.0-only
+syscall_user_dispatch
tools/testing/selftests/syscall_user_dispatch/Makefile new file +9 -0
new file mode 100644
index 000000000000..eeb07a791057
@@ -0,0 +1,9 @@
+# SPDX-License-Identifier: GPL-2.0
+top_srcdir = ../../../..
+INSTALL_HDR_PATH = $(top_srcdir)/usr
+LINUX_HDR_PATH = $(INSTALL_HDR_PATH)/include/
+
+CFLAGS += -Wall -I$(LINUX_HDR_PATH)
+
+TEST_GEN_PROGS := syscall_user_dispatch
+include ../lib.mk
tools/testing/selftests/syscall_user_dispatch/config new file +1 -0
new file mode 100644
index 000000000000..22c4dfe167ca
@@ -0,0 +1 @@
+CONFIG_SYSCALL_USER_DISPATCH=y
tools/testing/selftests/syscall_user_dispatch/syscall_user_dispatch.c new file +292 -0

292 lines not on this page · raw message

Signature · 1 line
2.28.0
Acked-by on v6 8/9
#37 lore raw

Re: [PATCH v6 8/9] selftests: Add kselftest for syscall user dispatch

Quoted 7 lines · Gabriel Krisman Bertazi

Implement functionality tests for syscall user dispatch. In order to make the test portable, refrain from open coding syscall dispatchers and calculating glibc memory ranges.

Changes since v4:
- Update bad selector test to reflect change in API

Quoted 6 lines

Changes since v3:
- Sort entry in Makefile
- Add SPDX header
- Use __NR_syscalls if available

This passes, looks good. Thank you again for the self tests!

Signature · 1 line
Kees Cook
#38 lore raw

PATCH v6 9/9 doc: Document Syscall User Dispatch

In reply to Gabriel Krisman Bertazi

Explain the interface, provide some background and security notes.

---
 .../admin-guide/syscall-user-dispatch.rst     | 87 +++++++++++++++++++
 1 file changed, 87 insertions(+)
 create mode 100644 Documentation/admin-guide/syscall-user-dispatch.rst
Documentation/admin-guide/syscall-user-dispatch.rst new file +87 -0
new file mode 100644
index 000000000000..96616660fded
@@ -0,0 +1,87 @@
+.. SPDX-License-Identifier: GPL-2.0
+
+=====================
+Syscall User Dispatch
+=====================
+
+Background
+----------
+
+Compatibility layers like Wine need a way to efficiently emulate system
+calls of only a part of their process - the part that has the
+incompatible code - while being able to execute native syscalls without
+a high performance penalty on the native part of the process.  Seccomp
+falls short on this task, since it has limited support to efficiently
+filter syscalls based on memory regions, and it doesn't support removing
+filters.  Therefore a new mechanism is necessary.
+
+Syscall User Dispatch brings the filtering of the syscall dispatcher
+address back to userspace.  The application is in control of a flip
+switch, indicating the current personality of the process.  A
+multiple-personality application can then flip the switch without
+invoking the kernel, when crossing the compatibility layer API
+boundaries, to enable/disable the syscall redirection and execute
+syscalls directly (disabled) or send them to be emulated in userspace
+through a SIGSYS.
+
+The goal of this design is to provide very quick compatibility layer
+boundary crosses, which is achieved by not executing a syscall to change
+personality every time the compatibility layer executes.  Instead, a
+userspace memory region exposed to the kernel indicates the current
+personality, and the application simply modifies that variable to
+configure the mechanism.
+
+There is a relatively high cost associated with handling signals on most
+architectures, like x86, but at least for Wine, syscalls issued by
+native Windows code are currently not known to be a performance problem,
+since they are quite rare, at least for modern gaming applications.
+
+Since this mechanism is designed to capture syscalls issued by
+non-native applications, it must function on syscalls whose invocation
+ABI is completely unexpected to Linux.  Syscall User Dispatch, therefore
+doesn't rely on any of the syscall ABI to make the filtering.  It uses
+only the syscall dispatcher address and the userspace key.
+
+Interface
+---------
+
+A process can setup this mechanism on supported kernels
+CONFIG_SYSCALL_USER_DISPATCH) by executing the following prctl:
+
+   prctl(PR_SET_SYSCALL_USER_DISPATCH, <op>, <start_addr>, <end_addr>, [selector])
+
+<op> is either PR_SYS_DISPATCH_ON or PR_SYS_DISPATCH_OFF, to enable and
+disable the mechanism globally for that thread.  When
+PR_SYS_DISPATCH_OFF is used, the other fields must be zero.
+
+<start_addr> and <end_addr> delimit a closed memory region interval from
+which syscalls are always executed directly, regardless of the userspace
+selector.  This provides a fast path for the C library, which includes
+the most common syscall dispatchers in the native code applications, and
+also provides a way for the signal handler to return without triggering
+a nested SIGSYS on (rt_)sigreturn.  Users of this interface should make
+sure that at least the signal trampoline code is included in this
+region. In addition, for syscalls that implement the trampoline code on
+the vDSO, that trampoline is never intercepted.
+
+[selector] is a pointer to a char-sized region in the process memory
+region, that provides a quick way to enable disable syscall redirection
+thread-wide, without the need to invoke the kernel directly.  selector
+can be set to PR_SYS_DISPATCH_ON or PR_SYS_DISPATCH_OFF.  Any other
+value should terminate the program with a SIGSYS.
+
+Security Notes
+--------------
+
+Syscall User Dispatch provides functionality for compatibility layers to
+quickly capture system calls issued by a non-native part of the
+application, while not impacting the Linux native regions of the
+process.  It is not a mechanism for sandboxing system calls, and it
+should not be seen as a security mechanism, since it is trivial for a
+malicious application to subvert the mechanism by jumping to an allowed
+dispatcher region prior to executing the syscall, or to discover the
+address and modify the selector value.  If the use case requires any
+kind of security sandboxing, Seccomp should be used instead.
+
+Any fork or exec of the existing process resets the mechanism to
+PR_SYS_DISPATCH_OFF.
Signature · 1 line
2.28.0