linux-arch.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Kees Cook <keescook@chromium.org>
To: linux-kernel@vger.kernel.org
Cc: Kees Cook <keescook@chromium.org>,
	Andy Lutomirski <luto@amacapital.net>,
	Alexei Starovoitov <ast@plumgrid.com>,
	"Michael Kerrisk (man-pages)" <mtk.manpages@gmail.com>,
	Andrew Morton <akpm@linux-foundation.org>,
	Daniel Borkmann <dborkman@redhat.com>,
	Oleg Nesterov <oleg@redhat.com>, Will Drewry <wad@chromium.org>,
	Julien Tinnes <jln@chromium.org>,
	David Drysdale <drysdale@google.com>,
	linux-api@vger.kernel.org, x86@kernel.org,
	linux-arm-kernel@lists.infradead.org, linux-mips@linux-mips.org,
	linux-arch@vger.kernel.org,
	linux-security-module@vger.kernel.org
Subject: [PATCH v7 5/9] seccomp: split mode set routines
Date: Mon, 23 Jun 2014 14:58:09 -0700	[thread overview]
Message-ID: <1403560693-21809-6-git-send-email-keescook@chromium.org> (raw)
In-Reply-To: <1403560693-21809-1-git-send-email-keescook@chromium.org>

Extracts the common check/assign logic, and separates the two mode
setting paths to make things more readable with fewer #ifdefs within
function bodies.

Signed-off-by: Kees Cook <keescook@chromium.org>
---
 kernel/seccomp.c |  124 +++++++++++++++++++++++++++++++++++++-----------------
 1 file changed, 85 insertions(+), 39 deletions(-)

diff --git a/kernel/seccomp.c b/kernel/seccomp.c
index 8ab0b7116ed8..1fb162e8b032 100644
--- a/kernel/seccomp.c
+++ b/kernel/seccomp.c
@@ -193,7 +193,29 @@ static u32 seccomp_run_filters(int syscall)
 	}
 	return ret;
 }
+#endif /* CONFIG_SECCOMP_FILTER */
 
+static inline bool seccomp_check_mode(struct task_struct *task,
+				      unsigned long seccomp_mode)
+{
+	BUG_ON(!spin_is_locked(&task->sighand->siglock));
+
+	if (task->seccomp.mode && task->seccomp.mode != seccomp_mode)
+		return false;
+
+	return true;
+}
+
+static inline void seccomp_assign_mode(struct task_struct *task,
+				       unsigned long seccomp_mode)
+{
+	BUG_ON(!spin_is_locked(&task->sighand->siglock));
+
+	task->seccomp.mode = seccomp_mode;
+	set_tsk_thread_flag(task, TIF_SECCOMP);
+}
+
+#ifdef CONFIG_SECCOMP_FILTER
 /**
  * seccomp_prepare_filter: Prepares a seccomp filter for use.
  * @fprog: BPF program to install
@@ -500,69 +522,86 @@ long prctl_get_seccomp(void)
 }
 
 /**
- * seccomp_set_mode: internal function for setting seccomp mode
- * @seccomp_mode: requested mode to use
- * @filter: optional struct sock_fprog for use with SECCOMP_MODE_FILTER
+ * seccomp_set_mode_strict: internal function for setting strict seccomp
  *
- * This function may be called repeatedly with a @seccomp_mode of
- * SECCOMP_MODE_FILTER to install additional filters.  Every filter
- * successfully installed will be evaluated (in reverse order) for each system
- * call the task makes.
+ * Once current->seccomp.mode is non-zero, it may not be changed.
+ *
+ * Returns 0 on success or -EINVAL on failure.
+ */
+static long seccomp_set_mode_strict(void)
+{
+	const unsigned long seccomp_mode = SECCOMP_MODE_STRICT;
+	unsigned long irqflags;
+	int ret = -EINVAL;
+
+	if (unlikely(!lock_task_sighand(current, &irqflags)))
+		return -EINVAL;
+
+	if (!seccomp_check_mode(current, seccomp_mode))
+		goto out;
+
+#ifdef TIF_NOTSC
+	disable_TSC();
+#endif
+	seccomp_assign_mode(current, seccomp_mode);
+	ret = 0;
+
+out:
+	unlock_task_sighand(current, &irqflags);
+
+	return ret;
+}
+
+#ifdef CONFIG_SECCOMP_FILTER
+/**
+ * seccomp_set_mode_filter: internal function for setting seccomp filter
+ * @filter: struct sock_fprog containing filter
+ *
+ * This function may be called repeatedly to install additional filters.
+ * Every filter successfully installed will be evaluated (in reverse order)
+ * for each system call the task makes.
  *
  * Once current->seccomp.mode is non-zero, it may not be changed.
  *
  * Returns 0 on success or -EINVAL on failure.
  */
-static long seccomp_set_mode(unsigned long seccomp_mode, char __user *filter)
+static long seccomp_set_mode_filter(char __user *filter)
 {
+	const unsigned long seccomp_mode = SECCOMP_MODE_FILTER;
 	struct seccomp_filter *prepared = NULL;
 	unsigned long irqflags;
 	long ret = -EINVAL;
 
-#ifdef CONFIG_SECCOMP_FILTER
-	/* Prepare the new filter outside of the seccomp lock. */
-	if (seccomp_mode == SECCOMP_MODE_FILTER) {
-		prepared = seccomp_prepare_user_filter(filter);
-		if (IS_ERR(prepared))
-			return PTR_ERR(prepared);
-	}
-#endif
+	/* Prepare the new filter outside of any locking. */
+	prepared = seccomp_prepare_user_filter(filter);
+	if (IS_ERR(prepared))
+		return PTR_ERR(prepared);
 
 	if (unlikely(!lock_task_sighand(current, &irqflags)))
 		goto out_free;
 
-	if (current->seccomp.mode &&
-	    current->seccomp.mode != seccomp_mode)
+	if (!seccomp_check_mode(current, seccomp_mode))
 		goto out;
 
-	switch (seccomp_mode) {
-	case SECCOMP_MODE_STRICT:
-		ret = 0;
-#ifdef TIF_NOTSC
-		disable_TSC();
-#endif
-		break;
-#ifdef CONFIG_SECCOMP_FILTER
-	case SECCOMP_MODE_FILTER:
-		ret = seccomp_attach_filter(prepared);
-		if (ret)
-			goto out;
-		/* Do not free the successfully attached filter. */
-		prepared = NULL;
-		break;
-#endif
-	default:
+	ret = seccomp_attach_filter(prepared);
+	if (ret)
 		goto out;
-	}
+	/* Do not free the successfully attached filter. */
+	prepared = NULL;
 
-	current->seccomp.mode = seccomp_mode;
-	set_thread_flag(TIF_SECCOMP);
+	seccomp_assign_mode(current, seccomp_mode);
 out:
 	unlock_task_sighand(current, &irqflags);
 out_free:
 	seccomp_filter_free(prepared);
 	return ret;
 }
+#else
+static inline long seccomp_set_mode_filter(char __user *filter)
+{
+	return -EINVAL;
+}
+#endif
 
 /**
  * prctl_set_seccomp: configures current->seccomp.mode
@@ -573,5 +612,12 @@ out_free:
  */
 long prctl_set_seccomp(unsigned long seccomp_mode, char __user *filter)
 {
-	return seccomp_set_mode(seccomp_mode, filter);
+	switch (seccomp_mode) {
+	case SECCOMP_MODE_STRICT:
+		return seccomp_set_mode_strict();
+	case SECCOMP_MODE_FILTER:
+		return seccomp_set_mode_filter(filter);
+	default:
+		return -EINVAL;
+	}
 }
-- 
1.7.9.5

  parent reply	other threads:[~2014-06-23 21:58 UTC|newest]

Thread overview: 68+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-06-23 21:58 [PATCH v7 0/9] seccomp: add thread sync ability Kees Cook
2014-06-23 21:58 ` Kees Cook
2014-06-23 21:58 ` [PATCH v7 1/9] seccomp: create internal mode-setting function Kees Cook
2014-06-23 21:58 ` [PATCH v7 2/9] seccomp: split filter prep from check and apply Kees Cook
2014-06-23 21:58   ` Kees Cook
2014-06-26 12:37   ` David Drysdale
2014-06-27 18:45     ` Kees Cook
2014-06-27 18:45       ` Kees Cook
2014-06-23 21:58 ` [PATCH v7 3/9] seccomp: introduce writer locking Kees Cook
2014-06-23 21:58   ` Kees Cook
2014-06-24 16:52   ` Oleg Nesterov
2014-06-24 16:52     ` Oleg Nesterov
2014-06-24 18:02     ` Kees Cook
2014-06-24 18:02       ` Kees Cook
     [not found]       ` <CAGXu5j+G8qAkGD7H=3R2iw2ZTqZSrMPa2f=czoEjwSW5wKqUWQ-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2014-06-24 18:35         ` Oleg Nesterov
2014-06-24 18:35           ` Oleg Nesterov
2014-06-24 20:26           ` Kees Cook
2014-06-24 18:30   ` Oleg Nesterov
2014-06-24 18:30     ` Oleg Nesterov
     [not found]     ` <20140624183024.GA1258-H+wXaHxf7aLQT0dZR+AlfA@public.gmane.org>
2014-06-24 19:46       ` Kees Cook
2014-06-24 19:46         ` Kees Cook
2014-06-23 21:58 ` [PATCH v7 4/9] seccomp: move no_new_privs into seccomp Kees Cook
2014-06-23 21:58   ` Kees Cook
2014-06-24 19:18   ` Oleg Nesterov
2014-06-24 19:18     ` Oleg Nesterov
2014-06-24 19:20     ` Andy Lutomirski
2014-06-24 19:20       ` Andy Lutomirski
2014-06-24 19:30       ` Oleg Nesterov
2014-06-24 19:30         ` Oleg Nesterov
2014-06-24 19:34         ` Andy Lutomirski
2014-06-24 19:34           ` Andy Lutomirski
     [not found]           ` <CALCETrU9x05ADgz9JToiw_BuCPz1h0xmOh=1R3eojL9far1aEA-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2014-06-24 19:50             ` Kees Cook
2014-06-24 19:50               ` Kees Cook
     [not found]               ` <CAGXu5jJjuNmf=FRzUPMChvL4D_xkg034pUbRAbaK38f37GYC0A-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2014-06-24 19:51                 ` Andy Lutomirski
2014-06-24 19:51                   ` Andy Lutomirski
2014-06-23 21:58 ` Kees Cook [this message]
2014-06-23 21:58   ` [PATCH v7 5/9] seccomp: split mode set routines Kees Cook
2014-06-23 21:58 ` [PATCH v7 6/9] seccomp: add "seccomp" syscall Kees Cook
2014-06-23 21:58   ` Kees Cook
2014-06-23 21:58 ` [PATCH v7 7/9] seccomp: implement SECCOMP_FILTER_FLAG_TSYNC Kees Cook
2014-06-23 21:58   ` Kees Cook
2014-06-24 17:08   ` Oleg Nesterov
2014-06-24 17:08     ` Oleg Nesterov
     [not found]     ` <20140624170800.GA30480-H+wXaHxf7aLQT0dZR+AlfA@public.gmane.org>
2014-06-24 18:19       ` Kees Cook
2014-06-24 18:19         ` Kees Cook
2014-06-24 17:27   ` Oleg Nesterov
2014-06-24 17:27     ` Oleg Nesterov
2014-06-24 18:05     ` Kees Cook
2014-06-24 18:37       ` Oleg Nesterov
2014-06-24 18:37         ` Oleg Nesterov
2014-06-24 19:08         ` Kees Cook
2014-06-24 19:08           ` Kees Cook
     [not found] ` <1403560693-21809-1-git-send-email-keescook-F7+t8E8rja9g9hUCZPvPmw@public.gmane.org>
2014-06-23 21:58   ` [PATCH v7 8/9] ARM: add seccomp syscall Kees Cook
2014-06-23 21:58     ` Kees Cook
2014-06-23 21:58 ` [PATCH v7 9/9] MIPS: " Kees Cook
2014-06-23 21:58   ` Kees Cook
2014-06-23 22:01 ` [PATCH v7 1/1] man-pages: seccomp.2: document syscall Kees Cook
2014-06-23 22:01   ` Kees Cook
     [not found]   ` <20140623220150.GM5412-oSa+0FWJbaXR7s880joybQ@public.gmane.org>
2014-06-24 10:23     ` Michael Kerrisk (man-pages)
2014-06-24 10:23       ` Michael Kerrisk (man-pages)
2014-06-24 16:43       ` Kees Cook
2014-06-24 16:43         ` Kees Cook
2014-06-24 17:48   ` [PATCH v7.1 " Kees Cook
2014-06-24 17:48     ` Kees Cook
2014-06-24 18:06   ` [PATCH v7 " Andy Lutomirski
2014-06-24 18:06     ` Andy Lutomirski
     [not found]     ` <CALCETrV=nAuWi8_Xj6KnJ6P1Yiaw36+n50-gHKaTgea4yH85Eg-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2014-06-24 19:18       ` Kees Cook
2014-06-24 19:18         ` Kees Cook

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1403560693-21809-6-git-send-email-keescook@chromium.org \
    --to=keescook@chromium.org \
    --cc=akpm@linux-foundation.org \
    --cc=ast@plumgrid.com \
    --cc=dborkman@redhat.com \
    --cc=drysdale@google.com \
    --cc=jln@chromium.org \
    --cc=linux-api@vger.kernel.org \
    --cc=linux-arch@vger.kernel.org \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mips@linux-mips.org \
    --cc=linux-security-module@vger.kernel.org \
    --cc=luto@amacapital.net \
    --cc=mtk.manpages@gmail.com \
    --cc=oleg@redhat.com \
    --cc=wad@chromium.org \
    --cc=x86@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).