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