1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * efi_secret module
4 *
5 * Copyright (C) 2022 IBM Corporation
6 * Author: Dov Murik <dovmurik@linux.ibm.com>
7 */
8
9 /**
10 * DOC: efi_secret: Allow reading EFI confidential computing (coco) secret area
11 * via securityfs interface.
12 *
13 * When the module is loaded (and securityfs is mounted, typically under
14 * /sys/kernel/security), a "secrets/coco" directory is created in securityfs.
15 * In it, a file is created for each secret entry. The name of each such file
16 * is the GUID of the secret entry, and its content is the secret data.
17 */
18
19 #include <linux/platform_device.h>
20 #include <linux/seq_file.h>
21 #include <linux/fs.h>
22 #include <linux/kernel.h>
23 #include <linux/init.h>
24 #include <linux/module.h>
25 #include <linux/io.h>
26 #include <linux/security.h>
27 #include <linux/efi.h>
28 #include <linux/cacheflush.h>
29
30 #define EFI_SECRET_NUM_FILES 64
31
32 struct efi_secret {
33 struct dentry *secrets_dir;
34 struct dentry *fs_dir;
35 struct dentry *fs_files[EFI_SECRET_NUM_FILES];
36 void __iomem *secret_data;
37 u64 secret_data_len;
38 };
39
40 /*
41 * Structure of the EFI secret area
42 *
43 * Offset Length
44 * (bytes) (bytes) Usage
45 * ------- ------- -----
46 * 0 16 Secret table header GUID (must be 1e74f542-71dd-4d66-963e-ef4287ff173b)
47 * 16 4 Length of bytes of the entire secret area
48 *
49 * 20 16 First secret entry's GUID
50 * 36 4 First secret entry's length in bytes (= 16 + 4 + x)
51 * 40 x First secret entry's data
52 *
53 * 40+x 16 Second secret entry's GUID
54 * 56+x 4 Second secret entry's length in bytes (= 16 + 4 + y)
55 * 60+x y Second secret entry's data
56 *
57 * (... and so on for additional entries)
58 *
59 * The GUID of each secret entry designates the usage of the secret data.
60 */
61
62 /**
63 * struct secret_header - Header of entire secret area; this should be followed
64 * by instances of struct secret_entry.
65 * @guid: Must be EFI_SECRET_TABLE_HEADER_GUID
66 * @len: Length in bytes of entire secret area, including header
67 */
68 struct secret_header {
69 efi_guid_t guid;
70 u32 len;
71 } __attribute((packed));
72
73 /**
74 * struct secret_entry - Holds one secret entry
75 * @guid: Secret-specific GUID (or NULL_GUID if this secret entry was deleted)
76 * @len: Length of secret entry, including its guid and len fields
77 * @data: The secret data (full of zeros if this secret entry was deleted)
78 */
79 struct secret_entry {
80 efi_guid_t guid;
81 u32 len;
82 u8 data[];
83 } __attribute((packed));
84
secret_entry_data_len(struct secret_entry * e)85 static size_t secret_entry_data_len(struct secret_entry *e)
86 {
87 return e->len - sizeof(*e);
88 }
89
90 static struct efi_secret the_efi_secret;
91
efi_secret_get(void)92 static inline struct efi_secret *efi_secret_get(void)
93 {
94 return &the_efi_secret;
95 }
96
efi_secret_bin_file_show(struct seq_file * file,void * data)97 static int efi_secret_bin_file_show(struct seq_file *file, void *data)
98 {
99 struct secret_entry *e = file->private;
100
101 if (e)
102 seq_write(file, e->data, secret_entry_data_len(e));
103
104 return 0;
105 }
106 DEFINE_SHOW_ATTRIBUTE(efi_secret_bin_file);
107
108 /*
109 * Overwrite memory content with zeroes, and ensure that dirty cache lines are
110 * actually written back to memory, to clear out the secret.
111 */
wipe_memory(void * addr,size_t size)112 static void wipe_memory(void *addr, size_t size)
113 {
114 memzero_explicit(addr, size);
115 #ifdef CONFIG_X86
116 clflush_cache_range(addr, size);
117 #endif
118 }
119
efi_secret_unlink(struct inode * dir,struct dentry * dentry)120 static int efi_secret_unlink(struct inode *dir, struct dentry *dentry)
121 {
122 struct efi_secret *s = efi_secret_get();
123 struct inode *inode = d_inode(dentry);
124 struct secret_entry *e = (struct secret_entry *)inode->i_private;
125 int i;
126
127 if (e) {
128 /* Zero out the secret data */
129 wipe_memory(e->data, secret_entry_data_len(e));
130 e->guid = NULL_GUID;
131 }
132
133 inode->i_private = NULL;
134
135 for (i = 0; i < EFI_SECRET_NUM_FILES; i++)
136 if (s->fs_files[i] == dentry)
137 s->fs_files[i] = NULL;
138
139 return simple_unlink(inode, dentry);
140 }
141
142 static const struct inode_operations efi_secret_dir_inode_operations = {
143 .lookup = simple_lookup,
144 .unlink = efi_secret_unlink,
145 };
146
efi_secret_map_area(struct platform_device * dev)147 static int efi_secret_map_area(struct platform_device *dev)
148 {
149 int ret;
150 struct efi_secret *s = efi_secret_get();
151 struct linux_efi_coco_secret_area *secret_area;
152
153 if (efi.coco_secret == EFI_INVALID_TABLE_ADDR) {
154 dev_err(&dev->dev, "Secret area address is not available\n");
155 return -EINVAL;
156 }
157
158 secret_area = memremap(efi.coco_secret, sizeof(*secret_area), MEMREMAP_WB);
159 if (secret_area == NULL) {
160 dev_err(&dev->dev, "Could not map secret area EFI config entry\n");
161 return -ENOMEM;
162 }
163 if (!secret_area->base_pa || secret_area->size < sizeof(struct secret_header)) {
164 dev_err(&dev->dev,
165 "Invalid secret area memory location (base_pa=0x%llx size=0x%llx)\n",
166 secret_area->base_pa, secret_area->size);
167 ret = -EINVAL;
168 goto unmap;
169 }
170
171 s->secret_data = ioremap_encrypted(secret_area->base_pa, secret_area->size);
172 if (s->secret_data == NULL) {
173 dev_err(&dev->dev, "Could not map secret area\n");
174 ret = -ENOMEM;
175 goto unmap;
176 }
177
178 s->secret_data_len = secret_area->size;
179 ret = 0;
180
181 unmap:
182 memunmap(secret_area);
183 return ret;
184 }
185
efi_secret_securityfs_teardown(struct platform_device * dev)186 static void efi_secret_securityfs_teardown(struct platform_device *dev)
187 {
188 struct efi_secret *s = efi_secret_get();
189 int i;
190
191 for (i = (EFI_SECRET_NUM_FILES - 1); i >= 0; i--) {
192 securityfs_remove(s->fs_files[i]);
193 s->fs_files[i] = NULL;
194 }
195
196 securityfs_remove(s->fs_dir);
197 s->fs_dir = NULL;
198
199 securityfs_remove(s->secrets_dir);
200 s->secrets_dir = NULL;
201
202 dev_dbg(&dev->dev, "Removed securityfs entries\n");
203 }
204
efi_secret_securityfs_setup(struct platform_device * dev)205 static int efi_secret_securityfs_setup(struct platform_device *dev)
206 {
207 struct efi_secret *s = efi_secret_get();
208 int ret = 0, i = 0, bytes_left;
209 unsigned char *ptr;
210 struct secret_header *h;
211 struct secret_entry *e;
212 struct dentry *dent;
213 char guid_str[EFI_VARIABLE_GUID_LEN + 1];
214
215 ptr = (void __force *)s->secret_data;
216 h = (struct secret_header *)ptr;
217 if (efi_guidcmp(h->guid, EFI_SECRET_TABLE_HEADER_GUID)) {
218 /*
219 * This is not an error: it just means that EFI defines secret
220 * area but it was not populated by the Guest Owner.
221 */
222 dev_dbg(&dev->dev, "EFI secret area does not start with correct GUID\n");
223 return -ENODEV;
224 }
225 if (h->len < sizeof(*h)) {
226 dev_err(&dev->dev, "EFI secret area reported length is too small\n");
227 return -EINVAL;
228 }
229 if (h->len > s->secret_data_len) {
230 dev_err(&dev->dev, "EFI secret area reported length is too big\n");
231 return -EINVAL;
232 }
233
234 s->secrets_dir = NULL;
235 s->fs_dir = NULL;
236 memset(s->fs_files, 0, sizeof(s->fs_files));
237
238 dent = securityfs_create_dir("secrets", NULL);
239 if (IS_ERR(dent)) {
240 dev_err(&dev->dev, "Error creating secrets securityfs directory entry err=%ld\n",
241 PTR_ERR(dent));
242 return PTR_ERR(dent);
243 }
244 s->secrets_dir = dent;
245
246 dent = securityfs_create_dir("coco", s->secrets_dir);
247 if (IS_ERR(dent)) {
248 dev_err(&dev->dev, "Error creating coco securityfs directory entry err=%ld\n",
249 PTR_ERR(dent));
250 return PTR_ERR(dent);
251 }
252 d_inode(dent)->i_op = &efi_secret_dir_inode_operations;
253 s->fs_dir = dent;
254
255 bytes_left = h->len - sizeof(*h);
256 ptr += sizeof(*h);
257 while (bytes_left >= (int)sizeof(*e) && i < EFI_SECRET_NUM_FILES) {
258 e = (struct secret_entry *)ptr;
259 if (e->len < sizeof(*e) || e->len > (unsigned int)bytes_left) {
260 dev_err(&dev->dev, "EFI secret area is corrupted\n");
261 ret = -EINVAL;
262 goto err_cleanup;
263 }
264
265 /* Skip deleted entries (which will have NULL_GUID) */
266 if (efi_guidcmp(e->guid, NULL_GUID)) {
267 efi_guid_to_str(&e->guid, guid_str);
268
269 dent = securityfs_create_file(guid_str, 0440, s->fs_dir, (void *)e,
270 &efi_secret_bin_file_fops);
271 if (IS_ERR(dent)) {
272 dev_err(&dev->dev, "Error creating efi_secret securityfs entry\n");
273 ret = PTR_ERR(dent);
274 goto err_cleanup;
275 }
276
277 s->fs_files[i++] = dent;
278 }
279 ptr += e->len;
280 bytes_left -= e->len;
281 }
282
283 dev_info(&dev->dev, "Created %d entries in securityfs secrets/coco\n", i);
284 return 0;
285
286 err_cleanup:
287 efi_secret_securityfs_teardown(dev);
288 return ret;
289 }
290
efi_secret_unmap_area(void)291 static void efi_secret_unmap_area(void)
292 {
293 struct efi_secret *s = efi_secret_get();
294
295 if (s->secret_data) {
296 iounmap(s->secret_data);
297 s->secret_data = NULL;
298 s->secret_data_len = 0;
299 }
300 }
301
efi_secret_probe(struct platform_device * dev)302 static int efi_secret_probe(struct platform_device *dev)
303 {
304 int ret;
305
306 ret = efi_secret_map_area(dev);
307 if (ret)
308 return ret;
309
310 ret = efi_secret_securityfs_setup(dev);
311 if (ret)
312 goto err_unmap;
313
314 return ret;
315
316 err_unmap:
317 efi_secret_unmap_area();
318 return ret;
319 }
320
efi_secret_remove(struct platform_device * dev)321 static void efi_secret_remove(struct platform_device *dev)
322 {
323 efi_secret_securityfs_teardown(dev);
324 efi_secret_unmap_area();
325 }
326
327 static struct platform_driver efi_secret_driver = {
328 .probe = efi_secret_probe,
329 .remove_new = efi_secret_remove,
330 .driver = {
331 .name = "efi_secret",
332 },
333 };
334
335 module_platform_driver(efi_secret_driver);
336
337 MODULE_DESCRIPTION("Confidential computing EFI secret area access");
338 MODULE_AUTHOR("IBM");
339 MODULE_LICENSE("GPL");
340 MODULE_ALIAS("platform:efi_secret");
341