Andrei Homescu | 08d66c5 | 2020-11-30 20:48:22 -0800 | [diff] [blame] | 1 | /* |
| 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 Prichard | 87c90e7 | 2023-09-21 19:44:47 -0700 | [diff] [blame] | 22 | #include <assert.h> |
Andrei Homescu | 08d66c5 | 2020-11-30 20:48:22 -0800 | [diff] [blame] | 23 | #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 | |
| 40 | using android::base::unique_fd; |
| 41 | using std::string; |
| 42 | |
| 43 | constexpr const char kTrustyDefaultDeviceName[] = "/dev/trusty-ipc-dev0"; |
| 44 | |
| 45 | static const char* dev_name = kTrustyDefaultDeviceName; |
| 46 | |
Andrei Homescu | 909beae | 2021-02-11 01:17:20 -0800 | [diff] [blame] | 47 | static const char* _sopts = "hD:"; |
Andrei Homescu | 08d66c5 | 2020-11-30 20:48:22 -0800 | [diff] [blame] | 48 | static const struct option _lopts[] = { |
| 49 | {"help", no_argument, 0, 'h'}, |
| 50 | {"dev", required_argument, 0, 'D'}, |
| 51 | {0, 0, 0, 0}, |
| 52 | }; |
| 53 | |
| 54 | static 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 | |
| 62 | static void print_usage_and_exit(const char* prog, int code) { |
| 63 | fprintf(stderr, usage, prog); |
| 64 | exit(code); |
| 65 | } |
| 66 | |
| 67 | static 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 | |
| 92 | static 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 Vo | 413ab28 | 2021-03-31 11:47:09 -0700 | [diff] [blame] | 100 | PLOG(ERROR) << "Error opening file " << file_name; |
Andrei Homescu | 08d66c5 | 2020-11-30 20:48:22 -0800 | [diff] [blame] | 101 | return {}; |
| 102 | } |
| 103 | |
| 104 | rc = fstat64(file_fd, &st); |
| 105 | if (rc < 0) { |
Tri Vo | 413ab28 | 2021-03-31 11:47:09 -0700 | [diff] [blame] | 106 | PLOG(ERROR) << "Error calling stat on file '" << file_name << "'"; |
Andrei Homescu | 08d66c5 | 2020-11-30 20:48:22 -0800 | [diff] [blame] | 107 | return {}; |
| 108 | } |
| 109 | |
Mike McTernan | b22e5bc | 2023-12-07 12:13:18 +0000 | [diff] [blame] | 110 | if (st.st_size == 0) { |
| 111 | LOG(ERROR) << "Zero length file '" << file_name << "'"; |
| 112 | return {}; |
| 113 | } |
| 114 | |
Andrei Homescu | 08d66c5 | 2020-11-30 20:48:22 -0800 | [diff] [blame] | 115 | file_size = st.st_size; |
| 116 | |
| 117 | /* The dmabuf size needs to be a multiple of the page size */ |
| 118 | file_page_offset = file_size & (page_size - 1); |
| 119 | if (file_page_offset) { |
| 120 | file_page_offset = page_size - file_page_offset; |
| 121 | } |
| 122 | if (__builtin_add_overflow(file_size, file_page_offset, &file_page_size)) { |
Tri Vo | 413ab28 | 2021-03-31 11:47:09 -0700 | [diff] [blame] | 123 | LOG(ERROR) << "Failed to page-align file size"; |
Andrei Homescu | 08d66c5 | 2020-11-30 20:48:22 -0800 | [diff] [blame] | 124 | return {}; |
| 125 | } |
| 126 | |
| 127 | BufferAllocator alloc; |
| 128 | unique_fd dmabuf_fd(alloc.Alloc(kDmabufSystemHeapName, file_page_size)); |
| 129 | if (!dmabuf_fd.ok()) { |
Mike McTernan | b22e5bc | 2023-12-07 12:13:18 +0000 | [diff] [blame] | 130 | LOG(ERROR) << "Error creating dmabuf for " << file_page_size |
| 131 | << " bytes: " << dmabuf_fd.get(); |
Andrei Homescu | 08d66c5 | 2020-11-30 20:48:22 -0800 | [diff] [blame] | 132 | return dmabuf_fd; |
| 133 | } |
| 134 | |
| 135 | void* shm = mmap(0, file_page_size, PROT_READ | PROT_WRITE, MAP_SHARED, dmabuf_fd, 0); |
| 136 | if (shm == MAP_FAILED) { |
| 137 | return {}; |
| 138 | } |
| 139 | |
| 140 | off64_t file_offset = 0; |
| 141 | while (file_offset < file_size) { |
| 142 | ssize_t num_read = TEMP_FAILURE_RETRY( |
| 143 | pread(file_fd, (char*)shm + file_offset, file_size - file_offset, file_offset)); |
| 144 | |
| 145 | if (num_read < 0) { |
Tri Vo | 413ab28 | 2021-03-31 11:47:09 -0700 | [diff] [blame] | 146 | PLOG(ERROR) << "Error reading package file '" << file_name << "'"; |
Andrei Homescu | 08d66c5 | 2020-11-30 20:48:22 -0800 | [diff] [blame] | 147 | break; |
| 148 | } |
| 149 | |
| 150 | if (num_read == 0) { |
Tri Vo | 413ab28 | 2021-03-31 11:47:09 -0700 | [diff] [blame] | 151 | LOG(ERROR) << "Unexpected end of file '" << file_name << "'"; |
Andrei Homescu | 08d66c5 | 2020-11-30 20:48:22 -0800 | [diff] [blame] | 152 | break; |
| 153 | } |
| 154 | |
| 155 | file_offset += (off64_t)num_read; |
| 156 | } |
| 157 | |
| 158 | munmap(shm, file_page_size); |
| 159 | |
| 160 | if (file_offset < file_size) { |
| 161 | return {}; |
| 162 | } |
| 163 | |
| 164 | assert(file_offset == file_size); |
| 165 | if (out_file_size) { |
| 166 | *out_file_size = file_size; |
| 167 | } |
| 168 | |
| 169 | return dmabuf_fd; |
| 170 | } |
| 171 | |
| 172 | static ssize_t send_load_message(int tipc_fd, int package_fd, off64_t package_size) { |
| 173 | struct apploader_header hdr = { |
| 174 | .cmd = APPLOADER_CMD_LOAD_APPLICATION, |
| 175 | }; |
| 176 | struct apploader_load_app_req req = { |
| 177 | .package_size = static_cast<uint64_t>(package_size), |
| 178 | }; |
| 179 | struct iovec tx[2] = {{&hdr, sizeof(hdr)}, {&req, sizeof(req)}}; |
| 180 | struct trusty_shm shm = { |
| 181 | .fd = package_fd, |
| 182 | .transfer = TRUSTY_SHARE, |
| 183 | }; |
| 184 | return tipc_send(tipc_fd, tx, 2, &shm, 1); |
| 185 | } |
| 186 | |
| 187 | static ssize_t read_response(int tipc_fd) { |
| 188 | struct apploader_resp resp; |
| 189 | ssize_t rc = read(tipc_fd, &resp, sizeof(resp)); |
| 190 | if (rc < 0) { |
Tri Vo | 413ab28 | 2021-03-31 11:47:09 -0700 | [diff] [blame] | 191 | PLOG(ERROR) << "Failed to read response"; |
Andrei Homescu | 08d66c5 | 2020-11-30 20:48:22 -0800 | [diff] [blame] | 192 | return rc; |
| 193 | } |
| 194 | |
| 195 | if (rc < sizeof(resp)) { |
Tri Vo | 413ab28 | 2021-03-31 11:47:09 -0700 | [diff] [blame] | 196 | LOG(ERROR) << "Not enough data in response: " << rc; |
Andrei Homescu | 08d66c5 | 2020-11-30 20:48:22 -0800 | [diff] [blame] | 197 | return -EIO; |
| 198 | } |
| 199 | |
| 200 | if (resp.hdr.cmd != (APPLOADER_CMD_LOAD_APPLICATION | APPLOADER_RESP_BIT)) { |
Tri Vo | 413ab28 | 2021-03-31 11:47:09 -0700 | [diff] [blame] | 201 | LOG(ERROR) << "Invalid command in response: " << resp.hdr.cmd; |
Andrei Homescu | 08d66c5 | 2020-11-30 20:48:22 -0800 | [diff] [blame] | 202 | return -EINVAL; |
| 203 | } |
| 204 | |
| 205 | switch (resp.error) { |
| 206 | case APPLOADER_NO_ERROR: |
| 207 | break; |
| 208 | case APPLOADER_ERR_UNKNOWN_CMD: |
Tri Vo | 413ab28 | 2021-03-31 11:47:09 -0700 | [diff] [blame] | 209 | LOG(ERROR) << "Error: unknown command"; |
Andrei Homescu | 08d66c5 | 2020-11-30 20:48:22 -0800 | [diff] [blame] | 210 | break; |
| 211 | case APPLOADER_ERR_INVALID_CMD: |
Tri Vo | 413ab28 | 2021-03-31 11:47:09 -0700 | [diff] [blame] | 212 | LOG(ERROR) << "Error: invalid command arguments"; |
Andrei Homescu | 08d66c5 | 2020-11-30 20:48:22 -0800 | [diff] [blame] | 213 | break; |
| 214 | case APPLOADER_ERR_NO_MEMORY: |
Tri Vo | 413ab28 | 2021-03-31 11:47:09 -0700 | [diff] [blame] | 215 | LOG(ERROR) << "Error: out of Trusty memory"; |
Andrei Homescu | 08d66c5 | 2020-11-30 20:48:22 -0800 | [diff] [blame] | 216 | break; |
| 217 | case APPLOADER_ERR_VERIFICATION_FAILED: |
Tri Vo | 413ab28 | 2021-03-31 11:47:09 -0700 | [diff] [blame] | 218 | LOG(ERROR) << "Error: failed to verify the package"; |
Andrei Homescu | 08d66c5 | 2020-11-30 20:48:22 -0800 | [diff] [blame] | 219 | break; |
| 220 | case APPLOADER_ERR_LOADING_FAILED: |
Tri Vo | 413ab28 | 2021-03-31 11:47:09 -0700 | [diff] [blame] | 221 | LOG(ERROR) << "Error: failed to load the package"; |
Andrei Homescu | 08d66c5 | 2020-11-30 20:48:22 -0800 | [diff] [blame] | 222 | break; |
| 223 | case APPLOADER_ERR_ALREADY_EXISTS: |
Tri Vo | 413ab28 | 2021-03-31 11:47:09 -0700 | [diff] [blame] | 224 | LOG(ERROR) << "Error: application already exists"; |
Andrei Homescu | 08d66c5 | 2020-11-30 20:48:22 -0800 | [diff] [blame] | 225 | break; |
| 226 | case APPLOADER_ERR_INTERNAL: |
Tri Vo | 413ab28 | 2021-03-31 11:47:09 -0700 | [diff] [blame] | 227 | LOG(ERROR) << "Error: internal apploader error"; |
Andrei Homescu | 08d66c5 | 2020-11-30 20:48:22 -0800 | [diff] [blame] | 228 | break; |
Andrei Homescu | 4b5afea | 2021-05-10 22:18:25 -0700 | [diff] [blame] | 229 | case APPLOADER_ERR_INVALID_VERSION: |
| 230 | LOG(ERROR) << "Error: invalid application version"; |
| 231 | break; |
Thurston Dang | 45dbdd1 | 2022-04-05 20:25:19 +0000 | [diff] [blame] | 232 | case APPLOADER_ERR_POLICY_VIOLATION: |
| 233 | LOG(ERROR) << "Error: loading denied by policy engine"; |
| 234 | break; |
Per Larsen | db9a554 | 2022-08-20 23:16:53 -0700 | [diff] [blame] | 235 | case APPLOADER_ERR_NOT_ENCRYPTED: |
| 236 | LOG(ERROR) << "Error: unmet application encryption requirement"; |
| 237 | break; |
Andrei Homescu | 08d66c5 | 2020-11-30 20:48:22 -0800 | [diff] [blame] | 238 | default: |
Tri Vo | 413ab28 | 2021-03-31 11:47:09 -0700 | [diff] [blame] | 239 | LOG(ERROR) << "Unrecognized error: " << resp.error; |
Andrei Homescu | 08d66c5 | 2020-11-30 20:48:22 -0800 | [diff] [blame] | 240 | break; |
| 241 | } |
| 242 | |
| 243 | return static_cast<ssize_t>(resp.error); |
| 244 | } |
| 245 | |
| 246 | static ssize_t send_app_package(const char* package_file_name) { |
| 247 | ssize_t rc = 0; |
| 248 | int tipc_fd = -1; |
| 249 | off64_t package_size; |
| 250 | |
| 251 | unique_fd package_fd = read_file(package_file_name, &package_size); |
| 252 | if (!package_fd.ok()) { |
| 253 | rc = -1; |
| 254 | goto err_read_file; |
| 255 | } |
| 256 | |
| 257 | tipc_fd = tipc_connect(dev_name, APPLOADER_PORT); |
| 258 | if (tipc_fd < 0) { |
Tri Vo | 413ab28 | 2021-03-31 11:47:09 -0700 | [diff] [blame] | 259 | LOG(ERROR) << "Failed to connect to Trusty app loader: " << strerror(-tipc_fd); |
Marco Nelissen | 92cd889 | 2021-09-21 15:23:33 -0700 | [diff] [blame] | 260 | // print this to stderr too to avoid silently exiting when run as non-root |
| 261 | fprintf(stderr, "Failed to connect to Trusty app loader: %s\n", strerror(-tipc_fd)); |
Andrei Homescu | 08d66c5 | 2020-11-30 20:48:22 -0800 | [diff] [blame] | 262 | rc = tipc_fd; |
| 263 | goto err_tipc_connect; |
| 264 | } |
| 265 | |
| 266 | rc = send_load_message(tipc_fd, package_fd, package_size); |
| 267 | if (rc < 0) { |
Tri Vo | 413ab28 | 2021-03-31 11:47:09 -0700 | [diff] [blame] | 268 | LOG(ERROR) << "Failed to send package: " << rc; |
Andrei Homescu | 08d66c5 | 2020-11-30 20:48:22 -0800 | [diff] [blame] | 269 | goto err_send; |
| 270 | } |
| 271 | |
| 272 | rc = read_response(tipc_fd); |
| 273 | |
| 274 | err_send: |
| 275 | tipc_close(tipc_fd); |
| 276 | err_tipc_connect: |
| 277 | err_read_file: |
| 278 | return rc; |
| 279 | } |
| 280 | |
| 281 | int main(int argc, char** argv) { |
| 282 | parse_options(argc, argv); |
| 283 | if (optind + 1 != argc) { |
| 284 | print_usage_and_exit(argv[0], EXIT_FAILURE); |
| 285 | } |
| 286 | |
| 287 | int rc = send_app_package(argv[optind]); |
| 288 | return rc == 0 ? EXIT_SUCCESS : EXIT_FAILURE; |
| 289 | } |