fuse.rst 16 KB

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