blob: 7fd66cdfdc213cc2b307c7ad869741a4692af9d3 [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 Salyzyn5d9e5ef2015-04-01 11:02:00 -070066#define ALIGN(x, alignment) ( ((x) + ((alignment) - 1)) & ~((alignment) - 1) )
67
Mark Salyzyn8b2c7de2015-04-01 07:42:01 -070068/* Rules for directories.
69** These rules are applied based on "first match", so they
70** should start with the most specific path and work their
71** way up to the root.
72*/
73
74static const struct fs_path_config android_dirs[] = {
75 { 00770, AID_SYSTEM, AID_CACHE, 0, "cache" },
76 { 00771, AID_SYSTEM, AID_SYSTEM, 0, "data/app" },
77 { 00771, AID_SYSTEM, AID_SYSTEM, 0, "data/app-private" },
78 { 00771, AID_ROOT, AID_ROOT, 0, "data/dalvik-cache" },
79 { 00771, AID_SYSTEM, AID_SYSTEM, 0, "data/data" },
80 { 00771, AID_SHELL, AID_SHELL, 0, "data/local/tmp" },
81 { 00771, AID_SHELL, AID_SHELL, 0, "data/local" },
82 { 01771, AID_SYSTEM, AID_MISC, 0, "data/misc" },
83 { 00770, AID_DHCP, AID_DHCP, 0, "data/misc/dhcp" },
84 { 00771, AID_SHARED_RELRO, AID_SHARED_RELRO, 0, "data/misc/shared_relro" },
85 { 00775, AID_MEDIA_RW, AID_MEDIA_RW, 0, "data/media" },
86 { 00775, AID_MEDIA_RW, AID_MEDIA_RW, 0, "data/media/Music" },
87 { 00771, AID_SYSTEM, AID_SYSTEM, 0, "data" },
88 { 00750, AID_ROOT, AID_SHELL, 0, "sbin" },
89 { 00755, AID_ROOT, AID_SHELL, 0, "system/bin" },
90 { 00755, AID_ROOT, AID_SHELL, 0, "system/vendor" },
91 { 00755, AID_ROOT, AID_SHELL, 0, "system/xbin" },
92 { 00755, AID_ROOT, AID_ROOT, 0, "system/etc/ppp" },
93 { 00755, AID_ROOT, AID_SHELL, 0, "vendor" },
94 { 00777, AID_ROOT, AID_ROOT, 0, "sdcard" },
95 { 00755, AID_ROOT, AID_ROOT, 0, 0 },
96};
97
98/* Rules for files.
99** These rules are applied based on "first match", so they
100** should start with the most specific path and work their
101** way up to the root. Prefixes ending in * denotes wildcard
102** and will allow partial matches.
103*/
Mark Salyzyn68651142015-04-01 09:24:22 -0700104static const char conf_dir[] = "/system/etc/fs_config_dirs";
105static const char conf_file[] = "/system/etc/fs_config_files";
106
Mark Salyzyn8b2c7de2015-04-01 07:42:01 -0700107static const struct fs_path_config android_files[] = {
108 { 00440, AID_ROOT, AID_SHELL, 0, "system/etc/init.goldfish.rc" },
109 { 00550, AID_ROOT, AID_SHELL, 0, "system/etc/init.goldfish.sh" },
110 { 00550, AID_ROOT, AID_SHELL, 0, "system/etc/init.ril" },
111 { 00550, AID_DHCP, AID_SHELL, 0, "system/etc/dhcpcd/dhcpcd-run-hooks" },
112 { 00555, AID_ROOT, AID_ROOT, 0, "system/etc/ppp/*" },
113 { 00555, AID_ROOT, AID_ROOT, 0, "system/etc/rc.*" },
Mark Salyzyn68651142015-04-01 09:24:22 -0700114 { 00444, AID_ROOT, AID_ROOT, 0, conf_dir + 1 },
115 { 00444, AID_ROOT, AID_ROOT, 0, conf_file + 1 },
Mark Salyzyn8b2c7de2015-04-01 07:42:01 -0700116 { 00644, AID_SYSTEM, AID_SYSTEM, 0, "data/app/*" },
117 { 00644, AID_MEDIA_RW, AID_MEDIA_RW, 0, "data/media/*" },
118 { 00644, AID_SYSTEM, AID_SYSTEM, 0, "data/app-private/*" },
119 { 00644, AID_APP, AID_APP, 0, "data/data/*" },
120
121 /* the following five files are INTENTIONALLY set-uid, but they
122 * are NOT included on user builds. */
123 { 04750, AID_ROOT, AID_SHELL, 0, "system/xbin/su" },
124 { 06755, AID_ROOT, AID_ROOT, 0, "system/xbin/librank" },
125 { 06755, AID_ROOT, AID_ROOT, 0, "system/xbin/procrank" },
126 { 06755, AID_ROOT, AID_ROOT, 0, "system/xbin/procmem" },
127 { 04770, AID_ROOT, AID_RADIO, 0, "system/bin/pppd-ril" },
128
129 /* the following files have enhanced capabilities and ARE included in user builds. */
130 { 00750, AID_ROOT, AID_SHELL, (1ULL << CAP_SETUID) | (1ULL << CAP_SETGID), "system/bin/run-as" },
131 { 00700, AID_SYSTEM, AID_SHELL, (1ULL << CAP_BLOCK_SUSPEND), "system/bin/inputflinger" },
132
133 { 00750, AID_ROOT, AID_ROOT, 0, "system/bin/uncrypt" },
134 { 00750, AID_ROOT, AID_ROOT, 0, "system/bin/install-recovery.sh" },
135 { 00755, AID_ROOT, AID_SHELL, 0, "system/bin/*" },
136 { 00755, AID_ROOT, AID_ROOT, 0, "system/lib/valgrind/*" },
137 { 00755, AID_ROOT, AID_ROOT, 0, "system/lib64/valgrind/*" },
138 { 00755, AID_ROOT, AID_SHELL, 0, "system/xbin/*" },
139 { 00755, AID_ROOT, AID_SHELL, 0, "system/vendor/bin/*" },
140 { 00755, AID_ROOT, AID_SHELL, 0, "vendor/bin/*" },
141 { 00750, AID_ROOT, AID_SHELL, 0, "sbin/*" },
142 { 00755, AID_ROOT, AID_ROOT, 0, "bin/*" },
143 { 00750, AID_ROOT, AID_SHELL, 0, "init*" },
144 { 00750, AID_ROOT, AID_SHELL, 0, "sbin/fs_mgr" },
145 { 00640, AID_ROOT, AID_SHELL, 0, "fstab.*" },
146 { 00644, AID_ROOT, AID_ROOT, 0, 0 },
147};
148
Mark Salyzyn68651142015-04-01 09:24:22 -0700149static int fs_config_open(int dir)
150{
151 int fd = -1;
152
153 const char *out = getenv("OUT");
154 if (out && *out) {
155 char *name = NULL;
156 asprintf(&name, "%s%s", out, dir ? conf_dir : conf_file);
157 if (name) {
158 fd = TEMP_FAILURE_RETRY(open(name, O_RDONLY));
159 free(name);
160 }
161 }
162 if (fd < 0) {
163 fd = TEMP_FAILURE_RETRY(open(dir ? conf_dir : conf_file, O_RDONLY));
164 }
165 return fd;
166}
167
168static bool fs_config_cmp(bool dir, const char *prefix, size_t len,
169 const char *path, size_t plen)
170{
171 if (dir) {
172 if (plen < len) {
173 return false;
174 }
175 } else {
176 /* If name ends in * then allow partial matches. */
177 if (prefix[len - 1] == '*') {
178 return !strncmp(prefix, path, len - 1);
179 }
180 if (plen != len) {
181 return false;
182 }
183 }
184 return !strncmp(prefix, path, len);
185}
186
Mark Salyzyn8b2c7de2015-04-01 07:42:01 -0700187void fs_config(const char *path, int dir,
188 unsigned *uid, unsigned *gid, unsigned *mode, uint64_t *capabilities)
189{
190 const struct fs_path_config *pc;
191 int plen;
Mark Salyzyn68651142015-04-01 09:24:22 -0700192 struct stat st;
193 void *address = NULL;
194
195 int fd = fs_config_open(dir);
196 if ((fd >= 0)
197 && (TEMP_FAILURE_RETRY(fstat(fd, &st)) >= 0)
198 && (size_t)st.st_size) {
199 address = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
200 if (address == MAP_FAILED) {
201 address = NULL;
202 }
203 } else if (fd >= 0) {
204 close(fd);
205 }
Mark Salyzyn8b2c7de2015-04-01 07:42:01 -0700206
207 if (path[0] == '/') {
208 path++;
209 }
210
Mark Salyzyn7e826332015-04-15 22:30:30 +0000211 plen = strlen(path);
Mark Salyzyn68651142015-04-01 09:24:22 -0700212
213 if (address) {
214 const struct fs_path_config_from_file *p = (const struct fs_path_config_from_file *)
215 address;
216 const char *end = (const char *)address + st.st_size;
217 const struct fs_path_config_from_file *e = (const struct fs_path_config_from_file *)
218 (end - sizeof(*p));
219 uint16_t host_len = sizeof(*p);
220 for (; p < e; p = (const struct fs_path_config_from_file *)(((const char *)p) + host_len)) {
221 host_len = get2LE((const uint8_t *)&(p->len));
222 ssize_t len, remainder = host_len - sizeof(*p);
223 if (remainder <= 0) {
224 ALOGE("%s is truncated", dir ? conf_dir : conf_file);
225 p = e;
226 break;
227 }
228 len = (const char *)e - (const char *)p;
229 if (remainder > len) {
230 remainder = len;
231 }
232 len = strnlen(p->prefix, remainder);
233 if (len >= remainder) { /* missing a terminating null */
234 ALOGE("%s is corrupted", dir ? conf_dir : conf_file);
235 p = e;
236 break;
237 }
238 if (fs_config_cmp(dir, p->prefix, len, path, plen)) {
239 break;
240 }
Mark Salyzyn7e826332015-04-15 22:30:30 +0000241 }
Mark Salyzyn68651142015-04-01 09:24:22 -0700242 if (p < e) {
243 *uid = get2LE((const uint8_t *)&(p->uid));
244 *gid = get2LE((const uint8_t *)&(p->gid));
245 *mode = (*mode & (~07777)) | get2LE((const uint8_t *)&(p->mode));
246 *capabilities = get8LE((const uint8_t *)&(p->capabilities));
247 }
248 munmap(address, st.st_size);
249 close(fd);
250 if (p < e) {
251 return;
252 }
253 }
254
255 pc = dir ? android_dirs : android_files;
256 for(; pc->prefix; pc++){
257 if (fs_config_cmp(dir, pc->prefix, strlen(pc->prefix), path, plen)) {
258 break;
Mark Salyzyn8b2c7de2015-04-01 07:42:01 -0700259 }
260 }
261 *uid = pc->uid;
262 *gid = pc->gid;
263 *mode = (*mode & (~07777)) | pc->mode;
264 *capabilities = pc->capabilities;
265}
Mark Salyzyn5d9e5ef2015-04-01 11:02:00 -0700266
267ssize_t fs_config_generate(char *buffer, size_t length, const struct fs_path_config *pc)
268{
269 struct fs_path_config_from_file *p = (struct fs_path_config_from_file *)buffer;
270 size_t len = ALIGN(sizeof(*p) + strlen(pc->prefix) + 1, sizeof(uint64_t));
271
272 if ((length < len) || (len > UINT16_MAX)) {
273 return -ENOSPC;
274 }
275 memset(p, 0, len);
276 uint16_t host_len = len;
277 p->len = get2LE((const uint8_t *)&host_len);
278 p->mode = get2LE((const uint8_t *)&(pc->mode));
279 p->uid = get2LE((const uint8_t *)&(pc->uid));
280 p->gid = get2LE((const uint8_t *)&(pc->gid));
281 p->capabilities = get8LE((const uint8_t *)&(pc->capabilities));
282 strcpy(p->prefix, pc->prefix);
283 return len;
284}