|
| 1 | +// SPDX-License-Identifier: GPL-2.0 |
| 2 | +/* |
| 3 | + * Secure Launch late validation/setup, securityfs exposure and finalization. |
| 4 | + * |
| 5 | + * Copyright (c) 2025 Apertus Solutions, LLC |
| 6 | + * Copyright (c) 2025 Assured Information Security, Inc. |
| 7 | + * Copyright (c) 2025, Oracle and/or its affiliates. |
| 8 | + */ |
| 9 | + |
| 10 | +#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 11 | + |
| 12 | +#include <linux/fs.h> |
| 13 | +#include <linux/init.h> |
| 14 | +#include <linux/linkage.h> |
| 15 | +#include <linux/mm.h> |
| 16 | +#include <linux/io.h> |
| 17 | +#include <linux/uaccess.h> |
| 18 | +#include <linux/security.h> |
| 19 | +#include <linux/memblock.h> |
| 20 | +#include <linux/tpm.h> |
| 21 | +#include <asm/segment.h> |
| 22 | +#include <asm/sections.h> |
| 23 | +#include <crypto/sha2.h> |
| 24 | +#include <linux/slr_table.h> |
| 25 | +#include <linux/slaunch.h> |
| 26 | + |
| 27 | +/* |
| 28 | + * The macro DECLARE_TXT_PUB_READ_U is used to read values from the TXT |
| 29 | + * public registers as unsigned values. |
| 30 | + */ |
| 31 | +#define DECLARE_TXT_PUB_READ_U(size, fmt, msg_size) \ |
| 32 | +static ssize_t txt_pub_read_u##size(unsigned int offset, \ |
| 33 | + loff_t *read_offset, \ |
| 34 | + size_t read_len, \ |
| 35 | + char __user *buf) \ |
| 36 | +{ \ |
| 37 | + char msg_buffer[msg_size]; \ |
| 38 | + u##size reg_value = 0; \ |
| 39 | + void __iomem *txt; \ |
| 40 | + \ |
| 41 | + txt = ioremap(TXT_PUB_CONFIG_REGS_BASE, \ |
| 42 | + TXT_NR_CONFIG_PAGES * PAGE_SIZE); \ |
| 43 | + if (!txt) \ |
| 44 | + return -EFAULT; \ |
| 45 | + memcpy_fromio(®_value, txt + offset, sizeof(u##size)); \ |
| 46 | + iounmap(txt); \ |
| 47 | + snprintf(msg_buffer, msg_size, fmt, reg_value); \ |
| 48 | + return simple_read_from_buffer(buf, read_len, read_offset, \ |
| 49 | + &msg_buffer, msg_size); \ |
| 50 | +} |
| 51 | + |
| 52 | +DECLARE_TXT_PUB_READ_U(8, "%#04x\n", 6); |
| 53 | +DECLARE_TXT_PUB_READ_U(32, "%#010x\n", 12); |
| 54 | +DECLARE_TXT_PUB_READ_U(64, "%#018llx\n", 20); |
| 55 | + |
| 56 | +#define DECLARE_TXT_FOPS(reg_name, reg_offset, reg_size) \ |
| 57 | +static ssize_t txt_##reg_name##_read(struct file *flip, \ |
| 58 | + char __user *buf, size_t read_len, loff_t *read_offset) \ |
| 59 | +{ \ |
| 60 | + return txt_pub_read_u##reg_size(reg_offset, read_offset, \ |
| 61 | + read_len, buf); \ |
| 62 | +} \ |
| 63 | +static const struct file_operations reg_name##_ops = { \ |
| 64 | + .read = txt_##reg_name##_read, \ |
| 65 | +} |
| 66 | + |
| 67 | +DECLARE_TXT_FOPS(sts, TXT_CR_STS, 64); |
| 68 | +DECLARE_TXT_FOPS(ests, TXT_CR_ESTS, 8); |
| 69 | +DECLARE_TXT_FOPS(errorcode, TXT_CR_ERRORCODE, 32); |
| 70 | +DECLARE_TXT_FOPS(didvid, TXT_CR_DIDVID, 64); |
| 71 | +DECLARE_TXT_FOPS(e2sts, TXT_CR_E2STS, 64); |
| 72 | +DECLARE_TXT_FOPS(ver_emif, TXT_CR_VER_EMIF, 32); |
| 73 | +DECLARE_TXT_FOPS(scratchpad, TXT_CR_SCRATCHPAD, 64); |
| 74 | + |
| 75 | +/* |
| 76 | + * Securityfs exposure |
| 77 | + */ |
| 78 | +struct memfile { |
| 79 | + char *name; |
| 80 | + void *addr; |
| 81 | + size_t size; |
| 82 | +}; |
| 83 | + |
| 84 | +static struct memfile sl_evtlog = { "eventlog", NULL, 0 }; |
| 85 | +static void *txt_heap; |
| 86 | +static struct txt_heap_event_log_pointer2_1_element *evtlog21; |
| 87 | +static DEFINE_MUTEX(sl_evt_log_mutex); |
| 88 | +static struct tcg_efi_specid_event_head *efi_head; |
| 89 | + |
| 90 | +static ssize_t sl_evtlog_read(struct file *file, char __user *buf, |
| 91 | + size_t count, loff_t *pos) |
| 92 | +{ |
| 93 | + ssize_t size; |
| 94 | + |
| 95 | + if (!sl_evtlog.addr) |
| 96 | + return 0; |
| 97 | + |
| 98 | + mutex_lock(&sl_evt_log_mutex); |
| 99 | + size = simple_read_from_buffer(buf, count, pos, sl_evtlog.addr, |
| 100 | + sl_evtlog.size); |
| 101 | + mutex_unlock(&sl_evt_log_mutex); |
| 102 | + |
| 103 | + return size; |
| 104 | +} |
| 105 | + |
| 106 | +static ssize_t sl_evtlog_write(struct file *file, const char __user *buf, |
| 107 | + size_t datalen, loff_t *ppos) |
| 108 | +{ |
| 109 | + ssize_t result; |
| 110 | + char *data; |
| 111 | + |
| 112 | + if (!sl_evtlog.addr) |
| 113 | + return 0; |
| 114 | + |
| 115 | + /* No partial writes. */ |
| 116 | + result = -EINVAL; |
| 117 | + if (*ppos != 0) |
| 118 | + goto out; |
| 119 | + |
| 120 | + data = memdup_user(buf, datalen); |
| 121 | + if (IS_ERR(data)) { |
| 122 | + result = PTR_ERR(data); |
| 123 | + goto out; |
| 124 | + } |
| 125 | + |
| 126 | + mutex_lock(&sl_evt_log_mutex); |
| 127 | + if (evtlog21) |
| 128 | + result = tpm2_log_event(evtlog21, sl_evtlog.addr, |
| 129 | + sl_evtlog.size, datalen, data); |
| 130 | + else |
| 131 | + result = tpm_log_event(sl_evtlog.addr, sl_evtlog.size, |
| 132 | + datalen, data); |
| 133 | + mutex_unlock(&sl_evt_log_mutex); |
| 134 | + |
| 135 | + kfree(data); |
| 136 | +out: |
| 137 | + return result; |
| 138 | +} |
| 139 | + |
| 140 | +static const struct file_operations sl_evtlog_ops = { |
| 141 | + .read = sl_evtlog_read, |
| 142 | + .write = sl_evtlog_write, |
| 143 | + .llseek = default_llseek, |
| 144 | +}; |
| 145 | + |
| 146 | +struct sfs_file { |
| 147 | + const char *name; |
| 148 | + const struct file_operations *fops; |
| 149 | +}; |
| 150 | + |
| 151 | +#define SL_TXT_ENTRY_COUNT 7 |
| 152 | +static const struct sfs_file sl_txt_files[] = { |
| 153 | + { "sts", &sts_ops }, |
| 154 | + { "ests", &ests_ops }, |
| 155 | + { "errorcode", &errorcode_ops }, |
| 156 | + { "didvid", &didvid_ops }, |
| 157 | + { "ver_emif", &ver_emif_ops }, |
| 158 | + { "scratchpad", &scratchpad_ops }, |
| 159 | + { "e2sts", &e2sts_ops } |
| 160 | +}; |
| 161 | + |
| 162 | +/* sysfs file handles */ |
| 163 | +static struct dentry *slaunch_dir; |
| 164 | +static struct dentry *event_file; |
| 165 | +static struct dentry *txt_dir; |
| 166 | +static struct dentry *txt_entries[SL_TXT_ENTRY_COUNT]; |
| 167 | + |
| 168 | +static long slaunch_expose_securityfs(void) |
| 169 | +{ |
| 170 | + long ret = 0; |
| 171 | + int i; |
| 172 | + |
| 173 | + slaunch_dir = securityfs_create_dir("slaunch", NULL); |
| 174 | + if (IS_ERR(slaunch_dir)) |
| 175 | + return PTR_ERR(slaunch_dir); |
| 176 | + |
| 177 | + if (slaunch_get_flags() & SL_FLAG_ARCH_TXT) { |
| 178 | + txt_dir = securityfs_create_dir("txt", slaunch_dir); |
| 179 | + if (IS_ERR(txt_dir)) { |
| 180 | + ret = PTR_ERR(txt_dir); |
| 181 | + goto remove_slaunch; |
| 182 | + } |
| 183 | + |
| 184 | + for (i = 0; i < ARRAY_SIZE(sl_txt_files); i++) { |
| 185 | + txt_entries[i] = |
| 186 | + securityfs_create_file(sl_txt_files[i].name, 0440, txt_dir, |
| 187 | + NULL, sl_txt_files[i].fops); |
| 188 | + if (IS_ERR(txt_entries[i])) { |
| 189 | + ret = PTR_ERR(txt_entries[i]); |
| 190 | + goto remove_files; |
| 191 | + } |
| 192 | + } |
| 193 | + } |
| 194 | + |
| 195 | + if (sl_evtlog.addr) { |
| 196 | + event_file = securityfs_create_file(sl_evtlog.name, 0440, |
| 197 | + slaunch_dir, NULL, |
| 198 | + &sl_evtlog_ops); |
| 199 | + if (IS_ERR(event_file)) { |
| 200 | + ret = PTR_ERR(event_file); |
| 201 | + goto remove_files; |
| 202 | + } |
| 203 | + } |
| 204 | + |
| 205 | + return 0; |
| 206 | + |
| 207 | +remove_files: |
| 208 | + if (slaunch_get_flags() & SL_FLAG_ARCH_TXT) { |
| 209 | + while (--i >= 0) |
| 210 | + securityfs_remove(txt_entries[i]); |
| 211 | + securityfs_remove(txt_dir); |
| 212 | + } |
| 213 | + |
| 214 | +remove_slaunch: |
| 215 | + securityfs_remove(slaunch_dir); |
| 216 | + |
| 217 | + return ret; |
| 218 | +} |
| 219 | + |
| 220 | +static void slaunch_teardown_securityfs(void) |
| 221 | +{ |
| 222 | + int i; |
| 223 | + |
| 224 | + securityfs_remove(event_file); |
| 225 | + if (sl_evtlog.addr) { |
| 226 | + memunmap(sl_evtlog.addr); |
| 227 | + sl_evtlog.addr = NULL; |
| 228 | + } |
| 229 | + sl_evtlog.size = 0; |
| 230 | + |
| 231 | + if (slaunch_get_flags() & SL_FLAG_ARCH_TXT) { |
| 232 | + for (i = 0; i < ARRAY_SIZE(sl_txt_files); i++) |
| 233 | + securityfs_remove(txt_entries[i]); |
| 234 | + |
| 235 | + securityfs_remove(txt_dir); |
| 236 | + |
| 237 | + if (txt_heap) { |
| 238 | + memunmap(txt_heap); |
| 239 | + txt_heap = NULL; |
| 240 | + } |
| 241 | + } |
| 242 | + |
| 243 | + securityfs_remove(slaunch_dir); |
| 244 | +} |
| 245 | + |
| 246 | +static void slaunch_intel_evtlog(void __iomem *txt) |
| 247 | +{ |
| 248 | + struct slr_entry_log_info *log_info; |
| 249 | + struct txt_os_mle_data *params; |
| 250 | + struct slr_table *slrt; |
| 251 | + void *os_sinit_data; |
| 252 | + u64 base, size; |
| 253 | + |
| 254 | + memcpy_fromio(&base, txt + TXT_CR_HEAP_BASE, sizeof(base)); |
| 255 | + memcpy_fromio(&size, txt + TXT_CR_HEAP_SIZE, sizeof(size)); |
| 256 | + |
| 257 | + /* now map TXT heap */ |
| 258 | + txt_heap = memremap(base, size, MEMREMAP_WB); |
| 259 | + if (!txt_heap) |
| 260 | + slaunch_reset(txt, "Error failed to memremap TXT heap\n", SL_ERROR_HEAP_MAP); |
| 261 | + |
| 262 | + params = (struct txt_os_mle_data *)txt_os_mle_data_start(txt_heap); |
| 263 | + |
| 264 | + /* Get the SLRT and remap it */ |
| 265 | + slrt = memremap(params->slrt, sizeof(*slrt), MEMREMAP_WB); |
| 266 | + if (!slrt) |
| 267 | + slaunch_reset(txt, "Error failed to memremap SLR Table\n", SL_ERROR_SLRT_MAP); |
| 268 | + size = slrt->size; |
| 269 | + memunmap(slrt); |
| 270 | + |
| 271 | + slrt = memremap(params->slrt, size, MEMREMAP_WB); |
| 272 | + if (!slrt) |
| 273 | + slaunch_reset(txt, "Error failed to memremap SLR Table\n", SL_ERROR_SLRT_MAP); |
| 274 | + |
| 275 | + log_info = slr_next_entry_by_tag(slrt, NULL, SLR_ENTRY_LOG_INFO); |
| 276 | + if (!log_info) |
| 277 | + slaunch_reset(txt, "Error failed to memremap SLR Table\n", SL_ERROR_SLRT_MISSING_ENTRY); |
| 278 | + |
| 279 | + sl_evtlog.size = log_info->size; |
| 280 | + sl_evtlog.addr = memremap(log_info->addr, log_info->size, MEMREMAP_WB); |
| 281 | + if (!sl_evtlog.addr) |
| 282 | + slaunch_reset(txt, "Error failed to memremap TPM event log\n", SL_ERROR_EVENTLOG_MAP); |
| 283 | + |
| 284 | + memunmap(slrt); |
| 285 | + |
| 286 | + /* Determine if this is TPM 1.2 or 2.0 event log */ |
| 287 | + if (memcmp(sl_evtlog.addr + sizeof(struct tcg_pcr_event), TCG_SPECID_SIG, sizeof(TCG_SPECID_SIG))) |
| 288 | + return; /* looks like it is not 2.0 */ |
| 289 | + |
| 290 | + /* For TPM 2.0 logs, the extended heap element must be located */ |
| 291 | + os_sinit_data = txt_os_sinit_data_start(txt_heap); |
| 292 | + |
| 293 | + evtlog21 = txt_find_log2_1_element(os_sinit_data); |
| 294 | + |
| 295 | + /* |
| 296 | + * If this fails, things are in really bad shape. Any attempt to write |
| 297 | + * events to the log will fail. |
| 298 | + */ |
| 299 | + if (!evtlog21) |
| 300 | + slaunch_reset(txt, "Error failed to find TPM20 event log element\n", SL_ERROR_TPM_INVALID_LOG20); |
| 301 | + |
| 302 | + /* Save pointer to the EFI SpecID log header */ |
| 303 | + efi_head = (struct tcg_efi_specid_event_head *)(sl_evtlog.addr + sizeof(struct tcg_pcr_event)); |
| 304 | +} |
| 305 | + |
| 306 | +static void slaunch_tpm_open_locality2(void __iomem *txt) |
| 307 | +{ |
| 308 | + struct tpm_chip *tpm; |
| 309 | + int rc; |
| 310 | + |
| 311 | + tpm = tpm_default_chip(); |
| 312 | + if (!tpm) |
| 313 | + slaunch_reset(txt, "Could not get default TPM chip\n", SL_ERROR_TPM_INIT); |
| 314 | + |
| 315 | + rc = tpm_chip_set_locality(tpm, 2); |
| 316 | + if (rc) |
| 317 | + slaunch_reset(txt, "Could not set TPM chip locality 2\n", SL_ERROR_TPM_INIT); |
| 318 | +} |
| 319 | + |
| 320 | +static int __init slaunch_module_init(void) |
| 321 | +{ |
| 322 | + void __iomem *txt; |
| 323 | + |
| 324 | + /* Check to see if Secure Launch happened */ |
| 325 | + if ((slaunch_get_flags() & (SL_FLAG_ACTIVE|SL_FLAG_ARCH_TXT)) != |
| 326 | + (SL_FLAG_ACTIVE | SL_FLAG_ARCH_TXT)) |
| 327 | + return 0; |
| 328 | + |
| 329 | + txt = ioremap(TXT_PRIV_CONFIG_REGS_BASE, TXT_NR_CONFIG_PAGES * |
| 330 | + PAGE_SIZE); |
| 331 | + if (!txt) |
| 332 | + panic("Error ioremap of TXT priv registers\n"); |
| 333 | + |
| 334 | + /* Only Intel TXT is supported at this point */ |
| 335 | + slaunch_intel_evtlog(txt); |
| 336 | + slaunch_tpm_open_locality2(txt); |
| 337 | + iounmap(txt); |
| 338 | + |
| 339 | + return slaunch_expose_securityfs(); |
| 340 | +} |
| 341 | + |
| 342 | +static void __exit slaunch_module_exit(void) |
| 343 | +{ |
| 344 | + slaunch_teardown_securityfs(); |
| 345 | +} |
| 346 | + |
| 347 | +late_initcall(slaunch_module_init); |
| 348 | +__exitcall(slaunch_module_exit); |
0 commit comments