linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] seccomp: plug syscall-dodging ptrace hole
@ 2016-05-26 21:04 Kees Cook
  2016-05-26 22:56 ` Jann Horn
  2016-05-27  2:10 ` Andy Lutomirski
  0 siblings, 2 replies; 12+ messages in thread
From: Kees Cook @ 2016-05-26 21:04 UTC (permalink / raw)
  To: Andy Lutomirski; +Cc: Jann Horn, Stephane Graber, Will Drewry, linux-kernel

One problem with seccomp was that ptrace could be used to change a
syscall after seccomp filtering had completed. This was a well documented
limitation, and it was recommended to block ptrace when defining a filter
to avoid this problem. This can be quite a limitation for containers or
other places where ptrace is desired even under seccomp filters.

Since seccomp filtering has been split into pre-trace and trace phases
(phase1 and phase2 respectively), it's possible to re-run phase1 seccomp
after ptrace. This makes that change, and updates the test suite for
both SECCOMP_RET_TRACE and PTRACE_SYSCALL manipulation.

Signed-off-by: Kees Cook <keescook@chromium.org>
---
 include/linux/seccomp.h                       |   6 +
 include/linux/tracehook.h                     |   8 +-
 kernel/seccomp.c                              |  42 ++++++
 tools/testing/selftests/seccomp/seccomp_bpf.c | 176 ++++++++++++++++++++++++--
 4 files changed, 220 insertions(+), 12 deletions(-)

diff --git a/include/linux/seccomp.h b/include/linux/seccomp.h
index 2296e6b2f690..e2b72394c200 100644
--- a/include/linux/seccomp.h
+++ b/include/linux/seccomp.h
@@ -85,6 +85,7 @@ static inline int seccomp_mode(struct seccomp *s)
 #ifdef CONFIG_SECCOMP_FILTER
 extern void put_seccomp_filter(struct task_struct *tsk);
 extern void get_seccomp_filter(struct task_struct *tsk);
+extern int seccomp_phase1_recheck(void);
 #else  /* CONFIG_SECCOMP_FILTER */
 static inline void put_seccomp_filter(struct task_struct *tsk)
 {
@@ -94,6 +95,11 @@ static inline void get_seccomp_filter(struct task_struct *tsk)
 {
 	return;
 }
+
+static inline int seccomp_phase1_recheck(void)
+{
+	return 0;
+}
 #endif /* CONFIG_SECCOMP_FILTER */
 
 #if defined(CONFIG_SECCOMP_FILTER) && defined(CONFIG_CHECKPOINT_RESTORE)
diff --git a/include/linux/tracehook.h b/include/linux/tracehook.h
index 26c152122a42..69b584d88508 100644
--- a/include/linux/tracehook.h
+++ b/include/linux/tracehook.h
@@ -48,6 +48,7 @@
 
 #include <linux/sched.h>
 #include <linux/ptrace.h>
+#include <linux/seccomp.h>
 #include <linux/security.h>
 #include <linux/task_work.h>
 #include <linux/memcontrol.h>
@@ -100,7 +101,12 @@ static inline int ptrace_report_syscall(struct pt_regs *regs)
 static inline __must_check int tracehook_report_syscall_entry(
 	struct pt_regs *regs)
 {
-	return ptrace_report_syscall(regs);
+	int skip;
+
+	skip = ptrace_report_syscall(regs);
+	if (skip)
+		return skip;
+	return seccomp_phase1_recheck();
 }
 
 /**
diff --git a/kernel/seccomp.c b/kernel/seccomp.c
index 7002796f14a4..6eaa3a1c5edb 100644
--- a/kernel/seccomp.c
+++ b/kernel/seccomp.c
@@ -665,6 +665,46 @@ u32 seccomp_phase1(struct seccomp_data *sd)
 }
 
 /**
+ * seccomp_phase1_recheck() - recheck phase1 in the context of ptrace
+ *
+ * This re-runs phase 1 seccomp checks in the case where ptrace may have
+ * just changed things out from under us.
+ *
+ * Returns 0 if the syscall should be processed or -1 to skip the syscall.
+ */
+int seccomp_phase1_recheck(void)
+{
+	u32 action;
+
+	/* If we're not under seccomp, continue normally. */
+	if (!test_thread_flag(TIF_SECCOMP))
+		return 0;
+
+	/* Pass NULL struct seccomp_data to force reload after ptrace. */
+	action = seccomp_phase1(NULL);
+	switch (action) {
+	case SECCOMP_PHASE1_OK:
+		/* Passes seccomp, continue normally. */
+		break;
+	case SECCOMP_PHASE1_SKIP:
+		/* Skip the syscall. */
+		return -1;
+	default:
+		if ((action & SECCOMP_RET_ACTION) != SECCOMP_RET_TRACE) {
+			/* Impossible return value: kill the process. */
+			do_exit(SIGSYS);
+		}
+		/*
+		 * We've hit a trace request, but ptrace already put us
+		 * into this state, so just continue.
+		 */
+		break;
+	}
+
+	return 0;
+}
+
+/**
  * seccomp_phase2() - finish slow path seccomp work for the current syscall
  * @phase1_result: The return value from seccomp_phase1()
  *
@@ -701,6 +741,8 @@ int seccomp_phase2(u32 phase1_result)
 		do_exit(SIGSYS);
 	if (syscall_get_nr(current, regs) < 0)
 		return -1;  /* Explicit request to skip. */
+	if (seccomp_phase1_recheck() < 0)
+		return -1;
 
 	return 0;
 }
diff --git a/tools/testing/selftests/seccomp/seccomp_bpf.c b/tools/testing/selftests/seccomp/seccomp_bpf.c
index 7947e568e057..b8f6c743998f 100644
--- a/tools/testing/selftests/seccomp/seccomp_bpf.c
+++ b/tools/testing/selftests/seccomp/seccomp_bpf.c
@@ -1021,8 +1021,8 @@ void tracer_stop(int sig)
 typedef void tracer_func_t(struct __test_metadata *_metadata,
 			   pid_t tracee, int status, void *args);
 
-void tracer(struct __test_metadata *_metadata, int fd, pid_t tracee,
-	    tracer_func_t tracer_func, void *args)
+void start_tracer(struct __test_metadata *_metadata, int fd, pid_t tracee,
+	    tracer_func_t tracer_func, void *args, bool ptrace_syscall)
 {
 	int ret = -1;
 	struct sigaction action = {
@@ -1042,12 +1042,16 @@ void tracer(struct __test_metadata *_metadata, int fd, pid_t tracee,
 	/* Wait for attach stop */
 	wait(NULL);
 
-	ret = ptrace(PTRACE_SETOPTIONS, tracee, NULL, PTRACE_O_TRACESECCOMP);
+	ret = ptrace(PTRACE_SETOPTIONS, tracee, NULL, ptrace_syscall ?
+						      PTRACE_O_TRACESYSGOOD :
+						      PTRACE_O_TRACESECCOMP);
 	ASSERT_EQ(0, ret) {
 		TH_LOG("Failed to set PTRACE_O_TRACESECCOMP");
 		kill(tracee, SIGKILL);
 	}
-	ptrace(PTRACE_CONT, tracee, NULL, 0);
+	ret = ptrace(ptrace_syscall ? PTRACE_SYSCALL : PTRACE_CONT,
+		     tracee, NULL, 0);
+	ASSERT_EQ(0, ret);
 
 	/* Unblock the tracee */
 	ASSERT_EQ(1, write(fd, "A", 1));
@@ -1063,12 +1067,13 @@ void tracer(struct __test_metadata *_metadata, int fd, pid_t tracee,
 			/* Child is dead. Time to go. */
 			return;
 
-		/* Make sure this is a seccomp event. */
-		ASSERT_EQ(true, IS_SECCOMP_EVENT(status));
+		/* Check if this is a seccomp event. */
+		ASSERT_EQ(!ptrace_syscall, IS_SECCOMP_EVENT(status));
 
 		tracer_func(_metadata, tracee, status, args);
 
-		ret = ptrace(PTRACE_CONT, tracee, NULL, NULL);
+		ret = ptrace(ptrace_syscall ? PTRACE_SYSCALL : PTRACE_CONT,
+			     tracee, NULL, 0);
 		ASSERT_EQ(0, ret);
 	}
 	/* Directly report the status of our test harness results. */
@@ -1079,7 +1084,7 @@ void tracer(struct __test_metadata *_metadata, int fd, pid_t tracee,
 void cont_handler(int num)
 { }
 pid_t setup_trace_fixture(struct __test_metadata *_metadata,
-			  tracer_func_t func, void *args)
+			  tracer_func_t func, void *args, bool ptrace_syscall)
 {
 	char sync;
 	int pipefd[2];
@@ -1095,7 +1100,8 @@ pid_t setup_trace_fixture(struct __test_metadata *_metadata,
 	signal(SIGALRM, cont_handler);
 	if (tracer_pid == 0) {
 		close(pipefd[0]);
-		tracer(_metadata, pipefd[1], tracee, func, args);
+		start_tracer(_metadata, pipefd[1], tracee, func, args,
+			     ptrace_syscall);
 		syscall(__NR_exit, 0);
 	}
 	close(pipefd[1]);
@@ -1177,7 +1183,7 @@ FIXTURE_SETUP(TRACE_poke)
 
 	/* Launch tracer. */
 	self->tracer = setup_trace_fixture(_metadata, tracer_poke,
-					   &self->tracer_args);
+					   &self->tracer_args, false);
 }
 
 FIXTURE_TEARDOWN(TRACE_poke)
@@ -1395,6 +1401,29 @@ void tracer_syscall(struct __test_metadata *_metadata, pid_t tracee,
 
 }
 
+void tracer_ptrace(struct __test_metadata *_metadata, pid_t tracee,
+		   int status, void *args)
+{
+	int ret, nr;
+	unsigned long msg;
+	static bool entry;
+
+	/* Make sure we got an empty message. */
+	ret = ptrace(PTRACE_GETEVENTMSG, tracee, NULL, &msg);
+	EXPECT_EQ(0, ret);
+	EXPECT_EQ(0, msg);
+
+	/* The only way to tell PTRACE_SYSCALL entry/exit is by counting. */
+	entry = !entry;
+	if (!entry)
+		return;
+
+	nr = get_syscall(_metadata, tracee);
+
+	if (nr == __NR_getpid)
+		change_syscall(_metadata, tracee, __NR_getppid);
+}
+
 FIXTURE_DATA(TRACE_syscall) {
 	struct sock_fprog prog;
 	pid_t tracer, mytid, mypid, parent;
@@ -1436,7 +1465,8 @@ FIXTURE_SETUP(TRACE_syscall)
 	ASSERT_NE(self->parent, self->mypid);
 
 	/* Launch tracer. */
-	self->tracer = setup_trace_fixture(_metadata, tracer_syscall, NULL);
+	self->tracer = setup_trace_fixture(_metadata, tracer_syscall, NULL,
+					   false);
 }
 
 FIXTURE_TEARDOWN(TRACE_syscall)
@@ -1496,6 +1526,130 @@ TEST_F(TRACE_syscall, syscall_dropped)
 	EXPECT_NE(self->mytid, syscall(__NR_gettid));
 }
 
+TEST_F(TRACE_syscall, skip_after_RET_TRACE)
+{
+	struct sock_filter filter[] = {
+		BPF_STMT(BPF_LD|BPF_W|BPF_ABS,
+			offsetof(struct seccomp_data, nr)),
+		BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, __NR_getppid, 0, 1),
+		BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ERRNO | EPERM),
+		BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ALLOW),
+	};
+	struct sock_fprog prog = {
+		.len = (unsigned short)ARRAY_SIZE(filter),
+		.filter = filter,
+	};
+	long ret;
+
+	ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0);
+	ASSERT_EQ(0, ret);
+
+	/* Install fixture filter. */
+	ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->prog, 0, 0);
+	ASSERT_EQ(0, ret);
+
+	/* Install "errno on getppid" filter. */
+	ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog, 0, 0);
+	ASSERT_EQ(0, ret);
+
+	/* Tracer will redirect getpid to getppid, and we should see EPERM. */
+	EXPECT_EQ(-1, syscall(__NR_getpid));
+	EXPECT_EQ(EPERM, errno);
+}
+
+TEST_F_SIGNAL(TRACE_syscall, kill_after_RET_TRACE, SIGSYS)
+{
+	struct sock_filter filter[] = {
+		BPF_STMT(BPF_LD|BPF_W|BPF_ABS,
+			offsetof(struct seccomp_data, nr)),
+		BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, __NR_getppid, 0, 1),
+		BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_KILL),
+		BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ALLOW),
+	};
+	struct sock_fprog prog = {
+		.len = (unsigned short)ARRAY_SIZE(filter),
+		.filter = filter,
+	};
+	long ret;
+
+	ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0);
+	ASSERT_EQ(0, ret);
+
+	/* Install fixture filter. */
+	ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->prog, 0, 0);
+	ASSERT_EQ(0, ret);
+
+	/* Install "death on getppid" filter. */
+	ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog, 0, 0);
+	ASSERT_EQ(0, ret);
+
+	/* Tracer will redirect getpid to getppid, and we should die. */
+	EXPECT_NE(self->mypid, syscall(__NR_getpid));
+}
+
+TEST_F(TRACE_syscall, skip_after_ptrace)
+{
+	struct sock_filter filter[] = {
+		BPF_STMT(BPF_LD|BPF_W|BPF_ABS,
+			offsetof(struct seccomp_data, nr)),
+		BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, __NR_getppid, 0, 1),
+		BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ERRNO | EPERM),
+		BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ALLOW),
+	};
+	struct sock_fprog prog = {
+		.len = (unsigned short)ARRAY_SIZE(filter),
+		.filter = filter,
+	};
+	long ret;
+
+	/* Swap SECCOMP_RET_TRACE tracer for PTRACE_SYSCALL tracer. */
+	teardown_trace_fixture(_metadata, self->tracer);
+	self->tracer = setup_trace_fixture(_metadata, tracer_ptrace, NULL,
+					   true);
+
+	ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0);
+	ASSERT_EQ(0, ret);
+
+	/* Install "errno on getppid" filter. */
+	ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog, 0, 0);
+	ASSERT_EQ(0, ret);
+
+	/* Tracer will redirect getpid to getppid, and we should see EPERM. */
+	EXPECT_EQ(-1, syscall(__NR_getpid));
+	EXPECT_EQ(EPERM, errno);
+}
+
+TEST_F_SIGNAL(TRACE_syscall, kill_after_ptrace, SIGSYS)
+{
+	struct sock_filter filter[] = {
+		BPF_STMT(BPF_LD|BPF_W|BPF_ABS,
+			offsetof(struct seccomp_data, nr)),
+		BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, __NR_getppid, 0, 1),
+		BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_KILL),
+		BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ALLOW),
+	};
+	struct sock_fprog prog = {
+		.len = (unsigned short)ARRAY_SIZE(filter),
+		.filter = filter,
+	};
+	long ret;
+
+	/* Swap SECCOMP_RET_TRACE tracer for PTRACE_SYSCALL tracer. */
+	teardown_trace_fixture(_metadata, self->tracer);
+	self->tracer = setup_trace_fixture(_metadata, tracer_ptrace, NULL,
+					   true);
+
+	ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0);
+	ASSERT_EQ(0, ret);
+
+	/* Install "death on getppid" filter. */
+	ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog, 0, 0);
+	ASSERT_EQ(0, ret);
+
+	/* Tracer will redirect getpid to getppid, and we should die. */
+	EXPECT_NE(self->mypid, syscall(__NR_getpid));
+}
+
 #ifndef __NR_seccomp
 # if defined(__i386__)
 #  define __NR_seccomp 354
-- 
2.6.3


-- 
Kees Cook
Chrome OS & Brillo Security

^ permalink raw reply related	[flat|nested] 12+ messages in thread

* Re: [PATCH] seccomp: plug syscall-dodging ptrace hole
  2016-05-26 21:04 [PATCH] seccomp: plug syscall-dodging ptrace hole Kees Cook
@ 2016-05-26 22:56 ` Jann Horn
  2016-05-27  2:10 ` Andy Lutomirski
  1 sibling, 0 replies; 12+ messages in thread
From: Jann Horn @ 2016-05-26 22:56 UTC (permalink / raw)
  To: Kees Cook; +Cc: Andy Lutomirski, Stephane Graber, Will Drewry, linux-kernel

[-- Attachment #1: Type: text/plain, Size: 4781 bytes --]

On Thu, May 26, 2016 at 02:04:50PM -0700, Kees Cook wrote:
> One problem with seccomp was that ptrace could be used to change a
> syscall after seccomp filtering had completed. This was a well documented
> limitation, and it was recommended to block ptrace when defining a filter
> to avoid this problem. This can be quite a limitation for containers or
> other places where ptrace is desired even under seccomp filters.
> 
> Since seccomp filtering has been split into pre-trace and trace phases
> (phase1 and phase2 respectively), it's possible to re-run phase1 seccomp
> after ptrace. This makes that change, and updates the test suite for
> both SECCOMP_RET_TRACE and PTRACE_SYSCALL manipulation.

Looks good to me. As far as I can tell, there are no codepaths that allow
manipulation of syscall arguments via ptrace register modification without
going through tracehook_report_syscall_entry() or seccomp_phase2(), and
the checks look good, too.


> Signed-off-by: Kees Cook <keescook@chromium.org>
> ---
>  include/linux/seccomp.h                       |   6 +
>  include/linux/tracehook.h                     |   8 +-
>  kernel/seccomp.c                              |  42 ++++++
>  tools/testing/selftests/seccomp/seccomp_bpf.c | 176 ++++++++++++++++++++++++--
>  4 files changed, 220 insertions(+), 12 deletions(-)
> 
> diff --git a/include/linux/seccomp.h b/include/linux/seccomp.h
> index 2296e6b2f690..e2b72394c200 100644
> --- a/include/linux/seccomp.h
> +++ b/include/linux/seccomp.h
> @@ -85,6 +85,7 @@ static inline int seccomp_mode(struct seccomp *s)
>  #ifdef CONFIG_SECCOMP_FILTER
>  extern void put_seccomp_filter(struct task_struct *tsk);
>  extern void get_seccomp_filter(struct task_struct *tsk);
> +extern int seccomp_phase1_recheck(void);
>  #else  /* CONFIG_SECCOMP_FILTER */
>  static inline void put_seccomp_filter(struct task_struct *tsk)
>  {
> @@ -94,6 +95,11 @@ static inline void get_seccomp_filter(struct task_struct *tsk)
>  {
>  	return;
>  }
> +
> +static inline int seccomp_phase1_recheck(void)
> +{
> +	return 0;
> +}
>  #endif /* CONFIG_SECCOMP_FILTER */
>  
>  #if defined(CONFIG_SECCOMP_FILTER) && defined(CONFIG_CHECKPOINT_RESTORE)
> diff --git a/include/linux/tracehook.h b/include/linux/tracehook.h
> index 26c152122a42..69b584d88508 100644
> --- a/include/linux/tracehook.h
> +++ b/include/linux/tracehook.h
> @@ -48,6 +48,7 @@
>  
>  #include <linux/sched.h>
>  #include <linux/ptrace.h>
> +#include <linux/seccomp.h>
>  #include <linux/security.h>
>  #include <linux/task_work.h>
>  #include <linux/memcontrol.h>
> @@ -100,7 +101,12 @@ static inline int ptrace_report_syscall(struct pt_regs *regs)
>  static inline __must_check int tracehook_report_syscall_entry(
>  	struct pt_regs *regs)
>  {
> -	return ptrace_report_syscall(regs);
> +	int skip;
> +
> +	skip = ptrace_report_syscall(regs);
> +	if (skip)
> +		return skip;
> +	return seccomp_phase1_recheck();
>  }
>  
>  /**
> diff --git a/kernel/seccomp.c b/kernel/seccomp.c
> index 7002796f14a4..6eaa3a1c5edb 100644
> --- a/kernel/seccomp.c
> +++ b/kernel/seccomp.c
> @@ -665,6 +665,46 @@ u32 seccomp_phase1(struct seccomp_data *sd)
>  }
>  
>  /**
> + * seccomp_phase1_recheck() - recheck phase1 in the context of ptrace
> + *
> + * This re-runs phase 1 seccomp checks in the case where ptrace may have
> + * just changed things out from under us.
> + *
> + * Returns 0 if the syscall should be processed or -1 to skip the syscall.
> + */
> +int seccomp_phase1_recheck(void)
> +{
> +	u32 action;
> +
> +	/* If we're not under seccomp, continue normally. */
> +	if (!test_thread_flag(TIF_SECCOMP))
> +		return 0;
> +
> +	/* Pass NULL struct seccomp_data to force reload after ptrace. */
> +	action = seccomp_phase1(NULL);
> +	switch (action) {
> +	case SECCOMP_PHASE1_OK:
> +		/* Passes seccomp, continue normally. */
> +		break;
> +	case SECCOMP_PHASE1_SKIP:
> +		/* Skip the syscall. */
> +		return -1;
> +	default:
> +		if ((action & SECCOMP_RET_ACTION) != SECCOMP_RET_TRACE) {
> +			/* Impossible return value: kill the process. */
> +			do_exit(SIGSYS);
> +		}
> +		/*
> +		 * We've hit a trace request, but ptrace already put us
> +		 * into this state, so just continue.
> +		 */
> +		break;
> +	}
> +
> +	return 0;
> +}
> +
> +/**
>   * seccomp_phase2() - finish slow path seccomp work for the current syscall
>   * @phase1_result: The return value from seccomp_phase1()
>   *
> @@ -701,6 +741,8 @@ int seccomp_phase2(u32 phase1_result)
>  		do_exit(SIGSYS);
>  	if (syscall_get_nr(current, regs) < 0)
>  		return -1;  /* Explicit request to skip. */
> +	if (seccomp_phase1_recheck() < 0)
> +		return -1;
>  
>  	return 0;
>  }
[...]

[-- Attachment #2: Digital signature --]
[-- Type: application/pgp-signature, Size: 819 bytes --]

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [PATCH] seccomp: plug syscall-dodging ptrace hole
  2016-05-26 21:04 [PATCH] seccomp: plug syscall-dodging ptrace hole Kees Cook
  2016-05-26 22:56 ` Jann Horn
@ 2016-05-27  2:10 ` Andy Lutomirski
  2016-05-27  2:41   ` Kees Cook
  1 sibling, 1 reply; 12+ messages in thread
From: Andy Lutomirski @ 2016-05-27  2:10 UTC (permalink / raw)
  To: Kees Cook; +Cc: Jann Horn, Stephane Graber, Will Drewry, linux-kernel

On Thu, May 26, 2016 at 2:04 PM, Kees Cook <keescook@chromium.org> wrote:
> One problem with seccomp was that ptrace could be used to change a
> syscall after seccomp filtering had completed. This was a well documented
> limitation, and it was recommended to block ptrace when defining a filter
> to avoid this problem. This can be quite a limitation for containers or
> other places where ptrace is desired even under seccomp filters.
>
> Since seccomp filtering has been split into pre-trace and trace phases
> (phase1 and phase2 respectively), it's possible to re-run phase1 seccomp
> after ptrace. This makes that change, and updates the test suite for
> both SECCOMP_RET_TRACE and PTRACE_SYSCALL manipulation.

I like fixing the hole, but I don't like this fix.

The two-phase seccomp mechanism is messy.  I wrote it because it was a
huge speedup.  Since then, I've made a ton of changes to the way that
x86 syscalls work, and there are two relevant effects: the slow path
is quite fast, and the phase-1-only path isn't really a win any more.

I suggest that we fix the by simplifying the code instead of making it
even more complicated.  Let's back out the two-phase mechanism (but
keep the ability for arch code to supply seccomp_data) and then just
reorder it so that seccomp happens after ptrace.  The result should be
considerably simpler.  (We'll still have to answer the question of
what happens when a SECCOMP_RET_TRACE event changes the syscall, but
maybe the answer is to just let it through -- after all,
SECCOMP_RET_TRACE might be a request by a tracer to do its own
internal filtering.)

--Andy

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [PATCH] seccomp: plug syscall-dodging ptrace hole
  2016-05-27  2:10 ` Andy Lutomirski
@ 2016-05-27  2:41   ` Kees Cook
  2016-05-27  4:45     ` Andy Lutomirski
  0 siblings, 1 reply; 12+ messages in thread
From: Kees Cook @ 2016-05-27  2:41 UTC (permalink / raw)
  To: Andy Lutomirski; +Cc: Jann Horn, Stephane Graber, Will Drewry, linux-kernel

On Thu, May 26, 2016 at 7:10 PM, Andy Lutomirski <luto@amacapital.net> wrote:
> On Thu, May 26, 2016 at 2:04 PM, Kees Cook <keescook@chromium.org> wrote:
>> One problem with seccomp was that ptrace could be used to change a
>> syscall after seccomp filtering had completed. This was a well documented
>> limitation, and it was recommended to block ptrace when defining a filter
>> to avoid this problem. This can be quite a limitation for containers or
>> other places where ptrace is desired even under seccomp filters.
>>
>> Since seccomp filtering has been split into pre-trace and trace phases
>> (phase1 and phase2 respectively), it's possible to re-run phase1 seccomp
>> after ptrace. This makes that change, and updates the test suite for
>> both SECCOMP_RET_TRACE and PTRACE_SYSCALL manipulation.
>
> I like fixing the hole, but I don't like this fix.
>
> The two-phase seccomp mechanism is messy.  I wrote it because it was a
> huge speedup.  Since then, I've made a ton of changes to the way that
> x86 syscalls work, and there are two relevant effects: the slow path
> is quite fast, and the phase-1-only path isn't really a win any more.
>
> I suggest that we fix the by simplifying the code instead of making it
> even more complicated.  Let's back out the two-phase mechanism (but
> keep the ability for arch code to supply seccomp_data) and then just
> reorder it so that seccomp happens after ptrace.  The result should be
> considerably simpler.  (We'll still have to answer the question of
> what happens when a SECCOMP_RET_TRACE event changes the syscall, but
> maybe the answer is to just let it through -- after all,
> SECCOMP_RET_TRACE might be a request by a tracer to do its own
> internal filtering.)

I'm really against this. I think seccomp needs to stay first, and I
like the two-phase split because it gives us a lot of flexibility on
other architectures. And we can't just let through RET_TRACE because
we'll have exactly the same problem: a process can add a RET_TRACE
filter for some syscall and then change it arbitrarily to escape the
filtering. The non-trace returns of seccomp need to be check first and
after ptrace manipulations. The patch seems like the best approach and
it covers all the corners.

-Kees

-- 
Kees Cook
Chrome OS & Brillo Security

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [PATCH] seccomp: plug syscall-dodging ptrace hole
  2016-05-27  2:41   ` Kees Cook
@ 2016-05-27  4:45     ` Andy Lutomirski
  2016-05-27 18:42       ` Kees Cook
  0 siblings, 1 reply; 12+ messages in thread
From: Andy Lutomirski @ 2016-05-27  4:45 UTC (permalink / raw)
  To: Kees Cook; +Cc: Jann Horn, Stephane Graber, Will Drewry, linux-kernel

On Thu, May 26, 2016 at 7:41 PM, Kees Cook <keescook@chromium.org> wrote:
> On Thu, May 26, 2016 at 7:10 PM, Andy Lutomirski <luto@amacapital.net> wrote:
>> On Thu, May 26, 2016 at 2:04 PM, Kees Cook <keescook@chromium.org> wrote:
>>> One problem with seccomp was that ptrace could be used to change a
>>> syscall after seccomp filtering had completed. This was a well documented
>>> limitation, and it was recommended to block ptrace when defining a filter
>>> to avoid this problem. This can be quite a limitation for containers or
>>> other places where ptrace is desired even under seccomp filters.
>>>
>>> Since seccomp filtering has been split into pre-trace and trace phases
>>> (phase1 and phase2 respectively), it's possible to re-run phase1 seccomp
>>> after ptrace. This makes that change, and updates the test suite for
>>> both SECCOMP_RET_TRACE and PTRACE_SYSCALL manipulation.
>>
>> I like fixing the hole, but I don't like this fix.
>>
>> The two-phase seccomp mechanism is messy.  I wrote it because it was a
>> huge speedup.  Since then, I've made a ton of changes to the way that
>> x86 syscalls work, and there are two relevant effects: the slow path
>> is quite fast, and the phase-1-only path isn't really a win any more.
>>
>> I suggest that we fix the by simplifying the code instead of making it
>> even more complicated.  Let's back out the two-phase mechanism (but
>> keep the ability for arch code to supply seccomp_data) and then just
>> reorder it so that seccomp happens after ptrace.  The result should be
>> considerably simpler.  (We'll still have to answer the question of
>> what happens when a SECCOMP_RET_TRACE event changes the syscall, but
>> maybe the answer is to just let it through -- after all,
>> SECCOMP_RET_TRACE might be a request by a tracer to do its own
>> internal filtering.)
>
> I'm really against this. I think seccomp needs to stay first,

Why?  What use case is improved with it going first?

> and I
> like the two-phase split because it gives us a lot of flexibility on
> other architectures.

I thought so too when I wrote it, and I even tried a bit to evangelize
it to other arch maintainers.  So far, it's used *only* in x86, and it
would IMO be a cleanup to stop using it in x86 now.  Given my
experience cleanup up the x86 syscall path, my current advice to other
arch maintainers would be to try hard to avoid having a context in
which syscall args are known but ptrace can't be invoked (as x86 had
before Linux 4.5).

> And we can't just let through RET_TRACE because
> we'll have exactly the same problem: a process can add a RET_TRACE
> filter for some syscall and then change it arbitrarily to escape the
> filtering. The non-trace returns of seccomp need to be check first and
> after ptrace manipulations. The patch seems like the best approach and
> it covers all the corners.

But RET_TRACE really is special.

Suppose you have a tracer and you use SECCOMP_RET_TRACE.  If the
tracer sees a syscall, approves, and calls PTRACE_CONT, then the
syscall will be allowed, whereas the effect of SECCOMP_RET_TRACE run
anew would be to either force -ENOSYS or to trap back to the tracer,
depending on whether there is a tracer.  Your patch has a
SECCOMP_RET_TRACE special case, whereas my approach wouldn't need a
special case.

I think your patch also has a minor hole: if you have
SECCOMP_RET_TRACE *and* a tracer that's catching syscalls directly
(PTRACE_SYSCALL), then the PTRACE_SYSCALL action can modify a syscall
after TRACE does its thing but before recheck, and can then redirect
to another RET_TRACE action that would otherwise be denied.  This is
minor because it could only happen if the tracer actively fights with
itself.

Finally, I think that the your approach would break an existing valid
use case.  Suppose I have a tracer that wants to intercept some
syscall sys_foo (using SECCOMP_RET_TRACE) and, when it sees a sys_foo
attempt, it will implement it by redirecting it so some other syscall
that wouldn't be allowed if called directly (i.e. it would return
SECCOMP_RET_KILL or similar).  Currently, it'll work.  With your
patch, it will kill the tracee.  I think the former behavior is
better.  On the flip side, if you write a program that uses
SECCOMP_RET_TRACE, you more or less have to trust the tracer to begin
with.

One more reason to prefer my approach: currently, if you strace a
process that gets killed by SECCOMP_RET_KILL, you can't tell what
killed it.  For example:

prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, {len = 1, filter =
0x7ffe7b2b7d30}) = 0
+++ killed by SIGSYS +++
Bad system call (core dumped)

With my approach, strace will have the IMO much more sensible behavior
of showing the fatal syscall entry before showing the "killed by
SIGSYS".

--Andy

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [PATCH] seccomp: plug syscall-dodging ptrace hole
  2016-05-27  4:45     ` Andy Lutomirski
@ 2016-05-27 18:42       ` Kees Cook
  2016-05-27 19:52         ` Andy Lutomirski
  0 siblings, 1 reply; 12+ messages in thread
From: Kees Cook @ 2016-05-27 18:42 UTC (permalink / raw)
  To: Andy Lutomirski; +Cc: Jann Horn, Stephane Graber, Will Drewry, linux-kernel

On Thu, May 26, 2016 at 9:45 PM, Andy Lutomirski <luto@amacapital.net> wrote:
> On Thu, May 26, 2016 at 7:41 PM, Kees Cook <keescook@chromium.org> wrote:
>> On Thu, May 26, 2016 at 7:10 PM, Andy Lutomirski <luto@amacapital.net> wrote:
>>> On Thu, May 26, 2016 at 2:04 PM, Kees Cook <keescook@chromium.org> wrote:
>>>> One problem with seccomp was that ptrace could be used to change a
>>>> syscall after seccomp filtering had completed. This was a well documented
>>>> limitation, and it was recommended to block ptrace when defining a filter
>>>> to avoid this problem. This can be quite a limitation for containers or
>>>> other places where ptrace is desired even under seccomp filters.
>>>>
>>>> Since seccomp filtering has been split into pre-trace and trace phases
>>>> (phase1 and phase2 respectively), it's possible to re-run phase1 seccomp
>>>> after ptrace. This makes that change, and updates the test suite for
>>>> both SECCOMP_RET_TRACE and PTRACE_SYSCALL manipulation.
>>>
>>> I like fixing the hole, but I don't like this fix.
>>>
>>> The two-phase seccomp mechanism is messy.  I wrote it because it was a
>>> huge speedup.  Since then, I've made a ton of changes to the way that
>>> x86 syscalls work, and there are two relevant effects: the slow path
>>> is quite fast, and the phase-1-only path isn't really a win any more.
>>>
>>> I suggest that we fix the by simplifying the code instead of making it
>>> even more complicated.  Let's back out the two-phase mechanism (but
>>> keep the ability for arch code to supply seccomp_data) and then just
>>> reorder it so that seccomp happens after ptrace.  The result should be
>>> considerably simpler.  (We'll still have to answer the question of
>>> what happens when a SECCOMP_RET_TRACE event changes the syscall, but
>>> maybe the answer is to just let it through -- after all,
>>> SECCOMP_RET_TRACE might be a request by a tracer to do its own
>>> internal filtering.)
>>
>> I'm really against this. I think seccomp needs to stay first,
>
> Why?  What use case is improved with it going first?

I feel that the critical purpose of seccomp is to minimize attack
surface. To that end, I am strongly against anything coming before it
in the syscall path. I really do not want ptrace going first, I think
it's just asking for bugs.

>> and I
>> like the two-phase split because it gives us a lot of flexibility on
>> other architectures.
>
> I thought so too when I wrote it, and I even tried a bit to evangelize
> it to other arch maintainers.  So far, it's used *only* in x86, and it
> would IMO be a cleanup to stop using it in x86 now.  Given my
> experience cleanup up the x86 syscall path, my current advice to other
> arch maintainers would be to try hard to avoid having a context in
> which syscall args are known but ptrace can't be invoked (as x86 had
> before Linux 4.5).

Well, I've got most of the ARM 2-phase port done, but haven't gotten
it all the way finished. But I could be talked into removing the
2-phase just from the perspective of reducing complexity.

>> And we can't just let through RET_TRACE because
>> we'll have exactly the same problem: a process can add a RET_TRACE
>> filter for some syscall and then change it arbitrarily to escape the
>> filtering. The non-trace returns of seccomp need to be check first and
>> after ptrace manipulations. The patch seems like the best approach and
>> it covers all the corners.
>
> But RET_TRACE really is special.
>
> Suppose you have a tracer and you use SECCOMP_RET_TRACE.  If the
> tracer sees a syscall, approves, and calls PTRACE_CONT, then the
> syscall will be allowed, whereas the effect of SECCOMP_RET_TRACE run
> anew would be to either force -ENOSYS or to trap back to the tracer,
> depending on whether there is a tracer.  Your patch has a
> SECCOMP_RET_TRACE special case, whereas my approach wouldn't need a
> special case.

But after the seccomp re-trap, we'd still have to re-check the
filters. So it's not cleaner, and we gain attack surface. Don't get me
wrong, I totally see why you're suggesting doing ptrace first, but I
still think that attack surface reduction must remain the primary
principle of seccomp.

> I think your patch also has a minor hole: if you have
> SECCOMP_RET_TRACE *and* a tracer that's catching syscalls directly
> (PTRACE_SYSCALL), then the PTRACE_SYSCALL action can modify a syscall
> after TRACE does its thing but before recheck, and can then redirect
> to another RET_TRACE action that would otherwise be denied.  This is
> minor because it could only happen if the tracer actively fights with
> itself.

So, a few thoughts went into the patch design, and here's why I don't
think this hole is a hole (which you already talk about a bit):
- no filtered syscall could be suddenly made to execute (this is core
goal, obviously)
- worst case, the tracer doesn't notice a syscall marked for
RET_TRACE, but that would be it's own fault because it chose to put
itself in that state.
- RET_TRACE is just under RET_ALLOW in priority, so even if there were
some really weird unintended side-effects, we still wouldn't be
allowing a syscall.

> Finally, I think that the your approach would break an existing valid
> use case.  Suppose I have a tracer that wants to intercept some
> syscall sys_foo (using SECCOMP_RET_TRACE) and, when it sees a sys_foo
> attempt, it will implement it by redirecting it so some other syscall
> that wouldn't be allowed if called directly (i.e. it would return
> SECCOMP_RET_KILL or similar).  Currently, it'll work.  With your
> patch, it will kill the tracee.  I think the former behavior is
> better.  On the flip side, if you write a program that uses
> SECCOMP_RET_TRACE, you more or less have to trust the tracer to begin
> with.

Well, you say "valid" here, but I actually think this is a
mis-feature. Luckily, nothing actually uses this behavior, and
multiple seccomp users want to see this fixed. As for tracer trust,
yes. RET_TRACE was designed for a tracer to do things on behalf of the
process, not to redirect it through an otherwise blocked syscall. The
use cases for that behavior is few and far between (and is currently
unused), where-as the use case for actually blocking the syscalls the
filters claim to block is much more desired.

> One more reason to prefer my approach: currently, if you strace a
> process that gets killed by SECCOMP_RET_KILL, you can't tell what
> killed it.  For example:
>
> prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, {len = 1, filter =
> 0x7ffe7b2b7d30}) = 0
> +++ killed by SIGSYS +++
> Bad system call (core dumped)
>
> With my approach, strace will have the IMO much more sensible behavior
> of showing the fatal syscall entry before showing the "killed by
> SIGSYS".

Right, I know, it's aesthetically much nicer that way, but I really
want to stay totally paranoid and keep seccomp absolutely first on the
path.

How about this: we'll use this patch as-is for now, since I'd like to
be able to start getting feedback from the container-using folks ASAP,
and then we can redesign the 2-phase system going forward from there.

-Kees

-- 
Kees Cook
Chrome OS & Brillo Security

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [PATCH] seccomp: plug syscall-dodging ptrace hole
  2016-05-27 18:42       ` Kees Cook
@ 2016-05-27 19:52         ` Andy Lutomirski
  2016-05-27 20:14           ` Andy Lutomirski
  2016-05-27 22:38           ` Kees Cook
  0 siblings, 2 replies; 12+ messages in thread
From: Andy Lutomirski @ 2016-05-27 19:52 UTC (permalink / raw)
  To: Kees Cook; +Cc: Stephane Graber, linux-kernel, Jann Horn, Will Drewry

On May 27, 2016 11:42 AM, "Kees Cook" <keescook@chromium.org> wrote:
>
> On Thu, May 26, 2016 at 9:45 PM, Andy Lutomirski <luto@amacapital.net> wrote:
> > On Thu, May 26, 2016 at 7:41 PM, Kees Cook <keescook@chromium.org> wrote:
> >> On Thu, May 26, 2016 at 7:10 PM, Andy Lutomirski <luto@amacapital.net> wrote:
> >>> On Thu, May 26, 2016 at 2:04 PM, Kees Cook <keescook@chromium.org> wrote:
> >>>> One problem with seccomp was that ptrace could be used to change a
> >>>> syscall after seccomp filtering had completed. This was a well documented
> >>>> limitation, and it was recommended to block ptrace when defining a filter
> >>>> to avoid this problem. This can be quite a limitation for containers or
> >>>> other places where ptrace is desired even under seccomp filters.
> >>>>
> >>>> Since seccomp filtering has been split into pre-trace and trace phases
> >>>> (phase1 and phase2 respectively), it's possible to re-run phase1 seccomp
> >>>> after ptrace. This makes that change, and updates the test suite for
> >>>> both SECCOMP_RET_TRACE and PTRACE_SYSCALL manipulation.
> >>>
> >>> I like fixing the hole, but I don't like this fix.
> >>>
> >>> The two-phase seccomp mechanism is messy.  I wrote it because it was a
> >>> huge speedup.  Since then, I've made a ton of changes to the way that
> >>> x86 syscalls work, and there are two relevant effects: the slow path
> >>> is quite fast, and the phase-1-only path isn't really a win any more.
> >>>
> >>> I suggest that we fix the by simplifying the code instead of making it
> >>> even more complicated.  Let's back out the two-phase mechanism (but
> >>> keep the ability for arch code to supply seccomp_data) and then just
> >>> reorder it so that seccomp happens after ptrace.  The result should be
> >>> considerably simpler.  (We'll still have to answer the question of
> >>> what happens when a SECCOMP_RET_TRACE event changes the syscall, but
> >>> maybe the answer is to just let it through -- after all,
> >>> SECCOMP_RET_TRACE might be a request by a tracer to do its own
> >>> internal filtering.)
> >>
> >> I'm really against this. I think seccomp needs to stay first,
> >
> > Why?  What use case is improved with it going first?
>
> I feel that the critical purpose of seccomp is to minimize attack
> surface. To that end, I am strongly against anything coming before it
> in the syscall path. I really do not want ptrace going first, I think
> it's just asking for bugs.

I disagree in this case.  There's no actual code surface opened up.
If seccomp allows even a single syscall through and there's a ptracer
attached, then the ptrace code is exposed.  As far as ptrace is
concerned, the syscall number is just a number, and ptrace has
basically no awareness of the arguments.

>
> >> and I
> >> like the two-phase split because it gives us a lot of flexibility on
> >> other architectures.
> >
> > I thought so too when I wrote it, and I even tried a bit to evangelize
> > it to other arch maintainers.  So far, it's used *only* in x86, and it
> > would IMO be a cleanup to stop using it in x86 now.  Given my
> > experience cleanup up the x86 syscall path, my current advice to other
> > arch maintainers would be to try hard to avoid having a context in
> > which syscall args are known but ptrace can't be invoked (as x86 had
> > before Linux 4.5).
>
> Well, I've got most of the ARM 2-phase port done, but haven't gotten
> it all the way finished. But I could be talked into removing the
> 2-phase just from the perspective of reducing complexity.

Does it help anything?  It's certainly more complex and harder to audit.

On x86, it used to save hundreds of cycles.  Now it's probably five
cycles or so at most.  It could even be a loss because of increased
code size.

>
> >> And we can't just let through RET_TRACE because
> >> we'll have exactly the same problem: a process can add a RET_TRACE
> >> filter for some syscall and then change it arbitrarily to escape the
> >> filtering. The non-trace returns of seccomp need to be check first and
> >> after ptrace manipulations. The patch seems like the best approach and
> >> it covers all the corners.
> >
> > But RET_TRACE really is special.
> >
> > Suppose you have a tracer and you use SECCOMP_RET_TRACE.  If the
> > tracer sees a syscall, approves, and calls PTRACE_CONT, then the
> > syscall will be allowed, whereas the effect of SECCOMP_RET_TRACE run
> > anew would be to either force -ENOSYS or to trap back to the tracer,
> > depending on whether there is a tracer.  Your patch has a
> > SECCOMP_RET_TRACE special case, whereas my approach wouldn't need a
> > special case.
>
> But after the seccomp re-trap, we'd still have to re-check the
> filters.

Why?  We don't do the now and, as far as I know, it's not a problem.

If we change that, I think it should be its own patch.

> So it's not cleaner, and we gain attack surface. Don't get me
> wrong, I totally see why you're suggesting doing ptrace first, but I
> still think that attack surface reduction must remain the primary
> principle of seccomp.
>
> > I think your patch also has a minor hole: if you have
> > SECCOMP_RET_TRACE *and* a tracer that's catching syscalls directly
> > (PTRACE_SYSCALL), then the PTRACE_SYSCALL action can modify a syscall
> > after TRACE does its thing but before recheck, and can then redirect
> > to another RET_TRACE action that would otherwise be denied.  This is
> > minor because it could only happen if the tracer actively fights with
> > itself.
>
> So, a few thoughts went into the patch design, and here's why I don't
> think this hole is a hole (which you already talk about a bit):
> - no filtered syscall could be suddenly made to execute (this is core
> goal, obviously)
> - worst case, the tracer doesn't notice a syscall marked for
> RET_TRACE, but that would be it's own fault because it chose to put
> itself in that state.
> - RET_TRACE is just under RET_ALLOW in priority, so even if there were
> some really weird unintended side-effects, we still wouldn't be
> allowing a syscall.
>
> > Finally, I think that the your approach would break an existing valid
> > use case.  Suppose I have a tracer that wants to intercept some
> > syscall sys_foo (using SECCOMP_RET_TRACE) and, when it sees a sys_foo
> > attempt, it will implement it by redirecting it so some other syscall
> > that wouldn't be allowed if called directly (i.e. it would return
> > SECCOMP_RET_KILL or similar).  Currently, it'll work.  With your
> > patch, it will kill the tracee.  I think the former behavior is
> > better.  On the flip side, if you write a program that uses
> > SECCOMP_RET_TRACE, you more or less have to trust the tracer to begin
> > with.
>
> Well, you say "valid" here, but I actually think this is a
> mis-feature. Luckily, nothing actually uses this behavior, and
> multiple seccomp users want to see this fixed. As for tracer trust,
> yes. RET_TRACE was designed for a tracer to do things on behalf of the
> process, not to redirect it through an otherwise blocked syscall. The
> use cases for that behavior is few and far between (and is currently
> unused), where-as the use case for actually blocking the syscalls the
> filters claim to block is much more desired.

Then let's make that a separate patch.

The logic could be:

if (ptraced)
  invoke ptrace;

if (seccomp) {
  evaluate the filter;
  if (result == SECCOMP_RET_TRACE) {
    evaluate filter again;
    if (result != SECCOMP_RET_TRACE)
      perform the action;
}

and we could do it in three steps:

1. Remove two-phase seccomp.

2. Move ptrace first.

3. Add the re-check on SECCOMP_RET_TRACE.

>
> > One more reason to prefer my approach: currently, if you strace a
> > process that gets killed by SECCOMP_RET_KILL, you can't tell what
> > killed it.  For example:
> >
> > prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, {len = 1, filter =
> > 0x7ffe7b2b7d30}) = 0
> > +++ killed by SIGSYS +++
> > Bad system call (core dumped)
> >
> > With my approach, strace will have the IMO much more sensible behavior
> > of showing the fatal syscall entry before showing the "killed by
> > SIGSYS".
>
> Right, I know, it's aesthetically much nicer that way, but I really
> want to stay totally paranoid and keep seccomp absolutely first on the
> path.
>
> How about this: we'll use this patch as-is for now, since I'd like to
> be able to start getting feedback from the container-using folks ASAP,
> and then we can redesign the 2-phase system going forward from there.
>

I think I'd rather change the ABI as few times as possible.  On the
other hand, it's still early, and I see nothing wrong with adding it
to -next.

--Andy

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [PATCH] seccomp: plug syscall-dodging ptrace hole
  2016-05-27 19:52         ` Andy Lutomirski
@ 2016-05-27 20:14           ` Andy Lutomirski
  2016-06-02  3:04             ` Kees Cook
  2016-05-27 22:38           ` Kees Cook
  1 sibling, 1 reply; 12+ messages in thread
From: Andy Lutomirski @ 2016-05-27 20:14 UTC (permalink / raw)
  To: Kees Cook; +Cc: Stephane Graber, linux-kernel, Jann Horn, Will Drewry

On Fri, May 27, 2016 at 12:52 PM, Andy Lutomirski <luto@amacapital.net> wrote:
>> Right, I know, it's aesthetically much nicer that way, but I really
>> want to stay totally paranoid and keep seccomp absolutely first on the
>> path.
>>
>> How about this: we'll use this patch as-is for now, since I'd like to
>> be able to start getting feedback from the container-using folks ASAP,
>> and then we can redesign the 2-phase system going forward from there.
>>
>
> I think I'd rather change the ABI as few times as possible.  On the
> other hand, it's still early, and I see nothing wrong with adding it
> to -next.

To get the ball rolling:

https://git.kernel.org/cgit/linux/kernel/git/luto/linux.git/log/?h=seccomp

It's incomplete, but it should be straightforward to finish it.  The
only interesting bit is dealing with SECCOMP_RET_TRACE.

--Andy

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [PATCH] seccomp: plug syscall-dodging ptrace hole
  2016-05-27 19:52         ` Andy Lutomirski
  2016-05-27 20:14           ` Andy Lutomirski
@ 2016-05-27 22:38           ` Kees Cook
  2016-05-27 23:20             ` Andy Lutomirski
  1 sibling, 1 reply; 12+ messages in thread
From: Kees Cook @ 2016-05-27 22:38 UTC (permalink / raw)
  To: Andy Lutomirski; +Cc: Stephane Graber, linux-kernel, Jann Horn, Will Drewry

On Fri, May 27, 2016 at 12:52 PM, Andy Lutomirski <luto@amacapital.net> wrote:
> On May 27, 2016 11:42 AM, "Kees Cook" <keescook@chromium.org> wrote:
>>
>> On Thu, May 26, 2016 at 9:45 PM, Andy Lutomirski <luto@amacapital.net> wrote:
>> > On Thu, May 26, 2016 at 7:41 PM, Kees Cook <keescook@chromium.org> wrote:
>> >> On Thu, May 26, 2016 at 7:10 PM, Andy Lutomirski <luto@amacapital.net> wrote:
>> >>> On Thu, May 26, 2016 at 2:04 PM, Kees Cook <keescook@chromium.org> wrote:
>> >>>> One problem with seccomp was that ptrace could be used to change a
>> >>>> syscall after seccomp filtering had completed. This was a well documented
>> >>>> limitation, and it was recommended to block ptrace when defining a filter
>> >>>> to avoid this problem. This can be quite a limitation for containers or
>> >>>> other places where ptrace is desired even under seccomp filters.
>> >>>>
>> >>>> Since seccomp filtering has been split into pre-trace and trace phases
>> >>>> (phase1 and phase2 respectively), it's possible to re-run phase1 seccomp
>> >>>> after ptrace. This makes that change, and updates the test suite for
>> >>>> both SECCOMP_RET_TRACE and PTRACE_SYSCALL manipulation.
>> >>>
>> >>> I like fixing the hole, but I don't like this fix.
>> >>>
>> >>> The two-phase seccomp mechanism is messy.  I wrote it because it was a
>> >>> huge speedup.  Since then, I've made a ton of changes to the way that
>> >>> x86 syscalls work, and there are two relevant effects: the slow path
>> >>> is quite fast, and the phase-1-only path isn't really a win any more.
>> >>>
>> >>> I suggest that we fix the by simplifying the code instead of making it
>> >>> even more complicated.  Let's back out the two-phase mechanism (but
>> >>> keep the ability for arch code to supply seccomp_data) and then just
>> >>> reorder it so that seccomp happens after ptrace.  The result should be
>> >>> considerably simpler.  (We'll still have to answer the question of
>> >>> what happens when a SECCOMP_RET_TRACE event changes the syscall, but
>> >>> maybe the answer is to just let it through -- after all,
>> >>> SECCOMP_RET_TRACE might be a request by a tracer to do its own
>> >>> internal filtering.)
>> >>
>> >> I'm really against this. I think seccomp needs to stay first,
>> >
>> > Why?  What use case is improved with it going first?
>>
>> I feel that the critical purpose of seccomp is to minimize attack
>> surface. To that end, I am strongly against anything coming before it
>> in the syscall path. I really do not want ptrace going first, I think
>> it's just asking for bugs.
>
> I disagree in this case.  There's no actual code surface opened up.
> If seccomp allows even a single syscall through and there's a ptracer
> attached, then the ptrace code is exposed.  As far as ptrace is
> concerned, the syscall number is just a number, and ptrace has
> basically no awareness of the arguments.

No, I completely disagree: there is a significant amount of surface
exposed. With a tracer attached there is significantly more happening
before the filter would be checked. Even less obvious things like
signal delivery, etc get exposed. Seccomp must be first -- this is
it's basic design principle. Bugs creep in, unexpected combinations
creep in, etc. Seccomp must mitigate this and be first on the syscall
path. The paranoia of this design principle must remain in place, even
at the expense of some inelegant results.

>> >> and I
>> >> like the two-phase split because it gives us a lot of flexibility on
>> >> other architectures.
>> >
>> > I thought so too when I wrote it, and I even tried a bit to evangelize
>> > it to other arch maintainers.  So far, it's used *only* in x86, and it
>> > would IMO be a cleanup to stop using it in x86 now.  Given my
>> > experience cleanup up the x86 syscall path, my current advice to other
>> > arch maintainers would be to try hard to avoid having a context in
>> > which syscall args are known but ptrace can't be invoked (as x86 had
>> > before Linux 4.5).
>>
>> Well, I've got most of the ARM 2-phase port done, but haven't gotten
>> it all the way finished. But I could be talked into removing the
>> 2-phase just from the perspective of reducing complexity.
>
> Does it help anything?  It's certainly more complex and harder to audit.
>
> On x86, it used to save hundreds of cycles.  Now it's probably five
> cycles or so at most.  It could even be a loss because of increased
> code size.

Yeah, on ARM I wasn't seeing much benefit, so I'm okay with dropping 2-phase.

>> >> And we can't just let through RET_TRACE because
>> >> we'll have exactly the same problem: a process can add a RET_TRACE
>> >> filter for some syscall and then change it arbitrarily to escape the
>> >> filtering. The non-trace returns of seccomp need to be check first and
>> >> after ptrace manipulations. The patch seems like the best approach and
>> >> it covers all the corners.
>> >
>> > But RET_TRACE really is special.
>> >
>> > Suppose you have a tracer and you use SECCOMP_RET_TRACE.  If the
>> > tracer sees a syscall, approves, and calls PTRACE_CONT, then the
>> > syscall will be allowed, whereas the effect of SECCOMP_RET_TRACE run
>> > anew would be to either force -ENOSYS or to trap back to the tracer,
>> > depending on whether there is a tracer.  Your patch has a
>> > SECCOMP_RET_TRACE special case, whereas my approach wouldn't need a
>> > special case.
>>
>> But after the seccomp re-trap, we'd still have to re-check the
>> filters.
>
> Why?  We don't do the now and, as far as I know, it's not a problem.

I feel like we're not talking about the same thing here. :) So, right
now, there are two places where we can turn over control to the
tracer: during tracerhook entry, and during phase2 RET_TRACE handling.
In both places we need to re-run phase1 to make sure any changes to
the syscall get seen by the seccomp filter.

> If we change that, I think it should be its own patch.

This was the patch. :)

>> So it's not cleaner, and we gain attack surface. Don't get me
>> wrong, I totally see why you're suggesting doing ptrace first, but I
>> still think that attack surface reduction must remain the primary
>> principle of seccomp.
>>
>> > I think your patch also has a minor hole: if you have
>> > SECCOMP_RET_TRACE *and* a tracer that's catching syscalls directly
>> > (PTRACE_SYSCALL), then the PTRACE_SYSCALL action can modify a syscall
>> > after TRACE does its thing but before recheck, and can then redirect
>> > to another RET_TRACE action that would otherwise be denied.  This is
>> > minor because it could only happen if the tracer actively fights with
>> > itself.
>>
>> So, a few thoughts went into the patch design, and here's why I don't
>> think this hole is a hole (which you already talk about a bit):
>> - no filtered syscall could be suddenly made to execute (this is core
>> goal, obviously)
>> - worst case, the tracer doesn't notice a syscall marked for
>> RET_TRACE, but that would be it's own fault because it chose to put
>> itself in that state.
>> - RET_TRACE is just under RET_ALLOW in priority, so even if there were
>> some really weird unintended side-effects, we still wouldn't be
>> allowing a syscall.
>>
>> > Finally, I think that the your approach would break an existing valid
>> > use case.  Suppose I have a tracer that wants to intercept some
>> > syscall sys_foo (using SECCOMP_RET_TRACE) and, when it sees a sys_foo
>> > attempt, it will implement it by redirecting it so some other syscall
>> > that wouldn't be allowed if called directly (i.e. it would return
>> > SECCOMP_RET_KILL or similar).  Currently, it'll work.  With your
>> > patch, it will kill the tracee.  I think the former behavior is
>> > better.  On the flip side, if you write a program that uses
>> > SECCOMP_RET_TRACE, you more or less have to trust the tracer to begin
>> > with.
>>
>> Well, you say "valid" here, but I actually think this is a
>> mis-feature. Luckily, nothing actually uses this behavior, and
>> multiple seccomp users want to see this fixed. As for tracer trust,
>> yes. RET_TRACE was designed for a tracer to do things on behalf of the
>> process, not to redirect it through an otherwise blocked syscall. The
>> use cases for that behavior is few and far between (and is currently
>> unused), where-as the use case for actually blocking the syscalls the
>> filters claim to block is much more desired.
>
> Then let's make that a separate patch.
>
> The logic could be:
>
> if (ptraced)
>   invoke ptrace;
>
> if (seccomp) {
>   evaluate the filter;
>   if (result == SECCOMP_RET_TRACE) {
>     evaluate filter again;
>     if (result != SECCOMP_RET_TRACE)
>       perform the action;
> }

We must run seccomp first. So:

if (seccomp) {
  evaluate the filter;
  if (result == SECCOMP_RET_TRACE) {
      invoke ptrace;
      evaluate filter again;
  }
}

if (ptraced) {
  invoke ptrace;
  evaluate filter again;
}

Which is what I've already sent. If we want to avoid multiple
re-evaluations in the worst-case tracer state, we could delay
re-evaluation until after the ptrace invocation, but I don't think
that's worth the design complication (since it requires per-arch
changes). Doing it after RET_TRACE and after ptrace gets done in
common code, and would be hit so rarely (a tracer with both RET_TRACE
and PTRACE_SYSCALL?!) as to be irrelevant.

> and we could do it in three steps:
>
> 1. Remove two-phase seccomp.
>
> 2. Move ptrace first.
>
> 3. Add the re-check on SECCOMP_RET_TRACE.
>
>>
>> > One more reason to prefer my approach: currently, if you strace a
>> > process that gets killed by SECCOMP_RET_KILL, you can't tell what
>> > killed it.  For example:
>> >
>> > prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, {len = 1, filter =
>> > 0x7ffe7b2b7d30}) = 0
>> > +++ killed by SIGSYS +++
>> > Bad system call (core dumped)
>> >
>> > With my approach, strace will have the IMO much more sensible behavior
>> > of showing the fatal syscall entry before showing the "killed by
>> > SIGSYS".
>>
>> Right, I know, it's aesthetically much nicer that way, but I really
>> want to stay totally paranoid and keep seccomp absolutely first on the
>> path.
>>
>> How about this: we'll use this patch as-is for now, since I'd like to
>> be able to start getting feedback from the container-using folks ASAP,
>> and then we can redesign the 2-phase system going forward from there.
>>
>
> I think I'd rather change the ABI as few times as possible.  On the
> other hand, it's still early, and I see nothing wrong with adding it
> to -next.

I much prefer to use what I sent originally unless it isn't actually
doing what it intends to be doing (i.e. there is still a hole I don't
see, etc).

-Kees

-- 
Kees Cook
Chrome OS & Brillo Security

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [PATCH] seccomp: plug syscall-dodging ptrace hole
  2016-05-27 22:38           ` Kees Cook
@ 2016-05-27 23:20             ` Andy Lutomirski
  2016-05-28  2:35               ` Kees Cook
  0 siblings, 1 reply; 12+ messages in thread
From: Andy Lutomirski @ 2016-05-27 23:20 UTC (permalink / raw)
  To: Kees Cook; +Cc: Stephane Graber, linux-kernel, Jann Horn, Will Drewry

On May 27, 2016 3:38 PM, "Kees Cook" <keescook@chromium.org> wrote:
>
> On Fri, May 27, 2016 at 12:52 PM, Andy Lutomirski <luto@amacapital.net> wrote:
> > On May 27, 2016 11:42 AM, "Kees Cook" <keescook@chromium.org> wrote:
> >>
> >> On Thu, May 26, 2016 at 9:45 PM, Andy Lutomirski <luto@amacapital.net> wrote:
> >> > On Thu, May 26, 2016 at 7:41 PM, Kees Cook <keescook@chromium.org> wrote:
> >> >> On Thu, May 26, 2016 at 7:10 PM, Andy Lutomirski <luto@amacapital.net> wrote:
> >> >>> On Thu, May 26, 2016 at 2:04 PM, Kees Cook <keescook@chromium.org> wrote:
> >> >>>> One problem with seccomp was that ptrace could be used to change a
> >> >>>> syscall after seccomp filtering had completed. This was a well documented
> >> >>>> limitation, and it was recommended to block ptrace when defining a filter
> >> >>>> to avoid this problem. This can be quite a limitation for containers or
> >> >>>> other places where ptrace is desired even under seccomp filters.
> >> >>>>
> >> >>>> Since seccomp filtering has been split into pre-trace and trace phases
> >> >>>> (phase1 and phase2 respectively), it's possible to re-run phase1 seccomp
> >> >>>> after ptrace. This makes that change, and updates the test suite for
> >> >>>> both SECCOMP_RET_TRACE and PTRACE_SYSCALL manipulation.
> >> >>>
> >> >>> I like fixing the hole, but I don't like this fix.
> >> >>>
> >> >>> The two-phase seccomp mechanism is messy.  I wrote it because it was a
> >> >>> huge speedup.  Since then, I've made a ton of changes to the way that
> >> >>> x86 syscalls work, and there are two relevant effects: the slow path
> >> >>> is quite fast, and the phase-1-only path isn't really a win any more.
> >> >>>
> >> >>> I suggest that we fix the by simplifying the code instead of making it
> >> >>> even more complicated.  Let's back out the two-phase mechanism (but
> >> >>> keep the ability for arch code to supply seccomp_data) and then just
> >> >>> reorder it so that seccomp happens after ptrace.  The result should be
> >> >>> considerably simpler.  (We'll still have to answer the question of
> >> >>> what happens when a SECCOMP_RET_TRACE event changes the syscall, but
> >> >>> maybe the answer is to just let it through -- after all,
> >> >>> SECCOMP_RET_TRACE might be a request by a tracer to do its own
> >> >>> internal filtering.)
> >> >>
> >> >> I'm really against this. I think seccomp needs to stay first,
> >> >
> >> > Why?  What use case is improved with it going first?
> >>
> >> I feel that the critical purpose of seccomp is to minimize attack
> >> surface. To that end, I am strongly against anything coming before it
> >> in the syscall path. I really do not want ptrace going first, I think
> >> it's just asking for bugs.
> >
> > I disagree in this case.  There's no actual code surface opened up.
> > If seccomp allows even a single syscall through and there's a ptracer
> > attached, then the ptrace code is exposed.  As far as ptrace is
> > concerned, the syscall number is just a number, and ptrace has
> > basically no awareness of the arguments.
>
> No, I completely disagree: there is a significant amount of surface
> exposed. With a tracer attached there is significantly more happening
> before the filter would be checked. Even less obvious things like
> signal delivery, etc get exposed. Seccomp must be first -- this is
> it's basic design principle. Bugs creep in, unexpected combinations
> creep in, etc. Seccomp must mitigate this and be first on the syscall
> path. The paranoia of this design principle must remain in place, even
> at the expense of some inelegant results.

But this only works if the filter is literally "deny everything".  If
there is even a single syscall allowed and a ptracer is attached, then
the whole ptrace machinery is exposed anyway.

Users who are this paranoid about attack surface need to disable
ptrace, full stop.  If you can do the ptrace(2) syscall, then you can
invoke all the nasty code paths by yourself, and there is nothing
seccomp can do about it.  All seccomp can do is prevent ptrace from
generating a syscall that would otherwise be filtered out.

Let's look at the actual supposed attack surface:


    if (unlikely(work & _TIF_SYSCALL_EMU))
        ret = -1L;

    if ((ret || test_thread_flag(TIF_SYSCALL_TRACE)) &&
        tracehook_report_syscall_entry(regs))
        ret = -1L;

That's all.

The only way that TIF_SYSCALL_EMU or TIF_SYSCALL_TRACE gets set is if
a ptracer is attached and uses PTRACE_SYSEMU, PTRACE_SYSCALL or
similar.  If that has happened, then there's very, very little that
seccomp can possibly do to reduce attack surface.  First, literally
any syscall that results in SECCOMP_RET_OK will cause all of the same
kernel code paths to run.  Second, most of those code paths can be
triggered without any syscall at all by using PTRACE_SINGLESTEP
instead.

So I challenge you to find a realistic scenario (i.e. something that a
real program might actually program into seccomp) in which running
seccomp before ptrace avoids even a single line of code worth of
attack surface.

On the flip side, changing the order has lots of benefits:

1. strace can actually be used to figure out why your program is
getting killed by seccomp.  (IIRC there was a long article about
trying to debug something in the chromium graphics sandbox that was
resulting in a seccomp kill.  strace would have make it easier if it
worked.)

2. The infamous ptrace hold gets plugged with much less code (and, if
we want to plug it for SECCOMP_RET_TRACE, that's still less code than
with your patch).  I would argue that reducing the amount of code in
seccomp is more important from an attack surface perspective than
restricting the values that a bunch of integers that the kernel's
ptrace code doesn't even care about.

3. IMO it makes more sense.  A debugger is trying to debug.  I think
it makes more sense for it to see syscalls as requested by the issuing
process rather than syscalls as modified by seccomp.


> >
> > The logic could be:
> >
> > if (ptraced)
> >   invoke ptrace;
> >
> > if (seccomp) {
> >   evaluate the filter;
> >   if (result == SECCOMP_RET_TRACE) {
> >     evaluate filter again;
> >     if (result != SECCOMP_RET_TRACE)
> >       perform the action;
> > }
>
> We must run seccomp first. So:
>
> if (seccomp) {
>   evaluate the filter;
>   if (result == SECCOMP_RET_TRACE) {
>       invoke ptrace;
>       evaluate filter again;
>   }
> }
>
> if (ptraced) {
>   invoke ptrace;
>   evaluate filter again;
> }

This is even more code, and I still disagree with your "must".

--Andy

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [PATCH] seccomp: plug syscall-dodging ptrace hole
  2016-05-27 23:20             ` Andy Lutomirski
@ 2016-05-28  2:35               ` Kees Cook
  0 siblings, 0 replies; 12+ messages in thread
From: Kees Cook @ 2016-05-28  2:35 UTC (permalink / raw)
  To: Andy Lutomirski; +Cc: Stephane Graber, linux-kernel, Jann Horn, Will Drewry

On Fri, May 27, 2016 at 4:20 PM, Andy Lutomirski <luto@amacapital.net> wrote:
> On May 27, 2016 3:38 PM, "Kees Cook" <keescook@chromium.org> wrote:
>>
>> On Fri, May 27, 2016 at 12:52 PM, Andy Lutomirski <luto@amacapital.net> wrote:
>> > On May 27, 2016 11:42 AM, "Kees Cook" <keescook@chromium.org> wrote:
>> >>
>> >> On Thu, May 26, 2016 at 9:45 PM, Andy Lutomirski <luto@amacapital.net> wrote:
>> >> > On Thu, May 26, 2016 at 7:41 PM, Kees Cook <keescook@chromium.org> wrote:
>> >> >> On Thu, May 26, 2016 at 7:10 PM, Andy Lutomirski <luto@amacapital.net> wrote:
>> >> >>> On Thu, May 26, 2016 at 2:04 PM, Kees Cook <keescook@chromium.org> wrote:
>> >> >>>> One problem with seccomp was that ptrace could be used to change a
>> >> >>>> syscall after seccomp filtering had completed. This was a well documented
>> >> >>>> limitation, and it was recommended to block ptrace when defining a filter
>> >> >>>> to avoid this problem. This can be quite a limitation for containers or
>> >> >>>> other places where ptrace is desired even under seccomp filters.
>> >> >>>>
>> >> >>>> Since seccomp filtering has been split into pre-trace and trace phases
>> >> >>>> (phase1 and phase2 respectively), it's possible to re-run phase1 seccomp
>> >> >>>> after ptrace. This makes that change, and updates the test suite for
>> >> >>>> both SECCOMP_RET_TRACE and PTRACE_SYSCALL manipulation.
>> >> >>>
>> >> >>> I like fixing the hole, but I don't like this fix.
>> >> >>>
>> >> >>> The two-phase seccomp mechanism is messy.  I wrote it because it was a
>> >> >>> huge speedup.  Since then, I've made a ton of changes to the way that
>> >> >>> x86 syscalls work, and there are two relevant effects: the slow path
>> >> >>> is quite fast, and the phase-1-only path isn't really a win any more.
>> >> >>>
>> >> >>> I suggest that we fix the by simplifying the code instead of making it
>> >> >>> even more complicated.  Let's back out the two-phase mechanism (but
>> >> >>> keep the ability for arch code to supply seccomp_data) and then just
>> >> >>> reorder it so that seccomp happens after ptrace.  The result should be
>> >> >>> considerably simpler.  (We'll still have to answer the question of
>> >> >>> what happens when a SECCOMP_RET_TRACE event changes the syscall, but
>> >> >>> maybe the answer is to just let it through -- after all,
>> >> >>> SECCOMP_RET_TRACE might be a request by a tracer to do its own
>> >> >>> internal filtering.)
>> >> >>
>> >> >> I'm really against this. I think seccomp needs to stay first,
>> >> >
>> >> > Why?  What use case is improved with it going first?
>> >>
>> >> I feel that the critical purpose of seccomp is to minimize attack
>> >> surface. To that end, I am strongly against anything coming before it
>> >> in the syscall path. I really do not want ptrace going first, I think
>> >> it's just asking for bugs.
>> >
>> > I disagree in this case.  There's no actual code surface opened up.
>> > If seccomp allows even a single syscall through and there's a ptracer
>> > attached, then the ptrace code is exposed.  As far as ptrace is
>> > concerned, the syscall number is just a number, and ptrace has
>> > basically no awareness of the arguments.
>>
>> No, I completely disagree: there is a significant amount of surface
>> exposed. With a tracer attached there is significantly more happening
>> before the filter would be checked. Even less obvious things like
>> signal delivery, etc get exposed. Seccomp must be first -- this is
>> it's basic design principle. Bugs creep in, unexpected combinations
>> creep in, etc. Seccomp must mitigate this and be first on the syscall
>> path. The paranoia of this design principle must remain in place, even
>> at the expense of some inelegant results.
>
> But this only works if the filter is literally "deny everything".  If
> there is even a single syscall allowed and a ptracer is attached, then
> the whole ptrace machinery is exposed anyway.

That's an excellent point. Thanks for persisting on this, I'm starting
to come around. :)

> Users who are this paranoid about attack surface need to disable
> ptrace, full stop.  If you can do the ptrace(2) syscall, then you can
> invoke all the nasty code paths by yourself, and there is nothing
> seccomp can do about it.  All seccomp can do is prevent ptrace from
> generating a syscall that would otherwise be filtered out.
>
> Let's look at the actual supposed attack surface:
>
>     if (unlikely(work & _TIF_SYSCALL_EMU))
>         ret = -1L;
>
>     if ((ret || test_thread_flag(TIF_SYSCALL_TRACE)) &&
>         tracehook_report_syscall_entry(regs))
>         ret = -1L;
>
> That's all.

Yeah, looking through this, I see that audit isn't part of this, so
I'm much more relaxed.

> The only way that TIF_SYSCALL_EMU or TIF_SYSCALL_TRACE gets set is if
> a ptracer is attached and uses PTRACE_SYSEMU, PTRACE_SYSCALL or
> similar.  If that has happened, then there's very, very little that
> seccomp can possibly do to reduce attack surface.  First, literally
> any syscall that results in SECCOMP_RET_OK will cause all of the same
> kernel code paths to run.  Second, most of those code paths can be
> triggered without any syscall at all by using PTRACE_SINGLESTEP
> instead.

Agreed: the attack surface on ptrace is unavoidable under normal
conditions. All the more reason to set Yama's sysctl to 2! ;)

> So I challenge you to find a realistic scenario (i.e. something that a
> real program might actually program into seccomp) in which running
> seccomp before ptrace avoids even a single line of code worth of
> attack surface.
>
> On the flip side, changing the order has lots of benefits:
>
> 1. strace can actually be used to figure out why your program is
> getting killed by seccomp.  (IIRC there was a long article about
> trying to debug something in the chromium graphics sandbox that was
> resulting in a seccomp kill.  strace would have make it easier if it
> worked.)
>
> 2. The infamous ptrace hold gets plugged with much less code (and, if
> we want to plug it for SECCOMP_RET_TRACE, that's still less code than
> with your patch).  I would argue that reducing the amount of code in
> seccomp is more important from an attack surface perspective than
> restricting the values that a bunch of integers that the kernel's
> ptrace code doesn't even care about.
>
> 3. IMO it makes more sense.  A debugger is trying to debug.  I think
> it makes more sense for it to see syscalls as requested by the issuing
> process rather than syscalls as modified by seccomp.

Yeah, it's much much cleaner and has more expected results when
reordered. The down side is that this needs to be changed on a
per-architecture basis. (But that might be a good time to pre-populate
struct seccomp_data.)

Hmmm. Reordering may actually let this not be arch-specific any more,
but I dunno... I'll look through it...

-Kees

-- 
Kees Cook
Chrome OS & Brillo Security

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [PATCH] seccomp: plug syscall-dodging ptrace hole
  2016-05-27 20:14           ` Andy Lutomirski
@ 2016-06-02  3:04             ` Kees Cook
  0 siblings, 0 replies; 12+ messages in thread
From: Kees Cook @ 2016-06-02  3:04 UTC (permalink / raw)
  To: Andy Lutomirski; +Cc: Stephane Graber, linux-kernel, Jann Horn, Will Drewry

On Fri, May 27, 2016 at 1:14 PM, Andy Lutomirski <luto@amacapital.net> wrote:
> On Fri, May 27, 2016 at 12:52 PM, Andy Lutomirski <luto@amacapital.net> wrote:
>>> Right, I know, it's aesthetically much nicer that way, but I really
>>> want to stay totally paranoid and keep seccomp absolutely first on the
>>> path.
>>>
>>> How about this: we'll use this patch as-is for now, since I'd like to
>>> be able to start getting feedback from the container-using folks ASAP,
>>> and then we can redesign the 2-phase system going forward from there.
>>>
>>
>> I think I'd rather change the ABI as few times as possible.  On the
>> other hand, it's still early, and I see nothing wrong with adding it
>> to -next.
>
> To get the ball rolling:
>
> https://git.kernel.org/cgit/linux/kernel/git/luto/linux.git/log/?h=seccomp
>
> It's incomplete, but it should be straightforward to finish it.  The
> only interesting bit is dealing with SECCOMP_RET_TRACE.

I did a bit more from there (though it needs further cleanup, I see my
"const" fixes landed in the wrong patch), this passes my tests on x86,
the other architectures need reordering and testing:

http://git.kernel.org/cgit/linux/kernel/git/kees/linux.git/log/?h=seccomp/reorder-ptrace

-Kees

-- 
Kees Cook
Chrome OS & Brillo Security

^ permalink raw reply	[flat|nested] 12+ messages in thread

end of thread, other threads:[~2016-06-02  3:04 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-05-26 21:04 [PATCH] seccomp: plug syscall-dodging ptrace hole Kees Cook
2016-05-26 22:56 ` Jann Horn
2016-05-27  2:10 ` Andy Lutomirski
2016-05-27  2:41   ` Kees Cook
2016-05-27  4:45     ` Andy Lutomirski
2016-05-27 18:42       ` Kees Cook
2016-05-27 19:52         ` Andy Lutomirski
2016-05-27 20:14           ` Andy Lutomirski
2016-06-02  3:04             ` Kees Cook
2016-05-27 22:38           ` Kees Cook
2016-05-27 23:20             ` Andy Lutomirski
2016-05-28  2:35               ` Kees Cook

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).