1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2012 Red Hat, Inc.
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);
24 struct efivarfs_fs_info *fsi = sb->s_fs_info;
25 struct efivarfs_mount_opts *opts = &fsi->mount_opts;
28 inode->i_uid = opts->uid;
29 inode->i_gid = opts->gid;
30 inode->i_ino = get_next_ino();
32 simple_inode_init_ts(inode);
33 inode->i_flags = is_removable ? 0 : S_IMMUTABLE;
34 switch (mode & S_IFMT) {
36 inode->i_op = &efivarfs_file_inode_operations;
37 inode->i_fop = &efivarfs_file_operations;
40 inode->i_op = &efivarfs_dir_inode_operations;
41 inode->i_fop = &simple_dir_operations;
50 * Return true if 'str' is a valid efivarfs filename of the form,
52 * VariableName-12345678-1234-1234-1234-1234567891bc
54 bool efivarfs_valid_name(const char *str, int len)
56 const char *s = str + len - EFI_VARIABLE_GUID_LEN;
59 * We need a GUID, plus at least one letter for the variable name,
60 * plus the '-' separator
62 if (len < EFI_VARIABLE_GUID_LEN + 2)
65 /* GUID must be preceded by a '-' */
70 * Validate that 's' is of the correct format, e.g.
72 * 12345678-1234-1234-1234-123456789abc
74 return uuid_is_valid(s);
77 static int efivarfs_create(struct mnt_idmap *idmap, struct inode *dir,
78 struct dentry *dentry, umode_t mode, bool excl)
80 struct efivarfs_fs_info *info = dir->i_sb->s_fs_info;
81 struct inode *inode = NULL;
82 struct efivar_entry *var;
83 int namelen, i = 0, err = 0;
84 bool is_removable = false;
86 if (!efivarfs_valid_name(dentry->d_name.name, dentry->d_name.len))
89 var = kzalloc(sizeof(struct efivar_entry), GFP_KERNEL);
93 /* length of the variable name itself: remove GUID and separator */
94 namelen = dentry->d_name.len - EFI_VARIABLE_GUID_LEN - 1;
96 err = guid_parse(dentry->d_name.name + namelen + 1, &var->var.VendorGuid);
99 if (guid_equal(&var->var.VendorGuid, &LINUX_EFI_RANDOM_SEED_TABLE_GUID)) {
104 if (efivar_variable_is_removable(var->var.VendorGuid,
105 dentry->d_name.name, namelen))
108 inode = efivarfs_get_inode(dir->i_sb, dir, mode, 0, is_removable);
114 for (i = 0; i < namelen; i++)
115 var->var.VariableName[i] = dentry->d_name.name[i];
117 var->var.VariableName[i] = '\0';
119 inode->i_private = var;
120 kmemleak_ignore(var);
122 err = efivar_entry_add(var, &info->efivarfs_list);
126 d_instantiate(dentry, inode);
137 static int efivarfs_unlink(struct inode *dir, struct dentry *dentry)
139 struct efivar_entry *var = d_inode(dentry)->i_private;
141 if (efivar_entry_delete(var))
144 drop_nlink(d_inode(dentry));
149 const struct inode_operations efivarfs_dir_inode_operations = {
150 .lookup = simple_lookup,
151 .unlink = efivarfs_unlink,
152 .create = efivarfs_create,
156 efivarfs_fileattr_get(struct dentry *dentry, struct fileattr *fa)
158 unsigned int i_flags;
159 unsigned int flags = 0;
161 i_flags = d_inode(dentry)->i_flags;
162 if (i_flags & S_IMMUTABLE)
163 flags |= FS_IMMUTABLE_FL;
165 fileattr_fill_flags(fa, flags);
171 efivarfs_fileattr_set(struct mnt_idmap *idmap,
172 struct dentry *dentry, struct fileattr *fa)
174 unsigned int i_flags = 0;
176 if (fileattr_has_fsx(fa))
179 if (fa->flags & ~FS_IMMUTABLE_FL)
182 if (fa->flags & FS_IMMUTABLE_FL)
183 i_flags |= S_IMMUTABLE;
185 inode_set_flags(d_inode(dentry), i_flags, S_IMMUTABLE);
190 static const struct inode_operations efivarfs_file_inode_operations = {
191 .fileattr_get = efivarfs_fileattr_get,
192 .fileattr_set = efivarfs_fileattr_set,