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