blob: ac239eeb41178a184bda4d9b5bf7ce0961c3358d [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
Tao Bao91a7aa42017-05-01 15:57:38 -070044#include "fuse_sideload.h"
45
Doug Zongker075ad802014-06-26 15:35:51 -070046#include <errno.h>
47#include <fcntl.h>
Tao Bao91a7aa42017-05-01 15:57:38 -070048#include <limits.h> // PATH_MAX
Doug Zongker075ad802014-06-26 15:35:51 -070049#include <linux/fuse.h>
Tao Bao91a7aa42017-05-01 15:57:38 -070050#include <stdint.h>
Doug Zongker075ad802014-06-26 15:35:51 -070051#include <stdio.h>
52#include <stdlib.h>
53#include <string.h>
Doug Zongker075ad802014-06-26 15:35:51 -070054#include <sys/mount.h>
Tao Bao91a7aa42017-05-01 15:57:38 -070055#include <sys/param.h> // MIN
Doug Zongker075ad802014-06-26 15:35:51 -070056#include <sys/stat.h>
Doug Zongker075ad802014-06-26 15:35:51 -070057#include <sys/uio.h>
58#include <unistd.h>
59
Tao Bao91a7aa42017-05-01 15:57:38 -070060#include <array>
Tao Baoed138192017-05-01 22:30:39 -070061#include <string>
Tao Bao91a7aa42017-05-01 15:57:38 -070062#include <vector>
Tao Baoed138192017-05-01 22:30:39 -070063
64#include <android-base/stringprintf.h>
Tao Bao91a7aa42017-05-01 15:57:38 -070065#include <android-base/unique_fd.h>
Mattias Nissler452df6d2016-04-04 16:17:01 +020066#include <openssl/sha.h>
67
Tao Bao91a7aa42017-05-01 15:57:38 -070068static constexpr uint64_t PACKAGE_FILE_ID = FUSE_ROOT_ID + 1;
69static constexpr uint64_t EXIT_FLAG_ID = FUSE_ROOT_ID + 2;
Doug Zongker075ad802014-06-26 15:35:51 -070070
Tao Bao91a7aa42017-05-01 15:57:38 -070071static constexpr int NO_STATUS = 1;
72static constexpr int NO_STATUS_EXIT = 2;
Doug Zongker075ad802014-06-26 15:35:51 -070073
Tao Bao91a7aa42017-05-01 15:57:38 -070074using SHA256Digest = std::array<uint8_t, SHA256_DIGEST_LENGTH>;
Doug Zongker075ad802014-06-26 15:35:51 -070075
Tom Marshallc07fbfc2020-03-06 18:50:31 +010076#define INSTALL_REQUIRED_MEMORY (400 * 1024 * 1024)
Tom Marshall0e5905c2014-12-16 14:06:02 -080077
Doug Zongker075ad802014-06-26 15:35:51 -070078struct fuse_data {
Tao Bao91a7aa42017-05-01 15:57:38 -070079 android::base::unique_fd ffd; // file descriptor for the fuse socket
Doug Zongker18a78e02014-07-10 07:31:46 -070080
xunchang5e6832a2019-03-15 16:04:32 -070081 FuseDataProvider* provider; // Provider of the source data.
Doug Zongker075ad802014-06-26 15:35:51 -070082
Tao Bao91a7aa42017-05-01 15:57:38 -070083 uint64_t file_size; // bytes
Doug Zongker075ad802014-06-26 15:35:51 -070084
Tao Bao91a7aa42017-05-01 15:57:38 -070085 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
Doug Zongker075ad802014-06-26 15:35:51 -070087
Tao Bao91a7aa42017-05-01 15:57:38 -070088 uid_t uid;
89 gid_t gid;
Doug Zongker075ad802014-06-26 15:35:51 -070090
Tom Marshall0e5905c2014-12-16 14:06:02 -080091 uint32_t curr_block; // cache the block most recently used
Tao Bao91a7aa42017-05-01 15:57:38 -070092 uint8_t* block_data;
Doug Zongker075ad802014-06-26 15:35:51 -070093
Tao Bao91a7aa42017-05-01 15:57:38 -070094 uint8_t* extra_block; // another block of storage for reads that span two blocks
Doug Zongker075ad802014-06-26 15:35:51 -070095
Tao Bao91a7aa42017-05-01 15:57:38 -070096 std::vector<SHA256Digest>
97 hashes; // SHA-256 hash of each block (all zeros if block hasn't been read yet)
Tom Marshall0e5905c2014-12-16 14:06:02 -080098
99 // Block cache
100 uint32_t block_cache_max_size; // Max allowed block cache size
101 uint32_t block_cache_size; // Current block cache size
102 uint8_t** block_cache; // Block cache data
Doug Zongker075ad802014-06-26 15:35:51 -0700103};
104
Tom Marshall0e5905c2014-12-16 14:06:02 -0800105static uint64_t free_memory() {
106 uint64_t mem = 0;
107 FILE* fp = fopen("/proc/meminfo", "r");
108 if (fp) {
109 char buf[256];
110 char* linebuf = buf;
111 size_t buflen = sizeof(buf);
112 while (getline(&linebuf, &buflen, fp) > 0) {
113 char* key = buf;
114 char* val = strchr(buf, ':');
115 *val = '\0';
116 ++val;
117 if (strcmp(key, "MemFree") == 0) {
118 mem += strtoul(val, nullptr, 0) * 1024;
119 }
120 if (strcmp(key, "Buffers") == 0) {
121 mem += strtoul(val, nullptr, 0) * 1024;
122 }
123 if (strcmp(key, "Cached") == 0) {
124 mem += strtoul(val, nullptr, 0) * 1024;
125 }
126 }
127 fclose(fp);
128 }
129 return mem;
130}
131
132static int block_cache_fetch(struct fuse_data* fd, uint32_t block) {
133 if (fd->block_cache == nullptr) {
134 return -1;
135 }
136 if (fd->block_cache[block] == nullptr) {
137 return -1;
138 }
139 memcpy(fd->block_data, fd->block_cache[block], fd->block_size);
140 return 0;
141}
142
143static void block_cache_enter(struct fuse_data* fd, uint32_t block) {
144 if (!fd->block_cache) return;
145 if (fd->block_cache_size == fd->block_cache_max_size) {
146 // Evict a block from the cache. Since the file is typically read
147 // sequentially, start looking from the block behind the current
148 // block and proceed backward.
149 int n;
150 for (n = fd->curr_block - 1; n != (int)fd->curr_block; --n) {
151 if (n < 0) {
152 n = fd->file_blocks - 1;
153 }
154 if (fd->block_cache[n]) {
155 free(fd->block_cache[n]);
156 fd->block_cache[n] = nullptr;
157 fd->block_cache_size--;
158 break;
159 }
160 }
161 }
162
163 fd->block_cache[block] = (uint8_t*)malloc(fd->block_size);
164 memcpy(fd->block_cache[block], fd->block_data, fd->block_size);
165
166 fd->block_cache_size++;
167}
168
Tao Bao91a7aa42017-05-01 15:57:38 -0700169static void fuse_reply(const fuse_data* fd, uint64_t unique, const void* data, size_t len) {
170 fuse_out_header hdr;
171 hdr.len = len + sizeof(hdr);
172 hdr.error = 0;
173 hdr.unique = unique;
Doug Zongker075ad802014-06-26 15:35:51 -0700174
Tao Bao91a7aa42017-05-01 15:57:38 -0700175 struct iovec vec[2];
176 vec[0].iov_base = &hdr;
177 vec[0].iov_len = sizeof(hdr);
178 vec[1].iov_base = const_cast<void*>(data);
179 vec[1].iov_len = len;
Doug Zongker075ad802014-06-26 15:35:51 -0700180
Tao Bao91a7aa42017-05-01 15:57:38 -0700181 int res = writev(fd->ffd, vec, 2);
182 if (res == -1) {
183 printf("*** REPLY FAILED *** %s\n", strerror(errno));
184 }
Doug Zongker075ad802014-06-26 15:35:51 -0700185}
186
Tao Bao91a7aa42017-05-01 15:57:38 -0700187static int handle_init(void* data, fuse_data* fd, const fuse_in_header* hdr) {
188 const fuse_init_in* req = static_cast<const fuse_init_in*>(data);
Christopher Ferris1d30c2f2014-09-16 14:53:39 -0700189
Tao Bao91a7aa42017-05-01 15:57:38 -0700190 // Kernel 2.6.16 is the first stable kernel with struct fuse_init_out defined (fuse version 7.6).
191 // The structure is the same from 7.6 through 7.22. Beginning with 7.23, the structure increased
192 // in size and added new parameters.
193 if (req->major != FUSE_KERNEL_VERSION || req->minor < 6) {
194 printf("Fuse kernel version mismatch: Kernel version %d.%d, Expected at least %d.6", req->major,
195 req->minor, FUSE_KERNEL_VERSION);
196 return -1;
197 }
Christopher Ferris1d30c2f2014-09-16 14:53:39 -0700198
Tao Bao91a7aa42017-05-01 15:57:38 -0700199 fuse_init_out out;
200 out.minor = MIN(req->minor, FUSE_KERNEL_MINOR_VERSION);
201 size_t fuse_struct_size = sizeof(out);
Christopher Ferris1d30c2f2014-09-16 14:53:39 -0700202#if defined(FUSE_COMPAT_22_INIT_OUT_SIZE)
Tao Bao91a7aa42017-05-01 15:57:38 -0700203 /* FUSE_KERNEL_VERSION >= 23. */
Christopher Ferris1d30c2f2014-09-16 14:53:39 -0700204
Tao Bao91a7aa42017-05-01 15:57:38 -0700205 // If the kernel only works on minor revs older than or equal to 22, then use the older structure
206 // size since this code only uses the 7.22 version of the structure.
207 if (req->minor <= 22) {
208 fuse_struct_size = FUSE_COMPAT_22_INIT_OUT_SIZE;
209 }
Christopher Ferris1d30c2f2014-09-16 14:53:39 -0700210#endif
Doug Zongker075ad802014-06-26 15:35:51 -0700211
Tao Bao91a7aa42017-05-01 15:57:38 -0700212 out.major = FUSE_KERNEL_VERSION;
213 out.max_readahead = req->max_readahead;
214 out.flags = 0;
215 out.max_background = 32;
216 out.congestion_threshold = 32;
217 out.max_write = 4096;
218 fuse_reply(fd, hdr->unique, &out, fuse_struct_size);
Doug Zongker075ad802014-06-26 15:35:51 -0700219
Tao Bao91a7aa42017-05-01 15:57:38 -0700220 return NO_STATUS;
Doug Zongker075ad802014-06-26 15:35:51 -0700221}
222
Tao Bao91a7aa42017-05-01 15:57:38 -0700223static void fill_attr(fuse_attr* attr, const fuse_data* fd, uint64_t nodeid, uint64_t size,
224 uint32_t mode) {
225 *attr = {};
226 attr->nlink = 1;
227 attr->uid = fd->uid;
228 attr->gid = fd->gid;
229 attr->blksize = 4096;
Doug Zongker075ad802014-06-26 15:35:51 -0700230
Tao Bao91a7aa42017-05-01 15:57:38 -0700231 attr->ino = nodeid;
232 attr->size = size;
233 attr->blocks = (size == 0) ? 0 : (((size - 1) / attr->blksize) + 1);
234 attr->mode = mode;
Doug Zongker075ad802014-06-26 15:35:51 -0700235}
236
Tao Bao91a7aa42017-05-01 15:57:38 -0700237static int handle_getattr(void* /* data */, const fuse_data* fd, const fuse_in_header* hdr) {
238 fuse_attr_out out = {};
239 out.attr_valid = 10;
Doug Zongker075ad802014-06-26 15:35:51 -0700240
Tao Bao91a7aa42017-05-01 15:57:38 -0700241 if (hdr->nodeid == FUSE_ROOT_ID) {
242 fill_attr(&(out.attr), fd, hdr->nodeid, 4096, S_IFDIR | 0555);
243 } else if (hdr->nodeid == PACKAGE_FILE_ID) {
244 fill_attr(&(out.attr), fd, PACKAGE_FILE_ID, fd->file_size, S_IFREG | 0444);
245 } else if (hdr->nodeid == EXIT_FLAG_ID) {
246 fill_attr(&(out.attr), fd, EXIT_FLAG_ID, 0, S_IFREG | 0);
247 } else {
248 return -ENOENT;
249 }
Doug Zongker075ad802014-06-26 15:35:51 -0700250
Tao Bao91a7aa42017-05-01 15:57:38 -0700251 fuse_reply(fd, hdr->unique, &out, sizeof(out));
252 return (hdr->nodeid == EXIT_FLAG_ID) ? NO_STATUS_EXIT : NO_STATUS;
Doug Zongker075ad802014-06-26 15:35:51 -0700253}
254
Tao Bao91a7aa42017-05-01 15:57:38 -0700255static int handle_lookup(void* data, const fuse_data* fd, const fuse_in_header* hdr) {
256 if (data == nullptr) return -ENOENT;
Doug Zongker075ad802014-06-26 15:35:51 -0700257
Tao Bao91a7aa42017-05-01 15:57:38 -0700258 fuse_entry_out out = {};
259 out.entry_valid = 10;
260 out.attr_valid = 10;
Doug Zongker075ad802014-06-26 15:35:51 -0700261
Tao Bao91a7aa42017-05-01 15:57:38 -0700262 std::string filename(static_cast<const char*>(data));
263 if (filename == FUSE_SIDELOAD_HOST_FILENAME) {
264 out.nodeid = PACKAGE_FILE_ID;
265 out.generation = PACKAGE_FILE_ID;
266 fill_attr(&(out.attr), fd, PACKAGE_FILE_ID, fd->file_size, S_IFREG | 0444);
267 } else if (filename == FUSE_SIDELOAD_HOST_EXIT_FLAG) {
268 out.nodeid = EXIT_FLAG_ID;
269 out.generation = EXIT_FLAG_ID;
270 fill_attr(&(out.attr), fd, EXIT_FLAG_ID, 0, S_IFREG | 0);
271 } else {
272 return -ENOENT;
273 }
274
275 fuse_reply(fd, hdr->unique, &out, sizeof(out));
276 return (out.nodeid == EXIT_FLAG_ID) ? NO_STATUS_EXIT : NO_STATUS;
Doug Zongker075ad802014-06-26 15:35:51 -0700277}
278
Tao Bao91a7aa42017-05-01 15:57:38 -0700279static int handle_open(void* /* data */, const fuse_data* fd, const fuse_in_header* hdr) {
280 if (hdr->nodeid == EXIT_FLAG_ID) return -EPERM;
281 if (hdr->nodeid != PACKAGE_FILE_ID) return -ENOENT;
Doug Zongker075ad802014-06-26 15:35:51 -0700282
Tao Bao91a7aa42017-05-01 15:57:38 -0700283 fuse_open_out out = {};
284 out.fh = 10; // an arbitrary number; we always use the same handle
285 fuse_reply(fd, hdr->unique, &out, sizeof(out));
286 return NO_STATUS;
Doug Zongker075ad802014-06-26 15:35:51 -0700287}
288
Tao Bao91a7aa42017-05-01 15:57:38 -0700289static int handle_flush(void* /* data */, fuse_data* /* fd */, const fuse_in_header* /* hdr */) {
290 return 0;
Doug Zongker075ad802014-06-26 15:35:51 -0700291}
292
Tao Bao91a7aa42017-05-01 15:57:38 -0700293static int handle_release(void* /* data */, fuse_data* /* fd */, const fuse_in_header* /* hdr */) {
294 return 0;
Doug Zongker075ad802014-06-26 15:35:51 -0700295}
296
297// Fetch a block from the host into fd->curr_block and fd->block_data.
298// Returns 0 on successful fetch, negative otherwise.
Tao Bao91a7aa42017-05-01 15:57:38 -0700299static int fetch_block(fuse_data* fd, uint32_t block) {
300 if (block == fd->curr_block) {
Doug Zongker075ad802014-06-26 15:35:51 -0700301 return 0;
Tao Bao91a7aa42017-05-01 15:57:38 -0700302 }
303
304 if (block >= fd->file_blocks) {
305 memset(fd->block_data, 0, fd->block_size);
306 fd->curr_block = block;
307 return 0;
308 }
309
Tom Marshall0e5905c2014-12-16 14:06:02 -0800310 if (block_cache_fetch(fd, block) == 0) {
311 fd->curr_block = block;
312 return 0;
313 }
314
xunchang5e6832a2019-03-15 16:04:32 -0700315 uint32_t fetch_size = fd->block_size;
Tao Bao91a7aa42017-05-01 15:57:38 -0700316 if (block * fd->block_size + fetch_size > fd->file_size) {
317 // If we're reading the last (partial) block of the file, expect a shorter response from the
318 // host, and pad the rest of the block with zeroes.
319 fetch_size = fd->file_size - (block * fd->block_size);
320 memset(fd->block_data + fetch_size, 0, fd->block_size - fetch_size);
321 }
322
xunchang5e6832a2019-03-15 16:04:32 -0700323 if (!fd->provider->ReadBlockAlignedData(fd->block_data, fetch_size, block)) {
xunchangea2912f2019-03-17 16:45:12 -0700324 return -EIO;
325 }
Tao Bao91a7aa42017-05-01 15:57:38 -0700326
327 fd->curr_block = block;
328
329 // Verify the hash of the block we just got from the host.
330 //
331 // - If the hash of the just-received data matches the stored hash for the block, accept it.
332 // - If the stored hash is all zeroes, store the new hash and accept the block (this is the first
333 // time we've read this block).
334 // - Otherwise, return -EINVAL for the read.
335
336 SHA256Digest hash;
337 SHA256(fd->block_data, fd->block_size, hash.data());
338
339 const SHA256Digest& blockhash = fd->hashes[block];
340 if (hash == blockhash) {
341 return 0;
342 }
343
344 for (uint8_t i : blockhash) {
345 if (i != 0) {
346 fd->curr_block = -1;
347 return -EIO;
348 }
349 }
350
351 fd->hashes[block] = hash;
Tom Marshall0e5905c2014-12-16 14:06:02 -0800352 block_cache_enter(fd, block);
Tao Bao91a7aa42017-05-01 15:57:38 -0700353 return 0;
Doug Zongker075ad802014-06-26 15:35:51 -0700354}
355
Tao Bao91a7aa42017-05-01 15:57:38 -0700356static int handle_read(void* data, fuse_data* fd, const fuse_in_header* hdr) {
357 if (hdr->nodeid != PACKAGE_FILE_ID) return -ENOENT;
Doug Zongker075ad802014-06-26 15:35:51 -0700358
Tao Bao91a7aa42017-05-01 15:57:38 -0700359 const fuse_read_in* req = static_cast<const fuse_read_in*>(data);
360 uint64_t offset = req->offset;
361 uint32_t size = req->size;
Doug Zongker075ad802014-06-26 15:35:51 -0700362
Tao Bao91a7aa42017-05-01 15:57:38 -0700363 // The docs on the fuse kernel interface are vague about what to do when a read request extends
364 // past the end of the file. We can return a short read -- the return structure does include a
365 // length field -- but in testing that caused the program using the file to segfault. (I
366 // speculate that this is due to the reading program accessing it via mmap; maybe mmap dislikes
367 // when you return something short of a whole page?) To fix this we zero-pad reads that extend
368 // past the end of the file so we're always returning exactly as many bytes as were requested.
369 // (Users of the mapped file have to know its real length anyway.)
Doug Zongker075ad802014-06-26 15:35:51 -0700370
Tao Bao91a7aa42017-05-01 15:57:38 -0700371 fuse_out_header outhdr;
372 outhdr.len = sizeof(outhdr) + size;
373 outhdr.error = 0;
374 outhdr.unique = hdr->unique;
Doug Zongker075ad802014-06-26 15:35:51 -0700375
Tao Bao91a7aa42017-05-01 15:57:38 -0700376 struct iovec vec[3];
377 vec[0].iov_base = &outhdr;
378 vec[0].iov_len = sizeof(outhdr);
Doug Zongker075ad802014-06-26 15:35:51 -0700379
Tao Bao91a7aa42017-05-01 15:57:38 -0700380 uint32_t block = offset / fd->block_size;
381 int result = fetch_block(fd, block);
382 if (result != 0) return result;
383
384 // Two cases:
385 //
386 // - the read request is entirely within this block. In this case we can reply immediately.
387 //
388 // - the read request goes over into the next block. Note that since we mount the filesystem
389 // with max_read=block_size, a read can never span more than two blocks. In this case we copy
390 // the block to extra_block and issue a fetch for the following block.
391
392 uint32_t block_offset = offset - (block * fd->block_size);
393
394 int vec_used;
395 if (size + block_offset <= fd->block_size) {
396 // First case: the read fits entirely in the first block.
397
398 vec[1].iov_base = fd->block_data + block_offset;
399 vec[1].iov_len = size;
400 vec_used = 2;
401 } else {
402 // Second case: the read spills over into the next block.
403
404 memcpy(fd->extra_block, fd->block_data + block_offset, fd->block_size - block_offset);
405 vec[1].iov_base = fd->extra_block;
406 vec[1].iov_len = fd->block_size - block_offset;
407
408 result = fetch_block(fd, block + 1);
Doug Zongker075ad802014-06-26 15:35:51 -0700409 if (result != 0) return result;
Tao Bao91a7aa42017-05-01 15:57:38 -0700410 vec[2].iov_base = fd->block_data;
411 vec[2].iov_len = size - vec[1].iov_len;
412 vec_used = 3;
413 }
Doug Zongker075ad802014-06-26 15:35:51 -0700414
Tao Bao91a7aa42017-05-01 15:57:38 -0700415 if (writev(fd->ffd, vec, vec_used) == -1) {
416 printf("*** READ REPLY FAILED: %s ***\n", strerror(errno));
417 }
418 return NO_STATUS;
Doug Zongker075ad802014-06-26 15:35:51 -0700419}
420
xunchang5e6832a2019-03-15 16:04:32 -0700421int run_fuse_sideload(std::unique_ptr<FuseDataProvider>&& provider, const char* mount_point) {
Tao Baoed138192017-05-01 22:30:39 -0700422 // If something's already mounted on our mountpoint, try to remove it. (Mostly in case of a
423 // previous abnormal exit.)
Tao Bao91a7aa42017-05-01 15:57:38 -0700424 umount2(mount_point, MNT_FORCE);
Doug Zongker075ad802014-06-26 15:35:51 -0700425
xunchang5e6832a2019-03-15 16:04:32 -0700426 uint64_t file_size = provider->file_size();
427 uint32_t block_size = provider->fuse_block_size();
428
Tao Baoed138192017-05-01 22:30:39 -0700429 // fs/fuse/inode.c in kernel code uses the greater of 4096 and the passed-in max_read.
430 if (block_size < 4096) {
431 fprintf(stderr, "block size (%u) is too small\n", block_size);
432 return -1;
433 }
434 if (block_size > (1 << 22)) { // 4 MiB
435 fprintf(stderr, "block size (%u) is too large\n", block_size);
436 return -1;
437 }
Doug Zongker075ad802014-06-26 15:35:51 -0700438
Tao Bao91a7aa42017-05-01 15:57:38 -0700439 fuse_data fd = {};
xunchang5e6832a2019-03-15 16:04:32 -0700440 fd.provider = provider.get();
Tao Baoed138192017-05-01 22:30:39 -0700441 fd.file_size = file_size;
442 fd.block_size = block_size;
443 fd.file_blocks = (file_size == 0) ? 0 : (((file_size - 1) / block_size) + 1);
Doug Zongker075ad802014-06-26 15:35:51 -0700444
Tom Marshall0e5905c2014-12-16 14:06:02 -0800445 uint64_t mem = free_memory();
446 uint64_t avail = mem - (INSTALL_REQUIRED_MEMORY + fd.file_blocks * sizeof(uint8_t*));
447
Tao Baoed138192017-05-01 22:30:39 -0700448 int result;
449 if (fd.file_blocks > (1 << 18)) {
450 fprintf(stderr, "file has too many blocks (%u)\n", fd.file_blocks);
451 result = -1;
452 goto done;
453 }
Doug Zongker075ad802014-06-26 15:35:51 -0700454
Tao Bao91a7aa42017-05-01 15:57:38 -0700455 // All hashes will be zero-initialized.
456 fd.hashes.resize(fd.file_blocks);
Tao Baoed138192017-05-01 22:30:39 -0700457 fd.uid = getuid();
458 fd.gid = getgid();
Doug Zongker075ad802014-06-26 15:35:51 -0700459
Tao Baoed138192017-05-01 22:30:39 -0700460 fd.curr_block = -1;
Tao Bao91a7aa42017-05-01 15:57:38 -0700461 fd.block_data = static_cast<uint8_t*>(malloc(block_size));
462 if (fd.block_data == nullptr) {
Tao Baoed138192017-05-01 22:30:39 -0700463 fprintf(stderr, "failed to allocate %d bites for block_data\n", block_size);
464 result = -1;
465 goto done;
466 }
Tao Bao91a7aa42017-05-01 15:57:38 -0700467 fd.extra_block = static_cast<uint8_t*>(malloc(block_size));
468 if (fd.extra_block == nullptr) {
Tao Baoed138192017-05-01 22:30:39 -0700469 fprintf(stderr, "failed to allocate %d bites for extra_block\n", block_size);
470 result = -1;
471 goto done;
472 }
Doug Zongker075ad802014-06-26 15:35:51 -0700473
Tom Marshall0e5905c2014-12-16 14:06:02 -0800474 fd.block_cache_max_size = 0;
475 fd.block_cache_size = 0;
476 fd.block_cache = nullptr;
477 if (mem > avail) {
478 uint32_t max_size = avail / fd.block_size;
479 if (max_size > fd.file_blocks) {
480 max_size = fd.file_blocks;
481 }
482 // The cache must be at least 1% of the file size or two blocks,
483 // whichever is larger.
484 if (max_size >= fd.file_blocks / 100 && max_size >= 2) {
485 fd.block_cache_max_size = max_size;
486 fd.block_cache = (uint8_t**)calloc(fd.file_blocks, sizeof(uint8_t*));
487 }
488 }
489
Tao Bao91a7aa42017-05-01 15:57:38 -0700490 fd.ffd.reset(open("/dev/fuse", O_RDWR));
Bernie Innocenti8bd6f452019-03-28 15:48:08 +0900491 if (fd.ffd == -1) {
Tao Baoed138192017-05-01 22:30:39 -0700492 perror("open /dev/fuse");
493 result = -1;
494 goto done;
495 }
Doug Zongker075ad802014-06-26 15:35:51 -0700496
Tao Baoed138192017-05-01 22:30:39 -0700497 {
498 std::string opts = android::base::StringPrintf(
Tao Bao91a7aa42017-05-01 15:57:38 -0700499 "fd=%d,user_id=%d,group_id=%d,max_read=%u,allow_other,rootmode=040000", fd.ffd.get(),
500 fd.uid, fd.gid, block_size);
Doug Zongker075ad802014-06-26 15:35:51 -0700501
Tao Bao91a7aa42017-05-01 15:57:38 -0700502 result = mount("/dev/fuse", mount_point, "fuse", MS_NOSUID | MS_NODEV | MS_RDONLY | MS_NOEXEC,
503 opts.c_str());
504 if (result == -1) {
Tao Baoed138192017-05-01 22:30:39 -0700505 perror("mount");
506 goto done;
Doug Zongker075ad802014-06-26 15:35:51 -0700507 }
Tao Baoed138192017-05-01 22:30:39 -0700508 }
Doug Zongker075ad802014-06-26 15:35:51 -0700509
Tao Bao91a7aa42017-05-01 15:57:38 -0700510 uint8_t request_buffer[sizeof(fuse_in_header) + PATH_MAX * 8];
Tao Baoed138192017-05-01 22:30:39 -0700511 for (;;) {
512 ssize_t len = TEMP_FAILURE_RETRY(read(fd.ffd, request_buffer, sizeof(request_buffer)));
513 if (len == -1) {
514 perror("read request");
515 if (errno == ENODEV) {
516 result = -1;
517 break;
518 }
519 continue;
Doug Zongker075ad802014-06-26 15:35:51 -0700520 }
521
Tao Bao91a7aa42017-05-01 15:57:38 -0700522 if (static_cast<size_t>(len) < sizeof(fuse_in_header)) {
Tao Baoed138192017-05-01 22:30:39 -0700523 fprintf(stderr, "request too short: len=%zd\n", len);
524 continue;
Doug Zongker075ad802014-06-26 15:35:51 -0700525 }
526
Tao Bao91a7aa42017-05-01 15:57:38 -0700527 fuse_in_header* hdr = reinterpret_cast<fuse_in_header*>(request_buffer);
528 void* data = request_buffer + sizeof(fuse_in_header);
Doug Zongker075ad802014-06-26 15:35:51 -0700529
Tao Baoed138192017-05-01 22:30:39 -0700530 result = -ENOSYS;
531
532 switch (hdr->opcode) {
533 case FUSE_INIT:
534 result = handle_init(data, &fd, hdr);
535 break;
536
537 case FUSE_LOOKUP:
538 result = handle_lookup(data, &fd, hdr);
539 break;
540
541 case FUSE_GETATTR:
542 result = handle_getattr(data, &fd, hdr);
543 break;
544
545 case FUSE_OPEN:
546 result = handle_open(data, &fd, hdr);
547 break;
548
549 case FUSE_READ:
550 result = handle_read(data, &fd, hdr);
551 break;
552
553 case FUSE_FLUSH:
554 result = handle_flush(data, &fd, hdr);
555 break;
556
557 case FUSE_RELEASE:
558 result = handle_release(data, &fd, hdr);
559 break;
560
561 default:
562 fprintf(stderr, "unknown fuse request opcode %d\n", hdr->opcode);
563 break;
564 }
565
566 if (result == NO_STATUS_EXIT) {
567 result = 0;
568 break;
569 }
570
571 if (result != NO_STATUS) {
Tao Bao91a7aa42017-05-01 15:57:38 -0700572 fuse_out_header outhdr;
Tao Baoed138192017-05-01 22:30:39 -0700573 outhdr.len = sizeof(outhdr);
574 outhdr.error = result;
575 outhdr.unique = hdr->unique;
576 TEMP_FAILURE_RETRY(write(fd.ffd, &outhdr, sizeof(outhdr)));
577 }
578 }
579
580done:
xunchang5e6832a2019-03-15 16:04:32 -0700581 provider->Close();
Tao Baoed138192017-05-01 22:30:39 -0700582
Tao Bao91a7aa42017-05-01 15:57:38 -0700583 if (umount2(mount_point, MNT_DETACH) == -1) {
584 fprintf(stderr, "fuse_sideload umount failed: %s\n", strerror(errno));
Tao Baoed138192017-05-01 22:30:39 -0700585 }
586
Tom Marshall0e5905c2014-12-16 14:06:02 -0800587 if (fd.block_cache) {
588 uint32_t n;
589 for (n = 0; n < fd.file_blocks; ++n) {
590 free(fd.block_cache[n]);
591 }
592 free(fd.block_cache);
593 }
594
Tao Baoed138192017-05-01 22:30:39 -0700595 free(fd.block_data);
596 free(fd.extra_block);
597
598 return result;
Doug Zongker075ad802014-06-26 15:35:51 -0700599}