xref: /freebsd/usr.sbin/jail/jail.8 (revision 66c14b21d3ab0b18376563ba643ddb49b4fd33dd)
1.\"
2.\" Copyright (c) 2000, 2003 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 May 11, 2006
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 J Ar jid_file
46.Op Fl s Ar securelevel
47.Op Fl l u Ar username | Fl U Ar username
48.Ar path hostname ip-number command ...
49.Sh DESCRIPTION
50The
51.Nm
52utility imprisons a process and all future descendants.
53.Pp
54The options are as follows:
55.Bl -tag -width ".Fl u Ar username"
56.It Fl i
57Output the jail identifier of the newly created jail.
58.It Fl J Ar jid_file
59Write a JidFile, like a PidFile, containing jailid, path, hostname, ip and
60command used to start the jail.
61.It Fl l
62Run program in the clean environment.
63The environment is discarded except for
64.Ev HOME , SHELL , TERM
65and
66.Ev USER .
67.Ev HOME
68and
69.Ev SHELL
70are set to the target login's default values.
71.Ev USER
72is set to the target login.
73.Ev TERM
74is imported from the current environment.
75The environment variables from the login class capability database for the
76target login are also set.
77.It Fl s Ar securelevel
78Sets
79.Va kern.securelevel
80to the specified value inside the newly created jail.
81.It Fl u Ar username
82The user name from host environment as whom the
83.Ar command
84should run.
85.It Fl U Ar username
86The user name from jailed environment as whom the
87.Ar command
88should run.
89.It Ar path
90Directory which is to be the root of the prison.
91.It Ar hostname
92Hostname of the prison.
93.It Ar ip-number
94IP number assigned to the prison.
95.It Ar command
96Pathname of the program which is to be executed.
97.El
98.Pp
99Jails are typically set up using one of two philosophies: either to
100constrain a specific application (possibly running with privilege), or
101to create a
102.Dq "virtual system image"
103running a variety of daemons and services.
104In both cases, a fairly complete file system install of
105.Fx
106is
107required, so as to provide the necessary command line tools, daemons,
108libraries, application configuration files, etc.
109However, for a virtual server configuration, a fair amount of
110additional work is required so as to configure the
111.Dq boot
112process.
113This manual page documents the configuration steps necessary to support
114either of these steps, although the configuration steps may be
115refined based on local requirements.
116.Pp
117Please see the
118.Xr jail 2
119man page for further details.
120.Sh EXAMPLES
121.Ss "Setting up a Jail Directory Tree"
122To set up a jail directory tree containing an entire
123.Fx
124distribution, the following
125.Xr sh 1
126command script can be used:
127.Bd -literal
128D=/here/is/the/jail
129cd /usr/src
130mkdir -p $D
131make world DESTDIR=$D
132make distribution DESTDIR=$D
133mount_devfs devfs $D/dev
134.Ed
135.Pp
136NOTE: It is important that only appropriate device nodes in devfs be
137exposed to a jail; access to disk devices in the jail may permit processes
138in the jail to bypass the jail sandboxing by modifying files outside of
139the jail.
140See
141.Xr devfs 8
142for information on how to use devfs rules to limit access to entries
143in the per-jail devfs.
144.Pp
145In many cases this example would put far more in the jail than needed.
146In the other extreme case a jail might contain only one file:
147the executable to be run in the jail.
148.Pp
149We recommend experimentation and caution that it is a lot easier to
150start with a
151.Dq fat
152jail and remove things until it stops working,
153than it is to start with a
154.Dq thin
155jail and add things until it works.
156.Ss "Setting Up a Jail"
157Do what was described in
158.Sx "Setting Up a Jail Directory Tree"
159to build the jail directory tree.
160For the sake of this example, we will
161assume you built it in
162.Pa /data/jail/192.168.11.100 ,
163named for the jailed IP address.
164Substitute below as needed with your
165own directory, IP address, and hostname.
166.Ss "Setting up the Host Environment"
167First, you will want to set up your real system's environment to be
168.Dq jail-friendly .
169For consistency, we will refer to the parent box as the
170.Dq "host environment" ,
171and to the jailed virtual machine as the
172.Dq "jail environment" .
173Since jail is implemented using IP aliases, one of the first things to do
174is to disable IP services on the host system that listen on all local
175IP addresses for a service.
176If a network service is present in the host environment that binds all
177available IP addresses rather than specific IP addresses, it may service
178requests sent to jail IP addresses.
179This means changing
180.Xr inetd 8
181to only listen on the
182appropriate IP address, and so forth.
183Add the following to
184.Pa /etc/rc.conf
185in the host environment:
186.Bd -literal -offset indent
187sendmail_enable="NO"
188inetd_flags="-wW -a 192.168.11.23"
189rpcbind_enable="NO"
190.Ed
191.Pp
192.Li 192.168.11.23
193is the native IP address for the host system, in this example.
194Daemons that run out of
195.Xr inetd 8
196can be easily set to use only the specified host IP address.
197Other daemons
198will need to be manually configured\(emfor some this is possible through
199the
200.Xr rc.conf 5
201flags entries; for others it is necessary to modify per-application
202configuration files, or to recompile the applications.
203The following frequently deployed services must have their individual
204configuration files modified to limit the application to listening
205to a specific IP address:
206.Pp
207To configure
208.Xr sshd 8 ,
209it is necessary to modify
210.Pa /etc/ssh/sshd_config .
211.Pp
212To configure
213.Xr sendmail 8 ,
214it is necessary to modify
215.Pa /etc/mail/sendmail.cf .
216.Pp
217For
218.Xr named 8 ,
219it is necessary to modify
220.Pa /etc/namedb/named.conf .
221.Pp
222In addition, a number of services must be recompiled in order to run
223them in the host environment.
224This includes most applications providing services using
225.Xr rpc 3 ,
226such as
227.Xr rpcbind 8 ,
228.Xr nfsd 8 ,
229and
230.Xr mountd 8 .
231In general, applications for which it is not possible to specify which
232IP address to bind should not be run in the host environment unless they
233should also service requests sent to jail IP addresses.
234Attempting to serve
235NFS from the host environment may also cause confusion, and cannot be
236easily reconfigured to use only specific IPs, as some NFS services are
237hosted directly from the kernel.
238Any third-party network software running
239in the host environment should also be checked and configured so that it
240does not bind all IP addresses, which would result in those services' also
241appearing to be offered by the jail environments.
242.Pp
243Once
244these daemons have been disabled or fixed in the host environment, it is
245best to reboot so that all daemons are in a known state, to reduce the
246potential for confusion later (such as finding that when you send mail
247to a jail, and its sendmail is down, the mail is delivered to the host,
248etc.).
249.Ss "Configuring the Jail"
250Start any jail for the first time without configuring the network
251interface so that you can clean it up a little and set up accounts.
252As
253with any machine (virtual or not) you will need to set a root password, time
254zone, etc.
255Some of these steps apply only if you intend to run a full virtual server
256inside the jail; others apply both for constraining a particular application
257or for running a virtual server.
258.Pp
259Start a shell in the jail:
260.Pp
261.Dl "jail /data/jail/192.168.11.100 testhostname 192.168.11.100 /bin/sh"
262.Pp
263Assuming no errors, you will end up with a shell prompt within the jail.
264You can now run
265.Pa /usr/sbin/sysinstall
266and do the post-install configuration to set various configuration options,
267or perform these actions manually by editing
268.Pa /etc/rc.conf ,
269etc.
270.Pp
271.Bl -bullet -offset indent -compact
272.It
273Create an empty
274.Pa /etc/fstab
275to quell startup warnings about missing fstab (virtual server only)
276.It
277Disable the port mapper
278.Pa ( /etc/rc.conf :
279.Li rpcbind_enable="NO" )
280(virtual server only)
281.It
282Configure
283.Pa /etc/resolv.conf
284so that name resolution within the jail will work correctly
285.It
286Run
287.Xr newaliases 1
288to quell
289.Xr sendmail 8
290warnings.
291.It
292Disable interface configuration to quell startup warnings about
293.Xr ifconfig 8
294.Pq Li network_interfaces=""
295(virtual server only)
296.It
297Set a root password, probably different from the real host system
298.It
299Set the timezone
300.It
301Add accounts for users in the jail environment
302.It
303Install any packages the environment requires
304.El
305.Pp
306You may also want to perform any package-specific configuration (web servers,
307SSH servers, etc), patch up
308.Pa /etc/syslog.conf
309so it logs as you would like, etc.
310If you are not using a virtual server, you may wish to modify
311.Xr syslogd 8
312in the host environment to listen on the syslog socket in the jail
313environment; in this example, the syslog socket would be stored in
314.Pa /data/jail/192.168.11.100/var/run/log .
315.Pp
316Exit from the shell, and the jail will be shut down.
317.Ss "Starting the Jail"
318You are now ready to restart the jail and bring up the environment with
319all of its daemons and other programs.
320If you are running a single application in the jail, substitute the
321command used to start the application for
322.Pa /etc/rc
323in the examples below.
324To start a virtual server environment,
325.Pa /etc/rc
326is run to launch various daemons and services.
327To do this, first bring up the
328virtual host interface, and then start the jail's
329.Pa /etc/rc
330script from within the jail.
331.Pp
332NOTE: If you plan to allow untrusted users to have root access inside the
333jail, you may wish to consider setting the
334.Va security.jail.set_hostname_allowed
335sysctl variable to 0.
336Please see the management discussion later in this document as to why this
337may be a good idea.
338If you do decide to set this variable,
339it must be set before starting any jails, and once each boot.
340.Bd -literal -offset indent
341ifconfig ed0 inet alias 192.168.11.100/32
342mount_devfs devfs /data/jail/192.168.11.100/dev
343mount -t procfs proc /data/jail/192.168.11.100/proc
344jail /data/jail/192.168.11.100 testhostname 192.168.11.100 \\
345	/bin/sh /etc/rc
346.Ed
347.Pp
348A few warnings will be produced, because most
349.Xr sysctl 8
350configuration variables cannot be set from within the jail, as they are
351global across all jails and the host environment.
352However, it should all
353work properly.
354You should be able to see
355.Xr inetd 8 ,
356.Xr syslogd 8 ,
357and other processes running within the jail using
358.Xr ps 1 ,
359with the
360.Ql J
361flag appearing beside jailed processes.
362To see an active list of jails, use the
363.Xr jls 8
364utility.
365You should also be able to
366.Xr telnet 1
367to the hostname or IP address of the jailed environment, and log
368in using the accounts you created previously.
369.Pp
370It is possible to have jails started at boot time.
371Please refer to the
372.Dq jail_*
373variables in
374.Xr rc.conf 5
375for more information.
376The
377.Xr rc 8
378jail script provides a flexible system to start/stop jails:
379.Bd -literal
380/etc/rc.d/jail start
381/etc/rc.d/jail stop
382/etc/rc.d/jail start myjail
383/etc/rc.d/jail stop myjail
384.Ed
385.Ss "Managing the Jail"
386Normal machine shutdown commands, such as
387.Xr halt 8 ,
388.Xr reboot 8 ,
389and
390.Xr shutdown 8 ,
391cannot be used successfully within the jail.
392To kill all processes in a
393jail, you may log into the jail and, as root, use one of the following
394commands, depending on what you want to accomplish:
395.Pp
396.Bd -literal -offset indent
397kill -TERM -1
398kill -KILL -1
399.Ed
400.Pp
401This will send the
402.Dv SIGTERM
403or
404.Dv SIGKILL
405signals to all processes in the jail from within the jail.
406Depending on
407the intended use of the jail, you may also want to run
408.Pa /etc/rc.shutdown
409from within the jail.
410To kill processes from outside the jail, use the
411.Xr jexec 8
412utility in conjunction with the one of the
413.Xr kill 1
414commands above.
415.Pp
416The
417.Pa /proc/ Ns Ar pid Ns Pa /status
418file contains, as its last field, the hostname of the jail in which the
419process runs, or
420.Dq Li -
421to indicate that the process is not running within a jail.
422The
423.Xr ps 1
424command also shows a
425.Ql J
426flag for processes in a jail.
427However, the hostname for a jail may be, by
428default, modified from within the jail, so the
429.Pa /proc
430status entry is unreliable by default.
431To disable the setting of the hostname
432from within a jail, set the
433.Va security.jail.set_hostname_allowed
434sysctl variable in the host environment to 0, which will affect all jails.
435You can have this sysctl set on each boot using
436.Xr sysctl.conf 5 .
437Just add the following line to
438.Pa /etc/sysctl.conf :
439.Pp
440.Dl security.jail.set_hostname_allowed=0
441.Pp
442You can also list/kill processes based on their jail ID.
443To show processes and their jail ID, use the following command:
444.Pp
445.Dl "ps ax -o pid,jid,args"
446.Pp
447To show and then kill processes in jail number 3 use the following commands:
448.Bd -literal -offset indent
449pgrep -lfj 3
450pkill -j 3
451.Ed
452or:
453.Pp
454.Dl "killall -j 3"
455.Ss "Sysctl MIB Entries"
456Certain aspects of the jail containments environment may be modified from
457the host environment using
458.Xr sysctl 8
459MIB variables.
460Currently, these variables affect all jails on the system, although in
461the future this functionality may be finer grained.
462.Bl -tag -width XXX
463.It Va security.jail.allow_raw_sockets
464This MIB entry determines whether or not prison root is allowed to
465create raw sockets.
466Setting this MIB to 1 allows utilities like
467.Xr ping 8
468and
469.Xr traceroute 8
470to operate inside the prison.
471If this MIB
472is set, the source IP addresses are enforced to comply
473with the IP address bound to the jail, regardless of whether or not
474the
475.Dv IP_HDRINCL
476flag has been set on the socket.
477Since raw sockets can be used to configure
478and interact with various network subsystems, extra caution should be used
479where privileged access to jails is given out to untrusted parties.
480As such,
481by default this option is disabled.
482.It Va security.jail.enforce_statfs
483This MIB entry determines which information processes in a jail are
484able to get about mount-points.
485It affects the behaviour of the following syscalls:
486.Xr statfs 2 ,
487.Xr fstatfs 2 ,
488.Xr getfsstat 2
489and
490.Xr fhstatfs 2
491(as well as similar compatibility syscalls).
492When set to 0, all mount-points are available without any restrictions.
493When set to 1, only mount-points below the jail's chroot directory are
494visible.
495In addition to that, the path to the jail's chroot directory is removed
496from the front of their pathnames.
497When set to 2 (default), above syscalls can operate only on a mount-point
498where the jail's chroot directory is located.
499.It Va security.jail.set_hostname_allowed
500This MIB entry determines whether or not processes within a jail are
501allowed to change their hostname via
502.Xr hostname 1
503or
504.Xr sethostname 3 .
505In the current jail implementation, the ability to set the hostname from
506within the jail can impact management tools relying on the accuracy of jail
507information in
508.Pa /proc .
509As such, this should be disabled in environments where privileged access to
510jails is given out to untrusted parties.
511.It Va security.jail.socket_unixiproute_only
512The jail functionality binds an IPv4 address to each jail, and limits
513access to other network addresses in the IPv4 space that may be available
514in the host environment.
515However, jail is not currently able to limit access to other network
516protocol stacks that have not had jail functionality added to them.
517As such, by default, processes within jails may only access protocols
518in the following domains:
519.Dv PF_LOCAL , PF_INET ,
520and
521.Dv PF_ROUTE ,
522permitting them access to
523.Ux
524domain sockets,
525IPv4 addresses, and routing sockets.
526To enable access to other domains, this MIB variable may be set to
5270.
528.It Va security.jail.sysvipc_allowed
529This MIB entry determines whether or not processes within a jail have access
530to System V IPC primitives.
531In the current jail implementation, System V primitives share a single
532namespace across the host and jail environments, meaning that processes
533within a jail would be able to communicate with (and potentially interfere
534with) processes outside of the jail, and in other jails.
535As such, this functionality is disabled by default, but can be enabled
536by setting this MIB entry to 1.
537.It Va security.jail.chflags_allowed
538This MIB entry determines how a privileged user inside a jail will be
539treated by
540.Xr chflags 2 .
541If zero, such users are treated as unprivileged, and are unable to set
542or clear system file flags; if non-zero, such users are treated as
543privileged, and may manipulate system file flags subject to the usual
544constraints on
545.Va kern.securelevel .
546.El
547.Pp
548The read-only
549.Va security.jail.jailed
550variable can be used to determine if a process is running inside a jail (value
551is one) or not (value is zero).
552.Pp
553The
554.Va security.jail.list
555MIB entry is read-only and it returns an array of
556.Vt "struct xprison"
557defined in
558.In sys/jail.h .
559It is recommended to use the
560.Xr jls 8
561utility to see current active list of jails.
562.Pp
563There are currently two MIB related variables that have per-jail settings.
564Changes to these variables by a jailed process do not effect the host
565environment, only the jail environment.
566The variables are
567.Va kern.securelevel
568and
569.Va kern.hostname .
570.Sh SEE ALSO
571.Xr killall 1 ,
572.Xr newaliases 1 ,
573.Xr pgrep 1 ,
574.Xr pkill 1 ,
575.Xr ps 1 ,
576.Xr chroot 2 ,
577.Xr jail 2 ,
578.Xr jail_attach 2 ,
579.Xr procfs 5 ,
580.Xr rc.conf 5 ,
581.Xr sysctl.conf 5 ,
582.Xr devfs 8 ,
583.Xr halt 8 ,
584.Xr inetd 8 ,
585.Xr jexec 8 ,
586.Xr jls 8 ,
587.Xr mount_devfs 8 ,
588.Xr named 8 ,
589.Xr reboot 8 ,
590.Xr rpcbind 8 ,
591.Xr sendmail 8 ,
592.Xr shutdown 8 ,
593.Xr sysctl 8 ,
594.Xr syslogd 8
595.Sh HISTORY
596The
597.Nm
598utility appeared in
599.Fx 4.0 .
600.Sh AUTHORS
601.An -nosplit
602The jail feature was written by
603.An Poul-Henning Kamp
604for R&D Associates
605.Pa http://www.rndassociates.com/
606who contributed it to
607.Fx .
608.Pp
609.An Robert Watson
610wrote the extended documentation, found a few bugs, added
611a few new features, and cleaned up the userland jail environment.
612.Sh BUGS
613Jail currently lacks the ability to allow access to
614specific jail information via
615.Xr ps 1
616as opposed to
617.Xr procfs 5 .
618Similarly, it might be a good idea to add an
619address alias flag such that daemons listening on all IPs
620.Pq Dv INADDR_ANY
621will not bind on that address, which would facilitate building a safe
622host environment such that host daemons do not impose on services offered
623from within jails.
624Currently, the simplest answer is to minimize services
625offered on the host, possibly limiting it to services offered from
626.Xr inetd 8
627which is easily configurable.
628