]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - drivers/char/tpm/tpm2_eventlog.c
Merge branches 'for-4.11/upstream-fixes', 'for-4.12/accutouch', 'for-4.12/cp2112...
[mirror_ubuntu-artful-kernel.git] / drivers / char / tpm / tpm2_eventlog.c
1 /*
2 * Copyright (C) 2016 IBM Corporation
3 *
4 * Authors:
5 * Nayna Jain <nayna@linux.vnet.ibm.com>
6 *
7 * Access to TPM 2.0 event log as written by Firmware.
8 * It assumes that writer of event log has followed TCG Specification
9 * for Family "2.0" and written the event data in little endian.
10 * With that, it doesn't need any endian conversion for structure
11 * content.
12 *
13 * This program is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU General Public License
15 * as published by the Free Software Foundation; either version
16 * 2 of the License, or (at your option) any later version.
17 */
18
19 #include <linux/seq_file.h>
20 #include <linux/fs.h>
21 #include <linux/security.h>
22 #include <linux/module.h>
23 #include <linux/slab.h>
24
25 #include "tpm.h"
26 #include "tpm_eventlog.h"
27
28 /*
29 * calc_tpm2_event_size() - calculate the event size, where event
30 * is an entry in the TPM 2.0 event log. The event is of type Crypto
31 * Agile Log Entry Format as defined in TCG EFI Protocol Specification
32 * Family "2.0".
33
34 * @event: event whose size is to be calculated.
35 * @event_header: the first event in the event log.
36 *
37 * Returns size of the event. If it is an invalid event, returns 0.
38 */
39 static int calc_tpm2_event_size(struct tcg_pcr_event2 *event,
40 struct tcg_pcr_event *event_header)
41 {
42 struct tcg_efi_specid_event *efispecid;
43 struct tcg_event_field *event_field;
44 void *marker;
45 void *marker_start;
46 u32 halg_size;
47 size_t size;
48 u16 halg;
49 int i;
50 int j;
51
52 marker = event;
53 marker_start = marker;
54 marker = marker + sizeof(event->pcr_idx) + sizeof(event->event_type)
55 + sizeof(event->count);
56
57 efispecid = (struct tcg_efi_specid_event *)event_header->event;
58
59 for (i = 0; (i < event->count) && (i < TPM2_ACTIVE_PCR_BANKS);
60 i++) {
61 halg_size = sizeof(event->digests[i].alg_id);
62 memcpy(&halg, marker, halg_size);
63 marker = marker + halg_size;
64 for (j = 0; (j < efispecid->num_algs); j++) {
65 if (halg == efispecid->digest_sizes[j].alg_id) {
66 marker = marker +
67 efispecid->digest_sizes[j].digest_size;
68 break;
69 }
70 }
71 }
72
73 event_field = (struct tcg_event_field *)marker;
74 marker = marker + sizeof(event_field->event_size)
75 + event_field->event_size;
76 size = marker - marker_start;
77
78 if ((event->event_type == 0) && (event_field->event_size == 0))
79 return 0;
80
81 return size;
82 }
83
84 static void *tpm2_bios_measurements_start(struct seq_file *m, loff_t *pos)
85 {
86 struct tpm_chip *chip = m->private;
87 struct tpm_bios_log *log = &chip->log;
88 void *addr = log->bios_event_log;
89 void *limit = log->bios_event_log_end;
90 struct tcg_pcr_event *event_header;
91 struct tcg_pcr_event2 *event;
92 size_t size;
93 int i;
94
95 event_header = addr;
96 size = sizeof(struct tcg_pcr_event) - sizeof(event_header->event)
97 + event_header->event_size;
98
99 if (*pos == 0) {
100 if (addr + size < limit) {
101 if ((event_header->event_type == 0) &&
102 (event_header->event_size == 0))
103 return NULL;
104 return SEQ_START_TOKEN;
105 }
106 }
107
108 if (*pos > 0) {
109 addr += size;
110 event = addr;
111 size = calc_tpm2_event_size(event, event_header);
112 if ((addr + size >= limit) || (size == 0))
113 return NULL;
114 }
115
116 for (i = 0; i < (*pos - 1); i++) {
117 event = addr;
118 size = calc_tpm2_event_size(event, event_header);
119
120 if ((addr + size >= limit) || (size == 0))
121 return NULL;
122 addr += size;
123 }
124
125 return addr;
126 }
127
128 static void *tpm2_bios_measurements_next(struct seq_file *m, void *v,
129 loff_t *pos)
130 {
131 struct tcg_pcr_event *event_header;
132 struct tcg_pcr_event2 *event;
133 struct tpm_chip *chip = m->private;
134 struct tpm_bios_log *log = &chip->log;
135 void *limit = log->bios_event_log_end;
136 size_t event_size;
137 void *marker;
138
139 event_header = log->bios_event_log;
140
141 if (v == SEQ_START_TOKEN) {
142 event_size = sizeof(struct tcg_pcr_event) -
143 sizeof(event_header->event) + event_header->event_size;
144 marker = event_header;
145 } else {
146 event = v;
147 event_size = calc_tpm2_event_size(event, event_header);
148 if (event_size == 0)
149 return NULL;
150 marker = event;
151 }
152
153 marker = marker + event_size;
154 if (marker >= limit)
155 return NULL;
156 v = marker;
157 event = v;
158
159 event_size = calc_tpm2_event_size(event, event_header);
160 if (((v + event_size) >= limit) || (event_size == 0))
161 return NULL;
162
163 (*pos)++;
164 return v;
165 }
166
167 static void tpm2_bios_measurements_stop(struct seq_file *m, void *v)
168 {
169 }
170
171 static int tpm2_binary_bios_measurements_show(struct seq_file *m, void *v)
172 {
173 struct tpm_chip *chip = m->private;
174 struct tpm_bios_log *log = &chip->log;
175 struct tcg_pcr_event *event_header = log->bios_event_log;
176 struct tcg_pcr_event2 *event = v;
177 void *temp_ptr;
178 size_t size;
179
180 if (v == SEQ_START_TOKEN) {
181 size = sizeof(struct tcg_pcr_event) -
182 sizeof(event_header->event) + event_header->event_size;
183
184 temp_ptr = event_header;
185
186 if (size > 0)
187 seq_write(m, temp_ptr, size);
188 } else {
189 size = calc_tpm2_event_size(event, event_header);
190 temp_ptr = event;
191 if (size > 0)
192 seq_write(m, temp_ptr, size);
193 }
194
195 return 0;
196 }
197
198 const struct seq_operations tpm2_binary_b_measurements_seqops = {
199 .start = tpm2_bios_measurements_start,
200 .next = tpm2_bios_measurements_next,
201 .stop = tpm2_bios_measurements_stop,
202 .show = tpm2_binary_bios_measurements_show,
203 };