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 | |
Alex Buynytskyy | 82c1c97 | 2020-03-27 14:34:21 -0700 | [diff] [blame^] | 19 | #include "incremental_utils.h" |
| 20 | |
Alex Buynytskyy | 96ff54b | 2020-02-13 06:52:04 -0800 | [diff] [blame] | 21 | #include <android-base/file.h> |
| 22 | #include <android-base/stringprintf.h> |
| 23 | #include <openssl/base64.h> |
| 24 | |
| 25 | #include "adb_client.h" |
Alex Buynytskyy | 96ff54b | 2020-02-13 06:52:04 -0800 | [diff] [blame] | 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 | |
Alex Buynytskyy | 96ff54b | 2020-02-13 06:52:04 -0800 | [diff] [blame] | 34 | using android::base::StringPrintf; |
| 35 | |
Yurii Zubrytskyi | 4532ea8 | 2020-03-26 18:20:39 -0700 | [diff] [blame] | 36 | // Read, verify and return the signature bytes. Keeping fd at the position of start of verity tree. |
| 37 | static std::pair<unique_fd, std::vector<char>> read_signature(Size file_size, |
| 38 | std::string signature_file, |
| 39 | bool silent) { |
Alex Buynytskyy | 96ff54b | 2020-02-13 06:52:04 -0800 | [diff] [blame] | 40 | signature_file += IDSIG; |
| 41 | |
| 42 | struct stat st; |
| 43 | if (stat(signature_file.c_str(), &st)) { |
Yurii Zubrytskyi | 4532ea8 | 2020-03-26 18:20:39 -0700 | [diff] [blame] | 44 | if (!silent) { |
| 45 | fprintf(stderr, "Failed to stat signature file %s. Abort.\n", signature_file.c_str()); |
| 46 | } |
Alex Buynytskyy | 96ff54b | 2020-02-13 06:52:04 -0800 | [diff] [blame] | 47 | return {}; |
| 48 | } |
| 49 | |
Alex Buynytskyy | 82c1c97 | 2020-03-27 14:34:21 -0700 | [diff] [blame^] | 50 | unique_fd fd(adb_open(signature_file.c_str(), O_RDONLY)); |
Alex Buynytskyy | 96ff54b | 2020-02-13 06:52:04 -0800 | [diff] [blame] | 51 | if (fd < 0) { |
Yurii Zubrytskyi | 4532ea8 | 2020-03-26 18:20:39 -0700 | [diff] [blame] | 52 | if (!silent) { |
| 53 | fprintf(stderr, "Failed to open signature file: %s. Abort.\n", signature_file.c_str()); |
| 54 | } |
Alex Buynytskyy | 96ff54b | 2020-02-13 06:52:04 -0800 | [diff] [blame] | 55 | return {}; |
| 56 | } |
| 57 | |
| 58 | auto [signature, tree_size] = read_id_sig_headers(fd); |
| 59 | if (auto expected = verity_tree_size_for_file(file_size); tree_size != expected) { |
Yurii Zubrytskyi | 4532ea8 | 2020-03-26 18:20:39 -0700 | [diff] [blame] | 60 | if (!silent) { |
| 61 | fprintf(stderr, |
| 62 | "Verity tree size mismatch in signature file: %s [was %lld, expected %lld].\n", |
| 63 | signature_file.c_str(), (long long)tree_size, (long long)expected); |
| 64 | } |
| 65 | return {}; |
| 66 | } |
| 67 | |
| 68 | return {std::move(fd), std::move(signature)}; |
| 69 | } |
| 70 | |
| 71 | // Base64-encode signature bytes. Keeping fd at the position of start of verity tree. |
| 72 | static std::pair<unique_fd, std::string> read_and_encode_signature(Size file_size, |
| 73 | std::string signature_file, |
| 74 | bool silent) { |
| 75 | auto [fd, signature] = read_signature(file_size, std::move(signature_file), silent); |
| 76 | if (!fd.ok()) { |
Alex Buynytskyy | 96ff54b | 2020-02-13 06:52:04 -0800 | [diff] [blame] | 77 | return {}; |
| 78 | } |
| 79 | |
| 80 | size_t base64_len = 0; |
| 81 | if (!EVP_EncodedLength(&base64_len, signature.size())) { |
Yurii Zubrytskyi | 4532ea8 | 2020-03-26 18:20:39 -0700 | [diff] [blame] | 82 | if (!silent) { |
| 83 | fprintf(stderr, "Fail to estimate base64 encoded length. Abort.\n"); |
| 84 | } |
Alex Buynytskyy | 96ff54b | 2020-02-13 06:52:04 -0800 | [diff] [blame] | 85 | return {}; |
| 86 | } |
Yurii Zubrytskyi | 4532ea8 | 2020-03-26 18:20:39 -0700 | [diff] [blame] | 87 | std::string encoded_signature(base64_len, '\0'); |
Alex Buynytskyy | 96ff54b | 2020-02-13 06:52:04 -0800 | [diff] [blame] | 88 | encoded_signature.resize(EVP_EncodeBlock((uint8_t*)encoded_signature.data(), |
| 89 | (const uint8_t*)signature.data(), signature.size())); |
| 90 | |
| 91 | return {std::move(fd), std::move(encoded_signature)}; |
| 92 | } |
| 93 | |
| 94 | // Send install-incremental to the device along with properly configured file descriptors in |
| 95 | // streaming format. Once connection established, send all fs-verity tree bytes. |
Yurii Zubrytskyi | 4532ea8 | 2020-03-26 18:20:39 -0700 | [diff] [blame] | 96 | static unique_fd start_install(const Files& files, bool silent) { |
Alex Buynytskyy | 96ff54b | 2020-02-13 06:52:04 -0800 | [diff] [blame] | 97 | std::vector<std::string> command_args{"package", "install-incremental"}; |
| 98 | |
| 99 | // fd's with positions at the beginning of fs-verity |
| 100 | std::vector<unique_fd> signature_fds; |
| 101 | signature_fds.reserve(files.size()); |
| 102 | for (int i = 0, size = files.size(); i < size; ++i) { |
| 103 | const auto& file = files[i]; |
| 104 | |
| 105 | struct stat st; |
| 106 | if (stat(file.c_str(), &st)) { |
Yurii Zubrytskyi | 4532ea8 | 2020-03-26 18:20:39 -0700 | [diff] [blame] | 107 | if (!silent) { |
| 108 | fprintf(stderr, "Failed to stat input file %s. Abort.\n", file.c_str()); |
| 109 | } |
Alex Buynytskyy | 96ff54b | 2020-02-13 06:52:04 -0800 | [diff] [blame] | 110 | return {}; |
| 111 | } |
| 112 | |
Yurii Zubrytskyi | 4532ea8 | 2020-03-26 18:20:39 -0700 | [diff] [blame] | 113 | auto [signature_fd, signature] = read_and_encode_signature(st.st_size, file, silent); |
Alex Buynytskyy | 96ff54b | 2020-02-13 06:52:04 -0800 | [diff] [blame] | 114 | if (!signature_fd.ok()) { |
| 115 | return {}; |
| 116 | } |
| 117 | |
Alex Buynytskyy | 82c1c97 | 2020-03-27 14:34:21 -0700 | [diff] [blame^] | 118 | auto file_desc = StringPrintf("%s:%lld:%d:%s:1", android::base::Basename(file).c_str(), |
| 119 | (long long)st.st_size, i, signature.c_str()); |
Alex Buynytskyy | 96ff54b | 2020-02-13 06:52:04 -0800 | [diff] [blame] | 120 | command_args.push_back(std::move(file_desc)); |
| 121 | |
| 122 | signature_fds.push_back(std::move(signature_fd)); |
| 123 | } |
| 124 | |
| 125 | std::string error; |
| 126 | auto connection_fd = unique_fd(send_abb_exec_command(command_args, &error)); |
| 127 | if (connection_fd < 0) { |
Yurii Zubrytskyi | 4532ea8 | 2020-03-26 18:20:39 -0700 | [diff] [blame] | 128 | if (!silent) { |
| 129 | fprintf(stderr, "Failed to run: %s, error: %s\n", |
| 130 | android::base::Join(command_args, " ").c_str(), error.c_str()); |
| 131 | } |
Alex Buynytskyy | 96ff54b | 2020-02-13 06:52:04 -0800 | [diff] [blame] | 132 | return {}; |
| 133 | } |
| 134 | |
Alex Buynytskyy | 96ff54b | 2020-02-13 06:52:04 -0800 | [diff] [blame] | 135 | return connection_fd; |
| 136 | } |
| 137 | |
Yurii Zubrytskyi | 4532ea8 | 2020-03-26 18:20:39 -0700 | [diff] [blame] | 138 | bool can_install(const Files& files) { |
| 139 | for (const auto& file : files) { |
| 140 | struct stat st; |
| 141 | if (stat(file.c_str(), &st)) { |
| 142 | return false; |
| 143 | } |
| 144 | |
| 145 | auto [fd, _] = read_signature(st.st_size, file, true); |
| 146 | if (!fd.ok()) { |
| 147 | return false; |
| 148 | } |
| 149 | } |
| 150 | return true; |
| 151 | } |
| 152 | |
| 153 | std::optional<Process> install(const Files& files, bool silent) { |
| 154 | auto connection_fd = start_install(files, silent); |
Alex Buynytskyy | 96ff54b | 2020-02-13 06:52:04 -0800 | [diff] [blame] | 155 | if (connection_fd < 0) { |
Yurii Zubrytskyi | 4532ea8 | 2020-03-26 18:20:39 -0700 | [diff] [blame] | 156 | if (!silent) { |
| 157 | fprintf(stderr, "adb: failed to initiate installation on device.\n"); |
| 158 | } |
Alex Buynytskyy | 96ff54b | 2020-02-13 06:52:04 -0800 | [diff] [blame] | 159 | return {}; |
| 160 | } |
| 161 | |
| 162 | std::string adb_path = android::base::GetExecutablePath(); |
| 163 | |
Yurii Zubrytskyi | e3e64b8 | 2020-03-26 18:16:36 -0700 | [diff] [blame] | 164 | auto osh = cast_handle_to_int(adb_get_os_handle(connection_fd.get())); |
Alex Buynytskyy | 96ff54b | 2020-02-13 06:52:04 -0800 | [diff] [blame] | 165 | auto fd_param = std::to_string(osh); |
Alex Buynytskyy | 96ff54b | 2020-02-13 06:52:04 -0800 | [diff] [blame] | 166 | |
Songchun Fan | 23bd552 | 2020-03-09 11:33:44 -0700 | [diff] [blame] | 167 | // pipe for child process to write output |
| 168 | int print_fds[2]; |
| 169 | if (adb_socketpair(print_fds) != 0) { |
Yurii Zubrytskyi | 4532ea8 | 2020-03-26 18:20:39 -0700 | [diff] [blame] | 170 | if (!silent) { |
| 171 | fprintf(stderr, "Failed to create socket pair for child to print to parent\n"); |
| 172 | } |
Songchun Fan | 23bd552 | 2020-03-09 11:33:44 -0700 | [diff] [blame] | 173 | return {}; |
| 174 | } |
| 175 | auto [pipe_read_fd, pipe_write_fd] = print_fds; |
Yurii Zubrytskyi | e3e64b8 | 2020-03-26 18:16:36 -0700 | [diff] [blame] | 176 | auto pipe_write_fd_param = std::to_string(cast_handle_to_int(adb_get_os_handle(pipe_write_fd))); |
Songchun Fan | 23bd552 | 2020-03-09 11:33:44 -0700 | [diff] [blame] | 177 | close_on_exec(pipe_read_fd); |
| 178 | |
Alex Buynytskyy | 96ff54b | 2020-02-13 06:52:04 -0800 | [diff] [blame] | 179 | std::vector<std::string> args(std::move(files)); |
Songchun Fan | 23bd552 | 2020-03-09 11:33:44 -0700 | [diff] [blame] | 180 | args.insert(args.begin(), {"inc-server", fd_param, pipe_write_fd_param}); |
| 181 | auto child = |
| 182 | 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] | 183 | if (!child) { |
Yurii Zubrytskyi | 4532ea8 | 2020-03-26 18:20:39 -0700 | [diff] [blame] | 184 | if (!silent) { |
| 185 | fprintf(stderr, "adb: failed to fork: %s\n", strerror(errno)); |
| 186 | } |
Alex Buynytskyy | 96ff54b | 2020-02-13 06:52:04 -0800 | [diff] [blame] | 187 | return {}; |
| 188 | } |
| 189 | |
Songchun Fan | 23bd552 | 2020-03-09 11:33:44 -0700 | [diff] [blame] | 190 | adb_close(pipe_write_fd); |
| 191 | |
Alex Buynytskyy | 96ff54b | 2020-02-13 06:52:04 -0800 | [diff] [blame] | 192 | auto killOnExit = [](Process* p) { p->kill(); }; |
| 193 | std::unique_ptr<Process, decltype(killOnExit)> serverKiller(&child, killOnExit); |
Alex Buynytskyy | 96ff54b | 2020-02-13 06:52:04 -0800 | [diff] [blame] | 194 | |
Songchun Fan | 23bd552 | 2020-03-09 11:33:44 -0700 | [diff] [blame] | 195 | Result result = wait_for_installation(pipe_read_fd); |
| 196 | adb_close(pipe_read_fd); |
| 197 | |
| 198 | if (result == Result::Success) { |
| 199 | // adb client exits now but inc-server can continue |
| 200 | serverKiller.release(); |
| 201 | } |
Alex Buynytskyy | 96ff54b | 2020-02-13 06:52:04 -0800 | [diff] [blame] | 202 | return child; |
| 203 | } |
| 204 | |
Songchun Fan | 23bd552 | 2020-03-09 11:33:44 -0700 | [diff] [blame] | 205 | Result wait_for_installation(int read_fd) { |
| 206 | static constexpr int maxMessageSize = 256; |
| 207 | std::vector<char> child_stdout(CHUNK_SIZE); |
| 208 | int bytes_read; |
| 209 | int buf_size = 0; |
| 210 | // TODO(b/150865433): optimize child's output parsing |
| 211 | while ((bytes_read = adb_read(read_fd, child_stdout.data() + buf_size, |
| 212 | child_stdout.size() - buf_size)) > 0) { |
| 213 | // print to parent's stdout |
| 214 | fprintf(stdout, "%.*s", bytes_read, child_stdout.data() + buf_size); |
| 215 | |
| 216 | buf_size += bytes_read; |
| 217 | const std::string_view stdout_str(child_stdout.data(), buf_size); |
| 218 | // wait till installation either succeeds or fails |
| 219 | if (stdout_str.find("Success") != std::string::npos) { |
| 220 | return Result::Success; |
| 221 | } |
| 222 | // on failure, wait for full message |
| 223 | static constexpr auto failure_msg_head = "Failure ["sv; |
| 224 | if (const auto begin_itr = stdout_str.find(failure_msg_head); |
| 225 | begin_itr != std::string::npos) { |
| 226 | if (buf_size >= maxMessageSize) { |
| 227 | return Result::Failure; |
| 228 | } |
| 229 | const auto end_itr = stdout_str.rfind("]"); |
| 230 | if (end_itr != std::string::npos && end_itr >= begin_itr + failure_msg_head.size()) { |
| 231 | return Result::Failure; |
| 232 | } |
| 233 | } |
| 234 | child_stdout.resize(buf_size + CHUNK_SIZE); |
| 235 | } |
| 236 | return Result::None; |
| 237 | } |
| 238 | |
Alex Buynytskyy | 96ff54b | 2020-02-13 06:52:04 -0800 | [diff] [blame] | 239 | } // namespace incremental |