linux-um.lists.infradead.org archive mirror
 help / color / mirror / Atom feed
From: benjamin@sipsolutions.net
To: linux-um@lists.infradead.org
Cc: Benjamin Berg <benjamin@sipsolutions.net>,
	Johannes Berg <johannes@sipsolutions.net>
Subject: [PATCH v2 23/28] um: Add stub side of SECCOMP/futex based process handling
Date: Tue, 22 Nov 2022 11:07:54 +0100	[thread overview]
Message-ID: <20221122100759.208290-24-benjamin@sipsolutions.net> (raw)
In-Reply-To: <20221122100759.208290-1-benjamin@sipsolutions.net>

From: Benjamin Berg <benjamin@sipsolutions.net>

This adds the stub side for the new seccomp process management code. In
this case we do register save/restore through the signal handler
mcontext. For the FS_BASE/GS_BASE register we need special handling.

Co-authored-by: Johannes Berg <johannes@sipsolutions.net>
Signed-off-by: Benjamin Berg <benjamin@sipsolutions.net>
---
 arch/um/include/shared/skas/stub-data.h | 15 +++++++
 arch/um/kernel/skas/clone.c             | 24 +++++++++++
 arch/um/kernel/skas/stub.c              | 54 +++++++++++++++++++++++++
 arch/x86/um/shared/sysdep/stub-data.h   | 12 ++++++
 arch/x86/um/shared/sysdep/stub.h        |  3 ++
 arch/x86/um/shared/sysdep/stub_32.h     |  7 ++++
 arch/x86/um/shared/sysdep/stub_64.h     | 14 +++++++
 7 files changed, 129 insertions(+)
 create mode 100644 arch/x86/um/shared/sysdep/stub-data.h

diff --git a/arch/um/include/shared/skas/stub-data.h b/arch/um/include/shared/skas/stub-data.h
index 821c1e98c051..6a6bc34f70c4 100644
--- a/arch/um/include/shared/skas/stub-data.h
+++ b/arch/um/include/shared/skas/stub-data.h
@@ -8,8 +8,13 @@
 #ifndef __STUB_DATA_H
 #define __STUB_DATA_H
 
+#include <linux/kconfig.h>
 #include <linux/compiler_types.h>
 #include <as-layout.h>
+#include <sysdep/stub-data.h>
+
+#define FUTEX_IN_CHILD 0
+#define FUTEX_IN_KERN 1
 
 #define STUB_NEXT_SYSCALL(s) \
 	((struct stub_syscall *) (((unsigned long) s) + (s)->cmd_len))
@@ -31,6 +36,16 @@ struct stub_data {
 	/* 128 leaves enough room for additional fields in the struct */
 	unsigned char syscall_data[UM_KERN_PAGE_SIZE - 128] __aligned(16);
 
+	/* data shared with signal handler (only used in seccomp mode) */
+	short restart_wait;
+	unsigned int futex;
+	int signal;
+	unsigned short si_offset;
+	unsigned short mctx_offset;
+
+	/* seccomp architecture specific state restore */
+	struct stub_data_arch arch_data;
+
 	/* Stack for our signal handlers and for calling into . */
 	unsigned char sigstack[UM_KERN_PAGE_SIZE] __aligned(UM_KERN_PAGE_SIZE);
 };
diff --git a/arch/um/kernel/skas/clone.c b/arch/um/kernel/skas/clone.c
index 8b6ea9c00133..97728b7dd54d 100644
--- a/arch/um/kernel/skas/clone.c
+++ b/arch/um/kernel/skas/clone.c
@@ -48,3 +48,27 @@ stub_clone_handler(void)
  done:
 	trap_myself();
 }
+
+#ifdef CONFIG_UML_SECCOMP
+void __attribute__ ((__section__ (".__syscall_stub")))
+stub_clone_handler_seccomp(void)
+{
+	struct stub_data *data = get_stub_page() + UM_KERN_PAGE_SIZE;
+	long err;
+
+	/* Use the syscall data as a temporary stack area (bottom half for clone). */
+	err = stub_syscall2(__NR_clone, CLONE_PARENT | CLONE_FILES | SIGCHLD,
+			    (unsigned long) data->syscall_data +
+					    sizeof(data->syscall_data) / 2 -
+					    sizeof(void *));
+	if (err) {
+		data->err = err;
+		goto done;
+	}
+
+	remap_stack_and_trap();
+
+ done:
+	trap_myself();
+}
+#endif
diff --git a/arch/um/kernel/skas/stub.c b/arch/um/kernel/skas/stub.c
index 0a13f5d21d08..4bb90395a12b 100644
--- a/arch/um/kernel/skas/stub.c
+++ b/arch/um/kernel/skas/stub.c
@@ -5,6 +5,11 @@
 
 #include <sysdep/stub.h>
 
+#ifdef CONFIG_UML_SECCOMP
+#include <linux/futex.h>
+#include <errno.h>
+#endif
+
 static __always_inline int syscall_handler(struct stub_data *d)
 {
 	struct stub_syscall *sc;
@@ -45,3 +50,52 @@ stub_syscall_handler(void)
 
 	trap_myself();
 }
+
+#ifdef CONFIG_UML_SECCOMP
+void __attribute__ ((__section__ (".__syscall_stub")))
+stub_signal_interrupt(int sig, siginfo_t *info, void *p)
+{
+	struct stub_data *d = get_stub_page();
+	ucontext_t *uc = p;
+	long res;
+
+	d->signal = sig;
+	d->si_offset = (unsigned long)info - (unsigned long)&d->sigstack[0];
+	d->mctx_offset = (unsigned long)&uc->uc_mcontext - (unsigned long)&d->sigstack[0];
+
+restart_wait:
+	d->futex = FUTEX_IN_KERN;
+	do {
+		res = stub_syscall3(__NR_futex, (unsigned long)&d->futex,
+				    FUTEX_WAKE, 1);
+	} while (res == -EINTR);
+	do {
+		res = stub_syscall4(__NR_futex, (unsigned long)&d->futex,
+				    FUTEX_WAIT, FUTEX_IN_KERN, 0);
+	} while (res == -EINTR || d->futex == FUTEX_IN_KERN);
+
+	if (res < 0 && res != -EAGAIN)
+		stub_syscall2(__NR_kill, 0, SIGKILL);
+
+	/* Try running queued syscalls. */
+	if (syscall_handler(d) < 0 || d->restart_wait) {
+		/* Report SIGTRAP if we restart. */
+		d->signal = SIGTRAP;
+		d->restart_wait = 0;
+		goto restart_wait;
+	}
+
+	/* Return so that the host modified mcontext is restored. */
+}
+
+void __attribute__ ((__section__ (".__syscall_stub")))
+stub_signal_restorer(void)
+{
+	struct stub_data *d = get_stub_page();
+
+	/* Restore arch dependent state that is not part of the mcontext */
+	stub_seccomp_restore_state(&d->arch_data);
+
+	stub_syscall0(__NR_rt_sigreturn);
+}
+#endif
diff --git a/arch/x86/um/shared/sysdep/stub-data.h b/arch/x86/um/shared/sysdep/stub-data.h
new file mode 100644
index 000000000000..b0b6f2a95f0e
--- /dev/null
+++ b/arch/x86/um/shared/sysdep/stub-data.h
@@ -0,0 +1,12 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifdef __i386__
+struct stub_data_arch { };
+#else
+#define STUB_SYNC_FS_BASE (1 << 0)
+#define STUB_SYNC_GS_BASE (1 << 1)
+struct stub_data_arch {
+	int sync;
+	unsigned long fs_base;
+	unsigned long gs_base;
+};
+#endif
diff --git a/arch/x86/um/shared/sysdep/stub.h b/arch/x86/um/shared/sysdep/stub.h
index 579681d12158..eb2e3a24d40b 100644
--- a/arch/x86/um/shared/sysdep/stub.h
+++ b/arch/x86/um/shared/sysdep/stub.h
@@ -14,3 +14,6 @@
 extern void stub_segv_handler(int, siginfo_t *, void *);
 extern void stub_syscall_handler(void);
 extern void stub_clone_handler(void);
+extern void stub_signal_interrupt(int, siginfo_t *, void *);
+extern void stub_signal_restorer(void);
+extern void stub_clone_handler_seccomp(void);
diff --git a/arch/x86/um/shared/sysdep/stub_32.h b/arch/x86/um/shared/sysdep/stub_32.h
index 9531e52b0a3e..c8328a1949ee 100644
--- a/arch/x86/um/shared/sysdep/stub_32.h
+++ b/arch/x86/um/shared/sysdep/stub_32.h
@@ -144,4 +144,11 @@ static __always_inline void *get_stub_page(void)
 
 	return (void *)ret;
 }
+
+static __always_inline void
+stub_seccomp_restore_state(struct stub_data_arch *arch)
+{
+	/* No extra arch specific restore */
+}
+
 #endif
diff --git a/arch/x86/um/shared/sysdep/stub_64.h b/arch/x86/um/shared/sysdep/stub_64.h
index bd2f62530bcc..99c5e54654a7 100644
--- a/arch/x86/um/shared/sysdep/stub_64.h
+++ b/arch/x86/um/shared/sysdep/stub_64.h
@@ -10,6 +10,7 @@
 #include <sysdep/ptrace_user.h>
 #include <generated/asm-offsets.h>
 #include <linux/stddef.h>
+#include <asm/prctl.h>
 
 #define STUB_MMAP_NR __NR_mmap
 #define MMAP_OFFSET(o) (o)
@@ -140,4 +141,17 @@ static __always_inline void *get_stub_page(void)
 
 	return (void *)ret - UM_KERN_PAGE_SIZE;
 }
+
+static __always_inline void
+stub_seccomp_restore_state(struct stub_data_arch *arch)
+{
+	/* TODO: Use _writefsbase_u64/_writegsbase_u64 when possible */
+	if (arch->sync & STUB_SYNC_FS_BASE)
+		stub_syscall2(__NR_arch_prctl, ARCH_SET_FS, arch->fs_base);
+	if (arch->sync & STUB_SYNC_GS_BASE)
+		stub_syscall2(__NR_arch_prctl, ARCH_SET_GS, arch->gs_base);
+
+	arch->sync = 0;
+}
+
 #endif
-- 
2.38.1


_______________________________________________
linux-um mailing list
linux-um@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-um

  parent reply	other threads:[~2022-11-22 10:11 UTC|newest]

Thread overview: 29+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-11-22 10:07 [PATCH v2 00/28] Implement SECCOMP based userland benjamin
2022-11-22 10:07 ` [PATCH v2 01/28] um: Switch printk calls to adhere to correct coding style benjamin
2022-11-22 10:07 ` [PATCH v2 02/28] um: Declare fix_range_common as a static function benjamin
2022-11-22 10:07 ` [PATCH v2 03/28] um: Drop support for hosts without SYSEMU_SINGLESTEP support benjamin
2022-11-22 10:07 ` [PATCH v2 04/28] um: Drop NULL check from start_userspace benjamin
2022-11-22 10:07 ` [PATCH v2 05/28] um: Make errors to stop ptraced child fatal during startup benjamin
2022-11-22 10:07 ` [PATCH v2 06/28] um: Don't use vfprintf() for os_info() benjamin
2022-11-22 10:07 ` [PATCH v2 07/28] um: Do not use printk in SIGWINCH helper thread benjamin
2022-11-22 10:07 ` [PATCH v2 08/28] um: Reap winch thread if it fails benjamin
2022-11-22 10:07 ` [PATCH v2 09/28] um: Do not use printk in userspace trampoline benjamin
2022-11-22 10:07 ` [PATCH v2 10/28] um: Always inline stub functions benjamin
2022-11-22 10:07 ` [PATCH v2 11/28] um: Rely on PTRACE_SETREGSET to set FS/GS base registers benjamin
2022-11-22 10:07 ` [PATCH v2 12/28] um: Remove unused register save/restore functions benjamin
2022-11-22 10:07 ` [PATCH v2 13/28] um: Mark 32bit syscall helpers as clobbering memory benjamin
2022-11-22 10:07 ` [PATCH v2 14/28] um: Remove stub-data.h include from common-offsets.h benjamin
2022-11-22 10:07 ` [PATCH v2 15/28] um: Create signal stack memory assignment in stub_data benjamin
2022-11-22 10:07 ` [PATCH v2 16/28] um: Add generic stub_syscall6 function benjamin
2022-11-22 10:07 ` [PATCH v2 17/28] um: Rework syscall handling benjamin
2022-11-22 10:07 ` [PATCH v2 18/28] um: Store full CSGSFS and SS register from mcontext benjamin
2022-11-22 10:07 ` [PATCH v2 19/28] um: Pass full mm_id to functions creating helper processes benjamin
2022-11-22 10:07 ` [PATCH v2 20/28] um: Move faultinfo extraction into userspace routine benjamin
2022-11-22 10:07 ` [PATCH v2 21/28] um: Use struct uml_pt_regs for copy_context_skas0 benjamin
2022-11-22 10:07 ` [PATCH v2 22/28] um: Add UML_SECCOMP configuration option benjamin
2022-11-22 10:07 ` benjamin [this message]
2022-11-22 10:07 ` [PATCH v2 24/28] um: Add helper functions to get/set state for SECCOMP benjamin
2022-11-22 10:07 ` [PATCH v2 25/28] um: Add SECCOMP support detection and initialization benjamin
2022-11-22 10:07 ` [PATCH v2 26/28] um: Die if a child dies unexpectedly in seccomp mode benjamin
2022-11-22 10:07 ` [PATCH v2 27/28] um: Implement kernel side of SECCOMP based process handling benjamin
2022-11-22 10:07 ` [PATCH v2 28/28] um: Delay flushing syscalls until the thread is restarted benjamin

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=20221122100759.208290-24-benjamin@sipsolutions.net \
    --to=benjamin@sipsolutions.net \
    --cc=johannes@sipsolutions.net \
    --cc=linux-um@lists.infradead.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).