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