2 * Copyright (c) 2014 John Baldwin
3 * Copyright (c) 2014, 2016 The FreeBSD Foundation
5 * Portions of this software were developed by Konstantin Belousov
6 * under sponsorship from the FreeBSD Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #include <sys/param.h>
34 #include <sys/_unrhdr.h>
35 #include <sys/systm.h>
36 #include <sys/capsicum.h>
39 #include <sys/mutex.h>
42 #include <sys/procctl.h>
44 #include <sys/syscallsubr.h>
45 #include <sys/sysproto.h>
50 #include <vm/vm_map.h>
51 #include <vm/vm_extern.h>
54 protect_setchild(struct thread *td, struct proc *p, int flags)
57 PROC_LOCK_ASSERT(p, MA_OWNED);
58 if (p->p_flag & P_SYSTEM || p_cansched(td, p) != 0)
60 if (flags & PPROT_SET) {
61 p->p_flag |= P_PROTECTED;
62 if (flags & PPROT_INHERIT)
63 p->p_flag2 |= P2_INHERIT_PROTECTED;
65 p->p_flag &= ~P_PROTECTED;
66 p->p_flag2 &= ~P2_INHERIT_PROTECTED;
72 protect_setchildren(struct thread *td, struct proc *top, int flags)
79 sx_assert(&proctree_lock, SX_LOCKED);
81 ret |= protect_setchild(td, p, flags);
84 * If this process has children, descend to them next,
85 * otherwise do any siblings, and if done with this level,
86 * follow back up the tree (but not past top).
88 if (!LIST_EMPTY(&p->p_children))
89 p = LIST_FIRST(&p->p_children);
95 if (LIST_NEXT(p, p_sibling)) {
96 p = LIST_NEXT(p, p_sibling);
106 protect_set(struct thread *td, struct proc *p, void *data)
108 int error, flags, ret;
110 flags = *(int *)data;
111 switch (PPROT_OP(flags)) {
119 if ((PPROT_FLAGS(flags) & ~(PPROT_DESCEND | PPROT_INHERIT)) != 0)
122 error = priv_check(td, PRIV_VM_MADV_PROTECT);
126 if (flags & PPROT_DESCEND)
127 ret = protect_setchildren(td, p, flags);
129 ret = protect_setchild(td, p, flags);
136 reap_acquire(struct thread *td, struct proc *p, void *data __unused)
139 sx_assert(&proctree_lock, SX_XLOCKED);
140 if (p != td->td_proc)
142 if ((p->p_treeflag & P_TREE_REAPER) != 0)
144 p->p_treeflag |= P_TREE_REAPER;
146 * We do not reattach existing children and the whole tree
147 * under them to us, since p->p_reaper already seen them.
153 reap_release(struct thread *td, struct proc *p, void *data __unused)
156 sx_assert(&proctree_lock, SX_XLOCKED);
157 if (p != td->td_proc)
161 if ((p->p_treeflag & P_TREE_REAPER) == 0)
163 reaper_abandon_children(p, false);
168 reap_status(struct thread *td, struct proc *p, void *data)
170 struct proc *reap, *p2, *first_p;
171 struct procctl_reaper_status *rs;
174 sx_assert(&proctree_lock, SX_LOCKED);
175 if ((p->p_treeflag & P_TREE_REAPER) == 0) {
179 rs->rs_flags |= REAPER_STATUS_OWNED;
181 if (reap == initproc)
182 rs->rs_flags |= REAPER_STATUS_REALINIT;
183 rs->rs_reaper = reap->p_pid;
184 rs->rs_descendants = 0;
186 if (!LIST_EMPTY(&reap->p_reaplist)) {
187 first_p = LIST_FIRST(&reap->p_children);
189 first_p = LIST_FIRST(&reap->p_reaplist);
190 rs->rs_pid = first_p->p_pid;
191 LIST_FOREACH(p2, &reap->p_reaplist, p_reapsibling) {
192 if (proc_realparent(p2) == reap)
194 rs->rs_descendants++;
203 reap_getpids(struct thread *td, struct proc *p, void *data)
205 struct proc *reap, *p2;
206 struct procctl_reaper_pidinfo *pi, *pip;
207 struct procctl_reaper_pids *rp;
212 sx_assert(&proctree_lock, SX_LOCKED);
214 reap = (p->p_treeflag & P_TREE_REAPER) == 0 ? p->p_reaper : p;
217 LIST_FOREACH(p2, &reap->p_reaplist, p_reapsibling)
219 sx_unlock(&proctree_lock);
220 if (rp->rp_count < n)
222 pi = malloc(n * sizeof(*pi), M_TEMP, M_WAITOK);
223 sx_slock(&proctree_lock);
224 LIST_FOREACH(p2, &reap->p_reaplist, p_reapsibling) {
228 bzero(pip, sizeof(*pip));
229 pip->pi_pid = p2->p_pid;
230 pip->pi_subtree = p2->p_reapsubtree;
231 pip->pi_flags = REAPER_PIDINFO_VALID;
232 if (proc_realparent(p2) == reap)
233 pip->pi_flags |= REAPER_PIDINFO_CHILD;
234 if ((p2->p_treeflag & P_TREE_REAPER) != 0)
235 pip->pi_flags |= REAPER_PIDINFO_REAPER;
238 sx_sunlock(&proctree_lock);
239 error = copyout(pi, rp->rp_pids, i * sizeof(*pi));
241 sx_slock(&proctree_lock);
247 reap_kill_proc_relock(struct proc *p, int xlocked)
251 sx_xlock(&proctree_lock);
253 sx_slock(&proctree_lock);
258 reap_kill_proc_locked(struct thread *td, struct proc *p2,
259 ksiginfo_t *ksi, struct procctl_reaper_kill *rk, int *error)
261 int error1, r, xlocked;
264 PROC_LOCK_ASSERT(p2, MA_OWNED);
265 PROC_ASSERT_HELD(p2);
267 error1 = p_cansignal(td, p2, rk->rk_sig);
269 if (*error == ESRCH) {
270 rk->rk_fpid = p2->p_pid;
277 * The need_stop indicates if the target process needs to be
278 * suspended before being signalled. This is needed when we
279 * guarantee that all processes in subtree are signalled,
280 * avoiding the race with some process not yet fully linked
281 * into all structures during fork, ignored by iterator, and
282 * then escaping signalling.
284 * If need_stop is true, then reap_kill_proc() returns true if
285 * the process was successfully stopped and signalled, and
286 * false if stopping failed and the signal was not sent.
288 * The thread cannot usefully stop itself anyway, and if other
289 * thread of the current process forks while the current
290 * thread signals the whole subtree, it is an application
293 need_stop = p2 != td->td_proc &&
294 (p2->p_flag & (P_KPROC | P_SYSTEM)) == 0 &&
295 (rk->rk_flags & REAPER_KILL_CHILDREN) == 0;
298 if (P_SHOULDSTOP(p2) == P_STOPPED_SINGLE)
299 return (false); /* retry later */
300 xlocked = sx_xlocked(&proctree_lock);
301 sx_unlock(&proctree_lock);
302 r = thread_single(p2, SINGLE_ALLPROC);
304 reap_kill_proc_relock(p2, xlocked);
309 pksignal(p2, rk->rk_sig, ksi);
314 reap_kill_proc_relock(p2, xlocked);
315 thread_single_end(p2, SINGLE_ALLPROC);
321 reap_kill_proc(struct thread *td, struct proc *p2, ksiginfo_t *ksi,
322 struct procctl_reaper_kill *rk, int *error)
328 if ((p2->p_flag2 & P2_WEXIT) == 0) {
330 res = reap_kill_proc_locked(td, p2, ksi, rk, error);
337 struct reap_kill_tracker {
339 TAILQ_ENTRY(reap_kill_tracker) link;
342 TAILQ_HEAD(reap_kill_tracker_head, reap_kill_tracker);
345 reap_kill_sched(struct reap_kill_tracker_head *tracker, struct proc *p2)
347 struct reap_kill_tracker *t;
349 t = malloc(sizeof(struct reap_kill_tracker), M_TEMP, M_WAITOK);
351 TAILQ_INSERT_TAIL(tracker, t, link);
355 reap_kill_children(struct thread *td, struct proc *reaper,
356 struct procctl_reaper_kill *rk, ksiginfo_t *ksi, int *error)
360 LIST_FOREACH(p2, &reaper->p_children, p_sibling) {
361 (void)reap_kill_proc(td, p2, ksi, rk, error);
363 * Do not end the loop on error, signal everything we
370 reap_kill_subtree_once(struct thread *td, struct proc *p, struct proc *reaper,
371 struct procctl_reaper_kill *rk, ksiginfo_t *ksi, int *error,
374 struct reap_kill_tracker_head tracker;
375 struct reap_kill_tracker *t;
380 TAILQ_INIT(&tracker);
381 reap_kill_sched(&tracker, reaper);
382 while ((t = TAILQ_FIRST(&tracker)) != NULL) {
383 MPASS((t->parent->p_treeflag & P_TREE_REAPER) != 0);
384 TAILQ_REMOVE(&tracker, t, link);
385 LIST_FOREACH(p2, &t->parent->p_reaplist, p_reapsibling) {
386 if (t->parent == reaper &&
387 (rk->rk_flags & REAPER_KILL_SUBTREE) != 0 &&
388 p2->p_reapsubtree != rk->rk_subtree)
390 if ((p2->p_treeflag & P_TREE_REAPER) != 0)
391 reap_kill_sched(&tracker, p2);
392 if (alloc_unr_specific(pids, p2->p_pid) != p2->p_pid)
394 if (!reap_kill_proc(td, p2, ksi, rk, error))
395 free_unr(pids, p2->p_pid);
404 reap_kill_subtree(struct thread *td, struct proc *p, struct proc *reaper,
405 struct procctl_reaper_kill *rk, ksiginfo_t *ksi, int *error)
410 * pids records processes which were already signalled, to
411 * avoid doubling signals to them if iteration needs to be
414 init_unrhdr(&pids, 1, PID_MAX, UNR_NO_MTX);
415 PROC_LOCK(td->td_proc);
416 if ((td->td_proc->p_flag2 & P2_WEXIT) != 0) {
417 PROC_UNLOCK(td->td_proc);
420 td->td_proc->p_singlethr++;
421 PROC_UNLOCK(td->td_proc);
422 while (reap_kill_subtree_once(td, p, reaper, rk, ksi, error, &pids))
424 PROC_LOCK(td->td_proc);
425 td->td_proc->p_singlethr--;
426 if (td->td_proc->p_singlethr == 0)
427 wakeup(&p->p_singlethr);
428 PROC_UNLOCK(td->td_proc);
435 reap_kill_sapblk(struct thread *td __unused, void *data)
437 struct procctl_reaper_kill *rk;
440 return ((rk->rk_flags & REAPER_KILL_CHILDREN) == 0);
444 reap_kill(struct thread *td, struct proc *p, void *data)
448 struct procctl_reaper_kill *rk;
452 sx_assert(&proctree_lock, SX_LOCKED);
453 if (IN_CAPABILITY_MODE(td))
455 if (rk->rk_sig <= 0 || rk->rk_sig > _SIG_MAXSIG ||
456 (rk->rk_flags & ~(REAPER_KILL_CHILDREN |
457 REAPER_KILL_SUBTREE)) != 0 || (rk->rk_flags &
458 (REAPER_KILL_CHILDREN | REAPER_KILL_SUBTREE)) ==
459 (REAPER_KILL_CHILDREN | REAPER_KILL_SUBTREE))
462 reaper = (p->p_treeflag & P_TREE_REAPER) == 0 ? p->p_reaper : p;
464 ksi.ksi_signo = rk->rk_sig;
465 ksi.ksi_code = SI_USER;
466 ksi.ksi_pid = td->td_proc->p_pid;
467 ksi.ksi_uid = td->td_ucred->cr_ruid;
471 if ((rk->rk_flags & REAPER_KILL_CHILDREN) != 0) {
472 reap_kill_children(td, reaper, rk, &ksi, &error);
474 reap_kill_subtree(td, p, reaper, rk, &ksi, &error);
481 trace_ctl(struct thread *td, struct proc *p, void *data)
485 PROC_LOCK_ASSERT(p, MA_OWNED);
486 state = *(int *)data;
489 * Ktrace changes p_traceflag from or to zero under the
490 * process lock, so the test does not need to acquire ktrace
493 if ((p->p_flag & P_TRACED) != 0 || p->p_traceflag != 0)
497 case PROC_TRACE_CTL_ENABLE:
498 if (td->td_proc != p)
500 p->p_flag2 &= ~(P2_NOTRACE | P2_NOTRACE_EXEC);
502 case PROC_TRACE_CTL_DISABLE_EXEC:
503 p->p_flag2 |= P2_NOTRACE_EXEC | P2_NOTRACE;
505 case PROC_TRACE_CTL_DISABLE:
506 if ((p->p_flag2 & P2_NOTRACE_EXEC) != 0) {
507 KASSERT((p->p_flag2 & P2_NOTRACE) != 0,
508 ("dandling P2_NOTRACE_EXEC"));
509 if (td->td_proc != p)
511 p->p_flag2 &= ~P2_NOTRACE_EXEC;
513 p->p_flag2 |= P2_NOTRACE;
523 trace_status(struct thread *td, struct proc *p, void *data)
528 if ((p->p_flag2 & P2_NOTRACE) != 0) {
529 KASSERT((p->p_flag & P_TRACED) == 0,
530 ("%d traced but tracing disabled", p->p_pid));
532 } else if ((p->p_flag & P_TRACED) != 0) {
533 *status = p->p_pptr->p_pid;
541 trapcap_ctl(struct thread *td, struct proc *p, void *data)
545 PROC_LOCK_ASSERT(p, MA_OWNED);
546 state = *(int *)data;
549 case PROC_TRAPCAP_CTL_ENABLE:
550 p->p_flag2 |= P2_TRAPCAP;
552 case PROC_TRAPCAP_CTL_DISABLE:
553 p->p_flag2 &= ~P2_TRAPCAP;
562 trapcap_status(struct thread *td, struct proc *p, void *data)
567 *status = (p->p_flag2 & P2_TRAPCAP) != 0 ? PROC_TRAPCAP_CTL_ENABLE :
568 PROC_TRAPCAP_CTL_DISABLE;
573 no_new_privs_ctl(struct thread *td, struct proc *p, void *data)
577 PROC_LOCK_ASSERT(p, MA_OWNED);
578 state = *(int *)data;
580 if (state != PROC_NO_NEW_PRIVS_ENABLE)
582 p->p_flag2 |= P2_NO_NEW_PRIVS;
587 no_new_privs_status(struct thread *td, struct proc *p, void *data)
590 *(int *)data = (p->p_flag2 & P2_NO_NEW_PRIVS) != 0 ?
591 PROC_NO_NEW_PRIVS_ENABLE : PROC_NO_NEW_PRIVS_DISABLE;
596 protmax_ctl(struct thread *td, struct proc *p, void *data)
600 PROC_LOCK_ASSERT(p, MA_OWNED);
601 state = *(int *)data;
604 case PROC_PROTMAX_FORCE_ENABLE:
605 p->p_flag2 &= ~P2_PROTMAX_DISABLE;
606 p->p_flag2 |= P2_PROTMAX_ENABLE;
608 case PROC_PROTMAX_FORCE_DISABLE:
609 p->p_flag2 |= P2_PROTMAX_DISABLE;
610 p->p_flag2 &= ~P2_PROTMAX_ENABLE;
612 case PROC_PROTMAX_NOFORCE:
613 p->p_flag2 &= ~(P2_PROTMAX_ENABLE | P2_PROTMAX_DISABLE);
622 protmax_status(struct thread *td, struct proc *p, void *data)
626 switch (p->p_flag2 & (P2_PROTMAX_ENABLE | P2_PROTMAX_DISABLE)) {
628 d = PROC_PROTMAX_NOFORCE;
630 case P2_PROTMAX_ENABLE:
631 d = PROC_PROTMAX_FORCE_ENABLE;
633 case P2_PROTMAX_DISABLE:
634 d = PROC_PROTMAX_FORCE_DISABLE;
637 if (kern_mmap_maxprot(p, PROT_READ) == PROT_READ)
638 d |= PROC_PROTMAX_ACTIVE;
644 aslr_ctl(struct thread *td, struct proc *p, void *data)
648 PROC_LOCK_ASSERT(p, MA_OWNED);
649 state = *(int *)data;
652 case PROC_ASLR_FORCE_ENABLE:
653 p->p_flag2 &= ~P2_ASLR_DISABLE;
654 p->p_flag2 |= P2_ASLR_ENABLE;
656 case PROC_ASLR_FORCE_DISABLE:
657 p->p_flag2 |= P2_ASLR_DISABLE;
658 p->p_flag2 &= ~P2_ASLR_ENABLE;
660 case PROC_ASLR_NOFORCE:
661 p->p_flag2 &= ~(P2_ASLR_ENABLE | P2_ASLR_DISABLE);
670 aslr_status(struct thread *td, struct proc *p, void *data)
675 switch (p->p_flag2 & (P2_ASLR_ENABLE | P2_ASLR_DISABLE)) {
677 d = PROC_ASLR_NOFORCE;
680 d = PROC_ASLR_FORCE_ENABLE;
682 case P2_ASLR_DISABLE:
683 d = PROC_ASLR_FORCE_DISABLE;
686 if ((p->p_flag & P_WEXIT) == 0) {
689 vm = vmspace_acquire_ref(p);
691 if ((vm->vm_map.flags & MAP_ASLR) != 0)
692 d |= PROC_ASLR_ACTIVE;
703 stackgap_ctl(struct thread *td, struct proc *p, void *data)
707 PROC_LOCK_ASSERT(p, MA_OWNED);
708 state = *(int *)data;
710 if ((state & ~(PROC_STACKGAP_ENABLE | PROC_STACKGAP_DISABLE |
711 PROC_STACKGAP_ENABLE_EXEC | PROC_STACKGAP_DISABLE_EXEC)) != 0)
713 switch (state & (PROC_STACKGAP_ENABLE | PROC_STACKGAP_DISABLE)) {
714 case PROC_STACKGAP_ENABLE:
715 if ((p->p_flag2 & P2_STKGAP_DISABLE) != 0)
718 case PROC_STACKGAP_DISABLE:
719 p->p_flag2 |= P2_STKGAP_DISABLE;
726 switch (state & (PROC_STACKGAP_ENABLE_EXEC |
727 PROC_STACKGAP_DISABLE_EXEC)) {
728 case PROC_STACKGAP_ENABLE_EXEC:
729 p->p_flag2 &= ~P2_STKGAP_DISABLE_EXEC;
731 case PROC_STACKGAP_DISABLE_EXEC:
732 p->p_flag2 |= P2_STKGAP_DISABLE_EXEC;
743 stackgap_status(struct thread *td, struct proc *p, void *data)
747 PROC_LOCK_ASSERT(p, MA_OWNED);
749 d = (p->p_flag2 & P2_STKGAP_DISABLE) != 0 ? PROC_STACKGAP_DISABLE :
750 PROC_STACKGAP_ENABLE;
751 d |= (p->p_flag2 & P2_STKGAP_DISABLE_EXEC) != 0 ?
752 PROC_STACKGAP_DISABLE_EXEC : PROC_STACKGAP_ENABLE_EXEC;
758 wxmap_ctl(struct thread *td, struct proc *p, void *data)
764 PROC_LOCK_ASSERT(p, MA_OWNED);
765 if ((p->p_flag & P_WEXIT) != 0)
767 state = *(int *)data;
770 case PROC_WX_MAPPINGS_PERMIT:
771 p->p_flag2 |= P2_WXORX_DISABLE;
774 vm = vmspace_acquire_ref(p);
778 map->flags &= ~MAP_WXORX;
785 case PROC_WX_MAPPINGS_DISALLOW_EXEC:
786 p->p_flag2 |= P2_WXORX_ENABLE_EXEC;
796 wxmap_status(struct thread *td, struct proc *p, void *data)
801 PROC_LOCK_ASSERT(p, MA_OWNED);
802 if ((p->p_flag & P_WEXIT) != 0)
806 if ((p->p_flag2 & P2_WXORX_DISABLE) != 0)
807 d |= PROC_WX_MAPPINGS_PERMIT;
808 if ((p->p_flag2 & P2_WXORX_ENABLE_EXEC) != 0)
809 d |= PROC_WX_MAPPINGS_DISALLOW_EXEC;
812 vm = vmspace_acquire_ref(p);
814 if ((vm->vm_map.flags & MAP_WXORX) != 0)
815 d |= PROC_WXORX_ENFORCE;
825 pdeathsig_ctl(struct thread *td, struct proc *p, void *data)
829 signum = *(int *)data;
830 if (p != td->td_proc || (signum != 0 && !_SIG_VALID(signum)))
832 p->p_pdeathsig = signum;
837 pdeathsig_status(struct thread *td, struct proc *p, void *data)
839 if (p != td->td_proc)
841 *(int *)data = p->p_pdeathsig;
851 struct procctl_cmd_info {
854 bool esrch_is_einval : 1;
855 bool copyout_on_error : 1;
856 bool no_nonnull_data : 1;
857 bool need_candebug : 1;
860 int (*exec)(struct thread *, struct proc *, void *);
861 bool (*sapblk)(struct thread *, void *);
863 static const struct procctl_cmd_info procctl_cmds_info[] = {
865 { .lock_tree = PCTL_SLOCKED, .one_proc = false,
866 .esrch_is_einval = false, .no_nonnull_data = false,
867 .need_candebug = false,
868 .copyin_sz = sizeof(int), .copyout_sz = 0,
869 .exec = protect_set, .copyout_on_error = false, },
870 [PROC_REAP_ACQUIRE] =
871 { .lock_tree = PCTL_XLOCKED, .one_proc = true,
872 .esrch_is_einval = false, .no_nonnull_data = true,
873 .need_candebug = false,
874 .copyin_sz = 0, .copyout_sz = 0,
875 .exec = reap_acquire, .copyout_on_error = false, },
876 [PROC_REAP_RELEASE] =
877 { .lock_tree = PCTL_XLOCKED, .one_proc = true,
878 .esrch_is_einval = false, .no_nonnull_data = true,
879 .need_candebug = false,
880 .copyin_sz = 0, .copyout_sz = 0,
881 .exec = reap_release, .copyout_on_error = false, },
883 { .lock_tree = PCTL_SLOCKED, .one_proc = true,
884 .esrch_is_einval = false, .no_nonnull_data = false,
885 .need_candebug = false,
887 .copyout_sz = sizeof(struct procctl_reaper_status),
888 .exec = reap_status, .copyout_on_error = false, },
889 [PROC_REAP_GETPIDS] =
890 { .lock_tree = PCTL_SLOCKED, .one_proc = true,
891 .esrch_is_einval = false, .no_nonnull_data = false,
892 .need_candebug = false,
893 .copyin_sz = sizeof(struct procctl_reaper_pids),
895 .exec = reap_getpids, .copyout_on_error = false, },
897 { .lock_tree = PCTL_SLOCKED, .one_proc = true,
898 .esrch_is_einval = false, .no_nonnull_data = false,
899 .need_candebug = false,
900 .copyin_sz = sizeof(struct procctl_reaper_kill),
901 .copyout_sz = sizeof(struct procctl_reaper_kill),
902 .exec = reap_kill, .copyout_on_error = true,
903 .sapblk = reap_kill_sapblk, },
905 { .lock_tree = PCTL_SLOCKED, .one_proc = false,
906 .esrch_is_einval = false, .no_nonnull_data = false,
907 .need_candebug = true,
908 .copyin_sz = sizeof(int), .copyout_sz = 0,
909 .exec = trace_ctl, .copyout_on_error = false, },
910 [PROC_TRACE_STATUS] =
911 { .lock_tree = PCTL_UNLOCKED, .one_proc = true,
912 .esrch_is_einval = false, .no_nonnull_data = false,
913 .need_candebug = false,
914 .copyin_sz = 0, .copyout_sz = sizeof(int),
915 .exec = trace_status, .copyout_on_error = false, },
917 { .lock_tree = PCTL_SLOCKED, .one_proc = false,
918 .esrch_is_einval = false, .no_nonnull_data = false,
919 .need_candebug = true,
920 .copyin_sz = sizeof(int), .copyout_sz = 0,
921 .exec = trapcap_ctl, .copyout_on_error = false, },
922 [PROC_TRAPCAP_STATUS] =
923 { .lock_tree = PCTL_UNLOCKED, .one_proc = true,
924 .esrch_is_einval = false, .no_nonnull_data = false,
925 .need_candebug = false,
926 .copyin_sz = 0, .copyout_sz = sizeof(int),
927 .exec = trapcap_status, .copyout_on_error = false, },
928 [PROC_PDEATHSIG_CTL] =
929 { .lock_tree = PCTL_UNLOCKED, .one_proc = true,
930 .esrch_is_einval = true, .no_nonnull_data = false,
931 .need_candebug = false,
932 .copyin_sz = sizeof(int), .copyout_sz = 0,
933 .exec = pdeathsig_ctl, .copyout_on_error = false, },
934 [PROC_PDEATHSIG_STATUS] =
935 { .lock_tree = PCTL_UNLOCKED, .one_proc = true,
936 .esrch_is_einval = true, .no_nonnull_data = false,
937 .need_candebug = false,
938 .copyin_sz = 0, .copyout_sz = sizeof(int),
939 .exec = pdeathsig_status, .copyout_on_error = false, },
941 { .lock_tree = PCTL_UNLOCKED, .one_proc = true,
942 .esrch_is_einval = false, .no_nonnull_data = false,
943 .need_candebug = true,
944 .copyin_sz = sizeof(int), .copyout_sz = 0,
945 .exec = aslr_ctl, .copyout_on_error = false, },
947 { .lock_tree = PCTL_UNLOCKED, .one_proc = true,
948 .esrch_is_einval = false, .no_nonnull_data = false,
949 .need_candebug = false,
950 .copyin_sz = 0, .copyout_sz = sizeof(int),
951 .exec = aslr_status, .copyout_on_error = false, },
953 { .lock_tree = PCTL_UNLOCKED, .one_proc = true,
954 .esrch_is_einval = false, .no_nonnull_data = false,
955 .need_candebug = true,
956 .copyin_sz = sizeof(int), .copyout_sz = 0,
957 .exec = protmax_ctl, .copyout_on_error = false, },
958 [PROC_PROTMAX_STATUS] =
959 { .lock_tree = PCTL_UNLOCKED, .one_proc = true,
960 .esrch_is_einval = false, .no_nonnull_data = false,
961 .need_candebug = false,
962 .copyin_sz = 0, .copyout_sz = sizeof(int),
963 .exec = protmax_status, .copyout_on_error = false, },
964 [PROC_STACKGAP_CTL] =
965 { .lock_tree = PCTL_UNLOCKED, .one_proc = true,
966 .esrch_is_einval = false, .no_nonnull_data = false,
967 .need_candebug = true,
968 .copyin_sz = sizeof(int), .copyout_sz = 0,
969 .exec = stackgap_ctl, .copyout_on_error = false, },
970 [PROC_STACKGAP_STATUS] =
971 { .lock_tree = PCTL_UNLOCKED, .one_proc = true,
972 .esrch_is_einval = false, .no_nonnull_data = false,
973 .need_candebug = false,
974 .copyin_sz = 0, .copyout_sz = sizeof(int),
975 .exec = stackgap_status, .copyout_on_error = false, },
976 [PROC_NO_NEW_PRIVS_CTL] =
977 { .lock_tree = PCTL_SLOCKED, .one_proc = true,
978 .esrch_is_einval = false, .no_nonnull_data = false,
979 .need_candebug = true,
980 .copyin_sz = sizeof(int), .copyout_sz = 0,
981 .exec = no_new_privs_ctl, .copyout_on_error = false, },
982 [PROC_NO_NEW_PRIVS_STATUS] =
983 { .lock_tree = PCTL_UNLOCKED, .one_proc = true,
984 .esrch_is_einval = false, .no_nonnull_data = false,
985 .need_candebug = false,
986 .copyin_sz = 0, .copyout_sz = sizeof(int),
987 .exec = no_new_privs_status, .copyout_on_error = false, },
989 { .lock_tree = PCTL_UNLOCKED, .one_proc = true,
990 .esrch_is_einval = false, .no_nonnull_data = false,
991 .need_candebug = true,
992 .copyin_sz = sizeof(int), .copyout_sz = 0,
993 .exec = wxmap_ctl, .copyout_on_error = false, },
994 [PROC_WXMAP_STATUS] =
995 { .lock_tree = PCTL_UNLOCKED, .one_proc = true,
996 .esrch_is_einval = false, .no_nonnull_data = false,
997 .need_candebug = false,
998 .copyin_sz = 0, .copyout_sz = sizeof(int),
999 .exec = wxmap_status, .copyout_on_error = false, },
1003 sys_procctl(struct thread *td, struct procctl_args *uap)
1006 struct procctl_reaper_status rs;
1007 struct procctl_reaper_pids rp;
1008 struct procctl_reaper_kill rk;
1011 const struct procctl_cmd_info *cmd_info;
1014 if (uap->com >= PROC_PROCCTL_MD_MIN)
1015 return (cpu_procctl(td, uap->idtype, uap->id,
1016 uap->com, uap->data));
1017 if (uap->com == 0 || uap->com >= nitems(procctl_cmds_info))
1019 cmd_info = &procctl_cmds_info[uap->com];
1020 bzero(&x, sizeof(x));
1022 if (cmd_info->copyin_sz > 0) {
1023 error = copyin(uap->data, &x, cmd_info->copyin_sz);
1026 } else if (cmd_info->no_nonnull_data && uap->data != NULL) {
1030 error = kern_procctl(td, uap->idtype, uap->id, uap->com, &x);
1032 if (cmd_info->copyout_sz > 0 && (error == 0 ||
1033 cmd_info->copyout_on_error)) {
1034 error1 = copyout(&x, uap->data, cmd_info->copyout_sz);
1042 kern_procctl_single(struct thread *td, struct proc *p, int com, void *data)
1045 PROC_LOCK_ASSERT(p, MA_OWNED);
1046 return (procctl_cmds_info[com].exec(td, p, data));
1050 kern_procctl(struct thread *td, idtype_t idtype, id_t id, int com, void *data)
1054 const struct procctl_cmd_info *cmd_info;
1055 int error, first_error, ok;
1058 MPASS(com > 0 && com < nitems(procctl_cmds_info));
1059 cmd_info = &procctl_cmds_info[com];
1060 if (idtype != P_PID && cmd_info->one_proc)
1064 if (cmd_info->sapblk != NULL) {
1065 sapblk = cmd_info->sapblk(td, data);
1067 stop_all_proc_block();
1070 switch (cmd_info->lock_tree) {
1072 sx_xlock(&proctree_lock);
1075 sx_slock(&proctree_lock);
1090 error = cmd_info->esrch_is_einval ?
1094 error = cmd_info->need_candebug ? p_candebug(td, p) :
1098 error = kern_procctl_single(td, p, com, data);
1103 * Attempt to apply the operation to all members of the
1104 * group. Ignore processes in the group that can't be
1105 * seen. Ignore errors so long as at least one process is
1106 * able to complete the request successfully.
1116 LIST_FOREACH(p, &pg->pg_members, p_pglist) {
1118 if (p->p_state == PRS_NEW ||
1119 p->p_state == PRS_ZOMBIE ||
1120 (cmd_info->need_candebug ? p_candebug(td, p) :
1121 p_cansee(td, p)) != 0) {
1125 error = kern_procctl_single(td, p, com, data);
1129 else if (first_error == 0)
1130 first_error = error;
1134 else if (first_error != 0)
1135 error = first_error;
1138 * Was not able to see any processes in the
1148 switch (cmd_info->lock_tree) {
1150 sx_xunlock(&proctree_lock);
1153 sx_sunlock(&proctree_lock);
1159 stop_all_proc_unblock();