kern_proc.c revision 1.243.2.1 1 /* $NetBSD: kern_proc.c,v 1.243.2.1 2020/04/20 11:29:10 bouyer 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.243.2.1 2020/04/20 11:29:10 bouyer 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
871 void
872 proc_free_mem(struct proc *p)
873 {
874
875 kdtrace_proc_dtor(NULL, p);
876 pool_cache_put(proc_cache, p);
877 }
878
879 /*
880 * proc_enterpgrp: move p to a new or existing process group (and session).
881 *
882 * If we are creating a new pgrp, the pgid should equal
883 * the calling process' pid.
884 * If is only valid to enter a process group that is in the session
885 * of the process.
886 * Also mksess should only be set if we are creating a process group
887 *
888 * Only called from sys_setsid, sys_setpgid and posix_spawn/spawn_return.
889 */
890 int
891 proc_enterpgrp(struct proc *curp, pid_t pid, pid_t pgid, bool mksess)
892 {
893 struct pgrp *new_pgrp, *pgrp;
894 struct session *sess;
895 struct proc *p;
896 int rval;
897 pid_t pg_id = NO_PGID;
898
899 sess = mksess ? kmem_alloc(sizeof(*sess), KM_SLEEP) : NULL;
900
901 /* Allocate data areas we might need before doing any validity checks */
902 mutex_enter(proc_lock); /* Because pid_table might change */
903 if (pid_table[pgid & pid_tbl_mask].pt_pgrp == 0) {
904 mutex_exit(proc_lock);
905 new_pgrp = kmem_alloc(sizeof(*new_pgrp), KM_SLEEP);
906 mutex_enter(proc_lock);
907 } else
908 new_pgrp = NULL;
909 rval = EPERM; /* most common error (to save typing) */
910
911 /* Check pgrp exists or can be created */
912 pgrp = pid_table[pgid & pid_tbl_mask].pt_pgrp;
913 if (pgrp != NULL && pgrp->pg_id != pgid)
914 goto done;
915
916 /* Can only set another process under restricted circumstances. */
917 if (pid != curp->p_pid) {
918 /* Must exist and be one of our children... */
919 p = proc_find(pid);
920 if (p == NULL || !p_inferior(p, curp)) {
921 rval = ESRCH;
922 goto done;
923 }
924 /* ... in the same session... */
925 if (sess != NULL || p->p_session != curp->p_session)
926 goto done;
927 /* ... existing pgid must be in same session ... */
928 if (pgrp != NULL && pgrp->pg_session != p->p_session)
929 goto done;
930 /* ... and not done an exec. */
931 if (p->p_flag & PK_EXEC) {
932 rval = EACCES;
933 goto done;
934 }
935 } else {
936 /* ... setsid() cannot re-enter a pgrp */
937 if (mksess && (curp->p_pgid == curp->p_pid ||
938 pgrp_find(curp->p_pid)))
939 goto done;
940 p = curp;
941 }
942
943 /* Changing the process group/session of a session
944 leader is definitely off limits. */
945 if (SESS_LEADER(p)) {
946 if (sess == NULL && p->p_pgrp == pgrp)
947 /* unless it's a definite noop */
948 rval = 0;
949 goto done;
950 }
951
952 /* Can only create a process group with id of process */
953 if (pgrp == NULL && pgid != pid)
954 goto done;
955
956 /* Can only create a session if creating pgrp */
957 if (sess != NULL && pgrp != NULL)
958 goto done;
959
960 /* Check we allocated memory for a pgrp... */
961 if (pgrp == NULL && new_pgrp == NULL)
962 goto done;
963
964 /* Don't attach to 'zombie' pgrp */
965 if (pgrp != NULL && LIST_EMPTY(&pgrp->pg_members))
966 goto done;
967
968 /* Expect to succeed now */
969 rval = 0;
970
971 if (pgrp == p->p_pgrp)
972 /* nothing to do */
973 goto done;
974
975 /* Ok all setup, link up required structures */
976
977 if (pgrp == NULL) {
978 pgrp = new_pgrp;
979 new_pgrp = NULL;
980 if (sess != NULL) {
981 sess->s_sid = p->p_pid;
982 sess->s_leader = p;
983 sess->s_count = 1;
984 sess->s_ttyvp = NULL;
985 sess->s_ttyp = NULL;
986 sess->s_flags = p->p_session->s_flags & ~S_LOGIN_SET;
987 memcpy(sess->s_login, p->p_session->s_login,
988 sizeof(sess->s_login));
989 p->p_lflag &= ~PL_CONTROLT;
990 } else {
991 sess = p->p_pgrp->pg_session;
992 proc_sesshold(sess);
993 }
994 pgrp->pg_session = sess;
995 sess = NULL;
996
997 pgrp->pg_id = pgid;
998 LIST_INIT(&pgrp->pg_members);
999 #ifdef DIAGNOSTIC
1000 if (__predict_false(pid_table[pgid & pid_tbl_mask].pt_pgrp))
1001 panic("enterpgrp: pgrp table slot in use");
1002 if (__predict_false(mksess && p != curp))
1003 panic("enterpgrp: mksession and p != curproc");
1004 #endif
1005 pid_table[pgid & pid_tbl_mask].pt_pgrp = pgrp;
1006 pgrp->pg_jobc = 0;
1007 }
1008
1009 /*
1010 * Adjust eligibility of affected pgrps to participate in job control.
1011 * Increment eligibility counts before decrementing, otherwise we
1012 * could reach 0 spuriously during the first call.
1013 */
1014 fixjobc(p, pgrp, 1);
1015 fixjobc(p, p->p_pgrp, 0);
1016
1017 /* Interlock with ttread(). */
1018 mutex_spin_enter(&tty_lock);
1019
1020 /* Move process to requested group. */
1021 LIST_REMOVE(p, p_pglist);
1022 if (LIST_EMPTY(&p->p_pgrp->pg_members))
1023 /* defer delete until we've dumped the lock */
1024 pg_id = p->p_pgrp->pg_id;
1025 p->p_pgrp = pgrp;
1026 LIST_INSERT_HEAD(&pgrp->pg_members, p, p_pglist);
1027
1028 /* Done with the swap; we can release the tty mutex. */
1029 mutex_spin_exit(&tty_lock);
1030
1031 done:
1032 if (pg_id != NO_PGID) {
1033 /* Releases proc_lock. */
1034 pg_delete(pg_id);
1035 } else {
1036 mutex_exit(proc_lock);
1037 }
1038 if (sess != NULL)
1039 kmem_free(sess, sizeof(*sess));
1040 if (new_pgrp != NULL)
1041 kmem_free(new_pgrp, sizeof(*new_pgrp));
1042 #ifdef DEBUG_PGRP
1043 if (__predict_false(rval))
1044 printf("enterpgrp(%d,%d,%d), curproc %d, rval %d\n",
1045 pid, pgid, mksess, curp->p_pid, rval);
1046 #endif
1047 return rval;
1048 }
1049
1050 /*
1051 * proc_leavepgrp: remove a process from its process group.
1052 * => must be called with the proc_lock held, which will be released;
1053 */
1054 void
1055 proc_leavepgrp(struct proc *p)
1056 {
1057 struct pgrp *pgrp;
1058
1059 KASSERT(mutex_owned(proc_lock));
1060
1061 /* Interlock with ttread() */
1062 mutex_spin_enter(&tty_lock);
1063 pgrp = p->p_pgrp;
1064 LIST_REMOVE(p, p_pglist);
1065 p->p_pgrp = NULL;
1066 mutex_spin_exit(&tty_lock);
1067
1068 if (LIST_EMPTY(&pgrp->pg_members)) {
1069 /* Releases proc_lock. */
1070 pg_delete(pgrp->pg_id);
1071 } else {
1072 mutex_exit(proc_lock);
1073 }
1074 }
1075
1076 /*
1077 * pg_remove: remove a process group from the table.
1078 * => must be called with the proc_lock held;
1079 * => returns process group to free;
1080 */
1081 static struct pgrp *
1082 pg_remove(pid_t pg_id)
1083 {
1084 struct pgrp *pgrp;
1085 struct pid_table *pt;
1086
1087 KASSERT(mutex_owned(proc_lock));
1088
1089 pt = &pid_table[pg_id & pid_tbl_mask];
1090 pgrp = pt->pt_pgrp;
1091
1092 KASSERT(pgrp != NULL);
1093 KASSERT(pgrp->pg_id == pg_id);
1094 KASSERT(LIST_EMPTY(&pgrp->pg_members));
1095
1096 pt->pt_pgrp = NULL;
1097
1098 if (!P_VALID(pt->pt_proc)) {
1099 /* Orphaned pgrp, put slot onto free list. */
1100 KASSERT((P_NEXT(pt->pt_proc) & pid_tbl_mask) == 0);
1101 pg_id &= pid_tbl_mask;
1102 pt = &pid_table[last_free_pt];
1103 pt->pt_proc = P_FREE(P_NEXT(pt->pt_proc) | pg_id);
1104 KASSERT(pt->pt_pid == 0);
1105 last_free_pt = pg_id;
1106 pid_alloc_cnt--;
1107 }
1108 return pgrp;
1109 }
1110
1111 /*
1112 * pg_delete: delete and free a process group.
1113 * => must be called with the proc_lock held, which will be released.
1114 */
1115 static void
1116 pg_delete(pid_t pg_id)
1117 {
1118 struct pgrp *pg;
1119 struct tty *ttyp;
1120 struct session *ss;
1121
1122 KASSERT(mutex_owned(proc_lock));
1123
1124 pg = pid_table[pg_id & pid_tbl_mask].pt_pgrp;
1125 if (pg == NULL || pg->pg_id != pg_id || !LIST_EMPTY(&pg->pg_members)) {
1126 mutex_exit(proc_lock);
1127 return;
1128 }
1129
1130 ss = pg->pg_session;
1131
1132 /* Remove reference (if any) from tty to this process group */
1133 mutex_spin_enter(&tty_lock);
1134 ttyp = ss->s_ttyp;
1135 if (ttyp != NULL && ttyp->t_pgrp == pg) {
1136 ttyp->t_pgrp = NULL;
1137 KASSERT(ttyp->t_session == ss);
1138 }
1139 mutex_spin_exit(&tty_lock);
1140
1141 /*
1142 * The leading process group in a session is freed by proc_sessrele(),
1143 * if last reference. Note: proc_sessrele() releases proc_lock.
1144 */
1145 pg = (ss->s_sid != pg->pg_id) ? pg_remove(pg_id) : NULL;
1146 proc_sessrele(ss);
1147
1148 if (pg != NULL) {
1149 /* Free it, if was not done by proc_sessrele(). */
1150 kmem_free(pg, sizeof(struct pgrp));
1151 }
1152 }
1153
1154 /*
1155 * Adjust pgrp jobc counters when specified process changes process group.
1156 * We count the number of processes in each process group that "qualify"
1157 * the group for terminal job control (those with a parent in a different
1158 * process group of the same session). If that count reaches zero, the
1159 * process group becomes orphaned. Check both the specified process'
1160 * process group and that of its children.
1161 * entering == 0 => p is leaving specified group.
1162 * entering == 1 => p is entering specified group.
1163 *
1164 * Call with proc_lock held.
1165 */
1166 void
1167 fixjobc(struct proc *p, struct pgrp *pgrp, int entering)
1168 {
1169 struct pgrp *hispgrp;
1170 struct session *mysession = pgrp->pg_session;
1171 struct proc *child;
1172
1173 KASSERT(mutex_owned(proc_lock));
1174
1175 /*
1176 * Check p's parent to see whether p qualifies its own process
1177 * group; if so, adjust count for p's process group.
1178 */
1179 hispgrp = p->p_pptr->p_pgrp;
1180 if (hispgrp != pgrp && hispgrp->pg_session == mysession) {
1181 if (entering) {
1182 pgrp->pg_jobc++;
1183 p->p_lflag &= ~PL_ORPHANPG;
1184 } else if (--pgrp->pg_jobc == 0)
1185 orphanpg(pgrp);
1186 }
1187
1188 /*
1189 * Check this process' children to see whether they qualify
1190 * their process groups; if so, adjust counts for children's
1191 * process groups.
1192 */
1193 LIST_FOREACH(child, &p->p_children, p_sibling) {
1194 hispgrp = child->p_pgrp;
1195 if (hispgrp != pgrp && hispgrp->pg_session == mysession &&
1196 !P_ZOMBIE(child)) {
1197 if (entering) {
1198 child->p_lflag &= ~PL_ORPHANPG;
1199 hispgrp->pg_jobc++;
1200 } else if (--hispgrp->pg_jobc == 0)
1201 orphanpg(hispgrp);
1202 }
1203 }
1204 }
1205
1206 /*
1207 * A process group has become orphaned;
1208 * if there are any stopped processes in the group,
1209 * hang-up all process in that group.
1210 *
1211 * Call with proc_lock held.
1212 */
1213 static void
1214 orphanpg(struct pgrp *pg)
1215 {
1216 struct proc *p;
1217
1218 KASSERT(mutex_owned(proc_lock));
1219
1220 LIST_FOREACH(p, &pg->pg_members, p_pglist) {
1221 if (p->p_stat == SSTOP) {
1222 p->p_lflag |= PL_ORPHANPG;
1223 psignal(p, SIGHUP);
1224 psignal(p, SIGCONT);
1225 }
1226 }
1227 }
1228
1229 #ifdef DDB
1230 #include <ddb/db_output.h>
1231 void pidtbl_dump(void);
1232 void
1233 pidtbl_dump(void)
1234 {
1235 struct pid_table *pt;
1236 struct proc *p;
1237 struct pgrp *pgrp;
1238 int id;
1239
1240 db_printf("pid table %p size %x, next %x, last %x\n",
1241 pid_table, pid_tbl_mask+1,
1242 next_free_pt, last_free_pt);
1243 for (pt = pid_table, id = 0; id <= pid_tbl_mask; id++, pt++) {
1244 p = pt->pt_proc;
1245 if (!P_VALID(p) && !pt->pt_pgrp)
1246 continue;
1247 db_printf(" id %x: ", id);
1248 if (P_VALID(p))
1249 db_printf("slotpid %d proc %p id %d (0x%x) %s\n",
1250 pt->pt_pid, p, p->p_pid, p->p_pid, p->p_comm);
1251 else
1252 db_printf("next %x use %x\n",
1253 P_NEXT(p) & pid_tbl_mask,
1254 P_NEXT(p) & ~pid_tbl_mask);
1255 if ((pgrp = pt->pt_pgrp)) {
1256 db_printf("\tsession %p, sid %d, count %d, login %s\n",
1257 pgrp->pg_session, pgrp->pg_session->s_sid,
1258 pgrp->pg_session->s_count,
1259 pgrp->pg_session->s_login);
1260 db_printf("\tpgrp %p, pg_id %d, pg_jobc %d, members %p\n",
1261 pgrp, pgrp->pg_id, pgrp->pg_jobc,
1262 LIST_FIRST(&pgrp->pg_members));
1263 LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
1264 db_printf("\t\tpid %d addr %p pgrp %p %s\n",
1265 p->p_pid, p, p->p_pgrp, p->p_comm);
1266 }
1267 }
1268 }
1269 }
1270 #endif /* DDB */
1271
1272 #ifdef KSTACK_CHECK_MAGIC
1273
1274 #define KSTACK_MAGIC 0xdeadbeaf
1275
1276 /* XXX should be per process basis? */
1277 static int kstackleftmin = KSTACK_SIZE;
1278 static int kstackleftthres = KSTACK_SIZE / 8;
1279
1280 void
1281 kstack_setup_magic(const struct lwp *l)
1282 {
1283 uint32_t *ip;
1284 uint32_t const *end;
1285
1286 KASSERT(l != NULL);
1287 KASSERT(l != &lwp0);
1288
1289 /*
1290 * fill all the stack with magic number
1291 * so that later modification on it can be detected.
1292 */
1293 ip = (uint32_t *)KSTACK_LOWEST_ADDR(l);
1294 end = (uint32_t *)((char *)KSTACK_LOWEST_ADDR(l) + KSTACK_SIZE);
1295 for (; ip < end; ip++) {
1296 *ip = KSTACK_MAGIC;
1297 }
1298 }
1299
1300 void
1301 kstack_check_magic(const struct lwp *l)
1302 {
1303 uint32_t const *ip, *end;
1304 int stackleft;
1305
1306 KASSERT(l != NULL);
1307
1308 /* don't check proc0 */ /*XXX*/
1309 if (l == &lwp0)
1310 return;
1311
1312 #ifdef __MACHINE_STACK_GROWS_UP
1313 /* stack grows upwards (eg. hppa) */
1314 ip = (uint32_t *)((void *)KSTACK_LOWEST_ADDR(l) + KSTACK_SIZE);
1315 end = (uint32_t *)KSTACK_LOWEST_ADDR(l);
1316 for (ip--; ip >= end; ip--)
1317 if (*ip != KSTACK_MAGIC)
1318 break;
1319
1320 stackleft = (void *)KSTACK_LOWEST_ADDR(l) + KSTACK_SIZE - (void *)ip;
1321 #else /* __MACHINE_STACK_GROWS_UP */
1322 /* stack grows downwards (eg. i386) */
1323 ip = (uint32_t *)KSTACK_LOWEST_ADDR(l);
1324 end = (uint32_t *)((char *)KSTACK_LOWEST_ADDR(l) + KSTACK_SIZE);
1325 for (; ip < end; ip++)
1326 if (*ip != KSTACK_MAGIC)
1327 break;
1328
1329 stackleft = ((const char *)ip) - (const char *)KSTACK_LOWEST_ADDR(l);
1330 #endif /* __MACHINE_STACK_GROWS_UP */
1331
1332 if (kstackleftmin > stackleft) {
1333 kstackleftmin = stackleft;
1334 if (stackleft < kstackleftthres)
1335 printf("warning: kernel stack left %d bytes"
1336 "(pid %u:lid %u)\n", stackleft,
1337 (u_int)l->l_proc->p_pid, (u_int)l->l_lid);
1338 }
1339
1340 if (stackleft <= 0) {
1341 panic("magic on the top of kernel stack changed for "
1342 "pid %u, lid %u: maybe kernel stack overflow",
1343 (u_int)l->l_proc->p_pid, (u_int)l->l_lid);
1344 }
1345 }
1346 #endif /* KSTACK_CHECK_MAGIC */
1347
1348 int
1349 proclist_foreach_call(struct proclist *list,
1350 int (*callback)(struct proc *, void *arg), void *arg)
1351 {
1352 struct proc marker;
1353 struct proc *p;
1354 int ret = 0;
1355
1356 marker.p_flag = PK_MARKER;
1357 mutex_enter(proc_lock);
1358 for (p = LIST_FIRST(list); ret == 0 && p != NULL;) {
1359 if (p->p_flag & PK_MARKER) {
1360 p = LIST_NEXT(p, p_list);
1361 continue;
1362 }
1363 LIST_INSERT_AFTER(p, &marker, p_list);
1364 ret = (*callback)(p, arg);
1365 KASSERT(mutex_owned(proc_lock));
1366 p = LIST_NEXT(&marker, p_list);
1367 LIST_REMOVE(&marker, p_list);
1368 }
1369 mutex_exit(proc_lock);
1370
1371 return ret;
1372 }
1373
1374 int
1375 proc_vmspace_getref(struct proc *p, struct vmspace **vm)
1376 {
1377
1378 /* XXXCDC: how should locking work here? */
1379
1380 /* curproc exception is for coredump. */
1381
1382 if ((p != curproc && (p->p_sflag & PS_WEXIT) != 0) ||
1383 (p->p_vmspace->vm_refcnt < 1)) { /* XXX */
1384 return EFAULT;
1385 }
1386
1387 uvmspace_addref(p->p_vmspace);
1388 *vm = p->p_vmspace;
1389
1390 return 0;
1391 }
1392
1393 /*
1394 * Acquire a write lock on the process credential.
1395 */
1396 void
1397 proc_crmod_enter(void)
1398 {
1399 struct lwp *l = curlwp;
1400 struct proc *p = l->l_proc;
1401 kauth_cred_t oc;
1402
1403 /* Reset what needs to be reset in plimit. */
1404 if (p->p_limit->pl_corename != defcorename) {
1405 lim_setcorename(p, defcorename, 0);
1406 }
1407
1408 mutex_enter(p->p_lock);
1409
1410 /* Ensure the LWP cached credentials are up to date. */
1411 if ((oc = l->l_cred) != p->p_cred) {
1412 kauth_cred_hold(p->p_cred);
1413 l->l_cred = p->p_cred;
1414 kauth_cred_free(oc);
1415 }
1416 }
1417
1418 /*
1419 * Set in a new process credential, and drop the write lock. The credential
1420 * must have a reference already. Optionally, free a no-longer required
1421 * credential. The scheduler also needs to inspect p_cred, so we also
1422 * briefly acquire the sched state mutex.
1423 */
1424 void
1425 proc_crmod_leave(kauth_cred_t scred, kauth_cred_t fcred, bool sugid)
1426 {
1427 struct lwp *l = curlwp, *l2;
1428 struct proc *p = l->l_proc;
1429 kauth_cred_t oc;
1430
1431 KASSERT(mutex_owned(p->p_lock));
1432
1433 /* Is there a new credential to set in? */
1434 if (scred != NULL) {
1435 p->p_cred = scred;
1436 LIST_FOREACH(l2, &p->p_lwps, l_sibling) {
1437 if (l2 != l)
1438 l2->l_prflag |= LPR_CRMOD;
1439 }
1440
1441 /* Ensure the LWP cached credentials are up to date. */
1442 if ((oc = l->l_cred) != scred) {
1443 kauth_cred_hold(scred);
1444 l->l_cred = scred;
1445 }
1446 } else
1447 oc = NULL; /* XXXgcc */
1448
1449 if (sugid) {
1450 /*
1451 * Mark process as having changed credentials, stops
1452 * tracing etc.
1453 */
1454 p->p_flag |= PK_SUGID;
1455 }
1456
1457 mutex_exit(p->p_lock);
1458
1459 /* If there is a credential to be released, free it now. */
1460 if (fcred != NULL) {
1461 KASSERT(scred != NULL);
1462 kauth_cred_free(fcred);
1463 if (oc != scred)
1464 kauth_cred_free(oc);
1465 }
1466 }
1467
1468 /*
1469 * proc_specific_key_create --
1470 * Create a key for subsystem proc-specific data.
1471 */
1472 int
1473 proc_specific_key_create(specificdata_key_t *keyp, specificdata_dtor_t dtor)
1474 {
1475
1476 return (specificdata_key_create(proc_specificdata_domain, keyp, dtor));
1477 }
1478
1479 /*
1480 * proc_specific_key_delete --
1481 * Delete a key for subsystem proc-specific data.
1482 */
1483 void
1484 proc_specific_key_delete(specificdata_key_t key)
1485 {
1486
1487 specificdata_key_delete(proc_specificdata_domain, key);
1488 }
1489
1490 /*
1491 * proc_initspecific --
1492 * Initialize a proc's specificdata container.
1493 */
1494 void
1495 proc_initspecific(struct proc *p)
1496 {
1497 int error __diagused;
1498
1499 error = specificdata_init(proc_specificdata_domain, &p->p_specdataref);
1500 KASSERT(error == 0);
1501 }
1502
1503 /*
1504 * proc_finispecific --
1505 * Finalize a proc's specificdata container.
1506 */
1507 void
1508 proc_finispecific(struct proc *p)
1509 {
1510
1511 specificdata_fini(proc_specificdata_domain, &p->p_specdataref);
1512 }
1513
1514 /*
1515 * proc_getspecific --
1516 * Return proc-specific data corresponding to the specified key.
1517 */
1518 void *
1519 proc_getspecific(struct proc *p, specificdata_key_t key)
1520 {
1521
1522 return (specificdata_getspecific(proc_specificdata_domain,
1523 &p->p_specdataref, key));
1524 }
1525
1526 /*
1527 * proc_setspecific --
1528 * Set proc-specific data corresponding to the specified key.
1529 */
1530 void
1531 proc_setspecific(struct proc *p, specificdata_key_t key, void *data)
1532 {
1533
1534 specificdata_setspecific(proc_specificdata_domain,
1535 &p->p_specdataref, key, data);
1536 }
1537
1538 int
1539 proc_uidmatch(kauth_cred_t cred, kauth_cred_t target)
1540 {
1541 int r = 0;
1542
1543 if (kauth_cred_getuid(cred) != kauth_cred_getuid(target) ||
1544 kauth_cred_getuid(cred) != kauth_cred_getsvuid(target)) {
1545 /*
1546 * suid proc of ours or proc not ours
1547 */
1548 r = EPERM;
1549 } else if (kauth_cred_getgid(target) != kauth_cred_getsvgid(target)) {
1550 /*
1551 * sgid proc has sgid back to us temporarily
1552 */
1553 r = EPERM;
1554 } else {
1555 /*
1556 * our rgid must be in target's group list (ie,
1557 * sub-processes started by a sgid process)
1558 */
1559 int ismember = 0;
1560
1561 if (kauth_cred_ismember_gid(cred,
1562 kauth_cred_getgid(target), &ismember) != 0 ||
1563 !ismember)
1564 r = EPERM;
1565 }
1566
1567 return (r);
1568 }
1569
1570 /*
1571 * sysctl stuff
1572 */
1573
1574 #define KERN_PROCSLOP (5 * sizeof(struct kinfo_proc))
1575
1576 static const u_int sysctl_flagmap[] = {
1577 PK_ADVLOCK, P_ADVLOCK,
1578 PK_EXEC, P_EXEC,
1579 PK_NOCLDWAIT, P_NOCLDWAIT,
1580 PK_32, P_32,
1581 PK_CLDSIGIGN, P_CLDSIGIGN,
1582 PK_SUGID, P_SUGID,
1583 0
1584 };
1585
1586 static const u_int sysctl_sflagmap[] = {
1587 PS_NOCLDSTOP, P_NOCLDSTOP,
1588 PS_WEXIT, P_WEXIT,
1589 PS_STOPFORK, P_STOPFORK,
1590 PS_STOPEXEC, P_STOPEXEC,
1591 PS_STOPEXIT, P_STOPEXIT,
1592 0
1593 };
1594
1595 static const u_int sysctl_slflagmap[] = {
1596 PSL_TRACED, P_TRACED,
1597 PSL_CHTRACED, P_CHTRACED,
1598 PSL_SYSCALL, P_SYSCALL,
1599 0
1600 };
1601
1602 static const u_int sysctl_lflagmap[] = {
1603 PL_CONTROLT, P_CONTROLT,
1604 PL_PPWAIT, P_PPWAIT,
1605 0
1606 };
1607
1608 static const u_int sysctl_stflagmap[] = {
1609 PST_PROFIL, P_PROFIL,
1610 0
1611
1612 };
1613
1614 /* used by kern_lwp also */
1615 const u_int sysctl_lwpflagmap[] = {
1616 LW_SINTR, L_SINTR,
1617 LW_SYSTEM, L_SYSTEM,
1618 0
1619 };
1620
1621 /*
1622 * Find the most ``active'' lwp of a process and return it for ps display
1623 * purposes
1624 */
1625 static struct lwp *
1626 proc_active_lwp(struct proc *p)
1627 {
1628 static const int ostat[] = {
1629 0,
1630 2, /* LSIDL */
1631 6, /* LSRUN */
1632 5, /* LSSLEEP */
1633 4, /* LSSTOP */
1634 0, /* LSZOMB */
1635 1, /* LSDEAD */
1636 7, /* LSONPROC */
1637 3 /* LSSUSPENDED */
1638 };
1639
1640 struct lwp *l, *lp = NULL;
1641 LIST_FOREACH(l, &p->p_lwps, l_sibling) {
1642 KASSERT(l->l_stat >= 0 && l->l_stat < __arraycount(ostat));
1643 if (lp == NULL ||
1644 ostat[l->l_stat] > ostat[lp->l_stat] ||
1645 (ostat[l->l_stat] == ostat[lp->l_stat] &&
1646 l->l_cpticks > lp->l_cpticks)) {
1647 lp = l;
1648 continue;
1649 }
1650 }
1651 return lp;
1652 }
1653
1654 static int
1655 sysctl_doeproc(SYSCTLFN_ARGS)
1656 {
1657 union {
1658 struct kinfo_proc kproc;
1659 struct kinfo_proc2 kproc2;
1660 } *kbuf;
1661 struct proc *p, *next, *marker;
1662 char *where, *dp;
1663 int type, op, arg, error;
1664 u_int elem_size, kelem_size, elem_count;
1665 size_t buflen, needed;
1666 bool match, zombie, mmmbrains;
1667 const bool allowaddr = get_expose_address(curproc);
1668
1669 if (namelen == 1 && name[0] == CTL_QUERY)
1670 return (sysctl_query(SYSCTLFN_CALL(rnode)));
1671
1672 dp = where = oldp;
1673 buflen = where != NULL ? *oldlenp : 0;
1674 error = 0;
1675 needed = 0;
1676 type = rnode->sysctl_num;
1677
1678 if (type == KERN_PROC) {
1679 if (namelen == 0)
1680 return EINVAL;
1681 switch (op = name[0]) {
1682 case KERN_PROC_ALL:
1683 if (namelen != 1)
1684 return EINVAL;
1685 arg = 0;
1686 break;
1687 default:
1688 if (namelen != 2)
1689 return EINVAL;
1690 arg = name[1];
1691 break;
1692 }
1693 elem_count = 0; /* Hush little compiler, don't you cry */
1694 kelem_size = elem_size = sizeof(kbuf->kproc);
1695 } else {
1696 if (namelen != 4)
1697 return EINVAL;
1698 op = name[0];
1699 arg = name[1];
1700 elem_size = name[2];
1701 elem_count = name[3];
1702 kelem_size = sizeof(kbuf->kproc2);
1703 }
1704
1705 sysctl_unlock();
1706
1707 kbuf = kmem_zalloc(sizeof(*kbuf), KM_SLEEP);
1708 marker = kmem_alloc(sizeof(*marker), KM_SLEEP);
1709 marker->p_flag = PK_MARKER;
1710
1711 mutex_enter(proc_lock);
1712 /*
1713 * Start with zombies to prevent reporting processes twice, in case they
1714 * are dying and being moved from the list of alive processes to zombies.
1715 */
1716 mmmbrains = true;
1717 for (p = LIST_FIRST(&zombproc);; p = next) {
1718 if (p == NULL) {
1719 if (mmmbrains) {
1720 p = LIST_FIRST(&allproc);
1721 mmmbrains = false;
1722 }
1723 if (p == NULL)
1724 break;
1725 }
1726 next = LIST_NEXT(p, p_list);
1727 if ((p->p_flag & PK_MARKER) != 0)
1728 continue;
1729
1730 /*
1731 * Skip embryonic processes.
1732 */
1733 if (p->p_stat == SIDL)
1734 continue;
1735
1736 mutex_enter(p->p_lock);
1737 error = kauth_authorize_process(l->l_cred,
1738 KAUTH_PROCESS_CANSEE, p,
1739 KAUTH_ARG(KAUTH_REQ_PROCESS_CANSEE_EPROC), NULL, NULL);
1740 if (error != 0) {
1741 mutex_exit(p->p_lock);
1742 continue;
1743 }
1744
1745 /*
1746 * Hande all the operations in one switch on the cost of
1747 * algorithm complexity is on purpose. The win splitting this
1748 * function into several similar copies makes maintenance burden
1749 * burden, code grow and boost is neglible in practical systems.
1750 */
1751 switch (op) {
1752 case KERN_PROC_PID:
1753 match = (p->p_pid == (pid_t)arg);
1754 break;
1755
1756 case KERN_PROC_PGRP:
1757 match = (p->p_pgrp->pg_id == (pid_t)arg);
1758 break;
1759
1760 case KERN_PROC_SESSION:
1761 match = (p->p_session->s_sid == (pid_t)arg);
1762 break;
1763
1764 case KERN_PROC_TTY:
1765 match = true;
1766 if (arg == (int) KERN_PROC_TTY_REVOKE) {
1767 if ((p->p_lflag & PL_CONTROLT) == 0 ||
1768 p->p_session->s_ttyp == NULL ||
1769 p->p_session->s_ttyvp != NULL) {
1770 match = false;
1771 }
1772 } else if ((p->p_lflag & PL_CONTROLT) == 0 ||
1773 p->p_session->s_ttyp == NULL) {
1774 if ((dev_t)arg != KERN_PROC_TTY_NODEV) {
1775 match = false;
1776 }
1777 } else if (p->p_session->s_ttyp->t_dev != (dev_t)arg) {
1778 match = false;
1779 }
1780 break;
1781
1782 case KERN_PROC_UID:
1783 match = (kauth_cred_geteuid(p->p_cred) == (uid_t)arg);
1784 break;
1785
1786 case KERN_PROC_RUID:
1787 match = (kauth_cred_getuid(p->p_cred) == (uid_t)arg);
1788 break;
1789
1790 case KERN_PROC_GID:
1791 match = (kauth_cred_getegid(p->p_cred) == (uid_t)arg);
1792 break;
1793
1794 case KERN_PROC_RGID:
1795 match = (kauth_cred_getgid(p->p_cred) == (uid_t)arg);
1796 break;
1797
1798 case KERN_PROC_ALL:
1799 match = true;
1800 /* allow everything */
1801 break;
1802
1803 default:
1804 error = EINVAL;
1805 mutex_exit(p->p_lock);
1806 goto cleanup;
1807 }
1808 if (!match) {
1809 mutex_exit(p->p_lock);
1810 continue;
1811 }
1812
1813 /*
1814 * Grab a hold on the process.
1815 */
1816 if (mmmbrains) {
1817 zombie = true;
1818 } else {
1819 zombie = !rw_tryenter(&p->p_reflock, RW_READER);
1820 }
1821 if (zombie) {
1822 LIST_INSERT_AFTER(p, marker, p_list);
1823 }
1824
1825 if (buflen >= elem_size &&
1826 (type == KERN_PROC || elem_count > 0)) {
1827 ruspace(p); /* Update process vm resource use */
1828
1829 if (type == KERN_PROC) {
1830 fill_proc(p, &kbuf->kproc.kp_proc, allowaddr);
1831 fill_eproc(p, &kbuf->kproc.kp_eproc, zombie,
1832 allowaddr);
1833 } else {
1834 fill_kproc2(p, &kbuf->kproc2, zombie,
1835 allowaddr);
1836 elem_count--;
1837 }
1838 mutex_exit(p->p_lock);
1839 mutex_exit(proc_lock);
1840 /*
1841 * Copy out elem_size, but not larger than kelem_size
1842 */
1843 error = sysctl_copyout(l, kbuf, dp,
1844 uimin(kelem_size, elem_size));
1845 mutex_enter(proc_lock);
1846 if (error) {
1847 goto bah;
1848 }
1849 dp += elem_size;
1850 buflen -= elem_size;
1851 } else {
1852 mutex_exit(p->p_lock);
1853 }
1854 needed += elem_size;
1855
1856 /*
1857 * Release reference to process.
1858 */
1859 if (zombie) {
1860 next = LIST_NEXT(marker, p_list);
1861 LIST_REMOVE(marker, p_list);
1862 } else {
1863 rw_exit(&p->p_reflock);
1864 next = LIST_NEXT(p, p_list);
1865 }
1866
1867 /*
1868 * Short-circuit break quickly!
1869 */
1870 if (op == KERN_PROC_PID)
1871 break;
1872 }
1873 mutex_exit(proc_lock);
1874
1875 if (where != NULL) {
1876 *oldlenp = dp - where;
1877 if (needed > *oldlenp) {
1878 error = ENOMEM;
1879 goto out;
1880 }
1881 } else {
1882 needed += KERN_PROCSLOP;
1883 *oldlenp = needed;
1884 }
1885 kmem_free(kbuf, sizeof(*kbuf));
1886 kmem_free(marker, sizeof(*marker));
1887 sysctl_relock();
1888 return 0;
1889 bah:
1890 if (zombie)
1891 LIST_REMOVE(marker, p_list);
1892 else
1893 rw_exit(&p->p_reflock);
1894 cleanup:
1895 mutex_exit(proc_lock);
1896 out:
1897 kmem_free(kbuf, sizeof(*kbuf));
1898 kmem_free(marker, sizeof(*marker));
1899 sysctl_relock();
1900 return error;
1901 }
1902
1903 int
1904 copyin_psstrings(struct proc *p, struct ps_strings *arginfo)
1905 {
1906 #if !defined(_RUMPKERNEL)
1907 int retval;
1908
1909 if (p->p_flag & PK_32) {
1910 MODULE_HOOK_CALL(kern_proc32_copyin_hook, (p, arginfo),
1911 enosys(), retval);
1912 return retval;
1913 }
1914 #endif /* !defined(_RUMPKERNEL) */
1915
1916 return copyin_proc(p, (void *)p->p_psstrp, arginfo, sizeof(*arginfo));
1917 }
1918
1919 static int
1920 copy_procargs_sysctl_cb(void *cookie_, const void *src, size_t off, size_t len)
1921 {
1922 void **cookie = cookie_;
1923 struct lwp *l = cookie[0];
1924 char *dst = cookie[1];
1925
1926 return sysctl_copyout(l, src, dst + off, len);
1927 }
1928
1929 /*
1930 * sysctl helper routine for kern.proc_args pseudo-subtree.
1931 */
1932 static int
1933 sysctl_kern_proc_args(SYSCTLFN_ARGS)
1934 {
1935 struct ps_strings pss;
1936 struct proc *p;
1937 pid_t pid;
1938 int type, error;
1939 void *cookie[2];
1940
1941 if (namelen == 1 && name[0] == CTL_QUERY)
1942 return (sysctl_query(SYSCTLFN_CALL(rnode)));
1943
1944 if (newp != NULL || namelen != 2)
1945 return (EINVAL);
1946 pid = name[0];
1947 type = name[1];
1948
1949 switch (type) {
1950 case KERN_PROC_PATHNAME:
1951 sysctl_unlock();
1952 error = fill_pathname(l, pid, oldp, oldlenp);
1953 sysctl_relock();
1954 return error;
1955
1956 case KERN_PROC_CWD:
1957 sysctl_unlock();
1958 error = fill_cwd(l, pid, oldp, oldlenp);
1959 sysctl_relock();
1960 return error;
1961
1962 case KERN_PROC_ARGV:
1963 case KERN_PROC_NARGV:
1964 case KERN_PROC_ENV:
1965 case KERN_PROC_NENV:
1966 /* ok */
1967 break;
1968 default:
1969 return (EINVAL);
1970 }
1971
1972 sysctl_unlock();
1973
1974 /* check pid */
1975 mutex_enter(proc_lock);
1976 if ((p = proc_find(pid)) == NULL) {
1977 error = EINVAL;
1978 goto out_locked;
1979 }
1980 mutex_enter(p->p_lock);
1981
1982 /* Check permission. */
1983 if (type == KERN_PROC_ARGV || type == KERN_PROC_NARGV)
1984 error = kauth_authorize_process(l->l_cred, KAUTH_PROCESS_CANSEE,
1985 p, KAUTH_ARG(KAUTH_REQ_PROCESS_CANSEE_ARGS), NULL, NULL);
1986 else if (type == KERN_PROC_ENV || type == KERN_PROC_NENV)
1987 error = kauth_authorize_process(l->l_cred, KAUTH_PROCESS_CANSEE,
1988 p, KAUTH_ARG(KAUTH_REQ_PROCESS_CANSEE_ENV), NULL, NULL);
1989 else
1990 error = EINVAL; /* XXXGCC */
1991 if (error) {
1992 mutex_exit(p->p_lock);
1993 goto out_locked;
1994 }
1995
1996 if (oldp == NULL) {
1997 if (type == KERN_PROC_NARGV || type == KERN_PROC_NENV)
1998 *oldlenp = sizeof (int);
1999 else
2000 *oldlenp = ARG_MAX; /* XXX XXX XXX */
2001 error = 0;
2002 mutex_exit(p->p_lock);
2003 goto out_locked;
2004 }
2005
2006 /*
2007 * Zombies don't have a stack, so we can't read their psstrings.
2008 * System processes also don't have a user stack.
2009 */
2010 if (P_ZOMBIE(p) || (p->p_flag & PK_SYSTEM) != 0) {
2011 error = EINVAL;
2012 mutex_exit(p->p_lock);
2013 goto out_locked;
2014 }
2015
2016 error = rw_tryenter(&p->p_reflock, RW_READER) ? 0 : EBUSY;
2017 mutex_exit(p->p_lock);
2018 if (error) {
2019 goto out_locked;
2020 }
2021 mutex_exit(proc_lock);
2022
2023 if (type == KERN_PROC_NARGV || type == KERN_PROC_NENV) {
2024 int value;
2025 if ((error = copyin_psstrings(p, &pss)) == 0) {
2026 if (type == KERN_PROC_NARGV)
2027 value = pss.ps_nargvstr;
2028 else
2029 value = pss.ps_nenvstr;
2030 error = sysctl_copyout(l, &value, oldp, sizeof(value));
2031 *oldlenp = sizeof(value);
2032 }
2033 } else {
2034 cookie[0] = l;
2035 cookie[1] = oldp;
2036 error = copy_procargs(p, type, oldlenp,
2037 copy_procargs_sysctl_cb, cookie);
2038 }
2039 rw_exit(&p->p_reflock);
2040 sysctl_relock();
2041 return error;
2042
2043 out_locked:
2044 mutex_exit(proc_lock);
2045 sysctl_relock();
2046 return error;
2047 }
2048
2049 int
2050 copy_procargs(struct proc *p, int oid, size_t *limit,
2051 int (*cb)(void *, const void *, size_t, size_t), void *cookie)
2052 {
2053 struct ps_strings pss;
2054 size_t len, i, loaded, entry_len;
2055 struct uio auio;
2056 struct iovec aiov;
2057 int error, argvlen;
2058 char *arg;
2059 char **argv;
2060 vaddr_t user_argv;
2061 struct vmspace *vmspace;
2062
2063 /*
2064 * Allocate a temporary buffer to hold the argument vector and
2065 * the arguments themselve.
2066 */
2067 arg = kmem_alloc(PAGE_SIZE, KM_SLEEP);
2068 argv = kmem_alloc(PAGE_SIZE, KM_SLEEP);
2069
2070 /*
2071 * Lock the process down in memory.
2072 */
2073 vmspace = p->p_vmspace;
2074 uvmspace_addref(vmspace);
2075
2076 /*
2077 * Read in the ps_strings structure.
2078 */
2079 if ((error = copyin_psstrings(p, &pss)) != 0)
2080 goto done;
2081
2082 /*
2083 * Now read the address of the argument vector.
2084 */
2085 switch (oid) {
2086 case KERN_PROC_ARGV:
2087 user_argv = (uintptr_t)pss.ps_argvstr;
2088 argvlen = pss.ps_nargvstr;
2089 break;
2090 case KERN_PROC_ENV:
2091 user_argv = (uintptr_t)pss.ps_envstr;
2092 argvlen = pss.ps_nenvstr;
2093 break;
2094 default:
2095 error = EINVAL;
2096 goto done;
2097 }
2098
2099 if (argvlen < 0) {
2100 error = EIO;
2101 goto done;
2102 }
2103
2104
2105 /*
2106 * Now copy each string.
2107 */
2108 len = 0; /* bytes written to user buffer */
2109 loaded = 0; /* bytes from argv already processed */
2110 i = 0; /* To make compiler happy */
2111 entry_len = PROC_PTRSZ(p);
2112
2113 for (; argvlen; --argvlen) {
2114 int finished = 0;
2115 vaddr_t base;
2116 size_t xlen;
2117 int j;
2118
2119 if (loaded == 0) {
2120 size_t rem = entry_len * argvlen;
2121 loaded = MIN(rem, PAGE_SIZE);
2122 error = copyin_vmspace(vmspace,
2123 (const void *)user_argv, argv, loaded);
2124 if (error)
2125 break;
2126 user_argv += loaded;
2127 i = 0;
2128 }
2129
2130 #if !defined(_RUMPKERNEL)
2131 if (p->p_flag & PK_32)
2132 MODULE_HOOK_CALL(kern_proc32_base_hook,
2133 (argv, i++), 0, base);
2134 else
2135 #endif /* !defined(_RUMPKERNEL) */
2136 base = (vaddr_t)argv[i++];
2137 loaded -= entry_len;
2138
2139 /*
2140 * The program has messed around with its arguments,
2141 * possibly deleting some, and replacing them with
2142 * NULL's. Treat this as the last argument and not
2143 * a failure.
2144 */
2145 if (base == 0)
2146 break;
2147
2148 while (!finished) {
2149 xlen = PAGE_SIZE - (base & PAGE_MASK);
2150
2151 aiov.iov_base = arg;
2152 aiov.iov_len = PAGE_SIZE;
2153 auio.uio_iov = &aiov;
2154 auio.uio_iovcnt = 1;
2155 auio.uio_offset = base;
2156 auio.uio_resid = xlen;
2157 auio.uio_rw = UIO_READ;
2158 UIO_SETUP_SYSSPACE(&auio);
2159 error = uvm_io(&vmspace->vm_map, &auio, 0);
2160 if (error)
2161 goto done;
2162
2163 /* Look for the end of the string */
2164 for (j = 0; j < xlen; j++) {
2165 if (arg[j] == '\0') {
2166 xlen = j + 1;
2167 finished = 1;
2168 break;
2169 }
2170 }
2171
2172 /* Check for user buffer overflow */
2173 if (len + xlen > *limit) {
2174 finished = 1;
2175 if (len > *limit)
2176 xlen = 0;
2177 else
2178 xlen = *limit - len;
2179 }
2180
2181 /* Copyout the page */
2182 error = (*cb)(cookie, arg, len, xlen);
2183 if (error)
2184 goto done;
2185
2186 len += xlen;
2187 base += xlen;
2188 }
2189 }
2190 *limit = len;
2191
2192 done:
2193 kmem_free(argv, PAGE_SIZE);
2194 kmem_free(arg, PAGE_SIZE);
2195 uvmspace_free(vmspace);
2196 return error;
2197 }
2198
2199 /*
2200 * Fill in a proc structure for the specified process.
2201 */
2202 static void
2203 fill_proc(const struct proc *psrc, struct proc *p, bool allowaddr)
2204 {
2205 COND_SET_VALUE(p->p_list, psrc->p_list, allowaddr);
2206 COND_SET_VALUE(p->p_auxlock, psrc->p_auxlock, allowaddr);
2207 COND_SET_VALUE(p->p_lock, psrc->p_lock, allowaddr);
2208 COND_SET_VALUE(p->p_stmutex, psrc->p_stmutex, allowaddr);
2209 COND_SET_VALUE(p->p_reflock, psrc->p_reflock, allowaddr);
2210 COND_SET_VALUE(p->p_waitcv, psrc->p_waitcv, allowaddr);
2211 COND_SET_VALUE(p->p_lwpcv, psrc->p_lwpcv, allowaddr);
2212 COND_SET_VALUE(p->p_cred, psrc->p_cred, allowaddr);
2213 COND_SET_VALUE(p->p_fd, psrc->p_fd, allowaddr);
2214 COND_SET_VALUE(p->p_cwdi, psrc->p_cwdi, allowaddr);
2215 COND_SET_VALUE(p->p_stats, psrc->p_stats, allowaddr);
2216 COND_SET_VALUE(p->p_limit, psrc->p_limit, allowaddr);
2217 COND_SET_VALUE(p->p_vmspace, psrc->p_vmspace, allowaddr);
2218 COND_SET_VALUE(p->p_sigacts, psrc->p_sigacts, allowaddr);
2219 COND_SET_VALUE(p->p_aio, psrc->p_aio, allowaddr);
2220 p->p_mqueue_cnt = psrc->p_mqueue_cnt;
2221 COND_SET_VALUE(p->p_specdataref, psrc->p_specdataref, allowaddr);
2222 p->p_exitsig = psrc->p_exitsig;
2223 p->p_flag = psrc->p_flag;
2224 p->p_sflag = psrc->p_sflag;
2225 p->p_slflag = psrc->p_slflag;
2226 p->p_lflag = psrc->p_lflag;
2227 p->p_stflag = psrc->p_stflag;
2228 p->p_stat = psrc->p_stat;
2229 p->p_trace_enabled = psrc->p_trace_enabled;
2230 p->p_pid = psrc->p_pid;
2231 COND_SET_VALUE(p->p_pglist, psrc->p_pglist, allowaddr);
2232 COND_SET_VALUE(p->p_pptr, psrc->p_pptr, allowaddr);
2233 COND_SET_VALUE(p->p_sibling, psrc->p_sibling, allowaddr);
2234 COND_SET_VALUE(p->p_children, psrc->p_children, allowaddr);
2235 COND_SET_VALUE(p->p_lwps, psrc->p_lwps, allowaddr);
2236 COND_SET_VALUE(p->p_raslist, psrc->p_raslist, allowaddr);
2237 p->p_nlwps = psrc->p_nlwps;
2238 p->p_nzlwps = psrc->p_nzlwps;
2239 p->p_nrlwps = psrc->p_nrlwps;
2240 p->p_nlwpwait = psrc->p_nlwpwait;
2241 p->p_ndlwps = psrc->p_ndlwps;
2242 p->p_nlwpid = psrc->p_nlwpid;
2243 p->p_nstopchild = psrc->p_nstopchild;
2244 p->p_waited = psrc->p_waited;
2245 COND_SET_VALUE(p->p_zomblwp, psrc->p_zomblwp, allowaddr);
2246 COND_SET_VALUE(p->p_vforklwp, psrc->p_vforklwp, allowaddr);
2247 COND_SET_VALUE(p->p_sched_info, psrc->p_sched_info, allowaddr);
2248 p->p_estcpu = psrc->p_estcpu;
2249 p->p_estcpu_inherited = psrc->p_estcpu_inherited;
2250 p->p_forktime = psrc->p_forktime;
2251 p->p_pctcpu = psrc->p_pctcpu;
2252 COND_SET_VALUE(p->p_opptr, psrc->p_opptr, allowaddr);
2253 COND_SET_VALUE(p->p_timers, psrc->p_timers, allowaddr);
2254 p->p_rtime = psrc->p_rtime;
2255 p->p_uticks = psrc->p_uticks;
2256 p->p_sticks = psrc->p_sticks;
2257 p->p_iticks = psrc->p_iticks;
2258 p->p_xutime = psrc->p_xutime;
2259 p->p_xstime = psrc->p_xstime;
2260 p->p_traceflag = psrc->p_traceflag;
2261 COND_SET_VALUE(p->p_tracep, psrc->p_tracep, allowaddr);
2262 COND_SET_VALUE(p->p_textvp, psrc->p_textvp, allowaddr);
2263 COND_SET_VALUE(p->p_emul, psrc->p_emul, allowaddr);
2264 COND_SET_VALUE(p->p_emuldata, psrc->p_emuldata, allowaddr);
2265 COND_SET_VALUE(p->p_execsw, psrc->p_execsw, allowaddr);
2266 COND_SET_VALUE(p->p_klist, psrc->p_klist, allowaddr);
2267 COND_SET_VALUE(p->p_sigwaiters, psrc->p_sigwaiters, allowaddr);
2268 COND_SET_VALUE(p->p_sigpend, psrc->p_sigpend, allowaddr);
2269 COND_SET_VALUE(p->p_lwpctl, psrc->p_lwpctl, allowaddr);
2270 p->p_ppid = psrc->p_ppid;
2271 p->p_oppid = psrc->p_oppid;
2272 COND_SET_VALUE(p->p_path, psrc->p_path, allowaddr);
2273 COND_SET_VALUE(p->p_sigctx, psrc->p_sigctx, allowaddr);
2274 p->p_nice = psrc->p_nice;
2275 memcpy(p->p_comm, psrc->p_comm, sizeof(p->p_comm));
2276 COND_SET_VALUE(p->p_pgrp, psrc->p_pgrp, allowaddr);
2277 COND_SET_VALUE(p->p_psstrp, psrc->p_psstrp, allowaddr);
2278 p->p_pax = psrc->p_pax;
2279 p->p_xexit = psrc->p_xexit;
2280 p->p_xsig = psrc->p_xsig;
2281 p->p_acflag = psrc->p_acflag;
2282 COND_SET_VALUE(p->p_md, psrc->p_md, allowaddr);
2283 p->p_stackbase = psrc->p_stackbase;
2284 COND_SET_VALUE(p->p_dtrace, psrc->p_dtrace, allowaddr);
2285 }
2286
2287 /*
2288 * Fill in an eproc structure for the specified process.
2289 */
2290 void
2291 fill_eproc(struct proc *p, struct eproc *ep, bool zombie, bool allowaddr)
2292 {
2293 struct tty *tp;
2294 struct lwp *l;
2295
2296 KASSERT(mutex_owned(proc_lock));
2297 KASSERT(mutex_owned(p->p_lock));
2298
2299 COND_SET_VALUE(ep->e_paddr, p, allowaddr);
2300 COND_SET_VALUE(ep->e_sess, p->p_session, allowaddr);
2301 if (p->p_cred) {
2302 kauth_cred_topcred(p->p_cred, &ep->e_pcred);
2303 kauth_cred_toucred(p->p_cred, &ep->e_ucred);
2304 }
2305 if (p->p_stat != SIDL && !P_ZOMBIE(p) && !zombie) {
2306 struct vmspace *vm = p->p_vmspace;
2307
2308 ep->e_vm.vm_rssize = vm_resident_count(vm);
2309 ep->e_vm.vm_tsize = vm->vm_tsize;
2310 ep->e_vm.vm_dsize = vm->vm_dsize;
2311 ep->e_vm.vm_ssize = vm->vm_ssize;
2312 ep->e_vm.vm_map.size = vm->vm_map.size;
2313
2314 /* Pick the primary (first) LWP */
2315 l = proc_active_lwp(p);
2316 KASSERT(l != NULL);
2317 lwp_lock(l);
2318 if (l->l_wchan)
2319 strncpy(ep->e_wmesg, l->l_wmesg, WMESGLEN);
2320 lwp_unlock(l);
2321 }
2322 ep->e_ppid = p->p_ppid;
2323 if (p->p_pgrp && p->p_session) {
2324 ep->e_pgid = p->p_pgrp->pg_id;
2325 ep->e_jobc = p->p_pgrp->pg_jobc;
2326 ep->e_sid = p->p_session->s_sid;
2327 if ((p->p_lflag & PL_CONTROLT) &&
2328 (tp = p->p_session->s_ttyp)) {
2329 ep->e_tdev = tp->t_dev;
2330 ep->e_tpgid = tp->t_pgrp ? tp->t_pgrp->pg_id : NO_PGID;
2331 COND_SET_VALUE(ep->e_tsess, tp->t_session, allowaddr);
2332 } else
2333 ep->e_tdev = (uint32_t)NODEV;
2334 ep->e_flag = p->p_session->s_ttyvp ? EPROC_CTTY : 0;
2335 if (SESS_LEADER(p))
2336 ep->e_flag |= EPROC_SLEADER;
2337 strncpy(ep->e_login, p->p_session->s_login, MAXLOGNAME);
2338 }
2339 ep->e_xsize = ep->e_xrssize = 0;
2340 ep->e_xccount = ep->e_xswrss = 0;
2341 }
2342
2343 /*
2344 * Fill in a kinfo_proc2 structure for the specified process.
2345 */
2346 void
2347 fill_kproc2(struct proc *p, struct kinfo_proc2 *ki, bool zombie, bool allowaddr)
2348 {
2349 struct tty *tp;
2350 struct lwp *l, *l2;
2351 struct timeval ut, st, rt;
2352 sigset_t ss1, ss2;
2353 struct rusage ru;
2354 struct vmspace *vm;
2355
2356 KASSERT(mutex_owned(proc_lock));
2357 KASSERT(mutex_owned(p->p_lock));
2358
2359 sigemptyset(&ss1);
2360 sigemptyset(&ss2);
2361
2362 COND_SET_VALUE(ki->p_paddr, PTRTOUINT64(p), allowaddr);
2363 COND_SET_VALUE(ki->p_fd, PTRTOUINT64(p->p_fd), allowaddr);
2364 COND_SET_VALUE(ki->p_cwdi, PTRTOUINT64(p->p_cwdi), allowaddr);
2365 COND_SET_VALUE(ki->p_stats, PTRTOUINT64(p->p_stats), allowaddr);
2366 COND_SET_VALUE(ki->p_limit, PTRTOUINT64(p->p_limit), allowaddr);
2367 COND_SET_VALUE(ki->p_vmspace, PTRTOUINT64(p->p_vmspace), allowaddr);
2368 COND_SET_VALUE(ki->p_sigacts, PTRTOUINT64(p->p_sigacts), allowaddr);
2369 COND_SET_VALUE(ki->p_sess, PTRTOUINT64(p->p_session), allowaddr);
2370 ki->p_tsess = 0; /* may be changed if controlling tty below */
2371 COND_SET_VALUE(ki->p_ru, PTRTOUINT64(&p->p_stats->p_ru), allowaddr);
2372 ki->p_eflag = 0;
2373 ki->p_exitsig = p->p_exitsig;
2374 ki->p_flag = L_INMEM; /* Process never swapped out */
2375 ki->p_flag |= sysctl_map_flags(sysctl_flagmap, p->p_flag);
2376 ki->p_flag |= sysctl_map_flags(sysctl_sflagmap, p->p_sflag);
2377 ki->p_flag |= sysctl_map_flags(sysctl_slflagmap, p->p_slflag);
2378 ki->p_flag |= sysctl_map_flags(sysctl_lflagmap, p->p_lflag);
2379 ki->p_flag |= sysctl_map_flags(sysctl_stflagmap, p->p_stflag);
2380 ki->p_pid = p->p_pid;
2381 ki->p_ppid = p->p_ppid;
2382 ki->p_uid = kauth_cred_geteuid(p->p_cred);
2383 ki->p_ruid = kauth_cred_getuid(p->p_cred);
2384 ki->p_gid = kauth_cred_getegid(p->p_cred);
2385 ki->p_rgid = kauth_cred_getgid(p->p_cred);
2386 ki->p_svuid = kauth_cred_getsvuid(p->p_cred);
2387 ki->p_svgid = kauth_cred_getsvgid(p->p_cred);
2388 ki->p_ngroups = kauth_cred_ngroups(p->p_cred);
2389 kauth_cred_getgroups(p->p_cred, ki->p_groups,
2390 uimin(ki->p_ngroups, sizeof(ki->p_groups) / sizeof(ki->p_groups[0])),
2391 UIO_SYSSPACE);
2392
2393 ki->p_uticks = p->p_uticks;
2394 ki->p_sticks = p->p_sticks;
2395 ki->p_iticks = p->p_iticks;
2396 ki->p_tpgid = NO_PGID; /* may be changed if controlling tty below */
2397 COND_SET_VALUE(ki->p_tracep, PTRTOUINT64(p->p_tracep), allowaddr);
2398 ki->p_traceflag = p->p_traceflag;
2399
2400 memcpy(&ki->p_sigignore, &p->p_sigctx.ps_sigignore,sizeof(ki_sigset_t));
2401 memcpy(&ki->p_sigcatch, &p->p_sigctx.ps_sigcatch, sizeof(ki_sigset_t));
2402
2403 ki->p_cpticks = 0;
2404 ki->p_pctcpu = p->p_pctcpu;
2405 ki->p_estcpu = 0;
2406 ki->p_stat = p->p_stat; /* Will likely be overridden by LWP status */
2407 ki->p_realstat = p->p_stat;
2408 ki->p_nice = p->p_nice;
2409 ki->p_xstat = P_WAITSTATUS(p);
2410 ki->p_acflag = p->p_acflag;
2411
2412 strncpy(ki->p_comm, p->p_comm,
2413 uimin(sizeof(ki->p_comm), sizeof(p->p_comm)));
2414 strncpy(ki->p_ename, p->p_emul->e_name, sizeof(ki->p_ename));
2415
2416 ki->p_nlwps = p->p_nlwps;
2417 ki->p_realflag = ki->p_flag;
2418
2419 if (p->p_stat != SIDL && !P_ZOMBIE(p) && !zombie) {
2420 vm = p->p_vmspace;
2421 ki->p_vm_rssize = vm_resident_count(vm);
2422 ki->p_vm_tsize = vm->vm_tsize;
2423 ki->p_vm_dsize = vm->vm_dsize;
2424 ki->p_vm_ssize = vm->vm_ssize;
2425 ki->p_vm_vsize = atop(vm->vm_map.size);
2426 /*
2427 * Since the stack is initially mapped mostly with
2428 * PROT_NONE and grown as needed, adjust the "mapped size"
2429 * to skip the unused stack portion.
2430 */
2431 ki->p_vm_msize =
2432 atop(vm->vm_map.size) - vm->vm_issize + vm->vm_ssize;
2433
2434 /* Pick the primary (first) LWP */
2435 l = proc_active_lwp(p);
2436 KASSERT(l != NULL);
2437 lwp_lock(l);
2438 ki->p_nrlwps = p->p_nrlwps;
2439 ki->p_forw = 0;
2440 ki->p_back = 0;
2441 COND_SET_VALUE(ki->p_addr, PTRTOUINT64(l->l_addr), allowaddr);
2442 ki->p_stat = l->l_stat;
2443 ki->p_flag |= sysctl_map_flags(sysctl_lwpflagmap, l->l_flag);
2444 ki->p_swtime = l->l_swtime;
2445 ki->p_slptime = l->l_slptime;
2446 if (l->l_stat == LSONPROC)
2447 ki->p_schedflags = l->l_cpu->ci_schedstate.spc_flags;
2448 else
2449 ki->p_schedflags = 0;
2450 ki->p_priority = lwp_eprio(l);
2451 ki->p_usrpri = l->l_priority;
2452 if (l->l_wchan)
2453 strncpy(ki->p_wmesg, l->l_wmesg, sizeof(ki->p_wmesg));
2454 COND_SET_VALUE(ki->p_wchan, PTRTOUINT64(l->l_wchan), allowaddr);
2455 ki->p_cpuid = cpu_index(l->l_cpu);
2456 lwp_unlock(l);
2457 LIST_FOREACH(l, &p->p_lwps, l_sibling) {
2458 /* This is hardly correct, but... */
2459 sigplusset(&l->l_sigpend.sp_set, &ss1);
2460 sigplusset(&l->l_sigmask, &ss2);
2461 ki->p_cpticks += l->l_cpticks;
2462 ki->p_pctcpu += l->l_pctcpu;
2463 ki->p_estcpu += l->l_estcpu;
2464 }
2465 }
2466 sigplusset(&p->p_sigpend.sp_set, &ss1);
2467 memcpy(&ki->p_siglist, &ss1, sizeof(ki_sigset_t));
2468 memcpy(&ki->p_sigmask, &ss2, sizeof(ki_sigset_t));
2469
2470 if (p->p_session != NULL) {
2471 ki->p_sid = p->p_session->s_sid;
2472 ki->p__pgid = p->p_pgrp->pg_id;
2473 if (p->p_session->s_ttyvp)
2474 ki->p_eflag |= EPROC_CTTY;
2475 if (SESS_LEADER(p))
2476 ki->p_eflag |= EPROC_SLEADER;
2477 strncpy(ki->p_login, p->p_session->s_login,
2478 uimin(sizeof ki->p_login - 1, sizeof p->p_session->s_login));
2479 ki->p_jobc = p->p_pgrp->pg_jobc;
2480 if ((p->p_lflag & PL_CONTROLT) && (tp = p->p_session->s_ttyp)) {
2481 ki->p_tdev = tp->t_dev;
2482 ki->p_tpgid = tp->t_pgrp ? tp->t_pgrp->pg_id : NO_PGID;
2483 COND_SET_VALUE(ki->p_tsess, PTRTOUINT64(tp->t_session),
2484 allowaddr);
2485 } else {
2486 ki->p_tdev = (int32_t)NODEV;
2487 }
2488 }
2489
2490 if (!P_ZOMBIE(p) && !zombie) {
2491 ki->p_uvalid = 1;
2492 ki->p_ustart_sec = p->p_stats->p_start.tv_sec;
2493 ki->p_ustart_usec = p->p_stats->p_start.tv_usec;
2494
2495 calcru(p, &ut, &st, NULL, &rt);
2496 ki->p_rtime_sec = rt.tv_sec;
2497 ki->p_rtime_usec = rt.tv_usec;
2498 ki->p_uutime_sec = ut.tv_sec;
2499 ki->p_uutime_usec = ut.tv_usec;
2500 ki->p_ustime_sec = st.tv_sec;
2501 ki->p_ustime_usec = st.tv_usec;
2502
2503 memcpy(&ru, &p->p_stats->p_ru, sizeof(ru));
2504 ki->p_uru_nvcsw = 0;
2505 ki->p_uru_nivcsw = 0;
2506 LIST_FOREACH(l2, &p->p_lwps, l_sibling) {
2507 ki->p_uru_nvcsw += (l2->l_ncsw - l2->l_nivcsw);
2508 ki->p_uru_nivcsw += l2->l_nivcsw;
2509 ruadd(&ru, &l2->l_ru);
2510 }
2511 ki->p_uru_maxrss = ru.ru_maxrss;
2512 ki->p_uru_ixrss = ru.ru_ixrss;
2513 ki->p_uru_idrss = ru.ru_idrss;
2514 ki->p_uru_isrss = ru.ru_isrss;
2515 ki->p_uru_minflt = ru.ru_minflt;
2516 ki->p_uru_majflt = ru.ru_majflt;
2517 ki->p_uru_nswap = ru.ru_nswap;
2518 ki->p_uru_inblock = ru.ru_inblock;
2519 ki->p_uru_oublock = ru.ru_oublock;
2520 ki->p_uru_msgsnd = ru.ru_msgsnd;
2521 ki->p_uru_msgrcv = ru.ru_msgrcv;
2522 ki->p_uru_nsignals = ru.ru_nsignals;
2523
2524 timeradd(&p->p_stats->p_cru.ru_utime,
2525 &p->p_stats->p_cru.ru_stime, &ut);
2526 ki->p_uctime_sec = ut.tv_sec;
2527 ki->p_uctime_usec = ut.tv_usec;
2528 }
2529 }
2530
2531
2532 int
2533 proc_find_locked(struct lwp *l, struct proc **p, pid_t pid)
2534 {
2535 int error;
2536
2537 mutex_enter(proc_lock);
2538 if (pid == -1)
2539 *p = l->l_proc;
2540 else
2541 *p = proc_find(pid);
2542
2543 if (*p == NULL) {
2544 if (pid != -1)
2545 mutex_exit(proc_lock);
2546 return ESRCH;
2547 }
2548 if (pid != -1)
2549 mutex_enter((*p)->p_lock);
2550 mutex_exit(proc_lock);
2551
2552 error = kauth_authorize_process(l->l_cred,
2553 KAUTH_PROCESS_CANSEE, *p,
2554 KAUTH_ARG(KAUTH_REQ_PROCESS_CANSEE_ENTRY), NULL, NULL);
2555 if (error) {
2556 if (pid != -1)
2557 mutex_exit((*p)->p_lock);
2558 }
2559 return error;
2560 }
2561
2562 static int
2563 fill_pathname(struct lwp *l, pid_t pid, void *oldp, size_t *oldlenp)
2564 {
2565 int error;
2566 struct proc *p;
2567
2568 if ((error = proc_find_locked(l, &p, pid)) != 0)
2569 return error;
2570
2571 if (p->p_path == NULL) {
2572 if (pid != -1)
2573 mutex_exit(p->p_lock);
2574 return ENOENT;
2575 }
2576
2577 size_t len = strlen(p->p_path) + 1;
2578 if (oldp != NULL) {
2579 size_t copylen = uimin(len, *oldlenp);
2580 error = sysctl_copyout(l, p->p_path, oldp, copylen);
2581 if (error == 0 && *oldlenp < len)
2582 error = ENOSPC;
2583 }
2584 *oldlenp = len;
2585 if (pid != -1)
2586 mutex_exit(p->p_lock);
2587 return error;
2588 }
2589
2590 static int
2591 fill_cwd(struct lwp *l, pid_t pid, void *oldp, size_t *oldlenp)
2592 {
2593 int error;
2594 struct proc *p;
2595 char *path;
2596 char *bp, *bend;
2597 const struct cwdinfo *cwdi;
2598 struct vnode *vp;
2599 size_t len, lenused;
2600
2601 if ((error = proc_find_locked(l, &p, pid)) != 0)
2602 return error;
2603
2604 len = MAXPATHLEN * 4;
2605
2606 path = kmem_alloc(len, KM_SLEEP);
2607
2608 bp = &path[len];
2609 bend = bp;
2610 *(--bp) = '\0';
2611
2612 cwdi = cwdlock(p);
2613 vp = cwdi->cwdi_cdir;
2614 vref(vp);
2615 cwdunlock(p);
2616 error = getcwd_common(vp, NULL, &bp, path, len/2, 0, l);
2617 vrele(vp);
2618
2619 if (error)
2620 goto out;
2621
2622 lenused = bend - bp;
2623
2624 if (oldp != NULL) {
2625 size_t copylen = uimin(lenused, *oldlenp);
2626 error = sysctl_copyout(l, bp, oldp, copylen);
2627 if (error == 0 && *oldlenp < lenused)
2628 error = ENOSPC;
2629 }
2630 *oldlenp = lenused;
2631 out:
2632 if (pid != -1)
2633 mutex_exit(p->p_lock);
2634 kmem_free(path, len);
2635 return error;
2636 }
2637
2638 int
2639 proc_getauxv(struct proc *p, void **buf, size_t *len)
2640 {
2641 struct ps_strings pss;
2642 int error;
2643 void *uauxv, *kauxv;
2644 size_t size;
2645
2646 if ((error = copyin_psstrings(p, &pss)) != 0)
2647 return error;
2648 if (pss.ps_envstr == NULL)
2649 return EIO;
2650
2651 size = p->p_execsw->es_arglen;
2652 if (size == 0)
2653 return EIO;
2654
2655 size_t ptrsz = PROC_PTRSZ(p);
2656 uauxv = (void *)((char *)pss.ps_envstr + (pss.ps_nenvstr + 1) * ptrsz);
2657
2658 kauxv = kmem_alloc(size, KM_SLEEP);
2659
2660 error = copyin_proc(p, uauxv, kauxv, size);
2661 if (error) {
2662 kmem_free(kauxv, size);
2663 return error;
2664 }
2665
2666 *buf = kauxv;
2667 *len = size;
2668
2669 return 0;
2670 }
2671
2672
2673 static int
2674 sysctl_security_expose_address(SYSCTLFN_ARGS)
2675 {
2676 int expose_address, error;
2677 struct sysctlnode node;
2678
2679 node = *rnode;
2680 node.sysctl_data = &expose_address;
2681 expose_address = *(int *)rnode->sysctl_data;
2682 error = sysctl_lookup(SYSCTLFN_CALL(&node));
2683 if (error || newp == NULL)
2684 return error;
2685
2686 if (kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_KERNADDR,
2687 0, NULL, NULL, NULL))
2688 return EPERM;
2689
2690 switch (expose_address) {
2691 case 0:
2692 case 1:
2693 case 2:
2694 break;
2695 default:
2696 return EINVAL;
2697 }
2698
2699 *(int *)rnode->sysctl_data = expose_address;
2700
2701 return 0;
2702 }
2703
2704 bool
2705 get_expose_address(struct proc *p)
2706 {
2707 /* allow only if sysctl variable is set or privileged */
2708 return kauth_authorize_process(kauth_cred_get(), KAUTH_PROCESS_CANSEE,
2709 p, KAUTH_ARG(KAUTH_REQ_PROCESS_CANSEE_KPTR), NULL, NULL) == 0;
2710 }
2711