security/landlock/tsync.c
Source file repositories/reference/linux-study-clean/security/landlock/tsync.c
File Facts
- System
- Linux kernel
- Corpus path
security/landlock/tsync.c- Extension
.c- Size
- 17226 bytes
- Lines
- 620
- Domain
- Core OS
- Bucket
- Security And Isolation
- Inferred role
- Core OS: implementation source
- Status
- source implementation candidate
Why This File Exists
Core operating-system implementation surface: boot, tasks, memory, VFS, syscall-facing interfaces, synchronization, credentials, and isolation.
- Core operating-system implementation surface: boot, tasks, memory, VFS, syscall-facing interfaces, synchronization, credentials, and isolation.
- Uses kernel synchronization; read lock ordering, sleepability, and interrupt context assumptions before translating.
- Allocates kernel memory; connect allocation flags and lifetime to context constraints.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
linux/atomic.hlinux/cleanup.hlinux/completion.hlinux/cred.hlinux/errno.hlinux/overflow.hlinux/rcupdate.hlinux/sched.hlinux/sched/signal.hlinux/sched/task.hlinux/slab.hlinux/task_work.hcred.htsync.h
Detected Declarations
struct tsync_shared_contextstruct tsync_workstruct tsync_worksfunction processfunction restrict_one_thread_callbackfunction tsync_works_trimfunction tsync_works_grow_byfunction tsync_works_contains_taskfunction tsync_works_releasefunction count_additional_threadsfunction for_each_threadfunction schedule_task_workfunction for_each_threadfunction cancel_tsync_worksfunction landlock_restrict_sibling_threads
Annotated Snippet
struct tsync_shared_context {
/* The old and tentative new creds of the calling thread. */
const struct cred *old_cred;
const struct cred *new_cred;
/* True if sibling tasks need to set the no_new_privs flag. */
bool set_no_new_privs;
/* An error encountered in preparation step, or 0. */
atomic_t preparation_error;
/*
* Barrier after preparation step in restrict_one_thread.
* The calling thread waits for completion.
*
* Re-initialized on every round of looking for newly spawned threads.
*/
atomic_t num_preparing;
struct completion all_prepared;
/* Sibling threads wait for completion. */
struct completion ready_to_commit;
/*
* Barrier after commit step (used by syscall impl to wait for
* completion).
*/
atomic_t num_unfinished;
struct completion all_finished;
};
struct tsync_work {
struct callback_head work;
struct task_struct *task;
struct tsync_shared_context *shared_ctx;
};
/*
* restrict_one_thread - update a thread's Landlock domain in lockstep with the
* other threads in the same process
*
* When this is run, the same function gets run in all other threads in the same
* process (except for the calling thread which called landlock_restrict_self).
* The concurrently running invocations of restrict_one_thread coordinate
* through the shared ctx object to do their work in lockstep to implement
* all-or-nothing semantics for enforcing the new Landlock domain.
*
* Afterwards, depending on the presence of an error, all threads either commit
* or abort the prepared credentials. The commit operation can not fail any
* more.
*/
static void restrict_one_thread(struct tsync_shared_context *ctx)
{
int err;
struct cred *cred = NULL;
if (current_cred() == ctx->old_cred) {
/*
* Switch out old_cred with new_cred, if possible.
*
* In the common case, where all threads initially point to the
* same struct cred, this optimization avoids creating separate
* redundant credentials objects for each, which would all have
* the same contents.
*
* Note: We are intentionally dropping the const qualifier
* here, because it is required by commit_creds() and
* abort_creds().
*/
cred = (struct cred *)get_cred(ctx->new_cred);
} else {
/* Else, prepare new creds and populate them. */
cred = prepare_creds();
if (!cred) {
atomic_set(&ctx->preparation_error, -ENOMEM);
/*
* Even on error, we need to adhere to the protocol and
* coordinate with concurrently running invocations.
*/
if (atomic_dec_return(&ctx->num_preparing) == 0)
complete_all(&ctx->all_prepared);
goto out;
}
landlock_cred_copy(landlock_cred(cred),
landlock_cred(ctx->new_cred));
}
Annotation
- Immediate include surface: `linux/atomic.h`, `linux/cleanup.h`, `linux/completion.h`, `linux/cred.h`, `linux/errno.h`, `linux/overflow.h`, `linux/rcupdate.h`, `linux/sched.h`.
- Detected declarations: `struct tsync_shared_context`, `struct tsync_work`, `struct tsync_works`, `function process`, `function restrict_one_thread_callback`, `function tsync_works_trim`, `function tsync_works_grow_by`, `function tsync_works_contains_task`, `function tsync_works_release`, `function count_additional_threads`.
- Atlas domain: Core OS / Security And Isolation.
- Implementation status: source implementation candidate.
- Synchronization appears in or near this file; preserve lock ordering, sleepability, and interrupt-context constraints.
Implementation Notes
- This generated page is the file-by-file coverage layer; curated subsystem chapters should link here when they synthesize a multi-file control flow.
- Core OS pages should be promoted from atlas-only to deep-reviewed when they explain data structures, invariants, locking, lifecycle, and C implementation snippets.
- Driver-family pages are intentionally pattern-oriented unless they are part of the selected PCIe/NVMe representative device path.