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