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