xref: /linux/fs/efivarfs/super.c (revision fd3aa3d5e5dbbf4254cd4ac6c550a4da671e07cc)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2012 Red Hat, Inc.
4  * Copyright (C) 2012 Jeremy Kerr <jeremy.kerr@canonical.com>
5  */
6 
7 #include <linux/ctype.h>
8 #include <linux/efi.h>
9 #include <linux/fs.h>
10 #include <linux/fs_context.h>
11 #include <linux/fs_parser.h>
12 #include <linux/module.h>
13 #include <linux/pagemap.h>
14 #include <linux/ucs2_string.h>
15 #include <linux/slab.h>
16 #include <linux/magic.h>
17 #include <linux/statfs.h>
18 #include <linux/notifier.h>
19 #include <linux/printk.h>
20 
21 #include "internal.h"
22 
23 static int efivarfs_ops_notifier(struct notifier_block *nb, unsigned long event,
24 				 void *data)
25 {
26 	struct efivarfs_fs_info *sfi = container_of(nb, struct efivarfs_fs_info, nb);
27 
28 	switch (event) {
29 	case EFIVAR_OPS_RDONLY:
30 		sfi->sb->s_flags |= SB_RDONLY;
31 		break;
32 	case EFIVAR_OPS_RDWR:
33 		sfi->sb->s_flags &= ~SB_RDONLY;
34 		break;
35 	default:
36 		return NOTIFY_DONE;
37 	}
38 
39 	return NOTIFY_OK;
40 }
41 
42 static struct inode *efivarfs_alloc_inode(struct super_block *sb)
43 {
44 	struct efivar_entry *entry = kzalloc(sizeof(*entry), GFP_KERNEL);
45 
46 	if (!entry)
47 		return NULL;
48 
49 	inode_init_once(&entry->vfs_inode);
50 	entry->removed = false;
51 
52 	return &entry->vfs_inode;
53 }
54 
55 static void efivarfs_free_inode(struct inode *inode)
56 {
57 	struct efivar_entry *entry = efivar_entry(inode);
58 
59 	kfree(entry);
60 }
61 
62 static int efivarfs_show_options(struct seq_file *m, struct dentry *root)
63 {
64 	struct super_block *sb = root->d_sb;
65 	struct efivarfs_fs_info *sbi = sb->s_fs_info;
66 	struct efivarfs_mount_opts *opts = &sbi->mount_opts;
67 
68 	if (!uid_eq(opts->uid, GLOBAL_ROOT_UID))
69 		seq_printf(m, ",uid=%u",
70 				from_kuid_munged(&init_user_ns, opts->uid));
71 	if (!gid_eq(opts->gid, GLOBAL_ROOT_GID))
72 		seq_printf(m, ",gid=%u",
73 				from_kgid_munged(&init_user_ns, opts->gid));
74 	return 0;
75 }
76 
77 static int efivarfs_statfs(struct dentry *dentry, struct kstatfs *buf)
78 {
79 	const u32 attr = EFI_VARIABLE_NON_VOLATILE |
80 			 EFI_VARIABLE_BOOTSERVICE_ACCESS |
81 			 EFI_VARIABLE_RUNTIME_ACCESS;
82 	u64 storage_space, remaining_space, max_variable_size;
83 	u64 id = huge_encode_dev(dentry->d_sb->s_dev);
84 	efi_status_t status;
85 
86 	/* Some UEFI firmware does not implement QueryVariableInfo() */
87 	storage_space = remaining_space = 0;
88 	if (efi_rt_services_supported(EFI_RT_SUPPORTED_QUERY_VARIABLE_INFO)) {
89 		status = efivar_query_variable_info(attr, &storage_space,
90 						    &remaining_space,
91 						    &max_variable_size);
92 		if (status != EFI_SUCCESS && status != EFI_UNSUPPORTED)
93 			pr_warn_ratelimited("query_variable_info() failed: 0x%lx\n",
94 					    status);
95 	}
96 
97 	/*
98 	 * This is not a normal filesystem, so no point in pretending it has a block
99 	 * size; we declare f_bsize to 1, so that we can then report the exact value
100 	 * sent by EFI QueryVariableInfo in f_blocks and f_bfree
101 	 */
102 	buf->f_bsize	= 1;
103 	buf->f_namelen	= NAME_MAX;
104 	buf->f_blocks	= storage_space;
105 	buf->f_bfree	= remaining_space;
106 	buf->f_type	= dentry->d_sb->s_magic;
107 	buf->f_fsid	= u64_to_fsid(id);
108 
109 	/*
110 	 * In f_bavail we declare the free space that the kernel will allow writing
111 	 * when the storage_paranoia x86 quirk is active. To use more, users
112 	 * should boot the kernel with efi_no_storage_paranoia.
113 	 */
114 	if (remaining_space > efivar_reserved_space())
115 		buf->f_bavail = remaining_space - efivar_reserved_space();
116 	else
117 		buf->f_bavail = 0;
118 
119 	return 0;
120 }
121 static const struct super_operations efivarfs_ops = {
122 	.statfs = efivarfs_statfs,
123 	.drop_inode = generic_delete_inode,
124 	.alloc_inode = efivarfs_alloc_inode,
125 	.free_inode = efivarfs_free_inode,
126 	.show_options = efivarfs_show_options,
127 };
128 
129 /*
130  * Compare two efivarfs file names.
131  *
132  * An efivarfs filename is composed of two parts,
133  *
134  *	1. A case-sensitive variable name
135  *	2. A case-insensitive GUID
136  *
137  * So we need to perform a case-sensitive match on part 1 and a
138  * case-insensitive match on part 2.
139  */
140 static int efivarfs_d_compare(const struct dentry *dentry,
141 			      unsigned int len, const char *str,
142 			      const struct qstr *name)
143 {
144 	int guid = len - EFI_VARIABLE_GUID_LEN;
145 
146 	if (name->len != len)
147 		return 1;
148 
149 	/* Case-sensitive compare for the variable name */
150 	if (memcmp(str, name->name, guid))
151 		return 1;
152 
153 	/* Case-insensitive compare for the GUID */
154 	return strncasecmp(name->name + guid, str + guid, EFI_VARIABLE_GUID_LEN);
155 }
156 
157 static int efivarfs_d_hash(const struct dentry *dentry, struct qstr *qstr)
158 {
159 	unsigned long hash = init_name_hash(dentry);
160 	const unsigned char *s = qstr->name;
161 	unsigned int len = qstr->len;
162 
163 	while (len-- > EFI_VARIABLE_GUID_LEN)
164 		hash = partial_name_hash(*s++, hash);
165 
166 	/* GUID is case-insensitive. */
167 	while (len--)
168 		hash = partial_name_hash(tolower(*s++), hash);
169 
170 	qstr->hash = end_name_hash(hash);
171 	return 0;
172 }
173 
174 static const struct dentry_operations efivarfs_d_ops = {
175 	.d_compare = efivarfs_d_compare,
176 	.d_hash = efivarfs_d_hash,
177 	.d_delete = always_delete_dentry,
178 };
179 
180 static struct dentry *efivarfs_alloc_dentry(struct dentry *parent, char *name)
181 {
182 	struct dentry *d;
183 	struct qstr q;
184 	int err;
185 
186 	q.name = name;
187 	q.len = strlen(name);
188 
189 	err = efivarfs_d_hash(parent, &q);
190 	if (err)
191 		return ERR_PTR(err);
192 
193 	d = d_alloc(parent, &q);
194 	if (d)
195 		return d;
196 
197 	return ERR_PTR(-ENOMEM);
198 }
199 
200 bool efivarfs_variable_is_present(efi_char16_t *variable_name,
201 				  efi_guid_t *vendor, void *data)
202 {
203 	char *name = efivar_get_utf8name(variable_name, vendor);
204 	struct super_block *sb = data;
205 	struct dentry *dentry;
206 	struct qstr qstr;
207 
208 	if (!name)
209 		/*
210 		 * If the allocation failed there'll already be an
211 		 * error in the log (and likely a huge and growing
212 		 * number of them since they system will be under
213 		 * extreme memory pressure), so simply assume
214 		 * collision for safety but don't add to the log
215 		 * flood.
216 		 */
217 		return true;
218 
219 	qstr.name = name;
220 	qstr.len = strlen(name);
221 	dentry = d_hash_and_lookup(sb->s_root, &qstr);
222 	kfree(name);
223 	if (!IS_ERR_OR_NULL(dentry))
224 		dput(dentry);
225 
226 	return dentry != NULL;
227 }
228 
229 static int efivarfs_callback(efi_char16_t *name16, efi_guid_t vendor,
230 			     unsigned long name_size, void *data)
231 {
232 	struct super_block *sb = (struct super_block *)data;
233 	struct efivar_entry *entry;
234 	struct inode *inode = NULL;
235 	struct dentry *dentry, *root = sb->s_root;
236 	unsigned long size = 0;
237 	char *name;
238 	int len;
239 	int err = -ENOMEM;
240 	bool is_removable = false;
241 
242 	if (guid_equal(&vendor, &LINUX_EFI_RANDOM_SEED_TABLE_GUID))
243 		return 0;
244 
245 	name = efivar_get_utf8name(name16, &vendor);
246 	if (!name)
247 		return err;
248 
249 	/* length of the variable name itself: remove GUID and separator */
250 	len = strlen(name) - EFI_VARIABLE_GUID_LEN - 1;
251 
252 	if (efivar_variable_is_removable(vendor, name, len))
253 		is_removable = true;
254 
255 	inode = efivarfs_get_inode(sb, d_inode(root), S_IFREG | 0644, 0,
256 				   is_removable);
257 	if (!inode)
258 		goto fail_name;
259 
260 	entry = efivar_entry(inode);
261 
262 	memcpy(entry->var.VariableName, name16, name_size);
263 	memcpy(&(entry->var.VendorGuid), &vendor, sizeof(efi_guid_t));
264 
265 	dentry = efivarfs_alloc_dentry(root, name);
266 	if (IS_ERR(dentry)) {
267 		err = PTR_ERR(dentry);
268 		goto fail_inode;
269 	}
270 
271 	__efivar_entry_get(entry, NULL, &size, NULL);
272 
273 	/* copied by the above to local storage in the dentry. */
274 	kfree(name);
275 
276 	inode_lock(inode);
277 	inode->i_private = entry;
278 	i_size_write(inode, size + sizeof(__u32)); /* attributes + data */
279 	inode_unlock(inode);
280 	d_add(dentry, inode);
281 
282 	return 0;
283 
284 fail_inode:
285 	iput(inode);
286 fail_name:
287 	kfree(name);
288 
289 	return err;
290 }
291 
292 enum {
293 	Opt_uid, Opt_gid,
294 };
295 
296 static const struct fs_parameter_spec efivarfs_parameters[] = {
297 	fsparam_uid("uid", Opt_uid),
298 	fsparam_gid("gid", Opt_gid),
299 	{},
300 };
301 
302 static int efivarfs_parse_param(struct fs_context *fc, struct fs_parameter *param)
303 {
304 	struct efivarfs_fs_info *sbi = fc->s_fs_info;
305 	struct efivarfs_mount_opts *opts = &sbi->mount_opts;
306 	struct fs_parse_result result;
307 	int opt;
308 
309 	opt = fs_parse(fc, efivarfs_parameters, param, &result);
310 	if (opt < 0)
311 		return opt;
312 
313 	switch (opt) {
314 	case Opt_uid:
315 		opts->uid = result.uid;
316 		break;
317 	case Opt_gid:
318 		opts->gid = result.gid;
319 		break;
320 	default:
321 		return -EINVAL;
322 	}
323 
324 	return 0;
325 }
326 
327 static int efivarfs_fill_super(struct super_block *sb, struct fs_context *fc)
328 {
329 	struct efivarfs_fs_info *sfi = sb->s_fs_info;
330 	struct inode *inode = NULL;
331 	struct dentry *root;
332 	int err;
333 
334 	sb->s_maxbytes          = MAX_LFS_FILESIZE;
335 	sb->s_blocksize         = PAGE_SIZE;
336 	sb->s_blocksize_bits    = PAGE_SHIFT;
337 	sb->s_magic             = EFIVARFS_MAGIC;
338 	sb->s_op                = &efivarfs_ops;
339 	sb->s_d_op		= &efivarfs_d_ops;
340 	sb->s_time_gran         = 1;
341 
342 	if (!efivar_supports_writes())
343 		sb->s_flags |= SB_RDONLY;
344 
345 	inode = efivarfs_get_inode(sb, NULL, S_IFDIR | 0755, 0, true);
346 	if (!inode)
347 		return -ENOMEM;
348 	inode->i_op = &efivarfs_dir_inode_operations;
349 
350 	root = d_make_root(inode);
351 	sb->s_root = root;
352 	if (!root)
353 		return -ENOMEM;
354 
355 	sfi->sb = sb;
356 	sfi->nb.notifier_call = efivarfs_ops_notifier;
357 	err = blocking_notifier_chain_register(&efivar_ops_nh, &sfi->nb);
358 	if (err)
359 		return err;
360 
361 	return efivar_init(efivarfs_callback, sb);
362 }
363 
364 static int efivarfs_get_tree(struct fs_context *fc)
365 {
366 	return get_tree_single(fc, efivarfs_fill_super);
367 }
368 
369 static int efivarfs_reconfigure(struct fs_context *fc)
370 {
371 	if (!efivar_supports_writes() && !(fc->sb_flags & SB_RDONLY)) {
372 		pr_err("Firmware does not support SetVariableRT. Can not remount with rw\n");
373 		return -EINVAL;
374 	}
375 
376 	return 0;
377 }
378 
379 static const struct fs_context_operations efivarfs_context_ops = {
380 	.get_tree	= efivarfs_get_tree,
381 	.parse_param	= efivarfs_parse_param,
382 	.reconfigure	= efivarfs_reconfigure,
383 };
384 
385 static int efivarfs_init_fs_context(struct fs_context *fc)
386 {
387 	struct efivarfs_fs_info *sfi;
388 
389 	if (!efivar_is_available())
390 		return -EOPNOTSUPP;
391 
392 	sfi = kzalloc(sizeof(*sfi), GFP_KERNEL);
393 	if (!sfi)
394 		return -ENOMEM;
395 
396 	sfi->mount_opts.uid = GLOBAL_ROOT_UID;
397 	sfi->mount_opts.gid = GLOBAL_ROOT_GID;
398 
399 	fc->s_fs_info = sfi;
400 	fc->ops = &efivarfs_context_ops;
401 	return 0;
402 }
403 
404 static void efivarfs_kill_sb(struct super_block *sb)
405 {
406 	struct efivarfs_fs_info *sfi = sb->s_fs_info;
407 
408 	blocking_notifier_chain_unregister(&efivar_ops_nh, &sfi->nb);
409 	kill_litter_super(sb);
410 
411 	kfree(sfi);
412 }
413 
414 static struct file_system_type efivarfs_type = {
415 	.owner   = THIS_MODULE,
416 	.name    = "efivarfs",
417 	.init_fs_context = efivarfs_init_fs_context,
418 	.kill_sb = efivarfs_kill_sb,
419 	.parameters = efivarfs_parameters,
420 };
421 
422 static __init int efivarfs_init(void)
423 {
424 	return register_filesystem(&efivarfs_type);
425 }
426 
427 static __exit void efivarfs_exit(void)
428 {
429 	unregister_filesystem(&efivarfs_type);
430 }
431 
432 MODULE_AUTHOR("Matthew Garrett, Jeremy Kerr");
433 MODULE_DESCRIPTION("EFI Variable Filesystem");
434 MODULE_LICENSE("GPL");
435 MODULE_ALIAS_FS("efivarfs");
436 
437 module_init(efivarfs_init);
438 module_exit(efivarfs_exit);
439