1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2012 Red Hat, Inc.
4 * Copyright (C) 2012 Jeremy Kerr <jeremy.kerr@canonical.com>
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>
17 static const struct inode_operations efivarfs_file_inode_operations;
19 struct inode *efivarfs_get_inode(struct super_block *sb,
20 const struct inode *dir, int mode,
21 dev_t dev, bool is_removable)
23 struct inode *inode = new_inode(sb);
26 inode->i_ino = get_next_ino();
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) {
32 inode->i_op = &efivarfs_file_inode_operations;
33 inode->i_fop = &efivarfs_file_operations;
36 inode->i_op = &efivarfs_dir_inode_operations;
37 inode->i_fop = &simple_dir_operations;
46 * Return true if 'str' is a valid efivarfs filename of the form,
48 * VariableName-12345678-1234-1234-1234-1234567891bc
50 bool efivarfs_valid_name(const char *str, int len)
52 const char *s = str + len - EFI_VARIABLE_GUID_LEN;
55 * We need a GUID, plus at least one letter for the variable name,
56 * plus the '-' separator
58 if (len < EFI_VARIABLE_GUID_LEN + 2)
61 /* GUID must be preceded by a '-' */
66 * Validate that 's' is of the correct format, e.g.
68 * 12345678-1234-1234-1234-123456789abc
70 return uuid_is_valid(s);
73 static int efivarfs_create(struct user_namespace *mnt_userns, struct inode *dir,
74 struct dentry *dentry, umode_t mode, bool excl)
76 struct inode *inode = NULL;
77 struct efivar_entry *var;
78 int namelen, i = 0, err = 0;
79 bool is_removable = false;
81 if (!efivarfs_valid_name(dentry->d_name.name, dentry->d_name.len))
84 var = kzalloc(sizeof(struct efivar_entry), GFP_KERNEL);
88 /* length of the variable name itself: remove GUID and separator */
89 namelen = dentry->d_name.len - EFI_VARIABLE_GUID_LEN - 1;
91 err = guid_parse(dentry->d_name.name + namelen + 1, &var->var.VendorGuid);
95 if (efivar_variable_is_removable(var->var.VendorGuid,
96 dentry->d_name.name, namelen))
99 inode = efivarfs_get_inode(dir->i_sb, dir, mode, 0, is_removable);
105 for (i = 0; i < namelen; i++)
106 var->var.VariableName[i] = dentry->d_name.name[i];
108 var->var.VariableName[i] = '\0';
110 inode->i_private = var;
111 kmemleak_ignore(var);
113 err = efivar_entry_add(var, &efivarfs_list);
117 d_instantiate(dentry, inode);
128 static int efivarfs_unlink(struct inode *dir, struct dentry *dentry)
130 struct efivar_entry *var = d_inode(dentry)->i_private;
132 if (efivar_entry_delete(var))
135 drop_nlink(d_inode(dentry));
140 const struct inode_operations efivarfs_dir_inode_operations = {
141 .lookup = simple_lookup,
142 .unlink = efivarfs_unlink,
143 .create = efivarfs_create,
147 efivarfs_fileattr_get(struct dentry *dentry, struct fileattr *fa)
149 unsigned int i_flags;
150 unsigned int flags = 0;
152 i_flags = d_inode(dentry)->i_flags;
153 if (i_flags & S_IMMUTABLE)
154 flags |= FS_IMMUTABLE_FL;
156 fileattr_fill_flags(fa, flags);
162 efivarfs_fileattr_set(struct user_namespace *mnt_userns,
163 struct dentry *dentry, struct fileattr *fa)
165 unsigned int i_flags = 0;
167 if (fileattr_has_fsx(fa))
170 if (fa->flags & ~FS_IMMUTABLE_FL)
173 if (fa->flags & FS_IMMUTABLE_FL)
174 i_flags |= S_IMMUTABLE;
176 inode_set_flags(d_inode(dentry), i_flags, S_IMMUTABLE);
181 static const struct inode_operations efivarfs_file_inode_operations = {
182 .fileattr_get = efivarfs_fileattr_get,
183 .fileattr_set = efivarfs_fileattr_set,