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