35#include <sys/capsicum.h>
41#include <sys/procctl.h>
43#include <sys/syscallsubr.h>
44#include <sys/sysproto.h>
50#include <vm/vm_extern.h>
56 PROC_LOCK_ASSERT(p, MA_OWNED);
57 if (p->p_flag & P_SYSTEM ||
p_cansched(td, p) != 0)
59 if (
flags & PPROT_SET) {
60 p->p_flag |= P_PROTECTED;
61 if (
flags & PPROT_INHERIT)
62 p->p_flag2 |= P2_INHERIT_PROTECTED;
64 p->p_flag &= ~P_PROTECTED;
65 p->p_flag2 &= ~P2_INHERIT_PROTECTED;
87 if (!LIST_EMPTY(&p->p_children))
88 p = LIST_FIRST(&p->p_children);
94 if (LIST_NEXT(p, p_sibling)) {
95 p = LIST_NEXT(p, p_sibling);
107 int error,
flags, ret;
110 switch (PPROT_OP(
flags)) {
118 if ((PPROT_FLAGS(
flags) & ~(PPROT_DESCEND | PPROT_INHERIT)) != 0)
125 if (
flags & PPROT_DESCEND)
139 if (p != td->td_proc)
141 if ((p->p_treeflag & P_TREE_REAPER) != 0)
143 p->p_treeflag |= P_TREE_REAPER;
156 if (p != td->td_proc)
160 if ((p->p_treeflag & P_TREE_REAPER) == 0)
169 struct proc *reap, *p2, *first_p;
170 struct procctl_reaper_status *rs;
174 if ((p->p_treeflag & P_TREE_REAPER) == 0) {
178 rs->rs_flags |= REAPER_STATUS_OWNED;
181 rs->rs_flags |= REAPER_STATUS_REALINIT;
182 rs->rs_reaper = reap->p_pid;
183 rs->rs_descendants = 0;
185 if (!LIST_EMPTY(&reap->p_reaplist)) {
186 first_p = LIST_FIRST(&reap->p_children);
188 first_p = LIST_FIRST(&reap->p_reaplist);
189 rs->rs_pid = first_p->p_pid;
190 LIST_FOREACH(p2, &reap->p_reaplist, p_reapsibling) {
193 rs->rs_descendants++;
204 struct proc *reap, *p2;
205 struct procctl_reaper_pidinfo *pi, *pip;
206 struct procctl_reaper_pids *rp;
213 reap = (p->p_treeflag & P_TREE_REAPER) == 0 ? p->p_reaper : p;
216 LIST_FOREACH(p2, &reap->p_reaplist, p_reapsibling)
219 if (rp->rp_count < n)
221 pi =
malloc(n *
sizeof(*pi), M_TEMP, M_WAITOK);
223 LIST_FOREACH(p2, &reap->p_reaplist, p_reapsibling) {
227 bzero(pip,
sizeof(*pip));
228 pip->pi_pid = p2->p_pid;
229 pip->pi_subtree = p2->p_reapsubtree;
230 pip->pi_flags = REAPER_PIDINFO_VALID;
232 pip->pi_flags |= REAPER_PIDINFO_CHILD;
233 if ((p2->p_treeflag & P_TREE_REAPER) != 0)
234 pip->pi_flags |= REAPER_PIDINFO_REAPER;
238 error = copyout(pi, rp->rp_pids, i *
sizeof(*pi));
247 struct procctl_reaper_kill *rk,
int *error)
257 }
else if (*error == ESRCH) {
258 rk->rk_fpid = p2->p_pid;
278 TAILQ_INSERT_TAIL(tracker, t, link);
284 struct proc *reap, *p2;
286 struct reap_kill_tracker_head tracker;
288 struct procctl_reaper_kill *rk;
293 if (IN_CAPABILITY_MODE(td))
295 if (rk->rk_sig <= 0 || rk->rk_sig > _SIG_MAXSIG ||
296 (rk->rk_flags & ~(REAPER_KILL_CHILDREN |
297 REAPER_KILL_SUBTREE)) != 0 || (rk->rk_flags &
298 (REAPER_KILL_CHILDREN | REAPER_KILL_SUBTREE)) ==
299 (REAPER_KILL_CHILDREN | REAPER_KILL_SUBTREE))
302 reap = (p->p_treeflag & P_TREE_REAPER) == 0 ? p->p_reaper : p;
304 ksi.ksi_signo = rk->rk_sig;
305 ksi.ksi_code = SI_USER;
306 ksi.ksi_pid = td->td_proc->p_pid;
307 ksi.ksi_uid = td->td_ucred->cr_ruid;
311 if ((rk->rk_flags & REAPER_KILL_CHILDREN) != 0) {
312 for (p2 = LIST_FIRST(&reap->p_children); p2 != NULL;
313 p2 = LIST_NEXT(p2, p_sibling)) {
321 TAILQ_INIT(&tracker);
323 while ((t = TAILQ_FIRST(&tracker)) != NULL) {
324 MPASS((t->
parent->p_treeflag & P_TREE_REAPER) != 0);
325 TAILQ_REMOVE(&tracker, t, link);
326 for (p2 = LIST_FIRST(&t->
parent->p_reaplist); p2 != NULL;
327 p2 = LIST_NEXT(p2, p_reapsibling)) {
329 (rk->rk_flags & REAPER_KILL_SUBTREE) != 0 &&
330 p2->p_reapsubtree != rk->rk_subtree)
332 if ((p2->p_treeflag & P_TREE_REAPER) != 0)
348 PROC_LOCK_ASSERT(p, MA_OWNED);
349 state = *(
int *)
data;
356 if ((p->p_flag & P_TRACED) != 0 || p->p_traceflag != 0)
360 case PROC_TRACE_CTL_ENABLE:
361 if (td->td_proc != p)
363 p->p_flag2 &= ~(P2_NOTRACE | P2_NOTRACE_EXEC);
365 case PROC_TRACE_CTL_DISABLE_EXEC:
366 p->p_flag2 |= P2_NOTRACE_EXEC | P2_NOTRACE;
368 case PROC_TRACE_CTL_DISABLE:
369 if ((p->p_flag2 & P2_NOTRACE_EXEC) != 0) {
370 KASSERT((p->p_flag2 & P2_NOTRACE) != 0,
371 (
"dandling P2_NOTRACE_EXEC"));
372 if (td->td_proc != p)
374 p->p_flag2 &= ~P2_NOTRACE_EXEC;
376 p->p_flag2 |= P2_NOTRACE;
391 if ((p->p_flag2 & P2_NOTRACE) != 0) {
392 KASSERT((p->p_flag & P_TRACED) == 0,
393 (
"%d traced but tracing disabled", p->p_pid));
395 }
else if ((p->p_flag & P_TRACED) != 0) {
396 *status = p->p_pptr->p_pid;
408 PROC_LOCK_ASSERT(p, MA_OWNED);
409 state = *(
int *)
data;
412 case PROC_TRAPCAP_CTL_ENABLE:
413 p->p_flag2 |= P2_TRAPCAP;
415 case PROC_TRAPCAP_CTL_DISABLE:
416 p->p_flag2 &= ~P2_TRAPCAP;
430 *status = (p->p_flag2 & P2_TRAPCAP) != 0 ? PROC_TRAPCAP_CTL_ENABLE :
431 PROC_TRAPCAP_CTL_DISABLE;
440 PROC_LOCK_ASSERT(p, MA_OWNED);
441 state = *(
int *)
data;
443 if (state != PROC_NO_NEW_PRIVS_ENABLE)
445 p->p_flag2 |= P2_NO_NEW_PRIVS;
453 *(
int *)
data = (p->p_flag2 & P2_NO_NEW_PRIVS) != 0 ?
454 PROC_NO_NEW_PRIVS_ENABLE : PROC_NO_NEW_PRIVS_DISABLE;
463 PROC_LOCK_ASSERT(p, MA_OWNED);
464 state = *(
int *)
data;
467 case PROC_PROTMAX_FORCE_ENABLE:
468 p->p_flag2 &= ~P2_PROTMAX_DISABLE;
469 p->p_flag2 |= P2_PROTMAX_ENABLE;
471 case PROC_PROTMAX_FORCE_DISABLE:
472 p->p_flag2 |= P2_PROTMAX_DISABLE;
473 p->p_flag2 &= ~P2_PROTMAX_ENABLE;
475 case PROC_PROTMAX_NOFORCE:
476 p->p_flag2 &= ~(P2_PROTMAX_ENABLE | P2_PROTMAX_DISABLE);
489 switch (p->p_flag2 & (P2_PROTMAX_ENABLE | P2_PROTMAX_DISABLE)) {
491 d = PROC_PROTMAX_NOFORCE;
493 case P2_PROTMAX_ENABLE:
494 d = PROC_PROTMAX_FORCE_ENABLE;
496 case P2_PROTMAX_DISABLE:
497 d = PROC_PROTMAX_FORCE_DISABLE;
500 if (kern_mmap_maxprot(p, PROT_READ) == PROT_READ)
501 d |= PROC_PROTMAX_ACTIVE;
511 PROC_LOCK_ASSERT(p, MA_OWNED);
512 state = *(
int *)
data;
515 case PROC_ASLR_FORCE_ENABLE:
516 p->p_flag2 &= ~P2_ASLR_DISABLE;
517 p->p_flag2 |= P2_ASLR_ENABLE;
519 case PROC_ASLR_FORCE_DISABLE:
520 p->p_flag2 |= P2_ASLR_DISABLE;
521 p->p_flag2 &= ~P2_ASLR_ENABLE;
523 case PROC_ASLR_NOFORCE:
524 p->p_flag2 &= ~(P2_ASLR_ENABLE | P2_ASLR_DISABLE);
538 switch (p->p_flag2 & (P2_ASLR_ENABLE | P2_ASLR_DISABLE)) {
540 d = PROC_ASLR_NOFORCE;
543 d = PROC_ASLR_FORCE_ENABLE;
545 case P2_ASLR_DISABLE:
546 d = PROC_ASLR_FORCE_DISABLE;
549 if ((p->p_flag & P_WEXIT) == 0) {
552 vm = vmspace_acquire_ref(p);
554 if ((vm->vm_map.flags & MAP_ASLR) != 0)
555 d |= PROC_ASLR_ACTIVE;
570 PROC_LOCK_ASSERT(p, MA_OWNED);
571 state = *(
int *)
data;
573 if ((state & ~(PROC_STACKGAP_ENABLE | PROC_STACKGAP_DISABLE |
574 PROC_STACKGAP_ENABLE_EXEC | PROC_STACKGAP_DISABLE_EXEC)) != 0)
576 switch (state & (PROC_STACKGAP_ENABLE | PROC_STACKGAP_DISABLE)) {
577 case PROC_STACKGAP_ENABLE:
578 if ((p->p_flag2 & P2_STKGAP_DISABLE) != 0)
581 case PROC_STACKGAP_DISABLE:
582 p->p_flag2 |= P2_STKGAP_DISABLE;
589 switch (state & (PROC_STACKGAP_ENABLE_EXEC |
590 PROC_STACKGAP_DISABLE_EXEC)) {
591 case PROC_STACKGAP_ENABLE_EXEC:
592 p->p_flag2 &= ~P2_STKGAP_DISABLE_EXEC;
594 case PROC_STACKGAP_DISABLE_EXEC:
595 p->p_flag2 |= P2_STKGAP_DISABLE_EXEC;
610 PROC_LOCK_ASSERT(p, MA_OWNED);
612 d = (p->p_flag2 & P2_STKGAP_DISABLE) != 0 ? PROC_STACKGAP_DISABLE :
613 PROC_STACKGAP_ENABLE;
614 d |= (p->p_flag2 & P2_STKGAP_DISABLE_EXEC) != 0 ?
615 PROC_STACKGAP_DISABLE_EXEC : PROC_STACKGAP_ENABLE_EXEC;
627 PROC_LOCK_ASSERT(p, MA_OWNED);
628 if ((p->p_flag & P_WEXIT) != 0)
630 state = *(
int *)
data;
633 case PROC_WX_MAPPINGS_PERMIT:
634 p->p_flag2 |= P2_WXORX_DISABLE;
637 vm = vmspace_acquire_ref(p);
641 map->flags &= ~MAP_WXORX;
648 case PROC_WX_MAPPINGS_DISALLOW_EXEC:
649 p->p_flag2 |= P2_WXORX_ENABLE_EXEC;
664 PROC_LOCK_ASSERT(p, MA_OWNED);
665 if ((p->p_flag & P_WEXIT) != 0)
669 if ((p->p_flag2 & P2_WXORX_DISABLE) != 0)
670 d |= PROC_WX_MAPPINGS_PERMIT;
671 if ((p->p_flag2 & P2_WXORX_ENABLE_EXEC) != 0)
672 d |= PROC_WX_MAPPINGS_DISALLOW_EXEC;
675 vm = vmspace_acquire_ref(p);
677 if ((vm->vm_map.flags & MAP_WXORX) != 0)
678 d |= PROC_WXORX_ENFORCE;
692 signum = *(
int *)
data;
693 if (p != td->td_proc || (signum != 0 && !_SIG_VALID(signum)))
695 p->p_pdeathsig = signum;
702 if (p != td->td_proc)
704 *(
int *)
data = p->p_pdeathsig;
723 int (*
exec)(
struct thread *,
struct proc *,
void *);
728 .esrch_is_einval =
false, .no_nonnull_data =
false,
729 .need_candebug =
false,
732 [PROC_REAP_ACQUIRE] =
734 .esrch_is_einval =
false, .no_nonnull_data =
true,
735 .need_candebug =
false,
736 .copyin_sz = 0, .copyout_sz = 0,
738 [PROC_REAP_RELEASE] =
740 .esrch_is_einval =
false, .no_nonnull_data =
true,
741 .need_candebug =
false,
742 .copyin_sz = 0, .copyout_sz = 0,
746 .esrch_is_einval =
false, .no_nonnull_data =
false,
747 .need_candebug =
false,
749 .copyout_sz =
sizeof(
struct procctl_reaper_status),
751 [PROC_REAP_GETPIDS] =
753 .esrch_is_einval =
false, .no_nonnull_data =
false,
754 .need_candebug =
false,
755 .copyin_sz =
sizeof(
struct procctl_reaper_pids),
760 .esrch_is_einval =
false, .no_nonnull_data =
false,
761 .need_candebug =
false,
762 .copyin_sz =
sizeof(
struct procctl_reaper_kill),
763 .copyout_sz =
sizeof(
struct procctl_reaper_kill),
764 .exec =
reap_kill, .copyout_on_error =
true, },
767 .esrch_is_einval =
false, .no_nonnull_data =
false,
768 .need_candebug =
true,
769 .copyin_sz =
sizeof(int), .copyout_sz = 0,
770 .exec =
trace_ctl, .copyout_on_error =
false, },
771 [PROC_TRACE_STATUS] =
773 .esrch_is_einval =
false, .no_nonnull_data =
false,
774 .need_candebug =
false,
775 .copyin_sz = 0, .copyout_sz =
sizeof(int),
779 .esrch_is_einval =
false, .no_nonnull_data =
false,
780 .need_candebug =
true,
781 .copyin_sz =
sizeof(int), .copyout_sz = 0,
783 [PROC_TRAPCAP_STATUS] =
785 .esrch_is_einval =
false, .no_nonnull_data =
false,
786 .need_candebug =
false,
787 .copyin_sz = 0, .copyout_sz =
sizeof(int),
789 [PROC_PDEATHSIG_CTL] =
791 .esrch_is_einval =
true, .no_nonnull_data =
false,
792 .need_candebug =
false,
793 .copyin_sz =
sizeof(int), .copyout_sz = 0,
795 [PROC_PDEATHSIG_STATUS] =
797 .esrch_is_einval =
true, .no_nonnull_data =
false,
798 .need_candebug =
false,
799 .copyin_sz = 0, .copyout_sz =
sizeof(int),
803 .esrch_is_einval =
false, .no_nonnull_data =
false,
804 .need_candebug =
true,
805 .copyin_sz =
sizeof(int), .copyout_sz = 0,
806 .exec =
aslr_ctl, .copyout_on_error =
false, },
809 .esrch_is_einval =
false, .no_nonnull_data =
false,
810 .need_candebug =
false,
811 .copyin_sz = 0, .copyout_sz =
sizeof(int),
815 .esrch_is_einval =
false, .no_nonnull_data =
false,
816 .need_candebug =
true,
817 .copyin_sz =
sizeof(int), .copyout_sz = 0,
819 [PROC_PROTMAX_STATUS] =
821 .esrch_is_einval =
false, .no_nonnull_data =
false,
822 .need_candebug =
false,
823 .copyin_sz = 0, .copyout_sz =
sizeof(int),
825 [PROC_STACKGAP_CTL] =
827 .esrch_is_einval =
false, .no_nonnull_data =
false,
828 .need_candebug =
true,
829 .copyin_sz =
sizeof(int), .copyout_sz = 0,
831 [PROC_STACKGAP_STATUS] =
833 .esrch_is_einval =
false, .no_nonnull_data =
false,
834 .need_candebug =
false,
835 .copyin_sz = 0, .copyout_sz =
sizeof(int),
837 [PROC_NO_NEW_PRIVS_CTL] =
839 .esrch_is_einval =
false, .no_nonnull_data =
false,
840 .need_candebug =
true,
841 .copyin_sz =
sizeof(int), .copyout_sz = 0,
843 [PROC_NO_NEW_PRIVS_STATUS] =
845 .esrch_is_einval =
false, .no_nonnull_data =
false,
846 .need_candebug =
false,
847 .copyin_sz = 0, .copyout_sz =
sizeof(int),
851 .esrch_is_einval =
false, .no_nonnull_data =
false,
852 .need_candebug =
true,
853 .copyin_sz =
sizeof(int), .copyout_sz = 0,
854 .exec =
wxmap_ctl, .copyout_on_error =
false, },
855 [PROC_WXMAP_STATUS] =
857 .esrch_is_einval =
false, .no_nonnull_data =
false,
858 .need_candebug =
false,
859 .copyin_sz = 0, .copyout_sz =
sizeof(int),
867 struct procctl_reaper_status rs;
868 struct procctl_reaper_pids rp;
869 struct procctl_reaper_kill rk;
875 if (uap->com >= PROC_PROCCTL_MD_MIN)
876 return (cpu_procctl(td, uap->idtype, uap->id,
877 uap->com, uap->data));
881 bzero(&x,
sizeof(x));
884 error = copyin(uap->data, &x, cmd_info->
copyin_sz);
891 error =
kern_procctl(td, uap->idtype, uap->id, uap->com, &x);
893 if (cmd_info->
copyout_sz > 0 && (error == 0 ||
895 error1 = copyout(&x, uap->data, cmd_info->
copyout_sz);
906 PROC_LOCK_ASSERT(p, MA_OWNED);
916 int error, first_error, ok;
920 if (idtype != P_PID && cmd_info->
one_proc)
969 LIST_FOREACH(p, &pg->pg_members, p_pglist) {
971 if (p->p_state == PRS_NEW ||
972 p->p_state == PRS_ZOMBIE ||
982 else if (first_error == 0)
987 else if (first_error != 0)
void reaper_abandon_children(struct proc *p, bool exiting)
struct proc * proc_realparent(struct proc *child)
void *() malloc(size_t size, struct malloc_type *mtp, int flags)
void free(void *addr, struct malloc_type *mtp)
int priv_check(struct thread *td, int priv)
struct sx __exclusive_cache_line proctree_lock
struct proc * pfind(pid_t pid)
struct pgrp * pgfind(pid_t pgid)
static int reap_getpids(struct thread *td, struct proc *p, void *data)
static int trace_ctl(struct thread *td, struct proc *p, void *data)
int kern_procctl(struct thread *td, idtype_t idtype, id_t id, int com, void *data)
TAILQ_HEAD(reap_kill_tracker_head, reap_kill_tracker)
static void reap_kill_sched(struct reap_kill_tracker_head *tracker, struct proc *p2)
static int trace_status(struct thread *td, struct proc *p, void *data)
static int reap_acquire(struct thread *td, struct proc *p, void *data __unused)
static int protect_set(struct thread *td, struct proc *p, void *data)
static int trapcap_ctl(struct thread *td, struct proc *p, void *data)
static int reap_status(struct thread *td, struct proc *p, void *data)
static int no_new_privs_status(struct thread *td, struct proc *p, void *data)
static int kern_procctl_single(struct thread *td, struct proc *p, int com, void *data)
static int reap_release(struct thread *td, struct proc *p, void *data __unused)
int sys_procctl(struct thread *td, struct procctl_args *uap)
static void reap_kill_proc(struct thread *td, struct proc *p2, ksiginfo_t *ksi, struct procctl_reaper_kill *rk, int *error)
static int no_new_privs_ctl(struct thread *td, struct proc *p, void *data)
static int wxmap_status(struct thread *td, struct proc *p, void *data)
static int pdeathsig_ctl(struct thread *td, struct proc *p, void *data)
static int trapcap_status(struct thread *td, struct proc *p, void *data)
static int stackgap_ctl(struct thread *td, struct proc *p, void *data)
static int reap_kill(struct thread *td, struct proc *p, void *data)
static int protect_setchildren(struct thread *td, struct proc *top, int flags)
static int aslr_status(struct thread *td, struct proc *p, void *data)
static int pdeathsig_status(struct thread *td, struct proc *p, void *data)
static const struct procctl_cmd_info procctl_cmds_info[]
static int stackgap_status(struct thread *td, struct proc *p, void *data)
static int aslr_ctl(struct thread *td, struct proc *p, void *data)
static int protmax_ctl(struct thread *td, struct proc *p, void *data)
static int protect_setchild(struct thread *td, struct proc *p, int flags)
static int protmax_status(struct thread *td, struct proc *p, void *data)
static int wxmap_ctl(struct thread *td, struct proc *p, void *data)
int p_cansched(struct thread *td, struct proc *p)
int p_cansee(struct thread *td, struct proc *p)
int p_candebug(struct thread *td, struct proc *p)
int p_cansignal(struct thread *td, struct proc *p, int signum)
int pksignal(struct proc *p, int sig, ksiginfo_t *ksi)
int(* exec)(struct thread *, struct proc *, void *)