From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=pass header.oldest-pass=0 smtp.remote-ip=100.103.45.18 ARC-Seal: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass; t=1795901461; b=VjA+gwaL3GBnBnxkUWq6uwYqYFiPg0Ycm54whOIt2xqa0HCw2qt0cFZITnTSfmSMT/E9 RX8mFu+XSwQaiwHyao+hYPy5DT6O1MqL60SaxkRdxa3TE/3QWjUdP/MX/CeeMDezRogjV AL3yyuYDAKMScA6hwxtXx5YUssGmWFW+9f/yWn8IV7USf2StzbPRebc9wD+tZg2Ihjwtn ZV8PYf/n3AyFwa8gQaHYkpLBxhh+Tt9HCkmbQCzUGAchgOmIJvhuq92x3aXWUNskOulDU IywdionSmFXWQA+ynR1z4+byBkglI/5gFCZC1ZjhYky+KRmn0ggsI88Oloxzt+E3ngA== ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795901461; h=Received:DKIM-Signature:Received:From:Date:Subject:MIME-Version: Content-Type:Message-Id:References:In-Reply-To:To:Cc:X-Mailer: X-Developer-Signature:X-Developer-Key:X-Endpoint-Received: X-Original-From:Reply-To:Content-Transfer-Encoding; bh=iJI4QgKHlfnsYsj92KoRftZCEiB+7vqNVQ+VscPHCCg=; b=MYLkZX/7umU9ozQ8JvRWU92ztOQ89fJYEPf32SVf/+q2PTDqo9BmIMFN0F7+G3ekcJRU 23ZW+sQQyqFvu06o7SwhapHAK0QImhptc0P6N0hXfD4ju0PoKe3l3FkYfnN4B9Y1hZE/o x6G0Z6AdWc/6BG5sr18T3+wGugwM8l8GNohe35vL7511q6dSaEdjxcPxALqd5Cqf8M6BM UDHPbM6L9IN7VsFAFx4iQMFp7ml1g54quHFPXGyM/3j+xbCUd6hq14tS70qULP5iWFYMI pXgWsOd3sX5qthQMH6TO6g+g6Icp21omG1TXNT7vSLrlHBpDcLkINdlCWoT3ARmTUYg== ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=i7Gp8Gaj; arc=pass header.oldest-pass=0 smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 348C9606A323; Sat, 28 Nov 2026 21:31:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=i7Gp8Gaj Authentication-Results: smtp.kernel.org; arc=none smtp.remote-ip=100.80.115.77 ARC-Seal: i=1; d=kernel.org; s=arc20260519; a=rsa-sha256; cv=none; t=1795901461; b=EbmaKS6tIk5SusDoI3inAcczDD/QYKADo250a0TKd8YpwA8Q1zi+m7zY41YyB0Ms9k77 Wq3wtyVaCKWoQjovTObBSRJxxijl2Nn7iZIwoW6hDNParNef+cRllCpdAMtSfVRDqgfzc uiPa/28sF7kd6mvN/6ImheyQ3JzgIdSRm3Pavm3KqWCNpoKH4br8SmBwIbTe5Pjs9I1WY wSrt8waa3QzX7pagXikjuJsr+8Rmih/Qq6Ix5WxGh4yRSrs/linaEr+wgUQij3e1taQ/C 4v0xJOVBuExl+ZwKEm6HSWSiEUleFytPGoK7cAnxin5po6Hy0xOTQUrZpJiuijBt7rA== ARC-Message-Signature: i=1; d=kernel.org; s=arc20260519; a=rsa-sha256; c=relaxed/relaxed; t=1795901461; h=DKIM-Signature:Received:From:Date:Subject:MIME-Version:Content-Type: Message-Id:References:In-Reply-To:To:Cc:X-Mailer: X-Developer-Signature:X-Developer-Key:X-Endpoint-Received: X-Original-From:Reply-To:Content-Transfer-Encoding; bh=iJI4QgKHlfnsYsj92KoRftZCEiB+7vqNVQ+VscPHCCg=; b=SXgG7akLD3Fg5HBym0YCLW8VbN8w5HRrMmSWTqa8dLTksVvsdM061GURwcFyTpqfdz/J JeHsWre6Cr9GRizE4yeoxD/MKujQzpdSpXuRemD+mrgZ45kZFpPhaBSPCAFkTLprvP4NM HrRy42LdR/EGrvNhxjgVe91FOSNHQB13JfnMmZf+Xy0M0saCQgHOqk5QQ5FhQoeE9DfEI J1MWlOvHYHyf06YqTJGWvjt93YowsMEem83vvF1MwQXlJXKVmzXw+I43zcPqgm3h+xJ1Y TA09CXqoeYfFuCO83UnxmXwxzCjLyfLtiXkQho6BaWjb0MHmG4VB+EgNU3RLtb85gRQ== ARC-Authentication-Results: i=1; smtp.kernel.org; arc=none smtp.remote-ip=100.80.115.77 Received: from lkml.kernel.org (nexus.subspace.kernel.org [100.80.115.77]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.kernel.org (Postfix) with ESMTPS id 123861F00897; Sat, 28 Nov 2026 21:31:00 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1795901461; bh=iJI4QgKHlfnsYsj92KoRftZCEiB+7vqNVQ+VscPHCCg=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To; b=i7Gp8Gajcd3oDWThGdxFUZzSGhX9mHNKK/t+7YMH3oVPoFewdp56A7C7YljX8u2oX HfBPK4r+SFiI5GKLVjy+UwYL+RTRAMJStQ3pRz/KaGbf13Ub139zgYVC0Xb5KSyu1q Vps+SLqH+m2ckIULshjbaqoJAibbUCix99GAwDTSS50mg4Yy5LW8zOrGxSfmBmmyWp wKIiqL93WhbTIs/OhSHnw3yo+dCBs2Hse4ij1ynB5YJUx2tmTo0sqGMfSoNhRxk3xt Ct6DrI0cxUxnbaoyDh8DuYWqpb0aCCZx6L/bJRE4Uy+3Zq2jSGMQUprPNOES1ZFE2z NTxmVvau1e0+Q== Received: from nexus.subspace.kernel.org (localhost [IPv6:::1]) by lkml.kernel.org (Postfix) with ESMTP id 934354E265; Sat, 28 Nov 2026 21:30:59 +0000 (UTC) From: Christopher Lusk via B4 Relay Date: Sat, 28 Nov 2026 16:30:56 -0500 Subject: [PATCH 2/2] selftests/landlock: Check that TSYNC restricts sibling threads X-Mailing-List: linux-security-module@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Message-Id: <20261128-b4-tsync-v1-2-31f0c0cef500@northecho.dev> References: <20261128-b4-tsync-v1-0-31f0c0cef500@northecho.dev> In-Reply-To: <20261128-b4-tsync-v1-0-31f0c0cef500@northecho.dev> To: =?utf-8?q?Micka=C3=ABl_Sala=C3=BCn?= Cc: =?utf-8?q?G=C3=BCnther_Noack?= , Jonathan Corbet , Shuah Khan , Randy Dunlap , linux-security-module@vger.kernel.org, linux-doc@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org, Christopher Lusk X-Mailer: b4 0.14.3 X-Developer-Signature: v=1; a=ed25519-sha256; t=1795901459; l=4744; i=clusk@northecho.dev; s=20261128; h=from:subject:message-id; bh=Exs8Q885HqKvGC9zNUiQFqjbqlf9hV8FObhWA/qeMr4=; b=0KpvB8S91VWGq8Qee1h2jb3OmxUJ0n7iVRVJYPHgxHjQyXAXydbr56+ccSkMXtjKzTgcBPrV9 xTRg6q9iirWAfIiTjFTd6VH+O0Cx8wOY+MyuyMZmB05IxIkPon0WQFK X-Developer-Key: i=clusk@northecho.dev; a=ed25519; pk=ZM3GLuxrIQN40dt+s1cLwHjpuFeiMfiEYnQ/qyYhVGo= X-Endpoint-Received: by B4 Relay for clusk@northecho.dev/20261128 with auth_id=1681 X-Original-From: Christopher Lusk Reply-To: clusk@northecho.dev Content-Transfer-Encoding: 7bit From: Christopher Lusk 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 --- 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 #include #include #include +#include #include +#include #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