blob: de93cb750352f6e8a344b6006b35834ddf3fa1ea [file] [log] [blame]
Alex Buynytskyy175ce292020-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 Buynytskyy681eec52020-03-27 14:34:21 -070019#include "incremental_utils.h"
20
Alex Buynytskyy175ce292020-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 Buynytskyy175ce292020-02-13 06:52:04 -080026#include "adb_utils.h"
27#include "commandline.h"
28#include "sysdeps.h"
29
Alex Buynytskyy175ce292020-02-13 06:52:04 -080030using namespace std::literals;
31
32namespace incremental {
33
Alex Buynytskyy175ce292020-02-13 06:52:04 -080034using android::base::StringPrintf;
35
Yurii Zubrytskyi39ee3d82020-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 Buynytskyy175ce292020-02-13 06:52:04 -080040 signature_file += IDSIG;
41
42 struct stat st;
43 if (stat(signature_file.c_str(), &st)) {
Yurii Zubrytskyi39ee3d82020-03-26 18:20:39 -070044 if (!silent) {
Alex Buynytskyy81440832020-05-18 06:26:35 -070045 fprintf(stderr, "Failed to stat signature file %s.\n", signature_file.c_str());
Yurii Zubrytskyi39ee3d82020-03-26 18:20:39 -070046 }
Alex Buynytskyy175ce292020-02-13 06:52:04 -080047 return {};
48 }
49
Alex Buynytskyy681eec52020-03-27 14:34:21 -070050 unique_fd fd(adb_open(signature_file.c_str(), O_RDONLY));
Alex Buynytskyy175ce292020-02-13 06:52:04 -080051 if (fd < 0) {
Yurii Zubrytskyi39ee3d82020-03-26 18:20:39 -070052 if (!silent) {
Alex Buynytskyy81440832020-05-18 06:26:35 -070053 fprintf(stderr, "Failed to open signature file: %s.\n", signature_file.c_str());
Yurii Zubrytskyi39ee3d82020-03-26 18:20:39 -070054 }
Alex Buynytskyy175ce292020-02-13 06:52:04 -080055 return {};
56 }
57
Songchun Fan4abe0db2020-07-27 09:45:33 -070058 auto [signature, tree_size] = read_id_sig_headers(fd);
Alex Buynytskyy81440832020-05-18 06:26:35 -070059
Songchun Fan4abe0db2020-07-27 09:45:33 -070060 std::vector<char> invalid_signature;
Alex Buynytskyy69c06802021-04-19 14:42:17 -070061 if (signature.empty()) {
62 if (!silent) {
63 fprintf(stderr, "Invalid signature format. Abort.\n");
64 }
65 return {std::move(fd), std::move(invalid_signature)};
66 }
Songchun Fan4abe0db2020-07-27 09:45:33 -070067 if (signature.size() > kMaxSignatureSize) {
Alex Buynytskyy81440832020-05-18 06:26:35 -070068 if (!silent) {
Alex Buynytskyy69c06802021-04-19 14:42:17 -070069 fprintf(stderr, "Signature is too long: %lld. Max allowed is %d. Abort.\n",
70 (long long)signature.size(), kMaxSignatureSize);
Alex Buynytskyy81440832020-05-18 06:26:35 -070071 }
72 return {std::move(fd), std::move(invalid_signature)};
73 }
74
Alex Buynytskyy175ce292020-02-13 06:52:04 -080075 if (auto expected = verity_tree_size_for_file(file_size); tree_size != expected) {
Yurii Zubrytskyi39ee3d82020-03-26 18:20:39 -070076 if (!silent) {
77 fprintf(stderr,
78 "Verity tree size mismatch in signature file: %s [was %lld, expected %lld].\n",
79 signature_file.c_str(), (long long)tree_size, (long long)expected);
80 }
Alex Buynytskyy81440832020-05-18 06:26:35 -070081 return {std::move(fd), std::move(invalid_signature)};
Yurii Zubrytskyi39ee3d82020-03-26 18:20:39 -070082 }
83
84 return {std::move(fd), std::move(signature)};
85}
86
87// Base64-encode signature bytes. Keeping fd at the position of start of verity tree.
88static std::pair<unique_fd, std::string> read_and_encode_signature(Size file_size,
89 std::string signature_file,
90 bool silent) {
Alex Buynytskyy81440832020-05-18 06:26:35 -070091 std::string encoded_signature;
92
Yurii Zubrytskyi39ee3d82020-03-26 18:20:39 -070093 auto [fd, signature] = read_signature(file_size, std::move(signature_file), silent);
Alex Buynytskyy81440832020-05-18 06:26:35 -070094 if (!fd.ok() || signature.empty()) {
95 return {std::move(fd), std::move(encoded_signature)};
Alex Buynytskyy175ce292020-02-13 06:52:04 -080096 }
97
98 size_t base64_len = 0;
99 if (!EVP_EncodedLength(&base64_len, signature.size())) {
Yurii Zubrytskyi39ee3d82020-03-26 18:20:39 -0700100 if (!silent) {
101 fprintf(stderr, "Fail to estimate base64 encoded length. Abort.\n");
102 }
Alex Buynytskyy81440832020-05-18 06:26:35 -0700103 return {std::move(fd), std::move(encoded_signature)};
Alex Buynytskyy175ce292020-02-13 06:52:04 -0800104 }
Alex Buynytskyy81440832020-05-18 06:26:35 -0700105
106 encoded_signature.resize(base64_len, '\0');
Alex Buynytskyy175ce292020-02-13 06:52:04 -0800107 encoded_signature.resize(EVP_EncodeBlock((uint8_t*)encoded_signature.data(),
108 (const uint8_t*)signature.data(), signature.size()));
109
110 return {std::move(fd), std::move(encoded_signature)};
111}
112
113// Send install-incremental to the device along with properly configured file descriptors in
114// streaming format. Once connection established, send all fs-verity tree bytes.
Alex Buynytskyy31ff0ca2020-05-14 13:29:05 -0700115static unique_fd start_install(const Files& files, const Args& passthrough_args, bool silent) {
Alex Buynytskyy175ce292020-02-13 06:52:04 -0800116 std::vector<std::string> command_args{"package", "install-incremental"};
Alex Buynytskyy31ff0ca2020-05-14 13:29:05 -0700117 command_args.insert(command_args.end(), passthrough_args.begin(), passthrough_args.end());
Alex Buynytskyy175ce292020-02-13 06:52:04 -0800118
Alex Buynytskyy175ce292020-02-13 06:52:04 -0800119 for (int i = 0, size = files.size(); i < size; ++i) {
120 const auto& file = files[i];
121
122 struct stat st;
123 if (stat(file.c_str(), &st)) {
Yurii Zubrytskyi39ee3d82020-03-26 18:20:39 -0700124 if (!silent) {
125 fprintf(stderr, "Failed to stat input file %s. Abort.\n", file.c_str());
126 }
Alex Buynytskyy175ce292020-02-13 06:52:04 -0800127 return {};
128 }
129
Yurii Zubrytskyi39ee3d82020-03-26 18:20:39 -0700130 auto [signature_fd, signature] = read_and_encode_signature(st.st_size, file, silent);
Alex Buynytskyyf3823372020-08-14 23:45:00 +0000131 if (signature_fd.ok() && signature.empty()) {
Alex Buynytskyy175ce292020-02-13 06:52:04 -0800132 return {};
133 }
134
Alex Buynytskyy681eec52020-03-27 14:34:21 -0700135 auto file_desc = StringPrintf("%s:%lld:%d:%s:1", android::base::Basename(file).c_str(),
136 (long long)st.st_size, i, signature.c_str());
Alex Buynytskyy175ce292020-02-13 06:52:04 -0800137 command_args.push_back(std::move(file_desc));
Alex Buynytskyy175ce292020-02-13 06:52:04 -0800138 }
139
140 std::string error;
141 auto connection_fd = unique_fd(send_abb_exec_command(command_args, &error));
142 if (connection_fd < 0) {
Yurii Zubrytskyi39ee3d82020-03-26 18:20:39 -0700143 if (!silent) {
144 fprintf(stderr, "Failed to run: %s, error: %s\n",
145 android::base::Join(command_args, " ").c_str(), error.c_str());
146 }
Alex Buynytskyy175ce292020-02-13 06:52:04 -0800147 return {};
148 }
149
Alex Buynytskyy175ce292020-02-13 06:52:04 -0800150 return connection_fd;
151}
152
Yurii Zubrytskyi39ee3d82020-03-26 18:20:39 -0700153bool can_install(const Files& files) {
154 for (const auto& file : files) {
155 struct stat st;
156 if (stat(file.c_str(), &st)) {
157 return false;
158 }
159
Alex Buynytskyy81440832020-05-18 06:26:35 -0700160 if (android::base::EndsWithIgnoreCase(file, ".apk")) {
161 // Signature has to be present for APKs.
162 auto [fd, _] = read_signature(st.st_size, file, /*silent=*/true);
163 if (!fd.ok()) {
164 return false;
165 }
Yurii Zubrytskyi39ee3d82020-03-26 18:20:39 -0700166 }
167 }
168 return true;
169}
170
Alex Buynytskyy31ff0ca2020-05-14 13:29:05 -0700171std::optional<Process> install(const Files& files, const Args& passthrough_args, bool silent) {
172 auto connection_fd = start_install(files, passthrough_args, silent);
Alex Buynytskyy175ce292020-02-13 06:52:04 -0800173 if (connection_fd < 0) {
Yurii Zubrytskyi39ee3d82020-03-26 18:20:39 -0700174 if (!silent) {
175 fprintf(stderr, "adb: failed to initiate installation on device.\n");
176 }
Alex Buynytskyy175ce292020-02-13 06:52:04 -0800177 return {};
178 }
179
180 std::string adb_path = android::base::GetExecutablePath();
181
Yurii Zubrytskyi3c0574f2020-03-26 18:16:36 -0700182 auto osh = cast_handle_to_int(adb_get_os_handle(connection_fd.get()));
Alex Buynytskyy175ce292020-02-13 06:52:04 -0800183 auto fd_param = std::to_string(osh);
Alex Buynytskyy175ce292020-02-13 06:52:04 -0800184
Songchun Fandaf826a2020-03-09 11:33:44 -0700185 // pipe for child process to write output
186 int print_fds[2];
187 if (adb_socketpair(print_fds) != 0) {
Yurii Zubrytskyi39ee3d82020-03-26 18:20:39 -0700188 if (!silent) {
Yurii Zubrytskyif818b4a2020-06-02 23:38:51 -0700189 fprintf(stderr, "adb: failed to create socket pair for child to print to parent\n");
Yurii Zubrytskyi39ee3d82020-03-26 18:20:39 -0700190 }
Songchun Fandaf826a2020-03-09 11:33:44 -0700191 return {};
192 }
193 auto [pipe_read_fd, pipe_write_fd] = print_fds;
Yurii Zubrytskyi3c0574f2020-03-26 18:16:36 -0700194 auto pipe_write_fd_param = std::to_string(cast_handle_to_int(adb_get_os_handle(pipe_write_fd)));
Songchun Fandaf826a2020-03-09 11:33:44 -0700195 close_on_exec(pipe_read_fd);
196
Alex Buynytskyy175ce292020-02-13 06:52:04 -0800197 std::vector<std::string> args(std::move(files));
Songchun Fandaf826a2020-03-09 11:33:44 -0700198 args.insert(args.begin(), {"inc-server", fd_param, pipe_write_fd_param});
199 auto child =
200 adb_launch_process(adb_path, std::move(args), {connection_fd.get(), pipe_write_fd});
Alex Buynytskyy175ce292020-02-13 06:52:04 -0800201 if (!child) {
Yurii Zubrytskyi39ee3d82020-03-26 18:20:39 -0700202 if (!silent) {
203 fprintf(stderr, "adb: failed to fork: %s\n", strerror(errno));
204 }
Alex Buynytskyy175ce292020-02-13 06:52:04 -0800205 return {};
206 }
207
Songchun Fandaf826a2020-03-09 11:33:44 -0700208 adb_close(pipe_write_fd);
209
Alex Buynytskyy175ce292020-02-13 06:52:04 -0800210 auto killOnExit = [](Process* p) { p->kill(); };
211 std::unique_ptr<Process, decltype(killOnExit)> serverKiller(&child, killOnExit);
Alex Buynytskyy175ce292020-02-13 06:52:04 -0800212
Songchun Fandaf826a2020-03-09 11:33:44 -0700213 Result result = wait_for_installation(pipe_read_fd);
214 adb_close(pipe_read_fd);
215
Yurii Zubrytskyif818b4a2020-06-02 23:38:51 -0700216 if (result != Result::Success) {
217 if (!silent) {
218 fprintf(stderr, "adb: install command failed");
219 }
220 return {};
Songchun Fandaf826a2020-03-09 11:33:44 -0700221 }
Yurii Zubrytskyif818b4a2020-06-02 23:38:51 -0700222
223 // adb client exits now but inc-server can continue
224 serverKiller.release();
Alex Buynytskyy175ce292020-02-13 06:52:04 -0800225 return child;
226}
227
Songchun Fandaf826a2020-03-09 11:33:44 -0700228Result wait_for_installation(int read_fd) {
229 static constexpr int maxMessageSize = 256;
230 std::vector<char> child_stdout(CHUNK_SIZE);
231 int bytes_read;
232 int buf_size = 0;
233 // TODO(b/150865433): optimize child's output parsing
234 while ((bytes_read = adb_read(read_fd, child_stdout.data() + buf_size,
235 child_stdout.size() - buf_size)) > 0) {
236 // print to parent's stdout
237 fprintf(stdout, "%.*s", bytes_read, child_stdout.data() + buf_size);
238
239 buf_size += bytes_read;
240 const std::string_view stdout_str(child_stdout.data(), buf_size);
241 // wait till installation either succeeds or fails
242 if (stdout_str.find("Success") != std::string::npos) {
243 return Result::Success;
244 }
245 // on failure, wait for full message
246 static constexpr auto failure_msg_head = "Failure ["sv;
247 if (const auto begin_itr = stdout_str.find(failure_msg_head);
248 begin_itr != std::string::npos) {
249 if (buf_size >= maxMessageSize) {
250 return Result::Failure;
251 }
252 const auto end_itr = stdout_str.rfind("]");
253 if (end_itr != std::string::npos && end_itr >= begin_itr + failure_msg_head.size()) {
254 return Result::Failure;
255 }
256 }
257 child_stdout.resize(buf_size + CHUNK_SIZE);
258 }
259 return Result::None;
260}
261
Alex Buynytskyy175ce292020-02-13 06:52:04 -0800262} // namespace incremental