1.\" 2.\" Copyright (c) 2000 Robert N. M. Watson 3.\" All rights reserved. 4.\" 5.\" Redistribution and use in source and binary forms, with or without 6.\" modification, are permitted provided that the following conditions 7.\" are met: 8.\" 1. Redistributions of source code must retain the above copyright 9.\" notice, this list of conditions and the following disclaimer. 10.\" 2. Redistributions in binary form must reproduce the above copyright 11.\" notice, this list of conditions and the following disclaimer in the 12.\" documentation and/or other materials provided with the distribution. 13.\" 14.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17.\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24.\" SUCH DAMAGE. 25.\" 26.\" 27.\" ---------------------------------------------------------------------------- 28.\" "THE BEER-WARE LICENSE" (Revision 42): 29.\" <phk@FreeBSD.ORG> wrote this file. As long as you retain this notice you 30.\" can do whatever you want with this stuff. If we meet some day, and you think 31.\" this stuff is worth it, you can buy me a beer in return. Poul-Henning Kamp 32.\" ---------------------------------------------------------------------------- 33.\" 34.\" 35.Dd August 28, 2019 36.Dt JAIL 8 37.Os 38.Sh NAME 39.Nm jail 40.Nd "imprison process and its descendants" 41.Sh SYNOPSIS 42.Nm 43.Op Fl i 44.Op Fl l u Ar username | Fl U Ar username 45.Ar path hostname ip-list command ... 46.Sh DESCRIPTION 47The 48.Nm 49command imprisons a process and all future descendants. 50.Pp 51The options are as follows: 52.Bl -tag -width ".Fl u Ar username" 53.It Fl i 54Output the jail identifier of the newly created jail. 55.It Fl l 56Run program in the clean environment. 57The environment is discarded except for 58.Ev HOME , SHELL , TERM 59and 60.Ev USER . 61.Ev HOME 62and 63.Ev SHELL 64are set to the target login's default values. 65.Ev USER 66is set to the target login. 67.Ev TERM 68is imported from the current environment. 69The environment variables from the login class capability database for the 70target login are also set. 71.It Fl u Ar username 72The user name as whom the 73.Ar command 74should run. 75.It Fl U Ar username 76The user name from jailed environment as whom the 77.Ar command 78should run. 79.It Ar path 80Directory which is to be the root of the prison. 81.It Ar hostname 82Hostname of the prison. 83.It Ar ip-list 84Comma separated IP list assigned to the prison. 85.It Ar command 86Pathname of the program which is to be executed. 87.El 88.Pp 89Please see the 90.Xr jail 2 91man page for further details. 92.Sh EXAMPLES 93.Ss "Setting Up a Jail Directory Tree" 94This example shows how to setup a jail directory tree 95containing an entire 96.Dx 97distribution, provided that you built world before already: 98.Bd -literal 99D=/here/is/the/jail 100cd /usr/src 101mkdir -p $D 102make installworld DESTDIR=$D 103cd etc 104make distribution DESTDIR=$D 105cd $D 106ln -sf dev/null boot/kernel 107.Ed 108.Pp 109In many cases this example would put far more stuff in the jail than is needed. 110In the other extreme case a jail might contain only one single file: 111the executable to be run in the jail. 112.Pp 113We recommend experimentation and caution that it is a lot easier to 114start with a 115.Dq fat 116jail and remove things until it stops working, 117than it is to start with a 118.Dq thin 119jail and add things until it works. 120.Ss "Setting Up a Jail" 121Do what was described in 122.Sx "Setting Up a Jail Directory Tree" 123to build the jail directory tree. 124For the sake of this example, we will 125assume you built it in 126.Pa /data/jail/192.168.11.100 , 127named for the external jail IP address. 128Substitute below as needed with your 129own directory, IP addresses, and hostname. 130.Pp 131First, you will want to set up your real system's environment to be 132.Dq jail-friendly . 133For consistency, we will refer to the parent box as the 134.Dq "host environment" , 135and to the jailed virtual machine as the 136.Dq "jail environment" . 137Generally speaking making a system jail-friendly does not require much 138work if you have 139.Va jail.defaults.allow_listen_override 140set to 1. 141This will allow all listen sockets inside the jail to overload 142and override wildcard listen sockets on the host. 143This methodology works extremely well as long as you restrict the IPs you 144pass into the jail to avoid any security concerns, which typically means 145making at least two IP aliases for each jail that you create (an external 146IP and a localhost IP). 147.Pp 148If you do not want to allow listener socket overloading, you must modify 149services you intend to leave running on the host system to listen only on 150specific IPs and not all IPs, or generally only run services inside your 151jails that do not conflict with services on the host system. 152This can be difficult and is not recommended. 153.Pp 154Common services include: 155.Xr inetd 8 , 156.Xr sendmail 8 , 157.Xr named 8 , 158.Xr rpcbind 8 , 159.Xr mountd 8 , 160.Xr nfsd 8 , 161.Xr sendmail 1 , 162and 163.Xr named 8 . 164.Pp 165For the purposes of our examples below, 166.Li 192.168.11.1 167is the host IP and we create aliases 168.Li 192.168.11.X 169for each jail. 170In addition, we leave 171.Li 127.0.0.1 172on the host only and create aliases 173.Li 127.0.0.X 174for each jail. 175.Pp 176Start any jails for the first time without configuring the network 177interface so that you can clean it up a little and set up accounts. 178As 179with any machine (virtual or not) you will need to set a root password, time 180zone, etc. 181.Pp 182To set the jail up for real you need to ifconfig some interface aliases. 183and set jail defaults. Here is an example: 184.Pp 185.Dl "sysctl jail.defaults.net_raw_sockets=1" 186.Dl "sysctl jail.defaults.allow_listen_override=1" 187.Dl "ifconfig lo0 127.0.0.2 alias" 188.Dl "ifconfig em0 192.168.11.100 netmask 255.255.255.0 alias" 189.Pp 190From within your jail you can generally run services normally. Just use 191'localhost' normally. Do not inform services that localhost is 127.0.0.2. 192The kernel will automatically remap 'localhost' (e.g. 127.0.0.1) to the 193localhost IP you specify for the jail. The kernel will also map the jail's 194localhost IP back to 127.0.0.1 in the 195.Xr accept 2 , 196.Xr getsockname 2 , 197and 198.Xr getpeername 2 199system calls. 200.Pp 201Listen sockets can be overloaded between jails and between the host and its 202jails. You can continue to use services that listen on the wildcard *.* 203socket normally on both the host and its jails if you set the 204.Va allow_listen_override 205flag to 1, and jailed listen sockets will override any host listen sockets 206listening on the wildcard address for the allowed jail IPs. 207.Pp 208.Dx 209also allows you to overload specific ports, but the jailed service will 210not receive any connections if the host is also specifically listening 211on the addr/port pair that the jail is using. If multiple jails are listening 212on the same addr/port pair (as specified by the jail's IP list), and the 213host is not, then only one jail will receive connections on that pair. 214.Pp 215In otherwords, it is still a good idea to give each jail its own local 216and non-local IP address rather than have jails share. 217.Pp 218Now start the jail: 219.Pp 220.Dl "jail /data/jail/192.168.11.100 testhostname 127.0.0.2,192.168.11.100 /bin/sh" 221.Pp 222You will end up with a shell prompt, assuming no errors, within the jail. 223You can now do the post-install configuration to set various configuration 224options by editing 225.Pa /etc/rc.conf , 226etc. 227.Pp 228.Bl -bullet -offset indent -compact 229.It 230Disable the port mapper 231.Pa ( /etc/rc.conf : 232.Li rpcbind_enable="NO" ) 233.It 234Run 235.Xr newaliases 1 236to quell 237.Xr sendmail 8 238warnings. 239.It 240Disable interface configuration to quell startup warnings about 241.Xr ifconfig 8 242.Pq Li network_interfaces="" 243.It 244Configure 245.Pa /etc/resolv.conf 246so that name resolution within the jail will work correctly 247.It 248Set a root password, probably different from the real host system 249.It 250Set the timezone with 251.Xr tzsetup 8 252.It 253Add accounts for users in the jail environment 254.It 255Install any packages that you think the environment requires 256.El 257.Pp 258You may also want to perform any package-specific configuration (web servers, 259SSH servers, etc), patch up 260.Pa /etc/syslog.conf 261so it logs as you would like, etc. 262.Pp 263Exit from the shell, and the jail will be shut down. 264.Ss "Starting the Jail" 265You are now ready to restart the jail and bring up the environment with 266all of its daemons and other programs. 267To do this, first bring up the 268virtual host interface, and then start the jail's 269.Pa /etc/rc 270script from within the jail. 271.Pp 272NOTE: If you plan to allow untrusted users to have root access inside the 273jail, you may wish to consider setting the 274.Va jail.defaults.set_hostname_allowed 275to 0. 276Please see the management reasons why this is a good idea. 277If you do decide to set this variable, 278it must be set before starting any jails, and once each boot. 279.Bd -literal -offset indent 280sysctl jail.defaults.net_raw_sockets=1 281sysctl jail.defaults.allow_listen_override=1 282ifconfig em0 inet alias 192.168.11.100/32 283ifconfig lo0 inet alias 127.0.0.2 284mount -t procfs proc /data/jail/192.168.11.100/proc 285jail /data/jail/192.168.11.100 testhostname 127.0.0.2,192.168.11.100 \\ 286 /bin/sh /etc/rc 287.Ed 288.Pp 289A few warnings will be produced, because most 290.Xr sysctl 8 291configuration variables cannot be set from within the jail, as they are 292global across all jails and the host environment. 293However, it should all 294work properly. 295You should be able to see 296.Xr inetd 8 , 297.Xr syslogd 8 , 298and other processes running within the jail using 299.Xr ps 1 , 300with the 301.Ql J 302flag appearing beside jailed processes. 303You should also be able to 304.Xr telnet 1 305to the hostname or IP address of the jailed environment, and log 306in using the accounts you created previously. 307.Ss "Managing the Jail" 308Normal machine shutdown commands, such as 309.Xr halt 8 , 310.Xr reboot 8 , 311and 312.Xr shutdown 8 , 313cannot be used successfully within the jail. 314To kill all processes in a 315jail, you may log into the jail and, as root, use one of the following 316commands, depending on what you want to accomplish: 317.Bd -literal -offset indent 318kill -TERM -1 319kill -KILL -1 320.Ed 321.Pp 322This will send the 323.Dv SIGTERM 324or 325.Dv SIGKILL 326signals to all processes in the jail from within the jail. 327Depending on 328the intended use of the jail, you may also want to run 329.Pa /etc/rc.shutdown 330from within the jail. 331Currently there is no way to insert new processes 332into a jail, so you must first log into the jail before performing these 333actions. 334.Pp 335To kill processes from outside the jail, you must individually identify the 336PID of each process to be killed. 337The 338.Pa /proc/ Ns Ar pid Ns Pa /status 339file contains, as its last field, the hostname of the jail in which the 340process runs, or 341.Dq Li - 342to indicate that the process is not running within a jail. 343The 344.Xr ps 1 345command also shows a 346.Ql J 347flag for processes in a jail. 348However, the hostname for a jail may be, by 349default, modified from within the jail, so the 350.Pa /proc 351status entry is unreliable by default. 352To disable the setting of the hostname 353from within a jail, set the 354.Va jail.set_hostname_allowed 355sysctl variable in the host environment to 0, which will affect all jails. 356You can have this sysctl set on each boot using 357.Xr sysctl.conf 5 . 358Just add the following line to 359.Pa /etc/sysctl.conf : 360.Pp 361.Dl jail.defaults.set_hostname_allowed=0 362.Pp 363In a future version of 364.Dx , 365the mechanisms for managing jails may be 366more refined. 367.Ss "Sysctl MIB Entries" 368Certain aspects of the jail containments environment may be modified from 369the host environment using 370.Xr sysctl 8 371MIB variables. 372For each jail there will be the same set of MIB variables as shown below but 373under 374.Va jail.<id> 375which allows control of every jail individually. 376The values of the variables under 377.Va jail.defaults 378will be copied to the per-jail MIB variables upon creation thus serving as 379a kind of system-wide template. 380.Bl -tag -width XXX 381.It Va jail.defaults.allow_raw_sockets 382This MIB entry determines whether or not prison root is allowed to 383create raw sockets. 384Setting this MIB to 1 allows utilities like 385.Xr ping 8 386and 387.Xr traceroute 8 388to operate inside the prison. 389If this MIB is set, the source IP addresses are enforced to comply 390with the IP address bound to the jail, regardless of whether or not the 391.Dv IP_HDRINCL 392flag has been set on the socket. Because raw sockets can be used to configure 393and interact with various network subsystems, extra caution should be used 394where privileged access to jails is given out to untrusted parties. 395As such, this option is disabled by default. 396.It Va jail.defaults.chflags_allowed 397This MIB entry determines how a privileged user inside a jail will be 398treated by 399.Xr chflags 2 . 400If zero, such users are treated as unprivileged, and are unable to set 401or clear system file flags; if non-zero, such users are treated as 402privileged, and may manipulate system file flags subject to the usual 403constraints on 404.Va kern.securelevel . 405.It Va jail.jailed 406This read-only MIB entry can be used to determine if a process is running 407inside a jail (value is 1) or not (value is 0). 408.It Va jail.defaults.set_hostname_allowed 409This MIB entry determines whether or not processes within a jail are 410allowed to change their hostname via 411.Xr hostname 1 412or 413.Xr sethostname 3 . 414In the current jail implementation, the ability to set the hostname from 415within the jail can impact management tools relying on the accuracy of jail 416information in 417.Pa /proc . 418As such, this should be disabled in environments where privileged access to 419jails is given out to untrusted parties. 420.It Va jail.defaults.allow_listen_override 421This feature allows both the host and your jails to overload services on 422the same ports. 423If enabled, the services in the jails will override wildcarded services on 424the host for the jail's IP list. 425As a safety mechanism, any services the host specifically binds to an IP 426will not be overridden. The host has visibility to all jail IPs but 427jails only have visibility to their specific IPs. 428.It Va jail.defaults.socket_unixiproute_only 429The jail functionality binds IPv4 and IPv6 addresses to each jail, and limits 430access to other network addresses in the IPv4 and IPv6 space that may be available 431in the host environment. 432However, jail is not currently able to limit access to other network 433protocol stacks that have not had jail functionality added to them. 434As such, by default, processes within jails may only access protocols 435in the following domains: 436.Dv PF_LOCAL , PF_INET , PF_INET6 , 437and 438.Dv PF_ROUTE , 439permitting them access to 440.Ux 441domain sockets, 442IPv4 and IPv6 addresses, and routing sockets. 443To enable access to other domains, this MIB variable may be set to 0. 444.It Va jail.defaults.sysvipc_allowed 445This MIB entry determines whether or not processes within a jail have access 446to System V IPC primitives. 447In the current jail implementation, System V primitives share a single 448namespace across the host and jail environments, meaning that processes 449within a jail would be able to communicate with (and potentially interfere 450with) processes outside of the jail, and in other jails. 451As such, this functionality is disabled by default, but can be enabled 452by setting this MIB entry to 1. 453.El 454.Sh SEE ALSO 455.Xr newaliases 1 , 456.Xr ps 1 , 457.Xr chroot 2 , 458.Xr jail 2 , 459.Xr procfs 5 , 460.Xr rc.conf 5 , 461.Xr sysctl.conf 5 , 462.Xr halt 8 , 463.Xr inetd 8 , 464.Xr named 8 , 465.Xr pw 8 , 466.Xr reboot 8 , 467.Xr rpcbind 8 , 468.Xr sendmail 8 , 469.Xr shutdown 8 , 470.Xr sysctl 8 , 471.Xr syslogd 8 , 472.Xr tzsetup 8 473.Sh HISTORY 474The 475.Nm 476command appeared in 477.Fx 4.0 . 478.Pp 479Support for multiple IPs and IPv6 appeared in 480.Dx 4811.7. 482.Sh AUTHORS 483.An -nosplit 484The jail feature was originally written by 485.An Poul-Henning Kamp 486for R&D Associates 487.Pa http://www.rndassociates.com/ 488who contributed it to 489.Fx . 490.Pp 491.An Robert Watson 492wrote the extended documentation, found a few bugs, added 493a few new features, and cleaned up the userland jail environment. 494.Pp 495.An Victor Balada Diaz 496wrote the support for multiple IPs and IPv6. Multiple IPs support 497is based on work done by 498.An Pawel Jakub Dawidek . 499.Pp 500.An Matthew Dillon 501added port overloading to make configuration easier. 502.Sh BUGS 503Jail currently lacks strong management functionality, such as the ability 504to deliver signals to all processes in a jail, and to allow access to 505specific jail information via 506.Xr ps 1 507as opposed to 508.Xr procfs 5 . 509Similarly, it might be a good idea to add an 510address alias flag such that daemons listening on all IPs 511.Pq Dv INADDR_ANY 512will not bind on that address, which would facilitate building a safe 513host environment such that host daemons do not impose on services offered 514from within jails. 515Currently, the simplist answer is to minimize services 516offered on the host, possibly limiting it to services offered from 517.Xr inetd 8 518which is easily configurable. 519