Doug Zongker | 075ad80 | 2014-06-26 15:35:51 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 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 | // This module creates a special filesystem containing two files. |
| 18 | // |
| 19 | // "/sideload/package.zip" appears to be a normal file, but reading |
| 20 | // from it causes data to be fetched from the adb host. We can use |
| 21 | // this to sideload packages over an adb connection without having to |
| 22 | // store the entire package in RAM on the device. |
| 23 | // |
| 24 | // Because we may not trust the adb host, this filesystem maintains |
| 25 | // the following invariant: each read of a given position returns the |
| 26 | // same data as the first read at that position. That is, once a |
| 27 | // section of the file is read, future reads of that section return |
| 28 | // the same data. (Otherwise, a malicious adb host process could |
| 29 | // return one set of bits when the package is read for signature |
| 30 | // verification, and then different bits for when the package is |
| 31 | // accessed by the installer.) If the adb host returns something |
| 32 | // different than it did on the first read, the reader of the file |
| 33 | // will see their read fail with EINVAL. |
| 34 | // |
| 35 | // The other file, "/sideload/exit", is used to control the subprocess |
| 36 | // that creates this filesystem. Calling stat() on the exit file |
| 37 | // causes the filesystem to be unmounted and the adb process on the |
| 38 | // device shut down. |
| 39 | // |
| 40 | // Note that only the minimal set of file operations needed for these |
| 41 | // two files is implemented. In particular, you can't opendir() or |
| 42 | // readdir() on the "/sideload" directory; ls on it won't work. |
| 43 | |
Tao Bao | 91a7aa4 | 2017-05-01 15:57:38 -0700 | [diff] [blame] | 44 | #include "fuse_sideload.h" |
| 45 | |
Doug Zongker | 075ad80 | 2014-06-26 15:35:51 -0700 | [diff] [blame] | 46 | #include <errno.h> |
| 47 | #include <fcntl.h> |
Tao Bao | 91a7aa4 | 2017-05-01 15:57:38 -0700 | [diff] [blame] | 48 | #include <limits.h> // PATH_MAX |
Doug Zongker | 075ad80 | 2014-06-26 15:35:51 -0700 | [diff] [blame] | 49 | #include <linux/fuse.h> |
Tao Bao | 91a7aa4 | 2017-05-01 15:57:38 -0700 | [diff] [blame] | 50 | #include <stdint.h> |
Doug Zongker | 075ad80 | 2014-06-26 15:35:51 -0700 | [diff] [blame] | 51 | #include <stdio.h> |
| 52 | #include <stdlib.h> |
| 53 | #include <string.h> |
Doug Zongker | 075ad80 | 2014-06-26 15:35:51 -0700 | [diff] [blame] | 54 | #include <sys/mount.h> |
Tao Bao | 91a7aa4 | 2017-05-01 15:57:38 -0700 | [diff] [blame] | 55 | #include <sys/param.h> // MIN |
Doug Zongker | 075ad80 | 2014-06-26 15:35:51 -0700 | [diff] [blame] | 56 | #include <sys/stat.h> |
Doug Zongker | 075ad80 | 2014-06-26 15:35:51 -0700 | [diff] [blame] | 57 | #include <sys/uio.h> |
| 58 | #include <unistd.h> |
| 59 | |
Tao Bao | 91a7aa4 | 2017-05-01 15:57:38 -0700 | [diff] [blame] | 60 | #include <array> |
Tao Bao | ed13819 | 2017-05-01 22:30:39 -0700 | [diff] [blame] | 61 | #include <string> |
Tao Bao | 91a7aa4 | 2017-05-01 15:57:38 -0700 | [diff] [blame] | 62 | #include <vector> |
Tao Bao | ed13819 | 2017-05-01 22:30:39 -0700 | [diff] [blame] | 63 | |
| 64 | #include <android-base/stringprintf.h> |
Tao Bao | 91a7aa4 | 2017-05-01 15:57:38 -0700 | [diff] [blame] | 65 | #include <android-base/unique_fd.h> |
Mattias Nissler | 452df6d | 2016-04-04 16:17:01 +0200 | [diff] [blame] | 66 | #include <openssl/sha.h> |
| 67 | |
Tao Bao | 91a7aa4 | 2017-05-01 15:57:38 -0700 | [diff] [blame] | 68 | static constexpr uint64_t PACKAGE_FILE_ID = FUSE_ROOT_ID + 1; |
| 69 | static constexpr uint64_t EXIT_FLAG_ID = FUSE_ROOT_ID + 2; |
Doug Zongker | 075ad80 | 2014-06-26 15:35:51 -0700 | [diff] [blame] | 70 | |
Tao Bao | 91a7aa4 | 2017-05-01 15:57:38 -0700 | [diff] [blame] | 71 | static constexpr int NO_STATUS = 1; |
| 72 | static constexpr int NO_STATUS_EXIT = 2; |
Doug Zongker | 075ad80 | 2014-06-26 15:35:51 -0700 | [diff] [blame] | 73 | |
Tao Bao | 91a7aa4 | 2017-05-01 15:57:38 -0700 | [diff] [blame] | 74 | using SHA256Digest = std::array<uint8_t, SHA256_DIGEST_LENGTH>; |
Doug Zongker | 075ad80 | 2014-06-26 15:35:51 -0700 | [diff] [blame] | 75 | |
Tom Marshall | c07fbfc | 2020-03-06 18:50:31 +0100 | [diff] [blame] | 76 | #define INSTALL_REQUIRED_MEMORY (400 * 1024 * 1024) |
Tom Marshall | 0e5905c | 2014-12-16 14:06:02 -0800 | [diff] [blame] | 77 | |
Doug Zongker | 075ad80 | 2014-06-26 15:35:51 -0700 | [diff] [blame] | 78 | struct fuse_data { |
Tao Bao | 91a7aa4 | 2017-05-01 15:57:38 -0700 | [diff] [blame] | 79 | android::base::unique_fd ffd; // file descriptor for the fuse socket |
Doug Zongker | 18a78e0 | 2014-07-10 07:31:46 -0700 | [diff] [blame] | 80 | |
xunchang | 5e6832a | 2019-03-15 16:04:32 -0700 | [diff] [blame] | 81 | FuseDataProvider* provider; // Provider of the source data. |
Doug Zongker | 075ad80 | 2014-06-26 15:35:51 -0700 | [diff] [blame] | 82 | |
Tao Bao | 91a7aa4 | 2017-05-01 15:57:38 -0700 | [diff] [blame] | 83 | uint64_t file_size; // bytes |
Doug Zongker | 075ad80 | 2014-06-26 15:35:51 -0700 | [diff] [blame] | 84 | |
Tao Bao | 91a7aa4 | 2017-05-01 15:57:38 -0700 | [diff] [blame] | 85 | uint32_t block_size; // block size that the adb host is using to send the file to us |
| 86 | uint32_t file_blocks; // file size in block_size blocks |
Doug Zongker | 075ad80 | 2014-06-26 15:35:51 -0700 | [diff] [blame] | 87 | |
Tao Bao | 91a7aa4 | 2017-05-01 15:57:38 -0700 | [diff] [blame] | 88 | uid_t uid; |
| 89 | gid_t gid; |
Doug Zongker | 075ad80 | 2014-06-26 15:35:51 -0700 | [diff] [blame] | 90 | |
Tom Marshall | 0e5905c | 2014-12-16 14:06:02 -0800 | [diff] [blame] | 91 | uint32_t curr_block; // cache the block most recently used |
Tao Bao | 91a7aa4 | 2017-05-01 15:57:38 -0700 | [diff] [blame] | 92 | uint8_t* block_data; |
Doug Zongker | 075ad80 | 2014-06-26 15:35:51 -0700 | [diff] [blame] | 93 | |
Tao Bao | 91a7aa4 | 2017-05-01 15:57:38 -0700 | [diff] [blame] | 94 | uint8_t* extra_block; // another block of storage for reads that span two blocks |
Doug Zongker | 075ad80 | 2014-06-26 15:35:51 -0700 | [diff] [blame] | 95 | |
Tao Bao | 91a7aa4 | 2017-05-01 15:57:38 -0700 | [diff] [blame] | 96 | std::vector<SHA256Digest> |
| 97 | hashes; // SHA-256 hash of each block (all zeros if block hasn't been read yet) |
Tom Marshall | 0e5905c | 2014-12-16 14:06:02 -0800 | [diff] [blame] | 98 | |
| 99 | // Block cache |
| 100 | uint32_t block_cache_max_size; // Max allowed block cache size |
| 101 | uint32_t block_cache_size; // Current block cache size |
| 102 | uint8_t** block_cache; // Block cache data |
Doug Zongker | 075ad80 | 2014-06-26 15:35:51 -0700 | [diff] [blame] | 103 | }; |
| 104 | |
Tom Marshall | 0e5905c | 2014-12-16 14:06:02 -0800 | [diff] [blame] | 105 | static uint64_t free_memory() { |
| 106 | uint64_t mem = 0; |
| 107 | FILE* fp = fopen("/proc/meminfo", "r"); |
| 108 | if (fp) { |
| 109 | char buf[256]; |
| 110 | char* linebuf = buf; |
| 111 | size_t buflen = sizeof(buf); |
| 112 | while (getline(&linebuf, &buflen, fp) > 0) { |
| 113 | char* key = buf; |
| 114 | char* val = strchr(buf, ':'); |
| 115 | *val = '\0'; |
| 116 | ++val; |
| 117 | if (strcmp(key, "MemFree") == 0) { |
| 118 | mem += strtoul(val, nullptr, 0) * 1024; |
| 119 | } |
| 120 | if (strcmp(key, "Buffers") == 0) { |
| 121 | mem += strtoul(val, nullptr, 0) * 1024; |
| 122 | } |
| 123 | if (strcmp(key, "Cached") == 0) { |
| 124 | mem += strtoul(val, nullptr, 0) * 1024; |
| 125 | } |
| 126 | } |
| 127 | fclose(fp); |
| 128 | } |
| 129 | return mem; |
| 130 | } |
| 131 | |
| 132 | static int block_cache_fetch(struct fuse_data* fd, uint32_t block) { |
| 133 | if (fd->block_cache == nullptr) { |
| 134 | return -1; |
| 135 | } |
| 136 | if (fd->block_cache[block] == nullptr) { |
| 137 | return -1; |
| 138 | } |
| 139 | memcpy(fd->block_data, fd->block_cache[block], fd->block_size); |
| 140 | return 0; |
| 141 | } |
| 142 | |
| 143 | static void block_cache_enter(struct fuse_data* fd, uint32_t block) { |
| 144 | if (!fd->block_cache) return; |
| 145 | if (fd->block_cache_size == fd->block_cache_max_size) { |
| 146 | // Evict a block from the cache. Since the file is typically read |
| 147 | // sequentially, start looking from the block behind the current |
| 148 | // block and proceed backward. |
| 149 | int n; |
| 150 | for (n = fd->curr_block - 1; n != (int)fd->curr_block; --n) { |
| 151 | if (n < 0) { |
| 152 | n = fd->file_blocks - 1; |
| 153 | } |
| 154 | if (fd->block_cache[n]) { |
| 155 | free(fd->block_cache[n]); |
| 156 | fd->block_cache[n] = nullptr; |
| 157 | fd->block_cache_size--; |
| 158 | break; |
| 159 | } |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | fd->block_cache[block] = (uint8_t*)malloc(fd->block_size); |
| 164 | memcpy(fd->block_cache[block], fd->block_data, fd->block_size); |
| 165 | |
| 166 | fd->block_cache_size++; |
| 167 | } |
| 168 | |
Tao Bao | 91a7aa4 | 2017-05-01 15:57:38 -0700 | [diff] [blame] | 169 | static void fuse_reply(const fuse_data* fd, uint64_t unique, const void* data, size_t len) { |
| 170 | fuse_out_header hdr; |
| 171 | hdr.len = len + sizeof(hdr); |
| 172 | hdr.error = 0; |
| 173 | hdr.unique = unique; |
Doug Zongker | 075ad80 | 2014-06-26 15:35:51 -0700 | [diff] [blame] | 174 | |
Tao Bao | 91a7aa4 | 2017-05-01 15:57:38 -0700 | [diff] [blame] | 175 | struct iovec vec[2]; |
| 176 | vec[0].iov_base = &hdr; |
| 177 | vec[0].iov_len = sizeof(hdr); |
| 178 | vec[1].iov_base = const_cast<void*>(data); |
| 179 | vec[1].iov_len = len; |
Doug Zongker | 075ad80 | 2014-06-26 15:35:51 -0700 | [diff] [blame] | 180 | |
Tao Bao | 91a7aa4 | 2017-05-01 15:57:38 -0700 | [diff] [blame] | 181 | int res = writev(fd->ffd, vec, 2); |
| 182 | if (res == -1) { |
| 183 | printf("*** REPLY FAILED *** %s\n", strerror(errno)); |
| 184 | } |
Doug Zongker | 075ad80 | 2014-06-26 15:35:51 -0700 | [diff] [blame] | 185 | } |
| 186 | |
Tao Bao | 91a7aa4 | 2017-05-01 15:57:38 -0700 | [diff] [blame] | 187 | static int handle_init(void* data, fuse_data* fd, const fuse_in_header* hdr) { |
| 188 | const fuse_init_in* req = static_cast<const fuse_init_in*>(data); |
Christopher Ferris | 1d30c2f | 2014-09-16 14:53:39 -0700 | [diff] [blame] | 189 | |
Tao Bao | 91a7aa4 | 2017-05-01 15:57:38 -0700 | [diff] [blame] | 190 | // Kernel 2.6.16 is the first stable kernel with struct fuse_init_out defined (fuse version 7.6). |
| 191 | // The structure is the same from 7.6 through 7.22. Beginning with 7.23, the structure increased |
| 192 | // in size and added new parameters. |
| 193 | if (req->major != FUSE_KERNEL_VERSION || req->minor < 6) { |
| 194 | printf("Fuse kernel version mismatch: Kernel version %d.%d, Expected at least %d.6", req->major, |
| 195 | req->minor, FUSE_KERNEL_VERSION); |
| 196 | return -1; |
| 197 | } |
Christopher Ferris | 1d30c2f | 2014-09-16 14:53:39 -0700 | [diff] [blame] | 198 | |
Tao Bao | 91a7aa4 | 2017-05-01 15:57:38 -0700 | [diff] [blame] | 199 | fuse_init_out out; |
| 200 | out.minor = MIN(req->minor, FUSE_KERNEL_MINOR_VERSION); |
| 201 | size_t fuse_struct_size = sizeof(out); |
Christopher Ferris | 1d30c2f | 2014-09-16 14:53:39 -0700 | [diff] [blame] | 202 | #if defined(FUSE_COMPAT_22_INIT_OUT_SIZE) |
Tao Bao | 91a7aa4 | 2017-05-01 15:57:38 -0700 | [diff] [blame] | 203 | /* FUSE_KERNEL_VERSION >= 23. */ |
Christopher Ferris | 1d30c2f | 2014-09-16 14:53:39 -0700 | [diff] [blame] | 204 | |
Tao Bao | 91a7aa4 | 2017-05-01 15:57:38 -0700 | [diff] [blame] | 205 | // If the kernel only works on minor revs older than or equal to 22, then use the older structure |
| 206 | // size since this code only uses the 7.22 version of the structure. |
| 207 | if (req->minor <= 22) { |
| 208 | fuse_struct_size = FUSE_COMPAT_22_INIT_OUT_SIZE; |
| 209 | } |
Christopher Ferris | 1d30c2f | 2014-09-16 14:53:39 -0700 | [diff] [blame] | 210 | #endif |
Doug Zongker | 075ad80 | 2014-06-26 15:35:51 -0700 | [diff] [blame] | 211 | |
Tao Bao | 91a7aa4 | 2017-05-01 15:57:38 -0700 | [diff] [blame] | 212 | out.major = FUSE_KERNEL_VERSION; |
| 213 | out.max_readahead = req->max_readahead; |
| 214 | out.flags = 0; |
| 215 | out.max_background = 32; |
| 216 | out.congestion_threshold = 32; |
| 217 | out.max_write = 4096; |
| 218 | fuse_reply(fd, hdr->unique, &out, fuse_struct_size); |
Doug Zongker | 075ad80 | 2014-06-26 15:35:51 -0700 | [diff] [blame] | 219 | |
Tao Bao | 91a7aa4 | 2017-05-01 15:57:38 -0700 | [diff] [blame] | 220 | return NO_STATUS; |
Doug Zongker | 075ad80 | 2014-06-26 15:35:51 -0700 | [diff] [blame] | 221 | } |
| 222 | |
Tao Bao | 91a7aa4 | 2017-05-01 15:57:38 -0700 | [diff] [blame] | 223 | static void fill_attr(fuse_attr* attr, const fuse_data* fd, uint64_t nodeid, uint64_t size, |
| 224 | uint32_t mode) { |
| 225 | *attr = {}; |
| 226 | attr->nlink = 1; |
| 227 | attr->uid = fd->uid; |
| 228 | attr->gid = fd->gid; |
| 229 | attr->blksize = 4096; |
Doug Zongker | 075ad80 | 2014-06-26 15:35:51 -0700 | [diff] [blame] | 230 | |
Tao Bao | 91a7aa4 | 2017-05-01 15:57:38 -0700 | [diff] [blame] | 231 | attr->ino = nodeid; |
| 232 | attr->size = size; |
| 233 | attr->blocks = (size == 0) ? 0 : (((size - 1) / attr->blksize) + 1); |
| 234 | attr->mode = mode; |
Doug Zongker | 075ad80 | 2014-06-26 15:35:51 -0700 | [diff] [blame] | 235 | } |
| 236 | |
Tao Bao | 91a7aa4 | 2017-05-01 15:57:38 -0700 | [diff] [blame] | 237 | static int handle_getattr(void* /* data */, const fuse_data* fd, const fuse_in_header* hdr) { |
| 238 | fuse_attr_out out = {}; |
| 239 | out.attr_valid = 10; |
Doug Zongker | 075ad80 | 2014-06-26 15:35:51 -0700 | [diff] [blame] | 240 | |
Tao Bao | 91a7aa4 | 2017-05-01 15:57:38 -0700 | [diff] [blame] | 241 | if (hdr->nodeid == FUSE_ROOT_ID) { |
| 242 | fill_attr(&(out.attr), fd, hdr->nodeid, 4096, S_IFDIR | 0555); |
| 243 | } else if (hdr->nodeid == PACKAGE_FILE_ID) { |
| 244 | fill_attr(&(out.attr), fd, PACKAGE_FILE_ID, fd->file_size, S_IFREG | 0444); |
| 245 | } else if (hdr->nodeid == EXIT_FLAG_ID) { |
| 246 | fill_attr(&(out.attr), fd, EXIT_FLAG_ID, 0, S_IFREG | 0); |
| 247 | } else { |
| 248 | return -ENOENT; |
| 249 | } |
Doug Zongker | 075ad80 | 2014-06-26 15:35:51 -0700 | [diff] [blame] | 250 | |
Tao Bao | 91a7aa4 | 2017-05-01 15:57:38 -0700 | [diff] [blame] | 251 | fuse_reply(fd, hdr->unique, &out, sizeof(out)); |
| 252 | return (hdr->nodeid == EXIT_FLAG_ID) ? NO_STATUS_EXIT : NO_STATUS; |
Doug Zongker | 075ad80 | 2014-06-26 15:35:51 -0700 | [diff] [blame] | 253 | } |
| 254 | |
Tao Bao | 91a7aa4 | 2017-05-01 15:57:38 -0700 | [diff] [blame] | 255 | static int handle_lookup(void* data, const fuse_data* fd, const fuse_in_header* hdr) { |
| 256 | if (data == nullptr) return -ENOENT; |
Doug Zongker | 075ad80 | 2014-06-26 15:35:51 -0700 | [diff] [blame] | 257 | |
Tao Bao | 91a7aa4 | 2017-05-01 15:57:38 -0700 | [diff] [blame] | 258 | fuse_entry_out out = {}; |
| 259 | out.entry_valid = 10; |
| 260 | out.attr_valid = 10; |
Doug Zongker | 075ad80 | 2014-06-26 15:35:51 -0700 | [diff] [blame] | 261 | |
Tao Bao | 91a7aa4 | 2017-05-01 15:57:38 -0700 | [diff] [blame] | 262 | std::string filename(static_cast<const char*>(data)); |
| 263 | if (filename == FUSE_SIDELOAD_HOST_FILENAME) { |
| 264 | out.nodeid = PACKAGE_FILE_ID; |
| 265 | out.generation = PACKAGE_FILE_ID; |
| 266 | fill_attr(&(out.attr), fd, PACKAGE_FILE_ID, fd->file_size, S_IFREG | 0444); |
| 267 | } else if (filename == FUSE_SIDELOAD_HOST_EXIT_FLAG) { |
| 268 | out.nodeid = EXIT_FLAG_ID; |
| 269 | out.generation = EXIT_FLAG_ID; |
| 270 | fill_attr(&(out.attr), fd, EXIT_FLAG_ID, 0, S_IFREG | 0); |
| 271 | } else { |
| 272 | return -ENOENT; |
| 273 | } |
| 274 | |
| 275 | fuse_reply(fd, hdr->unique, &out, sizeof(out)); |
| 276 | return (out.nodeid == EXIT_FLAG_ID) ? NO_STATUS_EXIT : NO_STATUS; |
Doug Zongker | 075ad80 | 2014-06-26 15:35:51 -0700 | [diff] [blame] | 277 | } |
| 278 | |
Tao Bao | 91a7aa4 | 2017-05-01 15:57:38 -0700 | [diff] [blame] | 279 | static int handle_open(void* /* data */, const fuse_data* fd, const fuse_in_header* hdr) { |
| 280 | if (hdr->nodeid == EXIT_FLAG_ID) return -EPERM; |
| 281 | if (hdr->nodeid != PACKAGE_FILE_ID) return -ENOENT; |
Doug Zongker | 075ad80 | 2014-06-26 15:35:51 -0700 | [diff] [blame] | 282 | |
Tao Bao | 91a7aa4 | 2017-05-01 15:57:38 -0700 | [diff] [blame] | 283 | fuse_open_out out = {}; |
| 284 | out.fh = 10; // an arbitrary number; we always use the same handle |
| 285 | fuse_reply(fd, hdr->unique, &out, sizeof(out)); |
| 286 | return NO_STATUS; |
Doug Zongker | 075ad80 | 2014-06-26 15:35:51 -0700 | [diff] [blame] | 287 | } |
| 288 | |
Tao Bao | 91a7aa4 | 2017-05-01 15:57:38 -0700 | [diff] [blame] | 289 | static int handle_flush(void* /* data */, fuse_data* /* fd */, const fuse_in_header* /* hdr */) { |
| 290 | return 0; |
Doug Zongker | 075ad80 | 2014-06-26 15:35:51 -0700 | [diff] [blame] | 291 | } |
| 292 | |
Tao Bao | 91a7aa4 | 2017-05-01 15:57:38 -0700 | [diff] [blame] | 293 | static int handle_release(void* /* data */, fuse_data* /* fd */, const fuse_in_header* /* hdr */) { |
| 294 | return 0; |
Doug Zongker | 075ad80 | 2014-06-26 15:35:51 -0700 | [diff] [blame] | 295 | } |
| 296 | |
| 297 | // Fetch a block from the host into fd->curr_block and fd->block_data. |
| 298 | // Returns 0 on successful fetch, negative otherwise. |
Tao Bao | 91a7aa4 | 2017-05-01 15:57:38 -0700 | [diff] [blame] | 299 | static int fetch_block(fuse_data* fd, uint32_t block) { |
| 300 | if (block == fd->curr_block) { |
Doug Zongker | 075ad80 | 2014-06-26 15:35:51 -0700 | [diff] [blame] | 301 | return 0; |
Tao Bao | 91a7aa4 | 2017-05-01 15:57:38 -0700 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | if (block >= fd->file_blocks) { |
| 305 | memset(fd->block_data, 0, fd->block_size); |
| 306 | fd->curr_block = block; |
| 307 | return 0; |
| 308 | } |
| 309 | |
Tom Marshall | 0e5905c | 2014-12-16 14:06:02 -0800 | [diff] [blame] | 310 | if (block_cache_fetch(fd, block) == 0) { |
| 311 | fd->curr_block = block; |
| 312 | return 0; |
| 313 | } |
| 314 | |
xunchang | 5e6832a | 2019-03-15 16:04:32 -0700 | [diff] [blame] | 315 | uint32_t fetch_size = fd->block_size; |
Tao Bao | 91a7aa4 | 2017-05-01 15:57:38 -0700 | [diff] [blame] | 316 | if (block * fd->block_size + fetch_size > fd->file_size) { |
| 317 | // If we're reading the last (partial) block of the file, expect a shorter response from the |
| 318 | // host, and pad the rest of the block with zeroes. |
| 319 | fetch_size = fd->file_size - (block * fd->block_size); |
| 320 | memset(fd->block_data + fetch_size, 0, fd->block_size - fetch_size); |
| 321 | } |
| 322 | |
xunchang | 5e6832a | 2019-03-15 16:04:32 -0700 | [diff] [blame] | 323 | if (!fd->provider->ReadBlockAlignedData(fd->block_data, fetch_size, block)) { |
xunchang | ea2912f | 2019-03-17 16:45:12 -0700 | [diff] [blame] | 324 | return -EIO; |
| 325 | } |
Tao Bao | 91a7aa4 | 2017-05-01 15:57:38 -0700 | [diff] [blame] | 326 | |
| 327 | fd->curr_block = block; |
| 328 | |
| 329 | // Verify the hash of the block we just got from the host. |
| 330 | // |
| 331 | // - If the hash of the just-received data matches the stored hash for the block, accept it. |
| 332 | // - If the stored hash is all zeroes, store the new hash and accept the block (this is the first |
| 333 | // time we've read this block). |
| 334 | // - Otherwise, return -EINVAL for the read. |
| 335 | |
| 336 | SHA256Digest hash; |
| 337 | SHA256(fd->block_data, fd->block_size, hash.data()); |
| 338 | |
| 339 | const SHA256Digest& blockhash = fd->hashes[block]; |
| 340 | if (hash == blockhash) { |
| 341 | return 0; |
| 342 | } |
| 343 | |
| 344 | for (uint8_t i : blockhash) { |
| 345 | if (i != 0) { |
| 346 | fd->curr_block = -1; |
| 347 | return -EIO; |
| 348 | } |
| 349 | } |
| 350 | |
| 351 | fd->hashes[block] = hash; |
Tom Marshall | 0e5905c | 2014-12-16 14:06:02 -0800 | [diff] [blame] | 352 | block_cache_enter(fd, block); |
Tao Bao | 91a7aa4 | 2017-05-01 15:57:38 -0700 | [diff] [blame] | 353 | return 0; |
Doug Zongker | 075ad80 | 2014-06-26 15:35:51 -0700 | [diff] [blame] | 354 | } |
| 355 | |
Tao Bao | 91a7aa4 | 2017-05-01 15:57:38 -0700 | [diff] [blame] | 356 | static int handle_read(void* data, fuse_data* fd, const fuse_in_header* hdr) { |
| 357 | if (hdr->nodeid != PACKAGE_FILE_ID) return -ENOENT; |
Doug Zongker | 075ad80 | 2014-06-26 15:35:51 -0700 | [diff] [blame] | 358 | |
Tao Bao | 91a7aa4 | 2017-05-01 15:57:38 -0700 | [diff] [blame] | 359 | const fuse_read_in* req = static_cast<const fuse_read_in*>(data); |
| 360 | uint64_t offset = req->offset; |
| 361 | uint32_t size = req->size; |
Doug Zongker | 075ad80 | 2014-06-26 15:35:51 -0700 | [diff] [blame] | 362 | |
Tao Bao | 91a7aa4 | 2017-05-01 15:57:38 -0700 | [diff] [blame] | 363 | // The docs on the fuse kernel interface are vague about what to do when a read request extends |
| 364 | // past the end of the file. We can return a short read -- the return structure does include a |
| 365 | // length field -- but in testing that caused the program using the file to segfault. (I |
| 366 | // speculate that this is due to the reading program accessing it via mmap; maybe mmap dislikes |
| 367 | // when you return something short of a whole page?) To fix this we zero-pad reads that extend |
| 368 | // past the end of the file so we're always returning exactly as many bytes as were requested. |
| 369 | // (Users of the mapped file have to know its real length anyway.) |
Doug Zongker | 075ad80 | 2014-06-26 15:35:51 -0700 | [diff] [blame] | 370 | |
Tao Bao | 91a7aa4 | 2017-05-01 15:57:38 -0700 | [diff] [blame] | 371 | fuse_out_header outhdr; |
| 372 | outhdr.len = sizeof(outhdr) + size; |
| 373 | outhdr.error = 0; |
| 374 | outhdr.unique = hdr->unique; |
Doug Zongker | 075ad80 | 2014-06-26 15:35:51 -0700 | [diff] [blame] | 375 | |
Tao Bao | 91a7aa4 | 2017-05-01 15:57:38 -0700 | [diff] [blame] | 376 | struct iovec vec[3]; |
| 377 | vec[0].iov_base = &outhdr; |
| 378 | vec[0].iov_len = sizeof(outhdr); |
Doug Zongker | 075ad80 | 2014-06-26 15:35:51 -0700 | [diff] [blame] | 379 | |
Tao Bao | 91a7aa4 | 2017-05-01 15:57:38 -0700 | [diff] [blame] | 380 | uint32_t block = offset / fd->block_size; |
| 381 | int result = fetch_block(fd, block); |
| 382 | if (result != 0) return result; |
| 383 | |
| 384 | // Two cases: |
| 385 | // |
| 386 | // - the read request is entirely within this block. In this case we can reply immediately. |
| 387 | // |
| 388 | // - the read request goes over into the next block. Note that since we mount the filesystem |
| 389 | // with max_read=block_size, a read can never span more than two blocks. In this case we copy |
| 390 | // the block to extra_block and issue a fetch for the following block. |
| 391 | |
| 392 | uint32_t block_offset = offset - (block * fd->block_size); |
| 393 | |
| 394 | int vec_used; |
| 395 | if (size + block_offset <= fd->block_size) { |
| 396 | // First case: the read fits entirely in the first block. |
| 397 | |
| 398 | vec[1].iov_base = fd->block_data + block_offset; |
| 399 | vec[1].iov_len = size; |
| 400 | vec_used = 2; |
| 401 | } else { |
| 402 | // Second case: the read spills over into the next block. |
| 403 | |
| 404 | memcpy(fd->extra_block, fd->block_data + block_offset, fd->block_size - block_offset); |
| 405 | vec[1].iov_base = fd->extra_block; |
| 406 | vec[1].iov_len = fd->block_size - block_offset; |
| 407 | |
| 408 | result = fetch_block(fd, block + 1); |
Doug Zongker | 075ad80 | 2014-06-26 15:35:51 -0700 | [diff] [blame] | 409 | if (result != 0) return result; |
Tao Bao | 91a7aa4 | 2017-05-01 15:57:38 -0700 | [diff] [blame] | 410 | vec[2].iov_base = fd->block_data; |
| 411 | vec[2].iov_len = size - vec[1].iov_len; |
| 412 | vec_used = 3; |
| 413 | } |
Doug Zongker | 075ad80 | 2014-06-26 15:35:51 -0700 | [diff] [blame] | 414 | |
Tao Bao | 91a7aa4 | 2017-05-01 15:57:38 -0700 | [diff] [blame] | 415 | if (writev(fd->ffd, vec, vec_used) == -1) { |
| 416 | printf("*** READ REPLY FAILED: %s ***\n", strerror(errno)); |
| 417 | } |
| 418 | return NO_STATUS; |
Doug Zongker | 075ad80 | 2014-06-26 15:35:51 -0700 | [diff] [blame] | 419 | } |
| 420 | |
xunchang | 5e6832a | 2019-03-15 16:04:32 -0700 | [diff] [blame] | 421 | int run_fuse_sideload(std::unique_ptr<FuseDataProvider>&& provider, const char* mount_point) { |
Tao Bao | ed13819 | 2017-05-01 22:30:39 -0700 | [diff] [blame] | 422 | // If something's already mounted on our mountpoint, try to remove it. (Mostly in case of a |
| 423 | // previous abnormal exit.) |
Tao Bao | 91a7aa4 | 2017-05-01 15:57:38 -0700 | [diff] [blame] | 424 | umount2(mount_point, MNT_FORCE); |
Doug Zongker | 075ad80 | 2014-06-26 15:35:51 -0700 | [diff] [blame] | 425 | |
xunchang | 5e6832a | 2019-03-15 16:04:32 -0700 | [diff] [blame] | 426 | uint64_t file_size = provider->file_size(); |
| 427 | uint32_t block_size = provider->fuse_block_size(); |
| 428 | |
Tao Bao | ed13819 | 2017-05-01 22:30:39 -0700 | [diff] [blame] | 429 | // fs/fuse/inode.c in kernel code uses the greater of 4096 and the passed-in max_read. |
| 430 | if (block_size < 4096) { |
| 431 | fprintf(stderr, "block size (%u) is too small\n", block_size); |
| 432 | return -1; |
| 433 | } |
| 434 | if (block_size > (1 << 22)) { // 4 MiB |
| 435 | fprintf(stderr, "block size (%u) is too large\n", block_size); |
| 436 | return -1; |
| 437 | } |
Doug Zongker | 075ad80 | 2014-06-26 15:35:51 -0700 | [diff] [blame] | 438 | |
Tao Bao | 91a7aa4 | 2017-05-01 15:57:38 -0700 | [diff] [blame] | 439 | fuse_data fd = {}; |
xunchang | 5e6832a | 2019-03-15 16:04:32 -0700 | [diff] [blame] | 440 | fd.provider = provider.get(); |
Tao Bao | ed13819 | 2017-05-01 22:30:39 -0700 | [diff] [blame] | 441 | fd.file_size = file_size; |
| 442 | fd.block_size = block_size; |
| 443 | fd.file_blocks = (file_size == 0) ? 0 : (((file_size - 1) / block_size) + 1); |
Doug Zongker | 075ad80 | 2014-06-26 15:35:51 -0700 | [diff] [blame] | 444 | |
Tom Marshall | 0e5905c | 2014-12-16 14:06:02 -0800 | [diff] [blame] | 445 | uint64_t mem = free_memory(); |
| 446 | uint64_t avail = mem - (INSTALL_REQUIRED_MEMORY + fd.file_blocks * sizeof(uint8_t*)); |
| 447 | |
Tao Bao | ed13819 | 2017-05-01 22:30:39 -0700 | [diff] [blame] | 448 | int result; |
| 449 | if (fd.file_blocks > (1 << 18)) { |
| 450 | fprintf(stderr, "file has too many blocks (%u)\n", fd.file_blocks); |
| 451 | result = -1; |
| 452 | goto done; |
| 453 | } |
Doug Zongker | 075ad80 | 2014-06-26 15:35:51 -0700 | [diff] [blame] | 454 | |
Tao Bao | 91a7aa4 | 2017-05-01 15:57:38 -0700 | [diff] [blame] | 455 | // All hashes will be zero-initialized. |
| 456 | fd.hashes.resize(fd.file_blocks); |
Tao Bao | ed13819 | 2017-05-01 22:30:39 -0700 | [diff] [blame] | 457 | fd.uid = getuid(); |
| 458 | fd.gid = getgid(); |
Doug Zongker | 075ad80 | 2014-06-26 15:35:51 -0700 | [diff] [blame] | 459 | |
Tao Bao | ed13819 | 2017-05-01 22:30:39 -0700 | [diff] [blame] | 460 | fd.curr_block = -1; |
Tao Bao | 91a7aa4 | 2017-05-01 15:57:38 -0700 | [diff] [blame] | 461 | fd.block_data = static_cast<uint8_t*>(malloc(block_size)); |
| 462 | if (fd.block_data == nullptr) { |
Tao Bao | ed13819 | 2017-05-01 22:30:39 -0700 | [diff] [blame] | 463 | fprintf(stderr, "failed to allocate %d bites for block_data\n", block_size); |
| 464 | result = -1; |
| 465 | goto done; |
| 466 | } |
Tao Bao | 91a7aa4 | 2017-05-01 15:57:38 -0700 | [diff] [blame] | 467 | fd.extra_block = static_cast<uint8_t*>(malloc(block_size)); |
| 468 | if (fd.extra_block == nullptr) { |
Tao Bao | ed13819 | 2017-05-01 22:30:39 -0700 | [diff] [blame] | 469 | fprintf(stderr, "failed to allocate %d bites for extra_block\n", block_size); |
| 470 | result = -1; |
| 471 | goto done; |
| 472 | } |
Doug Zongker | 075ad80 | 2014-06-26 15:35:51 -0700 | [diff] [blame] | 473 | |
Tom Marshall | 0e5905c | 2014-12-16 14:06:02 -0800 | [diff] [blame] | 474 | fd.block_cache_max_size = 0; |
| 475 | fd.block_cache_size = 0; |
| 476 | fd.block_cache = nullptr; |
| 477 | if (mem > avail) { |
| 478 | uint32_t max_size = avail / fd.block_size; |
| 479 | if (max_size > fd.file_blocks) { |
| 480 | max_size = fd.file_blocks; |
| 481 | } |
| 482 | // The cache must be at least 1% of the file size or two blocks, |
| 483 | // whichever is larger. |
| 484 | if (max_size >= fd.file_blocks / 100 && max_size >= 2) { |
| 485 | fd.block_cache_max_size = max_size; |
| 486 | fd.block_cache = (uint8_t**)calloc(fd.file_blocks, sizeof(uint8_t*)); |
| 487 | } |
| 488 | } |
| 489 | |
Tao Bao | 91a7aa4 | 2017-05-01 15:57:38 -0700 | [diff] [blame] | 490 | fd.ffd.reset(open("/dev/fuse", O_RDWR)); |
Bernie Innocenti | 8bd6f45 | 2019-03-28 15:48:08 +0900 | [diff] [blame] | 491 | if (fd.ffd == -1) { |
Tao Bao | ed13819 | 2017-05-01 22:30:39 -0700 | [diff] [blame] | 492 | perror("open /dev/fuse"); |
| 493 | result = -1; |
| 494 | goto done; |
| 495 | } |
Doug Zongker | 075ad80 | 2014-06-26 15:35:51 -0700 | [diff] [blame] | 496 | |
Tao Bao | ed13819 | 2017-05-01 22:30:39 -0700 | [diff] [blame] | 497 | { |
| 498 | std::string opts = android::base::StringPrintf( |
Tao Bao | 91a7aa4 | 2017-05-01 15:57:38 -0700 | [diff] [blame] | 499 | "fd=%d,user_id=%d,group_id=%d,max_read=%u,allow_other,rootmode=040000", fd.ffd.get(), |
| 500 | fd.uid, fd.gid, block_size); |
Doug Zongker | 075ad80 | 2014-06-26 15:35:51 -0700 | [diff] [blame] | 501 | |
Tao Bao | 91a7aa4 | 2017-05-01 15:57:38 -0700 | [diff] [blame] | 502 | result = mount("/dev/fuse", mount_point, "fuse", MS_NOSUID | MS_NODEV | MS_RDONLY | MS_NOEXEC, |
| 503 | opts.c_str()); |
| 504 | if (result == -1) { |
Tao Bao | ed13819 | 2017-05-01 22:30:39 -0700 | [diff] [blame] | 505 | perror("mount"); |
| 506 | goto done; |
Doug Zongker | 075ad80 | 2014-06-26 15:35:51 -0700 | [diff] [blame] | 507 | } |
Tao Bao | ed13819 | 2017-05-01 22:30:39 -0700 | [diff] [blame] | 508 | } |
Doug Zongker | 075ad80 | 2014-06-26 15:35:51 -0700 | [diff] [blame] | 509 | |
Tao Bao | 91a7aa4 | 2017-05-01 15:57:38 -0700 | [diff] [blame] | 510 | uint8_t request_buffer[sizeof(fuse_in_header) + PATH_MAX * 8]; |
Tao Bao | ed13819 | 2017-05-01 22:30:39 -0700 | [diff] [blame] | 511 | for (;;) { |
| 512 | ssize_t len = TEMP_FAILURE_RETRY(read(fd.ffd, request_buffer, sizeof(request_buffer))); |
| 513 | if (len == -1) { |
| 514 | perror("read request"); |
| 515 | if (errno == ENODEV) { |
| 516 | result = -1; |
| 517 | break; |
| 518 | } |
| 519 | continue; |
Doug Zongker | 075ad80 | 2014-06-26 15:35:51 -0700 | [diff] [blame] | 520 | } |
| 521 | |
Tao Bao | 91a7aa4 | 2017-05-01 15:57:38 -0700 | [diff] [blame] | 522 | if (static_cast<size_t>(len) < sizeof(fuse_in_header)) { |
Tao Bao | ed13819 | 2017-05-01 22:30:39 -0700 | [diff] [blame] | 523 | fprintf(stderr, "request too short: len=%zd\n", len); |
| 524 | continue; |
Doug Zongker | 075ad80 | 2014-06-26 15:35:51 -0700 | [diff] [blame] | 525 | } |
| 526 | |
Tao Bao | 91a7aa4 | 2017-05-01 15:57:38 -0700 | [diff] [blame] | 527 | fuse_in_header* hdr = reinterpret_cast<fuse_in_header*>(request_buffer); |
| 528 | void* data = request_buffer + sizeof(fuse_in_header); |
Doug Zongker | 075ad80 | 2014-06-26 15:35:51 -0700 | [diff] [blame] | 529 | |
Tao Bao | ed13819 | 2017-05-01 22:30:39 -0700 | [diff] [blame] | 530 | result = -ENOSYS; |
| 531 | |
| 532 | switch (hdr->opcode) { |
| 533 | case FUSE_INIT: |
| 534 | result = handle_init(data, &fd, hdr); |
| 535 | break; |
| 536 | |
| 537 | case FUSE_LOOKUP: |
| 538 | result = handle_lookup(data, &fd, hdr); |
| 539 | break; |
| 540 | |
| 541 | case FUSE_GETATTR: |
| 542 | result = handle_getattr(data, &fd, hdr); |
| 543 | break; |
| 544 | |
| 545 | case FUSE_OPEN: |
| 546 | result = handle_open(data, &fd, hdr); |
| 547 | break; |
| 548 | |
| 549 | case FUSE_READ: |
| 550 | result = handle_read(data, &fd, hdr); |
| 551 | break; |
| 552 | |
| 553 | case FUSE_FLUSH: |
| 554 | result = handle_flush(data, &fd, hdr); |
| 555 | break; |
| 556 | |
| 557 | case FUSE_RELEASE: |
| 558 | result = handle_release(data, &fd, hdr); |
| 559 | break; |
| 560 | |
| 561 | default: |
| 562 | fprintf(stderr, "unknown fuse request opcode %d\n", hdr->opcode); |
| 563 | break; |
| 564 | } |
| 565 | |
| 566 | if (result == NO_STATUS_EXIT) { |
| 567 | result = 0; |
| 568 | break; |
| 569 | } |
| 570 | |
| 571 | if (result != NO_STATUS) { |
Tao Bao | 91a7aa4 | 2017-05-01 15:57:38 -0700 | [diff] [blame] | 572 | fuse_out_header outhdr; |
Tao Bao | ed13819 | 2017-05-01 22:30:39 -0700 | [diff] [blame] | 573 | outhdr.len = sizeof(outhdr); |
| 574 | outhdr.error = result; |
| 575 | outhdr.unique = hdr->unique; |
| 576 | TEMP_FAILURE_RETRY(write(fd.ffd, &outhdr, sizeof(outhdr))); |
| 577 | } |
| 578 | } |
| 579 | |
| 580 | done: |
xunchang | 5e6832a | 2019-03-15 16:04:32 -0700 | [diff] [blame] | 581 | provider->Close(); |
Tao Bao | ed13819 | 2017-05-01 22:30:39 -0700 | [diff] [blame] | 582 | |
Tao Bao | 91a7aa4 | 2017-05-01 15:57:38 -0700 | [diff] [blame] | 583 | if (umount2(mount_point, MNT_DETACH) == -1) { |
| 584 | fprintf(stderr, "fuse_sideload umount failed: %s\n", strerror(errno)); |
Tao Bao | ed13819 | 2017-05-01 22:30:39 -0700 | [diff] [blame] | 585 | } |
| 586 | |
Tom Marshall | 0e5905c | 2014-12-16 14:06:02 -0800 | [diff] [blame] | 587 | if (fd.block_cache) { |
| 588 | uint32_t n; |
| 589 | for (n = 0; n < fd.file_blocks; ++n) { |
| 590 | free(fd.block_cache[n]); |
| 591 | } |
| 592 | free(fd.block_cache); |
| 593 | } |
| 594 | |
Tao Bao | ed13819 | 2017-05-01 22:30:39 -0700 | [diff] [blame] | 595 | free(fd.block_data); |
| 596 | free(fd.extra_block); |
| 597 | |
| 598 | return result; |
Doug Zongker | 075ad80 | 2014-06-26 15:35:51 -0700 | [diff] [blame] | 599 | } |