xref: /openbmc/linux/fs/efivarfs/inode.c (revision c4a54f70)
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/efi.h>
8 #include <linux/fs.h>
9 #include <linux/ctype.h>
10 #include <linux/slab.h>
11 #include <linux/uuid.h>
12 
13 #include "internal.h"
14 
15 struct inode *efivarfs_get_inode(struct super_block *sb,
16 				const struct inode *dir, int mode,
17 				dev_t dev, bool is_removable)
18 {
19 	struct inode *inode = new_inode(sb);
20 
21 	if (inode) {
22 		inode->i_ino = get_next_ino();
23 		inode->i_mode = mode;
24 		inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode);
25 		inode->i_flags = is_removable ? 0 : S_IMMUTABLE;
26 		switch (mode & S_IFMT) {
27 		case S_IFREG:
28 			inode->i_fop = &efivarfs_file_operations;
29 			break;
30 		case S_IFDIR:
31 			inode->i_op = &efivarfs_dir_inode_operations;
32 			inode->i_fop = &simple_dir_operations;
33 			inc_nlink(inode);
34 			break;
35 		}
36 	}
37 	return inode;
38 }
39 
40 /*
41  * Return true if 'str' is a valid efivarfs filename of the form,
42  *
43  *	VariableName-12345678-1234-1234-1234-1234567891bc
44  */
45 bool efivarfs_valid_name(const char *str, int len)
46 {
47 	const char *s = str + len - EFI_VARIABLE_GUID_LEN;
48 
49 	/*
50 	 * We need a GUID, plus at least one letter for the variable name,
51 	 * plus the '-' separator
52 	 */
53 	if (len < EFI_VARIABLE_GUID_LEN + 2)
54 		return false;
55 
56 	/* GUID must be preceded by a '-' */
57 	if (*(s - 1) != '-')
58 		return false;
59 
60 	/*
61 	 * Validate that 's' is of the correct format, e.g.
62 	 *
63 	 *	12345678-1234-1234-1234-123456789abc
64 	 */
65 	return uuid_is_valid(s);
66 }
67 
68 static int efivarfs_create(struct inode *dir, struct dentry *dentry,
69 			  umode_t mode, bool excl)
70 {
71 	struct inode *inode = NULL;
72 	struct efivar_entry *var;
73 	int namelen, i = 0, err = 0;
74 	bool is_removable = false;
75 
76 	if (!efivarfs_valid_name(dentry->d_name.name, dentry->d_name.len))
77 		return -EINVAL;
78 
79 	var = kzalloc(sizeof(struct efivar_entry), GFP_KERNEL);
80 	if (!var)
81 		return -ENOMEM;
82 
83 	/* length of the variable name itself: remove GUID and separator */
84 	namelen = dentry->d_name.len - EFI_VARIABLE_GUID_LEN - 1;
85 
86 	err = guid_parse(dentry->d_name.name + namelen + 1, &var->var.VendorGuid);
87 	if (err)
88 		goto out;
89 
90 	if (efivar_variable_is_removable(var->var.VendorGuid,
91 					 dentry->d_name.name, namelen))
92 		is_removable = true;
93 
94 	inode = efivarfs_get_inode(dir->i_sb, dir, mode, 0, is_removable);
95 	if (!inode) {
96 		err = -ENOMEM;
97 		goto out;
98 	}
99 
100 	for (i = 0; i < namelen; i++)
101 		var->var.VariableName[i] = dentry->d_name.name[i];
102 
103 	var->var.VariableName[i] = '\0';
104 
105 	inode->i_private = var;
106 
107 	err = efivar_entry_add(var, &efivarfs_list);
108 	if (err)
109 		goto out;
110 
111 	d_instantiate(dentry, inode);
112 	dget(dentry);
113 out:
114 	if (err) {
115 		kfree(var);
116 		if (inode)
117 			iput(inode);
118 	}
119 	return err;
120 }
121 
122 static int efivarfs_unlink(struct inode *dir, struct dentry *dentry)
123 {
124 	struct efivar_entry *var = d_inode(dentry)->i_private;
125 
126 	if (efivar_entry_delete(var))
127 		return -EINVAL;
128 
129 	drop_nlink(d_inode(dentry));
130 	dput(dentry);
131 	return 0;
132 };
133 
134 const struct inode_operations efivarfs_dir_inode_operations = {
135 	.lookup = simple_lookup,
136 	.unlink = efivarfs_unlink,
137 	.create = efivarfs_create,
138 };
139