memfd.c 8.4 KB

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