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