blob: e7228863290edea53508fd597ff6855b98c1440c [file] [log] [blame]
Kylene Jo Hall55a82ab2006-01-08 01:03:15 -08001/*
Kent Yodere5dcd872012-07-11 10:08:12 -05002 * Copyright (C) 2005, 2012 IBM Corporation
Kylene Jo Hall55a82ab2006-01-08 01:03:15 -08003 *
4 * Authors:
Kent Yodere5dcd872012-07-11 10:08:12 -05005 * Kent Yoder <key@linux.vnet.ibm.com>
Kylene Jo Hall55a82ab2006-01-08 01:03:15 -08006 * Seiji Munetoh <munetoh@jp.ibm.com>
7 * Stefan Berger <stefanb@us.ibm.com>
8 * Reiner Sailer <sailer@watson.ibm.com>
9 * Kylene Hall <kjhall@us.ibm.com>
10 *
Kent Yoder8e81cc12007-08-22 14:01:04 -070011 * Maintained by: <tpmdd-devel@lists.sourceforge.net>
12 *
Kent Yodere5dcd872012-07-11 10:08:12 -050013 * Access to the eventlog created by a system's firmware / BIOS
Kylene Jo Hall55a82ab2006-01-08 01:03:15 -080014 *
15 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License
17 * as published by the Free Software Foundation; either version
18 * 2 of the License, or (at your option) any later version.
19 *
20 */
21
22#include <linux/seq_file.h>
23#include <linux/fs.h>
24#include <linux/security.h>
25#include <linux/module.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090026#include <linux/slab.h>
Kent Yodere5dcd872012-07-11 10:08:12 -050027
Kylene Jo Hall55a82ab2006-01-08 01:03:15 -080028#include "tpm.h"
Kent Yodere5dcd872012-07-11 10:08:12 -050029#include "tpm_eventlog.h"
Kylene Jo Hall55a82ab2006-01-08 01:03:15 -080030
Kylene Jo Hall55a82ab2006-01-08 01:03:15 -080031
32static const char* tcpa_event_type_strings[] = {
33 "PREBOOT",
34 "POST CODE",
35 "",
36 "NO ACTION",
37 "SEPARATOR",
38 "ACTION",
39 "EVENT TAG",
40 "S-CRTM Contents",
41 "S-CRTM Version",
42 "CPU Microcode",
43 "Platform Config Flags",
44 "Table of Devices",
45 "Compact Hash",
46 "IPL",
47 "IPL Partition Data",
48 "Non-Host Code",
49 "Non-Host Config",
50 "Non-Host Info"
51};
52
Kylene Jo Hall55a82ab2006-01-08 01:03:15 -080053static const char* tcpa_pc_event_id_strings[] = {
Seiji Munetohde66a692006-05-30 21:25:47 -070054 "",
Kylene Jo Hall55a82ab2006-01-08 01:03:15 -080055 "SMBIOS",
56 "BIS Certificate",
57 "POST BIOS ",
58 "ESCD ",
59 "CMOS",
60 "NVRAM",
61 "Option ROM",
62 "Option ROM config",
Seiji Munetohde66a692006-05-30 21:25:47 -070063 "",
64 "Option ROM microcode ",
Kylene Jo Hall55a82ab2006-01-08 01:03:15 -080065 "S-CRTM Version",
Seiji Munetohde66a692006-05-30 21:25:47 -070066 "S-CRTM Contents ",
67 "POST Contents ",
68 "Table of Devices",
Kylene Jo Hall55a82ab2006-01-08 01:03:15 -080069};
70
Kylene Jo Hall55a82ab2006-01-08 01:03:15 -080071/* returns pointer to start of pos. entry of tcg log */
72static void *tpm_bios_measurements_start(struct seq_file *m, loff_t *pos)
73{
74 loff_t i;
Kylene Jo Halld09cf7d2006-01-08 01:03:48 -080075 struct tpm_bios_log *log = m->private;
76 void *addr = log->bios_event_log;
77 void *limit = log->bios_event_log_end;
Kylene Jo Hall55a82ab2006-01-08 01:03:15 -080078 struct tcpa_event *event;
Hon Ching \(Vicky\) Lo0cc698a2015-06-17 18:17:08 -040079 u32 converted_event_size;
80 u32 converted_event_type;
81
Kylene Jo Hall55a82ab2006-01-08 01:03:15 -080082
83 /* read over *pos measurements */
84 for (i = 0; i < *pos; i++) {
85 event = addr;
86
Hon Ching \(Vicky\) Lo0cc698a2015-06-17 18:17:08 -040087 converted_event_size =
88 do_endian_conversion(event->event_size);
89 converted_event_type =
90 do_endian_conversion(event->event_type);
91
Kylene Jo Halld09cf7d2006-01-08 01:03:48 -080092 if ((addr + sizeof(struct tcpa_event)) < limit) {
Hon Ching \(Vicky\) Lo0cc698a2015-06-17 18:17:08 -040093 if ((converted_event_type == 0) &&
94 (converted_event_size == 0))
Kylene Jo Hall55a82ab2006-01-08 01:03:15 -080095 return NULL;
Hon Ching \(Vicky\) Lo0cc698a2015-06-17 18:17:08 -040096 addr += (sizeof(struct tcpa_event) +
97 converted_event_size);
Kylene Jo Hall55a82ab2006-01-08 01:03:15 -080098 }
99 }
100
101 /* now check if current entry is valid */
Kylene Jo Halld09cf7d2006-01-08 01:03:48 -0800102 if ((addr + sizeof(struct tcpa_event)) >= limit)
Kylene Jo Hall55a82ab2006-01-08 01:03:15 -0800103 return NULL;
104
105 event = addr;
106
Hon Ching \(Vicky\) Lo0cc698a2015-06-17 18:17:08 -0400107 converted_event_size = do_endian_conversion(event->event_size);
108 converted_event_type = do_endian_conversion(event->event_type);
109
110 if (((converted_event_type == 0) && (converted_event_size == 0))
111 || ((addr + sizeof(struct tcpa_event) + converted_event_size)
112 >= limit))
Kylene Jo Hall55a82ab2006-01-08 01:03:15 -0800113 return NULL;
114
115 return addr;
116}
117
118static void *tpm_bios_measurements_next(struct seq_file *m, void *v,
119 loff_t *pos)
120{
121 struct tcpa_event *event = v;
Kylene Jo Halld09cf7d2006-01-08 01:03:48 -0800122 struct tpm_bios_log *log = m->private;
123 void *limit = log->bios_event_log_end;
Hon Ching \(Vicky\) Lo0cc698a2015-06-17 18:17:08 -0400124 u32 converted_event_size;
125 u32 converted_event_type;
Kylene Jo Hall55a82ab2006-01-08 01:03:15 -0800126
Hon Ching \(Vicky\) Lo0cc698a2015-06-17 18:17:08 -0400127 converted_event_size = do_endian_conversion(event->event_size);
128
129 v += sizeof(struct tcpa_event) + converted_event_size;
Kylene Jo Hall55a82ab2006-01-08 01:03:15 -0800130
131 /* now check if current entry is valid */
Kylene Jo Halld09cf7d2006-01-08 01:03:48 -0800132 if ((v + sizeof(struct tcpa_event)) >= limit)
Kylene Jo Hall55a82ab2006-01-08 01:03:15 -0800133 return NULL;
134
135 event = v;
136
Hon Ching \(Vicky\) Lo0cc698a2015-06-17 18:17:08 -0400137 converted_event_size = do_endian_conversion(event->event_size);
138 converted_event_type = do_endian_conversion(event->event_type);
Kylene Jo Hall55a82ab2006-01-08 01:03:15 -0800139
Hon Ching \(Vicky\) Lo0cc698a2015-06-17 18:17:08 -0400140 if (((converted_event_type == 0) && (converted_event_size == 0)) ||
141 ((v + sizeof(struct tcpa_event) + converted_event_size) >= limit))
Kylene Jo Hall55a82ab2006-01-08 01:03:15 -0800142 return NULL;
143
144 (*pos)++;
145 return v;
146}
147
148static void tpm_bios_measurements_stop(struct seq_file *m, void *v)
149{
150}
151
152static int get_event_name(char *dest, struct tcpa_event *event,
153 unsigned char * event_entry)
154{
155 const char *name = "";
Eric Parisfbaa5862009-05-13 12:50:40 -0400156 /* 41 so there is room for 40 data and 1 nul */
157 char data[41] = "";
Kylene Jo Hall55a82ab2006-01-08 01:03:15 -0800158 int i, n_len = 0, d_len = 0;
Seiji Munetohde66a692006-05-30 21:25:47 -0700159 struct tcpa_pc_event *pc_event;
Kylene Jo Hall55a82ab2006-01-08 01:03:15 -0800160
Hon Ching \(Vicky\) Lo0cc698a2015-06-17 18:17:08 -0400161 switch (do_endian_conversion(event->event_type)) {
Kylene Jo Hall55a82ab2006-01-08 01:03:15 -0800162 case PREBOOT:
163 case POST_CODE:
164 case UNUSED:
165 case NO_ACTION:
166 case SCRTM_CONTENTS:
167 case SCRTM_VERSION:
168 case CPU_MICROCODE:
169 case PLATFORM_CONFIG_FLAGS:
170 case TABLE_OF_DEVICES:
171 case COMPACT_HASH:
172 case IPL:
173 case IPL_PARTITION_DATA:
174 case NONHOST_CODE:
175 case NONHOST_CONFIG:
176 case NONHOST_INFO:
Hon Ching \(Vicky\) Lo0cc698a2015-06-17 18:17:08 -0400177 name = tcpa_event_type_strings[do_endian_conversion
178 (event->event_type)];
Kylene Jo Hall55a82ab2006-01-08 01:03:15 -0800179 n_len = strlen(name);
180 break;
181 case SEPARATOR:
182 case ACTION:
Hon Ching \(Vicky\) Lo0cc698a2015-06-17 18:17:08 -0400183 if (MAX_TEXT_EVENT >
184 do_endian_conversion(event->event_size)) {
Kylene Jo Hall55a82ab2006-01-08 01:03:15 -0800185 name = event_entry;
Hon Ching \(Vicky\) Lo0cc698a2015-06-17 18:17:08 -0400186 n_len = do_endian_conversion(event->event_size);
Kylene Jo Hall55a82ab2006-01-08 01:03:15 -0800187 }
188 break;
189 case EVENT_TAG:
Seiji Munetohde66a692006-05-30 21:25:47 -0700190 pc_event = (struct tcpa_pc_event *)event_entry;
Kylene Jo Hall55a82ab2006-01-08 01:03:15 -0800191
192 /* ToDo Row data -> Base64 */
193
Hon Ching \(Vicky\) Lo0cc698a2015-06-17 18:17:08 -0400194 switch (do_endian_conversion(pc_event->event_id)) {
Kylene Jo Hall55a82ab2006-01-08 01:03:15 -0800195 case SMBIOS:
196 case BIS_CERT:
197 case CMOS:
198 case NVRAM:
199 case OPTION_ROM_EXEC:
200 case OPTION_ROM_CONFIG:
Kylene Jo Hall55a82ab2006-01-08 01:03:15 -0800201 case S_CRTM_VERSION:
Hon Ching \(Vicky\) Lo0cc698a2015-06-17 18:17:08 -0400202 name = tcpa_pc_event_id_strings[do_endian_conversion
203 (pc_event->event_id)];
Kylene Jo Hall55a82ab2006-01-08 01:03:15 -0800204 n_len = strlen(name);
205 break;
Seiji Munetohde66a692006-05-30 21:25:47 -0700206 /* hash data */
Kylene Jo Hall55a82ab2006-01-08 01:03:15 -0800207 case POST_BIOS_ROM:
208 case ESCD:
Seiji Munetohde66a692006-05-30 21:25:47 -0700209 case OPTION_ROM_MICROCODE:
210 case S_CRTM_CONTENTS:
211 case POST_CONTENTS:
Hon Ching \(Vicky\) Lo0cc698a2015-06-17 18:17:08 -0400212 name = tcpa_pc_event_id_strings[do_endian_conversion
213 (pc_event->event_id)];
Kylene Jo Hall55a82ab2006-01-08 01:03:15 -0800214 n_len = strlen(name);
215 for (i = 0; i < 20; i++)
Seiji Munetohde66a692006-05-30 21:25:47 -0700216 d_len += sprintf(&data[2*i], "%02x",
217 pc_event->event_data[i]);
Kylene Jo Hall55a82ab2006-01-08 01:03:15 -0800218 break;
219 default:
220 break;
221 }
222 default:
223 break;
224 }
225
226 return snprintf(dest, MAX_TEXT_EVENT, "[%.*s%.*s]",
227 n_len, name, d_len, data);
228
229}
230
231static int tpm_binary_bios_measurements_show(struct seq_file *m, void *v)
232{
Seiji Munetoh44d7aff2006-05-30 21:25:52 -0700233 struct tcpa_event *event = v;
Hon Ching \(Vicky\) Lo0cc698a2015-06-17 18:17:08 -0400234 struct tcpa_event temp_event;
Harald Hoyer186d1242016-02-06 15:44:42 +0100235 char *temp_ptr;
Seiji Munetoh44d7aff2006-05-30 21:25:52 -0700236 int i;
Kylene Jo Hall55a82ab2006-01-08 01:03:15 -0800237
Hon Ching \(Vicky\) Lo0cc698a2015-06-17 18:17:08 -0400238 memcpy(&temp_event, event, sizeof(struct tcpa_event));
239
240 /* convert raw integers for endianness */
241 temp_event.pcr_index = do_endian_conversion(event->pcr_index);
242 temp_event.event_type = do_endian_conversion(event->event_type);
243 temp_event.event_size = do_endian_conversion(event->event_size);
244
Harald Hoyer186d1242016-02-06 15:44:42 +0100245 temp_ptr = (char *) &temp_event;
Hon Ching \(Vicky\) Lo0cc698a2015-06-17 18:17:08 -0400246
Harald Hoyer186d1242016-02-06 15:44:42 +0100247 for (i = 0; i < (sizeof(struct tcpa_event) - 1) ; i++)
248 seq_putc(m, temp_ptr[i]);
249
250 temp_ptr = (char *) v;
251
252 for (i = (sizeof(struct tcpa_event) - 1);
253 i < (sizeof(struct tcpa_event) + temp_event.event_size); i++)
254 seq_putc(m, temp_ptr[i]);
Kylene Jo Hall55a82ab2006-01-08 01:03:15 -0800255
Kylene Jo Hall55a82ab2006-01-08 01:03:15 -0800256 return 0;
Hon Ching \(Vicky\) Lo0cc698a2015-06-17 18:17:08 -0400257
Kylene Jo Hall55a82ab2006-01-08 01:03:15 -0800258}
259
260static int tpm_bios_measurements_release(struct inode *inode,
261 struct file *file)
262{
Kylene Jo Halld09cf7d2006-01-08 01:03:48 -0800263 struct seq_file *seq = file->private_data;
264 struct tpm_bios_log *log = seq->private;
265
266 if (log) {
267 kfree(log->bios_event_log);
268 kfree(log);
Kylene Jo Hall55a82ab2006-01-08 01:03:15 -0800269 }
Kylene Jo Halld09cf7d2006-01-08 01:03:48 -0800270
Kylene Jo Hall55a82ab2006-01-08 01:03:15 -0800271 return seq_release(inode, file);
272}
273
274static int tpm_ascii_bios_measurements_show(struct seq_file *m, void *v)
275{
276 int len = 0;
Kylene Jo Hall55a82ab2006-01-08 01:03:15 -0800277 char *eventname;
278 struct tcpa_event *event = v;
279 unsigned char *event_entry =
Hon Ching \(Vicky\) Lo0cc698a2015-06-17 18:17:08 -0400280 (unsigned char *)(v + sizeof(struct tcpa_event));
Kylene Jo Hall55a82ab2006-01-08 01:03:15 -0800281
282 eventname = kmalloc(MAX_TEXT_EVENT, GFP_KERNEL);
283 if (!eventname) {
284 printk(KERN_ERR "%s: ERROR - No Memory for event name\n ",
285 __func__);
286 return -EFAULT;
287 }
288
Hon Ching \(Vicky\) Lo0cc698a2015-06-17 18:17:08 -0400289 /* 1st: PCR */
290 seq_printf(m, "%2d ", do_endian_conversion(event->pcr_index));
Kylene Jo Hall55a82ab2006-01-08 01:03:15 -0800291
292 /* 2nd: SHA1 */
Andy Shevchenkoa3d64df2014-07-09 15:34:41 +0300293 seq_printf(m, "%20phN", event->pcr_value);
Kylene Jo Hall55a82ab2006-01-08 01:03:15 -0800294
295 /* 3rd: event type identifier */
Hon Ching \(Vicky\) Lo0cc698a2015-06-17 18:17:08 -0400296 seq_printf(m, " %02x", do_endian_conversion(event->event_type));
Kylene Jo Hall55a82ab2006-01-08 01:03:15 -0800297
298 len += get_event_name(eventname, event, event_entry);
299
300 /* 4th: eventname <= max + \'0' delimiter */
301 seq_printf(m, " %s\n", eventname);
302
Kylene Jo Hall59e89f32006-04-22 02:36:35 -0700303 kfree(eventname);
Kylene Jo Hall55a82ab2006-01-08 01:03:15 -0800304 return 0;
305}
306
James Morris88e9d342009-09-22 16:43:43 -0700307static const struct seq_operations tpm_ascii_b_measurments_seqops = {
Kylene Jo Hall55a82ab2006-01-08 01:03:15 -0800308 .start = tpm_bios_measurements_start,
309 .next = tpm_bios_measurements_next,
310 .stop = tpm_bios_measurements_stop,
311 .show = tpm_ascii_bios_measurements_show,
312};
313
James Morris88e9d342009-09-22 16:43:43 -0700314static const struct seq_operations tpm_binary_b_measurments_seqops = {
Kylene Jo Hall55a82ab2006-01-08 01:03:15 -0800315 .start = tpm_bios_measurements_start,
316 .next = tpm_bios_measurements_next,
317 .stop = tpm_bios_measurements_stop,
318 .show = tpm_binary_bios_measurements_show,
319};
320
Kylene Jo Hall55a82ab2006-01-08 01:03:15 -0800321static int tpm_ascii_bios_measurements_open(struct inode *inode,
322 struct file *file)
323{
324 int err;
Kylene Jo Halld09cf7d2006-01-08 01:03:48 -0800325 struct tpm_bios_log *log;
326 struct seq_file *seq;
Kylene Jo Hall55a82ab2006-01-08 01:03:15 -0800327
Kylene Jo Halld09cf7d2006-01-08 01:03:48 -0800328 log = kzalloc(sizeof(struct tpm_bios_log), GFP_KERNEL);
329 if (!log)
330 return -ENOMEM;
331
332 if ((err = read_log(log)))
Jesper Juhl178554a2007-07-20 00:31:48 -0700333 goto out_free;
Kylene Jo Hall55a82ab2006-01-08 01:03:15 -0800334
335 /* now register seq file */
Kylene Jo Halld09cf7d2006-01-08 01:03:48 -0800336 err = seq_open(file, &tpm_ascii_b_measurments_seqops);
337 if (!err) {
338 seq = file->private_data;
339 seq->private = log;
340 } else {
Jesper Juhl178554a2007-07-20 00:31:48 -0700341 goto out_free;
Kylene Jo Halld09cf7d2006-01-08 01:03:48 -0800342 }
Jesper Juhl178554a2007-07-20 00:31:48 -0700343
344out:
Kylene Jo Halld09cf7d2006-01-08 01:03:48 -0800345 return err;
Jesper Juhl178554a2007-07-20 00:31:48 -0700346out_free:
347 kfree(log->bios_event_log);
348 kfree(log);
349 goto out;
Kylene Jo Hall55a82ab2006-01-08 01:03:15 -0800350}
351
Adrian Bunk3bd60462008-07-25 19:44:58 -0700352static const struct file_operations tpm_ascii_bios_measurements_ops = {
Kylene Jo Hall55a82ab2006-01-08 01:03:15 -0800353 .open = tpm_ascii_bios_measurements_open,
354 .read = seq_read,
355 .llseek = seq_lseek,
356 .release = tpm_bios_measurements_release,
357};
358
359static int tpm_binary_bios_measurements_open(struct inode *inode,
360 struct file *file)
361{
362 int err;
Kylene Jo Halld09cf7d2006-01-08 01:03:48 -0800363 struct tpm_bios_log *log;
364 struct seq_file *seq;
Kylene Jo Hall55a82ab2006-01-08 01:03:15 -0800365
Kylene Jo Halld09cf7d2006-01-08 01:03:48 -0800366 log = kzalloc(sizeof(struct tpm_bios_log), GFP_KERNEL);
367 if (!log)
368 return -ENOMEM;
369
370 if ((err = read_log(log)))
Reiner Sailer03ce1102007-07-20 10:03:02 -0400371 goto out_free;
Kylene Jo Hall55a82ab2006-01-08 01:03:15 -0800372
373 /* now register seq file */
Kylene Jo Halld09cf7d2006-01-08 01:03:48 -0800374 err = seq_open(file, &tpm_binary_b_measurments_seqops);
375 if (!err) {
376 seq = file->private_data;
377 seq->private = log;
378 } else {
Reiner Sailer03ce1102007-07-20 10:03:02 -0400379 goto out_free;
Kylene Jo Halld09cf7d2006-01-08 01:03:48 -0800380 }
Reiner Sailer03ce1102007-07-20 10:03:02 -0400381
382out:
Kylene Jo Halld09cf7d2006-01-08 01:03:48 -0800383 return err;
Reiner Sailer03ce1102007-07-20 10:03:02 -0400384out_free:
385 kfree(log->bios_event_log);
386 kfree(log);
387 goto out;
Kylene Jo Hall55a82ab2006-01-08 01:03:15 -0800388}
389
Adrian Bunk3bd60462008-07-25 19:44:58 -0700390static const struct file_operations tpm_binary_bios_measurements_ops = {
Kylene Jo Hall55a82ab2006-01-08 01:03:15 -0800391 .open = tpm_binary_bios_measurements_open,
392 .read = seq_read,
393 .llseek = seq_lseek,
394 .release = tpm_bios_measurements_release,
395};
396
Andrew Mortonca4a031f2006-02-01 03:05:01 -0800397static int is_bad(void *p)
398{
399 if (!p)
400 return 1;
401 if (IS_ERR(p) && (PTR_ERR(p) != -ENODEV))
402 return 1;
403 return 0;
404}
405
Jason Gunthorpe3635e2e2016-02-29 12:29:48 -0500406struct dentry **tpm_bios_log_setup(const char *name)
Kylene Jo Hall55a82ab2006-01-08 01:03:15 -0800407{
408 struct dentry **ret = NULL, *tpm_dir, *bin_file, *ascii_file;
409
410 tpm_dir = securityfs_create_dir(name, NULL);
Andrew Mortonca4a031f2006-02-01 03:05:01 -0800411 if (is_bad(tpm_dir))
Kylene Jo Hall55a82ab2006-01-08 01:03:15 -0800412 goto out;
413
414 bin_file =
415 securityfs_create_file("binary_bios_measurements",
416 S_IRUSR | S_IRGRP, tpm_dir, NULL,
417 &tpm_binary_bios_measurements_ops);
Andrew Mortonca4a031f2006-02-01 03:05:01 -0800418 if (is_bad(bin_file))
Kylene Jo Hall55a82ab2006-01-08 01:03:15 -0800419 goto out_tpm;
420
421 ascii_file =
422 securityfs_create_file("ascii_bios_measurements",
423 S_IRUSR | S_IRGRP, tpm_dir, NULL,
424 &tpm_ascii_bios_measurements_ops);
Andrew Mortonca4a031f2006-02-01 03:05:01 -0800425 if (is_bad(ascii_file))
Kylene Jo Hall55a82ab2006-01-08 01:03:15 -0800426 goto out_bin;
427
428 ret = kmalloc(3 * sizeof(struct dentry *), GFP_KERNEL);
429 if (!ret)
430 goto out_ascii;
431
432 ret[0] = ascii_file;
433 ret[1] = bin_file;
434 ret[2] = tpm_dir;
435
436 return ret;
437
438out_ascii:
439 securityfs_remove(ascii_file);
440out_bin:
441 securityfs_remove(bin_file);
442out_tpm:
443 securityfs_remove(tpm_dir);
444out:
445 return NULL;
446}
Kylene Jo Hall55a82ab2006-01-08 01:03:15 -0800447
448void tpm_bios_log_teardown(struct dentry **lst)
449{
450 int i;
451
452 for (i = 0; i < 3; i++)
453 securityfs_remove(lst[i]);
454}