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