blob: 809e1b663da8960312276bde4b58fac3008c6d50 [file] [log] [blame]
Randall Spangler2d25e832015-05-12 16:39:01 -07001/* Copyright 2015 The Chromium OS Authors. All rights reserved.
2 * Use of this source code is governed by a BSD-style license that can be
3 * found in the LICENSE file.
4 *
5 * Kernel verified boot functions
6 */
7
8#include "2sysincludes.h"
Randall Spangler3d5cd882015-05-20 17:22:17 -07009#include "2misc.h"
10#include "2nvstorage.h"
Randall Spangler2d25e832015-05-12 16:39:01 -070011#include "2rsa.h"
Randall Spangler22da78c2015-05-29 10:53:11 -070012#include "2secdata.h"
Randall Spangler2d25e832015-05-12 16:39:01 -070013#include "2sha.h"
14#include "vb2_common.h"
15
Randall Spangler3d5cd882015-05-20 17:22:17 -070016static const uint8_t *vb2_signature_data_const(const struct vb2_signature *sig)
17{
18 return (uint8_t *)sig + sig->sig_offset;
19}
20
Randall Spangler22da78c2015-05-29 10:53:11 -070021/**
22 * Returns non-zero if the kernel needs to have a valid signature, instead of
23 * just a valid hash.
24 */
25static int vb2_need_signed_kernel(struct vb2_context *ctx)
26{
27 /* Recovery kernels are always signed */
28 if (ctx->flags & VB2_CONTEXT_RECOVERY_MODE)
29 return 1;
30
31 /* Normal mode kernels are always signed */
32 if (!(ctx->flags & VB2_CONTEXT_DEVELOPER_MODE))
33 return 1;
34
35 /* Developers may require signed kernels */
36 if (vb2_nv_get(ctx, VB2_NV_DEV_BOOT_SIGNED_ONLY))
37 return 1;
38
39 return 0;
40}
41
Randall Spangler3d5cd882015-05-20 17:22:17 -070042int vb2_verify_keyblock_hash(const struct vb2_keyblock *block,
43 uint32_t size,
44 const struct vb2_workbuf *wb)
45{
46 const struct vb2_signature *sig = &block->keyblock_hash;
47 struct vb2_workbuf wblocal = *wb;
48 struct vb2_digest_context *dc;
49 uint8_t *digest;
50 uint32_t digest_size;
51 int rv;
52
53 /* Sanity check keyblock before attempting hash check of data */
54 rv = vb2_check_keyblock(block, size, sig);
55 if (rv)
56 return rv;
57
58 VB2_DEBUG("Checking key block hash...\n");
59
60 /* Digest goes at start of work buffer */
61 digest_size = vb2_digest_size(VB2_HASH_SHA512);
62 digest = vb2_workbuf_alloc(&wblocal, digest_size);
63 if (!digest)
64 return VB2_ERROR_VDATA_WORKBUF_DIGEST;
65
66 /* Hashing requires temp space for the context */
67 dc = vb2_workbuf_alloc(&wblocal, sizeof(*dc));
68 if (!dc)
69 return VB2_ERROR_VDATA_WORKBUF_HASHING;
70
71 rv = vb2_digest_init(dc, VB2_HASH_SHA512);
72 if (rv)
73 return rv;
74
75 rv = vb2_digest_extend(dc, (const uint8_t *)block, sig->data_size);
76 if (rv)
77 return rv;
78
79 rv = vb2_digest_finalize(dc, digest, digest_size);
80 if (rv)
81 return rv;
82
83 if (vb2_safe_memcmp(vb2_signature_data_const(sig), digest,
84 digest_size) != 0) {
85 VB2_DEBUG("Invalid key block hash.\n");
86 return VB2_ERROR_KEYBLOCK_SIG_INVALID;
87 }
88
89 /* Success */
90 return VB2_SUCCESS;
91}
92
93int vb2_load_kernel_keyblock(struct vb2_context *ctx)
94{
95 struct vb2_shared_data *sd = vb2_get_sd(ctx);
96 struct vb2_workbuf wb;
97
98 uint8_t *key_data;
99 uint32_t key_size;
100 struct vb2_packed_key *packed_key;
101 struct vb2_public_key kernel_key;
102
103 struct vb2_keyblock *kb;
104 uint32_t block_size;
105
106 int rec_switch = (ctx->flags & VB2_CONTEXT_RECOVERY_MODE) != 0;
107 int dev_switch = (ctx->flags & VB2_CONTEXT_DEVELOPER_MODE) != 0;
Randall Spangler22da78c2015-05-29 10:53:11 -0700108 int need_keyblock_valid = vb2_need_signed_kernel(ctx);
Randall Spangler3d5cd882015-05-20 17:22:17 -0700109 int keyblock_is_valid = 1;
110
111 int rv;
112
113 vb2_workbuf_from_ctx(ctx, &wb);
114
115 /*
Randall Spangler3d5cd882015-05-20 17:22:17 -0700116 * Clear any previous keyblock-valid flag (for example, from a previous
117 * kernel where the keyblock was signed but the preamble failed
118 * verification).
119 */
120 sd->flags &= ~VB2_SD_FLAG_KERNEL_SIGNED;
121
122 /* Unpack the kernel key */
123 key_data = ctx->workbuf + sd->workbuf_kernel_key_offset;
124 key_size = sd->workbuf_kernel_key_size;
125 rv = vb2_unpack_key(&kernel_key, key_data, key_size);
126 if (rv)
127 return rv;
128
129 /* Load the kernel keyblock header after the root key */
130 kb = vb2_workbuf_alloc(&wb, sizeof(*kb));
131 if (!kb)
132 return VB2_ERROR_KERNEL_KEYBLOCK_WORKBUF_HEADER;
133
134 rv = vb2ex_read_resource(ctx, VB2_RES_KERNEL_VBLOCK, 0, kb,
135 sizeof(*kb));
136 if (rv)
137 return rv;
138
139 block_size = kb->keyblock_size;
140
141 /*
142 * Load the entire keyblock, now that we know how big it is. Note that
143 * we're loading the entire keyblock instead of just the piece after
144 * the header. That means we re-read the header. But that's a tiny
145 * amount of data, and it makes the code much more straightforward.
146 */
147 kb = vb2_workbuf_realloc(&wb, sizeof(*kb), block_size);
148 if (!kb)
149 return VB2_ERROR_KERNEL_KEYBLOCK_WORKBUF;
150
151 rv = vb2ex_read_resource(ctx, VB2_RES_KERNEL_VBLOCK, 0, kb, block_size);
152 if (rv)
153 return rv;
154
155 /* Verify the keyblock */
156 rv = vb2_verify_keyblock(kb, block_size, &kernel_key, &wb);
157 if (rv) {
158 keyblock_is_valid = 0;
159 if (need_keyblock_valid)
160 return rv;
161
162 /* Signature is invalid, but hash may be fine */
163 rv = vb2_verify_keyblock_hash(kb, block_size, &wb);
164 if (rv)
165 return rv;
166 }
167
168 /* Check the key block flags against the current boot mode */
169 if (!(kb->keyblock_flags &
170 (dev_switch ? VB2_KEY_BLOCK_FLAG_DEVELOPER_1 :
171 VB2_KEY_BLOCK_FLAG_DEVELOPER_0))) {
172 VB2_DEBUG("Key block developer flag mismatch.\n");
173 keyblock_is_valid = 0;
174 if (need_keyblock_valid)
175 return VB2_ERROR_KERNEL_KEYBLOCK_DEV_FLAG;
176 }
177 if (!(kb->keyblock_flags &
178 (rec_switch ? VB2_KEY_BLOCK_FLAG_RECOVERY_1 :
179 VB2_KEY_BLOCK_FLAG_RECOVERY_0))) {
180 VB2_DEBUG("Key block recovery flag mismatch.\n");
181 keyblock_is_valid = 0;
182 if (need_keyblock_valid)
183 return VB2_ERROR_KERNEL_KEYBLOCK_REC_FLAG;
184 }
185
186 /* Check for keyblock rollback if not in recovery mode */
187 /* Key version is the upper 16 bits of the composite version */
Randall Spanglerf7559e42016-06-23 13:45:59 -0700188 if (!rec_switch && kb->data_key.key_version > VB2_MAX_KEY_VERSION) {
Randall Spangler3d5cd882015-05-20 17:22:17 -0700189 keyblock_is_valid = 0;
190 if (need_keyblock_valid)
191 return VB2_ERROR_KERNEL_KEYBLOCK_VERSION_RANGE;
192 }
193 if (!rec_switch && kb->data_key.key_version <
194 (sd->kernel_version_secdatak >> 16)) {
195 keyblock_is_valid = 0;
196 if (need_keyblock_valid)
197 return VB2_ERROR_KERNEL_KEYBLOCK_VERSION_ROLLBACK;
198 }
199
200 sd->kernel_version = kb->data_key.key_version << 16;
201
202 /*
203 * At this point, we've checked everything. The kernel keyblock is at
204 * least self-consistent, and has either a valid signature or a valid
205 * hash. Track if it had a valid signature (that is, would we have
206 * been willing to boot it even if developer mode was off).
207 */
208 if (keyblock_is_valid)
209 sd->flags |= VB2_SD_FLAG_KERNEL_SIGNED;
210
211 /* Preamble follows the keyblock in the vblock */
212 sd->vblock_preamble_offset = kb->keyblock_size;
213
214 /*
215 * Keep just the data key from the vblock. This follows the kernel key
216 * (which we might still need to verify the next kernel, if the
217 * assoiciated kernel preamble and data don't verify).
218 */
219 sd->workbuf_data_key_offset = ctx->workbuf_used;
220 key_data = ctx->workbuf + sd->workbuf_data_key_offset;
221 packed_key = (struct vb2_packed_key *)key_data;
222 memmove(packed_key, &kb->data_key, sizeof(*packed_key));
223 packed_key->key_offset = sizeof(*packed_key);
224 memmove(key_data + packed_key->key_offset,
225 (uint8_t*)&kb->data_key + kb->data_key.key_offset,
226 packed_key->key_size);
227
228 /* Save the packed key size */
229 sd->workbuf_data_key_size =
230 packed_key->key_offset + packed_key->key_size;
231
232 /*
233 * Data key will persist in the workbuf after we return.
234 *
235 * Work buffer now contains:
236 * - vb2_shared_data
237 * - kernel key
238 * - packed kernel data key
239 */
240 ctx->workbuf_used = sd->workbuf_data_key_offset +
241 sd->workbuf_data_key_size;
242
243 return VB2_SUCCESS;
244}
245
Randall Spangler2d25e832015-05-12 16:39:01 -0700246int vb2_verify_kernel_preamble(struct vb2_kernel_preamble *preamble,
247 uint32_t size,
248 const struct vb2_public_key *key,
249 const struct vb2_workbuf *wb)
250{
251 struct vb2_signature *sig = &preamble->preamble_signature;
Randall Spangler158b2962016-06-03 14:00:27 -0700252 uint32_t min_size = EXPECTED_VB2_KERNEL_PREAMBLE_2_0_SIZE;
Randall Spangler2d25e832015-05-12 16:39:01 -0700253
254 VB2_DEBUG("Verifying kernel preamble.\n");
255
Randall Spangler158b2962016-06-03 14:00:27 -0700256 /* Make sure it's even safe to look at the struct */
257 if(size < min_size) {
Randall Spangler2d25e832015-05-12 16:39:01 -0700258 VB2_DEBUG("Not enough data for preamble header.\n");
259 return VB2_ERROR_PREAMBLE_TOO_SMALL_FOR_HEADER;
260 }
261 if (preamble->header_version_major !=
262 KERNEL_PREAMBLE_HEADER_VERSION_MAJOR) {
263 VB2_DEBUG("Incompatible kernel preamble header version.\n");
264 return VB2_ERROR_PREAMBLE_HEADER_VERSION;
265 }
Randall Spangler158b2962016-06-03 14:00:27 -0700266
267 if (preamble->header_version_minor >= 2)
268 min_size = EXPECTED_VB2_KERNEL_PREAMBLE_2_2_SIZE;
269 else if (preamble->header_version_minor == 1)
270 min_size = EXPECTED_VB2_KERNEL_PREAMBLE_2_1_SIZE;
271 if(preamble->preamble_size < min_size) {
272 VB2_DEBUG("Preamble size too small for header.\n");
273 return VB2_ERROR_PREAMBLE_TOO_SMALL_FOR_HEADER;
Randall Spangler2d25e832015-05-12 16:39:01 -0700274 }
275 if (size < preamble->preamble_size) {
276 VB2_DEBUG("Not enough data for preamble.\n");
277 return VB2_ERROR_PREAMBLE_SIZE;
278 }
279
280 /* Check signature */
281 if (vb2_verify_signature_inside(preamble, preamble->preamble_size,
282 sig)) {
283 VB2_DEBUG("Preamble signature off end of preamble\n");
284 return VB2_ERROR_PREAMBLE_SIG_OUTSIDE;
285 }
286
287 /* Make sure advertised signature data sizes are sane. */
288 if (preamble->preamble_size < sig->data_size) {
289 VB2_DEBUG("Signature calculated past end of the block\n");
290 return VB2_ERROR_PREAMBLE_SIGNED_TOO_MUCH;
291 }
292
293 if (vb2_verify_data((const uint8_t *)preamble, size, sig, key, wb)) {
294 VB2_DEBUG("Preamble signature validation failed\n");
295 return VB2_ERROR_PREAMBLE_SIG_INVALID;
296 }
297
298 /* Verify we signed enough data */
299 if (sig->data_size < sizeof(struct vb2_fw_preamble)) {
300 VB2_DEBUG("Didn't sign enough data\n");
301 return VB2_ERROR_PREAMBLE_SIGNED_TOO_LITTLE;
302 }
303
304 /* Verify body signature is inside the signed data */
305 if (vb2_verify_signature_inside(preamble, sig->data_size,
306 &preamble->body_signature)) {
307 VB2_DEBUG("Body signature off end of preamble\n");
308 return VB2_ERROR_PREAMBLE_BODY_SIG_OUTSIDE;
309 }
310
311 /*
312 * If bootloader is present, verify it's covered by the body
313 * signature.
314 */
315 if (preamble->bootloader_size) {
316 const void *body_ptr =
317 (const void *)(uintptr_t)preamble->body_load_address;
318 const void *bootloader_ptr =
319 (const void *)(uintptr_t)preamble->bootloader_address;
320 if (vb2_verify_member_inside(body_ptr,
321 preamble->body_signature.data_size,
322 bootloader_ptr,
323 preamble->bootloader_size,
324 0, 0)) {
325 VB2_DEBUG("Bootloader off end of signed data\n");
326 return VB2_ERROR_PREAMBLE_BOOTLOADER_OUTSIDE;
327 }
328 }
329
330 /*
331 * If vmlinuz header is present, verify it's covered by the body
332 * signature.
333 */
Randall Spangler158b2962016-06-03 14:00:27 -0700334 if (preamble->header_version_minor >= 1 &&
335 preamble->vmlinuz_header_size) {
Randall Spangler2d25e832015-05-12 16:39:01 -0700336 const void *body_ptr =
337 (const void *)(uintptr_t)preamble->body_load_address;
338 const void *vmlinuz_header_ptr = (const void *)
339 (uintptr_t)preamble->vmlinuz_header_address;
340 if (vb2_verify_member_inside(body_ptr,
341 preamble->body_signature.data_size,
342 vmlinuz_header_ptr,
343 preamble->vmlinuz_header_size,
344 0, 0)) {
345 VB2_DEBUG("Vmlinuz header off end of signed data\n");
346 return VB2_ERROR_PREAMBLE_VMLINUZ_HEADER_OUTSIDE;
347 }
348 }
349
350 /* Success */
351 return VB2_SUCCESS;
352}
Randall Spangler22da78c2015-05-29 10:53:11 -0700353
354int vb2_load_kernel_preamble(struct vb2_context *ctx)
355{
356 struct vb2_shared_data *sd = vb2_get_sd(ctx);
357 struct vb2_workbuf wb;
358
359 uint8_t *key_data = ctx->workbuf + sd->workbuf_data_key_offset;
360 uint32_t key_size = sd->workbuf_data_key_size;
361 struct vb2_public_key data_key;
362
363 /* Preamble goes in the next unused chunk of work buffer */
364 /* TODO: what's the minimum workbuf size? Kernel preamble is usually
365 * padded to around 64KB. */
366 struct vb2_kernel_preamble *pre;
367 uint32_t pre_size;
368
369 int rv;
370
371 vb2_workbuf_from_ctx(ctx, &wb);
372
373 /* Unpack the kernel data key */
374 if (!sd->workbuf_data_key_size)
375 return VB2_ERROR_KERNEL_PREAMBLE2_DATA_KEY;
376
377 rv = vb2_unpack_key(&data_key, key_data, key_size);
378 if (rv)
379 return rv;
380
381 /* Load the kernel preamble header */
382 pre = vb2_workbuf_alloc(&wb, sizeof(*pre));
383 if (!pre)
384 return VB2_ERROR_KERNEL_PREAMBLE2_WORKBUF_HEADER;
385
386 rv = vb2ex_read_resource(ctx, VB2_RES_KERNEL_VBLOCK,
387 sd->vblock_preamble_offset,
388 pre, sizeof(*pre));
389 if (rv)
390 return rv;
391
392 pre_size = pre->preamble_size;
393
394 /* Load the entire preamble, now that we know how big it is */
395 pre = vb2_workbuf_realloc(&wb, sizeof(*pre), pre_size);
396 if (!pre)
397 return VB2_ERROR_KERNEL_PREAMBLE2_WORKBUF;
398
399 rv = vb2ex_read_resource(ctx, VB2_RES_KERNEL_VBLOCK,
400 sd->vblock_preamble_offset,
401 pre, pre_size);
402 if (rv)
403 return rv;
404
405 /*
406 * Work buffer now contains:
407 * - vb2_shared_data
408 * - kernel key
409 * - packed kernel data key
410 * - kernel preamble
411 */
412
413 /* Verify the preamble */
414 rv = vb2_verify_kernel_preamble(pre, pre_size, &data_key, &wb);
415 if (rv)
416 return rv;
417
418 /*
419 * Kernel preamble version is the lower 16 bits of the composite kernel
420 * version.
421 */
Randall Spanglerf7559e42016-06-23 13:45:59 -0700422 if (pre->kernel_version > VB2_MAX_PREAMBLE_VERSION)
Randall Spangler22da78c2015-05-29 10:53:11 -0700423 return VB2_ERROR_KERNEL_PREAMBLE_VERSION_RANGE;
424
425 /* Combine with the key version from vb2_load_kernel_keyblock() */
426 sd->kernel_version |= pre->kernel_version;
427
428 if (vb2_need_signed_kernel(ctx) &&
429 sd->kernel_version < sd->kernel_version_secdatak)
430 return VB2_ERROR_KERNEL_PREAMBLE_VERSION_ROLLBACK;
431
432 /* Keep track of where we put the preamble */
433 sd->workbuf_preamble_offset = vb2_offset_of(ctx->workbuf, pre);
434 sd->workbuf_preamble_size = pre_size;
435
436 /*
437 * Preamble will persist in work buffer after we return.
438 *
439 * Work buffer now contains:
440 * - vb2_shared_data
441 * - kernel key
442 * - packed kernel data key
443 * - kernel preamble
444 *
445 * TODO: we could move the preamble down over the kernel data key
446 * since we don't need it anymore.
447 */
448 ctx->workbuf_used = sd->workbuf_preamble_offset + pre_size;
449
450 return VB2_SUCCESS;
451}
Randall Spangler158b2962016-06-03 14:00:27 -0700452
453void vb2_kernel_get_vmlinuz_header(const struct vb2_kernel_preamble *preamble,
454 uint64_t *vmlinuz_header_address,
455 uint32_t *vmlinuz_header_size)
456{
457 if (preamble->header_version_minor < 1) {
458 *vmlinuz_header_address = 0;
459 *vmlinuz_header_size = 0;
460 } else {
461 /*
462 * Set header and size only if the preamble header version is >
463 * 2.1 as they don't exist in version 2.0 (Note that we don't
464 * need to check header_version_major; if that's not 2 then
Randall Spangler13b10972016-10-14 11:04:27 -0700465 * vb2_verify_kernel_preamble() would have already failed.
Randall Spangler158b2962016-06-03 14:00:27 -0700466 */
467 *vmlinuz_header_address = preamble->vmlinuz_header_address;
468 *vmlinuz_header_size = preamble->vmlinuz_header_size;
469 }
470}
471
472uint32_t vb2_kernel_get_flags(const struct vb2_kernel_preamble *preamble)
473{
474 if (preamble->header_version_minor < 2)
475 return 0;
476
477 return preamble->flags;
478}