fuse.txt 14 KB

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