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.\" $FreeBSD$ 35.\" 36.Dd April 8, 2003 37.Dt JAIL 8 38.Os 39.Sh NAME 40.Nm jail 41.Nd "imprison process and its descendants" 42.Sh SYNOPSIS 43.Nm 44.Op Fl i 45.Op Fl u Ar username 46.Ar path hostname ip-number command ... 47.Sh DESCRIPTION 48The 49.Nm 50utility imprisons a process and all future descendants. 51.Pp 52The options are as follows: 53.Bl -tag -width ".Fl u Ar username" 54.It Fl i 55Output the jail identifier of the newly created jail. 56.It Fl u Ar username 57The user name as whom the 58.Ar command 59should run. 60.It Ar path 61Directory which is to be the root of the prison. 62.It Ar hostname 63Hostname of the prison. 64.It Ar ip-number 65IP number assigned to the prison. 66.It Ar command 67Pathname of the program which is to be executed. 68.El 69.Pp 70Please see the 71.Xr jail 2 72man page for further details. 73.Sh EXAMPLES 74.Ss "Setting up a Jail Directory Tree" 75This example shows how to setup a jail directory tree 76containing an entire 77.Fx 78distribution: 79.Bd -literal 80D=/here/is/the/jail 81cd /usr/src 82mkdir -p $D 83make world DESTDIR=$D 84cd etc 85make distribution DESTDIR=$D 86mount_devfs devfs $D/dev 87cd $D 88ln -sf dev/null kernel 89.Ed 90.Pp 91NOTE: It is important that only appropriate device nodes in devfs be 92exposed to a jail; access to disk devices in the jail may permit processes 93in the jail to bypass the jail sandboxing by modifying files outside of 94the jail. 95See 96.Xr devfs 8 97for information on how to use devfs rules to limit access to entries 98in the per-jail devfs. 99.Pp 100In many cases this example would put far more stuff in the jail than is needed. 101In the other extreme case a jail might contain only one single file: 102the executable to be run in the jail. 103.Pp 104We recommend experimentation and caution that it is a lot easier to 105start with a 106.Dq fat 107jail and remove things until it stops working, 108than it is to start with a 109.Dq thin 110jail and add things until it works. 111.Ss "Setting Up a Jail" 112Do what was described in 113.Sx "Setting Up a Jail Directory Tree" 114to build the jail directory tree. 115For the sake of this example, we will 116assume you built it in 117.Pa /data/jail/192.168.11.100 , 118named for the jailed IP address. 119Substitute below as needed with your 120own directory, IP address, and hostname. 121.Pp 122First, you will want to set up your real system's environment to be 123.Dq jail-friendly . 124For consistency, we will refer to the parent box as the 125.Dq "host environment" , 126and to the jailed virtual machine as the 127.Dq "jail environment" . 128Because jail is implemented using IP aliases, one of the first things to do 129is to disable IP services on the host system that listen on all local 130IP addresses for a service. 131This means changing 132.Xr inetd 8 133to only listen on the 134appropriate IP address, and so forth. 135Add the following to 136.Pa /etc/rc.conf 137in the host environment: 138.Bd -literal -offset indent 139sendmail_enable="NO" 140inetd_flags="-wW -a 192.168.11.23" 141rpcbind_enable="NO" 142.Ed 143.Pp 144.Li 192.168.11.23 145is the native IP address for the host system, in this example. 146Daemons that run out of 147.Xr inetd 8 148can be easily set to use only the specified host IP address. 149Other daemons 150will need to be manually configured\(emfor some this is possible through 151the 152.Xr rc.conf 5 153flags entries, for others it is not possible without munging 154the per-application configuration files, or even recompiling. 155For those 156applications that cannot specify the IP they run on, it is better to disable 157them, if possible. 158.Pp 159A number of daemons ship with the base system that may have problems when 160run from outside of a jail in a jail-centric environment. 161This includes 162.Xr sendmail 8 , 163.Xr named 8 , 164and 165.Xr rpcbind 8 . 166While 167.Xr sendmail 8 168and 169.Xr named 8 170can be configured to listen only on a specific 171IP using their configuration files, in most cases it is easier to simply 172run the daemons in jails only, and not in the host environment. 173Attempting to serve 174NFS from the host environment may also cause confusion, and cannot be 175easily reconfigured to use only specific IPs, as some NFS services are 176hosted directly from the kernel. 177Any third party network software running 178in the host environment should also be checked and configured so that it 179does not bind all IP addresses, which would result in those services also 180appearing to be offered by the jail environments. 181.Pp 182Once 183these daemons have been disabled or fixed in the host environment, it is 184best to reboot so that all daemons are in a known state, to reduce the 185potential for confusion later (such as finding that when you send mail 186to a jail, and its sendmail is down, the mail is delivered to the host, 187etc.) 188.Pp 189Start any jails for the first time without configuring the network 190interface so that you can clean it up a little and set up accounts. 191As 192with any machine (virtual or not) you will need to set a root password, time 193zone, etc. 194Before beginning, you may want to copy 195.Xr sysinstall 8 196into the tree so that you can use it to set things up easily. 197Do this using: 198.Bd -literal -offset indent 199mkdir /data/jail/192.168.11.100/stand 200cp /stand/sysinstall /data/jail/192.168.11.100/stand 201.Ed 202.Pp 203Now start the jail: 204.Pp 205.Dl "jail /data/jail/192.168.11.100 testhostname 192.168.11.100 /bin/sh" 206.Pp 207You will end up with a shell prompt, assuming no errors, within the jail. 208You can now run 209.Pa /stand/sysinstall 210and do the post-install configuration to set various configuration options, 211or perform these actions manually by editing 212.Pa /etc/rc.conf , 213etc. 214.Pp 215.Bl -bullet -offset indent -compact 216.It 217Create an empty 218.Pa /etc/fstab 219to quell startup warnings about missing fstab 220.It 221Disable the port mapper 222.Pa ( /etc/rc.conf : 223.Li rpcbind_enable="NO" ) 224.It 225Run 226.Xr newaliases 1 227to quell 228.Xr sendmail 8 229warnings. 230.It 231Disable interface configuration to quell startup warnings about 232.Xr ifconfig 8 233.Pq Li network_interfaces="" 234.It 235Configure 236.Pa /etc/resolv.conf 237so that name resolution within the jail will work correctly 238.It 239Set a root password, probably different from the real host system 240.It 241Set the timezone 242.It 243Add accounts for users in the jail environment 244.It 245Install any packages that you think the environment requires 246.El 247.Pp 248You may also want to perform any package-specific configuration (web servers, 249SSH servers, etc), patch up 250.Pa /etc/syslog.conf 251so it logs as you would like, etc. 252.Pp 253Exit from the shell, and the jail will be shut down. 254.Ss "Starting the Jail" 255You are now ready to restart the jail and bring up the environment with 256all of its daemons and other programs. 257To do this, first bring up the 258virtual host interface, and then start the jail's 259.Pa /etc/rc 260script from within the jail. 261.Pp 262NOTE: If you plan to allow untrusted users to have root access inside the 263jail, you may wish to consider setting the 264.Va security.jail.set_hostname_allowed 265to 0. 266Please see the management reasons why this is a good idea. 267If you do decide to set this variable, 268it must be set before starting any jails, and once each boot. 269.Bd -literal -offset indent 270ifconfig ed0 inet alias 192.168.11.100/32 271mount -t procfs proc /data/jail/192.168.11.100/proc 272jail /data/jail/192.168.11.100 testhostname 192.168.11.100 \\ 273 /bin/sh /etc/rc 274.Ed 275.Pp 276A few warnings will be produced, because most 277.Xr sysctl 8 278configuration variables cannot be set from within the jail, as they are 279global across all jails and the host environment. 280However, it should all 281work properly. 282You should be able to see 283.Xr inetd 8 , 284.Xr syslogd 8 , 285and other processes running within the jail using 286.Xr ps 1 , 287with the 288.Ql J 289flag appearing beside jailed processes. 290To see an active list of jails, use the 291.Xr jls 8 292utility. 293You should also be able to 294.Xr telnet 1 295to the hostname or IP address of the jailed environment, and log 296in using the accounts you created previously. 297.Ss "Managing the Jail" 298Normal machine shutdown commands, such as 299.Xr halt 8 , 300.Xr reboot 8 , 301and 302.Xr shutdown 8 , 303cannot be used successfully within the jail. 304To kill all processes in a 305jail, you may log into the jail and, as root, use one of the following 306commands, depending on what you want to accomplish: 307.Pp 308.Bd -literal -offset indent 309kill -TERM -1 310kill -KILL -1 311.Ed 312.Pp 313This will send the 314.Dv SIGTERM 315or 316.Dv SIGKILL 317signals to all processes in the jail from within the jail. 318Depending on 319the intended use of the jail, you may also want to run 320.Pa /etc/rc.shutdown 321from within the jail. 322To kill processes from outside the jail, use the 323.Xr jexec 8 324utility in conjuction with the one of the 325.Xr kill 1 326commands above, or use the 327.Xr killall 1 328utility with the 329.Fl j 330option. 331.Pp 332The 333.Pa /proc/ Ns Ar pid Ns Pa /status 334file contains, as its last field, the hostname of the jail in which the 335process runs, or 336.Dq Li - 337to indicate that the process is not running within a jail. 338The 339.Xr ps 1 340command also shows a 341.Ql J 342flag for processes in a jail. 343However, the hostname for a jail may be, by 344default, modified from within the jail, so the 345.Pa /proc 346status entry is unreliable by default. 347To disable the setting of the hostname 348from within a jail, set the 349.Va security.jail.set_hostname_allowed 350sysctl variable in the host environment to 0, which will affect all jails. 351You can have this sysctl set on each boot using 352.Xr sysctl.conf 5 . 353Just add the following line to 354.Pa /etc/sysctl.conf : 355.Pp 356.Dl security.jail.set_hostname_allowed=0 357.Ss "Sysctl MIB Entries" 358Certain aspects of the jail containments environment may be modified from 359the host environment using 360.Xr sysctl 8 361MIB variables. 362Currently, these variables affect all jails on the system, although in 363the future this functionality may be finer grained. 364.Bl -tag -width XXX 365.It Va security.jail.set_hostname_allowed 366This MIB entry determines whether or not processes within a jail are 367allowed to change their hostname via 368.Xr hostname 1 369or 370.Xr sethostname 3 . 371In the current jail implementation, the ability to set the hostname from 372within the jail can impact management tools relying on the accuracy of jail 373information in 374.Pa /proc . 375As such, this should be disabled in environments where privileged access to 376jails is given out to untrusted parties. 377.It Va security.jail.socket_unixiproute_only 378The jail functionality binds an IPv4 address to each jail, and limits 379access to other network addresses in the IPv4 space that may be available 380in the host environment. 381However, jail is not currently able to limit access to other network 382protocol stacks that have not had jail functionality added to them. 383As such, by default, processes within jails may only access protocols 384in the following domains: 385.Dv PF_LOCAL , PF_INET , 386and 387.Dv PF_ROUTE , 388permitting them access to 389.Ux 390domain sockets, 391IPv4 addresses, and routing sockets. 392To enable access to other domains, this MIB variable may be set to 3930. 394.It Va security.jail.sysvipc_allowed 395This MIB entry determines whether or not processes within a jail have access 396to System V IPC primitives. 397In the current jail implementation, System V primitives share a single 398namespace across the host and jail environments, meaning that processes 399within a jail would be able to communicate with (and potentially interfere 400with) processes outside of the jail, and in other jails. 401As such, this functionality is disabled by default, but can be enabled 402by setting this MIB entry to 1. 403.El 404.Sh SEE ALSO 405.Xr killall 1 , 406.Xr newaliases 1 , 407.Xr ps 1 , 408.Xr chroot 2 , 409.Xr jail 2 , 410.Xr jail_attach 2 , 411.Xr procfs 5 , 412.Xr rc.conf 5 , 413.Xr sysctl.conf 5 , 414.Xr devfs 8 , 415.Xr halt 8 , 416.Xr inetd 8 , 417.Xr jexec 8 , 418.Xr jls 8 , 419.Xr mount_devfs 8 , 420.Xr named 8 , 421.Xr reboot 8 , 422.Xr rpcbind 8 , 423.Xr sendmail 8 , 424.Xr shutdown 8 , 425.Xr sysctl 8 , 426.Xr syslogd 8 427.Sh HISTORY 428The 429.Nm 430utility appeared in 431.Fx 4.0 . 432.Sh AUTHORS 433.An -nosplit 434The jail feature was written by 435.An Poul-Henning Kamp 436for R&D Associates 437.Pa http://www.rndassociates.com/ 438who contributed it to 439.Fx . 440.Pp 441.An Robert Watson 442wrote the extended documentation, found a few bugs, added 443a few new features, and cleaned up the userland jail environment. 444.Sh BUGS 445Jail currently lacks the ability to allow access to 446specific jail information via 447.Xr ps 1 448as opposed to 449.Xr procfs 5 . 450Similarly, it might be a good idea to add an 451address alias flag such that daemons listening on all IPs 452.Pq Dv INADDR_ANY 453will not bind on that address, which would facilitate building a safe 454host environment such that host daemons do not impose on services offered 455from within jails. 456Currently, the simplist answer is to minimize services 457offered on the host, possibly limiting it to services offered from 458.Xr inetd 8 459which is easily configurable. 460