Fix makeUniqueWithTrailingData.

And do some additional checking on the generated verity digest size.

Bug: 194334498
Test: TH
Change-Id: If2ae3edc5427b4315d4be63663ab3ac4006f91e8
This commit is contained in:
Martijn Coenen 2021-07-22 03:24:28 +02:00
parent a68ba13323
commit d7b63d2c5c

View file

@ -83,7 +83,12 @@ Result<std::vector<uint8_t>> createDigest(int fd) {
if (ret < 0) {
return ErrnoError() << "Failed to compute fs-verity digest";
}
std::vector<uint8_t> 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<uint8_t> digestVector(&digest->digest[0], &digest->digest[expected_digest_size]);
free(digest);
return digestVector;
}
@ -111,7 +116,7 @@ template <typename T> using trailing_unique_ptr = std::unique_ptr<T, DeleteAsPOD
template <typename T>
static trailing_unique_ptr<T> 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<T>{ptr};
}