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