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