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