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