c4f87284545a391cbaa5a0de2ce156c2793cd4cf
[ikiwiki.git] / docs / newhandbook / Jails / index.mdwn
1
2  ***Contributed by Matteo Riondato. Updated for DragonFly by Dario Banno.***
3
4  [[!toc levels=3]]
5
6
7 ## Synopsis 
8
9
10
11  This chapter will provide an explanation of what DragonFly jails are and how to use them. Jails, sometimes referred to as an enhanced replacement of chroot environments, are a very powerful tool for system administrators, but their basic usage can also be useful for advanced users.
12
13
14
15  After reading this chapter, you will know:
16
17
18 * What a jail is, and what purpose it may serve in DragonFly installations.
19
20 * How to build, start, and stop a jail.
21
22 * The basics of jail administration, both from inside and outside the jail.
23
24
25
26  Other sources of useful information about jails are:
27
28
29
30
31 * The [jail(8)](http://leaf.dragonflybsd.org/cgi/web-man?command=jail&section=8) manual page. This is the full reference of the jail utility -- the administrative tool which can be used in DragonFly to start, stop, and control DragonFly jails.
32
33
34
35
36 * The mailing lists and their archives. The archives of the DragonFly general questions mailing list and other mailing lists hosted by the DragonFly list server already contain a wealth of material for jails. It should always be engaging to search the archives, or post a new question to the [users mailing list](mailto:users@lists.dragonflybsd.org).
37
38
39
40 ----
41
42
43
44
45
46
47 ## Terms Related to Jails 
48
49
50
51  To facilitate better understanding of parts of the DragonFly system related to jails, their internals and the way they interact with the rest of DragonFly, the following terms are used further in this chapter:
52
53
54
55  [chroot(8)](http://leaf.dragonflybsd.org/cgi/web-man?command=chroot&section=8) (command)
56
57
58   A system call of DragonFly, which changes the root directory of a process and all its descendants.
59
60
61
62 ***
63
64  [chroot(2)](http://leaf.dragonflybsd.org/cgi/web-man?command=chroot&section=2) (environment)
65
66
67   The environment of processes running in a “chroot”. This includes resources such as the part of the file system which is visible, user and group IDs which are available, network interfaces and other IPC mechanisms, etc.
68
69
70
71 ***
72
73  [jail(8)](http://leaf.dragonflybsd.org/cgi/web-man?command=jail&section=8) (command)
74
75
76
77   The system administration utility which allows launching of processes within a jail environment.
78
79
80
81 ***
82
83  host (system, process, user, etc.)
84
85
86   The controlling system of a jail environment. The host system has access to all the hardware resources available, and can control processes both outside of and inside a jail environment. One of the important differences of the host system from a jail is that the limitations which apply to superuser processes inside a jail are not enforced for processes of the host system.
87
88
89
90 ***
91
92  hosted (system, process, user, etc.)
93
94   A process, user or other entity, whose access to resources is restricted by a DragonFly jail.
95
96
97
98 ----
99
100
101
102
103
104 ## Introduction 
105
106  Since system administration is a difficult and perplexing task, many powerful tools were developed to make life easier for the administrator. These tools mostly provide enhancements of some sort to the way systems are installed, configured and maintained. Part of the tasks which an administrator is expected to do is to properly configure the security of a system, so that it can continue serving its real purpose, without allowing security violations.
107
108
109  One of the tools which can be used to enhance the security of a DragonFly system are jails. The jail feature was written by Poul-Henning Kamp <phk@freebsd.org> for R&D Associates [[http://www.rndassociates.com/]] who contributed it to FreeBSD 4.X. Support for multiple IPs and IPv6 were introduced in DragonFly 1.7. Their development still goes on, enhancing their usefulness, performance, reliability, and security.
110
111
112
113 ## What is a Jail 
114
115  BSD-like operating systems have had [chroot(2)](http://leaf.dragonflybsd.org/cgi/web-man?command=chroot&section=2) since the time of 4.2BSD. The [chroot(8)](http://leaf.dragonflybsd.org/cgi/web-man?command=chroot&section=8) utility can be used to change the root directory of a set of processes, creating a safe environment, separate from the rest of the system. Processes created in the chrooted environment can not access files or resources outside of it. For that reason, compromising a service running in a chrooted environment should not allow the attacker to compromise the entire system. The chroot(8) utility is good for easy tasks, which do not require a lot of flexibility or complex and advanced features. Since the inception of the chroot concept, however, many ways have been found to escape from a chrooted environment and, although they have been fixed in modern versions of the DragonFly kernel, it was clear that chroot(2) was not the ideal solution for securing services. A new subsystem had to be implemented.
116
117
118  This is one of the main reasons why ***jails*** were developed.
119
120
121  Jails improve on the concept of the traditional [chroot(2)](http://leaf.dragonflybsd.org/cgi/web-man?command=chroot&section=2) environment, in several ways. In a traditional [chroot(2)](http://leaf.dragonflybsd.org/cgi/web-man?command=chroot&section=2) environment, processes are only limited in the part of the file system they can access. The rest of the system resources (like the set of system users, the running processes, or the networking subsystem) are shared by the chrooted processes and the processes of the host system. Jails expand this model by virtualizing not only access to the file system, but also the set of users, the networking subsystem of the DragonFly kernel and a few other things. A more complete set of fine-grained controls available for tuning the access of a jailed environment is described in Section 12.5.
122
123
124  A jail is characterized by four elements:
125
126
127 * A directory subtree -- the starting point from which a jail is entered. Once inside the jail, a process is not permitted to escape outside of this subtree. Traditional security issues which plagued the original chroot(2) design will not affect DragonFly jails.
128
129
130 * A hostname -- the hostname which will be used within the jail. Jails are mainly used for hosting network services, therefore having a descriptive hostname for each jail can really help the system administrator.
131
132
133
134 * An IP address -- this will be assigned to the jail and cannot be changed in any way during the jail's life span. The IP address of a jail is usually an alias address for an existing network interface, but this is not strictly necessary.
135
136
137 * A command -- the path name of an executable to run inside the jail. This is relative to the root directory of the jail environment, and may vary a lot, depending on the type of the specific jail environment.
138
139
140
141  Apart from these, jails can have their own set of users and their own root user. Naturally, the powers of the root user are limited within the jail environment and, from the point of view of the host system, the jail root user is not an omnipotent user. In addition, the root user of a jail is not allowed to perform critical operations to the system outside of the associated jail(8) environment. More information about capabilities and restrictions of the root user will be discussed in Section 12.5 below.
142
143
144
145 ----
146
147
148
149
150
151 ## Creating and Controlling Jails 
152
153
154
155  Some administrators divide jails into the following two types: “complete” jails, which resemble a real DragonFly system, and “service” jails, dedicated to one application or service, possibly running with privileges. This is only a conceptual division and the process of building a jail is not affected by it. The [jail(8)](http://leaf.dragonflybsd.org/cgi/web-man?command=jail&section=8) manual page is quite clear about the procedure for building a jail:
156
157
158
159      
160
161     # setenv D /here/is/the/jail
162
163     # mkdir -p $D                                     (1)
164
165     # cd /usr/src
166
167     # make installworld DESTDIR=$D                    (2)
168
169     # cd etc 
170
171     # make distribution DESTDIR=$D -DNO_MAKEDEV_RUN   (3)
172
173     # cd $D
174
175     # ln -sf dev/null kernel
176
177     # mount_devfs /path/to/jail/dev
178
179
180
181
182   **(1)** 
183
184   Selecting a location for a jail is the best starting point. This is where the jail will physically reside within the file system of the jail's host. A good choice can be /usr/jail/jailname, where jailname is the hostname identifying the jail. The /usr/ file system usually has enough space for the jail file system, which for “complete” jails is, essentially, a replication of every file present in a default installation of the DragonFly base system.
185
186
187   **(2)** 
188
189   This command will populate the directory subtree chosen as jail's physical location on the file system with the necessary binaries, libraries, manual pages and so on. Everything is done in the typical DragonFly style -- first everything is built/compiled, then installed to the destination path.
190
191
192  **(3)**
193
194   The distribution target for make installs every needed configuration file. In simple words, it installs every installable file of /usr/src/etc/ to the /etc directory of the jail environment: ***$D/etc/***.
195
196
197
198 ***
199
200  Once a jail is installed, it can be started by using the [jail(8)](http://leaf.dragonflybsd.org/cgi/web-man?command=jail&section=8) utility. The [jail(8)](http://leaf.dragonflybsd.org/cgi/web-man?command=jail&section=8) utility takes four mandatory arguments which are described in the Section 12.3.1. Other arguments may be specified too, e.g., to run the jailed process with the credentials of a specific user. The command argument depends on the type of the jail; for a virtual system, ***/etc/rc*** is a good choice, since it will replicate the startup sequence of a real DragonFly system. For a service jail, it depends on the service or application that will run within the jail.
201
202
203
204  Jails are often started at boot time and the DragonFly rc mechanism provides an easy way to do this.
205
206
207  A list of the jails which are enabled to start at boot time should be added to the [rc.conf(5)](http://leaf.dragonflybsd.org/cgi/web-man?command=rc.conf&section=5) file:
208
209
210
211      
212
213     jail_enable="YES"   # Set to NO to disable starting of any jails
214
215     jail_list="www"     # Space separated list of names of jails
216
217
218
219
220  For each jail listed in ***jail_list***, a group of [rc.conf(5)](http://leaf.dragonflybsd.org/cgi/web-man?command=rc.conf&section=5) settings, which describe the particular jail, should be added:
221
222
223
224      
225
226     jail_www_rootdir="/usr/jail/www"     # jail's root directory
227
228     jail_www_hostname="www.example.org"  # jail's hostname
229
230     jail_www_ip="192.168.0.10"           # jail's IP address
231
232
233
234
235
236  The default startup of jails configured in [rc.conf(5)](http://leaf.dragonflybsd.org/cgi/web-man?command=rc.conf&section=5), will run the ***/etc/rc*** script of the jail, which assumes the jail is a complete virtual system. For service jails, the default startup command of the jail should be changed, by setting the ***jail_jailname_exec_start*** option appropriately.
237
238
239
240    **Note:**  For a full list of available options, please see the [rc.conf(5)](http://leaf.dragonflybsd.org/cgi/web-man?command=rc.conf&section=5) manual page.
241
242
243
244  The ***/etc/rc.d/jail*** script can be used to start or stop a jail by hand, if an entry for it exists in ***rc.conf***:
245
246
247
248     # /etc/rc.d/jail start www
249
250     # /etc/rc.d/jail stop www
251
252
253
254
255
256  A clean way to shut down a [jail(8)](http://leaf.dragonflybsd.org/cgi/web-man?command=jail&section=8) is not available at the moment. This is because commands normally used to accomplish a clean system shutdown cannot be used inside a jail. The best way to shut down a jail is to run the following command from within the jail itself or using the [jexec(8)](http://leaf.dragonflybsd.org/cgi/web-man?command=jexec&section=8) utility from outside the jail:
257
258
259
260      
261
262     # sh /etc/rc.shutdown
263
264
265
266
267  More information about this can be found in the [jail(8)](http://leaf.dragonflybsd.org/cgi/web-man?command=jail&section=8) manual page.
268
269
270
271 ----
272
273
274
275
276
277
278 ## Fine Tuning and Administration 
279
280
281
282  There are several options which can be set for any jail, and various ways of combining a host DragonFly system with jails, to produce higher level applications. This section presents some of the options available for tuning the behavior and security restrictions implemented by a jail installation.
283
284
285
286 ## System tools for jail tuning in DragonFly 
287
288
289
290  Fine tuning of a jail's configuration is mostly done by setting [sysctl(8)](http://leaf.dragonflybsd.org/cgi/web-man?command=sysctl&section=8) variables. A special subtree of sysctl exists as a basis for organizing all the relevant options: the ***security.jail.**** hierarchy of DragonFly kernel options. Here is a list of the main jail-related sysctls, complete with their default value. Names should be self-explanatory, but for more information about them, please refer to the [jail(8)](http://leaf.dragonflybsd.org/cgi/web-man?command=jail&section=8) and [sysctl(8)](http://leaf.dragonflybsd.org/cgi/web-man?command=sysctl&section=8) manual pages.
291
292
293
294
295 * jail.set_hostname_allowed: 1
296
297
298 * jail.socket_unixiproute_only: 1
299
300
301 * jail.sysvipc_allowed: 0
302
303
304 * jail.enforce_statfs: 2
305
306
307 * jail.allow_raw_sockets: 0
308
309
310 * jail.chflags_allowed: 0
311
312
313 * jail.jailed: 0
314
315
316
317 These variables can be used by the system administrator of the host system to add or remove some of the limitations imposed by default on the root user. Note that there are some limitations which cannot be removed. The root user is not allowed to mount or unmount file systems from within a [jail(8)](http://leaf.dragonflybsd.org/cgi/web-man?command=jail&section=8). The root inside a jail may not set firewall rules or do many other administrative tasks which require modifications of in-kernel data, such as setting the securelevel of the kernel.
318
319
320
321  The base system of DragonFly contains a basic set of tools for viewing information about the active jails, and attaching to a jail to run administrative commands. The [jls(8)](http://leaf.dragonflybsd.org/cgi/web-man?command=jls&section=8) and [jexec(8)](http://leaf.dragonflybsd.org/cgi/web-man?command=jexec&section=8) commands are part of the base DragonFly system, and can be used to perform the following simple tasks:
322
323  * Print a list of active jails and their corresponding jail identifier (JID), IP address, hostname and path.
324
325
326  * Attach to a running jail, from its host system, and run a command inside the jail or perform administrative tasks inside the jail itself. This is especially useful when the root user wants to cleanly shut down a jail. The [jexec(8)](http://leaf.dragonflybsd.org/cgi/web-man?command=jexec&section=8) utility can also be used to start a shell in a jail to do administration in it; for example:
327
328
329        
330
331           # jexec 1 tcsh