blob: f782d2acb842aced071cd48751c029b1eddfe0d4 [file] [log] [blame]
Andrei Homescu08d66c52020-11-30 20:48:22 -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#define LOG_TAG "TrustyAppLoader"
18
19#include <BufferAllocator/BufferAllocator.h>
20#include <android-base/logging.h>
21#include <android-base/unique_fd.h>
Ryan Prichard87c90e72023-09-21 19:44:47 -070022#include <assert.h>
Andrei Homescu08d66c52020-11-30 20:48:22 -080023#include <fcntl.h>
24#include <getopt.h>
25#include <stdbool.h>
26#include <stdio.h>
27#include <stdlib.h>
28#include <string.h>
29#include <sys/mman.h>
30#include <sys/sendfile.h>
31#include <sys/stat.h>
32#include <sys/types.h>
33#include <trusty/tipc.h>
34#include <unistd.h>
35#include <algorithm>
36#include <string>
37
38#include "apploader_ipc.h"
39
40using android::base::unique_fd;
41using std::string;
42
43constexpr const char kTrustyDefaultDeviceName[] = "/dev/trusty-ipc-dev0";
44
45static const char* dev_name = kTrustyDefaultDeviceName;
46
Andrei Homescu909beae2021-02-11 01:17:20 -080047static const char* _sopts = "hD:";
Andrei Homescu08d66c52020-11-30 20:48:22 -080048static const struct option _lopts[] = {
49 {"help", no_argument, 0, 'h'},
50 {"dev", required_argument, 0, 'D'},
51 {0, 0, 0, 0},
52};
53
54static const char* usage =
55 "Usage: %s [options] package-file\n"
56 "\n"
57 "options:\n"
58 " -h, --help prints this message and exit\n"
59 " -D, --dev name Trusty device name\n"
60 "\n";
61
62static void print_usage_and_exit(const char* prog, int code) {
63 fprintf(stderr, usage, prog);
64 exit(code);
65}
66
67static void parse_options(int argc, char** argv) {
68 int c;
69 int oidx = 0;
70
71 while (1) {
72 c = getopt_long(argc, argv, _sopts, _lopts, &oidx);
73 if (c == -1) {
74 break; /* done */
75 }
76
77 switch (c) {
78 case 'h':
79 print_usage_and_exit(argv[0], EXIT_SUCCESS);
80 break;
81
82 case 'D':
83 dev_name = strdup(optarg);
84 break;
85
86 default:
87 print_usage_and_exit(argv[0], EXIT_FAILURE);
88 }
89 }
90}
91
92static unique_fd read_file(const char* file_name, off64_t* out_file_size) {
93 int rc;
94 long page_size = sysconf(_SC_PAGESIZE);
95 off64_t file_size, file_page_offset, file_page_size;
96 struct stat64 st;
97
98 unique_fd file_fd(TEMP_FAILURE_RETRY(open(file_name, O_RDONLY)));
99 if (!file_fd.ok()) {
Tri Vo413ab282021-03-31 11:47:09 -0700100 PLOG(ERROR) << "Error opening file " << file_name;
Andrei Homescu08d66c52020-11-30 20:48:22 -0800101 return {};
102 }
103
104 rc = fstat64(file_fd, &st);
105 if (rc < 0) {
Tri Vo413ab282021-03-31 11:47:09 -0700106 PLOG(ERROR) << "Error calling stat on file '" << file_name << "'";
Andrei Homescu08d66c52020-11-30 20:48:22 -0800107 return {};
108 }
109
110 assert(st.st_size >= 0);
111 file_size = st.st_size;
112
113 /* The dmabuf size needs to be a multiple of the page size */
114 file_page_offset = file_size & (page_size - 1);
115 if (file_page_offset) {
116 file_page_offset = page_size - file_page_offset;
117 }
118 if (__builtin_add_overflow(file_size, file_page_offset, &file_page_size)) {
Tri Vo413ab282021-03-31 11:47:09 -0700119 LOG(ERROR) << "Failed to page-align file size";
Andrei Homescu08d66c52020-11-30 20:48:22 -0800120 return {};
121 }
122
123 BufferAllocator alloc;
124 unique_fd dmabuf_fd(alloc.Alloc(kDmabufSystemHeapName, file_page_size));
125 if (!dmabuf_fd.ok()) {
Tri Vo413ab282021-03-31 11:47:09 -0700126 LOG(ERROR) << "Error creating dmabuf: " << dmabuf_fd.get();
Andrei Homescu08d66c52020-11-30 20:48:22 -0800127 return dmabuf_fd;
128 }
129
130 void* shm = mmap(0, file_page_size, PROT_READ | PROT_WRITE, MAP_SHARED, dmabuf_fd, 0);
131 if (shm == MAP_FAILED) {
132 return {};
133 }
134
135 off64_t file_offset = 0;
136 while (file_offset < file_size) {
137 ssize_t num_read = TEMP_FAILURE_RETRY(
138 pread(file_fd, (char*)shm + file_offset, file_size - file_offset, file_offset));
139
140 if (num_read < 0) {
Tri Vo413ab282021-03-31 11:47:09 -0700141 PLOG(ERROR) << "Error reading package file '" << file_name << "'";
Andrei Homescu08d66c52020-11-30 20:48:22 -0800142 break;
143 }
144
145 if (num_read == 0) {
Tri Vo413ab282021-03-31 11:47:09 -0700146 LOG(ERROR) << "Unexpected end of file '" << file_name << "'";
Andrei Homescu08d66c52020-11-30 20:48:22 -0800147 break;
148 }
149
150 file_offset += (off64_t)num_read;
151 }
152
153 munmap(shm, file_page_size);
154
155 if (file_offset < file_size) {
156 return {};
157 }
158
159 assert(file_offset == file_size);
160 if (out_file_size) {
161 *out_file_size = file_size;
162 }
163
164 return dmabuf_fd;
165}
166
167static ssize_t send_load_message(int tipc_fd, int package_fd, off64_t package_size) {
168 struct apploader_header hdr = {
169 .cmd = APPLOADER_CMD_LOAD_APPLICATION,
170 };
171 struct apploader_load_app_req req = {
172 .package_size = static_cast<uint64_t>(package_size),
173 };
174 struct iovec tx[2] = {{&hdr, sizeof(hdr)}, {&req, sizeof(req)}};
175 struct trusty_shm shm = {
176 .fd = package_fd,
177 .transfer = TRUSTY_SHARE,
178 };
179 return tipc_send(tipc_fd, tx, 2, &shm, 1);
180}
181
182static ssize_t read_response(int tipc_fd) {
183 struct apploader_resp resp;
184 ssize_t rc = read(tipc_fd, &resp, sizeof(resp));
185 if (rc < 0) {
Tri Vo413ab282021-03-31 11:47:09 -0700186 PLOG(ERROR) << "Failed to read response";
Andrei Homescu08d66c52020-11-30 20:48:22 -0800187 return rc;
188 }
189
190 if (rc < sizeof(resp)) {
Tri Vo413ab282021-03-31 11:47:09 -0700191 LOG(ERROR) << "Not enough data in response: " << rc;
Andrei Homescu08d66c52020-11-30 20:48:22 -0800192 return -EIO;
193 }
194
195 if (resp.hdr.cmd != (APPLOADER_CMD_LOAD_APPLICATION | APPLOADER_RESP_BIT)) {
Tri Vo413ab282021-03-31 11:47:09 -0700196 LOG(ERROR) << "Invalid command in response: " << resp.hdr.cmd;
Andrei Homescu08d66c52020-11-30 20:48:22 -0800197 return -EINVAL;
198 }
199
200 switch (resp.error) {
201 case APPLOADER_NO_ERROR:
202 break;
203 case APPLOADER_ERR_UNKNOWN_CMD:
Tri Vo413ab282021-03-31 11:47:09 -0700204 LOG(ERROR) << "Error: unknown command";
Andrei Homescu08d66c52020-11-30 20:48:22 -0800205 break;
206 case APPLOADER_ERR_INVALID_CMD:
Tri Vo413ab282021-03-31 11:47:09 -0700207 LOG(ERROR) << "Error: invalid command arguments";
Andrei Homescu08d66c52020-11-30 20:48:22 -0800208 break;
209 case APPLOADER_ERR_NO_MEMORY:
Tri Vo413ab282021-03-31 11:47:09 -0700210 LOG(ERROR) << "Error: out of Trusty memory";
Andrei Homescu08d66c52020-11-30 20:48:22 -0800211 break;
212 case APPLOADER_ERR_VERIFICATION_FAILED:
Tri Vo413ab282021-03-31 11:47:09 -0700213 LOG(ERROR) << "Error: failed to verify the package";
Andrei Homescu08d66c52020-11-30 20:48:22 -0800214 break;
215 case APPLOADER_ERR_LOADING_FAILED:
Tri Vo413ab282021-03-31 11:47:09 -0700216 LOG(ERROR) << "Error: failed to load the package";
Andrei Homescu08d66c52020-11-30 20:48:22 -0800217 break;
218 case APPLOADER_ERR_ALREADY_EXISTS:
Tri Vo413ab282021-03-31 11:47:09 -0700219 LOG(ERROR) << "Error: application already exists";
Andrei Homescu08d66c52020-11-30 20:48:22 -0800220 break;
221 case APPLOADER_ERR_INTERNAL:
Tri Vo413ab282021-03-31 11:47:09 -0700222 LOG(ERROR) << "Error: internal apploader error";
Andrei Homescu08d66c52020-11-30 20:48:22 -0800223 break;
Andrei Homescu4b5afea2021-05-10 22:18:25 -0700224 case APPLOADER_ERR_INVALID_VERSION:
225 LOG(ERROR) << "Error: invalid application version";
226 break;
Thurston Dang45dbdd12022-04-05 20:25:19 +0000227 case APPLOADER_ERR_POLICY_VIOLATION:
228 LOG(ERROR) << "Error: loading denied by policy engine";
229 break;
Per Larsendb9a5542022-08-20 23:16:53 -0700230 case APPLOADER_ERR_NOT_ENCRYPTED:
231 LOG(ERROR) << "Error: unmet application encryption requirement";
232 break;
Andrei Homescu08d66c52020-11-30 20:48:22 -0800233 default:
Tri Vo413ab282021-03-31 11:47:09 -0700234 LOG(ERROR) << "Unrecognized error: " << resp.error;
Andrei Homescu08d66c52020-11-30 20:48:22 -0800235 break;
236 }
237
238 return static_cast<ssize_t>(resp.error);
239}
240
241static ssize_t send_app_package(const char* package_file_name) {
242 ssize_t rc = 0;
243 int tipc_fd = -1;
244 off64_t package_size;
245
246 unique_fd package_fd = read_file(package_file_name, &package_size);
247 if (!package_fd.ok()) {
248 rc = -1;
249 goto err_read_file;
250 }
251
252 tipc_fd = tipc_connect(dev_name, APPLOADER_PORT);
253 if (tipc_fd < 0) {
Tri Vo413ab282021-03-31 11:47:09 -0700254 LOG(ERROR) << "Failed to connect to Trusty app loader: " << strerror(-tipc_fd);
Marco Nelissen92cd8892021-09-21 15:23:33 -0700255 // print this to stderr too to avoid silently exiting when run as non-root
256 fprintf(stderr, "Failed to connect to Trusty app loader: %s\n", strerror(-tipc_fd));
Andrei Homescu08d66c52020-11-30 20:48:22 -0800257 rc = tipc_fd;
258 goto err_tipc_connect;
259 }
260
261 rc = send_load_message(tipc_fd, package_fd, package_size);
262 if (rc < 0) {
Tri Vo413ab282021-03-31 11:47:09 -0700263 LOG(ERROR) << "Failed to send package: " << rc;
Andrei Homescu08d66c52020-11-30 20:48:22 -0800264 goto err_send;
265 }
266
267 rc = read_response(tipc_fd);
268
269err_send:
270 tipc_close(tipc_fd);
271err_tipc_connect:
272err_read_file:
273 return rc;
274}
275
276int main(int argc, char** argv) {
277 parse_options(argc, argv);
278 if (optind + 1 != argc) {
279 print_usage_and_exit(argv[0], EXIT_FAILURE);
280 }
281
282 int rc = send_app_package(argv[optind]);
283 return rc == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
284}