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