blob: bdb24dc3e7f2972400efabf311fceed2c3f2deed [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>
33#include <sys/mman.h>
34#include <sys/stat.h>
35#include <sys/types.h>
36
37#include <cutils/fs.h>
38#include <log/log.h>
Mark Salyzyn8b2c7de2015-04-01 07:42:01 -070039#include <private/android_filesystem_config.h>
40
Mark Salyzyn68651142015-04-01 09:24:22 -070041/* The following structure is stored little endian */
42struct fs_path_config_from_file {
43 uint16_t len;
44 uint16_t mode;
45 uint16_t uid;
46 uint16_t gid;
47 uint64_t capabilities;
48 char prefix[];
49} __attribute__((__aligned__(sizeof(uint64_t))));
50
51/* My kingdom for <endian.h> */
52static inline uint16_t get2LE(const uint8_t* src)
53{
54 return src[0] | (src[1] << 8);
55}
56
57static inline uint64_t get8LE(const uint8_t* src)
58{
59 uint32_t low, high;
60
61 low = src[0] | (src[1] << 8) | (src[2] << 16) | (src[3] << 24);
62 high = src[4] | (src[5] << 8) | (src[6] << 16) | (src[7] << 24);
63 return ((uint64_t) high << 32) | (uint64_t) low;
64}
65
Mark Salyzyn8b2c7de2015-04-01 07:42:01 -070066/* Rules for directories.
67** These rules are applied based on "first match", so they
68** should start with the most specific path and work their
69** way up to the root.
70*/
71
72static const struct fs_path_config android_dirs[] = {
73 { 00770, AID_SYSTEM, AID_CACHE, 0, "cache" },
74 { 00771, AID_SYSTEM, AID_SYSTEM, 0, "data/app" },
75 { 00771, AID_SYSTEM, AID_SYSTEM, 0, "data/app-private" },
76 { 00771, AID_ROOT, AID_ROOT, 0, "data/dalvik-cache" },
77 { 00771, AID_SYSTEM, AID_SYSTEM, 0, "data/data" },
78 { 00771, AID_SHELL, AID_SHELL, 0, "data/local/tmp" },
79 { 00771, AID_SHELL, AID_SHELL, 0, "data/local" },
80 { 01771, AID_SYSTEM, AID_MISC, 0, "data/misc" },
81 { 00770, AID_DHCP, AID_DHCP, 0, "data/misc/dhcp" },
82 { 00771, AID_SHARED_RELRO, AID_SHARED_RELRO, 0, "data/misc/shared_relro" },
83 { 00775, AID_MEDIA_RW, AID_MEDIA_RW, 0, "data/media" },
84 { 00775, AID_MEDIA_RW, AID_MEDIA_RW, 0, "data/media/Music" },
85 { 00771, AID_SYSTEM, AID_SYSTEM, 0, "data" },
86 { 00750, AID_ROOT, AID_SHELL, 0, "sbin" },
87 { 00755, AID_ROOT, AID_SHELL, 0, "system/bin" },
88 { 00755, AID_ROOT, AID_SHELL, 0, "system/vendor" },
89 { 00755, AID_ROOT, AID_SHELL, 0, "system/xbin" },
90 { 00755, AID_ROOT, AID_ROOT, 0, "system/etc/ppp" },
91 { 00755, AID_ROOT, AID_SHELL, 0, "vendor" },
92 { 00777, AID_ROOT, AID_ROOT, 0, "sdcard" },
93 { 00755, AID_ROOT, AID_ROOT, 0, 0 },
94};
95
96/* Rules for files.
97** These rules are applied based on "first match", so they
98** should start with the most specific path and work their
99** way up to the root. Prefixes ending in * denotes wildcard
100** and will allow partial matches.
101*/
Mark Salyzyn68651142015-04-01 09:24:22 -0700102static const char conf_dir[] = "/system/etc/fs_config_dirs";
103static const char conf_file[] = "/system/etc/fs_config_files";
104
Mark Salyzyn8b2c7de2015-04-01 07:42:01 -0700105static const struct fs_path_config android_files[] = {
106 { 00440, AID_ROOT, AID_SHELL, 0, "system/etc/init.goldfish.rc" },
107 { 00550, AID_ROOT, AID_SHELL, 0, "system/etc/init.goldfish.sh" },
108 { 00550, AID_ROOT, AID_SHELL, 0, "system/etc/init.ril" },
109 { 00550, AID_DHCP, AID_SHELL, 0, "system/etc/dhcpcd/dhcpcd-run-hooks" },
110 { 00555, AID_ROOT, AID_ROOT, 0, "system/etc/ppp/*" },
111 { 00555, AID_ROOT, AID_ROOT, 0, "system/etc/rc.*" },
Mark Salyzyn68651142015-04-01 09:24:22 -0700112 { 00444, AID_ROOT, AID_ROOT, 0, conf_dir + 1 },
113 { 00444, AID_ROOT, AID_ROOT, 0, conf_file + 1 },
Mark Salyzyn8b2c7de2015-04-01 07:42:01 -0700114 { 00644, AID_SYSTEM, AID_SYSTEM, 0, "data/app/*" },
115 { 00644, AID_MEDIA_RW, AID_MEDIA_RW, 0, "data/media/*" },
116 { 00644, AID_SYSTEM, AID_SYSTEM, 0, "data/app-private/*" },
117 { 00644, AID_APP, AID_APP, 0, "data/data/*" },
118
119 /* the following five files are INTENTIONALLY set-uid, but they
120 * are NOT included on user builds. */
121 { 04750, AID_ROOT, AID_SHELL, 0, "system/xbin/su" },
122 { 06755, AID_ROOT, AID_ROOT, 0, "system/xbin/librank" },
123 { 06755, AID_ROOT, AID_ROOT, 0, "system/xbin/procrank" },
124 { 06755, AID_ROOT, AID_ROOT, 0, "system/xbin/procmem" },
125 { 04770, AID_ROOT, AID_RADIO, 0, "system/bin/pppd-ril" },
126
127 /* the following files have enhanced capabilities and ARE included in user builds. */
128 { 00750, AID_ROOT, AID_SHELL, (1ULL << CAP_SETUID) | (1ULL << CAP_SETGID), "system/bin/run-as" },
129 { 00700, AID_SYSTEM, AID_SHELL, (1ULL << CAP_BLOCK_SUSPEND), "system/bin/inputflinger" },
130
131 { 00750, AID_ROOT, AID_ROOT, 0, "system/bin/uncrypt" },
132 { 00750, AID_ROOT, AID_ROOT, 0, "system/bin/install-recovery.sh" },
133 { 00755, AID_ROOT, AID_SHELL, 0, "system/bin/*" },
134 { 00755, AID_ROOT, AID_ROOT, 0, "system/lib/valgrind/*" },
135 { 00755, AID_ROOT, AID_ROOT, 0, "system/lib64/valgrind/*" },
136 { 00755, AID_ROOT, AID_SHELL, 0, "system/xbin/*" },
137 { 00755, AID_ROOT, AID_SHELL, 0, "system/vendor/bin/*" },
138 { 00755, AID_ROOT, AID_SHELL, 0, "vendor/bin/*" },
139 { 00750, AID_ROOT, AID_SHELL, 0, "sbin/*" },
140 { 00755, AID_ROOT, AID_ROOT, 0, "bin/*" },
141 { 00750, AID_ROOT, AID_SHELL, 0, "init*" },
142 { 00750, AID_ROOT, AID_SHELL, 0, "sbin/fs_mgr" },
143 { 00640, AID_ROOT, AID_SHELL, 0, "fstab.*" },
144 { 00644, AID_ROOT, AID_ROOT, 0, 0 },
145};
146
Mark Salyzyn68651142015-04-01 09:24:22 -0700147static int fs_config_open(int dir)
148{
149 int fd = -1;
150
151 const char *out = getenv("OUT");
152 if (out && *out) {
153 char *name = NULL;
154 asprintf(&name, "%s%s", out, dir ? conf_dir : conf_file);
155 if (name) {
156 fd = TEMP_FAILURE_RETRY(open(name, O_RDONLY));
157 free(name);
158 }
159 }
160 if (fd < 0) {
161 fd = TEMP_FAILURE_RETRY(open(dir ? conf_dir : conf_file, O_RDONLY));
162 }
163 return fd;
164}
165
166static bool fs_config_cmp(bool dir, const char *prefix, size_t len,
167 const char *path, size_t plen)
168{
169 if (dir) {
170 if (plen < len) {
171 return false;
172 }
173 } else {
174 /* If name ends in * then allow partial matches. */
175 if (prefix[len - 1] == '*') {
176 return !strncmp(prefix, path, len - 1);
177 }
178 if (plen != len) {
179 return false;
180 }
181 }
182 return !strncmp(prefix, path, len);
183}
184
Mark Salyzyn8b2c7de2015-04-01 07:42:01 -0700185void fs_config(const char *path, int dir,
186 unsigned *uid, unsigned *gid, unsigned *mode, uint64_t *capabilities)
187{
188 const struct fs_path_config *pc;
189 int plen;
Mark Salyzyn68651142015-04-01 09:24:22 -0700190 struct stat st;
191 void *address = NULL;
192
193 int fd = fs_config_open(dir);
194 if ((fd >= 0)
195 && (TEMP_FAILURE_RETRY(fstat(fd, &st)) >= 0)
196 && (size_t)st.st_size) {
197 address = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
198 if (address == MAP_FAILED) {
199 address = NULL;
200 }
201 } else if (fd >= 0) {
202 close(fd);
203 }
Mark Salyzyn8b2c7de2015-04-01 07:42:01 -0700204
205 if (path[0] == '/') {
206 path++;
207 }
208
Mark Salyzyn7e826332015-04-15 22:30:30 +0000209 plen = strlen(path);
Mark Salyzyn68651142015-04-01 09:24:22 -0700210
211 if (address) {
212 const struct fs_path_config_from_file *p = (const struct fs_path_config_from_file *)
213 address;
214 const char *end = (const char *)address + st.st_size;
215 const struct fs_path_config_from_file *e = (const struct fs_path_config_from_file *)
216 (end - sizeof(*p));
217 uint16_t host_len = sizeof(*p);
218 for (; p < e; p = (const struct fs_path_config_from_file *)(((const char *)p) + host_len)) {
219 host_len = get2LE((const uint8_t *)&(p->len));
220 ssize_t len, remainder = host_len - sizeof(*p);
221 if (remainder <= 0) {
222 ALOGE("%s is truncated", dir ? conf_dir : conf_file);
223 p = e;
224 break;
225 }
226 len = (const char *)e - (const char *)p;
227 if (remainder > len) {
228 remainder = len;
229 }
230 len = strnlen(p->prefix, remainder);
231 if (len >= remainder) { /* missing a terminating null */
232 ALOGE("%s is corrupted", dir ? conf_dir : conf_file);
233 p = e;
234 break;
235 }
236 if (fs_config_cmp(dir, p->prefix, len, path, plen)) {
237 break;
238 }
Mark Salyzyn7e826332015-04-15 22:30:30 +0000239 }
Mark Salyzyn68651142015-04-01 09:24:22 -0700240 if (p < e) {
241 *uid = get2LE((const uint8_t *)&(p->uid));
242 *gid = get2LE((const uint8_t *)&(p->gid));
243 *mode = (*mode & (~07777)) | get2LE((const uint8_t *)&(p->mode));
244 *capabilities = get8LE((const uint8_t *)&(p->capabilities));
245 }
246 munmap(address, st.st_size);
247 close(fd);
248 if (p < e) {
249 return;
250 }
251 }
252
253 pc = dir ? android_dirs : android_files;
254 for(; pc->prefix; pc++){
255 if (fs_config_cmp(dir, pc->prefix, strlen(pc->prefix), path, plen)) {
256 break;
Mark Salyzyn8b2c7de2015-04-01 07:42:01 -0700257 }
258 }
259 *uid = pc->uid;
260 *gid = pc->gid;
261 *mode = (*mode & (~07777)) | pc->mode;
262 *capabilities = pc->capabilities;
263}