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