blob: bd72fb04225e43f8ae21acba1cc5ddb351701c5d [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;
235 char *tempPtr;
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
245 tempPtr = (char *)&temp_event;
246
247 for (i = 0; i < sizeof(struct tcpa_event) + temp_event.event_size; i++)
248 seq_putc(m, tempPtr[i]);
Kylene Jo Hall55a82ab2006-01-08 01:03:15 -0800249
Kylene Jo Hall55a82ab2006-01-08 01:03:15 -0800250 return 0;
Hon Ching \(Vicky\) Lo0cc698a2015-06-17 18:17:08 -0400251
Kylene Jo Hall55a82ab2006-01-08 01:03:15 -0800252}
253
254static int tpm_bios_measurements_release(struct inode *inode,
255 struct file *file)
256{
Kylene Jo Halld09cf7d2006-01-08 01:03:48 -0800257 struct seq_file *seq = file->private_data;
258 struct tpm_bios_log *log = seq->private;
259
260 if (log) {
261 kfree(log->bios_event_log);
262 kfree(log);
Kylene Jo Hall55a82ab2006-01-08 01:03:15 -0800263 }
Kylene Jo Halld09cf7d2006-01-08 01:03:48 -0800264
Kylene Jo Hall55a82ab2006-01-08 01:03:15 -0800265 return seq_release(inode, file);
266}
267
268static int tpm_ascii_bios_measurements_show(struct seq_file *m, void *v)
269{
270 int len = 0;
Kylene Jo Hall55a82ab2006-01-08 01:03:15 -0800271 char *eventname;
272 struct tcpa_event *event = v;
273 unsigned char *event_entry =
Hon Ching \(Vicky\) Lo0cc698a2015-06-17 18:17:08 -0400274 (unsigned char *)(v + sizeof(struct tcpa_event));
Kylene Jo Hall55a82ab2006-01-08 01:03:15 -0800275
276 eventname = kmalloc(MAX_TEXT_EVENT, GFP_KERNEL);
277 if (!eventname) {
278 printk(KERN_ERR "%s: ERROR - No Memory for event name\n ",
279 __func__);
280 return -EFAULT;
281 }
282
Hon Ching \(Vicky\) Lo0cc698a2015-06-17 18:17:08 -0400283 /* 1st: PCR */
284 seq_printf(m, "%2d ", do_endian_conversion(event->pcr_index));
Kylene Jo Hall55a82ab2006-01-08 01:03:15 -0800285
286 /* 2nd: SHA1 */
Andy Shevchenkoa3d64df2014-07-09 15:34:41 +0300287 seq_printf(m, "%20phN", event->pcr_value);
Kylene Jo Hall55a82ab2006-01-08 01:03:15 -0800288
289 /* 3rd: event type identifier */
Hon Ching \(Vicky\) Lo0cc698a2015-06-17 18:17:08 -0400290 seq_printf(m, " %02x", do_endian_conversion(event->event_type));
Kylene Jo Hall55a82ab2006-01-08 01:03:15 -0800291
292 len += get_event_name(eventname, event, event_entry);
293
294 /* 4th: eventname <= max + \'0' delimiter */
295 seq_printf(m, " %s\n", eventname);
296
Kylene Jo Hall59e89f32006-04-22 02:36:35 -0700297 kfree(eventname);
Kylene Jo Hall55a82ab2006-01-08 01:03:15 -0800298 return 0;
299}
300
James Morris88e9d342009-09-22 16:43:43 -0700301static const struct seq_operations tpm_ascii_b_measurments_seqops = {
Kylene Jo Hall55a82ab2006-01-08 01:03:15 -0800302 .start = tpm_bios_measurements_start,
303 .next = tpm_bios_measurements_next,
304 .stop = tpm_bios_measurements_stop,
305 .show = tpm_ascii_bios_measurements_show,
306};
307
James Morris88e9d342009-09-22 16:43:43 -0700308static const struct seq_operations tpm_binary_b_measurments_seqops = {
Kylene Jo Hall55a82ab2006-01-08 01:03:15 -0800309 .start = tpm_bios_measurements_start,
310 .next = tpm_bios_measurements_next,
311 .stop = tpm_bios_measurements_stop,
312 .show = tpm_binary_bios_measurements_show,
313};
314
Kylene Jo Hall55a82ab2006-01-08 01:03:15 -0800315static int tpm_ascii_bios_measurements_open(struct inode *inode,
316 struct file *file)
317{
318 int err;
Kylene Jo Halld09cf7d2006-01-08 01:03:48 -0800319 struct tpm_bios_log *log;
320 struct seq_file *seq;
Kylene Jo Hall55a82ab2006-01-08 01:03:15 -0800321
Kylene Jo Halld09cf7d2006-01-08 01:03:48 -0800322 log = kzalloc(sizeof(struct tpm_bios_log), GFP_KERNEL);
323 if (!log)
324 return -ENOMEM;
325
326 if ((err = read_log(log)))
Jesper Juhl178554a2007-07-20 00:31:48 -0700327 goto out_free;
Kylene Jo Hall55a82ab2006-01-08 01:03:15 -0800328
329 /* now register seq file */
Kylene Jo Halld09cf7d2006-01-08 01:03:48 -0800330 err = seq_open(file, &tpm_ascii_b_measurments_seqops);
331 if (!err) {
332 seq = file->private_data;
333 seq->private = log;
334 } else {
Jesper Juhl178554a2007-07-20 00:31:48 -0700335 goto out_free;
Kylene Jo Halld09cf7d2006-01-08 01:03:48 -0800336 }
Jesper Juhl178554a2007-07-20 00:31:48 -0700337
338out:
Kylene Jo Halld09cf7d2006-01-08 01:03:48 -0800339 return err;
Jesper Juhl178554a2007-07-20 00:31:48 -0700340out_free:
341 kfree(log->bios_event_log);
342 kfree(log);
343 goto out;
Kylene Jo Hall55a82ab2006-01-08 01:03:15 -0800344}
345
Adrian Bunk3bd60462008-07-25 19:44:58 -0700346static const struct file_operations tpm_ascii_bios_measurements_ops = {
Kylene Jo Hall55a82ab2006-01-08 01:03:15 -0800347 .open = tpm_ascii_bios_measurements_open,
348 .read = seq_read,
349 .llseek = seq_lseek,
350 .release = tpm_bios_measurements_release,
351};
352
353static int tpm_binary_bios_measurements_open(struct inode *inode,
354 struct file *file)
355{
356 int err;
Kylene Jo Halld09cf7d2006-01-08 01:03:48 -0800357 struct tpm_bios_log *log;
358 struct seq_file *seq;
Kylene Jo Hall55a82ab2006-01-08 01:03:15 -0800359
Kylene Jo Halld09cf7d2006-01-08 01:03:48 -0800360 log = kzalloc(sizeof(struct tpm_bios_log), GFP_KERNEL);
361 if (!log)
362 return -ENOMEM;
363
364 if ((err = read_log(log)))
Reiner Sailer03ce1102007-07-20 10:03:02 -0400365 goto out_free;
Kylene Jo Hall55a82ab2006-01-08 01:03:15 -0800366
367 /* now register seq file */
Kylene Jo Halld09cf7d2006-01-08 01:03:48 -0800368 err = seq_open(file, &tpm_binary_b_measurments_seqops);
369 if (!err) {
370 seq = file->private_data;
371 seq->private = log;
372 } else {
Reiner Sailer03ce1102007-07-20 10:03:02 -0400373 goto out_free;
Kylene Jo Halld09cf7d2006-01-08 01:03:48 -0800374 }
Reiner Sailer03ce1102007-07-20 10:03:02 -0400375
376out:
Kylene Jo Halld09cf7d2006-01-08 01:03:48 -0800377 return err;
Reiner Sailer03ce1102007-07-20 10:03:02 -0400378out_free:
379 kfree(log->bios_event_log);
380 kfree(log);
381 goto out;
Kylene Jo Hall55a82ab2006-01-08 01:03:15 -0800382}
383
Adrian Bunk3bd60462008-07-25 19:44:58 -0700384static const struct file_operations tpm_binary_bios_measurements_ops = {
Kylene Jo Hall55a82ab2006-01-08 01:03:15 -0800385 .open = tpm_binary_bios_measurements_open,
386 .read = seq_read,
387 .llseek = seq_lseek,
388 .release = tpm_bios_measurements_release,
389};
390
Andrew Mortonca4a031f2006-02-01 03:05:01 -0800391static int is_bad(void *p)
392{
393 if (!p)
394 return 1;
395 if (IS_ERR(p) && (PTR_ERR(p) != -ENODEV))
396 return 1;
397 return 0;
398}
399
Kylene Jo Hall55a82ab2006-01-08 01:03:15 -0800400struct dentry **tpm_bios_log_setup(char *name)
401{
402 struct dentry **ret = NULL, *tpm_dir, *bin_file, *ascii_file;
403
404 tpm_dir = securityfs_create_dir(name, NULL);
Andrew Mortonca4a031f2006-02-01 03:05:01 -0800405 if (is_bad(tpm_dir))
Kylene Jo Hall55a82ab2006-01-08 01:03:15 -0800406 goto out;
407
408 bin_file =
409 securityfs_create_file("binary_bios_measurements",
410 S_IRUSR | S_IRGRP, tpm_dir, NULL,
411 &tpm_binary_bios_measurements_ops);
Andrew Mortonca4a031f2006-02-01 03:05:01 -0800412 if (is_bad(bin_file))
Kylene Jo Hall55a82ab2006-01-08 01:03:15 -0800413 goto out_tpm;
414
415 ascii_file =
416 securityfs_create_file("ascii_bios_measurements",
417 S_IRUSR | S_IRGRP, tpm_dir, NULL,
418 &tpm_ascii_bios_measurements_ops);
Andrew Mortonca4a031f2006-02-01 03:05:01 -0800419 if (is_bad(ascii_file))
Kylene Jo Hall55a82ab2006-01-08 01:03:15 -0800420 goto out_bin;
421
422 ret = kmalloc(3 * sizeof(struct dentry *), GFP_KERNEL);
423 if (!ret)
424 goto out_ascii;
425
426 ret[0] = ascii_file;
427 ret[1] = bin_file;
428 ret[2] = tpm_dir;
429
430 return ret;
431
432out_ascii:
433 securityfs_remove(ascii_file);
434out_bin:
435 securityfs_remove(bin_file);
436out_tpm:
437 securityfs_remove(tpm_dir);
438out:
439 return NULL;
440}
Kylene Jo Hall55a82ab2006-01-08 01:03:15 -0800441
442void tpm_bios_log_teardown(struct dentry **lst)
443{
444 int i;
445
446 for (i = 0; i < 3; i++)
447 securityfs_remove(lst[i]);
448}