diff --git a/lib/libsys/pdfork.2 b/lib/libsys/pdfork.2 --- a/lib/libsys/pdfork.2 +++ b/lib/libsys/pdfork.2 @@ -175,6 +175,22 @@ .Er ESRCH error. .Pp +The +.Fn pdopenpid +request is allowed in the capability mode +.Pq see Xr capsicum 4 +.Bl -dash -compact +.It +when the target process is the child of the calling process +.It +when the calling process is the debugger of the target process. +.El +The debugger is attached to its target by +.Xr ptrace 2 , +.Xr pdptrace 2 , +or by other means, e.g. by debugging the process real parent with +the follow mode on fork. +.Pp .Fn pdgetpid queries the process ID (PID) in the process descriptor .Fa fd . @@ -441,6 +457,27 @@ refers to a file that cannot be duplicated across a process boundary, such as a kqueue. .El +.Pp +The +.Fn pdptrace +function might return the same errors as +.Xr ptrace 2 , +as well as the following specific errors: +.Bl -tag -width Er +.It Bq Er ECAPMODE +The process issuing the +.Fn pdptrace +call is in capability mode, +and the +.Dv security.bsd.ptrace_in_cap_mode +tunable is set to +.Dv false . +.It Bq Er ENOTCAPABLE +The process called +.Fn pdptrace +on the process descriptor that does not have the +.Dv CAP_PTRACE +capability enabled. .Sh SEE ALSO .Xr close 2 , .Xr fork 2 , diff --git a/lib/libsys/ptrace.2 b/lib/libsys/ptrace.2 --- a/lib/libsys/ptrace.2 +++ b/lib/libsys/ptrace.2 @@ -180,6 +180,12 @@ process'. These requests will fail with error .Er ESRCH . +.It Dv security.bsd.ptrace_in_cap_mode +Setting this sysctl to true allows the +.Fn ptrace +requests issued by the processes in capability mode, +see +.Xr pdptrace 2 . .It Dv securelevel and init The .Xr init 1 @@ -1511,6 +1517,10 @@ .Fa pve_pathlen holds the minimum buffer size required on return. .El +.It Bq ECAPMODE +The process issuing the +.Fn ptrace +call is in capability mode. .El .Sh SEE ALSO .Xr execve 2 , diff --git a/sys/compat/freebsd32/freebsd32_misc.c b/sys/compat/freebsd32/freebsd32_misc.c --- a/sys/compat/freebsd32/freebsd32_misc.c +++ b/sys/compat/freebsd32/freebsd32_misc.c @@ -1049,9 +1049,6 @@ void *addr; int data, error, i; - if (!allow_ptrace) - return (ENOSYS); - error = 0; addr = &r; data = udata; @@ -1259,20 +1256,29 @@ AUDIT_ARG_CMD(uap->req); AUDIT_ARG_VALUE(uap->data); - error = freebsd32_ptrace_copyinout(td, uap->req, false, uap->pid, - -1, -1, uap->addr, uap->data); + error = ptrace_check_allowed(td, uap->req, false, uap->pid); + if (error == 0) { + error = freebsd32_ptrace_copyinout(td, uap->req, false, + uap->pid, -1, -1, uap->addr, uap->data); + } return (error); } int freebsd32_pdptrace(struct thread *td, struct freebsd32_pdptrace_args *uap) { + int error; + AUDIT_ARG_FD(uap->pfd); AUDIT_ARG_CMD(uap->req); AUDIT_ARG_VALUE(uap->data); - return (freebsd32_ptrace_copyinout(td, uap->req, true, -1, - uap->pfd, uap->lwpid, uap->addr, uap->data)); + error = ptrace_check_allowed(td, uap->req, true, -1); + if (error == 0) { + error = freebsd32_ptrace_copyinout(td, uap->req, true, -1, + uap->pfd, uap->lwpid, uap->addr, uap->data); + } + return (error); } int diff --git a/sys/compat/freebsd32/freebsd32_sysent.c b/sys/compat/freebsd32/freebsd32_sysent.c --- a/sys/compat/freebsd32/freebsd32_sysent.c +++ b/sys/compat/freebsd32/freebsd32_sysent.c @@ -93,7 +93,7 @@ { .sy_narg = AS(setuid_args), .sy_call = (sy_call_t *)sys_setuid, .sy_auevent = AUE_SETUID, .sy_flags = SYF_CAPENABLED, .sy_thrcnt = SY_THR_STATIC }, /* 23 = setuid */ { .sy_narg = 0, .sy_call = (sy_call_t *)sys_getuid, .sy_auevent = AUE_GETUID, .sy_flags = SYF_CAPENABLED, .sy_thrcnt = SY_THR_STATIC }, /* 24 = getuid */ { .sy_narg = 0, .sy_call = (sy_call_t *)sys_geteuid, .sy_auevent = AUE_GETEUID, .sy_flags = SYF_CAPENABLED, .sy_thrcnt = SY_THR_STATIC }, /* 25 = geteuid */ - { .sy_narg = AS(freebsd32_ptrace_args), .sy_call = (sy_call_t *)freebsd32_ptrace, .sy_auevent = AUE_PTRACE, .sy_flags = 0, .sy_thrcnt = SY_THR_STATIC }, /* 26 = freebsd32_ptrace */ + { .sy_narg = AS(freebsd32_ptrace_args), .sy_call = (sy_call_t *)freebsd32_ptrace, .sy_auevent = AUE_PTRACE, .sy_flags = SYF_CAPENABLED, .sy_thrcnt = SY_THR_STATIC }, /* 26 = freebsd32_ptrace */ { .sy_narg = AS(freebsd32_recvmsg_args), .sy_call = (sy_call_t *)freebsd32_recvmsg, .sy_auevent = AUE_RECVMSG, .sy_flags = SYF_CAPENABLED, .sy_thrcnt = SY_THR_STATIC }, /* 27 = freebsd32_recvmsg */ { .sy_narg = AS(freebsd32_sendmsg_args), .sy_call = (sy_call_t *)freebsd32_sendmsg, .sy_auevent = AUE_SENDMSG, .sy_flags = SYF_CAPENABLED, .sy_thrcnt = SY_THR_STATIC }, /* 28 = freebsd32_sendmsg */ { .sy_narg = AS(recvfrom_args), .sy_call = (sy_call_t *)sys_recvfrom, .sy_auevent = AUE_RECVFROM, .sy_flags = SYF_CAPENABLED, .sy_thrcnt = SY_THR_STATIC }, /* 29 = recvfrom */ @@ -670,7 +670,7 @@ { .sy_narg = AS(pdrfork_args), .sy_call = (sy_call_t *)sys_pdrfork, .sy_auevent = AUE_PDRFORK, .sy_flags = SYF_CAPENABLED, .sy_thrcnt = SY_THR_STATIC }, /* 600 = pdrfork */ { .sy_narg = AS(freebsd32_pdwait_args), .sy_call = (sy_call_t *)freebsd32_pdwait, .sy_auevent = AUE_PDWAIT, .sy_flags = SYF_CAPENABLED, .sy_thrcnt = SY_THR_STATIC }, /* 601 = freebsd32_pdwait */ { .sy_narg = AS(renameat2_args), .sy_call = (sy_call_t *)sys_renameat2, .sy_auevent = AUE_RENAMEAT, .sy_flags = SYF_CAPENABLED, .sy_thrcnt = SY_THR_STATIC }, /* 602 = renameat2 */ - { .sy_narg = AS(pdopenpid_args), .sy_call = (sy_call_t *)sys_pdopenpid, .sy_auevent = AUE_PDOPENPID, .sy_flags = 0, .sy_thrcnt = SY_THR_STATIC }, /* 603 = pdopenpid */ + { .sy_narg = AS(pdopenpid_args), .sy_call = (sy_call_t *)sys_pdopenpid, .sy_auevent = AUE_PDOPENPID, .sy_flags = SYF_CAPENABLED, .sy_thrcnt = SY_THR_STATIC }, /* 603 = pdopenpid */ { .sy_narg = AS(pddupfd_args), .sy_call = (sy_call_t *)sys_pddupfd, .sy_auevent = AUE_NULL, .sy_flags = 0, .sy_thrcnt = SY_THR_STATIC }, /* 604 = pddupfd */ - { .sy_narg = AS(freebsd32_pdptrace_args), .sy_call = (sy_call_t *)freebsd32_pdptrace, .sy_auevent = AUE_PDPTRACE, .sy_flags = 0, .sy_thrcnt = SY_THR_STATIC }, /* 605 = freebsd32_pdptrace */ + { .sy_narg = AS(freebsd32_pdptrace_args), .sy_call = (sy_call_t *)freebsd32_pdptrace, .sy_auevent = AUE_PDPTRACE, .sy_flags = SYF_CAPENABLED, .sy_thrcnt = SY_THR_STATIC }, /* 605 = freebsd32_pdptrace */ }; diff --git a/sys/kern/init_sysent.c b/sys/kern/init_sysent.c --- a/sys/kern/init_sysent.c +++ b/sys/kern/init_sysent.c @@ -92,7 +92,7 @@ { .sy_narg = AS(setuid_args), .sy_call = (sy_call_t *)sys_setuid, .sy_auevent = AUE_SETUID, .sy_flags = SYF_CAPENABLED, .sy_thrcnt = SY_THR_STATIC }, /* 23 = setuid */ { .sy_narg = 0, .sy_call = (sy_call_t *)sys_getuid, .sy_auevent = AUE_GETUID, .sy_flags = SYF_CAPENABLED, .sy_thrcnt = SY_THR_STATIC }, /* 24 = getuid */ { .sy_narg = 0, .sy_call = (sy_call_t *)sys_geteuid, .sy_auevent = AUE_GETEUID, .sy_flags = SYF_CAPENABLED, .sy_thrcnt = SY_THR_STATIC }, /* 25 = geteuid */ - { .sy_narg = AS(ptrace_args), .sy_call = (sy_call_t *)sys_ptrace, .sy_auevent = AUE_PTRACE, .sy_flags = 0, .sy_thrcnt = SY_THR_STATIC }, /* 26 = ptrace */ + { .sy_narg = AS(ptrace_args), .sy_call = (sy_call_t *)sys_ptrace, .sy_auevent = AUE_PTRACE, .sy_flags = SYF_CAPENABLED, .sy_thrcnt = SY_THR_STATIC }, /* 26 = ptrace */ { .sy_narg = AS(recvmsg_args), .sy_call = (sy_call_t *)sys_recvmsg, .sy_auevent = AUE_RECVMSG, .sy_flags = SYF_CAPENABLED, .sy_thrcnt = SY_THR_STATIC }, /* 27 = recvmsg */ { .sy_narg = AS(sendmsg_args), .sy_call = (sy_call_t *)sys_sendmsg, .sy_auevent = AUE_SENDMSG, .sy_flags = SYF_CAPENABLED, .sy_thrcnt = SY_THR_STATIC }, /* 28 = sendmsg */ { .sy_narg = AS(recvfrom_args), .sy_call = (sy_call_t *)sys_recvfrom, .sy_auevent = AUE_RECVFROM, .sy_flags = SYF_CAPENABLED, .sy_thrcnt = SY_THR_STATIC }, /* 29 = recvfrom */ @@ -669,7 +669,7 @@ { .sy_narg = AS(pdrfork_args), .sy_call = (sy_call_t *)sys_pdrfork, .sy_auevent = AUE_PDRFORK, .sy_flags = SYF_CAPENABLED, .sy_thrcnt = SY_THR_STATIC }, /* 600 = pdrfork */ { .sy_narg = AS(pdwait_args), .sy_call = (sy_call_t *)sys_pdwait, .sy_auevent = AUE_PDWAIT, .sy_flags = SYF_CAPENABLED, .sy_thrcnt = SY_THR_STATIC }, /* 601 = pdwait */ { .sy_narg = AS(renameat2_args), .sy_call = (sy_call_t *)sys_renameat2, .sy_auevent = AUE_RENAMEAT, .sy_flags = SYF_CAPENABLED, .sy_thrcnt = SY_THR_STATIC }, /* 602 = renameat2 */ - { .sy_narg = AS(pdopenpid_args), .sy_call = (sy_call_t *)sys_pdopenpid, .sy_auevent = AUE_PDOPENPID, .sy_flags = 0, .sy_thrcnt = SY_THR_STATIC }, /* 603 = pdopenpid */ + { .sy_narg = AS(pdopenpid_args), .sy_call = (sy_call_t *)sys_pdopenpid, .sy_auevent = AUE_PDOPENPID, .sy_flags = SYF_CAPENABLED, .sy_thrcnt = SY_THR_STATIC }, /* 603 = pdopenpid */ { .sy_narg = AS(pddupfd_args), .sy_call = (sy_call_t *)sys_pddupfd, .sy_auevent = AUE_NULL, .sy_flags = 0, .sy_thrcnt = SY_THR_STATIC }, /* 604 = pddupfd */ - { .sy_narg = AS(pdptrace_args), .sy_call = (sy_call_t *)sys_pdptrace, .sy_auevent = AUE_PDPTRACE, .sy_flags = 0, .sy_thrcnt = SY_THR_STATIC }, /* 605 = pdptrace */ + { .sy_narg = AS(pdptrace_args), .sy_call = (sy_call_t *)sys_pdptrace, .sy_auevent = AUE_PDPTRACE, .sy_flags = SYF_CAPENABLED, .sy_thrcnt = SY_THR_STATIC }, /* 605 = pdptrace */ }; diff --git a/sys/kern/kern_prot.c b/sys/kern/kern_prot.c --- a/sys/kern/kern_prot.c +++ b/sys/kern/kern_prot.c @@ -46,14 +46,13 @@ * System calls related to processes and protection */ -#include #include "opt_inet.h" #include "opt_inet6.h" -#include #include #include #include +#include #include #include #include @@ -2313,6 +2312,24 @@ return (uidsubset && grpsubset); } +int +p_canopen(struct thread *td, struct proc *p) +{ +#ifdef INVARIANTS + if (IN_CAPABILITY_MODE(td)) + sx_assert(&proctree_lock, SX_LOCKED); +#endif + + /* + * Allow implicit parent in cap mode: either real parent or + * debugger can open pid. + */ + if (!IN_CAPABILITY_MODE(td) || (allow_ptrace_in_cap_mode && + (td->td_proc == p->p_pptr || p->p_oppid == td->td_proc->p_pid))) + return (0); + return (ECAPMODE); +} + /*- * Determine whether td may debug p. * Returns: 0 for permitted, an errno value otherwise diff --git a/sys/kern/sys_procdesc.c b/sys/kern/sys_procdesc.c --- a/sys/kern/sys_procdesc.c +++ b/sys/kern/sys_procdesc.c @@ -665,6 +665,12 @@ PROC_UNLOCK(p); return (EBUSY); } + error = p_canopen(td, p); + if (error != 0) { + PROC_UNLOCK(p); + return (error); + } + pd = p->p_procdesc; if (pd != NULL) { MPASS((p->p_zombieref & PZOMBIEREF_PROCDESC) != 0); diff --git a/sys/kern/sys_process.c b/sys/kern/sys_process.c --- a/sys/kern/sys_process.c +++ b/sys/kern/sys_process.c @@ -51,6 +51,7 @@ #include #include #include +#include #include #include #include @@ -70,6 +71,11 @@ #include #endif +bool allow_ptrace_in_cap_mode = true; +SYSCTL_BOOL(_security_bsd, OID_AUTO, allow_ptrace_in_cap_mode, CTLFLAG_RWTUN, + &allow_ptrace_in_cap_mode, 0, + "Allow ptrace(2) in capability mode"); + /* Assert it's safe to unlock a process, e.g. to allocate working memory */ #define PROC_ASSERT_TRACEREQ(p) MPASS(((p)->p_flag2 & P2_PTRACEREQ) != 0) @@ -742,9 +748,6 @@ void *addr; int error; - if (!allow_ptrace) - return (ENOSYS); - error = 0; addr = &r; @@ -1044,6 +1047,24 @@ return (NULL); } +int +ptrace_check_allowed(struct thread *td, int req, bool pd_mode, pid_t pid) +{ + if (!allow_ptrace) + return (ENOSYS); + if (!IN_CAPABILITY_MODE(td)) + return (0); + if (!allow_ptrace_in_cap_mode) + return (ECAPMODE); + if (pd_mode) + return (0); + if (req == PT_GET_CHILDREN && pid == td->td_proc->p_pid) + return (0); + if (req == PT_CLEARSTEP && pid == td->td_tid) + return (0); + return (ECAPMODE); +} + int ptrace_action(struct thread *td, int req, bool pd_mode, pid_t pid, int pfd, lwpid_t lwpid, void *addr, int data) @@ -2056,8 +2077,11 @@ AUDIT_ARG_CMD(uap->req); AUDIT_ARG_VALUE(uap->data); - error = ptrace_copyinout(td, uap->req, false, uap->pid, -1, -1, - uap->addr, uap->data); + error = ptrace_check_allowed(td, uap->req, false, uap->pid); + if (error == 0) { + error = ptrace_copyinout(td, uap->req, false, uap->pid, + -1, -1, uap->addr, uap->data); + } return (error); } @@ -2070,7 +2094,10 @@ AUDIT_ARG_CMD(uap->req); AUDIT_ARG_VALUE(uap->data); - error = ptrace_copyinout(td, uap->req, true, -1, uap->pfd, uap->lwpid, - uap->addr, uap->data); + error = ptrace_check_allowed(td, uap->req, true, -1); + if (error == 0) { + error = ptrace_copyinout(td, uap->req, true, -1, uap->pfd, + uap->lwpid, uap->addr, uap->data); + } return (error); } diff --git a/sys/kern/syscalls.master b/sys/kern/syscalls.master --- a/sys/kern/syscalls.master +++ b/sys/kern/syscalls.master @@ -264,7 +264,7 @@ 25 AUE_GETEUID STD|CAPENABLED { uid_t geteuid(void); } -26 AUE_PTRACE STD { +26 AUE_PTRACE STD|CAPENABLED { int ptrace( int req, pid_t pid, @@ -3429,7 +3429,7 @@ int flags ); } -603 AUE_PDOPENPID STD { +603 AUE_PDOPENPID STD|CAPENABLED { int pdopenpid( pid_t pid, int flags @@ -3442,7 +3442,7 @@ int flags ); } -605 AUE_PDPTRACE STD { +605 AUE_PDPTRACE STD|CAPENABLED { int pdptrace( int req, int pfd, diff --git a/sys/sys/proc.h b/sys/sys/proc.h --- a/sys/sys/proc.h +++ b/sys/sys/proc.h @@ -1195,6 +1195,7 @@ void maybe_yield(void); void mi_switch(int flags); int p_candebug(struct thread *td, struct proc *p); +int p_canopen(struct thread *td, struct proc *p); int p_cansee(struct thread *td, struct proc *p); int p_cansched(struct thread *td, struct proc *p); int p_cansignal(struct thread *td, struct proc *p, int signum); diff --git a/sys/sys/ptrace.h b/sys/sys/ptrace.h --- a/sys/sys/ptrace.h +++ b/sys/sys/ptrace.h @@ -293,8 +293,11 @@ void ptrace_unsuspend(struct proc *p); int ptrace_action(struct thread *td, int req, bool pd_mode, pid_t pid, int pfd, lwpid_t lwpid, void *addr, int data); +int ptrace_check_allowed(struct thread *td, int req, bool pd_mode, + pid_t pid); extern bool allow_ptrace; +extern bool allow_ptrace_in_cap_mode; #else /* !_KERNEL */