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/param.h>
31 #include <sys/_unrhdr.h>
32 #include <sys/systm.h>
33 #include <sys/capsicum.h>
36 #include <sys/mutex.h>
39 #include <sys/procctl.h>
41 #include <sys/syscallsubr.h>
42 #include <sys/sysproto.h>
43 #include <sys/taskqueue.h>
48 #include <vm/vm_map.h>
49 #include <vm/vm_extern.h>
52 protect_setchild(struct thread *td, struct proc *p, int flags)
55 PROC_LOCK_ASSERT(p, MA_OWNED);
56 if (p->p_flag & P_SYSTEM || p_cansched(td, p) != 0)
58 if (flags & PPROT_SET) {
59 p->p_flag |= P_PROTECTED;
60 if (flags & PPROT_INHERIT)
61 p->p_flag2 |= P2_INHERIT_PROTECTED;
63 p->p_flag &= ~P_PROTECTED;
64 p->p_flag2 &= ~P2_INHERIT_PROTECTED;
70 protect_setchildren(struct thread *td, struct proc *top, int flags)
77 sx_assert(&proctree_lock, SX_LOCKED);
79 ret |= protect_setchild(td, p, flags);
82 * If this process has children, descend to them next,
83 * otherwise do any siblings, and if done with this level,
84 * follow back up the tree (but not past top).
86 if (!LIST_EMPTY(&p->p_children))
87 p = LIST_FIRST(&p->p_children);
93 if (LIST_NEXT(p, p_sibling)) {
94 p = LIST_NEXT(p, p_sibling);
104 protect_set(struct thread *td, struct proc *p, void *data)
106 int error, flags, ret;
108 flags = *(int *)data;
109 switch (PPROT_OP(flags)) {
117 if ((PPROT_FLAGS(flags) & ~(PPROT_DESCEND | PPROT_INHERIT)) != 0)
120 error = priv_check(td, PRIV_VM_MADV_PROTECT);
124 if (flags & PPROT_DESCEND)
125 ret = protect_setchildren(td, p, flags);
127 ret = protect_setchild(td, p, flags);
134 reap_acquire(struct thread *td, struct proc *p, void *data __unused)
137 sx_assert(&proctree_lock, SX_XLOCKED);
138 if (p != td->td_proc)
140 if ((p->p_treeflag & P_TREE_REAPER) != 0)
142 p->p_treeflag |= P_TREE_REAPER;
144 * We do not reattach existing children and the whole tree
145 * under them to us, since p->p_reaper already seen them.
151 reap_release(struct thread *td, struct proc *p, void *data __unused)
154 sx_assert(&proctree_lock, SX_XLOCKED);
155 if (p != td->td_proc)
159 if ((p->p_treeflag & P_TREE_REAPER) == 0)
161 reaper_abandon_children(p, false);
166 reap_status(struct thread *td, struct proc *p, void *data)
168 struct proc *reap, *p2, *first_p;
169 struct procctl_reaper_status *rs;
172 sx_assert(&proctree_lock, SX_LOCKED);
173 if ((p->p_treeflag & P_TREE_REAPER) == 0) {
177 rs->rs_flags |= REAPER_STATUS_OWNED;
179 if (reap == initproc)
180 rs->rs_flags |= REAPER_STATUS_REALINIT;
181 rs->rs_reaper = reap->p_pid;
182 rs->rs_descendants = 0;
184 if (!LIST_EMPTY(&reap->p_reaplist)) {
185 first_p = LIST_FIRST(&reap->p_children);
187 first_p = LIST_FIRST(&reap->p_reaplist);
188 rs->rs_pid = first_p->p_pid;
189 LIST_FOREACH(p2, &reap->p_reaplist, p_reapsibling) {
190 if (proc_realparent(p2) == reap)
192 rs->rs_descendants++;
201 reap_getpids(struct thread *td, struct proc *p, void *data)
203 struct proc *reap, *p2;
204 struct procctl_reaper_pidinfo *pi, *pip;
205 struct procctl_reaper_pids *rp;
210 sx_assert(&proctree_lock, SX_LOCKED);
212 reap = (p->p_treeflag & P_TREE_REAPER) == 0 ? p->p_reaper : p;
215 LIST_FOREACH(p2, &reap->p_reaplist, p_reapsibling)
217 sx_unlock(&proctree_lock);
218 if (rp->rp_count < n)
220 pi = malloc(n * sizeof(*pi), M_TEMP, M_WAITOK);
221 sx_slock(&proctree_lock);
222 LIST_FOREACH(p2, &reap->p_reaplist, p_reapsibling) {
226 bzero(pip, sizeof(*pip));
227 pip->pi_pid = p2->p_pid;
228 pip->pi_subtree = p2->p_reapsubtree;
229 pip->pi_flags = REAPER_PIDINFO_VALID;
230 if (proc_realparent(p2) == reap)
231 pip->pi_flags |= REAPER_PIDINFO_CHILD;
232 if ((p2->p_treeflag & P_TREE_REAPER) != 0)
233 pip->pi_flags |= REAPER_PIDINFO_REAPER;
234 if ((p2->p_flag & P_STOPPED) != 0)
235 pip->pi_flags |= REAPER_PIDINFO_STOPPED;
236 if (p2->p_state == PRS_ZOMBIE)
237 pip->pi_flags |= REAPER_PIDINFO_ZOMBIE;
238 else if ((p2->p_flag & P_WEXIT) != 0)
239 pip->pi_flags |= REAPER_PIDINFO_EXITING;
242 sx_sunlock(&proctree_lock);
243 error = copyout(pi, rp->rp_pids, i * sizeof(*pi));
245 sx_slock(&proctree_lock);
250 struct reap_kill_proc_work {
254 struct procctl_reaper_kill *rk;
260 reap_kill_proc_locked(struct reap_kill_proc_work *w)
265 PROC_LOCK_ASSERT(w->target, MA_OWNED);
266 PROC_ASSERT_HELD(w->target);
268 error1 = cr_cansignal(w->cr, w->target, w->rk->rk_sig);
270 if (*w->error == ESRCH) {
271 w->rk->rk_fpid = w->target->p_pid;
278 * The need_stop indicates if the target process needs to be
279 * suspended before being signalled. This is needed when we
280 * guarantee that all processes in subtree are signalled,
281 * avoiding the race with some process not yet fully linked
282 * into all structures during fork, ignored by iterator, and
283 * then escaping signalling.
285 * The thread cannot usefully stop itself anyway, and if other
286 * thread of the current process forks while the current
287 * thread signals the whole subtree, it is an application
290 if ((w->target->p_flag & (P_KPROC | P_SYSTEM | P_STOPPED)) == 0)
291 need_stop = thread_single(w->target, SINGLE_ALLPROC) == 0;
295 (void)pksignal(w->target, w->rk->rk_sig, w->ksi);
300 thread_single_end(w->target, SINGLE_ALLPROC);
304 reap_kill_proc_work(void *arg, int pending __unused)
306 struct reap_kill_proc_work *w;
309 PROC_LOCK(w->target);
310 if ((w->target->p_flag2 & P2_WEXIT) == 0)
311 reap_kill_proc_locked(w);
312 PROC_UNLOCK(w->target);
314 sx_xlock(&proctree_lock);
317 sx_xunlock(&proctree_lock);
320 struct reap_kill_tracker {
322 TAILQ_ENTRY(reap_kill_tracker) link;
325 TAILQ_HEAD(reap_kill_tracker_head, reap_kill_tracker);
328 reap_kill_sched(struct reap_kill_tracker_head *tracker, struct proc *p2)
330 struct reap_kill_tracker *t;
333 if ((p2->p_flag2 & P2_WEXIT) != 0) {
339 t = malloc(sizeof(struct reap_kill_tracker), M_TEMP, M_WAITOK);
341 TAILQ_INSERT_TAIL(tracker, t, link);
345 reap_kill_sched_free(struct reap_kill_tracker *t)
352 reap_kill_children(struct thread *td, struct proc *reaper,
353 struct procctl_reaper_kill *rk, ksiginfo_t *ksi, int *error)
358 LIST_FOREACH(p2, &reaper->p_children, p_sibling) {
360 if ((p2->p_flag2 & P2_WEXIT) == 0) {
361 error1 = p_cansignal(td, p2, rk->rk_sig);
363 if (*error == ESRCH) {
364 rk->rk_fpid = p2->p_pid;
369 * Do not end the loop on error,
370 * signal everything we can.
373 (void)pksignal(p2, rk->rk_sig, ksi);
382 reap_kill_subtree_once(struct thread *td, struct proc *p, struct proc *reaper,
383 struct unrhdr *pids, struct reap_kill_proc_work *w)
385 struct reap_kill_tracker_head tracker;
386 struct reap_kill_tracker *t;
392 TAILQ_INIT(&tracker);
393 reap_kill_sched(&tracker, reaper);
394 while ((t = TAILQ_FIRST(&tracker)) != NULL) {
395 TAILQ_REMOVE(&tracker, t, link);
398 * Since reap_kill_proc() drops proctree_lock sx, it
399 * is possible that the tracked reaper is no longer.
400 * In this case the subtree is reparented to the new
401 * reaper, which should handle it.
403 if ((t->parent->p_treeflag & P_TREE_REAPER) == 0) {
404 reap_kill_sched_free(t);
409 LIST_FOREACH(p2, &t->parent->p_reaplist, p_reapsibling) {
410 if (t->parent == reaper &&
411 (w->rk->rk_flags & REAPER_KILL_SUBTREE) != 0 &&
412 p2->p_reapsubtree != w->rk->rk_subtree)
414 if ((p2->p_treeflag & P_TREE_REAPER) != 0)
415 reap_kill_sched(&tracker, p2);
418 * Handle possible pid reuse. If we recorded
419 * p2 as killed but its p_flag2 does not
420 * confirm it, that means that the process
421 * terminated and its id was reused by other
422 * process in the reaper subtree.
424 * Unlocked read of p2->p_flag2 is fine, it is
425 * our thread that set the tested flag.
427 if (alloc_unr_specific(pids, p2->p_pid) != p2->p_pid &&
428 (atomic_load_int(&p2->p_flag2) &
429 (P2_REAPKILLED | P2_WEXIT)) != 0)
432 if (p2 == td->td_proc) {
433 if ((p2->p_flag & P_HADTHREADS) != 0 &&
434 (p2->p_flag2 & P2_WEXIT) == 0) {
435 xlocked = sx_xlocked(&proctree_lock);
436 sx_unlock(&proctree_lock);
443 * sapblk ensures that only one thread
444 * in the system sets this flag.
446 p2->p_flag2 |= P2_REAPKILLED;
448 r = thread_single(p2, SINGLE_NO_EXIT);
449 (void)pksignal(p2, w->rk->rk_sig, w->ksi);
452 thread_single_end(p2, SINGLE_NO_EXIT);
456 sx_xlock(&proctree_lock);
458 sx_slock(&proctree_lock);
462 if ((p2->p_flag2 & P2_WEXIT) == 0) {
464 p2->p_flag2 |= P2_REAPKILLED;
467 taskqueue_enqueue(taskqueue_thread,
469 while (w->target != NULL) {
471 &proctree_lock, PWAIT,
481 reap_kill_sched_free(t);
487 reap_kill_subtree(struct thread *td, struct proc *p, struct proc *reaper,
488 struct reap_kill_proc_work *w)
496 * pids records processes which were already signalled, to
497 * avoid doubling signals to them if iteration needs to be
500 init_unrhdr(&pids, 1, PID_MAX, UNR_NO_MTX);
501 PROC_LOCK(td->td_proc);
502 if ((td->td_proc->p_flag2 & P2_WEXIT) != 0) {
503 PROC_UNLOCK(td->td_proc);
506 PROC_UNLOCK(td->td_proc);
507 while (reap_kill_subtree_once(td, p, reaper, &pids, w))
510 ihandle = create_iter_unr(&pids);
511 while ((pid = next_iter_unr(ihandle)) != -1) {
514 p2->p_flag2 &= ~P2_REAPKILLED;
518 free_iter_unr(ihandle);
526 reap_kill_sapblk(struct thread *td __unused, void *data)
528 struct procctl_reaper_kill *rk;
531 return ((rk->rk_flags & REAPER_KILL_CHILDREN) == 0);
535 reap_kill(struct thread *td, struct proc *p, void *data)
537 struct reap_kill_proc_work w;
540 struct procctl_reaper_kill *rk;
544 sx_assert(&proctree_lock, SX_LOCKED);
545 if (IN_CAPABILITY_MODE(td))
547 if (rk->rk_sig <= 0 || rk->rk_sig > _SIG_MAXSIG ||
548 (rk->rk_flags & ~(REAPER_KILL_CHILDREN |
549 REAPER_KILL_SUBTREE)) != 0 || (rk->rk_flags &
550 (REAPER_KILL_CHILDREN | REAPER_KILL_SUBTREE)) ==
551 (REAPER_KILL_CHILDREN | REAPER_KILL_SUBTREE))
554 reaper = (p->p_treeflag & P_TREE_REAPER) == 0 ? p->p_reaper : p;
556 ksi.ksi_signo = rk->rk_sig;
557 ksi.ksi_code = SI_USER;
558 ksi.ksi_pid = td->td_proc->p_pid;
559 ksi.ksi_uid = td->td_ucred->cr_ruid;
563 if ((rk->rk_flags & REAPER_KILL_CHILDREN) != 0) {
564 reap_kill_children(td, reaper, rk, &ksi, &error);
566 w.cr = crhold(td->td_ucred);
570 TASK_INIT(&w.t, 0, reap_kill_proc_work, &w);
573 * Prevent swapout, since w, ksi, and possibly rk, are
574 * allocated on the stack. We sleep in
575 * reap_kill_subtree_once() waiting for task to
576 * complete single-threading.
580 reap_kill_subtree(td, p, reaper, &w);
589 trace_ctl(struct thread *td, struct proc *p, void *data)
593 PROC_LOCK_ASSERT(p, MA_OWNED);
594 state = *(int *)data;
597 * Ktrace changes p_traceflag from or to zero under the
598 * process lock, so the test does not need to acquire ktrace
601 if ((p->p_flag & P_TRACED) != 0 || p->p_traceflag != 0)
605 case PROC_TRACE_CTL_ENABLE:
606 if (td->td_proc != p)
608 p->p_flag2 &= ~(P2_NOTRACE | P2_NOTRACE_EXEC);
610 case PROC_TRACE_CTL_DISABLE_EXEC:
611 p->p_flag2 |= P2_NOTRACE_EXEC | P2_NOTRACE;
613 case PROC_TRACE_CTL_DISABLE:
614 if ((p->p_flag2 & P2_NOTRACE_EXEC) != 0) {
615 KASSERT((p->p_flag2 & P2_NOTRACE) != 0,
616 ("dandling P2_NOTRACE_EXEC"));
617 if (td->td_proc != p)
619 p->p_flag2 &= ~P2_NOTRACE_EXEC;
621 p->p_flag2 |= P2_NOTRACE;
631 trace_status(struct thread *td, struct proc *p, void *data)
636 if ((p->p_flag2 & P2_NOTRACE) != 0) {
637 KASSERT((p->p_flag & P_TRACED) == 0,
638 ("%d traced but tracing disabled", p->p_pid));
640 } else if ((p->p_flag & P_TRACED) != 0) {
641 *status = p->p_pptr->p_pid;
649 trapcap_ctl(struct thread *td, struct proc *p, void *data)
653 PROC_LOCK_ASSERT(p, MA_OWNED);
654 state = *(int *)data;
657 case PROC_TRAPCAP_CTL_ENABLE:
658 p->p_flag2 |= P2_TRAPCAP;
660 case PROC_TRAPCAP_CTL_DISABLE:
661 p->p_flag2 &= ~P2_TRAPCAP;
670 trapcap_status(struct thread *td, struct proc *p, void *data)
675 *status = (p->p_flag2 & P2_TRAPCAP) != 0 ? PROC_TRAPCAP_CTL_ENABLE :
676 PROC_TRAPCAP_CTL_DISABLE;
681 no_new_privs_ctl(struct thread *td, struct proc *p, void *data)
685 PROC_LOCK_ASSERT(p, MA_OWNED);
686 state = *(int *)data;
688 if (state != PROC_NO_NEW_PRIVS_ENABLE)
690 p->p_flag2 |= P2_NO_NEW_PRIVS;
695 no_new_privs_status(struct thread *td, struct proc *p, void *data)
698 *(int *)data = (p->p_flag2 & P2_NO_NEW_PRIVS) != 0 ?
699 PROC_NO_NEW_PRIVS_ENABLE : PROC_NO_NEW_PRIVS_DISABLE;
704 protmax_ctl(struct thread *td, struct proc *p, void *data)
708 PROC_LOCK_ASSERT(p, MA_OWNED);
709 state = *(int *)data;
712 case PROC_PROTMAX_FORCE_ENABLE:
713 p->p_flag2 &= ~P2_PROTMAX_DISABLE;
714 p->p_flag2 |= P2_PROTMAX_ENABLE;
716 case PROC_PROTMAX_FORCE_DISABLE:
717 p->p_flag2 |= P2_PROTMAX_DISABLE;
718 p->p_flag2 &= ~P2_PROTMAX_ENABLE;
720 case PROC_PROTMAX_NOFORCE:
721 p->p_flag2 &= ~(P2_PROTMAX_ENABLE | P2_PROTMAX_DISABLE);
730 protmax_status(struct thread *td, struct proc *p, void *data)
734 switch (p->p_flag2 & (P2_PROTMAX_ENABLE | P2_PROTMAX_DISABLE)) {
736 d = PROC_PROTMAX_NOFORCE;
738 case P2_PROTMAX_ENABLE:
739 d = PROC_PROTMAX_FORCE_ENABLE;
741 case P2_PROTMAX_DISABLE:
742 d = PROC_PROTMAX_FORCE_DISABLE;
745 if (kern_mmap_maxprot(p, PROT_READ) == PROT_READ)
746 d |= PROC_PROTMAX_ACTIVE;
752 aslr_ctl(struct thread *td, struct proc *p, void *data)
756 PROC_LOCK_ASSERT(p, MA_OWNED);
757 state = *(int *)data;
760 case PROC_ASLR_FORCE_ENABLE:
761 p->p_flag2 &= ~P2_ASLR_DISABLE;
762 p->p_flag2 |= P2_ASLR_ENABLE;
764 case PROC_ASLR_FORCE_DISABLE:
765 p->p_flag2 |= P2_ASLR_DISABLE;
766 p->p_flag2 &= ~P2_ASLR_ENABLE;
768 case PROC_ASLR_NOFORCE:
769 p->p_flag2 &= ~(P2_ASLR_ENABLE | P2_ASLR_DISABLE);
778 aslr_status(struct thread *td, struct proc *p, void *data)
783 switch (p->p_flag2 & (P2_ASLR_ENABLE | P2_ASLR_DISABLE)) {
785 d = PROC_ASLR_NOFORCE;
788 d = PROC_ASLR_FORCE_ENABLE;
790 case P2_ASLR_DISABLE:
791 d = PROC_ASLR_FORCE_DISABLE;
794 if ((p->p_flag & P_WEXIT) == 0) {
797 vm = vmspace_acquire_ref(p);
799 if ((vm->vm_map.flags & MAP_ASLR) != 0)
800 d |= PROC_ASLR_ACTIVE;
811 stackgap_ctl(struct thread *td, struct proc *p, void *data)
815 PROC_LOCK_ASSERT(p, MA_OWNED);
816 state = *(int *)data;
818 if ((state & ~(PROC_STACKGAP_ENABLE | PROC_STACKGAP_DISABLE |
819 PROC_STACKGAP_ENABLE_EXEC | PROC_STACKGAP_DISABLE_EXEC)) != 0)
821 switch (state & (PROC_STACKGAP_ENABLE | PROC_STACKGAP_DISABLE)) {
822 case PROC_STACKGAP_ENABLE:
823 if ((p->p_flag2 & P2_STKGAP_DISABLE) != 0)
826 case PROC_STACKGAP_DISABLE:
827 p->p_flag2 |= P2_STKGAP_DISABLE;
834 switch (state & (PROC_STACKGAP_ENABLE_EXEC |
835 PROC_STACKGAP_DISABLE_EXEC)) {
836 case PROC_STACKGAP_ENABLE_EXEC:
837 p->p_flag2 &= ~P2_STKGAP_DISABLE_EXEC;
839 case PROC_STACKGAP_DISABLE_EXEC:
840 p->p_flag2 |= P2_STKGAP_DISABLE_EXEC;
851 stackgap_status(struct thread *td, struct proc *p, void *data)
855 PROC_LOCK_ASSERT(p, MA_OWNED);
857 d = (p->p_flag2 & P2_STKGAP_DISABLE) != 0 ? PROC_STACKGAP_DISABLE :
858 PROC_STACKGAP_ENABLE;
859 d |= (p->p_flag2 & P2_STKGAP_DISABLE_EXEC) != 0 ?
860 PROC_STACKGAP_DISABLE_EXEC : PROC_STACKGAP_ENABLE_EXEC;
866 wxmap_ctl(struct thread *td, struct proc *p, void *data)
872 PROC_LOCK_ASSERT(p, MA_OWNED);
873 if ((p->p_flag & P_WEXIT) != 0)
875 state = *(int *)data;
878 case PROC_WX_MAPPINGS_PERMIT:
879 p->p_flag2 |= P2_WXORX_DISABLE;
882 vm = vmspace_acquire_ref(p);
886 map->flags &= ~MAP_WXORX;
893 case PROC_WX_MAPPINGS_DISALLOW_EXEC:
894 p->p_flag2 |= P2_WXORX_ENABLE_EXEC;
904 wxmap_status(struct thread *td, struct proc *p, void *data)
909 PROC_LOCK_ASSERT(p, MA_OWNED);
910 if ((p->p_flag & P_WEXIT) != 0)
914 if ((p->p_flag2 & P2_WXORX_DISABLE) != 0)
915 d |= PROC_WX_MAPPINGS_PERMIT;
916 if ((p->p_flag2 & P2_WXORX_ENABLE_EXEC) != 0)
917 d |= PROC_WX_MAPPINGS_DISALLOW_EXEC;
920 vm = vmspace_acquire_ref(p);
922 if ((vm->vm_map.flags & MAP_WXORX) != 0)
923 d |= PROC_WXORX_ENFORCE;
933 pdeathsig_ctl(struct thread *td, struct proc *p, void *data)
937 signum = *(int *)data;
938 if (p != td->td_proc || (signum != 0 && !_SIG_VALID(signum)))
940 p->p_pdeathsig = signum;
945 pdeathsig_status(struct thread *td, struct proc *p, void *data)
947 if (p != td->td_proc)
949 *(int *)data = p->p_pdeathsig;
959 struct procctl_cmd_info {
962 bool esrch_is_einval : 1;
963 bool copyout_on_error : 1;
964 bool no_nonnull_data : 1;
965 bool need_candebug : 1;
968 int (*exec)(struct thread *, struct proc *, void *);
969 bool (*sapblk)(struct thread *, void *);
971 static const struct procctl_cmd_info procctl_cmds_info[] = {
973 { .lock_tree = PCTL_SLOCKED, .one_proc = false,
974 .esrch_is_einval = false, .no_nonnull_data = false,
975 .need_candebug = false,
976 .copyin_sz = sizeof(int), .copyout_sz = 0,
977 .exec = protect_set, .copyout_on_error = false, },
978 [PROC_REAP_ACQUIRE] =
979 { .lock_tree = PCTL_XLOCKED, .one_proc = true,
980 .esrch_is_einval = false, .no_nonnull_data = true,
981 .need_candebug = false,
982 .copyin_sz = 0, .copyout_sz = 0,
983 .exec = reap_acquire, .copyout_on_error = false, },
984 [PROC_REAP_RELEASE] =
985 { .lock_tree = PCTL_XLOCKED, .one_proc = true,
986 .esrch_is_einval = false, .no_nonnull_data = true,
987 .need_candebug = false,
988 .copyin_sz = 0, .copyout_sz = 0,
989 .exec = reap_release, .copyout_on_error = false, },
991 { .lock_tree = PCTL_SLOCKED, .one_proc = true,
992 .esrch_is_einval = false, .no_nonnull_data = false,
993 .need_candebug = false,
995 .copyout_sz = sizeof(struct procctl_reaper_status),
996 .exec = reap_status, .copyout_on_error = false, },
997 [PROC_REAP_GETPIDS] =
998 { .lock_tree = PCTL_SLOCKED, .one_proc = true,
999 .esrch_is_einval = false, .no_nonnull_data = false,
1000 .need_candebug = false,
1001 .copyin_sz = sizeof(struct procctl_reaper_pids),
1003 .exec = reap_getpids, .copyout_on_error = false, },
1005 { .lock_tree = PCTL_SLOCKED, .one_proc = true,
1006 .esrch_is_einval = false, .no_nonnull_data = false,
1007 .need_candebug = false,
1008 .copyin_sz = sizeof(struct procctl_reaper_kill),
1009 .copyout_sz = sizeof(struct procctl_reaper_kill),
1010 .exec = reap_kill, .copyout_on_error = true,
1011 .sapblk = reap_kill_sapblk, },
1013 { .lock_tree = PCTL_SLOCKED, .one_proc = false,
1014 .esrch_is_einval = false, .no_nonnull_data = false,
1015 .need_candebug = true,
1016 .copyin_sz = sizeof(int), .copyout_sz = 0,
1017 .exec = trace_ctl, .copyout_on_error = false, },
1018 [PROC_TRACE_STATUS] =
1019 { .lock_tree = PCTL_UNLOCKED, .one_proc = true,
1020 .esrch_is_einval = false, .no_nonnull_data = false,
1021 .need_candebug = false,
1022 .copyin_sz = 0, .copyout_sz = sizeof(int),
1023 .exec = trace_status, .copyout_on_error = false, },
1024 [PROC_TRAPCAP_CTL] =
1025 { .lock_tree = PCTL_SLOCKED, .one_proc = false,
1026 .esrch_is_einval = false, .no_nonnull_data = false,
1027 .need_candebug = true,
1028 .copyin_sz = sizeof(int), .copyout_sz = 0,
1029 .exec = trapcap_ctl, .copyout_on_error = false, },
1030 [PROC_TRAPCAP_STATUS] =
1031 { .lock_tree = PCTL_UNLOCKED, .one_proc = true,
1032 .esrch_is_einval = false, .no_nonnull_data = false,
1033 .need_candebug = false,
1034 .copyin_sz = 0, .copyout_sz = sizeof(int),
1035 .exec = trapcap_status, .copyout_on_error = false, },
1036 [PROC_PDEATHSIG_CTL] =
1037 { .lock_tree = PCTL_UNLOCKED, .one_proc = true,
1038 .esrch_is_einval = true, .no_nonnull_data = false,
1039 .need_candebug = false,
1040 .copyin_sz = sizeof(int), .copyout_sz = 0,
1041 .exec = pdeathsig_ctl, .copyout_on_error = false, },
1042 [PROC_PDEATHSIG_STATUS] =
1043 { .lock_tree = PCTL_UNLOCKED, .one_proc = true,
1044 .esrch_is_einval = true, .no_nonnull_data = false,
1045 .need_candebug = false,
1046 .copyin_sz = 0, .copyout_sz = sizeof(int),
1047 .exec = pdeathsig_status, .copyout_on_error = false, },
1049 { .lock_tree = PCTL_UNLOCKED, .one_proc = true,
1050 .esrch_is_einval = false, .no_nonnull_data = false,
1051 .need_candebug = true,
1052 .copyin_sz = sizeof(int), .copyout_sz = 0,
1053 .exec = aslr_ctl, .copyout_on_error = false, },
1054 [PROC_ASLR_STATUS] =
1055 { .lock_tree = PCTL_UNLOCKED, .one_proc = true,
1056 .esrch_is_einval = false, .no_nonnull_data = false,
1057 .need_candebug = false,
1058 .copyin_sz = 0, .copyout_sz = sizeof(int),
1059 .exec = aslr_status, .copyout_on_error = false, },
1060 [PROC_PROTMAX_CTL] =
1061 { .lock_tree = PCTL_UNLOCKED, .one_proc = true,
1062 .esrch_is_einval = false, .no_nonnull_data = false,
1063 .need_candebug = true,
1064 .copyin_sz = sizeof(int), .copyout_sz = 0,
1065 .exec = protmax_ctl, .copyout_on_error = false, },
1066 [PROC_PROTMAX_STATUS] =
1067 { .lock_tree = PCTL_UNLOCKED, .one_proc = true,
1068 .esrch_is_einval = false, .no_nonnull_data = false,
1069 .need_candebug = false,
1070 .copyin_sz = 0, .copyout_sz = sizeof(int),
1071 .exec = protmax_status, .copyout_on_error = false, },
1072 [PROC_STACKGAP_CTL] =
1073 { .lock_tree = PCTL_UNLOCKED, .one_proc = true,
1074 .esrch_is_einval = false, .no_nonnull_data = false,
1075 .need_candebug = true,
1076 .copyin_sz = sizeof(int), .copyout_sz = 0,
1077 .exec = stackgap_ctl, .copyout_on_error = false, },
1078 [PROC_STACKGAP_STATUS] =
1079 { .lock_tree = PCTL_UNLOCKED, .one_proc = true,
1080 .esrch_is_einval = false, .no_nonnull_data = false,
1081 .need_candebug = false,
1082 .copyin_sz = 0, .copyout_sz = sizeof(int),
1083 .exec = stackgap_status, .copyout_on_error = false, },
1084 [PROC_NO_NEW_PRIVS_CTL] =
1085 { .lock_tree = PCTL_SLOCKED, .one_proc = true,
1086 .esrch_is_einval = false, .no_nonnull_data = false,
1087 .need_candebug = true,
1088 .copyin_sz = sizeof(int), .copyout_sz = 0,
1089 .exec = no_new_privs_ctl, .copyout_on_error = false, },
1090 [PROC_NO_NEW_PRIVS_STATUS] =
1091 { .lock_tree = PCTL_UNLOCKED, .one_proc = true,
1092 .esrch_is_einval = false, .no_nonnull_data = false,
1093 .need_candebug = false,
1094 .copyin_sz = 0, .copyout_sz = sizeof(int),
1095 .exec = no_new_privs_status, .copyout_on_error = false, },
1097 { .lock_tree = PCTL_UNLOCKED, .one_proc = true,
1098 .esrch_is_einval = false, .no_nonnull_data = false,
1099 .need_candebug = true,
1100 .copyin_sz = sizeof(int), .copyout_sz = 0,
1101 .exec = wxmap_ctl, .copyout_on_error = false, },
1102 [PROC_WXMAP_STATUS] =
1103 { .lock_tree = PCTL_UNLOCKED, .one_proc = true,
1104 .esrch_is_einval = false, .no_nonnull_data = false,
1105 .need_candebug = false,
1106 .copyin_sz = 0, .copyout_sz = sizeof(int),
1107 .exec = wxmap_status, .copyout_on_error = false, },
1111 sys_procctl(struct thread *td, struct procctl_args *uap)
1114 struct procctl_reaper_status rs;
1115 struct procctl_reaper_pids rp;
1116 struct procctl_reaper_kill rk;
1119 const struct procctl_cmd_info *cmd_info;
1122 if (uap->com >= PROC_PROCCTL_MD_MIN)
1123 return (cpu_procctl(td, uap->idtype, uap->id,
1124 uap->com, uap->data));
1125 if (uap->com == 0 || uap->com >= nitems(procctl_cmds_info))
1127 cmd_info = &procctl_cmds_info[uap->com];
1128 bzero(&x, sizeof(x));
1130 if (cmd_info->copyin_sz > 0) {
1131 error = copyin(uap->data, &x, cmd_info->copyin_sz);
1134 } else if (cmd_info->no_nonnull_data && uap->data != NULL) {
1138 error = kern_procctl(td, uap->idtype, uap->id, uap->com, &x);
1140 if (cmd_info->copyout_sz > 0 && (error == 0 ||
1141 cmd_info->copyout_on_error)) {
1142 error1 = copyout(&x, uap->data, cmd_info->copyout_sz);
1150 kern_procctl_single(struct thread *td, struct proc *p, int com, void *data)
1153 PROC_LOCK_ASSERT(p, MA_OWNED);
1154 return (procctl_cmds_info[com].exec(td, p, data));
1158 kern_procctl(struct thread *td, idtype_t idtype, id_t id, int com, void *data)
1162 const struct procctl_cmd_info *cmd_info;
1163 int error, first_error, ok;
1166 MPASS(com > 0 && com < nitems(procctl_cmds_info));
1167 cmd_info = &procctl_cmds_info[com];
1168 if (idtype != P_PID && cmd_info->one_proc)
1172 if (cmd_info->sapblk != NULL) {
1173 sapblk = cmd_info->sapblk(td, data);
1174 if (sapblk && !stop_all_proc_block())
1178 switch (cmd_info->lock_tree) {
1180 sx_xlock(&proctree_lock);
1183 sx_slock(&proctree_lock);
1198 error = cmd_info->esrch_is_einval ?
1202 error = cmd_info->need_candebug ? p_candebug(td, p) :
1206 error = kern_procctl_single(td, p, com, data);
1211 * Attempt to apply the operation to all members of the
1212 * group. Ignore processes in the group that can't be
1213 * seen. Ignore errors so long as at least one process is
1214 * able to complete the request successfully.
1224 LIST_FOREACH(p, &pg->pg_members, p_pglist) {
1226 if (p->p_state == PRS_NEW ||
1227 p->p_state == PRS_ZOMBIE ||
1228 (cmd_info->need_candebug ? p_candebug(td, p) :
1229 p_cansee(td, p)) != 0) {
1233 error = kern_procctl_single(td, p, com, data);
1237 else if (first_error == 0)
1238 first_error = error;
1242 else if (first_error != 0)
1243 error = first_error;
1246 * Was not able to see any processes in the
1256 switch (cmd_info->lock_tree) {
1258 sx_xunlock(&proctree_lock);
1261 sx_sunlock(&proctree_lock);
1267 stop_all_proc_unblock();