Linux Security Modules development
 help / color / mirror / Atom feed
From: Christopher Lusk via B4 Relay <devnull+clusk.northecho.dev@kernel.org>
To: "Mickaël Salaün" <mic@digikod.net>
Cc: "Günther Noack" <gnoack@google.com>,
	"Jonathan Corbet" <corbet@lwn.net>,
	"Shuah Khan" <skhan@linuxfoundation.org>,
	"Randy Dunlap" <rdunlap@infradead.org>,
	linux-security-module@vger.kernel.org, linux-doc@vger.kernel.org,
	linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org,
	"Christopher Lusk" <clusk@northecho.dev>
Subject: [PATCH 2/2] selftests/landlock: Check that TSYNC restricts sibling threads
Date: Sat, 28 Nov 2026 16:30:56 -0500	[thread overview]
Message-ID: <20261128-b4-tsync-v1-2-31f0c0cef500@northecho.dev> (raw)
In-Reply-To: <20261128-b4-tsync-v1-0-31f0c0cef500@northecho.dev>

From: Christopher Lusk <clusk@northecho.dev>

The TSYNC tests check the return value and the sibling threads'
no_new_privs, but not that the siblings are actually restricted.  Add a
test that checks a sibling's write access before and after the call,
with a variant where the sibling first switches to another UID with the
raw setresuid(2), and that the sibling keeps its UID.

Signed-off-by: Christopher Lusk <clusk@northecho.dev>
---
 tools/testing/selftests/landlock/tsync_test.c | 129 ++++++++++++++++++++++++++
 1 file changed, 129 insertions(+)

diff --git a/tools/testing/selftests/landlock/tsync_test.c b/tools/testing/selftests/landlock/tsync_test.c
index 2b53596c986e..27f5fe1aa9c4 100644
--- a/tools/testing/selftests/landlock/tsync_test.c
+++ b/tools/testing/selftests/landlock/tsync_test.c
@@ -6,10 +6,13 @@
  */
 
 #define _GNU_SOURCE
+#include <fcntl.h>
 #include <linux/landlock.h>
 #include <pthread.h>
 #include <signal.h>
+#include <stdlib.h>
 #include <sys/prctl.h>
+#include <sys/stat.h>
 
 #include "common.h"
 
@@ -231,6 +234,132 @@ TEST(competing_enablement)
 	EXPECT_EQ(0, close(ruleset_fd));
 }
 
+struct sibling_probe {
+	pthread_barrier_t *ready;
+	pthread_barrier_t *restricted;
+	const char *path;
+	/* If not zero, the sibling switches to this UID before the enforcement. */
+	uid_t drop_to;
+	int drop_result;
+	int open_errno_before;
+	int open_errno_after;
+	int no_new_privs;
+	uid_t uid;
+};
+
+static int open_errno(const char *const path)
+{
+	const int fd = open(path, O_WRONLY | O_CLOEXEC);
+
+	if (fd < 0)
+		return errno;
+	close(fd);
+	return 0;
+}
+
+static void *probe_sibling(void *data)
+{
+	struct sibling_probe *const p = data;
+
+	/* Unlike setresuid(3), the raw system call only changes this thread. */
+	if (p->drop_to)
+		p->drop_result = syscall(SYS_setresuid, p->drop_to, p->drop_to,
+					 p->drop_to);
+	p->open_errno_before = open_errno(p->path);
+	pthread_barrier_wait(p->ready);
+
+	/* Waits for the other thread's landlock_restrict_self(2) to return. */
+	pthread_barrier_wait(p->restricted);
+	p->open_errno_after = open_errno(p->path);
+	p->no_new_privs = prctl(PR_GET_NO_NEW_PRIVS, 0, 0, 0, 0);
+	p->uid = syscall(SYS_getuid);
+	return NULL;
+}
+
+FIXTURE(tsync_sibling)
+{
+	char path[64];
+	int ruleset_fd;
+};
+
+FIXTURE_VARIANT(tsync_sibling)
+{
+	/* The sibling thread runs with a different UID than the caller. */
+	const bool diverged_creds;
+};
+
+/* clang-format off */
+FIXTURE_VARIANT_ADD(tsync_sibling, same_creds) {
+	/* clang-format on */
+	.diverged_creds = false,
+};
+
+/* clang-format off */
+FIXTURE_VARIANT_ADD(tsync_sibling, diverged_creds) {
+	/* clang-format on */
+	.diverged_creds = true,
+};
+
+FIXTURE_SETUP(tsync_sibling)
+{
+	int fd;
+
+	strcpy(self->path, "/tmp/landlock_tsync_XXXXXX");
+	fd = mkstemp(self->path);
+	ASSERT_LE(0, fd);
+	/* Lets a sibling with another UID write to it before the enforcement. */
+	ASSERT_EQ(0, fchmod(fd, 0666));
+	EXPECT_EQ(0, close(fd));
+	self->ruleset_fd = create_ruleset(_metadata);
+}
+
+FIXTURE_TEARDOWN(tsync_sibling)
+{
+	EXPECT_EQ(0, close(self->ruleset_fd));
+	/* LANDLOCK_ACCESS_FS_REMOVE_FILE is not handled by the ruleset. */
+	EXPECT_EQ(0, unlink(self->path));
+}
+
+/*
+ * Checks that a sibling thread is actually restricted once
+ * landlock_restrict_self(2) with LANDLOCK_RESTRICT_SELF_TSYNC returns, and
+ * that it keeps its own credentials apart from the Landlock domain and
+ * no_new_privs.
+ */
+TEST_F(tsync_sibling, restricted)
+{
+	pthread_barrier_t ready, restricted;
+	const uid_t caller_uid = getuid();
+	struct sibling_probe probe = {
+		.ready = &ready,
+		.restricted = &restricted,
+		.path = self->path,
+		.drop_to = variant->diverged_creds ? 65534 : 0,
+	};
+	pthread_t t;
+
+	ASSERT_EQ(0, pthread_barrier_init(&ready, NULL, 2));
+	ASSERT_EQ(0, pthread_barrier_init(&restricted, NULL, 2));
+	ASSERT_EQ(0, pthread_create(&t, NULL, probe_sibling, &probe));
+	pthread_barrier_wait(&ready);
+
+	disable_caps(_metadata);
+	ASSERT_EQ(0, prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0));
+	ASSERT_EQ(0, landlock_restrict_self(self->ruleset_fd,
+					    LANDLOCK_RESTRICT_SELF_TSYNC));
+	pthread_barrier_wait(&restricted);
+	ASSERT_EQ(0, pthread_join(t, NULL));
+
+	if (variant->diverged_creds && probe.drop_result)
+		SKIP(return, "Cannot switch the sibling thread's UID");
+
+	EXPECT_EQ(0, probe.open_errno_before);
+	EXPECT_EQ(EACCES, probe.open_errno_after);
+	EXPECT_EQ(1, probe.no_new_privs);
+	EXPECT_EQ(variant->diverged_creds ? 65534 : caller_uid, probe.uid);
+	EXPECT_EQ(EACCES, open_errno(self->path));
+}
+
 static void signal_nop_handler(int sig)
 {
 }

-- 
2.56.1



  parent reply	other threads:[~2026-11-28 21:31 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-28 21:30 [PATCH 0/2] landlock: Document and test TSYNC on sibling threads Christopher Lusk via B4 Relay
2026-11-28 21:30 ` [PATCH 1/2] landlock: Document TSYNC in the inheritance section Christopher Lusk via B4 Relay
2026-11-28 21:30 ` Christopher Lusk via B4 Relay [this message]
2026-11-28 21:41   ` [PATCH 2/2] selftests/landlock: Check that TSYNC restricts sibling threads sashiko-bot
2026-11-28 21:41 ` [PATCH 0/2] landlock: Document and test TSYNC on " 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=20261128-b4-tsync-v1-2-31f0c0cef500@northecho.dev \
    --to=devnull+clusk.northecho.dev@kernel.org \
    --cc=clusk@northecho.dev \
    --cc=corbet@lwn.net \
    --cc=gnoack@google.com \
    --cc=linux-doc@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=linux-security-module@vger.kernel.org \
    --cc=mic@digikod.net \
    --cc=rdunlap@infradead.org \
    --cc=skhan@linuxfoundation.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