From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=45.157.188.12 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796383091; b=CMP0yfd9IlwickqicKOxw6Zgrr2ywiXz+LvS/cZq3gRnMxVJN/2TPhoICv7IG2meV/D6 qcYpzxybA8uurKFNR4w/grssoqffYzUjUJuV6NNs+pbHTsrZoQ+2MOFCd9NSlTOj3m8Xx 4lNmaOSCbnZjQD/9aE3IU84FbUGtOJ3Ib0InHDPWz66osPNBZFsW6jbLUHDOHSw93lW7w BaUT6LLaw31U5zs5hpOQwVJyoiKWKTEeeXgK60IJi/FaJF/oGwk3RGAbMh4Gb3jiN0dfN hjjGlHgtMvOi+Gwn9CpzPttTYa+lfdpJA8AdWQ1kp5bP4ZjxelAlDE0yjCpmtjR8mug== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796383091; h=DMARC-Filter:Received:DKIM-Signature:Received:From:To:Cc:Subject: Date:Message-ID:MIME-Version:Content-Type:Content-Transfer-Encoding: X-Infomaniak-Routing; bh=Ag4YlMcJxJTOXowx2phYuDUTt/E6YkKBIXg0QYiBlhU=; b=hxI6MDFofpbEXKw84Y/MxgJKmw8ypswD2Y4xX3j4kbpePtYk6SZLfYyrh1OMHkX+AX2F +pQxfHSSNuNQ2NMmXdq/vrj6x6/8fmKV+mrR7meday5sZgL7K6KCzNlfuOQ1XMyDUaV/K qMVZNd8mHPa1p1mZ+pcM0g05jRiiQKO2iMapRQUwA4PjojQ24hyiBVQBubKAkZZO3Cvb6 55FfoI6Ot8OT1LPwogV/TvkVz/mFw+KFzy2RA2ChOm+7Y9KN4RP8BND95WNiens0DZ5Sn SZXo/Fld/JHMzsP+VXdKcoI1bzJU64i5Kzetk3uogcwIeggccyZ9n/v62T8edJxMH2A== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none header.from=digikod.net; spf=pass smtp.mailfrom=digikod.net; dkim=pass header.d=digikod.net header.i=@digikod.net header.a=rsa-sha256 header.s=20191114 header.b=mECUjQAd; arc=none smtp.remote-ip=45.157.188.12 Received: from smtp-bc0c.mail.infomaniak.ch (smtp-bc0c.mail.infomaniak.ch [45.157.188.12]) (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.subspace.kernel.org (Postfix) with ESMTPS id B226E606A332 for ; Fri, 4 Dec 2026 11:18:10 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org B226E606A332 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=digikod.net Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=digikod.net Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=digikod.net header.i=@digikod.net header.a=rsa-sha256 header.s=20191114 header.b=mECUjQAd Received: from smtp-4-0000.mail.infomaniak.ch (unknown [IPv6:2001:1600:7:10::a6b]) by smtp-3-3000.mail.infomaniak.ch (Postfix) with ESMTPS id 4jgprV65q6zpfL; Fri, 4 Dec 2026 12:18:02 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=digikod.net; s=20191114; t=1796383082; bh=Ag4YlMcJxJTOXowx2phYuDUTt/E6YkKBIXg0QYiBlhU=; h=From:To:Cc:Subject:Date:From; b=mECUjQAdJvFRq23Mh8Y7PF55MKi4QZfwTnd9t2Ih+20SQlbxg2c20S7DmqcEi4sAA qyaXTU8DMLwrj57tCmTxHimcFbGukMV/jxyZUwUwB+ZFnj2fKR1X61BnHRAQ1CXdVY dx9VfetiSYo7fkHmEs7okF6zxK0JDswnawfEx2To= Received: from unknown by smtp-4-0000.mail.infomaniak.ch (Postfix) with ESMTPA id 4jgprS3nmbzvZ5; Fri, 4 Dec 2026 12:18:00 +0100 (CET) From: =?UTF-8?q?Micka=C3=ABl=20Sala=C3=BCn?= To: Christian Brauner , =?UTF-8?q?G=C3=BCnther=20Noack?= , Linus Torvalds , Oleg Nesterov , Tejun Heo Cc: =?UTF-8?q?Micka=C3=ABl=20Sala=C3=BCn?= , Bart Van Assche , Cen Zhang , =?UTF-8?q?G=C3=BCnther=20Noack?= , James Morris , Jann Horn , kernel test robot , Lee Seong Hyeon , Marco Elver , NeilBrown , Nickolai Zeldovich , Paul Moore , Peter Zijlstra , "Serge E . Hallyn" , Yihan Ding , linux-kernel@vger.kernel.org, linux-security-module@vger.kernel.org, stable@vger.kernel.org Subject: [PATCH v3] landlock: Do not hold exec_update_lock across the TSYNC wait Date: Fri, 4 Dec 2026 12:17:53 +0100 Message-ID: <20261204111754.1452982-1-mic@digikod.net> 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 Content-Transfer-Encoding: 8bit X-Infomaniak-Routing: alpha landlock_restrict_sibling_threads() write-locks current->signal->exec_update_lock to serialize TSYNC callers and holds it until every sibling has run its task work. That wait is unbounded. Readers of the lock, including procfs and process inspection tools, can therefore stall on an unrelated task's Landlock operation. Reserve the thread group for a cross-thread credential update instead. A bit in signal_struct.flags has the right sharing and lifetime without growing the structure or allocating memory. A separate table would need per-operation storage and synchronization. The signal code takes sighand->siglock only to check or update the reservation, preserves IRQ state, and rejects acquisition during group exit or exec teardown. Unlike seccomp, which updates sibling filter state directly, Landlock waits for siblings to update their own credentials. A competing caller must restart rather than block so it can run the winner's task work. Landlock keeps that restart protocol and the guard releases only a successful reservation. No other credential path changes. Exercise the guard in KUnit, including release on success, preserving a held reservation when Landlock restarts, and IRQ-state preservation. Cc: Christian Brauner Cc: Günther Noack Cc: Nickolai Zeldovich Cc: Oleg Nesterov Cc: Tejun Heo Cc: Yihan Ding Cc: stable@vger.kernel.org Reported-by: Lee Seong Hyeon Fixes: ff88df67dbf7 ("landlock: Serialize TSYNC thread restriction") Signed-off-by: Mickaël Salaün --- Changes since v2: https://patch.msgid.link/20261130143603.512171-1-mic@digikod.net - Move ownership of the reservation to the signal core after the pull request discussion. Use a scoped SIGNAL_CRED_SYNC guard and keep the Landlock restart protocol unchanged: https://patch.msgid.link/CAHk-%3Dwg23T4sgfm0EjH%3DTKBmfjJfJ-pLp6-fySwFRxNmPzZNhA%40mail.gmail.com - Use spinlock_irqsave to preserve the caller's IRQ state on acquisition and release. Test preservation with IRQs initially disabled and use the guard rather than reading the bit directly. - Convert the v2 Acked-by and Reviewed-by tags to Cc for fresh review of the core interface. Changes since v1: https://patch.msgid.link/20261120151554.2651743-1-mic@digikod.net - Resolve current->sighand in current_siglock() before passing the lock to the guards, including the KUnit guard. This fixes the Sparse address-space warnings reported by the kernel test robot: https://lore.kernel.org/oe-kbuild-all/20261123155122.GQ2URc4f-lkp@intel.com/ - Carry forward the Acked-by and Reviewed-by tags from v1. Changes since the initial report from Lee Seong Hyeon: - Rewrite the fix, storing the marker in an unused signal_struct.flags bit protected by sighand->siglock instead of adding a signal_struct field. - Treat group exit and ongoing exec as a held marker, because group exit may overwrite the flags. - Release the marker with a cleanup class. - Clarify that not all readers wait in D state: perf_event_open() waits interruptibly. - Add KUnit tests. - Credit Lee Seong Hyeon with Reported-by instead of authorship, because the implementation was rewritten. --- include/linux/sched/signal.h | 11 ++++ kernel/signal.c | 47 ++++++++++++++ security/landlock/tsync.c | 120 +++++++++++++++++++++++++++++++---- 3 files changed, 165 insertions(+), 13 deletions(-) diff --git a/include/linux/sched/signal.h b/include/linux/sched/signal.h index 0adb51ab1542..28d1ae73dcf6 100644 --- a/include/linux/sched/signal.h +++ b/include/linux/sched/signal.h @@ -262,6 +262,7 @@ struct signal_struct { #define SIGNAL_STOP_STOPPED 0x00000001 /* job control stop in effect */ #define SIGNAL_STOP_CONTINUED 0x00000002 /* SIGCONT since WCONTINUED reap */ #define SIGNAL_GROUP_EXIT 0x00000004 /* group exit in progress */ +#define SIGNAL_CRED_SYNC 0x00000008 /* group credential sync in progress */ /* * Pending notifications to parent. */ @@ -281,6 +282,16 @@ static inline void signal_set_stop_flags(struct signal_struct *sig, sig->flags = (sig->flags & ~SIGNAL_STOP_MASK) | flags; } +/* + * Use ACQUIRE(task_cred_sync, ...) and check ACQUIRE_ERR() before proceeding. + * Do not copy or transfer the guard: acquire and release in the same task's + * scope, and do not hold it across a non-returning path such as do_exit(). + */ +typedef bool class_task_cred_sync_t; +__must_check class_task_cred_sync_t class_task_cred_sync_constructor(void); +void class_task_cred_sync_destructor(class_task_cred_sync_t *acquired); +DEFINE_CLASS_IS_COND_GUARD(task_cred_sync); + extern void flush_signals(struct task_struct *); extern void ignore_signals(struct task_struct *); extern void flush_signal_handlers(struct task_struct *, int force_default); diff --git a/kernel/signal.c b/kernel/signal.c index dd770e68faee..ae93766a74cb 100644 --- a/kernel/signal.c +++ b/kernel/signal.c @@ -3196,6 +3196,53 @@ static void retarget_shared_pending(struct task_struct *tsk, sigset_t *which) } } +static spinlock_t *current_siglock(void) +{ + /* Only the current task can replace its own sighand. */ + return &rcu_dereference_protected(current->sighand, true)->siglock; +} + +/** + * class_task_cred_sync_constructor - Reserve the current thread group + * + * The reservation serializes only users of this guard. The caller must + * allow siblings to make progress if acquisition fails. No lock is held + * on return, and the caller's IRQ state is preserved. + * + * Context: Task context, subject to the usual spinlock_t rules. + * Return: true on success, false if a reservation, group exit, or exec + * teardown is in progress. + */ +class_task_cred_sync_t class_task_cred_sync_constructor(void) +{ + struct signal_struct *sig = current->signal; + + guard(spinlock_irqsave)(current_siglock()); + + /* + * Group exit may replace the whole flags word. Both group exit and + * exec teardown kill siblings under siglock; do not start a new + * credential sync in either case. + */ + if ((sig->flags & (SIGNAL_GROUP_EXIT | SIGNAL_CRED_SYNC)) || + sig->group_exec_task) + return false; + + sig->flags |= SIGNAL_CRED_SYNC; + return true; +} + +void class_task_cred_sync_destructor(class_task_cred_sync_t *acquired) +{ + if (!*acquired) + return; + *acquired = false; + + guard(spinlock_irqsave)(current_siglock()); + + current->signal->flags &= ~SIGNAL_CRED_SYNC; +} + void exit_signals(struct task_struct *tsk) { LIST_HEAD(sigq_list); diff --git a/security/landlock/tsync.c b/security/landlock/tsync.c index cfa73a2a67ca..75b0fda81c8a 100644 --- a/security/landlock/tsync.c +++ b/security/landlock/tsync.c @@ -5,6 +5,7 @@ * Copyright © 2025 Google LLC */ +#include #include #include #include @@ -492,6 +493,19 @@ int landlock_restrict_sibling_threads(const struct cred *old_cred, size_t newly_discovered_threads; bool found_more_threads; + /* + * Serialize concurrent TSYNC operations to prevent deadlocks when + * multiple threads call landlock_restrict_self() simultaneously. If + * another operation got there first, gracefully yield by restarting the + * syscall. This allows the current thread to process pending + * task_works before retrying. + * + * The core reservation does not hold a lock across the wait below. + */ + ACQUIRE(task_cred_sync, reservation)(); + if (ACQUIRE_ERR(task_cred_sync, &reservation)) + return restart_syscall(); + atomic_set(&shared_ctx.preparation_error, 0); init_completion(&shared_ctx.all_prepared); init_completion(&shared_ctx.ready_to_commit); @@ -503,16 +517,6 @@ int landlock_restrict_sibling_threads(const struct cred *old_cred, (restrict_flags & LANDLOCK_RESTRICT_SELF_NO_NEW_PRIVS) || task_no_new_privs(current); - /* - * Serialize concurrent TSYNC operations to prevent deadlocks when - * multiple threads call landlock_restrict_self() simultaneously. - * If the lock is already held, we gracefully yield by restarting the - * syscall. This allows the current thread to process pending - * task_works before retrying. - */ - if (!down_write_trylock(¤t->signal->exec_update_lock)) - return restart_syscall(); - /* * We schedule a pseudo-signal task_work for each of the calling task's * sibling threads. In the task work, each thread: @@ -536,8 +540,9 @@ int landlock_restrict_sibling_threads(const struct cred *old_cred, * 5) signals that it's done altogether (barrier synchronization * "all_finished") * - * Unlike seccomp, which modifies sibling tasks directly, we do not - * need to acquire the cred_guard_mutex and sighand->siglock: + * Unlike seccomp, which modifies sibling tasks directly, we do not need + * to hold the cred_guard_mutex or sighand->siglock around the thread + * walk: * * - As in our case, all threads are themselves exchanging their own * struct cred through the credentials API, no locks are needed for @@ -634,6 +639,95 @@ int landlock_restrict_sibling_threads(const struct cred *old_cred, wait_for_completion(&shared_ctx.all_finished); tsync_works_release(&works); - up_write(¤t->signal->exec_update_lock); return atomic_read(&shared_ctx.preparation_error); } + +#ifdef CONFIG_SECURITY_LANDLOCK_KUNIT_TEST + +static int try_test_cred_sync(void) +{ + ACQUIRE(task_cred_sync, reservation)(); + return ACQUIRE_ERR(task_cred_sync, &reservation); +} + +static void test_tsync_marker_free(struct kunit *const test) +{ + const struct cred *const cred = current_cred(); + + KUNIT_ASSERT_TRUE(test, thread_group_empty(current)); + KUNIT_ASSERT_EQ(test, 0, try_test_cred_sync()); + + /* Takes and releases the reservation. */ + KUNIT_EXPECT_EQ(test, 0, + landlock_restrict_sibling_threads(cred, cred, 0)); + KUNIT_EXPECT_EQ(test, 0, try_test_cred_sync()); +} + +static void test_tsync_marker_held(struct kunit *const test) +{ + const struct cred *const cred = current_cred(); + /* Only current can replace its own sighand. */ + struct sighand_struct *const sighand = + rcu_dereference_protected(current->sighand, true); + + KUNIT_ASSERT_TRUE(test, thread_group_empty(current)); + ACQUIRE(task_cred_sync, reservation)(); + KUNIT_ASSERT_EQ(test, 0, ACQUIRE_ERR(task_cred_sync, &reservation)); + KUNIT_EXPECT_EQ(test, -EBUSY, try_test_cred_sync()); + + /* Restarts without releasing the reservation held by another caller. */ + KUNIT_EXPECT_EQ(test, -ERESTARTNOINTR, + landlock_restrict_sibling_threads(cred, cred, 0)); + KUNIT_EXPECT_EQ(test, -EBUSY, try_test_cred_sync()); + + /* Clears TIF_SIGPENDING set by restart_syscall(). */ + scoped_guard(spinlock_irq, &sighand->siglock) + recalc_sigpending(); +} + +static void test_cred_sync_irqsave(struct kunit *const test) +{ + bool irq_off_acquire, irq_off_busy, irq_off_release; + int acquired, busy; + + if (IS_ENABLED(CONFIG_PREEMPT_RT)) + kunit_skip(test, "RT spinlocks require IRQs enabled"); + KUNIT_ASSERT_TRUE(test, thread_group_empty(current)); + KUNIT_ASSERT_FALSE(test, irqs_disabled()); + + scoped_guard(irqsave) { + { + ACQUIRE(task_cred_sync, reservation)(); + acquired = ACQUIRE_ERR(task_cred_sync, &reservation); + irq_off_acquire = irqs_disabled(); + busy = try_test_cred_sync(); + irq_off_busy = irqs_disabled(); + } + irq_off_release = irqs_disabled(); + } + + KUNIT_EXPECT_EQ(test, 0, acquired); + KUNIT_EXPECT_EQ(test, -EBUSY, busy); + KUNIT_EXPECT_TRUE(test, irq_off_acquire); + KUNIT_EXPECT_TRUE(test, irq_off_busy); + KUNIT_EXPECT_TRUE(test, irq_off_release); + KUNIT_EXPECT_FALSE(test, irqs_disabled()); +} + +static struct kunit_case test_cases[] = { + /* clang-format off */ + KUNIT_CASE(test_tsync_marker_free), + KUNIT_CASE(test_tsync_marker_held), + KUNIT_CASE(test_cred_sync_irqsave), + {} + /* clang-format on */ +}; + +static struct kunit_suite test_suite = { + .name = "landlock_tsync", + .test_cases = test_cases, +}; + +kunit_test_suite(test_suite); + +#endif /* CONFIG_SECURITY_LANDLOCK_KUNIT_TEST */ base-commit: fe6ff8d5d675c01793b9f051d16055dd4aa75244 -- 2.56.1