xref: /linux/fs/efivarfs/super.c (revision ab93e0dd72c37d378dd936f031ffb83ff2bd87ce)
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 
efivarfs_ops_notifier(struct notifier_block * nb,unsigned long event,void * data)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 
efivarfs_alloc_inode(struct super_block * sb)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 
efivarfs_free_inode(struct inode * inode)58 static void efivarfs_free_inode(struct inode *inode)
59 {
60 	struct efivar_entry *entry = efivar_entry(inode);
61 
62 	kfree(entry);
63 }
64 
efivarfs_show_options(struct seq_file * m,struct dentry * root)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 
efivarfs_statfs(struct dentry * dentry,struct kstatfs * buf)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  */
efivarfs_d_compare(const struct dentry * dentry,unsigned int len,const char * str,const struct qstr * name)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 
efivarfs_d_hash(const struct dentry * dentry,struct qstr * qstr)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 };
187 
efivarfs_alloc_dentry(struct dentry * parent,char * name)188 static struct dentry *efivarfs_alloc_dentry(struct dentry *parent, char *name)
189 {
190 	struct dentry *d;
191 	struct qstr q;
192 	int err;
193 
194 	q.name = name;
195 	q.len = strlen(name);
196 
197 	err = efivarfs_d_hash(parent, &q);
198 	if (err)
199 		return ERR_PTR(err);
200 
201 	d = d_alloc(parent, &q);
202 	if (d)
203 		return d;
204 
205 	return ERR_PTR(-ENOMEM);
206 }
207 
efivarfs_variable_is_present(efi_char16_t * variable_name,efi_guid_t * vendor,void * data)208 bool efivarfs_variable_is_present(efi_char16_t *variable_name,
209 				  efi_guid_t *vendor, void *data)
210 {
211 	char *name = efivar_get_utf8name(variable_name, vendor);
212 	struct super_block *sb = data;
213 	struct dentry *dentry;
214 
215 	if (!name)
216 		/*
217 		 * If the allocation failed there'll already be an
218 		 * error in the log (and likely a huge and growing
219 		 * number of them since they system will be under
220 		 * extreme memory pressure), so simply assume
221 		 * collision for safety but don't add to the log
222 		 * flood.
223 		 */
224 		return true;
225 
226 	dentry = try_lookup_noperm(&QSTR(name), sb->s_root);
227 	kfree(name);
228 	if (!IS_ERR_OR_NULL(dentry))
229 		dput(dentry);
230 
231 	return dentry != NULL;
232 }
233 
efivarfs_create_dentry(struct super_block * sb,efi_char16_t * name16,unsigned long name_size,efi_guid_t vendor,char * name)234 static int efivarfs_create_dentry(struct super_block *sb, efi_char16_t *name16,
235 				  unsigned long name_size, efi_guid_t vendor,
236 				  char *name)
237 {
238 	struct efivar_entry *entry;
239 	struct inode *inode;
240 	struct dentry *dentry, *root = sb->s_root;
241 	unsigned long size = 0;
242 	int len;
243 	int err = -ENOMEM;
244 	bool is_removable = false;
245 
246 	/* length of the variable name itself: remove GUID and separator */
247 	len = strlen(name) - EFI_VARIABLE_GUID_LEN - 1;
248 
249 	if (efivar_variable_is_removable(vendor, name, len))
250 		is_removable = true;
251 
252 	inode = efivarfs_get_inode(sb, d_inode(root), S_IFREG | 0644, 0,
253 				   is_removable);
254 	if (!inode)
255 		goto fail_name;
256 
257 	entry = efivar_entry(inode);
258 
259 	memcpy(entry->var.VariableName, name16, name_size);
260 	memcpy(&(entry->var.VendorGuid), &vendor, sizeof(efi_guid_t));
261 
262 	dentry = efivarfs_alloc_dentry(root, name);
263 	if (IS_ERR(dentry)) {
264 		err = PTR_ERR(dentry);
265 		goto fail_inode;
266 	}
267 
268 	__efivar_entry_get(entry, NULL, &size, NULL);
269 
270 	/* copied by the above to local storage in the dentry. */
271 	kfree(name);
272 
273 	inode_lock(inode);
274 	inode->i_private = entry;
275 	i_size_write(inode, size + sizeof(__u32)); /* attributes + data */
276 	inode_unlock(inode);
277 	d_add(dentry, inode);
278 
279 	return 0;
280 
281 fail_inode:
282 	iput(inode);
283 fail_name:
284 	kfree(name);
285 
286 	return err;
287 }
288 
efivarfs_callback(efi_char16_t * name16,efi_guid_t vendor,unsigned long name_size,void * data)289 static int efivarfs_callback(efi_char16_t *name16, efi_guid_t vendor,
290 			     unsigned long name_size, void *data)
291 {
292 	struct super_block *sb = (struct super_block *)data;
293 	char *name;
294 
295 	if (guid_equal(&vendor, &LINUX_EFI_RANDOM_SEED_TABLE_GUID))
296 		return 0;
297 
298 	name = efivar_get_utf8name(name16, &vendor);
299 	if (!name)
300 		return -ENOMEM;
301 
302 	return efivarfs_create_dentry(sb, name16, name_size, vendor, name);
303 }
304 
305 enum {
306 	Opt_uid, Opt_gid,
307 };
308 
309 static const struct fs_parameter_spec efivarfs_parameters[] = {
310 	fsparam_uid("uid", Opt_uid),
311 	fsparam_gid("gid", Opt_gid),
312 	{},
313 };
314 
efivarfs_parse_param(struct fs_context * fc,struct fs_parameter * param)315 static int efivarfs_parse_param(struct fs_context *fc, struct fs_parameter *param)
316 {
317 	struct efivarfs_fs_info *sbi = fc->s_fs_info;
318 	struct efivarfs_mount_opts *opts = &sbi->mount_opts;
319 	struct fs_parse_result result;
320 	int opt;
321 
322 	opt = fs_parse(fc, efivarfs_parameters, param, &result);
323 	if (opt < 0)
324 		return opt;
325 
326 	switch (opt) {
327 	case Opt_uid:
328 		opts->uid = result.uid;
329 		break;
330 	case Opt_gid:
331 		opts->gid = result.gid;
332 		break;
333 	default:
334 		return -EINVAL;
335 	}
336 
337 	return 0;
338 }
339 
efivarfs_fill_super(struct super_block * sb,struct fs_context * fc)340 static int efivarfs_fill_super(struct super_block *sb, struct fs_context *fc)
341 {
342 	struct efivarfs_fs_info *sfi = sb->s_fs_info;
343 	struct inode *inode = NULL;
344 	struct dentry *root;
345 	int err;
346 
347 	sb->s_maxbytes          = MAX_LFS_FILESIZE;
348 	sb->s_blocksize         = PAGE_SIZE;
349 	sb->s_blocksize_bits    = PAGE_SHIFT;
350 	sb->s_magic             = EFIVARFS_MAGIC;
351 	sb->s_op                = &efivarfs_ops;
352 	set_default_d_op(sb, &efivarfs_d_ops);
353 	sb->s_d_flags |= DCACHE_DONTCACHE;
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 
efivarfs_get_tree(struct fs_context * fc)378 static int efivarfs_get_tree(struct fs_context *fc)
379 {
380 	return get_tree_single(fc, efivarfs_fill_super);
381 }
382 
efivarfs_reconfigure(struct fs_context * fc)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 
efivarfs_free(struct fs_context * fc)393 static void efivarfs_free(struct fs_context *fc)
394 {
395 	kfree(fc->s_fs_info);
396 }
397 
398 static const struct fs_context_operations efivarfs_context_ops = {
399 	.get_tree	= efivarfs_get_tree,
400 	.parse_param	= efivarfs_parse_param,
401 	.reconfigure	= efivarfs_reconfigure,
402 	.free		= efivarfs_free,
403 };
404 
efivarfs_check_missing(efi_char16_t * name16,efi_guid_t vendor,unsigned long name_size,void * data)405 static int efivarfs_check_missing(efi_char16_t *name16, efi_guid_t vendor,
406 				  unsigned long name_size, void *data)
407 {
408 	char *name;
409 	struct super_block *sb = data;
410 	struct dentry *dentry;
411 	int err;
412 
413 	if (guid_equal(&vendor, &LINUX_EFI_RANDOM_SEED_TABLE_GUID))
414 		return 0;
415 
416 	name = efivar_get_utf8name(name16, &vendor);
417 	if (!name)
418 		return -ENOMEM;
419 
420 	dentry = try_lookup_noperm(&QSTR(name), sb->s_root);
421 	if (IS_ERR(dentry)) {
422 		err = PTR_ERR(dentry);
423 		goto out;
424 	}
425 
426 	if (!dentry) {
427 		/* found missing entry */
428 		pr_info("efivarfs: creating variable %s\n", name);
429 		return efivarfs_create_dentry(sb, name16, name_size, vendor, name);
430 	}
431 
432 	dput(dentry);
433 	err = 0;
434 
435  out:
436 	kfree(name);
437 
438 	return err;
439 }
440 
441 static struct file_system_type efivarfs_type;
442 
efivarfs_freeze_fs(struct super_block * sb)443 static int efivarfs_freeze_fs(struct super_block *sb)
444 {
445 	/* Nothing for us to do. */
446 	return 0;
447 }
448 
efivarfs_unfreeze_fs(struct super_block * sb)449 static int efivarfs_unfreeze_fs(struct super_block *sb)
450 {
451 	struct dentry *child = NULL;
452 
453 	/*
454 	 * Unconditionally resync the variable state on a thaw request.
455 	 * Given the size of efivarfs it really doesn't matter to simply
456 	 * iterate through all of the entries and resync. Freeze/thaw
457 	 * requests are rare enough for that to not matter and the
458 	 * number of entries is pretty low too. So we really don't care.
459 	 */
460 	pr_info("efivarfs: resyncing variable state\n");
461 	for (;;) {
462 		int err;
463 		unsigned long size = 0;
464 		struct inode *inode;
465 		struct efivar_entry *entry;
466 
467 		child = find_next_child(sb->s_root, child);
468 		if (!child)
469 			break;
470 
471 		inode = d_inode(child);
472 		entry = efivar_entry(inode);
473 
474 		err = efivar_entry_size(entry, &size);
475 		if (err)
476 			size = 0;
477 		else
478 			size += sizeof(__u32);
479 
480 		inode_lock(inode);
481 		i_size_write(inode, size);
482 		inode_unlock(inode);
483 
484 		/* The variable doesn't exist anymore, delete it. */
485 		if (!size) {
486 			pr_info("efivarfs: removing variable %pd\n", child);
487 			simple_recursive_removal(child, NULL);
488 		}
489 	}
490 
491 	efivar_init(efivarfs_check_missing, sb, false);
492 	pr_info("efivarfs: finished resyncing variable state\n");
493 	return 0;
494 }
495 
efivarfs_init_fs_context(struct fs_context * fc)496 static int efivarfs_init_fs_context(struct fs_context *fc)
497 {
498 	struct efivarfs_fs_info *sfi;
499 
500 	if (!efivar_is_available())
501 		return -EOPNOTSUPP;
502 
503 	sfi = kzalloc(sizeof(*sfi), GFP_KERNEL);
504 	if (!sfi)
505 		return -ENOMEM;
506 
507 	sfi->mount_opts.uid = GLOBAL_ROOT_UID;
508 	sfi->mount_opts.gid = GLOBAL_ROOT_GID;
509 
510 	fc->s_fs_info = sfi;
511 	fc->ops = &efivarfs_context_ops;
512 
513 	return 0;
514 }
515 
efivarfs_kill_sb(struct super_block * sb)516 static void efivarfs_kill_sb(struct super_block *sb)
517 {
518 	struct efivarfs_fs_info *sfi = sb->s_fs_info;
519 
520 	blocking_notifier_chain_unregister(&efivar_ops_nh, &sfi->nb);
521 	kill_litter_super(sb);
522 
523 	kfree(sfi);
524 }
525 
526 static struct file_system_type efivarfs_type = {
527 	.owner   = THIS_MODULE,
528 	.name    = "efivarfs",
529 	.init_fs_context = efivarfs_init_fs_context,
530 	.kill_sb = efivarfs_kill_sb,
531 	.parameters = efivarfs_parameters,
532 };
533 
efivarfs_init(void)534 static __init int efivarfs_init(void)
535 {
536 	return register_filesystem(&efivarfs_type);
537 }
538 
efivarfs_exit(void)539 static __exit void efivarfs_exit(void)
540 {
541 	unregister_filesystem(&efivarfs_type);
542 }
543 
544 MODULE_AUTHOR("Matthew Garrett, Jeremy Kerr");
545 MODULE_DESCRIPTION("EFI Variable Filesystem");
546 MODULE_LICENSE("GPL");
547 MODULE_ALIAS_FS("efivarfs");
548 
549 module_init(efivarfs_init);
550 module_exit(efivarfs_exit);
551