blob: f57d479b0602b4013827484fb28b690337b106fe [file] [log] [blame]
Ethan Yonker58f21322018-08-24 11:17:36 -05001/*
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>
49#include "fuse.h"
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>
56#include <sys/param.h>
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
64#ifdef USE_MINCRYPT
65#include "mincrypt/sha256.h"
66#define SHA256_DIGEST_LENGTH SHA256_DIGEST_SIZE
67#else
68#include <openssl/sha.h>
69#endif
70
71#include "fuse_sideload.h"
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
79#ifndef MIN
80#define MIN(a, b) ((a) < (b) ? (a) : (b))
81#endif
82
83struct fuse_data {
84 int ffd; // file descriptor for the fuse socket
85
86 struct provider_vtab* vtab;
87 void* cookie;
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
107static 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);
119 vec[1].iov_base = /* const_cast */(void*)(data);
120 vec[1].iov_len = len;
121
122 res = writev(fd->ffd, vec, 2);
123 if (res < 0) {
124 printf("*** REPLY FAILED *** %s\n", strerror(errno));
125 }
126}
127
128static int handle_init(void* data, struct fuse_data* fd, const struct fuse_in_header* hdr) {
129 const struct fuse_init_in* req = reinterpret_cast<const struct fuse_init_in*>(data);
130 struct fuse_init_out out;
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
157
158 out.major = FUSE_KERNEL_VERSION;
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;
164 fuse_reply(fd, hdr->unique, &out, fuse_struct_size);
165
166 return NO_STATUS;
167}
168
169static 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
183static int handle_getattr(void* /* data */, struct fuse_data* fd, const struct fuse_in_header* hdr) {
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
202static 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
209 if (strncmp(FUSE_SIDELOAD_HOST_FILENAME, reinterpret_cast<const char*>(data),
210 sizeof(FUSE_SIDELOAD_HOST_FILENAME)) == 0) {
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);
214 } else if (strncmp(FUSE_SIDELOAD_HOST_EXIT_FLAG, reinterpret_cast<const char*>(data),
215 sizeof(FUSE_SIDELOAD_HOST_EXIT_FLAG)) == 0) {
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
227static int handle_open(void* /* data */, struct fuse_data* fd, const struct fuse_in_header* hdr) {
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
238static int handle_flush(void* data, struct fuse_data* fd, const struct fuse_in_header* hdr) {
239 return 0;
240}
241
242static int handle_release(void* data, struct fuse_data* fd, const struct fuse_in_header* hdr) {
243 return 0;
244}
245
246// Fetch a block from the host into fd->curr_block and fd->block_data.
247// Returns 0 on successful fetch, negative otherwise.
248static int fetch_block(struct fuse_data* fd, uint32_t block) {
249 if (block == fd->curr_block) {
250 return 0;
251 }
252
253 if (block >= fd->file_blocks) {
254 memset(fd->block_data, 0, fd->block_size);
255 fd->curr_block = block;
256 return 0;
257 }
258
259 size_t fetch_size = fd->block_size;
260 if (block * fd->block_size + fetch_size > fd->file_size) {
261 // If we're reading the last (partial) block of the file,
262 // expect a shorter response from the host, and pad the rest
263 // of the block with zeroes.
264 fetch_size = fd->file_size - (block * fd->block_size);
265 memset(fd->block_data + fetch_size, 0, fd->block_size - fetch_size);
266 }
267
268 int result = fd->vtab->read_block(fd->cookie, block, fd->block_data, fetch_size);
269 if (result < 0) return result;
270
271 fd->curr_block = block;
272
273 // Verify the hash of the block we just got from the host.
274 //
275 // - If the hash of the just-received data matches the stored hash
276 // for the block, accept it.
277 // - If the stored hash is all zeroes, store the new hash and
278 // accept the block (this is the first time we've read this
279 // block).
280 // - Otherwise, return -EINVAL for the read.
281
282 uint8_t hash[SHA256_DIGEST_LENGTH];
283#ifdef USE_MINCRYPT
284 SHA256_hash(fd->block_data, fd->block_size, hash);
285#else
286 SHA256(fd->block_data, fd->block_size, hash);
287#endif
288 uint8_t* blockhash = fd->hashes + block * SHA256_DIGEST_LENGTH;
289 if (memcmp(hash, blockhash, SHA256_DIGEST_LENGTH) == 0) {
290 return 0;
291 }
292
293 int i;
294 for (i = 0; i < SHA256_DIGEST_LENGTH; ++i) {
295 if (blockhash[i] != 0) {
296 fd->curr_block = -1;
297 return -EIO;
298 }
299 }
300
301 memcpy(blockhash, hash, SHA256_DIGEST_LENGTH);
302 return 0;
303}
304
305static int handle_read(void* data, struct fuse_data* fd, const struct fuse_in_header* hdr) {
306 const struct fuse_read_in* req = reinterpret_cast<const struct fuse_read_in*>(data);
307 struct fuse_out_header outhdr;
308 struct iovec vec[3];
309 int vec_used;
310 int result;
311
312 if (hdr->nodeid != PACKAGE_FILE_ID) return -ENOENT;
313
314 uint64_t offset = req->offset;
315 uint32_t size = req->size;
316
317 // The docs on the fuse kernel interface are vague about what to
318 // do when a read request extends past the end of the file. We
319 // can return a short read -- the return structure does include a
320 // length field -- but in testing that caused the program using
321 // the file to segfault. (I speculate that this is due to the
322 // reading program accessing it via mmap; maybe mmap dislikes when
323 // you return something short of a whole page?) To fix this we
324 // zero-pad reads that extend past the end of the file so we're
325 // always returning exactly as many bytes as were requested.
326 // (Users of the mapped file have to know its real length anyway.)
327
328 outhdr.len = sizeof(outhdr) + size;
329 outhdr.error = 0;
330 outhdr.unique = hdr->unique;
331 vec[0].iov_base = &outhdr;
332 vec[0].iov_len = sizeof(outhdr);
333
334 uint32_t block = offset / fd->block_size;
335 result = fetch_block(fd, block);
336 if (result != 0) return result;
337
338 // Two cases:
339 //
340 // - the read request is entirely within this block. In this
341 // case we can reply immediately.
342 //
343 // - the read request goes over into the next block. Note that
344 // since we mount the filesystem with max_read=block_size, a
345 // read can never span more than two blocks. In this case we
346 // copy the block to extra_block and issue a fetch for the
347 // following block.
348
349 uint32_t block_offset = offset - (block * fd->block_size);
350
351 if (size + block_offset <= fd->block_size) {
352 // First case: the read fits entirely in the first block.
353
354 vec[1].iov_base = fd->block_data + block_offset;
355 vec[1].iov_len = size;
356 vec_used = 2;
357 } else {
358 // Second case: the read spills over into the next block.
359
360 memcpy(fd->extra_block, fd->block_data + block_offset,
361 fd->block_size - block_offset);
362 vec[1].iov_base = fd->extra_block;
363 vec[1].iov_len = fd->block_size - block_offset;
364
365 result = fetch_block(fd, block+1);
366 if (result != 0) return result;
367 vec[2].iov_base = fd->block_data;
368 vec[2].iov_len = size - vec[1].iov_len;
369 vec_used = 3;
370 }
371
372 if (writev(fd->ffd, vec, vec_used) < 0) {
373 printf("*** READ REPLY FAILED: %s ***\n", strerror(errno));
374 }
375 return NO_STATUS;
376}
377
378int run_fuse_sideload(struct provider_vtab* vtab, void* cookie,
379 uint64_t file_size, uint32_t block_size)
380{
381 int result;
382
383 // If something's already mounted on our mountpoint, try to remove
384 // it. (Mostly in case of a previous abnormal exit.)
385 umount2(FUSE_SIDELOAD_HOST_MOUNTPOINT, MNT_FORCE);
386
387 if (block_size < 1024) {
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 }
395
396 struct fuse_data fd;
397 memset(&fd, 0, sizeof(fd));
398 fd.vtab = vtab;
399 fd.cookie = cookie;
400 fd.file_size = file_size;
401 fd.block_size = block_size;
402 fd.file_blocks = (file_size == 0) ? 0 : (((file_size-1) / block_size) + 1);
403
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 }
409
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 }
417
418 fd.uid = getuid();
419 fd.gid = getgid();
420
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 }
434
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 }
441
442 char opts[256];
443 snprintf(opts, sizeof(opts),
444 ("fd=%d,user_id=%d,group_id=%d,max_read=%u,"
445 "allow_other,rootmode=040000"),
446 fd.ffd, fd.uid, fd.gid, block_size);
447
448 result = mount("/dev/fuse", FUSE_SIDELOAD_HOST_MOUNTPOINT,
449 "fuse", MS_NOSUID | MS_NODEV | MS_RDONLY | MS_NOEXEC, opts);
450 if (result < 0) {
451 perror("mount");
452 goto done;
453 }
454 uint8_t request_buffer[sizeof(struct fuse_in_header) + PATH_MAX*8];
455 for (;;) {
456 ssize_t len = TEMP_FAILURE_RETRY(read(fd.ffd, request_buffer, sizeof(request_buffer)));
457 if (len == -1) {
458 perror("read request");
459 if (errno == ENODEV) {
460 result = -1;
461 break;
462 }
463 continue;
464 }
465
466 if ((size_t)len < sizeof(struct fuse_in_header)) {
467 fprintf(stderr, "request too short: len=%zu\n", (size_t)len);
468 continue;
469 }
470
471 struct fuse_in_header* hdr = (struct fuse_in_header*) request_buffer;
472 void* data = request_buffer + sizeof(struct fuse_in_header);
473
474 result = -ENOSYS;
475
476 switch (hdr->opcode) {
477 case FUSE_INIT:
478 result = handle_init(data, &fd, hdr);
479 break;
480
481 case FUSE_LOOKUP:
482 result = handle_lookup(data, &fd, hdr);
483 break;
484
485 case FUSE_GETATTR:
486 result = handle_getattr(data, &fd, hdr);
487 break;
488
489 case FUSE_OPEN:
490 result = handle_open(data, &fd, hdr);
491 break;
492
493 case FUSE_READ:
494 result = handle_read(data, &fd, hdr);
495 break;
496
497 case FUSE_FLUSH:
498 result = handle_flush(data, &fd, hdr);
499 break;
500
501 case FUSE_RELEASE:
502 result = handle_release(data, &fd, hdr);
503 break;
504
505 default:
506 fprintf(stderr, "unknown fuse request opcode %d\n", hdr->opcode);
507 break;
508 }
509
510 if (result == NO_STATUS_EXIT) {
511 result = 0;
512 break;
513 }
514
515 if (result != NO_STATUS) {
516 struct fuse_out_header outhdr;
517 outhdr.len = sizeof(outhdr);
518 outhdr.error = result;
519 outhdr.unique = hdr->unique;
520 TEMP_FAILURE_RETRY(write(fd.ffd, &outhdr, sizeof(outhdr)));
521 }
522 }
523
524 done:
525 fd.vtab->close(fd.cookie);
526
527 result = umount2(FUSE_SIDELOAD_HOST_MOUNTPOINT, MNT_DETACH);
528 if (result < 0) {
529 printf("fuse_sideload umount failed: %s\n", strerror(errno));
530 }
531
532 if (fd.ffd) close(fd.ffd);
533 free(fd.hashes);
534 free(fd.block_data);
535 free(fd.extra_block);
536
537 return result;
538}
539
540extern "C" int run_old_fuse_sideload(struct provider_vtab* vtab, void* cookie,
541 uint64_t file_size, uint32_t block_size)
542{
543 return run_fuse_sideload(vtab, cookie, file_size, block_size);
544}