memfd.c 8.5 KB

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