blob: 5efe2098bac8e59a1598dd7227ae9728e198b830 [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>
Elliott Hughes39d6c8e2021-03-08 18:50:59 +000038#include <cutils/fs.h>
Mark Salyzyn30f991f2017-01-10 13:19:54 -080039#include <log/log.h>
Mark Salyzyn8b2c7de2015-04-01 07:42:01 -070040#include <private/android_filesystem_config.h>
41
Tom Cherry68debff2019-06-17 14:19:39 -070042#include "fs_config.h"
43
Jiyong Parka2159c42019-02-03 00:34:29 +090044using android::base::EndsWith;
45using android::base::StartsWith;
46
Mark Salyzyn166e24c2017-03-20 08:15:40 -070047#define ALIGN(x, alignment) (((x) + ((alignment)-1)) & ~((alignment)-1))
Tom Cherry68debff2019-06-17 14:19:39 -070048#define CAP_MASK_LONG(cap_name) (1ULL << (cap_name))
Mark Salyzyn5d9e5ef2015-04-01 11:02:00 -070049
Mark Salyzyn163ecc62017-05-02 08:56:15 -070050// 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.
Mark Salyzyn8b2c7de2015-04-01 07:42:01 -070054
55static const struct fs_path_config android_dirs[] = {
Nick Kralevichf4fc9222018-11-11 08:39:20 -080056 // clang-format off
Mark Salyzync4f9f4c2017-03-20 08:15:40 -070057 { 00770, AID_SYSTEM, AID_CACHE, 0, "cache" },
Gwendal Grignou85264132018-02-07 10:55:56 -080058 { 00555, AID_ROOT, AID_ROOT, 0, "config" },
Mark Salyzync4f9f4c2017-03-20 08:15:40 -070059 { 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" },
Mark Salyzyn8b2c7de2015-04-01 07:42:01 -070067 { 00771, AID_SHARED_RELRO, AID_SHARED_RELRO, 0, "data/misc/shared_relro" },
Mark Salyzync4f9f4c2017-03-20 08:15:40 -070068 { 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 { 00750, AID_ROOT, AID_SHELL, 0, "data/nativetest" },
72 { 00750, AID_ROOT, AID_SHELL, 0, "data/nativetest64" },
Akos Denkebb61a6e2019-10-10 08:27:11 +020073 { 00750, AID_ROOT, AID_SHELL, 0, "data/benchmarktest" },
74 { 00750, AID_ROOT, AID_SHELL, 0, "data/benchmarktest64" },
Mark Salyzync4f9f4c2017-03-20 08:15:40 -070075 { 00775, AID_ROOT, AID_ROOT, 0, "data/preloads" },
76 { 00771, AID_SYSTEM, AID_SYSTEM, 0, "data" },
77 { 00755, AID_ROOT, AID_SYSTEM, 0, "mnt" },
Nick Kralevichf4fc9222018-11-11 08:39:20 -080078 { 00751, AID_ROOT, AID_SHELL, 0, "product/bin" },
Jooyung Hana70cea52020-09-21 19:37:05 +090079 { 00751, AID_ROOT, AID_SHELL, 0, "product/apex/*/bin" },
Mark Salyzync4f9f4c2017-03-20 08:15:40 -070080 { 00777, AID_ROOT, AID_ROOT, 0, "sdcard" },
81 { 00751, AID_ROOT, AID_SDCARD_R, 0, "storage" },
Maciej Żenczykowski1ba94402023-04-11 06:08:32 +000082 { 00750, AID_ROOT, AID_SYSTEM, 0, "system/apex/com.android.tethering/bin/for-system" },
Nick Kralevich8d1b43b2019-04-02 08:54:17 -070083 { 00751, AID_ROOT, AID_SHELL, 0, "system/bin" },
Mark Salyzync4f9f4c2017-03-20 08:15:40 -070084 { 00755, AID_ROOT, AID_ROOT, 0, "system/etc/ppp" },
85 { 00755, AID_ROOT, AID_SHELL, 0, "system/vendor" },
Mathew Inwood42a1a122020-07-20 15:14:55 +000086 { 00750, AID_ROOT, AID_SHELL, 0, "system/xbin" },
Nick Kralevich6bcd50e2019-09-10 18:54:28 -070087 { 00751, AID_ROOT, AID_SHELL, 0, "system/apex/*/bin" },
Maciej Żenczykowski93c9dfc2023-04-18 21:08:12 +000088 { 00750, AID_ROOT, AID_SYSTEM, 0, "system_ext/apex/com.android.tethering/bin/for-system" },
Jeongik Cha4e467e72019-11-07 15:41:48 +090089 { 00751, AID_ROOT, AID_SHELL, 0, "system_ext/bin" },
Jooyung Han08792eb2019-12-20 03:54:57 +090090 { 00751, AID_ROOT, AID_SHELL, 0, "system_ext/apex/*/bin" },
Nick Kralevichf4fc9222018-11-11 08:39:20 -080091 { 00751, AID_ROOT, AID_SHELL, 0, "vendor/bin" },
Jooyung Hana70cea52020-09-21 19:37:05 +090092 { 00751, AID_ROOT, AID_SHELL, 0, "vendor/apex/*/bin" },
Mark Salyzync4f9f4c2017-03-20 08:15:40 -070093 { 00755, AID_ROOT, AID_SHELL, 0, "vendor" },
Peter Collingbourne4a57c8f2024-02-27 19:37:41 -080094 {},
Nick Kralevichf4fc9222018-11-11 08:39:20 -080095 // clang-format on
Mark Salyzyn8b2c7de2015-04-01 07:42:01 -070096};
Mark Salyzyn0f6a2702017-05-02 08:56:15 -070097#ifndef __ANDROID_VNDK__
98auto __for_testing_only__android_dirs = android_dirs;
99#endif
Mark Salyzyn8b2c7de2015-04-01 07:42:01 -0700100
Mark Salyzyn163ecc62017-05-02 08:56:15 -0700101// 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.
Mark Salyzyn9ceadcb2017-03-20 08:15:40 -0700106static const char sys_conf_dir[] = "/system/etc/fs_config_dirs";
107static const char sys_conf_file[] = "/system/etc/fs_config_files";
Mark Salyzyn163ecc62017-05-02 08:56:15 -0700108// 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
Mark Salyzyn757658c2018-11-19 11:17:35 -0800112// customization can also land on /system/vendor, /system/oem, /system/odm,
Justin Yun7eaf9b52019-06-28 14:28:00 +0900113// /system/product or /system/system_ext.
Mark Salyzyn757658c2018-11-19 11:17:35 -0800114//
Mark Salyzyn163ecc62017-05-02 08:56:15 -0700115// We expect build-time checking or filtering when constructing the associated
116// fs_config_* files (see build/tools/fs_config/fs_config_generate.c)
Mark Salyzyn9ceadcb2017-03-20 08:15:40 -0700117static const char ven_conf_dir[] = "/vendor/etc/fs_config_dirs";
118static const char ven_conf_file[] = "/vendor/etc/fs_config_files";
Mark Salyzyn62c701e2017-03-21 08:09:52 -0700119static const char oem_conf_dir[] = "/oem/etc/fs_config_dirs";
120static const char oem_conf_file[] = "/oem/etc/fs_config_files";
Mark Salyzyn2b616ce2017-03-22 15:23:24 -0700121static const char odm_conf_dir[] = "/odm/etc/fs_config_dirs";
122static const char odm_conf_file[] = "/odm/etc/fs_config_files";
Mark Salyzyn757658c2018-11-19 11:17:35 -0800123static const char product_conf_dir[] = "/product/etc/fs_config_dirs";
124static const char product_conf_file[] = "/product/etc/fs_config_files";
Justin Yun7eaf9b52019-06-28 14:28:00 +0900125static const char system_ext_conf_dir[] = "/system_ext/etc/fs_config_dirs";
126static const char system_ext_conf_file[] = "/system_ext/etc/fs_config_files";
Mark Salyzyn9ceadcb2017-03-20 08:15:40 -0700127static const char* conf[][2] = {
Justin Yun7eaf9b52019-06-28 14:28:00 +0900128 {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},
Mark Salyzyn9ceadcb2017-03-20 08:15:40 -0700131};
Mark Salyzyn68651142015-04-01 09:24:22 -0700132
Tom Cherry07aa2a62018-03-28 13:25:11 -0700133// 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
Mark Salyzyn8b2c7de2015-04-01 07:42:01 -0700139static const struct fs_path_config android_files[] = {
Dario Freni4fa866a2018-05-25 16:07:19 +0100140 // clang-format off
Mark Salyzyna45eae92017-03-20 08:53:24 -0700141 { 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/*" },
Akos Denkebb61a6e2019-10-10 08:27:11 +0200150 { 00750, AID_ROOT, AID_SHELL, 0, "data/benchmarktest/*" },
151 { 00750, AID_ROOT, AID_SHELL, 0, "data/benchmarktest64/*" },
Hung-ying Tyan527d80d2017-06-02 18:59:46 +0800152 { 00600, AID_ROOT, AID_ROOT, 0, "default.prop" }, // legacy
153 { 00600, AID_ROOT, AID_ROOT, 0, "system/etc/prop.default" },
Bowgo Tsai9fc8dcf2019-05-17 15:40:18 +0800154 { 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" },
Mark Salyzyn2b616ce2017-03-22 15:23:24 -0700157 { 00444, AID_ROOT, AID_ROOT, 0, odm_conf_dir + 1 },
158 { 00444, AID_ROOT, AID_ROOT, 0, odm_conf_file + 1 },
Mark Salyzyn62c701e2017-03-21 08:09:52 -0700159 { 00444, AID_ROOT, AID_ROOT, 0, oem_conf_dir + 1 },
160 { 00444, AID_ROOT, AID_ROOT, 0, oem_conf_file + 1 },
Jaekyun Seokdff165d2017-11-28 12:10:10 +0900161 { 00600, AID_ROOT, AID_ROOT, 0, "product/build.prop" },
Mark Salyzyn757658c2018-11-19 11:17:35 -0800162 { 00444, AID_ROOT, AID_ROOT, 0, product_conf_dir + 1 },
163 { 00444, AID_ROOT, AID_ROOT, 0, product_conf_file + 1 },
Justin Yun7eaf9b52019-06-28 14:28:00 +0900164 { 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 },
Mark Salyzyna45eae92017-03-20 08:53:24 -0700167 { 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" },
Luis Hector Chavezd32c36c2018-06-08 15:00:40 -0700170 { 00550, AID_LOGD, AID_LOGD, 0, "system/bin/logd" },
Mark Salyzyna45eae92017-03-20 08:53:24 -0700171 { 00700, AID_ROOT, AID_ROOT, 0, "system/bin/secilc" },
172 { 00750, AID_ROOT, AID_ROOT, 0, "system/bin/uncrypt" },
Hung-ying Tyan33463382017-05-25 19:18:17 +0800173 { 00600, AID_ROOT, AID_ROOT, 0, "system/build.prop" },
Mark Salyzyn9ceadcb2017-03-20 08:15:40 -0700174 { 00444, AID_ROOT, AID_ROOT, 0, sys_conf_dir + 1 },
175 { 00444, AID_ROOT, AID_ROOT, 0, sys_conf_file + 1 },
Mark Salyzyn8b2c7de2015-04-01 07:42:01 -0700176 { 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" },
Mark Salyzyn8b2c7de2015-04-01 07:42:01 -0700179 { 00555, AID_ROOT, AID_ROOT, 0, "system/etc/ppp/*" },
180 { 00555, AID_ROOT, AID_ROOT, 0, "system/etc/rc.*" },
Bill Peckhamdea8c9c2019-09-17 16:30:37 -0700181 { 00750, AID_ROOT, AID_ROOT, 0, "vendor/bin/install-recovery.sh" },
Hung-ying Tyan33463382017-05-25 19:18:17 +0800182 { 00600, AID_ROOT, AID_ROOT, 0, "vendor/build.prop" },
183 { 00600, AID_ROOT, AID_ROOT, 0, "vendor/default.prop" },
Bill Peckhamdea8c9c2019-09-17 16:30:37 -0700184 { 00440, AID_ROOT, AID_ROOT, 0, "vendor/etc/recovery.img" },
Mark Salyzyn9ceadcb2017-03-20 08:15:40 -0700185 { 00444, AID_ROOT, AID_ROOT, 0, ven_conf_dir + 1 },
186 { 00444, AID_ROOT, AID_ROOT, 0, ven_conf_file + 1 },
Mark Salyzyn8b2c7de2015-04-01 07:42:01 -0700187
Mark Salyzyn163ecc62017-05-02 08:56:15 -0700188 // the following two files are INTENTIONALLY set-uid, but they
189 // are NOT included on user builds.
Mark Salyzyn8b2c7de2015-04-01 07:42:01 -0700190 { 06755, AID_ROOT, AID_ROOT, 0, "system/xbin/procmem" },
Mark Salyzyna45eae92017-03-20 08:53:24 -0700191 { 04750, AID_ROOT, AID_SHELL, 0, "system/xbin/su" },
Mark Salyzyn8b2c7de2015-04-01 07:42:01 -0700192
Mark Salyzyn163ecc62017-05-02 08:56:15 -0700193 // the following files have enhanced capabilities and ARE included
194 // in user builds.
Maciej Żenczykowski1ba94402023-04-11 06:08:32 +0000195 { 06755, AID_CLAT, AID_CLAT, 0, "system/apex/com.android.tethering/bin/for-system/clatd" },
Maciej Żenczykowski93c9dfc2023-04-18 21:08:12 +0000196 { 06755, AID_CLAT, AID_CLAT, 0, "system_ext/apex/com.android.tethering/bin/for-system/clatd" },
Mark Salyzyna45eae92017-03-20 08:53:24 -0700197 { 00700, AID_SYSTEM, AID_SHELL, CAP_MASK_LONG(CAP_BLOCK_SUSPEND),
198 "system/bin/inputflinger" },
Mark Salyzyn0d2a1dc2016-10-28 12:41:17 -0700199 { 00750, AID_ROOT, AID_SHELL, CAP_MASK_LONG(CAP_SETUID) |
200 CAP_MASK_LONG(CAP_SETGID),
201 "system/bin/run-as" },
Yabin Cuiaf3e30d2019-01-08 16:31:56 -0800202 { 00750, AID_ROOT, AID_SHELL, CAP_MASK_LONG(CAP_SETUID) |
203 CAP_MASK_LONG(CAP_SETGID),
204 "system/bin/simpleperf_app_runner" },
Yifan Hong04b932b2020-01-22 16:44:55 -0800205 { 00755, AID_ROOT, AID_ROOT, 0, "first_stage_ramdisk/system/bin/e2fsck" },
Yifan Hongc7ed02f2020-11-19 15:20:44 -0800206#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
Jaegeuk Kimb9be4272020-02-12 12:27:33 -0800211 { 00755, AID_ROOT, AID_ROOT, 0, "first_stage_ramdisk/system/bin/resize2fs" },
David Andersone80a1532020-07-08 13:31:37 -0700212 { 00755, AID_ROOT, AID_ROOT, 0, "first_stage_ramdisk/system/bin/snapuserd" },
Ray-cy.leed8654932023-07-31 20:00:43 +0800213 { 00755, AID_ROOT, AID_ROOT, 0, "first_stage_ramdisk/system/bin/snapuserd_ramdisk" },
Yifan Hongc7ed02f2020-11-19 15:20:44 -0800214 { 00755, AID_ROOT, AID_ROOT, 0, "first_stage_ramdisk/system/bin/tune2fs" },
Jaegeuk Kimb7833102021-12-29 15:11:00 -0800215 { 00755, AID_ROOT, AID_ROOT, 0, "first_stage_ramdisk/system/bin/fsck.f2fs" },
Mark Salyzyn163ecc62017-05-02 08:56:15 -0700216 // generic defaults
Mark Salyzyna45eae92017-03-20 08:53:24 -0700217 { 00755, AID_ROOT, AID_ROOT, 0, "bin/*" },
218 { 00640, AID_ROOT, AID_SHELL, 0, "fstab.*" },
219 { 00750, AID_ROOT, AID_SHELL, 0, "init*" },
Min Yuna8eb0072019-05-03 17:48:17 +0900220 { 00755, AID_ROOT, AID_SHELL, 0, "odm/bin/*" },
Peter Collingbourne4a57c8f2024-02-27 19:37:41 -0800221 { 00644, AID_ROOT, AID_ROOT, 0, "odm/framework/*" },
222 { 00644, AID_ROOT, AID_ROOT, 0, "odm/app/*" },
223 { 00644, AID_ROOT, AID_ROOT, 0, "odm/priv-app/*" },
Inseob Kim25195da2018-05-31 13:03:58 +0900224 { 00755, AID_ROOT, AID_SHELL, 0, "product/bin/*" },
Jooyung Hana70cea52020-09-21 19:37:05 +0900225 { 00755, AID_ROOT, AID_SHELL, 0, "product/apex/*bin/*" },
Peter Collingbourne4a57c8f2024-02-27 19:37:41 -0800226 { 00644, AID_ROOT, AID_ROOT, 0, "product/framework/*" },
227 { 00644, AID_ROOT, AID_ROOT, 0, "product/app/*" },
228 { 00644, AID_ROOT, AID_ROOT, 0, "product/priv-app/*" },
Mark Salyzyn8b2c7de2015-04-01 07:42:01 -0700229 { 00755, AID_ROOT, AID_SHELL, 0, "system/bin/*" },
Mark Salyzyna45eae92017-03-20 08:53:24 -0700230 { 00755, AID_ROOT, AID_SHELL, 0, "system/xbin/*" },
Jiyong Parkdefdb182019-02-09 02:58:12 +0900231 { 00755, AID_ROOT, AID_SHELL, 0, "system/apex/*/bin/*" },
Peter Collingbourne4a57c8f2024-02-27 19:37:41 -0800232 { 00644, AID_ROOT, AID_ROOT, 0, "system/framework/*" },
233 { 00644, AID_ROOT, AID_ROOT, 0, "system/app/*" },
234 { 00644, AID_ROOT, AID_ROOT, 0, "system/priv-app/*" },
Jeongik Cha4e467e72019-11-07 15:41:48 +0900235 { 00755, AID_ROOT, AID_SHELL, 0, "system_ext/bin/*" },
Jooyung Han08792eb2019-12-20 03:54:57 +0900236 { 00755, AID_ROOT, AID_SHELL, 0, "system_ext/apex/*/bin/*" },
Peter Collingbourne4a57c8f2024-02-27 19:37:41 -0800237 { 00644, AID_ROOT, AID_ROOT, 0, "system_ext/framework/*" },
238 { 00644, AID_ROOT, AID_ROOT, 0, "system_ext/app/*" },
239 { 00644, AID_ROOT, AID_ROOT, 0, "system_ext/priv-app/*" },
Mark Salyzyn8b2c7de2015-04-01 07:42:01 -0700240 { 00755, AID_ROOT, AID_SHELL, 0, "vendor/bin/*" },
Jooyung Hana70cea52020-09-21 19:37:05 +0900241 { 00755, AID_ROOT, AID_SHELL, 0, "vendor/apex/*bin/*" },
Erik Klinea5a9c742016-05-27 13:13:07 +0900242 { 00755, AID_ROOT, AID_SHELL, 0, "vendor/xbin/*" },
Peter Collingbourne4a57c8f2024-02-27 19:37:41 -0800243 { 00644, AID_ROOT, AID_ROOT, 0, "vendor/framework/*" },
244 { 00644, AID_ROOT, AID_ROOT, 0, "vendor/app/*" },
245 { 00644, AID_ROOT, AID_ROOT, 0, "vendor/priv-app/*" },
246 {},
Dario Freni4fa866a2018-05-25 16:07:19 +0100247 // clang-format on
Mark Salyzyn8b2c7de2015-04-01 07:42:01 -0700248};
Mark Salyzyn0f6a2702017-05-02 08:56:15 -0700249#ifndef __ANDROID_VNDK__
250auto __for_testing_only__android_files = android_files;
251#endif
Mark Salyzyn8b2c7de2015-04-01 07:42:01 -0700252
Mark Salyzynffa76322017-04-05 12:15:49 -0700253static size_t strip(const char* path, size_t len, const char suffix[]) {
254 if (len < strlen(suffix)) return len;
255 if (strncmp(path + len - strlen(suffix), suffix, strlen(suffix))) return len;
256 return len - strlen(suffix);
257}
258
Mark Salyzyn9ceadcb2017-03-20 08:15:40 -0700259static int fs_config_open(int dir, int which, const char* target_out_path) {
Mark Salyzyn68651142015-04-01 09:24:22 -0700260 int fd = -1;
261
Thierry Strudeldf33ffa2015-07-09 09:50:31 -0700262 if (target_out_path && *target_out_path) {
Mark Salyzyn163ecc62017-05-02 08:56:15 -0700263 // target_out_path is the path to the directory holding content of
264 // system partition but as we cannot guarantee it ends with '/system'
265 // or with or without a trailing slash, need to strip them carefully.
Mark Salyzyn166e24c2017-03-20 08:15:40 -0700266 char* name = NULL;
Mark Salyzynffa76322017-04-05 12:15:49 -0700267 size_t len = strlen(target_out_path);
268 len = strip(target_out_path, len, "/");
269 len = strip(target_out_path, len, "/system");
270 if (asprintf(&name, "%.*s%s", (int)len, target_out_path, conf[which][dir]) != -1) {
Elliott Hughesf6a84e42023-09-22 14:37:37 +0000271 fd = TEMP_FAILURE_RETRY(open(name, O_RDONLY));
Mark Salyzyn68651142015-04-01 09:24:22 -0700272 free(name);
273 }
274 }
275 if (fd < 0) {
Elliott Hughesf6a84e42023-09-22 14:37:37 +0000276 fd = TEMP_FAILURE_RETRY(open(conf[which][dir], O_RDONLY));
Mark Salyzyn68651142015-04-01 09:24:22 -0700277 }
278 return fd;
279}
280
Mark Salyzyn757658c2018-11-19 11:17:35 -0800281// if path is "odm/<stuff>", "oem/<stuff>", "product/<stuff>",
Justin Yun7eaf9b52019-06-28 14:28:00 +0900282// "system_ext/<stuff>" or "vendor/<stuff>"
Jiyong Parka2159c42019-02-03 00:34:29 +0900283static bool is_partition(const std::string& path) {
Justin Yun7eaf9b52019-06-28 14:28:00 +0900284 static const char* partitions[] = {"odm/", "oem/", "product/", "system_ext/", "vendor/"};
Mark Salyzyn2d377972017-05-02 14:02:17 -0700285 for (size_t i = 0; i < (sizeof(partitions) / sizeof(partitions[0])); ++i) {
Jiyong Parka2159c42019-02-03 00:34:29 +0900286 if (StartsWith(path, partitions[i])) return true;
Mark Salyzyn2d377972017-05-02 14:02:17 -0700287 }
288 return false;
289}
290
291// alias prefixes of "<partition>/<stuff>" to "system/<partition>/<stuff>" or
292// "system/<partition>/<stuff>" to "<partition>/<stuff>"
Jiyong Parka2159c42019-02-03 00:34:29 +0900293static bool fs_config_cmp(bool dir, const char* prefix, size_t len, const char* path, size_t plen) {
294 std::string pattern(prefix, len);
295 std::string input(path, plen);
296
297 // Massage pattern and input so that they can be used by fnmatch where
298 // directories have to end with /.
299 if (dir) {
300 if (!EndsWith(input, "/")) {
301 input.append("/");
302 }
303
304 if (!EndsWith(pattern, "/*")) {
305 if (EndsWith(pattern, "/")) {
306 pattern.append("*");
307 } else {
308 pattern.append("/*");
309 }
310 }
Ben Fennemaacd7b7b2017-06-22 15:15:56 -0700311 }
312
Jiyong Parka2159c42019-02-03 00:34:29 +0900313 // no FNM_PATHNAME is set in order to match a/b/c/d with a/*
314 // FNM_ESCAPE is set in order to prevent using \\? and \\* and maintenance issues.
315 const int fnm_flags = FNM_NOESCAPE;
316 if (fnmatch(pattern.c_str(), input.c_str(), fnm_flags) == 0) return true;
Mark Salyzyn2d377972017-05-02 14:02:17 -0700317
Min Yuna8eb0072019-05-03 17:48:17 +0900318 // Check match between logical partition's files and patterns.
Justin Yun7eaf9b52019-06-28 14:28:00 +0900319 static constexpr const char* kLogicalPartitions[] = {"system/product/", "system/system_ext/",
320 "system/vendor/", "vendor/odm/"};
Min Yuna8eb0072019-05-03 17:48:17 +0900321 for (auto& logical_partition : kLogicalPartitions) {
322 if (StartsWith(input, logical_partition)) {
323 std::string input_in_partition = input.substr(input.find('/') + 1);
324 if (!is_partition(input_in_partition)) continue;
325 if (fnmatch(pattern.c_str(), input_in_partition.c_str(), fnm_flags) == 0) {
326 return true;
327 }
328 }
Mark Salyzyn2d377972017-05-02 14:02:17 -0700329 }
Min Yuna8eb0072019-05-03 17:48:17 +0900330 return false;
Mark Salyzyn2d377972017-05-02 14:02:17 -0700331}
Ben Fennemaacd7b7b2017-06-22 15:15:56 -0700332#ifndef __ANDROID_VNDK__
333auto __for_testing_only__fs_config_cmp = fs_config_cmp;
334#endif
Mark Salyzyn68651142015-04-01 09:24:22 -0700335
Peter Collingbourne4a57c8f2024-02-27 19:37:41 -0800336bool get_fs_config(const char* path, bool dir, const char* target_out_path,
337 struct fs_config* fs_conf) {
Mark Salyzyn166e24c2017-03-20 08:15:40 -0700338 const struct fs_path_config* pc;
Mark Salyzyn9ceadcb2017-03-20 08:15:40 -0700339 size_t which, plen;
Mark Salyzyn8b2c7de2015-04-01 07:42:01 -0700340
341 if (path[0] == '/') {
342 path++;
343 }
344
Mark Salyzyn7e826332015-04-15 22:30:30 +0000345 plen = strlen(path);
Mark Salyzyn68651142015-04-01 09:24:22 -0700346
Mark Salyzyn9ceadcb2017-03-20 08:15:40 -0700347 for (which = 0; which < (sizeof(conf) / sizeof(conf[0])); ++which) {
Mark Salyzyn7977cc62015-04-15 19:27:39 -0700348 struct fs_path_config_from_file header;
349
Mark Salyzyn9ceadcb2017-03-20 08:15:40 -0700350 int fd = fs_config_open(dir, which, target_out_path);
351 if (fd < 0) continue;
352
Mark Salyzyn7977cc62015-04-15 19:27:39 -0700353 while (TEMP_FAILURE_RETRY(read(fd, &header, sizeof(header))) == sizeof(header)) {
Mark Salyzyn166e24c2017-03-20 08:15:40 -0700354 char* prefix;
Tom Cherry5550d4e2019-06-17 14:28:37 -0700355 uint16_t host_len = header.len;
Mark Salyzyn7977cc62015-04-15 19:27:39 -0700356 ssize_t len, remainder = host_len - sizeof(header);
Mark Salyzyn68651142015-04-01 09:24:22 -0700357 if (remainder <= 0) {
Mark Salyzyn9ceadcb2017-03-20 08:15:40 -0700358 ALOGE("%s len is corrupted", conf[which][dir]);
Mark Salyzyn68651142015-04-01 09:24:22 -0700359 break;
360 }
Mark Salyzyn163ecc62017-05-02 08:56:15 -0700361 prefix = static_cast<char*>(calloc(1, remainder));
Mark Salyzyn7977cc62015-04-15 19:27:39 -0700362 if (!prefix) {
Mark Salyzyn9ceadcb2017-03-20 08:15:40 -0700363 ALOGE("%s out of memory", conf[which][dir]);
Mark Salyzyn7977cc62015-04-15 19:27:39 -0700364 break;
Mark Salyzyn68651142015-04-01 09:24:22 -0700365 }
Mark Salyzyn7977cc62015-04-15 19:27:39 -0700366 if (TEMP_FAILURE_RETRY(read(fd, prefix, remainder)) != remainder) {
367 free(prefix);
Mark Salyzyn9ceadcb2017-03-20 08:15:40 -0700368 ALOGE("%s prefix is truncated", conf[which][dir]);
Mark Salyzyn7977cc62015-04-15 19:27:39 -0700369 break;
370 }
371 len = strnlen(prefix, remainder);
Mark Salyzyn163ecc62017-05-02 08:56:15 -0700372 if (len >= remainder) { // missing a terminating null
Mark Salyzyn7977cc62015-04-15 19:27:39 -0700373 free(prefix);
Mark Salyzyn9ceadcb2017-03-20 08:15:40 -0700374 ALOGE("%s is corrupted", conf[which][dir]);
Mark Salyzyn68651142015-04-01 09:24:22 -0700375 break;
376 }
Mark Salyzyn7977cc62015-04-15 19:27:39 -0700377 if (fs_config_cmp(dir, prefix, len, path, plen)) {
378 free(prefix);
379 close(fd);
Peter Collingbourne4a57c8f2024-02-27 19:37:41 -0800380 fs_conf->uid = header.uid;
381 fs_conf->gid = header.gid;
382 fs_conf->mode = header.mode;
383 fs_conf->capabilities = header.capabilities;
384 return true;
Mark Salyzyn68651142015-04-01 09:24:22 -0700385 }
Mark Salyzyn7977cc62015-04-15 19:27:39 -0700386 free(prefix);
Mark Salyzyn7e826332015-04-15 22:30:30 +0000387 }
Mark Salyzyn68651142015-04-01 09:24:22 -0700388 close(fd);
Mark Salyzyn68651142015-04-01 09:24:22 -0700389 }
390
Mark Salyzyn166e24c2017-03-20 08:15:40 -0700391 for (pc = dir ? android_dirs : android_files; pc->prefix; pc++) {
Mark Salyzyn68651142015-04-01 09:24:22 -0700392 if (fs_config_cmp(dir, pc->prefix, strlen(pc->prefix), path, plen)) {
Peter Collingbourne4a57c8f2024-02-27 19:37:41 -0800393 fs_conf->uid = pc->uid;
394 fs_conf->gid = pc->gid;
395 fs_conf->mode = pc->mode;
396 fs_conf->capabilities = pc->capabilities;
397 return true;
Mark Salyzyn8b2c7de2015-04-01 07:42:01 -0700398 }
399 }
Peter Collingbourne4a57c8f2024-02-27 19:37:41 -0800400 return false;
401}
402
403void fs_config(const char* path, int dir, const char* target_out_path, unsigned* uid, unsigned* gid,
404 unsigned* mode, uint64_t* capabilities) {
405 struct fs_config conf;
406 if (get_fs_config(path, dir, target_out_path, &conf)) {
407 *uid = conf.uid;
408 *gid = conf.gid;
409 *mode = (*mode & S_IFMT) | conf.mode;
410 *capabilities = conf.capabilities;
411 } else {
412 *uid = AID_ROOT;
413 *gid = AID_ROOT;
414 *mode = (*mode & S_IFMT) | (dir ? 0755 : 0644);
415 *capabilities = 0;
416 }
Mark Salyzyn8b2c7de2015-04-01 07:42:01 -0700417}