fuse.txt 16 KB

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