• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright IBM Corporation, 2021
4  *
5  * Author: Mike Rapoport <rppt@linux.ibm.com>
6  */
7 
8 #include <linux/mm.h>
9 #include <linux/fs.h>
10 #include <linux/swap.h>
11 #include <linux/mount.h>
12 #include <linux/memfd.h>
13 #include <linux/bitops.h>
14 #include <linux/printk.h>
15 #include <linux/pagemap.h>
16 #include <linux/syscalls.h>
17 #include <linux/pseudo_fs.h>
18 #include <linux/secretmem.h>
19 #include <linux/set_memory.h>
20 #include <linux/sched/signal.h>
21 
22 #include <uapi/linux/magic.h>
23 
24 #include <asm/tlbflush.h>
25 
26 #include "internal.h"
27 
28 #undef pr_fmt
29 #define pr_fmt(fmt) "secretmem: " fmt
30 
31 /*
32  * Define mode and flag masks to allow validation of the system call
33  * parameters.
34  */
35 #define SECRETMEM_MODE_MASK	(0x0)
36 #define SECRETMEM_FLAGS_MASK	SECRETMEM_MODE_MASK
37 
38 static bool secretmem_enable __ro_after_init;
39 module_param_named(enable, secretmem_enable, bool, 0400);
40 MODULE_PARM_DESC(secretmem_enable,
41 		 "Enable secretmem and memfd_secret(2) system call");
42 
43 static atomic_t secretmem_users;
44 
secretmem_active(void)45 bool secretmem_active(void)
46 {
47 	return !!atomic_read(&secretmem_users);
48 }
49 
secretmem_fault(struct vm_fault * vmf)50 static vm_fault_t secretmem_fault(struct vm_fault *vmf)
51 {
52 	struct address_space *mapping = vmf->vma->vm_file->f_mapping;
53 	struct inode *inode = file_inode(vmf->vma->vm_file);
54 	pgoff_t offset = vmf->pgoff;
55 	gfp_t gfp = vmf->gfp_mask;
56 	unsigned long addr;
57 	struct page *page;
58 	vm_fault_t ret;
59 	int err;
60 
61 	if (((loff_t)vmf->pgoff << PAGE_SHIFT) >= i_size_read(inode))
62 		return vmf_error(-EINVAL);
63 
64 	filemap_invalidate_lock_shared(mapping);
65 
66 retry:
67 	page = find_lock_page(mapping, offset);
68 	if (!page) {
69 		page = alloc_page(gfp | __GFP_ZERO);
70 		if (!page) {
71 			ret = VM_FAULT_OOM;
72 			goto out;
73 		}
74 
75 		err = set_direct_map_invalid_noflush(page);
76 		if (err) {
77 			put_page(page);
78 			ret = vmf_error(err);
79 			goto out;
80 		}
81 
82 		__SetPageUptodate(page);
83 		err = add_to_page_cache_lru(page, mapping, offset, gfp);
84 		if (unlikely(err)) {
85 			put_page(page);
86 			/*
87 			 * If a split of large page was required, it
88 			 * already happened when we marked the page invalid
89 			 * which guarantees that this call won't fail
90 			 */
91 			set_direct_map_default_noflush(page);
92 			if (err == -EEXIST)
93 				goto retry;
94 
95 			ret = vmf_error(err);
96 			goto out;
97 		}
98 
99 		addr = (unsigned long)page_address(page);
100 		flush_tlb_kernel_range(addr, addr + PAGE_SIZE);
101 	}
102 
103 	vmf->page = page;
104 	ret = VM_FAULT_LOCKED;
105 
106 out:
107 	filemap_invalidate_unlock_shared(mapping);
108 	return ret;
109 }
110 
111 static const struct vm_operations_struct secretmem_vm_ops = {
112 	.fault = secretmem_fault,
113 };
114 
secretmem_release(struct inode * inode,struct file * file)115 static int secretmem_release(struct inode *inode, struct file *file)
116 {
117 	atomic_dec(&secretmem_users);
118 	return 0;
119 }
120 
secretmem_mmap(struct file * file,struct vm_area_struct * vma)121 static int secretmem_mmap(struct file *file, struct vm_area_struct *vma)
122 {
123 	unsigned long len = vma->vm_end - vma->vm_start;
124 
125 	if ((vma->vm_flags & (VM_SHARED | VM_MAYSHARE)) == 0)
126 		return -EINVAL;
127 
128 	if (mlock_future_check(vma->vm_mm, vma->vm_flags | VM_LOCKED, len))
129 		return -EAGAIN;
130 
131 	vma->vm_flags |= VM_LOCKED | VM_DONTDUMP;
132 	vma->vm_ops = &secretmem_vm_ops;
133 
134 	return 0;
135 }
136 
vma_is_secretmem(struct vm_area_struct * vma)137 bool vma_is_secretmem(struct vm_area_struct *vma)
138 {
139 	return vma->vm_ops == &secretmem_vm_ops;
140 }
141 
142 static const struct file_operations secretmem_fops = {
143 	.release	= secretmem_release,
144 	.mmap		= secretmem_mmap,
145 };
146 
secretmem_isolate_page(struct page * page,isolate_mode_t mode)147 static bool secretmem_isolate_page(struct page *page, isolate_mode_t mode)
148 {
149 	return false;
150 }
151 
secretmem_migratepage(struct address_space * mapping,struct page * newpage,struct page * page,enum migrate_mode mode)152 static int secretmem_migratepage(struct address_space *mapping,
153 				 struct page *newpage, struct page *page,
154 				 enum migrate_mode mode)
155 {
156 	return -EBUSY;
157 }
158 
secretmem_freepage(struct page * page)159 static void secretmem_freepage(struct page *page)
160 {
161 	set_direct_map_default_noflush(page);
162 	clear_highpage(page);
163 }
164 
165 const struct address_space_operations secretmem_aops = {
166 	.set_page_dirty	= __set_page_dirty_no_writeback,
167 	.freepage	= secretmem_freepage,
168 	.migratepage	= secretmem_migratepage,
169 	.isolate_page	= secretmem_isolate_page,
170 };
171 
secretmem_setattr(struct user_namespace * mnt_userns,struct dentry * dentry,struct iattr * iattr)172 static int secretmem_setattr(struct user_namespace *mnt_userns,
173 			     struct dentry *dentry, struct iattr *iattr)
174 {
175 	struct inode *inode = d_inode(dentry);
176 	struct address_space *mapping = inode->i_mapping;
177 	unsigned int ia_valid = iattr->ia_valid;
178 	int ret;
179 
180 	filemap_invalidate_lock(mapping);
181 
182 	if ((ia_valid & ATTR_SIZE) && inode->i_size)
183 		ret = -EINVAL;
184 	else
185 		ret = simple_setattr(mnt_userns, dentry, iattr);
186 
187 	filemap_invalidate_unlock(mapping);
188 
189 	return ret;
190 }
191 
192 static const struct inode_operations secretmem_iops = {
193 	.setattr = secretmem_setattr,
194 };
195 
196 static struct vfsmount *secretmem_mnt;
197 
secretmem_file_create(unsigned long flags)198 static struct file *secretmem_file_create(unsigned long flags)
199 {
200 	struct file *file = ERR_PTR(-ENOMEM);
201 	struct inode *inode;
202 
203 	inode = alloc_anon_inode(secretmem_mnt->mnt_sb);
204 	if (IS_ERR(inode))
205 		return ERR_CAST(inode);
206 
207 	file = alloc_file_pseudo(inode, secretmem_mnt, "secretmem",
208 				 O_RDWR, &secretmem_fops);
209 	if (IS_ERR(file))
210 		goto err_free_inode;
211 
212 	mapping_set_gfp_mask(inode->i_mapping, GFP_HIGHUSER);
213 	mapping_set_unevictable(inode->i_mapping);
214 
215 	inode->i_op = &secretmem_iops;
216 	inode->i_mapping->a_ops = &secretmem_aops;
217 
218 	/* pretend we are a normal file with zero size */
219 	inode->i_mode |= S_IFREG;
220 	inode->i_size = 0;
221 
222 	return file;
223 
224 err_free_inode:
225 	iput(inode);
226 	return file;
227 }
228 
SYSCALL_DEFINE1(memfd_secret,unsigned int,flags)229 SYSCALL_DEFINE1(memfd_secret, unsigned int, flags)
230 {
231 	struct file *file;
232 	int fd, err;
233 
234 	/* make sure local flags do not confict with global fcntl.h */
235 	BUILD_BUG_ON(SECRETMEM_FLAGS_MASK & O_CLOEXEC);
236 
237 	if (!secretmem_enable)
238 		return -ENOSYS;
239 
240 	if (flags & ~(SECRETMEM_FLAGS_MASK | O_CLOEXEC))
241 		return -EINVAL;
242 	if (atomic_read(&secretmem_users) < 0)
243 		return -ENFILE;
244 
245 	fd = get_unused_fd_flags(flags & O_CLOEXEC);
246 	if (fd < 0)
247 		return fd;
248 
249 	file = secretmem_file_create(flags);
250 	if (IS_ERR(file)) {
251 		err = PTR_ERR(file);
252 		goto err_put_fd;
253 	}
254 
255 	file->f_flags |= O_LARGEFILE;
256 
257 	atomic_inc(&secretmem_users);
258 	fd_install(fd, file);
259 	return fd;
260 
261 err_put_fd:
262 	put_unused_fd(fd);
263 	return err;
264 }
265 
secretmem_init_fs_context(struct fs_context * fc)266 static int secretmem_init_fs_context(struct fs_context *fc)
267 {
268 	return init_pseudo(fc, SECRETMEM_MAGIC) ? 0 : -ENOMEM;
269 }
270 
271 static struct file_system_type secretmem_fs = {
272 	.name		= "secretmem",
273 	.init_fs_context = secretmem_init_fs_context,
274 	.kill_sb	= kill_anon_super,
275 };
276 
secretmem_init(void)277 static int secretmem_init(void)
278 {
279 	int ret = 0;
280 
281 	if (!secretmem_enable)
282 		return ret;
283 
284 	secretmem_mnt = kern_mount(&secretmem_fs);
285 	if (IS_ERR(secretmem_mnt))
286 		return PTR_ERR(secretmem_mnt);
287 
288 	/* prevent secretmem mappings from ever getting PROT_EXEC */
289 	secretmem_mnt->mnt_flags |= MNT_NOEXEC;
290 
291 	return ret;
292 }
293 fs_initcall(secretmem_init);
294