at commit 2f93bcb3
| 1 | // SPDX-License-Identifier: GPL-2.0-only |
| 2 | /* |
| 3 | * Generic pidhash and scalable, time-bounded PID allocator |
| 4 | * |
| 5 | * (C) 2002-2003 Nadia Yvette Chambers, IBM |
| 6 | * (C) 2004 Nadia Yvette Chambers, Oracle |
| 7 | * (C) 2002-2004 Ingo Molnar, Red Hat |
| 8 | * |
| 9 | * pid-structures are backing objects for tasks sharing a given ID to chain |
| 10 | * against. There is very little to them aside from hashing them and |
| 11 | * parking tasks using given ID's on a list. |
| 12 | * |
| 13 | * The hash is always changed with the tasklist_lock write-acquired, |
| 14 | * and the hash is only accessed with the tasklist_lock at least |
| 15 | * read-acquired, so there's no additional SMP locking needed here. |
| 16 | * |
| 17 | * We have a list of bitmap pages, which bitmaps represent the PID space. |
| 18 | * Allocating and freeing PIDs is completely lockless. The worst-case |
| 19 | * allocation scenario when all but one out of 1 million PIDs possible are |
| 20 | * allocated already: the scanning of 32 list entries and at most PAGE_SIZE |
| 21 | * bytes. The typical fastpath is a single successful setbit. Freeing is O(1). |
| 22 | * |
| 23 | * Pid namespaces: |
| 24 | * (C) 2007 Pavel Emelyanov <xemul@openvz.org>, OpenVZ, SWsoft Inc. |
| 25 | * (C) 2007 Sukadev Bhattiprolu <sukadev@us.ibm.com>, IBM |
| 26 | * Many thanks to Oleg Nesterov for comments and help |
| 27 | * |
| 28 | */ |
| 29 | |
| 30 | #include <linux/mm.h> |
| 31 | #include <linux/export.h> |
| 32 | #include <linux/slab.h> |
| 33 | #include <linux/init.h> |
| 34 | #include <linux/rculist.h> |
| 35 | #include <linux/memblock.h> |
| 36 | #include <linux/pid_namespace.h> |
| 37 | #include <linux/init_task.h> |
| 38 | #include <linux/syscalls.h> |
| 39 | #include <linux/proc_ns.h> |
| 40 | #include <linux/refcount.h> |
| 41 | #include <linux/anon_inodes.h> |
| 42 | #include <linux/sched/signal.h> |
| 43 | #include <linux/sched/task.h> |
| 44 | #include <linux/idr.h> |
| 45 | #include <linux/pidfs.h> |
| 46 | #include <net/sock.h> |
| 47 | #include <uapi/linux/pidfd.h> |
| 48 | |
| 49 | struct pid init_struct_pid = { |
| 50 | .count = REFCOUNT_INIT(1), |
| 51 | .tasks = { |
| 52 | { .first = NULL }, |
| 53 | { .first = NULL }, |
| 54 | { .first = NULL }, |
| 55 | }, |
| 56 | .level = 0, |
| 57 | .numbers = { { |
| 58 | .nr = 0, |
| 59 | .ns = &init_pid_ns, |
| 60 | }, } |
| 61 | }; |
| 62 | |
| 63 | static int pid_max_min = RESERVED_PIDS + 1; |
| 64 | static int pid_max_max = PID_MAX_LIMIT; |
| 65 | |
| 66 | /* |
| 67 | * PID-map pages start out as NULL, they get allocated upon |
| 68 | * first use and are never deallocated. This way a low pid_max |
| 69 | * value does not cause lots of bitmaps to be allocated, but |
| 70 | * the scheme scales to up to 4 million PIDs, runtime. |
| 71 | */ |
| 72 | struct pid_namespace init_pid_ns = { |
| 73 | .ns = NS_COMMON_INIT(init_pid_ns), |
| 74 | .idr = IDR_INIT(init_pid_ns.idr), |
| 75 | .pid_allocated = PIDNS_ADDING, |
| 76 | .level = 0, |
| 77 | .child_reaper = &init_task, |
| 78 | .user_ns = &init_user_ns, |
| 79 | .pid_max = PID_MAX_DEFAULT, |
| 80 | #if defined(CONFIG_SYSCTL) && defined(CONFIG_MEMFD_CREATE) |
| 81 | .memfd_noexec_scope = MEMFD_NOEXEC_SCOPE_EXEC, |
| 82 | #endif |
| 83 | }; |
| 84 | EXPORT_SYMBOL_GPL(init_pid_ns); |
| 85 | |
| 86 | static __cacheline_aligned_in_smp DEFINE_SPINLOCK(pidmap_lock); |
| 87 | |
| 88 | void put_pid(struct pid *pid) |
| 89 | { |
| 90 | struct pid_namespace *ns; |
| 91 | |
| 92 | if (!pid) |
| 93 | return; |
| 94 | |
| 95 | ns = pid->numbers[pid->level].ns; |
| 96 | if (refcount_dec_and_test(&pid->count)) { |
| 97 | pidfs_free_pid(pid); |
| 98 | kmem_cache_free(ns->pid_cachep, pid); |
| 99 | put_pid_ns(ns); |
| 100 | } |
| 101 | } |
| 102 | EXPORT_SYMBOL_GPL(put_pid); |
| 103 | |
| 104 | static void delayed_put_pid(struct rcu_head *rhp) |
| 105 | { |
| 106 | struct pid *pid = container_of(rhp, struct pid, rcu); |
| 107 | put_pid(pid); |
| 108 | } |
| 109 | |
| 110 | void free_pid(struct pid *pid) |
| 111 | { |
| 112 | int i; |
| 113 | struct pid_namespace *active_ns; |
| 114 | |
| 115 | lockdep_assert_not_held(&tasklist_lock); |
| 116 | |
| 117 | active_ns = pid->numbers[pid->level].ns; |
| 118 | ns_ref_active_put(active_ns); |
| 119 | |
| 120 | spin_lock(&pidmap_lock); |
| 121 | for (i = 0; i <= pid->level; i++) { |
| 122 | struct upid *upid = pid->numbers + i; |
| 123 | struct pid_namespace *ns = upid->ns; |
| 124 | switch (--ns->pid_allocated) { |
| 125 | case 2: |
| 126 | case 1: |
| 127 | /* When all that is left in the pid namespace |
| 128 | * is the reaper wake up the reaper. The reaper |
| 129 | * may be sleeping in zap_pid_ns_processes(). |
| 130 | */ |
| 131 | wake_up_process(READ_ONCE(ns->child_reaper)); |
| 132 | break; |
| 133 | case PIDNS_ADDING: |
| 134 | /* Only possible if the 1st fork fails */ |
| 135 | WARN_ON(READ_ONCE(ns->child_reaper)); |
| 136 | break; |
| 137 | } |
| 138 | |
| 139 | idr_remove(&ns->idr, upid->nr); |
| 140 | } |
| 141 | spin_unlock(&pidmap_lock); |
| 142 | |
| 143 | pidfs_remove_pid(pid); |
| 144 | call_rcu(&pid->rcu, delayed_put_pid); |
| 145 | } |
| 146 | |
| 147 | void free_pids(struct pid **pids) |
| 148 | { |
| 149 | int tmp; |
| 150 | |
| 151 | /* |
| 152 | * This can batch pidmap_lock. |
| 153 | */ |
| 154 | for (tmp = PIDTYPE_MAX; --tmp >= 0; ) |
| 155 | if (pids[tmp]) |
| 156 | free_pid(pids[tmp]); |
| 157 | } |
| 158 | |
| 159 | struct pid *alloc_pid(struct pid_namespace *ns, pid_t *arg_set_tid, |
| 160 | size_t arg_set_tid_size) |
| 161 | { |
| 162 | int set_tid[MAX_PID_NS_LEVEL + 1] = {}; |
| 163 | int pid_max[MAX_PID_NS_LEVEL + 1] = {}; |
| 164 | struct pid *pid; |
| 165 | enum pid_type type; |
| 166 | int i, nr; |
| 167 | struct pid_namespace *tmp; |
| 168 | struct upid *upid; |
| 169 | int retval = -ENOMEM; |
| 170 | bool retried_preload; |
| 171 | |
| 172 | /* |
| 173 | * arg_set_tid_size contains the size of the arg_set_tid array. Starting at |
| 174 | * the most nested currently active PID namespace it tells alloc_pid() |
| 175 | * which PID to set for a process in that most nested PID namespace |
| 176 | * up to arg_set_tid_size PID namespaces. It does not have to set the PID |
| 177 | * for a process in all nested PID namespaces but arg_set_tid_size must |
| 178 | * never be greater than the current ns->level + 1. |
| 179 | */ |
| 180 | if (arg_set_tid_size > ns->level + 1) |
| 181 | return ERR_PTR(-EINVAL); |
| 182 | |
| 183 | /* |
| 184 | * Prep before we take locks: |
| 185 | * |
| 186 | * 1. allocate and fill in pid struct |
| 187 | */ |
| 188 | pid = kmem_cache_alloc(ns->pid_cachep, GFP_KERNEL); |
| 189 | if (!pid) |
| 190 | return ERR_PTR(retval); |
| 191 | |
| 192 | get_pid_ns(ns); |
| 193 | pid->level = ns->level; |
| 194 | refcount_set(&pid->count, 1); |
| 195 | spin_lock_init(&pid->lock); |
| 196 | for (type = 0; type < PIDTYPE_MAX; ++type) |
| 197 | INIT_HLIST_HEAD(&pid->tasks[type]); |
| 198 | init_waitqueue_head(&pid->wait_pidfd); |
| 199 | INIT_HLIST_HEAD(&pid->inodes); |
| 200 | pidfs_prepare_pid(pid); |
| 201 | |
| 202 | /* |
| 203 | * 2. perm check checkpoint_restore_ns_capable() |
| 204 | * |
| 205 | * This stores found pid_max to make sure the used value is the same should |
| 206 | * later code need it. |
| 207 | */ |
| 208 | for (tmp = ns, i = ns->level; i >= 0; i--) { |
| 209 | pid_max[ns->level - i] = READ_ONCE(tmp->pid_max); |
| 210 | |
| 211 | if (arg_set_tid_size) { |
| 212 | int tid = set_tid[ns->level - i] = arg_set_tid[ns->level - i]; |
| 213 | |
| 214 | retval = -EINVAL; |
| 215 | if (tid < 1 || tid >= pid_max[ns->level - i]) |
| 216 | goto out_abort; |
| 217 | retval = -EPERM; |
| 218 | if (!checkpoint_restore_ns_capable(tmp->user_ns)) |
| 219 | goto out_abort; |
| 220 | arg_set_tid_size--; |
| 221 | } |
| 222 | |
| 223 | tmp = tmp->parent; |
| 224 | } |
| 225 | |
| 226 | /* |
| 227 | * Prep is done, id allocation goes here: |
| 228 | */ |
| 229 | retried_preload = false; |
| 230 | idr_preload(GFP_KERNEL); |
| 231 | spin_lock(&pidmap_lock); |
| 232 | /* For the case when the previous attempt to create init failed */ |
| 233 | if (ns->pid_allocated == PIDNS_ADDING) |
| 234 | idr_set_cursor(&ns->idr, 0); |
| 235 | |
| 236 | for (tmp = ns, i = ns->level; i >= 0;) { |
| 237 | int tid = set_tid[ns->level - i]; |
| 238 | |
| 239 | if (tid) { |
| 240 | nr = idr_alloc(&tmp->idr, NULL, tid, |
| 241 | tid + 1, GFP_ATOMIC); |
| 242 | /* |
| 243 | * If ENOSPC is returned it means that the PID is |
| 244 | * alreay in use. Return EEXIST in that case. |
| 245 | */ |
| 246 | if (nr == -ENOSPC) |
| 247 | |
| 248 | nr = -EEXIST; |
| 249 | } else { |
| 250 | int pid_min = 1; |
| 251 | /* |
| 252 | * init really needs pid 1, but after reaching the |
| 253 | * maximum wrap back to RESERVED_PIDS |
| 254 | */ |
| 255 | if (idr_get_cursor(&tmp->idr) > RESERVED_PIDS) |
| 256 | pid_min = RESERVED_PIDS; |
| 257 | |
| 258 | /* |
| 259 | * Store a null pointer so find_pid_ns does not find |
| 260 | * a partially initialized PID (see below). |
| 261 | */ |
| 262 | nr = idr_alloc_cyclic(&tmp->idr, NULL, pid_min, |
| 263 | pid_max[ns->level - i], GFP_ATOMIC); |
| 264 | if (nr == -ENOSPC) |
| 265 | nr = -EAGAIN; |
| 266 | } |
| 267 | |
| 268 | if (unlikely(nr < 0)) { |
| 269 | /* |
| 270 | * Preload more memory if idr_alloc{,cyclic} failed with -ENOMEM. |
| 271 | * |
| 272 | * The IDR API only allows us to preload memory for one call, while we may end |
| 273 | * up doing several under pidmap_lock with GFP_ATOMIC. The situation may be |
| 274 | * salvageable with GFP_KERNEL. But make sure to not loop indefinitely if preload |
| 275 | * did not help (the routine unfortunately returns void, so we have no idea |
| 276 | * if it got anywhere). |
| 277 | * |
| 278 | * The lock can be safely dropped and picked up as historically pid allocation |
| 279 | * for different namespaces was *not* atomic -- we try to hold on to it the |
| 280 | * entire time only for performance reasons. |
| 281 | */ |
| 282 | if (nr == -ENOMEM && !retried_preload) { |
| 283 | spin_unlock(&pidmap_lock); |
| 284 | idr_preload_end(); |
| 285 | retried_preload = true; |
| 286 | idr_preload(GFP_KERNEL); |
| 287 | spin_lock(&pidmap_lock); |
| 288 | continue; |
| 289 | } |
| 290 | retval = nr; |
| 291 | goto out_free; |
| 292 | } |
| 293 | |
| 294 | pid->numbers[i].nr = nr; |
| 295 | pid->numbers[i].ns = tmp; |
| 296 | i--; |
| 297 | retried_preload = false; |
| 298 | |
| 299 | /* |
| 300 | * PID 1 (init) must be created first. |
| 301 | */ |
| 302 | if (!READ_ONCE(tmp->child_reaper) && nr != 1) { |
| 303 | retval = -EINVAL; |
| 304 | goto out_free; |
| 305 | } |
| 306 | |
| 307 | tmp = tmp->parent; |
| 308 | } |
| 309 | |
| 310 | /* |
| 311 | * ENOMEM is not the most obvious choice especially for the case |
| 312 | * where the child subreaper has already exited and the pid |
| 313 | * namespace denies the creation of any new processes. But ENOMEM |
| 314 | * is what we have exposed to userspace for a long time and it is |
| 315 | * documented behavior for pid namespaces. So we can't easily |
| 316 | * change it even if there were an error code better suited. |
| 317 | * |
| 318 | * This can't be done earlier because we need to preserve other |
| 319 | * error conditions. |
| 320 | * |
| 321 | * We need this even if copy_process() does the same check. If two |
| 322 | * or more tasks from parent namespace try to inject a child into a |
| 323 | * dead namespace, one of free_pid() calls from the copy_process() |
| 324 | * error path may try to wakeup the possibly freed ns->child_reaper. |
| 325 | */ |
| 326 | retval = -ENOMEM; |
| 327 | for (upid = pid->numbers + ns->level; upid >= pid->numbers; --upid) |
| 328 | if (unlikely(!(upid->ns->pid_allocated & PIDNS_ADDING))) |
| 329 | goto out_free; |
| 330 | |
| 331 | for (upid = pid->numbers + ns->level; upid >= pid->numbers; --upid) { |
| 332 | /* Make the PID visible to find_pid_ns. */ |
| 333 | idr_replace(&upid->ns->idr, pid, upid->nr); |
| 334 | upid->ns->pid_allocated++; |
| 335 | } |
| 336 | spin_unlock(&pidmap_lock); |
| 337 | idr_preload_end(); |
| 338 | ns_ref_active_get(ns); |
| 339 | |
| 340 | retval = pidfs_add_pid(pid); |
| 341 | if (unlikely(retval)) { |
| 342 | free_pid(pid); |
| 343 | pid = ERR_PTR(-ENOMEM); |
| 344 | } |
| 345 | |
| 346 | return pid; |
| 347 | |
| 348 | out_free: |
| 349 | while (++i <= ns->level) { |
| 350 | upid = pid->numbers + i; |
| 351 | idr_remove(&upid->ns->idr, upid->nr); |
| 352 | } |
| 353 | |
| 354 | spin_unlock(&pidmap_lock); |
| 355 | idr_preload_end(); |
| 356 | |
| 357 | out_abort: |
| 358 | put_pid_ns(ns); |
| 359 | kmem_cache_free(ns->pid_cachep, pid); |
| 360 | return ERR_PTR(retval); |
| 361 | } |
| 362 | |
| 363 | void disable_pid_allocation(struct pid_namespace *ns) |
| 364 | { |
| 365 | spin_lock(&pidmap_lock); |
| 366 | ns->pid_allocated &= ~PIDNS_ADDING; |
| 367 | spin_unlock(&pidmap_lock); |
| 368 | } |
| 369 | |
| 370 | struct pid *find_pid_ns(int nr, struct pid_namespace *ns) |
| 371 | { |
| 372 | return idr_find(&ns->idr, nr); |
| 373 | } |
| 374 | EXPORT_SYMBOL_GPL(find_pid_ns); |
| 375 | |
| 376 | struct pid *find_vpid(int nr) |
| 377 | { |
| 378 | return find_pid_ns(nr, task_active_pid_ns(current)); |
| 379 | } |
| 380 | EXPORT_SYMBOL_GPL(find_vpid); |
| 381 | |
| 382 | static struct pid **task_pid_ptr(struct task_struct *task, enum pid_type type) |
| 383 | { |
| 384 | return (type == PIDTYPE_PID) ? |
| 385 | &task->thread_pid : |
| 386 | &task->signal->pids[type]; |
| 387 | } |
| 388 | |
| 389 | /* |
| 390 | * attach_pid() must be called with the tasklist_lock write-held. |
| 391 | */ |
| 392 | void attach_pid(struct task_struct *task, enum pid_type type) |
| 393 | { |
| 394 | struct pid *pid; |
| 395 | |
| 396 | lockdep_assert_held_write(&tasklist_lock); |
| 397 | |
| 398 | pid = *task_pid_ptr(task, type); |
| 399 | hlist_add_head_rcu(&task->pid_links[type], &pid->tasks[type]); |
| 400 | } |
| 401 | |
| 402 | static void __change_pid(struct pid **pids, struct task_struct *task, |
| 403 | enum pid_type type, struct pid *new) |
| 404 | { |
| 405 | struct pid **pid_ptr, *pid; |
| 406 | int tmp; |
| 407 | |
| 408 | lockdep_assert_held_write(&tasklist_lock); |
| 409 | |
| 410 | pid_ptr = task_pid_ptr(task, type); |
| 411 | pid = *pid_ptr; |
| 412 | |
| 413 | hlist_del_rcu(&task->pid_links[type]); |
| 414 | *pid_ptr = new; |
| 415 | |
| 416 | for (tmp = PIDTYPE_MAX; --tmp >= 0; ) |
| 417 | if (pid_has_task(pid, tmp)) |
| 418 | return; |
| 419 | |
| 420 | WARN_ON(pids[type]); |
| 421 | pids[type] = pid; |
| 422 | } |
| 423 | |
| 424 | void detach_pid(struct pid **pids, struct task_struct *task, enum pid_type type) |
| 425 | { |
| 426 | __change_pid(pids, task, type, NULL); |
| 427 | } |
| 428 | |
| 429 | void change_pid(struct pid **pids, struct task_struct *task, enum pid_type type, |
| 430 | struct pid *pid) |
| 431 | { |
| 432 | __change_pid(pids, task, type, pid); |
| 433 | attach_pid(task, type); |
| 434 | } |
| 435 | |
| 436 | void exchange_tids(struct task_struct *left, struct task_struct *right) |
| 437 | { |
| 438 | struct pid *pid1 = left->thread_pid; |
| 439 | struct pid *pid2 = right->thread_pid; |
| 440 | struct hlist_head *head1 = &pid1->tasks[PIDTYPE_PID]; |
| 441 | struct hlist_head *head2 = &pid2->tasks[PIDTYPE_PID]; |
| 442 | |
| 443 | lockdep_assert_held_write(&tasklist_lock); |
| 444 | |
| 445 | /* Swap the single entry tid lists */ |
| 446 | hlists_swap_heads_rcu(head1, head2); |
| 447 | |
| 448 | /* Swap the per task_struct pid */ |
| 449 | rcu_assign_pointer(left->thread_pid, pid2); |
| 450 | rcu_assign_pointer(right->thread_pid, pid1); |
| 451 | |
| 452 | /* Swap the cached value */ |
| 453 | WRITE_ONCE(left->pid, pid_nr(pid2)); |
| 454 | WRITE_ONCE(right->pid, pid_nr(pid1)); |
| 455 | } |
| 456 | |
| 457 | /* transfer_pid is an optimization of attach_pid(new), detach_pid(old) */ |
| 458 | void transfer_pid(struct task_struct *old, struct task_struct *new, |
| 459 | enum pid_type type) |
| 460 | { |
| 461 | WARN_ON_ONCE(type == PIDTYPE_PID); |
| 462 | lockdep_assert_held_write(&tasklist_lock); |
| 463 | hlist_replace_rcu(&old->pid_links[type], &new->pid_links[type]); |
| 464 | } |
| 465 | |
| 466 | struct task_struct *pid_task(struct pid *pid, enum pid_type type) |
| 467 | { |
| 468 | struct task_struct *result = NULL; |
| 469 | if (pid) { |
| 470 | struct hlist_node *first; |
| 471 | first = rcu_dereference_check(hlist_first_rcu(&pid->tasks[type]), |
| 472 | lockdep_tasklist_lock_is_held()); |
| 473 | if (first) |
| 474 | result = hlist_entry(first, struct task_struct, pid_links[(type)]); |
| 475 | } |
| 476 | return result; |
| 477 | } |
| 478 | EXPORT_SYMBOL(pid_task); |
| 479 | |
| 480 | /* |
| 481 | * Must be called under rcu_read_lock(). |
| 482 | */ |
| 483 | struct task_struct *find_task_by_pid_ns(pid_t nr, struct pid_namespace *ns) |
| 484 | { |
| 485 | RCU_LOCKDEP_WARN(!rcu_read_lock_held(), |
| 486 | "find_task_by_pid_ns() needs rcu_read_lock() protection"); |
| 487 | return pid_task(find_pid_ns(nr, ns), PIDTYPE_PID); |
| 488 | } |
| 489 | |
| 490 | struct task_struct *find_task_by_vpid(pid_t vnr) |
| 491 | { |
| 492 | return find_task_by_pid_ns(vnr, task_active_pid_ns(current)); |
| 493 | } |
| 494 | |
| 495 | struct task_struct *find_get_task_by_vpid(pid_t nr) |
| 496 | { |
| 497 | struct task_struct *task; |
| 498 | |
| 499 | rcu_read_lock(); |
| 500 | task = find_task_by_vpid(nr); |
| 501 | if (task) |
| 502 | get_task_struct(task); |
| 503 | rcu_read_unlock(); |
| 504 | |
| 505 | return task; |
| 506 | } |
| 507 | |
| 508 | struct pid *get_task_pid(struct task_struct *task, enum pid_type type) |
| 509 | { |
| 510 | struct pid *pid; |
| 511 | rcu_read_lock(); |
| 512 | pid = get_pid(rcu_dereference(*task_pid_ptr(task, type))); |
| 513 | rcu_read_unlock(); |
| 514 | return pid; |
| 515 | } |
| 516 | EXPORT_SYMBOL_GPL(get_task_pid); |
| 517 | |
| 518 | struct task_struct *get_pid_task(struct pid *pid, enum pid_type type) |
| 519 | { |
| 520 | struct task_struct *result; |
| 521 | rcu_read_lock(); |
| 522 | result = pid_task(pid, type); |
| 523 | if (result) |
| 524 | get_task_struct(result); |
| 525 | rcu_read_unlock(); |
| 526 | return result; |
| 527 | } |
| 528 | EXPORT_SYMBOL_GPL(get_pid_task); |
| 529 | |
| 530 | struct pid *find_get_pid(pid_t nr) |
| 531 | { |
| 532 | struct pid *pid; |
| 533 | |
| 534 | rcu_read_lock(); |
| 535 | pid = get_pid(find_vpid(nr)); |
| 536 | rcu_read_unlock(); |
| 537 | |
| 538 | return pid; |
| 539 | } |
| 540 | EXPORT_SYMBOL_GPL(find_get_pid); |
| 541 | |
| 542 | pid_t pid_nr_ns(struct pid *pid, struct pid_namespace *ns) |
| 543 | { |
| 544 | struct upid *upid; |
| 545 | pid_t nr = 0; |
| 546 | |
| 547 | if (pid && ns && ns->level <= pid->level) { |
| 548 | upid = &pid->numbers[ns->level]; |
| 549 | if (upid->ns == ns) |
| 550 | nr = upid->nr; |
| 551 | } |
| 552 | return nr; |
| 553 | } |
| 554 | EXPORT_SYMBOL_GPL(pid_nr_ns); |
| 555 | |
| 556 | pid_t pid_vnr(struct pid *pid) |
| 557 | { |
| 558 | return pid_nr_ns(pid, task_active_pid_ns(current)); |
| 559 | } |
| 560 | EXPORT_SYMBOL_GPL(pid_vnr); |
| 561 | |
| 562 | pid_t __task_pid_nr_ns(struct task_struct *task, enum pid_type type, |
| 563 | struct pid_namespace *ns) |
| 564 | { |
| 565 | pid_t nr = 0; |
| 566 | |
| 567 | rcu_read_lock(); |
| 568 | if (!ns) |
| 569 | ns = task_active_pid_ns(current); |
| 570 | nr = pid_nr_ns(rcu_dereference(*task_pid_ptr(task, type)), ns); |
| 571 | rcu_read_unlock(); |
| 572 | |
| 573 | return nr; |
| 574 | } |
| 575 | EXPORT_SYMBOL(__task_pid_nr_ns); |
| 576 | |
| 577 | struct pid_namespace *task_active_pid_ns(struct task_struct *tsk) |
| 578 | { |
| 579 | return ns_of_pid(task_pid(tsk)); |
| 580 | } |
| 581 | EXPORT_SYMBOL_GPL(task_active_pid_ns); |
| 582 | |
| 583 | /* |
| 584 | * Used by proc to find the first pid that is greater than or equal to nr. |
| 585 | * |
| 586 | * If there is a pid at nr this function is exactly the same as find_pid_ns. |
| 587 | */ |
| 588 | struct pid *find_ge_pid(int nr, struct pid_namespace *ns) |
| 589 | { |
| 590 | return idr_get_next(&ns->idr, &nr); |
| 591 | } |
| 592 | EXPORT_SYMBOL_GPL(find_ge_pid); |
| 593 | |
| 594 | struct pid *pidfd_get_pid(unsigned int fd, unsigned int *flags) |
| 595 | { |
| 596 | CLASS(fd, f)(fd); |
| 597 | struct pid *pid; |
| 598 | |
| 599 | if (fd_empty(f)) |
| 600 | return ERR_PTR(-EBADF); |
| 601 | |
| 602 | pid = pidfd_pid(fd_file(f)); |
| 603 | if (!IS_ERR(pid)) { |
| 604 | get_pid(pid); |
| 605 | *flags = fd_file(f)->f_flags; |
| 606 | } |
| 607 | return pid; |
| 608 | } |
| 609 | |
| 610 | /** |
| 611 | * pidfd_get_task() - Get the task associated with a pidfd |
| 612 | * |
| 613 | * @pidfd: pidfd for which to get the task |
| 614 | * @flags: flags associated with this pidfd |
| 615 | * |
| 616 | * Return the task associated with @pidfd. The function takes a reference on |
| 617 | * the returned task. The caller is responsible for releasing that reference. |
| 618 | * |
| 619 | * Return: On success, the task_struct associated with the pidfd. |
| 620 | * On error, a negative errno number will be returned. |
| 621 | */ |
| 622 | struct task_struct *pidfd_get_task(int pidfd, unsigned int *flags) |
| 623 | { |
| 624 | unsigned int f_flags = 0; |
| 625 | struct pid *pid; |
| 626 | struct task_struct *task; |
| 627 | enum pid_type type; |
| 628 | |
| 629 | switch (pidfd) { |
| 630 | case PIDFD_SELF_THREAD: |
| 631 | type = PIDTYPE_PID; |
| 632 | pid = get_task_pid(current, type); |
| 633 | break; |
| 634 | case PIDFD_SELF_THREAD_GROUP: |
| 635 | type = PIDTYPE_TGID; |
| 636 | pid = get_task_pid(current, type); |
| 637 | break; |
| 638 | default: |
| 639 | pid = pidfd_get_pid(pidfd, &f_flags); |
| 640 | if (IS_ERR(pid)) |
| 641 | return ERR_CAST(pid); |
| 642 | type = PIDTYPE_TGID; |
| 643 | break; |
| 644 | } |
| 645 | |
| 646 | task = get_pid_task(pid, type); |
| 647 | put_pid(pid); |
| 648 | if (!task) |
| 649 | return ERR_PTR(-ESRCH); |
| 650 | |
| 651 | *flags = f_flags; |
| 652 | return task; |
| 653 | } |
| 654 | |
| 655 | /** |
| 656 | * pidfd_create() - Create a new pid file descriptor. |
| 657 | * |
| 658 | * @pid: struct pid that the pidfd will reference |
| 659 | * @flags: flags to pass |
| 660 | * |
| 661 | * This creates a new pid file descriptor with the O_CLOEXEC flag set. |
| 662 | * |
| 663 | * Note, that this function can only be called after the fd table has |
| 664 | * been unshared to avoid leaking the pidfd to the new process. |
| 665 | * |
| 666 | * This symbol should not be explicitly exported to loadable modules. |
| 667 | * |
| 668 | * Return: On success, a cloexec pidfd is returned. |
| 669 | * On error, a negative errno number will be returned. |
| 670 | */ |
| 671 | static int pidfd_create(struct pid *pid, unsigned int flags) |
| 672 | { |
| 673 | int pidfd; |
| 674 | struct file *pidfd_file; |
| 675 | |
| 676 | pidfd = pidfd_prepare(pid, flags, &pidfd_file); |
| 677 | if (pidfd < 0) |
| 678 | return pidfd; |
| 679 | |
| 680 | fd_install(pidfd, pidfd_file); |
| 681 | return pidfd; |
| 682 | } |
| 683 | |
| 684 | /** |
| 685 | * sys_pidfd_open() - Open new pid file descriptor. |
| 686 | * |
| 687 | * @pid: pid for which to retrieve a pidfd |
| 688 | * @flags: flags to pass |
| 689 | * |
| 690 | * This creates a new pid file descriptor with the O_CLOEXEC flag set for |
| 691 | * the task identified by @pid. Without PIDFD_THREAD flag the target task |
| 692 | * must be a thread-group leader. |
| 693 | * |
| 694 | * Return: On success, a cloexec pidfd is returned. |
| 695 | * On error, a negative errno number will be returned. |
| 696 | */ |
| 697 | SYSCALL_DEFINE2(pidfd_open, pid_t, pid, unsigned int, flags) |
| 698 | { |
| 699 | int fd; |
| 700 | struct pid *p; |
| 701 | |
| 702 | if (flags & ~(PIDFD_NONBLOCK | PIDFD_THREAD)) |
| 703 | return -EINVAL; |
| 704 | |
| 705 | if (pid <= 0) |
| 706 | return -EINVAL; |
| 707 | |
| 708 | p = find_get_pid(pid); |
| 709 | if (!p) |
| 710 | return -ESRCH; |
| 711 | |
| 712 | fd = pidfd_create(p, flags); |
| 713 | |
| 714 | put_pid(p); |
| 715 | return fd; |
| 716 | } |
| 717 | |
| 718 | #ifdef CONFIG_SYSCTL |
| 719 | static struct ctl_table_set *pid_table_root_lookup(struct ctl_table_root *root) |
| 720 | { |
| 721 | return &task_active_pid_ns(current)->set; |
| 722 | } |
| 723 | |
| 724 | static int set_is_seen(struct ctl_table_set *set) |
| 725 | { |
| 726 | return &task_active_pid_ns(current)->set == set; |
| 727 | } |
| 728 | |
| 729 | static int pid_table_root_permissions(struct ctl_table_header *head, |
| 730 | const struct ctl_table *table) |
| 731 | { |
| 732 | struct pid_namespace *pidns = |
| 733 | container_of(head->set, struct pid_namespace, set); |
| 734 | int mode = table->mode; |
| 735 | |
| 736 | if (ns_capable_noaudit(pidns->user_ns, CAP_SYS_ADMIN) || |
| 737 | uid_eq(current_euid(), make_kuid(pidns->user_ns, 0))) |
| 738 | mode = (mode & S_IRWXU) >> 6; |
| 739 | else if (in_egroup_p(make_kgid(pidns->user_ns, 0))) |
| 740 | mode = (mode & S_IRWXG) >> 3; |
| 741 | else |
| 742 | mode = mode & S_IROTH; |
| 743 | return (mode << 6) | (mode << 3) | mode; |
| 744 | } |
| 745 | |
| 746 | static void pid_table_root_set_ownership(struct ctl_table_header *head, |
| 747 | kuid_t *uid, kgid_t *gid) |
| 748 | { |
| 749 | struct pid_namespace *pidns = |
| 750 | container_of(head->set, struct pid_namespace, set); |
| 751 | kuid_t ns_root_uid; |
| 752 | kgid_t ns_root_gid; |
| 753 | |
| 754 | ns_root_uid = make_kuid(pidns->user_ns, 0); |
| 755 | if (uid_valid(ns_root_uid)) |
| 756 | *uid = ns_root_uid; |
| 757 | |
| 758 | ns_root_gid = make_kgid(pidns->user_ns, 0); |
| 759 | if (gid_valid(ns_root_gid)) |
| 760 | *gid = ns_root_gid; |
| 761 | } |
| 762 | |
| 763 | static struct ctl_table_root pid_table_root = { |
| 764 | .lookup = pid_table_root_lookup, |
| 765 | .permissions = pid_table_root_permissions, |
| 766 | .set_ownership = pid_table_root_set_ownership, |
| 767 | }; |
| 768 | |
| 769 | static const struct ctl_table pid_table[] = { |
| 770 | { |
| 771 | .procname = "pid_max", |
| 772 | .data = &init_pid_ns.pid_max, |
| 773 | .maxlen = sizeof(int), |
| 774 | .mode = 0644, |
| 775 | .proc_handler = proc_dointvec_minmax, |
| 776 | .extra1 = &pid_max_min, |
| 777 | .extra2 = &pid_max_max, |
| 778 | }, |
| 779 | }; |
| 780 | #endif |
| 781 | |
| 782 | int register_pidns_sysctls(struct pid_namespace *pidns) |
| 783 | { |
| 784 | #ifdef CONFIG_SYSCTL |
| 785 | struct ctl_table *tbl; |
| 786 | |
| 787 | setup_sysctl_set(&pidns->set, &pid_table_root, set_is_seen); |
| 788 | |
| 789 | tbl = kmemdup(pid_table, sizeof(pid_table), GFP_KERNEL); |
| 790 | if (!tbl) |
| 791 | return -ENOMEM; |
| 792 | tbl->data = &pidns->pid_max; |
| 793 | pidns->pid_max = min(pid_max_max, max_t(int, pidns->pid_max, |
| 794 | PIDS_PER_CPU_DEFAULT * num_possible_cpus())); |
| 795 | |
| 796 | pidns->sysctls = __register_sysctl_table(&pidns->set, "kernel", tbl, |
| 797 | ARRAY_SIZE(pid_table)); |
| 798 | if (!pidns->sysctls) { |
| 799 | kfree(tbl); |
| 800 | retire_sysctl_set(&pidns->set); |
| 801 | return -ENOMEM; |
| 802 | } |
| 803 | #endif |
| 804 | return 0; |
| 805 | } |
| 806 | |
| 807 | void unregister_pidns_sysctls(struct pid_namespace *pidns) |
| 808 | { |
| 809 | #ifdef CONFIG_SYSCTL |
| 810 | const struct ctl_table *tbl; |
| 811 | |
| 812 | tbl = pidns->sysctls->ctl_table_arg; |
| 813 | unregister_sysctl_table(pidns->sysctls); |
| 814 | retire_sysctl_set(&pidns->set); |
| 815 | kfree(tbl); |
| 816 | #endif |
| 817 | } |
| 818 | |
| 819 | void __init pid_idr_init(void) |
| 820 | { |
| 821 | /* Verify no one has done anything silly: */ |
| 822 | BUILD_BUG_ON(PID_MAX_LIMIT >= PIDNS_ADDING); |
| 823 | |
| 824 | /* bump default and minimum pid_max based on number of cpus */ |
| 825 | init_pid_ns.pid_max = min(pid_max_max, max_t(int, init_pid_ns.pid_max, |
| 826 | PIDS_PER_CPU_DEFAULT * num_possible_cpus())); |
| 827 | pid_max_min = max_t(int, pid_max_min, |
| 828 | PIDS_PER_CPU_MIN * num_possible_cpus()); |
| 829 | pr_info("pid_max: default: %u minimum: %u\n", init_pid_ns.pid_max, pid_max_min); |
| 830 | |
| 831 | idr_init(&init_pid_ns.idr); |
| 832 | |
| 833 | init_pid_ns.pid_cachep = kmem_cache_create("pid", |
| 834 | struct_size_t(struct pid, numbers, 1), |
| 835 | __alignof__(struct pid), |
| 836 | SLAB_HWCACHE_ALIGN | SLAB_PANIC | SLAB_ACCOUNT, |
| 837 | NULL); |
| 838 | } |
| 839 | |
| 840 | static __init int pid_namespace_sysctl_init(void) |
| 841 | { |
| 842 | #ifdef CONFIG_SYSCTL |
| 843 | /* "kernel" directory will have already been initialized. */ |
| 844 | BUG_ON(register_pidns_sysctls(&init_pid_ns)); |
| 845 | #endif |
| 846 | return 0; |
| 847 | } |
| 848 | subsys_initcall(pid_namespace_sysctl_init); |
| 849 | |
| 850 | static struct file *__pidfd_fget(struct task_struct *task, int fd) |
| 851 | { |
| 852 | struct file *file; |
| 853 | int ret; |
| 854 | |
| 855 | ret = down_read_killable(&task->signal->exec_update_lock); |
| 856 | if (ret) |
| 857 | return ERR_PTR(ret); |
| 858 | |
| 859 | if (!ptrace_may_access(task, PTRACE_MODE_ATTACH_REALCREDS)) |
| 860 | file = ERR_PTR(-EPERM); |
| 861 | else if (task->flags & PF_EXITING) |
| 862 | file = ERR_PTR(-ESRCH); |
| 863 | else |
| 864 | file = fget_task(task, fd); |
| 865 | |
| 866 | up_read(&task->signal->exec_update_lock); |
| 867 | |
| 868 | if (!file) { |
| 869 | /* |
| 870 | * It is possible that the target thread is exiting; it can be |
| 871 | * either: |
| 872 | * 1. before exit_signals(), which gives a real fd |
| 873 | * 2. before exit_files() takes the task_lock() gives a real fd |
| 874 | * 3. after exit_files() releases task_lock(), ->files is NULL; |
| 875 | * this has PF_EXITING, since it was set in exit_signals(), |
| 876 | * __pidfd_fget() returns EBADF. |
| 877 | * In case 3 we get EBADF, but that really means ESRCH, since |
| 878 | * the task is currently exiting and has freed its files |
| 879 | * struct, so we fix it up. |
| 880 | */ |
| 881 | if (task->flags & PF_EXITING) |
| 882 | file = ERR_PTR(-ESRCH); |
| 883 | else |
| 884 | file = ERR_PTR(-EBADF); |
| 885 | } |
| 886 | |
| 887 | return file; |
| 888 | } |
| 889 | |
| 890 | static int pidfd_getfd(struct pid *pid, int fd) |
| 891 | { |
| 892 | struct task_struct *task; |
| 893 | struct file *file; |
| 894 | int ret; |
| 895 | |
| 896 | task = get_pid_task(pid, PIDTYPE_PID); |
| 897 | if (!task) |
| 898 | return -ESRCH; |
| 899 | |
| 900 | file = __pidfd_fget(task, fd); |
| 901 | put_task_struct(task); |
| 902 | if (IS_ERR(file)) |
| 903 | return PTR_ERR(file); |
| 904 | |
| 905 | ret = receive_fd(file, NULL, O_CLOEXEC); |
| 906 | fput(file); |
| 907 | |
| 908 | return ret; |
| 909 | } |
| 910 | |
| 911 | /** |
| 912 | * sys_pidfd_getfd() - Get a file descriptor from another process |
| 913 | * |
| 914 | * @pidfd: the pidfd file descriptor of the process |
| 915 | * @fd: the file descriptor number to get |
| 916 | * @flags: flags on how to get the fd (reserved) |
| 917 | * |
| 918 | * This syscall gets a copy of a file descriptor from another process |
| 919 | * based on the pidfd, and file descriptor number. It requires that |
| 920 | * the calling process has the ability to ptrace the process represented |
| 921 | * by the pidfd. The process which is having its file descriptor copied |
| 922 | * is otherwise unaffected. |
| 923 | * |
| 924 | * Return: On success, a cloexec file descriptor is returned. |
| 925 | * On error, a negative errno number will be returned. |
| 926 | */ |
| 927 | SYSCALL_DEFINE3(pidfd_getfd, int, pidfd, int, fd, |
| 928 | unsigned int, flags) |
| 929 | { |
| 930 | struct pid *pid; |
| 931 | |
| 932 | /* flags is currently unused - make sure it's unset */ |
| 933 | if (flags) |
| 934 | return -EINVAL; |
| 935 | |
| 936 | CLASS(fd, f)(pidfd); |
| 937 | if (fd_empty(f)) |
| 938 | return -EBADF; |
| 939 | |
| 940 | pid = pidfd_pid(fd_file(f)); |
| 941 | if (IS_ERR(pid)) |
| 942 | return PTR_ERR(pid); |
| 943 | |
| 944 | return pidfd_getfd(pid, fd); |
| 945 | } |