1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442
| static int handle_lookup(struct fuse* fuse, struct fuse_handler* handler, const struct fuse_in_header *hdr, const char* name) { struct node* parent_node; char parent_path[PATH_MAX]; char child_path[PATH_MAX]; const char* actual_name;
pthread_mutex_lock(&fuse->global->lock); parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid, parent_path, sizeof(parent_path)); DLOG(INFO) << "[" << handler->token << "] LOOKUP " << name << " @ " << hdr->nodeid << " (" << (parent_node ? parent_node->name : "?") << ")"; pthread_mutex_unlock(&fuse->global->lock); if (!parent_node || !(actual_name = find_file_within(parent_path, name, child_path, sizeof(child_path), 1))) { return -ENOENT; } if (!check_caller_access_to_name(fuse, hdr, parent_node, name, R_OK)) { return -EACCES; } return fuse_reply_entry(fuse, hdr->unique, parent_node, name, actual_name, child_path); }
static struct node* lookup_node_and_path_by_id_locked(struct fuse* fuse, __u64 nid, char* buf, size_t bufsize) { struct node* node = lookup_node_by_id_locked(fuse, nid); if (node && get_node_path_locked(node, buf, bufsize) < 0) { node = NULL; } return node; }
static struct node *lookup_node_by_id_locked(struct fuse *fuse, __u64 nid) { if (nid == FUSE_ROOT_ID) { return &fuse->global->root; } else { return static_cast<struct node*>(id_to_ptr(nid)); } }
static ssize_t get_node_path_locked(struct node* node, char* buf, size_t bufsize) { const char* name; size_t namelen; if (node->graft_path) { name = node->graft_path; namelen = node->graft_pathlen; } else if (node->actual_name) { name = node->actual_name; namelen = node->namelen; } else { name = node->name; namelen = node->namelen; }
if (bufsize < namelen + 1) { return -1; }
ssize_t pathlen = 0; if (node->parent && node->graft_path == NULL) { pathlen = get_node_path_locked(node->parent, buf, bufsize - namelen - 1); if (pathlen < 0) { return -1; } buf[pathlen++] = '/'; }
memcpy(buf + pathlen, name, namelen + 1); return pathlen + namelen; }
static char* find_file_within(const char* path, const char* name, char* buf, size_t bufsize, int search) { size_t pathlen = strlen(path); size_t namelen = strlen(name); size_t childlen = pathlen + namelen + 1; char* actual;
if (bufsize <= childlen) { return NULL; } memcpy(buf, path, pathlen); buf[pathlen] = '/'; actual = buf + pathlen + 1; memcpy(actual, name, namelen + 1); if (search && access(buf, F_OK)) { struct dirent* entry; DIR* dir = opendir(path); if (!dir) { PLOG(ERROR) << "opendir(" << path << ") failed"; return actual; } while ((entry = readdir(dir))) { if (!strcasecmp(entry->d_name, name)) { memcpy(actual, entry->d_name, namelen); break; } } closedir(dir); } return actual; }
check_caller_access_to_name(fuse, hdr, parent_node, name, R_OK)
static bool check_caller_access_to_name(struct fuse* fuse, const struct fuse_in_header *hdr, const struct node* parent_node, const char* name, int mode) { if (parent_node && parent_node->perm == PERM_ROOT) { if (!strcasecmp(name, "autorun.inf") || !strcasecmp(name, ".android_secure") || !strcasecmp(name, "android_secure")) { return false; } }
if (hdr->uid == 0) { return true; }
return true; }
return fuse_reply_entry(fuse, hdr->unique, parent_node, name, actual_name, child_path);
static int fuse_reply_entry(struct fuse* fuse, __u64 unique, struct node* parent, const char* name, const char* actual_name, const char* path) { struct node* node; struct fuse_entry_out out; struct stat s;
if (lstat(path, &s) == -1) { return -errno; }
pthread_mutex_lock(&fuse->global->lock); node = acquire_or_create_child_locked(fuse, parent, name, actual_name); if (!node) { pthread_mutex_unlock(&fuse->global->lock); return -ENOMEM; } memset(&out, 0, sizeof(out)); attr_from_stat(fuse, &out.attr, &s, node); out.attr_valid = 10; out.entry_valid = 10; out.nodeid = node->nid; out.generation = node->gen; pthread_mutex_unlock(&fuse->global->lock); fuse_reply(fuse, unique, &out, sizeof(out)); return NO_STATUS; }
node = acquire_or_create_child_locked(fuse, parent, name, actual_name);
static struct node* acquire_or_create_child_locked( struct fuse* fuse, struct node* parent, const char* name, const char* actual_name) { struct node* child = lookup_child_by_name_locked(parent, name); if (child) { acquire_node_locked(child); } else { child = create_node_locked(fuse, parent, name, actual_name); } return child; }
child = create_node_locked(fuse, parent, name, actual_name);
struct node *create_node_locked(struct fuse* fuse, struct node *parent, const char *name, const char* actual_name) { struct node *node; size_t namelen = strlen(name);
if (fuse->global->inode_ctr == 0) { LOG(ERROR) << "No more inode numbers available"; return NULL; }
node = static_cast<struct node*>(calloc(1, sizeof(struct node))); if (!node) { return NULL; } node->name = static_cast<char*>(malloc(namelen + 1)); if (!node->name) { free(node); return NULL; } memcpy(node->name, name, namelen + 1); if (strcmp(name, actual_name)) { node->actual_name = static_cast<char*>(malloc(namelen + 1)); if (!node->actual_name) { free(node->name); free(node); return NULL; } memcpy(node->actual_name, actual_name, namelen + 1); } node->namelen = namelen; node->nid = ptr_to_id(node); node->ino = fuse->global->inode_ctr++; node->gen = fuse->global->next_generation++;
node->deleted = false; derive_permissions_locked(fuse, parent, node); acquire_node_locked(node); add_node_to_parent_locked(node, parent); return node; }
derive_permissions_locked(fuse, parent, node); static void derive_permissions_locked(struct fuse* fuse, struct node *parent, struct node *node) { appid_t appid;
node->perm = PERM_INHERIT; node->userid = parent->userid; node->uid = parent->uid; node->under_android = parent->under_android;
switch (parent->perm) { case PERM_INHERIT: break; case PERM_PRE_ROOT: node->perm = PERM_ROOT; node->userid = strtoul(node->name, NULL, 10); break; case PERM_ROOT: if (!strcasecmp(node->name, "Android")) { node->perm = PERM_ANDROID; node->under_android = true; } break; case PERM_ANDROID: if (!strcasecmp(node->name, "data")) { node->perm = PERM_ANDROID_DATA; } else if (!strcasecmp(node->name, "obb")) { node->perm = PERM_ANDROID_OBB; node->graft_path = fuse->global->obb_path; node->graft_pathlen = strlen(fuse->global->obb_path); } else if (!strcasecmp(node->name, "media")) { node->perm = PERM_ANDROID_MEDIA; } break; case PERM_ANDROID_DATA: case PERM_ANDROID_OBB: case PERM_ANDROID_MEDIA: const auto& iter = fuse->global->package_to_appid->find(node->name); if (iter != fuse->global->package_to_appid->end()) { appid = iter->second; node->uid = multiuser_get_uid(parent->userid, appid); } break; } }
add_node_to_parent_locked(node, parent); static void add_node_to_parent_locked(struct node *node, struct node *parent) { node->parent = parent; node->next = parent->child; parent->child = node; acquire_node_locked(parent); }
attr_from_stat(fuse, &out.attr, &s, node); static void attr_from_stat(struct fuse* fuse, struct fuse_attr *attr, const struct stat *s, const struct node* node) { attr->ino = node->ino; attr->size = s->st_size; attr->blocks = s->st_blocks; attr->atime = s->st_atim.tv_sec; attr->mtime = s->st_mtim.tv_sec; attr->ctime = s->st_ctim.tv_sec; attr->atimensec = s->st_atim.tv_nsec; attr->mtimensec = s->st_mtim.tv_nsec; attr->ctimensec = s->st_ctim.tv_nsec; attr->mode = s->st_mode; attr->nlink = s->st_nlink;
attr->uid = node->uid; if (fuse->gid == AID_SDCARD_RW) { attr->gid = AID_SDCARD_RW; } else { attr->gid = multiuser_get_uid(node->userid, fuse->gid); } int visible_mode = 0775 & ~fuse->mask; if (node->perm == PERM_PRE_ROOT) {
visible_mode = 0711; } else if (node->under_android) {
if (fuse->gid == AID_SDCARD_RW) { visible_mode = visible_mode & ~0006; } else { visible_mode = visible_mode & ~0007; } } int owner_mode = s->st_mode & 0700; int filtered_mode = visible_mode & (owner_mode | (owner_mode >> 3) | (owner_mode >> 6)); attr->mode = (attr->mode & S_IFMT) | filtered_mode; }
fuse_reply(fuse, unique, &out, sizeof(out)); out.attr_valid = 10; out.entry_valid = 10; out.nodeid = node->nid; out.generation = node->gen;
static void fuse_reply(struct fuse *fuse, __u64 unique, void *data, int len) { struct fuse_out_header hdr; hdr.len = len + sizeof(hdr); hdr.error = 0; hdr.unique = unique; struct iovec vec[2]; vec[0].iov_base = &hdr; vec[0].iov_len = sizeof(hdr); vec[1].iov_base = data; vec[1].iov_len = len; ssize_t ret = TEMP_FAILURE_RETRY(writev(fuse->fd, vec, 2)); }
|