avb_slot_verify.c 64 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747
  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_slot_verify.h"
  25. #include "avb_chain_partition_descriptor.h"
  26. #include "avb_cmdline.h"
  27. #include "avb_footer.h"
  28. #include "avb_hash_descriptor.h"
  29. #include "avb_hashtree_descriptor.h"
  30. #include "avb_kernel_cmdline_descriptor.h"
  31. #include "avb_sha.h"
  32. #include "avb_util.h"
  33. #include "avb_vbmeta_image.h"
  34. #include "avb_version.h"
  35. /* Maximum number of partitions that can be loaded with avb_slot_verify(). */
  36. #define MAX_NUMBER_OF_LOADED_PARTITIONS 32
  37. /* Maximum number of vbmeta images that can be loaded with avb_slot_verify(). */
  38. #define MAX_NUMBER_OF_VBMETA_IMAGES 32
  39. /* Maximum size of a vbmeta image - 64 KiB. */
  40. #define VBMETA_MAX_SIZE (64 * 1024)
  41. static AvbSlotVerifyResult initialize_persistent_digest(
  42. AvbOps* ops,
  43. const char* part_name,
  44. const char* persistent_value_name,
  45. size_t digest_size,
  46. const uint8_t* initial_digest,
  47. uint8_t* out_digest);
  48. /* Helper function to see if we should continue with verification in
  49. * allow_verification_error=true mode if something goes wrong. See the
  50. * comments for the avb_slot_verify() function for more information.
  51. */
  52. static inline bool result_should_continue(AvbSlotVerifyResult result) {
  53. switch (result) {
  54. case AVB_SLOT_VERIFY_RESULT_ERROR_OOM:
  55. case AVB_SLOT_VERIFY_RESULT_ERROR_IO:
  56. case AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA:
  57. case AVB_SLOT_VERIFY_RESULT_ERROR_UNSUPPORTED_VERSION:
  58. case AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_ARGUMENT:
  59. return false;
  60. case AVB_SLOT_VERIFY_RESULT_OK:
  61. case AVB_SLOT_VERIFY_RESULT_ERROR_VERIFICATION:
  62. case AVB_SLOT_VERIFY_RESULT_ERROR_ROLLBACK_INDEX:
  63. case AVB_SLOT_VERIFY_RESULT_ERROR_PUBLIC_KEY_REJECTED:
  64. return true;
  65. }
  66. return false;
  67. }
  68. static AvbSlotVerifyResult load_full_partition(AvbOps* ops,
  69. const char* part_name,
  70. uint64_t image_size,
  71. uint8_t** out_image_buf,
  72. bool* out_image_preloaded) {
  73. size_t part_num_read;
  74. AvbIOResult io_ret;
  75. /* Make sure that we do not overwrite existing data. */
  76. avb_assert(*out_image_buf == NULL);
  77. avb_assert(!*out_image_preloaded);
  78. /* We are going to implicitly cast image_size from uint64_t to size_t in the
  79. * following code, so we need to make sure that the cast is safe. */
  80. if (image_size != (size_t)(image_size)) {
  81. avb_errorv(part_name, ": Partition size too large to load.\n", NULL);
  82. return AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA;
  83. }
  84. /* Try use a preloaded one. */
  85. if (ops->get_preloaded_partition != NULL) {
  86. io_ret = ops->get_preloaded_partition(
  87. ops, part_name, image_size, out_image_buf, &part_num_read);
  88. if (io_ret == AVB_IO_RESULT_ERROR_OOM) {
  89. return AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
  90. } else if (io_ret != AVB_IO_RESULT_OK) {
  91. avb_errorv(part_name, ": Error loading data from partition.\n", NULL);
  92. return AVB_SLOT_VERIFY_RESULT_ERROR_IO;
  93. }
  94. if (*out_image_buf != NULL) {
  95. if (part_num_read != image_size) {
  96. avb_errorv(part_name, ": Read incorrect number of bytes.\n", NULL);
  97. return AVB_SLOT_VERIFY_RESULT_ERROR_IO;
  98. }
  99. *out_image_preloaded = true;
  100. }
  101. }
  102. /* Allocate and copy the partition. */
  103. if (!*out_image_preloaded) {
  104. *out_image_buf = avb_malloc(image_size);
  105. if (*out_image_buf == NULL) {
  106. return AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
  107. }
  108. io_ret = ops->read_from_partition(ops,
  109. part_name,
  110. 0 /* offset */,
  111. image_size,
  112. *out_image_buf,
  113. &part_num_read);
  114. if (io_ret == AVB_IO_RESULT_ERROR_OOM) {
  115. return AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
  116. } else if (io_ret != AVB_IO_RESULT_OK) {
  117. avb_errorv(part_name, ": Error loading data from partition.\n", NULL);
  118. return AVB_SLOT_VERIFY_RESULT_ERROR_IO;
  119. }
  120. if (part_num_read != image_size) {
  121. avb_errorv(part_name, ": Read incorrect number of bytes.\n", NULL);
  122. return AVB_SLOT_VERIFY_RESULT_ERROR_IO;
  123. }
  124. }
  125. return AVB_SLOT_VERIFY_RESULT_OK;
  126. }
  127. /* Reads a persistent digest stored as a named persistent value corresponding to
  128. * the given |part_name|. The value is returned in |out_digest| which must point
  129. * to |expected_digest_size| bytes. If there is no digest stored for |part_name|
  130. * it can be initialized by providing a non-NULL |initial_digest| of length
  131. * |expected_digest_size|. This automatic initialization will only occur if the
  132. * device is currently locked. The |initial_digest| may be NULL.
  133. *
  134. * Returns AVB_SLOT_VERIFY_RESULT_OK on success, otherwise returns an
  135. * AVB_SLOT_VERIFY_RESULT_ERROR_* error code.
  136. *
  137. * If the value does not exist, is not supported, or is not populated, and
  138. * |initial_digest| is NULL, returns
  139. * AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA. If |expected_digest_size| does
  140. * not match the stored digest size, also returns
  141. * AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA.
  142. */
  143. static AvbSlotVerifyResult read_persistent_digest(AvbOps* ops,
  144. const char* part_name,
  145. size_t expected_digest_size,
  146. const uint8_t* initial_digest,
  147. uint8_t* out_digest) {
  148. char* persistent_value_name = NULL;
  149. AvbIOResult io_ret = AVB_IO_RESULT_OK;
  150. size_t stored_digest_size = 0;
  151. if (ops->read_persistent_value == NULL) {
  152. avb_errorv(part_name, ": Persistent values are not implemented.\n", NULL);
  153. return AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA;
  154. }
  155. persistent_value_name =
  156. avb_strdupv(AVB_NPV_PERSISTENT_DIGEST_PREFIX, part_name, NULL);
  157. if (persistent_value_name == NULL) {
  158. return AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
  159. }
  160. io_ret = ops->read_persistent_value(ops,
  161. persistent_value_name,
  162. expected_digest_size,
  163. out_digest,
  164. &stored_digest_size);
  165. // If no such named persistent value exists and an initial digest value was
  166. // given, initialize the named persistent value with the given digest. If
  167. // initialized successfully, this will recurse into this function but with a
  168. // NULL initial_digest.
  169. if (io_ret == AVB_IO_RESULT_ERROR_NO_SUCH_VALUE && initial_digest) {
  170. AvbSlotVerifyResult ret =
  171. initialize_persistent_digest(ops,
  172. part_name,
  173. persistent_value_name,
  174. expected_digest_size,
  175. initial_digest,
  176. out_digest);
  177. avb_free(persistent_value_name);
  178. return ret;
  179. }
  180. avb_free(persistent_value_name);
  181. if (io_ret == AVB_IO_RESULT_ERROR_OOM) {
  182. return AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
  183. } else if (io_ret == AVB_IO_RESULT_ERROR_NO_SUCH_VALUE) {
  184. // Treat a missing persistent value as a verification error, which is
  185. // ignoreable, rather than a metadata error which is not.
  186. avb_errorv(part_name, ": Persistent digest does not exist.\n", NULL);
  187. return AVB_SLOT_VERIFY_RESULT_ERROR_VERIFICATION;
  188. } else if (io_ret == AVB_IO_RESULT_ERROR_INVALID_VALUE_SIZE ||
  189. io_ret == AVB_IO_RESULT_ERROR_INSUFFICIENT_SPACE) {
  190. avb_errorv(
  191. part_name, ": Persistent digest is not of expected size.\n", NULL);
  192. return AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA;
  193. } else if (io_ret != AVB_IO_RESULT_OK) {
  194. avb_errorv(part_name, ": Error reading persistent digest.\n", NULL);
  195. return AVB_SLOT_VERIFY_RESULT_ERROR_IO;
  196. } else if (expected_digest_size != stored_digest_size) {
  197. avb_errorv(
  198. part_name, ": Persistent digest is not of expected size.\n", NULL);
  199. return AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA;
  200. }
  201. return AVB_SLOT_VERIFY_RESULT_OK;
  202. }
  203. static AvbSlotVerifyResult initialize_persistent_digest(
  204. AvbOps* ops,
  205. const char* part_name,
  206. const char* persistent_value_name,
  207. size_t digest_size,
  208. const uint8_t* initial_digest,
  209. uint8_t* out_digest) {
  210. AvbSlotVerifyResult ret;
  211. AvbIOResult io_ret = AVB_IO_RESULT_OK;
  212. bool is_device_unlocked = true;
  213. io_ret = ops->read_is_device_unlocked(ops, &is_device_unlocked);
  214. if (io_ret == AVB_IO_RESULT_ERROR_OOM) {
  215. return AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
  216. } else if (io_ret != AVB_IO_RESULT_OK) {
  217. avb_error("Error getting device lock state.\n");
  218. return AVB_SLOT_VERIFY_RESULT_ERROR_IO;
  219. }
  220. if (is_device_unlocked) {
  221. avb_debugv(part_name,
  222. ": Digest does not exist, device unlocked so not initializing "
  223. "digest.\n",
  224. NULL);
  225. return AVB_SLOT_VERIFY_RESULT_ERROR_VERIFICATION;
  226. }
  227. // Device locked; initialize digest with given initial value.
  228. avb_debugv(part_name,
  229. ": Digest does not exist, initializing persistent digest.\n",
  230. NULL);
  231. io_ret = ops->write_persistent_value(
  232. ops, persistent_value_name, digest_size, initial_digest);
  233. if (io_ret == AVB_IO_RESULT_ERROR_OOM) {
  234. return AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
  235. } else if (io_ret != AVB_IO_RESULT_OK) {
  236. avb_errorv(part_name, ": Error initializing persistent digest.\n", NULL);
  237. return AVB_SLOT_VERIFY_RESULT_ERROR_IO;
  238. }
  239. // To ensure that the digest value was written successfully - and avoid a
  240. // scenario where the digest is simply 'initialized' on every verify - recurse
  241. // into read_persistent_digest to read back the written value. The NULL
  242. // initial_digest ensures that this will not recurse again.
  243. ret = read_persistent_digest(ops, part_name, digest_size, NULL, out_digest);
  244. if (ret != AVB_SLOT_VERIFY_RESULT_OK) {
  245. avb_errorv(part_name,
  246. ": Reading back initialized persistent digest failed!\n",
  247. NULL);
  248. }
  249. return ret;
  250. }
  251. static AvbSlotVerifyResult load_and_verify_hash_partition(
  252. AvbOps* ops,
  253. const char* const* requested_partitions,
  254. const char* ab_suffix,
  255. bool allow_verification_error,
  256. const AvbDescriptor* descriptor,
  257. AvbSlotVerifyData* slot_data) {
  258. AvbHashDescriptor hash_desc;
  259. const uint8_t* desc_partition_name = NULL;
  260. const uint8_t* desc_salt;
  261. const uint8_t* desc_digest;
  262. char part_name[AVB_PART_NAME_MAX_SIZE];
  263. AvbSlotVerifyResult ret;
  264. AvbIOResult io_ret;
  265. uint8_t* image_buf = NULL;
  266. bool image_preloaded = false;
  267. uint8_t* digest;
  268. size_t digest_len;
  269. const char* found;
  270. uint64_t image_size;
  271. size_t expected_digest_len = 0;
  272. uint8_t expected_digest_buf[AVB_SHA512_DIGEST_SIZE];
  273. const uint8_t* expected_digest = NULL;
  274. if (!avb_hash_descriptor_validate_and_byteswap(
  275. (const AvbHashDescriptor*)descriptor, &hash_desc)) {
  276. ret = AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA;
  277. goto out;
  278. }
  279. desc_partition_name =
  280. ((const uint8_t*)descriptor) + sizeof(AvbHashDescriptor);
  281. desc_salt = desc_partition_name + hash_desc.partition_name_len;
  282. desc_digest = desc_salt + hash_desc.salt_len;
  283. if (!avb_validate_utf8(desc_partition_name, hash_desc.partition_name_len)) {
  284. avb_error("Partition name is not valid UTF-8.\n");
  285. ret = AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA;
  286. goto out;
  287. }
  288. /* Don't bother loading or validating unless the partition was
  289. * requested in the first place.
  290. */
  291. found = avb_strv_find_str(requested_partitions,
  292. (const char*)desc_partition_name,
  293. hash_desc.partition_name_len);
  294. if (found == NULL) {
  295. ret = AVB_SLOT_VERIFY_RESULT_OK;
  296. goto out;
  297. }
  298. if ((hash_desc.flags & AVB_HASH_DESCRIPTOR_FLAGS_DO_NOT_USE_AB) != 0) {
  299. /* No ab_suffix, just copy the partition name as is. */
  300. if (hash_desc.partition_name_len >= AVB_PART_NAME_MAX_SIZE) {
  301. avb_error("Partition name does not fit.\n");
  302. ret = AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA;
  303. goto out;
  304. }
  305. avb_memcpy(part_name, desc_partition_name, hash_desc.partition_name_len);
  306. part_name[hash_desc.partition_name_len] = '\0';
  307. } else if (hash_desc.digest_len == 0 && avb_strlen(ab_suffix) != 0) {
  308. /* No ab_suffix allowed for partitions without a digest in the descriptor
  309. * because these partitions hold data unique to this device and are not
  310. * updated using an A/B scheme.
  311. */
  312. avb_error("Cannot use A/B with a persistent digest.\n");
  313. ret = AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA;
  314. goto out;
  315. } else {
  316. /* Add ab_suffix to the partition name. */
  317. if (!avb_str_concat(part_name,
  318. sizeof part_name,
  319. (const char*)desc_partition_name,
  320. hash_desc.partition_name_len,
  321. ab_suffix,
  322. avb_strlen(ab_suffix))) {
  323. avb_error("Partition name and suffix does not fit.\n");
  324. ret = AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA;
  325. goto out;
  326. }
  327. }
  328. /* If we're allowing verification errors then hash_desc.image_size
  329. * may no longer match what's in the partition... so in this case
  330. * just load the entire partition.
  331. *
  332. * For example, this can happen if a developer does 'fastboot flash
  333. * boot /path/to/new/and/bigger/boot.img'. We want this to work
  334. * since it's such a common workflow.
  335. */
  336. image_size = hash_desc.image_size;
  337. if (allow_verification_error) {
  338. io_ret = ops->get_size_of_partition(ops, part_name, &image_size);
  339. if (io_ret == AVB_IO_RESULT_ERROR_OOM) {
  340. ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
  341. goto out;
  342. } else if (io_ret != AVB_IO_RESULT_OK) {
  343. avb_errorv(part_name, ": Error determining partition size.\n", NULL);
  344. ret = AVB_SLOT_VERIFY_RESULT_ERROR_IO;
  345. goto out;
  346. }
  347. avb_debugv(part_name, ": Loading entire partition.\n", NULL);
  348. }
  349. ret = load_full_partition(
  350. ops, part_name, image_size, &image_buf, &image_preloaded);
  351. if (ret != AVB_SLOT_VERIFY_RESULT_OK) {
  352. goto out;
  353. }
  354. // Although only one of the type might be used, we have to defined the
  355. // structure here so that they would live outside the 'if/else' scope to be
  356. // used later.
  357. AvbSHA256Ctx sha256_ctx;
  358. AvbSHA512Ctx sha512_ctx;
  359. size_t image_size_to_hash = hash_desc.image_size;
  360. // If we allow verification error and the whole partition is smaller than
  361. // image size in hash descriptor, we just hash the whole partition.
  362. if (image_size_to_hash > image_size) {
  363. image_size_to_hash = image_size;
  364. }
  365. if (avb_strcmp((const char*)hash_desc.hash_algorithm, "sha256") == 0) {
  366. avb_sha256_init(&sha256_ctx);
  367. avb_sha256_update(&sha256_ctx, desc_salt, hash_desc.salt_len);
  368. avb_sha256_update(&sha256_ctx, image_buf, image_size_to_hash);
  369. digest = avb_sha256_final(&sha256_ctx);
  370. digest_len = AVB_SHA256_DIGEST_SIZE;
  371. } else if (avb_strcmp((const char*)hash_desc.hash_algorithm, "sha512") == 0) {
  372. avb_sha512_init(&sha512_ctx);
  373. avb_sha512_update(&sha512_ctx, desc_salt, hash_desc.salt_len);
  374. avb_sha512_update(&sha512_ctx, image_buf, image_size_to_hash);
  375. digest = avb_sha512_final(&sha512_ctx);
  376. digest_len = AVB_SHA512_DIGEST_SIZE;
  377. } else {
  378. avb_errorv(part_name, ": Unsupported hash algorithm.\n", NULL);
  379. ret = AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA;
  380. goto out;
  381. }
  382. if (hash_desc.digest_len == 0) {
  383. /* Expect a match to a persistent digest. */
  384. avb_debugv(part_name, ": No digest, using persistent digest.\n", NULL);
  385. expected_digest_len = digest_len;
  386. expected_digest = expected_digest_buf;
  387. avb_assert(expected_digest_len <= sizeof(expected_digest_buf));
  388. /* Pass |digest| as the |initial_digest| so devices not yet initialized get
  389. * initialized to the current partition digest.
  390. */
  391. ret = read_persistent_digest(
  392. ops, part_name, digest_len, digest, expected_digest_buf);
  393. if (ret != AVB_SLOT_VERIFY_RESULT_OK) {
  394. goto out;
  395. }
  396. } else {
  397. /* Expect a match to the digest in the descriptor. */
  398. expected_digest_len = hash_desc.digest_len;
  399. expected_digest = desc_digest;
  400. }
  401. if (digest_len != expected_digest_len) {
  402. avb_errorv(
  403. part_name, ": Digest in descriptor not of expected size.\n", NULL);
  404. ret = AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA;
  405. goto out;
  406. }
  407. if (avb_safe_memcmp(digest, expected_digest, digest_len) != 0) {
  408. avb_errorv(part_name,
  409. ": Hash of data does not match digest in descriptor.\n",
  410. NULL);
  411. ret = AVB_SLOT_VERIFY_RESULT_ERROR_VERIFICATION;
  412. goto out;
  413. }
  414. ret = AVB_SLOT_VERIFY_RESULT_OK;
  415. out:
  416. /* If it worked and something was loaded, copy to slot_data. */
  417. if ((ret == AVB_SLOT_VERIFY_RESULT_OK || result_should_continue(ret)) &&
  418. image_buf != NULL) {
  419. AvbPartitionData* loaded_partition;
  420. if (slot_data->num_loaded_partitions == MAX_NUMBER_OF_LOADED_PARTITIONS) {
  421. avb_errorv(part_name, ": Too many loaded partitions.\n", NULL);
  422. ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
  423. goto fail;
  424. }
  425. loaded_partition =
  426. &slot_data->loaded_partitions[slot_data->num_loaded_partitions++];
  427. loaded_partition->partition_name = avb_strdup(found);
  428. loaded_partition->data_size = image_size;
  429. loaded_partition->data = image_buf;
  430. loaded_partition->preloaded = image_preloaded;
  431. image_buf = NULL;
  432. }
  433. fail:
  434. if (image_buf != NULL && !image_preloaded) {
  435. avb_free(image_buf);
  436. }
  437. return ret;
  438. }
  439. static AvbSlotVerifyResult load_requested_partitions(
  440. AvbOps* ops,
  441. const char* const* requested_partitions,
  442. const char* ab_suffix,
  443. AvbSlotVerifyData* slot_data) {
  444. AvbSlotVerifyResult ret;
  445. uint8_t* image_buf = NULL;
  446. bool image_preloaded = false;
  447. size_t n;
  448. for (n = 0; requested_partitions[n] != NULL; n++) {
  449. char part_name[AVB_PART_NAME_MAX_SIZE];
  450. AvbIOResult io_ret;
  451. uint64_t image_size;
  452. AvbPartitionData* loaded_partition;
  453. if (!avb_str_concat(part_name,
  454. sizeof part_name,
  455. requested_partitions[n],
  456. avb_strlen(requested_partitions[n]),
  457. ab_suffix,
  458. avb_strlen(ab_suffix))) {
  459. avb_error("Partition name and suffix does not fit.\n");
  460. ret = AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA;
  461. goto out;
  462. }
  463. io_ret = ops->get_size_of_partition(ops, part_name, &image_size);
  464. if (io_ret == AVB_IO_RESULT_ERROR_OOM) {
  465. ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
  466. goto out;
  467. } else if (io_ret != AVB_IO_RESULT_OK) {
  468. avb_errorv(part_name, ": Error determining partition size.\n", NULL);
  469. ret = AVB_SLOT_VERIFY_RESULT_ERROR_IO;
  470. goto out;
  471. }
  472. avb_debugv(part_name, ": Loading entire partition.\n", NULL);
  473. ret = load_full_partition(
  474. ops, part_name, image_size, &image_buf, &image_preloaded);
  475. if (ret != AVB_SLOT_VERIFY_RESULT_OK) {
  476. goto out;
  477. }
  478. /* Move to slot_data. */
  479. if (slot_data->num_loaded_partitions == MAX_NUMBER_OF_LOADED_PARTITIONS) {
  480. avb_errorv(part_name, ": Too many loaded partitions.\n", NULL);
  481. ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
  482. goto out;
  483. }
  484. loaded_partition =
  485. &slot_data->loaded_partitions[slot_data->num_loaded_partitions++];
  486. loaded_partition->partition_name = avb_strdup(requested_partitions[n]);
  487. if (loaded_partition->partition_name == NULL) {
  488. ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
  489. goto out;
  490. }
  491. loaded_partition->data_size = image_size;
  492. loaded_partition->data = image_buf; /* Transferring the owner. */
  493. loaded_partition->preloaded = image_preloaded;
  494. image_buf = NULL;
  495. image_preloaded = false;
  496. }
  497. ret = AVB_SLOT_VERIFY_RESULT_OK;
  498. out:
  499. /* Free the current buffer if any. */
  500. if (image_buf != NULL && !image_preloaded) {
  501. avb_free(image_buf);
  502. }
  503. /* Buffers that are already saved in slot_data will be handled by the caller
  504. * even on failure. */
  505. return ret;
  506. }
  507. static AvbSlotVerifyResult load_and_verify_vbmeta(
  508. AvbOps* ops,
  509. const char* const* requested_partitions,
  510. const char* ab_suffix,
  511. AvbSlotVerifyFlags flags,
  512. bool allow_verification_error,
  513. AvbVBMetaImageFlags toplevel_vbmeta_flags,
  514. int rollback_index_location,
  515. const char* partition_name,
  516. size_t partition_name_len,
  517. const uint8_t* expected_public_key,
  518. size_t expected_public_key_length,
  519. AvbSlotVerifyData* slot_data,
  520. AvbAlgorithmType* out_algorithm_type,
  521. AvbCmdlineSubstList* out_additional_cmdline_subst) {
  522. char full_partition_name[AVB_PART_NAME_MAX_SIZE];
  523. AvbSlotVerifyResult ret;
  524. AvbIOResult io_ret;
  525. uint64_t vbmeta_offset;
  526. size_t vbmeta_size;
  527. uint8_t* vbmeta_buf = NULL;
  528. size_t vbmeta_num_read;
  529. AvbVBMetaVerifyResult vbmeta_ret;
  530. const uint8_t* pk_data;
  531. size_t pk_len;
  532. AvbVBMetaImageHeader vbmeta_header;
  533. uint64_t stored_rollback_index;
  534. const AvbDescriptor** descriptors = NULL;
  535. size_t num_descriptors;
  536. size_t n;
  537. bool is_main_vbmeta;
  538. bool look_for_vbmeta_footer;
  539. AvbVBMetaData* vbmeta_image_data = NULL;
  540. ret = AVB_SLOT_VERIFY_RESULT_OK;
  541. avb_assert(slot_data != NULL);
  542. /* Since we allow top-level vbmeta in 'boot', use
  543. * rollback_index_location to determine whether we're the main
  544. * vbmeta struct.
  545. */
  546. is_main_vbmeta = false;
  547. if (rollback_index_location == 0) {
  548. if ((flags & AVB_SLOT_VERIFY_FLAGS_NO_VBMETA_PARTITION) == 0) {
  549. is_main_vbmeta = true;
  550. }
  551. }
  552. /* Don't use footers for vbmeta partitions ('vbmeta' or
  553. * 'vbmeta_<partition_name>').
  554. */
  555. look_for_vbmeta_footer = true;
  556. if (avb_strncmp(partition_name, "vbmeta", avb_strlen("vbmeta")) == 0) {
  557. look_for_vbmeta_footer = false;
  558. }
  559. if (!avb_validate_utf8((const uint8_t*)partition_name, partition_name_len)) {
  560. avb_error("Partition name is not valid UTF-8.\n");
  561. ret = AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA;
  562. goto out;
  563. }
  564. /* Construct full partition name e.g. system_a. */
  565. if (!avb_str_concat(full_partition_name,
  566. sizeof full_partition_name,
  567. partition_name,
  568. partition_name_len,
  569. ab_suffix,
  570. avb_strlen(ab_suffix))) {
  571. avb_error("Partition name and suffix does not fit.\n");
  572. ret = AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA;
  573. goto out;
  574. }
  575. /* If we're loading from the main vbmeta partition, the vbmeta struct is in
  576. * the beginning. Otherwise we may have to locate it via a footer... if no
  577. * footer is found, we look in the beginning to support e.g. vbmeta_<org>
  578. * partitions holding data for e.g. super partitions (b/80195851 for
  579. * rationale).
  580. */
  581. vbmeta_offset = 0;
  582. vbmeta_size = VBMETA_MAX_SIZE;
  583. if (look_for_vbmeta_footer) {
  584. uint8_t footer_buf[AVB_FOOTER_SIZE];
  585. size_t footer_num_read;
  586. AvbFooter footer;
  587. io_ret = ops->read_from_partition(ops,
  588. full_partition_name,
  589. -AVB_FOOTER_SIZE,
  590. AVB_FOOTER_SIZE,
  591. footer_buf,
  592. &footer_num_read);
  593. if (io_ret == AVB_IO_RESULT_ERROR_OOM) {
  594. ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
  595. goto out;
  596. } else if (io_ret != AVB_IO_RESULT_OK) {
  597. avb_errorv(full_partition_name, ": Error loading footer.\n", NULL);
  598. ret = AVB_SLOT_VERIFY_RESULT_ERROR_IO;
  599. goto out;
  600. }
  601. avb_assert(footer_num_read == AVB_FOOTER_SIZE);
  602. if (!avb_footer_validate_and_byteswap((const AvbFooter*)footer_buf,
  603. &footer)) {
  604. avb_debugv(full_partition_name, ": No footer detected.\n", NULL);
  605. } else {
  606. /* Basic footer sanity check since the data is untrusted. */
  607. if (footer.vbmeta_size > VBMETA_MAX_SIZE) {
  608. avb_errorv(
  609. full_partition_name, ": Invalid vbmeta size in footer.\n", NULL);
  610. } else {
  611. vbmeta_offset = (uint64_t)footer.vbmeta_offset;
  612. vbmeta_size = footer.vbmeta_size;
  613. }
  614. }
  615. }
  616. vbmeta_buf = avb_malloc(vbmeta_size);
  617. if (vbmeta_buf == NULL) {
  618. ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
  619. goto out;
  620. }
  621. if (vbmeta_offset != 0) {
  622. avb_debugv("Loading vbmeta struct in footer from partition '",
  623. full_partition_name,
  624. "'.\n",
  625. NULL);
  626. } else {
  627. avb_debugv("Loading vbmeta struct from partition '",
  628. full_partition_name,
  629. "'.\n",
  630. NULL);
  631. }
  632. io_ret = ops->read_from_partition(ops,
  633. full_partition_name,
  634. vbmeta_offset,
  635. vbmeta_size,
  636. vbmeta_buf,
  637. &vbmeta_num_read);
  638. if (io_ret == AVB_IO_RESULT_ERROR_OOM) {
  639. ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
  640. goto out;
  641. } else if (io_ret != AVB_IO_RESULT_OK) {
  642. /* If we're looking for 'vbmeta' but there is no such partition,
  643. * go try to get it from the boot partition instead.
  644. */
  645. if (is_main_vbmeta && io_ret == AVB_IO_RESULT_ERROR_NO_SUCH_PARTITION &&
  646. !look_for_vbmeta_footer) {
  647. avb_debugv(full_partition_name,
  648. ": No such partition. Trying 'boot' instead.\n",
  649. NULL);
  650. ret = load_and_verify_vbmeta(ops,
  651. requested_partitions,
  652. ab_suffix,
  653. flags,
  654. allow_verification_error,
  655. 0 /* toplevel_vbmeta_flags */,
  656. 0 /* rollback_index_location */,
  657. "boot",
  658. avb_strlen("boot"),
  659. NULL /* expected_public_key */,
  660. 0 /* expected_public_key_length */,
  661. slot_data,
  662. out_algorithm_type,
  663. out_additional_cmdline_subst);
  664. goto out;
  665. } else {
  666. avb_errorv(full_partition_name, ": Error loading vbmeta data.\n", NULL);
  667. ret = AVB_SLOT_VERIFY_RESULT_ERROR_IO;
  668. goto out;
  669. }
  670. }
  671. avb_assert(vbmeta_num_read <= vbmeta_size);
  672. /* Check if the image is properly signed and get the public key used
  673. * to sign the image.
  674. */
  675. vbmeta_ret =
  676. avb_vbmeta_image_verify(vbmeta_buf, vbmeta_num_read, &pk_data, &pk_len);
  677. switch (vbmeta_ret) {
  678. case AVB_VBMETA_VERIFY_RESULT_OK:
  679. avb_assert(pk_data != NULL && pk_len > 0);
  680. break;
  681. case AVB_VBMETA_VERIFY_RESULT_OK_NOT_SIGNED:
  682. case AVB_VBMETA_VERIFY_RESULT_HASH_MISMATCH:
  683. case AVB_VBMETA_VERIFY_RESULT_SIGNATURE_MISMATCH:
  684. ret = AVB_SLOT_VERIFY_RESULT_ERROR_VERIFICATION;
  685. avb_errorv(full_partition_name,
  686. ": Error verifying vbmeta image: ",
  687. avb_vbmeta_verify_result_to_string(vbmeta_ret),
  688. "\n",
  689. NULL);
  690. if (!allow_verification_error) {
  691. goto out;
  692. }
  693. break;
  694. case AVB_VBMETA_VERIFY_RESULT_INVALID_VBMETA_HEADER:
  695. /* No way to continue this case. */
  696. ret = AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA;
  697. avb_errorv(full_partition_name,
  698. ": Error verifying vbmeta image: invalid vbmeta header\n",
  699. NULL);
  700. goto out;
  701. case AVB_VBMETA_VERIFY_RESULT_UNSUPPORTED_VERSION:
  702. /* No way to continue this case. */
  703. ret = AVB_SLOT_VERIFY_RESULT_ERROR_UNSUPPORTED_VERSION;
  704. avb_errorv(full_partition_name,
  705. ": Error verifying vbmeta image: unsupported AVB version\n",
  706. NULL);
  707. goto out;
  708. }
  709. /* Byteswap the header. */
  710. avb_vbmeta_image_header_to_host_byte_order((AvbVBMetaImageHeader*)vbmeta_buf,
  711. &vbmeta_header);
  712. /* If we're the toplevel, assign flags so they'll be passed down. */
  713. if (is_main_vbmeta) {
  714. toplevel_vbmeta_flags = (AvbVBMetaImageFlags)vbmeta_header.flags;
  715. } else {
  716. if (vbmeta_header.flags != 0) {
  717. ret = AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA;
  718. avb_errorv(full_partition_name,
  719. ": chained vbmeta image has non-zero flags\n",
  720. NULL);
  721. goto out;
  722. }
  723. }
  724. uint32_t rollback_index_location_to_use = rollback_index_location;
  725. /* Check if key used to make signature matches what is expected. */
  726. if (pk_data != NULL) {
  727. if (expected_public_key != NULL) {
  728. avb_assert(!is_main_vbmeta);
  729. if (expected_public_key_length != pk_len ||
  730. avb_safe_memcmp(expected_public_key, pk_data, pk_len) != 0) {
  731. avb_errorv(full_partition_name,
  732. ": Public key used to sign data does not match key in chain "
  733. "partition descriptor.\n",
  734. NULL);
  735. ret = AVB_SLOT_VERIFY_RESULT_ERROR_PUBLIC_KEY_REJECTED;
  736. if (!allow_verification_error) {
  737. goto out;
  738. }
  739. }
  740. } else {
  741. bool key_is_trusted = false;
  742. const uint8_t* pk_metadata = NULL;
  743. size_t pk_metadata_len = 0;
  744. if (vbmeta_header.public_key_metadata_size > 0) {
  745. pk_metadata = vbmeta_buf + sizeof(AvbVBMetaImageHeader) +
  746. vbmeta_header.authentication_data_block_size +
  747. vbmeta_header.public_key_metadata_offset;
  748. pk_metadata_len = vbmeta_header.public_key_metadata_size;
  749. }
  750. // If we're not using a vbmeta partition, need to use another AvbOps...
  751. if (flags & AVB_SLOT_VERIFY_FLAGS_NO_VBMETA_PARTITION) {
  752. io_ret = ops->validate_public_key_for_partition(
  753. ops,
  754. full_partition_name,
  755. pk_data,
  756. pk_len,
  757. pk_metadata,
  758. pk_metadata_len,
  759. &key_is_trusted,
  760. &rollback_index_location_to_use);
  761. } else {
  762. avb_assert(is_main_vbmeta);
  763. io_ret = ops->validate_vbmeta_public_key(ops,
  764. pk_data,
  765. pk_len,
  766. pk_metadata,
  767. pk_metadata_len,
  768. &key_is_trusted);
  769. }
  770. if (io_ret == AVB_IO_RESULT_ERROR_OOM) {
  771. ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
  772. goto out;
  773. } else if (io_ret != AVB_IO_RESULT_OK) {
  774. avb_errorv(full_partition_name,
  775. ": Error while checking public key used to sign data.\n",
  776. NULL);
  777. ret = AVB_SLOT_VERIFY_RESULT_ERROR_IO;
  778. goto out;
  779. }
  780. if (!key_is_trusted) {
  781. avb_errorv(full_partition_name,
  782. ": Public key used to sign data rejected.\n",
  783. NULL);
  784. ret = AVB_SLOT_VERIFY_RESULT_ERROR_PUBLIC_KEY_REJECTED;
  785. if (!allow_verification_error) {
  786. goto out;
  787. }
  788. }
  789. }
  790. }
  791. /* Check rollback index. */
  792. io_ret = ops->read_rollback_index(
  793. ops, rollback_index_location_to_use, &stored_rollback_index);
  794. if (io_ret == AVB_IO_RESULT_ERROR_OOM) {
  795. ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
  796. goto out;
  797. } else if (io_ret != AVB_IO_RESULT_OK) {
  798. avb_errorv(full_partition_name,
  799. ": Error getting rollback index for location.\n",
  800. NULL);
  801. ret = AVB_SLOT_VERIFY_RESULT_ERROR_IO;
  802. goto out;
  803. }
  804. if (vbmeta_header.rollback_index < stored_rollback_index) {
  805. avb_errorv(
  806. full_partition_name,
  807. ": Image rollback index is less than the stored rollback index.\n",
  808. NULL);
  809. ret = AVB_SLOT_VERIFY_RESULT_ERROR_ROLLBACK_INDEX;
  810. if (!allow_verification_error) {
  811. goto out;
  812. }
  813. }
  814. /* Copy vbmeta to vbmeta_images before recursing. */
  815. if (is_main_vbmeta) {
  816. avb_assert(slot_data->num_vbmeta_images == 0);
  817. } else {
  818. if (!(flags & AVB_SLOT_VERIFY_FLAGS_NO_VBMETA_PARTITION)) {
  819. avb_assert(slot_data->num_vbmeta_images > 0);
  820. }
  821. }
  822. if (slot_data->num_vbmeta_images == MAX_NUMBER_OF_VBMETA_IMAGES) {
  823. avb_errorv(full_partition_name, ": Too many vbmeta images.\n", NULL);
  824. ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
  825. goto out;
  826. }
  827. vbmeta_image_data = &slot_data->vbmeta_images[slot_data->num_vbmeta_images++];
  828. vbmeta_image_data->partition_name = avb_strdup(partition_name);
  829. vbmeta_image_data->vbmeta_data = vbmeta_buf;
  830. /* Note that |vbmeta_buf| is actually |vbmeta_num_read| bytes long
  831. * and this includes data past the end of the image. Pass the
  832. * actual size of the vbmeta image. Also, no need to use
  833. * avb_safe_add() since the header has already been verified.
  834. */
  835. vbmeta_image_data->vbmeta_size =
  836. sizeof(AvbVBMetaImageHeader) +
  837. vbmeta_header.authentication_data_block_size +
  838. vbmeta_header.auxiliary_data_block_size;
  839. vbmeta_image_data->verify_result = vbmeta_ret;
  840. /* If verification has been disabled by setting a bit in the image,
  841. * we're done... except that we need to load the entirety of the
  842. * requested partitions.
  843. */
  844. if (vbmeta_header.flags & AVB_VBMETA_IMAGE_FLAGS_VERIFICATION_DISABLED) {
  845. AvbSlotVerifyResult sub_ret;
  846. avb_debugv(
  847. full_partition_name, ": VERIFICATION_DISABLED bit is set.\n", NULL);
  848. /* If load_requested_partitions() fail it is always a fatal
  849. * failure (e.g. ERROR_INVALID_ARGUMENT, ERROR_OOM, etc.) rather
  850. * than recoverable (e.g. one where result_should_continue()
  851. * returns true) and we want to convey that error.
  852. */
  853. sub_ret = load_requested_partitions(
  854. ops, requested_partitions, ab_suffix, slot_data);
  855. if (sub_ret != AVB_SLOT_VERIFY_RESULT_OK) {
  856. ret = sub_ret;
  857. }
  858. goto out;
  859. }
  860. /* Now go through all descriptors and take the appropriate action:
  861. *
  862. * - hash descriptor: Load data from partition, calculate hash, and
  863. * checks that it matches what's in the hash descriptor.
  864. *
  865. * - hashtree descriptor: Do nothing since verification happens
  866. * on-the-fly from within the OS. (Unless the descriptor uses a
  867. * persistent digest, in which case we need to find it).
  868. *
  869. * - chained partition descriptor: Load the footer, load the vbmeta
  870. * image, verify vbmeta image (includes rollback checks, hash
  871. * checks, bail on chained partitions).
  872. */
  873. descriptors =
  874. avb_descriptor_get_all(vbmeta_buf, vbmeta_num_read, &num_descriptors);
  875. for (n = 0; n < num_descriptors; n++) {
  876. AvbDescriptor desc;
  877. if (!avb_descriptor_validate_and_byteswap(descriptors[n], &desc)) {
  878. avb_errorv(full_partition_name, ": Descriptor is invalid.\n", NULL);
  879. ret = AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA;
  880. goto out;
  881. }
  882. switch (desc.tag) {
  883. case AVB_DESCRIPTOR_TAG_HASH: {
  884. AvbSlotVerifyResult sub_ret;
  885. sub_ret = load_and_verify_hash_partition(ops,
  886. requested_partitions,
  887. ab_suffix,
  888. allow_verification_error,
  889. descriptors[n],
  890. slot_data);
  891. if (sub_ret != AVB_SLOT_VERIFY_RESULT_OK) {
  892. ret = sub_ret;
  893. if (!allow_verification_error || !result_should_continue(ret)) {
  894. goto out;
  895. }
  896. }
  897. } break;
  898. case AVB_DESCRIPTOR_TAG_CHAIN_PARTITION: {
  899. AvbSlotVerifyResult sub_ret;
  900. AvbChainPartitionDescriptor chain_desc;
  901. const uint8_t* chain_partition_name;
  902. const uint8_t* chain_public_key;
  903. /* Only allow CHAIN_PARTITION descriptors in the main vbmeta image. */
  904. if (!is_main_vbmeta) {
  905. avb_errorv(full_partition_name,
  906. ": Encountered chain descriptor not in main image.\n",
  907. NULL);
  908. ret = AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA;
  909. goto out;
  910. }
  911. if (!avb_chain_partition_descriptor_validate_and_byteswap(
  912. (AvbChainPartitionDescriptor*)descriptors[n], &chain_desc)) {
  913. avb_errorv(full_partition_name,
  914. ": Chain partition descriptor is invalid.\n",
  915. NULL);
  916. ret = AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA;
  917. goto out;
  918. }
  919. if (chain_desc.rollback_index_location == 0) {
  920. avb_errorv(full_partition_name,
  921. ": Chain partition has invalid "
  922. "rollback_index_location field.\n",
  923. NULL);
  924. ret = AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA;
  925. goto out;
  926. }
  927. chain_partition_name = ((const uint8_t*)descriptors[n]) +
  928. sizeof(AvbChainPartitionDescriptor);
  929. chain_public_key = chain_partition_name + chain_desc.partition_name_len;
  930. sub_ret =
  931. load_and_verify_vbmeta(ops,
  932. requested_partitions,
  933. ab_suffix,
  934. flags,
  935. allow_verification_error,
  936. toplevel_vbmeta_flags,
  937. chain_desc.rollback_index_location,
  938. (const char*)chain_partition_name,
  939. chain_desc.partition_name_len,
  940. chain_public_key,
  941. chain_desc.public_key_len,
  942. slot_data,
  943. NULL, /* out_algorithm_type */
  944. NULL /* out_additional_cmdline_subst */);
  945. if (sub_ret != AVB_SLOT_VERIFY_RESULT_OK) {
  946. ret = sub_ret;
  947. if (!result_should_continue(ret)) {
  948. goto out;
  949. }
  950. }
  951. } break;
  952. case AVB_DESCRIPTOR_TAG_KERNEL_CMDLINE: {
  953. const uint8_t* kernel_cmdline;
  954. AvbKernelCmdlineDescriptor kernel_cmdline_desc;
  955. bool apply_cmdline;
  956. if (!avb_kernel_cmdline_descriptor_validate_and_byteswap(
  957. (AvbKernelCmdlineDescriptor*)descriptors[n],
  958. &kernel_cmdline_desc)) {
  959. avb_errorv(full_partition_name,
  960. ": Kernel cmdline descriptor is invalid.\n",
  961. NULL);
  962. ret = AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA;
  963. goto out;
  964. }
  965. kernel_cmdline = ((const uint8_t*)descriptors[n]) +
  966. sizeof(AvbKernelCmdlineDescriptor);
  967. if (!avb_validate_utf8(kernel_cmdline,
  968. kernel_cmdline_desc.kernel_cmdline_length)) {
  969. avb_errorv(full_partition_name,
  970. ": Kernel cmdline is not valid UTF-8.\n",
  971. NULL);
  972. ret = AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA;
  973. goto out;
  974. }
  975. /* Compare the flags for top-level VBMeta struct with flags in
  976. * the command-line descriptor so command-line snippets only
  977. * intended for a certain mode (dm-verity enabled/disabled)
  978. * are skipped if applicable.
  979. */
  980. apply_cmdline = true;
  981. if (toplevel_vbmeta_flags & AVB_VBMETA_IMAGE_FLAGS_HASHTREE_DISABLED) {
  982. if (kernel_cmdline_desc.flags &
  983. AVB_KERNEL_CMDLINE_FLAGS_USE_ONLY_IF_HASHTREE_NOT_DISABLED) {
  984. apply_cmdline = false;
  985. }
  986. } else {
  987. if (kernel_cmdline_desc.flags &
  988. AVB_KERNEL_CMDLINE_FLAGS_USE_ONLY_IF_HASHTREE_DISABLED) {
  989. apply_cmdline = false;
  990. }
  991. }
  992. if (apply_cmdline) {
  993. if (slot_data->cmdline == NULL) {
  994. slot_data->cmdline =
  995. avb_calloc(kernel_cmdline_desc.kernel_cmdline_length + 1);
  996. if (slot_data->cmdline == NULL) {
  997. ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
  998. goto out;
  999. }
  1000. avb_memcpy(slot_data->cmdline,
  1001. kernel_cmdline,
  1002. kernel_cmdline_desc.kernel_cmdline_length);
  1003. } else {
  1004. /* new cmdline is: <existing_cmdline> + ' ' + <newcmdline> + '\0' */
  1005. size_t orig_size = avb_strlen(slot_data->cmdline);
  1006. size_t new_size =
  1007. orig_size + 1 + kernel_cmdline_desc.kernel_cmdline_length + 1;
  1008. char* new_cmdline = avb_calloc(new_size);
  1009. if (new_cmdline == NULL) {
  1010. ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
  1011. goto out;
  1012. }
  1013. avb_memcpy(new_cmdline, slot_data->cmdline, orig_size);
  1014. new_cmdline[orig_size] = ' ';
  1015. avb_memcpy(new_cmdline + orig_size + 1,
  1016. kernel_cmdline,
  1017. kernel_cmdline_desc.kernel_cmdline_length);
  1018. avb_free(slot_data->cmdline);
  1019. slot_data->cmdline = new_cmdline;
  1020. }
  1021. }
  1022. } break;
  1023. case AVB_DESCRIPTOR_TAG_HASHTREE: {
  1024. AvbHashtreeDescriptor hashtree_desc;
  1025. if (!avb_hashtree_descriptor_validate_and_byteswap(
  1026. (AvbHashtreeDescriptor*)descriptors[n], &hashtree_desc)) {
  1027. avb_errorv(
  1028. full_partition_name, ": Hashtree descriptor is invalid.\n", NULL);
  1029. ret = AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA;
  1030. goto out;
  1031. }
  1032. /* We only need to continue when there is no digest in the descriptor.
  1033. * This is because the only processing here is to find the digest and
  1034. * make it available on the kernel command line.
  1035. */
  1036. if (hashtree_desc.root_digest_len == 0) {
  1037. char part_name[AVB_PART_NAME_MAX_SIZE];
  1038. size_t digest_len = 0;
  1039. uint8_t digest_buf[AVB_SHA512_DIGEST_SIZE];
  1040. const uint8_t* desc_partition_name =
  1041. ((const uint8_t*)descriptors[n]) + sizeof(AvbHashtreeDescriptor);
  1042. if (!avb_validate_utf8(desc_partition_name,
  1043. hashtree_desc.partition_name_len)) {
  1044. avb_error("Partition name is not valid UTF-8.\n");
  1045. ret = AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA;
  1046. goto out;
  1047. }
  1048. /* No ab_suffix for partitions without a digest in the descriptor
  1049. * because these partitions hold data unique to this device and are
  1050. * not updated using an A/B scheme.
  1051. */
  1052. if ((hashtree_desc.flags &
  1053. AVB_HASHTREE_DESCRIPTOR_FLAGS_DO_NOT_USE_AB) == 0 &&
  1054. avb_strlen(ab_suffix) != 0) {
  1055. avb_error("Cannot use A/B with a persistent root digest.\n");
  1056. ret = AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA;
  1057. goto out;
  1058. }
  1059. if (hashtree_desc.partition_name_len >= AVB_PART_NAME_MAX_SIZE) {
  1060. avb_error("Partition name does not fit.\n");
  1061. ret = AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA;
  1062. goto out;
  1063. }
  1064. avb_memcpy(
  1065. part_name, desc_partition_name, hashtree_desc.partition_name_len);
  1066. part_name[hashtree_desc.partition_name_len] = '\0';
  1067. /* Determine the expected digest size from the hash algorithm. */
  1068. if (avb_strcmp((const char*)hashtree_desc.hash_algorithm, "sha1") ==
  1069. 0) {
  1070. digest_len = AVB_SHA1_DIGEST_SIZE;
  1071. } else if (avb_strcmp((const char*)hashtree_desc.hash_algorithm,
  1072. "sha256") == 0) {
  1073. digest_len = AVB_SHA256_DIGEST_SIZE;
  1074. } else if (avb_strcmp((const char*)hashtree_desc.hash_algorithm,
  1075. "sha512") == 0) {
  1076. digest_len = AVB_SHA512_DIGEST_SIZE;
  1077. } else {
  1078. avb_errorv(part_name, ": Unsupported hash algorithm.\n", NULL);
  1079. ret = AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA;
  1080. goto out;
  1081. }
  1082. ret = read_persistent_digest(ops,
  1083. part_name,
  1084. digest_len,
  1085. NULL /* initial_digest */,
  1086. digest_buf);
  1087. if (ret != AVB_SLOT_VERIFY_RESULT_OK) {
  1088. goto out;
  1089. }
  1090. if (out_additional_cmdline_subst) {
  1091. ret =
  1092. avb_add_root_digest_substitution(part_name,
  1093. digest_buf,
  1094. digest_len,
  1095. out_additional_cmdline_subst);
  1096. if (ret != AVB_SLOT_VERIFY_RESULT_OK) {
  1097. goto out;
  1098. }
  1099. }
  1100. }
  1101. } break;
  1102. case AVB_DESCRIPTOR_TAG_PROPERTY:
  1103. /* Do nothing. */
  1104. break;
  1105. }
  1106. }
  1107. if (rollback_index_location < 0 ||
  1108. rollback_index_location >= AVB_MAX_NUMBER_OF_ROLLBACK_INDEX_LOCATIONS) {
  1109. avb_errorv(
  1110. full_partition_name, ": Invalid rollback_index_location.\n", NULL);
  1111. ret = AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA;
  1112. goto out;
  1113. }
  1114. slot_data->rollback_indexes[rollback_index_location] =
  1115. vbmeta_header.rollback_index;
  1116. if (out_algorithm_type != NULL) {
  1117. *out_algorithm_type = (AvbAlgorithmType)vbmeta_header.algorithm_type;
  1118. }
  1119. out:
  1120. /* If |vbmeta_image_data| isn't NULL it means that it adopted
  1121. * |vbmeta_buf| so in that case don't free it here.
  1122. */
  1123. if (vbmeta_image_data == NULL) {
  1124. if (vbmeta_buf != NULL) {
  1125. avb_free(vbmeta_buf);
  1126. }
  1127. }
  1128. if (descriptors != NULL) {
  1129. avb_free(descriptors);
  1130. }
  1131. return ret;
  1132. }
  1133. static AvbIOResult avb_manage_hashtree_error_mode(
  1134. AvbOps* ops,
  1135. AvbSlotVerifyFlags flags,
  1136. AvbSlotVerifyData* data,
  1137. AvbHashtreeErrorMode* out_hashtree_error_mode) {
  1138. AvbHashtreeErrorMode ret = AVB_HASHTREE_ERROR_MODE_RESTART;
  1139. AvbIOResult io_ret = AVB_IO_RESULT_OK;
  1140. uint8_t vbmeta_digest_sha256[AVB_SHA256_DIGEST_SIZE];
  1141. uint8_t stored_vbmeta_digest_sha256[AVB_SHA256_DIGEST_SIZE];
  1142. size_t num_bytes_read;
  1143. avb_assert(out_hashtree_error_mode != NULL);
  1144. avb_assert(ops->read_persistent_value != NULL);
  1145. avb_assert(ops->write_persistent_value != NULL);
  1146. // If we're rebooting because of dm-verity corruption, make a note of
  1147. // the vbmeta hash so we can stay in 'eio' mode until things change.
  1148. if (flags & AVB_SLOT_VERIFY_FLAGS_RESTART_CAUSED_BY_HASHTREE_CORRUPTION) {
  1149. avb_debug(
  1150. "Rebooting because of dm-verity corruption - "
  1151. "recording OS instance and using 'eio' mode.\n");
  1152. avb_slot_verify_data_calculate_vbmeta_digest(
  1153. data, AVB_DIGEST_TYPE_SHA256, vbmeta_digest_sha256);
  1154. io_ret = ops->write_persistent_value(ops,
  1155. AVB_NPV_MANAGED_VERITY_MODE,
  1156. AVB_SHA256_DIGEST_SIZE,
  1157. vbmeta_digest_sha256);
  1158. if (io_ret != AVB_IO_RESULT_OK) {
  1159. avb_error("Error writing to " AVB_NPV_MANAGED_VERITY_MODE ".\n");
  1160. goto out;
  1161. }
  1162. ret = AVB_HASHTREE_ERROR_MODE_EIO;
  1163. io_ret = AVB_IO_RESULT_OK;
  1164. goto out;
  1165. }
  1166. // See if we're in 'eio' mode.
  1167. io_ret = ops->read_persistent_value(ops,
  1168. AVB_NPV_MANAGED_VERITY_MODE,
  1169. AVB_SHA256_DIGEST_SIZE,
  1170. stored_vbmeta_digest_sha256,
  1171. &num_bytes_read);
  1172. if (io_ret == AVB_IO_RESULT_ERROR_NO_SUCH_VALUE ||
  1173. (io_ret == AVB_IO_RESULT_OK && num_bytes_read == 0)) {
  1174. // This is the usual case ('eio' mode not set).
  1175. avb_debug("No dm-verity corruption - using in 'restart' mode.\n");
  1176. ret = AVB_HASHTREE_ERROR_MODE_RESTART;
  1177. io_ret = AVB_IO_RESULT_OK;
  1178. goto out;
  1179. } else if (io_ret != AVB_IO_RESULT_OK) {
  1180. avb_error("Error reading from " AVB_NPV_MANAGED_VERITY_MODE ".\n");
  1181. goto out;
  1182. }
  1183. if (num_bytes_read != AVB_SHA256_DIGEST_SIZE) {
  1184. avb_error(
  1185. "Unexpected number of bytes read from " AVB_NPV_MANAGED_VERITY_MODE
  1186. ".\n");
  1187. io_ret = AVB_IO_RESULT_ERROR_IO;
  1188. goto out;
  1189. }
  1190. // OK, so we're currently in 'eio' mode and the vbmeta digest of the OS
  1191. // that caused this is in |stored_vbmeta_digest_sha256| ... now see if
  1192. // the OS we're dealing with now is the same.
  1193. avb_slot_verify_data_calculate_vbmeta_digest(
  1194. data, AVB_DIGEST_TYPE_SHA256, vbmeta_digest_sha256);
  1195. if (avb_memcmp(vbmeta_digest_sha256,
  1196. stored_vbmeta_digest_sha256,
  1197. AVB_SHA256_DIGEST_SIZE) == 0) {
  1198. // It's the same so we're still in 'eio' mode.
  1199. avb_debug("Same OS instance detected - staying in 'eio' mode.\n");
  1200. ret = AVB_HASHTREE_ERROR_MODE_EIO;
  1201. io_ret = AVB_IO_RESULT_OK;
  1202. } else {
  1203. // It did change!
  1204. avb_debug(
  1205. "New OS instance detected - changing from 'eio' to 'restart' mode.\n");
  1206. io_ret =
  1207. ops->write_persistent_value(ops,
  1208. AVB_NPV_MANAGED_VERITY_MODE,
  1209. 0, // This clears the persistent property.
  1210. vbmeta_digest_sha256);
  1211. if (io_ret != AVB_IO_RESULT_OK) {
  1212. avb_error("Error clearing " AVB_NPV_MANAGED_VERITY_MODE ".\n");
  1213. goto out;
  1214. }
  1215. ret = AVB_HASHTREE_ERROR_MODE_RESTART;
  1216. io_ret = AVB_IO_RESULT_OK;
  1217. }
  1218. out:
  1219. *out_hashtree_error_mode = ret;
  1220. return io_ret;
  1221. }
  1222. static bool has_system_partition(AvbOps* ops, const char* ab_suffix) {
  1223. char part_name[AVB_PART_NAME_MAX_SIZE];
  1224. char* system_part_name = "system";
  1225. char guid_buf[37];
  1226. AvbIOResult io_ret;
  1227. if (!avb_str_concat(part_name,
  1228. sizeof part_name,
  1229. system_part_name,
  1230. avb_strlen(system_part_name),
  1231. ab_suffix,
  1232. avb_strlen(ab_suffix))) {
  1233. avb_error("System partition name and suffix does not fit.\n");
  1234. return false;
  1235. }
  1236. io_ret = ops->get_unique_guid_for_partition(
  1237. ops, part_name, guid_buf, sizeof guid_buf);
  1238. if (io_ret == AVB_IO_RESULT_ERROR_NO_SUCH_PARTITION) {
  1239. avb_debug("No system partition.\n");
  1240. return false;
  1241. } else if (io_ret != AVB_IO_RESULT_OK) {
  1242. avb_error("Error getting unique GUID for system partition.\n");
  1243. return false;
  1244. }
  1245. return true;
  1246. }
  1247. AvbSlotVerifyResult avb_slot_verify(AvbOps* ops,
  1248. const char* const* requested_partitions,
  1249. const char* ab_suffix,
  1250. AvbSlotVerifyFlags flags,
  1251. AvbHashtreeErrorMode hashtree_error_mode,
  1252. AvbSlotVerifyData** out_data) {
  1253. AvbSlotVerifyResult ret;
  1254. AvbSlotVerifyData* slot_data = NULL;
  1255. AvbAlgorithmType algorithm_type = AVB_ALGORITHM_TYPE_NONE;
  1256. bool using_boot_for_vbmeta = false;
  1257. AvbVBMetaImageHeader toplevel_vbmeta;
  1258. int i;
  1259. bool allow_verification_error =
  1260. (flags & AVB_SLOT_VERIFY_FLAGS_ALLOW_VERIFICATION_ERROR);
  1261. AvbCmdlineSubstList* additional_cmdline_subst = NULL;
  1262. /* Fail early if we're missing the AvbOps needed for slot verification. */
  1263. avb_assert(ops->read_is_device_unlocked != NULL);
  1264. avb_assert(ops->read_from_partition != NULL);
  1265. avb_assert(ops->get_size_of_partition != NULL);
  1266. avb_assert(ops->read_rollback_index != NULL);
  1267. avb_assert(ops->get_unique_guid_for_partition != NULL);
  1268. if (out_data != NULL) {
  1269. *out_data = NULL;
  1270. }
  1271. /* Allowing dm-verity errors defeats the purpose of verified boot so
  1272. * only allow this if set up to allow verification errors
  1273. * (e.g. typically only UNLOCKED mode).
  1274. */
  1275. if (hashtree_error_mode == AVB_HASHTREE_ERROR_MODE_LOGGING &&
  1276. !allow_verification_error) {
  1277. ret = AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_ARGUMENT;
  1278. goto fail;
  1279. }
  1280. /* Make sure passed-in AvbOps support persistent values if
  1281. * asking for libavb to manage verity state.
  1282. */
  1283. if (hashtree_error_mode == AVB_HASHTREE_ERROR_MODE_MANAGED_RESTART_AND_EIO) {
  1284. if (ops->read_persistent_value == NULL ||
  1285. ops->write_persistent_value == NULL) {
  1286. avb_error(
  1287. "Persistent values required for "
  1288. "AVB_HASHTREE_ERROR_MODE_MANAGED_RESTART_AND_EIO "
  1289. "but are not implemented in given AvbOps.\n");
  1290. ret = AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_ARGUMENT;
  1291. goto fail;
  1292. }
  1293. }
  1294. /* Make sure passed-in AvbOps support verifying public keys and getting
  1295. * rollback index location if not using a vbmeta partition.
  1296. */
  1297. if (flags & AVB_SLOT_VERIFY_FLAGS_NO_VBMETA_PARTITION) {
  1298. if (ops->validate_public_key_for_partition == NULL) {
  1299. avb_error(
  1300. "AVB_SLOT_VERIFY_FLAGS_NO_VBMETA_PARTITION was passed but the "
  1301. "validate_public_key_for_partition() operation isn't implemented.\n");
  1302. ret = AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_ARGUMENT;
  1303. goto fail;
  1304. }
  1305. } else {
  1306. avb_assert(ops->validate_vbmeta_public_key != NULL);
  1307. }
  1308. slot_data = avb_calloc(sizeof(AvbSlotVerifyData));
  1309. if (slot_data == NULL) {
  1310. ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
  1311. goto fail;
  1312. }
  1313. slot_data->vbmeta_images =
  1314. avb_calloc(sizeof(AvbVBMetaData) * MAX_NUMBER_OF_VBMETA_IMAGES);
  1315. if (slot_data->vbmeta_images == NULL) {
  1316. ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
  1317. goto fail;
  1318. }
  1319. slot_data->loaded_partitions =
  1320. avb_calloc(sizeof(AvbPartitionData) * MAX_NUMBER_OF_LOADED_PARTITIONS);
  1321. if (slot_data->loaded_partitions == NULL) {
  1322. ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
  1323. goto fail;
  1324. }
  1325. for (i = 0; i < AVB_MAX_NUMBER_OF_ROLLBACK_INDEX_LOCATIONS; i++)
  1326. slot_data->rollback_indexes[i] = AVB_VER_INITIAL_VALUE;
  1327. additional_cmdline_subst = avb_new_cmdline_subst_list();
  1328. if (additional_cmdline_subst == NULL) {
  1329. ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
  1330. goto fail;
  1331. }
  1332. if (flags & AVB_SLOT_VERIFY_FLAGS_NO_VBMETA_PARTITION) {
  1333. if (requested_partitions == NULL || requested_partitions[0] == NULL) {
  1334. avb_fatal(
  1335. "Requested partitions cannot be empty when using "
  1336. "AVB_SLOT_VERIFY_FLAGS_NO_VBMETA_PARTITION");
  1337. ret = AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_ARGUMENT;
  1338. goto fail;
  1339. }
  1340. /* No vbmeta partition, go through each of the requested partitions... */
  1341. for (size_t n = 0; requested_partitions[n] != NULL; n++) {
  1342. ret = load_and_verify_vbmeta(ops,
  1343. requested_partitions,
  1344. ab_suffix,
  1345. flags,
  1346. allow_verification_error,
  1347. 0 /* toplevel_vbmeta_flags */,
  1348. 0 /* rollback_index_location */,
  1349. requested_partitions[n],
  1350. avb_strlen(requested_partitions[n]),
  1351. NULL /* expected_public_key */,
  1352. 0 /* expected_public_key_length */,
  1353. slot_data,
  1354. &algorithm_type,
  1355. additional_cmdline_subst);
  1356. if (!allow_verification_error && ret != AVB_SLOT_VERIFY_RESULT_OK) {
  1357. goto fail;
  1358. }
  1359. }
  1360. } else {
  1361. /* Usual path, load "vbmeta"... */
  1362. ret = load_and_verify_vbmeta(ops,
  1363. requested_partitions,
  1364. ab_suffix,
  1365. flags,
  1366. allow_verification_error,
  1367. 0 /* toplevel_vbmeta_flags */,
  1368. 0 /* rollback_index_location */,
  1369. "vbmeta",
  1370. avb_strlen("vbmeta"),
  1371. NULL /* expected_public_key */,
  1372. 0 /* expected_public_key_length */,
  1373. slot_data,
  1374. &algorithm_type,
  1375. additional_cmdline_subst);
  1376. if (!allow_verification_error && ret != AVB_SLOT_VERIFY_RESULT_OK) {
  1377. goto fail;
  1378. }
  1379. }
  1380. if (!result_should_continue(ret)) {
  1381. goto fail;
  1382. }
  1383. /* If things check out, mangle the kernel command-line as needed. */
  1384. if (!(flags & AVB_SLOT_VERIFY_FLAGS_NO_VBMETA_PARTITION)) {
  1385. if (avb_strcmp(slot_data->vbmeta_images[0].partition_name, "vbmeta") != 0) {
  1386. avb_assert(
  1387. avb_strcmp(slot_data->vbmeta_images[0].partition_name, "boot") == 0);
  1388. using_boot_for_vbmeta = true;
  1389. }
  1390. }
  1391. /* Byteswap top-level vbmeta header since we'll need it below. */
  1392. avb_vbmeta_image_header_to_host_byte_order(
  1393. (const AvbVBMetaImageHeader*)slot_data->vbmeta_images[0].vbmeta_data,
  1394. &toplevel_vbmeta);
  1395. /* Fill in |ab_suffix| field. */
  1396. slot_data->ab_suffix = avb_strdup(ab_suffix);
  1397. if (slot_data->ab_suffix == NULL) {
  1398. ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
  1399. goto fail;
  1400. }
  1401. /* If verification is disabled, we are done ... we specifically
  1402. * don't want to add any androidboot.* options since verification
  1403. * is disabled.
  1404. */
  1405. if (toplevel_vbmeta.flags & AVB_VBMETA_IMAGE_FLAGS_VERIFICATION_DISABLED) {
  1406. /* Since verification is disabled we didn't process any
  1407. * descriptors and thus there's no cmdline... so set root= such
  1408. * that the system partition is mounted.
  1409. */
  1410. avb_assert(slot_data->cmdline == NULL);
  1411. // Devices with dynamic partitions won't have system partition.
  1412. // Instead, it has a large super partition to accommodate *.img files.
  1413. // See b/119551429 for details.
  1414. if (has_system_partition(ops, ab_suffix)) {
  1415. slot_data->cmdline =
  1416. avb_strdup("root=PARTUUID=$(ANDROID_SYSTEM_PARTUUID)");
  1417. } else {
  1418. // The |cmdline| field should be a NUL-terminated string.
  1419. slot_data->cmdline = avb_strdup("");
  1420. }
  1421. if (slot_data->cmdline == NULL) {
  1422. ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
  1423. goto fail;
  1424. }
  1425. } else {
  1426. /* If requested, manage dm-verity mode... */
  1427. AvbHashtreeErrorMode resolved_hashtree_error_mode = hashtree_error_mode;
  1428. if (hashtree_error_mode ==
  1429. AVB_HASHTREE_ERROR_MODE_MANAGED_RESTART_AND_EIO) {
  1430. AvbIOResult io_ret;
  1431. io_ret = avb_manage_hashtree_error_mode(
  1432. ops, flags, slot_data, &resolved_hashtree_error_mode);
  1433. if (io_ret != AVB_IO_RESULT_OK) {
  1434. ret = AVB_SLOT_VERIFY_RESULT_ERROR_IO;
  1435. if (io_ret == AVB_IO_RESULT_ERROR_OOM) {
  1436. ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
  1437. }
  1438. goto fail;
  1439. }
  1440. }
  1441. slot_data->resolved_hashtree_error_mode = resolved_hashtree_error_mode;
  1442. /* Add options... */
  1443. AvbSlotVerifyResult sub_ret;
  1444. sub_ret = avb_append_options(ops,
  1445. flags,
  1446. slot_data,
  1447. &toplevel_vbmeta,
  1448. algorithm_type,
  1449. hashtree_error_mode,
  1450. resolved_hashtree_error_mode);
  1451. if (sub_ret != AVB_SLOT_VERIFY_RESULT_OK) {
  1452. ret = sub_ret;
  1453. goto fail;
  1454. }
  1455. }
  1456. /* Substitute $(ANDROID_SYSTEM_PARTUUID) and friends. */
  1457. if (slot_data->cmdline != NULL && avb_strlen(slot_data->cmdline) != 0) {
  1458. char* new_cmdline;
  1459. new_cmdline = avb_sub_cmdline(ops,
  1460. slot_data->cmdline,
  1461. ab_suffix,
  1462. using_boot_for_vbmeta,
  1463. additional_cmdline_subst);
  1464. if (new_cmdline != slot_data->cmdline) {
  1465. if (new_cmdline == NULL) {
  1466. ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
  1467. goto fail;
  1468. }
  1469. avb_free(slot_data->cmdline);
  1470. slot_data->cmdline = new_cmdline;
  1471. }
  1472. }
  1473. if (out_data != NULL) {
  1474. *out_data = slot_data;
  1475. } else {
  1476. avb_slot_verify_data_free(slot_data);
  1477. }
  1478. avb_free_cmdline_subst_list(additional_cmdline_subst);
  1479. additional_cmdline_subst = NULL;
  1480. if (!allow_verification_error) {
  1481. avb_assert(ret == AVB_SLOT_VERIFY_RESULT_OK);
  1482. }
  1483. return ret;
  1484. fail:
  1485. if (slot_data != NULL) {
  1486. avb_slot_verify_data_free(slot_data);
  1487. }
  1488. if (additional_cmdline_subst != NULL) {
  1489. avb_free_cmdline_subst_list(additional_cmdline_subst);
  1490. }
  1491. return ret;
  1492. }
  1493. void avb_slot_verify_data_free(AvbSlotVerifyData* data) {
  1494. if (data->ab_suffix != NULL) {
  1495. avb_free(data->ab_suffix);
  1496. }
  1497. if (data->cmdline != NULL) {
  1498. avb_free(data->cmdline);
  1499. }
  1500. if (data->vbmeta_images != NULL) {
  1501. size_t n;
  1502. for (n = 0; n < data->num_vbmeta_images; n++) {
  1503. AvbVBMetaData* vbmeta_image = &data->vbmeta_images[n];
  1504. if (vbmeta_image->partition_name != NULL) {
  1505. avb_free(vbmeta_image->partition_name);
  1506. }
  1507. if (vbmeta_image->vbmeta_data != NULL) {
  1508. avb_free(vbmeta_image->vbmeta_data);
  1509. }
  1510. }
  1511. avb_free(data->vbmeta_images);
  1512. }
  1513. if (data->loaded_partitions != NULL) {
  1514. size_t n;
  1515. for (n = 0; n < data->num_loaded_partitions; n++) {
  1516. AvbPartitionData* loaded_partition = &data->loaded_partitions[n];
  1517. if (loaded_partition->partition_name != NULL) {
  1518. avb_free(loaded_partition->partition_name);
  1519. }
  1520. if (loaded_partition->data != NULL && !loaded_partition->preloaded) {
  1521. avb_free(loaded_partition->data);
  1522. }
  1523. }
  1524. avb_free(data->loaded_partitions);
  1525. }
  1526. avb_free(data);
  1527. }
  1528. const char* avb_slot_verify_result_to_string(AvbSlotVerifyResult result) {
  1529. const char* ret = NULL;
  1530. switch (result) {
  1531. case AVB_SLOT_VERIFY_RESULT_OK:
  1532. ret = "OK";
  1533. break;
  1534. case AVB_SLOT_VERIFY_RESULT_ERROR_OOM:
  1535. ret = "ERROR_OOM";
  1536. break;
  1537. case AVB_SLOT_VERIFY_RESULT_ERROR_IO:
  1538. ret = "ERROR_IO";
  1539. break;
  1540. case AVB_SLOT_VERIFY_RESULT_ERROR_VERIFICATION:
  1541. ret = "ERROR_VERIFICATION";
  1542. break;
  1543. case AVB_SLOT_VERIFY_RESULT_ERROR_ROLLBACK_INDEX:
  1544. ret = "ERROR_ROLLBACK_INDEX";
  1545. break;
  1546. case AVB_SLOT_VERIFY_RESULT_ERROR_PUBLIC_KEY_REJECTED:
  1547. ret = "ERROR_PUBLIC_KEY_REJECTED";
  1548. break;
  1549. case AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA:
  1550. ret = "ERROR_INVALID_METADATA";
  1551. break;
  1552. case AVB_SLOT_VERIFY_RESULT_ERROR_UNSUPPORTED_VERSION:
  1553. ret = "ERROR_UNSUPPORTED_VERSION";
  1554. break;
  1555. case AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_ARGUMENT:
  1556. ret = "ERROR_INVALID_ARGUMENT";
  1557. break;
  1558. /* Do not add a 'default:' case here because of -Wswitch. */
  1559. }
  1560. if (ret == NULL) {
  1561. avb_error("Unknown AvbSlotVerifyResult value.\n");
  1562. ret = "(unknown)";
  1563. }
  1564. return ret;
  1565. }
  1566. void avb_slot_verify_data_calculate_vbmeta_digest(AvbSlotVerifyData* data,
  1567. AvbDigestType digest_type,
  1568. uint8_t* out_digest) {
  1569. bool ret = false;
  1570. size_t n;
  1571. switch (digest_type) {
  1572. case AVB_DIGEST_TYPE_SHA256: {
  1573. AvbSHA256Ctx ctx;
  1574. avb_sha256_init(&ctx);
  1575. for (n = 0; n < data->num_vbmeta_images; n++) {
  1576. avb_sha256_update(&ctx,
  1577. data->vbmeta_images[n].vbmeta_data,
  1578. data->vbmeta_images[n].vbmeta_size);
  1579. }
  1580. avb_memcpy(out_digest, avb_sha256_final(&ctx), AVB_SHA256_DIGEST_SIZE);
  1581. ret = true;
  1582. } break;
  1583. case AVB_DIGEST_TYPE_SHA512: {
  1584. AvbSHA512Ctx ctx;
  1585. avb_sha512_init(&ctx);
  1586. for (n = 0; n < data->num_vbmeta_images; n++) {
  1587. avb_sha512_update(&ctx,
  1588. data->vbmeta_images[n].vbmeta_data,
  1589. data->vbmeta_images[n].vbmeta_size);
  1590. }
  1591. avb_memcpy(out_digest, avb_sha512_final(&ctx), AVB_SHA512_DIGEST_SIZE);
  1592. ret = true;
  1593. } break;
  1594. /* Do not add a 'default:' case here because of -Wswitch. */
  1595. }
  1596. if (!ret) {
  1597. avb_fatal("Unknown digest type");
  1598. }
  1599. }