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>
46 #include <sys/taskqueue.h>
51 #include <vm/vm_map.h>
52 #include <vm/vm_extern.h>
55 protect_setchild(struct thread *td, struct proc *p, int flags)
58 PROC_LOCK_ASSERT(p, MA_OWNED);
59 if (p->p_flag & P_SYSTEM || p_cansched(td, p) != 0)
61 if (flags & PPROT_SET) {
62 p->p_flag |= P_PROTECTED;
63 if (flags & PPROT_INHERIT)
64 p->p_flag2 |= P2_INHERIT_PROTECTED;
66 p->p_flag &= ~P_PROTECTED;
67 p->p_flag2 &= ~P2_INHERIT_PROTECTED;
73 protect_setchildren(struct thread *td, struct proc *top, int flags)
80 sx_assert(&proctree_lock, SX_LOCKED);
82 ret |= protect_setchild(td, p, flags);
85 * If this process has children, descend to them next,
86 * otherwise do any siblings, and if done with this level,
87 * follow back up the tree (but not past top).
89 if (!LIST_EMPTY(&p->p_children))
90 p = LIST_FIRST(&p->p_children);
96 if (LIST_NEXT(p, p_sibling)) {
97 p = LIST_NEXT(p, p_sibling);
107 protect_set(struct thread *td, struct proc *p, void *data)
109 int error, flags, ret;
111 flags = *(int *)data;
112 switch (PPROT_OP(flags)) {
120 if ((PPROT_FLAGS(flags) & ~(PPROT_DESCEND | PPROT_INHERIT)) != 0)
123 error = priv_check(td, PRIV_VM_MADV_PROTECT);
127 if (flags & PPROT_DESCEND)
128 ret = protect_setchildren(td, p, flags);
130 ret = protect_setchild(td, p, flags);
137 reap_acquire(struct thread *td, struct proc *p, void *data __unused)
140 sx_assert(&proctree_lock, SX_XLOCKED);
141 if (p != td->td_proc)
143 if ((p->p_treeflag & P_TREE_REAPER) != 0)
145 p->p_treeflag |= P_TREE_REAPER;
147 * We do not reattach existing children and the whole tree
148 * under them to us, since p->p_reaper already seen them.
154 reap_release(struct thread *td, struct proc *p, void *data __unused)
157 sx_assert(&proctree_lock, SX_XLOCKED);
158 if (p != td->td_proc)
162 if ((p->p_treeflag & P_TREE_REAPER) == 0)
164 reaper_abandon_children(p, false);
169 reap_status(struct thread *td, struct proc *p, void *data)
171 struct proc *reap, *p2, *first_p;
172 struct procctl_reaper_status *rs;
175 sx_assert(&proctree_lock, SX_LOCKED);
176 if ((p->p_treeflag & P_TREE_REAPER) == 0) {
180 rs->rs_flags |= REAPER_STATUS_OWNED;
182 if (reap == initproc)
183 rs->rs_flags |= REAPER_STATUS_REALINIT;
184 rs->rs_reaper = reap->p_pid;
185 rs->rs_descendants = 0;
187 if (!LIST_EMPTY(&reap->p_reaplist)) {
188 first_p = LIST_FIRST(&reap->p_children);
190 first_p = LIST_FIRST(&reap->p_reaplist);
191 rs->rs_pid = first_p->p_pid;
192 LIST_FOREACH(p2, &reap->p_reaplist, p_reapsibling) {
193 if (proc_realparent(p2) == reap)
195 rs->rs_descendants++;
204 reap_getpids(struct thread *td, struct proc *p, void *data)
206 struct proc *reap, *p2;
207 struct procctl_reaper_pidinfo *pi, *pip;
208 struct procctl_reaper_pids *rp;
213 sx_assert(&proctree_lock, SX_LOCKED);
215 reap = (p->p_treeflag & P_TREE_REAPER) == 0 ? p->p_reaper : p;
218 LIST_FOREACH(p2, &reap->p_reaplist, p_reapsibling)
220 sx_unlock(&proctree_lock);
221 if (rp->rp_count < n)
223 pi = malloc(n * sizeof(*pi), M_TEMP, M_WAITOK);
224 sx_slock(&proctree_lock);
225 LIST_FOREACH(p2, &reap->p_reaplist, p_reapsibling) {
229 bzero(pip, sizeof(*pip));
230 pip->pi_pid = p2->p_pid;
231 pip->pi_subtree = p2->p_reapsubtree;
232 pip->pi_flags = REAPER_PIDINFO_VALID;
233 if (proc_realparent(p2) == reap)
234 pip->pi_flags |= REAPER_PIDINFO_CHILD;
235 if ((p2->p_treeflag & P_TREE_REAPER) != 0)
236 pip->pi_flags |= REAPER_PIDINFO_REAPER;
237 if ((p2->p_flag & P_STOPPED) != 0)
238 pip->pi_flags |= REAPER_PIDINFO_STOPPED;
239 if (p2->p_state == PRS_ZOMBIE)
240 pip->pi_flags |= REAPER_PIDINFO_ZOMBIE;
241 else if ((p2->p_flag & P_WEXIT) != 0)
242 pip->pi_flags |= REAPER_PIDINFO_EXITING;
245 sx_sunlock(&proctree_lock);
246 error = copyout(pi, rp->rp_pids, i * sizeof(*pi));
248 sx_slock(&proctree_lock);
253 struct reap_kill_proc_work {
257 struct procctl_reaper_kill *rk;
263 reap_kill_proc_locked(struct reap_kill_proc_work *w)
268 PROC_LOCK_ASSERT(w->target, MA_OWNED);
269 PROC_ASSERT_HELD(w->target);
271 error1 = cr_cansignal(w->cr, w->target, w->rk->rk_sig);
273 if (*w->error == ESRCH) {
274 w->rk->rk_fpid = w->target->p_pid;
281 * The need_stop indicates if the target process needs to be
282 * suspended before being signalled. This is needed when we
283 * guarantee that all processes in subtree are signalled,
284 * avoiding the race with some process not yet fully linked
285 * into all structures during fork, ignored by iterator, and
286 * then escaping signalling.
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 if ((w->target->p_flag & (P_KPROC | P_SYSTEM | P_STOPPED)) == 0)
294 need_stop = thread_single(w->target, SINGLE_ALLPROC) == 0;
298 (void)pksignal(w->target, w->rk->rk_sig, w->ksi);
303 thread_single_end(w->target, SINGLE_ALLPROC);
307 reap_kill_proc_work(void *arg, int pending __unused)
309 struct reap_kill_proc_work *w;
312 PROC_LOCK(w->target);
313 if ((w->target->p_flag2 & P2_WEXIT) == 0)
314 reap_kill_proc_locked(w);
315 PROC_UNLOCK(w->target);
317 sx_xlock(&proctree_lock);
320 sx_xunlock(&proctree_lock);
323 struct reap_kill_tracker {
325 TAILQ_ENTRY(reap_kill_tracker) link;
328 TAILQ_HEAD(reap_kill_tracker_head, reap_kill_tracker);
331 reap_kill_sched(struct reap_kill_tracker_head *tracker, struct proc *p2)
333 struct reap_kill_tracker *t;
336 if ((p2->p_flag2 & P2_WEXIT) != 0) {
342 t = malloc(sizeof(struct reap_kill_tracker), M_TEMP, M_WAITOK);
344 TAILQ_INSERT_TAIL(tracker, t, link);
348 reap_kill_sched_free(struct reap_kill_tracker *t)
355 reap_kill_children(struct thread *td, struct proc *reaper,
356 struct procctl_reaper_kill *rk, ksiginfo_t *ksi, int *error)
361 LIST_FOREACH(p2, &reaper->p_children, p_sibling) {
363 if ((p2->p_flag2 & P2_WEXIT) == 0) {
364 error1 = p_cansignal(td, p2, rk->rk_sig);
366 if (*error == ESRCH) {
367 rk->rk_fpid = p2->p_pid;
372 * Do not end the loop on error,
373 * signal everything we can.
376 (void)pksignal(p2, rk->rk_sig, ksi);
385 reap_kill_subtree_once(struct thread *td, struct proc *p, struct proc *reaper,
386 struct unrhdr *pids, struct reap_kill_proc_work *w)
388 struct reap_kill_tracker_head tracker;
389 struct reap_kill_tracker *t;
395 TAILQ_INIT(&tracker);
396 reap_kill_sched(&tracker, reaper);
397 while ((t = TAILQ_FIRST(&tracker)) != NULL) {
398 TAILQ_REMOVE(&tracker, t, link);
401 * Since reap_kill_proc() drops proctree_lock sx, it
402 * is possible that the tracked reaper is no longer.
403 * In this case the subtree is reparented to the new
404 * reaper, which should handle it.
406 if ((t->parent->p_treeflag & P_TREE_REAPER) == 0) {
407 reap_kill_sched_free(t);
412 LIST_FOREACH(p2, &t->parent->p_reaplist, p_reapsibling) {
413 if (t->parent == reaper &&
414 (w->rk->rk_flags & REAPER_KILL_SUBTREE) != 0 &&
415 p2->p_reapsubtree != w->rk->rk_subtree)
417 if ((p2->p_treeflag & P_TREE_REAPER) != 0)
418 reap_kill_sched(&tracker, p2);
421 * Handle possible pid reuse. If we recorded
422 * p2 as killed but its p_flag2 does not
423 * confirm it, that means that the process
424 * terminated and its id was reused by other
425 * process in the reaper subtree.
427 * Unlocked read of p2->p_flag2 is fine, it is
428 * our thread that set the tested flag.
430 if (alloc_unr_specific(pids, p2->p_pid) != p2->p_pid &&
431 (atomic_load_int(&p2->p_flag2) &
432 (P2_REAPKILLED | P2_WEXIT)) != 0)
435 if (p2 == td->td_proc) {
436 if ((p2->p_flag & P_HADTHREADS) != 0 &&
437 (p2->p_flag2 & P2_WEXIT) == 0) {
438 xlocked = sx_xlocked(&proctree_lock);
439 sx_unlock(&proctree_lock);
446 * sapblk ensures that only one thread
447 * in the system sets this flag.
449 p2->p_flag2 |= P2_REAPKILLED;
451 r = thread_single(p2, SINGLE_NO_EXIT);
452 (void)pksignal(p2, w->rk->rk_sig, w->ksi);
455 thread_single_end(p2, SINGLE_NO_EXIT);
459 sx_xlock(&proctree_lock);
461 sx_slock(&proctree_lock);
465 if ((p2->p_flag2 & P2_WEXIT) == 0) {
467 p2->p_flag2 |= P2_REAPKILLED;
470 taskqueue_enqueue(taskqueue_thread,
472 while (w->target != NULL) {
474 &proctree_lock, PWAIT,
484 reap_kill_sched_free(t);
490 reap_kill_subtree(struct thread *td, struct proc *p, struct proc *reaper,
491 struct reap_kill_proc_work *w)
499 * pids records processes which were already signalled, to
500 * avoid doubling signals to them if iteration needs to be
503 init_unrhdr(&pids, 1, PID_MAX, UNR_NO_MTX);
504 PROC_LOCK(td->td_proc);
505 if ((td->td_proc->p_flag2 & P2_WEXIT) != 0) {
506 PROC_UNLOCK(td->td_proc);
509 PROC_UNLOCK(td->td_proc);
510 while (reap_kill_subtree_once(td, p, reaper, &pids, w))
513 ihandle = create_iter_unr(&pids);
514 while ((pid = next_iter_unr(ihandle)) != -1) {
517 p2->p_flag2 &= ~P2_REAPKILLED;
521 free_iter_unr(ihandle);
529 reap_kill_sapblk(struct thread *td __unused, void *data)
531 struct procctl_reaper_kill *rk;
534 return ((rk->rk_flags & REAPER_KILL_CHILDREN) == 0);
538 reap_kill(struct thread *td, struct proc *p, void *data)
540 struct reap_kill_proc_work w;
543 struct procctl_reaper_kill *rk;
547 sx_assert(&proctree_lock, SX_LOCKED);
548 if (IN_CAPABILITY_MODE(td))
550 if (rk->rk_sig <= 0 || rk->rk_sig > _SIG_MAXSIG ||
551 (rk->rk_flags & ~(REAPER_KILL_CHILDREN |
552 REAPER_KILL_SUBTREE)) != 0 || (rk->rk_flags &
553 (REAPER_KILL_CHILDREN | REAPER_KILL_SUBTREE)) ==
554 (REAPER_KILL_CHILDREN | REAPER_KILL_SUBTREE))
557 reaper = (p->p_treeflag & P_TREE_REAPER) == 0 ? p->p_reaper : p;
559 ksi.ksi_signo = rk->rk_sig;
560 ksi.ksi_code = SI_USER;
561 ksi.ksi_pid = td->td_proc->p_pid;
562 ksi.ksi_uid = td->td_ucred->cr_ruid;
566 if ((rk->rk_flags & REAPER_KILL_CHILDREN) != 0) {
567 reap_kill_children(td, reaper, rk, &ksi, &error);
569 w.cr = crhold(td->td_ucred);
573 TASK_INIT(&w.t, 0, reap_kill_proc_work, &w);
576 * Prevent swapout, since w, ksi, and possibly rk, are
577 * allocated on the stack. We sleep in
578 * reap_kill_subtree_once() waiting for task to
579 * complete single-threading.
583 reap_kill_subtree(td, p, reaper, &w);
592 trace_ctl(struct thread *td, struct proc *p, void *data)
596 PROC_LOCK_ASSERT(p, MA_OWNED);
597 state = *(int *)data;
600 * Ktrace changes p_traceflag from or to zero under the
601 * process lock, so the test does not need to acquire ktrace
604 if ((p->p_flag & P_TRACED) != 0 || p->p_traceflag != 0)
608 case PROC_TRACE_CTL_ENABLE:
609 if (td->td_proc != p)
611 p->p_flag2 &= ~(P2_NOTRACE | P2_NOTRACE_EXEC);
613 case PROC_TRACE_CTL_DISABLE_EXEC:
614 p->p_flag2 |= P2_NOTRACE_EXEC | P2_NOTRACE;
616 case PROC_TRACE_CTL_DISABLE:
617 if ((p->p_flag2 & P2_NOTRACE_EXEC) != 0) {
618 KASSERT((p->p_flag2 & P2_NOTRACE) != 0,
619 ("dandling P2_NOTRACE_EXEC"));
620 if (td->td_proc != p)
622 p->p_flag2 &= ~P2_NOTRACE_EXEC;
624 p->p_flag2 |= P2_NOTRACE;
634 trace_status(struct thread *td, struct proc *p, void *data)
639 if ((p->p_flag2 & P2_NOTRACE) != 0) {
640 KASSERT((p->p_flag & P_TRACED) == 0,
641 ("%d traced but tracing disabled", p->p_pid));
643 } else if ((p->p_flag & P_TRACED) != 0) {
644 *status = p->p_pptr->p_pid;
652 trapcap_ctl(struct thread *td, struct proc *p, void *data)
656 PROC_LOCK_ASSERT(p, MA_OWNED);
657 state = *(int *)data;
660 case PROC_TRAPCAP_CTL_ENABLE:
661 p->p_flag2 |= P2_TRAPCAP;
663 case PROC_TRAPCAP_CTL_DISABLE:
664 p->p_flag2 &= ~P2_TRAPCAP;
673 trapcap_status(struct thread *td, struct proc *p, void *data)
678 *status = (p->p_flag2 & P2_TRAPCAP) != 0 ? PROC_TRAPCAP_CTL_ENABLE :
679 PROC_TRAPCAP_CTL_DISABLE;
684 no_new_privs_ctl(struct thread *td, struct proc *p, void *data)
688 PROC_LOCK_ASSERT(p, MA_OWNED);
689 state = *(int *)data;
691 if (state != PROC_NO_NEW_PRIVS_ENABLE)
693 p->p_flag2 |= P2_NO_NEW_PRIVS;
698 no_new_privs_status(struct thread *td, struct proc *p, void *data)
701 *(int *)data = (p->p_flag2 & P2_NO_NEW_PRIVS) != 0 ?
702 PROC_NO_NEW_PRIVS_ENABLE : PROC_NO_NEW_PRIVS_DISABLE;
707 protmax_ctl(struct thread *td, struct proc *p, void *data)
711 PROC_LOCK_ASSERT(p, MA_OWNED);
712 state = *(int *)data;
715 case PROC_PROTMAX_FORCE_ENABLE:
716 p->p_flag2 &= ~P2_PROTMAX_DISABLE;
717 p->p_flag2 |= P2_PROTMAX_ENABLE;
719 case PROC_PROTMAX_FORCE_DISABLE:
720 p->p_flag2 |= P2_PROTMAX_DISABLE;
721 p->p_flag2 &= ~P2_PROTMAX_ENABLE;
723 case PROC_PROTMAX_NOFORCE:
724 p->p_flag2 &= ~(P2_PROTMAX_ENABLE | P2_PROTMAX_DISABLE);
733 protmax_status(struct thread *td, struct proc *p, void *data)
737 switch (p->p_flag2 & (P2_PROTMAX_ENABLE | P2_PROTMAX_DISABLE)) {
739 d = PROC_PROTMAX_NOFORCE;
741 case P2_PROTMAX_ENABLE:
742 d = PROC_PROTMAX_FORCE_ENABLE;
744 case P2_PROTMAX_DISABLE:
745 d = PROC_PROTMAX_FORCE_DISABLE;
748 if (kern_mmap_maxprot(p, PROT_READ) == PROT_READ)
749 d |= PROC_PROTMAX_ACTIVE;
755 aslr_ctl(struct thread *td, struct proc *p, void *data)
759 PROC_LOCK_ASSERT(p, MA_OWNED);
760 state = *(int *)data;
763 case PROC_ASLR_FORCE_ENABLE:
764 p->p_flag2 &= ~P2_ASLR_DISABLE;
765 p->p_flag2 |= P2_ASLR_ENABLE;
767 case PROC_ASLR_FORCE_DISABLE:
768 p->p_flag2 |= P2_ASLR_DISABLE;
769 p->p_flag2 &= ~P2_ASLR_ENABLE;
771 case PROC_ASLR_NOFORCE:
772 p->p_flag2 &= ~(P2_ASLR_ENABLE | P2_ASLR_DISABLE);
781 aslr_status(struct thread *td, struct proc *p, void *data)
786 switch (p->p_flag2 & (P2_ASLR_ENABLE | P2_ASLR_DISABLE)) {
788 d = PROC_ASLR_NOFORCE;
791 d = PROC_ASLR_FORCE_ENABLE;
793 case P2_ASLR_DISABLE:
794 d = PROC_ASLR_FORCE_DISABLE;
797 if ((p->p_flag & P_WEXIT) == 0) {
800 vm = vmspace_acquire_ref(p);
802 if ((vm->vm_map.flags & MAP_ASLR) != 0)
803 d |= PROC_ASLR_ACTIVE;
814 stackgap_ctl(struct thread *td, struct proc *p, void *data)
818 PROC_LOCK_ASSERT(p, MA_OWNED);
819 state = *(int *)data;
821 if ((state & ~(PROC_STACKGAP_ENABLE | PROC_STACKGAP_DISABLE |
822 PROC_STACKGAP_ENABLE_EXEC | PROC_STACKGAP_DISABLE_EXEC)) != 0)
824 switch (state & (PROC_STACKGAP_ENABLE | PROC_STACKGAP_DISABLE)) {
825 case PROC_STACKGAP_ENABLE:
826 if ((p->p_flag2 & P2_STKGAP_DISABLE) != 0)
829 case PROC_STACKGAP_DISABLE:
830 p->p_flag2 |= P2_STKGAP_DISABLE;
837 switch (state & (PROC_STACKGAP_ENABLE_EXEC |
838 PROC_STACKGAP_DISABLE_EXEC)) {
839 case PROC_STACKGAP_ENABLE_EXEC:
840 p->p_flag2 &= ~P2_STKGAP_DISABLE_EXEC;
842 case PROC_STACKGAP_DISABLE_EXEC:
843 p->p_flag2 |= P2_STKGAP_DISABLE_EXEC;
854 stackgap_status(struct thread *td, struct proc *p, void *data)
858 PROC_LOCK_ASSERT(p, MA_OWNED);
860 d = (p->p_flag2 & P2_STKGAP_DISABLE) != 0 ? PROC_STACKGAP_DISABLE :
861 PROC_STACKGAP_ENABLE;
862 d |= (p->p_flag2 & P2_STKGAP_DISABLE_EXEC) != 0 ?
863 PROC_STACKGAP_DISABLE_EXEC : PROC_STACKGAP_ENABLE_EXEC;
869 wxmap_ctl(struct thread *td, struct proc *p, void *data)
875 PROC_LOCK_ASSERT(p, MA_OWNED);
876 if ((p->p_flag & P_WEXIT) != 0)
878 state = *(int *)data;
881 case PROC_WX_MAPPINGS_PERMIT:
882 p->p_flag2 |= P2_WXORX_DISABLE;
885 vm = vmspace_acquire_ref(p);
889 map->flags &= ~MAP_WXORX;
896 case PROC_WX_MAPPINGS_DISALLOW_EXEC:
897 p->p_flag2 |= P2_WXORX_ENABLE_EXEC;
907 wxmap_status(struct thread *td, struct proc *p, void *data)
912 PROC_LOCK_ASSERT(p, MA_OWNED);
913 if ((p->p_flag & P_WEXIT) != 0)
917 if ((p->p_flag2 & P2_WXORX_DISABLE) != 0)
918 d |= PROC_WX_MAPPINGS_PERMIT;
919 if ((p->p_flag2 & P2_WXORX_ENABLE_EXEC) != 0)
920 d |= PROC_WX_MAPPINGS_DISALLOW_EXEC;
923 vm = vmspace_acquire_ref(p);
925 if ((vm->vm_map.flags & MAP_WXORX) != 0)
926 d |= PROC_WXORX_ENFORCE;
936 pdeathsig_ctl(struct thread *td, struct proc *p, void *data)
940 signum = *(int *)data;
941 if (p != td->td_proc || (signum != 0 && !_SIG_VALID(signum)))
943 p->p_pdeathsig = signum;
948 pdeathsig_status(struct thread *td, struct proc *p, void *data)
950 if (p != td->td_proc)
952 *(int *)data = p->p_pdeathsig;
962 struct procctl_cmd_info {
965 bool esrch_is_einval : 1;
966 bool copyout_on_error : 1;
967 bool no_nonnull_data : 1;
968 bool need_candebug : 1;
971 int (*exec)(struct thread *, struct proc *, void *);
972 bool (*sapblk)(struct thread *, void *);
974 static const struct procctl_cmd_info procctl_cmds_info[] = {
976 { .lock_tree = PCTL_SLOCKED, .one_proc = false,
977 .esrch_is_einval = false, .no_nonnull_data = false,
978 .need_candebug = false,
979 .copyin_sz = sizeof(int), .copyout_sz = 0,
980 .exec = protect_set, .copyout_on_error = false, },
981 [PROC_REAP_ACQUIRE] =
982 { .lock_tree = PCTL_XLOCKED, .one_proc = true,
983 .esrch_is_einval = false, .no_nonnull_data = true,
984 .need_candebug = false,
985 .copyin_sz = 0, .copyout_sz = 0,
986 .exec = reap_acquire, .copyout_on_error = false, },
987 [PROC_REAP_RELEASE] =
988 { .lock_tree = PCTL_XLOCKED, .one_proc = true,
989 .esrch_is_einval = false, .no_nonnull_data = true,
990 .need_candebug = false,
991 .copyin_sz = 0, .copyout_sz = 0,
992 .exec = reap_release, .copyout_on_error = false, },
994 { .lock_tree = PCTL_SLOCKED, .one_proc = true,
995 .esrch_is_einval = false, .no_nonnull_data = false,
996 .need_candebug = false,
998 .copyout_sz = sizeof(struct procctl_reaper_status),
999 .exec = reap_status, .copyout_on_error = false, },
1000 [PROC_REAP_GETPIDS] =
1001 { .lock_tree = PCTL_SLOCKED, .one_proc = true,
1002 .esrch_is_einval = false, .no_nonnull_data = false,
1003 .need_candebug = false,
1004 .copyin_sz = sizeof(struct procctl_reaper_pids),
1006 .exec = reap_getpids, .copyout_on_error = false, },
1008 { .lock_tree = PCTL_SLOCKED, .one_proc = true,
1009 .esrch_is_einval = false, .no_nonnull_data = false,
1010 .need_candebug = false,
1011 .copyin_sz = sizeof(struct procctl_reaper_kill),
1012 .copyout_sz = sizeof(struct procctl_reaper_kill),
1013 .exec = reap_kill, .copyout_on_error = true,
1014 .sapblk = reap_kill_sapblk, },
1016 { .lock_tree = PCTL_SLOCKED, .one_proc = false,
1017 .esrch_is_einval = false, .no_nonnull_data = false,
1018 .need_candebug = true,
1019 .copyin_sz = sizeof(int), .copyout_sz = 0,
1020 .exec = trace_ctl, .copyout_on_error = false, },
1021 [PROC_TRACE_STATUS] =
1022 { .lock_tree = PCTL_UNLOCKED, .one_proc = true,
1023 .esrch_is_einval = false, .no_nonnull_data = false,
1024 .need_candebug = false,
1025 .copyin_sz = 0, .copyout_sz = sizeof(int),
1026 .exec = trace_status, .copyout_on_error = false, },
1027 [PROC_TRAPCAP_CTL] =
1028 { .lock_tree = PCTL_SLOCKED, .one_proc = false,
1029 .esrch_is_einval = false, .no_nonnull_data = false,
1030 .need_candebug = true,
1031 .copyin_sz = sizeof(int), .copyout_sz = 0,
1032 .exec = trapcap_ctl, .copyout_on_error = false, },
1033 [PROC_TRAPCAP_STATUS] =
1034 { .lock_tree = PCTL_UNLOCKED, .one_proc = true,
1035 .esrch_is_einval = false, .no_nonnull_data = false,
1036 .need_candebug = false,
1037 .copyin_sz = 0, .copyout_sz = sizeof(int),
1038 .exec = trapcap_status, .copyout_on_error = false, },
1039 [PROC_PDEATHSIG_CTL] =
1040 { .lock_tree = PCTL_UNLOCKED, .one_proc = true,
1041 .esrch_is_einval = true, .no_nonnull_data = false,
1042 .need_candebug = false,
1043 .copyin_sz = sizeof(int), .copyout_sz = 0,
1044 .exec = pdeathsig_ctl, .copyout_on_error = false, },
1045 [PROC_PDEATHSIG_STATUS] =
1046 { .lock_tree = PCTL_UNLOCKED, .one_proc = true,
1047 .esrch_is_einval = true, .no_nonnull_data = false,
1048 .need_candebug = false,
1049 .copyin_sz = 0, .copyout_sz = sizeof(int),
1050 .exec = pdeathsig_status, .copyout_on_error = false, },
1052 { .lock_tree = PCTL_UNLOCKED, .one_proc = true,
1053 .esrch_is_einval = false, .no_nonnull_data = false,
1054 .need_candebug = true,
1055 .copyin_sz = sizeof(int), .copyout_sz = 0,
1056 .exec = aslr_ctl, .copyout_on_error = false, },
1057 [PROC_ASLR_STATUS] =
1058 { .lock_tree = PCTL_UNLOCKED, .one_proc = true,
1059 .esrch_is_einval = false, .no_nonnull_data = false,
1060 .need_candebug = false,
1061 .copyin_sz = 0, .copyout_sz = sizeof(int),
1062 .exec = aslr_status, .copyout_on_error = false, },
1063 [PROC_PROTMAX_CTL] =
1064 { .lock_tree = PCTL_UNLOCKED, .one_proc = true,
1065 .esrch_is_einval = false, .no_nonnull_data = false,
1066 .need_candebug = true,
1067 .copyin_sz = sizeof(int), .copyout_sz = 0,
1068 .exec = protmax_ctl, .copyout_on_error = false, },
1069 [PROC_PROTMAX_STATUS] =
1070 { .lock_tree = PCTL_UNLOCKED, .one_proc = true,
1071 .esrch_is_einval = false, .no_nonnull_data = false,
1072 .need_candebug = false,
1073 .copyin_sz = 0, .copyout_sz = sizeof(int),
1074 .exec = protmax_status, .copyout_on_error = false, },
1075 [PROC_STACKGAP_CTL] =
1076 { .lock_tree = PCTL_UNLOCKED, .one_proc = true,
1077 .esrch_is_einval = false, .no_nonnull_data = false,
1078 .need_candebug = true,
1079 .copyin_sz = sizeof(int), .copyout_sz = 0,
1080 .exec = stackgap_ctl, .copyout_on_error = false, },
1081 [PROC_STACKGAP_STATUS] =
1082 { .lock_tree = PCTL_UNLOCKED, .one_proc = true,
1083 .esrch_is_einval = false, .no_nonnull_data = false,
1084 .need_candebug = false,
1085 .copyin_sz = 0, .copyout_sz = sizeof(int),
1086 .exec = stackgap_status, .copyout_on_error = false, },
1087 [PROC_NO_NEW_PRIVS_CTL] =
1088 { .lock_tree = PCTL_SLOCKED, .one_proc = true,
1089 .esrch_is_einval = false, .no_nonnull_data = false,
1090 .need_candebug = true,
1091 .copyin_sz = sizeof(int), .copyout_sz = 0,
1092 .exec = no_new_privs_ctl, .copyout_on_error = false, },
1093 [PROC_NO_NEW_PRIVS_STATUS] =
1094 { .lock_tree = PCTL_UNLOCKED, .one_proc = true,
1095 .esrch_is_einval = false, .no_nonnull_data = false,
1096 .need_candebug = false,
1097 .copyin_sz = 0, .copyout_sz = sizeof(int),
1098 .exec = no_new_privs_status, .copyout_on_error = false, },
1100 { .lock_tree = PCTL_UNLOCKED, .one_proc = true,
1101 .esrch_is_einval = false, .no_nonnull_data = false,
1102 .need_candebug = true,
1103 .copyin_sz = sizeof(int), .copyout_sz = 0,
1104 .exec = wxmap_ctl, .copyout_on_error = false, },
1105 [PROC_WXMAP_STATUS] =
1106 { .lock_tree = PCTL_UNLOCKED, .one_proc = true,
1107 .esrch_is_einval = false, .no_nonnull_data = false,
1108 .need_candebug = false,
1109 .copyin_sz = 0, .copyout_sz = sizeof(int),
1110 .exec = wxmap_status, .copyout_on_error = false, },
1114 sys_procctl(struct thread *td, struct procctl_args *uap)
1117 struct procctl_reaper_status rs;
1118 struct procctl_reaper_pids rp;
1119 struct procctl_reaper_kill rk;
1122 const struct procctl_cmd_info *cmd_info;
1125 if (uap->com >= PROC_PROCCTL_MD_MIN)
1126 return (cpu_procctl(td, uap->idtype, uap->id,
1127 uap->com, uap->data));
1128 if (uap->com == 0 || uap->com >= nitems(procctl_cmds_info))
1130 cmd_info = &procctl_cmds_info[uap->com];
1131 bzero(&x, sizeof(x));
1133 if (cmd_info->copyin_sz > 0) {
1134 error = copyin(uap->data, &x, cmd_info->copyin_sz);
1137 } else if (cmd_info->no_nonnull_data && uap->data != NULL) {
1141 error = kern_procctl(td, uap->idtype, uap->id, uap->com, &x);
1143 if (cmd_info->copyout_sz > 0 && (error == 0 ||
1144 cmd_info->copyout_on_error)) {
1145 error1 = copyout(&x, uap->data, cmd_info->copyout_sz);
1153 kern_procctl_single(struct thread *td, struct proc *p, int com, void *data)
1156 PROC_LOCK_ASSERT(p, MA_OWNED);
1157 return (procctl_cmds_info[com].exec(td, p, data));
1161 kern_procctl(struct thread *td, idtype_t idtype, id_t id, int com, void *data)
1165 const struct procctl_cmd_info *cmd_info;
1166 int error, first_error, ok;
1169 MPASS(com > 0 && com < nitems(procctl_cmds_info));
1170 cmd_info = &procctl_cmds_info[com];
1171 if (idtype != P_PID && cmd_info->one_proc)
1175 if (cmd_info->sapblk != NULL) {
1176 sapblk = cmd_info->sapblk(td, data);
1177 if (sapblk && !stop_all_proc_block())
1181 switch (cmd_info->lock_tree) {
1183 sx_xlock(&proctree_lock);
1186 sx_slock(&proctree_lock);
1201 error = cmd_info->esrch_is_einval ?
1205 error = cmd_info->need_candebug ? p_candebug(td, p) :
1209 error = kern_procctl_single(td, p, com, data);
1214 * Attempt to apply the operation to all members of the
1215 * group. Ignore processes in the group that can't be
1216 * seen. Ignore errors so long as at least one process is
1217 * able to complete the request successfully.
1227 LIST_FOREACH(p, &pg->pg_members, p_pglist) {
1229 if (p->p_state == PRS_NEW ||
1230 p->p_state == PRS_ZOMBIE ||
1231 (cmd_info->need_candebug ? p_candebug(td, p) :
1232 p_cansee(td, p)) != 0) {
1236 error = kern_procctl_single(td, p, com, data);
1240 else if (first_error == 0)
1241 first_error = error;
1245 else if (first_error != 0)
1246 error = first_error;
1249 * Was not able to see any processes in the
1259 switch (cmd_info->lock_tree) {
1261 sx_xunlock(&proctree_lock);
1264 sx_sunlock(&proctree_lock);
1270 stop_all_proc_unblock();