Debian Codegraph @

doc/howto/Chroot.html

raw symbols SCIP
1 <!DOCTYPE html>
2 <html>
3 <head>
4 <title>ProFTPD: Symlinks and chroot()</title>
5 </head>
6
7 <body bgcolor=white>
8
9 <hr>
10 <center><h2><b><i><code>DefaultRoot</code>, Symlinks and <code>chroot()</code></i></b></h2></center>
11 <hr>
12
13 <p>
14 <b>Restricting Users' Directories</b><br>
15 One of the most common questions for new users of ProFTPD is &quot;How do I
16 restrict my users to only certain directories?&quot; or, phrased another
17 way, &quot;How can I put my users in a chroot jail?&quot; As a common
18 question, it definitely has a place in the
19 <a href="http://www.proftpd.org/docs/faq/linked/faq-ch5.html#AEN524">FAQ</a>.
20 Many users, I fear, do not read the FAQ carefully, and so miss that section.
21 The answer is ProFTPD's <a href="../modules/mod_auth.html#DefaultRoot"><code>DefaultRoot</code></a> configuration
22 directive, which accomplishes this functionality by using the
23 <code>chroot(2)</code> function.
24
25 <p><a name="GroupExpressions"></a>
26 This configuration directive may appear in the <code>&lt;VirtualHost&gt;</code>,
27 <code>&lt;Global&gt;</code>, and the &quot;server config&quot; (meaning
28 not in any <code>&lt;VirtualHost&gt;</code> or <code>&lt;Global&gt;</code>
29 sections) configuration contexts. The most common configuration requested
30 is to restrict users to their home directories, which can be done simply
31 by adding the following line to your <code>proftpd.conf</code>:
32 <pre>
33 DefaultRoot ~
34 </pre>
35 The <code>~</code> (tilde) is a Unix-ism that is expanded to the logging-in
36 user's home directory. For slightly more complex setups, administrators
37 may want to restrict only a subset of their users into home directories
38 (or some other directory), but leave some privileged users unrestricted.
39 For example, say you have your privileged users all as members of a group
40 called <code>ftp-special</code>. The <code>DefaultRoot</code>'s optional
41 second parameter, a <i>group-expression</i>, can then be used, like so:
42 <pre>
43 DefaultRoot ~ !ftp-special
44 </pre>
45 This says to <code>chroot()</code> every user who is <b>not</b> a member of
46 group <code>ftp-special</code> to their respective home directory, and:
47 <pre>
48 DefaultRoot /path/to/dir group1,group2
49 </pre>
50 will <code>chroot()</code> users who are members of <b>both</b>
51 <code>group1</code> <b>and</b> <code>group2</code> into
52 <code>/path/to/dir</code>. More complex <i>group-expressions</i> can be used
53 as needed.
54
55 <p>
56 Note that the execute bit (<code>--x</code>) must be on in order to
57 <code>chroot()</code> a user into that directory. This bit is also needed
58 for a user to be able to <code>chdir</code> into that directory.
59
60 <p><a name="Symlinks"></a>
61 <b>Symlinks</b><br>
62 There have been many questions on the ProFTPD user mailing list about why
63 symlinked directories are not visible to <code>chroot</code>ed users (this
64 includes <code>&lt;Anonymous&gt;</code> users as well as users restricted using
65 <code>DefaultRoot</code>. This document is intended to clarify the issues and
66 discuss some ways of achieving what is commonly desired.
67
68 <p>
69 These issues are not specific to ProFTPD, but rather to the workings of a
70 Unix system. First, a brief review of how links work, and why
71 <code>chroot(2)</code> poses such a problem. Then a look at ways around
72 the issue.
73
74 <p>
75 <b>How Links Work</b><br>
76 There are two types of links in Unix: hard and symbolic.
77
78 <p>
79 A <i>hard</i> link is a file that is, for all intents and purposes, <i>the</i>
80 file to which it is linked. The difference between a hardlink and the linked
81 file is one of placement in the filesystem. Editing the hardlink edits the
82 linked file. One limitation of hard links is that linked files cannot reside
83 on different filesystems. This means that if <code>/var</code> and
84 <code>/home</code> are two different mount points in <code>/etc/fstab</code>
85 (or <code>/etc/vfstab</code>), then a file in <code>/var/tmp</code> cannot be
86 hardlinked with a file in <code>/home</code>:
87 <pre>
88 <b># pwd</b>
89 /var/tmp
90 <b># ln /home/tj/tmp/tmpfile tmplink</b>
91 ln: cannot create hard link `tmplink' to `/home/tj/tmp/tmpfile': Invalid cross-device link
92 </pre>
93 A <i>symbolic</i> link (also referred to as a &quot;symlink&quot;) is a file
94 whose contents contain the name of the file to which the symbolic link
95 points. For example:
96 <pre>
97 lrwxrwxrwx 1 root root 11 Mar 2 2000 rmt -&gt; /sbin/rmt
98 </pre>
99 The file <code>rmt</code> contains the nine characters <code>/sbin/rmt</code>.
100 The reason symbolic links fail when <code>chroot(2)</code> is used to
101 change the position of the root (<code>/</code>)of the filesystem is that,
102 once <code>/</code> is moved, the pointed-to file path changes. If, for
103 example, if <code>chroot(2)</code> is used to change the filesystem root
104 to <code>/ftp</code>, then the symlink above would be actually be pointing
105 to <code>/ftp/sbin/rmt</code>. Chances that that link, if
106 <code>chroot(2)</code> is used, now points to a path that does not exist.
107 Symbolic links that point to nonexistent files are known as <i>dangling</i>
108 symbolic links. Note that symbolic links to files underneath the new
109 root, such as symlinks to a file in the same directory:
110 <pre>
111 <b># pwd</b>
112 /var/ftp
113 <b># ls -l</b>
114 -rw-r--r-- 1 root root 0 Jan 16 11:50 tmpfile
115 lrwxrwxrwx 1 root root 7 Jan 16 11:50 tmplink -&gt; tmpfile
116 </pre>
117 will be unaffected; only paths that point outside/above the new root will be
118 affected.
119
120 <p>
121 <B>Link Creation Tricks</b><br>
122 Knowing the above, it is now possible to demonstrate how <i>some</i> symlinks
123 <i>can</i> work within a chrooted session, depending on how you create them.
124
125 <p>
126 Here is an example to demonstrate this point. Assume the following
127 directory structure:
128 <pre>
129 /path/to/ftp/
130 /path/to/ftp/folders/
131 /path/to/ftp/folders/incoming/
132 /path/to/ftp/incoming --&gt; /path/to/ftp/folders/incoming/ (<i>symlink</i>)
133 </pre>
134 And assume that you have the following in your <code>proftpd.conf</code>:
135 <pre>
136 DefaultRoot /path/to/ftp/
137 </pre>
138
139 <p>
140 If the symlink is created using:
141 <pre>
142 # ln -s /path/to/ftp/folders/incoming /path/to/ftp/incoming
143 </pre>
144 then that symlink will <b>not</b> work in the chrooted session; the path you
145 gave ("/path/to/ftp/folders/incoming") is an absolute path, and will not exist
146 after the chroot.
147
148 <p>
149 Instead, if you create the symlink using:
150 <pre>
151 # cd /path/to/ftp
152 # ln -s folders/incoming incoming
153 </pre>
154 then that symlink should work within the chroot; the key is to use relative
155 paths which <i>do not go above your <code>DefaultRoot</code> directory</i>.
156
157 <p>
158 If your symlink <i>does</i> need to go above the <code>DefaultRoot</code>
159 directory, then you need to use one of the other tricks described below.
160
161 <p>
162 <b>Filesystem Tricks</b><br>
163 A typical scenario is one where &quot;<code>DefaultRoot ~</code>&quot; is
164 used to restrict users to their home directories, and where the administrator
165 would like to have a shared upload directory, say
166 <code>/var/ftp/incoming</code>, in each user's home directory. Symbolic
167 links would normally be used to provide an arrangement like this. As
168 mentioned above, though, when <code>chroot(2)</code> is used (which is what
169 the <code>DefaultRoot</code> directive does), symlinks that point outside the
170 new root (the user's home directory in this case) will not work. To get
171 around this apparent limitation, it is possible on modern operating systems to
172 mount directories at several locations in the filesystem.
173
174 <p>
175 To have an exact duplicate of the <code>/var/ftp/incoming directory</code>
176 available in <code>/home/bob/incoming</code> and
177 <code>/home/dave/incoming</code>, use one of these commands:
178 <ul>
179 <li>Linux (as of the 2.4.0 kernel):
180 <pre>
181 mount --bind /var/ftp/incoming /home/bob/incoming
182 mount --bind /var/ftp/incoming /home/dave/incoming
183 </pre>
184 or, alternatively:
185 <pre>
186 mount -o bind /var/ftp/incoming /home/bob/incoming
187 mount -o bind /var/ftp/incoming /home/dave/incoming
188 </pre>
189 </li>
190
191 <li>BSD (as of 4.4BSD):
192 <pre>
193 mount_null /var/ftp/incoming /home/bob/incoming
194 mount_null /var/ftp/incoming /home/dave/incoming
195 </pre>
196 </li>
197
198 <li>Solaris:
199 <pre>
200 mount -F lofs /var/ftp/incoming /home/bob/incoming
201 mount -F lofs /var/ftp/incoming /home/dave/incoming
202 </pre>
203 </li>
204 </ul>
205 The same technique can be used for <code>&lt;Anonymous&gt;</code> directories,
206 which also operate in a <code>chroot()</code>ed environment. Also, it
207 should be possible to mount specific <i>files</i> this way, in addition to
208 directories, should you need to (a directory is just another file in Unix).
209
210 <p>
211 As usual, more information can be found by consulting the man pages for
212 the appropriate command for your platform. The commands for other flavors
213 of Unix will be added as needed.
214
215 <p>
216 In order to have these tricks persist, to survive a system reboot, the
217 <code>/etc/fstab</code> (or <code>/etc/vfstab</code>) file may need to have
218 these mounts added. Consult your local <code>fstab(5)</code> (or
219 <code>vfstab(4)</code> for Solaris) man pages for more information.
220
221 <p>
222 <b>Chroots and Remote Filesystems</b><br>
223 If the chroot directories for your users happen to reside on an NFS
224 partition, then you need to make sure that root privileges are <b>not</b>
225 blocked (<i>e.g.</i> often referred to as "root squash") by the NFS
226 mount. Otherwise, the chroot will fail.
227
228 <p><a name="FAQ"></a>
229 <b>Frequently Asked Questions</b><br>
230
231 <p>
232 <font color=red>Question</font>: I am using the <code>DefaultRoot</code>
233 directive, but my logins are failing. The debug logging shows the following:
234 <pre>
235 USER <i>user</i>: Login successful.
236 Preparing to chroot to directory '/home/users/<i>user</i>'
237 <i>user</i> chroot("/home/users/<i>user</i>"): Permission denied
238 error: unable to set default root directory
239 </pre>
240 I am starting <code>proftpd</code> with root privileges, so why is the
241 <code>chroot()</code> failing with "Permission denied"?<br>
242 <font color=blue>Answer</font>: The above situation can happen in cases
243 where even root privileges are insufficient. There have been reports of
244 this happening for security-hardened systems (<i>e.g.</i> SELinux, OpenWall,
245 <i>etc</i>); it can <i>also</i> happen when NFS is involved.
246
247 <p>
248 If the <code>DefaultRoot</code> directory in question is mounted via NFS, make
249 sure that the NFS configuration mounts the directory with root privileges. The
250 <code>chroot(2)</code> system call <i>requires</i> root privileges; a
251 no-root-privs mounted NFS directory does not allow the <code>chroot(2)</code>
252 to succeed.
253
254 <p>
255 Similarly, instead of "Permission denied", you might see "No such file or
256 directory":
257 <pre>
258 <i>user</i> chroot("~"): No such file or directory
259 </pre>
260 The reasons for this error are explained
261 <a href="Authentication.html#homedir">here</a>.
262
263 <p><a name="MultipleRoots">
264 <font color=red>Question</font>: Is it possible to configure <code>DefaultRoot</code> for all users <i>except</i> some special users, which will have a different root directory?<br>
265 <font color=blue>Answer</font>: Yes, this is possible. ProFTPD supports
266 having multiple <code>DefaultRoot</code> directives in the proftpd.conf at the
267 same time; <code>proftpd</code> checks all of them
268 <i>in the order they appear</i>. The first one which matches the logging-in
269 user is applied.
270
271 <p>
272 To illustrate, here's an example. Keep in mind that the optional parameters to
273 the <code>DefaultRoot</code> directive are <i>group</i> names, <b>not</b> user
274 names.
275 <pre>
276 DefaultRoot /path/to/admin/dir admin-group
277 DefaultRoot /path/to/special/dir special-group
278 DefaultRoot ~ <font color=blue># everyone else</font>
279 </pre>
280
281 <p>
282 If the logging-in user is a member of group 'admin-group', then
283 <code>proftpd</code> will chroot to the <code>/path/to/admin/dir</code>
284 directory. If the logging-in user is not a member of group 'admin-group' but
285 <b>is</b> a member of group 'special-group', then
286 <code>/path/to/special/dir</code> is used for the chroot. And if the user
287 is not a member of either of these groups, then the normal home directory is
288 used for the chroot. It's always a good idea of have a "applies to everyone"
289 <code>DefaultRoot</code> directive in your proftpd.conf, at the
290 <i>end of the list</i> of <code>DefaultRoot</code>s, as a catch-all.
291
292 <p><a name="SymlinksFAQ">
293 <font color=red>Question</font>: Does <code>DefaultRoot</code> work properly if
294 the path/home directory is a symlink?<br>
295 <font color=blue>Answer</font>: Yes.
296
297 <p>
298 Note that some sites consider this a security risk; if that home directory
299 can be deleted by remote users, and replaced with a symlink of their own
300 creation (<i>e.g.</i> via SSH or some other webapp), this can be a problem.
301 To help mitigate situations like this, you can use the <a href="../modules/mod_auth.html#AllowChrootSymlinks"><code>AllowChrootSymlinks</code></a> directive:
302 <pre>
303 # Do not follow symlinks when chrooting
304 AllowChrootSymlinks off
305 </pre>
306 As stated in the documentation, using <code>AllowChrootSymlinks</code> does
307 <b>not</b> prevent this problem entirely; it simply means that
308 <i><code>proftpd</code></i> cannot be used to get around the restrictions.
309
310 <p><a name="ChrootNotWorking">
311 <font color=red>Question</font>: I have configured <code>DefaultRoot</code> in my proftpd.conf, but my clients still see the root directory. Is it a bug?<br>
312 <font color=blue>Answer</font>. Usually not.
313
314 <p>
315 First, make sure that you have restarted <code>proftpd</code>, so that the
316 config changes you made (<i>e.g.</i> adding/modifying your
317 <code>DefaultRoot</code> settings) are picked up by the running daemon.
318
319 <p>
320 Second, make sure that you have cleared any client cache. Many FTP clients
321 (especially browsers) will cache the directory listings that they have obtained
322 from an FTP server. Thus once you have restarted <code>proftpd</code> and
323 you still see the root filesystem displayed by your client, you need to make
324 sure that that client is actually getting that listing from the FTP server,
325 rather than showing you a stale/cached copy. The command-line
326 <code>ftp(1)</code> client is good for testing this situation, as it is very
327 simplistic and does not cache such things.
328
329 <p>
330 Last, double-check the proftpd <a href="Debugging.html">debug logging</a>.
331 It could be that <code>proftpd</code> is not using the configuration like you
332 assume it is. Maybe a different config file is being used than the one you
333 edited, or maybe the <code>DefaultRoot</code> directive is not in a
334 <code>&lt;Global&gt;</code> section and you are using
335 <code>&lt;VirtualHost&gt;</code> sections, <i>etc</i>.
336
337 <p>
338 <hr>
339 <font size=2><b><i>
340 &copy; Copyright 2017 The ProFTPD Project<br>
341 All Rights Reserved<br>
342 </i></b></font>
343 <hr>
344
345 </body>
346 </html>