1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Landlock - Cross-thread ruleset enforcement 4 * 5 * Copyright © 2025 Google LLC 6 */ 7 8 #include <linux/atomic.h> 9 #include <linux/cleanup.h> 10 #include <linux/completion.h> 11 #include <linux/cred.h> 12 #include <linux/errno.h> 13 #include <linux/overflow.h> 14 #include <linux/rcupdate.h> 15 #include <linux/sched.h> 16 #include <linux/sched/signal.h> 17 #include <linux/sched/task.h> 18 #include <linux/slab.h> 19 #include <linux/task_work.h> 20 21 #include "cred.h" 22 #include "tsync.h" 23 24 /* 25 * Shared state between multiple threads which are enforcing Landlock rulesets 26 * in lockstep with each other. 27 */ 28 struct tsync_shared_context { 29 /* The old and tentative new creds of the calling thread. */ 30 const struct cred *old_cred; 31 const struct cred *new_cred; 32 33 /* True if sibling tasks need to set the no_new_privs flag. */ 34 bool set_no_new_privs; 35 36 /* An error encountered in preparation step, or 0. */ 37 atomic_t preparation_error; 38 39 /* 40 * Barrier after preparation step in restrict_one_thread. 41 * The calling thread waits for completion. 42 * 43 * Re-initialized on every round of looking for newly spawned threads. 44 */ 45 atomic_t num_preparing; 46 struct completion all_prepared; 47 48 /* Sibling threads wait for completion. */ 49 struct completion ready_to_commit; 50 51 /* 52 * Barrier after commit step (used by syscall impl to wait for 53 * completion). 54 */ 55 atomic_t num_unfinished; 56 struct completion all_finished; 57 }; 58 59 struct tsync_work { 60 struct callback_head work; 61 struct task_struct *task; 62 struct tsync_shared_context *shared_ctx; 63 }; 64 65 /* 66 * restrict_one_thread - update a thread's Landlock domain in lockstep with the 67 * other threads in the same process 68 * 69 * When this is run, the same function gets run in all other threads in the same 70 * process (except for the calling thread which called landlock_restrict_self). 71 * The concurrently running invocations of restrict_one_thread coordinate 72 * through the shared ctx object to do their work in lockstep to implement 73 * all-or-nothing semantics for enforcing the new Landlock domain. 74 * 75 * Afterwards, depending on the presence of an error, all threads either commit 76 * or abort the prepared credentials. The commit operation can not fail any 77 * more. 78 */ 79 static void restrict_one_thread(struct tsync_shared_context *ctx) 80 { 81 int err; 82 struct cred *cred = NULL; 83 84 if (current_cred() == ctx->old_cred) { 85 /* 86 * Switch out old_cred with new_cred, if possible. 87 * 88 * In the common case, where all threads initially point to the same 89 * struct cred, this optimization avoids creating separate redundant 90 * credentials objects for each, which would all have the same contents. 91 * 92 * Note: We are intentionally dropping the const qualifier here, because 93 * it is required by commit_creds() and abort_creds(). 94 */ 95 cred = (struct cred *)get_cred(ctx->new_cred); 96 } else { 97 /* Else, prepare new creds and populate them. */ 98 cred = prepare_creds(); 99 100 if (!cred) { 101 atomic_set(&ctx->preparation_error, -ENOMEM); 102 103 /* 104 * Even on error, we need to adhere to the protocol and coordinate 105 * with concurrently running invocations. 106 */ 107 if (atomic_dec_return(&ctx->num_preparing) == 0) 108 complete_all(&ctx->all_prepared); 109 110 goto out; 111 } 112 113 landlock_cred_copy(landlock_cred(cred), 114 landlock_cred(ctx->new_cred)); 115 } 116 117 /* 118 * Barrier: Wait until all threads are done preparing. 119 * After this point, we can have no more failures. 120 */ 121 if (atomic_dec_return(&ctx->num_preparing) == 0) 122 complete_all(&ctx->all_prepared); 123 124 /* 125 * Wait for signal from calling thread that it's safe to read the 126 * preparation error now and we are ready to commit (or abort). 127 */ 128 wait_for_completion(&ctx->ready_to_commit); 129 130 /* Abort the commit if any of the other threads had an error. */ 131 err = atomic_read(&ctx->preparation_error); 132 if (err) { 133 abort_creds(cred); 134 goto out; 135 } 136 137 /* 138 * Make sure that all sibling tasks fulfill the no_new_privs prerequisite. 139 * (This is in line with Seccomp's SECCOMP_FILTER_FLAG_TSYNC logic in 140 * kernel/seccomp.c) 141 */ 142 if (ctx->set_no_new_privs) 143 task_set_no_new_privs(current); 144 145 commit_creds(cred); 146 147 out: 148 /* Notify the calling thread once all threads are done */ 149 if (atomic_dec_return(&ctx->num_unfinished) == 0) 150 complete_all(&ctx->all_finished); 151 } 152 153 /* 154 * restrict_one_thread_callback - task_work callback for restricting a thread 155 * 156 * Calls restrict_one_thread with the struct landlock_shared_tsync_context. 157 */ 158 static void restrict_one_thread_callback(struct callback_head *work) 159 { 160 struct tsync_work *ctx = container_of(work, struct tsync_work, work); 161 162 restrict_one_thread(ctx->shared_ctx); 163 } 164 165 /* 166 * struct tsync_works - a growable array of per-task contexts 167 * 168 * The zero-initialized struct represents the empty array. 169 */ 170 struct tsync_works { 171 struct tsync_work **works; 172 size_t size; 173 size_t capacity; 174 }; 175 176 /* 177 * tsync_works_provide - provides a preallocated tsync_work for the given task 178 * 179 * This also stores a task pointer in the context and increments the reference 180 * count of the task. 181 * 182 * This function may fail in the case where we did not preallocate sufficient 183 * capacity. This can legitimately happen if new threads get started after we 184 * grew the capacity. 185 * 186 * Returns: 187 * A pointer to the preallocated context struct, with task filled in. 188 * 189 * NULL, if we ran out of preallocated context structs. 190 */ 191 static struct tsync_work *tsync_works_provide(struct tsync_works *s, 192 struct task_struct *task) 193 { 194 struct tsync_work *ctx; 195 196 if (s->size >= s->capacity) 197 return NULL; 198 199 ctx = s->works[s->size]; 200 s->size++; 201 202 ctx->task = get_task_struct(task); 203 return ctx; 204 } 205 206 /** 207 * tsync_works_trim - Put the last tsync_work element 208 * 209 * @s: TSYNC works to trim. 210 * 211 * Put the last task and decrement the size of @s. 212 * 213 * This helper does not cancel a running task, but just reset the last element 214 * to zero. 215 */ 216 static void tsync_works_trim(struct tsync_works *s) 217 { 218 struct tsync_work *ctx; 219 220 if (WARN_ON_ONCE(s->size <= 0)) 221 return; 222 223 ctx = s->works[s->size - 1]; 224 225 /* 226 * For consistency, remove the task from ctx so that it does not look like 227 * we handed it a task_work. 228 */ 229 put_task_struct(ctx->task); 230 *ctx = (typeof(*ctx)){}; 231 232 /* 233 * Cancel the tsync_works_provide() change to recycle the reserved memory 234 * for the next thread, if any. This also ensures that cancel_tsync_works() 235 * and tsync_works_release() do not see any NULL task pointers. 236 */ 237 s->size--; 238 } 239 240 /* 241 * tsync_works_grow_by - preallocates space for n more contexts in s 242 * 243 * On a successful return, the subsequent n calls to tsync_works_provide() are 244 * guaranteed to succeed. (size + n <= capacity) 245 * 246 * Returns: 247 * -ENOMEM if the (re)allocation fails 248 249 * 0 if the allocation succeeds, partially succeeds, or no reallocation 250 * was needed 251 */ 252 static int tsync_works_grow_by(struct tsync_works *s, size_t n, gfp_t flags) 253 { 254 size_t i; 255 size_t new_capacity; 256 struct tsync_work **works; 257 struct tsync_work *work; 258 259 if (check_add_overflow(s->size, n, &new_capacity)) 260 return -EOVERFLOW; 261 262 /* No need to reallocate if s already has sufficient capacity. */ 263 if (new_capacity <= s->capacity) 264 return 0; 265 266 works = krealloc_array(s->works, new_capacity, sizeof(s->works[0]), 267 flags); 268 if (!works) 269 return -ENOMEM; 270 271 s->works = works; 272 273 for (i = s->capacity; i < new_capacity; i++) { 274 work = kzalloc_obj(*work, flags); 275 if (!work) { 276 /* 277 * Leave the object in a consistent state, 278 * but return an error. 279 */ 280 s->capacity = i; 281 return -ENOMEM; 282 } 283 s->works[i] = work; 284 } 285 s->capacity = new_capacity; 286 return 0; 287 } 288 289 /* 290 * tsync_works_contains - checks for presence of task in s 291 */ 292 static bool tsync_works_contains_task(const struct tsync_works *s, 293 struct task_struct *task) 294 { 295 size_t i; 296 297 for (i = 0; i < s->size; i++) 298 if (s->works[i]->task == task) 299 return true; 300 return false; 301 } 302 303 /* 304 * tsync_works_release - frees memory held by s and drops all task references 305 * 306 * This does not free s itself, only the data structures held by it. 307 */ 308 static void tsync_works_release(struct tsync_works *s) 309 { 310 size_t i; 311 312 for (i = 0; i < s->size; i++) { 313 if (WARN_ON_ONCE(!s->works[i]->task)) 314 continue; 315 316 put_task_struct(s->works[i]->task); 317 } 318 319 for (i = 0; i < s->capacity; i++) 320 kfree(s->works[i]); 321 kfree(s->works); 322 s->works = NULL; 323 s->size = 0; 324 s->capacity = 0; 325 } 326 327 /* 328 * count_additional_threads - counts the sibling threads that are not in works 329 */ 330 static size_t count_additional_threads(const struct tsync_works *works) 331 { 332 struct task_struct *thread, *caller; 333 size_t n = 0; 334 335 caller = current; 336 337 guard(rcu)(); 338 339 for_each_thread(caller, thread) { 340 /* Skip current, since it is initiating the sync. */ 341 if (thread == caller) 342 continue; 343 344 /* Skip exited threads. */ 345 if (thread->flags & PF_EXITING) 346 continue; 347 348 /* Skip threads that we have already seen. */ 349 if (tsync_works_contains_task(works, thread)) 350 continue; 351 352 n++; 353 } 354 return n; 355 } 356 357 /* 358 * schedule_task_work - adds task_work for all eligible sibling threads 359 * which have not been scheduled yet 360 * 361 * For each added task_work, atomically increments shared_ctx->num_preparing and 362 * shared_ctx->num_unfinished. 363 * 364 * Returns: 365 * true, if at least one eligible sibling thread was found 366 */ 367 static bool schedule_task_work(struct tsync_works *works, 368 struct tsync_shared_context *shared_ctx) 369 { 370 int err; 371 struct task_struct *thread, *caller; 372 struct tsync_work *ctx; 373 bool found_more_threads = false; 374 375 caller = current; 376 377 guard(rcu)(); 378 379 for_each_thread(caller, thread) { 380 /* Skip current, since it is initiating the sync. */ 381 if (thread == caller) 382 continue; 383 384 /* Skip exited threads. */ 385 if (thread->flags & PF_EXITING) 386 continue; 387 388 /* Skip threads that we already looked at. */ 389 if (tsync_works_contains_task(works, thread)) 390 continue; 391 392 /* 393 * We found a sibling thread that is not doing its task_work yet, and 394 * which might spawn new threads before our task work runs, so we need 395 * at least one more round in the outer loop. 396 */ 397 found_more_threads = true; 398 399 ctx = tsync_works_provide(works, thread); 400 if (!ctx) { 401 /* 402 * We ran out of preallocated contexts -- we need to try again with 403 * this thread at a later time! 404 * found_more_threads is already true at this point. 405 */ 406 break; 407 } 408 409 ctx->shared_ctx = shared_ctx; 410 411 atomic_inc(&shared_ctx->num_preparing); 412 atomic_inc(&shared_ctx->num_unfinished); 413 414 init_task_work(&ctx->work, restrict_one_thread_callback); 415 err = task_work_add(thread, &ctx->work, TWA_SIGNAL); 416 if (unlikely(err)) { 417 /* 418 * task_work_add() only fails if the task is about to exit. We 419 * checked that earlier, but it can happen as a race. Resume 420 * without setting an error, as the task is probably gone in the 421 * next loop iteration. 422 */ 423 tsync_works_trim(works); 424 425 atomic_dec(&shared_ctx->num_preparing); 426 atomic_dec(&shared_ctx->num_unfinished); 427 } 428 } 429 430 return found_more_threads; 431 } 432 433 /* 434 * cancel_tsync_works - cancel all task works where it is possible 435 * 436 * Task works can be canceled as long as they are still queued and have not 437 * started running. If they get canceled, we decrement 438 * shared_ctx->num_preparing and shared_ctx->num_unfished and mark the two 439 * completions if needed, as if the task was never scheduled. 440 */ 441 static void cancel_tsync_works(struct tsync_works *works, 442 struct tsync_shared_context *shared_ctx) 443 { 444 int i; 445 446 for (i = 0; i < works->size; i++) { 447 if (WARN_ON_ONCE(!works->works[i]->task)) 448 continue; 449 450 if (!task_work_cancel(works->works[i]->task, 451 &works->works[i]->work)) 452 continue; 453 454 /* After dequeueing, act as if the task work had executed. */ 455 456 if (atomic_dec_return(&shared_ctx->num_preparing) == 0) 457 complete_all(&shared_ctx->all_prepared); 458 459 if (atomic_dec_return(&shared_ctx->num_unfinished) == 0) 460 complete_all(&shared_ctx->all_finished); 461 } 462 } 463 464 /* 465 * restrict_sibling_threads - enables a Landlock policy for all sibling threads 466 */ 467 int landlock_restrict_sibling_threads(const struct cred *old_cred, 468 const struct cred *new_cred) 469 { 470 int err; 471 struct tsync_shared_context shared_ctx; 472 struct tsync_works works = {}; 473 size_t newly_discovered_threads; 474 bool found_more_threads; 475 476 atomic_set(&shared_ctx.preparation_error, 0); 477 init_completion(&shared_ctx.all_prepared); 478 init_completion(&shared_ctx.ready_to_commit); 479 atomic_set(&shared_ctx.num_unfinished, 1); 480 init_completion(&shared_ctx.all_finished); 481 shared_ctx.old_cred = old_cred; 482 shared_ctx.new_cred = new_cred; 483 shared_ctx.set_no_new_privs = task_no_new_privs(current); 484 485 /* 486 * We schedule a pseudo-signal task_work for each of the calling task's 487 * sibling threads. In the task work, each thread: 488 * 489 * 1) runs prepare_creds() and writes back the error to 490 * shared_ctx.preparation_error, if needed. 491 * 492 * 2) signals that it's done with prepare_creds() to the calling task. 493 * (completion "all_prepared"). 494 * 495 * 3) waits for the completion "ready_to_commit". This is sent by the 496 * calling task after ensuring that all sibling threads have done 497 * with the "preparation" stage. 498 * 499 * After this barrier is reached, it's safe to read 500 * shared_ctx.preparation_error. 501 * 502 * 4) reads shared_ctx.preparation_error and then either does commit_creds() 503 * or abort_creds(). 504 * 505 * 5) signals that it's done altogether (barrier synchronization 506 * "all_finished") 507 * 508 * Unlike seccomp, which modifies sibling tasks directly, we do not need to 509 * acquire the cred_guard_mutex and sighand->siglock: 510 * 511 * - As in our case, all threads are themselves exchanging their own struct 512 * cred through the credentials API, no locks are needed for that. 513 * - Our for_each_thread() loops are protected by RCU. 514 * - We do not acquire a lock to keep the list of sibling threads stable 515 * between our for_each_thread loops. If the list of available sibling 516 * threads changes between these for_each_thread loops, we make up for 517 * that by continuing to look for threads until they are all discovered 518 * and have entered their task_work, where they are unable to spawn new 519 * threads. 520 */ 521 do { 522 /* In RCU read-lock, count the threads we need. */ 523 newly_discovered_threads = count_additional_threads(&works); 524 525 if (newly_discovered_threads == 0) 526 break; /* done */ 527 528 err = tsync_works_grow_by(&works, newly_discovered_threads, 529 GFP_KERNEL_ACCOUNT); 530 if (err) { 531 atomic_set(&shared_ctx.preparation_error, err); 532 break; 533 } 534 535 /* 536 * The "all_prepared" barrier is used locally to the loop body, this use 537 * of for_each_thread(). We can reset it on each loop iteration because 538 * all previous loop iterations are done with it already. 539 * 540 * num_preparing is initialized to 1 so that the counter can not go to 0 541 * and mark the completion as done before all task works are registered. 542 * We decrement it at the end of the loop body. 543 */ 544 atomic_set(&shared_ctx.num_preparing, 1); 545 reinit_completion(&shared_ctx.all_prepared); 546 547 /* 548 * In RCU read-lock, schedule task work on newly discovered sibling 549 * tasks. 550 */ 551 found_more_threads = schedule_task_work(&works, &shared_ctx); 552 553 /* 554 * Decrement num_preparing for current, to undo that we initialized it 555 * to 1 a few lines above. 556 */ 557 if (atomic_dec_return(&shared_ctx.num_preparing) > 0) { 558 if (wait_for_completion_interruptible( 559 &shared_ctx.all_prepared)) { 560 /* In case of interruption, we need to retry the system call. */ 561 atomic_set(&shared_ctx.preparation_error, 562 -ERESTARTNOINTR); 563 564 /* 565 * Cancel task works for tasks that did not start running yet, 566 * and decrement all_prepared and num_unfinished accordingly. 567 */ 568 cancel_tsync_works(&works, &shared_ctx); 569 570 /* 571 * The remaining task works have started running, so waiting for 572 * their completion will finish. 573 */ 574 wait_for_completion(&shared_ctx.all_prepared); 575 } 576 } 577 } while (found_more_threads && 578 !atomic_read(&shared_ctx.preparation_error)); 579 580 /* 581 * We now have all sibling threads blocking and in "prepared" state in the 582 * task work. Ask all threads to commit. 583 */ 584 complete_all(&shared_ctx.ready_to_commit); 585 586 /* 587 * Decrement num_unfinished for current, to undo that we initialized it to 1 588 * at the beginning. 589 */ 590 if (atomic_dec_return(&shared_ctx.num_unfinished) > 0) 591 wait_for_completion(&shared_ctx.all_finished); 592 593 tsync_works_release(&works); 594 595 return atomic_read(&shared_ctx.preparation_error); 596 } 597