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