xref: /openbmc/linux/fs/efivarfs/inode.c (revision aaa746ad)
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/kmemleak.h>
11 #include <linux/slab.h>
12 #include <linux/uuid.h>
13 #include <linux/fileattr.h>
14 
15 #include "internal.h"
16 
17 static const struct inode_operations efivarfs_file_inode_operations;
18 
19 struct inode *efivarfs_get_inode(struct super_block *sb,
20 				const struct inode *dir, int mode,
21 				dev_t dev, bool is_removable)
22 {
23 	struct inode *inode = new_inode(sb);
24 
25 	if (inode) {
26 		inode->i_ino = get_next_ino();
27 		inode->i_mode = mode;
28 		inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode);
29 		inode->i_flags = is_removable ? 0 : S_IMMUTABLE;
30 		switch (mode & S_IFMT) {
31 		case S_IFREG:
32 			inode->i_op = &efivarfs_file_inode_operations;
33 			inode->i_fop = &efivarfs_file_operations;
34 			break;
35 		case S_IFDIR:
36 			inode->i_op = &efivarfs_dir_inode_operations;
37 			inode->i_fop = &simple_dir_operations;
38 			inc_nlink(inode);
39 			break;
40 		}
41 	}
42 	return inode;
43 }
44 
45 /*
46  * Return true if 'str' is a valid efivarfs filename of the form,
47  *
48  *	VariableName-12345678-1234-1234-1234-1234567891bc
49  */
50 bool efivarfs_valid_name(const char *str, int len)
51 {
52 	const char *s = str + len - EFI_VARIABLE_GUID_LEN;
53 
54 	/*
55 	 * We need a GUID, plus at least one letter for the variable name,
56 	 * plus the '-' separator
57 	 */
58 	if (len < EFI_VARIABLE_GUID_LEN + 2)
59 		return false;
60 
61 	/* GUID must be preceded by a '-' */
62 	if (*(s - 1) != '-')
63 		return false;
64 
65 	/*
66 	 * Validate that 's' is of the correct format, e.g.
67 	 *
68 	 *	12345678-1234-1234-1234-123456789abc
69 	 */
70 	return uuid_is_valid(s);
71 }
72 
73 static int efivarfs_create(struct user_namespace *mnt_userns, struct inode *dir,
74 			   struct dentry *dentry, umode_t mode, bool excl)
75 {
76 	struct inode *inode = NULL;
77 	struct efivar_entry *var;
78 	int namelen, i = 0, err = 0;
79 	bool is_removable = false;
80 
81 	if (!efivarfs_valid_name(dentry->d_name.name, dentry->d_name.len))
82 		return -EINVAL;
83 
84 	var = kzalloc(sizeof(struct efivar_entry), GFP_KERNEL);
85 	if (!var)
86 		return -ENOMEM;
87 
88 	/* length of the variable name itself: remove GUID and separator */
89 	namelen = dentry->d_name.len - EFI_VARIABLE_GUID_LEN - 1;
90 
91 	err = guid_parse(dentry->d_name.name + namelen + 1, &var->var.VendorGuid);
92 	if (err)
93 		goto out;
94 	if (guid_equal(&var->var.VendorGuid, &LINUX_EFI_RANDOM_SEED_TABLE_GUID)) {
95 		err = -EPERM;
96 		goto out;
97 	}
98 
99 	if (efivar_variable_is_removable(var->var.VendorGuid,
100 					 dentry->d_name.name, namelen))
101 		is_removable = true;
102 
103 	inode = efivarfs_get_inode(dir->i_sb, dir, mode, 0, is_removable);
104 	if (!inode) {
105 		err = -ENOMEM;
106 		goto out;
107 	}
108 
109 	for (i = 0; i < namelen; i++)
110 		var->var.VariableName[i] = dentry->d_name.name[i];
111 
112 	var->var.VariableName[i] = '\0';
113 
114 	inode->i_private = var;
115 	kmemleak_ignore(var);
116 
117 	err = efivar_entry_add(var, &efivarfs_list);
118 	if (err)
119 		goto out;
120 
121 	d_instantiate(dentry, inode);
122 	dget(dentry);
123 out:
124 	if (err) {
125 		kfree(var);
126 		if (inode)
127 			iput(inode);
128 	}
129 	return err;
130 }
131 
132 static int efivarfs_unlink(struct inode *dir, struct dentry *dentry)
133 {
134 	struct efivar_entry *var = d_inode(dentry)->i_private;
135 
136 	if (efivar_entry_delete(var))
137 		return -EINVAL;
138 
139 	drop_nlink(d_inode(dentry));
140 	dput(dentry);
141 	return 0;
142 };
143 
144 const struct inode_operations efivarfs_dir_inode_operations = {
145 	.lookup = simple_lookup,
146 	.unlink = efivarfs_unlink,
147 	.create = efivarfs_create,
148 };
149 
150 static int
151 efivarfs_fileattr_get(struct dentry *dentry, struct fileattr *fa)
152 {
153 	unsigned int i_flags;
154 	unsigned int flags = 0;
155 
156 	i_flags = d_inode(dentry)->i_flags;
157 	if (i_flags & S_IMMUTABLE)
158 		flags |= FS_IMMUTABLE_FL;
159 
160 	fileattr_fill_flags(fa, flags);
161 
162 	return 0;
163 }
164 
165 static int
166 efivarfs_fileattr_set(struct user_namespace *mnt_userns,
167 		      struct dentry *dentry, struct fileattr *fa)
168 {
169 	unsigned int i_flags = 0;
170 
171 	if (fileattr_has_fsx(fa))
172 		return -EOPNOTSUPP;
173 
174 	if (fa->flags & ~FS_IMMUTABLE_FL)
175 		return -EOPNOTSUPP;
176 
177 	if (fa->flags & FS_IMMUTABLE_FL)
178 		i_flags |= S_IMMUTABLE;
179 
180 	inode_set_flags(d_inode(dentry), i_flags, S_IMMUTABLE);
181 
182 	return 0;
183 }
184 
185 static const struct inode_operations efivarfs_file_inode_operations = {
186 	.fileattr_get = efivarfs_fileattr_get,
187 	.fileattr_set = efivarfs_fileattr_set,
188 };
189