1.\" 2.\" Copyright (c) 2008-2009 Robert N. M. Watson 3.\" All rights reserved. 4.\" 5.\" This software was developed at the University of Cambridge Computer 6.\" Laboratory with support from a grant from Google, Inc. 7.\" 8.\" Redistribution and use in source and binary forms, with or without 9.\" modification, are permitted provided that the following conditions 10.\" are met: 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. 16.\" 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 27.\" SUCH DAMAGE. 28.\" 29.\" $FreeBSD$ 30.\" 31.Dd March 9, 2023 32.Dt CAP_ENTER 2 33.Os 34.Sh NAME 35.Nm cap_enter , 36.Nm cap_getmode 37.Nd Capability mode system calls 38.Sh LIBRARY 39.Lb libc 40.Sh SYNOPSIS 41.In sys/capsicum.h 42.Ft int 43.Fn cap_enter "void" 44.Ft int 45.Fn cap_getmode "u_int *modep" 46.Sh DESCRIPTION 47.Fn cap_enter 48places the current process into capability mode, a mode of execution in which 49processes may only issue system calls operating on file descriptors or 50reading limited global system state. 51Access to global name spaces, such as file system or IPC name spaces, is 52prevented. 53If the process is already in a capability mode sandbox, the system call is a 54no-op. 55Future process descendants created with 56.Xr fork 2 57or 58.Xr pdfork 2 59will be placed in capability mode from inception. 60.Pp 61When combined with 62.Xr cap_rights_limit 2 , 63.Xr cap_ioctls_limit 2 , 64.Xr cap_fcntls_limit 2 , 65.Fn cap_enter 66may be used to create kernel-enforced sandboxes in which 67appropriately-crafted applications or application components may be run. 68.Pp 69.Fn cap_getmode 70returns a flag indicating whether or not the process is in a capability mode 71sandbox. 72.Sh RUN-TIME SETTINGS 73If the 74.Dv kern.trap_enotcap 75sysctl MIB is set to a non-zero value, then for any process executing in a 76capability mode sandbox, any syscall which results in either an 77.Er ENOTCAPABLE 78or 79.Er ECAPMODE 80error also generates the synchronous 81.Dv SIGTRAP 82signal to the thread on the syscall return. 83On signal delivery, the 84.Va si_errno 85member of the 86.Fa siginfo 87signal handler parameter is set to the syscall error value, 88and the 89.Va si_code 90member is set to 91.Dv TRAP_CAP . 92.Pp 93See also the 94.Dv PROC_TRAPCAP_CTL 95and 96.Dv PROC_TRAPCAP_STATUS 97operations of the 98.Xr procctl 2 99function for similar per-process functionality. 100.Sh RETURN VALUES 101.Rv -std cap_enter cap_getmode 102.Pp 103When the process is in capability mode, 104.Fn cap_getmode 105sets the flag to a non-zero value. 106A zero value means the process is not in capability mode. 107.Sh ERRORS 108The 109.Fn cap_enter 110and 111.Fn cap_getmode 112system calls 113will fail if: 114.Bl -tag -width Er 115.It Bq Er ENOSYS 116The running kernel was compiled without 117.Cd "options CAPABILITY_MODE" . 118.El 119.Pp 120The 121.Fn cap_getmode 122system call may also return the following error: 123.Bl -tag -width Er 124.It Bq Er EFAULT 125Pointer 126.Fa modep 127points outside the process's allocated address space. 128.El 129.Sh SEE ALSO 130.Xr cap_fcntls_limit 2 , 131.Xr cap_ioctls_limit 2 , 132.Xr cap_rights_limit 2 , 133.Xr fexecve 2 , 134.Xr procctl 2 , 135.Xr cap_sandboxed 3 , 136.Xr capsicum 4 , 137.Xr sysctl 9 138.Sh HISTORY 139The 140.Fn cap_getmode 141system call first appeared in 142.Fx 8.3 . 143Support for capabilities and capabilities mode was developed as part of the 144.Tn TrustedBSD 145Project. 146.Sh AUTHORS 147These functions and the capability facility were created by 148.An "Robert N. M. Watson" 149at the University of Cambridge Computer Laboratory with support from a grant 150from Google, Inc. 151.Sh CAVEATS 152Creating effective process sandboxes is a tricky process that involves 153identifying the least possible rights required by the process and then 154passing those rights into the process in a safe manner. 155Consumers of 156.Fn cap_enter 157should also be aware of other inherited rights, such as access to VM 158resources, memory contents, and other process properties that should be 159considered. 160It is advisable to use 161.Xr fexecve 2 162to create a runtime environment inside the sandbox that has as few implicitly 163acquired rights as possible. 164