Back to home page

OSCL-LXR

 
 

    


0001 .. SPDX-License-Identifier: GPL-2.0
0002 
0003 ====
0004 FUSE
0005 ====
0006 
0007 Definitions
0008 ===========
0009 
0010 Userspace filesystem:
0011   A filesystem in which data and metadata are provided by an ordinary
0012   userspace process.  The filesystem can be accessed normally through
0013   the kernel interface.
0014 
0015 Filesystem daemon:
0016   The process(es) providing the data and metadata of the filesystem.
0017 
0018 Non-privileged mount (or user mount):
0019   A userspace filesystem mounted by a non-privileged (non-root) user.
0020   The filesystem daemon is running with the privileges of the mounting
0021   user.  NOTE: this is not the same as mounts allowed with the "user"
0022   option in /etc/fstab, which is not discussed here.
0023 
0024 Filesystem connection:
0025   A connection between the filesystem daemon and the kernel.  The
0026   connection exists until either the daemon dies, or the filesystem is
0027   umounted.  Note that detaching (or lazy umounting) the filesystem
0028   does *not* break the connection, in this case it will exist until
0029   the last reference to the filesystem is released.
0030 
0031 Mount owner:
0032   The user who does the mounting.
0033 
0034 User:
0035   The user who is performing filesystem operations.
0036 
0037 What is FUSE?
0038 =============
0039 
0040 FUSE is a userspace filesystem framework.  It consists of a kernel
0041 module (fuse.ko), a userspace library (libfuse.*) and a mount utility
0042 (fusermount).
0043 
0044 One of the most important features of FUSE is allowing secure,
0045 non-privileged mounts.  This opens up new possibilities for the use of
0046 filesystems.  A good example is sshfs: a secure network filesystem
0047 using the sftp protocol.
0048 
0049 The userspace library and utilities are available from the
0050 `FUSE homepage: <https://github.com/libfuse/>`_
0051 
0052 Filesystem type
0053 ===============
0054 
0055 The filesystem type given to mount(2) can be one of the following:
0056 
0057     fuse
0058       This is the usual way to mount a FUSE filesystem.  The first
0059       argument of the mount system call may contain an arbitrary string,
0060       which is not interpreted by the kernel.
0061 
0062     fuseblk
0063       The filesystem is block device based.  The first argument of the
0064       mount system call is interpreted as the name of the device.
0065 
0066 Mount options
0067 =============
0068 
0069 fd=N
0070   The file descriptor to use for communication between the userspace
0071   filesystem and the kernel.  The file descriptor must have been
0072   obtained by opening the FUSE device ('/dev/fuse').
0073 
0074 rootmode=M
0075   The file mode of the filesystem's root in octal representation.
0076 
0077 user_id=N
0078   The numeric user id of the mount owner.
0079 
0080 group_id=N
0081   The numeric group id of the mount owner.
0082 
0083 default_permissions
0084   By default FUSE doesn't check file access permissions, the
0085   filesystem is free to implement its access policy or leave it to
0086   the underlying file access mechanism (e.g. in case of network
0087   filesystems).  This option enables permission checking, restricting
0088   access based on file mode.  It is usually useful together with the
0089   'allow_other' mount option.
0090 
0091 allow_other
0092   This option overrides the security measure restricting file access
0093   to the user mounting the filesystem.  This option is by default only
0094   allowed to root, but this restriction can be removed with a
0095   (userspace) configuration option.
0096 
0097 max_read=N
0098   With this option the maximum size of read operations can be set.
0099   The default is infinite.  Note that the size of read requests is
0100   limited anyway to 32 pages (which is 128kbyte on i386).
0101 
0102 blksize=N
0103   Set the block size for the filesystem.  The default is 512.  This
0104   option is only valid for 'fuseblk' type mounts.
0105 
0106 Control filesystem
0107 ==================
0108 
0109 There's a control filesystem for FUSE, which can be mounted by::
0110 
0111   mount -t fusectl none /sys/fs/fuse/connections
0112 
0113 Mounting it under the '/sys/fs/fuse/connections' directory makes it
0114 backwards compatible with earlier versions.
0115 
0116 Under the fuse control filesystem each connection has a directory
0117 named by a unique number.
0118 
0119 For each connection the following files exist within this directory:
0120 
0121         waiting
0122           The number of requests which are waiting to be transferred to
0123           userspace or being processed by the filesystem daemon.  If there is
0124           no filesystem activity and 'waiting' is non-zero, then the
0125           filesystem is hung or deadlocked.
0126 
0127         abort
0128           Writing anything into this file will abort the filesystem
0129           connection.  This means that all waiting requests will be aborted an
0130           error returned for all aborted and new requests.
0131 
0132 Only the owner of the mount may read or write these files.
0133 
0134 Interrupting filesystem operations
0135 ##################################
0136 
0137 If a process issuing a FUSE filesystem request is interrupted, the
0138 following will happen:
0139 
0140   -  If the request is not yet sent to userspace AND the signal is
0141      fatal (SIGKILL or unhandled fatal signal), then the request is
0142      dequeued and returns immediately.
0143 
0144   -  If the request is not yet sent to userspace AND the signal is not
0145      fatal, then an interrupted flag is set for the request.  When
0146      the request has been successfully transferred to userspace and
0147      this flag is set, an INTERRUPT request is queued.
0148 
0149   -  If the request is already sent to userspace, then an INTERRUPT
0150      request is queued.
0151 
0152 INTERRUPT requests take precedence over other requests, so the
0153 userspace filesystem will receive queued INTERRUPTs before any others.
0154 
0155 The userspace filesystem may ignore the INTERRUPT requests entirely,
0156 or may honor them by sending a reply to the *original* request, with
0157 the error set to EINTR.
0158 
0159 It is also possible that there's a race between processing the
0160 original request and its INTERRUPT request.  There are two possibilities:
0161 
0162   1. The INTERRUPT request is processed before the original request is
0163      processed
0164 
0165   2. The INTERRUPT request is processed after the original request has
0166      been answered
0167 
0168 If the filesystem cannot find the original request, it should wait for
0169 some timeout and/or a number of new requests to arrive, after which it
0170 should reply to the INTERRUPT request with an EAGAIN error.  In case
0171 1) the INTERRUPT request will be requeued.  In case 2) the INTERRUPT
0172 reply will be ignored.
0173 
0174 Aborting a filesystem connection
0175 ================================
0176 
0177 It is possible to get into certain situations where the filesystem is
0178 not responding.  Reasons for this may be:
0179 
0180   a) Broken userspace filesystem implementation
0181 
0182   b) Network connection down
0183 
0184   c) Accidental deadlock
0185 
0186   d) Malicious deadlock
0187 
0188 (For more on c) and d) see later sections)
0189 
0190 In either of these cases it may be useful to abort the connection to
0191 the filesystem.  There are several ways to do this:
0192 
0193   - Kill the filesystem daemon.  Works in case of a) and b)
0194 
0195   - Kill the filesystem daemon and all users of the filesystem.  Works
0196     in all cases except some malicious deadlocks
0197 
0198   - Use forced umount (umount -f).  Works in all cases but only if
0199     filesystem is still attached (it hasn't been lazy unmounted)
0200 
0201   - Abort filesystem through the FUSE control filesystem.  Most
0202     powerful method, always works.
0203 
0204 How do non-privileged mounts work?
0205 ==================================
0206 
0207 Since the mount() system call is a privileged operation, a helper
0208 program (fusermount) is needed, which is installed setuid root.
0209 
0210 The implication of providing non-privileged mounts is that the mount
0211 owner must not be able to use this capability to compromise the
0212 system.  Obvious requirements arising from this are:
0213 
0214  A) mount owner should not be able to get elevated privileges with the
0215     help of the mounted filesystem
0216 
0217  B) mount owner should not get illegitimate access to information from
0218     other users' and the super user's processes
0219 
0220  C) mount owner should not be able to induce undesired behavior in
0221     other users' or the super user's processes
0222 
0223 How are requirements fulfilled?
0224 ===============================
0225 
0226  A) The mount owner could gain elevated privileges by either:
0227 
0228     1. creating a filesystem containing a device file, then opening this device
0229 
0230     2. creating a filesystem containing a suid or sgid application, then executing this application
0231 
0232     The solution is not to allow opening device files and ignore
0233     setuid and setgid bits when executing programs.  To ensure this
0234     fusermount always adds "nosuid" and "nodev" to the mount options
0235     for non-privileged mounts.
0236 
0237  B) If another user is accessing files or directories in the
0238     filesystem, the filesystem daemon serving requests can record the
0239     exact sequence and timing of operations performed.  This
0240     information is otherwise inaccessible to the mount owner, so this
0241     counts as an information leak.
0242 
0243     The solution to this problem will be presented in point 2) of C).
0244 
0245  C) There are several ways in which the mount owner can induce
0246     undesired behavior in other users' processes, such as:
0247 
0248      1) mounting a filesystem over a file or directory which the mount
0249         owner could otherwise not be able to modify (or could only
0250         make limited modifications).
0251 
0252         This is solved in fusermount, by checking the access
0253         permissions on the mountpoint and only allowing the mount if
0254         the mount owner can do unlimited modification (has write
0255         access to the mountpoint, and mountpoint is not a "sticky"
0256         directory)
0257 
0258      2) Even if 1) is solved the mount owner can change the behavior
0259         of other users' processes.
0260 
0261          i) It can slow down or indefinitely delay the execution of a
0262             filesystem operation creating a DoS against the user or the
0263             whole system.  For example a suid application locking a
0264             system file, and then accessing a file on the mount owner's
0265             filesystem could be stopped, and thus causing the system
0266             file to be locked forever.
0267 
0268          ii) It can present files or directories of unlimited length, or
0269              directory structures of unlimited depth, possibly causing a
0270              system process to eat up diskspace, memory or other
0271              resources, again causing *DoS*.
0272 
0273         The solution to this as well as B) is not to allow processes
0274         to access the filesystem, which could otherwise not be
0275         monitored or manipulated by the mount owner.  Since if the
0276         mount owner can ptrace a process, it can do all of the above
0277         without using a FUSE mount, the same criteria as used in
0278         ptrace can be used to check if a process is allowed to access
0279         the filesystem or not.
0280 
0281         Note that the *ptrace* check is not strictly necessary to
0282         prevent C/2/i, it is enough to check if mount owner has enough
0283         privilege to send signal to the process accessing the
0284         filesystem, since *SIGSTOP* can be used to get a similar effect.
0285 
0286 I think these limitations are unacceptable?
0287 ===========================================
0288 
0289 If a sysadmin trusts the users enough, or can ensure through other
0290 measures, that system processes will never enter non-privileged
0291 mounts, it can relax the last limitation in several ways:
0292 
0293   - With the 'user_allow_other' config option. If this config option is
0294     set, the mounting user can add the 'allow_other' mount option which
0295     disables the check for other users' processes.
0296 
0297     User namespaces have an unintuitive interaction with 'allow_other':
0298     an unprivileged user - normally restricted from mounting with
0299     'allow_other' - could do so in a user namespace where they're
0300     privileged. If any process could access such an 'allow_other' mount
0301     this would give the mounting user the ability to manipulate
0302     processes in user namespaces where they're unprivileged. For this
0303     reason 'allow_other' restricts access to users in the same userns
0304     or a descendant.
0305 
0306   - With the 'allow_sys_admin_access' module option. If this option is
0307     set, super user's processes have unrestricted access to mounts
0308     irrespective of allow_other setting or user namespace of the
0309     mounting user.
0310 
0311 Note that both of these relaxations expose the system to potential
0312 information leak or *DoS* as described in points B and C/2/i-ii in the
0313 preceding section.
0314 
0315 Kernel - userspace interface
0316 ============================
0317 
0318 The following diagram shows how a filesystem operation (in this
0319 example unlink) is performed in FUSE. ::
0320 
0321 
0322  |  "rm /mnt/fuse/file"               |  FUSE filesystem daemon
0323  |                                    |
0324  |                                    |  >sys_read()
0325  |                                    |    >fuse_dev_read()
0326  |                                    |      >request_wait()
0327  |                                    |        [sleep on fc->waitq]
0328  |                                    |
0329  |  >sys_unlink()                     |
0330  |    >fuse_unlink()                  |
0331  |      [get request from             |
0332  |       fc->unused_list]             |
0333  |      >request_send()               |
0334  |        [queue req on fc->pending]  |
0335  |        [wake up fc->waitq]         |        [woken up]
0336  |        >request_wait_answer()      |
0337  |          [sleep on req->waitq]     |
0338  |                                    |      <request_wait()
0339  |                                    |      [remove req from fc->pending]
0340  |                                    |      [copy req to read buffer]
0341  |                                    |      [add req to fc->processing]
0342  |                                    |    <fuse_dev_read()
0343  |                                    |  <sys_read()
0344  |                                    |
0345  |                                    |  [perform unlink]
0346  |                                    |
0347  |                                    |  >sys_write()
0348  |                                    |    >fuse_dev_write()
0349  |                                    |      [look up req in fc->processing]
0350  |                                    |      [remove from fc->processing]
0351  |                                    |      [copy write buffer to req]
0352  |          [woken up]                |      [wake up req->waitq]
0353  |                                    |    <fuse_dev_write()
0354  |                                    |  <sys_write()
0355  |        <request_wait_answer()      |
0356  |      <request_send()               |
0357  |      [add request to               |
0358  |       fc->unused_list]             |
0359  |    <fuse_unlink()                  |
0360  |  <sys_unlink()                     |
0361 
0362 .. note:: Everything in the description above is greatly simplified
0363 
0364 There are a couple of ways in which to deadlock a FUSE filesystem.
0365 Since we are talking about unprivileged userspace programs,
0366 something must be done about these.
0367 
0368 **Scenario 1 -  Simple deadlock**::
0369 
0370  |  "rm /mnt/fuse/file"               |  FUSE filesystem daemon
0371  |                                    |
0372  |  >sys_unlink("/mnt/fuse/file")     |
0373  |    [acquire inode semaphore        |
0374  |     for "file"]                    |
0375  |    >fuse_unlink()                  |
0376  |      [sleep on req->waitq]         |
0377  |                                    |  <sys_read()
0378  |                                    |  >sys_unlink("/mnt/fuse/file")
0379  |                                    |    [acquire inode semaphore
0380  |                                    |     for "file"]
0381  |                                    |    *DEADLOCK*
0382 
0383 The solution for this is to allow the filesystem to be aborted.
0384 
0385 **Scenario 2 - Tricky deadlock**
0386 
0387 
0388 This one needs a carefully crafted filesystem.  It's a variation on
0389 the above, only the call back to the filesystem is not explicit,
0390 but is caused by a pagefault. ::
0391 
0392  |  Kamikaze filesystem thread 1      |  Kamikaze filesystem thread 2
0393  |                                    |
0394  |  [fd = open("/mnt/fuse/file")]     |  [request served normally]
0395  |  [mmap fd to 'addr']               |
0396  |  [close fd]                        |  [FLUSH triggers 'magic' flag]
0397  |  [read a byte from addr]           |
0398  |    >do_page_fault()                |
0399  |      [find or create page]         |
0400  |      [lock page]                   |
0401  |      >fuse_readpage()              |
0402  |         [queue READ request]       |
0403  |         [sleep on req->waitq]      |
0404  |                                    |  [read request to buffer]
0405  |                                    |  [create reply header before addr]
0406  |                                    |  >sys_write(addr - headerlength)
0407  |                                    |    >fuse_dev_write()
0408  |                                    |      [look up req in fc->processing]
0409  |                                    |      [remove from fc->processing]
0410  |                                    |      [copy write buffer to req]
0411  |                                    |        >do_page_fault()
0412  |                                    |           [find or create page]
0413  |                                    |           [lock page]
0414  |                                    |           * DEADLOCK *
0415 
0416 The solution is basically the same as above.
0417 
0418 An additional problem is that while the write buffer is being copied
0419 to the request, the request must not be interrupted/aborted.  This is
0420 because the destination address of the copy may not be valid after the
0421 request has returned.
0422 
0423 This is solved with doing the copy atomically, and allowing abort
0424 while the page(s) belonging to the write buffer are faulted with
0425 get_user_pages().  The 'req->locked' flag indicates when the copy is
0426 taking place, and abort is delayed until this flag is unset.