misc.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /* Copyright (c) 2014 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. * Misc functions which need access to vb2_context but are not public APIs
  6. */
  7. #include "2sysincludes.h"
  8. #include "2api.h"
  9. #include "2common.h"
  10. #include "2misc.h"
  11. #include "2nvstorage.h"
  12. #include "2secdata.h"
  13. #include "2sha.h"
  14. #include "2rsa.h"
  15. #include "vb21_common.h"
  16. /**
  17. * Read an object with a common struct header from a verified boot resource.
  18. *
  19. * On success, an object buffer will be allocated in the work buffer, the
  20. * object will be stored into the buffer, and *buf_ptr will point to the
  21. * object.
  22. *
  23. * @param ctx Vboot context
  24. * @param index Resource index to read
  25. * @param offset Byte offset within resource to start at
  26. * @param buf_ptr Destination for object pointer
  27. * @return VB2_SUCCESS, or error code on error.
  28. */
  29. static int vb21_read_resource_object(struct vb2_context *ctx,
  30. enum vb2_resource_index index,
  31. uint32_t offset,
  32. struct vb2_workbuf *wb,
  33. void **buf_ptr)
  34. {
  35. struct vb21_struct_common c;
  36. void *buf;
  37. int rv;
  38. *buf_ptr = NULL;
  39. /* Read the common header */
  40. rv = vb2ex_read_resource(ctx, index, offset, &c, sizeof(c));
  41. if (rv)
  42. return rv;
  43. /* Allocate a buffer for the object, now that we know how big it is */
  44. buf = vb2_workbuf_alloc(wb, c.total_size);
  45. if (!buf)
  46. return VB2_ERROR_READ_RESOURCE_OBJECT_BUF;
  47. /* Read the object */
  48. rv = vb2ex_read_resource(ctx, index, offset, buf, c.total_size);
  49. if (rv) {
  50. vb2_workbuf_free(wb, c.total_size);
  51. return rv;
  52. }
  53. /* Save the pointer */
  54. *buf_ptr = buf;
  55. return VB2_SUCCESS;
  56. }
  57. int vb21_load_fw_keyblock(struct vb2_context *ctx)
  58. {
  59. struct vb2_shared_data *sd = vb2_get_sd(ctx);
  60. struct vb2_workbuf wb;
  61. uint8_t *key_data;
  62. uint32_t key_size;
  63. struct vb21_packed_key *packed_key;
  64. struct vb2_public_key root_key;
  65. struct vb21_keyblock *kb;
  66. int rv;
  67. vb2_workbuf_from_ctx(ctx, &wb);
  68. /* Read the root key */
  69. key_size = sd->gbb_rootkey_size;
  70. key_data = vb2_workbuf_alloc(&wb, key_size);
  71. if (!key_data)
  72. return VB2_ERROR_FW_KEYBLOCK_WORKBUF_ROOT_KEY;
  73. rv = vb2ex_read_resource(ctx, VB2_RES_GBB, sd->gbb_rootkey_offset,
  74. key_data, key_size);
  75. if (rv)
  76. return rv;
  77. /* Unpack the root key */
  78. rv = vb21_unpack_key(&root_key, key_data, key_size);
  79. if (rv)
  80. return rv;
  81. /*
  82. * Load the firmware keyblock common header into the work buffer after
  83. * the root key.
  84. */
  85. rv = vb21_read_resource_object(ctx, VB2_RES_FW_VBLOCK, 0, &wb,
  86. (void **)&kb);
  87. if (rv)
  88. return rv;
  89. /* Verify the keyblock */
  90. rv = vb21_verify_keyblock(kb, kb->c.total_size, &root_key, &wb);
  91. if (rv) {
  92. vb2_fail(ctx, VB2_RECOVERY_FW_KEYBLOCK, rv);
  93. return rv;
  94. }
  95. /* Preamble follows the keyblock in the vblock */
  96. sd->vblock_preamble_offset = kb->c.total_size;
  97. packed_key = (struct vb21_packed_key *)((uint8_t *)kb + kb->key_offset);
  98. /* Key version is the upper 16 bits of the composite firmware version */
  99. if (packed_key->key_version > 0xffff)
  100. rv = VB2_ERROR_FW_KEYBLOCK_VERSION_RANGE;
  101. if (!rv && packed_key->key_version < (sd->fw_version_secdata >> 16)) {
  102. if (sd->gbb_flags & VB2_GBB_FLAG_DISABLE_FW_ROLLBACK_CHECK)
  103. VB2_DEBUG("Ignoring FW key rollback due to GBB flag\n");
  104. else
  105. rv = VB2_ERROR_FW_KEYBLOCK_VERSION_ROLLBACK;
  106. }
  107. if (rv) {
  108. vb2_fail(ctx, VB2_RECOVERY_FW_KEY_ROLLBACK, rv);
  109. return rv;
  110. }
  111. sd->fw_version = packed_key->key_version << 16;
  112. /*
  113. * Save the data key in the work buffer. This overwrites the root key
  114. * we read above. That's ok, because now that we have the data key we
  115. * no longer need the root key.
  116. *
  117. * Use memmove() instead of memcpy(). In theory, the destination will
  118. * never overlap with the source because the root key is likely to be
  119. * at least as large as the data key, but there's no harm here in being
  120. * paranoid.
  121. */
  122. memmove(key_data, packed_key, packed_key->c.total_size);
  123. packed_key = (struct vb21_packed_key *)key_data;
  124. /* Save the packed key offset and size */
  125. sd->workbuf_data_key_offset = vb2_offset_of(ctx->workbuf, key_data);
  126. sd->workbuf_data_key_size = packed_key->c.total_size;
  127. /* Data key will persist in the workbuf after we return */
  128. ctx->workbuf_used = sd->workbuf_data_key_offset +
  129. sd->workbuf_data_key_size;
  130. return VB2_SUCCESS;
  131. }
  132. int vb21_load_fw_preamble(struct vb2_context *ctx)
  133. {
  134. struct vb2_shared_data *sd = vb2_get_sd(ctx);
  135. struct vb2_workbuf wb;
  136. uint8_t *key_data = ctx->workbuf + sd->workbuf_data_key_offset;
  137. uint32_t key_size = sd->workbuf_data_key_size;
  138. struct vb2_public_key data_key;
  139. /* Preamble goes in the next unused chunk of work buffer */
  140. struct vb21_fw_preamble *pre;
  141. int rv;
  142. vb2_workbuf_from_ctx(ctx, &wb);
  143. /* Unpack the firmware data key */
  144. if (!sd->workbuf_data_key_size)
  145. return VB2_ERROR_FW_PREAMBLE2_DATA_KEY;
  146. rv = vb21_unpack_key(&data_key, key_data, key_size);
  147. if (rv)
  148. return rv;
  149. /* Load the firmware preamble */
  150. rv = vb21_read_resource_object(ctx, VB2_RES_FW_VBLOCK,
  151. sd->vblock_preamble_offset, &wb,
  152. (void **)&pre);
  153. if (rv)
  154. return rv;
  155. /* Work buffer now contains the data subkey data and the preamble */
  156. /* Verify the preamble */
  157. rv = vb21_verify_fw_preamble(pre, pre->c.total_size, &data_key, &wb);
  158. if (rv) {
  159. vb2_fail(ctx, VB2_RECOVERY_FW_PREAMBLE, rv);
  160. return rv;
  161. }
  162. /* Move the preamble down now that the data key is no longer used */
  163. memmove(key_data, pre, pre->c.total_size);
  164. pre = (struct vb21_fw_preamble *)key_data;
  165. /* Data key is now gone */
  166. sd->workbuf_data_key_offset = sd->workbuf_data_key_size = 0;
  167. /*
  168. * Firmware version is the lower 16 bits of the composite firmware
  169. * version.
  170. */
  171. if (pre->fw_version > 0xffff)
  172. rv = VB2_ERROR_FW_PREAMBLE_VERSION_RANGE;
  173. /* Combine with the key version from vb2_load_fw_keyblock() */
  174. sd->fw_version |= pre->fw_version;
  175. if (!rv && sd->fw_version < sd->fw_version_secdata) {
  176. if (sd->gbb_flags & VB2_GBB_FLAG_DISABLE_FW_ROLLBACK_CHECK)
  177. VB2_DEBUG("Ignoring FW rollback due to GBB flag\n");
  178. else
  179. rv = VB2_ERROR_FW_PREAMBLE_VERSION_ROLLBACK;
  180. }
  181. if (rv) {
  182. vb2_fail(ctx, VB2_RECOVERY_FW_ROLLBACK, rv);
  183. return rv;
  184. }
  185. /*
  186. * If this is a newer version than in secure storage, and we
  187. * successfully booted the same slot last boot, roll forward the
  188. * version in secure storage.
  189. */
  190. if (sd->fw_version > sd->fw_version_secdata &&
  191. sd->last_fw_slot == sd->fw_slot &&
  192. sd->last_fw_result == VB2_FW_RESULT_SUCCESS) {
  193. sd->fw_version_secdata = sd->fw_version;
  194. rv = vb2_secdata_set(ctx, VB2_SECDATA_VERSIONS, sd->fw_version);
  195. if (rv)
  196. return rv;
  197. }
  198. /* Keep track of where we put the preamble */
  199. sd->workbuf_preamble_offset = vb2_offset_of(ctx->workbuf, pre);
  200. sd->workbuf_preamble_size = pre->c.total_size;
  201. /* Preamble will persist in work buffer after we return */
  202. ctx->workbuf_used = sd->workbuf_preamble_offset +
  203. sd->workbuf_preamble_size;
  204. return VB2_SUCCESS;
  205. }