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