blob: 3033059d018e052a59cfc4d308ec8c5a1653a451 [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) {
Alex Buynytskyy7405a922020-05-18 06:26:35 -070045 fprintf(stderr, "Failed to stat signature file %s.\n", signature_file.c_str());
Yurii Zubrytskyi4532ea82020-03-26 18:20:39 -070046 }
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) {
Alex Buynytskyy7405a922020-05-18 06:26:35 -070053 fprintf(stderr, "Failed to open signature file: %s.\n", signature_file.c_str());
Yurii Zubrytskyi4532ea82020-03-26 18:20:39 -070054 }
Alex Buynytskyy96ff54b2020-02-13 06:52:04 -080055 return {};
56 }
57
Alex Buynytskyy7405a922020-05-18 06:26:35 -070058 std::vector<char> invalid_signature;
59
60 if (st.st_size > kMaxSignatureSize) {
61 if (!silent) {
62 fprintf(stderr, "Signature is too long. Max allowed is %d. Abort.\n",
63 kMaxSignatureSize);
64 }
65 return {std::move(fd), std::move(invalid_signature)};
66 }
67
Alex Buynytskyy96ff54b2020-02-13 06:52:04 -080068 auto [signature, tree_size] = read_id_sig_headers(fd);
69 if (auto expected = verity_tree_size_for_file(file_size); tree_size != expected) {
Yurii Zubrytskyi4532ea82020-03-26 18:20:39 -070070 if (!silent) {
71 fprintf(stderr,
72 "Verity tree size mismatch in signature file: %s [was %lld, expected %lld].\n",
73 signature_file.c_str(), (long long)tree_size, (long long)expected);
74 }
Alex Buynytskyy7405a922020-05-18 06:26:35 -070075 return {std::move(fd), std::move(invalid_signature)};
Yurii Zubrytskyi4532ea82020-03-26 18:20:39 -070076 }
77
78 return {std::move(fd), std::move(signature)};
79}
80
81// Base64-encode signature bytes. Keeping fd at the position of start of verity tree.
82static std::pair<unique_fd, std::string> read_and_encode_signature(Size file_size,
83 std::string signature_file,
84 bool silent) {
Alex Buynytskyy7405a922020-05-18 06:26:35 -070085 std::string encoded_signature;
86
Yurii Zubrytskyi4532ea82020-03-26 18:20:39 -070087 auto [fd, signature] = read_signature(file_size, std::move(signature_file), silent);
Alex Buynytskyy7405a922020-05-18 06:26:35 -070088 if (!fd.ok() || signature.empty()) {
89 return {std::move(fd), std::move(encoded_signature)};
Alex Buynytskyy96ff54b2020-02-13 06:52:04 -080090 }
91
92 size_t base64_len = 0;
93 if (!EVP_EncodedLength(&base64_len, signature.size())) {
Yurii Zubrytskyi4532ea82020-03-26 18:20:39 -070094 if (!silent) {
95 fprintf(stderr, "Fail to estimate base64 encoded length. Abort.\n");
96 }
Alex Buynytskyy7405a922020-05-18 06:26:35 -070097 return {std::move(fd), std::move(encoded_signature)};
Alex Buynytskyy96ff54b2020-02-13 06:52:04 -080098 }
Alex Buynytskyy7405a922020-05-18 06:26:35 -070099
100 encoded_signature.resize(base64_len, '\0');
Alex Buynytskyy96ff54b2020-02-13 06:52:04 -0800101 encoded_signature.resize(EVP_EncodeBlock((uint8_t*)encoded_signature.data(),
102 (const uint8_t*)signature.data(), signature.size()));
103
104 return {std::move(fd), std::move(encoded_signature)};
105}
106
107// Send install-incremental to the device along with properly configured file descriptors in
108// streaming format. Once connection established, send all fs-verity tree bytes.
Alex Buynytskyy04aa5be2020-05-14 13:29:05 -0700109static unique_fd start_install(const Files& files, const Args& passthrough_args, bool silent) {
Alex Buynytskyy96ff54b2020-02-13 06:52:04 -0800110 std::vector<std::string> command_args{"package", "install-incremental"};
Alex Buynytskyy04aa5be2020-05-14 13:29:05 -0700111 command_args.insert(command_args.end(), passthrough_args.begin(), passthrough_args.end());
Alex Buynytskyy96ff54b2020-02-13 06:52:04 -0800112
Alex Buynytskyy96ff54b2020-02-13 06:52:04 -0800113 for (int i = 0, size = files.size(); i < size; ++i) {
114 const auto& file = files[i];
115
116 struct stat st;
117 if (stat(file.c_str(), &st)) {
Yurii Zubrytskyi4532ea82020-03-26 18:20:39 -0700118 if (!silent) {
119 fprintf(stderr, "Failed to stat input file %s. Abort.\n", file.c_str());
120 }
Alex Buynytskyy96ff54b2020-02-13 06:52:04 -0800121 return {};
122 }
123
Yurii Zubrytskyi4532ea82020-03-26 18:20:39 -0700124 auto [signature_fd, signature] = read_and_encode_signature(st.st_size, file, silent);
Alex Buynytskyy7405a922020-05-18 06:26:35 -0700125 if (signature_fd.ok() && signature.empty()) {
Alex Buynytskyy96ff54b2020-02-13 06:52:04 -0800126 return {};
127 }
128
Alex Buynytskyy82c1c972020-03-27 14:34:21 -0700129 auto file_desc = StringPrintf("%s:%lld:%d:%s:1", android::base::Basename(file).c_str(),
130 (long long)st.st_size, i, signature.c_str());
Alex Buynytskyy96ff54b2020-02-13 06:52:04 -0800131 command_args.push_back(std::move(file_desc));
Alex Buynytskyy96ff54b2020-02-13 06:52:04 -0800132 }
133
134 std::string error;
135 auto connection_fd = unique_fd(send_abb_exec_command(command_args, &error));
136 if (connection_fd < 0) {
Yurii Zubrytskyi4532ea82020-03-26 18:20:39 -0700137 if (!silent) {
138 fprintf(stderr, "Failed to run: %s, error: %s\n",
139 android::base::Join(command_args, " ").c_str(), error.c_str());
140 }
Alex Buynytskyy96ff54b2020-02-13 06:52:04 -0800141 return {};
142 }
143
Alex Buynytskyy96ff54b2020-02-13 06:52:04 -0800144 return connection_fd;
145}
146
Yurii Zubrytskyi4532ea82020-03-26 18:20:39 -0700147bool can_install(const Files& files) {
148 for (const auto& file : files) {
149 struct stat st;
150 if (stat(file.c_str(), &st)) {
151 return false;
152 }
153
Alex Buynytskyy7405a922020-05-18 06:26:35 -0700154 if (android::base::EndsWithIgnoreCase(file, ".apk")) {
155 // Signature has to be present for APKs.
156 auto [fd, _] = read_signature(st.st_size, file, /*silent=*/true);
157 if (!fd.ok()) {
158 return false;
159 }
Yurii Zubrytskyi4532ea82020-03-26 18:20:39 -0700160 }
161 }
162 return true;
163}
164
Alex Buynytskyy04aa5be2020-05-14 13:29:05 -0700165std::optional<Process> install(const Files& files, const Args& passthrough_args, bool silent) {
166 auto connection_fd = start_install(files, passthrough_args, silent);
Alex Buynytskyy96ff54b2020-02-13 06:52:04 -0800167 if (connection_fd < 0) {
Yurii Zubrytskyi4532ea82020-03-26 18:20:39 -0700168 if (!silent) {
169 fprintf(stderr, "adb: failed to initiate installation on device.\n");
170 }
Alex Buynytskyy96ff54b2020-02-13 06:52:04 -0800171 return {};
172 }
173
174 std::string adb_path = android::base::GetExecutablePath();
175
Yurii Zubrytskyie3e64b82020-03-26 18:16:36 -0700176 auto osh = cast_handle_to_int(adb_get_os_handle(connection_fd.get()));
Alex Buynytskyy96ff54b2020-02-13 06:52:04 -0800177 auto fd_param = std::to_string(osh);
Alex Buynytskyy96ff54b2020-02-13 06:52:04 -0800178
Songchun Fan23bd5522020-03-09 11:33:44 -0700179 // pipe for child process to write output
180 int print_fds[2];
181 if (adb_socketpair(print_fds) != 0) {
Yurii Zubrytskyi4532ea82020-03-26 18:20:39 -0700182 if (!silent) {
Yurii Zubrytskyi924915b2020-06-02 23:38:51 -0700183 fprintf(stderr, "adb: failed to create socket pair for child to print to parent\n");
Yurii Zubrytskyi4532ea82020-03-26 18:20:39 -0700184 }
Songchun Fan23bd5522020-03-09 11:33:44 -0700185 return {};
186 }
187 auto [pipe_read_fd, pipe_write_fd] = print_fds;
Yurii Zubrytskyie3e64b82020-03-26 18:16:36 -0700188 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 -0700189 close_on_exec(pipe_read_fd);
190
Alex Buynytskyy96ff54b2020-02-13 06:52:04 -0800191 std::vector<std::string> args(std::move(files));
Songchun Fan23bd5522020-03-09 11:33:44 -0700192 args.insert(args.begin(), {"inc-server", fd_param, pipe_write_fd_param});
193 auto child =
194 adb_launch_process(adb_path, std::move(args), {connection_fd.get(), pipe_write_fd});
Alex Buynytskyy96ff54b2020-02-13 06:52:04 -0800195 if (!child) {
Yurii Zubrytskyi4532ea82020-03-26 18:20:39 -0700196 if (!silent) {
197 fprintf(stderr, "adb: failed to fork: %s\n", strerror(errno));
198 }
Alex Buynytskyy96ff54b2020-02-13 06:52:04 -0800199 return {};
200 }
201
Songchun Fan23bd5522020-03-09 11:33:44 -0700202 adb_close(pipe_write_fd);
203
Alex Buynytskyy96ff54b2020-02-13 06:52:04 -0800204 auto killOnExit = [](Process* p) { p->kill(); };
205 std::unique_ptr<Process, decltype(killOnExit)> serverKiller(&child, killOnExit);
Alex Buynytskyy96ff54b2020-02-13 06:52:04 -0800206
Songchun Fan23bd5522020-03-09 11:33:44 -0700207 Result result = wait_for_installation(pipe_read_fd);
208 adb_close(pipe_read_fd);
209
Yurii Zubrytskyi924915b2020-06-02 23:38:51 -0700210 if (result != Result::Success) {
211 if (!silent) {
212 fprintf(stderr, "adb: install command failed");
213 }
214 return {};
Songchun Fan23bd5522020-03-09 11:33:44 -0700215 }
Yurii Zubrytskyi924915b2020-06-02 23:38:51 -0700216
217 // adb client exits now but inc-server can continue
218 serverKiller.release();
Alex Buynytskyy96ff54b2020-02-13 06:52:04 -0800219 return child;
220}
221
Songchun Fan23bd5522020-03-09 11:33:44 -0700222Result wait_for_installation(int read_fd) {
223 static constexpr int maxMessageSize = 256;
224 std::vector<char> child_stdout(CHUNK_SIZE);
225 int bytes_read;
226 int buf_size = 0;
227 // TODO(b/150865433): optimize child's output parsing
228 while ((bytes_read = adb_read(read_fd, child_stdout.data() + buf_size,
229 child_stdout.size() - buf_size)) > 0) {
230 // print to parent's stdout
231 fprintf(stdout, "%.*s", bytes_read, child_stdout.data() + buf_size);
232
233 buf_size += bytes_read;
234 const std::string_view stdout_str(child_stdout.data(), buf_size);
235 // wait till installation either succeeds or fails
236 if (stdout_str.find("Success") != std::string::npos) {
237 return Result::Success;
238 }
239 // on failure, wait for full message
240 static constexpr auto failure_msg_head = "Failure ["sv;
241 if (const auto begin_itr = stdout_str.find(failure_msg_head);
242 begin_itr != std::string::npos) {
243 if (buf_size >= maxMessageSize) {
244 return Result::Failure;
245 }
246 const auto end_itr = stdout_str.rfind("]");
247 if (end_itr != std::string::npos && end_itr >= begin_itr + failure_msg_head.size()) {
248 return Result::Failure;
249 }
250 }
251 child_stdout.resize(buf_size + CHUNK_SIZE);
252 }
253 return Result::None;
254}
255
Alex Buynytskyy96ff54b2020-02-13 06:52:04 -0800256} // namespace incremental