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