1 /* $NetBSD: kern_proc.c,v 1.287 2026/09/27 22:00:33 riastradh Exp $ */ 2 3 /*- 4 * Copyright (c) 1999, 2006, 2007, 2008, 2020, 2023 5 * The NetBSD Foundation, Inc. 6 * All rights reserved. 7 * 8 * This code is derived from software contributed to The NetBSD Foundation 9 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility, 10 * NASA Ames Research Center, and by Andrew Doran. 11 * 12 * Redistribution and use in source and binary forms, with or without 13 * modification, are permitted provided that the following conditions 14 * are met: 15 * 1. Redistributions of source code must retain the above copyright 16 * notice, this list of conditions and the following disclaimer. 17 * 2. Redistributions in binary form must reproduce the above copyright 18 * notice, this list of conditions and the following disclaimer in the 19 * documentation and/or other materials provided with the distribution. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 23 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 24 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 25 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 31 * POSSIBILITY OF SUCH DAMAGE. 32 */ 33 34 /* 35 * Copyright (c) 1982, 1986, 1989, 1991, 1993 36 * The Regents of the University of California. All rights reserved. 37 * 38 * Redistribution and use in source and binary forms, with or without 39 * modification, are permitted provided that the following conditions 40 * are met: 41 * 1. Redistributions of source code must retain the above copyright 42 * notice, this list of conditions and the following disclaimer. 43 * 2. Redistributions in binary form must reproduce the above copyright 44 * notice, this list of conditions and the following disclaimer in the 45 * documentation and/or other materials provided with the distribution. 46 * 3. Neither the name of the University nor the names of its contributors 47 * may be used to endorse or promote products derived from this software 48 * without specific prior written permission. 49 * 50 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 51 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 52 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 53 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 54 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 55 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 56 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 57 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 58 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 59 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 60 * SUCH DAMAGE. 61 * 62 * @(#)kern_proc.c 8.7 (Berkeley) 2/14/95 63 */ 64 65 #include <sys/cdefs.h> 66 __KERNEL_RCSID(0, "$NetBSD: kern_proc.c,v 1.287 2026/09/27 22:00:33 riastradh Exp $"); 67 68 #ifdef _KERNEL_OPT 69 #include "opt_kstack.h" 70 #include "opt_maxuprc.h" 71 #include "opt_dtrace.h" 72 #include "opt_compat_netbsd32.h" 73 #include "opt_kaslr.h" 74 #endif 75 76 #if defined(__HAVE_COMPAT_NETBSD32) && !defined(COMPAT_NETBSD32) \ 77 && !defined(_RUMPKERNEL) 78 #define COMPAT_NETBSD32 79 #endif 80 81 #include <sys/param.h> 82 #include <sys/types.h> 83 84 #include <sys/acct.h> 85 #include <sys/atomic.h> 86 #include <sys/buf.h> 87 #include <sys/compat_stub.h> 88 #include <sys/cpu.h> 89 #include <sys/dtrace_bsd.h> 90 #include <sys/exec.h> 91 #include <sys/file.h> 92 #include <sys/filedesc.h> 93 #include <sys/futex.h> 94 #include <sys/ioctl.h> 95 #include <sys/kauth.h> 96 #include <sys/kernel.h> 97 #include <sys/kmem.h> 98 #include <sys/namei.h> 99 #include <sys/pool.h> 100 #include <sys/proc.h> 101 #include <sys/pserialize.h> 102 #include <sys/pset.h> 103 #include <sys/ras.h> 104 #include <sys/resourcevar.h> 105 #include <sys/sdt.h> 106 #include <sys/signalvar.h> 107 #include <sys/sleepq.h> 108 #include <sys/stdalign.h> 109 #include <sys/syscall_stats.h> 110 #include <sys/sysctl.h> 111 #include <sys/systm.h> 112 #include <sys/tty.h> 113 #include <sys/uio.h> 114 #include <sys/wait.h> 115 #include <ufs/ufs/quota.h> 116 117 #include <uvm/uvm_extern.h> 118 119 /* 120 * Process lists. 121 */ 122 123 struct proclist allproc __cacheline_aligned; 124 struct proclist zombproc __cacheline_aligned; 125 126 kmutex_t proc_lock __cacheline_aligned; 127 static pserialize_t proc_psz; 128 129 /* 130 * pid to lwp/proc lookup is done by indexing the pid_table array. 131 * Since pid numbers are only allocated when an empty slot 132 * has been found, there is no need to search any lists ever. 133 * (an orphaned pgrp will lock the slot, a session will lock 134 * the pgrp with the same number.) 135 * If the table is too small it is reallocated with twice the 136 * previous size and the entries 'unzipped' into the two halves. 137 * A linked list of free entries is passed through the pt_lwp 138 * field of 'free' items - set odd to be an invalid ptr. Two 139 * additional bits are also used to indicate if the slot is 140 * currently occupied by a proc or lwp, and if the PID is 141 * hidden from certain kinds of lookups. We thus require a 142 * minimum alignment for proc and lwp structures (LWPs are 143 * at least 32-byte aligned). 144 */ 145 146 struct pid_table { 147 uintptr_t pt_slot; 148 struct pgrp *pt_pgrp; 149 pid_t pt_pid; 150 }; 151 152 #define PT_F_FREE ((uintptr_t)__BIT(0)) 153 #define PT_F_LWP 0 /* pseudo-flag */ 154 #define PT_F_PROC ((uintptr_t)__BIT(1)) 155 156 #define PT_F_TYPEBITS (PT_F_FREE|PT_F_PROC) 157 #define PT_F_ALLBITS (PT_F_FREE|PT_F_PROC) 158 159 #define PT_VALID(s) (((s) & PT_F_FREE) == 0) 160 #define PT_RESERVED(s) ((s) == 0) 161 #define PT_NEXT(s) ((u_int)(s) >> 1) 162 #define PT_SET_FREE(pid) (((pid) << 1) | PT_F_FREE) 163 #define PT_SET_LWP(l) ((uintptr_t)(l)) 164 #define PT_SET_PROC(p) (((uintptr_t)(p)) | PT_F_PROC) 165 #define PT_SET_RESERVED 0 166 #define PT_GET_LWP(s) ((struct lwp *)((s) & ~PT_F_ALLBITS)) 167 #define PT_GET_PROC(s) ((struct proc *)((s) & ~PT_F_ALLBITS)) 168 #define PT_GET_TYPE(s) ((s) & PT_F_TYPEBITS) 169 #define PT_IS_LWP(s) (PT_GET_TYPE(s) == PT_F_LWP && (s) != 0) 170 #define PT_IS_PROC(s) (PT_GET_TYPE(s) == PT_F_PROC) 171 172 #define MIN_PROC_ALIGNMENT (PT_F_ALLBITS + 1) 173 174 /* 175 * Table of process IDs (PIDs). 176 */ 177 static struct pid_table *pid_table __read_mostly; 178 179 #define INITIAL_PID_TABLE_SIZE (1 << 5) 180 181 /* Table mask, threshold for growing and number of allocated PIDs. */ 182 static u_int pid_tbl_mask __read_mostly; 183 static u_int pid_alloc_lim __read_mostly; 184 static u_int pid_alloc_cnt __cacheline_aligned; 185 186 /* Next free, last free and maximum PIDs. */ 187 static u_int next_free_pt __cacheline_aligned; 188 static u_int last_free_pt __cacheline_aligned; 189 static pid_t pid_max __read_mostly; 190 191 /* Components of the first process -- never freed. */ 192 193 struct session session0 = { 194 .s_count = 1, 195 .s_sid = 0, 196 }; 197 struct pgrp pgrp0 = { 198 .pg_members = LIST_HEAD_INITIALIZER(&pgrp0.pg_members), 199 .pg_session = &session0, 200 }; 201 filedesc_t filedesc0; 202 struct cwdinfo cwdi0 = { 203 .cwdi_cmask = CMASK, 204 .cwdi_refcnt = 1, 205 }; 206 struct plimit limit0; 207 struct pstats pstat0; 208 struct vmspace vmspace0; 209 struct sigacts sigacts0; 210 struct proc proc0 = { 211 .p_lwps = LIST_HEAD_INITIALIZER(&proc0.p_lwps), 212 .p_sigwaiters = LIST_HEAD_INITIALIZER(&proc0.p_sigwaiters), 213 .p_nlwps = 1, 214 .p_nrlwps = 1, 215 .p_pgrp = &pgrp0, 216 .p_comm = "system", 217 /* 218 * Set P_NOCLDWAIT so that kernel threads are reparented to init(8) 219 * when they exit. init(8) can easily wait them out for us. 220 */ 221 .p_flag = PK_SYSTEM | PK_NOCLDWAIT, 222 .p_stat = SACTIVE, 223 .p_nice = NZERO, 224 .p_emul = &emul_netbsd, 225 .p_cwdi = &cwdi0, 226 .p_limit = &limit0, 227 .p_fd = &filedesc0, 228 .p_vmspace = &vmspace0, 229 .p_stats = &pstat0, 230 .p_sigacts = &sigacts0, 231 #ifdef PROC0_MD_INITIALIZERS 232 PROC0_MD_INITIALIZERS 233 #endif 234 }; 235 kauth_cred_t cred0; 236 237 static const int nofile = NOFILE; 238 static const int maxuprc = MAXUPRC; 239 240 static int sysctl_doeproc(SYSCTLFN_PROTO); 241 static int sysctl_kern_proc_args(SYSCTLFN_PROTO); 242 static int sysctl_security_expose_address(SYSCTLFN_PROTO); 243 244 #ifdef KASLR 245 static int kern_expose_address = 0; 246 #else 247 static int kern_expose_address = 1; 248 #endif 249 /* 250 * The process list descriptors, used during pid allocation and 251 * by sysctl. No locking on this data structure is needed since 252 * it is completely static. 253 */ 254 const struct proclist_desc proclists[] = { 255 { &allproc }, 256 { &zombproc }, 257 { NULL }, 258 }; 259 260 static struct pgrp * pg_remove(pid_t); 261 static void pg_delete(pid_t); 262 static void orphanpg(struct pgrp *); 263 264 static specificdata_domain_t proc_specificdata_domain; 265 266 static pool_cache_t proc_cache; 267 268 static kauth_listener_t proc_listener; 269 270 static void fill_proc(const struct proc *, struct proc *, bool); 271 static int fill_pathname(struct lwp *, pid_t, void *, size_t *); 272 static int fill_cwd(struct lwp *, pid_t, void *, size_t *); 273 274 static int 275 proc_listener_cb(kauth_cred_t cred, kauth_action_t action, void *cookie, 276 void *arg0, void *arg1, void *arg2, void *arg3) 277 { 278 struct proc *p; 279 int result; 280 281 result = KAUTH_RESULT_DEFER; 282 p = arg0; 283 284 switch (action) { 285 case KAUTH_PROCESS_CANSEE: { 286 enum kauth_process_req req; 287 288 req = (enum kauth_process_req)(uintptr_t)arg1; 289 290 switch (req) { 291 case KAUTH_REQ_PROCESS_CANSEE_ARGS: 292 case KAUTH_REQ_PROCESS_CANSEE_ENTRY: 293 case KAUTH_REQ_PROCESS_CANSEE_OPENFILES: 294 case KAUTH_REQ_PROCESS_CANSEE_EPROC: 295 result = KAUTH_RESULT_ALLOW; 296 break; 297 298 case KAUTH_REQ_PROCESS_CANSEE_ENV: 299 if (kauth_cred_getuid(cred) != 300 kauth_cred_getuid(p->p_cred) || 301 kauth_cred_getuid(cred) != 302 kauth_cred_getsvuid(p->p_cred)) 303 break; 304 305 result = KAUTH_RESULT_ALLOW; 306 307 break; 308 309 case KAUTH_REQ_PROCESS_CANSEE_KPTR: 310 if (!kern_expose_address) 311 break; 312 313 if (kern_expose_address == 1 && !(p->p_flag & PK_KMEM)) 314 break; 315 316 result = KAUTH_RESULT_ALLOW; 317 318 break; 319 320 default: 321 break; 322 } 323 324 break; 325 } 326 327 case KAUTH_PROCESS_FORK: { 328 int lnprocs = (int)(unsigned long)arg2; 329 330 /* 331 * Don't allow a nonprivileged user to use the last few 332 * processes. The variable lnprocs is the current number of 333 * processes, maxproc is the limit. 334 */ 335 if (__predict_false((lnprocs >= maxproc - 5))) 336 break; 337 338 result = KAUTH_RESULT_ALLOW; 339 340 break; 341 } 342 343 case KAUTH_PROCESS_CORENAME: 344 case KAUTH_PROCESS_STOPFLAG: 345 if (proc_uidmatch(cred, p->p_cred) == 0) 346 result = KAUTH_RESULT_ALLOW; 347 348 break; 349 350 default: 351 break; 352 } 353 354 return result; 355 } 356 357 static int 358 proc_ctor(void *arg __unused, void *obj, int flags __unused) 359 { 360 struct proc *p = obj; 361 362 memset(p, 0, sizeof(*p)); 363 klist_init(&p->p_klist); 364 365 /* 366 * There is no need for a proc_dtor() to do a klist_fini(), 367 * since knote_proc_exit() ensures that p->p_klist is empty 368 * when a process exits. 369 */ 370 371 return 0; 372 } 373 374 static pid_t proc_alloc_pid_slot(struct proc *, uintptr_t); 375 376 /* 377 * Initialize global process hashing structures. 378 */ 379 void 380 procinit(void) 381 { 382 const struct proclist_desc *pd; 383 u_int i; 384 #define LINK_EMPTY ((PID_MAX + INITIAL_PID_TABLE_SIZE) & ~(INITIAL_PID_TABLE_SIZE - 1)) 385 386 for (pd = proclists; pd->pd_list != NULL; pd++) 387 LIST_INIT(pd->pd_list); 388 389 mutex_init(&proc_lock, MUTEX_DEFAULT, IPL_NONE); 390 391 proc_psz = pserialize_create(); 392 393 pid_table = kmem_alloc(INITIAL_PID_TABLE_SIZE 394 * sizeof(struct pid_table), KM_SLEEP); 395 pid_tbl_mask = INITIAL_PID_TABLE_SIZE - 1; 396 pid_max = PID_MAX; 397 398 /* Set free list running through table... 399 Preset 'use count' above PID_MAX so we allocate pid 1 next. */ 400 for (i = 0; i <= pid_tbl_mask; i++) { 401 pid_table[i].pt_slot = PT_SET_FREE(LINK_EMPTY + i + 1); 402 pid_table[i].pt_pgrp = 0; 403 pid_table[i].pt_pid = 0; 404 } 405 /* slot 0 is just grabbed */ 406 next_free_pt = 1; 407 /* Need to fix last entry. */ 408 last_free_pt = pid_tbl_mask; 409 pid_table[last_free_pt].pt_slot = PT_SET_FREE(LINK_EMPTY); 410 /* point at which we grow table - to avoid reusing pids too often */ 411 pid_alloc_lim = pid_tbl_mask - 1; 412 #undef LINK_EMPTY 413 414 /* Reserve PID 1 for init(8). */ /* XXX slightly gross */ 415 mutex_enter(&proc_lock); 416 if (proc_alloc_pid_slot(&proc0, PT_SET_RESERVED) != 1) 417 panic("failed to reserve PID 1 for init(8)"); 418 mutex_exit(&proc_lock); 419 420 proc_specificdata_domain = specificdata_domain_create(); 421 KASSERT(proc_specificdata_domain != NULL); 422 423 size_t proc_alignment = coherency_unit; 424 if (proc_alignment < MIN_PROC_ALIGNMENT) 425 proc_alignment = MIN_PROC_ALIGNMENT; 426 if (proc_alignment < alignof(struct proc)) 427 proc_alignment = alignof(struct proc); 428 429 proc_cache = pool_cache_init(sizeof(struct proc), proc_alignment, 0, 0, 430 "procpl", NULL, IPL_NONE, proc_ctor, NULL, NULL); 431 432 proc_listener = kauth_listen_scope(KAUTH_SCOPE_PROCESS, 433 proc_listener_cb, NULL); 434 } 435 436 void 437 procinit_sysctl(void) 438 { 439 static struct sysctllog *clog; 440 441 sysctl_createv(&clog, 0, NULL, NULL, 442 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 443 CTLTYPE_INT, "expose_address", 444 SYSCTL_DESCR("Enable exposing kernel addresses"), 445 sysctl_security_expose_address, 0, 446 &kern_expose_address, 0, CTL_KERN, CTL_CREATE, CTL_EOL); 447 sysctl_createv(&clog, 0, NULL, NULL, 448 CTLFLAG_PERMANENT, 449 CTLTYPE_NODE, "proc", 450 SYSCTL_DESCR("System-wide process information"), 451 sysctl_doeproc, 0, NULL, 0, 452 CTL_KERN, KERN_PROC, CTL_EOL); 453 sysctl_createv(&clog, 0, NULL, NULL, 454 CTLFLAG_PERMANENT, 455 CTLTYPE_NODE, "proc2", 456 SYSCTL_DESCR("Machine-independent process information"), 457 sysctl_doeproc, 0, NULL, 0, 458 CTL_KERN, KERN_PROC2, CTL_EOL); 459 sysctl_createv(&clog, 0, NULL, NULL, 460 CTLFLAG_PERMANENT, 461 CTLTYPE_NODE, "proc_args", 462 SYSCTL_DESCR("Process argument information"), 463 sysctl_kern_proc_args, 0, NULL, 0, 464 CTL_KERN, KERN_PROC_ARGS, CTL_EOL); 465 466 /* 467 "nodes" under these: 468 469 KERN_PROC_ALL 470 KERN_PROC_PID pid 471 KERN_PROC_PGRP pgrp 472 KERN_PROC_SESSION sess 473 KERN_PROC_TTY tty 474 KERN_PROC_UID uid 475 KERN_PROC_RUID uid 476 KERN_PROC_GID gid 477 KERN_PROC_RGID gid 478 479 all in all, probably not worth the effort... 480 */ 481 } 482 483 /* 484 * Initialize process 0. 485 */ 486 void 487 proc0_init(void) 488 { 489 struct proc *p; 490 struct pgrp *pg; 491 struct rlimit *rlim; 492 rlim_t lim; 493 int i; 494 495 p = &proc0; 496 pg = &pgrp0; 497 498 mutex_init(&p->p_stmutex, MUTEX_DEFAULT, IPL_HIGH); 499 mutex_init(&p->p_auxlock, MUTEX_DEFAULT, IPL_NONE); 500 p->p_lock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_NONE); 501 502 rw_init(&p->p_reflock); 503 cv_init(&p->p_waitcv, "wait"); 504 cv_init(&p->p_lwpcv, "lwpwait"); 505 506 LIST_INSERT_HEAD(&p->p_lwps, &lwp0, l_sibling); 507 508 KASSERT(lwp0.l_lid == 0); 509 pid_table[lwp0.l_lid].pt_slot = PT_SET_LWP(&lwp0); 510 LIST_INSERT_HEAD(&allproc, p, p_list); 511 512 pid_table[lwp0.l_lid].pt_pgrp = pg; 513 LIST_INSERT_HEAD(&pg->pg_members, p, p_pglist); 514 515 #ifdef __HAVE_SYSCALL_INTERN 516 (*p->p_emul->e_syscall_intern)(p); 517 #endif 518 519 /* Create credentials. */ 520 cred0 = kauth_cred_alloc(); 521 p->p_cred = cred0; 522 523 /* Create the CWD info. */ 524 rw_init(&cwdi0.cwdi_lock); 525 526 /* Create the limits structures. */ 527 mutex_init(&limit0.pl_lock, MUTEX_DEFAULT, IPL_NONE); 528 529 rlim = limit0.pl_rlimit; 530 for (i = 0; i < __arraycount(limit0.pl_rlimit); i++) { 531 rlim[i].rlim_cur = RLIM_INFINITY; 532 rlim[i].rlim_max = RLIM_INFINITY; 533 } 534 535 rlim[RLIMIT_NOFILE].rlim_max = maxfiles; 536 rlim[RLIMIT_NOFILE].rlim_cur = maxfiles < nofile ? maxfiles : nofile; 537 538 rlim[RLIMIT_NPROC].rlim_max = maxproc; 539 rlim[RLIMIT_NPROC].rlim_cur = maxproc < maxuprc ? maxproc : maxuprc; 540 541 lim = MIN(VM_MAXUSER_ADDRESS, ctob((rlim_t)uvm_availmem(false))); 542 rlim[RLIMIT_RSS].rlim_max = lim; 543 rlim[RLIMIT_MEMLOCK].rlim_max = lim; 544 rlim[RLIMIT_MEMLOCK].rlim_cur = lim / 3; 545 546 rlim[RLIMIT_NTHR].rlim_max = maxlwp; 547 rlim[RLIMIT_NTHR].rlim_cur = maxlwp / 2; 548 549 /* Note that default core name has zero length. */ 550 limit0.pl_corename = defcorename; 551 limit0.pl_cnlen = 0; 552 limit0.pl_refcnt = 1; 553 limit0.pl_writeable = false; 554 limit0.pl_sv_limit = NULL; 555 556 /* Configure virtual memory system, set vm rlimits. */ 557 uvm_init_limits(p); 558 559 /* Initialize file descriptor table for proc0. */ 560 fd_init(&filedesc0); 561 562 /* 563 * Initialize proc0's vmspace, which uses the kernel pmap. 564 * All kernel processes (which never have user space mappings) 565 * share proc0's vmspace, and thus, the kernel pmap. 566 */ 567 uvmspace_init(&vmspace0, pmap_kernel(), round_page(VM_MIN_ADDRESS), 568 trunc_page(VM_MAXUSER_ADDRESS), 569 #ifdef __USE_TOPDOWN_VM 570 true 571 #else 572 false 573 #endif 574 ); 575 576 /* Initialize signal state for proc0. XXX IPL_SCHED */ 577 mutex_init(&p->p_sigacts->sa_mutex, MUTEX_DEFAULT, IPL_SCHED); 578 siginit(p); 579 580 proc_initspecific(p); 581 kdtrace_proc_ctor(NULL, p); 582 } 583 584 /* 585 * Session reference counting. 586 */ 587 588 void 589 proc_sesshold(struct session *ss) 590 { 591 592 KASSERT(mutex_owned(&proc_lock)); 593 ss->s_count++; 594 } 595 596 void 597 proc_sessrele(struct session *ss) 598 { 599 struct pgrp *pg; 600 601 KASSERT(mutex_owned(&proc_lock)); 602 KASSERT(ss->s_count > 0); 603 604 /* 605 * We keep the pgrp with the same id as the session in order to 606 * stop a process being given the same pid. Since the pgrp holds 607 * a reference to the session, it must be a 'zombie' pgrp by now. 608 */ 609 if (--ss->s_count == 0) { 610 pg = pg_remove(ss->s_sid); 611 } else { 612 pg = NULL; 613 ss = NULL; 614 } 615 616 mutex_exit(&proc_lock); 617 618 if (pg) 619 kmem_free(pg, sizeof(struct pgrp)); 620 if (ss) 621 kmem_free(ss, sizeof(struct session)); 622 } 623 624 /* 625 * Check that the specified process group is in the session of the 626 * specified process. 627 * Treats -ve ids as process ids. 628 * Used to validate TIOCSPGRP requests. 629 */ 630 int 631 pgid_in_session(struct proc *p, pid_t pg_id) 632 { 633 struct pgrp *pgrp; 634 struct session *session; 635 int error; 636 637 if (pg_id <= INT_MIN) 638 return SET_ERROR(EINVAL); 639 640 mutex_enter(&proc_lock); 641 if (pg_id < 0) { 642 struct proc *p1 = proc_find(-pg_id); 643 if (p1 == NULL) { 644 error = SET_ERROR(EINVAL); 645 goto fail; 646 } 647 pgrp = p1->p_pgrp; 648 } else { 649 pgrp = pgrp_find(pg_id); 650 if (pgrp == NULL) { 651 error = SET_ERROR(EINVAL); 652 goto fail; 653 } 654 } 655 session = pgrp->pg_session; 656 error = (session != p->p_pgrp->pg_session) ? SET_ERROR(EPERM) : 0; 657 fail: 658 mutex_exit(&proc_lock); 659 return error; 660 } 661 662 /* 663 * p_inferior: is p an inferior of q? 664 */ 665 static inline bool 666 p_inferior(struct proc *p, struct proc *q) 667 { 668 669 KASSERT(mutex_owned(&proc_lock)); 670 671 for (; p != q; p = p->p_pptr) 672 if (p->p_pid == 0) 673 return false; 674 return true; 675 } 676 677 /* 678 * proc_find_lwp: locate an lwp in said proc by the ID. 679 * 680 * => Must be called with p::p_lock held. 681 * => LSIDL lwps are not returned because they are only partially 682 * constructed while occupying the slot. 683 * => Callers need to be careful about lwp::l_stat of the returned 684 * lwp. 685 */ 686 struct lwp * 687 proc_find_lwp(proc_t *p, pid_t pid) 688 { 689 struct pid_table *pt; 690 unsigned pt_mask; 691 struct lwp *l = NULL; 692 uintptr_t slot; 693 int s; 694 695 KASSERT(mutex_owned(p->p_lock)); 696 697 /* 698 * Look in the pid_table. This is done unlocked inside a 699 * pserialize read section covering pid_table's memory 700 * allocation only, so take care to read things in the correct 701 * order: 702 * 703 * 1. First read the table mask -- this only ever increases, in 704 * expand_pid_table, so a stale value is safely 705 * conservative. 706 * 707 * 2. Next read the pid table -- this is always set _before_ 708 * the mask increases, so if we see a new table and stale 709 * mask, the mask is still valid for the table. 710 */ 711 s = pserialize_read_enter(); 712 pt_mask = atomic_load_acquire(&pid_tbl_mask); 713 pt = &atomic_load_consume(&pid_table)[pid & pt_mask]; 714 slot = atomic_load_consume(&pt->pt_slot); 715 if (__predict_false(!PT_IS_LWP(slot))) { 716 pserialize_read_exit(s); 717 return NULL; 718 } 719 720 /* 721 * Check to see if the LWP is from the correct process. We won't 722 * see entries in pid_table from a prior process that also used "p", 723 * by virtue of the fact that allocating "p" means all prior updates 724 * to dependant data structures are visible to this thread. 725 */ 726 l = PT_GET_LWP(slot); 727 if (__predict_false(atomic_load_relaxed(&l->l_proc) != p)) { 728 pserialize_read_exit(s); 729 return NULL; 730 } 731 732 /* 733 * We now know that p->p_lock holds this LWP stable. 734 * 735 * If the status is not LSIDL, it means the LWP is intended to be 736 * findable by LID and l_lid cannot change behind us. 737 * 738 * No need to acquire the LWP's lock to check for LSIDL, as 739 * p->p_lock must be held to transition in and out of LSIDL. 740 * Any other observed state of is no particular interest. 741 */ 742 pserialize_read_exit(s); 743 return l->l_stat != LSIDL && l->l_lid == pid ? l : NULL; 744 } 745 746 /* 747 * proc_find_lwp_unlocked: locate an lwp in said proc by the ID. 748 * 749 * => Called in a pserialize read section with no locks held. 750 * => LSIDL lwps are not returned because they are only partially 751 * constructed while occupying the slot. 752 * => Callers need to be careful about lwp::l_stat of the returned 753 * lwp. 754 * => If an LWP is found, it's returned locked. 755 */ 756 struct lwp * 757 proc_find_lwp_unlocked(proc_t *p, pid_t pid) 758 { 759 struct pid_table *pt; 760 unsigned pt_mask; 761 struct lwp *l = NULL; 762 uintptr_t slot; 763 764 KASSERT(pserialize_in_read_section()); 765 766 /* 767 * Look in the pid_table. This is done unlocked inside a 768 * pserialize read section covering pid_table's memory 769 * allocation only, so take care to read things in the correct 770 * order: 771 * 772 * 1. First read the table mask -- this only ever increases, in 773 * expand_pid_table, so a stale value is safely 774 * conservative. 775 * 776 * 2. Next read the pid table -- this is always set _before_ 777 * the mask increases, so if we see a new table and stale 778 * mask, the mask is still valid for the table. 779 */ 780 pt_mask = atomic_load_acquire(&pid_tbl_mask); 781 pt = &atomic_load_consume(&pid_table)[pid & pt_mask]; 782 slot = atomic_load_consume(&pt->pt_slot); 783 if (__predict_false(!PT_IS_LWP(slot))) { 784 return NULL; 785 } 786 787 /* 788 * Lock the LWP we found to get it stable. If it's embryonic or 789 * reaped (LSIDL) then none of the other fields can safely be 790 * checked. 791 */ 792 l = PT_GET_LWP(slot); 793 lwp_lock(l); 794 if (__predict_false(l->l_stat == LSIDL)) { 795 lwp_unlock(l); 796 return NULL; 797 } 798 799 /* 800 * l_proc and l_lid are now known stable because the LWP is not 801 * LSIDL, so check those fields too to make sure we found the 802 * right thing. 803 */ 804 if (__predict_false(l->l_proc != p || l->l_lid != pid)) { 805 lwp_unlock(l); 806 return NULL; 807 } 808 809 /* Everything checks out, return it locked. */ 810 return l; 811 } 812 813 /* 814 * proc_find_lwp_acquire_proc: locate an lwp and acquire a lock 815 * on its containing proc. 816 * 817 * => Similar to proc_find_lwp(), but does not require you to have 818 * the proc a priori. 819 * => Also returns proc * to caller, with p::p_lock held. 820 * => Same caveats apply. 821 */ 822 struct lwp * 823 proc_find_lwp_acquire_proc(pid_t pid, struct proc **pp) 824 { 825 struct pid_table *pt; 826 struct proc *p = NULL; 827 struct lwp *l = NULL; 828 uintptr_t slot; 829 830 KASSERT(pp != NULL); 831 mutex_enter(&proc_lock); 832 pt = &pid_table[pid & pid_tbl_mask]; 833 834 slot = pt->pt_slot; 835 if (__predict_true(PT_IS_LWP(slot) && pt->pt_pid == pid)) { 836 l = PT_GET_LWP(slot); 837 p = l->l_proc; 838 mutex_enter(p->p_lock); 839 if (__predict_false(l->l_stat == LSIDL)) { 840 mutex_exit(p->p_lock); 841 l = NULL; 842 p = NULL; 843 } 844 } 845 mutex_exit(&proc_lock); 846 847 KASSERT(p == NULL || mutex_owned(p->p_lock)); 848 *pp = p; 849 return l; 850 } 851 852 /* 853 * proc_find_raw_pid_table_locked: locate a process by the ID. 854 * 855 * => Must be called with proc_lock held. 856 */ 857 static proc_t * 858 proc_find_raw_pid_table_locked(pid_t pid, bool any_lwpid) 859 { 860 struct pid_table *pt; 861 proc_t *p = NULL; 862 uintptr_t slot; 863 864 /* No - used by DDB. KASSERT(mutex_owned(&proc_lock)); */ 865 pt = &pid_table[pid & pid_tbl_mask]; 866 867 slot = pt->pt_slot; 868 if (__predict_true(PT_IS_LWP(slot) && pt->pt_pid == pid)) { 869 /* 870 * When looking up processes, require a direct match 871 * on the PID assigned to the proc, not just one of 872 * its LWPs. 873 * 874 * N.B. We require lwp::l_proc of LSIDL LWPs to be 875 * valid here. 876 */ 877 p = PT_GET_LWP(slot)->l_proc; 878 if (__predict_false(p->p_pid != pid && !any_lwpid)) 879 p = NULL; 880 } else if (PT_IS_PROC(slot) && pt->pt_pid == pid) { 881 p = PT_GET_PROC(slot); 882 } 883 return p; 884 } 885 886 proc_t * 887 proc_find_raw(pid_t pid) 888 { 889 890 return proc_find_raw_pid_table_locked(pid, false); 891 } 892 893 static proc_t * 894 proc_find_internal(pid_t pid, bool any_lwpid) 895 { 896 proc_t *p; 897 898 KASSERT(mutex_owned(&proc_lock)); 899 900 p = proc_find_raw_pid_table_locked(pid, any_lwpid); 901 if (__predict_false(p == NULL)) { 902 return NULL; 903 } 904 905 /* 906 * Only allow live processes to be found by PID. 907 * XXX: p_stat might change, since proc unlocked. 908 */ 909 if (__predict_true(p->p_stat == SACTIVE || p->p_stat == SSTOP)) { 910 return p; 911 } 912 return NULL; 913 } 914 915 proc_t * 916 proc_find(pid_t pid) 917 { 918 return proc_find_internal(pid, false); 919 } 920 921 proc_t * 922 proc_find_lwpid(pid_t pid) 923 { 924 return proc_find_internal(pid, true); 925 } 926 927 /* 928 * pgrp_find: locate a process group by the ID. 929 * 930 * => Must be called with proc_lock held. 931 */ 932 struct pgrp * 933 pgrp_find(pid_t pgid) 934 { 935 struct pgrp *pg; 936 937 KASSERT(mutex_owned(&proc_lock)); 938 939 pg = pid_table[pgid & pid_tbl_mask].pt_pgrp; 940 941 /* 942 * Cannot look up a process group that only exists because the 943 * session has not died yet (traditional). 944 */ 945 if (pg == NULL || pg->pg_id != pgid || LIST_EMPTY(&pg->pg_members)) { 946 return NULL; 947 } 948 return pg; 949 } 950 951 static void 952 expand_pid_table(void) 953 { 954 size_t pt_size, tsz; 955 struct pid_table *n_pt, *new_pt; 956 uintptr_t slot; 957 struct pgrp *pgrp; 958 pid_t pid, rpid; 959 u_int i; 960 uint new_pt_mask; 961 962 KASSERT(mutex_owned(&proc_lock)); 963 964 /* Unlock the pid_table briefly to allocate memory. */ 965 pt_size = pid_tbl_mask + 1; 966 mutex_exit(&proc_lock); 967 968 tsz = pt_size * 2 * sizeof(struct pid_table); 969 new_pt = kmem_alloc(tsz, KM_SLEEP); 970 new_pt_mask = pt_size * 2 - 1; 971 972 /* XXX For now. The practical limit is much lower anyway. */ 973 KASSERT(new_pt_mask <= FUTEX_TID_MASK); 974 975 mutex_enter(&proc_lock); 976 if (pt_size != pid_tbl_mask + 1) { 977 /* Another process beat us to it... */ 978 mutex_exit(&proc_lock); 979 kmem_free(new_pt, tsz); 980 goto out; 981 } 982 983 /* 984 * Copy entries from old table into new one. 985 * If 'pid' is 'odd' we need to place in the upper half, 986 * even pid's to the lower half. 987 * Free items stay in the low half so we don't have to 988 * fixup the reference to them. 989 * We stuff free items on the front of the freelist 990 * because we can't write to unmodified entries. 991 * Processing the table backwards maintains a semblance 992 * of issuing pid numbers that increase with time. 993 */ 994 i = pt_size - 1; 995 n_pt = new_pt + i; 996 for (; ; i--, n_pt--) { 997 slot = pid_table[i].pt_slot; 998 pgrp = pid_table[i].pt_pgrp; 999 if (!PT_VALID(slot)) { 1000 /* Up 'use count' so that link is valid */ 1001 pid = (PT_NEXT(slot) + pt_size) & ~pt_size; 1002 rpid = 0; 1003 slot = PT_SET_FREE(pid); 1004 if (pgrp) 1005 pid = pgrp->pg_id; 1006 } else { 1007 pid = pid_table[i].pt_pid; 1008 rpid = pid; 1009 } 1010 1011 /* Save entry in appropriate half of table */ 1012 n_pt[pid & pt_size].pt_slot = slot; 1013 n_pt[pid & pt_size].pt_pgrp = pgrp; 1014 n_pt[pid & pt_size].pt_pid = rpid; 1015 1016 /* Put other piece on start of free list */ 1017 pid = (pid ^ pt_size) & ~pid_tbl_mask; 1018 n_pt[pid & pt_size].pt_slot = 1019 PT_SET_FREE((pid & ~pt_size) | next_free_pt); 1020 n_pt[pid & pt_size].pt_pgrp = 0; 1021 n_pt[pid & pt_size].pt_pid = 0; 1022 1023 next_free_pt = i | (pid & pt_size); 1024 if (i == 0) 1025 break; 1026 } 1027 1028 /* Save old table size and switch tables */ 1029 tsz = pt_size * sizeof(struct pid_table); 1030 n_pt = pid_table; 1031 atomic_store_release(&pid_table, new_pt); 1032 KASSERT(new_pt_mask >= pid_tbl_mask); 1033 atomic_store_release(&pid_tbl_mask, new_pt_mask); 1034 1035 /* 1036 * pid_max starts as PID_MAX (= 30000), once we have 16384 1037 * allocated pids we need it to be larger! 1038 */ 1039 if (pid_tbl_mask > PID_MAX) { 1040 pid_max = pid_tbl_mask * 2 + 1; 1041 pid_alloc_lim |= pid_alloc_lim << 1; 1042 } else 1043 pid_alloc_lim <<= 1; /* doubles number of free slots... */ 1044 1045 mutex_exit(&proc_lock); 1046 1047 /* 1048 * Make sure that unlocked access to the old pid_table is complete 1049 * and then free it. 1050 */ 1051 pserialize_perform(proc_psz); 1052 kmem_free(n_pt, tsz); 1053 1054 out: /* Return with proc_lock held again. */ 1055 mutex_enter(&proc_lock); 1056 } 1057 1058 struct proc * 1059 proc_alloc(void) 1060 { 1061 struct proc *p; 1062 1063 p = pool_cache_get(proc_cache, PR_WAITOK); 1064 p->p_stat = SIDL; /* protect against others */ 1065 proc_initspecific(p); 1066 kdtrace_proc_ctor(NULL, p); 1067 1068 /* 1069 * Allocate a placeholder in the pid_table. When we create the 1070 * first LWP for this process, it will take ownership of the 1071 * slot. 1072 */ 1073 if (__predict_false(proc_alloc_pid(p) == -1)) { 1074 /* Allocating the PID failed; unwind. */ 1075 proc_finispecific(p); 1076 proc_free_mem(p); 1077 p = NULL; 1078 } 1079 return p; 1080 } 1081 1082 /* 1083 * proc_alloc_pid_slot: allocate PID and record the occupant so that 1084 * proc_find_raw() can find it by the PID. 1085 */ 1086 static pid_t __noinline 1087 proc_alloc_pid_slot(struct proc *p, uintptr_t slot) 1088 { 1089 struct pid_table *pt; 1090 pid_t pid; 1091 int nxt; 1092 1093 KASSERT(mutex_owned(&proc_lock)); 1094 1095 for (;;expand_pid_table()) { 1096 if (__predict_false(pid_alloc_cnt >= pid_alloc_lim)) { 1097 /* ensure pids cycle through 2000+ values */ 1098 continue; 1099 } 1100 /* 1101 * The first user process *must* be given PID 1. 1102 * it has already been reserved for us. This 1103 * will be coming in from the proc_alloc() call 1104 * above, and the entry will be usurped later when 1105 * the first user LWP is created. 1106 * XXX this is slightly gross. 1107 */ 1108 if (__predict_false(PT_RESERVED(pid_table[1].pt_slot) && 1109 p != &proc0)) { 1110 KASSERT(PT_IS_PROC(slot)); 1111 pt = &pid_table[1]; 1112 pt->pt_slot = slot; 1113 return 1; 1114 } 1115 pt = &pid_table[next_free_pt]; 1116 #ifdef DIAGNOSTIC 1117 if (__predict_false(PT_VALID(pt->pt_slot) || pt->pt_pgrp)) 1118 panic("proc_alloc: slot busy"); 1119 #endif 1120 nxt = PT_NEXT(pt->pt_slot); 1121 if (nxt & pid_tbl_mask) 1122 break; 1123 /* Table full - expand (NB last entry not used....) */ 1124 } 1125 1126 /* pid is 'saved use count' + 'size' + entry */ 1127 pid = (nxt & ~pid_tbl_mask) + pid_tbl_mask + 1 + next_free_pt; 1128 if ((uint)pid > (uint)pid_max) 1129 pid &= pid_tbl_mask; 1130 next_free_pt = nxt & pid_tbl_mask; 1131 1132 /* XXX For now. The practical limit is much lower anyway. */ 1133 KASSERT(pid <= FUTEX_TID_MASK); 1134 1135 /* Grab table slot */ 1136 pt->pt_slot = slot; 1137 1138 KASSERT(pt->pt_pid == 0); 1139 pt->pt_pid = pid; 1140 pid_alloc_cnt++; 1141 1142 return pid; 1143 } 1144 1145 pid_t 1146 proc_alloc_pid(struct proc *p) 1147 { 1148 pid_t pid; 1149 1150 KASSERT((((uintptr_t)p) & PT_F_ALLBITS) == 0); 1151 KASSERT(p->p_stat == SIDL); 1152 1153 mutex_enter(&proc_lock); 1154 pid = proc_alloc_pid_slot(p, PT_SET_PROC(p)); 1155 if (pid != -1) 1156 p->p_pid = pid; 1157 mutex_exit(&proc_lock); 1158 1159 return pid; 1160 } 1161 1162 pid_t 1163 proc_alloc_lwpid(struct proc *p, struct lwp *l) 1164 { 1165 struct pid_table *pt; 1166 pid_t pid; 1167 1168 KASSERT((((uintptr_t)l) & PT_F_ALLBITS) == 0); 1169 KASSERT(l->l_proc == p); 1170 KASSERT(l->l_stat == LSIDL); 1171 1172 /* 1173 * For unlocked lookup in proc_find_lwp(), make sure l->l_proc 1174 * is globally visible before the LWP becomes visible via the 1175 * pid_table. 1176 */ 1177 #ifndef __HAVE_ATOMIC_AS_MEMBAR 1178 membar_producer(); 1179 #endif 1180 1181 /* 1182 * If the slot for p->p_pid currently points to the proc, 1183 * then we should usurp this ID for the LWP. This happens 1184 * at least once per process (for the first LWP), and can 1185 * happen again if the first LWP for a process exits and 1186 * before the process creates another. 1187 */ 1188 mutex_enter(&proc_lock); 1189 pid = p->p_pid; 1190 pt = &pid_table[pid & pid_tbl_mask]; 1191 KASSERT(pt->pt_pid == pid); 1192 if (PT_IS_PROC(pt->pt_slot)) { 1193 KASSERT(PT_GET_PROC(pt->pt_slot) == p); 1194 l->l_lid = pid; 1195 pt->pt_slot = PT_SET_LWP(l); 1196 } else { 1197 /* Need to allocate a new slot. */ 1198 pid = proc_alloc_pid_slot(p, PT_SET_LWP(l)); 1199 if (pid != -1) 1200 l->l_lid = pid; 1201 } 1202 mutex_exit(&proc_lock); 1203 1204 return pid; 1205 } 1206 1207 static void __noinline 1208 proc_free_pid_internal(pid_t pid, uintptr_t type __diagused) 1209 { 1210 struct pid_table *pt; 1211 1212 KASSERT(mutex_owned(&proc_lock)); 1213 1214 pt = &pid_table[pid & pid_tbl_mask]; 1215 1216 KASSERT(PT_GET_TYPE(pt->pt_slot) == type); 1217 KASSERT(pt->pt_pid == pid); 1218 1219 /* save pid use count in slot */ 1220 pt->pt_slot = PT_SET_FREE(pid & ~pid_tbl_mask); 1221 pt->pt_pid = 0; 1222 1223 if (pt->pt_pgrp == NULL) { 1224 /* link last freed entry onto ours */ 1225 pid &= pid_tbl_mask; 1226 pt = &pid_table[last_free_pt]; 1227 pt->pt_slot = PT_SET_FREE(PT_NEXT(pt->pt_slot) | pid); 1228 pt->pt_pid = 0; 1229 last_free_pt = pid; 1230 pid_alloc_cnt--; 1231 } 1232 } 1233 1234 /* 1235 * Free a process id - called from proc_free (in kern_exit.c) 1236 * 1237 * Called with the proc_lock held. 1238 */ 1239 void 1240 proc_free_pid(pid_t pid) 1241 { 1242 1243 KASSERT(mutex_owned(&proc_lock)); 1244 proc_free_pid_internal(pid, PT_F_PROC); 1245 } 1246 1247 /* 1248 * Free a process id used by an LWP. If this was the process's 1249 * first LWP, we convert the slot to point to the process; the 1250 * entry will get cleaned up later when the process finishes exiting. 1251 * 1252 * If not, then it's the same as proc_free_pid(). 1253 */ 1254 void 1255 proc_free_lwpid(struct proc *p, pid_t pid) 1256 { 1257 1258 KASSERT(mutex_owned(&proc_lock)); 1259 1260 if (__predict_true(p->p_pid == pid)) { 1261 struct pid_table *pt; 1262 1263 pt = &pid_table[pid & pid_tbl_mask]; 1264 1265 KASSERT(pt->pt_pid == pid); 1266 KASSERT(PT_IS_LWP(pt->pt_slot)); 1267 KASSERT(PT_GET_LWP(pt->pt_slot)->l_proc == p); 1268 1269 pt->pt_slot = PT_SET_PROC(p); 1270 return; 1271 } 1272 proc_free_pid_internal(pid, PT_F_LWP); 1273 } 1274 1275 void 1276 proc_free_mem(struct proc *p) 1277 { 1278 1279 kdtrace_proc_dtor(NULL, p); 1280 pool_cache_put(proc_cache, p); 1281 } 1282 1283 /* 1284 * proc_enterpgrp: move p to a new or existing process group (and session). 1285 * 1286 * If we are creating a new pgrp, the pgid should equal 1287 * the calling process' pid. 1288 * If is only valid to enter a process group that is in the session 1289 * of the process. 1290 * Also mksess should only be set if we are creating a process group 1291 * 1292 * Only called from sys_setsid, sys_setpgid and posix_spawn/spawn_return. 1293 */ 1294 int 1295 proc_enterpgrp(struct proc *curp, pid_t pid, pid_t pgid, bool mksess) 1296 { 1297 struct pgrp *new_pgrp, *pgrp; 1298 struct session *sess; 1299 struct proc *p; 1300 int rval; 1301 pid_t pg_id = NO_PGID; 1302 1303 /* Allocate data areas we might need before doing any validity checks */ 1304 sess = mksess ? kmem_alloc(sizeof(*sess), KM_SLEEP) : NULL; 1305 new_pgrp = kmem_alloc(sizeof(*new_pgrp), KM_SLEEP); 1306 1307 mutex_enter(&proc_lock); 1308 1309 /* Check pgrp exists or can be created */ 1310 pgrp = pid_table[pgid & pid_tbl_mask].pt_pgrp; 1311 if (pgrp != NULL && pgrp->pg_id != pgid) 1312 goto eperm; 1313 1314 /* Can only set another process under restricted circumstances. */ 1315 if (pid != curp->p_pid) { 1316 /* Must exist and be one of our children... */ 1317 p = proc_find_internal(pid, false); 1318 if (p == NULL || !p_inferior(p, curp)) { 1319 rval = SET_ERROR(ESRCH); 1320 goto done; 1321 } 1322 /* ... in the same session... */ 1323 if (sess != NULL || p->p_session != curp->p_session) 1324 goto eperm; 1325 /* ... existing pgid must be in same session ... */ 1326 if (pgrp != NULL && pgrp->pg_session != p->p_session) 1327 goto eperm; 1328 /* ... and not done an exec. */ 1329 if (p->p_flag & PK_EXEC) { 1330 rval = SET_ERROR(EACCES); 1331 goto done; 1332 } 1333 } else { 1334 /* ... setsid() cannot re-enter a pgrp */ 1335 if (mksess && (curp->p_pgid == curp->p_pid || 1336 pgrp_find(curp->p_pid))) 1337 goto eperm; 1338 p = curp; 1339 } 1340 1341 /* Changing the process group/session of a session 1342 leader is definitely off limits. */ 1343 if (SESS_LEADER(p)) { 1344 if (sess == NULL && p->p_pgrp == pgrp) { 1345 /* unless it's a definite noop */ 1346 rval = 0; 1347 goto done; 1348 } 1349 goto eperm; 1350 } 1351 1352 /* Can only create a process group with id of process */ 1353 if (pgrp == NULL && pgid != pid) 1354 goto eperm; 1355 1356 /* Can only create a session if creating pgrp */ 1357 if (sess != NULL && pgrp != NULL) 1358 goto eperm; 1359 1360 /* Check we allocated memory for a pgrp... */ 1361 if (pgrp == NULL && new_pgrp == NULL) 1362 goto eperm; 1363 1364 /* Don't attach to 'zombie' pgrp */ 1365 if (pgrp != NULL && LIST_EMPTY(&pgrp->pg_members)) 1366 goto eperm; 1367 1368 /* Expect to succeed now */ 1369 rval = 0; 1370 1371 if (pgrp == p->p_pgrp) 1372 /* nothing to do */ 1373 goto done; 1374 1375 /* Ok all setup, link up required structures */ 1376 1377 if (pgrp == NULL) { 1378 pgrp = new_pgrp; 1379 new_pgrp = NULL; 1380 if (sess != NULL) { 1381 sess->s_sid = p->p_pid; 1382 sess->s_leader = p; 1383 sess->s_count = 1; 1384 sess->s_ttyvp = NULL; 1385 sess->s_ttyp = NULL; 1386 sess->s_flags = p->p_session->s_flags & ~S_LOGIN_SET; 1387 memcpy(sess->s_login, p->p_session->s_login, 1388 sizeof(sess->s_login)); 1389 p->p_lflag &= ~PL_CONTROLT; 1390 } else { 1391 sess = p->p_pgrp->pg_session; 1392 proc_sesshold(sess); 1393 } 1394 pgrp->pg_session = sess; 1395 sess = NULL; 1396 1397 pgrp->pg_id = pgid; 1398 LIST_INIT(&pgrp->pg_members); 1399 #ifdef DIAGNOSTIC 1400 if (__predict_false(pid_table[pgid & pid_tbl_mask].pt_pgrp)) 1401 panic("enterpgrp: pgrp table slot in use"); 1402 if (__predict_false(mksess && p != curp)) 1403 panic("enterpgrp: mksession and p != curproc"); 1404 #endif 1405 pid_table[pgid & pid_tbl_mask].pt_pgrp = pgrp; 1406 pgrp->pg_jobc = 0; 1407 } 1408 1409 /* 1410 * Adjust eligibility of affected pgrps to participate in job control. 1411 * Increment eligibility counts before decrementing, otherwise we 1412 * could reach 0 spuriously during the first call. 1413 */ 1414 fixjobc(p, pgrp, 1); 1415 fixjobc(p, p->p_pgrp, 0); 1416 1417 /* Interlock with ttread(). */ 1418 mutex_spin_enter(&tty_lock); 1419 1420 /* Move process to requested group. */ 1421 LIST_REMOVE(p, p_pglist); 1422 if (LIST_EMPTY(&p->p_pgrp->pg_members)) 1423 /* defer delete until we've dumped the lock */ 1424 pg_id = p->p_pgrp->pg_id; 1425 p->p_pgrp = pgrp; 1426 LIST_INSERT_HEAD(&pgrp->pg_members, p, p_pglist); 1427 1428 /* Done with the swap; we can release the tty mutex. */ 1429 mutex_spin_exit(&tty_lock); 1430 goto done; 1431 1432 eperm: 1433 rval = SET_ERROR(EPERM); 1434 done: 1435 if (pg_id != NO_PGID) { 1436 /* Releases proc_lock. */ 1437 pg_delete(pg_id); 1438 } else { 1439 mutex_exit(&proc_lock); 1440 } 1441 if (sess != NULL) 1442 kmem_free(sess, sizeof(*sess)); 1443 if (new_pgrp != NULL) 1444 kmem_free(new_pgrp, sizeof(*new_pgrp)); 1445 #ifdef DEBUG_PGRP 1446 if (__predict_false(rval)) 1447 printf("enterpgrp(%d,%d,%d), curproc %d, rval %d\n", 1448 pid, pgid, mksess, curp->p_pid, rval); 1449 #endif 1450 return rval; 1451 } 1452 1453 /* 1454 * proc_leavepgrp: remove a process from its process group. 1455 * => must be called with the proc_lock held, which will be released; 1456 */ 1457 void 1458 proc_leavepgrp(struct proc *p) 1459 { 1460 struct pgrp *pgrp; 1461 1462 KASSERT(mutex_owned(&proc_lock)); 1463 1464 /* Interlock with ttread() */ 1465 mutex_spin_enter(&tty_lock); 1466 pgrp = p->p_pgrp; 1467 LIST_REMOVE(p, p_pglist); 1468 p->p_pgrp = NULL; 1469 mutex_spin_exit(&tty_lock); 1470 1471 if (LIST_EMPTY(&pgrp->pg_members)) { 1472 /* Releases proc_lock. */ 1473 pg_delete(pgrp->pg_id); 1474 } else { 1475 mutex_exit(&proc_lock); 1476 } 1477 } 1478 1479 /* 1480 * pg_remove: remove a process group from the table. 1481 * => must be called with the proc_lock held; 1482 * => returns process group to free; 1483 */ 1484 static struct pgrp * 1485 pg_remove(pid_t pg_id) 1486 { 1487 struct pgrp *pgrp; 1488 struct pid_table *pt; 1489 1490 KASSERT(mutex_owned(&proc_lock)); 1491 1492 pt = &pid_table[pg_id & pid_tbl_mask]; 1493 pgrp = pt->pt_pgrp; 1494 1495 KASSERT(pgrp != NULL); 1496 KASSERT(pgrp->pg_id == pg_id); 1497 KASSERT(LIST_EMPTY(&pgrp->pg_members)); 1498 1499 pt->pt_pgrp = NULL; 1500 1501 if (!PT_VALID(pt->pt_slot)) { 1502 /* Orphaned pgrp, put slot onto free list. */ 1503 KASSERT((PT_NEXT(pt->pt_slot) & pid_tbl_mask) == 0); 1504 pg_id &= pid_tbl_mask; 1505 pt = &pid_table[last_free_pt]; 1506 pt->pt_slot = PT_SET_FREE(PT_NEXT(pt->pt_slot) | pg_id); 1507 KASSERT(pt->pt_pid == 0); 1508 last_free_pt = pg_id; 1509 pid_alloc_cnt--; 1510 } 1511 return pgrp; 1512 } 1513 1514 /* 1515 * pg_delete: delete and free a process group. 1516 * => must be called with the proc_lock held, which will be released. 1517 */ 1518 static void 1519 pg_delete(pid_t pg_id) 1520 { 1521 struct pgrp *pg; 1522 struct tty *ttyp; 1523 struct session *ss; 1524 1525 KASSERT(mutex_owned(&proc_lock)); 1526 1527 pg = pid_table[pg_id & pid_tbl_mask].pt_pgrp; 1528 if (pg == NULL || pg->pg_id != pg_id || !LIST_EMPTY(&pg->pg_members)) { 1529 mutex_exit(&proc_lock); 1530 return; 1531 } 1532 1533 ss = pg->pg_session; 1534 1535 /* Remove reference (if any) from tty to this process group */ 1536 mutex_spin_enter(&tty_lock); 1537 ttyp = ss->s_ttyp; 1538 if (ttyp != NULL && ttyp->t_pgrp == pg) { 1539 ttyp->t_pgrp = NULL; 1540 KASSERT(ttyp->t_session == ss); 1541 } 1542 mutex_spin_exit(&tty_lock); 1543 1544 /* 1545 * The leading process group in a session is freed by proc_sessrele(), 1546 * if last reference. It will also release the locks. 1547 */ 1548 pg = (ss->s_sid != pg->pg_id) ? pg_remove(pg_id) : NULL; 1549 proc_sessrele(ss); 1550 1551 if (pg != NULL) { 1552 /* Free it, if was not done above. */ 1553 kmem_free(pg, sizeof(struct pgrp)); 1554 } 1555 } 1556 1557 /* 1558 * Adjust pgrp jobc counters when specified process changes process group. 1559 * We count the number of processes in each process group that "qualify" 1560 * the group for terminal job control (those with a parent in a different 1561 * process group of the same session). If that count reaches zero, the 1562 * process group becomes orphaned. Check both the specified process' 1563 * process group and that of its children. 1564 * entering == 0 => p is leaving specified group. 1565 * entering == 1 => p is entering specified group. 1566 * 1567 * Call with proc_lock held. 1568 */ 1569 void 1570 fixjobc(struct proc *p, struct pgrp *pgrp, int entering) 1571 { 1572 struct pgrp *hispgrp; 1573 struct session *mysession = pgrp->pg_session; 1574 struct proc *child; 1575 1576 KASSERT(mutex_owned(&proc_lock)); 1577 1578 /* 1579 * Check p's parent to see whether p qualifies its own process 1580 * group; if so, adjust count for p's process group. 1581 */ 1582 hispgrp = p->p_pptr->p_pgrp; 1583 if (hispgrp != pgrp && hispgrp->pg_session == mysession) { 1584 if (entering) { 1585 pgrp->pg_jobc++; 1586 p->p_lflag &= ~PL_ORPHANPG; 1587 } else { 1588 /* KASSERT(pgrp->pg_jobc > 0); */ 1589 if (--pgrp->pg_jobc == 0) 1590 orphanpg(pgrp); 1591 } 1592 } 1593 1594 /* 1595 * Check this process' children to see whether they qualify 1596 * their process groups; if so, adjust counts for children's 1597 * process groups. 1598 */ 1599 LIST_FOREACH(child, &p->p_children, p_sibling) { 1600 hispgrp = child->p_pgrp; 1601 if (hispgrp != pgrp && hispgrp->pg_session == mysession && 1602 !P_ZOMBIE(child)) { 1603 if (entering) { 1604 child->p_lflag &= ~PL_ORPHANPG; 1605 hispgrp->pg_jobc++; 1606 } else { 1607 /* KASSERT(hispgrp->pg_jobc > 0); */ 1608 if (--hispgrp->pg_jobc == 0) 1609 orphanpg(hispgrp); 1610 } 1611 } 1612 } 1613 } 1614 1615 /* 1616 * A process group has become orphaned; 1617 * if there are any stopped processes in the group, 1618 * hang-up all process in that group. 1619 * 1620 * Call with proc_lock held. 1621 */ 1622 static void 1623 orphanpg(struct pgrp *pg) 1624 { 1625 struct proc *p; 1626 1627 KASSERT(mutex_owned(&proc_lock)); 1628 1629 LIST_FOREACH(p, &pg->pg_members, p_pglist) { 1630 if (p->p_stat == SSTOP) { 1631 p->p_lflag |= PL_ORPHANPG; 1632 psignal(p, SIGHUP); 1633 psignal(p, SIGCONT); 1634 } 1635 } 1636 } 1637 1638 #ifdef DDB 1639 #include <ddb/db_output.h> 1640 void pidtbl_dump(void); 1641 void 1642 pidtbl_dump(void) 1643 { 1644 struct pid_table *pt; 1645 struct proc *p; 1646 struct pgrp *pgrp; 1647 uintptr_t slot; 1648 int id; 1649 1650 db_printf("pid table %p size %x, next %x, last %x\n", 1651 pid_table, pid_tbl_mask+1, 1652 next_free_pt, last_free_pt); 1653 for (pt = pid_table, id = 0; id <= pid_tbl_mask; id++, pt++) { 1654 slot = pt->pt_slot; 1655 if (!PT_VALID(slot) && !pt->pt_pgrp) 1656 continue; 1657 if (PT_IS_LWP(slot)) { 1658 p = PT_GET_LWP(slot)->l_proc; 1659 } else if (PT_IS_PROC(slot)) { 1660 p = PT_GET_PROC(slot); 1661 } else { 1662 p = NULL; 1663 } 1664 db_printf(" id %x: ", id); 1665 if (p != NULL) 1666 db_printf("slotpid %d proc %p id %d (0x%x) %s\n", 1667 pt->pt_pid, p, p->p_pid, p->p_pid, p->p_comm); 1668 else 1669 db_printf("next %x use %x\n", 1670 PT_NEXT(slot) & pid_tbl_mask, 1671 PT_NEXT(slot) & ~pid_tbl_mask); 1672 if ((pgrp = pt->pt_pgrp)) { 1673 db_printf("\tsession %p, sid %d, count %d, login %s\n", 1674 pgrp->pg_session, pgrp->pg_session->s_sid, 1675 pgrp->pg_session->s_count, 1676 pgrp->pg_session->s_login); 1677 db_printf("\tpgrp %p, pg_id %d, pg_jobc %d, members %p\n", 1678 pgrp, pgrp->pg_id, pgrp->pg_jobc, 1679 LIST_FIRST(&pgrp->pg_members)); 1680 LIST_FOREACH(p, &pgrp->pg_members, p_pglist) { 1681 db_printf("\t\tpid %d addr %p pgrp %p %s\n", 1682 p->p_pid, p, p->p_pgrp, p->p_comm); 1683 } 1684 } 1685 } 1686 } 1687 #endif /* DDB */ 1688 1689 #ifdef KSTACK_CHECK_MAGIC 1690 1691 #define KSTACK_MAGIC 0xdeadbeaf 1692 1693 /* XXX should be per process basis? */ 1694 static int kstackleftmin = KSTACK_SIZE; 1695 static int kstackleftthres = KSTACK_SIZE / 8; 1696 1697 void 1698 kstack_setup_magic(const struct lwp *l) 1699 { 1700 uint32_t *ip; 1701 uint32_t const *end; 1702 1703 KASSERT(l != NULL); 1704 KASSERT(l != &lwp0); 1705 1706 /* 1707 * fill all the stack with magic number 1708 * so that later modification on it can be detected. 1709 */ 1710 ip = (uint32_t *)KSTACK_LOWEST_ADDR(l); 1711 end = (uint32_t *)((char *)KSTACK_LOWEST_ADDR(l) + KSTACK_SIZE); 1712 for (; ip < end; ip++) { 1713 *ip = KSTACK_MAGIC; 1714 } 1715 } 1716 1717 void 1718 kstack_check_magic(const struct lwp *l) 1719 { 1720 uint32_t const *ip, *end; 1721 int stackleft; 1722 1723 KASSERT(l != NULL); 1724 1725 /* don't check proc0 */ /*XXX*/ 1726 if (l == &lwp0) 1727 return; 1728 1729 #ifdef __MACHINE_STACK_GROWS_UP 1730 /* stack grows upwards (eg. hppa) */ 1731 ip = (uint32_t *)((void *)KSTACK_LOWEST_ADDR(l) + KSTACK_SIZE); 1732 end = (uint32_t *)KSTACK_LOWEST_ADDR(l); 1733 for (ip--; ip >= end; ip--) 1734 if (*ip != KSTACK_MAGIC) 1735 break; 1736 1737 stackleft = (void *)KSTACK_LOWEST_ADDR(l) + KSTACK_SIZE - (void *)ip; 1738 #else /* __MACHINE_STACK_GROWS_UP */ 1739 /* stack grows downwards (eg. i386) */ 1740 ip = (uint32_t *)KSTACK_LOWEST_ADDR(l); 1741 end = (uint32_t *)((char *)KSTACK_LOWEST_ADDR(l) + KSTACK_SIZE); 1742 for (; ip < end; ip++) 1743 if (*ip != KSTACK_MAGIC) 1744 break; 1745 1746 stackleft = ((const char *)ip) - (const char *)KSTACK_LOWEST_ADDR(l); 1747 #endif /* __MACHINE_STACK_GROWS_UP */ 1748 1749 if (kstackleftmin > stackleft) { 1750 kstackleftmin = stackleft; 1751 if (stackleft < kstackleftthres) 1752 printf("warning: kernel stack left %d bytes" 1753 "(pid %u:lid %u)\n", stackleft, 1754 (u_int)l->l_proc->p_pid, (u_int)l->l_lid); 1755 } 1756 1757 if (stackleft <= 0) { 1758 panic("magic on the top of kernel stack changed for " 1759 "pid %u, lid %u: maybe kernel stack overflow", 1760 (u_int)l->l_proc->p_pid, (u_int)l->l_lid); 1761 } 1762 } 1763 #endif /* KSTACK_CHECK_MAGIC */ 1764 1765 int 1766 proclist_foreach_call(struct proclist *list, 1767 int (*callback)(struct proc *, void *arg), void *arg) 1768 { 1769 struct proc marker; 1770 struct proc *p; 1771 int ret = 0; 1772 1773 marker.p_flag = PK_MARKER; 1774 mutex_enter(&proc_lock); 1775 for (p = LIST_FIRST(list); ret == 0 && p != NULL;) { 1776 if (p->p_flag & PK_MARKER) { 1777 p = LIST_NEXT(p, p_list); 1778 continue; 1779 } 1780 LIST_INSERT_AFTER(p, &marker, p_list); 1781 ret = (*callback)(p, arg); 1782 KASSERT(mutex_owned(&proc_lock)); 1783 p = LIST_NEXT(&marker, p_list); 1784 LIST_REMOVE(&marker, p_list); 1785 } 1786 mutex_exit(&proc_lock); 1787 1788 return ret; 1789 } 1790 1791 /* 1792 * proc_vmspace_getref(p, &vm) 1793 * 1794 * If p is not currently exiting, acquire a reference to its 1795 * vmspace. Return 0, set vm, and increment its reference count 1796 * on success. Return nonzero error code without on failure. 1797 * 1798 * Caller MUST NOT hold p->p_lock. Caller MAY hold p->p_reflock 1799 * and/or proc_lock. 1800 * 1801 * On success, caller MUST call vmspace_free(vm) when done. 1802 */ 1803 int 1804 proc_vmspace_getref(struct proc *p, struct vmspace **vm) 1805 { 1806 int error; 1807 1808 /* curproc exception is for coredump. */ 1809 if (p != curproc) { 1810 mutex_enter(p->p_lock); 1811 if (p->p_sflag & PS_WEXIT) { 1812 error = SET_ERROR(EFAULT); 1813 goto out; 1814 } 1815 } 1816 1817 if (p->p_vmspace->vm_refcnt < 1) { 1818 error = SET_ERROR(EFAULT); 1819 goto out; 1820 } 1821 1822 uvmspace_addref(p->p_vmspace); 1823 *vm = p->p_vmspace; 1824 error = 0; 1825 1826 out: if (p != curproc) 1827 mutex_exit(p->p_lock); 1828 return error; 1829 } 1830 1831 /* 1832 * Acquire a write lock on the process credential. 1833 */ 1834 void 1835 proc_crmod_enter(void) 1836 { 1837 struct lwp *l = curlwp; 1838 struct proc *p = l->l_proc; 1839 kauth_cred_t oc; 1840 1841 /* Reset what needs to be reset in plimit. */ 1842 if (p->p_limit->pl_corename != defcorename) { 1843 lim_setcorename(p, defcorename, 0); 1844 } 1845 1846 mutex_enter(p->p_lock); 1847 1848 /* Ensure the LWP cached credentials are up to date. */ 1849 if ((oc = l->l_cred) != p->p_cred) { 1850 l->l_cred = kauth_cred_hold(p->p_cred); 1851 kauth_cred_free(oc); 1852 } 1853 } 1854 1855 /* 1856 * Set in a new process credential, and drop the write lock. The credential 1857 * must have a reference already. Optionally, free a no-longer required 1858 * credential. 1859 */ 1860 void 1861 proc_crmod_leave(kauth_cred_t scred, kauth_cred_t fcred, bool sugid) 1862 { 1863 struct lwp *l = curlwp, *l2; 1864 struct proc *p = l->l_proc; 1865 kauth_cred_t oc; 1866 1867 KASSERT(mutex_owned(p->p_lock)); 1868 1869 /* Is there a new credential to set in? */ 1870 if (scred != NULL) { 1871 p->p_cred = scred; 1872 LIST_FOREACH(l2, &p->p_lwps, l_sibling) { 1873 if (l2 != l) { 1874 lwp_lock(l2); 1875 l2->l_flag |= LW_CACHECRED; 1876 lwp_need_userret(l2); 1877 lwp_unlock(l2); 1878 } 1879 } 1880 1881 /* Ensure the LWP cached credentials are up to date. */ 1882 if ((oc = l->l_cred) != scred) { 1883 l->l_cred = kauth_cred_hold(scred); 1884 } 1885 } else 1886 oc = NULL; /* XXXgcc */ 1887 1888 if (sugid) { 1889 /* 1890 * Mark process as having changed credentials, stops 1891 * tracing etc. 1892 */ 1893 p->p_flag |= PK_SUGID; 1894 } 1895 1896 mutex_exit(p->p_lock); 1897 1898 /* If there is a credential to be released, free it now. */ 1899 if (fcred != NULL) { 1900 KASSERT(scred != NULL); 1901 kauth_cred_free(fcred); 1902 if (oc != scred) 1903 kauth_cred_free(oc); 1904 } 1905 } 1906 1907 /* 1908 * proc_specific_key_create -- 1909 * Create a key for subsystem proc-specific data. 1910 */ 1911 int 1912 proc_specific_key_create(specificdata_key_t *keyp, specificdata_dtor_t dtor) 1913 { 1914 1915 return (specificdata_key_create(proc_specificdata_domain, keyp, dtor)); 1916 } 1917 1918 /* 1919 * proc_specific_key_delete -- 1920 * Delete a key for subsystem proc-specific data. 1921 */ 1922 void 1923 proc_specific_key_delete(specificdata_key_t key) 1924 { 1925 1926 specificdata_key_delete(proc_specificdata_domain, key); 1927 } 1928 1929 /* 1930 * proc_initspecific -- 1931 * Initialize a proc's specificdata container. 1932 */ 1933 void 1934 proc_initspecific(struct proc *p) 1935 { 1936 int error __diagused; 1937 1938 error = specificdata_init(proc_specificdata_domain, &p->p_specdataref); 1939 KASSERT(error == 0); 1940 } 1941 1942 /* 1943 * proc_finispecific -- 1944 * Finalize a proc's specificdata container. 1945 */ 1946 void 1947 proc_finispecific(struct proc *p) 1948 { 1949 1950 specificdata_fini(proc_specificdata_domain, &p->p_specdataref); 1951 } 1952 1953 /* 1954 * proc_getspecific -- 1955 * Return proc-specific data corresponding to the specified key. 1956 */ 1957 void * 1958 proc_getspecific(struct proc *p, specificdata_key_t key) 1959 { 1960 1961 return (specificdata_getspecific(proc_specificdata_domain, 1962 &p->p_specdataref, key)); 1963 } 1964 1965 /* 1966 * proc_setspecific -- 1967 * Set proc-specific data corresponding to the specified key. 1968 */ 1969 void 1970 proc_setspecific(struct proc *p, specificdata_key_t key, void *data) 1971 { 1972 1973 specificdata_setspecific(proc_specificdata_domain, 1974 &p->p_specdataref, key, data); 1975 } 1976 1977 int 1978 proc_uidmatch(kauth_cred_t cred, kauth_cred_t target) 1979 { 1980 1981 if (kauth_cred_getuid(cred) != kauth_cred_getuid(target) || 1982 kauth_cred_getuid(cred) != kauth_cred_getsvuid(target)) { 1983 /* 1984 * suid proc of ours or proc not ours 1985 */ 1986 return SET_ERROR(EPERM); 1987 } else if (kauth_cred_getgid(target) != kauth_cred_getsvgid(target)) { 1988 /* 1989 * sgid proc has sgid back to us temporarily 1990 */ 1991 return SET_ERROR(EPERM); 1992 } else { 1993 /* 1994 * our rgid must be in target's group list (ie, 1995 * sub-processes started by a sgid process) 1996 */ 1997 int ismember = 0; 1998 1999 if (kauth_cred_ismember_gid(cred, 2000 kauth_cred_getgid(target), &ismember) != 0 || 2001 !ismember) 2002 return SET_ERROR(EPERM); 2003 } 2004 2005 return 0; 2006 } 2007 2008 /* 2009 * sysctl stuff 2010 */ 2011 2012 #define KERN_PROCSLOP (5 * sizeof(struct kinfo_proc)) 2013 2014 static const u_int sysctl_flagmap[] = { 2015 PK_ADVLOCK, P_ADVLOCK, 2016 PK_EXEC, P_EXEC, 2017 PK_NOCLDWAIT, P_NOCLDWAIT, 2018 PK_32, P_32, 2019 PK_CLDSIGIGN, P_CLDSIGIGN, 2020 PK_SUGID, P_SUGID, 2021 0 2022 }; 2023 2024 static const u_int sysctl_sflagmap[] = { 2025 PS_NOCLDSTOP, P_NOCLDSTOP, 2026 PS_WEXIT, P_WEXIT, 2027 PS_STOPFORK, P_STOPFORK, 2028 PS_STOPEXEC, P_STOPEXEC, 2029 PS_STOPEXIT, P_STOPEXIT, 2030 0 2031 }; 2032 2033 static const u_int sysctl_slflagmap[] = { 2034 PSL_TRACED, P_TRACED, 2035 PSL_CHTRACED, P_CHTRACED, 2036 PSL_SYSCALL, P_SYSCALL, 2037 0 2038 }; 2039 2040 static const u_int sysctl_lflagmap[] = { 2041 PL_CONTROLT, P_CONTROLT, 2042 PL_PPWAIT, P_PPWAIT, 2043 0 2044 }; 2045 2046 static const u_int sysctl_stflagmap[] = { 2047 PST_PROFIL, P_PROFIL, 2048 0 2049 2050 }; 2051 2052 /* used by kern_lwp also */ 2053 const u_int sysctl_lwpflagmap[] = { 2054 LW_SINTR, L_SINTR, 2055 LW_SYSTEM, L_SYSTEM, 2056 0 2057 }; 2058 2059 /* 2060 * Find the most ``active'' lwp of a process and return it for ps display 2061 * purposes 2062 */ 2063 static struct lwp * 2064 proc_active_lwp(struct proc *p) 2065 { 2066 static const int ostat[] = { 2067 0, 2068 2, /* LSIDL */ 2069 6, /* LSRUN */ 2070 5, /* LSSLEEP */ 2071 4, /* LSSTOP */ 2072 0, /* LSZOMB */ 2073 1, /* LSDEAD */ 2074 7, /* LSONPROC */ 2075 3 /* LSSUSPENDED */ 2076 }; 2077 2078 struct lwp *l, *lp = NULL; 2079 LIST_FOREACH(l, &p->p_lwps, l_sibling) { 2080 KASSERT(l->l_stat >= 0); 2081 KASSERT(l->l_stat < __arraycount(ostat)); 2082 if (lp == NULL || 2083 ostat[l->l_stat] > ostat[lp->l_stat] || 2084 (ostat[l->l_stat] == ostat[lp->l_stat] && 2085 l->l_cpticks > lp->l_cpticks)) { 2086 lp = l; 2087 continue; 2088 } 2089 } 2090 return lp; 2091 } 2092 2093 static int 2094 sysctl_doeproc(SYSCTLFN_ARGS) 2095 { 2096 union { 2097 struct kinfo_proc kproc; 2098 struct kinfo_proc2 kproc2; 2099 } *kbuf; 2100 struct proc *p, *next, *marker; 2101 char *where, *dp; 2102 int type, op, arg, error; 2103 u_int elem_size, kelem_size, elem_count; 2104 size_t buflen, needed; 2105 bool match, zombie, mmmbrains; 2106 const bool allowaddr = get_expose_address(curproc); 2107 2108 if (namelen == 1 && name[0] == CTL_QUERY) 2109 return (sysctl_query(SYSCTLFN_CALL(rnode))); 2110 2111 dp = where = oldp; 2112 buflen = where != NULL ? *oldlenp : 0; 2113 error = 0; 2114 needed = 0; 2115 type = rnode->sysctl_num; 2116 2117 if (type == KERN_PROC) { 2118 if (namelen == 0) 2119 return SET_ERROR(EINVAL); 2120 switch (op = name[0]) { 2121 case KERN_PROC_ALL: 2122 /* 2123 * note: kvm_getprocs(KERN_PROC_ALL) gives 2124 * us namelen=2. ignore the extra argument, 2125 * which is typically 0. 2126 */ 2127 if (namelen != 1 && namelen != 2) 2128 return SET_ERROR(EINVAL); 2129 arg = 0; 2130 break; 2131 default: 2132 if (namelen != 2) 2133 return SET_ERROR(EINVAL); 2134 arg = name[1]; 2135 break; 2136 } 2137 elem_count = 0; /* Hush little compiler, don't you cry */ 2138 kelem_size = elem_size = sizeof(kbuf->kproc); 2139 } else { 2140 if (namelen != 4) 2141 return SET_ERROR(EINVAL); 2142 op = name[0]; 2143 arg = name[1]; 2144 elem_size = name[2]; 2145 elem_count = name[3]; 2146 kelem_size = sizeof(kbuf->kproc2); 2147 } 2148 2149 sysctl_unlock(); 2150 2151 kbuf = kmem_zalloc(sizeof(*kbuf), KM_SLEEP); 2152 marker = kmem_alloc(sizeof(*marker), KM_SLEEP); 2153 marker->p_flag = PK_MARKER; 2154 2155 mutex_enter(&proc_lock); 2156 /* 2157 * Start with zombies to prevent reporting processes twice, in case they 2158 * are dying and being moved from the list of alive processes to zombies. 2159 */ 2160 mmmbrains = true; 2161 for (p = LIST_FIRST(&zombproc);; p = next) { 2162 if (p == NULL) { 2163 if (mmmbrains) { 2164 p = LIST_FIRST(&allproc); 2165 mmmbrains = false; 2166 } 2167 if (p == NULL) 2168 break; 2169 } 2170 next = LIST_NEXT(p, p_list); 2171 if ((p->p_flag & PK_MARKER) != 0) 2172 continue; 2173 2174 /* 2175 * Skip embryonic processes. 2176 */ 2177 if (p->p_stat == SIDL) 2178 continue; 2179 2180 mutex_enter(p->p_lock); 2181 error = kauth_authorize_process(l->l_cred, 2182 KAUTH_PROCESS_CANSEE, p, 2183 KAUTH_ARG(KAUTH_REQ_PROCESS_CANSEE_EPROC), NULL, NULL); 2184 if (error != 0) { 2185 mutex_exit(p->p_lock); 2186 continue; 2187 } 2188 2189 /* 2190 * Handle all the operations in one switch on the cost of 2191 * algorithm complexity is on purpose. The win splitting this 2192 * function into several similar copies makes maintenance 2193 * burden, code grow and boost is negligible in practical 2194 * systems. 2195 */ 2196 switch (op) { 2197 case KERN_PROC_PID: 2198 match = (p->p_pid == (pid_t)arg); 2199 break; 2200 2201 case KERN_PROC_PGRP: 2202 match = (p->p_pgrp->pg_id == (pid_t)arg); 2203 break; 2204 2205 case KERN_PROC_SESSION: 2206 match = (p->p_session->s_sid == (pid_t)arg); 2207 break; 2208 2209 case KERN_PROC_TTY: 2210 match = true; 2211 if (arg == (int) KERN_PROC_TTY_REVOKE) { 2212 if ((p->p_lflag & PL_CONTROLT) == 0 || 2213 p->p_session->s_ttyp == NULL || 2214 p->p_session->s_ttyvp != NULL) { 2215 match = false; 2216 } 2217 } else if ((p->p_lflag & PL_CONTROLT) == 0 || 2218 p->p_session->s_ttyp == NULL) { 2219 if ((dev_t)arg != KERN_PROC_TTY_NODEV) { 2220 match = false; 2221 } 2222 } else if (p->p_session->s_ttyp->t_dev != (dev_t)arg) { 2223 match = false; 2224 } 2225 break; 2226 2227 case KERN_PROC_UID: 2228 match = (kauth_cred_geteuid(p->p_cred) == (uid_t)arg); 2229 break; 2230 2231 case KERN_PROC_RUID: 2232 match = (kauth_cred_getuid(p->p_cred) == (uid_t)arg); 2233 break; 2234 2235 case KERN_PROC_GID: 2236 match = (kauth_cred_getegid(p->p_cred) == (uid_t)arg); 2237 break; 2238 2239 case KERN_PROC_RGID: 2240 match = (kauth_cred_getgid(p->p_cred) == (uid_t)arg); 2241 break; 2242 2243 case KERN_PROC_ALL: 2244 match = true; 2245 /* allow everything */ 2246 break; 2247 2248 default: 2249 error = SET_ERROR(EINVAL); 2250 mutex_exit(p->p_lock); 2251 goto cleanup; 2252 } 2253 if (!match) { 2254 mutex_exit(p->p_lock); 2255 continue; 2256 } 2257 2258 /* 2259 * Grab a hold on the process. 2260 */ 2261 if (mmmbrains) { 2262 zombie = true; 2263 } else { 2264 zombie = !rw_tryenter(&p->p_reflock, RW_READER); 2265 } 2266 if (zombie) { 2267 LIST_INSERT_AFTER(p, marker, p_list); 2268 } 2269 2270 if (buflen >= elem_size && 2271 (type == KERN_PROC || elem_count > 0)) { 2272 ruspace(p); /* Update process vm resource use */ 2273 2274 if (type == KERN_PROC) { 2275 fill_proc(p, &kbuf->kproc.kp_proc, allowaddr); 2276 fill_eproc(p, &kbuf->kproc.kp_eproc, zombie, 2277 allowaddr); 2278 } else { 2279 fill_kproc2(p, &kbuf->kproc2, zombie, 2280 allowaddr); 2281 elem_count--; 2282 } 2283 mutex_exit(p->p_lock); 2284 mutex_exit(&proc_lock); 2285 /* 2286 * Copy out elem_size, but not larger than kelem_size 2287 */ 2288 error = sysctl_copyout(l, kbuf, dp, 2289 uimin(kelem_size, elem_size)); 2290 mutex_enter(&proc_lock); 2291 if (error) { 2292 goto bah; 2293 } 2294 dp += elem_size; 2295 buflen -= elem_size; 2296 } else { 2297 mutex_exit(p->p_lock); 2298 } 2299 needed += elem_size; 2300 2301 /* 2302 * Release reference to process. 2303 */ 2304 if (zombie) { 2305 next = LIST_NEXT(marker, p_list); 2306 LIST_REMOVE(marker, p_list); 2307 } else { 2308 rw_exit(&p->p_reflock); 2309 next = LIST_NEXT(p, p_list); 2310 } 2311 2312 /* 2313 * Short-circuit break quickly! 2314 */ 2315 if (op == KERN_PROC_PID) 2316 break; 2317 } 2318 mutex_exit(&proc_lock); 2319 2320 if (where != NULL) { 2321 *oldlenp = dp - where; 2322 if (needed > *oldlenp) { 2323 error = SET_ERROR(ENOMEM); 2324 goto out; 2325 } 2326 } else { 2327 needed += KERN_PROCSLOP; 2328 *oldlenp = needed; 2329 } 2330 kmem_free(kbuf, sizeof(*kbuf)); 2331 kmem_free(marker, sizeof(*marker)); 2332 sysctl_relock(); 2333 return 0; 2334 bah: 2335 if (zombie) 2336 LIST_REMOVE(marker, p_list); 2337 else 2338 rw_exit(&p->p_reflock); 2339 cleanup: 2340 mutex_exit(&proc_lock); 2341 out: 2342 kmem_free(kbuf, sizeof(*kbuf)); 2343 kmem_free(marker, sizeof(*marker)); 2344 sysctl_relock(); 2345 return error; 2346 } 2347 2348 int 2349 copyin_psstrings(struct proc *p, struct ps_strings *arginfo) 2350 { 2351 #if !defined(_RUMPKERNEL) 2352 int retval; 2353 2354 if (p->p_flag & PK_32) { 2355 MODULE_HOOK_CALL(kern_proc32_copyin_hook, (p, arginfo), 2356 enosys(), retval); 2357 return retval; 2358 } 2359 #endif /* !defined(_RUMPKERNEL) */ 2360 2361 return copyin_proc(p, (void *)p->p_psstrp, arginfo, sizeof(*arginfo)); 2362 } 2363 2364 static int 2365 copy_procargs_sysctl_cb(void *cookie_, const void *src, size_t off, size_t len) 2366 { 2367 void **cookie = cookie_; 2368 struct lwp *l = cookie[0]; 2369 char *dst = cookie[1]; 2370 2371 return sysctl_copyout(l, src, dst + off, len); 2372 } 2373 2374 /* 2375 * sysctl helper routine for kern.proc_args pseudo-subtree. 2376 */ 2377 static int 2378 sysctl_kern_proc_args(SYSCTLFN_ARGS) 2379 { 2380 struct ps_strings pss; 2381 struct proc *p; 2382 pid_t pid; 2383 int type, error; 2384 void *cookie[2]; 2385 2386 if (namelen == 1 && name[0] == CTL_QUERY) 2387 return (sysctl_query(SYSCTLFN_CALL(rnode))); 2388 2389 if (newp != NULL || namelen != 2) 2390 return SET_ERROR(EINVAL); 2391 pid = name[0]; 2392 type = name[1]; 2393 2394 switch (type) { 2395 case KERN_PROC_PATHNAME: 2396 sysctl_unlock(); 2397 error = fill_pathname(l, pid, oldp, oldlenp); 2398 sysctl_relock(); 2399 return error; 2400 2401 case KERN_PROC_CWD: 2402 sysctl_unlock(); 2403 error = fill_cwd(l, pid, oldp, oldlenp); 2404 sysctl_relock(); 2405 return error; 2406 2407 case KERN_PROC_ARGV: 2408 case KERN_PROC_NARGV: 2409 case KERN_PROC_ENV: 2410 case KERN_PROC_NENV: 2411 /* ok */ 2412 break; 2413 default: 2414 return SET_ERROR(EINVAL); 2415 } 2416 2417 sysctl_unlock(); 2418 2419 /* check pid */ 2420 mutex_enter(&proc_lock); 2421 if ((p = proc_find(pid)) == NULL) { 2422 error = SET_ERROR(EINVAL); 2423 goto out_locked; 2424 } 2425 mutex_enter(p->p_lock); 2426 2427 /* Check permission. */ 2428 if (type == KERN_PROC_ARGV || type == KERN_PROC_NARGV) 2429 error = kauth_authorize_process(l->l_cred, KAUTH_PROCESS_CANSEE, 2430 p, KAUTH_ARG(KAUTH_REQ_PROCESS_CANSEE_ARGS), NULL, NULL); 2431 else if (type == KERN_PROC_ENV || type == KERN_PROC_NENV) 2432 error = kauth_authorize_process(l->l_cred, KAUTH_PROCESS_CANSEE, 2433 p, KAUTH_ARG(KAUTH_REQ_PROCESS_CANSEE_ENV), NULL, NULL); 2434 else 2435 error = SET_ERROR(EINVAL); /* XXXGCC */ 2436 if (error) { 2437 mutex_exit(p->p_lock); 2438 goto out_locked; 2439 } 2440 2441 if (oldp == NULL) { 2442 if (type == KERN_PROC_NARGV || type == KERN_PROC_NENV) 2443 *oldlenp = sizeof (int); 2444 else 2445 *oldlenp = ARG_MAX; /* XXX XXX XXX */ 2446 error = 0; 2447 mutex_exit(p->p_lock); 2448 goto out_locked; 2449 } 2450 2451 /* 2452 * Zombies don't have a stack, so we can't read their psstrings. 2453 * System processes also don't have a user stack. 2454 */ 2455 if (P_ZOMBIE(p) || (p->p_flag & PK_SYSTEM) != 0) { 2456 error = SET_ERROR(EINVAL); 2457 mutex_exit(p->p_lock); 2458 goto out_locked; 2459 } 2460 2461 error = rw_tryenter(&p->p_reflock, RW_READER) ? 0 : SET_ERROR(EBUSY); 2462 mutex_exit(p->p_lock); 2463 if (error) { 2464 goto out_locked; 2465 } 2466 mutex_exit(&proc_lock); 2467 2468 if (type == KERN_PROC_NARGV || type == KERN_PROC_NENV) { 2469 int value; 2470 if ((error = copyin_psstrings(p, &pss)) == 0) { 2471 if (type == KERN_PROC_NARGV) 2472 value = pss.ps_nargvstr; 2473 else 2474 value = pss.ps_nenvstr; 2475 error = sysctl_copyout(l, &value, oldp, sizeof(value)); 2476 *oldlenp = sizeof(value); 2477 } 2478 } else { 2479 cookie[0] = l; 2480 cookie[1] = oldp; 2481 error = copy_procargs(p, type, oldlenp, 2482 copy_procargs_sysctl_cb, cookie); 2483 } 2484 rw_exit(&p->p_reflock); 2485 sysctl_relock(); 2486 return error; 2487 2488 out_locked: 2489 mutex_exit(&proc_lock); 2490 sysctl_relock(); 2491 return error; 2492 } 2493 2494 int 2495 copy_procargs(struct proc *p, int oid, size_t *limit, 2496 int (*cb)(void *, const void *, size_t, size_t), void *cookie) 2497 { 2498 struct ps_strings pss; 2499 size_t len, i, loaded, entry_len; 2500 struct uio auio; 2501 struct iovec aiov; 2502 int error, argvlen; 2503 char *arg; 2504 char **argv; 2505 vaddr_t user_argv; 2506 struct vmspace *vmspace; 2507 2508 /* 2509 * Allocate a temporary buffer to hold the argument vector and 2510 * the arguments themselve. 2511 */ 2512 arg = kmem_alloc(PAGE_SIZE, KM_SLEEP); 2513 argv = kmem_alloc(PAGE_SIZE, KM_SLEEP); 2514 2515 /* 2516 * Lock the process down in memory. 2517 */ 2518 vmspace = p->p_vmspace; 2519 uvmspace_addref(vmspace); 2520 2521 /* 2522 * Read in the ps_strings structure. 2523 */ 2524 if ((error = copyin_psstrings(p, &pss)) != 0) 2525 goto done; 2526 2527 /* 2528 * Now read the address of the argument vector. 2529 */ 2530 switch (oid) { 2531 case KERN_PROC_ARGV: 2532 user_argv = (uintptr_t)pss.ps_argvstr; 2533 argvlen = pss.ps_nargvstr; 2534 break; 2535 case KERN_PROC_ENV: 2536 user_argv = (uintptr_t)pss.ps_envstr; 2537 argvlen = pss.ps_nenvstr; 2538 break; 2539 default: 2540 error = SET_ERROR(EINVAL); 2541 goto done; 2542 } 2543 2544 if (argvlen < 0) { 2545 error = SET_ERROR(EIO); 2546 goto done; 2547 } 2548 2549 2550 /* 2551 * Now copy each string. 2552 */ 2553 len = 0; /* bytes written to user buffer */ 2554 loaded = 0; /* bytes from argv already processed */ 2555 i = 0; /* To make compiler happy */ 2556 entry_len = PROC_PTRSZ(p); 2557 2558 for (; argvlen; --argvlen) { 2559 int finished = 0; 2560 vaddr_t base; 2561 size_t xlen; 2562 int j; 2563 2564 if (loaded == 0) { 2565 size_t rem = entry_len * argvlen; 2566 loaded = MIN(rem, PAGE_SIZE); 2567 error = copyin_vmspace(vmspace, 2568 (const void *)user_argv, argv, loaded); 2569 if (error) 2570 break; 2571 user_argv += loaded; 2572 i = 0; 2573 } 2574 2575 #if !defined(_RUMPKERNEL) 2576 if (p->p_flag & PK_32) 2577 MODULE_HOOK_CALL(kern_proc32_base_hook, 2578 (argv, i++), 0, base); 2579 else 2580 #endif /* !defined(_RUMPKERNEL) */ 2581 base = (vaddr_t)argv[i++]; 2582 loaded -= entry_len; 2583 2584 /* 2585 * The program has messed around with its arguments, 2586 * possibly deleting some, and replacing them with 2587 * NULL's. Treat this as the last argument and not 2588 * a failure. 2589 */ 2590 if (base == 0) 2591 break; 2592 2593 while (!finished) { 2594 xlen = PAGE_SIZE - (base & PAGE_MASK); 2595 2596 aiov.iov_base = arg; 2597 aiov.iov_len = PAGE_SIZE; 2598 auio.uio_iov = &aiov; 2599 auio.uio_iovcnt = 1; 2600 auio.uio_offset = base; 2601 auio.uio_resid = xlen; 2602 auio.uio_rw = UIO_READ; 2603 UIO_SETUP_SYSSPACE(&auio); 2604 error = uvm_io(&vmspace->vm_map, &auio, 0); 2605 if (error) 2606 goto done; 2607 2608 /* Look for the end of the string */ 2609 for (j = 0; j < xlen; j++) { 2610 if (arg[j] == '\0') { 2611 xlen = j + 1; 2612 finished = 1; 2613 break; 2614 } 2615 } 2616 2617 /* Check for user buffer overflow */ 2618 if (len + xlen > *limit) { 2619 finished = 1; 2620 if (len > *limit) 2621 xlen = 0; 2622 else 2623 xlen = *limit - len; 2624 } 2625 2626 /* Copyout the page */ 2627 error = (*cb)(cookie, arg, len, xlen); 2628 if (error) 2629 goto done; 2630 2631 len += xlen; 2632 base += xlen; 2633 } 2634 } 2635 *limit = len; 2636 2637 done: 2638 kmem_free(argv, PAGE_SIZE); 2639 kmem_free(arg, PAGE_SIZE); 2640 uvmspace_free(vmspace); 2641 return error; 2642 } 2643 2644 /* 2645 * Fill in a proc structure for the specified process. 2646 */ 2647 static void 2648 fill_proc(const struct proc *psrc, struct proc *p, bool allowaddr) 2649 { 2650 COND_SET_STRUCT(p->p_list, psrc->p_list, allowaddr); 2651 memset(&p->p_auxlock, 0, sizeof(p->p_auxlock)); 2652 COND_SET_STRUCT(p->p_lock, psrc->p_lock, allowaddr); 2653 memset(&p->p_stmutex, 0, sizeof(p->p_stmutex)); 2654 memset(&p->p_reflock, 0, sizeof(p->p_reflock)); 2655 COND_SET_STRUCT(p->p_waitcv, psrc->p_waitcv, allowaddr); 2656 COND_SET_STRUCT(p->p_lwpcv, psrc->p_lwpcv, allowaddr); 2657 COND_SET_PTR(p->p_cred, psrc->p_cred, allowaddr); 2658 COND_SET_PTR(p->p_fd, psrc->p_fd, allowaddr); 2659 COND_SET_PTR(p->p_cwdi, psrc->p_cwdi, allowaddr); 2660 COND_SET_PTR(p->p_stats, psrc->p_stats, allowaddr); 2661 COND_SET_PTR(p->p_limit, psrc->p_limit, allowaddr); 2662 COND_SET_PTR(p->p_vmspace, psrc->p_vmspace, allowaddr); 2663 COND_SET_PTR(p->p_sigacts, psrc->p_sigacts, allowaddr); 2664 COND_SET_PTR(p->p_aio, psrc->p_aio, allowaddr); 2665 p->p_mqueue_cnt = psrc->p_mqueue_cnt; 2666 memset(&p->p_specdataref, 0, sizeof(p->p_specdataref)); 2667 p->p_exitsig = psrc->p_exitsig; 2668 p->p_flag = psrc->p_flag; 2669 p->p_sflag = psrc->p_sflag; 2670 p->p_slflag = psrc->p_slflag; 2671 p->p_lflag = psrc->p_lflag; 2672 p->p_stflag = psrc->p_stflag; 2673 p->p_stat = psrc->p_stat; 2674 p->p_trace_enabled = psrc->p_trace_enabled; 2675 p->p_pid = psrc->p_pid; 2676 COND_SET_STRUCT(p->p_pglist, psrc->p_pglist, allowaddr); 2677 COND_SET_PTR(p->p_pptr, psrc->p_pptr, allowaddr); 2678 COND_SET_STRUCT(p->p_sibling, psrc->p_sibling, allowaddr); 2679 COND_SET_STRUCT(p->p_children, psrc->p_children, allowaddr); 2680 COND_SET_STRUCT(p->p_lwps, psrc->p_lwps, allowaddr); 2681 COND_SET_PTR(p->p_raslist, psrc->p_raslist, allowaddr); 2682 p->p_nlwps = psrc->p_nlwps; 2683 p->p_nzlwps = psrc->p_nzlwps; 2684 p->p_nrlwps = psrc->p_nrlwps; 2685 p->p_nlwpwait = psrc->p_nlwpwait; 2686 p->p_ndlwps = psrc->p_ndlwps; 2687 p->p_nstopchild = psrc->p_nstopchild; 2688 p->p_waited = psrc->p_waited; 2689 COND_SET_PTR(p->p_zomblwp, psrc->p_zomblwp, allowaddr); 2690 COND_SET_PTR(p->p_vforklwp, psrc->p_vforklwp, allowaddr); 2691 COND_SET_PTR(p->p_sched_info, psrc->p_sched_info, allowaddr); 2692 p->p_estcpu = psrc->p_estcpu; 2693 p->p_estcpu_inherited = psrc->p_estcpu_inherited; 2694 p->p_forktime = psrc->p_forktime; 2695 p->p_pctcpu = psrc->p_pctcpu; 2696 COND_SET_PTR(p->p_opptr, psrc->p_opptr, allowaddr); 2697 COND_SET_PTR(p->p_timers, psrc->p_timers, allowaddr); 2698 p->p_rtime = psrc->p_rtime; 2699 p->p_uticks = psrc->p_uticks; 2700 p->p_sticks = psrc->p_sticks; 2701 p->p_iticks = psrc->p_iticks; 2702 p->p_xutime = psrc->p_xutime; 2703 p->p_xstime = psrc->p_xstime; 2704 p->p_traceflag = psrc->p_traceflag; 2705 COND_SET_PTR(p->p_tracep, psrc->p_tracep, allowaddr); 2706 COND_SET_PTR(p->p_textvp, psrc->p_textvp, allowaddr); 2707 COND_SET_PTR(p->p_emul, psrc->p_emul, allowaddr); 2708 COND_SET_PTR(p->p_emuldata, psrc->p_emuldata, allowaddr); 2709 COND_SET_CPTR(p->p_execsw, psrc->p_execsw, allowaddr); 2710 COND_SET_STRUCT(p->p_klist, psrc->p_klist, allowaddr); 2711 COND_SET_STRUCT(p->p_sigwaiters, psrc->p_sigwaiters, allowaddr); 2712 COND_SET_STRUCT(p->p_sigpend.sp_info, psrc->p_sigpend.sp_info, 2713 allowaddr); 2714 p->p_sigpend.sp_set = psrc->p_sigpend.sp_set; 2715 COND_SET_PTR(p->p_lwpctl, psrc->p_lwpctl, allowaddr); 2716 p->p_ppid = psrc->p_ppid; 2717 p->p_oppid = psrc->p_oppid; 2718 COND_SET_PTR(p->p_path, psrc->p_path, allowaddr); 2719 p->p_sigctx = psrc->p_sigctx; 2720 p->p_nice = psrc->p_nice; 2721 memcpy(p->p_comm, psrc->p_comm, sizeof(p->p_comm)); 2722 COND_SET_PTR(p->p_pgrp, psrc->p_pgrp, allowaddr); 2723 COND_SET_VALUE(p->p_psstrp, psrc->p_psstrp, allowaddr); 2724 p->p_pax = psrc->p_pax; 2725 p->p_xexit = psrc->p_xexit; 2726 p->p_xsig = psrc->p_xsig; 2727 p->p_acflag = psrc->p_acflag; 2728 COND_SET_STRUCT(p->p_md, psrc->p_md, allowaddr); 2729 p->p_stackbase = psrc->p_stackbase; 2730 COND_SET_PTR(p->p_dtrace, psrc->p_dtrace, allowaddr); 2731 } 2732 2733 /* 2734 * Fill in an eproc structure for the specified process. 2735 */ 2736 void 2737 fill_eproc(struct proc *p, struct eproc *ep, bool zombie, bool allowaddr) 2738 { 2739 struct tty *tp; 2740 struct lwp *l; 2741 2742 KASSERT(mutex_owned(&proc_lock)); 2743 KASSERT(mutex_owned(p->p_lock)); 2744 2745 COND_SET_PTR(ep->e_paddr, p, allowaddr); 2746 COND_SET_PTR(ep->e_sess, p->p_session, allowaddr); 2747 if (p->p_cred) { 2748 kauth_cred_topcred(p->p_cred, &ep->e_pcred); 2749 kauth_cred_toucred(p->p_cred, &ep->e_ucred); 2750 } 2751 if (p->p_stat != SIDL && !P_ZOMBIE(p) && !zombie) { 2752 struct vmspace *vm = p->p_vmspace; 2753 2754 ep->e_vm.vm_rssize = vm_resident_count(vm); 2755 ep->e_vm.vm_tsize = vm->vm_tsize; 2756 ep->e_vm.vm_dsize = vm->vm_dsize; 2757 ep->e_vm.vm_ssize = vm->vm_ssize; 2758 ep->e_vm.vm_map.size = vm->vm_map.size; 2759 2760 /* Pick the primary (first) LWP */ 2761 l = proc_active_lwp(p); 2762 KASSERT(l != NULL); 2763 lwp_lock(l); 2764 if (l->l_wchan) 2765 strncpy(ep->e_wmesg, l->l_wmesg, WMESGLEN); 2766 lwp_unlock(l); 2767 } 2768 ep->e_ppid = p->p_ppid; 2769 if (p->p_pgrp && p->p_session) { 2770 ep->e_pgid = p->p_pgrp->pg_id; 2771 ep->e_jobc = p->p_pgrp->pg_jobc; 2772 ep->e_sid = p->p_session->s_sid; 2773 if ((p->p_lflag & PL_CONTROLT) && 2774 (tp = p->p_session->s_ttyp)) { 2775 ep->e_tdev = tp->t_dev; 2776 ep->e_tpgid = tp->t_pgrp ? tp->t_pgrp->pg_id : NO_PGID; 2777 COND_SET_PTR(ep->e_tsess, tp->t_session, allowaddr); 2778 } else 2779 ep->e_tdev = (uint32_t)NODEV; 2780 ep->e_flag = p->p_session->s_ttyvp ? EPROC_CTTY : 0; 2781 if (SESS_LEADER(p)) 2782 ep->e_flag |= EPROC_SLEADER; 2783 strncpy(ep->e_login, p->p_session->s_login, MAXLOGNAME); 2784 } 2785 ep->e_xsize = ep->e_xrssize = 0; 2786 ep->e_xccount = ep->e_xswrss = 0; 2787 } 2788 2789 /* 2790 * Fill in a kinfo_proc2 structure for the specified process. 2791 */ 2792 void 2793 fill_kproc2(struct proc *p, struct kinfo_proc2 *ki, bool zombie, bool allowaddr) 2794 { 2795 struct tty *tp; 2796 struct lwp *l; 2797 struct timeval ut, st, rt; 2798 sigset_t ss1, ss2; 2799 struct rusage ru; 2800 struct vmspace *vm; 2801 2802 KASSERT(mutex_owned(&proc_lock)); 2803 KASSERT(mutex_owned(p->p_lock)); 2804 2805 sigemptyset(&ss1); 2806 sigemptyset(&ss2); 2807 2808 COND_SET_VALUE(ki->p_paddr, PTRTOUINT64(p), allowaddr); 2809 COND_SET_VALUE(ki->p_fd, PTRTOUINT64(p->p_fd), allowaddr); 2810 COND_SET_VALUE(ki->p_cwdi, PTRTOUINT64(p->p_cwdi), allowaddr); 2811 COND_SET_VALUE(ki->p_stats, PTRTOUINT64(p->p_stats), allowaddr); 2812 COND_SET_VALUE(ki->p_limit, PTRTOUINT64(p->p_limit), allowaddr); 2813 COND_SET_VALUE(ki->p_vmspace, PTRTOUINT64(p->p_vmspace), allowaddr); 2814 COND_SET_VALUE(ki->p_sigacts, PTRTOUINT64(p->p_sigacts), allowaddr); 2815 COND_SET_VALUE(ki->p_sess, PTRTOUINT64(p->p_session), allowaddr); 2816 ki->p_tsess = 0; /* may be changed if controlling tty below */ 2817 COND_SET_VALUE(ki->p_ru, PTRTOUINT64(&p->p_stats->p_ru), allowaddr); 2818 ki->p_eflag = 0; 2819 ki->p_exitsig = p->p_exitsig; 2820 ki->p_flag = L_INMEM; /* Process never swapped out */ 2821 ki->p_flag |= sysctl_map_flags(sysctl_flagmap, p->p_flag); 2822 ki->p_flag |= sysctl_map_flags(sysctl_sflagmap, p->p_sflag); 2823 ki->p_flag |= sysctl_map_flags(sysctl_slflagmap, p->p_slflag); 2824 ki->p_flag |= sysctl_map_flags(sysctl_lflagmap, p->p_lflag); 2825 ki->p_flag |= sysctl_map_flags(sysctl_stflagmap, p->p_stflag); 2826 ki->p_pid = p->p_pid; 2827 ki->p_ppid = p->p_ppid; 2828 ki->p_uid = kauth_cred_geteuid(p->p_cred); 2829 ki->p_ruid = kauth_cred_getuid(p->p_cred); 2830 ki->p_gid = kauth_cred_getegid(p->p_cred); 2831 ki->p_rgid = kauth_cred_getgid(p->p_cred); 2832 ki->p_svuid = kauth_cred_getsvuid(p->p_cred); 2833 ki->p_svgid = kauth_cred_getsvgid(p->p_cred); 2834 ki->p_ngroups = kauth_cred_ngroups(p->p_cred); 2835 kauth_cred_getgroups(p->p_cred, ki->p_groups, 2836 uimin(ki->p_ngroups, sizeof(ki->p_groups) / sizeof(ki->p_groups[0])), 2837 UIO_SYSSPACE); 2838 2839 ki->p_uticks = p->p_uticks; 2840 ki->p_sticks = p->p_sticks; 2841 ki->p_iticks = p->p_iticks; 2842 ki->p_tpgid = NO_PGID; /* may be changed if controlling tty below */ 2843 COND_SET_VALUE(ki->p_tracep, PTRTOUINT64(p->p_tracep), allowaddr); 2844 ki->p_traceflag = p->p_traceflag; 2845 2846 memcpy(&ki->p_sigignore, &p->p_sigctx.ps_sigignore,sizeof(ki_sigset_t)); 2847 memcpy(&ki->p_sigcatch, &p->p_sigctx.ps_sigcatch, sizeof(ki_sigset_t)); 2848 2849 ki->p_cpticks = 0; 2850 ki->p_pctcpu = p->p_pctcpu; 2851 ki->p_estcpu = 0; 2852 ki->p_stat = p->p_stat; /* Will likely be overridden by LWP status */ 2853 ki->p_realstat = p->p_stat; 2854 ki->p_nice = p->p_nice; 2855 ki->p_xstat = P_WAITSTATUS(p); 2856 ki->p_acflag = p->p_acflag; 2857 2858 strncpy(ki->p_comm, p->p_comm, 2859 uimin(sizeof(ki->p_comm), sizeof(p->p_comm))); 2860 strncpy(ki->p_ename, p->p_emul->e_name, sizeof(ki->p_ename)); 2861 2862 ki->p_nlwps = p->p_nlwps; 2863 ki->p_realflag = ki->p_flag; 2864 2865 if (p->p_stat != SIDL && !P_ZOMBIE(p) && !zombie) { 2866 vm = p->p_vmspace; 2867 ki->p_vm_rssize = vm_resident_count(vm); 2868 ki->p_vm_tsize = vm->vm_tsize; 2869 ki->p_vm_dsize = vm->vm_dsize; 2870 ki->p_vm_ssize = vm->vm_ssize; 2871 ki->p_vm_vsize = atop(vm->vm_map.size); 2872 /* 2873 * Since the stack is initially mapped mostly with 2874 * PROT_NONE and grown as needed, adjust the "mapped size" 2875 * to skip the unused stack portion. 2876 */ 2877 ki->p_vm_msize = 2878 atop(vm->vm_map.size) - vm->vm_issize + vm->vm_ssize; 2879 2880 /* Pick the primary (first) LWP */ 2881 l = proc_active_lwp(p); 2882 KASSERT(l != NULL); 2883 lwp_lock(l); 2884 ki->p_nrlwps = p->p_nrlwps; 2885 ki->p_forw = 0; 2886 ki->p_back = 0; 2887 COND_SET_VALUE(ki->p_addr, PTRTOUINT64(l->l_addr), allowaddr); 2888 ki->p_stat = l->l_stat; 2889 ki->p_flag |= sysctl_map_flags(sysctl_lwpflagmap, l->l_flag); 2890 ki->p_swtime = l->l_swtime; 2891 ki->p_slptime = l->l_slptime; 2892 if (l->l_stat == LSONPROC) 2893 ki->p_schedflags = l->l_cpu->ci_schedstate.spc_flags; 2894 else 2895 ki->p_schedflags = 0; 2896 ki->p_priority = lwp_eprio(l); 2897 ki->p_usrpri = l->l_priority; 2898 if (l->l_wchan) 2899 strncpy(ki->p_wmesg, l->l_wmesg, sizeof(ki->p_wmesg)); 2900 COND_SET_VALUE(ki->p_wchan, PTRTOUINT64(l->l_wchan), allowaddr); 2901 ki->p_cpuid = cpu_index(l->l_cpu); 2902 lwp_unlock(l); 2903 LIST_FOREACH(l, &p->p_lwps, l_sibling) { 2904 /* This is hardly correct, but... */ 2905 sigplusset(&l->l_sigpend.sp_set, &ss1); 2906 sigplusset(&l->l_sigmask, &ss2); 2907 ki->p_cpticks += l->l_cpticks; 2908 ki->p_pctcpu += l->l_pctcpu; 2909 ki->p_estcpu += l->l_estcpu; 2910 } 2911 } 2912 sigplusset(&p->p_sigpend.sp_set, &ss1); 2913 memcpy(&ki->p_siglist, &ss1, sizeof(ki_sigset_t)); 2914 memcpy(&ki->p_sigmask, &ss2, sizeof(ki_sigset_t)); 2915 2916 if (p->p_session != NULL) { 2917 ki->p_sid = p->p_session->s_sid; 2918 ki->p__pgid = p->p_pgrp->pg_id; 2919 if (p->p_session->s_ttyvp) 2920 ki->p_eflag |= EPROC_CTTY; 2921 if (SESS_LEADER(p)) 2922 ki->p_eflag |= EPROC_SLEADER; 2923 strncpy(ki->p_login, p->p_session->s_login, 2924 uimin(sizeof ki->p_login - 1, sizeof p->p_session->s_login)); 2925 ki->p_jobc = p->p_pgrp->pg_jobc; 2926 if ((p->p_lflag & PL_CONTROLT) && (tp = p->p_session->s_ttyp)) { 2927 ki->p_tdev = tp->t_dev; 2928 ki->p_tpgid = tp->t_pgrp ? tp->t_pgrp->pg_id : NO_PGID; 2929 COND_SET_VALUE(ki->p_tsess, PTRTOUINT64(tp->t_session), 2930 allowaddr); 2931 } else { 2932 ki->p_tdev = (int32_t)NODEV; 2933 } 2934 } 2935 2936 if (!P_ZOMBIE(p) && !zombie) { 2937 ki->p_uvalid = 1; 2938 ki->p_ustart_sec = p->p_stats->p_start.tv_sec; 2939 ki->p_ustart_usec = p->p_stats->p_start.tv_usec; 2940 2941 calcru(p, &ut, &st, NULL, &rt); 2942 ki->p_rtime_sec = rt.tv_sec; 2943 ki->p_rtime_usec = rt.tv_usec; 2944 ki->p_uutime_sec = ut.tv_sec; 2945 ki->p_uutime_usec = ut.tv_usec; 2946 ki->p_ustime_sec = st.tv_sec; 2947 ki->p_ustime_usec = st.tv_usec; 2948 2949 memcpy(&ru, &p->p_stats->p_ru, sizeof(ru)); 2950 rulwps(p, &ru); 2951 ki->p_uru_nvcsw = ru.ru_nvcsw; 2952 ki->p_uru_nivcsw = ru.ru_nivcsw; 2953 ki->p_uru_maxrss = ru.ru_maxrss; 2954 ki->p_uru_ixrss = ru.ru_ixrss; 2955 ki->p_uru_idrss = ru.ru_idrss; 2956 ki->p_uru_isrss = ru.ru_isrss; 2957 ki->p_uru_minflt = ru.ru_minflt; 2958 ki->p_uru_majflt = ru.ru_majflt; 2959 ki->p_uru_nswap = ru.ru_nswap; 2960 ki->p_uru_inblock = ru.ru_inblock; 2961 ki->p_uru_oublock = ru.ru_oublock; 2962 ki->p_uru_msgsnd = ru.ru_msgsnd; 2963 ki->p_uru_msgrcv = ru.ru_msgrcv; 2964 ki->p_uru_nsignals = ru.ru_nsignals; 2965 2966 timeradd(&p->p_stats->p_cru.ru_utime, 2967 &p->p_stats->p_cru.ru_stime, &ut); 2968 ki->p_uctime_sec = ut.tv_sec; 2969 ki->p_uctime_usec = ut.tv_usec; 2970 } 2971 } 2972 2973 /* 2974 * proc_find_locked(l, &p, pid, op) 2975 * 2976 * Look up a process p by pid, taking l->l_proc if pid == -1, and 2977 * take the mutex p->p_lock if pid != -1. Verify whether the 2978 * caller is allowed to see the target process before succeeding. 2979 * Return nonzero error on failure. 2980 * 2981 * Invariant on successful return: 2982 * 2983 * pid == -1 || mutex_owned(p->p_lock) 2984 * 2985 * Caller is responsible for mutex_exit(p->p_lock) if pid != -1. 2986 */ 2987 int 2988 proc_find_locked(struct lwp *l, struct proc **p, pid_t pid) 2989 { 2990 int error; 2991 2992 mutex_enter(&proc_lock); 2993 if (pid == -1) 2994 *p = l->l_proc; 2995 else 2996 *p = proc_find(pid); 2997 2998 if (*p == NULL) { 2999 mutex_exit(&proc_lock); 3000 error = SET_ERROR(ESRCH); 3001 goto out; 3002 } 3003 if (pid != -1) 3004 mutex_enter((*p)->p_lock); 3005 mutex_exit(&proc_lock); 3006 3007 error = kauth_authorize_process(l->l_cred, 3008 KAUTH_PROCESS_CANSEE, *p, 3009 KAUTH_ARG(KAUTH_REQ_PROCESS_CANSEE_ENTRY), NULL, NULL); 3010 if (error) { 3011 if (pid != -1) 3012 mutex_exit((*p)->p_lock); 3013 goto out; 3014 } 3015 out: KASSERT(error != 0 || pid == -1 || mutex_owned((*p)->p_lock)); 3016 return error; 3017 } 3018 3019 /* 3020 * proc_find_reflocked(l, &p, pid, op) 3021 * 3022 * Look up a process p by pid, taking l->l_proc iff pid == -1, and 3023 * take p->p_reflock as a reader or writer according to op iff pid 3024 * != -1. Verify whether the caller is allowed to see the target 3025 * process before succeeding. Return nonzero error on failure. 3026 * 3027 * Invariants on successful return: 3028 * 3029 * pid == -1 || rw_lock_held(p->p_lock) 3030 * pid == -1 || op != RW_READER || rw_read_held(p->p_lock) 3031 * pid == -1 || op != RW_WRITER || rw_write_held(p->p_lock) 3032 * 3033 * Caller is responsible for rw_exit(&p->p_reflock) iff pid != -1. 3034 */ 3035 int 3036 proc_find_reflocked(struct lwp *l, struct proc **p, pid_t pid, krw_t op) 3037 { 3038 int error; 3039 3040 mutex_enter(&proc_lock); 3041 if (pid == -1) 3042 *p = l->l_proc; 3043 else 3044 *p = proc_find(pid); 3045 3046 if (*p == NULL) { 3047 mutex_exit(&proc_lock); 3048 error = SET_ERROR(ESRCH); 3049 goto out; 3050 } 3051 if (pid != -1) { 3052 rw_enter(&(*p)->p_reflock, op); 3053 mutex_enter((*p)->p_lock); 3054 } 3055 mutex_exit(&proc_lock); 3056 3057 error = kauth_authorize_process(l->l_cred, 3058 KAUTH_PROCESS_CANSEE, *p, 3059 KAUTH_ARG(KAUTH_REQ_PROCESS_CANSEE_ENTRY), NULL, NULL); 3060 3061 out: if (pid != -1 && *p != NULL) { 3062 mutex_exit((*p)->p_lock); 3063 if (error) 3064 rw_exit(&(*p)->p_reflock); 3065 } 3066 KASSERT(error != 0 || pid == -1 || rw_lock_held(&(*p)->p_reflock)); 3067 KASSERT(error != 0 || pid == -1 || op != RW_READER || 3068 rw_read_held(&(*p)->p_reflock)); 3069 KASSERT(error != 0 || pid == -1 || op != RW_WRITER || 3070 rw_write_held(&(*p)->p_reflock)); 3071 return error; 3072 } 3073 3074 static int 3075 fill_pathname(struct lwp *l, pid_t pid, void *oldp, size_t *oldlenp) 3076 { 3077 int error; 3078 struct proc *p; 3079 3080 if ((error = proc_find_reflocked(l, &p, pid, RW_READER)) != 0) 3081 return error; 3082 KASSERT(pid == -1 || rw_read_held(&p->p_reflock)); 3083 3084 if (p->p_path == NULL) { 3085 error = SET_ERROR(ENOENT); 3086 goto out; 3087 } 3088 3089 size_t len = strlen(p->p_path) + 1; 3090 if (oldp != NULL) { 3091 size_t copylen = uimin(len, *oldlenp); 3092 error = sysctl_copyout(l, p->p_path, oldp, copylen); 3093 if (error == 0 && *oldlenp < len) 3094 error = SET_ERROR(ENOSPC); 3095 } 3096 *oldlenp = len; 3097 out: if (pid != -1) 3098 rw_exit(&p->p_reflock); 3099 return error; 3100 } 3101 3102 static int 3103 fill_cwd(struct lwp *l, pid_t pid, void *oldp, size_t *oldlenp) 3104 { 3105 int error; 3106 struct proc *p; 3107 char *path; 3108 char *bp, *bend; 3109 struct cwdinfo *cwdi; 3110 struct vnode *vp; 3111 size_t len, lenused; 3112 3113 if ((error = proc_find_reflocked(l, &p, pid, RW_READER)) != 0) 3114 return error; 3115 KASSERT(pid == -1 || rw_read_held(&p->p_reflock)); 3116 3117 len = MAXPATHLEN * 4; 3118 3119 path = kmem_alloc(len, KM_SLEEP); 3120 3121 bp = &path[len]; 3122 bend = bp; 3123 *(--bp) = '\0'; 3124 3125 cwdi = p->p_cwdi; 3126 rw_enter(&cwdi->cwdi_lock, RW_READER); 3127 vp = cwdi->cwdi_cdir; 3128 error = getcwd_common(vp, NULL, &bp, path, len/2, 0, l); 3129 rw_exit(&cwdi->cwdi_lock); 3130 3131 if (error) 3132 goto out; 3133 3134 lenused = bend - bp; 3135 3136 if (oldp != NULL) { 3137 size_t copylen = uimin(lenused, *oldlenp); 3138 error = sysctl_copyout(l, bp, oldp, copylen); 3139 if (error == 0 && *oldlenp < lenused) 3140 error = SET_ERROR(ENOSPC); 3141 } 3142 *oldlenp = lenused; 3143 out: 3144 if (pid != -1) 3145 rw_exit(&p->p_reflock); 3146 kmem_free(path, len); 3147 return error; 3148 } 3149 3150 int 3151 proc_getauxv(struct proc *p, void **buf, size_t *len) 3152 { 3153 struct ps_strings pss; 3154 int error; 3155 void *uauxv, *kauxv; 3156 size_t size; 3157 3158 if ((error = copyin_psstrings(p, &pss)) != 0) 3159 return error; 3160 if (pss.ps_envstr == NULL) 3161 return SET_ERROR(EIO); 3162 3163 size = p->p_execsw->es_arglen; 3164 if (size == 0) 3165 return SET_ERROR(EIO); 3166 3167 size_t ptrsz = PROC_PTRSZ(p); 3168 uauxv = (void *)((char *)pss.ps_envstr + (pss.ps_nenvstr + 1) * ptrsz); 3169 3170 kauxv = kmem_alloc(size, KM_SLEEP); 3171 3172 error = copyin_proc(p, uauxv, kauxv, size); 3173 if (error) { 3174 kmem_free(kauxv, size); 3175 return error; 3176 } 3177 3178 *buf = kauxv; 3179 *len = size; 3180 3181 return 0; 3182 } 3183 3184 3185 static int 3186 sysctl_security_expose_address(SYSCTLFN_ARGS) 3187 { 3188 int expose_address, error; 3189 struct sysctlnode node; 3190 3191 node = *rnode; 3192 node.sysctl_data = &expose_address; 3193 expose_address = *(int *)rnode->sysctl_data; 3194 error = sysctl_lookup(SYSCTLFN_CALL(&node)); 3195 if (error || newp == NULL) 3196 return error; 3197 3198 if (kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_KERNADDR, 3199 0, NULL, NULL, NULL)) 3200 return SET_ERROR(EPERM); 3201 3202 switch (expose_address) { 3203 case 0: 3204 case 1: 3205 case 2: 3206 break; 3207 default: 3208 return SET_ERROR(EINVAL); 3209 } 3210 3211 *(int *)rnode->sysctl_data = expose_address; 3212 3213 return 0; 3214 } 3215 3216 bool 3217 get_expose_address(struct proc *p) 3218 { 3219 /* allow only if sysctl variable is set or privileged */ 3220 return kauth_authorize_process(kauth_cred_get(), KAUTH_PROCESS_CANSEE, 3221 p, KAUTH_ARG(KAUTH_REQ_PROCESS_CANSEE_KPTR), NULL, NULL) == 0; 3222 } 3223