From d7b63d2c5c3dcd278adb798c983f47f398b322e6 Mon Sep 17 00:00:00 2001 From: Martijn Coenen Date: Thu, 22 Jul 2021 03:24:28 +0200 Subject: [PATCH] Fix makeUniqueWithTrailingData. And do some additional checking on the generated verity digest size. Bug: 194334498 Test: TH Change-Id: If2ae3edc5427b4315d4be63663ab3ac4006f91e8 --- ondevice-signing/VerityUtils.cpp | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/ondevice-signing/VerityUtils.cpp b/ondevice-signing/VerityUtils.cpp index e58de686..2beb7ebb 100644 --- a/ondevice-signing/VerityUtils.cpp +++ b/ondevice-signing/VerityUtils.cpp @@ -83,7 +83,12 @@ Result> createDigest(int fd) { if (ret < 0) { return ErrnoError() << "Failed to compute fs-verity digest"; } - std::vector digestVector(&digest->digest[0], &digest->digest[32]); + int expected_digest_size = libfsverity_get_digest_size(FS_VERITY_HASH_ALG_SHA256); + if (digest->digest_size != expected_digest_size) { + return Error() << "Digest does not have expected size: " << expected_digest_size + << " actual: " << digest->digest_size; + } + std::vector digestVector(&digest->digest[0], &digest->digest[expected_digest_size]); free(digest); return digestVector; } @@ -111,7 +116,7 @@ template using trailing_unique_ptr = std::unique_ptr static trailing_unique_ptr makeUniqueWithTrailingData(size_t trailing_data_size) { - uint8_t* memory = new uint8_t[sizeof(T*) + trailing_data_size]; + uint8_t* memory = new uint8_t[sizeof(T) + trailing_data_size]; T* ptr = new (memory) T; return trailing_unique_ptr{ptr}; }