blob: 2814932c9e8ce00ca2383c7241167be48add7b26 [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.
Yurii Zubrytskyi4532ea82020-03-26 18:20:39 -070096static unique_fd start_install(const Files& files, bool silent) {
Alex Buynytskyy96ff54b2020-02-13 06:52:04 -080097 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 Zubrytskyi4532ea82020-03-26 18:20:39 -0700107 if (!silent) {
108 fprintf(stderr, "Failed to stat input file %s. Abort.\n", file.c_str());
109 }
Alex Buynytskyy96ff54b2020-02-13 06:52:04 -0800110 return {};
111 }
112
Yurii Zubrytskyi4532ea82020-03-26 18:20:39 -0700113 auto [signature_fd, signature] = read_and_encode_signature(st.st_size, file, silent);
Alex Buynytskyy96ff54b2020-02-13 06:52:04 -0800114 if (!signature_fd.ok()) {
115 return {};
116 }
117
Alex Buynytskyy82c1c972020-03-27 14:34:21 -0700118 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 Buynytskyy96ff54b2020-02-13 06:52:04 -0800120 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 Zubrytskyi4532ea82020-03-26 18:20:39 -0700128 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 Buynytskyy96ff54b2020-02-13 06:52:04 -0800132 return {};
133 }
134
Alex Buynytskyy96ff54b2020-02-13 06:52:04 -0800135 return connection_fd;
136}
137
Yurii Zubrytskyi4532ea82020-03-26 18:20:39 -0700138bool 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
153std::optional<Process> install(const Files& files, bool silent) {
154 auto connection_fd = start_install(files, silent);
Alex Buynytskyy96ff54b2020-02-13 06:52:04 -0800155 if (connection_fd < 0) {
Yurii Zubrytskyi4532ea82020-03-26 18:20:39 -0700156 if (!silent) {
157 fprintf(stderr, "adb: failed to initiate installation on device.\n");
158 }
Alex Buynytskyy96ff54b2020-02-13 06:52:04 -0800159 return {};
160 }
161
162 std::string adb_path = android::base::GetExecutablePath();
163
Yurii Zubrytskyie3e64b82020-03-26 18:16:36 -0700164 auto osh = cast_handle_to_int(adb_get_os_handle(connection_fd.get()));
Alex Buynytskyy96ff54b2020-02-13 06:52:04 -0800165 auto fd_param = std::to_string(osh);
Alex Buynytskyy96ff54b2020-02-13 06:52:04 -0800166
Songchun Fan23bd5522020-03-09 11:33:44 -0700167 // pipe for child process to write output
168 int print_fds[2];
169 if (adb_socketpair(print_fds) != 0) {
Yurii Zubrytskyi4532ea82020-03-26 18:20:39 -0700170 if (!silent) {
171 fprintf(stderr, "Failed to create socket pair for child to print to parent\n");
172 }
Songchun Fan23bd5522020-03-09 11:33:44 -0700173 return {};
174 }
175 auto [pipe_read_fd, pipe_write_fd] = print_fds;
Yurii Zubrytskyie3e64b82020-03-26 18:16:36 -0700176 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 -0700177 close_on_exec(pipe_read_fd);
178
Alex Buynytskyy96ff54b2020-02-13 06:52:04 -0800179 std::vector<std::string> args(std::move(files));
Songchun Fan23bd5522020-03-09 11:33:44 -0700180 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 Buynytskyy96ff54b2020-02-13 06:52:04 -0800183 if (!child) {
Yurii Zubrytskyi4532ea82020-03-26 18:20:39 -0700184 if (!silent) {
185 fprintf(stderr, "adb: failed to fork: %s\n", strerror(errno));
186 }
Alex Buynytskyy96ff54b2020-02-13 06:52:04 -0800187 return {};
188 }
189
Songchun Fan23bd5522020-03-09 11:33:44 -0700190 adb_close(pipe_write_fd);
191
Alex Buynytskyy96ff54b2020-02-13 06:52:04 -0800192 auto killOnExit = [](Process* p) { p->kill(); };
193 std::unique_ptr<Process, decltype(killOnExit)> serverKiller(&child, killOnExit);
Alex Buynytskyy96ff54b2020-02-13 06:52:04 -0800194
Songchun Fan23bd5522020-03-09 11:33:44 -0700195 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 Buynytskyy96ff54b2020-02-13 06:52:04 -0800202 return child;
203}
204
Songchun Fan23bd5522020-03-09 11:33:44 -0700205Result 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 Buynytskyy96ff54b2020-02-13 06:52:04 -0800239} // namespace incremental