blob: 27981ff61d19237ad05a8e80f510df3b0d69666d [file] [log] [blame]
Mark Salyzyn8b2c7de2015-04-01 07:42:01 -07001/*
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 Salyzyn68651142015-04-01 09:24:22 -070022#define LOG_TAG "fs_config"
Mark Salyzyn4e5f71a2015-04-01 09:24:22 -070023
Mark Salyzyn68651142015-04-01 09:24:22 -070024#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 Salyzyn68651142015-04-01 09:24:22 -070033#include <sys/stat.h>
34#include <sys/types.h>
35
Mark Salyzyn68651142015-04-01 09:24:22 -070036#include <log/log.h>
Mark Salyzyn8b2c7de2015-04-01 07:42:01 -070037#include <private/android_filesystem_config.h>
Mark Salyzynfd5b4252015-04-16 08:13:32 -070038#include <utils/Compat.h>
Mark Salyzyn8b2c7de2015-04-01 07:42:01 -070039
Mark Salyzyn4232b372015-04-16 08:40:55 -070040#ifndef O_BINARY
41#define O_BINARY 0
42#endif
43
Mark Salyzyn68651142015-04-01 09:24:22 -070044/* The following structure is stored little endian */
45struct 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> */
55static inline uint16_t get2LE(const uint8_t* src)
56{
57 return src[0] | (src[1] << 8);
58}
59
60static 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 Salyzyn5d9e5ef2015-04-01 11:02:00 -070069#define ALIGN(x, alignment) ( ((x) + ((alignment) - 1)) & ~((alignment) - 1) )
70
Mark Salyzyn8b2c7de2015-04-01 07:42:01 -070071/* 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
77static const struct fs_path_config android_dirs[] = {
78 { 00770, AID_SYSTEM, AID_CACHE, 0, "cache" },
Daniel Rosenbergbbe796d2015-06-12 15:00:54 -070079 { 00500, AID_ROOT, AID_ROOT, 0, "config" },
Mark Salyzyn8b2c7de2015-04-01 07:42:01 -070080 { 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 Rosenbergbbe796d2015-06-12 15:00:54 -070092 { 00755, AID_ROOT, AID_SYSTEM, 0, "mnt" },
93 { 00755, AID_ROOT, AID_ROOT, 0, "root" },
Mark Salyzyn8b2c7de2015-04-01 07:42:01 -070094 { 00750, AID_ROOT, AID_SHELL, 0, "sbin" },
Daniel Rosenbergbbe796d2015-06-12 15:00:54 -070095 { 00751, AID_ROOT, AID_SDCARD_R, 0, "storage" },
Mark Salyzyn8b2c7de2015-04-01 07:42:01 -070096 { 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 Salyzyn68651142015-04-01 09:24:22 -0700111static const char conf_dir[] = "/system/etc/fs_config_dirs";
112static const char conf_file[] = "/system/etc/fs_config_files";
113
Mark Salyzyn8b2c7de2015-04-01 07:42:01 -0700114static 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.*" },
Tao Baoc6e93fe2015-07-18 19:46:16 -0700121 { 00440, AID_ROOT, AID_ROOT, 0, "system/etc/recovery.img" },
Mark Salyzyn68651142015-04-01 09:24:22 -0700122 { 00444, AID_ROOT, AID_ROOT, 0, conf_dir + 1 },
123 { 00444, AID_ROOT, AID_ROOT, 0, conf_file + 1 },
Mark Salyzyn8b2c7de2015-04-01 07:42:01 -0700124 { 00644, AID_SYSTEM, AID_SYSTEM, 0, "data/app/*" },
125 { 00644, AID_MEDIA_RW, AID_MEDIA_RW, 0, "data/media/*" },
126 { 00644, AID_SYSTEM, AID_SYSTEM, 0, "data/app-private/*" },
127 { 00644, AID_APP, AID_APP, 0, "data/data/*" },
128
129 /* the following five files are INTENTIONALLY set-uid, but they
130 * are NOT included on user builds. */
131 { 04750, AID_ROOT, AID_SHELL, 0, "system/xbin/su" },
132 { 06755, AID_ROOT, AID_ROOT, 0, "system/xbin/librank" },
133 { 06755, AID_ROOT, AID_ROOT, 0, "system/xbin/procrank" },
134 { 06755, AID_ROOT, AID_ROOT, 0, "system/xbin/procmem" },
135 { 04770, AID_ROOT, AID_RADIO, 0, "system/bin/pppd-ril" },
136
137 /* the following files have enhanced capabilities and ARE included in user builds. */
Jorge Lucangeli Obese920c462015-09-02 16:19:59 -0700138 { 00750, AID_ROOT, AID_SHELL, CAP_MASK_LONG(CAP_SETUID) | CAP_MASK_LONG(CAP_SETGID), "system/bin/run-as" },
139 { 00700, AID_SYSTEM, AID_SHELL, CAP_MASK_LONG(CAP_BLOCK_SUSPEND), "system/bin/inputflinger" },
Mark Salyzyn8b2c7de2015-04-01 07:42:01 -0700140
141 { 00750, AID_ROOT, AID_ROOT, 0, "system/bin/uncrypt" },
142 { 00750, AID_ROOT, AID_ROOT, 0, "system/bin/install-recovery.sh" },
143 { 00755, AID_ROOT, AID_SHELL, 0, "system/bin/*" },
144 { 00755, AID_ROOT, AID_ROOT, 0, "system/lib/valgrind/*" },
145 { 00755, AID_ROOT, AID_ROOT, 0, "system/lib64/valgrind/*" },
146 { 00755, AID_ROOT, AID_SHELL, 0, "system/xbin/*" },
147 { 00755, AID_ROOT, AID_SHELL, 0, "system/vendor/bin/*" },
148 { 00755, AID_ROOT, AID_SHELL, 0, "vendor/bin/*" },
149 { 00750, AID_ROOT, AID_SHELL, 0, "sbin/*" },
150 { 00755, AID_ROOT, AID_ROOT, 0, "bin/*" },
151 { 00750, AID_ROOT, AID_SHELL, 0, "init*" },
152 { 00750, AID_ROOT, AID_SHELL, 0, "sbin/fs_mgr" },
153 { 00640, AID_ROOT, AID_SHELL, 0, "fstab.*" },
154 { 00644, AID_ROOT, AID_ROOT, 0, 0 },
155};
156
Mark Salyzyn68651142015-04-01 09:24:22 -0700157static int fs_config_open(int dir)
158{
159 int fd = -1;
160
161 const char *out = getenv("OUT");
162 if (out && *out) {
163 char *name = NULL;
Elliott Hughes8fea1ed2015-07-24 18:48:31 -0700164 if (asprintf(&name, "%s%s", out, dir ? conf_dir : conf_file) != -1) {
Mark Salyzyn4232b372015-04-16 08:40:55 -0700165 fd = TEMP_FAILURE_RETRY(open(name, O_RDONLY | O_BINARY));
Mark Salyzyn68651142015-04-01 09:24:22 -0700166 free(name);
167 }
168 }
169 if (fd < 0) {
Mark Salyzyn4232b372015-04-16 08:40:55 -0700170 fd = TEMP_FAILURE_RETRY(open(dir ? conf_dir : conf_file, O_RDONLY | O_BINARY));
Mark Salyzyn68651142015-04-01 09:24:22 -0700171 }
172 return fd;
173}
174
175static 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 Salyzyn8b2c7de2015-04-01 07:42:01 -0700194void 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 Salyzyn7977cc62015-04-15 19:27:39 -0700198 int fd, plen;
Mark Salyzyn8b2c7de2015-04-01 07:42:01 -0700199
200 if (path[0] == '/') {
201 path++;
202 }
203
Mark Salyzyn7e826332015-04-15 22:30:30 +0000204 plen = strlen(path);
Mark Salyzyn68651142015-04-01 09:24:22 -0700205
Mark Salyzyn7977cc62015-04-15 19:27:39 -0700206 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 Salyzyn68651142015-04-01 09:24:22 -0700214 if (remainder <= 0) {
Mark Salyzyn7977cc62015-04-15 19:27:39 -0700215 ALOGE("%s len is corrupted", dir ? conf_dir : conf_file);
Mark Salyzyn68651142015-04-01 09:24:22 -0700216 break;
217 }
Mark Salyzyn7977cc62015-04-15 19:27:39 -0700218 prefix = calloc(1, remainder);
219 if (!prefix) {
220 ALOGE("%s out of memory", dir ? conf_dir : conf_file);
221 break;
Mark Salyzyn68651142015-04-01 09:24:22 -0700222 }
Mark Salyzyn7977cc62015-04-15 19:27:39 -0700223 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 Salyzyn68651142015-04-01 09:24:22 -0700229 if (len >= remainder) { /* missing a terminating null */
Mark Salyzyn7977cc62015-04-15 19:27:39 -0700230 free(prefix);
Mark Salyzyn68651142015-04-01 09:24:22 -0700231 ALOGE("%s is corrupted", dir ? conf_dir : conf_file);
Mark Salyzyn68651142015-04-01 09:24:22 -0700232 break;
233 }
Mark Salyzyn7977cc62015-04-15 19:27:39 -0700234 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 Salyzyn68651142015-04-01 09:24:22 -0700242 }
Mark Salyzyn7977cc62015-04-15 19:27:39 -0700243 free(prefix);
Mark Salyzyn7e826332015-04-15 22:30:30 +0000244 }
Mark Salyzyn68651142015-04-01 09:24:22 -0700245 close(fd);
Mark Salyzyn68651142015-04-01 09:24:22 -0700246 }
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 Salyzyn8b2c7de2015-04-01 07:42:01 -0700252 }
253 }
254 *uid = pc->uid;
255 *gid = pc->gid;
256 *mode = (*mode & (~07777)) | pc->mode;
257 *capabilities = pc->capabilities;
258}
Mark Salyzyn5d9e5ef2015-04-01 11:02:00 -0700259
260ssize_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}