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