blob: 11d4225a508e4acab3d255a8f35c2cc821b9c52a [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 Salyzyn4e5f71a2015-04-01 09:24:22 -070022#define LOG_TAG "fs_config"
Mark Salyzyn8b2c7de2015-04-01 07:42:01 -070023
Mark Salyzyn4e5f71a2015-04-01 09:24:22 -070024#define _GNU_SOURCE
25
26#include <endian.h>
27#include <errno.h>
28#include <fcntl.h>
29#include <stdbool.h>
30#include <stdint.h>
31#include <stdio.h>
32#include <stdlib.h>
33#include <string.h>
34#include <sys/mman.h>
35#include <sys/stat.h>
36#include <sys/types.h>
37
38#include <cutils/fs.h>
39#include <log/log.h>
Mark Salyzyn8b2c7de2015-04-01 07:42:01 -070040#include <private/android_filesystem_config.h>
41
Mark Salyzyn4e5f71a2015-04-01 09:24:22 -070042/* The following structure is stored little endian */
43struct fs_path_config_from_file {
44 uint16_t len;
45 uint16_t mode;
46 uint16_t uid;
47 uint16_t gid;
48 uint64_t capabilities;
49 char prefix[];
50} __attribute__((__aligned__(sizeof(uint64_t))));
51
Mark Salyzyn8b2c7de2015-04-01 07:42:01 -070052/* Rules for directories.
53** These rules are applied based on "first match", so they
54** should start with the most specific path and work their
55** way up to the root.
56*/
57
58static const struct fs_path_config android_dirs[] = {
59 { 00770, AID_SYSTEM, AID_CACHE, 0, "cache" },
60 { 00771, AID_SYSTEM, AID_SYSTEM, 0, "data/app" },
61 { 00771, AID_SYSTEM, AID_SYSTEM, 0, "data/app-private" },
62 { 00771, AID_ROOT, AID_ROOT, 0, "data/dalvik-cache" },
63 { 00771, AID_SYSTEM, AID_SYSTEM, 0, "data/data" },
64 { 00771, AID_SHELL, AID_SHELL, 0, "data/local/tmp" },
65 { 00771, AID_SHELL, AID_SHELL, 0, "data/local" },
66 { 01771, AID_SYSTEM, AID_MISC, 0, "data/misc" },
67 { 00770, AID_DHCP, AID_DHCP, 0, "data/misc/dhcp" },
68 { 00771, AID_SHARED_RELRO, AID_SHARED_RELRO, 0, "data/misc/shared_relro" },
69 { 00775, AID_MEDIA_RW, AID_MEDIA_RW, 0, "data/media" },
70 { 00775, AID_MEDIA_RW, AID_MEDIA_RW, 0, "data/media/Music" },
71 { 00771, AID_SYSTEM, AID_SYSTEM, 0, "data" },
72 { 00750, AID_ROOT, AID_SHELL, 0, "sbin" },
73 { 00755, AID_ROOT, AID_SHELL, 0, "system/bin" },
74 { 00755, AID_ROOT, AID_SHELL, 0, "system/vendor" },
75 { 00755, AID_ROOT, AID_SHELL, 0, "system/xbin" },
76 { 00755, AID_ROOT, AID_ROOT, 0, "system/etc/ppp" },
77 { 00755, AID_ROOT, AID_SHELL, 0, "vendor" },
78 { 00777, AID_ROOT, AID_ROOT, 0, "sdcard" },
79 { 00755, AID_ROOT, AID_ROOT, 0, 0 },
80};
81
82/* Rules for files.
83** These rules are applied based on "first match", so they
84** should start with the most specific path and work their
85** way up to the root. Prefixes ending in * denotes wildcard
86** and will allow partial matches.
87*/
Mark Salyzyn4e5f71a2015-04-01 09:24:22 -070088static const char conf_dir[] = "/system/etc/fs_config_dirs";
89static const char conf_file[] = "/system/etc/fs_config_files";
90
Mark Salyzyn8b2c7de2015-04-01 07:42:01 -070091static const struct fs_path_config android_files[] = {
92 { 00440, AID_ROOT, AID_SHELL, 0, "system/etc/init.goldfish.rc" },
93 { 00550, AID_ROOT, AID_SHELL, 0, "system/etc/init.goldfish.sh" },
94 { 00550, AID_ROOT, AID_SHELL, 0, "system/etc/init.ril" },
95 { 00550, AID_DHCP, AID_SHELL, 0, "system/etc/dhcpcd/dhcpcd-run-hooks" },
96 { 00555, AID_ROOT, AID_ROOT, 0, "system/etc/ppp/*" },
97 { 00555, AID_ROOT, AID_ROOT, 0, "system/etc/rc.*" },
Mark Salyzyn4e5f71a2015-04-01 09:24:22 -070098 { 00444, AID_ROOT, AID_ROOT, 0, conf_dir + 1 },
99 { 00444, AID_ROOT, AID_ROOT, 0, conf_file + 1 },
Mark Salyzyn8b2c7de2015-04-01 07:42:01 -0700100 { 00644, AID_SYSTEM, AID_SYSTEM, 0, "data/app/*" },
101 { 00644, AID_MEDIA_RW, AID_MEDIA_RW, 0, "data/media/*" },
102 { 00644, AID_SYSTEM, AID_SYSTEM, 0, "data/app-private/*" },
103 { 00644, AID_APP, AID_APP, 0, "data/data/*" },
104
105 /* the following five files are INTENTIONALLY set-uid, but they
106 * are NOT included on user builds. */
107 { 04750, AID_ROOT, AID_SHELL, 0, "system/xbin/su" },
108 { 06755, AID_ROOT, AID_ROOT, 0, "system/xbin/librank" },
109 { 06755, AID_ROOT, AID_ROOT, 0, "system/xbin/procrank" },
110 { 06755, AID_ROOT, AID_ROOT, 0, "system/xbin/procmem" },
111 { 04770, AID_ROOT, AID_RADIO, 0, "system/bin/pppd-ril" },
112
113 /* the following files have enhanced capabilities and ARE included in user builds. */
114 { 00750, AID_ROOT, AID_SHELL, (1ULL << CAP_SETUID) | (1ULL << CAP_SETGID), "system/bin/run-as" },
115 { 00700, AID_SYSTEM, AID_SHELL, (1ULL << CAP_BLOCK_SUSPEND), "system/bin/inputflinger" },
116
117 { 00750, AID_ROOT, AID_ROOT, 0, "system/bin/uncrypt" },
118 { 00750, AID_ROOT, AID_ROOT, 0, "system/bin/install-recovery.sh" },
119 { 00755, AID_ROOT, AID_SHELL, 0, "system/bin/*" },
120 { 00755, AID_ROOT, AID_ROOT, 0, "system/lib/valgrind/*" },
121 { 00755, AID_ROOT, AID_ROOT, 0, "system/lib64/valgrind/*" },
122 { 00755, AID_ROOT, AID_SHELL, 0, "system/xbin/*" },
123 { 00755, AID_ROOT, AID_SHELL, 0, "system/vendor/bin/*" },
124 { 00755, AID_ROOT, AID_SHELL, 0, "vendor/bin/*" },
125 { 00750, AID_ROOT, AID_SHELL, 0, "sbin/*" },
126 { 00755, AID_ROOT, AID_ROOT, 0, "bin/*" },
127 { 00750, AID_ROOT, AID_SHELL, 0, "init*" },
128 { 00750, AID_ROOT, AID_SHELL, 0, "sbin/fs_mgr" },
129 { 00640, AID_ROOT, AID_SHELL, 0, "fstab.*" },
130 { 00644, AID_ROOT, AID_ROOT, 0, 0 },
131};
132
Mark Salyzyn4e5f71a2015-04-01 09:24:22 -0700133static int fs_config_open(int dir)
134{
135 int fd = -1;
136
137 const char *out = getenv("OUT");
138 if (out && *out) {
139 char *name = NULL;
140 asprintf(&name, "%s%s", out, dir ? conf_dir : conf_file);
141 if (name) {
142 fd = TEMP_FAILURE_RETRY(open(name, O_RDONLY));
143 free(name);
144 }
145 }
146 if (fd < 0) {
147 fd = TEMP_FAILURE_RETRY(open(dir ? conf_dir : conf_file, O_RDONLY));
148 }
149 return fd;
150}
151
152static bool fs_config_cmp(bool dir, const char *prefix, size_t len,
153 const char *path, size_t plen)
154{
155 if (dir) {
156 if (plen < len) {
157 return false;
158 }
159 } else {
160 /* If name ends in * then allow partial matches. */
161 if (prefix[len - 1] == '*') {
162 return !strncmp(prefix, path, len - 1);
163 }
164 if (plen != len) {
165 return false;
166 }
167 }
168 return !strncmp(prefix, path, len);
169}
170
Mark Salyzyn8b2c7de2015-04-01 07:42:01 -0700171void fs_config(const char *path, int dir,
172 unsigned *uid, unsigned *gid, unsigned *mode, uint64_t *capabilities)
173{
174 const struct fs_path_config *pc;
175 int plen;
Mark Salyzyn4e5f71a2015-04-01 09:24:22 -0700176 struct stat st;
177 void *address = NULL;
178
179 int fd = fs_config_open(dir);
180 if ((fd >= 0)
181 && (TEMP_FAILURE_RETRY(fstat(fd, &st)) >= 0)
182 && (size_t)st.st_size) {
183 address = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
184 if (address == MAP_FAILED) {
185 address = NULL;
186 }
187 } else if (fd >= 0) {
188 close(fd);
189 }
Mark Salyzyn8b2c7de2015-04-01 07:42:01 -0700190
191 if (path[0] == '/') {
192 path++;
193 }
194
Mark Salyzyn8b2c7de2015-04-01 07:42:01 -0700195 plen = strlen(path);
Mark Salyzyn4e5f71a2015-04-01 09:24:22 -0700196
197 if (address) {
198 const struct fs_path_config_from_file *p = (const struct fs_path_config_from_file *)
199 address;
200 const char *end = (const char *)address + st.st_size;
201 const struct fs_path_config_from_file *e = (const struct fs_path_config_from_file *)
202 (end - sizeof(*p));
203 for (; p < e; p = (const struct fs_path_config_from_file *)(((const char *)p) + le16toh(p->len))) {
204 ssize_t len, remainder = le16toh(p->len) - sizeof(*p);
205 if (remainder <= 0) {
206 ALOGE("%s is truncated", dir ? conf_dir : conf_file);
207 p = e;
208 break;
209 }
210 len = (const char *)e - (const char *)p;
211 if (remainder > len) {
212 remainder = len;
213 }
214 len = strnlen(p->prefix, remainder);
215 if (len >= remainder) { /* missing a terminating null */
216 ALOGE("%s is corrupted", dir ? conf_dir : conf_file);
217 p = e;
218 break;
219 }
220 if (fs_config_cmp(dir, p->prefix, len, path, plen)) {
221 break;
222 }
Mark Salyzyn8b2c7de2015-04-01 07:42:01 -0700223 }
Mark Salyzyn4e5f71a2015-04-01 09:24:22 -0700224 if (p < e) {
225 *uid = le16toh(p->uid);
226 *gid = le16toh(p->gid);
227 *mode = (*mode & (~07777)) | le16toh(p->mode);
228 *capabilities = le64toh(p->capabilities);
229 }
230 munmap(address, st.st_size);
231 close(fd);
232 if (p < e) {
233 return;
234 }
235 }
236
237 pc = dir ? android_dirs : android_files;
238 for(; pc->prefix; pc++){
239 if (fs_config_cmp(dir, pc->prefix, strlen(pc->prefix), path, plen)) {
240 break;
Mark Salyzyn8b2c7de2015-04-01 07:42:01 -0700241 }
242 }
243 *uid = pc->uid;
244 *gid = pc->gid;
245 *mode = (*mode & (~07777)) | pc->mode;
246 *capabilities = pc->capabilities;
247}