Skip to contentSkip to conversation

x86: two-phase syscall tracing and seccomp fastpath

PATCH In review Andy Lutomirski 5 participants 33 messages Sep 5, 2014 – Feb 7, 2015

Subsystems (5)
lore
Share & tools
lore

Andy Lutomirski

author reviewer
#0 lore raw

v5 cover x86: two-phase syscall tracing and seccomp fastpath

This applies to:
git://git.kernel.org/pub/scm/linux/kernel/git/kees/linux.git seccomp-fastpath

Gitweb:
https://git.kernel.org/cgit/linux/kernel/git/kees/linux.git/log/?h=seccomp/fastpath

This is both a cleanup and a speedup. It reduces overhead due to installing a trivial seccomp filter by 87%. The speedup comes from avoiding the full syscall tracing mechanism for filters that don't return SECCOMP_RET_TRACE.

This series depends on splitting the seccomp hooks into two phases. The first phase evaluates the filter; it can skip syscalls, allow them, kill the calling task, or pass a u32 to the second phase. The second phase requires a full tracing context, and it sends ptrace events if necessary. The seccomp core part is in Kees' seccomp/fastpath tree.

These patches implement a similar split for the x86 syscall entry work. The C callback is invoked in two phases: the first has only a partial frame, and it can request phase 2 processing with a full frame.

Finally, I switch the 64-bit system_call code to use the new split entry work. This is a net deletion of assembly code: it replaces all of the audit entry muck.

In the process, I fixed some bugs.

If this is acceptable, someone can do the same tweak for the ia32entry and entry_32 code.

This passes all seccomp tests that I know of.

Changes from v4:
- Rebased (which seems to have been a no-op)
- Fixed embarrassing bug that broke allnoconfig
(patch 3 was missing an ifdef).

Changes from v3:
- Dropped the core seccomp changes from the email -- Kees has applied them.
- Add patch 2 (the TIF_NOHZ change).
- Fix TIF_NOHZ in the two-phase entry code (thanks, Oleg).

Changes from v2:
- Fixed 32-bit x86 build (and the tests pass).
- Put the doc patch where it belongs.

Changes from v1:
- Rebased on top of Kees' shiny new seccomp tree (no effect on the x86 part).
- Improved patch 6 vs patch 7 split (thanks Alexei!)
- Fixed bogus -ENOSYS in patch 5 (thanks Kees!)
- Improved changelog message in patch 6.

Changes from RFC version:
- The first three patches are more or less the same
- The rest is more or less a rewrite

Andy Lutomirski (5):
x86,x32,audit: Fix x32's AUDIT_ARCH wrt audit
x86,entry: Only call user_exit if TIF_NOHZ
x86: Split syscall_trace_enter into two phases x86_64,entry: Treat regs->ax the same in fastpath and slowpath
syscalls
x86_64,entry: Use split-phase syscall_trace_enter for 64-bit syscalls

 arch/x86/include/asm/calling.h |   6 +-
 arch/x86/include/asm/ptrace.h  |   5 ++
 arch/x86/kernel/entry_64.S     |  51 +++++--------
 arch/x86/kernel/ptrace.c       | 165 +++++++++++++++++++++++++++++++++--------
 4 files changed, 164 insertions(+), 63 deletions(-)
Signature · 1 line
1.9.3
author reviewer
#1 lore raw

PATCH v5 1/5 x86,x32,audit: Fix x32's AUDIT_ARCH wrt audit

is_compat_task() is the wrong check for audit arch; the check should be is_ia32_task(): x32 syscalls should be AUDIT_ARCH_X86_64, not AUDIT_ARCH_I386.

CONFIG_AUDITSYSCALL is currently incompatible with x32, so this has no visible effect.

---
 arch/x86/kernel/ptrace.c | 11 +----------
 1 file changed, 1 insertion(+), 10 deletions(-)
arch/x86/kernel/ptrace.c +1 -10
index 93c182a00506..39296d25708c 100644
@@ -1441,15 +1441,6 @@ void send_sigtrap(struct task_struct *tsk, struct pt_regs *regs,
 	force_sig_info(SIGTRAP, &info, tsk);
 }
 
-
-#ifdef CONFIG_X86_32
-# define IS_IA32	1
-#elif defined CONFIG_IA32_EMULATION
-# define IS_IA32	is_compat_task()
-#else
-# define IS_IA32	0
-#endif
-
 /*
  * We must return the syscall number to actually look up in the table.
  * This can be -1L to skip running any syscall at all.
@@ -1487,7 +1478,7 @@ long syscall_trace_enter(struct pt_regs *regs)
 	if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT)))
 		trace_sys_enter(regs, regs->orig_ax);
 
-	if (IS_IA32)
+	if (is_ia32_task())
 		audit_syscall_entry(AUDIT_ARCH_I386,
 				    regs->orig_ax,
 				    regs->bx, regs->cx,
Signature · 1 line
1.9.3
#2 lore raw

[tip:x86/seccomp] x86, x32, audit: Fix x32's AUDIT_ARCH wrt audit

x86, x32, audit: Fix x32's AUDIT_ARCH wrt audit
Commit-ID:  81f49a8fd7088cfcb588d182eeede862c0e3303e
Gitweb:     http://git.kernel.org/tip/81f49a8fd7088cfcb588d182eeede862c0e3303e
Author:     Andy Lutomirski <[email protected]>
AuthorDate: Fri, 5 Sep 2014 15:13:52 -0700
Committer:  H. Peter Anvin <[email protected]>
CommitDate: Mon, 8 Sep 2014 14:13:55 -0700

x86, x32, audit: Fix x32's AUDIT_ARCH wrt audit

is_compat_task() is the wrong check for audit arch; the check should be is_ia32_task(): x32 syscalls should be AUDIT_ARCH_X86_64, not AUDIT_ARCH_I386.

CONFIG_AUDITSYSCALL is currently incompatible with x32, so this has no visible effect.

---
 arch/x86/kernel/ptrace.c | 11 +----------
 1 file changed, 1 insertion(+), 10 deletions(-)
arch/x86/kernel/ptrace.c +1 -10
index 93c182a..39296d2 100644
@@ -1441,15 +1441,6 @@ void send_sigtrap(struct task_struct *tsk, struct pt_regs *regs,
 	force_sig_info(SIGTRAP, &info, tsk);
 }
 
-
-#ifdef CONFIG_X86_32
-# define IS_IA32	1
-#elif defined CONFIG_IA32_EMULATION
-# define IS_IA32	is_compat_task()
-#else
-# define IS_IA32	0
-#endif
-
 /*
  * We must return the syscall number to actually look up in the table.
  * This can be -1L to skip running any syscall at all.
@@ -1487,7 +1478,7 @@ long syscall_trace_enter(struct pt_regs *regs)
 	if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT)))
 		trace_sys_enter(regs, regs->orig_ax);
 
-	if (IS_IA32)
+	if (is_ia32_task())
 		audit_syscall_entry(AUDIT_ARCH_I386,
 				    regs->orig_ax,
 				    regs->bx, regs->cx,
author reviewer
#3 lore raw

PATCH v5 2/5 x86,entry: Only call user_exit if TIF_NOHZ

In reply to Andy Lutomirski

The RCU context tracking code requires that arch code call user_exit() on any entry into kernel code if TIF_NOHZ is set. This patch adds a check for TIF_NOHZ and a comment to the syscall entry tracing code.

The main purpose of this patch is to make the code easier to follow:
one can read the body of user_exit and of every function it calls without finding any explanation of why it's called for traced syscalls but not for untraced syscalls. This makes it clear when user_exit() is necessary.

Cc Frederic Weisbecker <[email protected]>
---
 arch/x86/kernel/ptrace.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
arch/x86/kernel/ptrace.c +6 -1
index 39296d25708c..bbf338a04a5d 100644
@@ -1449,7 +1449,12 @@ long syscall_trace_enter(struct pt_regs *regs)
 {
 	long ret = 0;
 
-	user_exit();
+	/*
+	 * If TIF_NOHZ is set, we are required to call user_exit() before
+	 * doing anything that could touch RCU.
+	 */
+	if (test_thread_flag(TIF_NOHZ))
+		user_exit();
 
 	/*
 	 * If we stepped into a sysenter/syscall insn, it trapped in
Signature · 1 line
1.9.3
#4 lore raw

[tip:x86/seccomp] x86, entry: Only call user_exit if TIF_NOHZ

x86, entry: Only call user_exit if TIF_NOHZ
Commit-ID:  fd143b210e685f0c4b37895f03fb79cd0555b00d
Gitweb:     http://git.kernel.org/tip/fd143b210e685f0c4b37895f03fb79cd0555b00d
Author:     Andy Lutomirski <[email protected]>
AuthorDate: Fri, 5 Sep 2014 15:13:53 -0700
Committer:  H. Peter Anvin <[email protected]>
CommitDate: Mon, 8 Sep 2014 14:13:59 -0700

x86, entry: Only call user_exit if TIF_NOHZ

The RCU context tracking code requires that arch code call user_exit() on any entry into kernel code if TIF_NOHZ is set. This patch adds a check for TIF_NOHZ and a comment to the syscall entry tracing code.

The main purpose of this patch is to make the code easier to follow:
one can read the body of user_exit and of every function it calls without finding any explanation of why it's called for traced syscalls but not for untraced syscalls. This makes it clear when user_exit() is necessary.

---
 arch/x86/kernel/ptrace.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
arch/x86/kernel/ptrace.c +6 -1
index 39296d2..bbf338a 100644
@@ -1449,7 +1449,12 @@ long syscall_trace_enter(struct pt_regs *regs)
 {
 	long ret = 0;
 
-	user_exit();
+	/*
+	 * If TIF_NOHZ is set, we are required to call user_exit() before
+	 * doing anything that could touch RCU.
+	 */
+	if (test_thread_flag(TIF_NOHZ))
+		user_exit();
 
 	/*
 	 * If we stepped into a sysenter/syscall insn, it trapped in
author reviewer
#5 lore raw

PATCH v5 3/5 x86: Split syscall_trace_enter into two phases

In reply to Andy Lutomirski

This splits syscall_trace_enter into syscall_trace_enter_phase1 and syscall_trace_enter_phase2. Only phase 2 has full pt_regs, and only phase 2 is permitted to modify any of pt_regs except for orig_ax.

The intent is that phase 1 can be called from the syscall fast path.

In this implementation, phase1 can handle any combination of TIF_NOHZ (RCU context tracking), TIF_SECCOMP, and TIF_SYSCALL_AUDIT, unless seccomp requests a ptrace event, in which case phase2 is forced.

In principle, this could yield a big speedup for TIF_NOHZ as well as for TIF_SECCOMP if syscall exit work were similarly split up.

---
 arch/x86/include/asm/ptrace.h |   5 ++
 arch/x86/kernel/ptrace.c      | 157 +++++++++++++++++++++++++++++++++++-------
 2 files changed, 138 insertions(+), 24 deletions(-)
arch/x86/include/asm/ptrace.h +5 -0
index 6205f0c434db..86fc2bb82287 100644
@@ -75,6 +75,11 @@ convert_ip_to_linear(struct task_struct *child, struct pt_regs *regs);
 extern void send_sigtrap(struct task_struct *tsk, struct pt_regs *regs,
 			 int error_code, int si_code);
 
+
+extern unsigned long syscall_trace_enter_phase1(struct pt_regs *, u32 arch);
+extern long syscall_trace_enter_phase2(struct pt_regs *, u32 arch,
+				       unsigned long phase1_result);
+
 extern long syscall_trace_enter(struct pt_regs *);
 extern void syscall_trace_leave(struct pt_regs *);
 
arch/x86/kernel/ptrace.c +133 -24

193 lines not on this page · raw message

Signature · 1 line
1.9.3
#6 lore raw

[tip:x86/seccomp] x86: Split syscall_trace_enter into two phases

x86: Split syscall_trace_enter into two phases
Commit-ID:  e0ffbaabc46db508b8717f023c0ce03b980eefac
Gitweb:     http://git.kernel.org/tip/e0ffbaabc46db508b8717f023c0ce03b980eefac
Author:     Andy Lutomirski <[email protected]>
AuthorDate: Fri, 5 Sep 2014 15:13:54 -0700
Committer:  H. Peter Anvin <[email protected]>
CommitDate: Mon, 8 Sep 2014 14:14:03 -0700

x86: Split syscall_trace_enter into two phases

This splits syscall_trace_enter into syscall_trace_enter_phase1 and syscall_trace_enter_phase2. Only phase 2 has full pt_regs, and only phase 2 is permitted to modify any of pt_regs except for orig_ax.

The intent is that phase 1 can be called from the syscall fast path.

In this implementation, phase1 can handle any combination of TIF_NOHZ (RCU context tracking), TIF_SECCOMP, and TIF_SYSCALL_AUDIT, unless seccomp requests a ptrace event, in which case phase2 is forced.

In principle, this could yield a big speedup for TIF_NOHZ as well as for TIF_SECCOMP if syscall exit work were similarly split up.

---
 arch/x86/include/asm/ptrace.h |   5 ++
 arch/x86/kernel/ptrace.c      | 157 +++++++++++++++++++++++++++++++++++-------
 2 files changed, 138 insertions(+), 24 deletions(-)
arch/x86/include/asm/ptrace.h +5 -0
index 6205f0c..86fc2bb 100644
@@ -75,6 +75,11 @@ convert_ip_to_linear(struct task_struct *child, struct pt_regs *regs);
 extern void send_sigtrap(struct task_struct *tsk, struct pt_regs *regs,
 			 int error_code, int si_code);
 
+
+extern unsigned long syscall_trace_enter_phase1(struct pt_regs *, u32 arch);
+extern long syscall_trace_enter_phase2(struct pt_regs *, u32 arch,
+				       unsigned long phase1_result);
+
 extern long syscall_trace_enter(struct pt_regs *);
 extern void syscall_trace_leave(struct pt_regs *);
 
arch/x86/kernel/ptrace.c +133 -24

193 lines not on this page · raw message

#7 lore raw

Re: [PATCH v5 3/5] x86: Split syscall_trace_enter into two phases

In reply to Andy Lutomirski

Hi,

Quoted 3 lines · Andy Lutomirski

This splits syscall_trace_enter into syscall_trace_enter_phase1 and syscall_trace_enter_phase2. Only phase 2 has full pt_regs, and only phase 2 is permitted to modify any of pt_regs except for orig_ax.

This breaks ptrace, see below.

Quoted 118 lines

The intent is that phase 1 can be called from the syscall fast path.

In this implementation, phase1 can handle any combination of TIF_NOHZ (RCU context tracking), TIF_SECCOMP, and TIF_SYSCALL_AUDIT, unless seccomp requests a ptrace event, in which case phase2 is forced.

In principle, this could yield a big speedup for TIF_NOHZ as well as for TIF_SECCOMP if syscall exit work were similarly split up.

---
 arch/x86/include/asm/ptrace.h |   5 ++
 arch/x86/kernel/ptrace.c      | 157 +++++++++++++++++++++++++++++++++++-------
 2 files changed, 138 insertions(+), 24 deletions(-)
arch/x86/include/asm/ptrace.h +5 -0
index 6205f0c434db..86fc2bb82287 100644
@@ -75,6 +75,11 @@ convert_ip_to_linear(struct task_struct *child, struct pt_regs *regs);
 extern void send_sigtrap(struct task_struct *tsk, struct pt_regs *regs,
 			 int error_code, int si_code);
 
+
+extern unsigned long syscall_trace_enter_phase1(struct pt_regs *, u32 arch);
+extern long syscall_trace_enter_phase2(struct pt_regs *, u32 arch,
+				       unsigned long phase1_result);
+
 extern long syscall_trace_enter(struct pt_regs *);
 extern void syscall_trace_leave(struct pt_regs *);
 
arch/x86/kernel/ptrace.c +64 -5
index bbf338a04a5d..29576c244699 100644
@@ -1441,20 +1441,126 @@ void send_sigtrap(struct task_struct *tsk, struct pt_regs *regs,
 	force_sig_info(SIGTRAP, &info, tsk);
 }
 
+static void do_audit_syscall_entry(struct pt_regs *regs, u32 arch)
+{
+#ifdef CONFIG_X86_64
+	if (arch == AUDIT_ARCH_X86_64) {
+		audit_syscall_entry(arch, regs->orig_ax, regs->di,
+				    regs->si, regs->dx, regs->r10);
+	} else
+#endif
+	{
+		audit_syscall_entry(arch, regs->orig_ax, regs->bx,
+				    regs->cx, regs->dx, regs->si);
+	}
+}
+
 /*
- * We must return the syscall number to actually look up in the table.
- * This can be -1L to skip running any syscall at all.
+ * We can return 0 to resume the syscall or anything else to go to phase
+ * 2.  If we resume the syscall, we need to put something appropriate in
+ * regs->orig_ax.
+ *
+ * NB: We don't have full pt_regs here, but regs->orig_ax and regs->ax
+ * are fully functional.
+ *
+ * For phase 2's benefit, our return value is:
+ * 0:			resume the syscall
+ * 1:			go to phase 2; no seccomp phase 2 needed
+ * anything else:	go to phase 2; pass return value to seccomp
  */
-long syscall_trace_enter(struct pt_regs *regs)
+unsigned long syscall_trace_enter_phase1(struct pt_regs *regs, u32 arch)
 {
-	long ret = 0;
+	unsigned long ret = 0;
+	u32 work;
+
+	BUG_ON(regs != task_pt_regs(current));
+
+	work = ACCESS_ONCE(current_thread_info()->flags) &
+		_TIF_WORK_SYSCALL_ENTRY;
 
 	/*
 	 * If TIF_NOHZ is set, we are required to call user_exit() before
 	 * doing anything that could touch RCU.
 	 */
-	if (test_thread_flag(TIF_NOHZ))
+	if (work & _TIF_NOHZ) {
 		user_exit();
+		work &= ~TIF_NOHZ;
+	}
+
+#ifdef CONFIG_SECCOMP
+	/*
+	 * Do seccomp first -- it should minimize exposure of other
+	 * code, and keeping seccomp fast is probably more valuable
+	 * than the rest of this.
+	 */
+	if (work & _TIF_SECCOMP) {
+		struct seccomp_data sd;
+
+		sd.arch = arch;
+		sd.nr = regs->orig_ax;
+		sd.instruction_pointer = regs->ip;
+#ifdef CONFIG_X86_64
+		if (arch == AUDIT_ARCH_X86_64) {
+			sd.args[0] = regs->di;
+			sd.args[1] = regs->si;
+			sd.args[2] = regs->dx;
+			sd.args[3] = regs->r10;
+			sd.args[4] = regs->r8;
+			sd.args[5] = regs->r9;
+		} else
+#endif
+		{
+			sd.args[0] = regs->bx;
+			sd.args[1] = regs->cx;
+			sd.args[2] = regs->dx;
+			sd.args[3] = regs->si;

arch/x86/kernel/ptrace.c void send_sigtrap(struct task_struct *tsk, struct pt_regs *regs, · line 1526

Quoted 10 lines
arch/x86/kernel/ptrace.c +10 -0
+			sd.args[4] = regs->di;
+			sd.args[5] = regs->bp;
+		}
+
+		BUILD_BUG_ON(SECCOMP_PHASE1_OK != 0);
+		BUILD_BUG_ON(SECCOMP_PHASE1_SKIP != 1);
+
+		ret = seccomp_phase1(&sd);
+		if (ret == SECCOMP_PHASE1_SKIP) {
+			regs->orig_ax = -1;

How the tracer is expected to get the correct syscall number after that?

Signature · 1 line
ldv
#8 lore raw

Re: [PATCH v5 3/5] x86: Split syscall_trace_enter into two phases

Quoted 131 lines · Dmitry V. Levin

Hi,

Quoted 3 lines · Andy Lutomirski

This splits syscall_trace_enter into syscall_trace_enter_phase1 and syscall_trace_enter_phase2. Only phase 2 has full pt_regs, and only phase 2 is permitted to modify any of pt_regs except for orig_ax.

This breaks ptrace, see below.

Quoted 122 lines

The intent is that phase 1 can be called from the syscall fast path.

In this implementation, phase1 can handle any combination of TIF_NOHZ (RCU context tracking), TIF_SECCOMP, and TIF_SYSCALL_AUDIT, unless seccomp requests a ptrace event, in which case phase2 is forced.

In principle, this could yield a big speedup for TIF_NOHZ as well as for TIF_SECCOMP if syscall exit work were similarly split up.

---
 arch/x86/include/asm/ptrace.h |   5 ++
 arch/x86/kernel/ptrace.c      | 157 +++++++++++++++++++++++++++++++++++-------
 2 files changed, 138 insertions(+), 24 deletions(-)
arch/x86/include/asm/ptrace.h +5 -0
index 6205f0c434db..86fc2bb82287 100644
@@ -75,6 +75,11 @@ convert_ip_to_linear(struct task_struct *child, struct pt_regs *regs);
 extern void send_sigtrap(struct task_struct *tsk, struct pt_regs *regs,
                       int error_code, int si_code);
 
+
+extern unsigned long syscall_trace_enter_phase1(struct pt_regs *, u32 arch);
+extern long syscall_trace_enter_phase2(struct pt_regs *, u32 arch,
+                                    unsigned long phase1_result);
+
 extern long syscall_trace_enter(struct pt_regs *);
 extern void syscall_trace_leave(struct pt_regs *);
 
arch/x86/kernel/ptrace.c +68 -5
index bbf338a04a5d..29576c244699 100644
@@ -1441,20 +1441,126 @@ void send_sigtrap(struct task_struct *tsk, struct pt_regs *regs,
      force_sig_info(SIGTRAP, &info, tsk);
 }
 
+static void do_audit_syscall_entry(struct pt_regs *regs, u32 arch)
+{
+#ifdef CONFIG_X86_64
+     if (arch == AUDIT_ARCH_X86_64) {
+             audit_syscall_entry(arch, regs->orig_ax, regs->di,
+                                 regs->si, regs->dx, regs->r10);
+     } else
+#endif
+     {
+             audit_syscall_entry(arch, regs->orig_ax, regs->bx,
+                                 regs->cx, regs->dx, regs->si);
+     }
+}
+
 /*
- * We must return the syscall number to actually look up in the table.
- * This can be -1L to skip running any syscall at all.
+ * We can return 0 to resume the syscall or anything else to go to phase
+ * 2.  If we resume the syscall, we need to put something appropriate in
+ * regs->orig_ax.
+ *
+ * NB: We don't have full pt_regs here, but regs->orig_ax and regs->ax
+ * are fully functional.
+ *
+ * For phase 2's benefit, our return value is:
+ * 0:                        resume the syscall
+ * 1:                        go to phase 2; no seccomp phase 2 needed
+ * anything else:    go to phase 2; pass return value to seccomp
  */
-long syscall_trace_enter(struct pt_regs *regs)
+unsigned long syscall_trace_enter_phase1(struct pt_regs *regs, u32 arch)
 {
-     long ret = 0;
+     unsigned long ret = 0;
+     u32 work;
+
+     BUG_ON(regs != task_pt_regs(current));
+
+     work = ACCESS_ONCE(current_thread_info()->flags) &
+             _TIF_WORK_SYSCALL_ENTRY;
 
      /*
       * If TIF_NOHZ is set, we are required to call user_exit() before
       * doing anything that could touch RCU.
       */
-     if (test_thread_flag(TIF_NOHZ))
+     if (work & _TIF_NOHZ) {
              user_exit();
+             work &= ~TIF_NOHZ;
+     }
+
+#ifdef CONFIG_SECCOMP
+     /*
+      * Do seccomp first -- it should minimize exposure of other
+      * code, and keeping seccomp fast is probably more valuable
+      * than the rest of this.
+      */
+     if (work & _TIF_SECCOMP) {
+             struct seccomp_data sd;
+
+             sd.arch = arch;
+             sd.nr = regs->orig_ax;
+             sd.instruction_pointer = regs->ip;
+#ifdef CONFIG_X86_64
+             if (arch == AUDIT_ARCH_X86_64) {
+                     sd.args[0] = regs->di;
+                     sd.args[1] = regs->si;
+                     sd.args[2] = regs->dx;
+                     sd.args[3] = regs->r10;
+                     sd.args[4] = regs->r8;
+                     sd.args[5] = regs->r9;
+             } else
+#endif
+             {
+                     sd.args[0] = regs->bx;
+                     sd.args[1] = regs->cx;
+                     sd.args[2] = regs->dx;
+                     sd.args[3] = regs->si;
+                     sd.args[4] = regs->di;
+                     sd.args[5] = regs->bp;
+             }
+
Quoted 8 lines

arch/x86/kernel/ptrace.c void send_sigtrap(struct task_struct *tsk, struct pt_regs *regs, · line 1526

Quoted 6 lines
arch/x86/kernel/ptrace.c +6 -0
+             BUILD_BUG_ON(SECCOMP_PHASE1_OK != 0);
+             BUILD_BUG_ON(SECCOMP_PHASE1_SKIP != 1);
+
+             ret = seccomp_phase1(&sd);
+             if (ret == SECCOMP_PHASE1_SKIP) {
+                     regs->orig_ax = -1;

How the tracer is expected to get the correct syscall number after that?

There shouldn't be a tracer if a skip is encountered. (A seccomp skip would skip ptrace.) This behavior hasn't changed, but maybe I don't see what you mean? (I haven't encountered any problems with syscall tracing as a result of these changes.)

-Kees

Signature · 2 lines
Kees Cook
Chrome OS Security
#9 lore raw

Re: [PATCH v5 3/5] x86: Split syscall_trace_enter into two phases

Quoted 141 lines · Kees Cook
Quoted 131 lines · Dmitry V. Levin

Hi,

Quoted 3 lines · Andy Lutomirski

This splits syscall_trace_enter into syscall_trace_enter_phase1 and syscall_trace_enter_phase2. Only phase 2 has full pt_regs, and only phase 2 is permitted to modify any of pt_regs except for orig_ax.

This breaks ptrace, see below.

Quoted 122 lines

The intent is that phase 1 can be called from the syscall fast path.

In this implementation, phase1 can handle any combination of TIF_NOHZ (RCU context tracking), TIF_SECCOMP, and TIF_SYSCALL_AUDIT, unless seccomp requests a ptrace event, in which case phase2 is forced.

In principle, this could yield a big speedup for TIF_NOHZ as well as for TIF_SECCOMP if syscall exit work were similarly split up.

---
 arch/x86/include/asm/ptrace.h |   5 ++
 arch/x86/kernel/ptrace.c      | 157 +++++++++++++++++++++++++++++++++++-------
 2 files changed, 138 insertions(+), 24 deletions(-)
arch/x86/include/asm/ptrace.h +5 -0
index 6205f0c434db..86fc2bb82287 100644
@@ -75,6 +75,11 @@ convert_ip_to_linear(struct task_struct *child, struct pt_regs *regs);
 extern void send_sigtrap(struct task_struct *tsk, struct pt_regs *regs,
                       int error_code, int si_code);
 
+
+extern unsigned long syscall_trace_enter_phase1(struct pt_regs *, u32 arch);
+extern long syscall_trace_enter_phase2(struct pt_regs *, u32 arch,
+                                    unsigned long phase1_result);
+
 extern long syscall_trace_enter(struct pt_regs *);
 extern void syscall_trace_leave(struct pt_regs *);
 
arch/x86/kernel/ptrace.c +68 -5
index bbf338a04a5d..29576c244699 100644
@@ -1441,20 +1441,126 @@ void send_sigtrap(struct task_struct *tsk, struct pt_regs *regs,
      force_sig_info(SIGTRAP, &info, tsk);
 }
 
+static void do_audit_syscall_entry(struct pt_regs *regs, u32 arch)
+{
+#ifdef CONFIG_X86_64
+     if (arch == AUDIT_ARCH_X86_64) {
+             audit_syscall_entry(arch, regs->orig_ax, regs->di,
+                                 regs->si, regs->dx, regs->r10);
+     } else
+#endif
+     {
+             audit_syscall_entry(arch, regs->orig_ax, regs->bx,
+                                 regs->cx, regs->dx, regs->si);
+     }
+}
+
 /*
- * We must return the syscall number to actually look up in the table.
- * This can be -1L to skip running any syscall at all.
+ * We can return 0 to resume the syscall or anything else to go to phase
+ * 2.  If we resume the syscall, we need to put something appropriate in
+ * regs->orig_ax.
+ *
+ * NB: We don't have full pt_regs here, but regs->orig_ax and regs->ax
+ * are fully functional.
+ *
+ * For phase 2's benefit, our return value is:
+ * 0:                        resume the syscall
+ * 1:                        go to phase 2; no seccomp phase 2 needed
+ * anything else:    go to phase 2; pass return value to seccomp
  */
-long syscall_trace_enter(struct pt_regs *regs)
+unsigned long syscall_trace_enter_phase1(struct pt_regs *regs, u32 arch)
 {
-     long ret = 0;
+     unsigned long ret = 0;
+     u32 work;
+
+     BUG_ON(regs != task_pt_regs(current));
+
+     work = ACCESS_ONCE(current_thread_info()->flags) &
+             _TIF_WORK_SYSCALL_ENTRY;
 
      /*
       * If TIF_NOHZ is set, we are required to call user_exit() before
       * doing anything that could touch RCU.
       */
-     if (test_thread_flag(TIF_NOHZ))
+     if (work & _TIF_NOHZ) {
              user_exit();
+             work &= ~TIF_NOHZ;
+     }
+
+#ifdef CONFIG_SECCOMP
+     /*
+      * Do seccomp first -- it should minimize exposure of other
+      * code, and keeping seccomp fast is probably more valuable
+      * than the rest of this.
+      */
+     if (work & _TIF_SECCOMP) {
+             struct seccomp_data sd;
+
+             sd.arch = arch;
+             sd.nr = regs->orig_ax;
+             sd.instruction_pointer = regs->ip;
+#ifdef CONFIG_X86_64
+             if (arch == AUDIT_ARCH_X86_64) {
+                     sd.args[0] = regs->di;
+                     sd.args[1] = regs->si;
+                     sd.args[2] = regs->dx;
+                     sd.args[3] = regs->r10;
+                     sd.args[4] = regs->r8;
+                     sd.args[5] = regs->r9;
+             } else
+#endif
+             {
+                     sd.args[0] = regs->bx;
+                     sd.args[1] = regs->cx;
+                     sd.args[2] = regs->dx;
+                     sd.args[3] = regs->si;
+                     sd.args[4] = regs->di;
+                     sd.args[5] = regs->bp;
+             }
+
Quoted 8 lines

arch/x86/kernel/ptrace.c void send_sigtrap(struct task_struct *tsk, struct pt_regs *regs, · line 1526

Quoted 6 lines
arch/x86/kernel/ptrace.c +6 -0
+             BUILD_BUG_ON(SECCOMP_PHASE1_OK != 0);
+             BUILD_BUG_ON(SECCOMP_PHASE1_SKIP != 1);
+
+             ret = seccomp_phase1(&sd);
+             if (ret == SECCOMP_PHASE1_SKIP) {
+                     regs->orig_ax = -1;

How the tracer is expected to get the correct syscall number after that?

Quoted 4 lines

There shouldn't be a tracer if a skip is encountered. (A seccomp skip would skip ptrace.) This behavior hasn't changed, but maybe I don't see what you mean? (I haven't encountered any problems with syscall tracing as a result of these changes.)

SECCOMP_RET_ERRNO leads to SECCOMP_PHASE1_SKIP, and if there is a tracer, it will get -1 as a syscall number.

I've found this while testing a strace parser for SECCOMP_MODE_FILTER/SECCOMP_SET_MODE_FILTER, so the problem is quite real.

Signature · 1 line
ldv
author reviewer
#10 lore raw

Re: [PATCH v5 3/5] x86: Split syscall_trace_enter into two phases

Quoted 147 lines · Dmitry V. Levin
Quoted 141 lines · Kees Cook
Quoted 131 lines · Dmitry V. Levin

Hi,

Quoted 3 lines · Andy Lutomirski

This splits syscall_trace_enter into syscall_trace_enter_phase1 and syscall_trace_enter_phase2. Only phase 2 has full pt_regs, and only phase 2 is permitted to modify any of pt_regs except for orig_ax.

This breaks ptrace, see below.

Quoted 122 lines

The intent is that phase 1 can be called from the syscall fast path.

In this implementation, phase1 can handle any combination of TIF_NOHZ (RCU context tracking), TIF_SECCOMP, and TIF_SYSCALL_AUDIT, unless seccomp requests a ptrace event, in which case phase2 is forced.

In principle, this could yield a big speedup for TIF_NOHZ as well as for TIF_SECCOMP if syscall exit work were similarly split up.

---
 arch/x86/include/asm/ptrace.h |   5 ++
 arch/x86/kernel/ptrace.c      | 157 +++++++++++++++++++++++++++++++++++-------
 2 files changed, 138 insertions(+), 24 deletions(-)
arch/x86/include/asm/ptrace.h +5 -0
index 6205f0c434db..86fc2bb82287 100644
@@ -75,6 +75,11 @@ convert_ip_to_linear(struct task_struct *child, struct pt_regs *regs);
 extern void send_sigtrap(struct task_struct *tsk, struct pt_regs *regs,
                       int error_code, int si_code);
 
+
+extern unsigned long syscall_trace_enter_phase1(struct pt_regs *, u32 arch);
+extern long syscall_trace_enter_phase2(struct pt_regs *, u32 arch,
+                                    unsigned long phase1_result);
+
 extern long syscall_trace_enter(struct pt_regs *);
 extern void syscall_trace_leave(struct pt_regs *);
 
arch/x86/kernel/ptrace.c +68 -5
index bbf338a04a5d..29576c244699 100644
@@ -1441,20 +1441,126 @@ void send_sigtrap(struct task_struct *tsk, struct pt_regs *regs,
      force_sig_info(SIGTRAP, &info, tsk);
 }
 
+static void do_audit_syscall_entry(struct pt_regs *regs, u32 arch)
+{
+#ifdef CONFIG_X86_64
+     if (arch == AUDIT_ARCH_X86_64) {
+             audit_syscall_entry(arch, regs->orig_ax, regs->di,
+                                 regs->si, regs->dx, regs->r10);
+     } else
+#endif
+     {
+             audit_syscall_entry(arch, regs->orig_ax, regs->bx,
+                                 regs->cx, regs->dx, regs->si);
+     }
+}
+
 /*
- * We must return the syscall number to actually look up in the table.
- * This can be -1L to skip running any syscall at all.
+ * We can return 0 to resume the syscall or anything else to go to phase
+ * 2.  If we resume the syscall, we need to put something appropriate in
+ * regs->orig_ax.
+ *
+ * NB: We don't have full pt_regs here, but regs->orig_ax and regs->ax
+ * are fully functional.
+ *
+ * For phase 2's benefit, our return value is:
+ * 0:                        resume the syscall
+ * 1:                        go to phase 2; no seccomp phase 2 needed
+ * anything else:    go to phase 2; pass return value to seccomp
  */
-long syscall_trace_enter(struct pt_regs *regs)
+unsigned long syscall_trace_enter_phase1(struct pt_regs *regs, u32 arch)
 {
-     long ret = 0;
+     unsigned long ret = 0;
+     u32 work;
+
+     BUG_ON(regs != task_pt_regs(current));
+
+     work = ACCESS_ONCE(current_thread_info()->flags) &
+             _TIF_WORK_SYSCALL_ENTRY;
 
      /*
       * If TIF_NOHZ is set, we are required to call user_exit() before
       * doing anything that could touch RCU.
       */
-     if (test_thread_flag(TIF_NOHZ))
+     if (work & _TIF_NOHZ) {
              user_exit();
+             work &= ~TIF_NOHZ;
+     }
+
+#ifdef CONFIG_SECCOMP
+     /*
+      * Do seccomp first -- it should minimize exposure of other
+      * code, and keeping seccomp fast is probably more valuable
+      * than the rest of this.
+      */
+     if (work & _TIF_SECCOMP) {
+             struct seccomp_data sd;
+
+             sd.arch = arch;
+             sd.nr = regs->orig_ax;
+             sd.instruction_pointer = regs->ip;
+#ifdef CONFIG_X86_64
+             if (arch == AUDIT_ARCH_X86_64) {
+                     sd.args[0] = regs->di;
+                     sd.args[1] = regs->si;
+                     sd.args[2] = regs->dx;
+                     sd.args[3] = regs->r10;
+                     sd.args[4] = regs->r8;
+                     sd.args[5] = regs->r9;
+             } else
+#endif
+             {
+                     sd.args[0] = regs->bx;
+                     sd.args[1] = regs->cx;
+                     sd.args[2] = regs->dx;
+                     sd.args[3] = regs->si;
+                     sd.args[4] = regs->di;
+                     sd.args[5] = regs->bp;
+             }
+
Quoted 8 lines

arch/x86/kernel/ptrace.c void send_sigtrap(struct task_struct *tsk, struct pt_regs *regs, · line 1526

Quoted 6 lines
arch/x86/kernel/ptrace.c +6 -0
+             BUILD_BUG_ON(SECCOMP_PHASE1_OK != 0);
+             BUILD_BUG_ON(SECCOMP_PHASE1_SKIP != 1);
+
+             ret = seccomp_phase1(&sd);
+             if (ret == SECCOMP_PHASE1_SKIP) {
+                     regs->orig_ax = -1;

How the tracer is expected to get the correct syscall number after that?

Quoted 4 lines

There shouldn't be a tracer if a skip is encountered. (A seccomp skip would skip ptrace.) This behavior hasn't changed, but maybe I don't see what you mean? (I haven't encountered any problems with syscall tracing as a result of these changes.)

Quoted 7 lines

SECCOMP_RET_ERRNO leads to SECCOMP_PHASE1_SKIP, and if there is a tracer, it will get -1 as a syscall number.

I've found this while testing a strace parser for SECCOMP_MODE_FILTER/SECCOMP_SET_MODE_FILTER, so the problem is quite real.

Hasn't it always been this way?

I admit that I kind of wish this worked the other way -- that is, I think it would be nice to have a mode in which ptrace runs before seccomp, which would close the ptrace hole (where ptrace can do things that seccomp would disallow) and maybe have more comprehensible results.

--Andy

Quoted 2 lines

--
ldv

Signature · 2 lines
Andy Lutomirski
AMA Capital Management, LLC
#11 lore raw

Re: [PATCH v5 3/5] x86: Split syscall_trace_enter into two phases

Quoted 151 lines · Andy Lutomirski
Quoted 150 lines · Dmitry V. Levin
Quoted 141 lines · Kees Cook
Quoted 131 lines · Dmitry V. Levin

Hi,

Quoted 3 lines · Andy Lutomirski

This splits syscall_trace_enter into syscall_trace_enter_phase1 and syscall_trace_enter_phase2. Only phase 2 has full pt_regs, and only phase 2 is permitted to modify any of pt_regs except for orig_ax.

This breaks ptrace, see below.

Quoted 122 lines

The intent is that phase 1 can be called from the syscall fast path.

In this implementation, phase1 can handle any combination of TIF_NOHZ (RCU context tracking), TIF_SECCOMP, and TIF_SYSCALL_AUDIT, unless seccomp requests a ptrace event, in which case phase2 is forced.

In principle, this could yield a big speedup for TIF_NOHZ as well as for TIF_SECCOMP if syscall exit work were similarly split up.

---
 arch/x86/include/asm/ptrace.h |   5 ++
 arch/x86/kernel/ptrace.c      | 157 +++++++++++++++++++++++++++++++++++-------
 2 files changed, 138 insertions(+), 24 deletions(-)
arch/x86/include/asm/ptrace.h +5 -0
index 6205f0c434db..86fc2bb82287 100644
@@ -75,6 +75,11 @@ convert_ip_to_linear(struct task_struct *child, struct pt_regs *regs);
 extern void send_sigtrap(struct task_struct *tsk, struct pt_regs *regs,
                       int error_code, int si_code);
 
+
+extern unsigned long syscall_trace_enter_phase1(struct pt_regs *, u32 arch);
+extern long syscall_trace_enter_phase2(struct pt_regs *, u32 arch,
+                                    unsigned long phase1_result);
+
 extern long syscall_trace_enter(struct pt_regs *);
 extern void syscall_trace_leave(struct pt_regs *);
 
arch/x86/kernel/ptrace.c +68 -5
index bbf338a04a5d..29576c244699 100644
@@ -1441,20 +1441,126 @@ void send_sigtrap(struct task_struct *tsk, struct pt_regs *regs,
      force_sig_info(SIGTRAP, &info, tsk);
 }
 
+static void do_audit_syscall_entry(struct pt_regs *regs, u32 arch)
+{
+#ifdef CONFIG_X86_64
+     if (arch == AUDIT_ARCH_X86_64) {
+             audit_syscall_entry(arch, regs->orig_ax, regs->di,
+                                 regs->si, regs->dx, regs->r10);
+     } else
+#endif
+     {
+             audit_syscall_entry(arch, regs->orig_ax, regs->bx,
+                                 regs->cx, regs->dx, regs->si);
+     }
+}
+
 /*
- * We must return the syscall number to actually look up in the table.
- * This can be -1L to skip running any syscall at all.
+ * We can return 0 to resume the syscall or anything else to go to phase
+ * 2.  If we resume the syscall, we need to put something appropriate in
+ * regs->orig_ax.
+ *
+ * NB: We don't have full pt_regs here, but regs->orig_ax and regs->ax
+ * are fully functional.
+ *
+ * For phase 2's benefit, our return value is:
+ * 0:                        resume the syscall
+ * 1:                        go to phase 2; no seccomp phase 2 needed
+ * anything else:    go to phase 2; pass return value to seccomp
  */
-long syscall_trace_enter(struct pt_regs *regs)
+unsigned long syscall_trace_enter_phase1(struct pt_regs *regs, u32 arch)
 {
-     long ret = 0;
+     unsigned long ret = 0;
+     u32 work;
+
+     BUG_ON(regs != task_pt_regs(current));
+
+     work = ACCESS_ONCE(current_thread_info()->flags) &
+             _TIF_WORK_SYSCALL_ENTRY;
 
      /*
       * If TIF_NOHZ is set, we are required to call user_exit() before
       * doing anything that could touch RCU.
       */
-     if (test_thread_flag(TIF_NOHZ))
+     if (work & _TIF_NOHZ) {
              user_exit();
+             work &= ~TIF_NOHZ;
+     }
+
+#ifdef CONFIG_SECCOMP
+     /*
+      * Do seccomp first -- it should minimize exposure of other
+      * code, and keeping seccomp fast is probably more valuable
+      * than the rest of this.
+      */
+     if (work & _TIF_SECCOMP) {
+             struct seccomp_data sd;
+
+             sd.arch = arch;
+             sd.nr = regs->orig_ax;
+             sd.instruction_pointer = regs->ip;
+#ifdef CONFIG_X86_64
+             if (arch == AUDIT_ARCH_X86_64) {
+                     sd.args[0] = regs->di;
+                     sd.args[1] = regs->si;
+                     sd.args[2] = regs->dx;
+                     sd.args[3] = regs->r10;
+                     sd.args[4] = regs->r8;
+                     sd.args[5] = regs->r9;
+             } else
+#endif
+             {
+                     sd.args[0] = regs->bx;
+                     sd.args[1] = regs->cx;
+                     sd.args[2] = regs->dx;
+                     sd.args[3] = regs->si;
+                     sd.args[4] = regs->di;
+                     sd.args[5] = regs->bp;
+             }
+
Quoted 8 lines

arch/x86/kernel/ptrace.c void send_sigtrap(struct task_struct *tsk, struct pt_regs *regs, · line 1526

Quoted 6 lines
arch/x86/kernel/ptrace.c +6 -0
+             BUILD_BUG_ON(SECCOMP_PHASE1_OK != 0);
+             BUILD_BUG_ON(SECCOMP_PHASE1_SKIP != 1);
+
+             ret = seccomp_phase1(&sd);
+             if (ret == SECCOMP_PHASE1_SKIP) {
+                     regs->orig_ax = -1;

How the tracer is expected to get the correct syscall number after that?

Quoted 4 lines

There shouldn't be a tracer if a skip is encountered. (A seccomp skip would skip ptrace.) This behavior hasn't changed, but maybe I don't see what you mean? (I haven't encountered any problems with syscall tracing as a result of these changes.)

SECCOMP_RET_ERRNO leads to SECCOMP_PHASE1_SKIP, and if there is a tracer, it will get -1 as a syscall number.

Quoted 6 lines
Quoted 4 lines

I've found this while testing a strace parser for SECCOMP_MODE_FILTER/SECCOMP_SET_MODE_FILTER, so the problem is quite real.

Hasn't it always been this way?

As far as I know, yes, it's always been this way. The point is to the skip the syscall, which is what the -1 signals. Userspace then reads back the errno.

Quoted 5 lines

I admit that I kind of wish this worked the other way -- that is, I think it would be nice to have a mode in which ptrace runs before seccomp, which would close the ptrace hole (where ptrace can do things that seccomp would disallow) and maybe have more comprehensible results.

I prefer keeping the seccomp attack surface as tiny as possible. I would not like to ptrace happening before seccomp.

-Kees

Signature · 2 lines
Kees Cook
Chrome OS Security
#12 lore raw

Re: [PATCH v5 3/5] x86: Split syscall_trace_enter into two phases

Quoted 13 lines · Kees Cook
Quoted 12 lines · Andy Lutomirski
Quoted 11 lines · Dmitry V. Levin
Quoted 10 lines · Kees Cook
Quoted 9 lines · Dmitry V. Levin

Hi,

Quoted 3 lines · Andy Lutomirski

This splits syscall_trace_enter into syscall_trace_enter_phase1 and syscall_trace_enter_phase2. Only phase 2 has full pt_regs, and only phase 2 is permitted to modify any of pt_regs except for orig_ax.

This breaks ptrace, see below.

[...]

Quoted 14 lines
Quoted 14 lines
Quoted 14 lines
Quoted 10 lines
Quoted 5 lines

arch/x86/kernel/ptrace.c void send_sigtrap(struct task_struct *tsk, struct pt_regs *regs, · line 1526

Quoted 3 lines
arch/x86/kernel/ptrace.c +3 -0
+             ret = seccomp_phase1(&sd);
+             if (ret == SECCOMP_PHASE1_SKIP) {
+                     regs->orig_ax = -1;

How the tracer is expected to get the correct syscall number after that?

There shouldn't be a tracer if a skip is encountered. (A seccomp skip would skip ptrace.) This behavior hasn't changed, but maybe I don't see what you mean? (I haven't encountered any problems with syscall tracing as a result of these changes.)

SECCOMP_RET_ERRNO leads to SECCOMP_PHASE1_SKIP, and if there is a tracer, it will get -1 as a syscall number.

Quoted 8 lines
Quoted 4 lines
Quoted 2 lines

I've found this while testing a strace parser for SECCOMP_MODE_FILTER/SECCOMP_SET_MODE_FILTER, so the problem is quite real.

Hasn't it always been this way?

As far as I know, yes, it's always been this way. The point is to the skip the syscall, which is what the -1 signals. Userspace then reads back the errno.

There is a clear difference: before these changes, SECCOMP_RET_ERRNO used to keep the syscall number unchanged and suppress syscall-exit-stop event, which was awful because userspace cannot distinguish syscall-enter-stop from syscall-exit-stop and therefore relies on the kernel that syscall-enter-stop is followed by syscall-exit-stop (or tracee's death, etc.).

After these changes, SECCOMP_RET_ERRNO no longer causes syscall-exit-stop events to be suppressed, but now the syscall number is lost.

Signature · 1 line
ldv
#13 lore raw

Re: [PATCH v5 3/5] x86: Split syscall_trace_enter into two phases

Quoted 38 lines · Dmitry V. Levin
Quoted 13 lines · Kees Cook
Quoted 12 lines · Andy Lutomirski
Quoted 11 lines · Dmitry V. Levin
Quoted 10 lines · Kees Cook
Quoted 9 lines · Dmitry V. Levin

Hi,

Quoted 3 lines · Andy Lutomirski

This splits syscall_trace_enter into syscall_trace_enter_phase1 and syscall_trace_enter_phase2. Only phase 2 has full pt_regs, and only phase 2 is permitted to modify any of pt_regs except for orig_ax.

This breaks ptrace, see below.

[...]

Quoted 14 lines
Quoted 14 lines
Quoted 14 lines
Quoted 10 lines
Quoted 5 lines

Review

Quoted 3 lines
(quoted diff) +3 -0
+             ret = seccomp_phase1(&sd);
+             if (ret == SECCOMP_PHASE1_SKIP) {
+                     regs->orig_ax = -1;

How the tracer is expected to get the correct syscall number after that?

There shouldn't be a tracer if a skip is encountered. (A seccomp skip would skip ptrace.) This behavior hasn't changed, but maybe I don't see what you mean? (I haven't encountered any problems with syscall tracing as a result of these changes.)

SECCOMP_RET_ERRNO leads to SECCOMP_PHASE1_SKIP, and if there is a tracer, it will get -1 as a syscall number.

Quoted 8 lines
Quoted 4 lines
Quoted 2 lines

I've found this while testing a strace parser for SECCOMP_MODE_FILTER/SECCOMP_SET_MODE_FILTER, so the problem is quite real.

Hasn't it always been this way?

As far as I know, yes, it's always been this way. The point is to the skip the syscall, which is what the -1 signals. Userspace then reads back the errno.

Quoted 8 lines

There is a clear difference: before these changes, SECCOMP_RET_ERRNO used to keep the syscall number unchanged and suppress syscall-exit-stop event, which was awful because userspace cannot distinguish syscall-enter-stop from syscall-exit-stop and therefore relies on the kernel that syscall-enter-stop is followed by syscall-exit-stop (or tracee's death, etc.).

After these changes, SECCOMP_RET_ERRNO no longer causes syscall-exit-stop events to be suppressed, but now the syscall number is lost.

Ah-ha! Okay, thanks, I understand now. I think this means seccomp phase1 should not treat RET_ERRNO as a "skip" event. Andy, what do you think here?

-Kees

Signature · 2 lines
Kees Cook
Chrome OS Security
author reviewer
#14 lore raw

Re: [PATCH v5 3/5] x86: Split syscall_trace_enter into two phases

Quoted 45 lines · Kees Cook
Quoted 44 lines · Dmitry V. Levin
Quoted 13 lines · Kees Cook
Quoted 12 lines · Andy Lutomirski
Quoted 11 lines · Dmitry V. Levin
Quoted 10 lines · Kees Cook
Quoted 9 lines · Dmitry V. Levin

Hi,

Quoted 3 lines · Andy Lutomirski

This splits syscall_trace_enter into syscall_trace_enter_phase1 and syscall_trace_enter_phase2. Only phase 2 has full pt_regs, and only phase 2 is permitted to modify any of pt_regs except for orig_ax.

This breaks ptrace, see below.

[...]

Quoted 14 lines
Quoted 14 lines
Quoted 14 lines
Quoted 10 lines
Quoted 5 lines

Review

Quoted 3 lines
(quoted diff) +3 -0
+             ret = seccomp_phase1(&sd);
+             if (ret == SECCOMP_PHASE1_SKIP) {
+                     regs->orig_ax = -1;

How the tracer is expected to get the correct syscall number after that?

There shouldn't be a tracer if a skip is encountered. (A seccomp skip would skip ptrace.) This behavior hasn't changed, but maybe I don't see what you mean? (I haven't encountered any problems with syscall tracing as a result of these changes.)

SECCOMP_RET_ERRNO leads to SECCOMP_PHASE1_SKIP, and if there is a tracer, it will get -1 as a syscall number.

Quoted 8 lines
Quoted 4 lines
Quoted 2 lines

I've found this while testing a strace parser for SECCOMP_MODE_FILTER/SECCOMP_SET_MODE_FILTER, so the problem is quite real.

Hasn't it always been this way?

As far as I know, yes, it's always been this way. The point is to the skip the syscall, which is what the -1 signals. Userspace then reads back the errno.

There is a clear difference: before these changes, SECCOMP_RET_ERRNO used to keep the syscall number unchanged and suppress syscall-exit-stop event, which was awful because userspace cannot distinguish syscall-enter-stop from syscall-exit-stop and therefore relies on the kernel that syscall-enter-stop is followed by syscall-exit-stop (or tracee's death, etc.).

Quoted 7 lines
Quoted 2 lines

After these changes, SECCOMP_RET_ERRNO no longer causes syscall-exit-stop events to be suppressed, but now the syscall number is lost.

Ah-ha! Okay, thanks, I understand now. I think this means seccomp phase1 should not treat RET_ERRNO as a "skip" event. Andy, what do you think here?

I still don't quite see how this change caused this. I can play with it a bit more. But RET_ERRNO *has* to be some kind of skip event, because it needs to skip the syscall.

We could change this by treating RET_ERRNO as an instruction to enter phase 2 and then asking for a skip in phase 2 without changing orig_ax, but IMO this is pretty ugly.

I think this all kind of sucks. We're trying to run ptrace after seccomp, so ptrace is seeing the syscalls as transformed by seccomp. That means that if we use RET_TRAP, then ptrace will see the possibly-modified syscall, if we use RET_ERRNO, then ptrace is (IMO correctly given the current design) showing syscall -1, and if we use RET_KILL, then ptrace just sees the process mysteriously die.

I think it would be more useful and easier to understand if ptrace saw syscalls as the traced process saw them, i.e. before seccomp modification. How would this meaningfully increase the attack surface? As far as ptrace is concerned, a syscall is just seven numbers, and as long as a process can issue *any* syscall that seccomp allows, then it can invoke the ptrace hooks. I don't think the entry/exit hooks care *at all* about the syscall nr or args.

Audit is a different story. I think we should absolutely continue to audit syscalls that actually happen, not syscalls that were requested.

Given this bug, I doubt we'd break anything if we changed it, since it appears that it's already rather broken. Also, changing it would make me happy, because I want to add a SECCOMP_RET_MONITOR that freezes the process, sends an event to a seccompfd, and then executes syscalls as requested by the holder of the seccompfd. Those syscalls would, in turn, be filtered again through inner layers of seccomp. One sticking point would be that the current ptrace behavior is very hard to reconcile with this type of feature.

--Andy

#15 lore raw

Re: [PATCH v5 3/5] x86: Split syscall_trace_enter into two phases

Quoted 2 lines · Andy Lutomirski
Quoted 1 line · Kees Cook

On Thu, Feb 5, 2015 at 3:39 PM, Dmitry V. Levin <[email protected]> wrote:

[...]

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

There is a clear difference: before these changes, SECCOMP_RET_ERRNO used to keep the syscall number unchanged and suppress syscall-exit-stop event, which was awful because userspace cannot distinguish syscall-enter-stop from syscall-exit-stop and therefore relies on the kernel that syscall-enter-stop is followed by syscall-exit-stop (or tracee's death, etc.).

Quoted 8 lines
Quoted 6 lines
Quoted 2 lines

After these changes, SECCOMP_RET_ERRNO no longer causes syscall-exit-stop events to be suppressed, but now the syscall number is lost.

Ah-ha! Okay, thanks, I understand now. I think this means seccomp phase1 should not treat RET_ERRNO as a "skip" event. Andy, what do you think here?

I still don't quite see how this change caused this.

I have a test for this at
http://sourceforge.net/p/strace/code/ci/HEAD/~/tree/test/seccomp.c

Quoted 8 lines

I can play with
it a bit more. But RET_ERRNO *has* to be some kind of skip event, because it needs to skip the syscall.

We could change this by treating RET_ERRNO as an instruction to enter phase 2 and then asking for a skip in phase 2 without changing orig_ax, but IMO this is pretty ugly.

Quoted 6 lines

I think this all kind of sucks. We're trying to run ptrace after seccomp, so ptrace is seeing the syscalls as transformed by seccomp. That means that if we use RET_TRAP, then ptrace will see the possibly-modified syscall, if we use RET_ERRNO, then ptrace is (IMO correctly given the current design) showing syscall -1, and if we use RET_KILL, then ptrace just sees the process mysteriously die.

Userspace is usually not prepared to see syscall -1.
For example, strace had to be patched, otherwise it just skipped such syscalls as "not a syscall" events or did other improper things:
http://sourceforge.net/p/strace/code/ci/c3948327717c29b10b5e00a436dc138b4ab1a486
http://sourceforge.net/p/strace/code/ci/8e398b6c4020fb2d33a5b3e40271ebf63199b891

A slightly different but related story: userspace is also not prepared to handle large errno values produced by seccomp filters like this:
BPF_STMT(BPF_RET, SECCOMP_RET_ERRNO | SECCOMP_RET_DATA)

For example, glibc assumes that syscalls do not return errno values greater than 0xfff:
https://sourceware.org/git/?p=glibc.git;a=blob;f=sysdeps/unix/sysv/linux/x86_64/sysdep.h#l55
https://sourceware.org/git/?p=glibc.git;a=blob;f=sysdeps/unix/sysv/linux/x86_64/syscall.S#l20

If it isn't too late, I'd recommend changing SECCOMP_RET_DATA mask applied in SECCOMP_RET_ERRNO case from current 0xffff to 0xfff.

Signature · 1 line
ldv
author reviewer
#16 lore raw

Re: [PATCH v5 3/5] x86: Split syscall_trace_enter into two phases

Quoted 37 lines · Dmitry V. Levin
Quoted 2 lines · Andy Lutomirski
Quoted 1 line · Kees Cook

On Thu, Feb 5, 2015 at 3:39 PM, Dmitry V. Levin <[email protected]> wrote:

[...]

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

There is a clear difference: before these changes, SECCOMP_RET_ERRNO used to keep the syscall number unchanged and suppress syscall-exit-stop event, which was awful because userspace cannot distinguish syscall-enter-stop from syscall-exit-stop and therefore relies on the kernel that syscall-enter-stop is followed by syscall-exit-stop (or tracee's death, etc.).

Quoted 8 lines
Quoted 6 lines
Quoted 2 lines

After these changes, SECCOMP_RET_ERRNO no longer causes syscall-exit-stop events to be suppressed, but now the syscall number is lost.

Ah-ha! Okay, thanks, I understand now. I think this means seccomp phase1 should not treat RET_ERRNO as a "skip" event. Andy, what do you think here?

I still don't quite see how this change caused this.

I have a test for this at
http://sourceforge.net/p/strace/code/ci/HEAD/~/tree/test/seccomp.c

Quoted 8 lines

I can play with
it a bit more. But RET_ERRNO *has* to be some kind of skip event, because it needs to skip the syscall.

We could change this by treating RET_ERRNO as an instruction to enter phase 2 and then asking for a skip in phase 2 without changing orig_ax, but IMO this is pretty ugly.

Quoted 6 lines

I think this all kind of sucks. We're trying to run ptrace after seccomp, so ptrace is seeing the syscalls as transformed by seccomp. That means that if we use RET_TRAP, then ptrace will see the possibly-modified syscall, if we use RET_ERRNO, then ptrace is (IMO correctly given the current design) showing syscall -1, and if we use RET_KILL, then ptrace just sees the process mysteriously die.

Quoted 6 lines

Userspace is usually not prepared to see syscall -1.
For example, strace had to be patched, otherwise it just skipped such syscalls as "not a syscall" events or did other improper things:
http://sourceforge.net/p/strace/code/ci/c3948327717c29b10b5e00a436dc138b4ab1a486
http://sourceforge.net/p/strace/code/ci/8e398b6c4020fb2d33a5b3e40271ebf63199b891

The x32 thing is a legit ABI bug, I'd argue. I'd be happy to submit a patch to fix that (clear the x32 bit if we're not x32).

Quoted 10 lines

A slightly different but related story: userspace is also not prepared to handle large errno values produced by seccomp filters like this:
BPF_STMT(BPF_RET, SECCOMP_RET_ERRNO | SECCOMP_RET_DATA)

For example, glibc assumes that syscalls do not return errno values greater than 0xfff:
https://sourceware.org/git/?p=glibc.git;a=blob;f=sysdeps/unix/sysv/linux/x86_64/sysdep.h#l55
https://sourceware.org/git/?p=glibc.git;a=blob;f=sysdeps/unix/sysv/linux/x86_64/syscall.S#l20

If it isn't too late, I'd recommend changing SECCOMP_RET_DATA mask applied in SECCOMP_RET_ERRNO case from current 0xffff to 0xfff.

I think this is solidly in the "don't do that" category. Seccomp lets you tamper with syscalls. If you tamper wrong, then you lose.

Kees, what do you think about reversing the whole thing so that ptracers always see the original syscall?

--Andy

#17 lore raw

Re: [PATCH v5 3/5] x86: Split syscall_trace_enter into two phases

Quoted 45 lines · Andy Lutomirski
Quoted 37 lines · Dmitry V. Levin
Quoted 2 lines · Andy Lutomirski
Quoted 1 line · Kees Cook

On Thu, Feb 5, 2015 at 3:39 PM, Dmitry V. Levin <[email protected]> wrote:

[...]

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

There is a clear difference: before these changes, SECCOMP_RET_ERRNO used to keep the syscall number unchanged and suppress syscall-exit-stop event, which was awful because userspace cannot distinguish syscall-enter-stop from syscall-exit-stop and therefore relies on the kernel that syscall-enter-stop is followed by syscall-exit-stop (or tracee's death, etc.).

Quoted 8 lines
Quoted 6 lines
Quoted 2 lines

After these changes, SECCOMP_RET_ERRNO no longer causes syscall-exit-stop events to be suppressed, but now the syscall number is lost.

Ah-ha! Okay, thanks, I understand now. I think this means seccomp phase1 should not treat RET_ERRNO as a "skip" event. Andy, what do you think here?

I still don't quite see how this change caused this.

I have a test for this at
http://sourceforge.net/p/strace/code/ci/HEAD/~/tree/test/seccomp.c

Quoted 8 lines

I can play with
it a bit more. But RET_ERRNO *has* to be some kind of skip event, because it needs to skip the syscall.

We could change this by treating RET_ERRNO as an instruction to enter phase 2 and then asking for a skip in phase 2 without changing orig_ax, but IMO this is pretty ugly.

Quoted 6 lines

I think this all kind of sucks. We're trying to run ptrace after seccomp, so ptrace is seeing the syscalls as transformed by seccomp. That means that if we use RET_TRAP, then ptrace will see the possibly-modified syscall, if we use RET_ERRNO, then ptrace is (IMO correctly given the current design) showing syscall -1, and if we use RET_KILL, then ptrace just sees the process mysteriously die.

Quoted 6 lines

Userspace is usually not prepared to see syscall -1.
For example, strace had to be patched, otherwise it just skipped such syscalls as "not a syscall" events or did other improper things:
http://sourceforge.net/p/strace/code/ci/c3948327717c29b10b5e00a436dc138b4ab1a486
http://sourceforge.net/p/strace/code/ci/8e398b6c4020fb2d33a5b3e40271ebf63199b891

Quoted 10 lines

The x32 thing is a legit ABI bug, I'd argue. I'd be happy to submit a patch to fix that (clear the x32 bit if we're not x32).

Quoted 7 lines

A slightly different but related story: userspace is also not prepared to handle large errno values produced by seccomp filters like this:
BPF_STMT(BPF_RET, SECCOMP_RET_ERRNO | SECCOMP_RET_DATA)

For example, glibc assumes that syscalls do not return errno values greater than 0xfff:
https://sourceware.org/git/?p=glibc.git;a=blob;f=sysdeps/unix/sysv/linux/x86_64/sysdep.h#l55
https://sourceware.org/git/?p=glibc.git;a=blob;f=sysdeps/unix/sysv/linux/x86_64/syscall.S#l20

To save others the link reading: "Linus said he will make sure the no syscall returns a value in -1 .. -4095 as a valid result so we can savely test with -4095."

Strictly speaking (ISO C, "man 3 errno"), errno is supposed to be a full int, though digging around I find this in include/linux/err.h:

/*
* Kernel pointers have redundant information, so we can use a
* scheme where we can return either an error code or a normal
* pointer with the same return value.
*
* This should be a per-architecture thing, to allow different
* error and pointer decisions.
*/
#define MAX_ERRNO 4095

#ifndef __ASSEMBLY__

#define IS_ERR_VALUE(x) unlikely((x) >= (unsigned long)-MAX_ERRNO)

But no architecture overrides this.

Quoted 2 lines
Quoted 2 lines

If it isn't too late, I'd recommend changing SECCOMP_RET_DATA mask applied in SECCOMP_RET_ERRNO case from current 0xffff to 0xfff.

I'm not opposed to this. I would want to more explicitly document the 4095 max value in man pages, though.

Quoted 5 lines

I think this is solidly in the "don't do that" category. Seccomp lets you tamper with syscalls. If you tamper wrong, then you lose.

Kees, what do you think about reversing the whole thing so that ptracers always see the original syscall?

What do you mean by "reversing"? The interactions I see here are:

PTRACE_SYSCALL
SECCOMP_RET_ERRNO
SECCOMP_RET_TRACE
SECCOMP_RET_TRAP

Both ptrace and seccomp will trigger via _TIF_WORK_SYSCALL_ENTRY. Only ptrace will trigger via _TIF_WORK_SYSCALL_EXIT.

For SECCOMP_RET_ERRNO to work, we must skip the syscall, as mentioned earlier:

arch/x86/kernel/entry_32.S ...
syscall_trace_entry:

        movl $-ENOSYS,PT_EAX(%esp)
        movl %esp, %eax
        call syscall_trace_enter
        /* What it returned is what we'll actually use.  */
        cmpl $(NR_syscalls), %eax
        jnae syscall_call
        jmp syscall_exit

END(syscall_trace_entry)

Both before and after the 2-phase change, syscall_trace_enter would return -1 if it hit SECCOMP_RET_ERRNO, before calling tracehook_report_syscall_entry. On exit, if PTRACE_SYSCALL, we'd hit tracehook_report_syscall_exit during syscall_trace_leave, which means a ptracer will see a syscall-exit-stop without a matching syscall-enter-stop.

Using SECCOMP_RET_TRACE with PTRACE_SYSCALL in place seems totally crazy, as the ptracer would need to be the same program, and if it chose to skip a syscall, it would be in the same place: it would see PTRACE_EVENT_SECCOMP, then no syscall-enter-stop, then a syscall-exit-stop. I think we can ignore this pathological case.

Using SECCOMP_RET_TRAP with PTRACE_SYSCALL also results in a skip, which produces the same "only syscall-exit-stop seen" problem.

In the SECCOMP_RET_ERRNO case, the syscall nr doesn't change (and isn't executed). In the SECCOMP_RET_TRAP case, the syscall nr doesn't change (and isn't executed). In the SECCOMP_RET_TRACE, the syscall nr _could_ change, but the ptracer would be doing it, so the crazy situation around PTRACE_SYSCALL is probably safe to ignore (as long as we document what is expected to happen).

So, the question is: should PTRACE_SYSCALL see a syscall that is _not_ being executed (due to seccomp)? Audit doesn't see it currently, and neither does ptrace. I would argue that it should continue to not see the syscall. That said, if it shouldn't be shown, we also shouldn't trigger syscall-exit-stop. If you can convince me it should see syscall-enter-stop, then I have two questions:

1) Do we accept that a ptracer can interfere with SECCOMP_RET_ERRNO? I think we probably must, since it can already interfere via syscall-exit-stop and change the errno. And especially since a ptracer can change syscalls during syscall-enter-stop to any syscall it wants, bypassing seccomp. This condition is already documented.

2) What do we do with audit? Suddenly we have ptrace seeing a syscall that audit doesn't?

And an unrelated thought:

3) Can't we find some way to fix the inability of a ptracer to distinguish between syscall-enter-stop and syscall-exit-stop?

-Kees

Signature · 2 lines
Kees Cook
Chrome OS Security
author reviewer
#18 lore raw

Re: [PATCH v5 3/5] x86: Split syscall_trace_enter into two phases

Quoted 138 lines · Kees Cook
Quoted 45 lines · Andy Lutomirski
Quoted 37 lines · Dmitry V. Levin
Quoted 2 lines · Andy Lutomirski
Quoted 1 line · Kees Cook

On Thu, Feb 5, 2015 at 3:39 PM, Dmitry V. Levin <[email protected]> wrote:

[...]

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

There is a clear difference: before these changes, SECCOMP_RET_ERRNO used to keep the syscall number unchanged and suppress syscall-exit-stop event, which was awful because userspace cannot distinguish syscall-enter-stop from syscall-exit-stop and therefore relies on the kernel that syscall-enter-stop is followed by syscall-exit-stop (or tracee's death, etc.).

Quoted 8 lines
Quoted 6 lines
Quoted 2 lines

After these changes, SECCOMP_RET_ERRNO no longer causes syscall-exit-stop events to be suppressed, but now the syscall number is lost.

Ah-ha! Okay, thanks, I understand now. I think this means seccomp phase1 should not treat RET_ERRNO as a "skip" event. Andy, what do you think here?

I still don't quite see how this change caused this.

I have a test for this at
http://sourceforge.net/p/strace/code/ci/HEAD/~/tree/test/seccomp.c

Quoted 8 lines

I can play with
it a bit more. But RET_ERRNO *has* to be some kind of skip event, because it needs to skip the syscall.

We could change this by treating RET_ERRNO as an instruction to enter phase 2 and then asking for a skip in phase 2 without changing orig_ax, but IMO this is pretty ugly.

Quoted 6 lines

I think this all kind of sucks. We're trying to run ptrace after seccomp, so ptrace is seeing the syscalls as transformed by seccomp. That means that if we use RET_TRAP, then ptrace will see the possibly-modified syscall, if we use RET_ERRNO, then ptrace is (IMO correctly given the current design) showing syscall -1, and if we use RET_KILL, then ptrace just sees the process mysteriously die.

Quoted 6 lines

Userspace is usually not prepared to see syscall -1.
For example, strace had to be patched, otherwise it just skipped such syscalls as "not a syscall" events or did other improper things:
http://sourceforge.net/p/strace/code/ci/c3948327717c29b10b5e00a436dc138b4ab1a486
http://sourceforge.net/p/strace/code/ci/8e398b6c4020fb2d33a5b3e40271ebf63199b891

Quoted 10 lines

The x32 thing is a legit ABI bug, I'd argue. I'd be happy to submit a patch to fix that (clear the x32 bit if we're not x32).

Quoted 7 lines

A slightly different but related story: userspace is also not prepared to handle large errno values produced by seccomp filters like this:
BPF_STMT(BPF_RET, SECCOMP_RET_ERRNO | SECCOMP_RET_DATA)

For example, glibc assumes that syscalls do not return errno values greater than 0xfff:
https://sourceware.org/git/?p=glibc.git;a=blob;f=sysdeps/unix/sysv/linux/x86_64/sysdep.h#l55
https://sourceware.org/git/?p=glibc.git;a=blob;f=sysdeps/unix/sysv/linux/x86_64/syscall.S#l20

To save others the link reading: "Linus said he will make sure the no syscall returns a value in -1 .. -4095 as a valid result so we can savely test with -4095."

Strictly speaking (ISO C, "man 3 errno"), errno is supposed to be a full int, though digging around I find this in include/linux/err.h:

/*
* Kernel pointers have redundant information, so we can use a
* scheme where we can return either an error code or a normal
* pointer with the same return value.
*
* This should be a per-architecture thing, to allow different
* error and pointer decisions.
*/
#define MAX_ERRNO 4095

#ifndef __ASSEMBLY__

#define IS_ERR_VALUE(x) unlikely((x) >= (unsigned long)-MAX_ERRNO)

But no architecture overrides this.

Quoted 2 lines
Quoted 2 lines

If it isn't too late, I'd recommend changing SECCOMP_RET_DATA mask applied in SECCOMP_RET_ERRNO case from current 0xffff to 0xfff.

I'm not opposed to this. I would want to more explicitly document the 4095 max value in man pages, though.

Quoted 5 lines

I think this is solidly in the "don't do that" category. Seccomp lets you tamper with syscalls. If you tamper wrong, then you lose.

Kees, what do you think about reversing the whole thing so that ptracers always see the original syscall?

What do you mean by "reversing"? The interactions I see here are:

PTRACE_SYSCALL
SECCOMP_RET_ERRNO
SECCOMP_RET_TRACE
SECCOMP_RET_TRAP

Both ptrace and seccomp will trigger via _TIF_WORK_SYSCALL_ENTRY. Only ptrace will trigger via _TIF_WORK_SYSCALL_EXIT.

For SECCOMP_RET_ERRNO to work, we must skip the syscall, as mentioned earlier:

arch/x86/kernel/entry_32.S ...
syscall_trace_entry:

        movl $-ENOSYS,PT_EAX(%esp)
        movl %esp, %eax
        call syscall_trace_enter
        /* What it returned is what we'll actually use.  */
        cmpl $(NR_syscalls), %eax
        jnae syscall_call
        jmp syscall_exit

END(syscall_trace_entry)

Both before and after the 2-phase change, syscall_trace_enter would return -1 if it hit SECCOMP_RET_ERRNO, before calling tracehook_report_syscall_entry. On exit, if PTRACE_SYSCALL, we'd hit tracehook_report_syscall_exit during syscall_trace_leave, which means a ptracer will see a syscall-exit-stop without a matching syscall-enter-stop.

Using SECCOMP_RET_TRACE with PTRACE_SYSCALL in place seems totally crazy, as the ptracer would need to be the same program, and if it chose to skip a syscall, it would be in the same place: it would see PTRACE_EVENT_SECCOMP, then no syscall-enter-stop, then a syscall-exit-stop. I think we can ignore this pathological case.

Using SECCOMP_RET_TRAP with PTRACE_SYSCALL also results in a skip, which produces the same "only syscall-exit-stop seen" problem.

In the SECCOMP_RET_ERRNO case, the syscall nr doesn't change (and isn't executed). In the SECCOMP_RET_TRAP case, the syscall nr doesn't change (and isn't executed). In the SECCOMP_RET_TRACE, the syscall nr _could_ change, but the ptracer would be doing it, so the crazy situation around PTRACE_SYSCALL is probably safe to ignore (as long as we document what is expected to happen).

Quoted 6 lines

So, the question is: should PTRACE_SYSCALL see a syscall that is _not_ being executed (due to seccomp)? Audit doesn't see it currently, and neither does ptrace. I would argue that it should continue to not see the syscall. That said, if it shouldn't be shown, we also shouldn't trigger syscall-exit-stop. If you can convince me it should see syscall-enter-stop, then I have two questions:

I think PTRACE_SYSCALL should see syscalls that are skipped due to seccomp. I think that the exit event should see the modified errno, if any, so that strace will show whatever the traced process thinks is happening.

Quoted 4 lines

1) Do we accept that a ptracer can interfere with SECCOMP_RET_ERRNO? I think we probably must, since it can already interfere via syscall-exit-stop and change the errno.

I think this is fine.

Quoted 3 lines

And especially since a ptracer
can change syscalls during syscall-enter-stop to any syscall it wants, bypassing seccomp. This condition is already documented.

If a ptracer (using PTRACE_SYSCALL) were to get the entry callback before seccomp, then this oddity would go away, which might be a good thing. A ptracer could change the syscall, but seccomp would based on what the ptracer changed the syscall to.

Quoted 3 lines

2) What do we do with audit? Suddenly we have ptrace seeing a syscall that audit doesn't?

Is this a problem? I'd be amazed if program uses both ptrace and audit -- after all, audit is a global thing, and it only has one implementation (AFAIK): auditd. auditd doesn't ptrace the world.

Quoted 6 lines

And an unrelated thought:

3) Can't we find some way to fix the inability of a ptracer to distinguish between syscall-enter-stop and syscall-exit-stop?

Couldn't we add PTRACE_O_TRACESYSENTRY and PTRACE_O_TRACESYSEXIT along the lines of PTRACE_O_TRACESYSGOOD?

--Andy

#19 lore raw

Re: [PATCH v5 3/5] x86: Split syscall_trace_enter into two phases

Quoted 158 lines · Andy Lutomirski
Quoted 138 lines · Kees Cook
Quoted 45 lines · Andy Lutomirski
Quoted 37 lines · Dmitry V. Levin
Quoted 2 lines · Andy Lutomirski
Quoted 1 line · Kees Cook

On Thu, Feb 5, 2015 at 3:39 PM, Dmitry V. Levin <[email protected]> wrote:

[...]

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

There is a clear difference: before these changes, SECCOMP_RET_ERRNO used to keep the syscall number unchanged and suppress syscall-exit-stop event, which was awful because userspace cannot distinguish syscall-enter-stop from syscall-exit-stop and therefore relies on the kernel that syscall-enter-stop is followed by syscall-exit-stop (or tracee's death, etc.).

Quoted 8 lines
Quoted 6 lines
Quoted 2 lines

After these changes, SECCOMP_RET_ERRNO no longer causes syscall-exit-stop events to be suppressed, but now the syscall number is lost.

Ah-ha! Okay, thanks, I understand now. I think this means seccomp phase1 should not treat RET_ERRNO as a "skip" event. Andy, what do you think here?

I still don't quite see how this change caused this.

I have a test for this at
http://sourceforge.net/p/strace/code/ci/HEAD/~/tree/test/seccomp.c

Quoted 8 lines

I can play with
it a bit more. But RET_ERRNO *has* to be some kind of skip event, because it needs to skip the syscall.

We could change this by treating RET_ERRNO as an instruction to enter phase 2 and then asking for a skip in phase 2 without changing orig_ax, but IMO this is pretty ugly.

Quoted 6 lines

I think this all kind of sucks. We're trying to run ptrace after seccomp, so ptrace is seeing the syscalls as transformed by seccomp. That means that if we use RET_TRAP, then ptrace will see the possibly-modified syscall, if we use RET_ERRNO, then ptrace is (IMO correctly given the current design) showing syscall -1, and if we use RET_KILL, then ptrace just sees the process mysteriously die.

Quoted 6 lines

Userspace is usually not prepared to see syscall -1.
For example, strace had to be patched, otherwise it just skipped such syscalls as "not a syscall" events or did other improper things:
http://sourceforge.net/p/strace/code/ci/c3948327717c29b10b5e00a436dc138b4ab1a486
http://sourceforge.net/p/strace/code/ci/8e398b6c4020fb2d33a5b3e40271ebf63199b891

Quoted 10 lines

The x32 thing is a legit ABI bug, I'd argue. I'd be happy to submit a patch to fix that (clear the x32 bit if we're not x32).

Quoted 7 lines

A slightly different but related story: userspace is also not prepared to handle large errno values produced by seccomp filters like this:
BPF_STMT(BPF_RET, SECCOMP_RET_ERRNO | SECCOMP_RET_DATA)

For example, glibc assumes that syscalls do not return errno values greater than 0xfff:
https://sourceware.org/git/?p=glibc.git;a=blob;f=sysdeps/unix/sysv/linux/x86_64/sysdep.h#l55
https://sourceware.org/git/?p=glibc.git;a=blob;f=sysdeps/unix/sysv/linux/x86_64/syscall.S#l20

To save others the link reading: "Linus said he will make sure the no syscall returns a value in -1 .. -4095 as a valid result so we can savely test with -4095."

Strictly speaking (ISO C, "man 3 errno"), errno is supposed to be a full int, though digging around I find this in include/linux/err.h:

/*
* Kernel pointers have redundant information, so we can use a
* scheme where we can return either an error code or a normal
* pointer with the same return value.
*
* This should be a per-architecture thing, to allow different
* error and pointer decisions.
*/
#define MAX_ERRNO 4095

#ifndef __ASSEMBLY__

#define IS_ERR_VALUE(x) unlikely((x) >= (unsigned long)-MAX_ERRNO)

But no architecture overrides this.

Quoted 2 lines
Quoted 2 lines

If it isn't too late, I'd recommend changing SECCOMP_RET_DATA mask applied in SECCOMP_RET_ERRNO case from current 0xffff to 0xfff.

I'm not opposed to this. I would want to more explicitly document the 4095 max value in man pages, though.

Quoted 5 lines

I think this is solidly in the "don't do that" category. Seccomp lets you tamper with syscalls. If you tamper wrong, then you lose.

Kees, what do you think about reversing the whole thing so that ptracers always see the original syscall?

What do you mean by "reversing"? The interactions I see here are:

PTRACE_SYSCALL
SECCOMP_RET_ERRNO
SECCOMP_RET_TRACE
SECCOMP_RET_TRAP

Both ptrace and seccomp will trigger via _TIF_WORK_SYSCALL_ENTRY. Only ptrace will trigger via _TIF_WORK_SYSCALL_EXIT.

For SECCOMP_RET_ERRNO to work, we must skip the syscall, as mentioned earlier:

arch/x86/kernel/entry_32.S ...
syscall_trace_entry:

        movl $-ENOSYS,PT_EAX(%esp)
        movl %esp, %eax
        call syscall_trace_enter
        /* What it returned is what we'll actually use.  */
        cmpl $(NR_syscalls), %eax
        jnae syscall_call
        jmp syscall_exit

END(syscall_trace_entry)

Both before and after the 2-phase change, syscall_trace_enter would return -1 if it hit SECCOMP_RET_ERRNO, before calling tracehook_report_syscall_entry. On exit, if PTRACE_SYSCALL, we'd hit tracehook_report_syscall_exit during syscall_trace_leave, which means a ptracer will see a syscall-exit-stop without a matching syscall-enter-stop.

Using SECCOMP_RET_TRACE with PTRACE_SYSCALL in place seems totally crazy, as the ptracer would need to be the same program, and if it chose to skip a syscall, it would be in the same place: it would see PTRACE_EVENT_SECCOMP, then no syscall-enter-stop, then a syscall-exit-stop. I think we can ignore this pathological case.

Using SECCOMP_RET_TRAP with PTRACE_SYSCALL also results in a skip, which produces the same "only syscall-exit-stop seen" problem.

In the SECCOMP_RET_ERRNO case, the syscall nr doesn't change (and isn't executed). In the SECCOMP_RET_TRAP case, the syscall nr doesn't change (and isn't executed). In the SECCOMP_RET_TRACE, the syscall nr _could_ change, but the ptracer would be doing it, so the crazy situation around PTRACE_SYSCALL is probably safe to ignore (as long as we document what is expected to happen).

Quoted 6 lines

So, the question is: should PTRACE_SYSCALL see a syscall that is _not_ being executed (due to seccomp)? Audit doesn't see it currently, and neither does ptrace. I would argue that it should continue to not see the syscall. That said, if it shouldn't be shown, we also shouldn't trigger syscall-exit-stop. If you can convince me it should see syscall-enter-stop, then I have two questions:

I think PTRACE_SYSCALL should see syscalls that are skipped due to seccomp. I think that the exit event should see the modified errno, if any, so that strace will show whatever the traced process thinks is happening.

Quoted 4 lines

1) Do we accept that a ptracer can interfere with SECCOMP_RET_ERRNO? I think we probably must, since it can already interfere via syscall-exit-stop and change the errno.

I think this is fine.

Quoted 8 lines
Quoted 3 lines

And especially since a ptracer
can change syscalls during syscall-enter-stop to any syscall it wants, bypassing seccomp. This condition is already documented.

If a ptracer (using PTRACE_SYSCALL) were to get the entry callback before seccomp, then this oddity would go away, which might be a good thing. A ptracer could change the syscall, but seccomp would based on what the ptracer changed the syscall to.

I want kill events to trigger immediately. I don't want to leave the ptrace surface available on a SECCOMP_RET_KILL. So maybe it can be seccomp phase 1, then ptrace, then seccomp phase 2? And pass more information between phases to determine how things should behave beyond just "skip"?

Quoted 8 lines
Quoted 2 lines

2) What do we do with audit? Suddenly we have ptrace seeing a syscall that audit doesn't?

Is this a problem? I'd be amazed if program uses both ptrace and audit -- after all, audit is a global thing, and it only has one implementation (AFAIK): auditd. auditd doesn't ptrace the world.

Quoted 1 line
Quoted 8 lines
Quoted 5 lines

And an unrelated thought:

3) Can't we find some way to fix the inability of a ptracer to distinguish between syscall-enter-stop and syscall-exit-stop?

Couldn't we add PTRACE_O_TRACESYSENTRY and PTRACE_O_TRACESYSEXIT along the lines of PTRACE_O_TRACESYSGOOD?

That might be a nice idea. I haven't written a test to see, but what does PTRACE_GETEVENTMSG return on syscall-enter/exit-stop? If we can't add something there, then yeah, adding PTRACE_O_TRACESYSENTRY and PTRACE_O_TRACESYSEXIT with their own event msgs would be nice. Could even add the syscall nr to the event msg so ptracers don't have to dig around in per-arch registers, too.

-Kees

Signature · 2 lines
Kees Cook
Chrome OS Security
author reviewer
#20 lore raw

Re: [PATCH v5 3/5] x86: Split syscall_trace_enter into two phases

Quoted 11 lines · Kees Cook
Quoted 9 lines · Andy Lutomirski
Quoted 3 lines · Kees Cook

And especially since a ptracer
can change syscalls during syscall-enter-stop to any syscall it wants, bypassing seccomp. This condition is already documented.

If a ptracer (using PTRACE_SYSCALL) were to get the entry callback before seccomp, then this oddity would go away, which might be a good thing. A ptracer could change the syscall, but seccomp would based on what the ptracer changed the syscall to.

Quoted 5 lines

I want kill events to trigger immediately. I don't want to leave the ptrace surface available on a SECCOMP_RET_KILL. So maybe it can be seccomp phase 1, then ptrace, then seccomp phase 2? And pass more information between phases to determine how things should behave beyond just "skip"?

I thought so too, originally, but I'm far less convinced now, for two reasons:

1. I think that a lot of filters these days use RET_ERRNO heavily, so this won't benefit them.

2. I'm not convinced it really reduces the attack surface for anyone. Unless your filter is literally "return SECCOMP_RET_KILL", then the seccomp-filtered task can always cause the ptracer to get a pair of syscall notifications. Also, the task can send itself signals (using page faults, breakpoints, etc) and cause ptrace events via other paths.

--Andy

#21 lore raw

Re: [PATCH v5 3/5] x86: Split syscall_trace_enter into two phases

Quoted 23 lines · Andy Lutomirski
Quoted 11 lines · Kees Cook
Quoted 9 lines · Andy Lutomirski
Quoted 3 lines · Kees Cook

And especially since a ptracer
can change syscalls during syscall-enter-stop to any syscall it wants, bypassing seccomp. This condition is already documented.

If a ptracer (using PTRACE_SYSCALL) were to get the entry callback before seccomp, then this oddity would go away, which might be a good thing. A ptracer could change the syscall, but seccomp would based on what the ptracer changed the syscall to.

Quoted 5 lines

I want kill events to trigger immediately. I don't want to leave the ptrace surface available on a SECCOMP_RET_KILL. So maybe it can be seccomp phase 1, then ptrace, then seccomp phase 2? And pass more information between phases to determine how things should behave beyond just "skip"?

I thought so too, originally, but I'm far less convinced now, for two reasons:

1. I think that a lot of filters these days use RET_ERRNO heavily, so this won't benefit them.

Quoted 6 lines

2. I'm not convinced it really reduces the attack surface for anyone. Unless your filter is literally "return SECCOMP_RET_KILL", then the seccomp-filtered task can always cause the ptracer to get a pair of syscall notifications. Also, the task can send itself signals (using page faults, breakpoints, etc) and cause ptrace events via other paths.

What are you thinking for a solution?

As for capping SECCOMP_RET_ERRNO to MAX_ERRNO, how about this (sorry if gmail butchers the paste):

kernel/seccomp.c +3 -1
index 4ef9687ac115..c88148d20bd5 100644
@@ -629,7 +629,9 @@ static u32 __seccomp_phase1_filter(int this_syscall, struct
 
        switch (action) {
        case SECCOMP_RET_ERRNO:
-               /* Set the low-order 16-bits as a errno. */
+               /* Set the low-order bits as a errno. */
+               if (data > MAX_ERRNO)
+                       data = MAX_ERRNO;
                syscall_set_return_value(current, task_pt_regs(current),
                                         -data, 0);
                goto skip;
Signature · 2 lines
Kees Cook
Chrome OS Security
author reviewer
#22 lore raw

Re: [PATCH v5 3/5] x86: Split syscall_trace_enter into two phases

Quoted 24 lines · Kees Cook
Quoted 23 lines · Andy Lutomirski
Quoted 11 lines · Kees Cook
Quoted 9 lines · Andy Lutomirski
Quoted 3 lines · Kees Cook

And especially since a ptracer
can change syscalls during syscall-enter-stop to any syscall it wants, bypassing seccomp. This condition is already documented.

If a ptracer (using PTRACE_SYSCALL) were to get the entry callback before seccomp, then this oddity would go away, which might be a good thing. A ptracer could change the syscall, but seccomp would based on what the ptracer changed the syscall to.

Quoted 5 lines

I want kill events to trigger immediately. I don't want to leave the ptrace surface available on a SECCOMP_RET_KILL. So maybe it can be seccomp phase 1, then ptrace, then seccomp phase 2? And pass more information between phases to determine how things should behave beyond just "skip"?

I thought so too, originally, but I'm far less convinced now, for two reasons:

1. I think that a lot of filters these days use RET_ERRNO heavily, so this won't benefit them.

Quoted 9 lines
Quoted 6 lines

2. I'm not convinced it really reduces the attack surface for anyone. Unless your filter is literally "return SECCOMP_RET_KILL", then the seccomp-filtered task can always cause the ptracer to get a pair of syscall notifications. Also, the task can send itself signals (using page faults, breakpoints, etc) and cause ptrace events via other paths.

What are you thinking for a solution?

I'm writing a patch now. It's an ABI break, but this thread seems to show that the ABI was somewhat useless before the split-phase changes, and it's differently broken now, so I would be surprised if the change broke anything that was currently working. I'll send it later today, hopefully.

Quoted 9 lines

As for capping SECCOMP_RET_ERRNO to MAX_ERRNO, how about this (sorry if gmail butchers the paste):

kernel/seccomp.c +0 -0
index 4ef9687ac115..c88148d20bd5 100644

kernel/seccomp.c static u32 __seccomp_phase1_filter(int this_syscall, struct · line 637

Quoted 10 lines
kernel/seccomp.c +3 -1
@@ -629,7 +629,9 @@ static u32 __seccomp_phase1_filter(int this_syscall, struct
 
        switch (action) {
        case SECCOMP_RET_ERRNO:
-               /* Set the low-order 16-bits as a errno. */
+               /* Set the low-order bits as a errno. */
+               if (data > MAX_ERRNO)
+                       data = MAX_ERRNO;
                syscall_set_return_value(current, task_pt_regs(current),
                                         -data, 0);
                goto skip;

I'm fine with this, but I'm not entirely convinced it solves a problem. SECCOMP_RET_ERRNO | 5000 didn't work before, and it doesn't work now. Admittedly, the new failure mode is possibly better.

--Andy

#23 lore raw

Re: a method to distinguish between syscall-enter/exit-stop

In reply to Kees Cook
Quoted 2 lines · Kees Cook
Quoted 1 line · Andy Lutomirski

On Fri, Feb 6, 2015 at 11:23 AM, Kees Cook <[email protected]> wrote:

[...]

Quoted 10 lines
Quoted 7 lines
Quoted 4 lines

And an unrelated thought:

3) Can't we find some way to fix the inability of a ptracer to distinguish between syscall-enter-stop and syscall-exit-stop?

Couldn't we add PTRACE_O_TRACESYSENTRY and PTRACE_O_TRACESYSEXIT along the lines of PTRACE_O_TRACESYSGOOD?

That might be a nice idea. I haven't written a test to see, but what does PTRACE_GETEVENTMSG return on syscall-enter/exit-stop?

The value returned by PTRACE_GETEVENTMSG is the value set along with the latest PTRACE_EVENT_*.
In case of syscall-enter/exit-stop (which is not a PTRACE_EVENT_*), there is no particular value set for PTRACE_GETEVENTMSG.

Signature · 1 line
ldv
#24 lore raw

Re: a method to distinguish between syscall-enter/exit-stop

Quoted 12 lines · Dmitry V. Levin
Quoted 2 lines · Kees Cook
Quoted 1 line · Andy Lutomirski

On Fri, Feb 6, 2015 at 11:23 AM, Kees Cook <[email protected]> wrote:

[...]

Quoted 8 lines
Quoted 7 lines
Quoted 4 lines

And an unrelated thought:

3) Can't we find some way to fix the inability of a ptracer to distinguish between syscall-enter-stop and syscall-exit-stop?

Couldn't we add PTRACE_O_TRACESYSENTRY and PTRACE_O_TRACESYSEXIT along the lines of PTRACE_O_TRACESYSGOOD?

Quoted 7 lines
Quoted 2 lines

That might be a nice idea. I haven't written a test to see, but what does PTRACE_GETEVENTMSG return on syscall-enter/exit-stop?

The value returned by PTRACE_GETEVENTMSG is the value set along with the latest PTRACE_EVENT_*.
In case of syscall-enter/exit-stop (which is not a PTRACE_EVENT_*), there is no particular value set for PTRACE_GETEVENTMSG.

Could we define one to help distinguish?

-Kees

Signature · 2 lines
Kees Cook
Chrome OS Security
#25 lore raw

Re: a method to distinguish between syscall-enter/exit-stop

Quoted 16 lines · Kees Cook
Quoted 15 lines · Dmitry V. Levin
Quoted 2 lines · Kees Cook
Quoted 1 line · Andy Lutomirski

On Fri, Feb 6, 2015 at 11:23 AM, Kees Cook <[email protected]> wrote:

[...]

Quoted 10 lines
Quoted 7 lines
Quoted 4 lines

And an unrelated thought:

3) Can't we find some way to fix the inability of a ptracer to distinguish between syscall-enter-stop and syscall-exit-stop?

Couldn't we add PTRACE_O_TRACESYSENTRY and PTRACE_O_TRACESYSEXIT along the lines of PTRACE_O_TRACESYSGOOD?

That might be a nice idea. I haven't written a test to see, but what does PTRACE_GETEVENTMSG return on syscall-enter/exit-stop?

Quoted 6 lines
Quoted 4 lines

The value returned by PTRACE_GETEVENTMSG is the value set along with the latest PTRACE_EVENT_*.
In case of syscall-enter/exit-stop (which is not a PTRACE_EVENT_*), there is no particular value set for PTRACE_GETEVENTMSG.

Could we define one to help distinguish?

I suppose we could define one, but performing extra PTRACE_GETEVENTMSG for every syscall-stop may be too expensive.

For example, strace makes about 4.5 syscalls per syscall-stop. The minimum is 4 syscalls: wait4, PTRACE_GETREGSET, write, and PTRACE_SYSCALL; processing some syscall-stops may require additional process_vm_readv calls.

That is, forcing strace to make extra PTRACE_GETEVENTMSG per syscall-stop would result to about 20% more syscalls per syscall-stop, that is a noticeable cost.

A better alternative is to define an event that wouldn't require this extra PTRACE_GETEVENTMSG per syscall-stop. For example, it could be a PTRACE_EVENT_SYSCALL_ENTRY and/or PTRACE_EVENT_SYSCALL_EXIT. In practice, adding just one of these two events would be enough to distinguish two kinds of syscall-stops. Adding two events would look less surprising, though.

If the decision would be to add both events, I'd recommend adding just one new option to cover both events - there is a room only for 32 different PTRACE_O_* options.

Signature · 1 line
ldv
#26 lore raw

Re: [PATCH v5 3/5] x86: Split syscall_trace_enter into two phases

In reply to Kees Cook
Quoted 4 lines · Kees Cook

Strictly speaking (ISO C, "man 3 errno"), errno is supposed to be a full int, though digging around I find this in include/linux/err.h:

That doesn't mean the kernel has to support them.

Quoted 10 lines

/*
* Kernel pointers have redundant information, so we can use a
* scheme where we can return either an error code or a normal
* pointer with the same return value.
*
* This should be a per-architecture thing, to allow different
* error and pointer decisions.
*/
#define MAX_ERRNO 4095

Quoted 6 lines

#ifndef __ASSEMBLY__

#define IS_ERR_VALUE(x) unlikely((x) >= (unsigned long)-MAX_ERRNO)

But no architecture overrides this.

We used to have a much lower value, that was per-architecture, in order to optimize the resulting assembly (e.g. 8-bit immediates on x86). This didn't work as the number of errnos increased. The other motivation was probably binary compatibility with other Unices, which was an idea for a while.

-hpa

author reviewer
#27 lore raw

PATCH v5 4/5 x86_64,entry: Treat regs->ax the same in fastpath and slowpath syscalls

In reply to Andy Lutomirski

For slowpath syscalls, we initialize regs->ax to -ENOSYS and stick the syscall number into regs->orig_ax prior to any possible tracing and syscall execution. This is user-visible ABI used by ptrace syscall emulation and seccomp.

For fastpath syscalls, there's no good reason not to do the same thing. It's even slightly simpler than what we're currently doing. It probably has no measureable performance impact. It should have no user-visible effect.

The purpose of this patch is to prepare for two-phase syscall tracing, in which the first phase might modify the saved RAX without leaving the fast path. This change is just subtle enough that I'm keeping it separate.

---
 arch/x86/include/asm/calling.h |  6 +++++-
 arch/x86/kernel/entry_64.S     | 13 ++++---------
 2 files changed, 9 insertions(+), 10 deletions(-)
arch/x86/include/asm/calling.h +5 -1
index cb4c73bfeb48..76659b67fd11 100644
@@ -85,7 +85,7 @@ For 32-bit we have the following conventions - kernel is built with
 #define ARGOFFSET	R11
 #define SWFRAME		ORIG_RAX
 
-	.macro SAVE_ARGS addskip=0, save_rcx=1, save_r891011=1
+	.macro SAVE_ARGS addskip=0, save_rcx=1, save_r891011=1, rax_enosys=0
 	subq  $9*8+\addskip, %rsp
 	CFI_ADJUST_CFA_OFFSET	9*8+\addskip
 	movq_cfi rdi, 8*8
@@ -96,7 +96,11 @@ For 32-bit we have the following conventions - kernel is built with
 	movq_cfi rcx, 5*8
 	.endif
 
+	.if \rax_enosys
+	movq $-ENOSYS, 4*8(%rsp)
+	.else
 	movq_cfi rax, 4*8
+	.endif
 
 	.if \save_r891011
 	movq_cfi r8,  3*8
arch/x86/kernel/entry_64.S +4 -9
index 2fac1343a90b..0bd6d3c28064 100644
@@ -404,8 +404,8 @@ GLOBAL(system_call_after_swapgs)
 	 * and short:
 	 */
 	ENABLE_INTERRUPTS(CLBR_NONE)
-	SAVE_ARGS 8,0
-	movq  %rax,ORIG_RAX-ARGOFFSET(%rsp)
+	SAVE_ARGS 8, 0, rax_enosys=1
+	movq_cfi rax,(ORIG_RAX-ARGOFFSET)
 	movq  %rcx,RIP-ARGOFFSET(%rsp)
 	CFI_REL_OFFSET rip,RIP-ARGOFFSET
 	testl $_TIF_WORK_SYSCALL_ENTRY,TI_flags+THREAD_INFO(%rsp,RIP-ARGOFFSET)
@@ -417,7 +417,7 @@ system_call_fastpath:
 	andl $__SYSCALL_MASK,%eax
 	cmpl $__NR_syscall_max,%eax
 #endif
-	ja badsys
+	ja ret_from_sys_call  /* and return regs->ax */
 	movq %r10,%rcx
 	call *sys_call_table(,%rax,8)  # XXX:	 rip relative
 	movq %rax,RAX-ARGOFFSET(%rsp)
@@ -476,10 +476,6 @@ sysret_signal:
 	FIXUP_TOP_OF_STACK %r11, -ARGOFFSET
 	jmp int_check_syscall_exit_work
 
-badsys:
-	movq $-ENOSYS,RAX-ARGOFFSET(%rsp)
-	jmp ret_from_sys_call
-
 #ifdef CONFIG_AUDITSYSCALL
 	/*
 	 * Fast path for syscall audit without full syscall trace.
@@ -519,7 +515,6 @@ tracesys:
 	jz auditsys
 #endif
 	SAVE_REST
-	movq $-ENOSYS,RAX(%rsp) /* ptrace can change this for a bad syscall */
 	FIXUP_TOP_OF_STACK %rdi
 	movq %rsp,%rdi
 	call syscall_trace_enter
@@ -536,7 +531,7 @@ tracesys:
 	andl $__SYSCALL_MASK,%eax
 	cmpl $__NR_syscall_max,%eax
 #endif
-	ja   int_ret_from_sys_call	/* RAX(%rsp) set to -ENOSYS above */
+	ja   int_ret_from_sys_call	/* RAX(%rsp) is already set */
 	movq %r10,%rcx	/* fixup for C */
 	call *sys_call_table(,%rax,8)
 	movq %rax,RAX-ARGOFFSET(%rsp)
Signature · 1 line
1.9.3
#28 lore raw

[tip:x86/seccomp] x86_64, entry: Treat regs-> ax the same in fastpath and slowpath syscalls

x86_64, entry: Treat regs->ax the same in fastpath and slowpath syscalls
Commit-ID:  54eea9957f5763dd1a2555d7e4cb53b4dd389cc6
Gitweb:     http://git.kernel.org/tip/54eea9957f5763dd1a2555d7e4cb53b4dd389cc6
Author:     Andy Lutomirski <[email protected]>
AuthorDate: Fri, 5 Sep 2014 15:13:55 -0700
Committer:  H. Peter Anvin <[email protected]>
CommitDate: Mon, 8 Sep 2014 14:14:08 -0700

x86_64, entry: Treat regs->ax the same in fastpath and slowpath syscalls

For slowpath syscalls, we initialize regs->ax to -ENOSYS and stick the syscall number into regs->orig_ax prior to any possible tracing and syscall execution. This is user-visible ABI used by ptrace syscall emulation and seccomp.

For fastpath syscalls, there's no good reason not to do the same thing. It's even slightly simpler than what we're currently doing. It probably has no measureable performance impact. It should have no user-visible effect.

The purpose of this patch is to prepare for two-phase syscall tracing, in which the first phase might modify the saved RAX without leaving the fast path. This change is just subtle enough that I'm keeping it separate.

---
 arch/x86/include/asm/calling.h |  6 +++++-
 arch/x86/kernel/entry_64.S     | 13 ++++---------
 2 files changed, 9 insertions(+), 10 deletions(-)
arch/x86/include/asm/calling.h +5 -1
index cb4c73b..76659b6 100644
@@ -85,7 +85,7 @@ For 32-bit we have the following conventions - kernel is built with
 #define ARGOFFSET	R11
 #define SWFRAME		ORIG_RAX
 
-	.macro SAVE_ARGS addskip=0, save_rcx=1, save_r891011=1
+	.macro SAVE_ARGS addskip=0, save_rcx=1, save_r891011=1, rax_enosys=0
 	subq  $9*8+\addskip, %rsp
 	CFI_ADJUST_CFA_OFFSET	9*8+\addskip
 	movq_cfi rdi, 8*8
@@ -96,7 +96,11 @@ For 32-bit we have the following conventions - kernel is built with
 	movq_cfi rcx, 5*8
 	.endif
 
+	.if \rax_enosys
+	movq $-ENOSYS, 4*8(%rsp)
+	.else
 	movq_cfi rax, 4*8
+	.endif
 
 	.if \save_r891011
 	movq_cfi r8,  3*8
arch/x86/kernel/entry_64.S +4 -9
index 2fac134..0bd6d3c 100644
@@ -404,8 +404,8 @@ GLOBAL(system_call_after_swapgs)
 	 * and short:
 	 */
 	ENABLE_INTERRUPTS(CLBR_NONE)
-	SAVE_ARGS 8,0
-	movq  %rax,ORIG_RAX-ARGOFFSET(%rsp)
+	SAVE_ARGS 8, 0, rax_enosys=1
+	movq_cfi rax,(ORIG_RAX-ARGOFFSET)
 	movq  %rcx,RIP-ARGOFFSET(%rsp)
 	CFI_REL_OFFSET rip,RIP-ARGOFFSET
 	testl $_TIF_WORK_SYSCALL_ENTRY,TI_flags+THREAD_INFO(%rsp,RIP-ARGOFFSET)
@@ -417,7 +417,7 @@ system_call_fastpath:
 	andl $__SYSCALL_MASK,%eax
 	cmpl $__NR_syscall_max,%eax
 #endif
-	ja badsys
+	ja ret_from_sys_call  /* and return regs->ax */
 	movq %r10,%rcx
 	call *sys_call_table(,%rax,8)  # XXX:	 rip relative
 	movq %rax,RAX-ARGOFFSET(%rsp)
@@ -476,10 +476,6 @@ sysret_signal:
 	FIXUP_TOP_OF_STACK %r11, -ARGOFFSET
 	jmp int_check_syscall_exit_work
 
-badsys:
-	movq $-ENOSYS,RAX-ARGOFFSET(%rsp)
-	jmp ret_from_sys_call
-
 #ifdef CONFIG_AUDITSYSCALL
 	/*
 	 * Fast path for syscall audit without full syscall trace.
@@ -519,7 +515,6 @@ tracesys:
 	jz auditsys
 #endif
 	SAVE_REST
-	movq $-ENOSYS,RAX(%rsp) /* ptrace can change this for a bad syscall */
 	FIXUP_TOP_OF_STACK %rdi
 	movq %rsp,%rdi
 	call syscall_trace_enter
@@ -536,7 +531,7 @@ tracesys:
 	andl $__SYSCALL_MASK,%eax
 	cmpl $__NR_syscall_max,%eax
 #endif
-	ja   int_ret_from_sys_call	/* RAX(%rsp) set to -ENOSYS above */
+	ja   int_ret_from_sys_call	/* RAX(%rsp) is already set */
 	movq %r10,%rcx	/* fixup for C */
 	call *sys_call_table(,%rax,8)
 	movq %rax,RAX-ARGOFFSET(%rsp)
author reviewer
#29 lore raw

PATCH v5 5/5 x86_64,entry: Use split-phase syscall_trace_enter for 64-bit syscalls

In reply to Andy Lutomirski

On KVM on my box, this reduces the overhead from an always-accept seccomp filter from ~130ns to ~17ns. Most of that comes from avoiding IRET on every syscall when seccomp is enabled.

In extremely approximate hacked-up benchmarking, just bypassing IRET saves about 80ns, so there's another 43ns of savings here from simplifying the seccomp path.

The diffstat is also rather nice :)

---
 arch/x86/kernel/entry_64.S | 38 +++++++++++++++-----------------------
 1 file changed, 15 insertions(+), 23 deletions(-)
arch/x86/kernel/entry_64.S +15 -23
index 0bd6d3c28064..df088bb03fb3 100644
@@ -478,22 +478,6 @@ sysret_signal:
 
 #ifdef CONFIG_AUDITSYSCALL
 	/*
-	 * Fast path for syscall audit without full syscall trace.
-	 * We just call __audit_syscall_entry() directly, and then
-	 * jump back to the normal fast path.
-	 */
-auditsys:
-	movq %r10,%r9			/* 6th arg: 4th syscall arg */
-	movq %rdx,%r8			/* 5th arg: 3rd syscall arg */
-	movq %rsi,%rcx			/* 4th arg: 2nd syscall arg */
-	movq %rdi,%rdx			/* 3rd arg: 1st syscall arg */
-	movq %rax,%rsi			/* 2nd arg: syscall number */
-	movl $AUDIT_ARCH_X86_64,%edi	/* 1st arg: audit arch */
-	call __audit_syscall_entry
-	LOAD_ARGS 0		/* reload call-clobbered registers */
-	jmp system_call_fastpath
-
-	/*
 	 * Return fast path for syscall audit.  Call __audit_syscall_exit()
 	 * directly and then jump back to the fast path with TIF_SYSCALL_AUDIT
 	 * masked off.
@@ -510,17 +494,25 @@ sysret_audit:
 
 	/* Do syscall tracing */
 tracesys:
-#ifdef CONFIG_AUDITSYSCALL
-	testl $(_TIF_WORK_SYSCALL_ENTRY & ~_TIF_SYSCALL_AUDIT),TI_flags+THREAD_INFO(%rsp,RIP-ARGOFFSET)
-	jz auditsys
-#endif
+	leaq -REST_SKIP(%rsp), %rdi
+	movq $AUDIT_ARCH_X86_64, %rsi
+	call syscall_trace_enter_phase1
+	test %rax, %rax
+	jnz tracesys_phase2		/* if needed, run the slow path */
+	LOAD_ARGS 0			/* else restore clobbered regs */
+	jmp system_call_fastpath	/*      and return to the fast path */
+
+tracesys_phase2:
 	SAVE_REST
 	FIXUP_TOP_OF_STACK %rdi
-	movq %rsp,%rdi
-	call syscall_trace_enter
+	movq %rsp, %rdi
+	movq $AUDIT_ARCH_X86_64, %rsi
+	movq %rax,%rdx
+	call syscall_trace_enter_phase2
+
 	/*
 	 * Reload arg registers from stack in case ptrace changed them.
-	 * We don't reload %rax because syscall_trace_enter() returned
+	 * We don't reload %rax because syscall_trace_entry_phase2() returned
 	 * the value it wants us to use in the table lookup.
 	 */
 	LOAD_ARGS ARGOFFSET, 1
Signature · 1 line
1.9.3
#30 lore raw

[tip:x86/seccomp] x86_64, entry: Use split-phase syscall_trace_enter for 64-bit syscalls

x86_64, entry: Use split-phase syscall_trace_enter for 64-bit syscalls
Commit-ID:  1dcf74f6edfc3a9acd84d83d8865dd9e2a3b1d1e
Gitweb:     http://git.kernel.org/tip/1dcf74f6edfc3a9acd84d83d8865dd9e2a3b1d1e
Author:     Andy Lutomirski <[email protected]>
AuthorDate: Fri, 5 Sep 2014 15:13:56 -0700
Committer:  H. Peter Anvin <[email protected]>
CommitDate: Mon, 8 Sep 2014 14:14:12 -0700

x86_64, entry: Use split-phase syscall_trace_enter for 64-bit syscalls

On KVM on my box, this reduces the overhead from an always-accept seccomp filter from ~130ns to ~17ns. Most of that comes from avoiding IRET on every syscall when seccomp is enabled.

In extremely approximate hacked-up benchmarking, just bypassing IRET saves about 80ns, so there's another 43ns of savings here from simplifying the seccomp path.

The diffstat is also rather nice :)

---
 arch/x86/kernel/entry_64.S | 38 +++++++++++++++-----------------------
 1 file changed, 15 insertions(+), 23 deletions(-)
arch/x86/kernel/entry_64.S +15 -23
index 0bd6d3c..df088bb 100644
@@ -478,22 +478,6 @@ sysret_signal:
 
 #ifdef CONFIG_AUDITSYSCALL
 	/*
-	 * Fast path for syscall audit without full syscall trace.
-	 * We just call __audit_syscall_entry() directly, and then
-	 * jump back to the normal fast path.
-	 */
-auditsys:
-	movq %r10,%r9			/* 6th arg: 4th syscall arg */
-	movq %rdx,%r8			/* 5th arg: 3rd syscall arg */
-	movq %rsi,%rcx			/* 4th arg: 2nd syscall arg */
-	movq %rdi,%rdx			/* 3rd arg: 1st syscall arg */
-	movq %rax,%rsi			/* 2nd arg: syscall number */
-	movl $AUDIT_ARCH_X86_64,%edi	/* 1st arg: audit arch */
-	call __audit_syscall_entry
-	LOAD_ARGS 0		/* reload call-clobbered registers */
-	jmp system_call_fastpath
-
-	/*
 	 * Return fast path for syscall audit.  Call __audit_syscall_exit()
 	 * directly and then jump back to the fast path with TIF_SYSCALL_AUDIT
 	 * masked off.
@@ -510,17 +494,25 @@ sysret_audit:
 
 	/* Do syscall tracing */
 tracesys:
-#ifdef CONFIG_AUDITSYSCALL
-	testl $(_TIF_WORK_SYSCALL_ENTRY & ~_TIF_SYSCALL_AUDIT),TI_flags+THREAD_INFO(%rsp,RIP-ARGOFFSET)
-	jz auditsys
-#endif
+	leaq -REST_SKIP(%rsp), %rdi
+	movq $AUDIT_ARCH_X86_64, %rsi
+	call syscall_trace_enter_phase1
+	test %rax, %rax
+	jnz tracesys_phase2		/* if needed, run the slow path */
+	LOAD_ARGS 0			/* else restore clobbered regs */
+	jmp system_call_fastpath	/*      and return to the fast path */
+
+tracesys_phase2:
 	SAVE_REST
 	FIXUP_TOP_OF_STACK %rdi
-	movq %rsp,%rdi
-	call syscall_trace_enter
+	movq %rsp, %rdi
+	movq $AUDIT_ARCH_X86_64, %rsi
+	movq %rax,%rdx
+	call syscall_trace_enter_phase2
+
 	/*
 	 * Reload arg registers from stack in case ptrace changed them.
-	 * We don't reload %rax because syscall_trace_enter() returned
+	 * We don't reload %rax because syscall_trace_entry_phase2() returned
 	 * the value it wants us to use in the table lookup.
 	 */
 	LOAD_ARGS ARGOFFSET, 1
Acked-by on the v5 series
#31 lore raw
In reply to Andy Lutomirski
Quoted 67 lines · Andy Lutomirski

This applies to:
git://git.kernel.org/pub/scm/linux/kernel/git/kees/linux.git seccomp-fastpath

Gitweb:
https://git.kernel.org/cgit/linux/kernel/git/kees/linux.git/log/?h=seccomp/fastpath

This is both a cleanup and a speedup. It reduces overhead due to installing a trivial seccomp filter by 87%. The speedup comes from avoiding the full syscall tracing mechanism for filters that don't return SECCOMP_RET_TRACE.

This series depends on splitting the seccomp hooks into two phases. The first phase evaluates the filter; it can skip syscalls, allow them, kill the calling task, or pass a u32 to the second phase. The second phase requires a full tracing context, and it sends ptrace events if necessary. The seccomp core part is in Kees' seccomp/fastpath tree.

These patches implement a similar split for the x86 syscall entry work. The C callback is invoked in two phases: the first has only a partial frame, and it can request phase 2 processing with a full frame.

Finally, I switch the 64-bit system_call code to use the new split entry work. This is a net deletion of assembly code: it replaces all of the audit entry muck.

In the process, I fixed some bugs.

If this is acceptable, someone can do the same tweak for the ia32entry and entry_32 code.

This passes all seccomp tests that I know of.

Changes from v4:
- Rebased (which seems to have been a no-op)
- Fixed embarrassing bug that broke allnoconfig
(patch 3 was missing an ifdef).

Changes from v3:
- Dropped the core seccomp changes from the email -- Kees has applied them.
- Add patch 2 (the TIF_NOHZ change).
- Fix TIF_NOHZ in the two-phase entry code (thanks, Oleg).

Changes from v2:
- Fixed 32-bit x86 build (and the tests pass).
- Put the doc patch where it belongs.

Changes from v1:
- Rebased on top of Kees' shiny new seccomp tree (no effect on the x86 part).
- Improved patch 6 vs patch 7 split (thanks Alexei!)
- Fixed bogus -ENOSYS in patch 5 (thanks Kees!)
- Improved changelog message in patch 6.

Changes from RFC version:
- The first three patches are more or less the same
- The rest is more or less a rewrite

Andy Lutomirski (5):
x86,x32,audit: Fix x32's AUDIT_ARCH wrt audit
x86,entry: Only call user_exit if TIF_NOHZ
x86: Split syscall_trace_enter into two phases x86_64,entry: Treat regs->ax the same in fastpath and slowpath
syscalls
x86_64,entry: Use split-phase syscall_trace_enter for 64-bit syscalls

Quoted 5 lines
 arch/x86/include/asm/calling.h |   6 +-
 arch/x86/include/asm/ptrace.h  |   5 ++
 arch/x86/kernel/entry_64.S     |  51 +++++--------
 arch/x86/kernel/ptrace.c       | 165 +++++++++++++++++++++++++++++++++--------
 4 files changed, 164 insertions(+), 63 deletions(-)

Consider the series:

As far as doing pulls, Peter, can you take the seccomp change from my tree as well? It makes sense to land all of this together.

Thanks!

-Kees

Signature · 2 lines
Kees Cook
Chrome OS Security
#32 lore raw
Quoted 4 lines · Kees Cook

As far as doing pulls, Peter, can you take the seccomp change from my tree as well? It makes sense to land all of this together.

That is the plan.

-hpa