Linux Security Modules development
 help / color / mirror / Atom feed
From: Christian Brauner <brauner@kernel.org>
To: Jann Horn <jannh@google.com>
Cc: Linus Torvalds <torvalds@linux-foundation.org>,
	 linux-fsdevel@vger.kernel.org,
	Arnaldo Carvalho de Melo <acme@kernel.org>,
	 linux-perf-users@vger.kernel.org, bpf@vger.kernel.org,
	 selinux@vger.kernel.org, Kees Cook <kees@kernel.org>,
	linux-mm@kvack.org,  linux-security-module@vger.kernel.org,
	apparmor@lists.ubuntu.com,  David Hildenbrand <david@kernel.org>,
	 "Christian Brauner (Amutable)" <brauner@kernel.org>
Subject: [PATCH RFC 08/19] selftests/exec: test exec with a shared signal table
Date: Tue, 24 Nov 2026 23:58:31 +0100	[thread overview]
Message-ID: <20261124-work-exec-update_lock-v1-8-e489035395b8@kernel.org> (raw)
In-Reply-To: <20261124-work-exec-update_lock-v1-0-e489035395b8@kernel.org>

Test that exec gives a task that shares its signal table with another
process a private one:

- handlers the processes shared are reset in the new image

- an ignored signal stays ignored in the new image

- the other process keeps all of its handlers

- a signal the other process ignores right after vfork() returned isn't
  ignored in the new image

The task is created with CLONE_SIGHAND but without CLONE_THREAD so it
isn't taken care of by de_thread(). For the last one the parent runs in
a realtime scheduling class on the cpu of the task so that it gets to
ignore the signal as soon as it's woken up.

Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>
---
 tools/testing/selftests/exec/.gitignore        |   1 +
 tools/testing/selftests/exec/Makefile          |   2 +
 tools/testing/selftests/exec/unshare-sighand.c | 236 +++++++++++++++++++++++++
 3 files changed, 239 insertions(+)

diff --git a/tools/testing/selftests/exec/.gitignore b/tools/testing/selftests/exec/.gitignore
index 4613c68537395..b27f4db42f87b 100644
--- a/tools/testing/selftests/exec/.gitignore
+++ b/tools/testing/selftests/exec/.gitignore
@@ -16,6 +16,7 @@ null-argv
 !load_address.c
 /recursion-depth
 /set-exec
+/unshare-sighand
 /update-lock-fault
 xxxxxxxx*
 pipe
diff --git a/tools/testing/selftests/exec/Makefile b/tools/testing/selftests/exec/Makefile
index 3756a250f9591..70f090a5f7648 100644
--- a/tools/testing/selftests/exec/Makefile
+++ b/tools/testing/selftests/exec/Makefile
@@ -21,6 +21,7 @@ TEST_GEN_PROGS += recursion-depth
 TEST_GEN_PROGS += null-argv
 TEST_GEN_PROGS += check-exec
 TEST_GEN_PROGS += update-lock-fault
+TEST_GEN_PROGS += unshare-sighand
 
 # binfmt_misc must not be reachable as an exec source or as a stacking layer,
 # or an 'F' entry can pin the instance that owns it. Unprivileged, no bpf.
@@ -117,6 +118,7 @@ $(OUTPUT)/binfmt_misc_interplimit: CFLAGS += $(TOOLS_INCLUDES)
 $(OUTPUT)/binfmt_misc_interplimit: ../filesystems/utils.c
 
 $(OUTPUT)/update-lock-fault: LDLIBS += -lpthread
+$(OUTPUT)/unshare-sighand: LDLIBS += -lpthread
 
 # --- binfmt_misc bpf ('B') handler test ---------------------------------
 # The struct_ops bpf objects are compiled against the running kernel's BTF.
diff --git a/tools/testing/selftests/exec/unshare-sighand.c b/tools/testing/selftests/exec/unshare-sighand.c
new file mode 100644
index 0000000000000..425b46d4d5c9c
--- /dev/null
+++ b/tools/testing/selftests/exec/unshare-sighand.c
@@ -0,0 +1,236 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Test exec in a task that shares its signal table with another process
+ * without being one of its threads, i.e. CLONE_SIGHAND without CLONE_THREAD.
+ * The exec has to give the task a private table before it resets the
+ * handlers so the other process keeps the table they shared. If the other
+ * process waits for the task in vfork() the task has to have its private
+ * table before that process runs again.
+ */
+#define _GNU_SOURCE
+#include <pthread.h>
+#include <sched.h>
+#include <signal.h>
+#include <stdbool.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/mman.h>
+#include <sys/wait.h>
+#include <unistd.h>
+
+#include "kselftest_harness.h"
+
+#define STACK_SIZE	(64 * 1024)
+
+/* Makes the exec'd image check its dispositions. */
+#define EXECED_ARG	"execed"
+/* Makes the exec'd image check the signal its vfork() parent ignored. */
+#define EXECED_VFORK_ARG	"execed-vfork"
+
+/* Exit status of a sharer that never made it into the exec'd image. */
+#define EXIT_NO_EXEC	127
+
+/* Handled before the clone. */
+static const int handled_signals[] = { SIGUSR1, SIGUSR2 };
+/* Handled by the sharer right before its exec. */
+#define SHARER_SIGNAL	SIGTERM
+/* Ignored before the clone. */
+#define IGNORED_SIGNAL	SIGHUP
+/* Ignored by a vfork() parent as soon as it runs again. */
+#define VFORK_SIGNAL	SIGINT
+
+/* How often a vfork() parent races with the exec of its child. */
+#define VFORK_ROUNDS	16
+
+/* How much the thread that holds the mmap lock has to work on. */
+#define WRITER_SIZE	(1UL << 30)
+
+static void handler(int sig)
+{
+}
+
+static int set_disposition(int sig, sighandler_t disposition)
+{
+	const struct sigaction sa = { .sa_handler = disposition };
+
+	return sigaction(sig, &sa, NULL);
+}
+
+static const char *disposition_name(sighandler_t disposition)
+{
+	if (disposition == SIG_DFL)
+		return "default";
+	if (disposition == SIG_IGN)
+		return "ignored";
+	return "handled";
+}
+
+static bool has_disposition(const char *who, int sig, sighandler_t disposition)
+{
+	struct sigaction sa;
+
+	if (sigaction(sig, NULL, &sa))
+		return false;
+	ksft_print_msg("%s: signal %d (%s) is %s\n", who, sig, strsignal(sig),
+		       disposition_name(sa.sa_handler));
+	return sa.sa_handler == disposition;
+}
+
+/* The signals with a handler must have @handled, the ignored one SIG_IGN. */
+static bool check_dispositions(const char *who, sighandler_t handled)
+{
+	bool ret = true;
+	unsigned int i;
+
+	for (i = 0; i < ARRAY_SIZE(handled_signals); i++)
+		ret &= has_disposition(who, handled_signals[i], handled);
+	ret &= has_disposition(who, SHARER_SIGNAL, handled);
+	ret &= has_disposition(who, IGNORED_SIGNAL, SIG_IGN);
+	return ret;
+}
+
+static int setup_dispositions(void)
+{
+	unsigned int i;
+
+	for (i = 0; i < ARRAY_SIZE(handled_signals); i++) {
+		if (set_disposition(handled_signals[i], handler))
+			return -1;
+	}
+	return set_disposition(IGNORED_SIGNAL, SIG_IGN);
+}
+
+static int sharer(void *arg)
+{
+	/* The parent only gets to see this if the table really is shared. */
+	if (!set_disposition(SHARER_SIGNAL, handler))
+		execl("/proc/self/exe", "unshare-sighand", EXECED_ARG,
+		      (char *)NULL);
+	_exit(EXIT_NO_EXEC);
+}
+
+TEST(exec_unshares_sighand)
+{
+	char *stack;
+	int status;
+	pid_t pid;
+
+	ASSERT_EQ(setup_dispositions(), 0);
+
+	stack = mmap(NULL, STACK_SIZE, PROT_READ | PROT_WRITE,
+		     MAP_PRIVATE | MAP_ANONYMOUS | MAP_STACK, -1, 0);
+	ASSERT_NE(stack, MAP_FAILED);
+
+	pid = clone(sharer, stack + STACK_SIZE,
+		    CLONE_VM | CLONE_SIGHAND | SIGCHLD, NULL);
+	ASSERT_GT(pid, 0);
+	ASSERT_EQ(waitpid(pid, &status, 0), pid);
+	ASSERT_TRUE(WIFEXITED(status));
+	/* The exec'd image has the handlers reset and SIG_IGN inherited. */
+	EXPECT_EQ(WEXITSTATUS(status), EXIT_SUCCESS);
+
+	/* The table was shared up to the exec and the exec left it alone. */
+	EXPECT_TRUE(check_dispositions("parent", handler));
+
+	EXPECT_EQ(munmap(stack, STACK_SIZE), 0);
+}
+
+struct mmap_writer {
+	pthread_t thread;
+	char *map;
+	bool stop;
+};
+
+/*
+ * Hold the mmap lock for writing most of the time. An exec that waits for
+ * the mmap lock of the old mm after it has woken up a vfork() parent gives
+ * that parent all the time it needs.
+ */
+static void *mmap_writer(void *arg)
+{
+	struct mmap_writer *writer = arg;
+
+	while (!__atomic_load_n(&writer->stop, __ATOMIC_RELAXED)) {
+		mprotect(writer->map, WRITER_SIZE, PROT_NONE);
+		mprotect(writer->map, WRITER_SIZE, PROT_READ);
+	}
+	return NULL;
+}
+
+static int vfork_sharer(void *arg)
+{
+	execl("/proc/self/exe", "unshare-sighand", EXECED_VFORK_ARG,
+	      (char *)NULL);
+	_exit(EXIT_NO_EXEC);
+}
+
+TEST(exec_unshares_sighand_before_vfork_returns)
+{
+	struct sched_param param = { .sched_priority = 1 };
+	struct mmap_writer writer = {};
+	cpu_set_t cpu;
+	char *stack;
+	int status;
+	pid_t pid;
+	int i;
+
+	stack = mmap(NULL, STACK_SIZE, PROT_READ | PROT_WRITE,
+		     MAP_PRIVATE | MAP_ANONYMOUS | MAP_STACK, -1, 0);
+	ASSERT_NE(stack, MAP_FAILED);
+
+	/* One page table entry per page, all of them the zero page. */
+	writer.map = mmap(NULL, WRITER_SIZE, PROT_READ,
+			  MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE, -1, 0);
+	ASSERT_NE(writer.map, MAP_FAILED);
+	madvise(writer.map, WRITER_SIZE, MADV_NOHUGEPAGE);
+	madvise(writer.map, WRITER_SIZE, MADV_POPULATE_READ);
+	ASSERT_EQ(pthread_create(&writer.thread, NULL, mmap_writer, &writer), 0);
+
+	/*
+	 * The task that is cloned below runs on our cpu and, as the policy
+	 * isn't inherited, not in a realtime scheduling class. On a kernel
+	 * with preemption we run as soon as it wakes us up and it doesn't get
+	 * any further meanwhile. Without the privilege for that only an exec
+	 * that sleeps after it has woken us up is caught.
+	 */
+	CPU_ZERO(&cpu);
+	CPU_SET(sched_getcpu(), &cpu);
+	if (!sched_setaffinity(0, sizeof(cpu), &cpu))
+		sched_setscheduler(0, SCHED_FIFO | SCHED_RESET_ON_FORK, &param);
+
+	for (i = 0; i < VFORK_ROUNDS; i++) {
+		ASSERT_EQ(set_disposition(VFORK_SIGNAL, SIG_DFL), 0);
+
+		pid = clone(vfork_sharer, stack + STACK_SIZE,
+			    CLONE_VM | CLONE_VFORK | CLONE_SIGHAND | SIGCHLD,
+			    NULL);
+		/* The task has its own table. This is none of its business. */
+		set_disposition(VFORK_SIGNAL, SIG_IGN);
+		ASSERT_GT(pid, 0);
+		ASSERT_EQ(waitpid(pid, &status, 0), pid);
+		ASSERT_TRUE(WIFEXITED(status));
+		ASSERT_EQ(WEXITSTATUS(status), EXIT_SUCCESS) {
+			TH_LOG("round %d: ignored after vfork(), ignored in the exec'd image", i);
+		}
+	}
+
+	__atomic_store_n(&writer.stop, true, __ATOMIC_RELAXED);
+	ASSERT_EQ(pthread_join(writer.thread, NULL), 0);
+	EXPECT_EQ(munmap(writer.map, WRITER_SIZE), 0);
+	EXPECT_EQ(munmap(stack, STACK_SIZE), 0);
+}
+
+int main(int argc, char **argv)
+{
+	struct sigaction sa;
+
+	if (argc == 2 && !strcmp(argv[1], EXECED_ARG))
+		return check_dispositions("exec'd image", SIG_DFL) ?
+		       EXIT_SUCCESS : EXIT_FAILURE;
+	if (argc == 2 && !strcmp(argv[1], EXECED_VFORK_ARG)) {
+		if (sigaction(VFORK_SIGNAL, NULL, &sa))
+			return EXIT_FAILURE;
+		return sa.sa_handler == SIG_DFL ? EXIT_SUCCESS : EXIT_FAILURE;
+	}
+	return test_harness_run(argc, argv);
+}

-- 
2.53.0


  parent reply	other threads:[~2026-11-24 23:00 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-24 22:58 [PATCH RFC 00/19] exec: move sleeping work out of exec_update_lock Christian Brauner
2026-11-24 22:58 ` [PATCH RFC 01/19] proc: copy namespace link after dropping exec_update_lock Christian Brauner
2026-11-24 22:58 ` [PATCH RFC 02/19] kcmp: look up epoll target without exec_update_lock held Christian Brauner
2026-11-24 22:58 ` [PATCH RFC 03/19] perf: copy counter values after dropping ctx->mutex Christian Brauner
2026-11-24 22:58 ` [PATCH RFC 04/19] perf: copy ioctl arguments without ctx->mutex held Christian Brauner
2026-11-24 22:58 ` [PATCH RFC 05/19] selinux: close inherited files that can't be replaced Christian Brauner
2026-11-24 22:58 ` [PATCH RFC 06/19] exec: move unshare_sighand() before exec_mmap() Christian Brauner
2026-11-24 22:58 ` [PATCH RFC 07/19] selftests/exec: test exec with a task sleeping in a page fault Christian Brauner
2026-11-24 22:58 ` Christian Brauner [this message]
2026-11-24 22:58 ` [PATCH RFC 09/19] lsm: add security_bprm_revalidate_files() Christian Brauner
2026-11-24 22:58 ` [PATCH RFC 10/19] bpf: mark bprm_revalidate_files as sleepable Christian Brauner
2026-11-24 22:58 ` [PATCH RFC 11/19] selinux: revalidate inherited files before taking exec_update_lock Christian Brauner
2026-11-24 22:58 ` [PATCH RFC 12/19] apparmor: " Christian Brauner
2026-11-24 22:58 ` [PATCH RFC 13/19] exec: drop exec_update_lock after the credentials are committed Christian Brauner
2026-11-24 22:58 ` [PATCH RFC 14/19] exec: switch time namespace after dropping exec_update_lock Christian Brauner
2026-11-24 22:58 ` [PATCH RFC 15/19] exec: don't take the mmap lock of the old mm Christian Brauner
2026-11-24 22:58 ` [PATCH RFC 16/19] exit: don't take the mmap lock in exit_mm() Christian Brauner
2026-11-24 22:58 ` [PATCH RFC 17/19] proc: look up map_files vma without exec_update_lock held Christian Brauner
2026-11-24 23:09   ` sashiko-bot
2026-11-24 22:58 ` [PATCH RFC 18/19] vfork: wake up the parent once the child has left the mm Christian Brauner
2026-11-24 22:58 ` [PATCH RFC 19/19] selftests/exec: test when a vfork() parent is woken up Christian Brauner
2026-11-24 23:15 ` [PATCH RFC 00/19] exec: move sleeping work out of exec_update_lock sashiko-bot

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=20261124-work-exec-update_lock-v1-8-e489035395b8@kernel.org \
    --to=brauner@kernel.org \
    --cc=acme@kernel.org \
    --cc=apparmor@lists.ubuntu.com \
    --cc=bpf@vger.kernel.org \
    --cc=david@kernel.org \
    --cc=jannh@google.com \
    --cc=kees@kernel.org \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=linux-perf-users@vger.kernel.org \
    --cc=linux-security-module@vger.kernel.org \
    --cc=selinux@vger.kernel.org \
    --cc=torvalds@linux-foundation.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