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