1 // SPDX-License-Identifier: GPL-2.0
5 * Copyright (C) 2022 IBM Corporation
6 * Author: Dov Murik <dovmurik@linux.ibm.com>
10 * DOC: efi_secret: Allow reading EFI confidential computing (coco) secret area
11 * via securityfs interface.
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.
19 #include <linux/platform_device.h>
20 #include <linux/seq_file.h>
22 #include <linux/kernel.h>
23 #include <linux/init.h>
24 #include <linux/module.h>
26 #include <linux/security.h>
27 #include <linux/efi.h>
28 #include <linux/cacheflush.h>
30 #define EFI_SECRET_NUM_FILES 64
33 struct dentry
*secrets_dir
;
34 struct dentry
*fs_dir
;
35 struct dentry
*fs_files
[EFI_SECRET_NUM_FILES
];
36 void __iomem
*secret_data
;
41 * Structure of the EFI secret area
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
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
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
57 * (... and so on for additional entries)
59 * The GUID of each secret entry designates the usage of the secret data.
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
68 struct secret_header
{
71 } __attribute((packed
));
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)
83 } __attribute((packed
));
85 static size_t secret_entry_data_len(struct secret_entry
*e
)
87 return e
->len
- sizeof(*e
);
90 static struct efi_secret the_efi_secret
;
92 static inline struct efi_secret
*efi_secret_get(void)
94 return &the_efi_secret
;
97 static int efi_secret_bin_file_show(struct seq_file
*file
, void *data
)
99 struct secret_entry
*e
= file
->private;
102 seq_write(file
, e
->data
, secret_entry_data_len(e
));
106 DEFINE_SHOW_ATTRIBUTE(efi_secret_bin_file
);
109 * Overwrite memory content with zeroes, and ensure that dirty cache lines are
110 * actually written back to memory, to clear out the secret.
112 static void wipe_memory(void *addr
, size_t size
)
114 memzero_explicit(addr
, size
);
116 clflush_cache_range(addr
, size
);
120 static int efi_secret_unlink(struct inode
*dir
, struct dentry
*dentry
)
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
;
128 /* Zero out the secret data */
129 wipe_memory(e
->data
, secret_entry_data_len(e
));
133 inode
->i_private
= NULL
;
135 for (i
= 0; i
< EFI_SECRET_NUM_FILES
; i
++)
136 if (s
->fs_files
[i
] == dentry
)
137 s
->fs_files
[i
] = NULL
;
140 * securityfs_remove tries to lock the directory's inode, but we reach
141 * the unlink callback when it's already locked
144 securityfs_remove(dentry
);
150 static const struct inode_operations efi_secret_dir_inode_operations
= {
151 .lookup
= simple_lookup
,
152 .unlink
= efi_secret_unlink
,
155 static int efi_secret_map_area(struct platform_device
*dev
)
158 struct efi_secret
*s
= efi_secret_get();
159 struct linux_efi_coco_secret_area
*secret_area
;
161 if (efi
.coco_secret
== EFI_INVALID_TABLE_ADDR
) {
162 dev_err(&dev
->dev
, "Secret area address is not available\n");
166 secret_area
= memremap(efi
.coco_secret
, sizeof(*secret_area
), MEMREMAP_WB
);
167 if (secret_area
== NULL
) {
168 dev_err(&dev
->dev
, "Could not map secret area EFI config entry\n");
171 if (!secret_area
->base_pa
|| secret_area
->size
< sizeof(struct secret_header
)) {
173 "Invalid secret area memory location (base_pa=0x%llx size=0x%llx)\n",
174 secret_area
->base_pa
, secret_area
->size
);
179 s
->secret_data
= ioremap_encrypted(secret_area
->base_pa
, secret_area
->size
);
180 if (s
->secret_data
== NULL
) {
181 dev_err(&dev
->dev
, "Could not map secret area\n");
186 s
->secret_data_len
= secret_area
->size
;
190 memunmap(secret_area
);
194 static void efi_secret_securityfs_teardown(struct platform_device
*dev
)
196 struct efi_secret
*s
= efi_secret_get();
199 for (i
= (EFI_SECRET_NUM_FILES
- 1); i
>= 0; i
--) {
200 securityfs_remove(s
->fs_files
[i
]);
201 s
->fs_files
[i
] = NULL
;
204 securityfs_remove(s
->fs_dir
);
207 securityfs_remove(s
->secrets_dir
);
208 s
->secrets_dir
= NULL
;
210 dev_dbg(&dev
->dev
, "Removed securityfs entries\n");
213 static int efi_secret_securityfs_setup(struct platform_device
*dev
)
215 struct efi_secret
*s
= efi_secret_get();
216 int ret
= 0, i
= 0, bytes_left
;
218 struct secret_header
*h
;
219 struct secret_entry
*e
;
221 char guid_str
[EFI_VARIABLE_GUID_LEN
+ 1];
223 ptr
= (void __force
*)s
->secret_data
;
224 h
= (struct secret_header
*)ptr
;
225 if (efi_guidcmp(h
->guid
, EFI_SECRET_TABLE_HEADER_GUID
)) {
227 * This is not an error: it just means that EFI defines secret
228 * area but it was not populated by the Guest Owner.
230 dev_dbg(&dev
->dev
, "EFI secret area does not start with correct GUID\n");
233 if (h
->len
< sizeof(*h
)) {
234 dev_err(&dev
->dev
, "EFI secret area reported length is too small\n");
237 if (h
->len
> s
->secret_data_len
) {
238 dev_err(&dev
->dev
, "EFI secret area reported length is too big\n");
242 s
->secrets_dir
= NULL
;
244 memset(s
->fs_files
, 0, sizeof(s
->fs_files
));
246 dent
= securityfs_create_dir("secrets", NULL
);
248 dev_err(&dev
->dev
, "Error creating secrets securityfs directory entry err=%ld\n",
250 return PTR_ERR(dent
);
252 s
->secrets_dir
= dent
;
254 dent
= securityfs_create_dir("coco", s
->secrets_dir
);
256 dev_err(&dev
->dev
, "Error creating coco securityfs directory entry err=%ld\n",
258 return PTR_ERR(dent
);
260 d_inode(dent
)->i_op
= &efi_secret_dir_inode_operations
;
263 bytes_left
= h
->len
- sizeof(*h
);
265 while (bytes_left
>= (int)sizeof(*e
) && i
< EFI_SECRET_NUM_FILES
) {
266 e
= (struct secret_entry
*)ptr
;
267 if (e
->len
< sizeof(*e
) || e
->len
> (unsigned int)bytes_left
) {
268 dev_err(&dev
->dev
, "EFI secret area is corrupted\n");
273 /* Skip deleted entries (which will have NULL_GUID) */
274 if (efi_guidcmp(e
->guid
, NULL_GUID
)) {
275 efi_guid_to_str(&e
->guid
, guid_str
);
277 dent
= securityfs_create_file(guid_str
, 0440, s
->fs_dir
, (void *)e
,
278 &efi_secret_bin_file_fops
);
280 dev_err(&dev
->dev
, "Error creating efi_secret securityfs entry\n");
285 s
->fs_files
[i
++] = dent
;
288 bytes_left
-= e
->len
;
291 dev_info(&dev
->dev
, "Created %d entries in securityfs secrets/coco\n", i
);
295 efi_secret_securityfs_teardown(dev
);
299 static void efi_secret_unmap_area(void)
301 struct efi_secret
*s
= efi_secret_get();
303 if (s
->secret_data
) {
304 iounmap(s
->secret_data
);
305 s
->secret_data
= NULL
;
306 s
->secret_data_len
= 0;
310 static int efi_secret_probe(struct platform_device
*dev
)
314 ret
= efi_secret_map_area(dev
);
318 ret
= efi_secret_securityfs_setup(dev
);
325 efi_secret_unmap_area();
329 static void efi_secret_remove(struct platform_device
*dev
)
331 efi_secret_securityfs_teardown(dev
);
332 efi_secret_unmap_area();
335 static struct platform_driver efi_secret_driver
= {
336 .probe
= efi_secret_probe
,
337 .remove
= efi_secret_remove
,
339 .name
= "efi_secret",
343 module_platform_driver(efi_secret_driver
);
345 MODULE_DESCRIPTION("Confidential computing EFI secret area access");
346 MODULE_AUTHOR("IBM");
347 MODULE_LICENSE("GPL");
348 MODULE_ALIAS("platform:efi_secret");