kernel.c 14 KB

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