| Alex Buynytskyy | 96ff54b | 2020-02-13 06:52:04 -0800 | [diff] [blame] | 1 | /* | 
|  | 2 | * Copyright (C) 2020 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 |  | 
|  | 17 | #include "incremental.h" | 
|  | 18 |  | 
| Yurii Zubrytskyi | b659506 | 2020-02-20 15:30:45 -0800 | [diff] [blame] | 19 | #include <android-base/endian.h> | 
| Alex Buynytskyy | 96ff54b | 2020-02-13 06:52:04 -0800 | [diff] [blame] | 20 | #include <android-base/file.h> | 
|  | 21 | #include <android-base/stringprintf.h> | 
|  | 22 | #include <openssl/base64.h> | 
|  | 23 |  | 
|  | 24 | #include "adb_client.h" | 
|  | 25 | #include "adb_io.h" | 
|  | 26 | #include "adb_utils.h" | 
|  | 27 | #include "commandline.h" | 
|  | 28 | #include "sysdeps.h" | 
|  | 29 |  | 
| Alex Buynytskyy | 96ff54b | 2020-02-13 06:52:04 -0800 | [diff] [blame] | 30 | using namespace std::literals; | 
|  | 31 |  | 
|  | 32 | namespace incremental { | 
|  | 33 |  | 
|  | 34 | namespace { | 
|  | 35 |  | 
|  | 36 | static constexpr auto IDSIG = ".idsig"sv; | 
|  | 37 |  | 
|  | 38 | using android::base::StringPrintf; | 
|  | 39 |  | 
|  | 40 | using Size = int64_t; | 
|  | 41 |  | 
|  | 42 | static inline int32_t read_int32(borrowed_fd fd) { | 
|  | 43 | int32_t result; | 
|  | 44 | ReadFully(fd, &result, sizeof(result)); | 
|  | 45 | return result; | 
|  | 46 | } | 
|  | 47 |  | 
|  | 48 | static inline int32_t read_be_int32(borrowed_fd fd) { | 
|  | 49 | return int32_t(be32toh(read_int32(fd))); | 
|  | 50 | } | 
|  | 51 |  | 
| Yurii Zubrytskyi | 516c441 | 2020-02-19 14:46:16 -0800 | [diff] [blame] | 52 | static inline void append_int(borrowed_fd fd, std::vector<char>* bytes) { | 
|  | 53 | int32_t be_val = read_int32(fd); | 
|  | 54 | auto old_size = bytes->size(); | 
|  | 55 | bytes->resize(old_size + sizeof(be_val)); | 
|  | 56 | memcpy(bytes->data() + old_size, &be_val, sizeof(be_val)); | 
|  | 57 | } | 
|  | 58 |  | 
| Alex Buynytskyy | 96ff54b | 2020-02-13 06:52:04 -0800 | [diff] [blame] | 59 | static inline void append_bytes_with_size(borrowed_fd fd, std::vector<char>* bytes) { | 
|  | 60 | int32_t be_size = read_int32(fd); | 
|  | 61 | int32_t size = int32_t(be32toh(be_size)); | 
|  | 62 | auto old_size = bytes->size(); | 
|  | 63 | bytes->resize(old_size + sizeof(be_size) + size); | 
|  | 64 | memcpy(bytes->data() + old_size, &be_size, sizeof(be_size)); | 
|  | 65 | ReadFully(fd, bytes->data() + old_size + sizeof(be_size), size); | 
|  | 66 | } | 
|  | 67 |  | 
|  | 68 | static inline std::pair<std::vector<char>, int32_t> read_id_sig_headers(borrowed_fd fd) { | 
|  | 69 | std::vector<char> result; | 
| Yurii Zubrytskyi | 516c441 | 2020-02-19 14:46:16 -0800 | [diff] [blame] | 70 | append_int(fd, &result);              // version | 
| Alex Buynytskyy | 96ff54b | 2020-02-13 06:52:04 -0800 | [diff] [blame] | 71 | append_bytes_with_size(fd, &result);  // verityRootHash | 
|  | 72 | append_bytes_with_size(fd, &result);  // v3Digest | 
|  | 73 | append_bytes_with_size(fd, &result);  // pkcs7SignatureBlock | 
|  | 74 | auto tree_size = read_be_int32(fd);   // size of the verity tree | 
|  | 75 | return {std::move(result), tree_size}; | 
|  | 76 | } | 
|  | 77 |  | 
|  | 78 | static inline Size verity_tree_size_for_file(Size fileSize) { | 
|  | 79 | constexpr int INCFS_DATA_FILE_BLOCK_SIZE = 4096; | 
|  | 80 | constexpr int SHA256_DIGEST_SIZE = 32; | 
|  | 81 | constexpr int digest_size = SHA256_DIGEST_SIZE; | 
|  | 82 | constexpr int hash_per_block = INCFS_DATA_FILE_BLOCK_SIZE / digest_size; | 
|  | 83 |  | 
|  | 84 | Size total_tree_block_count = 0; | 
|  | 85 |  | 
|  | 86 | auto block_count = 1 + (fileSize - 1) / INCFS_DATA_FILE_BLOCK_SIZE; | 
|  | 87 | auto hash_block_count = block_count; | 
|  | 88 | for (auto i = 0; hash_block_count > 1; i++) { | 
|  | 89 | hash_block_count = (hash_block_count + hash_per_block - 1) / hash_per_block; | 
|  | 90 | total_tree_block_count += hash_block_count; | 
|  | 91 | } | 
|  | 92 | return total_tree_block_count * INCFS_DATA_FILE_BLOCK_SIZE; | 
|  | 93 | } | 
|  | 94 |  | 
|  | 95 | // Base64-encode signature bytes. Keeping fd at the position of start of verity tree. | 
|  | 96 | static std::pair<unique_fd, std::string> read_and_encode_signature(Size file_size, | 
|  | 97 | std::string signature_file) { | 
|  | 98 | signature_file += IDSIG; | 
|  | 99 |  | 
|  | 100 | struct stat st; | 
|  | 101 | if (stat(signature_file.c_str(), &st)) { | 
|  | 102 | fprintf(stderr, "Failed to stat signature file %s. Abort.\n", signature_file.c_str()); | 
|  | 103 | return {}; | 
|  | 104 | } | 
|  | 105 |  | 
|  | 106 | unique_fd fd(adb_open(signature_file.c_str(), O_RDONLY | O_CLOEXEC)); | 
|  | 107 | if (fd < 0) { | 
|  | 108 | fprintf(stderr, "Failed to open signature file: %s. Abort.\n", signature_file.c_str()); | 
|  | 109 | return {}; | 
|  | 110 | } | 
|  | 111 |  | 
|  | 112 | auto [signature, tree_size] = read_id_sig_headers(fd); | 
|  | 113 | if (auto expected = verity_tree_size_for_file(file_size); tree_size != expected) { | 
|  | 114 | fprintf(stderr, | 
|  | 115 | "Verity tree size mismatch in signature file: %s [was %lld, expected %lld].\n", | 
|  | 116 | signature_file.c_str(), (long long)tree_size, (long long)expected); | 
|  | 117 | return {}; | 
|  | 118 | } | 
|  | 119 |  | 
|  | 120 | size_t base64_len = 0; | 
|  | 121 | if (!EVP_EncodedLength(&base64_len, signature.size())) { | 
|  | 122 | fprintf(stderr, "Fail to estimate base64 encoded length. Abort.\n"); | 
|  | 123 | return {}; | 
|  | 124 | } | 
|  | 125 | std::string encoded_signature; | 
|  | 126 | encoded_signature.resize(base64_len); | 
|  | 127 | encoded_signature.resize(EVP_EncodeBlock((uint8_t*)encoded_signature.data(), | 
|  | 128 | (const uint8_t*)signature.data(), signature.size())); | 
|  | 129 |  | 
|  | 130 | return {std::move(fd), std::move(encoded_signature)}; | 
|  | 131 | } | 
|  | 132 |  | 
|  | 133 | // Send install-incremental to the device along with properly configured file descriptors in | 
|  | 134 | // streaming format. Once connection established, send all fs-verity tree bytes. | 
|  | 135 | static unique_fd start_install(const std::vector<std::string>& files) { | 
|  | 136 | std::vector<std::string> command_args{"package", "install-incremental"}; | 
|  | 137 |  | 
|  | 138 | // fd's with positions at the beginning of fs-verity | 
|  | 139 | std::vector<unique_fd> signature_fds; | 
|  | 140 | signature_fds.reserve(files.size()); | 
|  | 141 | for (int i = 0, size = files.size(); i < size; ++i) { | 
|  | 142 | const auto& file = files[i]; | 
|  | 143 |  | 
|  | 144 | struct stat st; | 
|  | 145 | if (stat(file.c_str(), &st)) { | 
|  | 146 | fprintf(stderr, "Failed to stat input file %s. Abort.\n", file.c_str()); | 
|  | 147 | return {}; | 
|  | 148 | } | 
|  | 149 |  | 
|  | 150 | auto [signature_fd, signature] = read_and_encode_signature(st.st_size, file); | 
|  | 151 | if (!signature_fd.ok()) { | 
|  | 152 | return {}; | 
|  | 153 | } | 
|  | 154 |  | 
|  | 155 | auto file_desc = | 
|  | 156 | StringPrintf("%s:%lld:%s:%s", android::base::Basename(file).c_str(), | 
|  | 157 | (long long)st.st_size, std::to_string(i).c_str(), signature.c_str()); | 
|  | 158 | command_args.push_back(std::move(file_desc)); | 
|  | 159 |  | 
|  | 160 | signature_fds.push_back(std::move(signature_fd)); | 
|  | 161 | } | 
|  | 162 |  | 
|  | 163 | std::string error; | 
|  | 164 | auto connection_fd = unique_fd(send_abb_exec_command(command_args, &error)); | 
|  | 165 | if (connection_fd < 0) { | 
|  | 166 | fprintf(stderr, "Failed to run: %s, error: %s\n", | 
|  | 167 | android::base::Join(command_args, " ").c_str(), error.c_str()); | 
|  | 168 | return {}; | 
|  | 169 | } | 
|  | 170 |  | 
|  | 171 | // Pushing verity trees for all installation files. | 
|  | 172 | for (auto&& local_fd : signature_fds) { | 
|  | 173 | if (!copy_to_file(local_fd.get(), connection_fd.get())) { | 
|  | 174 | fprintf(stderr, "Failed to stream tree bytes: %s. Abort.\n", strerror(errno)); | 
|  | 175 | return {}; | 
|  | 176 | } | 
|  | 177 | } | 
|  | 178 |  | 
|  | 179 | return connection_fd; | 
|  | 180 | } | 
|  | 181 |  | 
|  | 182 | }  // namespace | 
|  | 183 |  | 
|  | 184 | std::optional<Process> install(std::vector<std::string> files) { | 
|  | 185 | auto connection_fd = start_install(files); | 
|  | 186 | if (connection_fd < 0) { | 
|  | 187 | fprintf(stderr, "adb: failed to initiate installation on device.\n"); | 
|  | 188 | return {}; | 
|  | 189 | } | 
|  | 190 |  | 
|  | 191 | std::string adb_path = android::base::GetExecutablePath(); | 
|  | 192 |  | 
|  | 193 | auto osh = adb_get_os_handle(connection_fd.get()); | 
|  | 194 | #ifdef _WIN32 | 
|  | 195 | auto fd_param = std::to_string(reinterpret_cast<intptr_t>(osh)); | 
|  | 196 | #else /* !_WIN32 a.k.a. Unix */ | 
|  | 197 | auto fd_param = std::to_string(osh); | 
|  | 198 | #endif | 
|  | 199 |  | 
|  | 200 | std::vector<std::string> args(std::move(files)); | 
|  | 201 | args.insert(args.begin(), {"inc-server", fd_param}); | 
|  | 202 | auto child = adb_launch_process(adb_path, std::move(args), {connection_fd.get()}); | 
|  | 203 | if (!child) { | 
|  | 204 | fprintf(stderr, "adb: failed to fork: %s\n", strerror(errno)); | 
|  | 205 | return {}; | 
|  | 206 | } | 
|  | 207 |  | 
|  | 208 | auto killOnExit = [](Process* p) { p->kill(); }; | 
|  | 209 | std::unique_ptr<Process, decltype(killOnExit)> serverKiller(&child, killOnExit); | 
|  | 210 | // TODO: Terminate server process if installation fails. | 
|  | 211 | serverKiller.release(); | 
|  | 212 |  | 
|  | 213 | return child; | 
|  | 214 | } | 
|  | 215 |  | 
|  | 216 | }  // namespace incremental |