fuse.txt 15 KB

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