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