summaryrefslogtreecommitdiff
path: root/tests/vb2_gbb_tests.c
blob: 4acb328c030baa3748b9ae5d33254662022dea82 (plain)
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
/* Copyright 2019 The Chromium OS Authors. All rights reserved.
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 *
 * Tests for GBB library.
 */

#include "2common.h"
#include "2misc.h"
#include "test_common.h"

/* Mock data */
static char gbb_data[4096 + sizeof(struct vb2_gbb_header)];
static struct vb2_gbb_header *gbb = (struct vb2_gbb_header *)gbb_data;
static struct vb2_packed_key *rootkey;
static struct vb2_context ctx;
static struct vb2_workbuf wb;
static uint8_t workbuf[VB2_KERNEL_WORKBUF_RECOMMENDED_SIZE];

static void set_gbb_hwid(const char *hwid, size_t size)
{
	memcpy(gbb_data + gbb->hwid_offset, hwid, size);
	gbb->hwid_size = size;
}

static void reset_common_data(void)
{
	int gbb_used;

	memset(gbb_data, 0, sizeof(gbb_data));
	gbb->header_size = sizeof(*gbb);
	gbb->major_version = VB2_GBB_MAJOR_VER;
	gbb->minor_version = VB2_GBB_MINOR_VER;
	gbb->flags = 0;
	gbb_used = sizeof(struct vb2_gbb_header);

	gbb->recovery_key_offset = gbb_used;
	gbb->recovery_key_size = 64;
	gbb_used += gbb->recovery_key_size;
	gbb->rootkey_offset = gbb_used;
	gbb->rootkey_size = sizeof(struct vb2_packed_key);
	gbb_used += gbb->rootkey_size;

	rootkey = ((void *)gbb + gbb->rootkey_offset);
	rootkey->key_offset = sizeof(*rootkey);

	gbb->hwid_offset = gbb_used;
	const char hwid_src[] = "Test HWID";
	set_gbb_hwid(hwid_src, sizeof(hwid_src));

	memset(workbuf, 0, sizeof(workbuf));
	memset(&ctx, 0, sizeof(ctx));
	ctx.workbuf = workbuf;
	ctx.workbuf_size = sizeof(workbuf);
	vb2_init_context(&ctx);
	vb2_workbuf_from_ctx(&ctx, &wb);
}

/* Mocks */
struct vb2_gbb_header *vb2_get_gbb(struct vb2_context *c)
{
	return gbb;
}

vb2_error_t vb2ex_read_resource(struct vb2_context *c,
				enum vb2_resource_index index, uint32_t offset,
				void *buf, uint32_t size)
{
	uint8_t *rptr;
	uint32_t rsize;

	switch(index) {
	case VB2_RES_GBB:
		rptr = (uint8_t *)&gbb_data;
		rsize = sizeof(gbb_data);
		break;
	default:
		return VB2_ERROR_EX_READ_RESOURCE_INDEX;
	}

	if (offset + size >= rsize)
		return VB2_ERROR_EX_READ_RESOURCE_SIZE;

	memcpy(buf, rptr + offset, size);
	return VB2_SUCCESS;
}

/* Tests */
static void flag_tests(void)
{
	reset_common_data();
	gbb->flags = 0xdeadbeef;
	TEST_EQ(vb2api_gbb_get_flags(&ctx), gbb->flags,
		"retrieve GBB flags");
}

static void key_tests(void)
{
	/* Assume that root key and recovery key are dealt with using the same
	   code in our GBB library functions. */
	struct vb2_packed_key *keyp;
	struct vb2_workbuf wborig;
	const char key_data[] = "HELLOWORLD";
	uint32_t size;

	/* gbb.offset < sizeof(vb2_gbb_header) */
	reset_common_data();
	wborig = wb;
	gbb->rootkey_offset = sizeof(*gbb) - 1;
	TEST_EQ(vb2_gbb_read_root_key(&ctx, &keyp, &size, &wb),
		VB2_ERROR_GBB_INVALID,
		"gbb.rootkey offset too small");
	TEST_TRUE(wb.buf == wborig.buf,
		  "  workbuf restored on error");

	/* gbb.offset > gbb_data */
	reset_common_data();
	wborig = wb;
	gbb->rootkey_offset = sizeof(gbb_data) + 1;
	TEST_EQ(vb2_gbb_read_root_key(&ctx, &keyp, &size, &wb),
		VB2_ERROR_EX_READ_RESOURCE_SIZE,
		"gbb.rootkey offset too large");
	TEST_TRUE(wb.buf == wborig.buf,
		  "  workbuf restored on error");

	/* gbb.size < sizeof(vb2_packed_key) */
	reset_common_data();
	wborig = wb;
	gbb->rootkey_size = sizeof(*rootkey) - 1;
	TEST_EQ(vb2_gbb_read_root_key(&ctx, &keyp, &size, &wb),
		VB2_ERROR_GBB_INVALID,
		"gbb.rootkey size too small");
	TEST_TRUE(wb.buf == wborig.buf,
		  "  workbuf restored on error");

	/* sizeof(vb2_packed_key) > workbuf.size */
	reset_common_data();
	wborig = wb;
	wb.size = sizeof(*rootkey) - 1;
	TEST_EQ(vb2_gbb_read_root_key(&ctx, &keyp, &size, &wb),
		VB2_ERROR_GBB_WORKBUF,
		"workbuf size too small for vb2_packed_key header");
	TEST_TRUE(wb.buf == wborig.buf,
		  "  workbuf restored on error");

	/* packed_key.offset < sizeof(vb2_packed_key) */
	reset_common_data();
	wborig = wb;
	rootkey->key_size = 1;
	rootkey->key_offset = sizeof(*rootkey) - 1;
	TEST_EQ(vb2_gbb_read_root_key(&ctx, &keyp, &size, &wb),
		VB2_ERROR_INSIDE_DATA_OVERLAP,
		"rootkey offset too small");
	TEST_TRUE(wb.buf == wborig.buf,
		  "  workbuf restored on error");

	/* packed_key.offset > gbb_data */
	reset_common_data();
	wborig = wb;
	rootkey->key_size = 1;
	rootkey->key_offset = sizeof(gbb_data) + 1;
	gbb->rootkey_size = rootkey->key_offset + rootkey->key_size;
	TEST_EQ(vb2_gbb_read_root_key(&ctx, &keyp, &size, &wb),
		VB2_ERROR_EX_READ_RESOURCE_SIZE,
		"rootkey size too large");
	TEST_TRUE(wb.buf == wborig.buf,
		  "  workbuf restored on error");

	/* packed_key.size > workbuf.size */
	reset_common_data();
	wborig = wb;
	rootkey->key_size = wb.size + 1;
	gbb->rootkey_size = rootkey->key_offset + rootkey->key_size + 1;
	TEST_EQ(vb2_gbb_read_root_key(&ctx, &keyp, &size, &wb),
		VB2_ERROR_GBB_WORKBUF,
		"workbuf size too small for vb2_packed_key contents");
	TEST_TRUE(wb.buf == wborig.buf,
		  "  workbuf restored on error");

	/* gbb.size < sizeof(vb2_packed_key) + packed_key.size */
	reset_common_data();
	wborig = wb;
	rootkey->key_size = 2;
	gbb->rootkey_size = rootkey->key_offset + rootkey->key_size - 1;
	TEST_EQ(vb2_gbb_read_root_key(&ctx, &keyp, &size, &wb),
		VB2_ERROR_INSIDE_DATA_OUTSIDE,
		"rootkey size exceeds gbb.rootkey size");
	TEST_TRUE(wb.buf == wborig.buf,
		  "  workbuf restored on error");

	/* gbb.size == sizeof(vb2_packed_key) + packed_key.size */
	reset_common_data();
	wborig = wb;
	rootkey->key_size = sizeof(key_data);
	memcpy((void *)rootkey + rootkey->key_offset,
	       key_data, sizeof(key_data));
	gbb->rootkey_size = rootkey->key_offset + rootkey->key_size;
	TEST_SUCC(vb2_gbb_read_root_key(&ctx, &keyp, &size, &wb),
		  "succeeds when gbb.rootkey and rootkey sizes agree");
	TEST_TRUE(wb.size < wborig.size,
		  "  workbuf shrank on success");
	TEST_EQ(memcmp(rootkey, keyp, rootkey->key_offset + rootkey->key_size),
		0, "  copied key data successfully");
	TEST_EQ(size, rootkey->key_offset + rootkey->key_size,
		"  correct size returned");

	/* gbb.size > sizeof(vb2_packed_key) + packed_key.size
	   packed_key.offset = +0 */
	reset_common_data();
	wborig = wb;
	rootkey->key_size = 1;
	gbb->rootkey_size = rootkey->key_offset + rootkey->key_size + 1;
	TEST_SUCC(vb2_gbb_read_root_key(&ctx, &keyp, &size, &wb),
		  "succeeds when gbb.rootkey is padded after key");
	TEST_TRUE(wb.size < wborig.size,
		  "  workbuf shrank on success");
	TEST_EQ(size, rootkey->key_offset + rootkey->key_size,
		"  correct size returned");

	/* gbb.size > sizeof(vb2_packed_key) + packed_key.size
	   packed_key.offset = +1 */
	reset_common_data();
	wborig = wb;
	rootkey->key_offset = sizeof(*rootkey) + 1;
	rootkey->key_size = 1;
	gbb->rootkey_size = rootkey->key_offset + rootkey->key_size;
	TEST_SUCC(vb2_gbb_read_root_key(&ctx, &keyp, &size, &wb),
		  "succeeds when gbb.rootkey is padded before key");
	TEST_TRUE(wb.size < wborig.size,
		  "  workbuf shrank on success");
	TEST_EQ(size, rootkey->key_offset + rootkey->key_size,
		"  correct size returned");

	/* packed_key.size = 0, packed_key.offset = +1 */
	reset_common_data();
	wborig = wb;
	rootkey->key_offset = sizeof(*rootkey) + 1;
	rootkey->key_size = 0;
	gbb->rootkey_size = rootkey->key_offset + rootkey->key_size + 1;
	TEST_SUCC(vb2_gbb_read_root_key(&ctx, &keyp, &size, &wb),
		  "succeeds when gbb.rootkey is padded; empty test key");
	TEST_TRUE(wb.size < wborig.size,
		  "  workbuf shrank on success");
	TEST_EQ(size, rootkey->key_offset + rootkey->key_size,
		"  correct size returned");

	/* packed_key.size = 0, packed_key.offset = -1 */
	reset_common_data();
	wborig = wb;
	rootkey->key_offset = sizeof(*rootkey) - 1;
	rootkey->key_size = 0;
	gbb->rootkey_size = sizeof(*rootkey) + rootkey->key_size + 1;
	TEST_SUCC(vb2_gbb_read_root_key(&ctx, &keyp, &size, &wb),
		  "succeeds when gbb.rootkey is padded; empty test key");
	TEST_TRUE(wb.size < wborig.size,
		  "  workbuf shrank on success");
	TEST_EQ(size, sizeof(*rootkey), "  correct size returned");
}

static void hwid_tests(void)
{
	char hwid[VB2_GBB_HWID_MAX_SIZE];
	uint32_t size;

	/* GBB HWID size = 0 */
	{
		reset_common_data();
		gbb->hwid_size = 0;
		size = VB2_GBB_HWID_MAX_SIZE;
		TEST_EQ(vb2api_gbb_read_hwid(&ctx, hwid, &size),
			VB2_ERROR_GBB_INVALID,
			"GBB HWID size invalid (HWID missing)");
	}

	/* GBB HWID offset > GBB size */
	{
		reset_common_data();
		gbb->hwid_offset = sizeof(gbb_data) + 1;
		size = VB2_GBB_HWID_MAX_SIZE;
		TEST_EQ(vb2api_gbb_read_hwid(&ctx, hwid, &size),
			VB2_ERROR_EX_READ_RESOURCE_SIZE,
			"GBB HWID offset invalid");
	}

	/* buffer size < HWID size */
	{
		const char hwid_src[] = "Test HWID";
		reset_common_data();
		set_gbb_hwid(hwid_src, sizeof(hwid_src));
		size = sizeof(hwid_src) - 1;
		TEST_EQ(vb2api_gbb_read_hwid(&ctx, hwid, &size),
			VB2_ERROR_INVALID_PARAMETER,
			"HWID too large for buffer");
	}

	/* GBB HWID size < HWID size */
	{
		const char hwid_src[] = "Test HWID";
		reset_common_data();
		set_gbb_hwid(hwid_src, sizeof(hwid_src) - 1);
		size = sizeof(hwid_src);
		TEST_EQ(vb2api_gbb_read_hwid(&ctx, hwid, &size),
			VB2_ERROR_INVALID_PARAMETER,
			"HWID larger than GBB HWID size");
	}

	/* buffer size == HWID size */
	{
		const char hwid_src[] = "Test HWID";
		reset_common_data();
		set_gbb_hwid(hwid_src, sizeof(hwid_src));
		size = sizeof(hwid_src);
		TEST_SUCC(vb2api_gbb_read_hwid(&ctx, hwid, &size),
			  "read normal HWID");
		TEST_EQ(strcmp(hwid, "Test HWID"), 0, "  HWID correct");
		TEST_EQ(strlen(hwid) + 1, size, "  HWID size consistent");
		TEST_EQ(strlen(hwid), strlen("Test HWID"),
			"  HWID size correct");
	}

	/* buffer size > HWID size */
	{
		const char hwid_src[] = "Test HWID";
		reset_common_data();
		set_gbb_hwid(hwid_src, sizeof(hwid_src));
		size = sizeof(hwid_src) + 1;
		TEST_SUCC(vb2api_gbb_read_hwid(&ctx, hwid, &size),
			  "read normal HWID");
		TEST_EQ(strcmp(hwid, "Test HWID"), 0, "  HWID correct");
		TEST_EQ(strlen(hwid) + 1, size, "  HWID size consistent");
		TEST_EQ(strlen(hwid), strlen("Test HWID"),
			"  HWID size correct");
	}

	/* HWID with garbage */
	{
		const char hwid_src[] = "Test HWID\0garbagegarbage";
		reset_common_data();
		set_gbb_hwid(hwid_src, sizeof(hwid_src));
		size = VB2_GBB_HWID_MAX_SIZE;
		TEST_SUCC(vb2api_gbb_read_hwid(&ctx, hwid, &size),
			  "read HWID with garbage");
		TEST_EQ(strcmp(hwid, "Test HWID"), 0, "  HWID correct");
		TEST_EQ(strlen(hwid) + 1, size, "  HWID size consistent");
		TEST_EQ(strlen(hwid), strlen("Test HWID"),
			"  HWID size correct");
	}
}

int main(int argc, char* argv[])
{
	flag_tests();
	key_tests();
	hwid_tests();

	return gTestSuccess ? 0 : 255;
}