Page Menu
Home
FreeBSD
Search
Configure Global Search
Log In
Files
F177855765
D61919.1795438192.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Flag For Later
Award Token
Authored By
Unknown
Size
25 KB
Referenced Files
None
Subscribers
None
D61919.1795438192.diff
View Options
diff --git a/lib/libsys/procctl.2 b/lib/libsys/procctl.2
--- a/lib/libsys/procctl.2
+++ b/lib/libsys/procctl.2
@@ -27,7 +27,7 @@
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
.\" SUCH DAMAGE.
.\"
-.Dd April 21, 2025
+.Dd November 22, 2026
.Dt PROCCTL 2
.Os
.Sh NAME
@@ -78,6 +78,25 @@
.Pq Dv *_STATUS
require the caller to have the right to observe the target.
.Pp
+In capability mode, see
+.Xr capsicum 4 ,
+the only allowed target is the calling process
+.Po
+.Fa idtype
+.Dv P_PID
+with
+.Fa id
+zero or the caller's process ID
+.Pc ,
+and the
+.Dv PROC_SPROTECT ,
+.Dv PROC_REAP_ACQUIRE ,
+.Dv PROC_REAP_RELEASE ,
+.Dv PROC_REAP_GETPIDS ,
+and
+.Dv PROC_REAP_KILL
+requests are not allowed.
+.Pp
The following commands are supported:
.Bl -tag -width indent
.It Dv PROC_ASLR_CTL
@@ -842,6 +861,9 @@
system call
will fail if:
.Bl -tag -width Er
+.It Bq Er ECAPMODE
+The calling process is in capability mode, and the request targets
+another process or is not allowed in capability mode.
.It Bq Er EFAULT
The
.Fa data
diff --git a/sys/amd64/amd64/vm_machdep.c b/sys/amd64/amd64/vm_machdep.c
--- a/sys/amd64/amd64/vm_machdep.c
+++ b/sys/amd64/amd64/vm_machdep.c
@@ -511,9 +511,15 @@
error = EINVAL;
break;
}
- error = pget(id, PGET_CANSEE | PGET_NOTWEXIT | PGET_NOTID, &p);
- if (error != 0)
- break;
+ if (id == 0) {
+ p = curproc;
+ PROC_LOCK(p);
+ } else {
+ error = pget(id, PGET_CANSEE | PGET_NOTWEXIT |
+ PGET_NOTID, &p);
+ if (error != 0)
+ break;
+ }
switch (com) {
case PROC_KPTI_CTL:
cpu_procctl_kpti_ctl(p, val);
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
@@ -3892,14 +3892,23 @@
union {
struct procctl_reaper_pids32 rp;
} x32;
- int error, error1, flags, signum;
+ id_t id;
+ int com, error, error1, flags, signum;
- if (uap->com >= PROC_PROCCTL_MD_MIN)
- return (cpu_procctl(td, uap->idtype, PAIR32TO64(id_t, uap->id),
- uap->com, PTRIN(uap->data)));
+ id = PAIR32TO64(id_t, uap->id);
+ com = uap->com;
+
+ error = kern_procctl_check_perm(td, uap->idtype, id, com);
+ if (error != 0)
+ return (error);
+
+ if (com >= PROC_PROCCTL_MD_MIN) {
+ return (cpu_procctl(td, uap->idtype, id, com,
+ PTRIN(uap->data)));
+ }
bzero(&x, sizeof(x));
- switch (uap->com) {
+ switch (com) {
case PROC_ASLR_CTL:
case PROC_PROTMAX_CTL:
case PROC_SPROTECT:
@@ -3959,9 +3968,8 @@
default:
return (EINVAL);
}
- error = kern_procctl(td, uap->idtype, PAIR32TO64(id_t, uap->id),
- uap->com, data);
- switch (uap->com) {
+ error = kern_procctl(td, uap->idtype, id, com, data);
+ switch (com) {
case PROC_REAP_STATUS:
if (error == 0)
error = copyout(&x.rs, uap->data, sizeof(x.rs));
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
@@ -617,7 +617,7 @@
{ .sy_narg = AS(accept4_args), .sy_call = (sy_call_t *)sys_accept4, .sy_auevent = AUE_ACCEPT, .sy_flags = SYF_CAPENABLED, .sy_thrcnt = SY_THR_STATIC }, /* 541 = accept4 */
{ .sy_narg = AS(pipe2_args), .sy_call = (sy_call_t *)sys_pipe2, .sy_auevent = AUE_PIPE, .sy_flags = SYF_CAPENABLED, .sy_thrcnt = SY_THR_STATIC }, /* 542 = pipe2 */
{ .sy_narg = AS(freebsd32_aio_mlock_args), .sy_call = (sy_call_t *)freebsd32_aio_mlock, .sy_auevent = AUE_AIO_MLOCK, .sy_flags = 0, .sy_thrcnt = SY_THR_STATIC }, /* 543 = freebsd32_aio_mlock */
- { .sy_narg = AS(freebsd32_procctl_args), .sy_call = (sy_call_t *)freebsd32_procctl, .sy_auevent = AUE_PROCCTL, .sy_flags = 0, .sy_thrcnt = SY_THR_STATIC }, /* 544 = freebsd32_procctl */
+ { .sy_narg = AS(freebsd32_procctl_args), .sy_call = (sy_call_t *)freebsd32_procctl, .sy_auevent = AUE_PROCCTL, .sy_flags = SYF_CAPENABLED, .sy_thrcnt = SY_THR_STATIC }, /* 544 = freebsd32_procctl */
{ .sy_narg = AS(freebsd32_ppoll_args), .sy_call = (sy_call_t *)freebsd32_ppoll, .sy_auevent = AUE_POLL, .sy_flags = SYF_CAPENABLED, .sy_thrcnt = SY_THR_STATIC }, /* 545 = freebsd32_ppoll */
{ .sy_narg = AS(freebsd32_futimens_args), .sy_call = (sy_call_t *)freebsd32_futimens, .sy_auevent = AUE_FUTIMES, .sy_flags = SYF_CAPENABLED, .sy_thrcnt = SY_THR_STATIC }, /* 546 = freebsd32_futimens */
{ .sy_narg = AS(freebsd32_utimensat_args), .sy_call = (sy_call_t *)freebsd32_utimensat, .sy_auevent = AUE_FUTIMESAT, .sy_flags = SYF_CAPENABLED, .sy_thrcnt = SY_THR_STATIC }, /* 547 = freebsd32_utimensat */
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
@@ -616,7 +616,7 @@
{ .sy_narg = AS(accept4_args), .sy_call = (sy_call_t *)sys_accept4, .sy_auevent = AUE_ACCEPT, .sy_flags = SYF_CAPENABLED, .sy_thrcnt = SY_THR_STATIC }, /* 541 = accept4 */
{ .sy_narg = AS(pipe2_args), .sy_call = (sy_call_t *)sys_pipe2, .sy_auevent = AUE_PIPE, .sy_flags = SYF_CAPENABLED, .sy_thrcnt = SY_THR_STATIC }, /* 542 = pipe2 */
{ .sy_narg = AS(aio_mlock_args), .sy_call = (sy_call_t *)sys_aio_mlock, .sy_auevent = AUE_AIO_MLOCK, .sy_flags = 0, .sy_thrcnt = SY_THR_STATIC }, /* 543 = aio_mlock */
- { .sy_narg = AS(procctl_args), .sy_call = (sy_call_t *)sys_procctl, .sy_auevent = AUE_PROCCTL, .sy_flags = 0, .sy_thrcnt = SY_THR_STATIC }, /* 544 = procctl */
+ { .sy_narg = AS(procctl_args), .sy_call = (sy_call_t *)sys_procctl, .sy_auevent = AUE_PROCCTL, .sy_flags = SYF_CAPENABLED, .sy_thrcnt = SY_THR_STATIC }, /* 544 = procctl */
{ .sy_narg = AS(ppoll_args), .sy_call = (sy_call_t *)sys_ppoll, .sy_auevent = AUE_POLL, .sy_flags = SYF_CAPENABLED, .sy_thrcnt = SY_THR_STATIC }, /* 545 = ppoll */
{ .sy_narg = AS(futimens_args), .sy_call = (sy_call_t *)sys_futimens, .sy_auevent = AUE_FUTIMES, .sy_flags = SYF_CAPENABLED, .sy_thrcnt = SY_THR_STATIC }, /* 546 = futimens */
{ .sy_narg = AS(utimensat_args), .sy_call = (sy_call_t *)sys_utimensat, .sy_auevent = AUE_FUTIMESAT, .sy_flags = SYF_CAPENABLED, .sy_thrcnt = SY_THR_STATIC }, /* 547 = utimensat */
diff --git a/sys/kern/kern_procctl.c b/sys/kern/kern_procctl.c
--- a/sys/kern/kern_procctl.c
+++ b/sys/kern/kern_procctl.c
@@ -1012,6 +1012,7 @@
bool copyout_on_error : 1;
bool no_nonnull_data : 1;
bool need_candebug : 1;
+ bool cap_enabled : 1;
int copyin_sz;
int copyout_sz;
int (*exec)(struct thread *, struct proc *, void *);
@@ -1021,39 +1022,39 @@
[PROC_SPROTECT] =
{ .lock_tree = PCTL_SLOCKED, .one_proc = false,
.esrch_is_einval = false, .no_nonnull_data = false,
- .need_candebug = false,
+ .need_candebug = false, .cap_enabled = false,
.copyin_sz = sizeof(int), .copyout_sz = 0,
.exec = protect_set, .copyout_on_error = false, },
[PROC_REAP_ACQUIRE] =
{ .lock_tree = PCTL_XLOCKED, .one_proc = true,
.esrch_is_einval = false, .no_nonnull_data = true,
- .need_candebug = false,
+ .need_candebug = false, .cap_enabled = false,
.copyin_sz = 0, .copyout_sz = 0,
.exec = reap_acquire, .copyout_on_error = false, },
[PROC_REAP_RELEASE] =
{ .lock_tree = PCTL_XLOCKED, .one_proc = true,
.esrch_is_einval = false, .no_nonnull_data = true,
- .need_candebug = false,
+ .need_candebug = false, .cap_enabled = false,
.copyin_sz = 0, .copyout_sz = 0,
.exec = reap_release, .copyout_on_error = false, },
[PROC_REAP_STATUS] =
{ .lock_tree = PCTL_SLOCKED, .one_proc = true,
.esrch_is_einval = false, .no_nonnull_data = false,
- .need_candebug = false,
+ .need_candebug = false, .cap_enabled = true,
.copyin_sz = 0,
.copyout_sz = sizeof(struct procctl_reaper_status),
.exec = reap_status, .copyout_on_error = false, },
[PROC_REAP_GETPIDS] =
{ .lock_tree = PCTL_SLOCKED, .one_proc = true,
.esrch_is_einval = false, .no_nonnull_data = false,
- .need_candebug = false,
+ .need_candebug = false, .cap_enabled = false,
.copyin_sz = sizeof(struct procctl_reaper_pids),
.copyout_sz = 0,
.exec = reap_getpids, .copyout_on_error = false, },
[PROC_REAP_KILL] =
{ .lock_tree = PCTL_SLOCKED, .one_proc = true,
.esrch_is_einval = false, .no_nonnull_data = false,
- .need_candebug = false,
+ .need_candebug = false, .cap_enabled = false,
.copyin_sz = sizeof(struct procctl_reaper_kill),
.copyout_sz = sizeof(struct procctl_reaper_kill),
.exec = reap_kill, .copyout_on_error = true,
@@ -1061,113 +1062,139 @@
[PROC_TRACE_CTL] =
{ .lock_tree = PCTL_SLOCKED, .one_proc = false,
.esrch_is_einval = false, .no_nonnull_data = false,
- .need_candebug = true,
+ .need_candebug = true, .cap_enabled = true,
.copyin_sz = sizeof(int), .copyout_sz = 0,
.exec = trace_ctl, .copyout_on_error = false, },
[PROC_TRACE_STATUS] =
{ .lock_tree = PCTL_UNLOCKED, .one_proc = true,
.esrch_is_einval = false, .no_nonnull_data = false,
- .need_candebug = false,
+ .need_candebug = false, .cap_enabled = true,
.copyin_sz = 0, .copyout_sz = sizeof(int),
.exec = trace_status, .copyout_on_error = false, },
[PROC_TRAPCAP_CTL] =
{ .lock_tree = PCTL_SLOCKED, .one_proc = false,
.esrch_is_einval = false, .no_nonnull_data = false,
- .need_candebug = true,
+ .need_candebug = true, .cap_enabled = true,
.copyin_sz = sizeof(int), .copyout_sz = 0,
.exec = trapcap_ctl, .copyout_on_error = false, },
[PROC_TRAPCAP_STATUS] =
{ .lock_tree = PCTL_UNLOCKED, .one_proc = true,
.esrch_is_einval = false, .no_nonnull_data = false,
- .need_candebug = false,
+ .need_candebug = false, .cap_enabled = true,
.copyin_sz = 0, .copyout_sz = sizeof(int),
.exec = trapcap_status, .copyout_on_error = false, },
[PROC_PDEATHSIG_CTL] =
{ .lock_tree = PCTL_UNLOCKED, .one_proc = true,
.esrch_is_einval = true, .no_nonnull_data = false,
- .need_candebug = false,
+ .need_candebug = false, .cap_enabled = true,
.copyin_sz = sizeof(int), .copyout_sz = 0,
.exec = pdeathsig_ctl, .copyout_on_error = false, },
[PROC_PDEATHSIG_STATUS] =
{ .lock_tree = PCTL_UNLOCKED, .one_proc = true,
.esrch_is_einval = true, .no_nonnull_data = false,
- .need_candebug = false,
+ .need_candebug = false, .cap_enabled = true,
.copyin_sz = 0, .copyout_sz = sizeof(int),
.exec = pdeathsig_status, .copyout_on_error = false, },
[PROC_ASLR_CTL] =
{ .lock_tree = PCTL_UNLOCKED, .one_proc = true,
.esrch_is_einval = false, .no_nonnull_data = false,
- .need_candebug = true,
+ .need_candebug = true, .cap_enabled = true,
.copyin_sz = sizeof(int), .copyout_sz = 0,
.exec = aslr_ctl, .copyout_on_error = false, },
[PROC_ASLR_STATUS] =
{ .lock_tree = PCTL_UNLOCKED, .one_proc = true,
.esrch_is_einval = false, .no_nonnull_data = false,
- .need_candebug = false,
+ .need_candebug = false, .cap_enabled = true,
.copyin_sz = 0, .copyout_sz = sizeof(int),
.exec = aslr_status, .copyout_on_error = false, },
[PROC_PROTMAX_CTL] =
{ .lock_tree = PCTL_UNLOCKED, .one_proc = true,
.esrch_is_einval = false, .no_nonnull_data = false,
- .need_candebug = true,
+ .need_candebug = true, .cap_enabled = true,
.copyin_sz = sizeof(int), .copyout_sz = 0,
.exec = protmax_ctl, .copyout_on_error = false, },
[PROC_PROTMAX_STATUS] =
{ .lock_tree = PCTL_UNLOCKED, .one_proc = true,
.esrch_is_einval = false, .no_nonnull_data = false,
- .need_candebug = false,
+ .need_candebug = false, .cap_enabled = true,
.copyin_sz = 0, .copyout_sz = sizeof(int),
.exec = protmax_status, .copyout_on_error = false, },
[PROC_STACKGAP_CTL] =
{ .lock_tree = PCTL_UNLOCKED, .one_proc = true,
.esrch_is_einval = false, .no_nonnull_data = false,
- .need_candebug = true,
+ .need_candebug = true, .cap_enabled = true,
.copyin_sz = sizeof(int), .copyout_sz = 0,
.exec = stackgap_ctl, .copyout_on_error = false, },
[PROC_STACKGAP_STATUS] =
{ .lock_tree = PCTL_UNLOCKED, .one_proc = true,
.esrch_is_einval = false, .no_nonnull_data = false,
- .need_candebug = false,
+ .need_candebug = false, .cap_enabled = true,
.copyin_sz = 0, .copyout_sz = sizeof(int),
.exec = stackgap_status, .copyout_on_error = false, },
[PROC_NO_NEW_PRIVS_CTL] =
{ .lock_tree = PCTL_SLOCKED, .one_proc = true,
.esrch_is_einval = false, .no_nonnull_data = false,
- .need_candebug = true,
+ .need_candebug = true, .cap_enabled = true,
.copyin_sz = sizeof(int), .copyout_sz = 0,
.exec = no_new_privs_ctl, .copyout_on_error = false, },
[PROC_NO_NEW_PRIVS_STATUS] =
{ .lock_tree = PCTL_UNLOCKED, .one_proc = true,
.esrch_is_einval = false, .no_nonnull_data = false,
- .need_candebug = false,
+ .need_candebug = false, .cap_enabled = true,
.copyin_sz = 0, .copyout_sz = sizeof(int),
.exec = no_new_privs_status, .copyout_on_error = false, },
[PROC_WXMAP_CTL] =
{ .lock_tree = PCTL_UNLOCKED, .one_proc = true,
.esrch_is_einval = false, .no_nonnull_data = false,
- .need_candebug = true,
+ .need_candebug = true, .cap_enabled = true,
.copyin_sz = sizeof(int), .copyout_sz = 0,
.exec = wxmap_ctl, .copyout_on_error = false, },
[PROC_WXMAP_STATUS] =
{ .lock_tree = PCTL_UNLOCKED, .one_proc = true,
.esrch_is_einval = false, .no_nonnull_data = false,
- .need_candebug = false,
+ .need_candebug = false, .cap_enabled = true,
.copyin_sz = 0, .copyout_sz = sizeof(int),
.exec = wxmap_status, .copyout_on_error = false, },
[PROC_LOGSIGEXIT_CTL] =
{ .lock_tree = PCTL_SLOCKED, .one_proc = true,
.esrch_is_einval = false, .no_nonnull_data = false,
- .need_candebug = true,
+ .need_candebug = true, .cap_enabled = true,
.copyin_sz = sizeof(int), .copyout_sz = 0,
.exec = logsigexit_ctl, .copyout_on_error = false, },
[PROC_LOGSIGEXIT_STATUS] =
{ .lock_tree = PCTL_UNLOCKED, .one_proc = true,
.esrch_is_einval = false, .no_nonnull_data = false,
- .need_candebug = false,
+ .need_candebug = false, .cap_enabled = true,
.copyin_sz = 0, .copyout_sz = sizeof(int),
.exec = logsigexit_status, .copyout_on_error = false, },
};
+int
+kern_procctl_check_perm(struct thread *td, idtype_t idtype, id_t id, int com)
+{
+ int error;
+
+ error = 0;
+ if (idtype != P_PID || (id != 0 && id != td->td_proc->p_pid)) {
+ error = ECAPMODE;
+ } else if (com >= PROC_PROCCTL_MD_MIN) {
+ /*
+ * An MD control that is unsafe for the caller in
+ * capability mode must refuse it itself.
+ */
+ ;
+ } else if (com > 0 && com < nitems(procctl_cmds_info) &&
+ !procctl_cmds_info[com].cap_enabled) {
+ error = ECAPMODE;
+ }
+
+ if (error != 0 && CAP_TRACING(td))
+ ktrcapfail(CAPFAIL_SYSCALL, NULL);
+ if (!IN_CAPABILITY_MODE(td))
+ error = 0;
+ return (error);
+}
+
int
sys_procctl(struct thread *td, struct procctl_args *uap)
{
@@ -1180,6 +1207,10 @@
const struct procctl_cmd_info *cmd_info;
int error, error1;
+ error = kern_procctl_check_perm(td, uap->idtype, uap->id, uap->com);
+ if (error != 0)
+ return (error);
+
if (uap->com >= PROC_PROCCTL_MD_MIN)
return (cpu_procctl(td, uap->idtype, uap->id,
uap->com, uap->data));
diff --git a/sys/kern/syscalls.master b/sys/kern/syscalls.master
--- a/sys/kern/syscalls.master
+++ b/sys/kern/syscalls.master
@@ -3023,7 +3023,7 @@
_In_ _Contains_long_ptr_ struct aiocb *aiocbp
);
}
-544 AUE_PROCCTL STD {
+544 AUE_PROCCTL STD|CAPENABLED {
int procctl(
idtype_t idtype,
id_t id,
diff --git a/sys/sys/syscallsubr.h b/sys/sys/syscallsubr.h
--- a/sys/sys/syscallsubr.h
+++ b/sys/sys/syscallsubr.h
@@ -428,6 +428,8 @@
int options, struct __wrusage *wrusage, siginfo_t *sip);
int kern_procctl(struct thread *td, enum idtype idtype, id_t id, int com,
void *data);
+int kern_procctl_check_perm(struct thread *td, enum idtype idtype,
+ id_t id, int com);
int kern_profil(struct thread *td, char *samples, size_t size,
size_t offset, u_int scale);
int kern_pread(struct thread *td, int fd, void *buf, size_t nbyte,
diff --git a/tests/sys/capsicum/Makefile b/tests/sys/capsicum/Makefile
--- a/tests/sys/capsicum/Makefile
+++ b/tests/sys/capsicum/Makefile
@@ -4,6 +4,7 @@
ATF_TESTS_C+= bindat_connectat
ATF_TESTS_C+= ioctls_test
+ATF_TESTS_C+= procctl_test
CFLAGS+= -I${SRCTOP}/tests
diff --git a/tests/sys/capsicum/procctl_test.c b/tests/sys/capsicum/procctl_test.c
new file mode 100644
--- /dev/null
+++ b/tests/sys/capsicum/procctl_test.c
@@ -0,0 +1,299 @@
+/*
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * Copyright (c) 2026 The FreeBSD Foundation
+ *
+ * This software was developed by Konstantin Belousov <kib@FreeBSD.org>
+ * under sponsorship from the FreeBSD Foundation.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include <sys/param.h>
+#include <sys/capsicum.h>
+#include <sys/procctl.h>
+#include <sys/wait.h>
+
+#include <errno.h>
+#include <signal.h>
+#include <string.h>
+#include <unistd.h>
+
+#include <atf-c.h>
+
+#include "freebsd_test_suite/macros.h"
+
+/*
+ * Fork a child that waits until *wfdp is closed. It serves as a
+ * target that is not the calling process.
+ */
+static pid_t
+spawn_target(int *wfdp)
+{
+ pid_t pid;
+ int fds[2];
+ char c;
+
+ ATF_REQUIRE(pipe(fds) == 0);
+ pid = fork();
+ ATF_REQUIRE(pid != -1);
+ if (pid == 0) {
+ close(fds[1]);
+ (void)read(fds[0], &c, 1);
+ _exit(0);
+ }
+ close(fds[0]);
+ *wfdp = fds[1];
+ return (pid);
+}
+
+static void
+reap_target(pid_t pid, int wfd)
+{
+ int status;
+
+ ATF_REQUIRE(close(wfd) == 0);
+ ATF_REQUIRE(waitpid(pid, &status, 0) == pid);
+ ATF_REQUIRE(WIFEXITED(status) && WEXITSTATUS(status) == 0);
+}
+
+/*
+ * Status queries on the calling process work in capability mode, with
+ * both the zero shortcut and the explicit process ID.
+ */
+ATF_TC_WITHOUT_HEAD(capmode_self_status);
+ATF_TC_BODY(capmode_self_status, tc)
+{
+ struct procctl_reaper_status rs;
+ pid_t ids[2];
+ u_int i;
+ int st;
+
+ ATF_REQUIRE_FEATURE("security_capability_mode");
+ ATF_REQUIRE(cap_enter() == 0);
+
+ ids[0] = 0;
+ ids[1] = getpid();
+ for (i = 0; i < nitems(ids); i++) {
+ ATF_REQUIRE(procctl(P_PID, ids[i], PROC_TRACE_STATUS,
+ &st) == 0);
+ ATF_REQUIRE(procctl(P_PID, ids[i], PROC_TRAPCAP_STATUS,
+ &st) == 0);
+ ATF_REQUIRE(procctl(P_PID, ids[i], PROC_PDEATHSIG_STATUS,
+ &st) == 0);
+ ATF_REQUIRE(procctl(P_PID, ids[i], PROC_ASLR_STATUS,
+ &st) == 0);
+ ATF_REQUIRE(procctl(P_PID, ids[i], PROC_PROTMAX_STATUS,
+ &st) == 0);
+ ATF_REQUIRE(procctl(P_PID, ids[i], PROC_STACKGAP_STATUS,
+ &st) == 0);
+ ATF_REQUIRE(procctl(P_PID, ids[i], PROC_NO_NEW_PRIVS_STATUS,
+ &st) == 0);
+ ATF_REQUIRE(procctl(P_PID, ids[i], PROC_WXMAP_STATUS,
+ &st) == 0);
+ ATF_REQUIRE(procctl(P_PID, ids[i], PROC_LOGSIGEXIT_STATUS,
+ &st) == 0);
+ ATF_REQUIRE(procctl(P_PID, ids[i], PROC_REAP_STATUS,
+ &rs) == 0);
+ }
+}
+
+/*
+ * Control requests on the calling process work in capability mode.
+ */
+ATF_TC_WITHOUT_HEAD(capmode_self_ctl);
+ATF_TC_BODY(capmode_self_ctl, tc)
+{
+ int arg, st;
+
+ ATF_REQUIRE_FEATURE("security_capability_mode");
+ ATF_REQUIRE(cap_enter() == 0);
+
+ arg = SIGUSR1;
+ ATF_REQUIRE(procctl(P_PID, 0, PROC_PDEATHSIG_CTL, &arg) == 0);
+ ATF_REQUIRE(procctl(P_PID, getpid(), PROC_PDEATHSIG_STATUS,
+ &st) == 0);
+ ATF_REQUIRE_EQ(st, SIGUSR1);
+ arg = 0;
+ ATF_REQUIRE(procctl(P_PID, getpid(), PROC_PDEATHSIG_CTL, &arg) == 0);
+ ATF_REQUIRE(procctl(P_PID, 0, PROC_PDEATHSIG_STATUS, &st) == 0);
+ ATF_REQUIRE_EQ(st, 0);
+
+ arg = PROC_TRAPCAP_CTL_ENABLE;
+ ATF_REQUIRE(procctl(P_PID, 0, PROC_TRAPCAP_CTL, &arg) == 0);
+ ATF_REQUIRE(procctl(P_PID, 0, PROC_TRAPCAP_STATUS, &st) == 0);
+ ATF_REQUIRE_EQ(st, PROC_TRAPCAP_CTL_ENABLE);
+ arg = PROC_TRAPCAP_CTL_DISABLE;
+ ATF_REQUIRE(procctl(P_PID, getpid(), PROC_TRAPCAP_CTL, &arg) == 0);
+ ATF_REQUIRE(procctl(P_PID, getpid(), PROC_TRAPCAP_STATUS, &st) == 0);
+ ATF_REQUIRE_EQ(st, PROC_TRAPCAP_CTL_DISABLE);
+
+ arg = PROC_NO_NEW_PRIVS_ENABLE;
+ ATF_REQUIRE(procctl(P_PID, 0, PROC_NO_NEW_PRIVS_CTL, &arg) == 0);
+ ATF_REQUIRE(procctl(P_PID, 0, PROC_NO_NEW_PRIVS_STATUS, &st) == 0);
+ ATF_REQUIRE_EQ(st, PROC_NO_NEW_PRIVS_ENABLE);
+}
+
+/*
+ * The reaper and OOM protection requests are refused in capability
+ * mode even for the calling process.
+ */
+ATF_TC_WITHOUT_HEAD(capmode_self_denied);
+ATF_TC_BODY(capmode_self_denied, tc)
+{
+ struct procctl_reaper_kill rk;
+ struct procctl_reaper_pids rp;
+ pid_t ids[2];
+ u_int i;
+ int flags;
+
+ ATF_REQUIRE_FEATURE("security_capability_mode");
+ ATF_REQUIRE(cap_enter() == 0);
+
+ ids[0] = 0;
+ ids[1] = getpid();
+ for (i = 0; i < nitems(ids); i++) {
+ flags = PPROT_SET;
+ ATF_REQUIRE_ERRNO(ECAPMODE,
+ procctl(P_PID, ids[i], PROC_SPROTECT, &flags) == -1);
+ ATF_REQUIRE_ERRNO(ECAPMODE,
+ procctl(P_PID, ids[i], PROC_REAP_ACQUIRE, NULL) == -1);
+ ATF_REQUIRE_ERRNO(ECAPMODE,
+ procctl(P_PID, ids[i], PROC_REAP_RELEASE, NULL) == -1);
+ memset(&rp, 0, sizeof(rp));
+ ATF_REQUIRE_ERRNO(ECAPMODE,
+ procctl(P_PID, ids[i], PROC_REAP_GETPIDS, &rp) == -1);
+ /*
+ * Signal 0 is invalid for PROC_REAP_KILL, so the request
+ * cannot kill anything even if it were not refused.
+ */
+ memset(&rk, 0, sizeof(rk));
+ ATF_REQUIRE_ERRNO(ECAPMODE,
+ procctl(P_PID, ids[i], PROC_REAP_KILL, &rk) == -1);
+ }
+}
+
+/*
+ * Other processes and process groups cannot be targeted in capability
+ * mode.
+ */
+ATF_TC_WITHOUT_HEAD(capmode_other_denied);
+ATF_TC_BODY(capmode_other_denied, tc)
+{
+ pid_t pgid, pid;
+ int st, wfd;
+
+ ATF_REQUIRE_FEATURE("security_capability_mode");
+ pid = spawn_target(&wfd);
+ pgid = getpgrp();
+
+ /* Outside capability mode, the target is reachable. */
+ ATF_REQUIRE(procctl(P_PID, pid, PROC_TRACE_STATUS, &st) == 0);
+
+ ATF_REQUIRE(cap_enter() == 0);
+ ATF_REQUIRE_ERRNO(ECAPMODE,
+ procctl(P_PID, pid, PROC_TRACE_STATUS, &st) == -1);
+ ATF_REQUIRE_ERRNO(ECAPMODE,
+ procctl(P_PID, pid, PROC_TRAPCAP_STATUS, &st) == -1);
+ ATF_REQUIRE_ERRNO(ECAPMODE,
+ procctl(P_PID, getppid(), PROC_TRACE_STATUS, &st) == -1);
+ ATF_REQUIRE_ERRNO(ECAPMODE,
+ procctl(P_PGID, pgid, PROC_TRACE_STATUS, &st) == -1);
+
+ reap_target(pid, wfd);
+}
+
+/*
+ * Machine-dependent requests are refused for other processes. On
+ * amd64, the status requests work for the calling process.
+ */
+ATF_TC_WITHOUT_HEAD(capmode_md);
+ATF_TC_BODY(capmode_md, tc)
+{
+ pid_t pid;
+ int st, wfd;
+
+ ATF_REQUIRE_FEATURE("security_capability_mode");
+ pid = spawn_target(&wfd);
+ ATF_REQUIRE(cap_enter() == 0);
+
+ /* An invalid argument, should the request get through. */
+ st = 0;
+ ATF_REQUIRE_ERRNO(ECAPMODE,
+ procctl(P_PID, pid, PROC_PROCCTL_MD_MIN, &st) == -1);
+#ifdef __amd64__
+ ATF_REQUIRE_ERRNO(ECAPMODE,
+ procctl(P_PID, pid, PROC_LA_STATUS, &st) == -1);
+ ATF_REQUIRE(procctl(P_PID, 0, PROC_LA_STATUS, &st) == 0);
+ ATF_REQUIRE(procctl(P_PID, getpid(), PROC_LA_STATUS, &st) == 0);
+ ATF_REQUIRE(procctl(P_PID, 0, PROC_KPTI_STATUS, &st) == 0);
+#endif
+
+ reap_target(pid, wfd);
+}
+
+static volatile sig_atomic_t trapped;
+
+static void
+sigtrap_handler(int sig __unused)
+{
+ trapped = 1;
+}
+
+/*
+ * A refused request delivers SIGTRAP when PROC_TRAPCAP_CTL is enabled.
+ */
+ATF_TC_WITHOUT_HEAD(capmode_trapcap);
+ATF_TC_BODY(capmode_trapcap, tc)
+{
+ struct sigaction sa;
+ int arg, st;
+
+ ATF_REQUIRE_FEATURE("security_capability_mode");
+ memset(&sa, 0, sizeof(sa));
+ sa.sa_handler = sigtrap_handler;
+ ATF_REQUIRE(sigemptyset(&sa.sa_mask) == 0);
+ ATF_REQUIRE(sigaction(SIGTRAP, &sa, NULL) == 0);
+
+ ATF_REQUIRE(cap_enter() == 0);
+ arg = PROC_TRAPCAP_CTL_ENABLE;
+ ATF_REQUIRE(procctl(P_PID, 0, PROC_TRAPCAP_CTL, &arg) == 0);
+ ATF_REQUIRE_EQ(trapped, 0);
+ ATF_REQUIRE_ERRNO(ECAPMODE,
+ procctl(P_PID, getppid(), PROC_TRACE_STATUS, &st) == -1);
+ ATF_REQUIRE_EQ(trapped, 1);
+
+ arg = PROC_TRAPCAP_CTL_DISABLE;
+ ATF_REQUIRE(procctl(P_PID, 0, PROC_TRAPCAP_CTL, &arg) == 0);
+}
+
+ATF_TP_ADD_TCS(tp)
+{
+ ATF_TP_ADD_TC(tp, capmode_self_status);
+ ATF_TP_ADD_TC(tp, capmode_self_ctl);
+ ATF_TP_ADD_TC(tp, capmode_self_denied);
+ ATF_TP_ADD_TC(tp, capmode_other_denied);
+ ATF_TP_ADD_TC(tp, capmode_md);
+ ATF_TP_ADD_TC(tp, capmode_trapcap);
+
+ return (atf_no_error());
+}
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Mon, Nov 23, 12:49 PM (19 h, 19 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
41931535
Default Alt Text
D61919.1795438192.diff (25 KB)
Attached To
Mode
D61919: Enable procctl(2) in cap mode for itself
Attached
Detach File
Event Timeline