Mark Salyzyn | 8b2c7de | 2015-04-01 07:42:01 -0700 | [diff] [blame] | 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 | |
Mark Salyzyn | 6865114 | 2015-04-01 09:24:22 -0700 | [diff] [blame] | 22 | #define LOG_TAG "fs_config" |
Mark Salyzyn | 4e5f71a | 2015-04-01 09:24:22 -0700 | [diff] [blame] | 23 | |
Mark Salyzyn | 6865114 | 2015-04-01 09:24:22 -0700 | [diff] [blame] | 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> |
Mark Salyzyn | 6865114 | 2015-04-01 09:24:22 -0700 | [diff] [blame] | 33 | #include <sys/stat.h> |
| 34 | #include <sys/types.h> |
| 35 | |
Mark Salyzyn | 6865114 | 2015-04-01 09:24:22 -0700 | [diff] [blame] | 36 | #include <log/log.h> |
Mark Salyzyn | 8b2c7de | 2015-04-01 07:42:01 -0700 | [diff] [blame] | 37 | #include <private/android_filesystem_config.h> |
Mark Salyzyn | fd5b425 | 2015-04-16 08:13:32 -0700 | [diff] [blame] | 38 | #include <utils/Compat.h> |
Mark Salyzyn | 8b2c7de | 2015-04-01 07:42:01 -0700 | [diff] [blame] | 39 | |
Mark Salyzyn | 4232b37 | 2015-04-16 08:40:55 -0700 | [diff] [blame] | 40 | #ifndef O_BINARY |
| 41 | #define O_BINARY 0 |
| 42 | #endif |
| 43 | |
Mark Salyzyn | 6865114 | 2015-04-01 09:24:22 -0700 | [diff] [blame] | 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 | |
Mark Salyzyn | 5d9e5ef | 2015-04-01 11:02:00 -0700 | [diff] [blame] | 69 | #define ALIGN(x, alignment) ( ((x) + ((alignment) - 1)) & ~((alignment) - 1) ) |
| 70 | |
Mark Salyzyn | 8b2c7de | 2015-04-01 07:42:01 -0700 | [diff] [blame] | 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" }, |
Daniel Rosenberg | bbe796d | 2015-06-12 15:00:54 -0700 | [diff] [blame^] | 79 | { 00500, AID_ROOT, AID_ROOT, 0, "config" }, |
Mark Salyzyn | 8b2c7de | 2015-04-01 07:42:01 -0700 | [diff] [blame] | 80 | { 00771, AID_SYSTEM, AID_SYSTEM, 0, "data/app" }, |
| 81 | { 00771, AID_SYSTEM, AID_SYSTEM, 0, "data/app-private" }, |
| 82 | { 00771, AID_ROOT, AID_ROOT, 0, "data/dalvik-cache" }, |
| 83 | { 00771, AID_SYSTEM, AID_SYSTEM, 0, "data/data" }, |
| 84 | { 00771, AID_SHELL, AID_SHELL, 0, "data/local/tmp" }, |
| 85 | { 00771, AID_SHELL, AID_SHELL, 0, "data/local" }, |
| 86 | { 01771, AID_SYSTEM, AID_MISC, 0, "data/misc" }, |
| 87 | { 00770, AID_DHCP, AID_DHCP, 0, "data/misc/dhcp" }, |
| 88 | { 00771, AID_SHARED_RELRO, AID_SHARED_RELRO, 0, "data/misc/shared_relro" }, |
| 89 | { 00775, AID_MEDIA_RW, AID_MEDIA_RW, 0, "data/media" }, |
| 90 | { 00775, AID_MEDIA_RW, AID_MEDIA_RW, 0, "data/media/Music" }, |
| 91 | { 00771, AID_SYSTEM, AID_SYSTEM, 0, "data" }, |
Daniel Rosenberg | bbe796d | 2015-06-12 15:00:54 -0700 | [diff] [blame^] | 92 | { 00755, AID_ROOT, AID_SYSTEM, 0, "mnt" }, |
| 93 | { 00755, AID_ROOT, AID_ROOT, 0, "root" }, |
Mark Salyzyn | 8b2c7de | 2015-04-01 07:42:01 -0700 | [diff] [blame] | 94 | { 00750, AID_ROOT, AID_SHELL, 0, "sbin" }, |
Daniel Rosenberg | bbe796d | 2015-06-12 15:00:54 -0700 | [diff] [blame^] | 95 | { 00751, AID_ROOT, AID_SDCARD_R, 0, "storage" }, |
Mark Salyzyn | 8b2c7de | 2015-04-01 07:42:01 -0700 | [diff] [blame] | 96 | { 00755, AID_ROOT, AID_SHELL, 0, "system/bin" }, |
| 97 | { 00755, AID_ROOT, AID_SHELL, 0, "system/vendor" }, |
| 98 | { 00755, AID_ROOT, AID_SHELL, 0, "system/xbin" }, |
| 99 | { 00755, AID_ROOT, AID_ROOT, 0, "system/etc/ppp" }, |
| 100 | { 00755, AID_ROOT, AID_SHELL, 0, "vendor" }, |
| 101 | { 00777, AID_ROOT, AID_ROOT, 0, "sdcard" }, |
| 102 | { 00755, AID_ROOT, AID_ROOT, 0, 0 }, |
| 103 | }; |
| 104 | |
| 105 | /* Rules for files. |
| 106 | ** These rules are applied based on "first match", so they |
| 107 | ** should start with the most specific path and work their |
| 108 | ** way up to the root. Prefixes ending in * denotes wildcard |
| 109 | ** and will allow partial matches. |
| 110 | */ |
Mark Salyzyn | 6865114 | 2015-04-01 09:24:22 -0700 | [diff] [blame] | 111 | static const char conf_dir[] = "/system/etc/fs_config_dirs"; |
| 112 | static const char conf_file[] = "/system/etc/fs_config_files"; |
| 113 | |
Mark Salyzyn | 8b2c7de | 2015-04-01 07:42:01 -0700 | [diff] [blame] | 114 | static const struct fs_path_config android_files[] = { |
| 115 | { 00440, AID_ROOT, AID_SHELL, 0, "system/etc/init.goldfish.rc" }, |
| 116 | { 00550, AID_ROOT, AID_SHELL, 0, "system/etc/init.goldfish.sh" }, |
| 117 | { 00550, AID_ROOT, AID_SHELL, 0, "system/etc/init.ril" }, |
| 118 | { 00550, AID_DHCP, AID_SHELL, 0, "system/etc/dhcpcd/dhcpcd-run-hooks" }, |
| 119 | { 00555, AID_ROOT, AID_ROOT, 0, "system/etc/ppp/*" }, |
| 120 | { 00555, AID_ROOT, AID_ROOT, 0, "system/etc/rc.*" }, |
Mark Salyzyn | 6865114 | 2015-04-01 09:24:22 -0700 | [diff] [blame] | 121 | { 00444, AID_ROOT, AID_ROOT, 0, conf_dir + 1 }, |
| 122 | { 00444, AID_ROOT, AID_ROOT, 0, conf_file + 1 }, |
Mark Salyzyn | 8b2c7de | 2015-04-01 07:42:01 -0700 | [diff] [blame] | 123 | { 00644, AID_SYSTEM, AID_SYSTEM, 0, "data/app/*" }, |
| 124 | { 00644, AID_MEDIA_RW, AID_MEDIA_RW, 0, "data/media/*" }, |
| 125 | { 00644, AID_SYSTEM, AID_SYSTEM, 0, "data/app-private/*" }, |
| 126 | { 00644, AID_APP, AID_APP, 0, "data/data/*" }, |
| 127 | |
| 128 | /* the following five files are INTENTIONALLY set-uid, but they |
| 129 | * are NOT included on user builds. */ |
| 130 | { 04750, AID_ROOT, AID_SHELL, 0, "system/xbin/su" }, |
| 131 | { 06755, AID_ROOT, AID_ROOT, 0, "system/xbin/librank" }, |
| 132 | { 06755, AID_ROOT, AID_ROOT, 0, "system/xbin/procrank" }, |
| 133 | { 06755, AID_ROOT, AID_ROOT, 0, "system/xbin/procmem" }, |
| 134 | { 04770, AID_ROOT, AID_RADIO, 0, "system/bin/pppd-ril" }, |
| 135 | |
| 136 | /* the following files have enhanced capabilities and ARE included in user builds. */ |
| 137 | { 00750, AID_ROOT, AID_SHELL, (1ULL << CAP_SETUID) | (1ULL << CAP_SETGID), "system/bin/run-as" }, |
| 138 | { 00700, AID_SYSTEM, AID_SHELL, (1ULL << CAP_BLOCK_SUSPEND), "system/bin/inputflinger" }, |
| 139 | |
| 140 | { 00750, AID_ROOT, AID_ROOT, 0, "system/bin/uncrypt" }, |
| 141 | { 00750, AID_ROOT, AID_ROOT, 0, "system/bin/install-recovery.sh" }, |
| 142 | { 00755, AID_ROOT, AID_SHELL, 0, "system/bin/*" }, |
| 143 | { 00755, AID_ROOT, AID_ROOT, 0, "system/lib/valgrind/*" }, |
| 144 | { 00755, AID_ROOT, AID_ROOT, 0, "system/lib64/valgrind/*" }, |
| 145 | { 00755, AID_ROOT, AID_SHELL, 0, "system/xbin/*" }, |
| 146 | { 00755, AID_ROOT, AID_SHELL, 0, "system/vendor/bin/*" }, |
| 147 | { 00755, AID_ROOT, AID_SHELL, 0, "vendor/bin/*" }, |
| 148 | { 00750, AID_ROOT, AID_SHELL, 0, "sbin/*" }, |
| 149 | { 00755, AID_ROOT, AID_ROOT, 0, "bin/*" }, |
| 150 | { 00750, AID_ROOT, AID_SHELL, 0, "init*" }, |
| 151 | { 00750, AID_ROOT, AID_SHELL, 0, "sbin/fs_mgr" }, |
| 152 | { 00640, AID_ROOT, AID_SHELL, 0, "fstab.*" }, |
| 153 | { 00644, AID_ROOT, AID_ROOT, 0, 0 }, |
| 154 | }; |
| 155 | |
Mark Salyzyn | 6865114 | 2015-04-01 09:24:22 -0700 | [diff] [blame] | 156 | static int fs_config_open(int dir) |
| 157 | { |
| 158 | int fd = -1; |
| 159 | |
| 160 | const char *out = getenv("OUT"); |
| 161 | if (out && *out) { |
| 162 | char *name = NULL; |
| 163 | asprintf(&name, "%s%s", out, dir ? conf_dir : conf_file); |
| 164 | if (name) { |
Mark Salyzyn | 4232b37 | 2015-04-16 08:40:55 -0700 | [diff] [blame] | 165 | fd = TEMP_FAILURE_RETRY(open(name, O_RDONLY | O_BINARY)); |
Mark Salyzyn | 6865114 | 2015-04-01 09:24:22 -0700 | [diff] [blame] | 166 | free(name); |
| 167 | } |
| 168 | } |
| 169 | if (fd < 0) { |
Mark Salyzyn | 4232b37 | 2015-04-16 08:40:55 -0700 | [diff] [blame] | 170 | fd = TEMP_FAILURE_RETRY(open(dir ? conf_dir : conf_file, O_RDONLY | O_BINARY)); |
Mark Salyzyn | 6865114 | 2015-04-01 09:24:22 -0700 | [diff] [blame] | 171 | } |
| 172 | return fd; |
| 173 | } |
| 174 | |
| 175 | static bool fs_config_cmp(bool dir, const char *prefix, size_t len, |
| 176 | const char *path, size_t plen) |
| 177 | { |
| 178 | if (dir) { |
| 179 | if (plen < len) { |
| 180 | return false; |
| 181 | } |
| 182 | } else { |
| 183 | /* If name ends in * then allow partial matches. */ |
| 184 | if (prefix[len - 1] == '*') { |
| 185 | return !strncmp(prefix, path, len - 1); |
| 186 | } |
| 187 | if (plen != len) { |
| 188 | return false; |
| 189 | } |
| 190 | } |
| 191 | return !strncmp(prefix, path, len); |
| 192 | } |
| 193 | |
Mark Salyzyn | 8b2c7de | 2015-04-01 07:42:01 -0700 | [diff] [blame] | 194 | void fs_config(const char *path, int dir, |
| 195 | unsigned *uid, unsigned *gid, unsigned *mode, uint64_t *capabilities) |
| 196 | { |
| 197 | const struct fs_path_config *pc; |
Mark Salyzyn | 7977cc6 | 2015-04-15 19:27:39 -0700 | [diff] [blame] | 198 | int fd, plen; |
Mark Salyzyn | 8b2c7de | 2015-04-01 07:42:01 -0700 | [diff] [blame] | 199 | |
| 200 | if (path[0] == '/') { |
| 201 | path++; |
| 202 | } |
| 203 | |
Mark Salyzyn | 7e82633 | 2015-04-15 22:30:30 +0000 | [diff] [blame] | 204 | plen = strlen(path); |
Mark Salyzyn | 6865114 | 2015-04-01 09:24:22 -0700 | [diff] [blame] | 205 | |
Mark Salyzyn | 7977cc6 | 2015-04-15 19:27:39 -0700 | [diff] [blame] | 206 | fd = fs_config_open(dir); |
| 207 | if (fd >= 0) { |
| 208 | struct fs_path_config_from_file header; |
| 209 | |
| 210 | while (TEMP_FAILURE_RETRY(read(fd, &header, sizeof(header))) == sizeof(header)) { |
| 211 | char *prefix; |
| 212 | uint16_t host_len = get2LE((const uint8_t *)&header.len); |
| 213 | ssize_t len, remainder = host_len - sizeof(header); |
Mark Salyzyn | 6865114 | 2015-04-01 09:24:22 -0700 | [diff] [blame] | 214 | if (remainder <= 0) { |
Mark Salyzyn | 7977cc6 | 2015-04-15 19:27:39 -0700 | [diff] [blame] | 215 | ALOGE("%s len is corrupted", dir ? conf_dir : conf_file); |
Mark Salyzyn | 6865114 | 2015-04-01 09:24:22 -0700 | [diff] [blame] | 216 | break; |
| 217 | } |
Mark Salyzyn | 7977cc6 | 2015-04-15 19:27:39 -0700 | [diff] [blame] | 218 | prefix = calloc(1, remainder); |
| 219 | if (!prefix) { |
| 220 | ALOGE("%s out of memory", dir ? conf_dir : conf_file); |
| 221 | break; |
Mark Salyzyn | 6865114 | 2015-04-01 09:24:22 -0700 | [diff] [blame] | 222 | } |
Mark Salyzyn | 7977cc6 | 2015-04-15 19:27:39 -0700 | [diff] [blame] | 223 | if (TEMP_FAILURE_RETRY(read(fd, prefix, remainder)) != remainder) { |
| 224 | free(prefix); |
| 225 | ALOGE("%s prefix is truncated", dir ? conf_dir : conf_file); |
| 226 | break; |
| 227 | } |
| 228 | len = strnlen(prefix, remainder); |
Mark Salyzyn | 6865114 | 2015-04-01 09:24:22 -0700 | [diff] [blame] | 229 | if (len >= remainder) { /* missing a terminating null */ |
Mark Salyzyn | 7977cc6 | 2015-04-15 19:27:39 -0700 | [diff] [blame] | 230 | free(prefix); |
Mark Salyzyn | 6865114 | 2015-04-01 09:24:22 -0700 | [diff] [blame] | 231 | ALOGE("%s is corrupted", dir ? conf_dir : conf_file); |
Mark Salyzyn | 6865114 | 2015-04-01 09:24:22 -0700 | [diff] [blame] | 232 | break; |
| 233 | } |
Mark Salyzyn | 7977cc6 | 2015-04-15 19:27:39 -0700 | [diff] [blame] | 234 | if (fs_config_cmp(dir, prefix, len, path, plen)) { |
| 235 | free(prefix); |
| 236 | close(fd); |
| 237 | *uid = get2LE((const uint8_t *)&(header.uid)); |
| 238 | *gid = get2LE((const uint8_t *)&(header.gid)); |
| 239 | *mode = (*mode & (~07777)) | get2LE((const uint8_t *)&(header.mode)); |
| 240 | *capabilities = get8LE((const uint8_t *)&(header.capabilities)); |
| 241 | return; |
Mark Salyzyn | 6865114 | 2015-04-01 09:24:22 -0700 | [diff] [blame] | 242 | } |
Mark Salyzyn | 7977cc6 | 2015-04-15 19:27:39 -0700 | [diff] [blame] | 243 | free(prefix); |
Mark Salyzyn | 7e82633 | 2015-04-15 22:30:30 +0000 | [diff] [blame] | 244 | } |
Mark Salyzyn | 6865114 | 2015-04-01 09:24:22 -0700 | [diff] [blame] | 245 | close(fd); |
Mark Salyzyn | 6865114 | 2015-04-01 09:24:22 -0700 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | pc = dir ? android_dirs : android_files; |
| 249 | for(; pc->prefix; pc++){ |
| 250 | if (fs_config_cmp(dir, pc->prefix, strlen(pc->prefix), path, plen)) { |
| 251 | break; |
Mark Salyzyn | 8b2c7de | 2015-04-01 07:42:01 -0700 | [diff] [blame] | 252 | } |
| 253 | } |
| 254 | *uid = pc->uid; |
| 255 | *gid = pc->gid; |
| 256 | *mode = (*mode & (~07777)) | pc->mode; |
| 257 | *capabilities = pc->capabilities; |
| 258 | } |
Mark Salyzyn | 5d9e5ef | 2015-04-01 11:02:00 -0700 | [diff] [blame] | 259 | |
| 260 | ssize_t fs_config_generate(char *buffer, size_t length, const struct fs_path_config *pc) |
| 261 | { |
| 262 | struct fs_path_config_from_file *p = (struct fs_path_config_from_file *)buffer; |
| 263 | size_t len = ALIGN(sizeof(*p) + strlen(pc->prefix) + 1, sizeof(uint64_t)); |
| 264 | |
| 265 | if ((length < len) || (len > UINT16_MAX)) { |
| 266 | return -ENOSPC; |
| 267 | } |
| 268 | memset(p, 0, len); |
| 269 | uint16_t host_len = len; |
| 270 | p->len = get2LE((const uint8_t *)&host_len); |
| 271 | p->mode = get2LE((const uint8_t *)&(pc->mode)); |
| 272 | p->uid = get2LE((const uint8_t *)&(pc->uid)); |
| 273 | p->gid = get2LE((const uint8_t *)&(pc->gid)); |
| 274 | p->capabilities = get8LE((const uint8_t *)&(pc->capabilities)); |
| 275 | strcpy(p->prefix, pc->prefix); |
| 276 | return len; |
| 277 | } |