brintos

brintos / linux-shallow public Read only

0
0
Text · 16.7 KiB · 1e31e87 Raw
427 lines · plain
1.. SPDX-License-Identifier: GPL-2.02 3====4FUSE5====6 7Definitions8===========9 10Userspace filesystem:11  A filesystem in which data and metadata are provided by an ordinary12  userspace process.  The filesystem can be accessed normally through13  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 mounting21  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.  The26  connection exists until either the daemon dies, or the filesystem is27  umounted.  Note that detaching (or lazy umounting) the filesystem28  does *not* break the connection, in this case it will exist until29  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 kernel41module (fuse.ko), a userspace library (libfuse.*) and a mount utility42(fusermount).43 44One of the most important features of FUSE is allowing secure,45non-privileged mounts.  This opens up new possibilities for the use of46filesystems.  A good example is sshfs: a secure network filesystem47using the sftp protocol.48 49The userspace library and utilities are available from the50`FUSE homepage: <https://github.com/libfuse/>`_51 52Filesystem type53===============54 55The filesystem type given to mount(2) can be one of the following:56 57    fuse58      This is the usual way to mount a FUSE filesystem.  The first59      argument of the mount system call may contain an arbitrary string,60      which is not interpreted by the kernel.61 62    fuseblk63      The filesystem is block device based.  The first argument of the64      mount system call is interpreted as the name of the device.65 66Mount options67=============68 69fd=N70  The file descriptor to use for communication between the userspace71  filesystem and the kernel.  The file descriptor must have been72  obtained by opening the FUSE device ('/dev/fuse').73 74rootmode=M75  The file mode of the filesystem's root in octal representation.76 77user_id=N78  The numeric user id of the mount owner.79 80group_id=N81  The numeric group id of the mount owner.82 83default_permissions84  By default FUSE doesn't check file access permissions, the85  filesystem is free to implement its access policy or leave it to86  the underlying file access mechanism (e.g. in case of network87  filesystems).  This option enables permission checking, restricting88  access based on file mode.  It is usually useful together with the89  'allow_other' mount option.90 91allow_other92  This option overrides the security measure restricting file access93  to the user mounting the filesystem.  This option is by default only94  allowed to root, but this restriction can be removed with a95  (userspace) configuration option.96 97max_read=N98  With this option the maximum size of read operations can be set.99  The default is infinite.  Note that the size of read requests is100  limited anyway to 32 pages (which is 128kbyte on i386).101 102blksize=N103  Set the block size for the filesystem.  The default is 512.  This104  option is only valid for 'fuseblk' type mounts.105 106Control filesystem107==================108 109There's a control filesystem for FUSE, which can be mounted by::110 111  mount -t fusectl none /sys/fs/fuse/connections112 113Mounting it under the '/sys/fs/fuse/connections' directory makes it114backwards compatible with earlier versions.115 116Under the fuse control filesystem each connection has a directory117named by a unique number.118 119For each connection the following files exist within this directory:120 121	waiting122	  The number of requests which are waiting to be transferred to123	  userspace or being processed by the filesystem daemon.  If there is124	  no filesystem activity and 'waiting' is non-zero, then the125	  filesystem is hung or deadlocked.126 127	abort128	  Writing anything into this file will abort the filesystem129	  connection.  This means that all waiting requests will be aborted an130	  error returned for all aborted and new requests.131 132Only the owner of the mount may read or write these files.133 134Interrupting filesystem operations135##################################136 137If a process issuing a FUSE filesystem request is interrupted, the138following will happen:139 140  -  If the request is not yet sent to userspace AND the signal is141     fatal (SIGKILL or unhandled fatal signal), then the request is142     dequeued and returns immediately.143 144  -  If the request is not yet sent to userspace AND the signal is not145     fatal, then an interrupted flag is set for the request.  When146     the request has been successfully transferred to userspace and147     this flag is set, an INTERRUPT request is queued.148 149  -  If the request is already sent to userspace, then an INTERRUPT150     request is queued.151 152INTERRUPT requests take precedence over other requests, so the153userspace filesystem will receive queued INTERRUPTs before any others.154 155The userspace filesystem may ignore the INTERRUPT requests entirely,156or may honor them by sending a reply to the *original* request, with157the error set to EINTR.158 159It is also possible that there's a race between processing the160original request and its INTERRUPT request.  There are two possibilities:161 162  1. The INTERRUPT request is processed before the original request is163     processed164 165  2. The INTERRUPT request is processed after the original request has166     been answered167 168If the filesystem cannot find the original request, it should wait for169some timeout and/or a number of new requests to arrive, after which it170should reply to the INTERRUPT request with an EAGAIN error.  In case1711) the INTERRUPT request will be requeued.  In case 2) the INTERRUPT172reply will be ignored.173 174Aborting a filesystem connection175================================176 177It is possible to get into certain situations where the filesystem is178not responding.  Reasons for this may be:179 180  a) Broken userspace filesystem implementation181 182  b) Network connection down183 184  c) Accidental deadlock185 186  d) Malicious deadlock187 188(For more on c) and d) see later sections)189 190In either of these cases it may be useful to abort the connection to191the filesystem.  There are several ways to do this:192 193  - Kill the filesystem daemon.  Works in case of a) and b)194 195  - Kill the filesystem daemon and all users of the filesystem.  Works196    in all cases except some malicious deadlocks197 198  - Use forced umount (umount -f).  Works in all cases but only if199    filesystem is still attached (it hasn't been lazy unmounted)200 201  - Abort filesystem through the FUSE control filesystem.  Most202    powerful method, always works.203 204How do non-privileged mounts work?205==================================206 207Since the mount() system call is a privileged operation, a helper208program (fusermount) is needed, which is installed setuid root.209 210The implication of providing non-privileged mounts is that the mount211owner must not be able to use this capability to compromise the212system.  Obvious requirements arising from this are:213 214 A) mount owner should not be able to get elevated privileges with the215    help of the mounted filesystem216 217 B) mount owner should not get illegitimate access to information from218    other users' and the super user's processes219 220 C) mount owner should not be able to induce undesired behavior in221    other users' or the super user's processes222 223How are requirements fulfilled?224===============================225 226 A) The mount owner could gain elevated privileges by either:227 228    1. creating a filesystem containing a device file, then opening this device229 230    2. creating a filesystem containing a suid or sgid application, then executing this application231 232    The solution is not to allow opening device files and ignore233    setuid and setgid bits when executing programs.  To ensure this234    fusermount always adds "nosuid" and "nodev" to the mount options235    for non-privileged mounts.236 237 B) If another user is accessing files or directories in the238    filesystem, the filesystem daemon serving requests can record the239    exact sequence and timing of operations performed.  This240    information is otherwise inaccessible to the mount owner, so this241    counts as an information leak.242 243    The solution to this problem will be presented in point 2) of C).244 245 C) There are several ways in which the mount owner can induce246    undesired behavior in other users' processes, such as:247 248     1) mounting a filesystem over a file or directory which the mount249        owner could otherwise not be able to modify (or could only250        make limited modifications).251 252        This is solved in fusermount, by checking the access253        permissions on the mountpoint and only allowing the mount if254        the mount owner can do unlimited modification (has write255        access to the mountpoint, and mountpoint is not a "sticky"256        directory)257 258     2) Even if 1) is solved the mount owner can change the behavior259        of other users' processes.260 261         i) It can slow down or indefinitely delay the execution of a262            filesystem operation creating a DoS against the user or the263            whole system.  For example a suid application locking a264            system file, and then accessing a file on the mount owner's265            filesystem could be stopped, and thus causing the system266            file to be locked forever.267 268         ii) It can present files or directories of unlimited length, or269             directory structures of unlimited depth, possibly causing a270             system process to eat up diskspace, memory or other271             resources, again causing *DoS*.272 273	The solution to this as well as B) is not to allow processes274	to access the filesystem, which could otherwise not be275	monitored or manipulated by the mount owner.  Since if the276	mount owner can ptrace a process, it can do all of the above277	without using a FUSE mount, the same criteria as used in278	ptrace can be used to check if a process is allowed to access279	the filesystem or not.280 281	Note that the *ptrace* check is not strictly necessary to282	prevent C/2/i, it is enough to check if mount owner has enough283	privilege to send signal to the process accessing the284	filesystem, since *SIGSTOP* can be used to get a similar effect.285 286I think these limitations are unacceptable?287===========================================288 289If a sysadmin trusts the users enough, or can ensure through other290measures, that system processes will never enter non-privileged291mounts, it can relax the last limitation in several ways:292 293  - With the 'user_allow_other' config option. If this config option is294    set, the mounting user can add the 'allow_other' mount option which295    disables the check for other users' processes.296 297    User namespaces have an unintuitive interaction with 'allow_other':298    an unprivileged user - normally restricted from mounting with299    'allow_other' - could do so in a user namespace where they're300    privileged. If any process could access such an 'allow_other' mount301    this would give the mounting user the ability to manipulate302    processes in user namespaces where they're unprivileged. For this303    reason 'allow_other' restricts access to users in the same userns304    or a descendant.305 306  - With the 'allow_sys_admin_access' module option. If this option is307    set, super user's processes have unrestricted access to mounts308    irrespective of allow_other setting or user namespace of the309    mounting user.310 311Note that both of these relaxations expose the system to potential312information leak or *DoS* as described in points B and C/2/i-ii in the313preceding section.314 315Kernel - userspace interface316============================317 318The following diagram shows how a filesystem operation (in this319example unlink) is performed in FUSE. ::320 321 322 |  "rm /mnt/fuse/file"               |  FUSE filesystem daemon323 |                                    |324 |                                    |  >sys_read()325 |                                    |    >fuse_dev_read()326 |                                    |      >request_wait()327 |                                    |        [sleep on fc->waitq]328 |                                    |329 |  >sys_unlink()                     |330 |    >fuse_unlink()                  |331 |      [get request from             |332 |       fc->unused_list]             |333 |      >request_send()               |334 |        [queue req on fc->pending]  |335 |        [wake up fc->waitq]         |        [woken up]336 |        >request_wait_answer()      |337 |          [sleep on req->waitq]     |338 |                                    |      <request_wait()339 |                                    |      [remove req from fc->pending]340 |                                    |      [copy req to read buffer]341 |                                    |      [add req to fc->processing]342 |                                    |    <fuse_dev_read()343 |                                    |  <sys_read()344 |                                    |345 |                                    |  [perform unlink]346 |                                    |347 |                                    |  >sys_write()348 |                                    |    >fuse_dev_write()349 |                                    |      [look up req in fc->processing]350 |                                    |      [remove from fc->processing]351 |                                    |      [copy write buffer to req]352 |          [woken up]                |      [wake up req->waitq]353 |                                    |    <fuse_dev_write()354 |                                    |  <sys_write()355 |        <request_wait_answer()      |356 |      <request_send()               |357 |      [add request to               |358 |       fc->unused_list]             |359 |    <fuse_unlink()                  |360 |  <sys_unlink()                     |361 362.. note:: Everything in the description above is greatly simplified363 364There are a couple of ways in which to deadlock a FUSE filesystem.365Since we are talking about unprivileged userspace programs,366something must be done about these.367 368**Scenario 1 -  Simple deadlock**::369 370 |  "rm /mnt/fuse/file"               |  FUSE filesystem daemon371 |                                    |372 |  >sys_unlink("/mnt/fuse/file")     |373 |    [acquire inode semaphore        |374 |     for "file"]                    |375 |    >fuse_unlink()                  |376 |      [sleep on req->waitq]         |377 |                                    |  <sys_read()378 |                                    |  >sys_unlink("/mnt/fuse/file")379 |                                    |    [acquire inode semaphore380 |                                    |     for "file"]381 |                                    |    *DEADLOCK*382 383The solution for this is to allow the filesystem to be aborted.384 385**Scenario 2 - Tricky deadlock**386 387 388This one needs a carefully crafted filesystem.  It's a variation on389the above, only the call back to the filesystem is not explicit,390but is caused by a pagefault. ::391 392 |  Kamikaze filesystem thread 1      |  Kamikaze filesystem thread 2393 |                                    |394 |  [fd = open("/mnt/fuse/file")]     |  [request served normally]395 |  [mmap fd to 'addr']               |396 |  [close fd]                        |  [FLUSH triggers 'magic' flag]397 |  [read a byte from addr]           |398 |    >do_page_fault()                |399 |      [find or create page]         |400 |      [lock page]                   |401 |      >fuse_readpage()              |402 |         [queue READ request]       |403 |         [sleep on req->waitq]      |404 |                                    |  [read request to buffer]405 |                                    |  [create reply header before addr]406 |                                    |  >sys_write(addr - headerlength)407 |                                    |    >fuse_dev_write()408 |                                    |      [look up req in fc->processing]409 |                                    |      [remove from fc->processing]410 |                                    |      [copy write buffer to req]411 |                                    |        >do_page_fault()412 |                                    |           [find or create page]413 |                                    |           [lock page]414 |                                    |           * DEADLOCK *415 416The solution is basically the same as above.417 418An additional problem is that while the write buffer is being copied419to the request, the request must not be interrupted/aborted.  This is420because the destination address of the copy may not be valid after the421request has returned.422 423This is solved with doing the copy atomically, and allowing abort424while the page(s) belonging to the write buffer are faulted with425get_user_pages().  The 'req->locked' flag indicates when the copy is426taking place, and abort is delayed until this flag is unset.427