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