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