blob: 27e91c11fe0a24ce0ea7dcb501e51ad1482ae24e [file] [log] [blame]
Brian Swetland03ee9472010-08-12 18:01:08 -07001/*
2 * Copyright (C) 2010 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
Elliott Hughes300d5642014-07-08 13:53:26 -070017#define LOG_TAG "sdcard"
18
Elliott Hughes60281d52014-05-07 14:39:58 -070019#include <ctype.h>
20#include <dirent.h>
21#include <errno.h>
22#include <fcntl.h>
Marcus Oaklande43b99a2014-07-23 13:04:59 +010023#include <inttypes.h>
Elliott Hughes60281d52014-05-07 14:39:58 -070024#include <limits.h>
25#include <linux/fuse.h>
26#include <pthread.h>
William Robertse5099802015-07-31 13:11:19 -070027#include <stdbool.h>
Brian Swetland03ee9472010-08-12 18:01:08 -070028#include <stdio.h>
29#include <stdlib.h>
30#include <string.h>
Elliott Hughes60281d52014-05-07 14:39:58 -070031#include <sys/inotify.h>
Brian Swetland03ee9472010-08-12 18:01:08 -070032#include <sys/mount.h>
Christopher Ferrisff649ea2014-09-13 13:53:08 -070033#include <sys/param.h>
Elliott Hughes60281d52014-05-07 14:39:58 -070034#include <sys/resource.h>
Brian Swetland03ee9472010-08-12 18:01:08 -070035#include <sys/stat.h>
Mike Lockwood4553b082010-08-16 14:14:44 -040036#include <sys/statfs.h>
Ken Sumrall2fd72cc2013-02-08 16:50:55 -080037#include <sys/time.h>
William Robertse5099802015-07-31 13:11:19 -070038#include <sys/types.h>
Elliott Hughes60281d52014-05-07 14:39:58 -070039#include <sys/uio.h>
40#include <unistd.h>
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -070041
Jeff Sharkey44d63422013-09-12 09:44:48 -070042#include <cutils/fs.h>
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -070043#include <cutils/hashmap.h>
Elliott Hughes300d5642014-07-08 13:53:26 -070044#include <cutils/log.h>
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -070045#include <cutils/multiuser.h>
Jeff Sharkey20ca9832016-04-07 11:05:21 -060046#include <cutils/properties.h>
William Robertse5099802015-07-31 13:11:19 -070047#include <packagelistparser/packagelistparser.h>
Brian Swetland03ee9472010-08-12 18:01:08 -070048
Brian Swetlandb14a2c62010-08-12 18:21:12 -070049#include <private/android_filesystem_config.h>
50
Daniel Rosenberg2abee9e2016-04-19 18:33:08 -070051/* FUSE_CANONICAL_PATH is not currently upstreamed */
52#define FUSE_CANONICAL_PATH 2016
53
Brian Swetland03ee9472010-08-12 18:01:08 -070054/* README
55 *
56 * What is this?
Elliott Hughes60281d52014-05-07 14:39:58 -070057 *
Brian Swetland03ee9472010-08-12 18:01:08 -070058 * sdcard is a program that uses FUSE to emulate FAT-on-sdcard style
Elliott Hughes60281d52014-05-07 14:39:58 -070059 * directory permissions (all files are given fixed owner, group, and
60 * permissions at creation, owner, group, and permissions are not
Brian Swetland03ee9472010-08-12 18:01:08 -070061 * changeable, symlinks and hardlinks are not createable, etc.
62 *
Jeff Sharkeye169bd02012-08-13 16:44:42 -070063 * See usage() for command line options.
Brian Swetland03ee9472010-08-12 18:01:08 -070064 *
Jeff Sharkeye169bd02012-08-13 16:44:42 -070065 * It must be run as root, but will drop to requested UID/GID as soon as it
66 * mounts a filesystem. It will refuse to run if requested UID/GID are zero.
Brian Swetland03ee9472010-08-12 18:01:08 -070067 *
68 * Things I believe to be true:
69 *
70 * - ops that return a fuse_entry (LOOKUP, MKNOD, MKDIR, LINK, SYMLINK,
71 * CREAT) must bump that node's refcount
72 * - don't forget that FORGET can forget multiple references (req->nlookup)
73 * - if an op that returns a fuse_entry fails writing the reply to the
74 * kernel, you must rollback the refcount to reflect the reference the
75 * kernel did not actually acquire
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -070076 *
77 * This daemon can also derive custom filesystem permissions based on directory
78 * structure when requested. These custom permissions support several features:
79 *
80 * - Apps can access their own files in /Android/data/com.example/ without
81 * requiring any additional GIDs.
82 * - Separate permissions for protecting directories like Pictures and Music.
83 * - Multi-user separation on the same physical device.
Brian Swetland03ee9472010-08-12 18:01:08 -070084 */
85
86#define FUSE_TRACE 0
87
88#if FUSE_TRACE
Elliott Hughes300d5642014-07-08 13:53:26 -070089#define TRACE(x...) ALOGD(x)
Brian Swetland03ee9472010-08-12 18:01:08 -070090#else
91#define TRACE(x...) do {} while (0)
92#endif
93
Elliott Hughes300d5642014-07-08 13:53:26 -070094#define ERROR(x...) ALOGE(x)
Brian Swetland03ee9472010-08-12 18:01:08 -070095
Jeff Sharkey20ca9832016-04-07 11:05:21 -060096#define PROP_SDCARDFS_DEVICE "ro.sys.sdcardfs"
97#define PROP_SDCARDFS_USER "persist.sys.sdcardfs"
98
Brian Swetland03ee9472010-08-12 18:01:08 -070099#define FUSE_UNKNOWN_INO 0xffffffff
100
Jeff Brown84715842012-05-25 14:07:47 -0700101/* Maximum number of bytes to write in one request. */
102#define MAX_WRITE (256 * 1024)
103
104/* Maximum number of bytes to read in one request. */
105#define MAX_READ (128 * 1024)
106
107/* Largest possible request.
108 * The request size is bounded by the maximum size of a FUSE_WRITE request because it has
109 * the largest possible data payload. */
110#define MAX_REQUEST_SIZE (sizeof(struct fuse_in_header) + sizeof(struct fuse_write_in) + MAX_WRITE)
111
Jeff Brown6249b902012-05-26 14:32:54 -0700112/* Pseudo-error constant used to indicate that no fuse status is needed
113 * or that a reply has already been written. */
114#define NO_STATUS 1
115
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700116/* Supplementary groups to execute with */
117static const gid_t kGroups[1] = { AID_PACKAGE_INFO };
118
119/* Permission mode for a specific node. Controls how file permissions
120 * are derived for children nodes. */
121typedef enum {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700122 /* Nothing special; this node should just inherit from its parent. */
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700123 PERM_INHERIT,
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700124 /* This node is one level above a normal root; used for legacy layouts
125 * which use the first level to represent user_id. */
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700126 PERM_PRE_ROOT,
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700127 /* This node is "/" */
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700128 PERM_ROOT,
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700129 /* This node is "/Android" */
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700130 PERM_ANDROID,
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700131 /* This node is "/Android/data" */
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700132 PERM_ANDROID_DATA,
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700133 /* This node is "/Android/obb" */
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700134 PERM_ANDROID_OBB,
Jeff Sharkey2e7d80d2014-05-30 15:38:31 -0700135 /* This node is "/Android/media" */
136 PERM_ANDROID_MEDIA,
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700137} perm_t;
138
Brian Swetland03ee9472010-08-12 18:01:08 -0700139struct handle {
Brian Swetland03ee9472010-08-12 18:01:08 -0700140 int fd;
141};
142
143struct dirhandle {
Brian Swetland03ee9472010-08-12 18:01:08 -0700144 DIR *d;
145};
146
147struct node {
Jeff Brown6249b902012-05-26 14:32:54 -0700148 __u32 refcount;
Brian Swetland03ee9472010-08-12 18:01:08 -0700149 __u64 nid;
150 __u64 gen;
Narayan Kamathfaa09352015-01-13 18:21:10 +0000151 /*
152 * The inode number for this FUSE node. Note that this isn't stable across
153 * multiple invocations of the FUSE daemon.
154 */
155 __u32 ino;
Brian Swetland03ee9472010-08-12 18:01:08 -0700156
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700157 /* State derived based on current position in hierarchy. */
158 perm_t perm;
159 userid_t userid;
160 uid_t uid;
Jeff Sharkey10a239b2015-07-28 10:49:41 -0700161 bool under_android;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700162
Paul Eastham11ccdb32010-10-14 11:04:26 -0700163 struct node *next; /* per-dir sibling list */
164 struct node *child; /* first contained file by this dir */
Paul Eastham11ccdb32010-10-14 11:04:26 -0700165 struct node *parent; /* containing directory */
Brian Swetland03ee9472010-08-12 18:01:08 -0700166
Jeff Brown6249b902012-05-26 14:32:54 -0700167 size_t namelen;
Paul Eastham11ccdb32010-10-14 11:04:26 -0700168 char *name;
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800169 /* If non-null, this is the real name of the file in the underlying storage.
170 * This may differ from the field "name" only by case.
171 * strlen(actual_name) will always equal strlen(name), so it is safe to use
172 * namelen for both fields.
173 */
174 char *actual_name;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700175
176 /* If non-null, an exact underlying path that should be grafted into this
177 * position. Used to support things like OBB. */
178 char* graft_path;
179 size_t graft_pathlen;
Krzysztof Adamskic5353122014-07-16 08:34:30 +0200180
181 bool deleted;
Brian Swetland03ee9472010-08-12 18:01:08 -0700182};
183
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700184static int str_hash(void *key) {
185 return hashmapHash(key, strlen(key));
186}
187
Jeff Sharkey44d63422013-09-12 09:44:48 -0700188/** Test if two string keys are equal ignoring case */
189static bool str_icase_equals(void *keyA, void *keyB) {
190 return strcasecmp(keyA, keyB) == 0;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700191}
192
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700193/* Global data for all FUSE mounts */
194struct fuse_global {
Jeff Brown6249b902012-05-26 14:32:54 -0700195 pthread_mutex_t lock;
196
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700197 uid_t uid;
198 gid_t gid;
199 bool multi_user;
200
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700201 char source_path[PATH_MAX];
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700202 char obb_path[PATH_MAX];
203
Jeff Sharkeyed2fe572015-07-16 09:13:52 -0700204 Hashmap* package_to_appid;
205
Brian Swetland03ee9472010-08-12 18:01:08 -0700206 __u64 next_generation;
Brian Swetland03ee9472010-08-12 18:01:08 -0700207 struct node root;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700208
Narayan Kamathfaa09352015-01-13 18:21:10 +0000209 /* Used to allocate unique inode numbers for fuse nodes. We use
210 * a simple counter based scheme where inode numbers from deleted
211 * nodes aren't reused. Note that inode allocations are not stable
212 * across multiple invocation of the sdcard daemon, but that shouldn't
213 * be a huge problem in practice.
214 *
215 * Note that we restrict inodes to 32 bit unsigned integers to prevent
216 * truncation on 32 bit processes when unsigned long long stat.st_ino is
217 * assigned to an unsigned long ino_t type in an LP32 process.
218 *
219 * Also note that fuse_attr and fuse_dirent inode values are 64 bits wide
220 * on both LP32 and LP64, but the fuse kernel code doesn't squash 64 bit
221 * inode numbers into 32 bit values on 64 bit kernels (see fuse_squash_ino
222 * in fs/fuse/inode.c).
223 *
224 * Accesses must be guarded by |lock|.
225 */
226 __u32 inode_ctr;
Jeff Sharkeyed2fe572015-07-16 09:13:52 -0700227
228 struct fuse* fuse_default;
229 struct fuse* fuse_read;
230 struct fuse* fuse_write;
231};
232
233/* Single FUSE mount */
234struct fuse {
235 struct fuse_global* global;
236
237 char dest_path[PATH_MAX];
238
239 int fd;
240
241 gid_t gid;
242 mode_t mask;
Brian Swetland03ee9472010-08-12 18:01:08 -0700243};
244
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700245/* Private data used by a single FUSE handler */
Jeff Brown7729d242012-05-25 15:35:28 -0700246struct fuse_handler {
Jeff Brown6249b902012-05-26 14:32:54 -0700247 struct fuse* fuse;
248 int token;
249
Jeff Brown7729d242012-05-25 15:35:28 -0700250 /* To save memory, we never use the contents of the request buffer and the read
251 * buffer at the same time. This allows us to share the underlying storage. */
252 union {
253 __u8 request_buffer[MAX_REQUEST_SIZE];
Elliott Hughese24e9a52015-07-28 16:36:47 -0700254 __u8 read_buffer[MAX_READ + PAGE_SIZE];
Jeff Brown7729d242012-05-25 15:35:28 -0700255 };
256};
Brian Swetland03ee9472010-08-12 18:01:08 -0700257
Jeff Brown6249b902012-05-26 14:32:54 -0700258static inline void *id_to_ptr(__u64 nid)
Brian Swetland03ee9472010-08-12 18:01:08 -0700259{
Jeff Brown6249b902012-05-26 14:32:54 -0700260 return (void *) (uintptr_t) nid;
261}
Brian Swetland03ee9472010-08-12 18:01:08 -0700262
Jeff Brown6249b902012-05-26 14:32:54 -0700263static inline __u64 ptr_to_id(void *ptr)
264{
265 return (__u64) (uintptr_t) ptr;
266}
Brian Swetland03ee9472010-08-12 18:01:08 -0700267
Jeff Brown6249b902012-05-26 14:32:54 -0700268static void acquire_node_locked(struct node* node)
269{
270 node->refcount++;
271 TRACE("ACQUIRE %p (%s) rc=%d\n", node, node->name, node->refcount);
272}
273
274static void remove_node_from_parent_locked(struct node* node);
275
276static void release_node_locked(struct node* node)
277{
278 TRACE("RELEASE %p (%s) rc=%d\n", node, node->name, node->refcount);
279 if (node->refcount > 0) {
280 node->refcount--;
281 if (!node->refcount) {
282 TRACE("DESTROY %p (%s)\n", node, node->name);
283 remove_node_from_parent_locked(node);
284
285 /* TODO: remove debugging - poison memory */
286 memset(node->name, 0xef, node->namelen);
287 free(node->name);
288 free(node->actual_name);
289 memset(node, 0xfc, sizeof(*node));
290 free(node);
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800291 }
Jeff Brown6249b902012-05-26 14:32:54 -0700292 } else {
293 ERROR("Zero refcnt %p\n", node);
294 }
295}
296
297static void add_node_to_parent_locked(struct node *node, struct node *parent) {
298 node->parent = parent;
299 node->next = parent->child;
300 parent->child = node;
301 acquire_node_locked(parent);
302}
303
304static void remove_node_from_parent_locked(struct node* node)
305{
306 if (node->parent) {
307 if (node->parent->child == node) {
308 node->parent->child = node->parent->child->next;
309 } else {
310 struct node *node2;
311 node2 = node->parent->child;
312 while (node2->next != node)
313 node2 = node2->next;
314 node2->next = node->next;
315 }
316 release_node_locked(node->parent);
317 node->parent = NULL;
318 node->next = NULL;
319 }
320}
321
322/* Gets the absolute path to a node into the provided buffer.
323 *
324 * Populates 'buf' with the path and returns the length of the path on success,
325 * or returns -1 if the path is too long for the provided buffer.
326 */
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700327static ssize_t get_node_path_locked(struct node* node, char* buf, size_t bufsize) {
328 const char* name;
329 size_t namelen;
330 if (node->graft_path) {
331 name = node->graft_path;
332 namelen = node->graft_pathlen;
333 } else if (node->actual_name) {
334 name = node->actual_name;
335 namelen = node->namelen;
336 } else {
337 name = node->name;
338 namelen = node->namelen;
339 }
340
Jeff Brown6249b902012-05-26 14:32:54 -0700341 if (bufsize < namelen + 1) {
342 return -1;
Brian Swetland03ee9472010-08-12 18:01:08 -0700343 }
344
Jeff Brown6249b902012-05-26 14:32:54 -0700345 ssize_t pathlen = 0;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700346 if (node->parent && node->graft_path == NULL) {
Daniel Rosenbergdb4638e2016-04-12 16:30:28 -0700347 pathlen = get_node_path_locked(node->parent, buf, bufsize - namelen - 1);
Jeff Brown6249b902012-05-26 14:32:54 -0700348 if (pathlen < 0) {
349 return -1;
350 }
351 buf[pathlen++] = '/';
352 }
353
Jeff Brown6249b902012-05-26 14:32:54 -0700354 memcpy(buf + pathlen, name, namelen + 1); /* include trailing \0 */
355 return pathlen + namelen;
356}
357
358/* Finds the absolute path of a file within a given directory.
359 * Performs a case-insensitive search for the file and sets the buffer to the path
360 * of the first matching file. If 'search' is zero or if no match is found, sets
361 * the buffer to the path that the file would have, assuming the name were case-sensitive.
362 *
363 * Populates 'buf' with the path and returns the actual name (within 'buf') on success,
364 * or returns NULL if the path is too long for the provided buffer.
365 */
366static char* find_file_within(const char* path, const char* name,
367 char* buf, size_t bufsize, int search)
368{
369 size_t pathlen = strlen(path);
370 size_t namelen = strlen(name);
371 size_t childlen = pathlen + namelen + 1;
372 char* actual;
373
374 if (bufsize <= childlen) {
375 return NULL;
376 }
377
378 memcpy(buf, path, pathlen);
379 buf[pathlen] = '/';
380 actual = buf + pathlen + 1;
381 memcpy(actual, name, namelen + 1);
382
383 if (search && access(buf, F_OK)) {
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800384 struct dirent* entry;
Jeff Brown6249b902012-05-26 14:32:54 -0700385 DIR* dir = opendir(path);
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800386 if (!dir) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700387 ERROR("opendir %s failed: %s\n", path, strerror(errno));
Jeff Brown6249b902012-05-26 14:32:54 -0700388 return actual;
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800389 }
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800390 while ((entry = readdir(dir))) {
Jeff Brown6249b902012-05-26 14:32:54 -0700391 if (!strcasecmp(entry->d_name, name)) {
392 /* we have a match - replace the name, don't need to copy the null again */
393 memcpy(actual, entry->d_name, namelen);
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800394 break;
395 }
396 }
397 closedir(dir);
398 }
Jeff Brown6249b902012-05-26 14:32:54 -0700399 return actual;
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800400}
401
Jeff Sharkeyed2fe572015-07-16 09:13:52 -0700402static void attr_from_stat(struct fuse* fuse, struct fuse_attr *attr,
403 const struct stat *s, const struct node* node) {
Narayan Kamathfaa09352015-01-13 18:21:10 +0000404 attr->ino = node->ino;
Brian Swetland03ee9472010-08-12 18:01:08 -0700405 attr->size = s->st_size;
406 attr->blocks = s->st_blocks;
Elliott Hughesf1df8542014-11-10 11:03:38 -0800407 attr->atime = s->st_atim.tv_sec;
408 attr->mtime = s->st_mtim.tv_sec;
409 attr->ctime = s->st_ctim.tv_sec;
410 attr->atimensec = s->st_atim.tv_nsec;
411 attr->mtimensec = s->st_mtim.tv_nsec;
412 attr->ctimensec = s->st_ctim.tv_nsec;
Brian Swetland03ee9472010-08-12 18:01:08 -0700413 attr->mode = s->st_mode;
414 attr->nlink = s->st_nlink;
Brian Swetland03ee9472010-08-12 18:01:08 -0700415
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700416 attr->uid = node->uid;
Jeff Sharkeyed2fe572015-07-16 09:13:52 -0700417
418 if (fuse->gid == AID_SDCARD_RW) {
419 /* As an optimization, certain trusted system components only run
420 * as owner but operate across all users. Since we're now handing
421 * out the sdcard_rw GID only to trusted apps, we're okay relaxing
422 * the user boundary enforcement for the default view. The UIDs
423 * assigned to app directories are still multiuser aware. */
424 attr->gid = AID_SDCARD_RW;
425 } else {
426 attr->gid = multiuser_get_uid(node->userid, fuse->gid);
427 }
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700428
Jeff Sharkey10a239b2015-07-28 10:49:41 -0700429 int visible_mode = 0775 & ~fuse->mask;
430 if (node->perm == PERM_PRE_ROOT) {
431 /* Top of multi-user view should always be visible to ensure
432 * secondary users can traverse inside. */
433 visible_mode = 0711;
434 } else if (node->under_android) {
435 /* Block "other" access to Android directories, since only apps
436 * belonging to a specific user should be in there; we still
437 * leave +x open for the default view. */
438 if (fuse->gid == AID_SDCARD_RW) {
439 visible_mode = visible_mode & ~0006;
440 } else {
441 visible_mode = visible_mode & ~0007;
442 }
Jeff Sharkeyed2fe572015-07-16 09:13:52 -0700443 }
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700444 int owner_mode = s->st_mode & 0700;
Jeff Sharkeyed2fe572015-07-16 09:13:52 -0700445 int filtered_mode = visible_mode & (owner_mode | (owner_mode >> 3) | (owner_mode >> 6));
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700446 attr->mode = (attr->mode & S_IFMT) | filtered_mode;
447}
448
Jeff Sharkey44d63422013-09-12 09:44:48 -0700449static int touch(char* path, mode_t mode) {
450 int fd = open(path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW, mode);
451 if (fd == -1) {
452 if (errno == EEXIST) {
453 return 0;
454 } else {
455 ERROR("Failed to open(%s): %s\n", path, strerror(errno));
456 return -1;
457 }
458 }
459 close(fd);
460 return 0;
461}
462
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700463static void derive_permissions_locked(struct fuse* fuse, struct node *parent,
464 struct node *node) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700465 appid_t appid;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700466
467 /* By default, each node inherits from its parent */
468 node->perm = PERM_INHERIT;
469 node->userid = parent->userid;
470 node->uid = parent->uid;
Jeff Sharkey10a239b2015-07-28 10:49:41 -0700471 node->under_android = parent->under_android;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700472
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700473 /* Derive custom permissions based on parent and current node */
474 switch (parent->perm) {
475 case PERM_INHERIT:
476 /* Already inherited above */
477 break;
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700478 case PERM_PRE_ROOT:
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700479 /* Legacy internal layout places users at top level */
480 node->perm = PERM_ROOT;
481 node->userid = strtoul(node->name, NULL, 10);
482 break;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700483 case PERM_ROOT:
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700484 /* Assume masked off by default. */
Jeff Sharkey44d63422013-09-12 09:44:48 -0700485 if (!strcasecmp(node->name, "Android")) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700486 /* App-specific directories inside; let anyone traverse */
487 node->perm = PERM_ANDROID;
Jeff Sharkey10a239b2015-07-28 10:49:41 -0700488 node->under_android = true;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700489 }
490 break;
491 case PERM_ANDROID:
Jeff Sharkey44d63422013-09-12 09:44:48 -0700492 if (!strcasecmp(node->name, "data")) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700493 /* App-specific directories inside; let anyone traverse */
494 node->perm = PERM_ANDROID_DATA;
Jeff Sharkey44d63422013-09-12 09:44:48 -0700495 } else if (!strcasecmp(node->name, "obb")) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700496 /* App-specific directories inside; let anyone traverse */
497 node->perm = PERM_ANDROID_OBB;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700498 /* Single OBB directory is always shared */
Jeff Sharkeyed2fe572015-07-16 09:13:52 -0700499 node->graft_path = fuse->global->obb_path;
500 node->graft_pathlen = strlen(fuse->global->obb_path);
Jeff Sharkey2e7d80d2014-05-30 15:38:31 -0700501 } else if (!strcasecmp(node->name, "media")) {
502 /* App-specific directories inside; let anyone traverse */
503 node->perm = PERM_ANDROID_MEDIA;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700504 }
505 break;
506 case PERM_ANDROID_DATA:
507 case PERM_ANDROID_OBB:
Jeff Sharkey2e7d80d2014-05-30 15:38:31 -0700508 case PERM_ANDROID_MEDIA:
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700509 appid = (appid_t) (uintptr_t) hashmapGet(fuse->global->package_to_appid, node->name);
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700510 if (appid != 0) {
511 node->uid = multiuser_get_uid(parent->userid, appid);
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700512 }
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700513 break;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700514 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700515}
516
Jeff Sharkeyfe764612015-12-14 11:02:01 -0700517static void derive_permissions_recursive_locked(struct fuse* fuse, struct node *parent) {
518 struct node *node;
519 for (node = parent->child; node; node = node->next) {
520 derive_permissions_locked(fuse, parent, node);
521 if (node->child) {
522 derive_permissions_recursive_locked(fuse, node);
523 }
524 }
525}
526
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700527/* Kernel has already enforced everything we returned through
528 * derive_permissions_locked(), so this is used to lock down access
529 * even further, such as enforcing that apps hold sdcard_rw. */
530static bool check_caller_access_to_name(struct fuse* fuse,
531 const struct fuse_in_header *hdr, const struct node* parent_node,
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700532 const char* name, int mode) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700533 /* Always block security-sensitive files at root */
534 if (parent_node && parent_node->perm == PERM_ROOT) {
Jeff Sharkey44d63422013-09-12 09:44:48 -0700535 if (!strcasecmp(name, "autorun.inf")
536 || !strcasecmp(name, ".android_secure")
537 || !strcasecmp(name, "android_secure")) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700538 return false;
539 }
540 }
541
Jeff Sharkey44d63422013-09-12 09:44:48 -0700542 /* Root always has access; access for any other UIDs should always
543 * be controlled through packages.list. */
544 if (hdr->uid == 0) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700545 return true;
546 }
547
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700548 /* No extra permissions to enforce */
549 return true;
550}
551
552static bool check_caller_access_to_node(struct fuse* fuse,
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700553 const struct fuse_in_header *hdr, const struct node* node, int mode) {
554 return check_caller_access_to_name(fuse, hdr, node->parent, node->name, mode);
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700555}
556
Jeff Brown6249b902012-05-26 14:32:54 -0700557struct node *create_node_locked(struct fuse* fuse,
558 struct node *parent, const char *name, const char* actual_name)
Brian Swetland03ee9472010-08-12 18:01:08 -0700559{
560 struct node *node;
Jeff Brown6249b902012-05-26 14:32:54 -0700561 size_t namelen = strlen(name);
Brian Swetland03ee9472010-08-12 18:01:08 -0700562
Narayan Kamathfaa09352015-01-13 18:21:10 +0000563 // Detect overflows in the inode counter. "4 billion nodes should be enough
564 // for everybody".
Jeff Sharkeyed2fe572015-07-16 09:13:52 -0700565 if (fuse->global->inode_ctr == 0) {
Narayan Kamathfaa09352015-01-13 18:21:10 +0000566 ERROR("No more inode numbers available");
567 return NULL;
568 }
569
Paul Eastham11ccdb32010-10-14 11:04:26 -0700570 node = calloc(1, sizeof(struct node));
Jeff Brown6249b902012-05-26 14:32:54 -0700571 if (!node) {
572 return NULL;
Brian Swetland03ee9472010-08-12 18:01:08 -0700573 }
Paul Eastham11ccdb32010-10-14 11:04:26 -0700574 node->name = malloc(namelen + 1);
Jeff Brown6249b902012-05-26 14:32:54 -0700575 if (!node->name) {
Paul Eastham11ccdb32010-10-14 11:04:26 -0700576 free(node);
Jeff Brown6249b902012-05-26 14:32:54 -0700577 return NULL;
Paul Eastham11ccdb32010-10-14 11:04:26 -0700578 }
Brian Swetland03ee9472010-08-12 18:01:08 -0700579 memcpy(node->name, name, namelen + 1);
Jeff Brown6249b902012-05-26 14:32:54 -0700580 if (strcmp(name, actual_name)) {
581 node->actual_name = malloc(namelen + 1);
582 if (!node->actual_name) {
583 free(node->name);
584 free(node);
585 return NULL;
586 }
587 memcpy(node->actual_name, actual_name, namelen + 1);
588 }
Brian Swetland03ee9472010-08-12 18:01:08 -0700589 node->namelen = namelen;
Jeff Brown6249b902012-05-26 14:32:54 -0700590 node->nid = ptr_to_id(node);
Jeff Sharkeyed2fe572015-07-16 09:13:52 -0700591 node->ino = fuse->global->inode_ctr++;
592 node->gen = fuse->global->next_generation++;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700593
Krzysztof Adamskic5353122014-07-16 08:34:30 +0200594 node->deleted = false;
595
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700596 derive_permissions_locked(fuse, parent, node);
Jeff Brown6249b902012-05-26 14:32:54 -0700597 acquire_node_locked(node);
598 add_node_to_parent_locked(node, parent);
Brian Swetland03ee9472010-08-12 18:01:08 -0700599 return node;
600}
601
Jeff Brown6249b902012-05-26 14:32:54 -0700602static int rename_node_locked(struct node *node, const char *name,
603 const char* actual_name)
Paul Eastham11ccdb32010-10-14 11:04:26 -0700604{
Jeff Brown6249b902012-05-26 14:32:54 -0700605 size_t namelen = strlen(name);
606 int need_actual_name = strcmp(name, actual_name);
607
608 /* make the storage bigger without actually changing the name
609 * in case an error occurs part way */
610 if (namelen > node->namelen) {
611 char* new_name = realloc(node->name, namelen + 1);
612 if (!new_name) {
613 return -ENOMEM;
614 }
615 node->name = new_name;
616 if (need_actual_name && node->actual_name) {
617 char* new_actual_name = realloc(node->actual_name, namelen + 1);
618 if (!new_actual_name) {
619 return -ENOMEM;
620 }
621 node->actual_name = new_actual_name;
622 }
623 }
624
625 /* update the name, taking care to allocate storage before overwriting the old name */
626 if (need_actual_name) {
627 if (!node->actual_name) {
628 node->actual_name = malloc(namelen + 1);
629 if (!node->actual_name) {
630 return -ENOMEM;
631 }
632 }
633 memcpy(node->actual_name, actual_name, namelen + 1);
634 } else {
635 free(node->actual_name);
636 node->actual_name = NULL;
637 }
638 memcpy(node->name, name, namelen + 1);
639 node->namelen = namelen;
640 return 0;
Paul Eastham11ccdb32010-10-14 11:04:26 -0700641}
642
Jeff Brown6249b902012-05-26 14:32:54 -0700643static struct node *lookup_node_by_id_locked(struct fuse *fuse, __u64 nid)
Brian Swetland03ee9472010-08-12 18:01:08 -0700644{
Jeff Brown6249b902012-05-26 14:32:54 -0700645 if (nid == FUSE_ROOT_ID) {
Jeff Sharkeyed2fe572015-07-16 09:13:52 -0700646 return &fuse->global->root;
Brian Swetland03ee9472010-08-12 18:01:08 -0700647 } else {
648 return id_to_ptr(nid);
649 }
650}
651
Jeff Brown6249b902012-05-26 14:32:54 -0700652static struct node* lookup_node_and_path_by_id_locked(struct fuse* fuse, __u64 nid,
653 char* buf, size_t bufsize)
654{
655 struct node* node = lookup_node_by_id_locked(fuse, nid);
656 if (node && get_node_path_locked(node, buf, bufsize) < 0) {
657 node = NULL;
658 }
659 return node;
660}
661
662static struct node *lookup_child_by_name_locked(struct node *node, const char *name)
Brian Swetland03ee9472010-08-12 18:01:08 -0700663{
664 for (node = node->child; node; node = node->next) {
Jeff Brown6249b902012-05-26 14:32:54 -0700665 /* use exact string comparison, nodes that differ by case
666 * must be considered distinct even if they refer to the same
667 * underlying file as otherwise operations such as "mv x x"
668 * will not work because the source and target nodes are the same. */
Krzysztof Adamskic5353122014-07-16 08:34:30 +0200669 if (!strcmp(name, node->name) && !node->deleted) {
Brian Swetland03ee9472010-08-12 18:01:08 -0700670 return node;
671 }
672 }
673 return 0;
674}
675
Jeff Brown6249b902012-05-26 14:32:54 -0700676static struct node* acquire_or_create_child_locked(
677 struct fuse* fuse, struct node* parent,
678 const char* name, const char* actual_name)
Brian Swetland03ee9472010-08-12 18:01:08 -0700679{
Jeff Brown6249b902012-05-26 14:32:54 -0700680 struct node* child = lookup_child_by_name_locked(parent, name);
681 if (child) {
682 acquire_node_locked(child);
Paul Eastham77085c52011-01-04 21:06:03 -0800683 } else {
Jeff Brown6249b902012-05-26 14:32:54 -0700684 child = create_node_locked(fuse, parent, name, actual_name);
Paul Eastham77085c52011-01-04 21:06:03 -0800685 }
Jeff Brown6249b902012-05-26 14:32:54 -0700686 return child;
Paul Eastham11ccdb32010-10-14 11:04:26 -0700687}
688
Jeff Brown6249b902012-05-26 14:32:54 -0700689static void fuse_status(struct fuse *fuse, __u64 unique, int err)
Brian Swetland03ee9472010-08-12 18:01:08 -0700690{
691 struct fuse_out_header hdr;
692 hdr.len = sizeof(hdr);
693 hdr.error = err;
694 hdr.unique = unique;
Brian Swetland03ee9472010-08-12 18:01:08 -0700695 write(fuse->fd, &hdr, sizeof(hdr));
696}
697
Jeff Brown6249b902012-05-26 14:32:54 -0700698static void fuse_reply(struct fuse *fuse, __u64 unique, void *data, int len)
Brian Swetland03ee9472010-08-12 18:01:08 -0700699{
700 struct fuse_out_header hdr;
701 struct iovec vec[2];
702 int res;
703
704 hdr.len = len + sizeof(hdr);
705 hdr.error = 0;
706 hdr.unique = unique;
707
708 vec[0].iov_base = &hdr;
709 vec[0].iov_len = sizeof(hdr);
710 vec[1].iov_base = data;
711 vec[1].iov_len = len;
712
713 res = writev(fuse->fd, vec, 2);
714 if (res < 0) {
715 ERROR("*** REPLY FAILED *** %d\n", errno);
716 }
717}
718
Jeff Brown6249b902012-05-26 14:32:54 -0700719static int fuse_reply_entry(struct fuse* fuse, __u64 unique,
720 struct node* parent, const char* name, const char* actual_name,
721 const char* path)
Brian Swetland03ee9472010-08-12 18:01:08 -0700722{
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700723 struct node* node;
Jeff Brown6249b902012-05-26 14:32:54 -0700724 struct fuse_entry_out out;
725 struct stat s;
Brian Swetland03ee9472010-08-12 18:01:08 -0700726
Jeff Brown6249b902012-05-26 14:32:54 -0700727 if (lstat(path, &s) < 0) {
Jeff Sharkey44d63422013-09-12 09:44:48 -0700728 return -errno;
Brian Swetland03ee9472010-08-12 18:01:08 -0700729 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700730
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700731 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700732 node = acquire_or_create_child_locked(fuse, parent, name, actual_name);
733 if (!node) {
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700734 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700735 return -ENOMEM;
736 }
737 memset(&out, 0, sizeof(out));
Jeff Sharkeyed2fe572015-07-16 09:13:52 -0700738 attr_from_stat(fuse, &out.attr, &s, node);
Jeff Brown6249b902012-05-26 14:32:54 -0700739 out.attr_valid = 10;
740 out.entry_valid = 10;
Brian Swetland03ee9472010-08-12 18:01:08 -0700741 out.nodeid = node->nid;
742 out.generation = node->gen;
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700743 pthread_mutex_unlock(&fuse->global->lock);
Brian Swetland03ee9472010-08-12 18:01:08 -0700744 fuse_reply(fuse, unique, &out, sizeof(out));
Jeff Brown6249b902012-05-26 14:32:54 -0700745 return NO_STATUS;
Brian Swetland03ee9472010-08-12 18:01:08 -0700746}
747
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700748static int fuse_reply_attr(struct fuse* fuse, __u64 unique, const struct node* node,
Jeff Brown6249b902012-05-26 14:32:54 -0700749 const char* path)
750{
751 struct fuse_attr_out out;
752 struct stat s;
753
754 if (lstat(path, &s) < 0) {
755 return -errno;
756 }
757 memset(&out, 0, sizeof(out));
Jeff Sharkeyed2fe572015-07-16 09:13:52 -0700758 attr_from_stat(fuse, &out.attr, &s, node);
Jeff Brown6249b902012-05-26 14:32:54 -0700759 out.attr_valid = 10;
760 fuse_reply(fuse, unique, &out, sizeof(out));
761 return NO_STATUS;
762}
763
Jeff Sharkeyed2fe572015-07-16 09:13:52 -0700764static void fuse_notify_delete(struct fuse* fuse, const __u64 parent,
765 const __u64 child, const char* name) {
766 struct fuse_out_header hdr;
767 struct fuse_notify_delete_out data;
768 struct iovec vec[3];
769 size_t namelen = strlen(name);
770 int res;
771
772 hdr.len = sizeof(hdr) + sizeof(data) + namelen + 1;
773 hdr.error = FUSE_NOTIFY_DELETE;
774 hdr.unique = 0;
775
776 data.parent = parent;
777 data.child = child;
778 data.namelen = namelen;
779 data.padding = 0;
780
781 vec[0].iov_base = &hdr;
782 vec[0].iov_len = sizeof(hdr);
783 vec[1].iov_base = &data;
784 vec[1].iov_len = sizeof(data);
785 vec[2].iov_base = (void*) name;
786 vec[2].iov_len = namelen + 1;
787
788 res = writev(fuse->fd, vec, 3);
789 /* Ignore ENOENT, since other views may not have seen the entry */
790 if (res < 0 && errno != ENOENT) {
791 ERROR("*** NOTIFY FAILED *** %d\n", errno);
792 }
793}
794
Jeff Brown6249b902012-05-26 14:32:54 -0700795static int handle_lookup(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700796 const struct fuse_in_header *hdr, const char* name)
Brian Swetland03ee9472010-08-12 18:01:08 -0700797{
Jeff Brown6249b902012-05-26 14:32:54 -0700798 struct node* parent_node;
799 char parent_path[PATH_MAX];
800 char child_path[PATH_MAX];
801 const char* actual_name;
Brian Swetland03ee9472010-08-12 18:01:08 -0700802
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700803 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700804 parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
805 parent_path, sizeof(parent_path));
Marcus Oaklande43b99a2014-07-23 13:04:59 +0100806 TRACE("[%d] LOOKUP %s @ %"PRIx64" (%s)\n", handler->token, name, hdr->nodeid,
Jeff Brown6249b902012-05-26 14:32:54 -0700807 parent_node ? parent_node->name : "?");
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700808 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700809
810 if (!parent_node || !(actual_name = find_file_within(parent_path, name,
811 child_path, sizeof(child_path), 1))) {
812 return -ENOENT;
Brian Swetland03ee9472010-08-12 18:01:08 -0700813 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700814 if (!check_caller_access_to_name(fuse, hdr, parent_node, name, R_OK)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700815 return -EACCES;
816 }
817
Jeff Brown6249b902012-05-26 14:32:54 -0700818 return fuse_reply_entry(fuse, hdr->unique, parent_node, name, actual_name, child_path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700819}
820
Jeff Brown6249b902012-05-26 14:32:54 -0700821static int handle_forget(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700822 const struct fuse_in_header *hdr, const struct fuse_forget_in *req)
823{
Jeff Brown6249b902012-05-26 14:32:54 -0700824 struct node* node;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700825
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700826 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700827 node = lookup_node_by_id_locked(fuse, hdr->nodeid);
Marcus Oaklande43b99a2014-07-23 13:04:59 +0100828 TRACE("[%d] FORGET #%"PRIu64" @ %"PRIx64" (%s)\n", handler->token, req->nlookup,
Jeff Brown6249b902012-05-26 14:32:54 -0700829 hdr->nodeid, node ? node->name : "?");
830 if (node) {
831 __u64 n = req->nlookup;
Daniel Micaydf9c4a02016-04-26 11:42:08 -0400832 while (n) {
833 n--;
Jeff Brown6249b902012-05-26 14:32:54 -0700834 release_node_locked(node);
835 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700836 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700837 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700838 return NO_STATUS; /* no reply */
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700839}
840
Jeff Brown6249b902012-05-26 14:32:54 -0700841static int handle_getattr(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700842 const struct fuse_in_header *hdr, const struct fuse_getattr_in *req)
843{
Jeff Brown6249b902012-05-26 14:32:54 -0700844 struct node* node;
845 char path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700846
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700847 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700848 node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid, path, sizeof(path));
Marcus Oaklande43b99a2014-07-23 13:04:59 +0100849 TRACE("[%d] GETATTR flags=%x fh=%"PRIx64" @ %"PRIx64" (%s)\n", handler->token,
Jeff Brown6249b902012-05-26 14:32:54 -0700850 req->getattr_flags, req->fh, hdr->nodeid, node ? node->name : "?");
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700851 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700852
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700853 if (!node) {
Jeff Brown6249b902012-05-26 14:32:54 -0700854 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700855 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700856 if (!check_caller_access_to_node(fuse, hdr, node, R_OK)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700857 return -EACCES;
858 }
859
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700860 return fuse_reply_attr(fuse, hdr->unique, node, path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700861}
862
Jeff Brown6249b902012-05-26 14:32:54 -0700863static int handle_setattr(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700864 const struct fuse_in_header *hdr, const struct fuse_setattr_in *req)
865{
Jeff Brown6249b902012-05-26 14:32:54 -0700866 struct node* node;
867 char path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700868 struct timespec times[2];
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700869
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700870 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700871 node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid, path, sizeof(path));
Marcus Oaklande43b99a2014-07-23 13:04:59 +0100872 TRACE("[%d] SETATTR fh=%"PRIx64" valid=%x @ %"PRIx64" (%s)\n", handler->token,
Jeff Brown6249b902012-05-26 14:32:54 -0700873 req->fh, req->valid, hdr->nodeid, node ? node->name : "?");
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700874 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700875
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700876 if (!node) {
Jeff Brown6249b902012-05-26 14:32:54 -0700877 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700878 }
Marco Nelissena80f0982014-12-10 10:44:20 -0800879
880 if (!(req->valid & FATTR_FH) &&
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700881 !check_caller_access_to_node(fuse, hdr, node, W_OK)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700882 return -EACCES;
883 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700884
Jeff Brown6249b902012-05-26 14:32:54 -0700885 /* XXX: incomplete implementation on purpose.
886 * chmod/chown should NEVER be implemented.*/
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700887
Elliott Hughes853574d2014-07-31 12:03:03 -0700888 if ((req->valid & FATTR_SIZE) && truncate64(path, req->size) < 0) {
Jeff Brown6249b902012-05-26 14:32:54 -0700889 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700890 }
891
892 /* Handle changing atime and mtime. If FATTR_ATIME_and FATTR_ATIME_NOW
893 * are both set, then set it to the current time. Else, set it to the
894 * time specified in the request. Same goes for mtime. Use utimensat(2)
895 * as it allows ATIME and MTIME to be changed independently, and has
896 * nanosecond resolution which fuse also has.
897 */
898 if (req->valid & (FATTR_ATIME | FATTR_MTIME)) {
899 times[0].tv_nsec = UTIME_OMIT;
900 times[1].tv_nsec = UTIME_OMIT;
901 if (req->valid & FATTR_ATIME) {
902 if (req->valid & FATTR_ATIME_NOW) {
903 times[0].tv_nsec = UTIME_NOW;
904 } else {
905 times[0].tv_sec = req->atime;
906 times[0].tv_nsec = req->atimensec;
907 }
908 }
909 if (req->valid & FATTR_MTIME) {
910 if (req->valid & FATTR_MTIME_NOW) {
911 times[1].tv_nsec = UTIME_NOW;
912 } else {
913 times[1].tv_sec = req->mtime;
914 times[1].tv_nsec = req->mtimensec;
915 }
916 }
Jeff Brown6249b902012-05-26 14:32:54 -0700917 TRACE("[%d] Calling utimensat on %s with atime %ld, mtime=%ld\n",
918 handler->token, path, times[0].tv_sec, times[1].tv_sec);
919 if (utimensat(-1, path, times, 0) < 0) {
920 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700921 }
922 }
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700923 return fuse_reply_attr(fuse, hdr->unique, node, path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700924}
925
Jeff Brown6249b902012-05-26 14:32:54 -0700926static int handle_mknod(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700927 const struct fuse_in_header* hdr, const struct fuse_mknod_in* req, const char* name)
928{
Jeff Brown6249b902012-05-26 14:32:54 -0700929 struct node* parent_node;
930 char parent_path[PATH_MAX];
931 char child_path[PATH_MAX];
932 const char* actual_name;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700933
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700934 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700935 parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
936 parent_path, sizeof(parent_path));
Marcus Oaklande43b99a2014-07-23 13:04:59 +0100937 TRACE("[%d] MKNOD %s 0%o @ %"PRIx64" (%s)\n", handler->token,
Jeff Brown6249b902012-05-26 14:32:54 -0700938 name, req->mode, hdr->nodeid, parent_node ? parent_node->name : "?");
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700939 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700940
941 if (!parent_node || !(actual_name = find_file_within(parent_path, name,
942 child_path, sizeof(child_path), 1))) {
943 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700944 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700945 if (!check_caller_access_to_name(fuse, hdr, parent_node, name, W_OK)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700946 return -EACCES;
947 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700948 __u32 mode = (req->mode & (~0777)) | 0664;
Jeff Brown6249b902012-05-26 14:32:54 -0700949 if (mknod(child_path, mode, req->rdev) < 0) {
950 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700951 }
Jeff Brown6249b902012-05-26 14:32:54 -0700952 return fuse_reply_entry(fuse, hdr->unique, parent_node, name, actual_name, child_path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700953}
954
Jeff Brown6249b902012-05-26 14:32:54 -0700955static int handle_mkdir(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700956 const struct fuse_in_header* hdr, const struct fuse_mkdir_in* req, const char* name)
957{
Jeff Brown6249b902012-05-26 14:32:54 -0700958 struct node* parent_node;
959 char parent_path[PATH_MAX];
960 char child_path[PATH_MAX];
961 const char* actual_name;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700962
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700963 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700964 parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
965 parent_path, sizeof(parent_path));
Marcus Oaklande43b99a2014-07-23 13:04:59 +0100966 TRACE("[%d] MKDIR %s 0%o @ %"PRIx64" (%s)\n", handler->token,
Jeff Brown6249b902012-05-26 14:32:54 -0700967 name, req->mode, hdr->nodeid, parent_node ? parent_node->name : "?");
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700968 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700969
970 if (!parent_node || !(actual_name = find_file_within(parent_path, name,
971 child_path, sizeof(child_path), 1))) {
972 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700973 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700974 if (!check_caller_access_to_name(fuse, hdr, parent_node, name, W_OK)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700975 return -EACCES;
976 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700977 __u32 mode = (req->mode & (~0777)) | 0775;
Jeff Brown6249b902012-05-26 14:32:54 -0700978 if (mkdir(child_path, mode) < 0) {
979 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700980 }
Jeff Sharkey44d63422013-09-12 09:44:48 -0700981
982 /* When creating /Android/data and /Android/obb, mark them as .nomedia */
983 if (parent_node->perm == PERM_ANDROID && !strcasecmp(name, "data")) {
984 char nomedia[PATH_MAX];
985 snprintf(nomedia, PATH_MAX, "%s/.nomedia", child_path);
986 if (touch(nomedia, 0664) != 0) {
987 ERROR("Failed to touch(%s): %s\n", nomedia, strerror(errno));
988 return -ENOENT;
989 }
990 }
991 if (parent_node->perm == PERM_ANDROID && !strcasecmp(name, "obb")) {
992 char nomedia[PATH_MAX];
Jeff Sharkeyed2fe572015-07-16 09:13:52 -0700993 snprintf(nomedia, PATH_MAX, "%s/.nomedia", fuse->global->obb_path);
Jeff Sharkey44d63422013-09-12 09:44:48 -0700994 if (touch(nomedia, 0664) != 0) {
995 ERROR("Failed to touch(%s): %s\n", nomedia, strerror(errno));
996 return -ENOENT;
997 }
998 }
999
Jeff Brown6249b902012-05-26 14:32:54 -07001000 return fuse_reply_entry(fuse, hdr->unique, parent_node, name, actual_name, child_path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001001}
1002
Jeff Brown6249b902012-05-26 14:32:54 -07001003static int handle_unlink(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001004 const struct fuse_in_header* hdr, const char* name)
1005{
Jeff Brown6249b902012-05-26 14:32:54 -07001006 struct node* parent_node;
Krzysztof Adamskic5353122014-07-16 08:34:30 +02001007 struct node* child_node;
Jeff Brown6249b902012-05-26 14:32:54 -07001008 char parent_path[PATH_MAX];
1009 char child_path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001010
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001011 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -07001012 parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
1013 parent_path, sizeof(parent_path));
Marcus Oaklande43b99a2014-07-23 13:04:59 +01001014 TRACE("[%d] UNLINK %s @ %"PRIx64" (%s)\n", handler->token,
Jeff Brown6249b902012-05-26 14:32:54 -07001015 name, hdr->nodeid, parent_node ? parent_node->name : "?");
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001016 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -07001017
1018 if (!parent_node || !find_file_within(parent_path, name,
1019 child_path, sizeof(child_path), 1)) {
1020 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001021 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001022 if (!check_caller_access_to_name(fuse, hdr, parent_node, name, W_OK)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001023 return -EACCES;
1024 }
Jeff Brown6249b902012-05-26 14:32:54 -07001025 if (unlink(child_path) < 0) {
1026 return -errno;
1027 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001028 pthread_mutex_lock(&fuse->global->lock);
Krzysztof Adamskic5353122014-07-16 08:34:30 +02001029 child_node = lookup_child_by_name_locked(parent_node, name);
1030 if (child_node) {
1031 child_node->deleted = true;
1032 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001033 pthread_mutex_unlock(&fuse->global->lock);
Jeff Sharkeyed2fe572015-07-16 09:13:52 -07001034 if (parent_node && child_node) {
1035 /* Tell all other views that node is gone */
1036 TRACE("[%d] fuse_notify_delete parent=%"PRIx64", child=%"PRIx64", name=%s\n",
1037 handler->token, (uint64_t) parent_node->nid, (uint64_t) child_node->nid, name);
1038 if (fuse != fuse->global->fuse_default) {
1039 fuse_notify_delete(fuse->global->fuse_default, parent_node->nid, child_node->nid, name);
1040 }
1041 if (fuse != fuse->global->fuse_read) {
1042 fuse_notify_delete(fuse->global->fuse_read, parent_node->nid, child_node->nid, name);
1043 }
1044 if (fuse != fuse->global->fuse_write) {
1045 fuse_notify_delete(fuse->global->fuse_write, parent_node->nid, child_node->nid, name);
1046 }
1047 }
Jeff Brown6249b902012-05-26 14:32:54 -07001048 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001049}
1050
Jeff Brown6249b902012-05-26 14:32:54 -07001051static int handle_rmdir(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001052 const struct fuse_in_header* hdr, const char* name)
1053{
Krzysztof Adamskic5353122014-07-16 08:34:30 +02001054 struct node* child_node;
Jeff Brown6249b902012-05-26 14:32:54 -07001055 struct node* parent_node;
1056 char parent_path[PATH_MAX];
1057 char child_path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001058
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001059 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -07001060 parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
1061 parent_path, sizeof(parent_path));
Marcus Oaklande43b99a2014-07-23 13:04:59 +01001062 TRACE("[%d] RMDIR %s @ %"PRIx64" (%s)\n", handler->token,
Jeff Brown6249b902012-05-26 14:32:54 -07001063 name, hdr->nodeid, parent_node ? parent_node->name : "?");
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001064 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -07001065
1066 if (!parent_node || !find_file_within(parent_path, name,
1067 child_path, sizeof(child_path), 1)) {
1068 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001069 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001070 if (!check_caller_access_to_name(fuse, hdr, parent_node, name, W_OK)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001071 return -EACCES;
1072 }
Jeff Brown6249b902012-05-26 14:32:54 -07001073 if (rmdir(child_path) < 0) {
1074 return -errno;
1075 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001076 pthread_mutex_lock(&fuse->global->lock);
Krzysztof Adamskic5353122014-07-16 08:34:30 +02001077 child_node = lookup_child_by_name_locked(parent_node, name);
1078 if (child_node) {
1079 child_node->deleted = true;
1080 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001081 pthread_mutex_unlock(&fuse->global->lock);
Jeff Sharkeyed2fe572015-07-16 09:13:52 -07001082 if (parent_node && child_node) {
1083 /* Tell all other views that node is gone */
1084 TRACE("[%d] fuse_notify_delete parent=%"PRIx64", child=%"PRIx64", name=%s\n",
1085 handler->token, (uint64_t) parent_node->nid, (uint64_t) child_node->nid, name);
1086 if (fuse != fuse->global->fuse_default) {
1087 fuse_notify_delete(fuse->global->fuse_default, parent_node->nid, child_node->nid, name);
1088 }
1089 if (fuse != fuse->global->fuse_read) {
1090 fuse_notify_delete(fuse->global->fuse_read, parent_node->nid, child_node->nid, name);
1091 }
1092 if (fuse != fuse->global->fuse_write) {
1093 fuse_notify_delete(fuse->global->fuse_write, parent_node->nid, child_node->nid, name);
1094 }
1095 }
Jeff Brown6249b902012-05-26 14:32:54 -07001096 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001097}
1098
Jeff Brown6249b902012-05-26 14:32:54 -07001099static int handle_rename(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001100 const struct fuse_in_header* hdr, const struct fuse_rename_in* req,
Jeff Brown6249b902012-05-26 14:32:54 -07001101 const char* old_name, const char* new_name)
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001102{
Jeff Brown6249b902012-05-26 14:32:54 -07001103 struct node* old_parent_node;
1104 struct node* new_parent_node;
1105 struct node* child_node;
1106 char old_parent_path[PATH_MAX];
1107 char new_parent_path[PATH_MAX];
1108 char old_child_path[PATH_MAX];
1109 char new_child_path[PATH_MAX];
1110 const char* new_actual_name;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001111 int res;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001112
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001113 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -07001114 old_parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
1115 old_parent_path, sizeof(old_parent_path));
1116 new_parent_node = lookup_node_and_path_by_id_locked(fuse, req->newdir,
1117 new_parent_path, sizeof(new_parent_path));
Marcus Oaklande43b99a2014-07-23 13:04:59 +01001118 TRACE("[%d] RENAME %s->%s @ %"PRIx64" (%s) -> %"PRIx64" (%s)\n", handler->token,
Jeff Brown6249b902012-05-26 14:32:54 -07001119 old_name, new_name,
1120 hdr->nodeid, old_parent_node ? old_parent_node->name : "?",
1121 req->newdir, new_parent_node ? new_parent_node->name : "?");
1122 if (!old_parent_node || !new_parent_node) {
1123 res = -ENOENT;
1124 goto lookup_error;
1125 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001126 if (!check_caller_access_to_name(fuse, hdr, old_parent_node, old_name, W_OK)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001127 res = -EACCES;
1128 goto lookup_error;
1129 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001130 if (!check_caller_access_to_name(fuse, hdr, new_parent_node, new_name, W_OK)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001131 res = -EACCES;
1132 goto lookup_error;
1133 }
Jeff Brown6249b902012-05-26 14:32:54 -07001134 child_node = lookup_child_by_name_locked(old_parent_node, old_name);
1135 if (!child_node || get_node_path_locked(child_node,
1136 old_child_path, sizeof(old_child_path)) < 0) {
1137 res = -ENOENT;
1138 goto lookup_error;
1139 }
1140 acquire_node_locked(child_node);
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001141 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -07001142
1143 /* Special case for renaming a file where destination is same path
1144 * differing only by case. In this case we don't want to look for a case
1145 * insensitive match. This allows commands like "mv foo FOO" to work as expected.
1146 */
1147 int search = old_parent_node != new_parent_node
1148 || strcasecmp(old_name, new_name);
1149 if (!(new_actual_name = find_file_within(new_parent_path, new_name,
1150 new_child_path, sizeof(new_child_path), search))) {
1151 res = -ENOENT;
1152 goto io_error;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001153 }
1154
Jeff Brown6249b902012-05-26 14:32:54 -07001155 TRACE("[%d] RENAME %s->%s\n", handler->token, old_child_path, new_child_path);
1156 res = rename(old_child_path, new_child_path);
1157 if (res < 0) {
1158 res = -errno;
1159 goto io_error;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001160 }
1161
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001162 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -07001163 res = rename_node_locked(child_node, new_name, new_actual_name);
1164 if (!res) {
1165 remove_node_from_parent_locked(child_node);
Jeff Sharkeyfe764612015-12-14 11:02:01 -07001166 derive_permissions_locked(fuse, new_parent_node, child_node);
1167 derive_permissions_recursive_locked(fuse, child_node);
Jeff Brown6249b902012-05-26 14:32:54 -07001168 add_node_to_parent_locked(child_node, new_parent_node);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001169 }
Jeff Brown6249b902012-05-26 14:32:54 -07001170 goto done;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001171
Jeff Brown6249b902012-05-26 14:32:54 -07001172io_error:
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001173 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -07001174done:
1175 release_node_locked(child_node);
1176lookup_error:
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001177 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -07001178 return res;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001179}
1180
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001181static int open_flags_to_access_mode(int open_flags) {
1182 if ((open_flags & O_ACCMODE) == O_RDONLY) {
1183 return R_OK;
1184 } else if ((open_flags & O_ACCMODE) == O_WRONLY) {
1185 return W_OK;
1186 } else {
1187 /* Probably O_RDRW, but treat as default to be safe */
1188 return R_OK | W_OK;
1189 }
1190}
1191
Jeff Brown6249b902012-05-26 14:32:54 -07001192static int handle_open(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001193 const struct fuse_in_header* hdr, const struct fuse_open_in* req)
1194{
Jeff Brown6249b902012-05-26 14:32:54 -07001195 struct node* node;
1196 char path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001197 struct fuse_open_out out;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001198 struct handle *h;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001199
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001200 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -07001201 node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid, path, sizeof(path));
Marcus Oaklande43b99a2014-07-23 13:04:59 +01001202 TRACE("[%d] OPEN 0%o @ %"PRIx64" (%s)\n", handler->token,
Jeff Brown6249b902012-05-26 14:32:54 -07001203 req->flags, hdr->nodeid, node ? node->name : "?");
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001204 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -07001205
1206 if (!node) {
1207 return -ENOENT;
1208 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001209 if (!check_caller_access_to_node(fuse, hdr, node,
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001210 open_flags_to_access_mode(req->flags))) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001211 return -EACCES;
1212 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001213 h = malloc(sizeof(*h));
1214 if (!h) {
Jeff Brown6249b902012-05-26 14:32:54 -07001215 return -ENOMEM;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001216 }
Jeff Brown6249b902012-05-26 14:32:54 -07001217 TRACE("[%d] OPEN %s\n", handler->token, path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001218 h->fd = open(path, req->flags);
1219 if (h->fd < 0) {
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001220 free(h);
Jeff Brown6249b902012-05-26 14:32:54 -07001221 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001222 }
1223 out.fh = ptr_to_id(h);
1224 out.open_flags = 0;
1225 out.padding = 0;
1226 fuse_reply(fuse, hdr->unique, &out, sizeof(out));
Jeff Brown6249b902012-05-26 14:32:54 -07001227 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001228}
1229
Jeff Brown6249b902012-05-26 14:32:54 -07001230static int handle_read(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001231 const struct fuse_in_header* hdr, const struct fuse_read_in* req)
1232{
1233 struct handle *h = id_to_ptr(req->fh);
1234 __u64 unique = hdr->unique;
1235 __u32 size = req->size;
1236 __u64 offset = req->offset;
Jeff Brown6249b902012-05-26 14:32:54 -07001237 int res;
Elliott Hughese24e9a52015-07-28 16:36:47 -07001238 __u8 *read_buffer = (__u8 *) ((uintptr_t)(handler->read_buffer + PAGE_SIZE) & ~((uintptr_t)PAGE_SIZE-1));
Jeff Brown6249b902012-05-26 14:32:54 -07001239
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001240 /* Don't access any other fields of hdr or req beyond this point, the read buffer
1241 * overlaps the request buffer and will clobber data in the request. This
1242 * saves us 128KB per request handler thread at the cost of this scary comment. */
Jeff Brown6249b902012-05-26 14:32:54 -07001243
Marcus Oaklande43b99a2014-07-23 13:04:59 +01001244 TRACE("[%d] READ %p(%d) %u@%"PRIu64"\n", handler->token,
1245 h, h->fd, size, (uint64_t) offset);
Arpad Horvath80b435a2014-02-14 16:42:27 -08001246 if (size > MAX_READ) {
Jeff Brown6249b902012-05-26 14:32:54 -07001247 return -EINVAL;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001248 }
Arpad Horvath80b435a2014-02-14 16:42:27 -08001249 res = pread64(h->fd, read_buffer, size, offset);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001250 if (res < 0) {
Jeff Brown6249b902012-05-26 14:32:54 -07001251 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001252 }
Arpad Horvath80b435a2014-02-14 16:42:27 -08001253 fuse_reply(fuse, unique, read_buffer, res);
Jeff Brown6249b902012-05-26 14:32:54 -07001254 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001255}
1256
Jeff Brown6249b902012-05-26 14:32:54 -07001257static int handle_write(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001258 const struct fuse_in_header* hdr, const struct fuse_write_in* req,
1259 const void* buffer)
1260{
1261 struct fuse_write_out out;
1262 struct handle *h = id_to_ptr(req->fh);
1263 int res;
Elliott Hughese24e9a52015-07-28 16:36:47 -07001264 __u8 aligned_buffer[req->size] __attribute__((__aligned__(PAGE_SIZE)));
Elliott Hughes60281d52014-05-07 14:39:58 -07001265
Arpad Horvath49e93442014-02-18 10:18:25 +01001266 if (req->flags & O_DIRECT) {
1267 memcpy(aligned_buffer, buffer, req->size);
1268 buffer = (const __u8*) aligned_buffer;
1269 }
Jeff Brown6249b902012-05-26 14:32:54 -07001270
Marcus Oaklande43b99a2014-07-23 13:04:59 +01001271 TRACE("[%d] WRITE %p(%d) %u@%"PRIu64"\n", handler->token,
Jeff Brown6249b902012-05-26 14:32:54 -07001272 h, h->fd, req->size, req->offset);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001273 res = pwrite64(h->fd, buffer, req->size, req->offset);
1274 if (res < 0) {
Jeff Brown6249b902012-05-26 14:32:54 -07001275 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001276 }
1277 out.size = res;
Daisuke Okitsu19ec8862013-08-05 12:18:15 +09001278 out.padding = 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001279 fuse_reply(fuse, hdr->unique, &out, sizeof(out));
Jeff Brown6249b902012-05-26 14:32:54 -07001280 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001281}
1282
Jeff Brown6249b902012-05-26 14:32:54 -07001283static int handle_statfs(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001284 const struct fuse_in_header* hdr)
1285{
Jeff Brown6249b902012-05-26 14:32:54 -07001286 char path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001287 struct statfs stat;
1288 struct fuse_statfs_out out;
1289 int res;
1290
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001291 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -07001292 TRACE("[%d] STATFS\n", handler->token);
Jeff Sharkeyed2fe572015-07-16 09:13:52 -07001293 res = get_node_path_locked(&fuse->global->root, path, sizeof(path));
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001294 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -07001295 if (res < 0) {
1296 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001297 }
Jeff Sharkeyed2fe572015-07-16 09:13:52 -07001298 if (statfs(fuse->global->root.name, &stat) < 0) {
Jeff Brown6249b902012-05-26 14:32:54 -07001299 return -errno;
1300 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001301 memset(&out, 0, sizeof(out));
1302 out.st.blocks = stat.f_blocks;
1303 out.st.bfree = stat.f_bfree;
1304 out.st.bavail = stat.f_bavail;
1305 out.st.files = stat.f_files;
1306 out.st.ffree = stat.f_ffree;
1307 out.st.bsize = stat.f_bsize;
1308 out.st.namelen = stat.f_namelen;
1309 out.st.frsize = stat.f_frsize;
1310 fuse_reply(fuse, hdr->unique, &out, sizeof(out));
Jeff Brown6249b902012-05-26 14:32:54 -07001311 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001312}
1313
Jeff Brown6249b902012-05-26 14:32:54 -07001314static int handle_release(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001315 const struct fuse_in_header* hdr, const struct fuse_release_in* req)
1316{
1317 struct handle *h = id_to_ptr(req->fh);
Jeff Brown6249b902012-05-26 14:32:54 -07001318
1319 TRACE("[%d] RELEASE %p(%d)\n", handler->token, h, h->fd);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001320 close(h->fd);
1321 free(h);
Jeff Brown6249b902012-05-26 14:32:54 -07001322 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001323}
1324
Jeff Brown6249b902012-05-26 14:32:54 -07001325static int handle_fsync(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001326 const struct fuse_in_header* hdr, const struct fuse_fsync_in* req)
1327{
Elliott Hughesf6d67372014-07-08 14:38:26 -07001328 bool is_dir = (hdr->opcode == FUSE_FSYNCDIR);
1329 bool is_data_sync = req->fsync_flags & 1;
Jeff Brown6249b902012-05-26 14:32:54 -07001330
Elliott Hughesf6d67372014-07-08 14:38:26 -07001331 int fd = -1;
1332 if (is_dir) {
1333 struct dirhandle *dh = id_to_ptr(req->fh);
1334 fd = dirfd(dh->d);
1335 } else {
1336 struct handle *h = id_to_ptr(req->fh);
1337 fd = h->fd;
1338 }
1339
1340 TRACE("[%d] %s %p(%d) is_data_sync=%d\n", handler->token,
1341 is_dir ? "FSYNCDIR" : "FSYNC",
1342 id_to_ptr(req->fh), fd, is_data_sync);
1343 int res = is_data_sync ? fdatasync(fd) : fsync(fd);
1344 if (res == -1) {
Jeff Brown6249b902012-05-26 14:32:54 -07001345 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001346 }
Jeff Brown6249b902012-05-26 14:32:54 -07001347 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001348}
1349
Jeff Brown6249b902012-05-26 14:32:54 -07001350static int handle_flush(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001351 const struct fuse_in_header* hdr)
1352{
Jeff Brown6249b902012-05-26 14:32:54 -07001353 TRACE("[%d] FLUSH\n", handler->token);
1354 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001355}
1356
Jeff Brown6249b902012-05-26 14:32:54 -07001357static int handle_opendir(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001358 const struct fuse_in_header* hdr, const struct fuse_open_in* req)
1359{
Jeff Brown6249b902012-05-26 14:32:54 -07001360 struct node* node;
1361 char path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001362 struct fuse_open_out out;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001363 struct dirhandle *h;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001364
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001365 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -07001366 node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid, path, sizeof(path));
Marcus Oaklande43b99a2014-07-23 13:04:59 +01001367 TRACE("[%d] OPENDIR @ %"PRIx64" (%s)\n", handler->token,
Jeff Brown6249b902012-05-26 14:32:54 -07001368 hdr->nodeid, node ? node->name : "?");
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001369 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -07001370
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001371 if (!node) {
Jeff Brown6249b902012-05-26 14:32:54 -07001372 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001373 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001374 if (!check_caller_access_to_node(fuse, hdr, node, R_OK)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001375 return -EACCES;
1376 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001377 h = malloc(sizeof(*h));
1378 if (!h) {
Jeff Brown6249b902012-05-26 14:32:54 -07001379 return -ENOMEM;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001380 }
Jeff Brown6249b902012-05-26 14:32:54 -07001381 TRACE("[%d] OPENDIR %s\n", handler->token, path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001382 h->d = opendir(path);
Jeff Brown6249b902012-05-26 14:32:54 -07001383 if (!h->d) {
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001384 free(h);
Jeff Brown6249b902012-05-26 14:32:54 -07001385 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001386 }
1387 out.fh = ptr_to_id(h);
Ken Sumrall3a876882013-08-14 20:02:13 -07001388 out.open_flags = 0;
1389 out.padding = 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001390 fuse_reply(fuse, hdr->unique, &out, sizeof(out));
Jeff Brown6249b902012-05-26 14:32:54 -07001391 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001392}
1393
Jeff Brown6249b902012-05-26 14:32:54 -07001394static int handle_readdir(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001395 const struct fuse_in_header* hdr, const struct fuse_read_in* req)
1396{
1397 char buffer[8192];
1398 struct fuse_dirent *fde = (struct fuse_dirent*) buffer;
1399 struct dirent *de;
1400 struct dirhandle *h = id_to_ptr(req->fh);
Jeff Brown6249b902012-05-26 14:32:54 -07001401
1402 TRACE("[%d] READDIR %p\n", handler->token, h);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001403 if (req->offset == 0) {
1404 /* rewinddir() might have been called above us, so rewind here too */
Jeff Brown6249b902012-05-26 14:32:54 -07001405 TRACE("[%d] calling rewinddir()\n", handler->token);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001406 rewinddir(h->d);
1407 }
1408 de = readdir(h->d);
1409 if (!de) {
Jeff Brown6249b902012-05-26 14:32:54 -07001410 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001411 }
1412 fde->ino = FUSE_UNKNOWN_INO;
1413 /* increment the offset so we can detect when rewinddir() seeks back to the beginning */
1414 fde->off = req->offset + 1;
1415 fde->type = de->d_type;
1416 fde->namelen = strlen(de->d_name);
1417 memcpy(fde->name, de->d_name, fde->namelen + 1);
1418 fuse_reply(fuse, hdr->unique, fde,
Jeff Brown6249b902012-05-26 14:32:54 -07001419 FUSE_DIRENT_ALIGN(sizeof(struct fuse_dirent) + fde->namelen));
1420 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001421}
1422
Jeff Brown6249b902012-05-26 14:32:54 -07001423static int handle_releasedir(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001424 const struct fuse_in_header* hdr, const struct fuse_release_in* req)
1425{
1426 struct dirhandle *h = id_to_ptr(req->fh);
Jeff Brown6249b902012-05-26 14:32:54 -07001427
1428 TRACE("[%d] RELEASEDIR %p\n", handler->token, h);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001429 closedir(h->d);
1430 free(h);
Jeff Brown6249b902012-05-26 14:32:54 -07001431 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001432}
1433
Jeff Brown6249b902012-05-26 14:32:54 -07001434static int handle_init(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001435 const struct fuse_in_header* hdr, const struct fuse_init_in* req)
1436{
1437 struct fuse_init_out out;
Christopher Ferrisff649ea2014-09-13 13:53:08 -07001438 size_t fuse_struct_size;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001439
Jeff Brown6249b902012-05-26 14:32:54 -07001440 TRACE("[%d] INIT ver=%d.%d maxread=%d flags=%x\n",
1441 handler->token, req->major, req->minor, req->max_readahead, req->flags);
Christopher Ferrisff649ea2014-09-13 13:53:08 -07001442
1443 /* Kernel 2.6.16 is the first stable kernel with struct fuse_init_out
1444 * defined (fuse version 7.6). The structure is the same from 7.6 through
1445 * 7.22. Beginning with 7.23, the structure increased in size and added
1446 * new parameters.
1447 */
1448 if (req->major != FUSE_KERNEL_VERSION || req->minor < 6) {
1449 ERROR("Fuse kernel version mismatch: Kernel version %d.%d, Expected at least %d.6",
1450 req->major, req->minor, FUSE_KERNEL_VERSION);
1451 return -1;
1452 }
1453
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001454 /* We limit ourselves to 15 because we don't handle BATCH_FORGET yet */
1455 out.minor = MIN(req->minor, 15);
Christopher Ferrisff649ea2014-09-13 13:53:08 -07001456 fuse_struct_size = sizeof(out);
1457#if defined(FUSE_COMPAT_22_INIT_OUT_SIZE)
1458 /* FUSE_KERNEL_VERSION >= 23. */
1459
1460 /* If the kernel only works on minor revs older than or equal to 22,
1461 * then use the older structure size since this code only uses the 7.22
1462 * version of the structure. */
1463 if (req->minor <= 22) {
1464 fuse_struct_size = FUSE_COMPAT_22_INIT_OUT_SIZE;
1465 }
1466#endif
1467
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001468 out.major = FUSE_KERNEL_VERSION;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001469 out.max_readahead = req->max_readahead;
1470 out.flags = FUSE_ATOMIC_O_TRUNC | FUSE_BIG_WRITES;
1471 out.max_background = 32;
1472 out.congestion_threshold = 32;
1473 out.max_write = MAX_WRITE;
Christopher Ferrisff649ea2014-09-13 13:53:08 -07001474 fuse_reply(fuse, hdr->unique, &out, fuse_struct_size);
Jeff Brown6249b902012-05-26 14:32:54 -07001475 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001476}
1477
Daniel Rosenberg2abee9e2016-04-19 18:33:08 -07001478static int handle_canonical_path(struct fuse* fuse, struct fuse_handler* handler,
1479 const struct fuse_in_header *hdr)
1480{
1481 struct node* node;
1482 char path[PATH_MAX];
1483 int len;
1484
1485 pthread_mutex_lock(&fuse->global->lock);
1486 node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
1487 path, sizeof(path));
1488 TRACE("[%d] CANONICAL_PATH @ %" PRIx64 " (%s)\n", handler->token, hdr->nodeid,
1489 node ? node->name : "?");
1490 pthread_mutex_unlock(&fuse->global->lock);
1491
1492 if (!node) {
1493 return -ENOENT;
1494 }
1495 if (!check_caller_access_to_node(fuse, hdr, node, R_OK)) {
1496 return -EACCES;
1497 }
1498 len = strlen(path);
1499 if (len + 1 > PATH_MAX)
1500 len = PATH_MAX - 1;
1501 path[PATH_MAX - 1] = 0;
1502 fuse_reply(fuse, hdr->unique, path, len + 1);
1503 return NO_STATUS;
1504}
1505
1506
Jeff Brown6249b902012-05-26 14:32:54 -07001507static int handle_fuse_request(struct fuse *fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001508 const struct fuse_in_header *hdr, const void *data, size_t data_len)
1509{
Brian Swetland03ee9472010-08-12 18:01:08 -07001510 switch (hdr->opcode) {
1511 case FUSE_LOOKUP: { /* bytez[] -> entry_out */
Jeff Brown84715842012-05-25 14:07:47 -07001512 const char* name = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001513 return handle_lookup(fuse, handler, hdr, name);
Brian Swetland03ee9472010-08-12 18:01:08 -07001514 }
Jeff Brown84715842012-05-25 14:07:47 -07001515
Brian Swetland03ee9472010-08-12 18:01:08 -07001516 case FUSE_FORGET: {
Jeff Brown84715842012-05-25 14:07:47 -07001517 const struct fuse_forget_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001518 return handle_forget(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001519 }
Jeff Brown84715842012-05-25 14:07:47 -07001520
Brian Swetland03ee9472010-08-12 18:01:08 -07001521 case FUSE_GETATTR: { /* getattr_in -> attr_out */
Jeff Brown84715842012-05-25 14:07:47 -07001522 const struct fuse_getattr_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001523 return handle_getattr(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001524 }
Jeff Brown84715842012-05-25 14:07:47 -07001525
Brian Swetland03ee9472010-08-12 18:01:08 -07001526 case FUSE_SETATTR: { /* setattr_in -> attr_out */
Jeff Brown84715842012-05-25 14:07:47 -07001527 const struct fuse_setattr_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001528 return handle_setattr(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001529 }
Jeff Brown84715842012-05-25 14:07:47 -07001530
Brian Swetland03ee9472010-08-12 18:01:08 -07001531// case FUSE_READLINK:
1532// case FUSE_SYMLINK:
1533 case FUSE_MKNOD: { /* mknod_in, bytez[] -> entry_out */
Jeff Brown84715842012-05-25 14:07:47 -07001534 const struct fuse_mknod_in *req = data;
1535 const char *name = ((const char*) data) + sizeof(*req);
Jeff Brown6249b902012-05-26 14:32:54 -07001536 return handle_mknod(fuse, handler, hdr, req, name);
Brian Swetland03ee9472010-08-12 18:01:08 -07001537 }
Jeff Brown84715842012-05-25 14:07:47 -07001538
Brian Swetland03ee9472010-08-12 18:01:08 -07001539 case FUSE_MKDIR: { /* mkdir_in, bytez[] -> entry_out */
Jeff Brown84715842012-05-25 14:07:47 -07001540 const struct fuse_mkdir_in *req = data;
1541 const char *name = ((const char*) data) + sizeof(*req);
Jeff Brown6249b902012-05-26 14:32:54 -07001542 return handle_mkdir(fuse, handler, hdr, req, name);
Brian Swetland03ee9472010-08-12 18:01:08 -07001543 }
Jeff Brown84715842012-05-25 14:07:47 -07001544
Brian Swetland03ee9472010-08-12 18:01:08 -07001545 case FUSE_UNLINK: { /* bytez[] -> */
Jeff Brown84715842012-05-25 14:07:47 -07001546 const char* name = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001547 return handle_unlink(fuse, handler, hdr, name);
Brian Swetland03ee9472010-08-12 18:01:08 -07001548 }
Jeff Brown84715842012-05-25 14:07:47 -07001549
Brian Swetland03ee9472010-08-12 18:01:08 -07001550 case FUSE_RMDIR: { /* bytez[] -> */
Jeff Brown84715842012-05-25 14:07:47 -07001551 const char* name = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001552 return handle_rmdir(fuse, handler, hdr, name);
Brian Swetland03ee9472010-08-12 18:01:08 -07001553 }
Jeff Brown84715842012-05-25 14:07:47 -07001554
Brian Swetland03ee9472010-08-12 18:01:08 -07001555 case FUSE_RENAME: { /* rename_in, oldname, newname -> */
Jeff Brown84715842012-05-25 14:07:47 -07001556 const struct fuse_rename_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001557 const char *old_name = ((const char*) data) + sizeof(*req);
1558 const char *new_name = old_name + strlen(old_name) + 1;
1559 return handle_rename(fuse, handler, hdr, req, old_name, new_name);
Brian Swetland03ee9472010-08-12 18:01:08 -07001560 }
Jeff Brown84715842012-05-25 14:07:47 -07001561
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001562// case FUSE_LINK:
Brian Swetland03ee9472010-08-12 18:01:08 -07001563 case FUSE_OPEN: { /* open_in -> open_out */
Jeff Brown84715842012-05-25 14:07:47 -07001564 const struct fuse_open_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001565 return handle_open(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001566 }
Jeff Brown84715842012-05-25 14:07:47 -07001567
Brian Swetland03ee9472010-08-12 18:01:08 -07001568 case FUSE_READ: { /* read_in -> byte[] */
Jeff Brown84715842012-05-25 14:07:47 -07001569 const struct fuse_read_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001570 return handle_read(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001571 }
Jeff Brown84715842012-05-25 14:07:47 -07001572
Brian Swetland03ee9472010-08-12 18:01:08 -07001573 case FUSE_WRITE: { /* write_in, byte[write_in.size] -> write_out */
Jeff Brown84715842012-05-25 14:07:47 -07001574 const struct fuse_write_in *req = data;
1575 const void* buffer = (const __u8*)data + sizeof(*req);
Jeff Brown6249b902012-05-26 14:32:54 -07001576 return handle_write(fuse, handler, hdr, req, buffer);
Brian Swetland03ee9472010-08-12 18:01:08 -07001577 }
Jeff Brown84715842012-05-25 14:07:47 -07001578
Mike Lockwood4553b082010-08-16 14:14:44 -04001579 case FUSE_STATFS: { /* getattr_in -> attr_out */
Jeff Brown6249b902012-05-26 14:32:54 -07001580 return handle_statfs(fuse, handler, hdr);
Mike Lockwood4553b082010-08-16 14:14:44 -04001581 }
Jeff Brown84715842012-05-25 14:07:47 -07001582
Brian Swetland03ee9472010-08-12 18:01:08 -07001583 case FUSE_RELEASE: { /* release_in -> */
Jeff Brown84715842012-05-25 14:07:47 -07001584 const struct fuse_release_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001585 return handle_release(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001586 }
Jeff Brown84715842012-05-25 14:07:47 -07001587
Daisuke Okitsub2831a22014-02-17 10:33:11 +01001588 case FUSE_FSYNC:
1589 case FUSE_FSYNCDIR: {
Jeff Brown6fd921a2012-05-25 15:01:21 -07001590 const struct fuse_fsync_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001591 return handle_fsync(fuse, handler, hdr, req);
Jeff Brown6fd921a2012-05-25 15:01:21 -07001592 }
1593
Brian Swetland03ee9472010-08-12 18:01:08 -07001594// case FUSE_SETXATTR:
1595// case FUSE_GETXATTR:
1596// case FUSE_LISTXATTR:
1597// case FUSE_REMOVEXATTR:
Jeff Brown84715842012-05-25 14:07:47 -07001598 case FUSE_FLUSH: {
Jeff Brown6249b902012-05-26 14:32:54 -07001599 return handle_flush(fuse, handler, hdr);
Jeff Brown84715842012-05-25 14:07:47 -07001600 }
1601
Brian Swetland03ee9472010-08-12 18:01:08 -07001602 case FUSE_OPENDIR: { /* open_in -> open_out */
Jeff Brown84715842012-05-25 14:07:47 -07001603 const struct fuse_open_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001604 return handle_opendir(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001605 }
Jeff Brown84715842012-05-25 14:07:47 -07001606
Brian Swetland03ee9472010-08-12 18:01:08 -07001607 case FUSE_READDIR: {
Jeff Brown84715842012-05-25 14:07:47 -07001608 const struct fuse_read_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001609 return handle_readdir(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001610 }
Jeff Brown84715842012-05-25 14:07:47 -07001611
Brian Swetland03ee9472010-08-12 18:01:08 -07001612 case FUSE_RELEASEDIR: { /* release_in -> */
Jeff Brown84715842012-05-25 14:07:47 -07001613 const struct fuse_release_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001614 return handle_releasedir(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001615 }
Jeff Brown84715842012-05-25 14:07:47 -07001616
Brian Swetland03ee9472010-08-12 18:01:08 -07001617 case FUSE_INIT: { /* init_in -> init_out */
Jeff Brown84715842012-05-25 14:07:47 -07001618 const struct fuse_init_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001619 return handle_init(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001620 }
Jeff Brown84715842012-05-25 14:07:47 -07001621
Daniel Rosenberg2abee9e2016-04-19 18:33:08 -07001622 case FUSE_CANONICAL_PATH: { /* nodeid -> bytez[] */
1623 return handle_canonical_path(fuse, handler, hdr);
1624 }
1625
Brian Swetland03ee9472010-08-12 18:01:08 -07001626 default: {
Marcus Oaklande43b99a2014-07-23 13:04:59 +01001627 TRACE("[%d] NOTIMPL op=%d uniq=%"PRIx64" nid=%"PRIx64"\n",
Jeff Brown6249b902012-05-26 14:32:54 -07001628 handler->token, hdr->opcode, hdr->unique, hdr->nodeid);
1629 return -ENOSYS;
Brian Swetland03ee9472010-08-12 18:01:08 -07001630 }
Jeff Brown84715842012-05-25 14:07:47 -07001631 }
Brian Swetland03ee9472010-08-12 18:01:08 -07001632}
1633
Jeff Brown6249b902012-05-26 14:32:54 -07001634static void handle_fuse_requests(struct fuse_handler* handler)
Brian Swetland03ee9472010-08-12 18:01:08 -07001635{
Jeff Brown6249b902012-05-26 14:32:54 -07001636 struct fuse* fuse = handler->fuse;
Brian Swetland03ee9472010-08-12 18:01:08 -07001637 for (;;) {
Mark Salyzyn6b6c1bd2015-07-06 10:00:36 -07001638 ssize_t len = TEMP_FAILURE_RETRY(read(fuse->fd,
1639 handler->request_buffer, sizeof(handler->request_buffer)));
Brian Swetland03ee9472010-08-12 18:01:08 -07001640 if (len < 0) {
Jeff Sharkey4a485812015-06-30 16:02:40 -07001641 if (errno == ENODEV) {
1642 ERROR("[%d] someone stole our marbles!\n", handler->token);
1643 exit(2);
1644 }
Mark Salyzyn6b6c1bd2015-07-06 10:00:36 -07001645 ERROR("[%d] handle_fuse_requests: errno=%d\n", handler->token, errno);
Jeff Brown6249b902012-05-26 14:32:54 -07001646 continue;
Brian Swetland03ee9472010-08-12 18:01:08 -07001647 }
Jeff Brown84715842012-05-25 14:07:47 -07001648
1649 if ((size_t)len < sizeof(struct fuse_in_header)) {
Jeff Brown6249b902012-05-26 14:32:54 -07001650 ERROR("[%d] request too short: len=%zu\n", handler->token, (size_t)len);
1651 continue;
Jeff Brown84715842012-05-25 14:07:47 -07001652 }
1653
Jeff Brown7729d242012-05-25 15:35:28 -07001654 const struct fuse_in_header *hdr = (void*)handler->request_buffer;
Jeff Brown84715842012-05-25 14:07:47 -07001655 if (hdr->len != (size_t)len) {
Jeff Brown6249b902012-05-26 14:32:54 -07001656 ERROR("[%d] malformed header: len=%zu, hdr->len=%u\n",
1657 handler->token, (size_t)len, hdr->len);
1658 continue;
Jeff Brown84715842012-05-25 14:07:47 -07001659 }
1660
Jeff Brown7729d242012-05-25 15:35:28 -07001661 const void *data = handler->request_buffer + sizeof(struct fuse_in_header);
Jeff Brown84715842012-05-25 14:07:47 -07001662 size_t data_len = len - sizeof(struct fuse_in_header);
Jeff Brown6249b902012-05-26 14:32:54 -07001663 __u64 unique = hdr->unique;
1664 int res = handle_fuse_request(fuse, handler, hdr, data, data_len);
Jeff Brown7729d242012-05-25 15:35:28 -07001665
1666 /* We do not access the request again after this point because the underlying
1667 * buffer storage may have been reused while processing the request. */
Jeff Brown6249b902012-05-26 14:32:54 -07001668
1669 if (res != NO_STATUS) {
1670 if (res) {
1671 TRACE("[%d] ERROR %d\n", handler->token, res);
1672 }
1673 fuse_status(fuse, unique, res);
1674 }
Brian Swetland03ee9472010-08-12 18:01:08 -07001675 }
1676}
1677
Jeff Brown6249b902012-05-26 14:32:54 -07001678static void* start_handler(void* data)
Jeff Brown7729d242012-05-25 15:35:28 -07001679{
Jeff Brown6249b902012-05-26 14:32:54 -07001680 struct fuse_handler* handler = data;
1681 handle_fuse_requests(handler);
1682 return NULL;
1683}
1684
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001685static bool remove_str_to_int(void *key, void *value, void *context) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001686 Hashmap* map = context;
1687 hashmapRemove(map, key);
1688 free(key);
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001689 return true;
1690}
1691
William Robertse5099802015-07-31 13:11:19 -07001692static bool package_parse_callback(pkg_info *info, void *userdata) {
1693 struct fuse_global *global = (struct fuse_global *)userdata;
1694
1695 char* name = strdup(info->name);
1696 hashmapPut(global->package_to_appid, name, (void*) (uintptr_t) info->uid);
1697 packagelist_free(info);
1698 return true;
1699}
1700
1701static bool read_package_list(struct fuse_global* global) {
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001702 pthread_mutex_lock(&global->lock);
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001703
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001704 hashmapForEach(global->package_to_appid, remove_str_to_int, global->package_to_appid);
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001705
William Robertse5099802015-07-31 13:11:19 -07001706 bool rc = packagelist_parse(package_parse_callback, global);
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001707 TRACE("read_package_list: found %zu packages\n",
1708 hashmapSize(global->package_to_appid));
William Robertse5099802015-07-31 13:11:19 -07001709
Jeff Sharkeyfe764612015-12-14 11:02:01 -07001710 /* Regenerate ownership details using newly loaded mapping */
1711 derive_permissions_recursive_locked(global->fuse_default, &global->root);
1712
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001713 pthread_mutex_unlock(&global->lock);
William Robertse5099802015-07-31 13:11:19 -07001714
1715 return rc;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001716}
1717
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001718static void watch_package_list(struct fuse_global* global) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001719 struct inotify_event *event;
1720 char event_buf[512];
1721
1722 int nfd = inotify_init();
1723 if (nfd < 0) {
1724 ERROR("inotify_init failed: %s\n", strerror(errno));
1725 return;
1726 }
1727
1728 bool active = false;
1729 while (1) {
1730 if (!active) {
William Robertse5099802015-07-31 13:11:19 -07001731 int res = inotify_add_watch(nfd, PACKAGES_LIST_FILE, IN_DELETE_SELF);
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001732 if (res == -1) {
1733 if (errno == ENOENT || errno == EACCES) {
1734 /* Framework may not have created yet, sleep and retry */
William Robertse5099802015-07-31 13:11:19 -07001735 ERROR("missing \"%s\"; retrying\n", PACKAGES_LIST_FILE);
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001736 sleep(3);
1737 continue;
1738 } else {
1739 ERROR("inotify_add_watch failed: %s\n", strerror(errno));
1740 return;
1741 }
1742 }
1743
1744 /* Watch above will tell us about any future changes, so
1745 * read the current state. */
William Robertse5099802015-07-31 13:11:19 -07001746 if (read_package_list(global) == false) {
1747 ERROR("read_package_list failed\n");
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001748 return;
1749 }
1750 active = true;
1751 }
1752
1753 int event_pos = 0;
1754 int res = read(nfd, event_buf, sizeof(event_buf));
1755 if (res < (int) sizeof(*event)) {
1756 if (errno == EINTR)
1757 continue;
1758 ERROR("failed to read inotify event: %s\n", strerror(errno));
1759 return;
1760 }
1761
1762 while (res >= (int) sizeof(*event)) {
1763 int event_size;
1764 event = (struct inotify_event *) (event_buf + event_pos);
1765
1766 TRACE("inotify event: %08x\n", event->mask);
1767 if ((event->mask & IN_IGNORED) == IN_IGNORED) {
1768 /* Previously watched file was deleted, probably due to move
1769 * that swapped in new data; re-arm the watch and read. */
1770 active = false;
1771 }
1772
1773 event_size = sizeof(*event) + event->len;
1774 res -= event_size;
1775 event_pos += event_size;
1776 }
1777 }
1778}
1779
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001780static int usage() {
1781 ERROR("usage: sdcard [OPTIONS] <source_path> <label>\n"
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001782 " -u: specify UID to run as\n"
1783 " -g: specify GID to run as\n"
Jeff Sharkey10a239b2015-07-28 10:49:41 -07001784 " -U: specify user ID that owns device\n"
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001785 " -m: source_path is multi-user\n"
Jeff Sharkeyb9f438f2015-08-06 11:39:44 -07001786 " -w: runtime write mount has full write access\n"
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001787 "\n");
Jeff Brown26567352012-05-25 13:27:43 -07001788 return 1;
1789}
1790
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001791static int fuse_setup(struct fuse* fuse, gid_t gid, mode_t mask) {
Jeff Brown26567352012-05-25 13:27:43 -07001792 char opts[256];
Jeff Brown26567352012-05-25 13:27:43 -07001793
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001794 fuse->fd = open("/dev/fuse", O_RDWR);
1795 if (fuse->fd == -1) {
1796 ERROR("failed to open fuse device: %s\n", strerror(errno));
Jeff Brown26567352012-05-25 13:27:43 -07001797 return -1;
1798 }
1799
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001800 umount2(fuse->dest_path, MNT_DETACH);
1801
Jeff Brown26567352012-05-25 13:27:43 -07001802 snprintf(opts, sizeof(opts),
1803 "fd=%i,rootmode=40000,default_permissions,allow_other,user_id=%d,group_id=%d",
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001804 fuse->fd, fuse->global->uid, fuse->global->gid);
1805 if (mount("/dev/fuse", fuse->dest_path, "fuse", MS_NOSUID | MS_NODEV | MS_NOEXEC |
1806 MS_NOATIME, opts) != 0) {
1807 ERROR("failed to mount fuse filesystem: %s\n", strerror(errno));
1808 return -1;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001809 }
1810
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001811 fuse->gid = gid;
1812 fuse->mask = mask;
1813
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001814 return 0;
Mike Lockwood4f35e622011-01-12 14:39:44 -05001815}
1816
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001817static void run(const char* source_path, const char* label, uid_t uid,
Jeff Sharkey10a239b2015-07-28 10:49:41 -07001818 gid_t gid, userid_t userid, bool multi_user, bool full_write) {
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001819 struct fuse_global global;
1820 struct fuse fuse_default;
1821 struct fuse fuse_read;
1822 struct fuse fuse_write;
1823 struct fuse_handler handler_default;
1824 struct fuse_handler handler_read;
1825 struct fuse_handler handler_write;
1826 pthread_t thread_default;
1827 pthread_t thread_read;
1828 pthread_t thread_write;
1829
1830 memset(&global, 0, sizeof(global));
1831 memset(&fuse_default, 0, sizeof(fuse_default));
1832 memset(&fuse_read, 0, sizeof(fuse_read));
1833 memset(&fuse_write, 0, sizeof(fuse_write));
1834 memset(&handler_default, 0, sizeof(handler_default));
1835 memset(&handler_read, 0, sizeof(handler_read));
1836 memset(&handler_write, 0, sizeof(handler_write));
1837
1838 pthread_mutex_init(&global.lock, NULL);
1839 global.package_to_appid = hashmapCreate(256, str_hash, str_icase_equals);
1840 global.uid = uid;
1841 global.gid = gid;
1842 global.multi_user = multi_user;
Jeff Sharkeyed2fe572015-07-16 09:13:52 -07001843 global.next_generation = 0;
1844 global.inode_ctr = 1;
1845
1846 memset(&global.root, 0, sizeof(global.root));
1847 global.root.nid = FUSE_ROOT_ID; /* 1 */
1848 global.root.refcount = 2;
1849 global.root.namelen = strlen(source_path);
1850 global.root.name = strdup(source_path);
Jeff Sharkey10a239b2015-07-28 10:49:41 -07001851 global.root.userid = userid;
Jeff Sharkeyed2fe572015-07-16 09:13:52 -07001852 global.root.uid = AID_ROOT;
Jeff Sharkey10a239b2015-07-28 10:49:41 -07001853 global.root.under_android = false;
Jeff Sharkeyed2fe572015-07-16 09:13:52 -07001854
1855 strcpy(global.source_path, source_path);
1856
1857 if (multi_user) {
1858 global.root.perm = PERM_PRE_ROOT;
Jeff Sharkeyed2fe572015-07-16 09:13:52 -07001859 snprintf(global.obb_path, sizeof(global.obb_path), "%s/obb", source_path);
1860 } else {
1861 global.root.perm = PERM_ROOT;
Jeff Sharkeyed2fe572015-07-16 09:13:52 -07001862 snprintf(global.obb_path, sizeof(global.obb_path), "%s/Android/obb", source_path);
1863 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001864
1865 fuse_default.global = &global;
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001866 fuse_read.global = &global;
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001867 fuse_write.global = &global;
Jeff Sharkeyed2fe572015-07-16 09:13:52 -07001868
1869 global.fuse_default = &fuse_default;
1870 global.fuse_read = &fuse_read;
1871 global.fuse_write = &fuse_write;
1872
Jeff Sharkeyb9f438f2015-08-06 11:39:44 -07001873 snprintf(fuse_default.dest_path, PATH_MAX, "/mnt/runtime/default/%s", label);
1874 snprintf(fuse_read.dest_path, PATH_MAX, "/mnt/runtime/read/%s", label);
1875 snprintf(fuse_write.dest_path, PATH_MAX, "/mnt/runtime/write/%s", label);
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001876
1877 handler_default.fuse = &fuse_default;
1878 handler_read.fuse = &fuse_read;
1879 handler_write.fuse = &fuse_write;
1880
Jeff Sharkeyed2fe572015-07-16 09:13:52 -07001881 handler_default.token = 0;
1882 handler_read.token = 1;
1883 handler_write.token = 2;
1884
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001885 umask(0);
1886
Jeff Sharkey10a239b2015-07-28 10:49:41 -07001887 if (multi_user) {
1888 /* Multi-user storage is fully isolated per user, so "other"
1889 * permissions are completely masked off. */
1890 if (fuse_setup(&fuse_default, AID_SDCARD_RW, 0006)
1891 || fuse_setup(&fuse_read, AID_EVERYBODY, 0027)
1892 || fuse_setup(&fuse_write, AID_EVERYBODY, full_write ? 0007 : 0027)) {
1893 ERROR("failed to fuse_setup\n");
1894 exit(1);
1895 }
1896 } else {
1897 /* Physical storage is readable by all users on device, but
1898 * the Android directories are masked off to a single user
1899 * deep inside attr_from_stat(). */
1900 if (fuse_setup(&fuse_default, AID_SDCARD_RW, 0006)
1901 || fuse_setup(&fuse_read, AID_EVERYBODY, full_write ? 0027 : 0022)
1902 || fuse_setup(&fuse_write, AID_EVERYBODY, full_write ? 0007 : 0022)) {
1903 ERROR("failed to fuse_setup\n");
1904 exit(1);
1905 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001906 }
1907
1908 /* Drop privs */
1909 if (setgroups(sizeof(kGroups) / sizeof(kGroups[0]), kGroups) < 0) {
1910 ERROR("cannot setgroups: %s\n", strerror(errno));
1911 exit(1);
1912 }
1913 if (setgid(gid) < 0) {
1914 ERROR("cannot setgid: %s\n", strerror(errno));
1915 exit(1);
1916 }
1917 if (setuid(uid) < 0) {
1918 ERROR("cannot setuid: %s\n", strerror(errno));
1919 exit(1);
1920 }
1921
1922 if (multi_user) {
Jeff Sharkeyed2fe572015-07-16 09:13:52 -07001923 fs_prepare_dir(global.obb_path, 0775, uid, gid);
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001924 }
1925
1926 if (pthread_create(&thread_default, NULL, start_handler, &handler_default)
1927 || pthread_create(&thread_read, NULL, start_handler, &handler_read)
1928 || pthread_create(&thread_write, NULL, start_handler, &handler_write)) {
1929 ERROR("failed to pthread_create\n");
1930 exit(1);
1931 }
1932
1933 watch_package_list(&global);
1934 ERROR("terminated prematurely\n");
1935 exit(1);
1936}
1937
Daniel Rosenberg3aa261c2016-03-31 22:00:47 +00001938static int sdcardfs_setup(const char *source_path, const char *dest_path, uid_t fsuid,
1939 gid_t fsgid, bool multi_user, userid_t userid, gid_t gid, mode_t mask) {
1940 char opts[256];
1941
1942 snprintf(opts, sizeof(opts),
1943 "fsuid=%d,fsgid=%d,%smask=%d,userid=%d,gid=%d",
1944 fsuid, fsgid, multi_user?"multiuser,":"", mask, userid, gid);
1945
1946 if (mount(source_path, dest_path, "sdcardfs",
1947 MS_NOSUID | MS_NODEV | MS_NOEXEC | MS_NOATIME, opts) != 0) {
1948 ERROR("failed to mount sdcardfs filesystem: %s\n", strerror(errno));
1949 return -1;
1950 }
1951
1952 return 0;
1953}
1954
1955static void run_sdcardfs(const char* source_path, const char* label, uid_t uid,
1956 gid_t gid, userid_t userid, bool multi_user, bool full_write) {
1957 char dest_path_default[PATH_MAX];
1958 char dest_path_read[PATH_MAX];
1959 char dest_path_write[PATH_MAX];
1960 char obb_path[PATH_MAX];
1961 snprintf(dest_path_default, PATH_MAX, "/mnt/runtime/default/%s", label);
1962 snprintf(dest_path_read, PATH_MAX, "/mnt/runtime/read/%s", label);
1963 snprintf(dest_path_write, PATH_MAX, "/mnt/runtime/write/%s", label);
1964
1965 umask(0);
1966 if (multi_user) {
1967 /* Multi-user storage is fully isolated per user, so "other"
1968 * permissions are completely masked off. */
1969 if (sdcardfs_setup(source_path, dest_path_default, uid, gid, multi_user, userid,
1970 AID_SDCARD_RW, 0006)
1971 || sdcardfs_setup(source_path, dest_path_read, uid, gid, multi_user, userid,
1972 AID_EVERYBODY, 0027)
1973 || sdcardfs_setup(source_path, dest_path_write, uid, gid, multi_user, userid,
1974 AID_EVERYBODY, full_write ? 0007 : 0027)) {
1975 ERROR("failed to fuse_setup\n");
1976 exit(1);
1977 }
1978 } else {
1979 /* Physical storage is readable by all users on device, but
1980 * the Android directories are masked off to a single user
1981 * deep inside attr_from_stat(). */
1982 if (sdcardfs_setup(source_path, dest_path_default, uid, gid, multi_user, userid,
1983 AID_SDCARD_RW, 0006)
1984 || sdcardfs_setup(source_path, dest_path_read, uid, gid, multi_user, userid,
1985 AID_EVERYBODY, full_write ? 0027 : 0022)
1986 || sdcardfs_setup(source_path, dest_path_write, uid, gid, multi_user, userid,
1987 AID_EVERYBODY, full_write ? 0007 : 0022)) {
1988 ERROR("failed to fuse_setup\n");
1989 exit(1);
1990 }
1991 }
1992
1993 /* Drop privs */
1994 if (setgroups(sizeof(kGroups) / sizeof(kGroups[0]), kGroups) < 0) {
1995 ERROR("cannot setgroups: %s\n", strerror(errno));
1996 exit(1);
1997 }
1998 if (setgid(gid) < 0) {
1999 ERROR("cannot setgid: %s\n", strerror(errno));
2000 exit(1);
2001 }
2002 if (setuid(uid) < 0) {
2003 ERROR("cannot setuid: %s\n", strerror(errno));
2004 exit(1);
2005 }
2006
2007 if (multi_user) {
2008 snprintf(obb_path, sizeof(obb_path), "%s/obb", source_path);
2009 fs_prepare_dir(&obb_path[0], 0775, uid, gid);
2010 }
2011
2012 exit(0);
2013}
2014
2015static bool supports_sdcardfs(void) {
2016 FILE *fp;
2017 char *buf = NULL;
2018 size_t buflen = 0;
2019
2020 fp = fopen("/proc/filesystems", "r");
2021 if (!fp) {
2022 ERROR("Could not read /proc/filesystems, error: %s\n", strerror(errno));
2023 return false;
2024 }
2025 while ((getline(&buf, &buflen, fp)) > 0) {
2026 if (strstr(buf, "sdcardfs\n")) {
2027 free(buf);
2028 fclose(fp);
2029 return true;
2030 }
2031 }
2032 free(buf);
2033 fclose(fp);
2034 return false;
2035}
2036
Jeff Sharkey20ca9832016-04-07 11:05:21 -06002037static bool should_use_sdcardfs(void) {
2038 char property[PROPERTY_VALUE_MAX];
2039
2040 // Allow user to have a strong opinion about state
2041 property_get(PROP_SDCARDFS_USER, property, "");
2042 if (!strcmp(property, "force_on")) {
2043 ALOGW("User explicitly enabled sdcardfs");
2044 return supports_sdcardfs();
2045 } else if (!strcmp(property, "force_off")) {
2046 ALOGW("User explicitly disabled sdcardfs");
2047 return false;
2048 }
2049
2050 // Fall back to device opinion about state
2051 if (property_get_bool(PROP_SDCARDFS_DEVICE, false)) {
2052 ALOGW("Device explicitly enabled sdcardfs");
2053 return supports_sdcardfs();
2054 } else {
2055 ALOGW("Device explicitly disabled sdcardfs");
2056 return false;
2057 }
2058}
2059
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07002060int main(int argc, char **argv) {
Jeff Sharkeye169bd02012-08-13 16:44:42 -07002061 const char *source_path = NULL;
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07002062 const char *label = NULL;
Jeff Brown26567352012-05-25 13:27:43 -07002063 uid_t uid = 0;
2064 gid_t gid = 0;
Jeff Sharkey10a239b2015-07-28 10:49:41 -07002065 userid_t userid = 0;
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07002066 bool multi_user = false;
2067 bool full_write = false;
Mike Lockwood4f35e622011-01-12 14:39:44 -05002068 int i;
Ken Sumrall2fd72cc2013-02-08 16:50:55 -08002069 struct rlimit rlim;
Nick Kralevich8d28fa72014-07-24 17:05:59 -07002070 int fs_version;
Brian Swetland03ee9472010-08-12 18:01:08 -07002071
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07002072 int opt;
Jeff Sharkey10a239b2015-07-28 10:49:41 -07002073 while ((opt = getopt(argc, argv, "u:g:U:mw")) != -1) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07002074 switch (opt) {
2075 case 'u':
2076 uid = strtoul(optarg, NULL, 10);
2077 break;
2078 case 'g':
2079 gid = strtoul(optarg, NULL, 10);
2080 break;
Jeff Sharkey10a239b2015-07-28 10:49:41 -07002081 case 'U':
2082 userid = strtoul(optarg, NULL, 10);
2083 break;
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07002084 case 'm':
2085 multi_user = true;
2086 break;
Jeff Sharkeye93a0512013-10-08 10:14:24 -07002087 case 'w':
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07002088 full_write = true;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07002089 break;
2090 case '?':
2091 default:
2092 return usage();
2093 }
2094 }
2095
2096 for (i = optind; i < argc; i++) {
Mike Lockwood4f35e622011-01-12 14:39:44 -05002097 char* arg = argv[i];
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07002098 if (!source_path) {
Jeff Sharkeye169bd02012-08-13 16:44:42 -07002099 source_path = arg;
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07002100 } else if (!label) {
2101 label = arg;
Jean-Baptiste Querue92372b2012-08-15 09:54:30 -07002102 } else {
Mike Lockwood575a2bb2011-01-23 14:46:30 -08002103 ERROR("too many arguments\n");
2104 return usage();
Mike Lockwood4f35e622011-01-12 14:39:44 -05002105 }
Brian Swetland03ee9472010-08-12 18:01:08 -07002106 }
2107
Jeff Sharkeye169bd02012-08-13 16:44:42 -07002108 if (!source_path) {
2109 ERROR("no source path specified\n");
2110 return usage();
2111 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07002112 if (!label) {
2113 ERROR("no label specified\n");
Mike Lockwood4f35e622011-01-12 14:39:44 -05002114 return usage();
2115 }
Jeff Brown26567352012-05-25 13:27:43 -07002116 if (!uid || !gid) {
Brian Swetland03ee9472010-08-12 18:01:08 -07002117 ERROR("uid and gid must be nonzero\n");
Mike Lockwood4f35e622011-01-12 14:39:44 -05002118 return usage();
Brian Swetland03ee9472010-08-12 18:01:08 -07002119 }
2120
Ken Sumrall2fd72cc2013-02-08 16:50:55 -08002121 rlim.rlim_cur = 8192;
2122 rlim.rlim_max = 8192;
2123 if (setrlimit(RLIMIT_NOFILE, &rlim)) {
2124 ERROR("Error setting RLIMIT_NOFILE, errno = %d\n", errno);
2125 }
2126
Nick Kralevich8d28fa72014-07-24 17:05:59 -07002127 while ((fs_read_atomic_int("/data/.layout_version", &fs_version) == -1) || (fs_version < 3)) {
2128 ERROR("installd fs upgrade not yet complete. Waiting...\n");
2129 sleep(1);
2130 }
2131
Jeff Sharkey20ca9832016-04-07 11:05:21 -06002132 if (should_use_sdcardfs()) {
Daniel Rosenberg3aa261c2016-03-31 22:00:47 +00002133 run_sdcardfs(source_path, label, uid, gid, userid, multi_user, full_write);
2134 } else {
2135 run(source_path, label, uid, gid, userid, multi_user, full_write);
2136 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07002137 return 1;
Brian Swetland03ee9472010-08-12 18:01:08 -07002138}