Dario Freni | 5a25929 | 2018-08-14 17:49:00 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Jiyong Park | 69c0f11 | 2018-11-22 20:38:05 +0900 | [diff] [blame] | 17 | #include "apex_file.h" |
Andreas Gampe | 356e40c | 2018-12-26 10:59:57 -0800 | [diff] [blame] | 18 | |
| 19 | #include <fcntl.h> |
| 20 | #include <sys/stat.h> |
| 21 | #include <sys/types.h> |
| 22 | #include <unistd.h> |
| 23 | |
Jiyong Park | 5e81023 | 2019-04-01 15:24:26 +0900 | [diff] [blame] | 24 | #include <filesystem> |
Andreas Gampe | 356e40c | 2018-12-26 10:59:57 -0800 | [diff] [blame] | 25 | #include <fstream> |
Nikita Ioffe | fe685a3 | 2020-09-10 19:02:56 +0100 | [diff] [blame] | 26 | #include <span> |
Jiyong Park | 69c0f11 | 2018-11-22 20:38:05 +0900 | [diff] [blame] | 27 | |
Jiyong Park | d02c88c | 2018-11-13 19:23:32 +0900 | [diff] [blame] | 28 | #include <android-base/file.h> |
Dario Freni | 5a25929 | 2018-08-14 17:49:00 +0100 | [diff] [blame] | 29 | #include <android-base/logging.h> |
Jiyong Park | 69c0f11 | 2018-11-22 20:38:05 +0900 | [diff] [blame] | 30 | #include <android-base/scopeguard.h> |
Andreas Gampe | 356e40c | 2018-12-26 10:59:57 -0800 | [diff] [blame] | 31 | #include <android-base/strings.h> |
| 32 | #include <android-base/unique_fd.h> |
| 33 | #include <libavb/libavb.h> |
Nikita Ioffe | fe685a3 | 2020-09-10 19:02:56 +0100 | [diff] [blame] | 34 | #include <ziparchive/zip_archive.h> |
Andreas Gampe | 356e40c | 2018-12-26 10:59:57 -0800 | [diff] [blame] | 35 | |
Nikita Ioffe | 264c421 | 2019-09-13 16:30:17 +0100 | [diff] [blame] | 36 | #include "apex_constants.h" |
Jiyong Park | 5e81023 | 2019-04-01 15:24:26 +0900 | [diff] [blame] | 37 | #include "apexd_utils.h" |
Andreas Gampe | 356e40c | 2018-12-26 10:59:57 -0800 | [diff] [blame] | 38 | |
Theotime Combes | d12e76b | 2020-07-08 16:51:23 +0000 | [diff] [blame] | 39 | using android::base::borrowed_fd; |
Mohammad Samiul Islam | bd6ab0f | 2019-06-20 15:55:27 +0100 | [diff] [blame] | 40 | using android::base::Error; |
Andreas Gampe | 356e40c | 2018-12-26 10:59:57 -0800 | [diff] [blame] | 41 | using android::base::ReadFullyAtOffset; |
Mohammad Samiul Islam | 158ef97 | 2021-01-22 17:47:41 +0000 | [diff] [blame^] | 42 | using android::base::RemoveFileIfExists; |
Mohammad Samiul Islam | bd6ab0f | 2019-06-20 15:55:27 +0100 | [diff] [blame] | 43 | using android::base::Result; |
Andreas Gampe | 356e40c | 2018-12-26 10:59:57 -0800 | [diff] [blame] | 44 | using android::base::unique_fd; |
Dario Freni | 5a25929 | 2018-08-14 17:49:00 +0100 | [diff] [blame] | 45 | |
Dario Freni | 5a25929 | 2018-08-14 17:49:00 +0100 | [diff] [blame] | 46 | namespace android { |
| 47 | namespace apex { |
Jiyong Park | 69c0f11 | 2018-11-22 20:38:05 +0900 | [diff] [blame] | 48 | namespace { |
Dario Freni | 5a25929 | 2018-08-14 17:49:00 +0100 | [diff] [blame] | 49 | |
Jiyong Park | 69c0f11 | 2018-11-22 20:38:05 +0900 | [diff] [blame] | 50 | constexpr const char* kImageFilename = "apex_payload.img"; |
Mohammad Samiul Islam | 7e3140c | 2020-11-30 16:53:11 +0000 | [diff] [blame] | 51 | constexpr const char* kCompressedApexFilename = "original_apex"; |
Jiyong Park | 9181a2d | 2018-12-27 15:14:45 +0900 | [diff] [blame] | 52 | constexpr const char* kBundledPublicKeyFilename = "apex_pubkey"; |
Andreas Gampe | 2efadc0 | 2018-11-19 16:39:45 -0800 | [diff] [blame] | 53 | |
Theotime Combes | d12e76b | 2020-07-08 16:51:23 +0000 | [diff] [blame] | 54 | struct FsMagic { |
| 55 | const char* type; |
| 56 | int32_t offset; |
| 57 | int16_t len; |
| 58 | const char* magic; |
| 59 | }; |
| 60 | constexpr const FsMagic kFsType[] = {{"f2fs", 1024, 4, "\x10\x20\xf5\xf2"}, |
| 61 | {"ext4", 1024 + 0x38, 2, "\123\357"}}; |
| 62 | |
| 63 | Result<std::string> RetrieveFsType(borrowed_fd fd, int32_t image_offset) { |
| 64 | for (const auto& fs : kFsType) { |
| 65 | char buf[fs.len]; |
| 66 | if (!ReadFullyAtOffset(fd, buf, fs.len, image_offset + fs.offset)) { |
| 67 | return ErrnoError() << "Couldn't read filesystem magic"; |
| 68 | } |
| 69 | if (memcmp(buf, fs.magic, fs.len) == 0) { |
| 70 | return std::string(fs.type); |
| 71 | } |
| 72 | } |
| 73 | return Error() << "Couldn't find filesystem magic"; |
| 74 | } |
| 75 | |
Jooyung Han | 7dca50c | 2019-04-12 04:52:42 +0900 | [diff] [blame] | 76 | } // namespace |
| 77 | |
Mohammad Samiul Islam | bd6ab0f | 2019-06-20 15:55:27 +0100 | [diff] [blame] | 78 | Result<ApexFile> ApexFile::Open(const std::string& path) { |
Mohammad Samiul Islam | 4025306 | 2021-01-08 13:24:54 +0000 | [diff] [blame] | 79 | std::optional<int32_t> image_offset; |
| 80 | std::optional<size_t> image_size; |
Jiyong Park | 69c0f11 | 2018-11-22 20:38:05 +0900 | [diff] [blame] | 81 | std::string manifest_content; |
Jiyong Park | 9181a2d | 2018-12-27 15:14:45 +0900 | [diff] [blame] | 82 | std::string pubkey; |
Mohammad Samiul Islam | 4025306 | 2021-01-08 13:24:54 +0000 | [diff] [blame] | 83 | std::optional<std::string> fs_type; |
Mohammad Samiul Islam | 7e3140c | 2020-11-30 16:53:11 +0000 | [diff] [blame] | 84 | ZipEntry entry; |
Jiyong Park | 69c0f11 | 2018-11-22 20:38:05 +0900 | [diff] [blame] | 85 | |
Theotime Combes | d12e76b | 2020-07-08 16:51:23 +0000 | [diff] [blame] | 86 | unique_fd fd(open(path.c_str(), O_RDONLY | O_BINARY | O_CLOEXEC)); |
| 87 | if (fd < 0) { |
| 88 | return Error() << "Failed to open package " << path << ": " |
| 89 | << "I/O error"; |
| 90 | } |
George Burgess IV | dfc059d | 2020-08-31 10:40:27 -0700 | [diff] [blame] | 91 | |
| 92 | ZipArchiveHandle handle; |
| 93 | auto handle_guard = |
| 94 | android::base::make_scope_guard([&handle] { CloseArchive(handle); }); |
Theotime Combes | d12e76b | 2020-07-08 16:51:23 +0000 | [diff] [blame] | 95 | int ret = OpenArchiveFd(fd.get(), path.c_str(), &handle, false); |
Jiyong Park | 8f55a21 | 2019-06-03 20:48:15 +0900 | [diff] [blame] | 96 | if (ret < 0) { |
Mohammad Samiul Islam | bd6ab0f | 2019-06-20 15:55:27 +0100 | [diff] [blame] | 97 | return Error() << "Failed to open package " << path << ": " |
| 98 | << ErrorCodeString(ret); |
Jiyong Park | 8f55a21 | 2019-06-03 20:48:15 +0900 | [diff] [blame] | 99 | } |
Jiyong Park | 69c0f11 | 2018-11-22 20:38:05 +0900 | [diff] [blame] | 100 | |
Mohammad Samiul Islam | 7e3140c | 2020-11-30 16:53:11 +0000 | [diff] [blame] | 101 | bool is_compressed = true; |
| 102 | ret = FindEntry(handle, kCompressedApexFilename, &entry); |
Jiyong Park | 8f55a21 | 2019-06-03 20:48:15 +0900 | [diff] [blame] | 103 | if (ret < 0) { |
Mohammad Samiul Islam | 7e3140c | 2020-11-30 16:53:11 +0000 | [diff] [blame] | 104 | is_compressed = false; |
Jiyong Park | 8f55a21 | 2019-06-03 20:48:15 +0900 | [diff] [blame] | 105 | } |
Jiyong Park | 69c0f11 | 2018-11-22 20:38:05 +0900 | [diff] [blame] | 106 | |
Mohammad Samiul Islam | 7e3140c | 2020-11-30 16:53:11 +0000 | [diff] [blame] | 107 | if (!is_compressed) { |
| 108 | // Locate the mountable image within the zipfile and store offset and size. |
| 109 | ret = FindEntry(handle, kImageFilename, &entry); |
| 110 | if (ret < 0) { |
| 111 | return Error() << "Could not find entry \"" << kImageFilename |
Mohammad Samiul Islam | a3ec5bb | 2021-01-08 18:52:59 +0000 | [diff] [blame] | 112 | << "\" or \"" << kCompressedApexFilename |
Mohammad Samiul Islam | 7e3140c | 2020-11-30 16:53:11 +0000 | [diff] [blame] | 113 | << "\" in package " << path << ": " |
| 114 | << ErrorCodeString(ret); |
| 115 | } |
| 116 | image_offset = entry.offset; |
| 117 | image_size = entry.uncompressed_length; |
| 118 | |
Mohammad Samiul Islam | 4025306 | 2021-01-08 13:24:54 +0000 | [diff] [blame] | 119 | auto fs_type_result = RetrieveFsType(fd, image_offset.value()); |
| 120 | if (!fs_type_result.ok()) { |
Mohammad Samiul Islam | 7e3140c | 2020-11-30 16:53:11 +0000 | [diff] [blame] | 121 | return Error() << "Failed to retrieve filesystem type for " << path |
Mohammad Samiul Islam | 4025306 | 2021-01-08 13:24:54 +0000 | [diff] [blame] | 122 | << ": " << fs_type_result.error(); |
Mohammad Samiul Islam | 7e3140c | 2020-11-30 16:53:11 +0000 | [diff] [blame] | 123 | } |
Mohammad Samiul Islam | 4025306 | 2021-01-08 13:24:54 +0000 | [diff] [blame] | 124 | fs_type = std::move(*fs_type_result); |
Theotime Combes | d12e76b | 2020-07-08 16:51:23 +0000 | [diff] [blame] | 125 | } |
| 126 | |
Dario Freni | a277bdf | 2019-11-05 22:37:49 +0000 | [diff] [blame] | 127 | ret = FindEntry(handle, kManifestFilenamePb, &entry); |
Jiyong Park | 8f55a21 | 2019-06-03 20:48:15 +0900 | [diff] [blame] | 128 | if (ret < 0) { |
Jooyung Han | 43ec48f | 2020-05-12 12:01:05 +0900 | [diff] [blame] | 129 | return Error() << "Could not find entry \"" << kManifestFilenamePb |
| 130 | << "\" in package " << path << ": " << ErrorCodeString(ret); |
Jiyong Park | 8f55a21 | 2019-06-03 20:48:15 +0900 | [diff] [blame] | 131 | } |
Jiyong Park | 69c0f11 | 2018-11-22 20:38:05 +0900 | [diff] [blame] | 132 | |
Jiyong Park | 8f55a21 | 2019-06-03 20:48:15 +0900 | [diff] [blame] | 133 | uint32_t length = entry.uncompressed_length; |
| 134 | manifest_content.resize(length, '\0'); |
| 135 | ret = ExtractToMemory(handle, &entry, |
| 136 | reinterpret_cast<uint8_t*>(&(manifest_content)[0]), |
| 137 | length); |
| 138 | if (ret != 0) { |
Mohammad Samiul Islam | bd6ab0f | 2019-06-20 15:55:27 +0100 | [diff] [blame] | 139 | return Error() << "Failed to extract manifest from package " << path << ": " |
| 140 | << ErrorCodeString(ret); |
Jiyong Park | 8f55a21 | 2019-06-03 20:48:15 +0900 | [diff] [blame] | 141 | } |
Jiyong Park | 69c0f11 | 2018-11-22 20:38:05 +0900 | [diff] [blame] | 142 | |
Jiyong Park | 8f55a21 | 2019-06-03 20:48:15 +0900 | [diff] [blame] | 143 | ret = FindEntry(handle, kBundledPublicKeyFilename, &entry); |
| 144 | if (ret >= 0) { |
Jiyong Park | 8f55a21 | 2019-06-03 20:48:15 +0900 | [diff] [blame] | 145 | length = entry.uncompressed_length; |
| 146 | pubkey.resize(length, '\0'); |
Jiyong Park | 69c0f11 | 2018-11-22 20:38:05 +0900 | [diff] [blame] | 147 | ret = ExtractToMemory(handle, &entry, |
Jiyong Park | 8f55a21 | 2019-06-03 20:48:15 +0900 | [diff] [blame] | 148 | reinterpret_cast<uint8_t*>(&(pubkey)[0]), length); |
Jiyong Park | 69c0f11 | 2018-11-22 20:38:05 +0900 | [diff] [blame] | 149 | if (ret != 0) { |
Mohammad Samiul Islam | bd6ab0f | 2019-06-20 15:55:27 +0100 | [diff] [blame] | 150 | return Error() << "Failed to extract public key from package " << path |
| 151 | << ": " << ErrorCodeString(ret); |
Jiyong Park | 69c0f11 | 2018-11-22 20:38:05 +0900 | [diff] [blame] | 152 | } |
Jiyong Park | d02c88c | 2018-11-13 19:23:32 +0900 | [diff] [blame] | 153 | } |
| 154 | |
Jooyung Han | 43ec48f | 2020-05-12 12:01:05 +0900 | [diff] [blame] | 155 | Result<ApexManifest> manifest = ParseManifest(manifest_content); |
Bernie Innocenti | e3ba3ac | 2020-02-06 22:01:51 +0900 | [diff] [blame] | 156 | if (!manifest.ok()) { |
Mohammad Samiul Islam | bd6ab0f | 2019-06-20 15:55:27 +0100 | [diff] [blame] | 157 | return manifest.error(); |
Dario Freni | 5a25929 | 2018-08-14 17:49:00 +0100 | [diff] [blame] | 158 | } |
| 159 | |
Jooyung Han | 43ec48f | 2020-05-12 12:01:05 +0900 | [diff] [blame] | 160 | return ApexFile(path, image_offset, image_size, std::move(*manifest), pubkey, |
Mohammad Samiul Islam | 4025306 | 2021-01-08 13:24:54 +0000 | [diff] [blame] | 161 | fs_type, is_compressed); |
Dario Freni | 5a25929 | 2018-08-14 17:49:00 +0100 | [diff] [blame] | 162 | } |
| 163 | |
Andreas Gampe | 356e40c | 2018-12-26 10:59:57 -0800 | [diff] [blame] | 164 | // AVB-related code. |
| 165 | |
| 166 | namespace { |
| 167 | |
Andreas Gampe | 356e40c | 2018-12-26 10:59:57 -0800 | [diff] [blame] | 168 | static constexpr int kVbMetaMaxSize = 64 * 1024; |
| 169 | |
Nikita Ioffe | 77ae498 | 2020-12-21 21:45:43 +0000 | [diff] [blame] | 170 | std::string BytesToHex(const uint8_t* bytes, size_t bytes_len) { |
Andreas Gampe | 356e40c | 2018-12-26 10:59:57 -0800 | [diff] [blame] | 171 | std::ostringstream s; |
| 172 | |
| 173 | s << std::hex << std::setfill('0'); |
| 174 | for (size_t i = 0; i < bytes_len; i++) { |
| 175 | s << std::setw(2) << static_cast<int>(bytes[i]); |
| 176 | } |
| 177 | return s.str(); |
| 178 | } |
| 179 | |
Nikita Ioffe | 77ae498 | 2020-12-21 21:45:43 +0000 | [diff] [blame] | 180 | std::string GetSalt(const AvbHashtreeDescriptor& desc, |
| 181 | const uint8_t* trailing_data) { |
| 182 | const uint8_t* desc_salt = trailing_data + desc.partition_name_len; |
Andreas Gampe | 356e40c | 2018-12-26 10:59:57 -0800 | [diff] [blame] | 183 | |
Nikita Ioffe | 77ae498 | 2020-12-21 21:45:43 +0000 | [diff] [blame] | 184 | return BytesToHex(desc_salt, desc.salt_len); |
Andreas Gampe | 356e40c | 2018-12-26 10:59:57 -0800 | [diff] [blame] | 185 | } |
| 186 | |
Nikita Ioffe | 77ae498 | 2020-12-21 21:45:43 +0000 | [diff] [blame] | 187 | std::string GetDigest(const AvbHashtreeDescriptor& desc, |
| 188 | const uint8_t* trailing_data) { |
Andreas Gampe | 356e40c | 2018-12-26 10:59:57 -0800 | [diff] [blame] | 189 | const uint8_t* desc_digest = |
Nikita Ioffe | 77ae498 | 2020-12-21 21:45:43 +0000 | [diff] [blame] | 190 | trailing_data + desc.partition_name_len + desc.salt_len; |
Andreas Gampe | 356e40c | 2018-12-26 10:59:57 -0800 | [diff] [blame] | 191 | |
Nikita Ioffe | 77ae498 | 2020-12-21 21:45:43 +0000 | [diff] [blame] | 192 | return BytesToHex(desc_digest, desc.root_digest_len); |
Andreas Gampe | 356e40c | 2018-12-26 10:59:57 -0800 | [diff] [blame] | 193 | } |
| 194 | |
Nikita Ioffe | 77ae498 | 2020-12-21 21:45:43 +0000 | [diff] [blame] | 195 | Result<std::unique_ptr<AvbFooter>> GetAvbFooter(const ApexFile& apex, |
Mohammad Samiul Islam | bd6ab0f | 2019-06-20 15:55:27 +0100 | [diff] [blame] | 196 | const unique_fd& fd) { |
Andreas Gampe | 356e40c | 2018-12-26 10:59:57 -0800 | [diff] [blame] | 197 | std::array<uint8_t, AVB_FOOTER_SIZE> footer_data; |
| 198 | auto footer = std::make_unique<AvbFooter>(); |
| 199 | |
| 200 | // The AVB footer is located in the last part of the image |
Mohammad Samiul Islam | 4025306 | 2021-01-08 13:24:54 +0000 | [diff] [blame] | 201 | if (!apex.GetImageOffset() || !apex.GetImageSize()) { |
| 202 | return Error() << "Cannot check avb footer without image offset and size"; |
| 203 | } |
| 204 | off_t offset = apex.GetImageSize().value() + apex.GetImageOffset().value() - |
| 205 | AVB_FOOTER_SIZE; |
Andreas Gampe | 356e40c | 2018-12-26 10:59:57 -0800 | [diff] [blame] | 206 | int ret = lseek(fd, offset, SEEK_SET); |
| 207 | if (ret == -1) { |
Mohammad Samiul Islam | bd6ab0f | 2019-06-20 15:55:27 +0100 | [diff] [blame] | 208 | return ErrnoError() << "Couldn't seek to AVB footer"; |
Andreas Gampe | 356e40c | 2018-12-26 10:59:57 -0800 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | ret = read(fd, footer_data.data(), AVB_FOOTER_SIZE); |
| 212 | if (ret != AVB_FOOTER_SIZE) { |
Mohammad Samiul Islam | bd6ab0f | 2019-06-20 15:55:27 +0100 | [diff] [blame] | 213 | return ErrnoError() << "Couldn't read AVB footer"; |
Andreas Gampe | 356e40c | 2018-12-26 10:59:57 -0800 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | if (!avb_footer_validate_and_byteswap((const AvbFooter*)footer_data.data(), |
| 217 | footer.get())) { |
Mohammad Samiul Islam | bd6ab0f | 2019-06-20 15:55:27 +0100 | [diff] [blame] | 218 | return Error() << "AVB footer verification failed."; |
Andreas Gampe | 356e40c | 2018-12-26 10:59:57 -0800 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | LOG(VERBOSE) << "AVB footer verification successful."; |
Mohammad Samiul Islam | bd6ab0f | 2019-06-20 15:55:27 +0100 | [diff] [blame] | 222 | return footer; |
Andreas Gampe | 356e40c | 2018-12-26 10:59:57 -0800 | [diff] [blame] | 223 | } |
| 224 | |
Nikita Ioffe | a78f8ef | 2019-11-08 13:36:38 +0000 | [diff] [blame] | 225 | bool CompareKeys(const uint8_t* key, size_t length, |
| 226 | const std::string& public_key_content) { |
| 227 | return public_key_content.length() == length && |
| 228 | memcmp(&public_key_content[0], key, length) == 0; |
Jiyong Park | 9181a2d | 2018-12-27 15:14:45 +0900 | [diff] [blame] | 229 | } |
| 230 | |
Nikita Ioffe | fe685a3 | 2020-09-10 19:02:56 +0100 | [diff] [blame] | 231 | // Verifies correctness of vbmeta and returns public key it was signed with. |
Nikita Ioffe | 77ae498 | 2020-12-21 21:45:43 +0000 | [diff] [blame] | 232 | Result<std::span<const uint8_t>> VerifyVbMetaSignature(const ApexFile& apex, |
Nikita Ioffe | fe685a3 | 2020-09-10 19:02:56 +0100 | [diff] [blame] | 233 | const uint8_t* data, |
| 234 | size_t length) { |
Andreas Gampe | 356e40c | 2018-12-26 10:59:57 -0800 | [diff] [blame] | 235 | const uint8_t* pk; |
| 236 | size_t pk_len; |
| 237 | AvbVBMetaVerifyResult res; |
| 238 | |
| 239 | res = avb_vbmeta_image_verify(data, length, &pk, &pk_len); |
| 240 | switch (res) { |
| 241 | case AVB_VBMETA_VERIFY_RESULT_OK: |
| 242 | break; |
| 243 | case AVB_VBMETA_VERIFY_RESULT_OK_NOT_SIGNED: |
| 244 | case AVB_VBMETA_VERIFY_RESULT_HASH_MISMATCH: |
| 245 | case AVB_VBMETA_VERIFY_RESULT_SIGNATURE_MISMATCH: |
Mohammad Samiul Islam | bd6ab0f | 2019-06-20 15:55:27 +0100 | [diff] [blame] | 246 | return Error() << "Error verifying " << apex.GetPath() << ": " |
| 247 | << avb_vbmeta_verify_result_to_string(res); |
Andreas Gampe | 356e40c | 2018-12-26 10:59:57 -0800 | [diff] [blame] | 248 | case AVB_VBMETA_VERIFY_RESULT_INVALID_VBMETA_HEADER: |
Mohammad Samiul Islam | bd6ab0f | 2019-06-20 15:55:27 +0100 | [diff] [blame] | 249 | return Error() << "Error verifying " << apex.GetPath() << ": " |
| 250 | << "invalid vbmeta header"; |
Andreas Gampe | 356e40c | 2018-12-26 10:59:57 -0800 | [diff] [blame] | 251 | case AVB_VBMETA_VERIFY_RESULT_UNSUPPORTED_VERSION: |
Mohammad Samiul Islam | bd6ab0f | 2019-06-20 15:55:27 +0100 | [diff] [blame] | 252 | return Error() << "Error verifying " << apex.GetPath() << ": " |
| 253 | << "unsupported version"; |
Andreas Gampe | 356e40c | 2018-12-26 10:59:57 -0800 | [diff] [blame] | 254 | default: |
Nikita Ioffe | fe685a3 | 2020-09-10 19:02:56 +0100 | [diff] [blame] | 255 | return Error() << "Unknown vmbeta_image_verify return value : " << res; |
Andreas Gampe | 356e40c | 2018-12-26 10:59:57 -0800 | [diff] [blame] | 256 | } |
| 257 | |
Nikita Ioffe | fe685a3 | 2020-09-10 19:02:56 +0100 | [diff] [blame] | 258 | return std::span<const uint8_t>(pk, pk_len); |
Andreas Gampe | 356e40c | 2018-12-26 10:59:57 -0800 | [diff] [blame] | 259 | } |
| 260 | |
Nikita Ioffe | 9714fe9 | 2020-12-22 20:56:33 +0000 | [diff] [blame] | 261 | Result<std::unique_ptr<uint8_t[]>> VerifyVbMeta(const ApexFile& apex, |
Mohammad Samiul Islam | bd6ab0f | 2019-06-20 15:55:27 +0100 | [diff] [blame] | 262 | const unique_fd& fd, |
Nikita Ioffe | fe685a3 | 2020-09-10 19:02:56 +0100 | [diff] [blame] | 263 | const AvbFooter& footer, |
| 264 | const std::string& public_key) { |
Andreas Gampe | 356e40c | 2018-12-26 10:59:57 -0800 | [diff] [blame] | 265 | if (footer.vbmeta_size > kVbMetaMaxSize) { |
Mohammad Samiul Islam | bd6ab0f | 2019-06-20 15:55:27 +0100 | [diff] [blame] | 266 | return Errorf("VbMeta size in footer exceeds kVbMetaMaxSize."); |
Andreas Gampe | 356e40c | 2018-12-26 10:59:57 -0800 | [diff] [blame] | 267 | } |
| 268 | |
Mohammad Samiul Islam | 4025306 | 2021-01-08 13:24:54 +0000 | [diff] [blame] | 269 | if (!apex.GetImageOffset()) { |
| 270 | return Error() << "Cannot check VbMeta size without image offset"; |
| 271 | } |
| 272 | |
| 273 | off_t offset = apex.GetImageOffset().value() + footer.vbmeta_offset; |
Andreas Gampe | 356e40c | 2018-12-26 10:59:57 -0800 | [diff] [blame] | 274 | std::unique_ptr<uint8_t[]> vbmeta_buf(new uint8_t[footer.vbmeta_size]); |
| 275 | |
| 276 | if (!ReadFullyAtOffset(fd, vbmeta_buf.get(), footer.vbmeta_size, offset)) { |
Mohammad Samiul Islam | bd6ab0f | 2019-06-20 15:55:27 +0100 | [diff] [blame] | 277 | return ErrnoError() << "Couldn't read AVB meta-data"; |
Andreas Gampe | 356e40c | 2018-12-26 10:59:57 -0800 | [diff] [blame] | 278 | } |
| 279 | |
Nikita Ioffe | fe685a3 | 2020-09-10 19:02:56 +0100 | [diff] [blame] | 280 | Result<std::span<const uint8_t>> st = |
Nikita Ioffe | 77ae498 | 2020-12-21 21:45:43 +0000 | [diff] [blame] | 281 | VerifyVbMetaSignature(apex, vbmeta_buf.get(), footer.vbmeta_size); |
Bernie Innocenti | e3ba3ac | 2020-02-06 22:01:51 +0900 | [diff] [blame] | 282 | if (!st.ok()) { |
Mohammad Samiul Islam | bd6ab0f | 2019-06-20 15:55:27 +0100 | [diff] [blame] | 283 | return st.error(); |
Andreas Gampe | 356e40c | 2018-12-26 10:59:57 -0800 | [diff] [blame] | 284 | } |
| 285 | |
Nikita Ioffe | fe685a3 | 2020-09-10 19:02:56 +0100 | [diff] [blame] | 286 | if (!CompareKeys(st->data(), st->size(), public_key)) { |
| 287 | return Error() << "Error verifying " << apex.GetPath() << " : " |
| 288 | << "public key doesn't match the pre-installed one"; |
| 289 | } |
| 290 | |
Mohammad Samiul Islam | bd6ab0f | 2019-06-20 15:55:27 +0100 | [diff] [blame] | 291 | return vbmeta_buf; |
Andreas Gampe | 356e40c | 2018-12-26 10:59:57 -0800 | [diff] [blame] | 292 | } |
| 293 | |
Nikita Ioffe | 77ae498 | 2020-12-21 21:45:43 +0000 | [diff] [blame] | 294 | Result<const AvbHashtreeDescriptor*> FindDescriptor(uint8_t* vbmeta_data, |
Mohammad Samiul Islam | bd6ab0f | 2019-06-20 15:55:27 +0100 | [diff] [blame] | 295 | size_t vbmeta_size) { |
Andreas Gampe | 356e40c | 2018-12-26 10:59:57 -0800 | [diff] [blame] | 296 | const AvbDescriptor** descriptors; |
| 297 | size_t num_descriptors; |
| 298 | |
| 299 | descriptors = |
| 300 | avb_descriptor_get_all(vbmeta_data, vbmeta_size, &num_descriptors); |
| 301 | |
Jooyung Han | df858e8 | 2019-04-01 18:06:39 +0900 | [diff] [blame] | 302 | // avb_descriptor_get_all() returns an internally allocated array |
| 303 | // of pointers and it needs to be avb_free()ed after using it. |
| 304 | auto guard = android::base::ScopeGuard(std::bind(avb_free, descriptors)); |
| 305 | |
Andreas Gampe | 356e40c | 2018-12-26 10:59:57 -0800 | [diff] [blame] | 306 | for (size_t i = 0; i < num_descriptors; i++) { |
| 307 | AvbDescriptor desc; |
| 308 | if (!avb_descriptor_validate_and_byteswap(descriptors[i], &desc)) { |
Mohammad Samiul Islam | bd6ab0f | 2019-06-20 15:55:27 +0100 | [diff] [blame] | 309 | return Errorf("Couldn't validate AvbDescriptor."); |
Andreas Gampe | 356e40c | 2018-12-26 10:59:57 -0800 | [diff] [blame] | 310 | } |
| 311 | |
| 312 | if (desc.tag != AVB_DESCRIPTOR_TAG_HASHTREE) { |
| 313 | // Ignore other descriptors |
| 314 | continue; |
| 315 | } |
| 316 | |
Nikita Ioffe | d71ce1d | 2020-04-30 01:19:41 +0100 | [diff] [blame] | 317 | // Check that hashtree descriptor actually fits into memory. |
| 318 | const uint8_t* vbmeta_end = vbmeta_data + vbmeta_size; |
| 319 | if ((uint8_t*)descriptors[i] + sizeof(AvbHashtreeDescriptor) > vbmeta_end) { |
| 320 | return Errorf("Invalid length for AvbHashtreeDescriptor"); |
| 321 | } |
Mohammad Samiul Islam | bd6ab0f | 2019-06-20 15:55:27 +0100 | [diff] [blame] | 322 | return (const AvbHashtreeDescriptor*)descriptors[i]; |
Andreas Gampe | 356e40c | 2018-12-26 10:59:57 -0800 | [diff] [blame] | 323 | } |
| 324 | |
Mohammad Samiul Islam | bd6ab0f | 2019-06-20 15:55:27 +0100 | [diff] [blame] | 325 | return Errorf("Couldn't find any AVB hashtree descriptors."); |
Andreas Gampe | 356e40c | 2018-12-26 10:59:57 -0800 | [diff] [blame] | 326 | } |
| 327 | |
Nikita Ioffe | 77ae498 | 2020-12-21 21:45:43 +0000 | [diff] [blame] | 328 | Result<std::unique_ptr<AvbHashtreeDescriptor>> VerifyDescriptor( |
Andreas Gampe | 356e40c | 2018-12-26 10:59:57 -0800 | [diff] [blame] | 329 | const AvbHashtreeDescriptor* desc) { |
Nikita Ioffe | 77ae498 | 2020-12-21 21:45:43 +0000 | [diff] [blame] | 330 | auto verified_desc = std::make_unique<AvbHashtreeDescriptor>(); |
Andreas Gampe | 356e40c | 2018-12-26 10:59:57 -0800 | [diff] [blame] | 331 | |
| 332 | if (!avb_hashtree_descriptor_validate_and_byteswap(desc, |
Nikita Ioffe | 77ae498 | 2020-12-21 21:45:43 +0000 | [diff] [blame] | 333 | verified_desc.get())) { |
Mohammad Samiul Islam | bd6ab0f | 2019-06-20 15:55:27 +0100 | [diff] [blame] | 334 | return Errorf("Couldn't validate AvbDescriptor."); |
Andreas Gampe | 356e40c | 2018-12-26 10:59:57 -0800 | [diff] [blame] | 335 | } |
| 336 | |
Nikita Ioffe | 77ae498 | 2020-12-21 21:45:43 +0000 | [diff] [blame] | 337 | return verified_desc; |
Andreas Gampe | 356e40c | 2018-12-26 10:59:57 -0800 | [diff] [blame] | 338 | } |
| 339 | |
| 340 | } // namespace |
| 341 | |
Nikita Ioffe | fe685a3 | 2020-09-10 19:02:56 +0100 | [diff] [blame] | 342 | Result<ApexVerityData> ApexFile::VerifyApexVerity( |
| 343 | const std::string& public_key) const { |
Mohammad Samiul Islam | a3ec5bb | 2021-01-08 18:52:59 +0000 | [diff] [blame] | 344 | if (IsCompressed()) { |
| 345 | return Error() << "Cannot verify ApexVerity of compressed APEX"; |
| 346 | } |
| 347 | |
Nikita Ioffe | 77ae498 | 2020-12-21 21:45:43 +0000 | [diff] [blame] | 348 | ApexVerityData verity_data; |
Andreas Gampe | 356e40c | 2018-12-26 10:59:57 -0800 | [diff] [blame] | 349 | |
| 350 | unique_fd fd(open(GetPath().c_str(), O_RDONLY | O_CLOEXEC)); |
| 351 | if (fd.get() == -1) { |
Mohammad Samiul Islam | bd6ab0f | 2019-06-20 15:55:27 +0100 | [diff] [blame] | 352 | return ErrnoError() << "Failed to open " << GetPath(); |
Andreas Gampe | 356e40c | 2018-12-26 10:59:57 -0800 | [diff] [blame] | 353 | } |
| 354 | |
Nikita Ioffe | 77ae498 | 2020-12-21 21:45:43 +0000 | [diff] [blame] | 355 | Result<std::unique_ptr<AvbFooter>> footer = GetAvbFooter(*this, fd); |
Bernie Innocenti | e3ba3ac | 2020-02-06 22:01:51 +0900 | [diff] [blame] | 356 | if (!footer.ok()) { |
Mohammad Samiul Islam | bd6ab0f | 2019-06-20 15:55:27 +0100 | [diff] [blame] | 357 | return footer.error(); |
Andreas Gampe | 356e40c | 2018-12-26 10:59:57 -0800 | [diff] [blame] | 358 | } |
| 359 | |
Mohammad Samiul Islam | bd6ab0f | 2019-06-20 15:55:27 +0100 | [diff] [blame] | 360 | Result<std::unique_ptr<uint8_t[]>> vbmeta_data = |
Nikita Ioffe | 9714fe9 | 2020-12-22 20:56:33 +0000 | [diff] [blame] | 361 | VerifyVbMeta(*this, fd, **footer, public_key); |
Bernie Innocenti | e3ba3ac | 2020-02-06 22:01:51 +0900 | [diff] [blame] | 362 | if (!vbmeta_data.ok()) { |
Mohammad Samiul Islam | bd6ab0f | 2019-06-20 15:55:27 +0100 | [diff] [blame] | 363 | return vbmeta_data.error(); |
Andreas Gampe | 356e40c | 2018-12-26 10:59:57 -0800 | [diff] [blame] | 364 | } |
| 365 | |
Mohammad Samiul Islam | bd6ab0f | 2019-06-20 15:55:27 +0100 | [diff] [blame] | 366 | Result<const AvbHashtreeDescriptor*> descriptor = |
Nikita Ioffe | 77ae498 | 2020-12-21 21:45:43 +0000 | [diff] [blame] | 367 | FindDescriptor(vbmeta_data->get(), (*footer)->vbmeta_size); |
Bernie Innocenti | e3ba3ac | 2020-02-06 22:01:51 +0900 | [diff] [blame] | 368 | if (!descriptor.ok()) { |
Mohammad Samiul Islam | bd6ab0f | 2019-06-20 15:55:27 +0100 | [diff] [blame] | 369 | return descriptor.error(); |
Andreas Gampe | 356e40c | 2018-12-26 10:59:57 -0800 | [diff] [blame] | 370 | } |
| 371 | |
Nikita Ioffe | 77ae498 | 2020-12-21 21:45:43 +0000 | [diff] [blame] | 372 | Result<std::unique_ptr<AvbHashtreeDescriptor>> verified_descriptor = |
| 373 | VerifyDescriptor(*descriptor); |
| 374 | if (!verified_descriptor.ok()) { |
| 375 | return verified_descriptor.error(); |
Andreas Gampe | 356e40c | 2018-12-26 10:59:57 -0800 | [diff] [blame] | 376 | } |
Nikita Ioffe | 77ae498 | 2020-12-21 21:45:43 +0000 | [diff] [blame] | 377 | verity_data.desc = std::move(*verified_descriptor); |
Andreas Gampe | 356e40c | 2018-12-26 10:59:57 -0800 | [diff] [blame] | 378 | |
| 379 | // This area is now safe to access, because we just verified it |
Nikita Ioffe | 77ae498 | 2020-12-21 21:45:43 +0000 | [diff] [blame] | 380 | const uint8_t* trailing_data = |
Andreas Gampe | 356e40c | 2018-12-26 10:59:57 -0800 | [diff] [blame] | 381 | (const uint8_t*)*descriptor + sizeof(AvbHashtreeDescriptor); |
Nikita Ioffe | 77ae498 | 2020-12-21 21:45:43 +0000 | [diff] [blame] | 382 | verity_data.hash_algorithm = |
Jooyung Han | f7c8d03 | 2019-04-11 15:12:09 +0900 | [diff] [blame] | 383 | reinterpret_cast<const char*>((*descriptor)->hash_algorithm); |
Nikita Ioffe | 77ae498 | 2020-12-21 21:45:43 +0000 | [diff] [blame] | 384 | verity_data.salt = GetSalt(*verity_data.desc, trailing_data); |
| 385 | verity_data.root_digest = GetDigest(*verity_data.desc, trailing_data); |
Andreas Gampe | 356e40c | 2018-12-26 10:59:57 -0800 | [diff] [blame] | 386 | |
Nikita Ioffe | 77ae498 | 2020-12-21 21:45:43 +0000 | [diff] [blame] | 387 | return verity_data; |
Andreas Gampe | 356e40c | 2018-12-26 10:59:57 -0800 | [diff] [blame] | 388 | } |
| 389 | |
Mohammad Samiul Islam | 11126f8 | 2020-12-23 10:20:38 +0000 | [diff] [blame] | 390 | Result<void> ApexFile::Decompress(const std::string& dest_path) const { |
| 391 | const std::string& src_path = GetPath(); |
| 392 | |
| 393 | // We should decompress compressed APEX files only |
| 394 | if (!IsCompressed()) { |
| 395 | return ErrnoError() << "Cannot decompress an uncompressed APEX"; |
| 396 | } |
| 397 | |
| 398 | // Get file descriptor of the compressed apex file |
| 399 | unique_fd src_fd(open(src_path.c_str(), O_RDONLY | O_CLOEXEC)); |
| 400 | if (src_fd.get() == -1) { |
| 401 | return ErrnoError() << "Failed to open compressed APEX " << GetPath(); |
| 402 | } |
| 403 | |
| 404 | // Open it as a zip file |
| 405 | ZipArchiveHandle handle; |
| 406 | int ret = OpenArchiveFd(src_fd.get(), src_path.c_str(), &handle, false); |
| 407 | if (ret < 0) { |
| 408 | return Error() << "Failed to open package " << src_path << ": " |
| 409 | << ErrorCodeString(ret); |
| 410 | } |
| 411 | auto handle_guard = |
| 412 | android::base::make_scope_guard([&handle] { CloseArchive(handle); }); |
| 413 | |
| 414 | // Find the original apex file inside the zip and extract to dest |
| 415 | ZipEntry entry; |
| 416 | ret = FindEntry(handle, kCompressedApexFilename, &entry); |
| 417 | if (ret < 0) { |
| 418 | return Error() << "Could not find entry \"" << kCompressedApexFilename |
| 419 | << "\" in package " << src_path << ": " |
| 420 | << ErrorCodeString(ret); |
| 421 | } |
| 422 | |
| 423 | // Open destination file descriptor |
| 424 | unique_fd dest_fd( |
| 425 | open(dest_path.c_str(), O_WRONLY | O_CLOEXEC | O_CREAT, 0644)); |
| 426 | if (dest_fd.get() == -1) { |
| 427 | return ErrnoError() << "Failed to open decompression destination " |
| 428 | << GetPath(); |
| 429 | } |
Mohammad Samiul Islam | 158ef97 | 2021-01-22 17:47:41 +0000 | [diff] [blame^] | 430 | |
| 431 | // Prepare a guard that deletes the extracted file if anything goes wrong |
| 432 | auto decompressed_guard = android::base::make_scope_guard( |
| 433 | [&dest_path] { RemoveFileIfExists(dest_path); }); |
| 434 | |
| 435 | // Extract the original_apex to dest_path |
Mohammad Samiul Islam | 11126f8 | 2020-12-23 10:20:38 +0000 | [diff] [blame] | 436 | ret = ExtractEntryToFile(handle, &entry, dest_fd.get()); |
| 437 | if (ret < 0) { |
| 438 | return Error() << "Could not decompress to file " << dest_path |
| 439 | << ErrorCodeString(ret); |
| 440 | } |
| 441 | |
Mohammad Samiul Islam | 158ef97 | 2021-01-22 17:47:41 +0000 | [diff] [blame^] | 442 | // Post decompression verification |
| 443 | auto decompressed_apex = ApexFile::Open(dest_path); |
| 444 | if (!decompressed_apex.ok()) { |
| 445 | return Error() << "Could not open decompressed APEX: " |
| 446 | << decompressed_apex.error(); |
| 447 | } |
| 448 | if (GetBundledPublicKey() != decompressed_apex->GetBundledPublicKey()) { |
| 449 | return Error() |
| 450 | << "Public key of compressed APEX is different than original APEX"; |
| 451 | } |
| 452 | |
| 453 | // Verification complete. Accept the decompressed file |
| 454 | decompressed_guard.Disable(); |
Mohammad Samiul Islam | 11126f8 | 2020-12-23 10:20:38 +0000 | [diff] [blame] | 455 | LOG(VERBOSE) << "Decompressed " << src_path << " to " << dest_path; |
Mohammad Samiul Islam | 158ef97 | 2021-01-22 17:47:41 +0000 | [diff] [blame^] | 456 | |
Mohammad Samiul Islam | 11126f8 | 2020-12-23 10:20:38 +0000 | [diff] [blame] | 457 | return {}; |
| 458 | } |
| 459 | |
Dario Freni | 5a25929 | 2018-08-14 17:49:00 +0100 | [diff] [blame] | 460 | } // namespace apex |
| 461 | } // namespace android |