blob: 618481361432439e203950aa2209f78044944591 [file] [log] [blame]
Mark Salyzyn8b2c7de2015-04-01 07:42:01 -07001/*
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
Elliott Hughes8e9aeb92017-11-10 10:22:07 -080017#include <private/fs_config.h>
18
Mark Salyzyn163ecc62017-05-02 08:56:15 -070019// 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.
Mark Salyzyn8b2c7de2015-04-01 07:42:01 -070022
Mark Salyzyn68651142015-04-01 09:24:22 -070023#define LOG_TAG "fs_config"
Mark Salyzyn4e5f71a2015-04-01 09:24:22 -070024
Mark Salyzyn68651142015-04-01 09:24:22 -070025#include <errno.h>
26#include <fcntl.h>
Jiyong Parka2159c42019-02-03 00:34:29 +090027#include <fnmatch.h>
Mark Salyzyn68651142015-04-01 09:24:22 -070028#include <stdint.h>
29#include <stdio.h>
30#include <stdlib.h>
31#include <string.h>
Mark Salyzyn68651142015-04-01 09:24:22 -070032#include <sys/stat.h>
33#include <sys/types.h>
34
Jiyong Parka2159c42019-02-03 00:34:29 +090035#include <string>
36
37#include <android-base/strings.h>
Mark Salyzyn30f991f2017-01-10 13:19:54 -080038#include <log/log.h>
Mark Salyzyn8b2c7de2015-04-01 07:42:01 -070039#include <private/android_filesystem_config.h>
Mark Salyzynfd5b4252015-04-16 08:13:32 -070040#include <utils/Compat.h>
Mark Salyzyn8b2c7de2015-04-01 07:42:01 -070041
Mark Salyzyn4232b372015-04-16 08:40:55 -070042#ifndef O_BINARY
43#define O_BINARY 0
44#endif
45
Jiyong Parka2159c42019-02-03 00:34:29 +090046using android::base::EndsWith;
47using android::base::StartsWith;
48
Mark Salyzyn163ecc62017-05-02 08:56:15 -070049// My kingdom for <endian.h>
50static inline uint16_t get2LE(const uint8_t* src) {
51 return src[0] | (src[1] << 8);
52}
Mark Salyzyn68651142015-04-01 09:24:22 -070053
Mark Salyzyn166e24c2017-03-20 08:15:40 -070054static inline uint64_t get8LE(const uint8_t* src) {
Mark Salyzyn68651142015-04-01 09:24:22 -070055 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);
Mark Salyzyn166e24c2017-03-20 08:15:40 -070059 return ((uint64_t)high << 32) | (uint64_t)low;
Mark Salyzyn68651142015-04-01 09:24:22 -070060}
61
Mark Salyzyn166e24c2017-03-20 08:15:40 -070062#define ALIGN(x, alignment) (((x) + ((alignment)-1)) & ~((alignment)-1))
Mark Salyzyn5d9e5ef2015-04-01 11:02:00 -070063
Mark Salyzyn163ecc62017-05-02 08:56:15 -070064// 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.
Mark Salyzyn8b2c7de2015-04-01 07:42:01 -070068
69static const struct fs_path_config android_dirs[] = {
Nick Kralevichf4fc9222018-11-11 08:39:20 -080070 // clang-format off
Mark Salyzync4f9f4c2017-03-20 08:15:40 -070071 { 00770, AID_SYSTEM, AID_CACHE, 0, "cache" },
Gwendal Grignou85264132018-02-07 10:55:56 -080072 { 00555, AID_ROOT, AID_ROOT, 0, "config" },
Mark Salyzync4f9f4c2017-03-20 08:15:40 -070073 { 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" },
Mark Salyzyn8b2c7de2015-04-01 07:42:01 -070081 { 00771, AID_SHARED_RELRO, AID_SHARED_RELRO, 0, "data/misc/shared_relro" },
Mark Salyzync4f9f4c2017-03-20 08:15:40 -070082 { 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" },
Nick Kralevichf4fc9222018-11-11 08:39:20 -080090 { 00751, AID_ROOT, AID_SHELL, 0, "product/bin" },
Mark Salyzync4f9f4c2017-03-20 08:15:40 -070091 { 00750, AID_ROOT, AID_SHELL, 0, "sbin" },
92 { 00777, AID_ROOT, AID_ROOT, 0, "sdcard" },
93 { 00751, AID_ROOT, AID_SDCARD_R, 0, "storage" },
Nick Kralevich53842f82018-12-13 10:56:33 -080094 { 00755, AID_ROOT, AID_SHELL, 0, "system/bin" },
Mark Salyzync4f9f4c2017-03-20 08:15:40 -070095 { 00755, AID_ROOT, AID_ROOT, 0, "system/etc/ppp" },
96 { 00755, AID_ROOT, AID_SHELL, 0, "system/vendor" },
Nick Kralevichf4fc9222018-11-11 08:39:20 -080097 { 00751, AID_ROOT, AID_SHELL, 0, "system/xbin" },
Jiyong Park3435c882019-02-01 20:46:22 +090098 // TODO(b/123743953): eliminate the APEX name with better pattern matching
99 { 00755, AID_ROOT, AID_SHELL, 0, "system/apex/com.android.runtime.debug/bin" },
100 { 00755, AID_ROOT, AID_SHELL, 0, "system/apex/com.android.runtime.release/bin" },
Nick Kralevichf4fc9222018-11-11 08:39:20 -0800101 { 00751, AID_ROOT, AID_SHELL, 0, "vendor/bin" },
Mark Salyzync4f9f4c2017-03-20 08:15:40 -0700102 { 00755, AID_ROOT, AID_SHELL, 0, "vendor" },
103 { 00755, AID_ROOT, AID_ROOT, 0, 0 },
Nick Kralevichf4fc9222018-11-11 08:39:20 -0800104 // clang-format on
Mark Salyzyn8b2c7de2015-04-01 07:42:01 -0700105};
Mark Salyzyn0f6a2702017-05-02 08:56:15 -0700106#ifndef __ANDROID_VNDK__
107auto __for_testing_only__android_dirs = android_dirs;
108#endif
Mark Salyzyn8b2c7de2015-04-01 07:42:01 -0700109
Mark Salyzyn163ecc62017-05-02 08:56:15 -0700110// Rules for files.
111// These rules are applied based on "first match", so they
112// should start with the most specific path and work their
113// way up to the root. Prefixes ending in * denotes wildcard
114// and will allow partial matches.
Mark Salyzyn9ceadcb2017-03-20 08:15:40 -0700115static const char sys_conf_dir[] = "/system/etc/fs_config_dirs";
116static const char sys_conf_file[] = "/system/etc/fs_config_files";
Mark Salyzyn163ecc62017-05-02 08:56:15 -0700117// No restrictions are placed on the vendor and oem file-system config files,
118// although the developer is advised to restrict the scope to the /vendor or
119// oem/ file-system since the intent is to provide support for customized
120// portions of a separate vendor.img or oem.img. Has to remain open so that
Mark Salyzyn757658c2018-11-19 11:17:35 -0800121// customization can also land on /system/vendor, /system/oem, /system/odm,
122// /system/product or /system/product_services.
123//
Mark Salyzyn163ecc62017-05-02 08:56:15 -0700124// We expect build-time checking or filtering when constructing the associated
125// fs_config_* files (see build/tools/fs_config/fs_config_generate.c)
Mark Salyzyn9ceadcb2017-03-20 08:15:40 -0700126static const char ven_conf_dir[] = "/vendor/etc/fs_config_dirs";
127static const char ven_conf_file[] = "/vendor/etc/fs_config_files";
Mark Salyzyn62c701e2017-03-21 08:09:52 -0700128static const char oem_conf_dir[] = "/oem/etc/fs_config_dirs";
129static const char oem_conf_file[] = "/oem/etc/fs_config_files";
Mark Salyzyn2b616ce2017-03-22 15:23:24 -0700130static const char odm_conf_dir[] = "/odm/etc/fs_config_dirs";
131static const char odm_conf_file[] = "/odm/etc/fs_config_files";
Mark Salyzyn757658c2018-11-19 11:17:35 -0800132static const char product_conf_dir[] = "/product/etc/fs_config_dirs";
133static const char product_conf_file[] = "/product/etc/fs_config_files";
134static const char product_services_conf_dir[] = "/product_services/etc/fs_config_dirs";
135static const char product_services_conf_file[] = "/product_services/etc/fs_config_files";
Mark Salyzyn9ceadcb2017-03-20 08:15:40 -0700136static const char* conf[][2] = {
Mark Salyzyn757658c2018-11-19 11:17:35 -0800137 {sys_conf_file, sys_conf_dir},
138 {ven_conf_file, ven_conf_dir},
139 {oem_conf_file, oem_conf_dir},
140 {odm_conf_file, odm_conf_dir},
141 {product_conf_file, product_conf_dir},
142 {product_services_conf_file, product_services_conf_dir},
Mark Salyzyn9ceadcb2017-03-20 08:15:40 -0700143};
Mark Salyzyn68651142015-04-01 09:24:22 -0700144
Tom Cherry07aa2a62018-03-28 13:25:11 -0700145// Do not use android_files to grant Linux capabilities. Use ambient capabilities in their
146// associated init.rc file instead. See https://source.android.com/devices/tech/config/ambient.
147
148// Do not place any new vendor/, data/vendor/, etc entries in android_files.
149// Vendor entries should be done via a vendor or device specific config.fs.
150// See https://source.android.com/devices/tech/config/filesystem#using-file-system-capabilities
Mark Salyzyn8b2c7de2015-04-01 07:42:01 -0700151static const struct fs_path_config android_files[] = {
Dario Freni4fa866a2018-05-25 16:07:19 +0100152 // clang-format off
Mark Salyzyna45eae92017-03-20 08:53:24 -0700153 { 00644, AID_SYSTEM, AID_SYSTEM, 0, "data/app/*" },
154 { 00644, AID_SYSTEM, AID_SYSTEM, 0, "data/app-ephemeral/*" },
155 { 00644, AID_SYSTEM, AID_SYSTEM, 0, "data/app-private/*" },
156 { 00644, AID_APP, AID_APP, 0, "data/data/*" },
157 { 00644, AID_MEDIA_RW, AID_MEDIA_RW, 0, "data/media/*" },
158 { 00640, AID_ROOT, AID_SHELL, 0, "data/nativetest/tests.txt" },
159 { 00640, AID_ROOT, AID_SHELL, 0, "data/nativetest64/tests.txt" },
160 { 00750, AID_ROOT, AID_SHELL, 0, "data/nativetest/*" },
161 { 00750, AID_ROOT, AID_SHELL, 0, "data/nativetest64/*" },
Hung-ying Tyan527d80d2017-06-02 18:59:46 +0800162 { 00600, AID_ROOT, AID_ROOT, 0, "default.prop" }, // legacy
163 { 00600, AID_ROOT, AID_ROOT, 0, "system/etc/prop.default" },
Hung-ying Tyan33463382017-05-25 19:18:17 +0800164 { 00600, AID_ROOT, AID_ROOT, 0, "odm/build.prop" },
165 { 00600, AID_ROOT, AID_ROOT, 0, "odm/default.prop" },
Mark Salyzyn2b616ce2017-03-22 15:23:24 -0700166 { 00444, AID_ROOT, AID_ROOT, 0, odm_conf_dir + 1 },
167 { 00444, AID_ROOT, AID_ROOT, 0, odm_conf_file + 1 },
Mark Salyzyn62c701e2017-03-21 08:09:52 -0700168 { 00444, AID_ROOT, AID_ROOT, 0, oem_conf_dir + 1 },
169 { 00444, AID_ROOT, AID_ROOT, 0, oem_conf_file + 1 },
Jaekyun Seokdff165d2017-11-28 12:10:10 +0900170 { 00600, AID_ROOT, AID_ROOT, 0, "product/build.prop" },
Mark Salyzyn757658c2018-11-19 11:17:35 -0800171 { 00444, AID_ROOT, AID_ROOT, 0, product_conf_dir + 1 },
172 { 00444, AID_ROOT, AID_ROOT, 0, product_conf_file + 1 },
Dario Freniab5583b2018-08-17 01:01:25 +0100173 { 00600, AID_ROOT, AID_ROOT, 0, "product_services/build.prop" },
Mark Salyzyn757658c2018-11-19 11:17:35 -0800174 { 00444, AID_ROOT, AID_ROOT, 0, product_services_conf_dir + 1 },
175 { 00444, AID_ROOT, AID_ROOT, 0, product_services_conf_file + 1 },
Mark Salyzyna45eae92017-03-20 08:53:24 -0700176 { 00750, AID_ROOT, AID_SHELL, 0, "sbin/fs_mgr" },
177 { 00755, AID_ROOT, AID_SHELL, 0, "system/bin/crash_dump32" },
178 { 00755, AID_ROOT, AID_SHELL, 0, "system/bin/crash_dump64" },
179 { 00755, AID_ROOT, AID_SHELL, 0, "system/bin/debuggerd" },
180 { 00750, AID_ROOT, AID_ROOT, 0, "system/bin/install-recovery.sh" },
Luis Hector Chavezd32c36c2018-06-08 15:00:40 -0700181 { 00550, AID_LOGD, AID_LOGD, 0, "system/bin/logd" },
Mark Salyzyna45eae92017-03-20 08:53:24 -0700182 { 00700, AID_ROOT, AID_ROOT, 0, "system/bin/secilc" },
183 { 00750, AID_ROOT, AID_ROOT, 0, "system/bin/uncrypt" },
Hung-ying Tyan33463382017-05-25 19:18:17 +0800184 { 00600, AID_ROOT, AID_ROOT, 0, "system/build.prop" },
Mark Salyzyn9ceadcb2017-03-20 08:15:40 -0700185 { 00444, AID_ROOT, AID_ROOT, 0, sys_conf_dir + 1 },
186 { 00444, AID_ROOT, AID_ROOT, 0, sys_conf_file + 1 },
Mark Salyzyn8b2c7de2015-04-01 07:42:01 -0700187 { 00440, AID_ROOT, AID_SHELL, 0, "system/etc/init.goldfish.rc" },
188 { 00550, AID_ROOT, AID_SHELL, 0, "system/etc/init.goldfish.sh" },
189 { 00550, AID_ROOT, AID_SHELL, 0, "system/etc/init.ril" },
Mark Salyzyn8b2c7de2015-04-01 07:42:01 -0700190 { 00555, AID_ROOT, AID_ROOT, 0, "system/etc/ppp/*" },
191 { 00555, AID_ROOT, AID_ROOT, 0, "system/etc/rc.*" },
Tao Baoc6e93fe2015-07-18 19:46:16 -0700192 { 00440, AID_ROOT, AID_ROOT, 0, "system/etc/recovery.img" },
Hung-ying Tyan33463382017-05-25 19:18:17 +0800193 { 00600, AID_ROOT, AID_ROOT, 0, "vendor/build.prop" },
194 { 00600, AID_ROOT, AID_ROOT, 0, "vendor/default.prop" },
Mark Salyzyn9ceadcb2017-03-20 08:15:40 -0700195 { 00444, AID_ROOT, AID_ROOT, 0, ven_conf_dir + 1 },
196 { 00444, AID_ROOT, AID_ROOT, 0, ven_conf_file + 1 },
Mark Salyzyn8b2c7de2015-04-01 07:42:01 -0700197
Mark Salyzyn163ecc62017-05-02 08:56:15 -0700198 // the following two files are INTENTIONALLY set-uid, but they
199 // are NOT included on user builds.
Mark Salyzyn8b2c7de2015-04-01 07:42:01 -0700200 { 06755, AID_ROOT, AID_ROOT, 0, "system/xbin/procmem" },
Mark Salyzyna45eae92017-03-20 08:53:24 -0700201 { 04750, AID_ROOT, AID_SHELL, 0, "system/xbin/su" },
Mark Salyzyn8b2c7de2015-04-01 07:42:01 -0700202
Mark Salyzyn163ecc62017-05-02 08:56:15 -0700203 // the following files have enhanced capabilities and ARE included
204 // in user builds.
Mark Salyzyna45eae92017-03-20 08:53:24 -0700205 { 00700, AID_SYSTEM, AID_SHELL, CAP_MASK_LONG(CAP_BLOCK_SUSPEND),
206 "system/bin/inputflinger" },
Mark Salyzyn0d2a1dc2016-10-28 12:41:17 -0700207 { 00750, AID_ROOT, AID_SHELL, CAP_MASK_LONG(CAP_SETUID) |
208 CAP_MASK_LONG(CAP_SETGID),
209 "system/bin/run-as" },
Yabin Cuiaf3e30d2019-01-08 16:31:56 -0800210 { 00750, AID_ROOT, AID_SHELL, CAP_MASK_LONG(CAP_SETUID) |
211 CAP_MASK_LONG(CAP_SETGID),
212 "system/bin/simpleperf_app_runner" },
Mark Salyzyn8b2c7de2015-04-01 07:42:01 -0700213
Mark Salyzyn163ecc62017-05-02 08:56:15 -0700214 // Support FIFO scheduling mode in SurfaceFlinger.
Mark Salyzyn325aa702016-10-28 12:41:17 -0700215 { 00755, AID_SYSTEM, AID_GRAPHICS, CAP_MASK_LONG(CAP_SYS_NICE),
216 "system/bin/surfaceflinger" },
Mark Salyzyn163ecc62017-05-02 08:56:15 -0700217 // generic defaults
Mark Salyzyna45eae92017-03-20 08:53:24 -0700218 { 00755, AID_ROOT, AID_ROOT, 0, "bin/*" },
219 { 00640, AID_ROOT, AID_SHELL, 0, "fstab.*" },
220 { 00750, AID_ROOT, AID_SHELL, 0, "init*" },
Inseob Kim25195da2018-05-31 13:03:58 +0900221 { 00755, AID_ROOT, AID_SHELL, 0, "product/bin/*" },
Mark Salyzyna45eae92017-03-20 08:53:24 -0700222 { 00750, AID_ROOT, AID_SHELL, 0, "sbin/*" },
Mark Salyzyn8b2c7de2015-04-01 07:42:01 -0700223 { 00755, AID_ROOT, AID_SHELL, 0, "system/bin/*" },
224 { 00755, AID_ROOT, AID_ROOT, 0, "system/lib/valgrind/*" },
225 { 00755, AID_ROOT, AID_ROOT, 0, "system/lib64/valgrind/*" },
Mark Salyzyna45eae92017-03-20 08:53:24 -0700226 { 00755, AID_ROOT, AID_SHELL, 0, "system/xbin/*" },
Jiyong Park3435c882019-02-01 20:46:22 +0900227 // TODO(b/123743953): eliminate the APEX name with better pattern matching
228 { 00755, AID_ROOT, AID_SHELL, 0, "system/apex/com.android.runtime.debug/bin/*" },
229 { 00755, AID_ROOT, AID_SHELL, 0, "system/apex/com.android.runtime.release/bin/*" },
Mark Salyzyn8b2c7de2015-04-01 07:42:01 -0700230 { 00755, AID_ROOT, AID_SHELL, 0, "vendor/bin/*" },
Erik Klinea5a9c742016-05-27 13:13:07 +0900231 { 00755, AID_ROOT, AID_SHELL, 0, "vendor/xbin/*" },
Mark Salyzyn8b2c7de2015-04-01 07:42:01 -0700232 { 00644, AID_ROOT, AID_ROOT, 0, 0 },
Dario Freni4fa866a2018-05-25 16:07:19 +0100233 // clang-format on
Mark Salyzyn8b2c7de2015-04-01 07:42:01 -0700234};
Mark Salyzyn0f6a2702017-05-02 08:56:15 -0700235#ifndef __ANDROID_VNDK__
236auto __for_testing_only__android_files = android_files;
237#endif
Mark Salyzyn8b2c7de2015-04-01 07:42:01 -0700238
Mark Salyzynffa76322017-04-05 12:15:49 -0700239static size_t strip(const char* path, size_t len, const char suffix[]) {
240 if (len < strlen(suffix)) return len;
241 if (strncmp(path + len - strlen(suffix), suffix, strlen(suffix))) return len;
242 return len - strlen(suffix);
243}
244
Mark Salyzyn9ceadcb2017-03-20 08:15:40 -0700245static int fs_config_open(int dir, int which, const char* target_out_path) {
Mark Salyzyn68651142015-04-01 09:24:22 -0700246 int fd = -1;
247
Thierry Strudeldf33ffa2015-07-09 09:50:31 -0700248 if (target_out_path && *target_out_path) {
Mark Salyzyn163ecc62017-05-02 08:56:15 -0700249 // target_out_path is the path to the directory holding content of
250 // system partition but as we cannot guarantee it ends with '/system'
251 // or with or without a trailing slash, need to strip them carefully.
Mark Salyzyn166e24c2017-03-20 08:15:40 -0700252 char* name = NULL;
Mark Salyzynffa76322017-04-05 12:15:49 -0700253 size_t len = strlen(target_out_path);
254 len = strip(target_out_path, len, "/");
255 len = strip(target_out_path, len, "/system");
256 if (asprintf(&name, "%.*s%s", (int)len, target_out_path, conf[which][dir]) != -1) {
Mark Salyzyn4232b372015-04-16 08:40:55 -0700257 fd = TEMP_FAILURE_RETRY(open(name, O_RDONLY | O_BINARY));
Mark Salyzyn68651142015-04-01 09:24:22 -0700258 free(name);
259 }
260 }
261 if (fd < 0) {
Mark Salyzyn9ceadcb2017-03-20 08:15:40 -0700262 fd = TEMP_FAILURE_RETRY(open(conf[which][dir], O_RDONLY | O_BINARY));
Mark Salyzyn68651142015-04-01 09:24:22 -0700263 }
264 return fd;
265}
266
Mark Salyzyn757658c2018-11-19 11:17:35 -0800267// if path is "odm/<stuff>", "oem/<stuff>", "product/<stuff>",
268// "product_services/<stuff>" or "vendor/<stuff>"
Jiyong Parka2159c42019-02-03 00:34:29 +0900269static bool is_partition(const std::string& path) {
Mark Salyzyn757658c2018-11-19 11:17:35 -0800270 static const char* partitions[] = {"odm/", "oem/", "product/", "product_services/", "vendor/"};
Mark Salyzyn2d377972017-05-02 14:02:17 -0700271 for (size_t i = 0; i < (sizeof(partitions) / sizeof(partitions[0])); ++i) {
Jiyong Parka2159c42019-02-03 00:34:29 +0900272 if (StartsWith(path, partitions[i])) return true;
Mark Salyzyn2d377972017-05-02 14:02:17 -0700273 }
274 return false;
275}
276
277// alias prefixes of "<partition>/<stuff>" to "system/<partition>/<stuff>" or
278// "system/<partition>/<stuff>" to "<partition>/<stuff>"
Jiyong Parka2159c42019-02-03 00:34:29 +0900279static bool fs_config_cmp(bool dir, const char* prefix, size_t len, const char* path, size_t plen) {
280 std::string pattern(prefix, len);
281 std::string input(path, plen);
282
283 // Massage pattern and input so that they can be used by fnmatch where
284 // directories have to end with /.
285 if (dir) {
286 if (!EndsWith(input, "/")) {
287 input.append("/");
288 }
289
290 if (!EndsWith(pattern, "/*")) {
291 if (EndsWith(pattern, "/")) {
292 pattern.append("*");
293 } else {
294 pattern.append("/*");
295 }
296 }
Ben Fennemaacd7b7b2017-06-22 15:15:56 -0700297 }
298
Jiyong Parka2159c42019-02-03 00:34:29 +0900299 // no FNM_PATHNAME is set in order to match a/b/c/d with a/*
300 // FNM_ESCAPE is set in order to prevent using \\? and \\* and maintenance issues.
301 const int fnm_flags = FNM_NOESCAPE;
302 if (fnmatch(pattern.c_str(), input.c_str(), fnm_flags) == 0) return true;
Mark Salyzyn2d377972017-05-02 14:02:17 -0700303
Jiyong Parka2159c42019-02-03 00:34:29 +0900304 static constexpr const char* kSystem = "system/";
305 if (StartsWith(input, kSystem)) {
306 input.erase(0, strlen(kSystem));
307 } else if (input.size() <= strlen(kSystem)) {
Mark Salyzyn2d377972017-05-02 14:02:17 -0700308 return false;
Jiyong Parka2159c42019-02-03 00:34:29 +0900309 } else if (StartsWith(pattern, kSystem)) {
310 pattern.erase(0, strlen(kSystem));
Mark Salyzyn2d377972017-05-02 14:02:17 -0700311 } else {
Jiyong Parka2159c42019-02-03 00:34:29 +0900312 return false;
Mark Salyzyn2d377972017-05-02 14:02:17 -0700313 }
Jiyong Parka2159c42019-02-03 00:34:29 +0900314
315 if (!is_partition(pattern)) return false;
316 if (!is_partition(input)) return false;
317 return fnmatch(pattern.c_str(), input.c_str(), fnm_flags) == 0;
Mark Salyzyn2d377972017-05-02 14:02:17 -0700318}
Ben Fennemaacd7b7b2017-06-22 15:15:56 -0700319#ifndef __ANDROID_VNDK__
320auto __for_testing_only__fs_config_cmp = fs_config_cmp;
321#endif
Mark Salyzyn68651142015-04-01 09:24:22 -0700322
Mark Salyzyn166e24c2017-03-20 08:15:40 -0700323void fs_config(const char* path, int dir, const char* target_out_path, unsigned* uid, unsigned* gid,
324 unsigned* mode, uint64_t* capabilities) {
325 const struct fs_path_config* pc;
Mark Salyzyn9ceadcb2017-03-20 08:15:40 -0700326 size_t which, plen;
Mark Salyzyn8b2c7de2015-04-01 07:42:01 -0700327
328 if (path[0] == '/') {
329 path++;
330 }
331
Mark Salyzyn7e826332015-04-15 22:30:30 +0000332 plen = strlen(path);
Mark Salyzyn68651142015-04-01 09:24:22 -0700333
Mark Salyzyn9ceadcb2017-03-20 08:15:40 -0700334 for (which = 0; which < (sizeof(conf) / sizeof(conf[0])); ++which) {
Mark Salyzyn7977cc62015-04-15 19:27:39 -0700335 struct fs_path_config_from_file header;
336
Mark Salyzyn9ceadcb2017-03-20 08:15:40 -0700337 int fd = fs_config_open(dir, which, target_out_path);
338 if (fd < 0) continue;
339
Mark Salyzyn7977cc62015-04-15 19:27:39 -0700340 while (TEMP_FAILURE_RETRY(read(fd, &header, sizeof(header))) == sizeof(header)) {
Mark Salyzyn166e24c2017-03-20 08:15:40 -0700341 char* prefix;
342 uint16_t host_len = get2LE((const uint8_t*)&header.len);
Mark Salyzyn7977cc62015-04-15 19:27:39 -0700343 ssize_t len, remainder = host_len - sizeof(header);
Mark Salyzyn68651142015-04-01 09:24:22 -0700344 if (remainder <= 0) {
Mark Salyzyn9ceadcb2017-03-20 08:15:40 -0700345 ALOGE("%s len is corrupted", conf[which][dir]);
Mark Salyzyn68651142015-04-01 09:24:22 -0700346 break;
347 }
Mark Salyzyn163ecc62017-05-02 08:56:15 -0700348 prefix = static_cast<char*>(calloc(1, remainder));
Mark Salyzyn7977cc62015-04-15 19:27:39 -0700349 if (!prefix) {
Mark Salyzyn9ceadcb2017-03-20 08:15:40 -0700350 ALOGE("%s out of memory", conf[which][dir]);
Mark Salyzyn7977cc62015-04-15 19:27:39 -0700351 break;
Mark Salyzyn68651142015-04-01 09:24:22 -0700352 }
Mark Salyzyn7977cc62015-04-15 19:27:39 -0700353 if (TEMP_FAILURE_RETRY(read(fd, prefix, remainder)) != remainder) {
354 free(prefix);
Mark Salyzyn9ceadcb2017-03-20 08:15:40 -0700355 ALOGE("%s prefix is truncated", conf[which][dir]);
Mark Salyzyn7977cc62015-04-15 19:27:39 -0700356 break;
357 }
358 len = strnlen(prefix, remainder);
Mark Salyzyn163ecc62017-05-02 08:56:15 -0700359 if (len >= remainder) { // missing a terminating null
Mark Salyzyn7977cc62015-04-15 19:27:39 -0700360 free(prefix);
Mark Salyzyn9ceadcb2017-03-20 08:15:40 -0700361 ALOGE("%s is corrupted", conf[which][dir]);
Mark Salyzyn68651142015-04-01 09:24:22 -0700362 break;
363 }
Mark Salyzyn7977cc62015-04-15 19:27:39 -0700364 if (fs_config_cmp(dir, prefix, len, path, plen)) {
365 free(prefix);
366 close(fd);
Mark Salyzyn166e24c2017-03-20 08:15:40 -0700367 *uid = get2LE((const uint8_t*)&(header.uid));
368 *gid = get2LE((const uint8_t*)&(header.gid));
369 *mode = (*mode & (~07777)) | get2LE((const uint8_t*)&(header.mode));
370 *capabilities = get8LE((const uint8_t*)&(header.capabilities));
Mark Salyzyn7977cc62015-04-15 19:27:39 -0700371 return;
Mark Salyzyn68651142015-04-01 09:24:22 -0700372 }
Mark Salyzyn7977cc62015-04-15 19:27:39 -0700373 free(prefix);
Mark Salyzyn7e826332015-04-15 22:30:30 +0000374 }
Mark Salyzyn68651142015-04-01 09:24:22 -0700375 close(fd);
Mark Salyzyn68651142015-04-01 09:24:22 -0700376 }
377
Mark Salyzyn166e24c2017-03-20 08:15:40 -0700378 for (pc = dir ? android_dirs : android_files; pc->prefix; pc++) {
Mark Salyzyn68651142015-04-01 09:24:22 -0700379 if (fs_config_cmp(dir, pc->prefix, strlen(pc->prefix), path, plen)) {
380 break;
Mark Salyzyn8b2c7de2015-04-01 07:42:01 -0700381 }
382 }
383 *uid = pc->uid;
384 *gid = pc->gid;
385 *mode = (*mode & (~07777)) | pc->mode;
386 *capabilities = pc->capabilities;
387}
Mark Salyzyn5d9e5ef2015-04-01 11:02:00 -0700388
Mark Salyzyn166e24c2017-03-20 08:15:40 -0700389ssize_t fs_config_generate(char* buffer, size_t length, const struct fs_path_config* pc) {
390 struct fs_path_config_from_file* p = (struct fs_path_config_from_file*)buffer;
Mark Salyzyn5d9e5ef2015-04-01 11:02:00 -0700391 size_t len = ALIGN(sizeof(*p) + strlen(pc->prefix) + 1, sizeof(uint64_t));
392
393 if ((length < len) || (len > UINT16_MAX)) {
394 return -ENOSPC;
395 }
396 memset(p, 0, len);
397 uint16_t host_len = len;
Mark Salyzyn166e24c2017-03-20 08:15:40 -0700398 p->len = get2LE((const uint8_t*)&host_len);
399 p->mode = get2LE((const uint8_t*)&(pc->mode));
400 p->uid = get2LE((const uint8_t*)&(pc->uid));
401 p->gid = get2LE((const uint8_t*)&(pc->gid));
402 p->capabilities = get8LE((const uint8_t*)&(pc->capabilities));
Mark Salyzyn5d9e5ef2015-04-01 11:02:00 -0700403 strcpy(p->prefix, pc->prefix);
404 return len;
405}