forked from rhboot/shim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sbat.c
297 lines (248 loc) · 6.65 KB
/
sbat.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
// SPDX-License-Identifier: BSD-2-Clause-Patent
/*
* sbat.c - parse SBAT data from the .sbat section data
*/
#include "shim.h"
#include "string.h"
EFI_STATUS
parse_sbat_section(char *section_base, size_t section_size,
size_t *n_entries,
struct sbat_section_entry ***entriesp)
{
struct sbat_section_entry *entry = NULL, **entries;
EFI_STATUS efi_status = EFI_SUCCESS;
list_t csv, *pos = NULL;
char * end = section_base + section_size - 1;
size_t allocsz = 0;
size_t n;
char *strtab;
if (!section_base || !section_size || !n_entries || !entriesp)
return EFI_INVALID_PARAMETER;
INIT_LIST_HEAD(&csv);
efi_status =
parse_csv_data(section_base, end, SBAT_SECTION_COLUMNS, &csv);
if (EFI_ERROR(efi_status)) {
return efi_status;
}
n = 0;
list_for_each(pos, &csv) {
struct csv_row * row;
size_t i;
row = list_entry(pos, struct csv_row, list);
if (row->n_columns < SBAT_SECTION_COLUMNS) {
efi_status = EFI_INVALID_PARAMETER;
goto err;
}
allocsz += sizeof(struct sbat_section_entry *);
allocsz += sizeof(struct sbat_section_entry);
for (i = 0; i < row->n_columns; i++) {
if (row->columns[i][0] == '\000') {
efi_status = EFI_INVALID_PARAMETER;
goto err;
}
allocsz += strlena(row->columns[i]) + 1;
}
n++;
}
strtab = AllocateZeroPool(allocsz);
if (!strtab) {
efi_status = EFI_OUT_OF_RESOURCES;
goto err;
}
entries = (struct sbat_section_entry **)strtab;
strtab += sizeof(struct sbat_section_entry *) * n;
entry = (struct sbat_section_entry *)strtab;
strtab += sizeof(struct sbat_section_entry) * n;
n = 0;
list_for_each(pos, &csv) {
struct csv_row * row;
size_t i;
const char **ptrs[] = {
&entry->component_name,
&entry->component_generation,
&entry->vendor_name,
&entry->vendor_package_name,
&entry->vendor_version,
&entry->vendor_url,
};
row = list_entry(pos, struct csv_row, list);
for (i = 0; i < row->n_columns; i++) {
*(ptrs[i]) = strtab;
strtab = stpcpy(strtab, row->columns[i]) + 1;
}
entries[n] = entry;
entry++;
n++;
}
*entriesp = entries;
*n_entries = n;
err:
free_csv_list(&csv);
return efi_status;
}
void
cleanup_sbat_section_entries(size_t n, struct sbat_section_entry **entries)
{
if (!n || !entries)
return;
FreePool(entries);
}
EFI_STATUS
verify_single_entry(struct sbat_section_entry *entry, struct sbat_var_entry *sbat_var_entry)
{
UINT16 sbat_gen, sbat_var_gen;
if (strcmp((const char *)entry->component_name, (const char *)sbat_var_entry->component_name) == 0) {
dprint(L"component %a has a matching SBAT variable entry, verifying\n",
entry->component_name);
/*
* atoi returns zero for failed conversion, so essentially
* badly parsed component_generation will be treated as zero
*/
sbat_gen = atoi((const char *)entry->component_generation);
sbat_var_gen = atoi((const char *)sbat_var_entry->component_generation);
if (sbat_gen < sbat_var_gen) {
dprint(L"component %a, generation %d, was revoked by SBAT variable",
entry->component_name, sbat_gen);
LogError(L"image did not pass SBAT verification\n");
return EFI_SECURITY_VIOLATION;
}
}
return EFI_SUCCESS;
}
void
cleanup_sbat_var(list_t *entries)
{
list_t *pos = NULL, *tmp = NULL;
struct sbat_var_entry *entry;
void *first = NULL;
list_for_each_safe(pos, tmp, entries) {
entry = list_entry(pos, struct sbat_var_entry, list);
if ((uintptr_t)entry < (uintptr_t)first && entry != NULL)
first = entry;
list_del(&entry->list);
}
if (first)
FreePool(first);
}
EFI_STATUS
verify_sbat_helper(list_t *local_sbat_var, size_t n, struct sbat_section_entry **entries)
{
unsigned int i;
list_t *pos = NULL;
EFI_STATUS efi_status = EFI_SUCCESS;
struct sbat_var_entry *sbat_var_entry;
if (list_empty(local_sbat_var)) {
dprint(L"SBAT variable not present\n");
return EFI_SUCCESS;
}
for (i = 0; i < n; i++) {
list_for_each(pos, local_sbat_var) {
sbat_var_entry = list_entry(pos, struct sbat_var_entry, list);
efi_status = verify_single_entry(entries[i], sbat_var_entry);
if (EFI_ERROR(efi_status))
goto out;
}
}
out:
dprint(L"finished verifying SBAT data: %r\n", efi_status);
return efi_status;
}
EFI_STATUS
verify_sbat(size_t n, struct sbat_section_entry **entries)
{
EFI_STATUS efi_status;
efi_status = verify_sbat_helper(&sbat_var, n, entries);
return efi_status;
}
EFI_STATUS
parse_sbat_var_data(list_t *entry_list, UINT8 *data, UINTN datasize)
{
struct sbat_var_entry *entry = NULL, **entries;
EFI_STATUS efi_status = EFI_SUCCESS;
list_t csv, *pos = NULL;
char * start = (char *)data;
char * end = (char *)data + datasize - 1;
size_t allocsz = 0;
size_t n;
char *strtab;
if (!entry_list|| !data || datasize == 0)
return EFI_INVALID_PARAMETER;
INIT_LIST_HEAD(&csv);
efi_status = parse_csv_data(start, end, SBAT_VAR_COLUMNS, &csv);
if (EFI_ERROR(efi_status)) {
return efi_status;
}
n = 0;
list_for_each(pos, &csv) {
struct csv_row * row;
size_t i;
row = list_entry(pos, struct csv_row, list);
if (row->n_columns < SBAT_VAR_REQUIRED_COLUMNS) {
efi_status = EFI_INVALID_PARAMETER;
goto err;
}
allocsz += sizeof(struct sbat_var_entry *);
allocsz += sizeof(struct sbat_var_entry);
for (i = 0; i < row->n_columns; i++) {
if (row->columns[i][0]) {
efi_status = EFI_INVALID_PARAMETER;
goto err;
}
allocsz += strlena(row->columns[i]) + 1;
}
n++;
}
strtab = AllocateZeroPool(allocsz);
if (!strtab) {
efi_status = EFI_OUT_OF_RESOURCES;
goto err;
}
INIT_LIST_HEAD(entry_list);
entries = (struct sbat_var_entry **)strtab;
strtab += sizeof(struct sbat_var_entry *) * n;
entry = (struct sbat_var_entry *)strtab;
strtab += sizeof(struct sbat_var_entry) * n;
n = 0;
list_for_each(pos, &csv) {
struct csv_row * row;
size_t i;
const char **ptrs[] = {
&entry->component_name,
&entry->component_generation,
&entry->sbat_datestamp,
};
row = list_entry(pos, struct csv_row, list);
for (i = 0; i < row->n_columns; i++) {
*(ptrs[i]) = strtab;
strtab = stpcpy(strtab, row->columns[i]) + 1;
}
INIT_LIST_HEAD(&entry->list);
list_add_tail(&entry->list, entry_list);
entries[n] = entry;
entry++;
n++;
}
err:
free_csv_list(&csv);
return efi_status;
}
EFI_STATUS
parse_sbat_var(list_t *entries)
{
UINT8 *data = 0;
UINTN datasize;
EFI_STATUS efi_status;
if (!entries)
return EFI_INVALID_PARAMETER;
efi_status = get_variable(L"SBAT", &data, &datasize, SHIM_LOCK_GUID);
if (EFI_ERROR(efi_status)) {
LogError(L"Failed to read SBAT variable\n", efi_status);
return efi_status;
}
/*
* We've intentionally made sure there's a NUL byte on all variable
* allocations, so use that here.
*/
return parse_sbat_var_data(entries, data, datasize+1);
}
// vim:fenc=utf-8:tw=75:noet