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