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