blob: 8e455065ce9e2c089abee0df2bcf1ffada473e41 [file] [log] [blame]
Daniel W. S. Almeida8ab13bc2020-01-29 02:06:21 -03001.. SPDX-License-Identifier: GPL-2.0
2==============
3FUSE
4==============
5
Miklos Szeredi334f4852005-09-09 13:10:27 -07006Definitions
Daniel W. S. Almeida8ab13bc2020-01-29 02:06:21 -03007===========
Miklos Szeredi334f4852005-09-09 13:10:27 -07008
9Userspace filesystem:
Miklos Szeredi334f4852005-09-09 13:10:27 -070010 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:
Miklos Szeredi334f4852005-09-09 13:10:27 -070015 The process(es) providing the data and metadata of the filesystem.
16
17Non-privileged mount (or user mount):
Miklos Szeredi334f4852005-09-09 13:10:27 -070018 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
Miklos Szeredibafa9652006-06-25 05:48:51 -070023Filesystem connection:
Miklos Szeredibafa9652006-06-25 05:48:51 -070024 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
Daniel W. S. Almeida8ab13bc2020-01-29 02:06:21 -030027 does *not* break the connection, in this case it will exist until
Miklos Szeredibafa9652006-06-25 05:48:51 -070028 the last reference to the filesystem is released.
29
Miklos Szeredi334f4852005-09-09 13:10:27 -070030Mount owner:
Miklos Szeredi334f4852005-09-09 13:10:27 -070031 The user who does the mounting.
32
33User:
Miklos Szeredi334f4852005-09-09 13:10:27 -070034 The user who is performing filesystem operations.
35
36What is FUSE?
Daniel W. S. Almeida8ab13bc2020-01-29 02:06:21 -030037=============
Miklos Szeredi334f4852005-09-09 13:10:27 -070038
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
Daniel W. S. Almeida8ab13bc2020-01-29 02:06:21 -030048The userspace library and utilities are available from the
49`FUSE homepage: <http://fuse.sourceforge.net/>`_
Miklos Szeredi334f4852005-09-09 13:10:27 -070050
Miklos Szeredid6392f82006-12-06 20:35:44 -080051Filesystem type
Daniel W. S. Almeida8ab13bc2020-01-29 02:06:21 -030052===============
Miklos Szeredid6392f82006-12-06 20:35:44 -080053
54The filesystem type given to mount(2) can be one of the following:
55
Daniel W. S. Almeida8ab13bc2020-01-29 02:06:21 -030056 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.
Miklos Szeredid6392f82006-12-06 20:35:44 -080060
Daniel W. S. Almeida8ab13bc2020-01-29 02:06:21 -030061 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.
Miklos Szeredid6392f82006-12-06 20:35:44 -080064
Miklos Szeredi334f4852005-09-09 13:10:27 -070065Mount options
Daniel W. S. Almeida8ab13bc2020-01-29 02:06:21 -030066=============
Miklos Szeredi334f4852005-09-09 13:10:27 -070067
Daniel W. S. Almeida8ab13bc2020-01-29 02:06:21 -030068fd=N
Miklos Szeredi334f4852005-09-09 13:10:27 -070069 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
Daniel W. S. Almeida8ab13bc2020-01-29 02:06:21 -030073rootmode=M
Miklos Szeredi334f4852005-09-09 13:10:27 -070074 The file mode of the filesystem's root in octal representation.
75
Daniel W. S. Almeida8ab13bc2020-01-29 02:06:21 -030076user_id=N
Miklos Szeredi334f4852005-09-09 13:10:27 -070077 The numeric user id of the mount owner.
78
Daniel W. S. Almeida8ab13bc2020-01-29 02:06:21 -030079group_id=N
Miklos Szeredi334f4852005-09-09 13:10:27 -070080 The numeric group id of the mount owner.
81
Daniel W. S. Almeida8ab13bc2020-01-29 02:06:21 -030082default_permissions
Miklos Szeredi334f4852005-09-09 13:10:27 -070083 By default FUSE doesn't check file access permissions, the
Francis Galieguea33f3222010-04-23 00:08:02 +020084 filesystem is free to implement its access policy or leave it to
Miklos Szeredi334f4852005-09-09 13:10:27 -070085 the underlying file access mechanism (e.g. in case of network
86 filesystems). This option enables permission checking, restricting
Alexey Dobriyan91f6e542006-12-29 16:50:08 -080087 access based on file mode. It is usually useful together with the
88 'allow_other' mount option.
Miklos Szeredi334f4852005-09-09 13:10:27 -070089
Daniel W. S. Almeida8ab13bc2020-01-29 02:06:21 -030090allow_other
Miklos Szeredi334f4852005-09-09 13:10:27 -070091 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
Daniel W. S. Almeida8ab13bc2020-01-29 02:06:21 -030096max_read=N
Miklos Szeredi334f4852005-09-09 13:10:27 -070097 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
Daniel W. S. Almeida8ab13bc2020-01-29 02:06:21 -0300101blksize=N
Miklos Szeredid8091612006-12-06 20:35:48 -0800102 Set the block size for the filesystem. The default is 512. This
103 option is only valid for 'fuseblk' type mounts.
104
Miklos Szeredibafa9652006-06-25 05:48:51 -0700105Control filesystem
Daniel W. S. Almeida8ab13bc2020-01-29 02:06:21 -0300106==================
Miklos Szeredibacac382006-01-16 22:14:47 -0800107
Daniel W. S. Almeida8ab13bc2020-01-29 02:06:21 -0300108There's a control filesystem for FUSE, which can be mounted by::
Miklos Szeredibacac382006-01-16 22:14:47 -0800109
Miklos Szeredibafa9652006-06-25 05:48:51 -0700110 mount -t fusectl none /sys/fs/fuse/connections
Miklos Szeredibacac382006-01-16 22:14:47 -0800111
Miklos Szeredibafa9652006-06-25 05:48:51 -0700112Mounting it under the '/sys/fs/fuse/connections' directory makes it
113backwards compatible with earlier versions.
Miklos Szeredibacac382006-01-16 22:14:47 -0800114
Miklos Szeredibafa9652006-06-25 05:48:51 -0700115Under 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:
Miklos Szeredibacac382006-01-16 22:14:47 -0800119
Daniel W. S. Almeida8ab13bc2020-01-29 02:06:21 -0300120 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.
Miklos Szeredibacac382006-01-16 22:14:47 -0800125
Daniel W. S. Almeida8ab13bc2020-01-29 02:06:21 -0300126 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.
Miklos Szeredibacac382006-01-16 22:14:47 -0800130
Miklos Szeredibafa9652006-06-25 05:48:51 -0700131Only the owner of the mount may read or write these files.
Miklos Szeredibacac382006-01-16 22:14:47 -0800132
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700133Interrupting filesystem operations
Daniel W. S. Almeida8ab13bc2020-01-29 02:06:21 -0300134##################################
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700135
136If a process issuing a FUSE filesystem request is interrupted, the
137following will happen:
138
Daniel W. S. Almeida8ab13bc2020-01-29 02:06:21 -0300139 - If the request is not yet sent to userspace AND the signal is
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700140 fatal (SIGKILL or unhandled fatal signal), then the request is
141 dequeued and returns immediately.
142
Daniel W. S. Almeida8ab13bc2020-01-29 02:06:21 -0300143 - 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
Matt LaPlantefa00e7e2006-11-30 04:55:36 +0100145 the request has been successfully transferred to userspace and
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700146 this flag is set, an INTERRUPT request is queued.
147
Daniel W. S. Almeida8ab13bc2020-01-29 02:06:21 -0300148 - If the request is already sent to userspace, then an INTERRUPT
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700149 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,
Daniel W. S. Almeida8ab13bc2020-01-29 02:06:21 -0300155or may honor them by sending a reply to the *original* request, with
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700156the error set to EINTR.
157
158It is also possible that there's a race between processing the
Francis Galieguea33f3222010-04-23 00:08:02 +0200159original request and its INTERRUPT request. There are two possibilities:
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700160
Daniel W. S. Almeida8ab13bc2020-01-29 02:06:21 -0300161 1. The INTERRUPT request is processed before the original request is
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700162 processed
163
Daniel W. S. Almeida8ab13bc2020-01-29 02:06:21 -0300164 2. The INTERRUPT request is processed after the original request has
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700165 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
Miklos Szeredibacac382006-01-16 22:14:47 -0800173Aborting a filesystem connection
Daniel W. S. Almeida8ab13bc2020-01-29 02:06:21 -0300174================================
Miklos Szeredibacac382006-01-16 22:14:47 -0800175
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
Miklos Szeredibafa9652006-06-25 05:48:51 -0700200 - Abort filesystem through the FUSE control filesystem. Most
201 powerful method, always works.
Miklos Szeredibacac382006-01-16 22:14:47 -0800202
Miklos Szeredi334f4852005-09-09 13:10:27 -0700203How do non-privileged mounts work?
Daniel W. S. Almeida8ab13bc2020-01-29 02:06:21 -0300204==================================
Miklos Szeredi334f4852005-09-09 13:10:27 -0700205
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?
Daniel W. S. Almeida8ab13bc2020-01-29 02:06:21 -0300223===============================
Miklos Szeredi334f4852005-09-09 13:10:27 -0700224
225 A) The mount owner could gain elevated privileges by either:
226
Daniel W. S. Almeida8ab13bc2020-01-29 02:06:21 -0300227 1. creating a filesystem containing a device file, then opening this device
Miklos Szeredi334f4852005-09-09 13:10:27 -0700228
Daniel W. S. Almeida8ab13bc2020-01-29 02:06:21 -0300229 2. creating a filesystem containing a suid or sgid application, then executing this application
Miklos Szeredi334f4852005-09-09 13:10:27 -0700230
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
Daniel W. S. Almeida8ab13bc2020-01-29 02:06:21 -0300261 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.
Miklos Szeredi334f4852005-09-09 13:10:27 -0700266
267 ii) It can present files or directories of unlimited length, or
Daniel W. S. Almeida8ab13bc2020-01-29 02:06:21 -0300268 directory structures of unlimited depth, possibly causing a
269 system process to eat up diskspace, memory or other
270 resources, again causing *DoS*.
Miklos Szeredi334f4852005-09-09 13:10:27 -0700271
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
Daniel W. S. Almeida8ab13bc2020-01-29 02:06:21 -0300280 Note that the *ptrace* check is not strictly necessary to
Miklos Szeredi334f4852005-09-09 13:10:27 -0700281 prevent B/2/i, it is enough to check if mount owner has enough
282 privilege to send signal to the process accessing the
Daniel W. S. Almeida8ab13bc2020-01-29 02:06:21 -0300283 filesystem, since *SIGSTOP* can be used to get a similar effect.
Miklos Szeredi334f4852005-09-09 13:10:27 -0700284
285I think these limitations are unacceptable?
Daniel W. S. Almeida8ab13bc2020-01-29 02:06:21 -0300286===========================================
Miklos Szeredi334f4852005-09-09 13:10:27 -0700287
288If a sysadmin trusts the users enough, or can ensure through other
289measures, that system processes will never enter non-privileged
Daniel W. S. Almeida8ab13bc2020-01-29 02:06:21 -0300290mounts, it can relax the last limitation with a 'user_allow_other'
Miklos Szeredi334f4852005-09-09 13:10:27 -0700291config option. If this config option is set, the mounting user can
Daniel W. S. Almeida8ab13bc2020-01-29 02:06:21 -0300292add the 'allow_other' mount option which disables the check for other
Miklos Szeredi334f4852005-09-09 13:10:27 -0700293users' processes.
294
295Kernel - userspace interface
Daniel W. S. Almeida8ab13bc2020-01-29 02:06:21 -0300296============================
Miklos Szeredi334f4852005-09-09 13:10:27 -0700297
298The following diagram shows how a filesystem operation (in this
Daniel W. S. Almeida8ab13bc2020-01-29 02:06:21 -0300299example unlink) is performed in FUSE. ::
Miklos Szeredi334f4852005-09-09 13:10:27 -0700300
Miklos Szeredi334f4852005-09-09 13:10:27 -0700301
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
Daniel W. S. Almeida8ab13bc2020-01-29 02:06:21 -0300342.. note:: Everything in the description above is greatly simplified
343
Miklos Szeredi334f4852005-09-09 13:10:27 -0700344There 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
Daniel W. S. Almeida8ab13bc2020-01-29 02:06:21 -0300348**Scenario 1 - Simple deadlock**::
Miklos Szeredi334f4852005-09-09 13:10:27 -0700349
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
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700363The solution for this is to allow the filesystem to be aborted.
Miklos Szeredi334f4852005-09-09 13:10:27 -0700364
Daniel W. S. Almeida8ab13bc2020-01-29 02:06:21 -0300365**Scenario 2 - Tricky deadlock**
366
Miklos Szeredi334f4852005-09-09 13:10:27 -0700367
368This one needs a carefully crafted filesystem. It's a variation on
369the above, only the call back to the filesystem is not explicit,
Daniel W. S. Almeida8ab13bc2020-01-29 02:06:21 -0300370but is caused by a pagefault. ::
Miklos Szeredi334f4852005-09-09 13:10:27 -0700371
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
Daniel W. S. Almeida8ab13bc2020-01-29 02:06:21 -0300396The solution is basically the same as above.
Miklos Szeredi334f4852005-09-09 13:10:27 -0700397
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700398An 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.
Miklos Szeredi334f4852005-09-09 13:10:27 -0700402
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700403This 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.