fuse.txt 16 KB

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