btrfs: Don't clear SGID when inheriting ACLs
[linux/fpc-iii.git] / drivers / char / tpm / tpm2_eventlog.c
blob34a8afa69138f32fcef5ea3e13cf0ea2545bce2c
1 /*
2 * Copyright (C) 2016 IBM Corporation
4 * Authors:
5 * Nayna Jain <nayna@linux.vnet.ibm.com>
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.
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.
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>
25 #include "tpm.h"
26 #include "tpm_eventlog.h"
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".
34 * @event: event whose size is to be calculated.
35 * @event_header: the first event in the event log.
37 * Returns size of the event. If it is an invalid event, returns 0.
39 static int calc_tpm2_event_size(struct tcg_pcr_event2 *event,
40 struct tcg_pcr_event *event_header)
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;
52 marker = event;
53 marker_start = marker;
54 marker = marker + sizeof(event->pcr_idx) + sizeof(event->event_type)
55 + sizeof(event->count);
57 efispecid = (struct tcg_efi_specid_event *)event_header->event;
59 /* Check if event is malformed. */
60 if (event->count > efispecid->num_algs)
61 return 0;
63 for (i = 0; i < event->count; i++) {
64 halg_size = sizeof(event->digests[i].alg_id);
65 memcpy(&halg, marker, halg_size);
66 marker = marker + halg_size;
67 for (j = 0; j < efispecid->num_algs; j++) {
68 if (halg == efispecid->digest_sizes[j].alg_id) {
69 marker +=
70 efispecid->digest_sizes[j].digest_size;
71 break;
74 /* Algorithm without known length. Such event is unparseable. */
75 if (j == efispecid->num_algs)
76 return 0;
79 event_field = (struct tcg_event_field *)marker;
80 marker = marker + sizeof(event_field->event_size)
81 + event_field->event_size;
82 size = marker - marker_start;
84 if ((event->event_type == 0) && (event_field->event_size == 0))
85 return 0;
87 return size;
90 static void *tpm2_bios_measurements_start(struct seq_file *m, loff_t *pos)
92 struct tpm_chip *chip = m->private;
93 struct tpm_bios_log *log = &chip->log;
94 void *addr = log->bios_event_log;
95 void *limit = log->bios_event_log_end;
96 struct tcg_pcr_event *event_header;
97 struct tcg_pcr_event2 *event;
98 size_t size;
99 int i;
101 event_header = addr;
102 size = sizeof(struct tcg_pcr_event) - sizeof(event_header->event)
103 + event_header->event_size;
105 if (*pos == 0) {
106 if (addr + size < limit) {
107 if ((event_header->event_type == 0) &&
108 (event_header->event_size == 0))
109 return NULL;
110 return SEQ_START_TOKEN;
114 if (*pos > 0) {
115 addr += size;
116 event = addr;
117 size = calc_tpm2_event_size(event, event_header);
118 if ((addr + size >= limit) || (size == 0))
119 return NULL;
122 for (i = 0; i < (*pos - 1); i++) {
123 event = addr;
124 size = calc_tpm2_event_size(event, event_header);
126 if ((addr + size >= limit) || (size == 0))
127 return NULL;
128 addr += size;
131 return addr;
134 static void *tpm2_bios_measurements_next(struct seq_file *m, void *v,
135 loff_t *pos)
137 struct tcg_pcr_event *event_header;
138 struct tcg_pcr_event2 *event;
139 struct tpm_chip *chip = m->private;
140 struct tpm_bios_log *log = &chip->log;
141 void *limit = log->bios_event_log_end;
142 size_t event_size;
143 void *marker;
145 event_header = log->bios_event_log;
147 if (v == SEQ_START_TOKEN) {
148 event_size = sizeof(struct tcg_pcr_event) -
149 sizeof(event_header->event) + event_header->event_size;
150 marker = event_header;
151 } else {
152 event = v;
153 event_size = calc_tpm2_event_size(event, event_header);
154 if (event_size == 0)
155 return NULL;
156 marker = event;
159 marker = marker + event_size;
160 if (marker >= limit)
161 return NULL;
162 v = marker;
163 event = v;
165 event_size = calc_tpm2_event_size(event, event_header);
166 if (((v + event_size) >= limit) || (event_size == 0))
167 return NULL;
169 (*pos)++;
170 return v;
173 static void tpm2_bios_measurements_stop(struct seq_file *m, void *v)
177 static int tpm2_binary_bios_measurements_show(struct seq_file *m, void *v)
179 struct tpm_chip *chip = m->private;
180 struct tpm_bios_log *log = &chip->log;
181 struct tcg_pcr_event *event_header = log->bios_event_log;
182 struct tcg_pcr_event2 *event = v;
183 void *temp_ptr;
184 size_t size;
186 if (v == SEQ_START_TOKEN) {
187 size = sizeof(struct tcg_pcr_event) -
188 sizeof(event_header->event) + event_header->event_size;
190 temp_ptr = event_header;
192 if (size > 0)
193 seq_write(m, temp_ptr, size);
194 } else {
195 size = calc_tpm2_event_size(event, event_header);
196 temp_ptr = event;
197 if (size > 0)
198 seq_write(m, temp_ptr, size);
201 return 0;
204 const struct seq_operations tpm2_binary_b_measurements_seqops = {
205 .start = tpm2_bios_measurements_start,
206 .next = tpm2_bios_measurements_next,
207 .stop = tpm2_bios_measurements_stop,
208 .show = tpm2_binary_bios_measurements_show,