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 <uapi/linux/memfd.h>
24 * We need a tag: a new tag would expand every xa_node by 8 bytes,
25 * so reuse a tag which we firmly believe is never set or cleared on tmpfs
26 * or hugetlbfs because they are memory only filesystems.
28 #define MEMFD_TAG_PINNED PAGECACHE_TAG_TOWRITE
29 #define LAST_SCAN 4 /* about 150ms max */
31 static void memfd_tag_pins(struct xa_state *xas)
34 unsigned int tagged = 0;
39 xas_for_each(xas, page, ULONG_MAX) {
40 if (xa_is_value(page))
42 page = find_subpage(page, xas->xa_index);
43 if (page_count(page) - page_mapcount(page) > 1)
44 xas_set_mark(xas, MEMFD_TAG_PINNED);
46 if (++tagged % XA_CHECK_SCHED)
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
63 * The caller must guarantee that no new user will acquire writable references
64 * to those pages to avoid races.
66 static int memfd_wait_for_pins(struct address_space *mapping)
68 XA_STATE(xas, &mapping->i_pages, 0);
75 for (scan = 0; scan <= LAST_SCAN; scan++) {
76 unsigned int tagged = 0;
78 if (!xas_marked(&xas, MEMFD_TAG_PINNED))
83 else if (schedule_timeout_killable((HZ << scan) / 200))
88 xas_for_each_marked(&xas, page, ULONG_MAX, MEMFD_TAG_PINNED) {
90 if (xa_is_value(page))
92 page = find_subpage(page, xas.xa_index);
93 if (page_count(page) - page_mapcount(page) != 1) {
95 * On the last scan, we clean up all those tags
96 * we inserted; but make a note that we still
99 if (scan == LAST_SCAN)
105 xas_clear_mark(&xas, MEMFD_TAG_PINNED);
106 if (++tagged % XA_CHECK_SCHED)
110 xas_unlock_irq(&xas);
114 xas_unlock_irq(&xas);
120 static unsigned int *memfd_file_seals_ptr(struct file *file)
122 if (shmem_file(file))
123 return &SHMEM_I(file_inode(file))->seals;
125 #ifdef CONFIG_HUGETLBFS
126 if (is_file_hugepages(file))
127 return &HUGETLBFS_I(file_inode(file))->seals;
133 #define F_ALL_SEALS (F_SEAL_SEAL | \
139 static int memfd_add_seals(struct file *file, unsigned int seals)
141 struct inode *inode = file_inode(file);
142 unsigned int *file_seals;
147 * Sealing allows multiple parties to share a tmpfs or hugetlbfs file
148 * but restrict access to a specific subset of file operations. Seals
149 * can only be added, but never removed. This way, mutually untrusted
150 * parties can share common memory regions with a well-defined policy.
151 * A malicious peer can thus never perform unwanted operations on a
154 * Seals are only supported on special tmpfs or hugetlbfs files and
155 * always affect the whole underlying inode. Once a seal is set, it
156 * may prevent some kinds of access to the file. Currently, the
157 * following seals are defined:
158 * SEAL_SEAL: Prevent further seals from being set on this file
159 * SEAL_SHRINK: Prevent the file from shrinking
160 * SEAL_GROW: Prevent the file from growing
161 * SEAL_WRITE: Prevent write access to the file
163 * As we don't require any trust relationship between two parties, we
164 * must prevent seals from being removed. Therefore, sealing a file
165 * only adds a given set of seals to the file, it never touches
166 * existing seals. Furthermore, the "setting seals"-operation can be
167 * sealed itself, which basically prevents any further seal from being
170 * Semantics of sealing are only defined on volatile files. Only
171 * anonymous tmpfs and hugetlbfs files support sealing. More
172 * importantly, seals are never written to disk. Therefore, there's
173 * no plan to support it on other file types.
176 if (!(file->f_mode & FMODE_WRITE))
178 if (seals & ~(unsigned int)F_ALL_SEALS)
183 file_seals = memfd_file_seals_ptr(file);
189 if (*file_seals & F_SEAL_SEAL) {
194 if ((seals & F_SEAL_WRITE) && !(*file_seals & F_SEAL_WRITE)) {
195 error = mapping_deny_writable(file->f_mapping);
199 error = memfd_wait_for_pins(file->f_mapping);
201 mapping_allow_writable(file->f_mapping);
206 *file_seals |= seals;
214 static int memfd_get_seals(struct file *file)
216 unsigned int *seals = memfd_file_seals_ptr(file);
218 return seals ? *seals : -EINVAL;
221 long memfd_fcntl(struct file *file, unsigned int cmd, unsigned long arg)
227 /* disallow upper 32bit */
231 error = memfd_add_seals(file, arg);
234 error = memfd_get_seals(file);
243 EXPORT_SYMBOL_GPL(memfd_fcntl);
245 #define MFD_NAME_PREFIX "memfd:"
246 #define MFD_NAME_PREFIX_LEN (sizeof(MFD_NAME_PREFIX) - 1)
247 #define MFD_NAME_MAX_LEN (NAME_MAX - MFD_NAME_PREFIX_LEN)
249 #define MFD_ALL_FLAGS (MFD_CLOEXEC | MFD_ALLOW_SEALING | MFD_HUGETLB)
251 SYSCALL_DEFINE2(memfd_create,
252 const char __user *, uname,
255 unsigned int *file_seals;
261 if (!(flags & MFD_HUGETLB)) {
262 if (flags & ~(unsigned int)MFD_ALL_FLAGS)
265 /* Allow huge page size encoding in flags. */
266 if (flags & ~(unsigned int)(MFD_ALL_FLAGS |
267 (MFD_HUGE_MASK << MFD_HUGE_SHIFT)))
271 /* length includes terminating zero */
272 len = strnlen_user(uname, MFD_NAME_MAX_LEN + 1);
275 if (len > MFD_NAME_MAX_LEN + 1)
278 name = kmalloc(len + MFD_NAME_PREFIX_LEN, GFP_KERNEL);
282 strcpy(name, MFD_NAME_PREFIX);
283 if (copy_from_user(&name[MFD_NAME_PREFIX_LEN], uname, len)) {
288 /* terminating-zero may have changed after strnlen_user() returned */
289 if (name[len + MFD_NAME_PREFIX_LEN - 1]) {
294 fd = get_unused_fd_flags((flags & MFD_CLOEXEC) ? O_CLOEXEC : 0);
300 if (flags & MFD_HUGETLB) {
301 struct user_struct *user = NULL;
303 file = hugetlb_file_setup(name, 0, VM_NORESERVE, &user,
304 HUGETLB_ANONHUGE_INODE,
305 (flags >> MFD_HUGE_SHIFT) &
308 file = shmem_file_setup(name, 0, VM_NORESERVE);
310 error = PTR_ERR(file);
313 file->f_mode |= FMODE_LSEEK | FMODE_PREAD | FMODE_PWRITE;
314 file->f_flags |= O_LARGEFILE;
316 if (flags & MFD_ALLOW_SEALING) {
317 file_seals = memfd_file_seals_ptr(file);
318 *file_seals &= ~F_SEAL_SEAL;
321 fd_install(fd, file);