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