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 #include <private/fs_config.h>
18
19 // This file is used to define the properties of the filesystem
20 // images generated by build tools (mkbootfs and mkyaffs2image) and
21 // by the device side of adb.
22
23 #define LOG_TAG "fs_config"
24
25 #include <errno.h>
26 #include <fcntl.h>
27 #include <fnmatch.h>
28 #include <stdint.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <sys/stat.h>
33 #include <sys/types.h>
34
35 #include <string>
36
37 #include <android-base/strings.h>
38 #include <log/log.h>
39 #include <private/android_filesystem_config.h>
40 #include <utils/Compat.h>
41
42 #ifndef O_BINARY
43 #define O_BINARY 0
44 #endif
45
46 using android::base::EndsWith;
47 using android::base::StartsWith;
48
49 // My kingdom for <endian.h>
get2LE(const uint8_t * src)50 static inline uint16_t get2LE(const uint8_t* src) {
51 return src[0] | (src[1] << 8);
52 }
53
get8LE(const uint8_t * src)54 static inline uint64_t get8LE(const uint8_t* src) {
55 uint32_t low, high;
56
57 low = src[0] | (src[1] << 8) | (src[2] << 16) | (src[3] << 24);
58 high = src[4] | (src[5] << 8) | (src[6] << 16) | (src[7] << 24);
59 return ((uint64_t)high << 32) | (uint64_t)low;
60 }
61
62 #define ALIGN(x, alignment) (((x) + ((alignment)-1)) & ~((alignment)-1))
63
64 // Rules for directories.
65 // These rules are applied based on "first match", so they
66 // should start with the most specific path and work their
67 // way up to the root.
68
69 static const struct fs_path_config android_dirs[] = {
70 // clang-format off
71 { 00770, AID_SYSTEM, AID_CACHE, 0, "cache" },
72 { 00555, AID_ROOT, AID_ROOT, 0, "config" },
73 { 00771, AID_SYSTEM, AID_SYSTEM, 0, "data/app" },
74 { 00771, AID_SYSTEM, AID_SYSTEM, 0, "data/app-private" },
75 { 00771, AID_SYSTEM, AID_SYSTEM, 0, "data/app-ephemeral" },
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 { 00770, AID_DHCP, AID_DHCP, 0, "data/misc/dhcp" },
81 { 00771, AID_SHARED_RELRO, AID_SHARED_RELRO, 0, "data/misc/shared_relro" },
82 { 01771, AID_SYSTEM, AID_MISC, 0, "data/misc" },
83 { 00775, AID_MEDIA_RW, AID_MEDIA_RW, 0, "data/media/Music" },
84 { 00775, AID_MEDIA_RW, AID_MEDIA_RW, 0, "data/media" },
85 { 00750, AID_ROOT, AID_SHELL, 0, "data/nativetest" },
86 { 00750, AID_ROOT, AID_SHELL, 0, "data/nativetest64" },
87 { 00775, AID_ROOT, AID_ROOT, 0, "data/preloads" },
88 { 00771, AID_SYSTEM, AID_SYSTEM, 0, "data" },
89 { 00755, AID_ROOT, AID_SYSTEM, 0, "mnt" },
90 { 00751, AID_ROOT, AID_SHELL, 0, "product/bin" },
91 { 00750, AID_ROOT, AID_SHELL, 0, "sbin" },
92 { 00777, AID_ROOT, AID_ROOT, 0, "sdcard" },
93 { 00751, AID_ROOT, AID_SDCARD_R, 0, "storage" },
94 { 00751, AID_ROOT, AID_SHELL, 0, "system/bin" },
95 { 00755, AID_ROOT, AID_ROOT, 0, "system/etc/ppp" },
96 { 00755, AID_ROOT, AID_SHELL, 0, "system/vendor" },
97 { 00751, AID_ROOT, AID_SHELL, 0, "system/xbin" },
98 { 00755, AID_ROOT, AID_SHELL, 0, "system/apex/*/bin" },
99 { 00751, AID_ROOT, AID_SHELL, 0, "vendor/bin" },
100 { 00755, AID_ROOT, AID_SHELL, 0, "vendor" },
101 { 00755, AID_ROOT, AID_ROOT, 0, 0 },
102 // clang-format on
103 };
104 #ifndef __ANDROID_VNDK__
105 auto __for_testing_only__android_dirs = android_dirs;
106 #endif
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 static const char sys_conf_dir[] = "/system/etc/fs_config_dirs";
114 static const char sys_conf_file[] = "/system/etc/fs_config_files";
115 // No restrictions are placed on the vendor and oem file-system config files,
116 // although the developer is advised to restrict the scope to the /vendor or
117 // oem/ file-system since the intent is to provide support for customized
118 // portions of a separate vendor.img or oem.img. Has to remain open so that
119 // customization can also land on /system/vendor, /system/oem, /system/odm,
120 // /system/product or /system/product_services.
121 //
122 // We expect build-time checking or filtering when constructing the associated
123 // fs_config_* files (see build/tools/fs_config/fs_config_generate.c)
124 static const char ven_conf_dir[] = "/vendor/etc/fs_config_dirs";
125 static const char ven_conf_file[] = "/vendor/etc/fs_config_files";
126 static const char oem_conf_dir[] = "/oem/etc/fs_config_dirs";
127 static const char oem_conf_file[] = "/oem/etc/fs_config_files";
128 static const char odm_conf_dir[] = "/odm/etc/fs_config_dirs";
129 static const char odm_conf_file[] = "/odm/etc/fs_config_files";
130 static const char product_conf_dir[] = "/product/etc/fs_config_dirs";
131 static const char product_conf_file[] = "/product/etc/fs_config_files";
132 static const char product_services_conf_dir[] = "/product_services/etc/fs_config_dirs";
133 static const char product_services_conf_file[] = "/product_services/etc/fs_config_files";
134 static const char* conf[][2] = {
135 {sys_conf_file, sys_conf_dir},
136 {ven_conf_file, ven_conf_dir},
137 {oem_conf_file, oem_conf_dir},
138 {odm_conf_file, odm_conf_dir},
139 {product_conf_file, product_conf_dir},
140 {product_services_conf_file, product_services_conf_dir},
141 };
142
143 // Do not use android_files to grant Linux capabilities. Use ambient capabilities in their
144 // associated init.rc file instead. See https://source.android.com/devices/tech/config/ambient.
145
146 // Do not place any new vendor/, data/vendor/, etc entries in android_files.
147 // Vendor entries should be done via a vendor or device specific config.fs.
148 // See https://source.android.com/devices/tech/config/filesystem#using-file-system-capabilities
149 static const struct fs_path_config android_files[] = {
150 // clang-format off
151 { 00644, AID_SYSTEM, AID_SYSTEM, 0, "data/app/*" },
152 { 00644, AID_SYSTEM, AID_SYSTEM, 0, "data/app-ephemeral/*" },
153 { 00644, AID_SYSTEM, AID_SYSTEM, 0, "data/app-private/*" },
154 { 00644, AID_APP, AID_APP, 0, "data/data/*" },
155 { 00644, AID_MEDIA_RW, AID_MEDIA_RW, 0, "data/media/*" },
156 { 00640, AID_ROOT, AID_SHELL, 0, "data/nativetest/tests.txt" },
157 { 00640, AID_ROOT, AID_SHELL, 0, "data/nativetest64/tests.txt" },
158 { 00750, AID_ROOT, AID_SHELL, 0, "data/nativetest/*" },
159 { 00750, AID_ROOT, AID_SHELL, 0, "data/nativetest64/*" },
160 { 00600, AID_ROOT, AID_ROOT, 0, "default.prop" }, // legacy
161 { 00600, AID_ROOT, AID_ROOT, 0, "system/etc/prop.default" },
162 { 00600, AID_ROOT, AID_ROOT, 0, "odm/build.prop" }, // legacy; only for P release
163 { 00600, AID_ROOT, AID_ROOT, 0, "odm/default.prop" }, // legacy; only for P release
164 { 00600, AID_ROOT, AID_ROOT, 0, "odm/etc/build.prop" },
165 { 00444, AID_ROOT, AID_ROOT, 0, odm_conf_dir + 1 },
166 { 00444, AID_ROOT, AID_ROOT, 0, odm_conf_file + 1 },
167 { 00444, AID_ROOT, AID_ROOT, 0, oem_conf_dir + 1 },
168 { 00444, AID_ROOT, AID_ROOT, 0, oem_conf_file + 1 },
169 { 00600, AID_ROOT, AID_ROOT, 0, "product/build.prop" },
170 { 00444, AID_ROOT, AID_ROOT, 0, product_conf_dir + 1 },
171 { 00444, AID_ROOT, AID_ROOT, 0, product_conf_file + 1 },
172 { 00600, AID_ROOT, AID_ROOT, 0, "product_services/build.prop" },
173 { 00444, AID_ROOT, AID_ROOT, 0, product_services_conf_dir + 1 },
174 { 00444, AID_ROOT, AID_ROOT, 0, product_services_conf_file + 1 },
175 { 00750, AID_ROOT, AID_SHELL, 0, "sbin/fs_mgr" },
176 { 00755, AID_ROOT, AID_SHELL, 0, "system/bin/crash_dump32" },
177 { 00755, AID_ROOT, AID_SHELL, 0, "system/bin/crash_dump64" },
178 { 00755, AID_ROOT, AID_SHELL, 0, "system/bin/debuggerd" },
179 { 00750, AID_ROOT, AID_ROOT, 0, "system/bin/install-recovery.sh" },
180 { 00550, AID_LOGD, AID_LOGD, 0, "system/bin/logd" },
181 { 00700, AID_ROOT, AID_ROOT, 0, "system/bin/secilc" },
182 { 00750, AID_ROOT, AID_ROOT, 0, "system/bin/uncrypt" },
183 { 00600, AID_ROOT, AID_ROOT, 0, "system/build.prop" },
184 { 00444, AID_ROOT, AID_ROOT, 0, sys_conf_dir + 1 },
185 { 00444, AID_ROOT, AID_ROOT, 0, sys_conf_file + 1 },
186 { 00440, AID_ROOT, AID_SHELL, 0, "system/etc/init.goldfish.rc" },
187 { 00550, AID_ROOT, AID_SHELL, 0, "system/etc/init.goldfish.sh" },
188 { 00550, AID_ROOT, AID_SHELL, 0, "system/etc/init.ril" },
189 { 00555, AID_ROOT, AID_ROOT, 0, "system/etc/ppp/*" },
190 { 00555, AID_ROOT, AID_ROOT, 0, "system/etc/rc.*" },
191 { 00440, AID_ROOT, AID_ROOT, 0, "system/etc/recovery.img" },
192 { 00600, AID_ROOT, AID_ROOT, 0, "vendor/build.prop" },
193 { 00600, AID_ROOT, AID_ROOT, 0, "vendor/default.prop" },
194 { 00444, AID_ROOT, AID_ROOT, 0, ven_conf_dir + 1 },
195 { 00444, AID_ROOT, AID_ROOT, 0, ven_conf_file + 1 },
196
197 // the following two files are INTENTIONALLY set-uid, but they
198 // are NOT included on user builds.
199 { 06755, AID_ROOT, AID_ROOT, 0, "system/xbin/procmem" },
200 { 04750, AID_ROOT, AID_SHELL, 0, "system/xbin/su" },
201
202 // the following files have enhanced capabilities and ARE included
203 // in user builds.
204 { 00700, AID_SYSTEM, AID_SHELL, CAP_MASK_LONG(CAP_BLOCK_SUSPEND),
205 "system/bin/inputflinger" },
206 { 00750, AID_ROOT, AID_SHELL, CAP_MASK_LONG(CAP_SETUID) |
207 CAP_MASK_LONG(CAP_SETGID),
208 "system/bin/run-as" },
209 { 00750, AID_ROOT, AID_SHELL, CAP_MASK_LONG(CAP_SETUID) |
210 CAP_MASK_LONG(CAP_SETGID),
211 "system/bin/simpleperf_app_runner" },
212
213 // Support FIFO scheduling mode in SurfaceFlinger.
214 { 00755, AID_SYSTEM, AID_GRAPHICS, CAP_MASK_LONG(CAP_SYS_NICE),
215 "system/bin/surfaceflinger" },
216 // generic defaults
217 { 00755, AID_ROOT, AID_ROOT, 0, "bin/*" },
218 { 00640, AID_ROOT, AID_SHELL, 0, "fstab.*" },
219 { 00750, AID_ROOT, AID_SHELL, 0, "init*" },
220 { 00755, AID_ROOT, AID_SHELL, 0, "product/bin/*" },
221 { 00750, AID_ROOT, AID_SHELL, 0, "sbin/*" },
222 { 00755, AID_ROOT, AID_SHELL, 0, "system/bin/*" },
223 { 00755, AID_ROOT, AID_SHELL, 0, "system/xbin/*" },
224 { 00755, AID_ROOT, AID_SHELL, 0, "system/apex/*/bin/*" },
225 { 00755, AID_ROOT, AID_SHELL, 0, "vendor/bin/*" },
226 { 00755, AID_ROOT, AID_SHELL, 0, "vendor/xbin/*" },
227 { 00644, AID_ROOT, AID_ROOT, 0, 0 },
228 // clang-format on
229 };
230 #ifndef __ANDROID_VNDK__
231 auto __for_testing_only__android_files = android_files;
232 #endif
233
strip(const char * path,size_t len,const char suffix[])234 static size_t strip(const char* path, size_t len, const char suffix[]) {
235 if (len < strlen(suffix)) return len;
236 if (strncmp(path + len - strlen(suffix), suffix, strlen(suffix))) return len;
237 return len - strlen(suffix);
238 }
239
fs_config_open(int dir,int which,const char * target_out_path)240 static int fs_config_open(int dir, int which, const char* target_out_path) {
241 int fd = -1;
242
243 if (target_out_path && *target_out_path) {
244 // target_out_path is the path to the directory holding content of
245 // system partition but as we cannot guarantee it ends with '/system'
246 // or with or without a trailing slash, need to strip them carefully.
247 char* name = NULL;
248 size_t len = strlen(target_out_path);
249 len = strip(target_out_path, len, "/");
250 len = strip(target_out_path, len, "/system");
251 if (asprintf(&name, "%.*s%s", (int)len, target_out_path, conf[which][dir]) != -1) {
252 fd = TEMP_FAILURE_RETRY(open(name, O_RDONLY | O_BINARY));
253 free(name);
254 }
255 }
256 if (fd < 0) {
257 fd = TEMP_FAILURE_RETRY(open(conf[which][dir], O_RDONLY | O_BINARY));
258 }
259 return fd;
260 }
261
262 // if path is "odm/<stuff>", "oem/<stuff>", "product/<stuff>",
263 // "product_services/<stuff>" or "vendor/<stuff>"
is_partition(const std::string & path)264 static bool is_partition(const std::string& path) {
265 static const char* partitions[] = {"odm/", "oem/", "product/", "product_services/", "vendor/"};
266 for (size_t i = 0; i < (sizeof(partitions) / sizeof(partitions[0])); ++i) {
267 if (StartsWith(path, partitions[i])) return true;
268 }
269 return false;
270 }
271
272 // alias prefixes of "<partition>/<stuff>" to "system/<partition>/<stuff>" or
273 // "system/<partition>/<stuff>" to "<partition>/<stuff>"
fs_config_cmp(bool dir,const char * prefix,size_t len,const char * path,size_t plen)274 static bool fs_config_cmp(bool dir, const char* prefix, size_t len, const char* path, size_t plen) {
275 std::string pattern(prefix, len);
276 std::string input(path, plen);
277
278 // Massage pattern and input so that they can be used by fnmatch where
279 // directories have to end with /.
280 if (dir) {
281 if (!EndsWith(input, "/")) {
282 input.append("/");
283 }
284
285 if (!EndsWith(pattern, "/*")) {
286 if (EndsWith(pattern, "/")) {
287 pattern.append("*");
288 } else {
289 pattern.append("/*");
290 }
291 }
292 }
293
294 // no FNM_PATHNAME is set in order to match a/b/c/d with a/*
295 // FNM_ESCAPE is set in order to prevent using \\? and \\* and maintenance issues.
296 const int fnm_flags = FNM_NOESCAPE;
297 if (fnmatch(pattern.c_str(), input.c_str(), fnm_flags) == 0) return true;
298
299 static constexpr const char* kSystem = "system/";
300 if (StartsWith(input, kSystem)) {
301 input.erase(0, strlen(kSystem));
302 } else if (input.size() <= strlen(kSystem)) {
303 return false;
304 } else if (StartsWith(pattern, kSystem)) {
305 pattern.erase(0, strlen(kSystem));
306 } else {
307 return false;
308 }
309
310 if (!is_partition(pattern)) return false;
311 if (!is_partition(input)) return false;
312 return fnmatch(pattern.c_str(), input.c_str(), fnm_flags) == 0;
313 }
314 #ifndef __ANDROID_VNDK__
315 auto __for_testing_only__fs_config_cmp = fs_config_cmp;
316 #endif
317
fs_config(const char * path,int dir,const char * target_out_path,unsigned * uid,unsigned * gid,unsigned * mode,uint64_t * capabilities)318 void fs_config(const char* path, int dir, const char* target_out_path, unsigned* uid, unsigned* gid,
319 unsigned* mode, uint64_t* capabilities) {
320 const struct fs_path_config* pc;
321 size_t which, plen;
322
323 if (path[0] == '/') {
324 path++;
325 }
326
327 plen = strlen(path);
328
329 for (which = 0; which < (sizeof(conf) / sizeof(conf[0])); ++which) {
330 struct fs_path_config_from_file header;
331
332 int fd = fs_config_open(dir, which, target_out_path);
333 if (fd < 0) continue;
334
335 while (TEMP_FAILURE_RETRY(read(fd, &header, sizeof(header))) == sizeof(header)) {
336 char* prefix;
337 uint16_t host_len = get2LE((const uint8_t*)&header.len);
338 ssize_t len, remainder = host_len - sizeof(header);
339 if (remainder <= 0) {
340 ALOGE("%s len is corrupted", conf[which][dir]);
341 break;
342 }
343 prefix = static_cast<char*>(calloc(1, remainder));
344 if (!prefix) {
345 ALOGE("%s out of memory", conf[which][dir]);
346 break;
347 }
348 if (TEMP_FAILURE_RETRY(read(fd, prefix, remainder)) != remainder) {
349 free(prefix);
350 ALOGE("%s prefix is truncated", conf[which][dir]);
351 break;
352 }
353 len = strnlen(prefix, remainder);
354 if (len >= remainder) { // missing a terminating null
355 free(prefix);
356 ALOGE("%s is corrupted", conf[which][dir]);
357 break;
358 }
359 if (fs_config_cmp(dir, prefix, len, path, plen)) {
360 free(prefix);
361 close(fd);
362 *uid = get2LE((const uint8_t*)&(header.uid));
363 *gid = get2LE((const uint8_t*)&(header.gid));
364 *mode = (*mode & (~07777)) | get2LE((const uint8_t*)&(header.mode));
365 *capabilities = get8LE((const uint8_t*)&(header.capabilities));
366 return;
367 }
368 free(prefix);
369 }
370 close(fd);
371 }
372
373 for (pc = dir ? android_dirs : android_files; pc->prefix; pc++) {
374 if (fs_config_cmp(dir, pc->prefix, strlen(pc->prefix), path, plen)) {
375 break;
376 }
377 }
378 *uid = pc->uid;
379 *gid = pc->gid;
380 *mode = (*mode & (~07777)) | pc->mode;
381 *capabilities = pc->capabilities;
382 }
383
fs_config_generate(char * buffer,size_t length,const struct fs_path_config * pc)384 ssize_t fs_config_generate(char* buffer, size_t length, const struct fs_path_config* pc) {
385 struct fs_path_config_from_file* p = (struct fs_path_config_from_file*)buffer;
386 size_t len = ALIGN(sizeof(*p) + strlen(pc->prefix) + 1, sizeof(uint64_t));
387
388 if ((length < len) || (len > UINT16_MAX)) {
389 return -ENOSPC;
390 }
391 memset(p, 0, len);
392 uint16_t host_len = len;
393 p->len = get2LE((const uint8_t*)&host_len);
394 p->mode = get2LE((const uint8_t*)&(pc->mode));
395 p->uid = get2LE((const uint8_t*)&(pc->uid));
396 p->gid = get2LE((const uint8_t*)&(pc->gid));
397 p->capabilities = get8LE((const uint8_t*)&(pc->capabilities));
398 strcpy(p->prefix, pc->prefix);
399 return len;
400 }
401