blob: a8b0ab3d0f30bf11d863c66fa6800607a42a427e [file] [log] [blame]
Alex Buynytskyy96ff54b2020-02-13 06:52:04 -08001/*
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 Buynytskyy82c1c972020-03-27 14:34:21 -070019#include "incremental_utils.h"
20
Alex Buynytskyy96ff54b2020-02-13 06:52:04 -080021#include <android-base/file.h>
22#include <android-base/stringprintf.h>
23#include <openssl/base64.h>
24
25#include "adb_client.h"
Alex Buynytskyy96ff54b2020-02-13 06:52:04 -080026#include "adb_utils.h"
27#include "commandline.h"
28#include "sysdeps.h"
29
Alex Buynytskyy96ff54b2020-02-13 06:52:04 -080030using namespace std::literals;
31
32namespace incremental {
33
Alex Buynytskyy96ff54b2020-02-13 06:52:04 -080034using android::base::StringPrintf;
35
Yurii Zubrytskyi4532ea82020-03-26 18:20:39 -070036// Read, verify and return the signature bytes. Keeping fd at the position of start of verity tree.
37static std::pair<unique_fd, std::vector<char>> read_signature(Size file_size,
38 std::string signature_file,
39 bool silent) {
Alex Buynytskyy96ff54b2020-02-13 06:52:04 -080040 signature_file += IDSIG;
41
42 struct stat st;
43 if (stat(signature_file.c_str(), &st)) {
Yurii Zubrytskyi4532ea82020-03-26 18:20:39 -070044 if (!silent) {
45 fprintf(stderr, "Failed to stat signature file %s. Abort.\n", signature_file.c_str());
46 }
Alex Buynytskyy96ff54b2020-02-13 06:52:04 -080047 return {};
48 }
49
Alex Buynytskyy82c1c972020-03-27 14:34:21 -070050 unique_fd fd(adb_open(signature_file.c_str(), O_RDONLY));
Alex Buynytskyy96ff54b2020-02-13 06:52:04 -080051 if (fd < 0) {
Yurii Zubrytskyi4532ea82020-03-26 18:20:39 -070052 if (!silent) {
53 fprintf(stderr, "Failed to open signature file: %s. Abort.\n", signature_file.c_str());
54 }
Alex Buynytskyy96ff54b2020-02-13 06:52:04 -080055 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 Zubrytskyi4532ea82020-03-26 18:20:39 -070060 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.
72static 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 Buynytskyy96ff54b2020-02-13 06:52:04 -080077 return {};
78 }
79
80 size_t base64_len = 0;
81 if (!EVP_EncodedLength(&base64_len, signature.size())) {
Yurii Zubrytskyi4532ea82020-03-26 18:20:39 -070082 if (!silent) {
83 fprintf(stderr, "Fail to estimate base64 encoded length. Abort.\n");
84 }
Alex Buynytskyy96ff54b2020-02-13 06:52:04 -080085 return {};
86 }
Yurii Zubrytskyi4532ea82020-03-26 18:20:39 -070087 std::string encoded_signature(base64_len, '\0');
Alex Buynytskyy96ff54b2020-02-13 06:52:04 -080088 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.
Alex Buynytskyy04aa5be2020-05-14 13:29:05 -070096static unique_fd start_install(const Files& files, const Args& passthrough_args, bool silent) {
Alex Buynytskyy96ff54b2020-02-13 06:52:04 -080097 std::vector<std::string> command_args{"package", "install-incremental"};
Alex Buynytskyy04aa5be2020-05-14 13:29:05 -070098 command_args.insert(command_args.end(), passthrough_args.begin(), passthrough_args.end());
Alex Buynytskyy96ff54b2020-02-13 06:52:04 -080099
Alex Buynytskyy96ff54b2020-02-13 06:52:04 -0800100 for (int i = 0, size = files.size(); i < size; ++i) {
101 const auto& file = files[i];
102
103 struct stat st;
104 if (stat(file.c_str(), &st)) {
Yurii Zubrytskyi4532ea82020-03-26 18:20:39 -0700105 if (!silent) {
106 fprintf(stderr, "Failed to stat input file %s. Abort.\n", file.c_str());
107 }
Alex Buynytskyy96ff54b2020-02-13 06:52:04 -0800108 return {};
109 }
110
Yurii Zubrytskyi4532ea82020-03-26 18:20:39 -0700111 auto [signature_fd, signature] = read_and_encode_signature(st.st_size, file, silent);
Alex Buynytskyy96ff54b2020-02-13 06:52:04 -0800112 if (!signature_fd.ok()) {
113 return {};
114 }
115
Alex Buynytskyy82c1c972020-03-27 14:34:21 -0700116 auto file_desc = StringPrintf("%s:%lld:%d:%s:1", android::base::Basename(file).c_str(),
117 (long long)st.st_size, i, signature.c_str());
Alex Buynytskyy96ff54b2020-02-13 06:52:04 -0800118 command_args.push_back(std::move(file_desc));
Alex Buynytskyy96ff54b2020-02-13 06:52:04 -0800119 }
120
121 std::string error;
122 auto connection_fd = unique_fd(send_abb_exec_command(command_args, &error));
123 if (connection_fd < 0) {
Yurii Zubrytskyi4532ea82020-03-26 18:20:39 -0700124 if (!silent) {
125 fprintf(stderr, "Failed to run: %s, error: %s\n",
126 android::base::Join(command_args, " ").c_str(), error.c_str());
127 }
Alex Buynytskyy96ff54b2020-02-13 06:52:04 -0800128 return {};
129 }
130
Alex Buynytskyy96ff54b2020-02-13 06:52:04 -0800131 return connection_fd;
132}
133
Yurii Zubrytskyi4532ea82020-03-26 18:20:39 -0700134bool can_install(const Files& files) {
135 for (const auto& file : files) {
136 struct stat st;
137 if (stat(file.c_str(), &st)) {
138 return false;
139 }
140
141 auto [fd, _] = read_signature(st.st_size, file, true);
142 if (!fd.ok()) {
143 return false;
144 }
145 }
146 return true;
147}
148
Alex Buynytskyy04aa5be2020-05-14 13:29:05 -0700149std::optional<Process> install(const Files& files, const Args& passthrough_args, bool silent) {
150 auto connection_fd = start_install(files, passthrough_args, silent);
Alex Buynytskyy96ff54b2020-02-13 06:52:04 -0800151 if (connection_fd < 0) {
Yurii Zubrytskyi4532ea82020-03-26 18:20:39 -0700152 if (!silent) {
153 fprintf(stderr, "adb: failed to initiate installation on device.\n");
154 }
Alex Buynytskyy96ff54b2020-02-13 06:52:04 -0800155 return {};
156 }
157
158 std::string adb_path = android::base::GetExecutablePath();
159
Yurii Zubrytskyie3e64b82020-03-26 18:16:36 -0700160 auto osh = cast_handle_to_int(adb_get_os_handle(connection_fd.get()));
Alex Buynytskyy96ff54b2020-02-13 06:52:04 -0800161 auto fd_param = std::to_string(osh);
Alex Buynytskyy96ff54b2020-02-13 06:52:04 -0800162
Songchun Fan23bd5522020-03-09 11:33:44 -0700163 // pipe for child process to write output
164 int print_fds[2];
165 if (adb_socketpair(print_fds) != 0) {
Yurii Zubrytskyi4532ea82020-03-26 18:20:39 -0700166 if (!silent) {
167 fprintf(stderr, "Failed to create socket pair for child to print to parent\n");
168 }
Songchun Fan23bd5522020-03-09 11:33:44 -0700169 return {};
170 }
171 auto [pipe_read_fd, pipe_write_fd] = print_fds;
Yurii Zubrytskyie3e64b82020-03-26 18:16:36 -0700172 auto pipe_write_fd_param = std::to_string(cast_handle_to_int(adb_get_os_handle(pipe_write_fd)));
Songchun Fan23bd5522020-03-09 11:33:44 -0700173 close_on_exec(pipe_read_fd);
174
Alex Buynytskyy96ff54b2020-02-13 06:52:04 -0800175 std::vector<std::string> args(std::move(files));
Songchun Fan23bd5522020-03-09 11:33:44 -0700176 args.insert(args.begin(), {"inc-server", fd_param, pipe_write_fd_param});
177 auto child =
178 adb_launch_process(adb_path, std::move(args), {connection_fd.get(), pipe_write_fd});
Alex Buynytskyy96ff54b2020-02-13 06:52:04 -0800179 if (!child) {
Yurii Zubrytskyi4532ea82020-03-26 18:20:39 -0700180 if (!silent) {
181 fprintf(stderr, "adb: failed to fork: %s\n", strerror(errno));
182 }
Alex Buynytskyy96ff54b2020-02-13 06:52:04 -0800183 return {};
184 }
185
Songchun Fan23bd5522020-03-09 11:33:44 -0700186 adb_close(pipe_write_fd);
187
Alex Buynytskyy96ff54b2020-02-13 06:52:04 -0800188 auto killOnExit = [](Process* p) { p->kill(); };
189 std::unique_ptr<Process, decltype(killOnExit)> serverKiller(&child, killOnExit);
Alex Buynytskyy96ff54b2020-02-13 06:52:04 -0800190
Songchun Fan23bd5522020-03-09 11:33:44 -0700191 Result result = wait_for_installation(pipe_read_fd);
192 adb_close(pipe_read_fd);
193
194 if (result == Result::Success) {
195 // adb client exits now but inc-server can continue
196 serverKiller.release();
197 }
Alex Buynytskyy96ff54b2020-02-13 06:52:04 -0800198 return child;
199}
200
Songchun Fan23bd5522020-03-09 11:33:44 -0700201Result wait_for_installation(int read_fd) {
202 static constexpr int maxMessageSize = 256;
203 std::vector<char> child_stdout(CHUNK_SIZE);
204 int bytes_read;
205 int buf_size = 0;
206 // TODO(b/150865433): optimize child's output parsing
207 while ((bytes_read = adb_read(read_fd, child_stdout.data() + buf_size,
208 child_stdout.size() - buf_size)) > 0) {
209 // print to parent's stdout
210 fprintf(stdout, "%.*s", bytes_read, child_stdout.data() + buf_size);
211
212 buf_size += bytes_read;
213 const std::string_view stdout_str(child_stdout.data(), buf_size);
214 // wait till installation either succeeds or fails
215 if (stdout_str.find("Success") != std::string::npos) {
216 return Result::Success;
217 }
218 // on failure, wait for full message
219 static constexpr auto failure_msg_head = "Failure ["sv;
220 if (const auto begin_itr = stdout_str.find(failure_msg_head);
221 begin_itr != std::string::npos) {
222 if (buf_size >= maxMessageSize) {
223 return Result::Failure;
224 }
225 const auto end_itr = stdout_str.rfind("]");
226 if (end_itr != std::string::npos && end_itr >= begin_itr + failure_msg_head.size()) {
227 return Result::Failure;
228 }
229 }
230 child_stdout.resize(buf_size + CHUNK_SIZE);
231 }
232 return Result::None;
233}
234
Alex Buynytskyy96ff54b2020-02-13 06:52:04 -0800235} // namespace incremental