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> |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 48 | #include <limits.h> // PATH_MAX |
| 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> |
Doug Zongker | 075ad80 | 2014-06-26 15:35:51 -0700 | [diff] [blame] | 63 | |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 64 | #include <android-base/stringprintf.h> |
| 65 | #include <android-base/unique_fd.h> |
| 66 | #include <openssl/sha.h> |
Doug Zongker | 075ad80 | 2014-06-26 15:35:51 -0700 | [diff] [blame] | 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 | |
| 76 | struct fuse_data { |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 77 | android::base::unique_fd ffd; // file descriptor for the fuse socket |
Doug Zongker | 18a78e0 | 2014-07-10 07:31:46 -0700 | [diff] [blame] | 78 | |
xunchang | 5e6832a | 2019-03-15 16:04:32 -0700 | [diff] [blame] | 79 | FuseDataProvider* provider; // Provider of the source data. |
Doug Zongker | 075ad80 | 2014-06-26 15:35:51 -0700 | [diff] [blame] | 80 | |
Tao Bao | 91a7aa4 | 2017-05-01 15:57:38 -0700 | [diff] [blame] | 81 | uint64_t file_size; // bytes |
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 | uint32_t block_size; // block size that the adb host is using to send the file to us |
| 84 | uint32_t file_blocks; // file size in block_size blocks |
Doug Zongker | 075ad80 | 2014-06-26 15:35:51 -0700 | [diff] [blame] | 85 | |
Tao Bao | 91a7aa4 | 2017-05-01 15:57:38 -0700 | [diff] [blame] | 86 | uid_t uid; |
| 87 | gid_t gid; |
Doug Zongker | 075ad80 | 2014-06-26 15:35:51 -0700 | [diff] [blame] | 88 | |
Tao Bao | 91a7aa4 | 2017-05-01 15:57:38 -0700 | [diff] [blame] | 89 | uint32_t curr_block; // cache the block most recently read from the host |
| 90 | uint8_t* block_data; |
Doug Zongker | 075ad80 | 2014-06-26 15:35:51 -0700 | [diff] [blame] | 91 | |
Tao Bao | 91a7aa4 | 2017-05-01 15:57:38 -0700 | [diff] [blame] | 92 | 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] | 93 | |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 94 | std::vector<SHA256Digest> |
| 95 | hashes; // SHA-256 hash of each block (all zeros if block hasn't been read yet) |
Doug Zongker | 075ad80 | 2014-06-26 15:35:51 -0700 | [diff] [blame] | 96 | }; |
| 97 | |
Tao Bao | 91a7aa4 | 2017-05-01 15:57:38 -0700 | [diff] [blame] | 98 | static void fuse_reply(const fuse_data* fd, uint64_t unique, const void* data, size_t len) { |
| 99 | fuse_out_header hdr; |
| 100 | hdr.len = len + sizeof(hdr); |
| 101 | hdr.error = 0; |
| 102 | hdr.unique = unique; |
Doug Zongker | 075ad80 | 2014-06-26 15:35:51 -0700 | [diff] [blame] | 103 | |
Tao Bao | 91a7aa4 | 2017-05-01 15:57:38 -0700 | [diff] [blame] | 104 | struct iovec vec[2]; |
| 105 | vec[0].iov_base = &hdr; |
| 106 | vec[0].iov_len = sizeof(hdr); |
| 107 | vec[1].iov_base = const_cast<void*>(data); |
| 108 | vec[1].iov_len = len; |
Doug Zongker | 075ad80 | 2014-06-26 15:35:51 -0700 | [diff] [blame] | 109 | |
Tao Bao | 91a7aa4 | 2017-05-01 15:57:38 -0700 | [diff] [blame] | 110 | int res = writev(fd->ffd, vec, 2); |
| 111 | if (res == -1) { |
| 112 | printf("*** REPLY FAILED *** %s\n", strerror(errno)); |
| 113 | } |
Doug Zongker | 075ad80 | 2014-06-26 15:35:51 -0700 | [diff] [blame] | 114 | } |
| 115 | |
Tao Bao | 91a7aa4 | 2017-05-01 15:57:38 -0700 | [diff] [blame] | 116 | static int handle_init(void* data, fuse_data* fd, const fuse_in_header* hdr) { |
| 117 | const fuse_init_in* req = static_cast<const fuse_init_in*>(data); |
Christopher Ferris | 1d30c2f | 2014-09-16 14:53:39 -0700 | [diff] [blame] | 118 | |
Tao Bao | 91a7aa4 | 2017-05-01 15:57:38 -0700 | [diff] [blame] | 119 | // Kernel 2.6.16 is the first stable kernel with struct fuse_init_out defined (fuse version 7.6). |
| 120 | // The structure is the same from 7.6 through 7.22. Beginning with 7.23, the structure increased |
| 121 | // in size and added new parameters. |
| 122 | if (req->major != FUSE_KERNEL_VERSION || req->minor < 6) { |
| 123 | printf("Fuse kernel version mismatch: Kernel version %d.%d, Expected at least %d.6", req->major, |
| 124 | req->minor, FUSE_KERNEL_VERSION); |
| 125 | return -1; |
| 126 | } |
Christopher Ferris | 1d30c2f | 2014-09-16 14:53:39 -0700 | [diff] [blame] | 127 | |
Tao Bao | 91a7aa4 | 2017-05-01 15:57:38 -0700 | [diff] [blame] | 128 | fuse_init_out out; |
| 129 | out.minor = MIN(req->minor, FUSE_KERNEL_MINOR_VERSION); |
| 130 | size_t fuse_struct_size = sizeof(out); |
Christopher Ferris | 1d30c2f | 2014-09-16 14:53:39 -0700 | [diff] [blame] | 131 | #if defined(FUSE_COMPAT_22_INIT_OUT_SIZE) |
Tao Bao | 91a7aa4 | 2017-05-01 15:57:38 -0700 | [diff] [blame] | 132 | /* FUSE_KERNEL_VERSION >= 23. */ |
Christopher Ferris | 1d30c2f | 2014-09-16 14:53:39 -0700 | [diff] [blame] | 133 | |
Tao Bao | 91a7aa4 | 2017-05-01 15:57:38 -0700 | [diff] [blame] | 134 | // If the kernel only works on minor revs older than or equal to 22, then use the older structure |
| 135 | // size since this code only uses the 7.22 version of the structure. |
| 136 | if (req->minor <= 22) { |
| 137 | fuse_struct_size = FUSE_COMPAT_22_INIT_OUT_SIZE; |
| 138 | } |
Christopher Ferris | 1d30c2f | 2014-09-16 14:53:39 -0700 | [diff] [blame] | 139 | #endif |
Doug Zongker | 075ad80 | 2014-06-26 15:35:51 -0700 | [diff] [blame] | 140 | |
Tao Bao | 91a7aa4 | 2017-05-01 15:57:38 -0700 | [diff] [blame] | 141 | out.major = FUSE_KERNEL_VERSION; |
| 142 | out.max_readahead = req->max_readahead; |
| 143 | out.flags = 0; |
| 144 | out.max_background = 32; |
| 145 | out.congestion_threshold = 32; |
| 146 | out.max_write = 4096; |
| 147 | fuse_reply(fd, hdr->unique, &out, fuse_struct_size); |
Doug Zongker | 075ad80 | 2014-06-26 15:35:51 -0700 | [diff] [blame] | 148 | |
Tao Bao | 91a7aa4 | 2017-05-01 15:57:38 -0700 | [diff] [blame] | 149 | return NO_STATUS; |
Doug Zongker | 075ad80 | 2014-06-26 15:35:51 -0700 | [diff] [blame] | 150 | } |
| 151 | |
Tao Bao | 91a7aa4 | 2017-05-01 15:57:38 -0700 | [diff] [blame] | 152 | static void fill_attr(fuse_attr* attr, const fuse_data* fd, uint64_t nodeid, uint64_t size, |
| 153 | uint32_t mode) { |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 154 | *attr = {}; |
Tao Bao | 91a7aa4 | 2017-05-01 15:57:38 -0700 | [diff] [blame] | 155 | attr->nlink = 1; |
| 156 | attr->uid = fd->uid; |
| 157 | attr->gid = fd->gid; |
| 158 | attr->blksize = 4096; |
Doug Zongker | 075ad80 | 2014-06-26 15:35:51 -0700 | [diff] [blame] | 159 | |
Tao Bao | 91a7aa4 | 2017-05-01 15:57:38 -0700 | [diff] [blame] | 160 | attr->ino = nodeid; |
| 161 | attr->size = size; |
| 162 | attr->blocks = (size == 0) ? 0 : (((size - 1) / attr->blksize) + 1); |
| 163 | attr->mode = mode; |
Doug Zongker | 075ad80 | 2014-06-26 15:35:51 -0700 | [diff] [blame] | 164 | } |
| 165 | |
Tao Bao | 91a7aa4 | 2017-05-01 15:57:38 -0700 | [diff] [blame] | 166 | static int handle_getattr(void* /* data */, const fuse_data* fd, const fuse_in_header* hdr) { |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 167 | fuse_attr_out out = {}; |
Tao Bao | 91a7aa4 | 2017-05-01 15:57:38 -0700 | [diff] [blame] | 168 | out.attr_valid = 10; |
Doug Zongker | 075ad80 | 2014-06-26 15:35:51 -0700 | [diff] [blame] | 169 | |
Tao Bao | 91a7aa4 | 2017-05-01 15:57:38 -0700 | [diff] [blame] | 170 | if (hdr->nodeid == FUSE_ROOT_ID) { |
| 171 | fill_attr(&(out.attr), fd, hdr->nodeid, 4096, S_IFDIR | 0555); |
| 172 | } else if (hdr->nodeid == PACKAGE_FILE_ID) { |
| 173 | fill_attr(&(out.attr), fd, PACKAGE_FILE_ID, fd->file_size, S_IFREG | 0444); |
| 174 | } else if (hdr->nodeid == EXIT_FLAG_ID) { |
| 175 | fill_attr(&(out.attr), fd, EXIT_FLAG_ID, 0, S_IFREG | 0); |
| 176 | } else { |
| 177 | return -ENOENT; |
| 178 | } |
Doug Zongker | 075ad80 | 2014-06-26 15:35:51 -0700 | [diff] [blame] | 179 | |
Tao Bao | 91a7aa4 | 2017-05-01 15:57:38 -0700 | [diff] [blame] | 180 | fuse_reply(fd, hdr->unique, &out, sizeof(out)); |
| 181 | return (hdr->nodeid == EXIT_FLAG_ID) ? NO_STATUS_EXIT : NO_STATUS; |
Doug Zongker | 075ad80 | 2014-06-26 15:35:51 -0700 | [diff] [blame] | 182 | } |
| 183 | |
Tao Bao | 91a7aa4 | 2017-05-01 15:57:38 -0700 | [diff] [blame] | 184 | static int handle_lookup(void* data, const fuse_data* fd, const fuse_in_header* hdr) { |
| 185 | if (data == nullptr) return -ENOENT; |
Doug Zongker | 075ad80 | 2014-06-26 15:35:51 -0700 | [diff] [blame] | 186 | |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 187 | fuse_entry_out out = {}; |
Tao Bao | 91a7aa4 | 2017-05-01 15:57:38 -0700 | [diff] [blame] | 188 | out.entry_valid = 10; |
| 189 | out.attr_valid = 10; |
Doug Zongker | 075ad80 | 2014-06-26 15:35:51 -0700 | [diff] [blame] | 190 | |
Tao Bao | 91a7aa4 | 2017-05-01 15:57:38 -0700 | [diff] [blame] | 191 | std::string filename(static_cast<const char*>(data)); |
| 192 | if (filename == FUSE_SIDELOAD_HOST_FILENAME) { |
| 193 | out.nodeid = PACKAGE_FILE_ID; |
| 194 | out.generation = PACKAGE_FILE_ID; |
| 195 | fill_attr(&(out.attr), fd, PACKAGE_FILE_ID, fd->file_size, S_IFREG | 0444); |
| 196 | } else if (filename == FUSE_SIDELOAD_HOST_EXIT_FLAG) { |
| 197 | out.nodeid = EXIT_FLAG_ID; |
| 198 | out.generation = EXIT_FLAG_ID; |
| 199 | fill_attr(&(out.attr), fd, EXIT_FLAG_ID, 0, S_IFREG | 0); |
| 200 | } else { |
| 201 | return -ENOENT; |
| 202 | } |
| 203 | |
| 204 | fuse_reply(fd, hdr->unique, &out, sizeof(out)); |
| 205 | return (out.nodeid == EXIT_FLAG_ID) ? NO_STATUS_EXIT : NO_STATUS; |
Doug Zongker | 075ad80 | 2014-06-26 15:35:51 -0700 | [diff] [blame] | 206 | } |
| 207 | |
Tao Bao | 91a7aa4 | 2017-05-01 15:57:38 -0700 | [diff] [blame] | 208 | static int handle_open(void* /* data */, const fuse_data* fd, const fuse_in_header* hdr) { |
| 209 | if (hdr->nodeid == EXIT_FLAG_ID) return -EPERM; |
| 210 | if (hdr->nodeid != PACKAGE_FILE_ID) return -ENOENT; |
Doug Zongker | 075ad80 | 2014-06-26 15:35:51 -0700 | [diff] [blame] | 211 | |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 212 | fuse_open_out out = {}; |
Tao Bao | 91a7aa4 | 2017-05-01 15:57:38 -0700 | [diff] [blame] | 213 | out.fh = 10; // an arbitrary number; we always use the same handle |
| 214 | fuse_reply(fd, hdr->unique, &out, sizeof(out)); |
| 215 | return NO_STATUS; |
Doug Zongker | 075ad80 | 2014-06-26 15:35:51 -0700 | [diff] [blame] | 216 | } |
| 217 | |
Tao Bao | 91a7aa4 | 2017-05-01 15:57:38 -0700 | [diff] [blame] | 218 | static int handle_flush(void* /* data */, fuse_data* /* fd */, const fuse_in_header* /* hdr */) { |
| 219 | return 0; |
Doug Zongker | 075ad80 | 2014-06-26 15:35:51 -0700 | [diff] [blame] | 220 | } |
| 221 | |
Tao Bao | 91a7aa4 | 2017-05-01 15:57:38 -0700 | [diff] [blame] | 222 | static int handle_release(void* /* data */, fuse_data* /* fd */, const fuse_in_header* /* hdr */) { |
| 223 | return 0; |
Doug Zongker | 075ad80 | 2014-06-26 15:35:51 -0700 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | // Fetch a block from the host into fd->curr_block and fd->block_data. |
| 227 | // Returns 0 on successful fetch, negative otherwise. |
luoqiangwei1 | cd1f4ca | 2021-09-16 18:25:23 +0800 | [diff] [blame] | 228 | static int fetch_block(fuse_data* fd, uint64_t block) { |
Tao Bao | 91a7aa4 | 2017-05-01 15:57:38 -0700 | [diff] [blame] | 229 | if (block == fd->curr_block) { |
Doug Zongker | 075ad80 | 2014-06-26 15:35:51 -0700 | [diff] [blame] | 230 | return 0; |
Tao Bao | 91a7aa4 | 2017-05-01 15:57:38 -0700 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | if (block >= fd->file_blocks) { |
| 234 | memset(fd->block_data, 0, fd->block_size); |
| 235 | fd->curr_block = block; |
| 236 | return 0; |
| 237 | } |
| 238 | |
xunchang | 5e6832a | 2019-03-15 16:04:32 -0700 | [diff] [blame] | 239 | uint32_t fetch_size = fd->block_size; |
Tao Bao | 91a7aa4 | 2017-05-01 15:57:38 -0700 | [diff] [blame] | 240 | if (block * fd->block_size + fetch_size > fd->file_size) { |
| 241 | // If we're reading the last (partial) block of the file, expect a shorter response from the |
| 242 | // host, and pad the rest of the block with zeroes. |
| 243 | fetch_size = fd->file_size - (block * fd->block_size); |
| 244 | memset(fd->block_data + fetch_size, 0, fd->block_size - fetch_size); |
| 245 | } |
| 246 | |
xunchang | 5e6832a | 2019-03-15 16:04:32 -0700 | [diff] [blame] | 247 | if (!fd->provider->ReadBlockAlignedData(fd->block_data, fetch_size, block)) { |
xunchang | ea2912f | 2019-03-17 16:45:12 -0700 | [diff] [blame] | 248 | return -EIO; |
| 249 | } |
Tao Bao | 91a7aa4 | 2017-05-01 15:57:38 -0700 | [diff] [blame] | 250 | |
| 251 | fd->curr_block = block; |
| 252 | |
| 253 | // Verify the hash of the block we just got from the host. |
| 254 | // |
| 255 | // - If the hash of the just-received data matches the stored hash for the block, accept it. |
| 256 | // - If the stored hash is all zeroes, store the new hash and accept the block (this is the first |
| 257 | // time we've read this block). |
| 258 | // - Otherwise, return -EINVAL for the read. |
| 259 | |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 260 | SHA256Digest hash; |
| 261 | SHA256(fd->block_data, fd->block_size, hash.data()); |
| 262 | |
| 263 | const SHA256Digest& blockhash = fd->hashes[block]; |
| 264 | if (hash == blockhash) { |
Tao Bao | 91a7aa4 | 2017-05-01 15:57:38 -0700 | [diff] [blame] | 265 | return 0; |
| 266 | } |
| 267 | |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 268 | for (uint8_t i : blockhash) { |
| 269 | if (i != 0) { |
Tao Bao | 91a7aa4 | 2017-05-01 15:57:38 -0700 | [diff] [blame] | 270 | fd->curr_block = -1; |
| 271 | return -EIO; |
| 272 | } |
| 273 | } |
| 274 | |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 275 | fd->hashes[block] = hash; |
Tao Bao | 91a7aa4 | 2017-05-01 15:57:38 -0700 | [diff] [blame] | 276 | return 0; |
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_read(void* data, fuse_data* fd, const fuse_in_header* hdr) { |
| 280 | if (hdr->nodeid != PACKAGE_FILE_ID) return -ENOENT; |
Doug Zongker | 075ad80 | 2014-06-26 15:35:51 -0700 | [diff] [blame] | 281 | |
Tao Bao | 91a7aa4 | 2017-05-01 15:57:38 -0700 | [diff] [blame] | 282 | const fuse_read_in* req = static_cast<const fuse_read_in*>(data); |
| 283 | uint64_t offset = req->offset; |
| 284 | uint32_t size = req->size; |
Doug Zongker | 075ad80 | 2014-06-26 15:35:51 -0700 | [diff] [blame] | 285 | |
Tao Bao | 91a7aa4 | 2017-05-01 15:57:38 -0700 | [diff] [blame] | 286 | // The docs on the fuse kernel interface are vague about what to do when a read request extends |
| 287 | // past the end of the file. We can return a short read -- the return structure does include a |
| 288 | // length field -- but in testing that caused the program using the file to segfault. (I |
| 289 | // speculate that this is due to the reading program accessing it via mmap; maybe mmap dislikes |
| 290 | // when you return something short of a whole page?) To fix this we zero-pad reads that extend |
| 291 | // past the end of the file so we're always returning exactly as many bytes as were requested. |
| 292 | // (Users of the mapped file have to know its real length anyway.) |
Doug Zongker | 075ad80 | 2014-06-26 15:35:51 -0700 | [diff] [blame] | 293 | |
Tao Bao | 91a7aa4 | 2017-05-01 15:57:38 -0700 | [diff] [blame] | 294 | fuse_out_header outhdr; |
| 295 | outhdr.len = sizeof(outhdr) + size; |
| 296 | outhdr.error = 0; |
| 297 | outhdr.unique = hdr->unique; |
Doug Zongker | 075ad80 | 2014-06-26 15:35:51 -0700 | [diff] [blame] | 298 | |
Tao Bao | 91a7aa4 | 2017-05-01 15:57:38 -0700 | [diff] [blame] | 299 | struct iovec vec[3]; |
| 300 | vec[0].iov_base = &outhdr; |
| 301 | vec[0].iov_len = sizeof(outhdr); |
Doug Zongker | 075ad80 | 2014-06-26 15:35:51 -0700 | [diff] [blame] | 302 | |
Tao Bao | 91a7aa4 | 2017-05-01 15:57:38 -0700 | [diff] [blame] | 303 | uint32_t block = offset / fd->block_size; |
| 304 | int result = fetch_block(fd, block); |
| 305 | if (result != 0) return result; |
| 306 | |
| 307 | // Two cases: |
| 308 | // |
| 309 | // - the read request is entirely within this block. In this case we can reply immediately. |
| 310 | // |
| 311 | // - the read request goes over into the next block. Note that since we mount the filesystem |
| 312 | // with max_read=block_size, a read can never span more than two blocks. In this case we copy |
| 313 | // the block to extra_block and issue a fetch for the following block. |
| 314 | |
| 315 | uint32_t block_offset = offset - (block * fd->block_size); |
| 316 | |
| 317 | int vec_used; |
| 318 | if (size + block_offset <= fd->block_size) { |
| 319 | // First case: the read fits entirely in the first block. |
| 320 | |
| 321 | vec[1].iov_base = fd->block_data + block_offset; |
| 322 | vec[1].iov_len = size; |
| 323 | vec_used = 2; |
| 324 | } else { |
| 325 | // Second case: the read spills over into the next block. |
| 326 | |
| 327 | memcpy(fd->extra_block, fd->block_data + block_offset, fd->block_size - block_offset); |
| 328 | vec[1].iov_base = fd->extra_block; |
| 329 | vec[1].iov_len = fd->block_size - block_offset; |
| 330 | |
| 331 | result = fetch_block(fd, block + 1); |
Doug Zongker | 075ad80 | 2014-06-26 15:35:51 -0700 | [diff] [blame] | 332 | if (result != 0) return result; |
Tao Bao | 91a7aa4 | 2017-05-01 15:57:38 -0700 | [diff] [blame] | 333 | vec[2].iov_base = fd->block_data; |
| 334 | vec[2].iov_len = size - vec[1].iov_len; |
| 335 | vec_used = 3; |
| 336 | } |
Doug Zongker | 075ad80 | 2014-06-26 15:35:51 -0700 | [diff] [blame] | 337 | |
Tao Bao | 91a7aa4 | 2017-05-01 15:57:38 -0700 | [diff] [blame] | 338 | if (writev(fd->ffd, vec, vec_used) == -1) { |
| 339 | printf("*** READ REPLY FAILED: %s ***\n", strerror(errno)); |
| 340 | } |
| 341 | return NO_STATUS; |
Doug Zongker | 075ad80 | 2014-06-26 15:35:51 -0700 | [diff] [blame] | 342 | } |
| 343 | |
xunchang | 5e6832a | 2019-03-15 16:04:32 -0700 | [diff] [blame] | 344 | 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] | 345 | // If something's already mounted on our mountpoint, try to remove it. (Mostly in case of a |
| 346 | // previous abnormal exit.) |
Tao Bao | 91a7aa4 | 2017-05-01 15:57:38 -0700 | [diff] [blame] | 347 | umount2(mount_point, MNT_FORCE); |
Doug Zongker | 075ad80 | 2014-06-26 15:35:51 -0700 | [diff] [blame] | 348 | |
xunchang | 5e6832a | 2019-03-15 16:04:32 -0700 | [diff] [blame] | 349 | uint64_t file_size = provider->file_size(); |
| 350 | uint32_t block_size = provider->fuse_block_size(); |
| 351 | |
Tao Bao | ed13819 | 2017-05-01 22:30:39 -0700 | [diff] [blame] | 352 | // fs/fuse/inode.c in kernel code uses the greater of 4096 and the passed-in max_read. |
| 353 | if (block_size < 4096) { |
| 354 | fprintf(stderr, "block size (%u) is too small\n", block_size); |
| 355 | return -1; |
| 356 | } |
| 357 | if (block_size > (1 << 22)) { // 4 MiB |
| 358 | fprintf(stderr, "block size (%u) is too large\n", block_size); |
| 359 | return -1; |
| 360 | } |
Doug Zongker | 075ad80 | 2014-06-26 15:35:51 -0700 | [diff] [blame] | 361 | |
Tao Bao | 91a7aa4 | 2017-05-01 15:57:38 -0700 | [diff] [blame] | 362 | fuse_data fd = {}; |
xunchang | 5e6832a | 2019-03-15 16:04:32 -0700 | [diff] [blame] | 363 | fd.provider = provider.get(); |
Tao Bao | ed13819 | 2017-05-01 22:30:39 -0700 | [diff] [blame] | 364 | fd.file_size = file_size; |
| 365 | fd.block_size = block_size; |
| 366 | 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] | 367 | |
Tao Bao | ed13819 | 2017-05-01 22:30:39 -0700 | [diff] [blame] | 368 | int result; |
| 369 | if (fd.file_blocks > (1 << 18)) { |
| 370 | fprintf(stderr, "file has too many blocks (%u)\n", fd.file_blocks); |
| 371 | result = -1; |
| 372 | goto done; |
| 373 | } |
Doug Zongker | 075ad80 | 2014-06-26 15:35:51 -0700 | [diff] [blame] | 374 | |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 375 | // All hashes will be zero-initialized. |
| 376 | fd.hashes.resize(fd.file_blocks); |
Tao Bao | ed13819 | 2017-05-01 22:30:39 -0700 | [diff] [blame] | 377 | fd.uid = getuid(); |
| 378 | fd.gid = getgid(); |
Doug Zongker | 075ad80 | 2014-06-26 15:35:51 -0700 | [diff] [blame] | 379 | |
Tao Bao | ed13819 | 2017-05-01 22:30:39 -0700 | [diff] [blame] | 380 | fd.curr_block = -1; |
Tao Bao | 91a7aa4 | 2017-05-01 15:57:38 -0700 | [diff] [blame] | 381 | fd.block_data = static_cast<uint8_t*>(malloc(block_size)); |
| 382 | if (fd.block_data == nullptr) { |
Tao Bao | ed13819 | 2017-05-01 22:30:39 -0700 | [diff] [blame] | 383 | fprintf(stderr, "failed to allocate %d bites for block_data\n", block_size); |
| 384 | result = -1; |
| 385 | goto done; |
| 386 | } |
Tao Bao | 91a7aa4 | 2017-05-01 15:57:38 -0700 | [diff] [blame] | 387 | fd.extra_block = static_cast<uint8_t*>(malloc(block_size)); |
| 388 | if (fd.extra_block == nullptr) { |
Tao Bao | ed13819 | 2017-05-01 22:30:39 -0700 | [diff] [blame] | 389 | fprintf(stderr, "failed to allocate %d bites for extra_block\n", block_size); |
| 390 | result = -1; |
| 391 | goto done; |
| 392 | } |
Doug Zongker | 075ad80 | 2014-06-26 15:35:51 -0700 | [diff] [blame] | 393 | |
Tao Bao | 91a7aa4 | 2017-05-01 15:57:38 -0700 | [diff] [blame] | 394 | fd.ffd.reset(open("/dev/fuse", O_RDWR)); |
Bernie Innocenti | 8bd6f45 | 2019-03-28 15:48:08 +0900 | [diff] [blame] | 395 | if (fd.ffd == -1) { |
Tao Bao | ed13819 | 2017-05-01 22:30:39 -0700 | [diff] [blame] | 396 | perror("open /dev/fuse"); |
| 397 | result = -1; |
| 398 | goto done; |
| 399 | } |
Doug Zongker | 075ad80 | 2014-06-26 15:35:51 -0700 | [diff] [blame] | 400 | |
Tao Bao | ed13819 | 2017-05-01 22:30:39 -0700 | [diff] [blame] | 401 | { |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 402 | std::string opts = android::base::StringPrintf( |
| 403 | "fd=%d,user_id=%d,group_id=%d,max_read=%u,allow_other,rootmode=040000", fd.ffd.get(), |
| 404 | fd.uid, fd.gid, block_size); |
Doug Zongker | 075ad80 | 2014-06-26 15:35:51 -0700 | [diff] [blame] | 405 | |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 406 | result = mount("/dev/fuse", mount_point, "fuse", MS_NOSUID | MS_NODEV | MS_RDONLY | MS_NOEXEC, |
| 407 | opts.c_str()); |
| 408 | if (result == -1) { |
Tao Bao | ed13819 | 2017-05-01 22:30:39 -0700 | [diff] [blame] | 409 | perror("mount"); |
| 410 | goto done; |
Doug Zongker | 075ad80 | 2014-06-26 15:35:51 -0700 | [diff] [blame] | 411 | } |
Tao Bao | ed13819 | 2017-05-01 22:30:39 -0700 | [diff] [blame] | 412 | } |
Doug Zongker | 075ad80 | 2014-06-26 15:35:51 -0700 | [diff] [blame] | 413 | |
Tao Bao | 91a7aa4 | 2017-05-01 15:57:38 -0700 | [diff] [blame] | 414 | uint8_t request_buffer[sizeof(fuse_in_header) + PATH_MAX * 8]; |
Tao Bao | ed13819 | 2017-05-01 22:30:39 -0700 | [diff] [blame] | 415 | for (;;) { |
| 416 | ssize_t len = TEMP_FAILURE_RETRY(read(fd.ffd, request_buffer, sizeof(request_buffer))); |
| 417 | if (len == -1) { |
| 418 | perror("read request"); |
| 419 | if (errno == ENODEV) { |
| 420 | result = -1; |
| 421 | break; |
| 422 | } |
| 423 | continue; |
Doug Zongker | 075ad80 | 2014-06-26 15:35:51 -0700 | [diff] [blame] | 424 | } |
| 425 | |
Tao Bao | 91a7aa4 | 2017-05-01 15:57:38 -0700 | [diff] [blame] | 426 | if (static_cast<size_t>(len) < sizeof(fuse_in_header)) { |
Tao Bao | ed13819 | 2017-05-01 22:30:39 -0700 | [diff] [blame] | 427 | fprintf(stderr, "request too short: len=%zd\n", len); |
| 428 | continue; |
Doug Zongker | 075ad80 | 2014-06-26 15:35:51 -0700 | [diff] [blame] | 429 | } |
| 430 | |
Tao Bao | 91a7aa4 | 2017-05-01 15:57:38 -0700 | [diff] [blame] | 431 | fuse_in_header* hdr = reinterpret_cast<fuse_in_header*>(request_buffer); |
| 432 | void* data = request_buffer + sizeof(fuse_in_header); |
Doug Zongker | 075ad80 | 2014-06-26 15:35:51 -0700 | [diff] [blame] | 433 | |
Tao Bao | ed13819 | 2017-05-01 22:30:39 -0700 | [diff] [blame] | 434 | result = -ENOSYS; |
| 435 | |
| 436 | switch (hdr->opcode) { |
| 437 | case FUSE_INIT: |
| 438 | result = handle_init(data, &fd, hdr); |
| 439 | break; |
| 440 | |
| 441 | case FUSE_LOOKUP: |
| 442 | result = handle_lookup(data, &fd, hdr); |
| 443 | break; |
| 444 | |
| 445 | case FUSE_GETATTR: |
| 446 | result = handle_getattr(data, &fd, hdr); |
| 447 | break; |
| 448 | |
| 449 | case FUSE_OPEN: |
| 450 | result = handle_open(data, &fd, hdr); |
| 451 | break; |
| 452 | |
| 453 | case FUSE_READ: |
| 454 | result = handle_read(data, &fd, hdr); |
| 455 | break; |
| 456 | |
| 457 | case FUSE_FLUSH: |
| 458 | result = handle_flush(data, &fd, hdr); |
| 459 | break; |
| 460 | |
| 461 | case FUSE_RELEASE: |
| 462 | result = handle_release(data, &fd, hdr); |
| 463 | break; |
| 464 | |
| 465 | default: |
| 466 | fprintf(stderr, "unknown fuse request opcode %d\n", hdr->opcode); |
| 467 | break; |
| 468 | } |
| 469 | |
| 470 | if (result == NO_STATUS_EXIT) { |
| 471 | result = 0; |
| 472 | break; |
| 473 | } |
| 474 | |
| 475 | if (result != NO_STATUS) { |
Tao Bao | 91a7aa4 | 2017-05-01 15:57:38 -0700 | [diff] [blame] | 476 | fuse_out_header outhdr; |
Tao Bao | ed13819 | 2017-05-01 22:30:39 -0700 | [diff] [blame] | 477 | outhdr.len = sizeof(outhdr); |
| 478 | outhdr.error = result; |
| 479 | outhdr.unique = hdr->unique; |
| 480 | TEMP_FAILURE_RETRY(write(fd.ffd, &outhdr, sizeof(outhdr))); |
| 481 | } |
| 482 | } |
| 483 | |
| 484 | done: |
xunchang | 5e6832a | 2019-03-15 16:04:32 -0700 | [diff] [blame] | 485 | provider->Close(); |
Tao Bao | ed13819 | 2017-05-01 22:30:39 -0700 | [diff] [blame] | 486 | |
Tao Bao | 91a7aa4 | 2017-05-01 15:57:38 -0700 | [diff] [blame] | 487 | if (umount2(mount_point, MNT_DETACH) == -1) { |
| 488 | fprintf(stderr, "fuse_sideload umount failed: %s\n", strerror(errno)); |
Tao Bao | ed13819 | 2017-05-01 22:30:39 -0700 | [diff] [blame] | 489 | } |
| 490 | |
Tao Bao | ed13819 | 2017-05-01 22:30:39 -0700 | [diff] [blame] | 491 | free(fd.block_data); |
| 492 | free(fd.extra_block); |
| 493 | |
| 494 | return result; |
Doug Zongker | 075ad80 | 2014-06-26 15:35:51 -0700 | [diff] [blame] | 495 | } |