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