2 * memfd_create system call and file sealing support
4 * Code was originally included in shmem.c, and broken out to facilitate
5 * use by hugetlbfs as well as tmpfs.
7 * This file is released under the GPL.
11 #include <linux/vfs.h>
12 #include <linux/pagemap.h>
13 #include <linux/file.h>
15 #include <linux/sched/signal.h>
16 #include <linux/khugepaged.h>
17 #include <linux/syscalls.h>
18 #include <linux/hugetlb.h>
19 #include <linux/shmem_fs.h>
20 #include <linux/memfd.h>
21 #include <linux/pid_namespace.h>
22 #include <uapi/linux/memfd.h>
25 * We need a tag: a new tag would expand every xa_node by 8 bytes,
26 * so reuse a tag which we firmly believe is never set or cleared on tmpfs
27 * or hugetlbfs because they are memory only filesystems.
29 #define MEMFD_TAG_PINNED PAGECACHE_TAG_TOWRITE
30 #define LAST_SCAN 4 /* about 150ms max */
32 static bool memfd_folio_has_extra_refs(struct folio *folio)
34 return folio_ref_count(folio) - folio_mapcount(folio) !=
35 folio_nr_pages(folio);
38 static void memfd_tag_pins(struct xa_state *xas)
46 xas_for_each(xas, folio, ULONG_MAX) {
47 if (!xa_is_value(folio) && memfd_folio_has_extra_refs(folio))
48 xas_set_mark(xas, MEMFD_TAG_PINNED);
50 if (++latency < XA_CHECK_SCHED)
63 * This is a helper function used by memfd_pin_user_pages() in GUP (gup.c).
64 * It is mainly called to allocate a folio in a memfd when the caller
65 * (memfd_pin_folios()) cannot find a folio in the page cache at a given
66 * index in the mapping.
68 struct folio *memfd_alloc_folio(struct file *memfd, pgoff_t idx)
70 #ifdef CONFIG_HUGETLB_PAGE
75 if (is_file_hugepages(memfd)) {
77 * The folio would most likely be accessed by a DMA driver,
78 * therefore, we have zone memory constraints where we can
79 * alloc from. Also, the folio will be pinned for an indefinite
80 * amount of time, so it is not expected to be migrated away.
82 gfp_mask = htlb_alloc_mask(hstate_file(memfd));
83 gfp_mask &= ~(__GFP_HIGHMEM | __GFP_MOVABLE);
85 folio = alloc_hugetlb_folio_nodemask(hstate_file(memfd),
90 if (folio && folio_try_get(folio)) {
91 err = hugetlb_add_to_page_cache(folio,
96 free_huge_folio(folio);
101 return ERR_PTR(-ENOMEM);
104 return shmem_read_folio(memfd->f_mapping, idx);
108 * Setting SEAL_WRITE requires us to verify there's no pending writer. However,
109 * via get_user_pages(), drivers might have some pending I/O without any active
110 * user-space mappings (eg., direct-IO, AIO). Therefore, we look at all folios
111 * and see whether it has an elevated ref-count. If so, we tag them and wait for
112 * them to be dropped.
113 * The caller must guarantee that no new user will acquire writable references
114 * to those folios to avoid races.
116 static int memfd_wait_for_pins(struct address_space *mapping)
118 XA_STATE(xas, &mapping->i_pages, 0);
122 memfd_tag_pins(&xas);
125 for (scan = 0; scan <= LAST_SCAN; scan++) {
128 if (!xas_marked(&xas, MEMFD_TAG_PINNED))
133 else if (schedule_timeout_killable((HZ << scan) / 200))
138 xas_for_each_marked(&xas, folio, ULONG_MAX, MEMFD_TAG_PINNED) {
141 if (!xa_is_value(folio) &&
142 memfd_folio_has_extra_refs(folio)) {
144 * On the last scan, we clean up all those tags
145 * we inserted; but make a note that we still
146 * found folios pinned.
148 if (scan == LAST_SCAN)
154 xas_clear_mark(&xas, MEMFD_TAG_PINNED);
156 if (++latency < XA_CHECK_SCHED)
161 xas_unlock_irq(&xas);
165 xas_unlock_irq(&xas);
171 static unsigned int *memfd_file_seals_ptr(struct file *file)
173 if (shmem_file(file))
174 return &SHMEM_I(file_inode(file))->seals;
176 #ifdef CONFIG_HUGETLBFS
177 if (is_file_hugepages(file))
178 return &HUGETLBFS_I(file_inode(file))->seals;
184 #define F_ALL_SEALS (F_SEAL_SEAL | \
191 static int memfd_add_seals(struct file *file, unsigned int seals)
193 struct inode *inode = file_inode(file);
194 unsigned int *file_seals;
199 * Sealing allows multiple parties to share a tmpfs or hugetlbfs file
200 * but restrict access to a specific subset of file operations. Seals
201 * can only be added, but never removed. This way, mutually untrusted
202 * parties can share common memory regions with a well-defined policy.
203 * A malicious peer can thus never perform unwanted operations on a
206 * Seals are only supported on special tmpfs or hugetlbfs files and
207 * always affect the whole underlying inode. Once a seal is set, it
208 * may prevent some kinds of access to the file. Currently, the
209 * following seals are defined:
210 * SEAL_SEAL: Prevent further seals from being set on this file
211 * SEAL_SHRINK: Prevent the file from shrinking
212 * SEAL_GROW: Prevent the file from growing
213 * SEAL_WRITE: Prevent write access to the file
214 * SEAL_EXEC: Prevent modification of the exec bits in the file mode
216 * As we don't require any trust relationship between two parties, we
217 * must prevent seals from being removed. Therefore, sealing a file
218 * only adds a given set of seals to the file, it never touches
219 * existing seals. Furthermore, the "setting seals"-operation can be
220 * sealed itself, which basically prevents any further seal from being
223 * Semantics of sealing are only defined on volatile files. Only
224 * anonymous tmpfs and hugetlbfs files support sealing. More
225 * importantly, seals are never written to disk. Therefore, there's
226 * no plan to support it on other file types.
229 if (!(file->f_mode & FMODE_WRITE))
231 if (seals & ~(unsigned int)F_ALL_SEALS)
236 file_seals = memfd_file_seals_ptr(file);
242 if (*file_seals & F_SEAL_SEAL) {
247 if ((seals & F_SEAL_WRITE) && !(*file_seals & F_SEAL_WRITE)) {
248 error = mapping_deny_writable(file->f_mapping);
252 error = memfd_wait_for_pins(file->f_mapping);
254 mapping_allow_writable(file->f_mapping);
260 * SEAL_EXEC implys SEAL_WRITE, making W^X from the start.
262 if (seals & F_SEAL_EXEC && inode->i_mode & 0111)
263 seals |= F_SEAL_SHRINK|F_SEAL_GROW|F_SEAL_WRITE|F_SEAL_FUTURE_WRITE;
265 *file_seals |= seals;
273 static int memfd_get_seals(struct file *file)
275 unsigned int *seals = memfd_file_seals_ptr(file);
277 return seals ? *seals : -EINVAL;
280 long memfd_fcntl(struct file *file, unsigned int cmd, unsigned int arg)
286 error = memfd_add_seals(file, arg);
289 error = memfd_get_seals(file);
299 #define MFD_NAME_PREFIX "memfd:"
300 #define MFD_NAME_PREFIX_LEN (sizeof(MFD_NAME_PREFIX) - 1)
301 #define MFD_NAME_MAX_LEN (NAME_MAX - MFD_NAME_PREFIX_LEN)
303 #define MFD_ALL_FLAGS (MFD_CLOEXEC | MFD_ALLOW_SEALING | MFD_HUGETLB | MFD_NOEXEC_SEAL | MFD_EXEC)
305 static int check_sysctl_memfd_noexec(unsigned int *flags)
308 struct pid_namespace *ns = task_active_pid_ns(current);
309 int sysctl = pidns_memfd_noexec_scope(ns);
311 if (!(*flags & (MFD_EXEC | MFD_NOEXEC_SEAL))) {
312 if (sysctl >= MEMFD_NOEXEC_SCOPE_NOEXEC_SEAL)
313 *flags |= MFD_NOEXEC_SEAL;
318 if (!(*flags & MFD_NOEXEC_SEAL) && sysctl >= MEMFD_NOEXEC_SCOPE_NOEXEC_ENFORCED) {
320 "%s[%d]: memfd_create() requires MFD_NOEXEC_SEAL with vm.memfd_noexec=%d\n",
321 current->comm, task_pid_nr(current), sysctl);
328 SYSCALL_DEFINE2(memfd_create,
329 const char __user *, uname,
332 unsigned int *file_seals;
338 if (!(flags & MFD_HUGETLB)) {
339 if (flags & ~(unsigned int)MFD_ALL_FLAGS)
342 /* Allow huge page size encoding in flags. */
343 if (flags & ~(unsigned int)(MFD_ALL_FLAGS |
344 (MFD_HUGE_MASK << MFD_HUGE_SHIFT)))
348 /* Invalid if both EXEC and NOEXEC_SEAL are set.*/
349 if ((flags & MFD_EXEC) && (flags & MFD_NOEXEC_SEAL))
352 error = check_sysctl_memfd_noexec(&flags);
356 /* length includes terminating zero */
357 len = strnlen_user(uname, MFD_NAME_MAX_LEN + 1);
360 if (len > MFD_NAME_MAX_LEN + 1)
363 name = kmalloc(len + MFD_NAME_PREFIX_LEN, GFP_KERNEL);
367 strcpy(name, MFD_NAME_PREFIX);
368 if (copy_from_user(&name[MFD_NAME_PREFIX_LEN], uname, len)) {
373 /* terminating-zero may have changed after strnlen_user() returned */
374 if (name[len + MFD_NAME_PREFIX_LEN - 1]) {
379 fd = get_unused_fd_flags((flags & MFD_CLOEXEC) ? O_CLOEXEC : 0);
385 if (flags & MFD_HUGETLB) {
386 file = hugetlb_file_setup(name, 0, VM_NORESERVE,
387 HUGETLB_ANONHUGE_INODE,
388 (flags >> MFD_HUGE_SHIFT) &
391 file = shmem_file_setup(name, 0, VM_NORESERVE);
393 error = PTR_ERR(file);
396 file->f_mode |= FMODE_LSEEK | FMODE_PREAD | FMODE_PWRITE;
397 file->f_flags |= O_LARGEFILE;
399 if (flags & MFD_NOEXEC_SEAL) {
400 struct inode *inode = file_inode(file);
402 inode->i_mode &= ~0111;
403 file_seals = memfd_file_seals_ptr(file);
405 *file_seals &= ~F_SEAL_SEAL;
406 *file_seals |= F_SEAL_EXEC;
408 } else if (flags & MFD_ALLOW_SEALING) {
409 /* MFD_EXEC and MFD_ALLOW_SEALING are set */
410 file_seals = memfd_file_seals_ptr(file);
412 *file_seals &= ~F_SEAL_SEAL;
415 fd_install(fd, file);