xref: /aosp_15_r20/external/vboot_reference/firmware/lib20/kernel.c (revision 8617a60d3594060b7ecbd21bc622a7c14f3cf2bc)
1 /* Copyright 2015 The ChromiumOS Authors
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 "2common.h"
9 #include "2misc.h"
10 #include "2nvstorage.h"
11 #include "2rsa.h"
12 #include "2secdata.h"
13 #include "2sha.h"
14 #include "2sysincludes.h"
15 
16 /**
17  * Returns non-zero if the kernel needs to have a valid signature, instead of
18  * just a valid hash.
19  */
vb2_need_signed_kernel(struct vb2_context * ctx)20 static int vb2_need_signed_kernel(struct vb2_context *ctx)
21 {
22 	/* Recovery kernels are always signed */
23 	if (ctx->flags & VB2_CONTEXT_RECOVERY_MODE)
24 		return 1;
25 
26 	/* Normal mode kernels are always signed */
27 	if (!(ctx->flags & VB2_CONTEXT_DEVELOPER_MODE))
28 		return 1;
29 
30 	/* Developers may require signed kernels */
31 	if (vb2_nv_get(ctx, VB2_NV_DEV_BOOT_SIGNED_ONLY))
32 		return 1;
33 
34 	return 0;
35 }
36 
vb2_load_kernel_keyblock(struct vb2_context * ctx)37 vb2_error_t vb2_load_kernel_keyblock(struct vb2_context *ctx)
38 {
39 	struct vb2_shared_data *sd = vb2_get_sd(ctx);
40 	struct vb2_workbuf wb;
41 
42 	uint8_t *key_data;
43 	uint32_t key_size;
44 	struct vb2_packed_key *packed_key;
45 	struct vb2_public_key kernel_key;
46 
47 	struct vb2_keyblock *kb;
48 	uint32_t block_size;
49 
50 	int rec_switch = (ctx->flags & VB2_CONTEXT_RECOVERY_MODE) != 0;
51 	int dev_switch = (ctx->flags & VB2_CONTEXT_DEVELOPER_MODE) != 0;
52 	int need_keyblock_valid = vb2_need_signed_kernel(ctx);
53 	int keyblock_is_valid = 1;
54 
55 	vb2_error_t rv;
56 
57 	vb2_workbuf_from_ctx(ctx, &wb);
58 
59 	/*
60 	 * Clear any previous keyblock-valid flag (for example, from a previous
61 	 * kernel where the keyblock was signed but the preamble failed
62 	 * verification).
63 	 */
64 	sd->flags &= ~VB2_SD_FLAG_KERNEL_SIGNED;
65 
66 	/* Unpack the kernel key */
67 	key_data = vb2_member_of(sd, sd->kernel_key_offset);
68 	key_size = sd->kernel_key_size;
69 	VB2_TRY(vb2_unpack_key_buffer(&kernel_key, key_data, key_size));
70 
71 	/* Load the kernel keyblock header after the root key */
72 	kb = vb2_workbuf_alloc(&wb, sizeof(*kb));
73 	if (!kb)
74 		return VB2_ERROR_KERNEL_KEYBLOCK_WORKBUF_HEADER;
75 
76 	VB2_TRY(vb2ex_read_resource(ctx, VB2_RES_KERNEL_VBLOCK, 0, kb,
77 				    sizeof(*kb)));
78 
79 	block_size = kb->keyblock_size;
80 
81 	/*
82 	 * Load the entire keyblock, now that we know how big it is.  Note that
83 	 * we're loading the entire keyblock instead of just the piece after
84 	 * the header.  That means we re-read the header.  But that's a tiny
85 	 * amount of data, and it makes the code much more straightforward.
86 	 */
87 	kb = vb2_workbuf_realloc(&wb, sizeof(*kb), block_size);
88 	if (!kb)
89 		return VB2_ERROR_KERNEL_KEYBLOCK_WORKBUF;
90 
91 	VB2_TRY(vb2ex_read_resource(ctx, VB2_RES_KERNEL_VBLOCK, 0, kb,
92 				    block_size));
93 
94 	/* Verify the keyblock */
95 	rv = vb2_verify_keyblock(kb, block_size, &kernel_key, &wb);
96 	if (rv) {
97 		keyblock_is_valid = 0;
98 		if (need_keyblock_valid)
99 			return rv;
100 
101 		/* Signature is invalid, but hash may be fine */
102 		VB2_TRY(vb2_verify_keyblock_hash(kb, block_size, &wb));
103 	}
104 
105 	/* Check the keyblock flags against the current boot mode */
106 	if (!(kb->keyblock_flags &
107 	      (dev_switch ? VB2_KEYBLOCK_FLAG_DEVELOPER_1 :
108 	       VB2_KEYBLOCK_FLAG_DEVELOPER_0))) {
109 		VB2_DEBUG("Keyblock developer flag mismatch.\n");
110 		keyblock_is_valid = 0;
111 		if (need_keyblock_valid)
112 			return VB2_ERROR_KERNEL_KEYBLOCK_DEV_FLAG;
113 	}
114 	if (!(kb->keyblock_flags &
115 	      (rec_switch ? VB2_KEYBLOCK_FLAG_RECOVERY_1 :
116 	       VB2_KEYBLOCK_FLAG_RECOVERY_0))) {
117 		VB2_DEBUG("Keyblock recovery flag mismatch.\n");
118 		keyblock_is_valid = 0;
119 		if (need_keyblock_valid)
120 			return VB2_ERROR_KERNEL_KEYBLOCK_REC_FLAG;
121 	}
122 
123 	/* Check for keyblock rollback if not in recovery mode */
124 	/* Key version is the upper 16 bits of the composite version */
125 	if (!rec_switch && kb->data_key.key_version > VB2_MAX_KEY_VERSION) {
126 		keyblock_is_valid = 0;
127 		if (need_keyblock_valid)
128 			return VB2_ERROR_KERNEL_KEYBLOCK_VERSION_RANGE;
129 	}
130 	if (!rec_switch && kb->data_key.key_version <
131 	    (sd->kernel_version_secdata >> 16)) {
132 		keyblock_is_valid = 0;
133 		if (need_keyblock_valid)
134 			return VB2_ERROR_KERNEL_KEYBLOCK_VERSION_ROLLBACK;
135 	}
136 
137 	sd->kernel_version = kb->data_key.key_version << 16;
138 
139 	/*
140 	 * At this point, we've checked everything.  The kernel keyblock is at
141 	 * least self-consistent, and has either a valid signature or a valid
142 	 * hash.  Track if it had a valid signature (that is, would we have
143 	 * been willing to boot it even if developer mode was off).
144 	 */
145 	if (keyblock_is_valid)
146 		sd->flags |= VB2_SD_FLAG_KERNEL_SIGNED;
147 
148 	/* Preamble follows the keyblock in the vblock */
149 	sd->vblock_preamble_offset = kb->keyblock_size;
150 
151 	/*
152 	 * Keep just the data key from the vblock.  This follows the kernel key
153 	 * (which we might still need to verify the next kernel, if the
154 	 * assoiciated kernel preamble and data don't verify).
155 	 */
156 	sd->data_key_offset = sd->workbuf_used;
157 	key_data = vb2_member_of(sd, sd->data_key_offset);
158 	packed_key = (struct vb2_packed_key *)key_data;
159 	memmove(packed_key, &kb->data_key, sizeof(*packed_key));
160 	packed_key->key_offset = sizeof(*packed_key);
161 	memmove(key_data + packed_key->key_offset,
162 		(uint8_t*)&kb->data_key + kb->data_key.key_offset,
163 		packed_key->key_size);
164 
165 	/* Save the packed key size */
166 	sd->data_key_size =
167 		packed_key->key_offset + packed_key->key_size;
168 
169 	/*
170 	 * Data key will persist in the workbuf after we return.
171 	 *
172 	 * Work buffer now contains:
173 	 *   - vb2_shared_data
174 	 *   - kernel key
175 	 *   - packed kernel data key
176 	 */
177 	vb2_set_workbuf_used(ctx, sd->data_key_offset + sd->data_key_size);
178 
179 	return VB2_SUCCESS;
180 }
181 
vb2_load_kernel_preamble(struct vb2_context * ctx)182 vb2_error_t vb2_load_kernel_preamble(struct vb2_context *ctx)
183 {
184 	struct vb2_shared_data *sd = vb2_get_sd(ctx);
185 	struct vb2_workbuf wb;
186 
187 	uint8_t *key_data = vb2_member_of(sd, sd->data_key_offset);
188 	uint32_t key_size = sd->data_key_size;
189 	struct vb2_public_key data_key;
190 
191 	/* Preamble goes in the next unused chunk of work buffer */
192 	/* TODO: what's the minimum workbuf size?  Kernel preamble is usually
193 	 * padded to around 64KB. */
194 	struct vb2_kernel_preamble *pre;
195 	uint32_t pre_size;
196 
197 	vb2_workbuf_from_ctx(ctx, &wb);
198 
199 	/* Unpack the kernel data key */
200 	if (!sd->data_key_size)
201 		return VB2_ERROR_KERNEL_PREAMBLE2_DATA_KEY;
202 
203 	VB2_TRY(vb2_unpack_key_buffer(&data_key, key_data, key_size));
204 
205 	/* Load the kernel preamble header */
206 	pre = vb2_workbuf_alloc(&wb, sizeof(*pre));
207 	if (!pre)
208 		return VB2_ERROR_KERNEL_PREAMBLE2_WORKBUF_HEADER;
209 
210 	VB2_TRY(vb2ex_read_resource(ctx, VB2_RES_KERNEL_VBLOCK,
211 				    sd->vblock_preamble_offset,
212 				    pre, sizeof(*pre)));
213 
214 	pre_size = pre->preamble_size;
215 
216 	/* Load the entire preamble, now that we know how big it is */
217 	pre = vb2_workbuf_realloc(&wb, sizeof(*pre), pre_size);
218 	if (!pre)
219 		return VB2_ERROR_KERNEL_PREAMBLE2_WORKBUF;
220 
221 	VB2_TRY(vb2ex_read_resource(ctx, VB2_RES_KERNEL_VBLOCK,
222 				    sd->vblock_preamble_offset,
223 				    pre, pre_size));
224 
225 	/*
226 	 * Work buffer now contains:
227 	 *   - vb2_shared_data
228 	 *   - kernel key
229 	 *   - packed kernel data key
230 	 *   - kernel preamble
231 	 */
232 
233 	/* Verify the preamble */
234 	VB2_TRY(vb2_verify_kernel_preamble(pre, pre_size, &data_key, &wb));
235 
236 	/*
237 	 * Kernel preamble version is the lower 16 bits of the composite kernel
238 	 * version.
239 	 */
240 	if (pre->kernel_version > VB2_MAX_PREAMBLE_VERSION)
241 		return VB2_ERROR_KERNEL_PREAMBLE_VERSION_RANGE;
242 
243 	/* Combine with the key version from vb2_load_kernel_keyblock() */
244 	sd->kernel_version |= pre->kernel_version;
245 
246 	if (vb2_need_signed_kernel(ctx) &&
247 	    sd->kernel_version < sd->kernel_version_secdata)
248 		return VB2_ERROR_KERNEL_PREAMBLE_VERSION_ROLLBACK;
249 
250 	/* Keep track of where we put the preamble */
251 	sd->preamble_offset = vb2_offset_of(sd, pre);
252 	sd->preamble_size = pre_size;
253 
254 	/*
255 	 * Preamble will persist in work buffer after we return.
256 	 *
257 	 * Work buffer now contains:
258 	 *   - vb2_shared_data
259 	 *   - vb2_gbb_header
260 	 *   - kernel key
261 	 *   - packed kernel data key
262 	 *   - kernel preamble
263 	 *
264 	 * TODO: we could move the preamble down over the kernel data key
265 	 * since we don't need it anymore.
266 	 */
267 	vb2_set_workbuf_used(ctx, sd->preamble_offset + pre_size);
268 
269 	return VB2_SUCCESS;
270 }
271