fuse.txt 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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. How do non-privileged mounts work?
  59. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  60. Since the mount() system call is a privileged operation, a helper
  61. program (fusermount) is needed, which is installed setuid root.
  62. The implication of providing non-privileged mounts is that the mount
  63. owner must not be able to use this capability to compromise the
  64. system. Obvious requirements arising from this are:
  65. A) mount owner should not be able to get elevated privileges with the
  66. help of the mounted filesystem
  67. B) mount owner should not get illegitimate access to information from
  68. other users' and the super user's processes
  69. C) mount owner should not be able to induce undesired behavior in
  70. other users' or the super user's processes
  71. How are requirements fulfilled?
  72. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  73. A) The mount owner could gain elevated privileges by either:
  74. 1) creating a filesystem containing a device file, then opening
  75. this device
  76. 2) creating a filesystem containing a suid or sgid application,
  77. then executing this application
  78. The solution is not to allow opening device files and ignore
  79. setuid and setgid bits when executing programs. To ensure this
  80. fusermount always adds "nosuid" and "nodev" to the mount options
  81. for non-privileged mounts.
  82. B) If another user is accessing files or directories in the
  83. filesystem, the filesystem daemon serving requests can record the
  84. exact sequence and timing of operations performed. This
  85. information is otherwise inaccessible to the mount owner, so this
  86. counts as an information leak.
  87. The solution to this problem will be presented in point 2) of C).
  88. C) There are several ways in which the mount owner can induce
  89. undesired behavior in other users' processes, such as:
  90. 1) mounting a filesystem over a file or directory which the mount
  91. owner could otherwise not be able to modify (or could only
  92. make limited modifications).
  93. This is solved in fusermount, by checking the access
  94. permissions on the mountpoint and only allowing the mount if
  95. the mount owner can do unlimited modification (has write
  96. access to the mountpoint, and mountpoint is not a "sticky"
  97. directory)
  98. 2) Even if 1) is solved the mount owner can change the behavior
  99. of other users' processes.
  100. i) It can slow down or indefinitely delay the execution of a
  101. filesystem operation creating a DoS against the user or the
  102. whole system. For example a suid application locking a
  103. system file, and then accessing a file on the mount owner's
  104. filesystem could be stopped, and thus causing the system
  105. file to be locked forever.
  106. ii) It can present files or directories of unlimited length, or
  107. directory structures of unlimited depth, possibly causing a
  108. system process to eat up diskspace, memory or other
  109. resources, again causing DoS.
  110. The solution to this as well as B) is not to allow processes
  111. to access the filesystem, which could otherwise not be
  112. monitored or manipulated by the mount owner. Since if the
  113. mount owner can ptrace a process, it can do all of the above
  114. without using a FUSE mount, the same criteria as used in
  115. ptrace can be used to check if a process is allowed to access
  116. the filesystem or not.
  117. Note that the ptrace check is not strictly necessary to
  118. prevent B/2/i, it is enough to check if mount owner has enough
  119. privilege to send signal to the process accessing the
  120. filesystem, since SIGSTOP can be used to get a similar effect.
  121. I think these limitations are unacceptable?
  122. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  123. If a sysadmin trusts the users enough, or can ensure through other
  124. measures, that system processes will never enter non-privileged
  125. mounts, it can relax the last limitation with a "user_allow_other"
  126. config option. If this config option is set, the mounting user can
  127. add the "allow_other" mount option which disables the check for other
  128. users' processes.
  129. Kernel - userspace interface
  130. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  131. The following diagram shows how a filesystem operation (in this
  132. example unlink) is performed in FUSE.
  133. NOTE: everything in this description is greatly simplified
  134. | "rm /mnt/fuse/file" | FUSE filesystem daemon
  135. | |
  136. | | >sys_read()
  137. | | >fuse_dev_read()
  138. | | >request_wait()
  139. | | [sleep on fc->waitq]
  140. | |
  141. | >sys_unlink() |
  142. | >fuse_unlink() |
  143. | [get request from |
  144. | fc->unused_list] |
  145. | >request_send() |
  146. | [queue req on fc->pending] |
  147. | [wake up fc->waitq] | [woken up]
  148. | >request_wait_answer() |
  149. | [sleep on req->waitq] |
  150. | | <request_wait()
  151. | | [remove req from fc->pending]
  152. | | [copy req to read buffer]
  153. | | [add req to fc->processing]
  154. | | <fuse_dev_read()
  155. | | <sys_read()
  156. | |
  157. | | [perform unlink]
  158. | |
  159. | | >sys_write()
  160. | | >fuse_dev_write()
  161. | | [look up req in fc->processing]
  162. | | [remove from fc->processing]
  163. | | [copy write buffer to req]
  164. | [woken up] | [wake up req->waitq]
  165. | | <fuse_dev_write()
  166. | | <sys_write()
  167. | <request_wait_answer() |
  168. | <request_send() |
  169. | [add request to |
  170. | fc->unused_list] |
  171. | <fuse_unlink() |
  172. | <sys_unlink() |
  173. There are a couple of ways in which to deadlock a FUSE filesystem.
  174. Since we are talking about unprivileged userspace programs,
  175. something must be done about these.
  176. Scenario 1 - Simple deadlock
  177. -----------------------------
  178. | "rm /mnt/fuse/file" | FUSE filesystem daemon
  179. | |
  180. | >sys_unlink("/mnt/fuse/file") |
  181. | [acquire inode semaphore |
  182. | for "file"] |
  183. | >fuse_unlink() |
  184. | [sleep on req->waitq] |
  185. | | <sys_read()
  186. | | >sys_unlink("/mnt/fuse/file")
  187. | | [acquire inode semaphore
  188. | | for "file"]
  189. | | *DEADLOCK*
  190. The solution for this is to allow requests to be interrupted while
  191. they are in userspace:
  192. | [interrupted by signal] |
  193. | <fuse_unlink() |
  194. | [release semaphore] | [semaphore acquired]
  195. | <sys_unlink() |
  196. | | >fuse_unlink()
  197. | | [queue req on fc->pending]
  198. | | [wake up fc->waitq]
  199. | | [sleep on req->waitq]
  200. If the filesystem daemon was single threaded, this will stop here,
  201. since there's no other thread to dequeue and execute the request.
  202. In this case the solution is to kill the FUSE daemon as well. If
  203. there are multiple serving threads, you just have to kill them as
  204. long as any remain.
  205. Moral: a filesystem which deadlocks, can soon find itself dead.
  206. Scenario 2 - Tricky deadlock
  207. ----------------------------
  208. This one needs a carefully crafted filesystem. It's a variation on
  209. the above, only the call back to the filesystem is not explicit,
  210. but is caused by a pagefault.
  211. | Kamikaze filesystem thread 1 | Kamikaze filesystem thread 2
  212. | |
  213. | [fd = open("/mnt/fuse/file")] | [request served normally]
  214. | [mmap fd to 'addr'] |
  215. | [close fd] | [FLUSH triggers 'magic' flag]
  216. | [read a byte from addr] |
  217. | >do_page_fault() |
  218. | [find or create page] |
  219. | [lock page] |
  220. | >fuse_readpage() |
  221. | [queue READ request] |
  222. | [sleep on req->waitq] |
  223. | | [read request to buffer]
  224. | | [create reply header before addr]
  225. | | >sys_write(addr - headerlength)
  226. | | >fuse_dev_write()
  227. | | [look up req in fc->processing]
  228. | | [remove from fc->processing]
  229. | | [copy write buffer to req]
  230. | | >do_page_fault()
  231. | | [find or create page]
  232. | | [lock page]
  233. | | * DEADLOCK *
  234. Solution is again to let the the request be interrupted (not
  235. elaborated further).
  236. An additional problem is that while the write buffer is being
  237. copied to the request, the request must not be interrupted. This
  238. is because the destination address of the copy may not be valid
  239. after the request is interrupted.
  240. This is solved with doing the copy atomically, and allowing
  241. interruption while the page(s) belonging to the write buffer are
  242. faulted with get_user_pages(). The 'req->locked' flag indicates
  243. when the copy is taking place, and interruption is delayed until
  244. this flag is unset.