landlock: Do not hold exec_update_lock across the TSYNC wait
Subsystems (3)
Mickaël Salaün
maintainerPATCH 1/1 landlock: Do not hold exec_update_lock across the TSYNC wait
landlock_restrict_sibling_threads() write-locks current->signal->exec_update_lock to serialize TSYNC callers, and holds it until every sibling thread has run its task_work. This wait is
unbounded: TWA_SIGNAL does not wake a TASK_KILLABLE sleeper, so a sibling blocked as a vfork() parent runs it only once its child execs or exits.
Meanwhile, readers of this lock block, mostly in D state:
/proc/<pid>/stat, mm_access(), pidfd_getfd(), kcmp(), perf_event_open(), etc. An unprivileged process can thus stall tools run by other users, including root, such as ps and top.
Only a per-process "TSYNC in progress" marker is needed. Use an unused signal_struct.flags bit, protected by sighand->siglock, which is held only to set or clear the marker. A second caller still restarts the syscall, and a cleanup class releases the marker on every return.
Like de_thread(), treat a group exit or an ongoing exec as a held
marker: a group exit may overwrite the flags, and both kill the other threads, which the restart lets the caller handle.
Add KUnit tests for taking a free marker and for rejecting a held one. A concurrent TSYNC caller cannot be started deterministically from user space, but a KUnit thread has no sibling and can hold the marker itself.
---
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 and Suggested-by instead of authorship, because the implementation was rewritten.
Christian, Oleg, and Tejun, this patch reserves a signal_struct.flags bit for Landlock and relies on group-exit paths overwriting these flags under siglock. Please check that this use of signal-core state is acceptable.
Lee, this rewrites the patch you sent with your report, so it credits you with Reported-by and Suggested-by. Please tell me if you prefer a different credit.
--- include/linux/sched/signal.h | 1 + security/landlock/tsync.c | 134 +++++++++++++++++++++++++++++++---- 2 files changed, 122 insertions(+), 13 deletions(-)
include/linux/sched/signal.h +1 -0
@@ -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_LANDLOCK_TSYNC 0x00000008/* Landlock TSYNC in progress */ /* * Pending notifications to parent. */
security/landlock/tsync.c +121 -13
169 lines not on this page · raw message
base-commit: aebb7274476403040d582c61478df8f4a7bf9cd3
Signature · 1 line
2.56.0
[email protected]
AI reviewSashiko has reviewed this patch and found no issues.
Quoted 15 lines
landlock_restrict_sibling_threads()write-lockscurrent->signal->exec_update_lockto serialize TSYNC callers, and holds it until every sibling thread has run itstask_work. This wait is
unbounded:TWA_SIGNALdoes not wake aTASK_KILLABLEsleeper, so a sibling blocked as avfork()parent runs it only once its child execs or exits.Meanwhile, readers of this lock block, mostly in D state:
/proc/<pid>/stat,mm_access(),pidfd_getfd(),kcmp(),perf_event_open(), etc. An unprivileged process can thus stall tools run by other users, including root, such as ps and top.Only a per-process "TSYNC in progress" marker is needed. Use an unused signal_struct.flags bit, protected by
sighand->siglock, which is held only to set or clear the marker. A second caller still restarts the
Quoted 6 lines
[ ... ]Reported-by Lee Seong Hyeon <[email protected]>Suggested-by Lee Seong Hyeon <[email protected]>Fixes ff88df67dbf7 ("landlock: Serialize TSYNC thread restriction")Signed-off-by Mickaël Salaün <[email protected]>
Sashiko has reviewed this patch and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Quoted 38 lines · Mickaël Salaün
landlock_restrict_sibling_threads()write-lockscurrent->signal->exec_update_lockto serialize TSYNC callers, and holds it until every sibling thread has run itstask_work. This wait is
unbounded:TWA_SIGNALdoes not wake aTASK_KILLABLEsleeper, so a sibling blocked as avfork()parent runs it only once its child execs or exits.Meanwhile, readers of this lock block, mostly in D state:
/proc/<pid>/stat,mm_access(),pidfd_getfd(),kcmp(),perf_event_open(), etc. An unprivileged process can thus stall tools run by other users, including root, such as ps and top.Only a per-process "TSYNC in progress" marker is needed. Use an unused signal_struct.flags bit, protected by
sighand->siglock, which is held only to set or clear the marker. A second caller still restarts the syscall, and a cleanup class releases the marker on every return.Like
de_thread(), treat a group exit or an ongoing exec as a held
marker: a group exit may overwrite the flags, and both kill the other threads, which the restart lets the caller handle.Add KUnit tests for taking a free marker and for rejecting a held one. A concurrent TSYNC caller cannot be started deterministically from user space, but a KUnit thread has no sibling and can hold the marker itself.
Cc Christian Brauner <[email protected]>Cc Günther Noack <[email protected]>Cc Nickolai Zeldovich <[email protected]>Cc Oleg Nesterov <[email protected]>Cc Tejun Heo <[email protected]>Cc Yihan Ding <[email protected]>Reported-by Lee Seong Hyeon <[email protected]>Suggested-by Lee Seong Hyeon <[email protected]>Fixes ff88df67dbf7 ("landlock: Serialize TSYNC thread restriction")Signed-off-by Mickaël Salaün <[email protected]>---
Quoted 7 lines
Changes since the initial report from Lee Seong Hyeon:
- Rewrite the fix, storing the marker in an unused signal_struct.flags bit protected bysighand->siglockinstead of adding asignal_structfield.
- Treat group exit and ongoing exec as a held marker, because group exit may overwrite the flags.
- Release the marker with a cleanup class.
It's not clear to me why the cleanup class and the fix are bundled in one commit here. (I do find the cleanup class useful, but shouldn't a bugfix commit be as minimal as possible so that it is easy for people to apply to their downstream kernels?)
–Günther
Quoted 215 lines
- Clarify that not all readers wait in D state:
perf_event_open()waits interruptibly.
- Add KUnit tests.
- Credit Lee Seong Hyeon with Reported-by and Suggested-by instead of authorship, because the implementation was rewritten.Christian, Oleg, and Tejun, this patch reserves a signal_struct.flags bit for Landlock and relies on group-exit paths overwriting these flags under siglock. Please check that this use of signal-core state is acceptable.
Lee, this rewrites the patch you sent with your report, so it credits you with Reported-by and Suggested-by. Please tell me if you prefer a different credit.
--- include/linux/sched/signal.h | 1 + security/landlock/tsync.c | 134 +++++++++++++++++++++++++++++++---- 2 files changed, 122 insertions(+), 13 deletions(-)include/linux/sched/signal.h +1
-0@@ -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_LANDLOCK_TSYNC 0x00000008/* Landlock TSYNC in progress */ /* * Pending notifications to parent. */ security/landlock/tsync.c +121
-13@@ -5,6 +5,7 @@* Copyright © 2025 Google LLC */ +#include <kunit/test.h> #include <linux/atomic.h> #include <linux/cleanup.h> #include <linux/completion.h> @@ -479,6 +480,46 @@ static void cancel_tsync_works(const struct tsync_works *works, } } +/* +* try_set_current_tsync_marker - mark the current thread group as under TSYNC +* +* Returns false if another TSYNC operation, group exit, or exec is in progress. +*/ +static bool try_set_current_tsync_marker (void ) +{ +struct signal_struct *const signal = current->signal; + +guard (spinlock_irq)(¤t->sighand->siglock); + +/* +* Group-exit paths may replace all signal flags, including an active +* marker. Treat SIGNAL_GROUP_EXIT as a held marker to prevent another +* TSYNC operation from starting during group teardown. +* +* Like de_thread(), also treat an ongoing exec as a group action in +* progress. de_thread() sets group_exec_task in the same siglock +* critical section that sends SIGKILL to all other threads, including +* current. Instead of queuing task_work for exiting threads, +* restart_syscall() lets current handle its pending SIGKILL. +*/ +if ((signal->flags & (SIGNAL_GROUP_EXIT |SIGNAL_LANDLOCK_TSYNC )) || + signal->group_exec_task) +return false; + + signal->flags |=SIGNAL_LANDLOCK_TSYNC ; +return true; +} + +static void clear_current_tsync_marker (void ) +{ +guard (spinlock_irq)(¤t->sighand->siglock); + + current->signal->flags &= ~SIGNAL_LANDLOCK_TSYNC ; +} + +DEFINE_CLASS(current_tsync_marker, bool, if (_T) clear_current_tsync_marker(), +try_set_current_tsync_marker (), void) +/* * restrict_sibling_threads - enables a Landlock policy for all sibling threads */ @@ -492,6 +533,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. +* +* sighand->siglock protects only the marker, not the wait below. +*/ +CLASS (current_tsync_marker, marker)(); +if (!marker) +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 +557,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 +580,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 +679,69 @@ 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 bool is_current_tsync_marker_set (void ) +{ +return current->signal->flags &SIGNAL_LANDLOCK_TSYNC ; +} + +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_FALSE (test,is_current_tsync_marker_set ()); + +KUNIT_EXPECT_TRUE (test,try_set_current_tsync_marker ()); +KUNIT_EXPECT_TRUE (test,is_current_tsync_marker_set ()); +clear_current_tsync_marker (); +KUNIT_EXPECT_FALSE (test,is_current_tsync_marker_set ()); + +/* Takes and releases the marker. */ +KUNIT_EXPECT_EQ (test,0 , +landlock_restrict_sibling_threads (cred, cred,0 )); +KUNIT_EXPECT_FALSE (test,is_current_tsync_marker_set ()); +} + +static void test_tsync_marker_held (struct kunit *const test) +{ +const struct cred *const cred =current_cred (); + +KUNIT_ASSERT_TRUE (test,thread_group_empty (current)); +KUNIT_ASSERT_TRUE (test,try_set_current_tsync_marker ()); + +KUNIT_EXPECT_FALSE (test,try_set_current_tsync_marker ()); +KUNIT_EXPECT_TRUE (test,is_current_tsync_marker_set ()); + +/* Restarts without releasing the marker held by another caller. */ +KUNIT_EXPECT_EQ (test, -ERESTARTNOINTR , +landlock_restrict_sibling_threads (cred, cred,0 )); +KUNIT_EXPECT_TRUE (test,is_current_tsync_marker_set ()); + +/* Clears TIF_SIGPENDING set by restart_syscall(). */ + scoped_guard(spinlock_irq, ¤t->sighand->siglock) +recalc_sigpending (); + +clear_current_tsync_marker (); +} + +static struct kunit_case test_cases[] = { +/* clang-format off */ +KUNIT_CASE (test_tsync_marker_free), +KUNIT_CASE (test_tsync_marker_held), + {} +/* 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:
aebb7274476403040d582c61478df8f4a7bf9cd3Signature · 1 line
2.56.0
Mickaël Salaün
maintainerQuoted 47 lines · Günther Noack
Quoted 38 lines · Mickaël Salaün
landlock_restrict_sibling_threads()write-lockscurrent->signal->exec_update_lockto serialize TSYNC callers, and holds it until every sibling thread has run itstask_work. This wait is
unbounded:TWA_SIGNALdoes not wake aTASK_KILLABLEsleeper, so a sibling blocked as avfork()parent runs it only once its child execs or exits.Meanwhile, readers of this lock block, mostly in D state:
/proc/<pid>/stat,mm_access(),pidfd_getfd(),kcmp(),perf_event_open(), etc. An unprivileged process can thus stall tools run by other users, including root, such as ps and top.Only a per-process "TSYNC in progress" marker is needed. Use an unused signal_struct.flags bit, protected by
sighand->siglock, which is held only to set or clear the marker. A second caller still restarts the syscall, and a cleanup class releases the marker on every return.Like
de_thread(), treat a group exit or an ongoing exec as a held
marker: a group exit may overwrite the flags, and both kill the other threads, which the restart lets the caller handle.Add KUnit tests for taking a free marker and for rejecting a held one. A concurrent TSYNC caller cannot be started deterministically from user space, but a KUnit thread has no sibling and can hold the marker itself.
Cc Christian Brauner <[email protected]>Cc Günther Noack <[email protected]>Cc Nickolai Zeldovich <[email protected]>Cc Oleg Nesterov <[email protected]>Cc Tejun Heo <[email protected]>Cc Yihan Ding <[email protected]>Reported-by Lee Seong Hyeon <[email protected]>Suggested-by Lee Seong Hyeon <[email protected]>Fixes ff88df67dbf7 ("landlock: Serialize TSYNC thread restriction")Signed-off-by Mickaël Salaün <[email protected]>---
Quoted 7 lines
Changes since the initial report from Lee Seong Hyeon:
- Rewrite the fix, storing the marker in an unused signal_struct.flags bit protected bysighand->siglockinstead of adding asignal_structfield.
- Treat group exit and ongoing exec as a held marker, because group exit may overwrite the flags.
- Release the marker with a cleanup class.
Quoted 4 lines
It's not clear to me why the cleanup class and the fix are bundled in one commit here. (I do find the cleanup class useful, but shouldn't a bugfix commit be as minimal as possible so that it is easy for people to apply to their downstream kernels?)
I included the cleanup class because we needed to change the up_write() call anyway, so it's a good opportunity to use an automatic cleanup mechanism instead of replacing it with an explicit lock + flag change.
I also moved the initial check before the initialization because it makes more sense there and it was a good opportunity to do it now (I updated the comment as well).
This extra cleanup doesn't have any impact on backporting.
Quoted 219 lines
–Günther
Quoted 215 lines
- Clarify that not all readers wait in D state:
perf_event_open()waits interruptibly.
- Add KUnit tests.
- Credit Lee Seong Hyeon with Reported-by and Suggested-by instead of authorship, because the implementation was rewritten.Christian, Oleg, and Tejun, this patch reserves a signal_struct.flags bit for Landlock and relies on group-exit paths overwriting these flags under siglock. Please check that this use of signal-core state is acceptable.
Lee, this rewrites the patch you sent with your report, so it credits you with Reported-by and Suggested-by. Please tell me if you prefer a different credit.
--- include/linux/sched/signal.h | 1 + security/landlock/tsync.c | 134 +++++++++++++++++++++++++++++++---- 2 files changed, 122 insertions(+), 13 deletions(-)include/linux/sched/signal.h +1
-0@@ -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_LANDLOCK_TSYNC 0x00000008/* Landlock TSYNC in progress */ /* * Pending notifications to parent. */ security/landlock/tsync.c +121
-13@@ -5,6 +5,7 @@* Copyright © 2025 Google LLC */ +#include <kunit/test.h> #include <linux/atomic.h> #include <linux/cleanup.h> #include <linux/completion.h> @@ -479,6 +480,46 @@ static void cancel_tsync_works(const struct tsync_works *works, } } +/* +* try_set_current_tsync_marker - mark the current thread group as under TSYNC +* +* Returns false if another TSYNC operation, group exit, or exec is in progress. +*/ +static bool try_set_current_tsync_marker (void ) +{ +struct signal_struct *const signal = current->signal; + +guard (spinlock_irq)(¤t->sighand->siglock); + +/* +* Group-exit paths may replace all signal flags, including an active +* marker. Treat SIGNAL_GROUP_EXIT as a held marker to prevent another +* TSYNC operation from starting during group teardown. +* +* Like de_thread(), also treat an ongoing exec as a group action in +* progress. de_thread() sets group_exec_task in the same siglock +* critical section that sends SIGKILL to all other threads, including +* current. Instead of queuing task_work for exiting threads, +* restart_syscall() lets current handle its pending SIGKILL. +*/ +if ((signal->flags & (SIGNAL_GROUP_EXIT |SIGNAL_LANDLOCK_TSYNC )) || + signal->group_exec_task) +return false; + + signal->flags |=SIGNAL_LANDLOCK_TSYNC ; +return true; +} + +static void clear_current_tsync_marker (void ) +{ +guard (spinlock_irq)(¤t->sighand->siglock); + + current->signal->flags &= ~SIGNAL_LANDLOCK_TSYNC ; +} + +DEFINE_CLASS(current_tsync_marker, bool, if (_T) clear_current_tsync_marker(), +try_set_current_tsync_marker (), void) +/* * restrict_sibling_threads - enables a Landlock policy for all sibling threads */ @@ -492,6 +533,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. +* +* sighand->siglock protects only the marker, not the wait below. +*/ +CLASS (current_tsync_marker, marker)(); +if (!marker) +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 +557,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 +580,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 +679,69 @@ 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 bool is_current_tsync_marker_set (void ) +{ +return current->signal->flags &SIGNAL_LANDLOCK_TSYNC ; +} + +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_FALSE (test,is_current_tsync_marker_set ()); + +KUNIT_EXPECT_TRUE (test,try_set_current_tsync_marker ()); +KUNIT_EXPECT_TRUE (test,is_current_tsync_marker_set ()); +clear_current_tsync_marker (); +KUNIT_EXPECT_FALSE (test,is_current_tsync_marker_set ()); + +/* Takes and releases the marker. */ +KUNIT_EXPECT_EQ (test,0 , +landlock_restrict_sibling_threads (cred, cred,0 )); +KUNIT_EXPECT_FALSE (test,is_current_tsync_marker_set ()); +} + +static void test_tsync_marker_held (struct kunit *const test) +{ +const struct cred *const cred =current_cred (); + +KUNIT_ASSERT_TRUE (test,thread_group_empty (current)); +KUNIT_ASSERT_TRUE (test,try_set_current_tsync_marker ()); + +KUNIT_EXPECT_FALSE (test,try_set_current_tsync_marker ()); +KUNIT_EXPECT_TRUE (test,is_current_tsync_marker_set ()); + +/* Restarts without releasing the marker held by another caller. */ +KUNIT_EXPECT_EQ (test, -ERESTARTNOINTR , +landlock_restrict_sibling_threads (cred, cred,0 )); +KUNIT_EXPECT_TRUE (test,is_current_tsync_marker_set ()); + +/* Clears TIF_SIGPENDING set by restart_syscall(). */ + scoped_guard(spinlock_irq, ¤t->sighand->siglock) +recalc_sigpending (); + +clear_current_tsync_marker (); +} + +static struct kunit_case test_cases[] = { +/* clang-format off */ +KUNIT_CASE (test_tsync_marker_free), +KUNIT_CASE (test_tsync_marker_held), + {} +/* 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:
aebb7274476403040d582c61478df8f4a7bf9cd3Signature · 1 line
2.56.0
Quoted 24 lines · Mickaël Salaün
security/landlock/tsync.c +13
-0@@ -492,6 +533,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. +* +* sighand->siglock protects only the marker, not the wait below. +*/ +CLASS (current_tsync_marker, marker)(); +if (!marker) +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 +557,6 @@ int landlock_restrict_sibling_threads(const struct cred *old_cred, (restrict_flags &LANDLOCK_RESTRICT_SELF_NO_NEW_PRIVS ) ||task_no_new_privs (current);
security/landlock/tsync.c int landlock_restrict_sibling_threads(const struct cred *old_cred, · line 514
Quoted 10 lines
security/landlock/tsync.c +0
-9-/* -* 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 ();
Just in case, the patch looks obviously correct, so feel free to add
-------------------------------------------------------------------------------
But when I look at this code...
Well, if we change landlock_restrict_sibling_threads() to rely on ->siglock, it seems that it can be greatly simplified? I mean, it can roughly do:
...
for (nt nr_threads = 0;; tsync_works_grow_by(&works, nr_threads))
{
guard(spinlock_irq)(¤t->sighand->siglock);
if (!nr_threads) {
if (signal->flags & SIGNAL_LANDLOCK_TSYNC || ...)
return restart_syscall();
else
signal->flags |= SIGNAL_LANDLOCK_TSYNC;
}
if (nr_threads < get_nr_threads(current)) {
nr_threads = get_nr_threads(current));
continue;
}
for_other_threads(current, thread) {
if (thread->flags & PF_EXITING) // stable under siglock
continue;
ctx = tsync_works_provide(...); // can't fail
...
task_work_add(thread, &ctx->work, TWA_SIGNAL); // can't fail
}
break;
}
complete_all(&shared_ctx.ready_to_commit);
...We can kill tsync_works_trim/tsync_works_contains_task/count_additional_threads/etc.
Sure, this needs another change even if I am right.
Oleg.
Mickaël Salaün
maintainerQuoted 38 lines · Oleg Nesterov
Quoted 24 lines · Mickaël Salaün
security/landlock/tsync.c +13
-0@@ -492,6 +533,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. +* +* sighand->siglock protects only the marker, not the wait below. +*/ +CLASS (current_tsync_marker, marker)(); +if (!marker) +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 +557,6 @@ int landlock_restrict_sibling_threads(const struct cred *old_cred, (restrict_flags &LANDLOCK_RESTRICT_SELF_NO_NEW_PRIVS ) ||task_no_new_privs (current);security/landlock/tsync.c int landlock_restrict_sibling_threads(const struct cred *old_cred, · line 514
Quoted 10 lines
security/landlock/tsync.c +0
-9-/* -* 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 ();Just in case, the patch looks obviously correct, so feel free to add
Quoted 1 line
Reviewed-by Oleg Nesterov <[email protected]>
Thanks for the review.
Quoted 42 lines
-------------------------------------------------------------------------------
But when I look at this code...Well, if we change
landlock_restrict_sibling_threads()to rely on ->siglock, it seems that it can be greatly simplified? I mean, it can roughly do:... for (nt nr_threads = 0;; tsync_works_grow_by(&works, nr_threads)) { guard(spinlock_irq)(¤t->sighand->siglock); if (!nr_threads) { if (signal->flags & SIGNAL_LANDLOCK_TSYNC || ...) return restart_syscall(); else signal->flags |= SIGNAL_LANDLOCK_TSYNC; } if (nr_threads < get_nr_threads(current)) { nr_threads = get_nr_threads(current)); continue; } for_other_threads(current, thread) { if (thread->flags & PF_EXITING) // stable under siglock continue; ctx = tsync_works_provide(...); // can't fail ... task_work_add(thread, &ctx->work, TWA_SIGNAL); // can't fail } break; } complete_all(&shared_ctx.ready_to_commit); ...
Quoted 1 line
We can kill tsync_works_trim/tsync_works_contains_task/count_additional_threads/etc.
That would indeed be simpler, but a single pass would misses threads that a sibling is creating at the same time: the cred is copied before taking siglock and only fatal_signal_pending is checked.
Keeping the rescan loop and walking under siglock would be correct and would drop tsync_works_trim() and count_additional_threads(). However, it would hold siglock with IRQ disabled across a task_work_add() for every thread, which /proc/<pid>/stat readers would then wait for. That is bounded and not worse than what SIGCONT already does, but the RCU walk avoids it entirely, so I'm wondering if it's worth it.
Quoted 2 lines
Sure, this needs another change even if I am right.
Yes, I'll send a follow-up if needed.
Quoted 4 lines
Oleg.
Quoted 4 lines · Mickaël Salaün
That would indeed be simpler, but a single pass would misses threads that a sibling is creating at the same time: the cred is copied before taking siglock and only
fatal_signal_pendingis checked.
Aaah, I see. Thanks for your explanation!
Oleg.