* [PATCH v3] landlock: Do not hold exec_update_lock across the TSYNC wait
@ 2026-12-04 11:17 Mickaël Salaün
2026-12-04 11:41 ` sashiko-bot
0 siblings, 1 reply; 2+ messages in thread
From: Mickaël Salaün @ 2026-12-04 11:17 UTC (permalink / raw)
To: Christian Brauner, Günther Noack, Linus Torvalds,
Oleg Nesterov, Tejun Heo
Cc: Mickaël Salaün, Bart Van Assche, Cen Zhang,
Günther Noack, 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, linux-security-module, stable
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 <brauner@kernel.org>
Cc: Günther Noack <gnoack@google.com>
Cc: Nickolai Zeldovich <nickolai@csail.mit.edu>
Cc: Oleg Nesterov <oleg@redhat.com>
Cc: Tejun Heo <tj@kernel.org>
Cc: Yihan Ding <dingyihan@uniontech.com>
Cc: stable@vger.kernel.org
Reported-by: Lee Seong Hyeon <ihopenre@gmail.com>
Fixes: ff88df67dbf7 ("landlock: Serialize TSYNC thread restriction")
Signed-off-by: Mickaël Salaün <mic@digikod.net>
---
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 <kunit/test.h>
#include <linux/atomic.h>
#include <linux/cleanup.h>
#include <linux/completion.h>
@@ -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
^ permalink raw reply related [flat|nested] 2+ messages in thread
* Re: [PATCH v3] landlock: Do not hold exec_update_lock across the TSYNC wait
2026-12-04 11:17 [PATCH v3] landlock: Do not hold exec_update_lock across the TSYNC wait Mickaël Salaün
@ 2026-12-04 11:41 ` sashiko-bot
0 siblings, 0 replies; 2+ messages in thread
From: sashiko-bot @ 2026-12-04 11:41 UTC (permalink / raw)
Cc: linux-security-module
> 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
> [ ... ]
> Cc: Yihan Ding <dingyihan@uniontech.com>
> Cc: stable@vger.kernel.org
> Reported-by: Lee Seong Hyeon <ihopenre@gmail.com>
> Fixes: ff88df67dbf7 ("landlock: Serialize TSYNC thread restriction")
> Signed-off-by: Mickaël Salaün <mic@digikod.net>
Sashiko has reviewed this patch and found no issues.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261204111754.1452982-1-mic@digikod.net?part=1
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-12-04 11:41 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-04 11:17 [PATCH v3] landlock: Do not hold exec_update_lock across the TSYNC wait Mickaël Salaün
2026-12-04 11:41 ` sashiko-bot
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox