vb20_kernel_tests.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  1. /* 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. * Tests for kernel verification library
  6. */
  7. #include <stdio.h>
  8. #include "2sysincludes.h"
  9. #include "2api.h"
  10. #include "2common.h"
  11. #include "2misc.h"
  12. #include "2nvstorage.h"
  13. #include "2rsa.h"
  14. #include "2secdata.h"
  15. #include "vb2_common.h"
  16. #include "test_common.h"
  17. /* Common context for tests */
  18. static uint8_t workbuf[VB2_KERNEL_WORKBUF_RECOMMENDED_SIZE]
  19. __attribute__ ((aligned (VB2_WORKBUF_ALIGN)));
  20. static struct vb2_workbuf wb;
  21. static struct vb2_context cc;
  22. static struct vb2_shared_data *sd;
  23. /* Mocked function data */
  24. static struct {
  25. struct vb2_gbb_header h;
  26. struct vb2_packed_key recovery_key;
  27. char recovery_key_data[32];
  28. } mock_gbb;
  29. static struct {
  30. /* Keyblock */
  31. struct {
  32. struct vb2_keyblock kb;
  33. char data_key_data[16];
  34. uint8_t kbdata[128];
  35. uint8_t hash[VB2_SHA512_DIGEST_SIZE];
  36. } k;
  37. /* Preamble follows keyblock */
  38. struct {
  39. struct vb2_kernel_preamble pre;
  40. uint8_t predata[128];
  41. } p;
  42. } mock_vblock;
  43. static int mock_read_res_fail_on_call;
  44. static int mock_unpack_key_retval;
  45. static int mock_verify_keyblock_retval;
  46. static int mock_verify_preamble_retval;
  47. /* Type of test to reset for */
  48. enum reset_type {
  49. FOR_KEYBLOCK,
  50. FOR_PREAMBLE
  51. };
  52. static void rehash_keyblock(void)
  53. {
  54. struct vb2_keyblock *kb = &mock_vblock.k.kb;
  55. struct vb2_signature *hashsig = &mock_vblock.k.kb.keyblock_hash;
  56. struct vb2_digest_context dc;
  57. hashsig->sig_offset = vb2_offset_of(hashsig, mock_vblock.k.hash);
  58. hashsig->sig_size = sizeof(mock_vblock.k.hash);
  59. hashsig->data_size = hashsig->sig_offset;
  60. vb2_digest_init(&dc, VB2_HASH_SHA512);
  61. vb2_digest_extend(&dc, (const uint8_t *)kb, hashsig->data_size);
  62. vb2_digest_finalize(&dc, mock_vblock.k.hash, hashsig->sig_size);
  63. }
  64. static void reset_common_data(enum reset_type t)
  65. {
  66. struct vb2_keyblock *kb = &mock_vblock.k.kb;
  67. struct vb2_kernel_preamble *pre = &mock_vblock.p.pre;
  68. memset(workbuf, 0xaa, sizeof(workbuf));
  69. memset(&cc, 0, sizeof(cc));
  70. cc.workbuf = workbuf;
  71. cc.workbuf_size = sizeof(workbuf);
  72. vb2_workbuf_from_ctx(&cc, &wb);
  73. vb2_init_context(&cc);
  74. sd = vb2_get_sd(&cc);
  75. vb2_nv_init(&cc);
  76. vb2_secdatak_create(&cc);
  77. vb2_secdatak_init(&cc);
  78. mock_read_res_fail_on_call = 0;
  79. mock_unpack_key_retval = VB2_SUCCESS;
  80. mock_verify_keyblock_retval = VB2_SUCCESS;
  81. mock_verify_preamble_retval = VB2_SUCCESS;
  82. /* Set up mock data for verifying keyblock */
  83. sd->kernel_version_secdatak = 0x20002;
  84. vb2_secdatak_set(&cc, VB2_SECDATAK_VERSIONS, 0x20002);
  85. mock_gbb.recovery_key.algorithm = 11;
  86. mock_gbb.recovery_key.key_offset =
  87. vb2_offset_of(&mock_gbb.recovery_key,
  88. &mock_gbb.recovery_key_data);
  89. mock_gbb.recovery_key.key_size = sizeof(mock_gbb.recovery_key_data);
  90. kb->keyblock_size = sizeof(mock_vblock.k);
  91. memcpy(kb->magic, KEY_BLOCK_MAGIC, KEY_BLOCK_MAGIC_SIZE);
  92. kb->keyblock_flags = VB2_KEY_BLOCK_FLAG_DEVELOPER_1 |
  93. VB2_KEY_BLOCK_FLAG_DEVELOPER_0 |
  94. VB2_KEY_BLOCK_FLAG_RECOVERY_1 | VB2_KEY_BLOCK_FLAG_RECOVERY_0;
  95. kb->header_version_major = KEY_BLOCK_HEADER_VERSION_MAJOR;
  96. kb->header_version_minor = KEY_BLOCK_HEADER_VERSION_MINOR;
  97. kb->data_key.algorithm = 7;
  98. kb->data_key.key_version = 2;
  99. kb->data_key.key_offset =
  100. vb2_offset_of(&mock_vblock.k, &mock_vblock.k.data_key_data) -
  101. vb2_offset_of(&mock_vblock.k, &kb->data_key);
  102. kb->data_key.key_size = sizeof(mock_vblock.k.data_key_data);
  103. strcpy(mock_vblock.k.data_key_data, "data key data!!");
  104. rehash_keyblock();
  105. pre->preamble_size = sizeof(mock_vblock.p);
  106. pre->kernel_version = 2;
  107. /* If verifying preamble, verify keyblock first to set up data key */
  108. if (t == FOR_PREAMBLE)
  109. vb2_load_kernel_keyblock(&cc);
  110. };
  111. /* Mocked functions */
  112. int vb2ex_read_resource(struct vb2_context *ctx,
  113. enum vb2_resource_index index,
  114. uint32_t offset,
  115. void *buf,
  116. uint32_t size)
  117. {
  118. uint8_t *rptr;
  119. uint32_t rsize;
  120. if (--mock_read_res_fail_on_call == 0)
  121. return VB2_ERROR_MOCK;
  122. switch(index) {
  123. case VB2_RES_GBB:
  124. rptr = (uint8_t *)&mock_gbb;
  125. rsize = sizeof(mock_gbb);
  126. break;
  127. case VB2_RES_KERNEL_VBLOCK:
  128. rptr = (uint8_t *)&mock_vblock;
  129. rsize = sizeof(mock_vblock);
  130. break;
  131. default:
  132. return VB2_ERROR_EX_READ_RESOURCE_INDEX;
  133. }
  134. if (offset > rsize || offset + size > rsize)
  135. return VB2_ERROR_EX_READ_RESOURCE_SIZE;
  136. memcpy(buf, rptr + offset, size);
  137. return VB2_SUCCESS;
  138. }
  139. int vb2_unpack_key_buffer(struct vb2_public_key *key,
  140. const uint8_t *buf,
  141. uint32_t size)
  142. {
  143. key->arrsize = 0;
  144. return mock_unpack_key_retval;
  145. }
  146. int vb2_verify_keyblock(struct vb2_keyblock *block,
  147. uint32_t size,
  148. const struct vb2_public_key *key,
  149. const struct vb2_workbuf *wb)
  150. {
  151. return mock_verify_keyblock_retval;
  152. }
  153. int vb2_verify_kernel_preamble(struct vb2_kernel_preamble *preamble,
  154. uint32_t size,
  155. const struct vb2_public_key *key,
  156. const struct vb2_workbuf *wb)
  157. {
  158. return mock_verify_preamble_retval;
  159. }
  160. /* Tests */
  161. static void verify_keyblock_hash_tests(void)
  162. {
  163. struct vb2_keyblock *kb = &mock_vblock.k.kb;
  164. /* Test successful call */
  165. reset_common_data(FOR_KEYBLOCK);
  166. TEST_SUCC(vb2_verify_keyblock_hash(kb, kb->keyblock_size, &wb),
  167. "Keyblock hash good");
  168. /* Sanity check keyblock */
  169. reset_common_data(FOR_KEYBLOCK);
  170. kb->magic[0] ^= 0xd0;
  171. TEST_EQ(vb2_verify_keyblock_hash(kb, kb->keyblock_size, &wb),
  172. VB2_ERROR_KEYBLOCK_MAGIC, "Keyblock sanity check");
  173. /*
  174. * Sanity check should be looking at the keyblock hash struct, not the
  175. * keyblock signature struct.
  176. */
  177. reset_common_data(FOR_KEYBLOCK);
  178. kb->keyblock_hash.data_size = sizeof(*kb) - 1;
  179. TEST_EQ(vb2_verify_keyblock_hash(kb, kb->keyblock_size, &wb),
  180. VB2_ERROR_KEYBLOCK_SIGNED_TOO_LITTLE,
  181. "Keyblock check hash sig");
  182. reset_common_data(FOR_KEYBLOCK);
  183. wb.size = VB2_SHA512_DIGEST_SIZE - 1;
  184. TEST_EQ(vb2_verify_keyblock_hash(kb, kb->keyblock_size, &wb),
  185. VB2_ERROR_VDATA_WORKBUF_DIGEST,
  186. "Keyblock check hash workbuf digest");
  187. reset_common_data(FOR_KEYBLOCK);
  188. wb.size = VB2_SHA512_DIGEST_SIZE +
  189. sizeof(struct vb2_digest_context) - 1;
  190. TEST_EQ(vb2_verify_keyblock_hash(kb, kb->keyblock_size, &wb),
  191. VB2_ERROR_VDATA_WORKBUF_HASHING,
  192. "Keyblock check hash workbuf hashing");
  193. reset_common_data(FOR_KEYBLOCK);
  194. mock_vblock.k.data_key_data[0] ^= 0xa0;
  195. TEST_EQ(vb2_verify_keyblock_hash(kb, kb->keyblock_size, &wb),
  196. VB2_ERROR_KEYBLOCK_SIG_INVALID,
  197. "Keyblock check hash invalid");
  198. }
  199. static void load_kernel_keyblock_tests(void)
  200. {
  201. struct vb2_keyblock *kb = &mock_vblock.k.kb;
  202. struct vb2_packed_key *k;
  203. int wb_used_before;
  204. /* Test successful call */
  205. reset_common_data(FOR_KEYBLOCK);
  206. wb_used_before = cc.workbuf_used;
  207. TEST_SUCC(vb2_load_kernel_keyblock(&cc), "Kernel keyblock good");
  208. TEST_NEQ(sd->flags & VB2_SD_FLAG_KERNEL_SIGNED, 0, " Kernel signed");
  209. TEST_EQ(sd->kernel_version, 0x20000, "keyblock version");
  210. TEST_EQ(sd->vblock_preamble_offset, sizeof(mock_vblock.k),
  211. "preamble offset");
  212. TEST_EQ(sd->workbuf_data_key_offset,
  213. (wb_used_before + (VB2_WORKBUF_ALIGN - 1)) &
  214. ~(VB2_WORKBUF_ALIGN - 1),
  215. "keyblock data key offset");
  216. TEST_EQ(cc.workbuf_used,
  217. sd->workbuf_data_key_offset + sd->workbuf_data_key_size,
  218. "workbuf used");
  219. /* Make sure data key was properly saved */
  220. k = (struct vb2_packed_key *)(cc.workbuf + sd->workbuf_data_key_offset);
  221. TEST_EQ(k->algorithm, 7, "data key algorithm");
  222. TEST_EQ(k->key_version, 2, "data key version");
  223. TEST_EQ(k->key_size, sizeof(mock_vblock.k.data_key_data),
  224. "data key size");
  225. TEST_EQ(memcmp(cc.workbuf + sd->workbuf_data_key_offset +
  226. k->key_offset, mock_vblock.k.data_key_data,
  227. sizeof(mock_vblock.k.data_key_data)),
  228. 0, "data key data");
  229. TEST_EQ(cc.workbuf_used,
  230. sd->workbuf_data_key_offset + sd->workbuf_data_key_size,
  231. "workbuf used after");
  232. /* Test failures */
  233. reset_common_data(FOR_KEYBLOCK);
  234. mock_unpack_key_retval = VB2_ERROR_MOCK;
  235. TEST_EQ(vb2_load_kernel_keyblock(&cc),
  236. VB2_ERROR_MOCK, "Kernel keyblock unpack key");
  237. reset_common_data(FOR_KEYBLOCK);
  238. cc.workbuf_used = cc.workbuf_size - (sizeof(*kb) - 1);
  239. TEST_EQ(vb2_load_kernel_keyblock(&cc),
  240. VB2_ERROR_KERNEL_KEYBLOCK_WORKBUF_HEADER,
  241. "Kernel keyblock workbuf header");
  242. reset_common_data(FOR_KEYBLOCK);
  243. mock_read_res_fail_on_call = 1;
  244. TEST_EQ(vb2_load_kernel_keyblock(&cc),
  245. VB2_ERROR_MOCK, "Kernel keyblock read header");
  246. reset_common_data(FOR_KEYBLOCK);
  247. cc.workbuf_used = cc.workbuf_size - (kb->keyblock_size - 1);
  248. TEST_EQ(vb2_load_kernel_keyblock(&cc),
  249. VB2_ERROR_KERNEL_KEYBLOCK_WORKBUF,
  250. "Kernel keyblock workbuf");
  251. reset_common_data(FOR_KEYBLOCK);
  252. mock_read_res_fail_on_call = 2;
  253. TEST_EQ(vb2_load_kernel_keyblock(&cc),
  254. VB2_ERROR_MOCK, "Kernel keyblock read");
  255. /* Normally, require signed keyblock */
  256. reset_common_data(FOR_KEYBLOCK);
  257. mock_verify_keyblock_retval = VB2_ERROR_MOCK;
  258. TEST_EQ(vb2_load_kernel_keyblock(&cc),
  259. VB2_ERROR_MOCK, "Verify keyblock");
  260. /* Not in dev mode */
  261. reset_common_data(FOR_KEYBLOCK);
  262. cc.flags |= VB2_CONTEXT_DEVELOPER_MODE;
  263. mock_verify_keyblock_retval = VB2_ERROR_MOCK;
  264. TEST_SUCC(vb2_load_kernel_keyblock(&cc), "Kernel keyblock hash good");
  265. TEST_EQ(sd->flags & VB2_SD_FLAG_KERNEL_SIGNED, 0, " Kernel signed");
  266. /* But we do in dev+rec mode */
  267. reset_common_data(FOR_KEYBLOCK);
  268. cc.flags |= VB2_CONTEXT_DEVELOPER_MODE | VB2_CONTEXT_RECOVERY_MODE;
  269. mock_verify_keyblock_retval = VB2_ERROR_MOCK;
  270. TEST_EQ(vb2_load_kernel_keyblock(&cc),
  271. VB2_ERROR_MOCK, "Kernel keyblock dev+rec");
  272. /* Test keyblock flags matching mode */
  273. reset_common_data(FOR_KEYBLOCK);
  274. kb->keyblock_flags &= ~VB2_KEY_BLOCK_FLAG_DEVELOPER_0;
  275. TEST_EQ(vb2_load_kernel_keyblock(&cc),
  276. VB2_ERROR_KERNEL_KEYBLOCK_DEV_FLAG,
  277. "Kernel keyblock dev only");
  278. reset_common_data(FOR_KEYBLOCK);
  279. kb->keyblock_flags &= ~VB2_KEY_BLOCK_FLAG_RECOVERY_0;
  280. TEST_EQ(vb2_load_kernel_keyblock(&cc),
  281. VB2_ERROR_KERNEL_KEYBLOCK_REC_FLAG,
  282. "Kernel keyblock rec only");
  283. reset_common_data(FOR_KEYBLOCK);
  284. kb->keyblock_flags &= ~VB2_KEY_BLOCK_FLAG_RECOVERY_1;
  285. cc.flags |= VB2_CONTEXT_RECOVERY_MODE;
  286. TEST_EQ(vb2_load_kernel_keyblock(&cc),
  287. VB2_ERROR_KERNEL_KEYBLOCK_REC_FLAG,
  288. "Kernel keyblock not rec");
  289. reset_common_data(FOR_KEYBLOCK);
  290. kb->keyblock_flags &= ~VB2_KEY_BLOCK_FLAG_DEVELOPER_0;
  291. kb->keyblock_flags &= ~VB2_KEY_BLOCK_FLAG_RECOVERY_0;
  292. cc.flags |= VB2_CONTEXT_RECOVERY_MODE;
  293. TEST_EQ(vb2_load_kernel_keyblock(&cc),
  294. VB2_ERROR_KERNEL_KEYBLOCK_DEV_FLAG,
  295. "Kernel keyblock rec but not dev+rec");
  296. reset_common_data(FOR_KEYBLOCK);
  297. kb->keyblock_flags &= ~VB2_KEY_BLOCK_FLAG_DEVELOPER_0;
  298. kb->keyblock_flags &= ~VB2_KEY_BLOCK_FLAG_RECOVERY_0;
  299. cc.flags |= VB2_CONTEXT_DEVELOPER_MODE | VB2_CONTEXT_RECOVERY_MODE;
  300. TEST_SUCC(vb2_load_kernel_keyblock(&cc),
  301. "Kernel keyblock flags dev+rec");
  302. /* System in dev mode ignores flags */
  303. reset_common_data(FOR_KEYBLOCK);
  304. cc.flags |= VB2_CONTEXT_DEVELOPER_MODE;
  305. kb->keyblock_flags = 0;
  306. TEST_SUCC(vb2_load_kernel_keyblock(&cc), "Kernel keyblock dev flags");
  307. /* Test rollback */
  308. reset_common_data(FOR_KEYBLOCK);
  309. kb->data_key.key_version = 0x10000;
  310. TEST_EQ(vb2_load_kernel_keyblock(&cc),
  311. VB2_ERROR_KERNEL_KEYBLOCK_VERSION_RANGE,
  312. "Kernel keyblock version range");
  313. reset_common_data(FOR_KEYBLOCK);
  314. kb->data_key.key_version = 1;
  315. TEST_EQ(vb2_load_kernel_keyblock(&cc),
  316. VB2_ERROR_KERNEL_KEYBLOCK_VERSION_ROLLBACK,
  317. "Kernel keyblock rollback");
  318. /* Rollback ok in developer mode */
  319. reset_common_data(FOR_KEYBLOCK);
  320. kb->data_key.key_version = 1;
  321. cc.flags |= VB2_CONTEXT_DEVELOPER_MODE;
  322. TEST_SUCC(vb2_load_kernel_keyblock(&cc),
  323. "Kernel keyblock rollback dev");
  324. /*
  325. * Recovery keyblocks aren't versioned (and even if they were, it
  326. * wouldn't be with the same version as a normal kernel).
  327. */
  328. reset_common_data(FOR_KEYBLOCK);
  329. kb->data_key.key_version = 1;
  330. cc.flags |= VB2_CONTEXT_RECOVERY_MODE;
  331. TEST_SUCC(vb2_load_kernel_keyblock(&cc),
  332. "Kernel keyblock rollback rec");
  333. }
  334. static void load_kernel_preamble_tests(void)
  335. {
  336. struct vb2_kernel_preamble *pre = &mock_vblock.p.pre;
  337. int wb_used_before;
  338. //uint32_t v;
  339. /* Test successful call */
  340. reset_common_data(FOR_PREAMBLE);
  341. wb_used_before = cc.workbuf_used;
  342. TEST_SUCC(vb2_load_kernel_preamble(&cc), "preamble good");
  343. TEST_EQ(sd->kernel_version, 0x20002, "combined version");
  344. TEST_EQ(sd->workbuf_preamble_offset,
  345. (wb_used_before + (VB2_WORKBUF_ALIGN - 1)) &
  346. ~(VB2_WORKBUF_ALIGN - 1),
  347. "preamble offset");
  348. TEST_EQ(sd->workbuf_preamble_size, pre->preamble_size, "preamble size");
  349. TEST_EQ(cc.workbuf_used,
  350. sd->workbuf_preamble_offset + sd->workbuf_preamble_size,
  351. "workbuf used");
  352. /* Expected failures */
  353. reset_common_data(FOR_PREAMBLE);
  354. sd->workbuf_data_key_size = 0;
  355. TEST_EQ(vb2_load_kernel_preamble(&cc),
  356. VB2_ERROR_KERNEL_PREAMBLE2_DATA_KEY,
  357. "preamble no data key");
  358. reset_common_data(FOR_PREAMBLE);
  359. mock_unpack_key_retval = VB2_ERROR_UNPACK_KEY_HASH_ALGORITHM;
  360. TEST_EQ(vb2_load_kernel_preamble(&cc),
  361. VB2_ERROR_UNPACK_KEY_HASH_ALGORITHM,
  362. "preamble unpack data key");
  363. reset_common_data(FOR_PREAMBLE);
  364. cc.workbuf_used = cc.workbuf_size -
  365. sizeof(struct vb2_kernel_preamble) + 8;
  366. TEST_EQ(vb2_load_kernel_preamble(&cc),
  367. VB2_ERROR_KERNEL_PREAMBLE2_WORKBUF_HEADER,
  368. "preamble not enough workbuf for header");
  369. reset_common_data(FOR_PREAMBLE);
  370. sd->vblock_preamble_offset = sizeof(mock_vblock);
  371. TEST_EQ(vb2_load_kernel_preamble(&cc),
  372. VB2_ERROR_EX_READ_RESOURCE_SIZE,
  373. "preamble read header");
  374. reset_common_data(FOR_PREAMBLE);
  375. cc.workbuf_used = cc.workbuf_size - sizeof(mock_vblock.p) + 8;
  376. TEST_EQ(vb2_load_kernel_preamble(&cc),
  377. VB2_ERROR_KERNEL_PREAMBLE2_WORKBUF,
  378. "preamble not enough workbuf");
  379. reset_common_data(FOR_PREAMBLE);
  380. pre->preamble_size = sizeof(mock_vblock);
  381. TEST_EQ(vb2_load_kernel_preamble(&cc),
  382. VB2_ERROR_EX_READ_RESOURCE_SIZE,
  383. "preamble read full");
  384. reset_common_data(FOR_PREAMBLE);
  385. mock_verify_preamble_retval = VB2_ERROR_MOCK;
  386. TEST_EQ(vb2_load_kernel_preamble(&cc),
  387. VB2_ERROR_MOCK,
  388. "preamble verify");
  389. reset_common_data(FOR_PREAMBLE);
  390. pre->kernel_version = 0x10000;
  391. TEST_EQ(vb2_load_kernel_preamble(&cc),
  392. VB2_ERROR_KERNEL_PREAMBLE_VERSION_RANGE,
  393. "preamble version range");
  394. reset_common_data(FOR_PREAMBLE);
  395. pre->kernel_version = 1;
  396. TEST_EQ(vb2_load_kernel_preamble(&cc),
  397. VB2_ERROR_KERNEL_PREAMBLE_VERSION_ROLLBACK,
  398. "preamble version rollback");
  399. }
  400. int main(int argc, char* argv[])
  401. {
  402. verify_keyblock_hash_tests();
  403. load_kernel_keyblock_tests();
  404. load_kernel_preamble_tests();
  405. return gTestSuccess ? 0 : 255;
  406. }