1 /*
2 * Copyright (C) 2012 Red Hat, Inc.
3 * Copyright (C) 2012 Jeremy Kerr <jeremy.kerr@canonical.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
8 */
9
10 #include <linux/efi.h>
11 #include <linux/fs.h>
12 #include <linux/ctype.h>
13 #include <linux/kmemleak.h>
14 #include <linux/slab.h>
15 #include <linux/uuid.h>
16
17 #include "internal.h"
18
efivarfs_get_inode(struct super_block * sb,const struct inode * dir,int mode,dev_t dev,bool is_removable)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_fop = &efivarfs_file_operations;
33 break;
34 case S_IFDIR:
35 inode->i_op = &efivarfs_dir_inode_operations;
36 inode->i_fop = &simple_dir_operations;
37 inc_nlink(inode);
38 break;
39 }
40 }
41 return inode;
42 }
43
44 /*
45 * Return true if 'str' is a valid efivarfs filename of the form,
46 *
47 * VariableName-12345678-1234-1234-1234-1234567891bc
48 */
efivarfs_valid_name(const char * str,int len)49 bool efivarfs_valid_name(const char *str, int len)
50 {
51 const char *s = str + len - EFI_VARIABLE_GUID_LEN;
52
53 /*
54 * We need a GUID, plus at least one letter for the variable name,
55 * plus the '-' separator
56 */
57 if (len < EFI_VARIABLE_GUID_LEN + 2)
58 return false;
59
60 /* GUID must be preceded by a '-' */
61 if (*(s - 1) != '-')
62 return false;
63
64 /*
65 * Validate that 's' is of the correct format, e.g.
66 *
67 * 12345678-1234-1234-1234-123456789abc
68 */
69 return uuid_is_valid(s);
70 }
71
efivarfs_create(struct inode * dir,struct dentry * dentry,umode_t mode,bool excl)72 static int efivarfs_create(struct inode *dir, struct dentry *dentry,
73 umode_t mode, bool excl)
74 {
75 struct inode *inode = NULL;
76 struct efivar_entry *var;
77 int namelen, i = 0, err = 0;
78 bool is_removable = false;
79
80 if (!efivarfs_valid_name(dentry->d_name.name, dentry->d_name.len))
81 return -EINVAL;
82
83 var = kzalloc(sizeof(struct efivar_entry), GFP_KERNEL);
84 if (!var)
85 return -ENOMEM;
86
87 /* length of the variable name itself: remove GUID and separator */
88 namelen = dentry->d_name.len - EFI_VARIABLE_GUID_LEN - 1;
89
90 err = guid_parse(dentry->d_name.name + namelen + 1, &var->var.VendorGuid);
91 if (err)
92 goto out;
93
94 if (efivar_variable_is_removable(var->var.VendorGuid,
95 dentry->d_name.name, namelen))
96 is_removable = true;
97
98 inode = efivarfs_get_inode(dir->i_sb, dir, mode, 0, is_removable);
99 if (!inode) {
100 err = -ENOMEM;
101 goto out;
102 }
103
104 for (i = 0; i < namelen; i++)
105 var->var.VariableName[i] = dentry->d_name.name[i];
106
107 var->var.VariableName[i] = '\0';
108
109 inode->i_private = var;
110 kmemleak_ignore(var);
111
112 err = efivar_entry_add(var, &efivarfs_list);
113 if (err)
114 goto out;
115
116 d_instantiate(dentry, inode);
117 dget(dentry);
118 out:
119 if (err) {
120 kfree(var);
121 if (inode)
122 iput(inode);
123 }
124 return err;
125 }
126
efivarfs_unlink(struct inode * dir,struct dentry * dentry)127 static int efivarfs_unlink(struct inode *dir, struct dentry *dentry)
128 {
129 struct efivar_entry *var = d_inode(dentry)->i_private;
130
131 if (efivar_entry_delete(var))
132 return -EINVAL;
133
134 drop_nlink(d_inode(dentry));
135 dput(dentry);
136 return 0;
137 };
138
139 const struct inode_operations efivarfs_dir_inode_operations = {
140 .lookup = simple_lookup,
141 .unlink = efivarfs_unlink,
142 .create = efivarfs_create,
143 };
144