avb_descriptor.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /*
  2. * Copyright (C) 2016 The Android Open Source Project
  3. *
  4. * Permission is hereby granted, free of charge, to any person
  5. * obtaining a copy of this software and associated documentation
  6. * files (the "Software"), to deal in the Software without
  7. * restriction, including without limitation the rights to use, copy,
  8. * modify, merge, publish, distribute, sublicense, and/or sell copies
  9. * of the Software, and to permit persons to whom the Software is
  10. * furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be
  13. * included in all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  16. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  18. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  19. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  20. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  21. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  22. * SOFTWARE.
  23. */
  24. #include "avb_descriptor.h"
  25. #include "avb_util.h"
  26. #include "avb_vbmeta_image.h"
  27. bool avb_descriptor_validate_and_byteswap(const AvbDescriptor* src,
  28. AvbDescriptor* dest) {
  29. dest->tag = avb_be64toh(src->tag);
  30. dest->num_bytes_following = avb_be64toh(src->num_bytes_following);
  31. if ((dest->num_bytes_following & 0x07) != 0) {
  32. avb_error("Descriptor size is not divisible by 8.\n");
  33. return false;
  34. }
  35. return true;
  36. }
  37. bool avb_descriptor_foreach(const uint8_t* image_data,
  38. size_t image_size,
  39. AvbDescriptorForeachFunc foreach_func,
  40. void* user_data) {
  41. const AvbVBMetaImageHeader* header = NULL;
  42. bool ret = false;
  43. const uint8_t* image_end;
  44. const uint8_t* desc_start;
  45. const uint8_t* desc_end;
  46. const uint8_t* p;
  47. if (image_data == NULL) {
  48. avb_error("image_data is NULL\n.");
  49. goto out;
  50. }
  51. if (foreach_func == NULL) {
  52. avb_error("foreach_func is NULL\n.");
  53. goto out;
  54. }
  55. if (image_size < sizeof(AvbVBMetaImageHeader)) {
  56. avb_error("Length is smaller than header.\n");
  57. goto out;
  58. }
  59. /* Ensure magic is correct. */
  60. if (avb_memcmp(image_data, AVB_MAGIC, AVB_MAGIC_LEN) != 0) {
  61. avb_error("Magic is incorrect.\n");
  62. goto out;
  63. }
  64. /* Careful, not byteswapped - also ensure it's aligned properly. */
  65. avb_assert_aligned(image_data);
  66. header = (const AvbVBMetaImageHeader*)image_data;
  67. image_end = image_data + image_size;
  68. desc_start = image_data + sizeof(AvbVBMetaImageHeader) +
  69. avb_be64toh(header->authentication_data_block_size) +
  70. avb_be64toh(header->descriptors_offset);
  71. desc_end = desc_start + avb_be64toh(header->descriptors_size);
  72. if (desc_start < image_data || desc_start > image_end ||
  73. desc_end < image_data || desc_end > image_end || desc_end < desc_start) {
  74. avb_error("Descriptors not inside passed-in data.\n");
  75. goto out;
  76. }
  77. for (p = desc_start; p < desc_end;) {
  78. const AvbDescriptor* dh = (const AvbDescriptor*)p;
  79. avb_assert_aligned(dh);
  80. uint64_t nb_following = avb_be64toh(dh->num_bytes_following);
  81. uint64_t nb_total = 0;
  82. if (!avb_safe_add(&nb_total, sizeof(AvbDescriptor), nb_following)) {
  83. avb_error("Invalid descriptor length.\n");
  84. goto out;
  85. }
  86. if ((nb_total & 7) != 0) {
  87. avb_error("Invalid descriptor length.\n");
  88. goto out;
  89. }
  90. if (nb_total + p < desc_start || nb_total + p > desc_end) {
  91. avb_error("Invalid data in descriptors array.\n");
  92. goto out;
  93. }
  94. if (foreach_func(dh, user_data) == 0) {
  95. goto out;
  96. }
  97. if (!avb_safe_add_to((uint64_t*)(&p), nb_total)) {
  98. avb_error("Invalid descriptor length.\n");
  99. goto out;
  100. }
  101. }
  102. ret = true;
  103. out:
  104. return ret;
  105. }
  106. static bool count_descriptors(const AvbDescriptor* descriptor,
  107. void* user_data) {
  108. size_t* num_descriptors = user_data;
  109. *num_descriptors += 1;
  110. return true;
  111. }
  112. typedef struct {
  113. size_t descriptor_number;
  114. const AvbDescriptor** descriptors;
  115. } SetDescriptorData;
  116. static bool set_descriptors(const AvbDescriptor* descriptor, void* user_data) {
  117. SetDescriptorData* data = user_data;
  118. data->descriptors[data->descriptor_number++] = descriptor;
  119. return true;
  120. }
  121. const AvbDescriptor** avb_descriptor_get_all(const uint8_t* image_data,
  122. size_t image_size,
  123. size_t* out_num_descriptors) {
  124. size_t num_descriptors = 0;
  125. SetDescriptorData data;
  126. avb_descriptor_foreach(
  127. image_data, image_size, count_descriptors, &num_descriptors);
  128. data.descriptor_number = 0;
  129. data.descriptors =
  130. avb_calloc(sizeof(const AvbDescriptor*) * (num_descriptors + 1));
  131. if (data.descriptors == NULL) {
  132. return NULL;
  133. }
  134. avb_descriptor_foreach(image_data, image_size, set_descriptors, &data);
  135. avb_assert(data.descriptor_number == num_descriptors);
  136. if (out_num_descriptors != NULL) {
  137. *out_num_descriptors = num_descriptors;
  138. }
  139. return data.descriptors;
  140. }