Fix jail mount devfs instruction
[ikiwiki.git] / docs / newhandbook / Jails / index.mdwn
CommitLineData
2d9d2dba 1 ***Contributed by Matteo Riondato. Updated for DragonFly by Dario Banno.***
2
3 [[!toc levels=3]]
4
5
6## Synopsis
7
8
9
10 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.
11
12
13
14 After reading this chapter, you will know:
15
16
17* What a jail is, and what purpose it may serve in DragonFly installations.
18
19* How to build, start, and stop a jail.
20
21* The basics of jail administration, both from inside and outside the jail.
14d3ea40 22***
2d9d2dba 23
24
25 Other sources of useful information about jails are:
26
27
28
29
b18173d2 30* 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.
2d9d2dba 31
32
33
34
35* 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).
14d3ea40 36***
37 ***
2d9d2dba 38
cb669d67 39For information on how to setup a jail, see: [Setting up a jail](http://www.youtube.com/watch?v=s4_dJjfwtR4)
2d9d2dba 40
41
42----
43
44
45
46
47
48
49## Terms Related to Jails
50
51
52
53 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:
54
55
56
14d3ea40 57 ***[chroot(8)](http://leaf.dragonflybsd.org/cgi/web-man?command=chroot&section=8) (command)***
2d9d2dba 58
59
60 A system call of DragonFly, which changes the root directory of a process and all its descendants.
61
62
63
64***
65
14d3ea40 66 ***[chroot(2)](http://leaf.dragonflybsd.org/cgi/web-man?command=chroot&section=2) (environment)***
2d9d2dba 67
68
69 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.
70
71
72
73***
74
14d3ea40 75*** [jail(8)](http://leaf.dragonflybsd.org/cgi/web-man?command=jail&section=8) (command)***
2d9d2dba 76
77
78
79 The system administration utility which allows launching of processes within a jail environment.
80
81
82
83***
84
14d3ea40 85 ***host (system, process, user, etc.)***
2d9d2dba 86
87
88 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.
89
90
91
92***
93
14d3ea40 94 ***hosted (system, process, user, etc.)***
2d9d2dba 95
96 A process, user or other entity, whose access to resources is restricted by a DragonFly jail.
97
98
99
100----
101
102
103
104
105
106## Introduction
107
108 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.
14d3ea40 109***
2d9d2dba 110
111 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.
112
113
114
115## What is a Jail
116
b18173d2 117 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.
2d9d2dba 118
119
120 This is one of the main reasons why ***jails*** were developed.
121
122
b18173d2 123 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.
14d3ea40 124***
2d9d2dba 125
126 A jail is characterized by four elements:
127
128
129* 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.
130
131
132* 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.
133
134
135
136* 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.
137
138
139* 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.
14d3ea40 140***
2d9d2dba 141
142
143 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.
144
145
146
147----
148
149
150
151
152
153## Creating and Controlling Jails
154
155
156
14d3ea40 157 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:
2d9d2dba 158
159
160
161
162
163 # setenv D /here/is/the/jail
164
165 # mkdir -p $D (1)
166
167 # cd /usr/src
168
169 # make installworld DESTDIR=$D (2)
170
171 # cd etc
172
173 # make distribution DESTDIR=$D -DNO_MAKEDEV_RUN (3)
174
2d9d2dba 175 # cd $D
176
177 # ln -sf dev/null kernel
178
c58035c4 179 # mount_devfs -o $D/dev
2d9d2dba 180
181
182
183
184 **(1)**
185
14d3ea40 186 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.
187***
2d9d2dba 188
189 **(2)**
190
191 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.
14d3ea40 192***
2d9d2dba 193
194 **(3)**
195
14d3ea40 196 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/***.
2d9d2dba 197
198
199
200***
201
b18173d2 202 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.
2d9d2dba 203
204
205
206 Jails are often started at boot time and the DragonFly rc mechanism provides an easy way to do this.
207
14d3ea40 208***
b18173d2 209 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:
2d9d2dba 210
211
212
213
214
215 jail_enable="YES" # Set to NO to disable starting of any jails
216
217 jail_list="www" # Space separated list of names of jails
218
14d3ea40 219***
2d9d2dba 220
b18173d2 221 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:
2d9d2dba 222
223
224
225
226
227 jail_www_rootdir="/usr/jail/www" # jail's root directory
228
229 jail_www_hostname="www.example.org" # jail's hostname
230
231 jail_www_ip="192.168.0.10" # jail's IP address
232
233
234
235
236
b18173d2 237 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.
2d9d2dba 238
239
240
b18173d2 241 **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.
2d9d2dba 242
243
244
245 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***:
246
247
248
249 # /etc/rc.d/jail start www
250
251 # /etc/rc.d/jail stop www
252
253
14d3ea40 254***
2d9d2dba 255
256
b18173d2 257 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:
2d9d2dba 258
259
260
261
262
263 # sh /etc/rc.shutdown
264
265
266
267
b18173d2 268 More information about this can be found in the [jail(8)](http://leaf.dragonflybsd.org/cgi/web-man?command=jail&section=8) manual page.
2d9d2dba 269
270
271
272----
273
274
275
276
277
278
279## Fine Tuning and Administration
280
281
282
283 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.
284
285
286
287## System tools for jail tuning in DragonFly
288
289
290
14d3ea40 291 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 ***securityjail**** 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.
2d9d2dba 292
293
294
295
296* jail.set_hostname_allowed: 1
297
298
299* jail.socket_unixiproute_only: 1
300
301
302* jail.sysvipc_allowed: 0
303
304
305* jail.enforce_statfs: 2
306
307
308* jail.allow_raw_sockets: 0
309
310
311* jail.chflags_allowed: 0
312
313
314* jail.jailed: 0
315
316
317
b18173d2 318These 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.
2d9d2dba 319
14d3ea40 320***
2d9d2dba 321
b18173d2 322 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:
2d9d2dba 323
324 * Print a list of active jails and their corresponding jail identifier (JID), IP address, hostname and path.
325
326
b18173d2 327 * 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:
2d9d2dba 328
329
330
331
332 # jexec 1 tcsh