[PATCH] fuse: add support for block device based filesystems
[pandora-kernel.git] / Documentation / filesystems / fuse.txt
1 Definitions
2 ~~~~~~~~~~~
3
4 Userspace filesystem:
5
6   A filesystem in which data and metadata are provided by an ordinary
7   userspace process.  The filesystem can be accessed normally through
8   the kernel interface.
9
10 Filesystem daemon:
11
12   The process(es) providing the data and metadata of the filesystem.
13
14 Non-privileged mount (or user mount):
15
16   A userspace filesystem mounted by a non-privileged (non-root) user.
17   The filesystem daemon is running with the privileges of the mounting
18   user.  NOTE: this is not the same as mounts allowed with the "user"
19   option in /etc/fstab, which is not discussed here.
20
21 Filesystem connection:
22
23   A connection between the filesystem daemon and the kernel.  The
24   connection exists until either the daemon dies, or the filesystem is
25   umounted.  Note that detaching (or lazy umounting) the filesystem
26   does _not_ break the connection, in this case it will exist until
27   the last reference to the filesystem is released.
28
29 Mount owner:
30
31   The user who does the mounting.
32
33 User:
34
35   The user who is performing filesystem operations.
36
37 What is FUSE?
38 ~~~~~~~~~~~~~
39
40 FUSE is a userspace filesystem framework.  It consists of a kernel
41 module (fuse.ko), a userspace library (libfuse.*) and a mount utility
42 (fusermount).
43
44 One of the most important features of FUSE is allowing secure,
45 non-privileged mounts.  This opens up new possibilities for the use of
46 filesystems.  A good example is sshfs: a secure network filesystem
47 using the sftp protocol.
48
49 The userspace library and utilities are available from the FUSE
50 homepage:
51
52   http://fuse.sourceforge.net/
53
54 Filesystem type
55 ~~~~~~~~~~~~~~~
56
57 The filesystem type given to mount(2) can be one of the following:
58
59 'fuse'
60
61   This is the usual way to mount a FUSE filesystem.  The first
62   argument of the mount system call may contain an arbitrary string,
63   which is not interpreted by the kernel.
64
65 'fuseblk'
66
67   The filesystem is block device based.  The first argument of the
68   mount system call is interpreted as the name of the device.
69
70 Mount options
71 ~~~~~~~~~~~~~
72
73 'fd=N'
74
75   The file descriptor to use for communication between the userspace
76   filesystem and the kernel.  The file descriptor must have been
77   obtained by opening the FUSE device ('/dev/fuse').
78
79 'rootmode=M'
80
81   The file mode of the filesystem's root in octal representation.
82
83 'user_id=N'
84
85   The numeric user id of the mount owner.
86
87 'group_id=N'
88
89   The numeric group id of the mount owner.
90
91 'default_permissions'
92
93   By default FUSE doesn't check file access permissions, the
94   filesystem is free to implement it's access policy or leave it to
95   the underlying file access mechanism (e.g. in case of network
96   filesystems).  This option enables permission checking, restricting
97   access based on file mode.  This is option is usually useful
98   together with the 'allow_other' mount option.
99
100 'allow_other'
101
102   This option overrides the security measure restricting file access
103   to the user mounting the filesystem.  This option is by default only
104   allowed to root, but this restriction can be removed with a
105   (userspace) configuration option.
106
107 'max_read=N'
108
109   With this option the maximum size of read operations can be set.
110   The default is infinite.  Note that the size of read requests is
111   limited anyway to 32 pages (which is 128kbyte on i386).
112
113 Control filesystem
114 ~~~~~~~~~~~~~~~~~~
115
116 There's a control filesystem for FUSE, which can be mounted by:
117
118   mount -t fusectl none /sys/fs/fuse/connections
119
120 Mounting it under the '/sys/fs/fuse/connections' directory makes it
121 backwards compatible with earlier versions.
122
123 Under the fuse control filesystem each connection has a directory
124 named by a unique number.
125
126 For each connection the following files exist within this directory:
127
128  'waiting'
129
130   The number of requests which are waiting to be transferred to
131   userspace or being processed by the filesystem daemon.  If there is
132   no filesystem activity and 'waiting' is non-zero, then the
133   filesystem is hung or deadlocked.
134
135  'abort'
136
137   Writing anything into this file will abort the filesystem
138   connection.  This means that all waiting requests will be aborted an
139   error returned for all aborted and new requests.
140
141 Only the owner of the mount may read or write these files.
142
143 Interrupting filesystem operations
144 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
145
146 If a process issuing a FUSE filesystem request is interrupted, the
147 following will happen:
148
149   1) If the request is not yet sent to userspace AND the signal is
150      fatal (SIGKILL or unhandled fatal signal), then the request is
151      dequeued and returns immediately.
152
153   2) If the request is not yet sent to userspace AND the signal is not
154      fatal, then an 'interrupted' flag is set for the request.  When
155      the request has been successfully transferred to userspace and
156      this flag is set, an INTERRUPT request is queued.
157
158   3) If the request is already sent to userspace, then an INTERRUPT
159      request is queued.
160
161 INTERRUPT requests take precedence over other requests, so the
162 userspace filesystem will receive queued INTERRUPTs before any others.
163
164 The userspace filesystem may ignore the INTERRUPT requests entirely,
165 or may honor them by sending a reply to the _original_ request, with
166 the error set to EINTR.
167
168 It is also possible that there's a race between processing the
169 original request and it's INTERRUPT request.  There are two possibilities:
170
171   1) The INTERRUPT request is processed before the original request is
172      processed
173
174   2) The INTERRUPT request is processed after the original request has
175      been answered
176
177 If the filesystem cannot find the original request, it should wait for
178 some timeout and/or a number of new requests to arrive, after which it
179 should reply to the INTERRUPT request with an EAGAIN error.  In case
180 1) the INTERRUPT request will be requeued.  In case 2) the INTERRUPT
181 reply will be ignored.
182
183 Aborting a filesystem connection
184 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
185
186 It is possible to get into certain situations where the filesystem is
187 not responding.  Reasons for this may be:
188
189   a) Broken userspace filesystem implementation
190
191   b) Network connection down
192
193   c) Accidental deadlock
194
195   d) Malicious deadlock
196
197 (For more on c) and d) see later sections)
198
199 In either of these cases it may be useful to abort the connection to
200 the filesystem.  There are several ways to do this:
201
202   - Kill the filesystem daemon.  Works in case of a) and b)
203
204   - Kill the filesystem daemon and all users of the filesystem.  Works
205     in all cases except some malicious deadlocks
206
207   - Use forced umount (umount -f).  Works in all cases but only if
208     filesystem is still attached (it hasn't been lazy unmounted)
209
210   - Abort filesystem through the FUSE control filesystem.  Most
211     powerful method, always works.
212
213 How do non-privileged mounts work?
214 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
215
216 Since the mount() system call is a privileged operation, a helper
217 program (fusermount) is needed, which is installed setuid root.
218
219 The implication of providing non-privileged mounts is that the mount
220 owner must not be able to use this capability to compromise the
221 system.  Obvious requirements arising from this are:
222
223  A) mount owner should not be able to get elevated privileges with the
224     help of the mounted filesystem
225
226  B) mount owner should not get illegitimate access to information from
227     other users' and the super user's processes
228
229  C) mount owner should not be able to induce undesired behavior in
230     other users' or the super user's processes
231
232 How are requirements fulfilled?
233 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
234
235  A) The mount owner could gain elevated privileges by either:
236
237      1) creating a filesystem containing a device file, then opening
238         this device
239
240      2) creating a filesystem containing a suid or sgid application,
241         then executing this application
242
243     The solution is not to allow opening device files and ignore
244     setuid and setgid bits when executing programs.  To ensure this
245     fusermount always adds "nosuid" and "nodev" to the mount options
246     for non-privileged mounts.
247
248  B) If another user is accessing files or directories in the
249     filesystem, the filesystem daemon serving requests can record the
250     exact sequence and timing of operations performed.  This
251     information is otherwise inaccessible to the mount owner, so this
252     counts as an information leak.
253
254     The solution to this problem will be presented in point 2) of C).
255
256  C) There are several ways in which the mount owner can induce
257     undesired behavior in other users' processes, such as:
258
259      1) mounting a filesystem over a file or directory which the mount
260         owner could otherwise not be able to modify (or could only
261         make limited modifications).
262
263         This is solved in fusermount, by checking the access
264         permissions on the mountpoint and only allowing the mount if
265         the mount owner can do unlimited modification (has write
266         access to the mountpoint, and mountpoint is not a "sticky"
267         directory)
268
269      2) Even if 1) is solved the mount owner can change the behavior
270         of other users' processes.
271
272          i) It can slow down or indefinitely delay the execution of a
273            filesystem operation creating a DoS against the user or the
274            whole system.  For example a suid application locking a
275            system file, and then accessing a file on the mount owner's
276            filesystem could be stopped, and thus causing the system
277            file to be locked forever.
278
279          ii) It can present files or directories of unlimited length, or
280            directory structures of unlimited depth, possibly causing a
281            system process to eat up diskspace, memory or other
282            resources, again causing DoS.
283
284         The solution to this as well as B) is not to allow processes
285         to access the filesystem, which could otherwise not be
286         monitored or manipulated by the mount owner.  Since if the
287         mount owner can ptrace a process, it can do all of the above
288         without using a FUSE mount, the same criteria as used in
289         ptrace can be used to check if a process is allowed to access
290         the filesystem or not.
291
292         Note that the ptrace check is not strictly necessary to
293         prevent B/2/i, it is enough to check if mount owner has enough
294         privilege to send signal to the process accessing the
295         filesystem, since SIGSTOP can be used to get a similar effect.
296
297 I think these limitations are unacceptable?
298 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
299
300 If a sysadmin trusts the users enough, or can ensure through other
301 measures, that system processes will never enter non-privileged
302 mounts, it can relax the last limitation with a "user_allow_other"
303 config option.  If this config option is set, the mounting user can
304 add the "allow_other" mount option which disables the check for other
305 users' processes.
306
307 Kernel - userspace interface
308 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
309
310 The following diagram shows how a filesystem operation (in this
311 example unlink) is performed in FUSE.
312
313 NOTE: everything in this description is greatly simplified
314
315  |  "rm /mnt/fuse/file"               |  FUSE filesystem daemon
316  |                                    |
317  |                                    |  >sys_read()
318  |                                    |    >fuse_dev_read()
319  |                                    |      >request_wait()
320  |                                    |        [sleep on fc->waitq]
321  |                                    |
322  |  >sys_unlink()                     |
323  |    >fuse_unlink()                  |
324  |      [get request from             |
325  |       fc->unused_list]             |
326  |      >request_send()               |
327  |        [queue req on fc->pending]  |
328  |        [wake up fc->waitq]         |        [woken up]
329  |        >request_wait_answer()      |
330  |          [sleep on req->waitq]     |
331  |                                    |      <request_wait()
332  |                                    |      [remove req from fc->pending]
333  |                                    |      [copy req to read buffer]
334  |                                    |      [add req to fc->processing]
335  |                                    |    <fuse_dev_read()
336  |                                    |  <sys_read()
337  |                                    |
338  |                                    |  [perform unlink]
339  |                                    |
340  |                                    |  >sys_write()
341  |                                    |    >fuse_dev_write()
342  |                                    |      [look up req in fc->processing]
343  |                                    |      [remove from fc->processing]
344  |                                    |      [copy write buffer to req]
345  |          [woken up]                |      [wake up req->waitq]
346  |                                    |    <fuse_dev_write()
347  |                                    |  <sys_write()
348  |        <request_wait_answer()      |
349  |      <request_send()               |
350  |      [add request to               |
351  |       fc->unused_list]             |
352  |    <fuse_unlink()                  |
353  |  <sys_unlink()                     |
354
355 There are a couple of ways in which to deadlock a FUSE filesystem.
356 Since we are talking about unprivileged userspace programs,
357 something must be done about these.
358
359 Scenario 1 -  Simple deadlock
360 -----------------------------
361
362  |  "rm /mnt/fuse/file"               |  FUSE filesystem daemon
363  |                                    |
364  |  >sys_unlink("/mnt/fuse/file")     |
365  |    [acquire inode semaphore        |
366  |     for "file"]                    |
367  |    >fuse_unlink()                  |
368  |      [sleep on req->waitq]         |
369  |                                    |  <sys_read()
370  |                                    |  >sys_unlink("/mnt/fuse/file")
371  |                                    |    [acquire inode semaphore
372  |                                    |     for "file"]
373  |                                    |    *DEADLOCK*
374
375 The solution for this is to allow the filesystem to be aborted.
376
377 Scenario 2 - Tricky deadlock
378 ----------------------------
379
380 This one needs a carefully crafted filesystem.  It's a variation on
381 the above, only the call back to the filesystem is not explicit,
382 but is caused by a pagefault.
383
384  |  Kamikaze filesystem thread 1      |  Kamikaze filesystem thread 2
385  |                                    |
386  |  [fd = open("/mnt/fuse/file")]     |  [request served normally]
387  |  [mmap fd to 'addr']               |
388  |  [close fd]                        |  [FLUSH triggers 'magic' flag]
389  |  [read a byte from addr]           |
390  |    >do_page_fault()                |
391  |      [find or create page]         |
392  |      [lock page]                   |
393  |      >fuse_readpage()              |
394  |         [queue READ request]       |
395  |         [sleep on req->waitq]      |
396  |                                    |  [read request to buffer]
397  |                                    |  [create reply header before addr]
398  |                                    |  >sys_write(addr - headerlength)
399  |                                    |    >fuse_dev_write()
400  |                                    |      [look up req in fc->processing]
401  |                                    |      [remove from fc->processing]
402  |                                    |      [copy write buffer to req]
403  |                                    |        >do_page_fault()
404  |                                    |           [find or create page]
405  |                                    |           [lock page]
406  |                                    |           * DEADLOCK *
407
408 Solution is basically the same as above.
409
410 An additional problem is that while the write buffer is being copied
411 to the request, the request must not be interrupted/aborted.  This is
412 because the destination address of the copy may not be valid after the
413 request has returned.
414
415 This is solved with doing the copy atomically, and allowing abort
416 while the page(s) belonging to the write buffer are faulted with
417 get_user_pages().  The 'req->locked' flag indicates when the copy is
418 taking place, and abort is delayed until this flag is unset.