1 /* 2 * Copyright (C) 2007 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 /* This file is used to define the properties of the filesystem 18 ** images generated by build tools (mkbootfs and mkyaffs2image) and 19 ** by the device side of adb. 20 */ 21 22 #define LOG_TAG "fs_config" 23 24 #define _GNU_SOURCE 25 26 #include <errno.h> 27 #include <fcntl.h> 28 #include <stdbool.h> 29 #include <stdint.h> 30 #include <stdio.h> 31 #include <stdlib.h> 32 #include <string.h> 33 #include <sys/stat.h> 34 #include <sys/types.h> 35 36 #include <log/log.h> 37 #include <private/android_filesystem_config.h> 38 #include <utils/Compat.h> 39 40 #ifndef O_BINARY 41 #define O_BINARY 0 42 #endif 43 44 /* The following structure is stored little endian */ 45 struct fs_path_config_from_file { 46 uint16_t len; 47 uint16_t mode; 48 uint16_t uid; 49 uint16_t gid; 50 uint64_t capabilities; 51 char prefix[]; 52 } __attribute__((__aligned__(sizeof(uint64_t)))); 53 54 /* My kingdom for <endian.h> */ 55 static inline uint16_t get2LE(const uint8_t* src) 56 { 57 return src[0] | (src[1] << 8); 58 } 59 60 static inline uint64_t get8LE(const uint8_t* src) 61 { 62 uint32_t low, high; 63 64 low = src[0] | (src[1] << 8) | (src[2] << 16) | (src[3] << 24); 65 high = src[4] | (src[5] << 8) | (src[6] << 16) | (src[7] << 24); 66 return ((uint64_t) high << 32) | (uint64_t) low; 67 } 68 69 #define ALIGN(x, alignment) ( ((x) + ((alignment) - 1)) & ~((alignment) - 1) ) 70 71 /* Rules for directories. 72 ** These rules are applied based on "first match", so they 73 ** should start with the most specific path and work their 74 ** way up to the root. 75 */ 76 77 static const struct fs_path_config android_dirs[] = { 78 { 00770, AID_SYSTEM, AID_CACHE, 0, "cache" }, 79 { 00500, AID_ROOT, AID_ROOT, 0, "config" }, 80 { 00771, AID_SYSTEM, AID_SYSTEM, 0, "data/app" }, 81 { 00771, AID_SYSTEM, AID_SYSTEM, 0, "data/app-private" }, 82 { 00771, AID_SYSTEM, AID_SYSTEM, 0, "data/app-ephemeral" }, 83 { 00771, AID_ROOT, AID_ROOT, 0, "data/dalvik-cache" }, 84 { 00771, AID_SYSTEM, AID_SYSTEM, 0, "data/data" }, 85 { 00771, AID_SHELL, AID_SHELL, 0, "data/local/tmp" }, 86 { 00771, AID_SHELL, AID_SHELL, 0, "data/local" }, 87 { 01771, AID_SYSTEM, AID_MISC, 0, "data/misc" }, 88 { 00770, AID_DHCP, AID_DHCP, 0, "data/misc/dhcp" }, 89 { 00771, AID_SHARED_RELRO, AID_SHARED_RELRO, 0, "data/misc/shared_relro" }, 90 { 00775, AID_MEDIA_RW, AID_MEDIA_RW, 0, "data/media" }, 91 { 00775, AID_MEDIA_RW, AID_MEDIA_RW, 0, "data/media/Music" }, 92 { 00750, AID_ROOT, AID_SHELL, 0, "data/nativetest" }, 93 { 00750, AID_ROOT, AID_SHELL, 0, "data/nativetest64" }, 94 { 00775, AID_ROOT, AID_ROOT, 0, "data/preloads" }, 95 { 00771, AID_SYSTEM, AID_SYSTEM, 0, "data" }, 96 { 00755, AID_ROOT, AID_SYSTEM, 0, "mnt" }, 97 { 00755, AID_ROOT, AID_ROOT, 0, "root" }, 98 { 00750, AID_ROOT, AID_SHELL, 0, "sbin" }, 99 { 00751, AID_ROOT, AID_SDCARD_R, 0, "storage" }, 100 { 00755, AID_ROOT, AID_SHELL, 0, "system/bin" }, 101 { 00755, AID_ROOT, AID_SHELL, 0, "system/vendor" }, 102 { 00755, AID_ROOT, AID_SHELL, 0, "system/xbin" }, 103 { 00755, AID_ROOT, AID_ROOT, 0, "system/etc/ppp" }, 104 { 00755, AID_ROOT, AID_SHELL, 0, "vendor" }, 105 { 00777, AID_ROOT, AID_ROOT, 0, "sdcard" }, 106 { 00755, AID_ROOT, AID_ROOT, 0, 0 }, 107 }; 108 109 /* Rules for files. 110 ** These rules are applied based on "first match", so they 111 ** should start with the most specific path and work their 112 ** way up to the root. Prefixes ending in * denotes wildcard 113 ** and will allow partial matches. 114 */ 115 static const char conf_dir[] = "/system/etc/fs_config_dirs"; 116 static const char conf_file[] = "/system/etc/fs_config_files"; 117 118 static const struct fs_path_config android_files[] = { 119 { 00440, AID_ROOT, AID_SHELL, 0, "system/etc/init.goldfish.rc" }, 120 { 00550, AID_ROOT, AID_SHELL, 0, "system/etc/init.goldfish.sh" }, 121 { 00550, AID_ROOT, AID_SHELL, 0, "system/etc/init.ril" }, 122 { 00555, AID_ROOT, AID_ROOT, 0, "system/etc/ppp/*" }, 123 { 00555, AID_ROOT, AID_ROOT, 0, "system/etc/rc.*" }, 124 { 00440, AID_ROOT, AID_ROOT, 0, "system/etc/recovery.img" }, 125 { 00444, AID_ROOT, AID_ROOT, 0, conf_dir + 1 }, 126 { 00444, AID_ROOT, AID_ROOT, 0, conf_file + 1 }, 127 { 00644, AID_SYSTEM, AID_SYSTEM, 0, "data/app/*" }, 128 { 00644, AID_MEDIA_RW, AID_MEDIA_RW, 0, "data/media/*" }, 129 { 00644, AID_SYSTEM, AID_SYSTEM, 0, "data/app-private/*" }, 130 { 00644, AID_SYSTEM, AID_SYSTEM, 0, "data/app-ephemeral/*" }, 131 { 00644, AID_APP, AID_APP, 0, "data/data/*" }, 132 { 00640, AID_ROOT, AID_SHELL, 0, "data/nativetest/tests.txt" }, 133 { 00640, AID_ROOT, AID_SHELL, 0, "data/nativetest64/tests.txt" }, 134 { 00750, AID_ROOT, AID_SHELL, 0, "data/nativetest/*" }, 135 { 00750, AID_ROOT, AID_SHELL, 0, "data/nativetest64/*" }, 136 137 /* the following two files are INTENTIONALLY set-uid, but they 138 * are NOT included on user builds. */ 139 { 04750, AID_ROOT, AID_SHELL, 0, "system/xbin/su" }, 140 { 06755, AID_ROOT, AID_ROOT, 0, "system/xbin/procmem" }, 141 142 /* the following files have enhanced capabilities and ARE included in user builds. */ 143 { 00750, AID_ROOT, AID_SHELL, CAP_MASK_LONG(CAP_SETUID) | CAP_MASK_LONG(CAP_SETGID), "system/bin/run-as" }, 144 { 00700, AID_SYSTEM, AID_SHELL, CAP_MASK_LONG(CAP_BLOCK_SUSPEND), "system/bin/inputflinger" }, 145 146 /* Support FIFO scheduling mode in SurfaceFlinger. */ 147 { 00755, AID_SYSTEM, AID_GRAPHICS, CAP_MASK_LONG(CAP_SYS_NICE), "system/bin/surfaceflinger" }, 148 149 { 00750, AID_ROOT, AID_ROOT, 0, "system/bin/uncrypt" }, 150 { 00750, AID_ROOT, AID_ROOT, 0, "system/bin/install-recovery.sh" }, 151 { 00755, AID_ROOT, AID_SHELL, 0, "system/bin/*" }, 152 { 00755, AID_ROOT, AID_ROOT, 0, "system/lib/valgrind/*" }, 153 { 00755, AID_ROOT, AID_ROOT, 0, "system/lib64/valgrind/*" }, 154 { 00755, AID_ROOT, AID_SHELL, 0, "system/xbin/*" }, 155 { 00755, AID_ROOT, AID_SHELL, 0, "system/vendor/bin/*" }, 156 { 00755, AID_ROOT, AID_SHELL, 0, "system/vendor/xbin/*" }, 157 { 00755, AID_ROOT, AID_SHELL, 0, "vendor/bin/*" }, 158 { 00755, AID_ROOT, AID_SHELL, 0, "vendor/xbin/*" }, 159 { 00750, AID_ROOT, AID_SHELL, 0, "sbin/*" }, 160 { 00755, AID_ROOT, AID_ROOT, 0, "bin/*" }, 161 { 00750, AID_ROOT, AID_SHELL, 0, "init*" }, 162 { 00750, AID_ROOT, AID_SHELL, 0, "sbin/fs_mgr" }, 163 { 00640, AID_ROOT, AID_SHELL, 0, "fstab.*" }, 164 { 00644, AID_ROOT, AID_ROOT, 0, 0 }, 165 }; 166 167 static int fs_config_open(int dir, const char *target_out_path) 168 { 169 int fd = -1; 170 171 if (target_out_path && *target_out_path) { 172 /* target_out_path is the path to the directory holding content of system partition 173 but as we cannot guaranty it ends with '/system' we need this below skip_len logic */ 174 char *name = NULL; 175 int target_out_path_len = strlen(target_out_path); 176 int skip_len = strlen("/system"); 177 178 if (target_out_path[target_out_path_len] == '/') { 179 skip_len++; 180 } 181 if (asprintf(&name, "%s%s", target_out_path, (dir ? conf_dir : conf_file) + skip_len) != -1) { 182 fd = TEMP_FAILURE_RETRY(open(name, O_RDONLY | O_BINARY)); 183 free(name); 184 } 185 } 186 if (fd < 0) { 187 fd = TEMP_FAILURE_RETRY(open(dir ? conf_dir : conf_file, O_RDONLY | O_BINARY)); 188 } 189 return fd; 190 } 191 192 static bool fs_config_cmp(bool dir, const char *prefix, size_t len, 193 const char *path, size_t plen) 194 { 195 if (dir) { 196 if (plen < len) { 197 return false; 198 } 199 } else { 200 /* If name ends in * then allow partial matches. */ 201 if (prefix[len - 1] == '*') { 202 return !strncmp(prefix, path, len - 1); 203 } 204 if (plen != len) { 205 return false; 206 } 207 } 208 return !strncmp(prefix, path, len); 209 } 210 211 void fs_config(const char *path, int dir, const char *target_out_path, 212 unsigned *uid, unsigned *gid, unsigned *mode, uint64_t *capabilities) 213 { 214 const struct fs_path_config *pc; 215 int fd, plen; 216 217 if (path[0] == '/') { 218 path++; 219 } 220 221 plen = strlen(path); 222 223 fd = fs_config_open(dir, target_out_path); 224 if (fd >= 0) { 225 struct fs_path_config_from_file header; 226 227 while (TEMP_FAILURE_RETRY(read(fd, &header, sizeof(header))) == sizeof(header)) { 228 char *prefix; 229 uint16_t host_len = get2LE((const uint8_t *)&header.len); 230 ssize_t len, remainder = host_len - sizeof(header); 231 if (remainder <= 0) { 232 ALOGE("%s len is corrupted", dir ? conf_dir : conf_file); 233 break; 234 } 235 prefix = calloc(1, remainder); 236 if (!prefix) { 237 ALOGE("%s out of memory", dir ? conf_dir : conf_file); 238 break; 239 } 240 if (TEMP_FAILURE_RETRY(read(fd, prefix, remainder)) != remainder) { 241 free(prefix); 242 ALOGE("%s prefix is truncated", dir ? conf_dir : conf_file); 243 break; 244 } 245 len = strnlen(prefix, remainder); 246 if (len >= remainder) { /* missing a terminating null */ 247 free(prefix); 248 ALOGE("%s is corrupted", dir ? conf_dir : conf_file); 249 break; 250 } 251 if (fs_config_cmp(dir, prefix, len, path, plen)) { 252 free(prefix); 253 close(fd); 254 *uid = get2LE((const uint8_t *)&(header.uid)); 255 *gid = get2LE((const uint8_t *)&(header.gid)); 256 *mode = (*mode & (~07777)) | get2LE((const uint8_t *)&(header.mode)); 257 *capabilities = get8LE((const uint8_t *)&(header.capabilities)); 258 return; 259 } 260 free(prefix); 261 } 262 close(fd); 263 } 264 265 pc = dir ? android_dirs : android_files; 266 for(; pc->prefix; pc++){ 267 if (fs_config_cmp(dir, pc->prefix, strlen(pc->prefix), path, plen)) { 268 break; 269 } 270 } 271 *uid = pc->uid; 272 *gid = pc->gid; 273 *mode = (*mode & (~07777)) | pc->mode; 274 *capabilities = pc->capabilities; 275 } 276 277 ssize_t fs_config_generate(char *buffer, size_t length, const struct fs_path_config *pc) 278 { 279 struct fs_path_config_from_file *p = (struct fs_path_config_from_file *)buffer; 280 size_t len = ALIGN(sizeof(*p) + strlen(pc->prefix) + 1, sizeof(uint64_t)); 281 282 if ((length < len) || (len > UINT16_MAX)) { 283 return -ENOSPC; 284 } 285 memset(p, 0, len); 286 uint16_t host_len = len; 287 p->len = get2LE((const uint8_t *)&host_len); 288 p->mode = get2LE((const uint8_t *)&(pc->mode)); 289 p->uid = get2LE((const uint8_t *)&(pc->uid)); 290 p->gid = get2LE((const uint8_t *)&(pc->gid)); 291 p->capabilities = get8LE((const uint8_t *)&(pc->capabilities)); 292 strcpy(p->prefix, pc->prefix); 293 return len; 294 } 295