blob: 3afb5ad701e14ad87b6e5173b2974f1309399b8e [file] [log] [blame]
Mike Rapoport1507f512021-07-07 18:08:03 -07001// 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
Mike Rapoport (IBM)b758fe62023-05-15 11:34:00 +030038static bool secretmem_enable __ro_after_init = 1;
Mike Rapoport1507f512021-07-07 18:08:03 -070039module_param_named(enable, secretmem_enable, bool, 0400);
40MODULE_PARM_DESC(secretmem_enable,
41 "Enable secretmem and memfd_secret(2) system call");
42
Linus Torvalds87066fd2021-10-24 09:48:33 -100043static atomic_t secretmem_users;
Mike Rapoport9a436f82021-07-07 18:08:07 -070044
45bool secretmem_active(void)
46{
Linus Torvalds87066fd2021-10-24 09:48:33 -100047 return !!atomic_read(&secretmem_users);
Mike Rapoport9a436f82021-07-07 18:08:07 -070048}
49
Mike Rapoport1507f512021-07-07 18:08:03 -070050static 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;
ZhangPeng7e2fca52023-08-12 14:26:12 +080058 struct folio *folio;
Mike Rapoport84ac01302022-07-07 19:56:50 +030059 vm_fault_t ret;
Mike Rapoport1507f512021-07-07 18:08:03 -070060 int err;
61
62 if (((loff_t)vmf->pgoff << PAGE_SHIFT) >= i_size_read(inode))
63 return vmf_error(-EINVAL);
64
Mike Rapoport84ac01302022-07-07 19:56:50 +030065 filemap_invalidate_lock_shared(mapping);
66
Mike Rapoport1507f512021-07-07 18:08:03 -070067retry:
68 page = find_lock_page(mapping, offset);
69 if (!page) {
ZhangPeng7e2fca52023-08-12 14:26:12 +080070 folio = folio_alloc(gfp | __GFP_ZERO, 0);
71 if (!folio) {
Mike Rapoport84ac01302022-07-07 19:56:50 +030072 ret = VM_FAULT_OOM;
73 goto out;
74 }
Mike Rapoport1507f512021-07-07 18:08:03 -070075
ZhangPeng7e2fca52023-08-12 14:26:12 +080076 page = &folio->page;
Mike Rapoport1507f512021-07-07 18:08:03 -070077 err = set_direct_map_invalid_noflush(page);
78 if (err) {
ZhangPeng7e2fca52023-08-12 14:26:12 +080079 folio_put(folio);
Mike Rapoport84ac01302022-07-07 19:56:50 +030080 ret = vmf_error(err);
81 goto out;
Mike Rapoport1507f512021-07-07 18:08:03 -070082 }
83
ZhangPeng7e2fca52023-08-12 14:26:12 +080084 __folio_mark_uptodate(folio);
85 err = filemap_add_folio(mapping, folio, offset, gfp);
Mike Rapoport1507f512021-07-07 18:08:03 -070086 if (unlikely(err)) {
ZhangPeng7e2fca52023-08-12 14:26:12 +080087 folio_put(folio);
Mike Rapoport1507f512021-07-07 18:08:03 -070088 /*
89 * If a split of large page was required, it
90 * already happened when we marked the page invalid
91 * which guarantees that this call won't fail
92 */
93 set_direct_map_default_noflush(page);
94 if (err == -EEXIST)
95 goto retry;
96
Mike Rapoport84ac01302022-07-07 19:56:50 +030097 ret = vmf_error(err);
98 goto out;
Mike Rapoport1507f512021-07-07 18:08:03 -070099 }
100
101 addr = (unsigned long)page_address(page);
102 flush_tlb_kernel_range(addr, addr + PAGE_SIZE);
103 }
104
105 vmf->page = page;
Mike Rapoport84ac01302022-07-07 19:56:50 +0300106 ret = VM_FAULT_LOCKED;
107
108out:
109 filemap_invalidate_unlock_shared(mapping);
110 return ret;
Mike Rapoport1507f512021-07-07 18:08:03 -0700111}
112
113static const struct vm_operations_struct secretmem_vm_ops = {
114 .fault = secretmem_fault,
115};
116
Mike Rapoport9a436f82021-07-07 18:08:07 -0700117static int secretmem_release(struct inode *inode, struct file *file)
118{
Linus Torvalds87066fd2021-10-24 09:48:33 -1000119 atomic_dec(&secretmem_users);
Mike Rapoport9a436f82021-07-07 18:08:07 -0700120 return 0;
121}
122
Mike Rapoport1507f512021-07-07 18:08:03 -0700123static int secretmem_mmap(struct file *file, struct vm_area_struct *vma)
124{
125 unsigned long len = vma->vm_end - vma->vm_start;
126
127 if ((vma->vm_flags & (VM_SHARED | VM_MAYSHARE)) == 0)
128 return -EINVAL;
129
Andrew Mortonb0cc5e82023-05-22 13:52:10 -0700130 if (!mlock_future_ok(vma->vm_mm, vma->vm_flags | VM_LOCKED, len))
Mike Rapoport1507f512021-07-07 18:08:03 -0700131 return -EAGAIN;
132
Suren Baghdasaryan1c712222023-01-26 11:37:49 -0800133 vm_flags_set(vma, VM_LOCKED | VM_DONTDUMP);
Mike Rapoport1507f512021-07-07 18:08:03 -0700134 vma->vm_ops = &secretmem_vm_ops;
135
136 return 0;
137}
138
139bool vma_is_secretmem(struct vm_area_struct *vma)
140{
141 return vma->vm_ops == &secretmem_vm_ops;
142}
143
144static const struct file_operations secretmem_fops = {
Mike Rapoport9a436f82021-07-07 18:08:07 -0700145 .release = secretmem_release,
Mike Rapoport1507f512021-07-07 18:08:03 -0700146 .mmap = secretmem_mmap,
147};
148
Matthew Wilcox (Oracle)54095482022-06-06 11:30:43 -0400149static int secretmem_migrate_folio(struct address_space *mapping,
150 struct folio *dst, struct folio *src, enum migrate_mode mode)
Mike Rapoport1507f512021-07-07 18:08:03 -0700151{
152 return -EBUSY;
153}
154
Matthew Wilcox (Oracle)6612ed22022-05-02 01:47:42 -0400155static void secretmem_free_folio(struct folio *folio)
Mike Rapoport1507f512021-07-07 18:08:03 -0700156{
Matthew Wilcox (Oracle)6612ed22022-05-02 01:47:42 -0400157 set_direct_map_default_noflush(&folio->page);
158 folio_zero_segment(folio, 0, folio_size(folio));
Mike Rapoport1507f512021-07-07 18:08:03 -0700159}
160
161const struct address_space_operations secretmem_aops = {
Matthew Wilcox (Oracle)46de8b972022-02-09 20:22:13 +0000162 .dirty_folio = noop_dirty_folio,
Matthew Wilcox (Oracle)6612ed22022-05-02 01:47:42 -0400163 .free_folio = secretmem_free_folio,
Matthew Wilcox (Oracle)54095482022-06-06 11:30:43 -0400164 .migrate_folio = secretmem_migrate_folio,
Mike Rapoport1507f512021-07-07 18:08:03 -0700165};
166
Christian Braunerc1632a02023-01-13 12:49:11 +0100167static int secretmem_setattr(struct mnt_idmap *idmap,
Axel Rasmussenf9b141f2022-04-14 19:13:31 -0700168 struct dentry *dentry, struct iattr *iattr)
169{
170 struct inode *inode = d_inode(dentry);
Mike Rapoport84ac01302022-07-07 19:56:50 +0300171 struct address_space *mapping = inode->i_mapping;
Axel Rasmussenf9b141f2022-04-14 19:13:31 -0700172 unsigned int ia_valid = iattr->ia_valid;
Mike Rapoport84ac01302022-07-07 19:56:50 +0300173 int ret;
174
175 filemap_invalidate_lock(mapping);
Axel Rasmussenf9b141f2022-04-14 19:13:31 -0700176
177 if ((ia_valid & ATTR_SIZE) && inode->i_size)
Mike Rapoport84ac01302022-07-07 19:56:50 +0300178 ret = -EINVAL;
179 else
Christian Braunerc1632a02023-01-13 12:49:11 +0100180 ret = simple_setattr(idmap, dentry, iattr);
Axel Rasmussenf9b141f2022-04-14 19:13:31 -0700181
Mike Rapoport84ac01302022-07-07 19:56:50 +0300182 filemap_invalidate_unlock(mapping);
183
184 return ret;
Axel Rasmussenf9b141f2022-04-14 19:13:31 -0700185}
186
187static const struct inode_operations secretmem_iops = {
188 .setattr = secretmem_setattr,
189};
190
Mike Rapoport1507f512021-07-07 18:08:03 -0700191static struct vfsmount *secretmem_mnt;
192
193static struct file *secretmem_file_create(unsigned long flags)
194{
Colin Ian King98001fd2023-01-16 16:43:32 +0000195 struct file *file;
Mike Rapoport1507f512021-07-07 18:08:03 -0700196 struct inode *inode;
Christian Göttsche2bfe15c2022-01-25 15:33:04 +0100197 const char *anon_name = "[secretmem]";
198 const struct qstr qname = QSTR_INIT(anon_name, strlen(anon_name));
199 int err;
Mike Rapoport1507f512021-07-07 18:08:03 -0700200
201 inode = alloc_anon_inode(secretmem_mnt->mnt_sb);
202 if (IS_ERR(inode))
203 return ERR_CAST(inode);
204
Christian Göttsche2bfe15c2022-01-25 15:33:04 +0100205 err = security_inode_init_security_anon(inode, &qname, NULL);
206 if (err) {
207 file = ERR_PTR(err);
208 goto err_free_inode;
209 }
210
Mike Rapoport1507f512021-07-07 18:08:03 -0700211 file = alloc_file_pseudo(inode, secretmem_mnt, "secretmem",
212 O_RDWR, &secretmem_fops);
213 if (IS_ERR(file))
214 goto err_free_inode;
215
216 mapping_set_gfp_mask(inode->i_mapping, GFP_HIGHUSER);
217 mapping_set_unevictable(inode->i_mapping);
218
Axel Rasmussenf9b141f2022-04-14 19:13:31 -0700219 inode->i_op = &secretmem_iops;
Mike Rapoport1507f512021-07-07 18:08:03 -0700220 inode->i_mapping->a_ops = &secretmem_aops;
221
222 /* pretend we are a normal file with zero size */
223 inode->i_mode |= S_IFREG;
224 inode->i_size = 0;
225
226 return file;
227
228err_free_inode:
229 iput(inode);
230 return file;
231}
232
233SYSCALL_DEFINE1(memfd_secret, unsigned int, flags)
234{
235 struct file *file;
236 int fd, err;
237
238 /* make sure local flags do not confict with global fcntl.h */
239 BUILD_BUG_ON(SECRETMEM_FLAGS_MASK & O_CLOEXEC);
240
241 if (!secretmem_enable)
242 return -ENOSYS;
243
244 if (flags & ~(SECRETMEM_FLAGS_MASK | O_CLOEXEC))
245 return -EINVAL;
Matthew Wilcox (Oracle)cb685432021-10-25 19:16:34 +0100246 if (atomic_read(&secretmem_users) < 0)
247 return -ENFILE;
Mike Rapoport1507f512021-07-07 18:08:03 -0700248
249 fd = get_unused_fd_flags(flags & O_CLOEXEC);
250 if (fd < 0)
251 return fd;
252
253 file = secretmem_file_create(flags);
254 if (IS_ERR(file)) {
255 err = PTR_ERR(file);
256 goto err_put_fd;
257 }
258
259 file->f_flags |= O_LARGEFILE;
260
Linus Torvalds87066fd2021-10-24 09:48:33 -1000261 atomic_inc(&secretmem_users);
Kees Cook855d4442021-10-28 14:36:21 -0700262 fd_install(fd, file);
Mike Rapoport1507f512021-07-07 18:08:03 -0700263 return fd;
264
265err_put_fd:
266 put_unused_fd(fd);
267 return err;
268}
269
270static int secretmem_init_fs_context(struct fs_context *fc)
271{
272 return init_pseudo(fc, SECRETMEM_MAGIC) ? 0 : -ENOMEM;
273}
274
275static struct file_system_type secretmem_fs = {
276 .name = "secretmem",
277 .init_fs_context = secretmem_init_fs_context,
278 .kill_sb = kill_anon_super,
279};
280
Xiu Jianfeng1ea41592022-09-15 09:16:02 +0800281static int __init secretmem_init(void)
Mike Rapoport1507f512021-07-07 18:08:03 -0700282{
Mike Rapoport1507f512021-07-07 18:08:03 -0700283 if (!secretmem_enable)
Xiu Jianfengf7c5b1a2022-09-20 09:22:05 +0800284 return 0;
Mike Rapoport1507f512021-07-07 18:08:03 -0700285
286 secretmem_mnt = kern_mount(&secretmem_fs);
287 if (IS_ERR(secretmem_mnt))
Binyi Han4eb5bbd2022-09-04 00:46:47 -0700288 return PTR_ERR(secretmem_mnt);
Mike Rapoport1507f512021-07-07 18:08:03 -0700289
290 /* prevent secretmem mappings from ever getting PROT_EXEC */
291 secretmem_mnt->mnt_flags |= MNT_NOEXEC;
292
Xiu Jianfengf7c5b1a2022-09-20 09:22:05 +0800293 return 0;
Mike Rapoport1507f512021-07-07 18:08:03 -0700294}
295fs_initcall(secretmem_init);