| 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; | 
| Yurii Zubrytskyi | c3ac339 | 2020-03-18 15:49:45 -0700 | [diff] [blame] | 44 | return ReadFdExactly(fd, &result, sizeof(result)) ? result : -1; | 
| Alex Buynytskyy | 96ff54b | 2020-02-13 06:52:04 -0800 | [diff] [blame] | 45 | } | 
|  | 46 |  | 
| Yurii Zubrytskyi | 516c441 | 2020-02-19 14:46:16 -0800 | [diff] [blame] | 47 | static inline void append_int(borrowed_fd fd, std::vector<char>* bytes) { | 
| Alex Buynytskyy | 33ac1b0 | 2020-03-13 08:39:31 -0700 | [diff] [blame] | 48 | int32_t le_val = read_int32(fd); | 
| Yurii Zubrytskyi | 516c441 | 2020-02-19 14:46:16 -0800 | [diff] [blame] | 49 | auto old_size = bytes->size(); | 
| Alex Buynytskyy | 33ac1b0 | 2020-03-13 08:39:31 -0700 | [diff] [blame] | 50 | bytes->resize(old_size + sizeof(le_val)); | 
|  | 51 | memcpy(bytes->data() + old_size, &le_val, sizeof(le_val)); | 
| Yurii Zubrytskyi | 516c441 | 2020-02-19 14:46:16 -0800 | [diff] [blame] | 52 | } | 
|  | 53 |  | 
| Alex Buynytskyy | 96ff54b | 2020-02-13 06:52:04 -0800 | [diff] [blame] | 54 | static inline void append_bytes_with_size(borrowed_fd fd, std::vector<char>* bytes) { | 
| Alex Buynytskyy | 33ac1b0 | 2020-03-13 08:39:31 -0700 | [diff] [blame] | 55 | int32_t le_size = read_int32(fd); | 
| Yurii Zubrytskyi | c3ac339 | 2020-03-18 15:49:45 -0700 | [diff] [blame] | 56 | if (le_size < 0) { | 
|  | 57 | return; | 
|  | 58 | } | 
| Alex Buynytskyy | 33ac1b0 | 2020-03-13 08:39:31 -0700 | [diff] [blame] | 59 | int32_t size = int32_t(le32toh(le_size)); | 
| Alex Buynytskyy | 96ff54b | 2020-02-13 06:52:04 -0800 | [diff] [blame] | 60 | auto old_size = bytes->size(); | 
| Alex Buynytskyy | 33ac1b0 | 2020-03-13 08:39:31 -0700 | [diff] [blame] | 61 | bytes->resize(old_size + sizeof(le_size) + size); | 
|  | 62 | memcpy(bytes->data() + old_size, &le_size, sizeof(le_size)); | 
| Yurii Zubrytskyi | c3ac339 | 2020-03-18 15:49:45 -0700 | [diff] [blame] | 63 | ReadFdExactly(fd, bytes->data() + old_size + sizeof(le_size), size); | 
| Alex Buynytskyy | 96ff54b | 2020-02-13 06:52:04 -0800 | [diff] [blame] | 64 | } | 
|  | 65 |  | 
|  | 66 | static inline std::pair<std::vector<char>, int32_t> read_id_sig_headers(borrowed_fd fd) { | 
|  | 67 | std::vector<char> result; | 
| Yurii Zubrytskyi | 516c441 | 2020-02-19 14:46:16 -0800 | [diff] [blame] | 68 | append_int(fd, &result);              // version | 
| Alex Buynytskyy | 33ac1b0 | 2020-03-13 08:39:31 -0700 | [diff] [blame] | 69 | append_bytes_with_size(fd, &result);  // hashingInfo | 
|  | 70 | append_bytes_with_size(fd, &result);  // signingInfo | 
|  | 71 | auto le_tree_size = read_int32(fd); | 
|  | 72 | auto tree_size = int32_t(le32toh(le_tree_size));  // size of the verity tree | 
| Alex Buynytskyy | 96ff54b | 2020-02-13 06:52:04 -0800 | [diff] [blame] | 73 | return {std::move(result), tree_size}; | 
|  | 74 | } | 
|  | 75 |  | 
|  | 76 | static inline Size verity_tree_size_for_file(Size fileSize) { | 
|  | 77 | constexpr int INCFS_DATA_FILE_BLOCK_SIZE = 4096; | 
|  | 78 | constexpr int SHA256_DIGEST_SIZE = 32; | 
|  | 79 | constexpr int digest_size = SHA256_DIGEST_SIZE; | 
|  | 80 | constexpr int hash_per_block = INCFS_DATA_FILE_BLOCK_SIZE / digest_size; | 
|  | 81 |  | 
|  | 82 | Size total_tree_block_count = 0; | 
|  | 83 |  | 
|  | 84 | auto block_count = 1 + (fileSize - 1) / INCFS_DATA_FILE_BLOCK_SIZE; | 
|  | 85 | auto hash_block_count = block_count; | 
|  | 86 | for (auto i = 0; hash_block_count > 1; i++) { | 
|  | 87 | hash_block_count = (hash_block_count + hash_per_block - 1) / hash_per_block; | 
|  | 88 | total_tree_block_count += hash_block_count; | 
|  | 89 | } | 
|  | 90 | return total_tree_block_count * INCFS_DATA_FILE_BLOCK_SIZE; | 
|  | 91 | } | 
|  | 92 |  | 
|  | 93 | // Base64-encode signature bytes. Keeping fd at the position of start of verity tree. | 
|  | 94 | static std::pair<unique_fd, std::string> read_and_encode_signature(Size file_size, | 
|  | 95 | std::string signature_file) { | 
|  | 96 | signature_file += IDSIG; | 
|  | 97 |  | 
|  | 98 | struct stat st; | 
|  | 99 | if (stat(signature_file.c_str(), &st)) { | 
|  | 100 | fprintf(stderr, "Failed to stat signature file %s. Abort.\n", signature_file.c_str()); | 
|  | 101 | return {}; | 
|  | 102 | } | 
|  | 103 |  | 
|  | 104 | unique_fd fd(adb_open(signature_file.c_str(), O_RDONLY | O_CLOEXEC)); | 
|  | 105 | if (fd < 0) { | 
|  | 106 | fprintf(stderr, "Failed to open signature file: %s. Abort.\n", signature_file.c_str()); | 
|  | 107 | return {}; | 
|  | 108 | } | 
|  | 109 |  | 
|  | 110 | auto [signature, tree_size] = read_id_sig_headers(fd); | 
|  | 111 | if (auto expected = verity_tree_size_for_file(file_size); tree_size != expected) { | 
|  | 112 | fprintf(stderr, | 
|  | 113 | "Verity tree size mismatch in signature file: %s [was %lld, expected %lld].\n", | 
|  | 114 | signature_file.c_str(), (long long)tree_size, (long long)expected); | 
|  | 115 | return {}; | 
|  | 116 | } | 
|  | 117 |  | 
|  | 118 | size_t base64_len = 0; | 
|  | 119 | if (!EVP_EncodedLength(&base64_len, signature.size())) { | 
|  | 120 | fprintf(stderr, "Fail to estimate base64 encoded length. Abort.\n"); | 
|  | 121 | return {}; | 
|  | 122 | } | 
|  | 123 | std::string encoded_signature; | 
|  | 124 | encoded_signature.resize(base64_len); | 
|  | 125 | encoded_signature.resize(EVP_EncodeBlock((uint8_t*)encoded_signature.data(), | 
|  | 126 | (const uint8_t*)signature.data(), signature.size())); | 
|  | 127 |  | 
|  | 128 | return {std::move(fd), std::move(encoded_signature)}; | 
|  | 129 | } | 
|  | 130 |  | 
|  | 131 | // Send install-incremental to the device along with properly configured file descriptors in | 
|  | 132 | // streaming format. Once connection established, send all fs-verity tree bytes. | 
|  | 133 | static unique_fd start_install(const std::vector<std::string>& files) { | 
|  | 134 | std::vector<std::string> command_args{"package", "install-incremental"}; | 
|  | 135 |  | 
|  | 136 | // fd's with positions at the beginning of fs-verity | 
|  | 137 | std::vector<unique_fd> signature_fds; | 
|  | 138 | signature_fds.reserve(files.size()); | 
|  | 139 | for (int i = 0, size = files.size(); i < size; ++i) { | 
|  | 140 | const auto& file = files[i]; | 
|  | 141 |  | 
|  | 142 | struct stat st; | 
|  | 143 | if (stat(file.c_str(), &st)) { | 
|  | 144 | fprintf(stderr, "Failed to stat input file %s. Abort.\n", file.c_str()); | 
|  | 145 | return {}; | 
|  | 146 | } | 
|  | 147 |  | 
|  | 148 | auto [signature_fd, signature] = read_and_encode_signature(st.st_size, file); | 
|  | 149 | if (!signature_fd.ok()) { | 
|  | 150 | return {}; | 
|  | 151 | } | 
|  | 152 |  | 
|  | 153 | auto file_desc = | 
|  | 154 | StringPrintf("%s:%lld:%s:%s", android::base::Basename(file).c_str(), | 
|  | 155 | (long long)st.st_size, std::to_string(i).c_str(), signature.c_str()); | 
|  | 156 | command_args.push_back(std::move(file_desc)); | 
|  | 157 |  | 
|  | 158 | signature_fds.push_back(std::move(signature_fd)); | 
|  | 159 | } | 
|  | 160 |  | 
|  | 161 | std::string error; | 
|  | 162 | auto connection_fd = unique_fd(send_abb_exec_command(command_args, &error)); | 
|  | 163 | if (connection_fd < 0) { | 
|  | 164 | fprintf(stderr, "Failed to run: %s, error: %s\n", | 
|  | 165 | android::base::Join(command_args, " ").c_str(), error.c_str()); | 
|  | 166 | return {}; | 
|  | 167 | } | 
|  | 168 |  | 
|  | 169 | // Pushing verity trees for all installation files. | 
|  | 170 | for (auto&& local_fd : signature_fds) { | 
|  | 171 | if (!copy_to_file(local_fd.get(), connection_fd.get())) { | 
|  | 172 | fprintf(stderr, "Failed to stream tree bytes: %s. Abort.\n", strerror(errno)); | 
|  | 173 | return {}; | 
|  | 174 | } | 
|  | 175 | } | 
|  | 176 |  | 
|  | 177 | return connection_fd; | 
|  | 178 | } | 
|  | 179 |  | 
|  | 180 | }  // namespace | 
|  | 181 |  | 
|  | 182 | std::optional<Process> install(std::vector<std::string> files) { | 
|  | 183 | auto connection_fd = start_install(files); | 
|  | 184 | if (connection_fd < 0) { | 
|  | 185 | fprintf(stderr, "adb: failed to initiate installation on device.\n"); | 
|  | 186 | return {}; | 
|  | 187 | } | 
|  | 188 |  | 
|  | 189 | std::string adb_path = android::base::GetExecutablePath(); | 
|  | 190 |  | 
|  | 191 | auto osh = adb_get_os_handle(connection_fd.get()); | 
|  | 192 | #ifdef _WIN32 | 
|  | 193 | auto fd_param = std::to_string(reinterpret_cast<intptr_t>(osh)); | 
|  | 194 | #else /* !_WIN32 a.k.a. Unix */ | 
|  | 195 | auto fd_param = std::to_string(osh); | 
|  | 196 | #endif | 
|  | 197 |  | 
| Songchun Fan | c4a9d16 | 2020-03-09 11:33:44 -0700 | [diff] [blame] | 198 | // pipe for child process to write output | 
|  | 199 | int print_fds[2]; | 
|  | 200 | if (adb_socketpair(print_fds) != 0) { | 
|  | 201 | fprintf(stderr, "Failed to create socket pair for child to print to parent\n"); | 
|  | 202 | return {}; | 
|  | 203 | } | 
|  | 204 | auto [pipe_read_fd, pipe_write_fd] = print_fds; | 
| Yurii Zubrytskyi | c3ac339 | 2020-03-18 15:49:45 -0700 | [diff] [blame] | 205 | auto pipe_write_fd_param = std::to_string(intptr_t(adb_get_os_handle(pipe_write_fd))); | 
| Songchun Fan | c4a9d16 | 2020-03-09 11:33:44 -0700 | [diff] [blame] | 206 | close_on_exec(pipe_read_fd); | 
|  | 207 |  | 
| Alex Buynytskyy | 96ff54b | 2020-02-13 06:52:04 -0800 | [diff] [blame] | 208 | std::vector<std::string> args(std::move(files)); | 
| Songchun Fan | c4a9d16 | 2020-03-09 11:33:44 -0700 | [diff] [blame] | 209 | args.insert(args.begin(), {"inc-server", fd_param, pipe_write_fd_param}); | 
|  | 210 | auto child = | 
|  | 211 | adb_launch_process(adb_path, std::move(args), {connection_fd.get(), pipe_write_fd}); | 
| Alex Buynytskyy | 96ff54b | 2020-02-13 06:52:04 -0800 | [diff] [blame] | 212 | if (!child) { | 
|  | 213 | fprintf(stderr, "adb: failed to fork: %s\n", strerror(errno)); | 
|  | 214 | return {}; | 
|  | 215 | } | 
|  | 216 |  | 
| Songchun Fan | c4a9d16 | 2020-03-09 11:33:44 -0700 | [diff] [blame] | 217 | adb_close(pipe_write_fd); | 
|  | 218 |  | 
| Alex Buynytskyy | 96ff54b | 2020-02-13 06:52:04 -0800 | [diff] [blame] | 219 | auto killOnExit = [](Process* p) { p->kill(); }; | 
|  | 220 | std::unique_ptr<Process, decltype(killOnExit)> serverKiller(&child, killOnExit); | 
| Alex Buynytskyy | 96ff54b | 2020-02-13 06:52:04 -0800 | [diff] [blame] | 221 |  | 
| Songchun Fan | c4a9d16 | 2020-03-09 11:33:44 -0700 | [diff] [blame] | 222 | Result result = wait_for_installation(pipe_read_fd); | 
|  | 223 | adb_close(pipe_read_fd); | 
|  | 224 |  | 
|  | 225 | if (result == Result::Success) { | 
|  | 226 | // adb client exits now but inc-server can continue | 
|  | 227 | serverKiller.release(); | 
|  | 228 | } | 
| Alex Buynytskyy | 96ff54b | 2020-02-13 06:52:04 -0800 | [diff] [blame] | 229 | return child; | 
|  | 230 | } | 
|  | 231 |  | 
| Songchun Fan | c4a9d16 | 2020-03-09 11:33:44 -0700 | [diff] [blame] | 232 | Result wait_for_installation(int read_fd) { | 
|  | 233 | static constexpr int maxMessageSize = 256; | 
|  | 234 | std::vector<char> child_stdout(CHUNK_SIZE); | 
|  | 235 | int bytes_read; | 
|  | 236 | int buf_size = 0; | 
|  | 237 | // TODO(b/150865433): optimize child's output parsing | 
|  | 238 | while ((bytes_read = adb_read(read_fd, child_stdout.data() + buf_size, | 
|  | 239 | child_stdout.size() - buf_size)) > 0) { | 
|  | 240 | // print to parent's stdout | 
|  | 241 | fprintf(stdout, "%.*s", bytes_read, child_stdout.data() + buf_size); | 
|  | 242 |  | 
|  | 243 | buf_size += bytes_read; | 
|  | 244 | const std::string_view stdout_str(child_stdout.data(), buf_size); | 
|  | 245 | // wait till installation either succeeds or fails | 
|  | 246 | if (stdout_str.find("Success") != std::string::npos) { | 
|  | 247 | return Result::Success; | 
|  | 248 | } | 
|  | 249 | // on failure, wait for full message | 
|  | 250 | static constexpr auto failure_msg_head = "Failure ["sv; | 
|  | 251 | if (const auto begin_itr = stdout_str.find(failure_msg_head); | 
|  | 252 | begin_itr != std::string::npos) { | 
|  | 253 | if (buf_size >= maxMessageSize) { | 
|  | 254 | return Result::Failure; | 
|  | 255 | } | 
|  | 256 | const auto end_itr = stdout_str.rfind("]"); | 
|  | 257 | if (end_itr != std::string::npos && end_itr >= begin_itr + failure_msg_head.size()) { | 
|  | 258 | return Result::Failure; | 
|  | 259 | } | 
|  | 260 | } | 
|  | 261 | child_stdout.resize(buf_size + CHUNK_SIZE); | 
|  | 262 | } | 
|  | 263 | return Result::None; | 
|  | 264 | } | 
|  | 265 |  | 
| Alex Buynytskyy | 96ff54b | 2020-02-13 06:52:04 -0800 | [diff] [blame] | 266 | }  // namespace incremental |