memfd.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. /*
  2. * memfd_create system call and file sealing support
  3. *
  4. * Code was originally included in shmem.c, and broken out to facilitate
  5. * use by hugetlbfs as well as tmpfs.
  6. *
  7. * This file is released under the GPL.
  8. */
  9. #include <linux/fs.h>
  10. #include <linux/vfs.h>
  11. #include <linux/pagemap.h>
  12. #include <linux/file.h>
  13. #include <linux/mm.h>
  14. #include <linux/sched/signal.h>
  15. #include <linux/khugepaged.h>
  16. #include <linux/syscalls.h>
  17. #include <linux/hugetlb.h>
  18. #include <linux/shmem_fs.h>
  19. #include <linux/memfd.h>
  20. #include <uapi/linux/memfd.h>
  21. /*
  22. * We need a tag: a new tag would expand every xa_node by 8 bytes,
  23. * so reuse a tag which we firmly believe is never set or cleared on tmpfs
  24. * or hugetlbfs because they are memory only filesystems.
  25. */
  26. #define MEMFD_TAG_PINNED PAGECACHE_TAG_TOWRITE
  27. #define LAST_SCAN 4 /* about 150ms max */
  28. static void memfd_tag_pins(struct xa_state *xas)
  29. {
  30. struct page *page;
  31. unsigned int tagged = 0;
  32. lru_add_drain();
  33. xas_lock_irq(xas);
  34. xas_for_each(xas, page, ULONG_MAX) {
  35. if (xa_is_value(page))
  36. continue;
  37. if (page_count(page) - page_mapcount(page) > 1)
  38. xas_set_mark(xas, MEMFD_TAG_PINNED);
  39. if (++tagged % XA_CHECK_SCHED)
  40. continue;
  41. xas_pause(xas);
  42. xas_unlock_irq(xas);
  43. cond_resched();
  44. xas_lock_irq(xas);
  45. }
  46. xas_unlock_irq(xas);
  47. }
  48. /*
  49. * Setting SEAL_WRITE requires us to verify there's no pending writer. However,
  50. * via get_user_pages(), drivers might have some pending I/O without any active
  51. * user-space mappings (eg., direct-IO, AIO). Therefore, we look at all pages
  52. * and see whether it has an elevated ref-count. If so, we tag them and wait for
  53. * them to be dropped.
  54. * The caller must guarantee that no new user will acquire writable references
  55. * to those pages to avoid races.
  56. */
  57. static int memfd_wait_for_pins(struct address_space *mapping)
  58. {
  59. XA_STATE(xas, &mapping->i_pages, 0);
  60. struct page *page;
  61. int error, scan;
  62. memfd_tag_pins(&xas);
  63. error = 0;
  64. for (scan = 0; scan <= LAST_SCAN; scan++) {
  65. unsigned int tagged = 0;
  66. if (!xas_marked(&xas, MEMFD_TAG_PINNED))
  67. break;
  68. if (!scan)
  69. lru_add_drain_all();
  70. else if (schedule_timeout_killable((HZ << scan) / 200))
  71. scan = LAST_SCAN;
  72. xas_set(&xas, 0);
  73. xas_lock_irq(&xas);
  74. xas_for_each_marked(&xas, page, ULONG_MAX, MEMFD_TAG_PINNED) {
  75. bool clear = true;
  76. if (xa_is_value(page))
  77. continue;
  78. if (page_count(page) - page_mapcount(page) != 1) {
  79. /*
  80. * On the last scan, we clean up all those tags
  81. * we inserted; but make a note that we still
  82. * found pages pinned.
  83. */
  84. if (scan == LAST_SCAN)
  85. error = -EBUSY;
  86. else
  87. clear = false;
  88. }
  89. if (clear)
  90. xas_clear_mark(&xas, MEMFD_TAG_PINNED);
  91. if (++tagged % XA_CHECK_SCHED)
  92. continue;
  93. xas_pause(&xas);
  94. xas_unlock_irq(&xas);
  95. cond_resched();
  96. xas_lock_irq(&xas);
  97. }
  98. xas_unlock_irq(&xas);
  99. }
  100. return error;
  101. }
  102. static unsigned int *memfd_file_seals_ptr(struct file *file)
  103. {
  104. if (shmem_file(file))
  105. return &SHMEM_I(file_inode(file))->seals;
  106. #ifdef CONFIG_HUGETLBFS
  107. if (is_file_hugepages(file))
  108. return &HUGETLBFS_I(file_inode(file))->seals;
  109. #endif
  110. return NULL;
  111. }
  112. #define F_ALL_SEALS (F_SEAL_SEAL | \
  113. F_SEAL_SHRINK | \
  114. F_SEAL_GROW | \
  115. F_SEAL_WRITE)
  116. static int memfd_add_seals(struct file *file, unsigned int seals)
  117. {
  118. struct inode *inode = file_inode(file);
  119. unsigned int *file_seals;
  120. int error;
  121. /*
  122. * SEALING
  123. * Sealing allows multiple parties to share a tmpfs or hugetlbfs file
  124. * but restrict access to a specific subset of file operations. Seals
  125. * can only be added, but never removed. This way, mutually untrusted
  126. * parties can share common memory regions with a well-defined policy.
  127. * A malicious peer can thus never perform unwanted operations on a
  128. * shared object.
  129. *
  130. * Seals are only supported on special tmpfs or hugetlbfs files and
  131. * always affect the whole underlying inode. Once a seal is set, it
  132. * may prevent some kinds of access to the file. Currently, the
  133. * following seals are defined:
  134. * SEAL_SEAL: Prevent further seals from being set on this file
  135. * SEAL_SHRINK: Prevent the file from shrinking
  136. * SEAL_GROW: Prevent the file from growing
  137. * SEAL_WRITE: Prevent write access to the file
  138. *
  139. * As we don't require any trust relationship between two parties, we
  140. * must prevent seals from being removed. Therefore, sealing a file
  141. * only adds a given set of seals to the file, it never touches
  142. * existing seals. Furthermore, the "setting seals"-operation can be
  143. * sealed itself, which basically prevents any further seal from being
  144. * added.
  145. *
  146. * Semantics of sealing are only defined on volatile files. Only
  147. * anonymous tmpfs and hugetlbfs files support sealing. More
  148. * importantly, seals are never written to disk. Therefore, there's
  149. * no plan to support it on other file types.
  150. */
  151. if (!(file->f_mode & FMODE_WRITE))
  152. return -EPERM;
  153. if (seals & ~(unsigned int)F_ALL_SEALS)
  154. return -EINVAL;
  155. inode_lock(inode);
  156. file_seals = memfd_file_seals_ptr(file);
  157. if (!file_seals) {
  158. error = -EINVAL;
  159. goto unlock;
  160. }
  161. if (*file_seals & F_SEAL_SEAL) {
  162. error = -EPERM;
  163. goto unlock;
  164. }
  165. if ((seals & F_SEAL_WRITE) && !(*file_seals & F_SEAL_WRITE)) {
  166. error = mapping_deny_writable(file->f_mapping);
  167. if (error)
  168. goto unlock;
  169. error = memfd_wait_for_pins(file->f_mapping);
  170. if (error) {
  171. mapping_allow_writable(file->f_mapping);
  172. goto unlock;
  173. }
  174. }
  175. *file_seals |= seals;
  176. error = 0;
  177. unlock:
  178. inode_unlock(inode);
  179. return error;
  180. }
  181. static int memfd_get_seals(struct file *file)
  182. {
  183. unsigned int *seals = memfd_file_seals_ptr(file);
  184. return seals ? *seals : -EINVAL;
  185. }
  186. long memfd_fcntl(struct file *file, unsigned int cmd, unsigned long arg)
  187. {
  188. long error;
  189. switch (cmd) {
  190. case F_ADD_SEALS:
  191. /* disallow upper 32bit */
  192. if (arg > UINT_MAX)
  193. return -EINVAL;
  194. error = memfd_add_seals(file, arg);
  195. break;
  196. case F_GET_SEALS:
  197. error = memfd_get_seals(file);
  198. break;
  199. default:
  200. error = -EINVAL;
  201. break;
  202. }
  203. return error;
  204. }
  205. #define MFD_NAME_PREFIX "memfd:"
  206. #define MFD_NAME_PREFIX_LEN (sizeof(MFD_NAME_PREFIX) - 1)
  207. #define MFD_NAME_MAX_LEN (NAME_MAX - MFD_NAME_PREFIX_LEN)
  208. #define MFD_ALL_FLAGS (MFD_CLOEXEC | MFD_ALLOW_SEALING | MFD_HUGETLB)
  209. SYSCALL_DEFINE2(memfd_create,
  210. const char __user *, uname,
  211. unsigned int, flags)
  212. {
  213. unsigned int *file_seals;
  214. struct file *file;
  215. int fd, error;
  216. char *name;
  217. long len;
  218. if (!(flags & MFD_HUGETLB)) {
  219. if (flags & ~(unsigned int)MFD_ALL_FLAGS)
  220. return -EINVAL;
  221. } else {
  222. /* Allow huge page size encoding in flags. */
  223. if (flags & ~(unsigned int)(MFD_ALL_FLAGS |
  224. (MFD_HUGE_MASK << MFD_HUGE_SHIFT)))
  225. return -EINVAL;
  226. }
  227. /* length includes terminating zero */
  228. len = strnlen_user(uname, MFD_NAME_MAX_LEN + 1);
  229. if (len <= 0)
  230. return -EFAULT;
  231. if (len > MFD_NAME_MAX_LEN + 1)
  232. return -EINVAL;
  233. name = kmalloc(len + MFD_NAME_PREFIX_LEN, GFP_KERNEL);
  234. if (!name)
  235. return -ENOMEM;
  236. strcpy(name, MFD_NAME_PREFIX);
  237. if (copy_from_user(&name[MFD_NAME_PREFIX_LEN], uname, len)) {
  238. error = -EFAULT;
  239. goto err_name;
  240. }
  241. /* terminating-zero may have changed after strnlen_user() returned */
  242. if (name[len + MFD_NAME_PREFIX_LEN - 1]) {
  243. error = -EFAULT;
  244. goto err_name;
  245. }
  246. fd = get_unused_fd_flags((flags & MFD_CLOEXEC) ? O_CLOEXEC : 0);
  247. if (fd < 0) {
  248. error = fd;
  249. goto err_name;
  250. }
  251. if (flags & MFD_HUGETLB) {
  252. struct user_struct *user = NULL;
  253. file = hugetlb_file_setup(name, 0, VM_NORESERVE, &user,
  254. HUGETLB_ANONHUGE_INODE,
  255. (flags >> MFD_HUGE_SHIFT) &
  256. MFD_HUGE_MASK);
  257. } else
  258. file = shmem_file_setup(name, 0, VM_NORESERVE);
  259. if (IS_ERR(file)) {
  260. error = PTR_ERR(file);
  261. goto err_fd;
  262. }
  263. file->f_mode |= FMODE_LSEEK | FMODE_PREAD | FMODE_PWRITE;
  264. file->f_flags |= O_LARGEFILE;
  265. if (flags & MFD_ALLOW_SEALING) {
  266. file_seals = memfd_file_seals_ptr(file);
  267. *file_seals &= ~F_SEAL_SEAL;
  268. }
  269. fd_install(fd, file);
  270. kfree(name);
  271. return fd;
  272. err_fd:
  273. put_unused_fd(fd);
  274. err_name:
  275. kfree(name);
  276. return error;
  277. }