Daniel W. S. Almeida | 8ab13bc | 2020-01-29 02:06:21 -0300 | [diff] [blame^] | 1 | .. SPDX-License-Identifier: GPL-2.0 |
| 2 | ============== |
| 3 | FUSE |
| 4 | ============== |
| 5 | |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 6 | Definitions |
Daniel W. S. Almeida | 8ab13bc | 2020-01-29 02:06:21 -0300 | [diff] [blame^] | 7 | =========== |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 8 | |
| 9 | Userspace filesystem: |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 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 | |
| 14 | Filesystem daemon: |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 15 | The process(es) providing the data and metadata of the filesystem. |
| 16 | |
| 17 | Non-privileged mount (or user mount): |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 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 | |
Miklos Szeredi | bafa965 | 2006-06-25 05:48:51 -0700 | [diff] [blame] | 23 | Filesystem connection: |
Miklos Szeredi | bafa965 | 2006-06-25 05:48:51 -0700 | [diff] [blame] | 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 |
Daniel W. S. Almeida | 8ab13bc | 2020-01-29 02:06:21 -0300 | [diff] [blame^] | 27 | does *not* break the connection, in this case it will exist until |
Miklos Szeredi | bafa965 | 2006-06-25 05:48:51 -0700 | [diff] [blame] | 28 | the last reference to the filesystem is released. |
| 29 | |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 30 | Mount owner: |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 31 | The user who does the mounting. |
| 32 | |
| 33 | User: |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 34 | The user who is performing filesystem operations. |
| 35 | |
| 36 | What is FUSE? |
Daniel W. S. Almeida | 8ab13bc | 2020-01-29 02:06:21 -0300 | [diff] [blame^] | 37 | ============= |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 38 | |
| 39 | FUSE is a userspace filesystem framework. It consists of a kernel |
| 40 | module (fuse.ko), a userspace library (libfuse.*) and a mount utility |
| 41 | (fusermount). |
| 42 | |
| 43 | One of the most important features of FUSE is allowing secure, |
| 44 | non-privileged mounts. This opens up new possibilities for the use of |
| 45 | filesystems. A good example is sshfs: a secure network filesystem |
| 46 | using the sftp protocol. |
| 47 | |
Daniel W. S. Almeida | 8ab13bc | 2020-01-29 02:06:21 -0300 | [diff] [blame^] | 48 | The userspace library and utilities are available from the |
| 49 | `FUSE homepage: <http://fuse.sourceforge.net/>`_ |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 50 | |
Miklos Szeredi | d6392f8 | 2006-12-06 20:35:44 -0800 | [diff] [blame] | 51 | Filesystem type |
Daniel W. S. Almeida | 8ab13bc | 2020-01-29 02:06:21 -0300 | [diff] [blame^] | 52 | =============== |
Miklos Szeredi | d6392f8 | 2006-12-06 20:35:44 -0800 | [diff] [blame] | 53 | |
| 54 | The filesystem type given to mount(2) can be one of the following: |
| 55 | |
Daniel W. S. Almeida | 8ab13bc | 2020-01-29 02:06:21 -0300 | [diff] [blame^] | 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. |
Miklos Szeredi | d6392f8 | 2006-12-06 20:35:44 -0800 | [diff] [blame] | 60 | |
Daniel W. S. Almeida | 8ab13bc | 2020-01-29 02:06:21 -0300 | [diff] [blame^] | 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. |
Miklos Szeredi | d6392f8 | 2006-12-06 20:35:44 -0800 | [diff] [blame] | 64 | |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 65 | Mount options |
Daniel W. S. Almeida | 8ab13bc | 2020-01-29 02:06:21 -0300 | [diff] [blame^] | 66 | ============= |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 67 | |
Daniel W. S. Almeida | 8ab13bc | 2020-01-29 02:06:21 -0300 | [diff] [blame^] | 68 | fd=N |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 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 | |
Daniel W. S. Almeida | 8ab13bc | 2020-01-29 02:06:21 -0300 | [diff] [blame^] | 73 | rootmode=M |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 74 | The file mode of the filesystem's root in octal representation. |
| 75 | |
Daniel W. S. Almeida | 8ab13bc | 2020-01-29 02:06:21 -0300 | [diff] [blame^] | 76 | user_id=N |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 77 | The numeric user id of the mount owner. |
| 78 | |
Daniel W. S. Almeida | 8ab13bc | 2020-01-29 02:06:21 -0300 | [diff] [blame^] | 79 | group_id=N |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 80 | The numeric group id of the mount owner. |
| 81 | |
Daniel W. S. Almeida | 8ab13bc | 2020-01-29 02:06:21 -0300 | [diff] [blame^] | 82 | default_permissions |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 83 | By default FUSE doesn't check file access permissions, the |
Francis Galiegue | a33f322 | 2010-04-23 00:08:02 +0200 | [diff] [blame] | 84 | filesystem is free to implement its access policy or leave it to |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 85 | the underlying file access mechanism (e.g. in case of network |
| 86 | filesystems). This option enables permission checking, restricting |
Alexey Dobriyan | 91f6e54 | 2006-12-29 16:50:08 -0800 | [diff] [blame] | 87 | access based on file mode. It is usually useful together with the |
| 88 | 'allow_other' mount option. |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 89 | |
Daniel W. S. Almeida | 8ab13bc | 2020-01-29 02:06:21 -0300 | [diff] [blame^] | 90 | allow_other |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 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 | |
Daniel W. S. Almeida | 8ab13bc | 2020-01-29 02:06:21 -0300 | [diff] [blame^] | 96 | max_read=N |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 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 | |
Daniel W. S. Almeida | 8ab13bc | 2020-01-29 02:06:21 -0300 | [diff] [blame^] | 101 | blksize=N |
Miklos Szeredi | d809161 | 2006-12-06 20:35:48 -0800 | [diff] [blame] | 102 | Set the block size for the filesystem. The default is 512. This |
| 103 | option is only valid for 'fuseblk' type mounts. |
| 104 | |
Miklos Szeredi | bafa965 | 2006-06-25 05:48:51 -0700 | [diff] [blame] | 105 | Control filesystem |
Daniel W. S. Almeida | 8ab13bc | 2020-01-29 02:06:21 -0300 | [diff] [blame^] | 106 | ================== |
Miklos Szeredi | bacac38 | 2006-01-16 22:14:47 -0800 | [diff] [blame] | 107 | |
Daniel W. S. Almeida | 8ab13bc | 2020-01-29 02:06:21 -0300 | [diff] [blame^] | 108 | There's a control filesystem for FUSE, which can be mounted by:: |
Miklos Szeredi | bacac38 | 2006-01-16 22:14:47 -0800 | [diff] [blame] | 109 | |
Miklos Szeredi | bafa965 | 2006-06-25 05:48:51 -0700 | [diff] [blame] | 110 | mount -t fusectl none /sys/fs/fuse/connections |
Miklos Szeredi | bacac38 | 2006-01-16 22:14:47 -0800 | [diff] [blame] | 111 | |
Miklos Szeredi | bafa965 | 2006-06-25 05:48:51 -0700 | [diff] [blame] | 112 | Mounting it under the '/sys/fs/fuse/connections' directory makes it |
| 113 | backwards compatible with earlier versions. |
Miklos Szeredi | bacac38 | 2006-01-16 22:14:47 -0800 | [diff] [blame] | 114 | |
Miklos Szeredi | bafa965 | 2006-06-25 05:48:51 -0700 | [diff] [blame] | 115 | Under the fuse control filesystem each connection has a directory |
| 116 | named by a unique number. |
| 117 | |
| 118 | For each connection the following files exist within this directory: |
Miklos Szeredi | bacac38 | 2006-01-16 22:14:47 -0800 | [diff] [blame] | 119 | |
Daniel W. S. Almeida | 8ab13bc | 2020-01-29 02:06:21 -0300 | [diff] [blame^] | 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. |
Miklos Szeredi | bacac38 | 2006-01-16 22:14:47 -0800 | [diff] [blame] | 125 | |
Daniel W. S. Almeida | 8ab13bc | 2020-01-29 02:06:21 -0300 | [diff] [blame^] | 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. |
Miklos Szeredi | bacac38 | 2006-01-16 22:14:47 -0800 | [diff] [blame] | 130 | |
Miklos Szeredi | bafa965 | 2006-06-25 05:48:51 -0700 | [diff] [blame] | 131 | Only the owner of the mount may read or write these files. |
Miklos Szeredi | bacac38 | 2006-01-16 22:14:47 -0800 | [diff] [blame] | 132 | |
Miklos Szeredi | a4d27e7 | 2006-06-25 05:48:54 -0700 | [diff] [blame] | 133 | Interrupting filesystem operations |
Daniel W. S. Almeida | 8ab13bc | 2020-01-29 02:06:21 -0300 | [diff] [blame^] | 134 | ################################## |
Miklos Szeredi | a4d27e7 | 2006-06-25 05:48:54 -0700 | [diff] [blame] | 135 | |
| 136 | If a process issuing a FUSE filesystem request is interrupted, the |
| 137 | following will happen: |
| 138 | |
Daniel W. S. Almeida | 8ab13bc | 2020-01-29 02:06:21 -0300 | [diff] [blame^] | 139 | - If the request is not yet sent to userspace AND the signal is |
Miklos Szeredi | a4d27e7 | 2006-06-25 05:48:54 -0700 | [diff] [blame] | 140 | fatal (SIGKILL or unhandled fatal signal), then the request is |
| 141 | dequeued and returns immediately. |
| 142 | |
Daniel W. S. Almeida | 8ab13bc | 2020-01-29 02:06:21 -0300 | [diff] [blame^] | 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 |
Matt LaPlante | fa00e7e | 2006-11-30 04:55:36 +0100 | [diff] [blame] | 145 | the request has been successfully transferred to userspace and |
Miklos Szeredi | a4d27e7 | 2006-06-25 05:48:54 -0700 | [diff] [blame] | 146 | this flag is set, an INTERRUPT request is queued. |
| 147 | |
Daniel W. S. Almeida | 8ab13bc | 2020-01-29 02:06:21 -0300 | [diff] [blame^] | 148 | - If the request is already sent to userspace, then an INTERRUPT |
Miklos Szeredi | a4d27e7 | 2006-06-25 05:48:54 -0700 | [diff] [blame] | 149 | request is queued. |
| 150 | |
| 151 | INTERRUPT requests take precedence over other requests, so the |
| 152 | userspace filesystem will receive queued INTERRUPTs before any others. |
| 153 | |
| 154 | The userspace filesystem may ignore the INTERRUPT requests entirely, |
Daniel W. S. Almeida | 8ab13bc | 2020-01-29 02:06:21 -0300 | [diff] [blame^] | 155 | or may honor them by sending a reply to the *original* request, with |
Miklos Szeredi | a4d27e7 | 2006-06-25 05:48:54 -0700 | [diff] [blame] | 156 | the error set to EINTR. |
| 157 | |
| 158 | It is also possible that there's a race between processing the |
Francis Galiegue | a33f322 | 2010-04-23 00:08:02 +0200 | [diff] [blame] | 159 | original request and its INTERRUPT request. There are two possibilities: |
Miklos Szeredi | a4d27e7 | 2006-06-25 05:48:54 -0700 | [diff] [blame] | 160 | |
Daniel W. S. Almeida | 8ab13bc | 2020-01-29 02:06:21 -0300 | [diff] [blame^] | 161 | 1. The INTERRUPT request is processed before the original request is |
Miklos Szeredi | a4d27e7 | 2006-06-25 05:48:54 -0700 | [diff] [blame] | 162 | processed |
| 163 | |
Daniel W. S. Almeida | 8ab13bc | 2020-01-29 02:06:21 -0300 | [diff] [blame^] | 164 | 2. The INTERRUPT request is processed after the original request has |
Miklos Szeredi | a4d27e7 | 2006-06-25 05:48:54 -0700 | [diff] [blame] | 165 | been answered |
| 166 | |
| 167 | If the filesystem cannot find the original request, it should wait for |
| 168 | some timeout and/or a number of new requests to arrive, after which it |
| 169 | should reply to the INTERRUPT request with an EAGAIN error. In case |
| 170 | 1) the INTERRUPT request will be requeued. In case 2) the INTERRUPT |
| 171 | reply will be ignored. |
| 172 | |
Miklos Szeredi | bacac38 | 2006-01-16 22:14:47 -0800 | [diff] [blame] | 173 | Aborting a filesystem connection |
Daniel W. S. Almeida | 8ab13bc | 2020-01-29 02:06:21 -0300 | [diff] [blame^] | 174 | ================================ |
Miklos Szeredi | bacac38 | 2006-01-16 22:14:47 -0800 | [diff] [blame] | 175 | |
| 176 | It is possible to get into certain situations where the filesystem is |
| 177 | not 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 | |
| 189 | In either of these cases it may be useful to abort the connection to |
| 190 | the 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 Szeredi | bafa965 | 2006-06-25 05:48:51 -0700 | [diff] [blame] | 200 | - Abort filesystem through the FUSE control filesystem. Most |
| 201 | powerful method, always works. |
Miklos Szeredi | bacac38 | 2006-01-16 22:14:47 -0800 | [diff] [blame] | 202 | |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 203 | How do non-privileged mounts work? |
Daniel W. S. Almeida | 8ab13bc | 2020-01-29 02:06:21 -0300 | [diff] [blame^] | 204 | ================================== |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 205 | |
| 206 | Since the mount() system call is a privileged operation, a helper |
| 207 | program (fusermount) is needed, which is installed setuid root. |
| 208 | |
| 209 | The implication of providing non-privileged mounts is that the mount |
| 210 | owner must not be able to use this capability to compromise the |
| 211 | system. 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 | |
| 222 | How are requirements fulfilled? |
Daniel W. S. Almeida | 8ab13bc | 2020-01-29 02:06:21 -0300 | [diff] [blame^] | 223 | =============================== |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 224 | |
| 225 | A) The mount owner could gain elevated privileges by either: |
| 226 | |
Daniel W. S. Almeida | 8ab13bc | 2020-01-29 02:06:21 -0300 | [diff] [blame^] | 227 | 1. creating a filesystem containing a device file, then opening this device |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 228 | |
Daniel W. S. Almeida | 8ab13bc | 2020-01-29 02:06:21 -0300 | [diff] [blame^] | 229 | 2. creating a filesystem containing a suid or sgid application, then executing this application |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 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 |
Daniel W. S. Almeida | 8ab13bc | 2020-01-29 02:06:21 -0300 | [diff] [blame^] | 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. |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 266 | |
| 267 | ii) It can present files or directories of unlimited length, or |
Daniel W. S. Almeida | 8ab13bc | 2020-01-29 02:06:21 -0300 | [diff] [blame^] | 268 | directory structures of unlimited depth, possibly causing a |
| 269 | system process to eat up diskspace, memory or other |
| 270 | resources, again causing *DoS*. |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 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 | |
Daniel W. S. Almeida | 8ab13bc | 2020-01-29 02:06:21 -0300 | [diff] [blame^] | 280 | Note that the *ptrace* check is not strictly necessary to |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 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 |
Daniel W. S. Almeida | 8ab13bc | 2020-01-29 02:06:21 -0300 | [diff] [blame^] | 283 | filesystem, since *SIGSTOP* can be used to get a similar effect. |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 284 | |
| 285 | I think these limitations are unacceptable? |
Daniel W. S. Almeida | 8ab13bc | 2020-01-29 02:06:21 -0300 | [diff] [blame^] | 286 | =========================================== |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 287 | |
| 288 | If a sysadmin trusts the users enough, or can ensure through other |
| 289 | measures, that system processes will never enter non-privileged |
Daniel W. S. Almeida | 8ab13bc | 2020-01-29 02:06:21 -0300 | [diff] [blame^] | 290 | mounts, it can relax the last limitation with a 'user_allow_other' |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 291 | config option. If this config option is set, the mounting user can |
Daniel W. S. Almeida | 8ab13bc | 2020-01-29 02:06:21 -0300 | [diff] [blame^] | 292 | add the 'allow_other' mount option which disables the check for other |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 293 | users' processes. |
| 294 | |
| 295 | Kernel - userspace interface |
Daniel W. S. Almeida | 8ab13bc | 2020-01-29 02:06:21 -0300 | [diff] [blame^] | 296 | ============================ |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 297 | |
| 298 | The following diagram shows how a filesystem operation (in this |
Daniel W. S. Almeida | 8ab13bc | 2020-01-29 02:06:21 -0300 | [diff] [blame^] | 299 | example unlink) is performed in FUSE. :: |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 300 | |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 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 | |
Daniel W. S. Almeida | 8ab13bc | 2020-01-29 02:06:21 -0300 | [diff] [blame^] | 342 | .. note:: Everything in the description above is greatly simplified |
| 343 | |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 344 | There are a couple of ways in which to deadlock a FUSE filesystem. |
| 345 | Since we are talking about unprivileged userspace programs, |
| 346 | something must be done about these. |
| 347 | |
Daniel W. S. Almeida | 8ab13bc | 2020-01-29 02:06:21 -0300 | [diff] [blame^] | 348 | **Scenario 1 - Simple deadlock**:: |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 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 | |
Miklos Szeredi | 51eb01e | 2006-06-25 05:48:50 -0700 | [diff] [blame] | 363 | The solution for this is to allow the filesystem to be aborted. |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 364 | |
Daniel W. S. Almeida | 8ab13bc | 2020-01-29 02:06:21 -0300 | [diff] [blame^] | 365 | **Scenario 2 - Tricky deadlock** |
| 366 | |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 367 | |
| 368 | This one needs a carefully crafted filesystem. It's a variation on |
| 369 | the above, only the call back to the filesystem is not explicit, |
Daniel W. S. Almeida | 8ab13bc | 2020-01-29 02:06:21 -0300 | [diff] [blame^] | 370 | but is caused by a pagefault. :: |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 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 | |
Daniel W. S. Almeida | 8ab13bc | 2020-01-29 02:06:21 -0300 | [diff] [blame^] | 396 | The solution is basically the same as above. |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 397 | |
Miklos Szeredi | a4d27e7 | 2006-06-25 05:48:54 -0700 | [diff] [blame] | 398 | An additional problem is that while the write buffer is being copied |
| 399 | to the request, the request must not be interrupted/aborted. This is |
| 400 | because the destination address of the copy may not be valid after the |
| 401 | request has returned. |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 402 | |
Miklos Szeredi | 51eb01e | 2006-06-25 05:48:50 -0700 | [diff] [blame] | 403 | This is solved with doing the copy atomically, and allowing abort |
| 404 | while the page(s) belonging to the write buffer are faulted with |
| 405 | get_user_pages(). The 'req->locked' flag indicates when the copy is |
| 406 | taking place, and abort is delayed until this flag is unset. |