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