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" . 137Because jail is implemented using IP aliases, one of the first things to do 138is to disable IP services on the host system that listen on all local 139IP addresses for a service. 140This means changing 141.Xr inetd 8 142to only listen on the 143appropriate IP address, and so forth. 144Add the following to 145.Pa /etc/rc.conf 146in the host environment: 147.Bd -literal -offset indent 148sendmail_enable="NO" 149inetd_flags="-wW -a 192.168.11.23" 150rpcbind_enable="NO" 151.Ed 152.Pp 153.Li 192.168.11.23 154is the native IP address for the host system, in this example. 155Daemons that run out of 156.Xr inetd 8 157can be easily set to use only the specified host IP address. 158Other daemons 159will need to be manually configured\(emfor some this is possible through 160the 161.Xr rc.conf 5 162flags entries, for others it is not possible without munging 163the per-application configuration files, or even recompiling. 164For those 165applications that cannot specify the IP they run on, it is better to disable 166them, if possible. 167.Pp 168A number of daemons ship with the base system that may have problems when 169run from outside of a jail in a jail-centric environment. 170This includes 171.Xr sendmail 8 , 172.Xr named 8 , 173and 174.Xr rpcbind 8 . 175While 176.Xr sendmail 8 177and 178.Xr named 8 179can be configured to listen only on a specific 180IP using their configuration files, in most cases it is easier to simply 181run the daemons in jails only, and not in the host environment. 182Attempting to serve 183NFS from the host environment may also cause confusion, and cannot be 184easily reconfigured to use only specific IPs, as some NFS services are 185hosted directly from the kernel. 186Any third party network software running 187in the host environment should also be checked and configured so that it 188does not bind all IP addresses, which would result in those services also 189appearing to be offered by the jail environments. 190.Pp 191Once 192these daemons have been disabled or fixed in the host environment, it is 193best to reboot so that all daemons are in a known state, to reduce the 194potential for confusion later (such as finding that when you send mail 195to a jail, and its sendmail is down, the mail is delivered to the host, 196etc.) 197.Pp 198Start any jails for the first time without configuring the network 199interface so that you can clean it up a little and set up accounts. 200As 201with any machine (virtual or not) you will need to set a root password, time 202zone, etc. 203.Pp 204Now start the jail: 205.Pp 206.Dl "jail /data/jail/192.168.11.100 testhostname 127.0.0.1,192.168.11.100 /bin/sh" 207.Pp 208You will end up with a shell prompt, assuming no errors, within the jail. 209You can now do the post-install configuration to set various configuration 210options by editing 211.Pa /etc/rc.conf , 212etc. 213.Pp 214.Bl -bullet -offset indent -compact 215.It 216Disable the port mapper 217.Pa ( /etc/rc.conf : 218.Li rpcbind_enable="NO" ) 219.It 220Run 221.Xr newaliases 1 222to quell 223.Xr sendmail 8 224warnings. 225.It 226Disable interface configuration to quell startup warnings about 227.Xr ifconfig 8 228.Pq Li network_interfaces="" 229.It 230Configure 231.Pa /etc/resolv.conf 232so that name resolution within the jail will work correctly 233.It 234Set a root password, probably different from the real host system 235.It 236Set the timezone with 237.Xr tzsetup 8 238.It 239Add accounts for users in the jail environment 240.It 241Install any packages that you think the environment requires 242.El 243.Pp 244You may also want to perform any package-specific configuration (web servers, 245SSH servers, etc), patch up 246.Pa /etc/syslog.conf 247so it logs as you would like, etc. 248.Pp 249Exit from the shell, and the jail will be shut down. 250.Ss "Starting the Jail" 251You are now ready to restart the jail and bring up the environment with 252all of its daemons and other programs. 253To do this, first bring up the 254virtual host interface, and then start the jail's 255.Pa /etc/rc 256script from within the jail. 257.Pp 258NOTE: If you plan to allow untrusted users to have root access inside the 259jail, you may wish to consider setting the 260.Va jail.defaults.set_hostname_allowed 261to 0. 262Please see the management reasons why this is a good idea. 263If you do decide to set this variable, 264it must be set before starting any jails, and once each boot. 265.Bd -literal -offset indent 266ifconfig ed0 inet alias 192.168.11.100/32 267mount -t procfs proc /data/jail/192.168.11.100/proc 268jail /data/jail/192.168.11.100 testhostname 127.0.0.1,192.168.11.100 \\ 269 /bin/sh /etc/rc 270.Ed 271.Pp 272A few warnings will be produced, because most 273.Xr sysctl 8 274configuration variables cannot be set from within the jail, as they are 275global across all jails and the host environment. 276However, it should all 277work properly. 278You should be able to see 279.Xr inetd 8 , 280.Xr syslogd 8 , 281and other processes running within the jail using 282.Xr ps 1 , 283with the 284.Ql J 285flag appearing beside jailed processes. 286You should also be able to 287.Xr telnet 1 288to the hostname or IP address of the jailed environment, and log 289in using the accounts you created previously. 290.Ss "Managing the Jail" 291Normal machine shutdown commands, such as 292.Xr halt 8 , 293.Xr reboot 8 , 294and 295.Xr shutdown 8 , 296cannot be used successfully within the jail. 297To kill all processes in a 298jail, you may log into the jail and, as root, use one of the following 299commands, depending on what you want to accomplish: 300.Bd -literal -offset indent 301kill -TERM -1 302kill -KILL -1 303.Ed 304.Pp 305This will send the 306.Dv SIGTERM 307or 308.Dv SIGKILL 309signals to all processes in the jail from within the jail. 310Depending on 311the intended use of the jail, you may also want to run 312.Pa /etc/rc.shutdown 313from within the jail. 314Currently there is no way to insert new processes 315into a jail, so you must first log into the jail before performing these 316actions. 317.Pp 318To kill processes from outside the jail, you must individually identify the 319PID of each process to be killed. 320The 321.Pa /proc/ Ns Ar pid Ns Pa /status 322file contains, as its last field, the hostname of the jail in which the 323process runs, or 324.Dq Li - 325to indicate that the process is not running within a jail. 326The 327.Xr ps 1 328command also shows a 329.Ql J 330flag for processes in a jail. 331However, the hostname for a jail may be, by 332default, modified from within the jail, so the 333.Pa /proc 334status entry is unreliable by default. 335To disable the setting of the hostname 336from within a jail, set the 337.Va jail.set_hostname_allowed 338sysctl variable in the host environment to 0, which will affect all jails. 339You can have this sysctl set on each boot using 340.Xr sysctl.conf 5 . 341Just add the following line to 342.Pa /etc/sysctl.conf : 343.Pp 344.Dl jail.defaults.set_hostname_allowed=0 345.Pp 346In a future version of 347.Dx , 348the mechanisms for managing jails may be 349more refined. 350.Ss "Sysctl MIB Entries" 351Certain aspects of the jail containments environment may be modified from 352the host environment using 353.Xr sysctl 8 354MIB variables. 355For each jail there will be the same set of MIB variables as shown below but 356under 357.Va jail.<id> 358which allows control of every jail individually. 359The values of the variables under 360.Va jail.defaults 361will be copied to the per-jail MIB variables upon creation thus serving as 362a kind of system-wide template. 363.Bl -tag -width XXX 364.It Va jail.defaults.allow_raw_sockets 365This MIB entry determines whether or not prison root is allowed to 366create raw sockets. 367Setting this MIB to 1 allows utilities like 368.Xr ping 8 369and 370.Xr traceroute 8 371to operate inside the prison. 372If this MIB is set, the source IP addresses are enforced to comply 373with the IP address bound to the jail, regardless of whether or not the 374.Dv IP_HDRINCL 375flag has been set on the socket. Because raw sockets can be used to configure 376and interact with various network subsystems, extra caution should be used 377where privileged access to jails is given out to untrusted parties. 378As such, this option is disabled by default. 379.It Va jail.defaults.chflags_allowed 380This MIB entry determines how a privileged user inside a jail will be 381treated by 382.Xr chflags 2 . 383If zero, such users are treated as unprivileged, and are unable to set 384or clear system file flags; if non-zero, such users are treated as 385privileged, and may manipulate system file flags subject to the usual 386constraints on 387.Va kern.securelevel . 388.It Va jail.jailed 389This read-only MIB entry can be used to determine if a process is running 390inside a jail (value is 1) or not (value is 0). 391.It Va jail.defaults.set_hostname_allowed 392This MIB entry determines whether or not processes within a jail are 393allowed to change their hostname via 394.Xr hostname 1 395or 396.Xr sethostname 3 . 397In the current jail implementation, the ability to set the hostname from 398within the jail can impact management tools relying on the accuracy of jail 399information in 400.Pa /proc . 401As such, this should be disabled in environments where privileged access to 402jails is given out to untrusted parties. 403.It Va jail.defaults.socket_unixiproute_only 404The jail functionality binds IPv4 and IPv6 addresses to each jail, and limits 405access to other network addresses in the IPv4 and IPv6 space that may be available 406in the host environment. 407However, jail is not currently able to limit access to other network 408protocol stacks that have not had jail functionality added to them. 409As such, by default, processes within jails may only access protocols 410in the following domains: 411.Dv PF_LOCAL , PF_INET , PF_INET6 , 412and 413.Dv PF_ROUTE , 414permitting them access to 415.Ux 416domain sockets, 417IPv4 and IPv6 addresses, and routing sockets. 418To enable access to other domains, this MIB variable may be set to 0. 419.It Va jail.defaults.sysvipc_allowed 420This MIB entry determines whether or not processes within a jail have access 421to System V IPC primitives. 422In the current jail implementation, System V primitives share a single 423namespace across the host and jail environments, meaning that processes 424within a jail would be able to communicate with (and potentially interfere 425with) processes outside of the jail, and in other jails. 426As such, this functionality is disabled by default, but can be enabled 427by setting this MIB entry to 1. 428.El 429.Sh SEE ALSO 430.Xr newaliases 1 , 431.Xr ps 1 , 432.Xr chroot 2 , 433.Xr jail 2 , 434.Xr procfs 5 , 435.Xr rc.conf 5 , 436.Xr sysctl.conf 5 , 437.Xr halt 8 , 438.Xr inetd 8 , 439.Xr named 8 , 440.Xr pw 8 , 441.Xr reboot 8 , 442.Xr rpcbind 8 , 443.Xr sendmail 8 , 444.Xr shutdown 8 , 445.Xr sysctl 8 , 446.Xr syslogd 8 , 447.Xr tzsetup 8 448.Sh HISTORY 449The 450.Nm 451command appeared in 452.Fx 4.0 . 453.Pp 454Support for multiple IPs and IPv6 appeared in 455.Dx 4561.7. 457.Sh AUTHORS 458.An -nosplit 459The jail feature was written by 460.An Poul-Henning Kamp 461for R&D Associates 462.Pa http://www.rndassociates.com/ 463who contributed it to 464.Fx . 465.Pp 466.An Robert Watson 467wrote the extended documentation, found a few bugs, added 468a few new features, and cleaned up the userland jail environment. 469.Pp 470.An Victor Balada Diaz 471wrote the support for multiple IPs and IPv6. Multiple IPs support 472is based on work done by 473.An Pawel Jakub Dawidek . 474.Sh BUGS 475Jail currently lacks strong management functionality, such as the ability 476to deliver signals to all processes in a jail, and to allow access to 477specific jail information via 478.Xr ps 1 479as opposed to 480.Xr procfs 5 . 481Similarly, it might be a good idea to add an 482address alias flag such that daemons listening on all IPs 483.Pq Dv INADDR_ANY 484will not bind on that address, which would facilitate building a safe 485host environment such that host daemons do not impose on services offered 486from within jails. 487Currently, the simplist answer is to minimize services 488offered on the host, possibly limiting it to services offered from 489.Xr inetd 8 490which is easily configurable. 491