| Mark Salyzyn | 8b2c7de | 2015-04-01 07:42:01 -0700 | [diff] [blame] | 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 |  | 
| Elliott Hughes | 8e9aeb9 | 2017-11-10 10:22:07 -0800 | [diff] [blame] | 17 | #include <private/fs_config.h> | 
|  | 18 |  | 
| Mark Salyzyn | 163ecc6 | 2017-05-02 08:56:15 -0700 | [diff] [blame] | 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. | 
| Mark Salyzyn | 8b2c7de | 2015-04-01 07:42:01 -0700 | [diff] [blame] | 22 |  | 
| Mark Salyzyn | 6865114 | 2015-04-01 09:24:22 -0700 | [diff] [blame] | 23 | #define LOG_TAG "fs_config" | 
| Mark Salyzyn | 4e5f71a | 2015-04-01 09:24:22 -0700 | [diff] [blame] | 24 |  | 
| Mark Salyzyn | 6865114 | 2015-04-01 09:24:22 -0700 | [diff] [blame] | 25 | #include <errno.h> | 
|  | 26 | #include <fcntl.h> | 
| Jiyong Park | a2159c4 | 2019-02-03 00:34:29 +0900 | [diff] [blame] | 27 | #include <fnmatch.h> | 
| Mark Salyzyn | 6865114 | 2015-04-01 09:24:22 -0700 | [diff] [blame] | 28 | #include <stdint.h> | 
|  | 29 | #include <stdio.h> | 
|  | 30 | #include <stdlib.h> | 
|  | 31 | #include <string.h> | 
| Mark Salyzyn | 6865114 | 2015-04-01 09:24:22 -0700 | [diff] [blame] | 32 | #include <sys/stat.h> | 
|  | 33 | #include <sys/types.h> | 
|  | 34 |  | 
| Jiyong Park | a2159c4 | 2019-02-03 00:34:29 +0900 | [diff] [blame] | 35 | #include <string> | 
|  | 36 |  | 
|  | 37 | #include <android-base/strings.h> | 
| Elliott Hughes | 39d6c8e | 2021-03-08 18:50:59 +0000 | [diff] [blame] | 38 | #include <cutils/fs.h> | 
| Mark Salyzyn | 30f991f | 2017-01-10 13:19:54 -0800 | [diff] [blame] | 39 | #include <log/log.h> | 
| Mark Salyzyn | 8b2c7de | 2015-04-01 07:42:01 -0700 | [diff] [blame] | 40 | #include <private/android_filesystem_config.h> | 
|  | 41 |  | 
| Tom Cherry | 68debff | 2019-06-17 14:19:39 -0700 | [diff] [blame] | 42 | #include "fs_config.h" | 
|  | 43 |  | 
| Jiyong Park | a2159c4 | 2019-02-03 00:34:29 +0900 | [diff] [blame] | 44 | using android::base::EndsWith; | 
|  | 45 | using android::base::StartsWith; | 
|  | 46 |  | 
| Mark Salyzyn | 166e24c | 2017-03-20 08:15:40 -0700 | [diff] [blame] | 47 | #define ALIGN(x, alignment) (((x) + ((alignment)-1)) & ~((alignment)-1)) | 
| Tom Cherry | 68debff | 2019-06-17 14:19:39 -0700 | [diff] [blame] | 48 | #define CAP_MASK_LONG(cap_name) (1ULL << (cap_name)) | 
| Mark Salyzyn | 5d9e5ef | 2015-04-01 11:02:00 -0700 | [diff] [blame] | 49 |  | 
| Mark Salyzyn | 163ecc6 | 2017-05-02 08:56:15 -0700 | [diff] [blame] | 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. | 
| Mark Salyzyn | 8b2c7de | 2015-04-01 07:42:01 -0700 | [diff] [blame] | 54 |  | 
|  | 55 | static const struct fs_path_config android_dirs[] = { | 
| Nick Kralevich | f4fc922 | 2018-11-11 08:39:20 -0800 | [diff] [blame] | 56 | // clang-format off | 
| Mark Salyzyn | c4f9f4c | 2017-03-20 08:15:40 -0700 | [diff] [blame] | 57 | { 00770, AID_SYSTEM,       AID_CACHE,        0, "cache" }, | 
| Gwendal Grignou | 8526413 | 2018-02-07 10:55:56 -0800 | [diff] [blame] | 58 | { 00555, AID_ROOT,         AID_ROOT,         0, "config" }, | 
| Mark Salyzyn | c4f9f4c | 2017-03-20 08:15:40 -0700 | [diff] [blame] | 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" }, | 
| Mark Salyzyn | 8b2c7de | 2015-04-01 07:42:01 -0700 | [diff] [blame] | 67 | { 00771, AID_SHARED_RELRO, AID_SHARED_RELRO, 0, "data/misc/shared_relro" }, | 
| Mark Salyzyn | c4f9f4c | 2017-03-20 08:15:40 -0700 | [diff] [blame] | 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 | { 00750, AID_ROOT,         AID_SHELL,        0, "data/nativetest" }, | 
|  | 72 | { 00750, AID_ROOT,         AID_SHELL,        0, "data/nativetest64" }, | 
| Akos Denke | bb61a6e | 2019-10-10 08:27:11 +0200 | [diff] [blame] | 73 | { 00750, AID_ROOT,         AID_SHELL,        0, "data/benchmarktest" }, | 
|  | 74 | { 00750, AID_ROOT,         AID_SHELL,        0, "data/benchmarktest64" }, | 
| Mark Salyzyn | c4f9f4c | 2017-03-20 08:15:40 -0700 | [diff] [blame] | 75 | { 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 Kralevich | f4fc922 | 2018-11-11 08:39:20 -0800 | [diff] [blame] | 78 | { 00751, AID_ROOT,         AID_SHELL,        0, "product/bin" }, | 
| Jooyung Han | a70cea5 | 2020-09-21 19:37:05 +0900 | [diff] [blame] | 79 | { 00751, AID_ROOT,         AID_SHELL,        0, "product/apex/*/bin" }, | 
| Mark Salyzyn | c4f9f4c | 2017-03-20 08:15:40 -0700 | [diff] [blame] | 80 | { 00777, AID_ROOT,         AID_ROOT,         0, "sdcard" }, | 
|  | 81 | { 00751, AID_ROOT,         AID_SDCARD_R,     0, "storage" }, | 
| Maciej Żenczykowski | 1ba9440 | 2023-04-11 06:08:32 +0000 | [diff] [blame] | 82 | { 00750, AID_ROOT,         AID_SYSTEM,       0, "system/apex/com.android.tethering/bin/for-system" }, | 
| Nick Kralevich | 8d1b43b | 2019-04-02 08:54:17 -0700 | [diff] [blame] | 83 | { 00751, AID_ROOT,         AID_SHELL,        0, "system/bin" }, | 
| Mark Salyzyn | c4f9f4c | 2017-03-20 08:15:40 -0700 | [diff] [blame] | 84 | { 00755, AID_ROOT,         AID_ROOT,         0, "system/etc/ppp" }, | 
|  | 85 | { 00755, AID_ROOT,         AID_SHELL,        0, "system/vendor" }, | 
| Mathew Inwood | 42a1a12 | 2020-07-20 15:14:55 +0000 | [diff] [blame] | 86 | { 00750, AID_ROOT,         AID_SHELL,        0, "system/xbin" }, | 
| Nick Kralevich | 6bcd50e | 2019-09-10 18:54:28 -0700 | [diff] [blame] | 87 | { 00751, AID_ROOT,         AID_SHELL,        0, "system/apex/*/bin" }, | 
| Maciej Żenczykowski | 93c9dfc | 2023-04-18 21:08:12 +0000 | [diff] [blame] | 88 | { 00750, AID_ROOT,         AID_SYSTEM,       0, "system_ext/apex/com.android.tethering/bin/for-system" }, | 
| Jeongik Cha | 4e467e7 | 2019-11-07 15:41:48 +0900 | [diff] [blame] | 89 | { 00751, AID_ROOT,         AID_SHELL,        0, "system_ext/bin" }, | 
| Jooyung Han | 08792eb | 2019-12-20 03:54:57 +0900 | [diff] [blame] | 90 | { 00751, AID_ROOT,         AID_SHELL,        0, "system_ext/apex/*/bin" }, | 
| Nick Kralevich | f4fc922 | 2018-11-11 08:39:20 -0800 | [diff] [blame] | 91 | { 00751, AID_ROOT,         AID_SHELL,        0, "vendor/bin" }, | 
| Jooyung Han | a70cea5 | 2020-09-21 19:37:05 +0900 | [diff] [blame] | 92 | { 00751, AID_ROOT,         AID_SHELL,        0, "vendor/apex/*/bin" }, | 
| Mark Salyzyn | c4f9f4c | 2017-03-20 08:15:40 -0700 | [diff] [blame] | 93 | { 00755, AID_ROOT,         AID_SHELL,        0, "vendor" }, | 
|  | 94 | { 00755, AID_ROOT,         AID_ROOT,         0, 0 }, | 
| Nick Kralevich | f4fc922 | 2018-11-11 08:39:20 -0800 | [diff] [blame] | 95 | // clang-format on | 
| Mark Salyzyn | 8b2c7de | 2015-04-01 07:42:01 -0700 | [diff] [blame] | 96 | }; | 
| Mark Salyzyn | 0f6a270 | 2017-05-02 08:56:15 -0700 | [diff] [blame] | 97 | #ifndef __ANDROID_VNDK__ | 
|  | 98 | auto __for_testing_only__android_dirs = android_dirs; | 
|  | 99 | #endif | 
| Mark Salyzyn | 8b2c7de | 2015-04-01 07:42:01 -0700 | [diff] [blame] | 100 |  | 
| Mark Salyzyn | 163ecc6 | 2017-05-02 08:56:15 -0700 | [diff] [blame] | 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. | 
| Mark Salyzyn | 9ceadcb | 2017-03-20 08:15:40 -0700 | [diff] [blame] | 106 | static const char sys_conf_dir[] = "/system/etc/fs_config_dirs"; | 
|  | 107 | static const char sys_conf_file[] = "/system/etc/fs_config_files"; | 
| Mark Salyzyn | 163ecc6 | 2017-05-02 08:56:15 -0700 | [diff] [blame] | 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 | 
| Mark Salyzyn | 757658c | 2018-11-19 11:17:35 -0800 | [diff] [blame] | 112 | // customization can also land on /system/vendor, /system/oem, /system/odm, | 
| Justin Yun | 7eaf9b5 | 2019-06-28 14:28:00 +0900 | [diff] [blame] | 113 | // /system/product or /system/system_ext. | 
| Mark Salyzyn | 757658c | 2018-11-19 11:17:35 -0800 | [diff] [blame] | 114 | // | 
| Mark Salyzyn | 163ecc6 | 2017-05-02 08:56:15 -0700 | [diff] [blame] | 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) | 
| Mark Salyzyn | 9ceadcb | 2017-03-20 08:15:40 -0700 | [diff] [blame] | 117 | static const char ven_conf_dir[] = "/vendor/etc/fs_config_dirs"; | 
|  | 118 | static const char ven_conf_file[] = "/vendor/etc/fs_config_files"; | 
| Mark Salyzyn | 62c701e | 2017-03-21 08:09:52 -0700 | [diff] [blame] | 119 | static const char oem_conf_dir[] = "/oem/etc/fs_config_dirs"; | 
|  | 120 | static const char oem_conf_file[] = "/oem/etc/fs_config_files"; | 
| Mark Salyzyn | 2b616ce | 2017-03-22 15:23:24 -0700 | [diff] [blame] | 121 | static const char odm_conf_dir[] = "/odm/etc/fs_config_dirs"; | 
|  | 122 | static const char odm_conf_file[] = "/odm/etc/fs_config_files"; | 
| Mark Salyzyn | 757658c | 2018-11-19 11:17:35 -0800 | [diff] [blame] | 123 | static const char product_conf_dir[] = "/product/etc/fs_config_dirs"; | 
|  | 124 | static const char product_conf_file[] = "/product/etc/fs_config_files"; | 
| Justin Yun | 7eaf9b5 | 2019-06-28 14:28:00 +0900 | [diff] [blame] | 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"; | 
| Mark Salyzyn | 9ceadcb | 2017-03-20 08:15:40 -0700 | [diff] [blame] | 127 | static const char* conf[][2] = { | 
| Justin Yun | 7eaf9b5 | 2019-06-28 14:28:00 +0900 | [diff] [blame] | 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}, | 
| Mark Salyzyn | 9ceadcb | 2017-03-20 08:15:40 -0700 | [diff] [blame] | 131 | }; | 
| Mark Salyzyn | 6865114 | 2015-04-01 09:24:22 -0700 | [diff] [blame] | 132 |  | 
| Tom Cherry | 07aa2a6 | 2018-03-28 13:25:11 -0700 | [diff] [blame] | 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 | 
| Mark Salyzyn | 8b2c7de | 2015-04-01 07:42:01 -0700 | [diff] [blame] | 139 | static const struct fs_path_config android_files[] = { | 
| Dario Freni | 4fa866a | 2018-05-25 16:07:19 +0100 | [diff] [blame] | 140 | // clang-format off | 
| Mark Salyzyn | a45eae9 | 2017-03-20 08:53:24 -0700 | [diff] [blame] | 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/*" }, | 
| Akos Denke | bb61a6e | 2019-10-10 08:27:11 +0200 | [diff] [blame] | 150 | { 00750, AID_ROOT,      AID_SHELL,     0, "data/benchmarktest/*" }, | 
|  | 151 | { 00750, AID_ROOT,      AID_SHELL,     0, "data/benchmarktest64/*" }, | 
| Hung-ying Tyan | 527d80d | 2017-06-02 18:59:46 +0800 | [diff] [blame] | 152 | { 00600, AID_ROOT,      AID_ROOT,      0, "default.prop" }, // legacy | 
|  | 153 | { 00600, AID_ROOT,      AID_ROOT,      0, "system/etc/prop.default" }, | 
| Bowgo Tsai | 9fc8dcf | 2019-05-17 15:40:18 +0800 | [diff] [blame] | 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" }, | 
| Mark Salyzyn | 2b616ce | 2017-03-22 15:23:24 -0700 | [diff] [blame] | 157 | { 00444, AID_ROOT,      AID_ROOT,      0, odm_conf_dir + 1 }, | 
|  | 158 | { 00444, AID_ROOT,      AID_ROOT,      0, odm_conf_file + 1 }, | 
| Mark Salyzyn | 62c701e | 2017-03-21 08:09:52 -0700 | [diff] [blame] | 159 | { 00444, AID_ROOT,      AID_ROOT,      0, oem_conf_dir + 1 }, | 
|  | 160 | { 00444, AID_ROOT,      AID_ROOT,      0, oem_conf_file + 1 }, | 
| Jaekyun Seok | dff165d | 2017-11-28 12:10:10 +0900 | [diff] [blame] | 161 | { 00600, AID_ROOT,      AID_ROOT,      0, "product/build.prop" }, | 
| Mark Salyzyn | 757658c | 2018-11-19 11:17:35 -0800 | [diff] [blame] | 162 | { 00444, AID_ROOT,      AID_ROOT,      0, product_conf_dir + 1 }, | 
|  | 163 | { 00444, AID_ROOT,      AID_ROOT,      0, product_conf_file + 1 }, | 
| Justin Yun | 7eaf9b5 | 2019-06-28 14:28:00 +0900 | [diff] [blame] | 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 }, | 
| Mark Salyzyn | a45eae9 | 2017-03-20 08:53:24 -0700 | [diff] [blame] | 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" }, | 
| Luis Hector Chavez | d32c36c | 2018-06-08 15:00:40 -0700 | [diff] [blame] | 170 | { 00550, AID_LOGD,      AID_LOGD,      0, "system/bin/logd" }, | 
| Mark Salyzyn | a45eae9 | 2017-03-20 08:53:24 -0700 | [diff] [blame] | 171 | { 00700, AID_ROOT,      AID_ROOT,      0, "system/bin/secilc" }, | 
|  | 172 | { 00750, AID_ROOT,      AID_ROOT,      0, "system/bin/uncrypt" }, | 
| Hung-ying Tyan | 3346338 | 2017-05-25 19:18:17 +0800 | [diff] [blame] | 173 | { 00600, AID_ROOT,      AID_ROOT,      0, "system/build.prop" }, | 
| Mark Salyzyn | 9ceadcb | 2017-03-20 08:15:40 -0700 | [diff] [blame] | 174 | { 00444, AID_ROOT,      AID_ROOT,      0, sys_conf_dir + 1 }, | 
|  | 175 | { 00444, AID_ROOT,      AID_ROOT,      0, sys_conf_file + 1 }, | 
| Mark Salyzyn | 8b2c7de | 2015-04-01 07:42:01 -0700 | [diff] [blame] | 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" }, | 
| Mark Salyzyn | 8b2c7de | 2015-04-01 07:42:01 -0700 | [diff] [blame] | 179 | { 00555, AID_ROOT,      AID_ROOT,      0, "system/etc/ppp/*" }, | 
|  | 180 | { 00555, AID_ROOT,      AID_ROOT,      0, "system/etc/rc.*" }, | 
| Bill Peckham | dea8c9c | 2019-09-17 16:30:37 -0700 | [diff] [blame] | 181 | { 00750, AID_ROOT,      AID_ROOT,      0, "vendor/bin/install-recovery.sh" }, | 
| Hung-ying Tyan | 3346338 | 2017-05-25 19:18:17 +0800 | [diff] [blame] | 182 | { 00600, AID_ROOT,      AID_ROOT,      0, "vendor/build.prop" }, | 
|  | 183 | { 00600, AID_ROOT,      AID_ROOT,      0, "vendor/default.prop" }, | 
| Bill Peckham | dea8c9c | 2019-09-17 16:30:37 -0700 | [diff] [blame] | 184 | { 00440, AID_ROOT,      AID_ROOT,      0, "vendor/etc/recovery.img" }, | 
| Mark Salyzyn | 9ceadcb | 2017-03-20 08:15:40 -0700 | [diff] [blame] | 185 | { 00444, AID_ROOT,      AID_ROOT,      0, ven_conf_dir + 1 }, | 
|  | 186 | { 00444, AID_ROOT,      AID_ROOT,      0, ven_conf_file + 1 }, | 
| Mark Salyzyn | 8b2c7de | 2015-04-01 07:42:01 -0700 | [diff] [blame] | 187 |  | 
| Mark Salyzyn | 163ecc6 | 2017-05-02 08:56:15 -0700 | [diff] [blame] | 188 | // the following two files are INTENTIONALLY set-uid, but they | 
|  | 189 | // are NOT included on user builds. | 
| Mark Salyzyn | 8b2c7de | 2015-04-01 07:42:01 -0700 | [diff] [blame] | 190 | { 06755, AID_ROOT,      AID_ROOT,      0, "system/xbin/procmem" }, | 
| Mark Salyzyn | a45eae9 | 2017-03-20 08:53:24 -0700 | [diff] [blame] | 191 | { 04750, AID_ROOT,      AID_SHELL,     0, "system/xbin/su" }, | 
| Mark Salyzyn | 8b2c7de | 2015-04-01 07:42:01 -0700 | [diff] [blame] | 192 |  | 
| Mark Salyzyn | 163ecc6 | 2017-05-02 08:56:15 -0700 | [diff] [blame] | 193 | // the following files have enhanced capabilities and ARE included | 
|  | 194 | // in user builds. | 
| Maciej Żenczykowski | 1ba9440 | 2023-04-11 06:08:32 +0000 | [diff] [blame] | 195 | { 06755, AID_CLAT,      AID_CLAT,      0, "system/apex/com.android.tethering/bin/for-system/clatd" }, | 
| Maciej Żenczykowski | 93c9dfc | 2023-04-18 21:08:12 +0000 | [diff] [blame] | 196 | { 06755, AID_CLAT,      AID_CLAT,      0, "system_ext/apex/com.android.tethering/bin/for-system/clatd" }, | 
| Mark Salyzyn | a45eae9 | 2017-03-20 08:53:24 -0700 | [diff] [blame] | 197 | { 00700, AID_SYSTEM,    AID_SHELL,     CAP_MASK_LONG(CAP_BLOCK_SUSPEND), | 
|  | 198 | "system/bin/inputflinger" }, | 
| Mark Salyzyn | 0d2a1dc | 2016-10-28 12:41:17 -0700 | [diff] [blame] | 199 | { 00750, AID_ROOT,      AID_SHELL,     CAP_MASK_LONG(CAP_SETUID) | | 
|  | 200 | CAP_MASK_LONG(CAP_SETGID), | 
|  | 201 | "system/bin/run-as" }, | 
| Yabin Cui | af3e30d | 2019-01-08 16:31:56 -0800 | [diff] [blame] | 202 | { 00750, AID_ROOT,      AID_SHELL,     CAP_MASK_LONG(CAP_SETUID) | | 
|  | 203 | CAP_MASK_LONG(CAP_SETGID), | 
|  | 204 | "system/bin/simpleperf_app_runner" }, | 
| Yifan Hong | 04b932b | 2020-01-22 16:44:55 -0800 | [diff] [blame] | 205 | { 00755, AID_ROOT,      AID_ROOT,      0, "first_stage_ramdisk/system/bin/e2fsck" }, | 
| Yifan Hong | c7ed02f | 2020-11-19 15:20:44 -0800 | [diff] [blame] | 206 | #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 Kim | b9be427 | 2020-02-12 12:27:33 -0800 | [diff] [blame] | 211 | { 00755, AID_ROOT,      AID_ROOT,      0, "first_stage_ramdisk/system/bin/resize2fs" }, | 
| David Anderson | e80a153 | 2020-07-08 13:31:37 -0700 | [diff] [blame] | 212 | { 00755, AID_ROOT,      AID_ROOT,      0, "first_stage_ramdisk/system/bin/snapuserd" }, | 
| Ray-cy.lee | d865493 | 2023-07-31 20:00:43 +0800 | [diff] [blame] | 213 | { 00755, AID_ROOT,      AID_ROOT,      0, "first_stage_ramdisk/system/bin/snapuserd_ramdisk" }, | 
| Yifan Hong | c7ed02f | 2020-11-19 15:20:44 -0800 | [diff] [blame] | 214 | { 00755, AID_ROOT,      AID_ROOT,      0, "first_stage_ramdisk/system/bin/tune2fs" }, | 
| Jaegeuk Kim | b783310 | 2021-12-29 15:11:00 -0800 | [diff] [blame] | 215 | { 00755, AID_ROOT,      AID_ROOT,      0, "first_stage_ramdisk/system/bin/fsck.f2fs" }, | 
| Mark Salyzyn | 163ecc6 | 2017-05-02 08:56:15 -0700 | [diff] [blame] | 216 | // generic defaults | 
| Mark Salyzyn | a45eae9 | 2017-03-20 08:53:24 -0700 | [diff] [blame] | 217 | { 00755, AID_ROOT,      AID_ROOT,      0, "bin/*" }, | 
|  | 218 | { 00640, AID_ROOT,      AID_SHELL,     0, "fstab.*" }, | 
|  | 219 | { 00750, AID_ROOT,      AID_SHELL,     0, "init*" }, | 
| Min Yun | a8eb007 | 2019-05-03 17:48:17 +0900 | [diff] [blame] | 220 | { 00755, AID_ROOT,      AID_SHELL,     0, "odm/bin/*" }, | 
| Inseob Kim | 25195da | 2018-05-31 13:03:58 +0900 | [diff] [blame] | 221 | { 00755, AID_ROOT,      AID_SHELL,     0, "product/bin/*" }, | 
| Jooyung Han | a70cea5 | 2020-09-21 19:37:05 +0900 | [diff] [blame] | 222 | { 00755, AID_ROOT,      AID_SHELL,     0, "product/apex/*bin/*" }, | 
| Mark Salyzyn | 8b2c7de | 2015-04-01 07:42:01 -0700 | [diff] [blame] | 223 | { 00755, AID_ROOT,      AID_SHELL,     0, "system/bin/*" }, | 
| Mark Salyzyn | a45eae9 | 2017-03-20 08:53:24 -0700 | [diff] [blame] | 224 | { 00755, AID_ROOT,      AID_SHELL,     0, "system/xbin/*" }, | 
| Jiyong Park | defdb18 | 2019-02-09 02:58:12 +0900 | [diff] [blame] | 225 | { 00755, AID_ROOT,      AID_SHELL,     0, "system/apex/*/bin/*" }, | 
| Jeongik Cha | 4e467e7 | 2019-11-07 15:41:48 +0900 | [diff] [blame] | 226 | { 00755, AID_ROOT,      AID_SHELL,     0, "system_ext/bin/*" }, | 
| Jooyung Han | 08792eb | 2019-12-20 03:54:57 +0900 | [diff] [blame] | 227 | { 00755, AID_ROOT,      AID_SHELL,     0, "system_ext/apex/*/bin/*" }, | 
| Mark Salyzyn | 8b2c7de | 2015-04-01 07:42:01 -0700 | [diff] [blame] | 228 | { 00755, AID_ROOT,      AID_SHELL,     0, "vendor/bin/*" }, | 
| Jooyung Han | a70cea5 | 2020-09-21 19:37:05 +0900 | [diff] [blame] | 229 | { 00755, AID_ROOT,      AID_SHELL,     0, "vendor/apex/*bin/*" }, | 
| Erik Kline | a5a9c74 | 2016-05-27 13:13:07 +0900 | [diff] [blame] | 230 | { 00755, AID_ROOT,      AID_SHELL,     0, "vendor/xbin/*" }, | 
| Mark Salyzyn | 8b2c7de | 2015-04-01 07:42:01 -0700 | [diff] [blame] | 231 | { 00644, AID_ROOT,      AID_ROOT,      0, 0 }, | 
| Dario Freni | 4fa866a | 2018-05-25 16:07:19 +0100 | [diff] [blame] | 232 | // clang-format on | 
| Mark Salyzyn | 8b2c7de | 2015-04-01 07:42:01 -0700 | [diff] [blame] | 233 | }; | 
| Mark Salyzyn | 0f6a270 | 2017-05-02 08:56:15 -0700 | [diff] [blame] | 234 | #ifndef __ANDROID_VNDK__ | 
|  | 235 | auto __for_testing_only__android_files = android_files; | 
|  | 236 | #endif | 
| Mark Salyzyn | 8b2c7de | 2015-04-01 07:42:01 -0700 | [diff] [blame] | 237 |  | 
| Mark Salyzyn | ffa7632 | 2017-04-05 12:15:49 -0700 | [diff] [blame] | 238 | static size_t strip(const char* path, size_t len, const char suffix[]) { | 
|  | 239 | if (len < strlen(suffix)) return len; | 
|  | 240 | if (strncmp(path + len - strlen(suffix), suffix, strlen(suffix))) return len; | 
|  | 241 | return len - strlen(suffix); | 
|  | 242 | } | 
|  | 243 |  | 
| Mark Salyzyn | 9ceadcb | 2017-03-20 08:15:40 -0700 | [diff] [blame] | 244 | static int fs_config_open(int dir, int which, const char* target_out_path) { | 
| Mark Salyzyn | 6865114 | 2015-04-01 09:24:22 -0700 | [diff] [blame] | 245 | int fd = -1; | 
|  | 246 |  | 
| Thierry Strudel | df33ffa | 2015-07-09 09:50:31 -0700 | [diff] [blame] | 247 | if (target_out_path && *target_out_path) { | 
| Mark Salyzyn | 163ecc6 | 2017-05-02 08:56:15 -0700 | [diff] [blame] | 248 | // target_out_path is the path to the directory holding content of | 
|  | 249 | // system partition but as we cannot guarantee it ends with '/system' | 
|  | 250 | // or with or without a trailing slash, need to strip them carefully. | 
| Mark Salyzyn | 166e24c | 2017-03-20 08:15:40 -0700 | [diff] [blame] | 251 | char* name = NULL; | 
| Mark Salyzyn | ffa7632 | 2017-04-05 12:15:49 -0700 | [diff] [blame] | 252 | size_t len = strlen(target_out_path); | 
|  | 253 | len = strip(target_out_path, len, "/"); | 
|  | 254 | len = strip(target_out_path, len, "/system"); | 
|  | 255 | if (asprintf(&name, "%.*s%s", (int)len, target_out_path, conf[which][dir]) != -1) { | 
| Elliott Hughes | f6a84e4 | 2023-09-22 14:37:37 +0000 | [diff] [blame] | 256 | fd = TEMP_FAILURE_RETRY(open(name, O_RDONLY)); | 
| Mark Salyzyn | 6865114 | 2015-04-01 09:24:22 -0700 | [diff] [blame] | 257 | free(name); | 
|  | 258 | } | 
|  | 259 | } | 
|  | 260 | if (fd < 0) { | 
| Elliott Hughes | f6a84e4 | 2023-09-22 14:37:37 +0000 | [diff] [blame] | 261 | fd = TEMP_FAILURE_RETRY(open(conf[which][dir], O_RDONLY)); | 
| Mark Salyzyn | 6865114 | 2015-04-01 09:24:22 -0700 | [diff] [blame] | 262 | } | 
|  | 263 | return fd; | 
|  | 264 | } | 
|  | 265 |  | 
| Mark Salyzyn | 757658c | 2018-11-19 11:17:35 -0800 | [diff] [blame] | 266 | // if path is "odm/<stuff>", "oem/<stuff>", "product/<stuff>", | 
| Justin Yun | 7eaf9b5 | 2019-06-28 14:28:00 +0900 | [diff] [blame] | 267 | // "system_ext/<stuff>" or "vendor/<stuff>" | 
| Jiyong Park | a2159c4 | 2019-02-03 00:34:29 +0900 | [diff] [blame] | 268 | static bool is_partition(const std::string& path) { | 
| Justin Yun | 7eaf9b5 | 2019-06-28 14:28:00 +0900 | [diff] [blame] | 269 | static const char* partitions[] = {"odm/", "oem/", "product/", "system_ext/", "vendor/"}; | 
| Mark Salyzyn | 2d37797 | 2017-05-02 14:02:17 -0700 | [diff] [blame] | 270 | for (size_t i = 0; i < (sizeof(partitions) / sizeof(partitions[0])); ++i) { | 
| Jiyong Park | a2159c4 | 2019-02-03 00:34:29 +0900 | [diff] [blame] | 271 | if (StartsWith(path, partitions[i])) return true; | 
| Mark Salyzyn | 2d37797 | 2017-05-02 14:02:17 -0700 | [diff] [blame] | 272 | } | 
|  | 273 | return false; | 
|  | 274 | } | 
|  | 275 |  | 
|  | 276 | // alias prefixes of "<partition>/<stuff>" to "system/<partition>/<stuff>" or | 
|  | 277 | // "system/<partition>/<stuff>" to "<partition>/<stuff>" | 
| Jiyong Park | a2159c4 | 2019-02-03 00:34:29 +0900 | [diff] [blame] | 278 | static bool fs_config_cmp(bool dir, const char* prefix, size_t len, const char* path, size_t plen) { | 
|  | 279 | std::string pattern(prefix, len); | 
|  | 280 | std::string input(path, plen); | 
|  | 281 |  | 
|  | 282 | // Massage pattern and input so that they can be used by fnmatch where | 
|  | 283 | // directories have to end with /. | 
|  | 284 | if (dir) { | 
|  | 285 | if (!EndsWith(input, "/")) { | 
|  | 286 | input.append("/"); | 
|  | 287 | } | 
|  | 288 |  | 
|  | 289 | if (!EndsWith(pattern, "/*")) { | 
|  | 290 | if (EndsWith(pattern, "/")) { | 
|  | 291 | pattern.append("*"); | 
|  | 292 | } else { | 
|  | 293 | pattern.append("/*"); | 
|  | 294 | } | 
|  | 295 | } | 
| Ben Fennema | acd7b7b | 2017-06-22 15:15:56 -0700 | [diff] [blame] | 296 | } | 
|  | 297 |  | 
| Jiyong Park | a2159c4 | 2019-02-03 00:34:29 +0900 | [diff] [blame] | 298 | // no FNM_PATHNAME is set in order to match a/b/c/d with a/* | 
|  | 299 | // FNM_ESCAPE is set in order to prevent using \\? and \\* and maintenance issues. | 
|  | 300 | const int fnm_flags = FNM_NOESCAPE; | 
|  | 301 | if (fnmatch(pattern.c_str(), input.c_str(), fnm_flags) == 0) return true; | 
| Mark Salyzyn | 2d37797 | 2017-05-02 14:02:17 -0700 | [diff] [blame] | 302 |  | 
| Min Yun | a8eb007 | 2019-05-03 17:48:17 +0900 | [diff] [blame] | 303 | // Check match between logical partition's files and patterns. | 
| Justin Yun | 7eaf9b5 | 2019-06-28 14:28:00 +0900 | [diff] [blame] | 304 | static constexpr const char* kLogicalPartitions[] = {"system/product/", "system/system_ext/", | 
|  | 305 | "system/vendor/", "vendor/odm/"}; | 
| Min Yun | a8eb007 | 2019-05-03 17:48:17 +0900 | [diff] [blame] | 306 | for (auto& logical_partition : kLogicalPartitions) { | 
|  | 307 | if (StartsWith(input, logical_partition)) { | 
|  | 308 | std::string input_in_partition = input.substr(input.find('/') + 1); | 
|  | 309 | if (!is_partition(input_in_partition)) continue; | 
|  | 310 | if (fnmatch(pattern.c_str(), input_in_partition.c_str(), fnm_flags) == 0) { | 
|  | 311 | return true; | 
|  | 312 | } | 
|  | 313 | } | 
| Mark Salyzyn | 2d37797 | 2017-05-02 14:02:17 -0700 | [diff] [blame] | 314 | } | 
| Min Yun | a8eb007 | 2019-05-03 17:48:17 +0900 | [diff] [blame] | 315 | return false; | 
| Mark Salyzyn | 2d37797 | 2017-05-02 14:02:17 -0700 | [diff] [blame] | 316 | } | 
| Ben Fennema | acd7b7b | 2017-06-22 15:15:56 -0700 | [diff] [blame] | 317 | #ifndef __ANDROID_VNDK__ | 
|  | 318 | auto __for_testing_only__fs_config_cmp = fs_config_cmp; | 
|  | 319 | #endif | 
| Mark Salyzyn | 6865114 | 2015-04-01 09:24:22 -0700 | [diff] [blame] | 320 |  | 
| Mark Salyzyn | 166e24c | 2017-03-20 08:15:40 -0700 | [diff] [blame] | 321 | void fs_config(const char* path, int dir, const char* target_out_path, unsigned* uid, unsigned* gid, | 
|  | 322 | unsigned* mode, uint64_t* capabilities) { | 
|  | 323 | const struct fs_path_config* pc; | 
| Mark Salyzyn | 9ceadcb | 2017-03-20 08:15:40 -0700 | [diff] [blame] | 324 | size_t which, plen; | 
| Mark Salyzyn | 8b2c7de | 2015-04-01 07:42:01 -0700 | [diff] [blame] | 325 |  | 
|  | 326 | if (path[0] == '/') { | 
|  | 327 | path++; | 
|  | 328 | } | 
|  | 329 |  | 
| Mark Salyzyn | 7e82633 | 2015-04-15 22:30:30 +0000 | [diff] [blame] | 330 | plen = strlen(path); | 
| Mark Salyzyn | 6865114 | 2015-04-01 09:24:22 -0700 | [diff] [blame] | 331 |  | 
| Mark Salyzyn | 9ceadcb | 2017-03-20 08:15:40 -0700 | [diff] [blame] | 332 | for (which = 0; which < (sizeof(conf) / sizeof(conf[0])); ++which) { | 
| Mark Salyzyn | 7977cc6 | 2015-04-15 19:27:39 -0700 | [diff] [blame] | 333 | struct fs_path_config_from_file header; | 
|  | 334 |  | 
| Mark Salyzyn | 9ceadcb | 2017-03-20 08:15:40 -0700 | [diff] [blame] | 335 | int fd = fs_config_open(dir, which, target_out_path); | 
|  | 336 | if (fd < 0) continue; | 
|  | 337 |  | 
| Mark Salyzyn | 7977cc6 | 2015-04-15 19:27:39 -0700 | [diff] [blame] | 338 | while (TEMP_FAILURE_RETRY(read(fd, &header, sizeof(header))) == sizeof(header)) { | 
| Mark Salyzyn | 166e24c | 2017-03-20 08:15:40 -0700 | [diff] [blame] | 339 | char* prefix; | 
| Tom Cherry | 5550d4e | 2019-06-17 14:28:37 -0700 | [diff] [blame] | 340 | uint16_t host_len = header.len; | 
| Mark Salyzyn | 7977cc6 | 2015-04-15 19:27:39 -0700 | [diff] [blame] | 341 | ssize_t len, remainder = host_len - sizeof(header); | 
| Mark Salyzyn | 6865114 | 2015-04-01 09:24:22 -0700 | [diff] [blame] | 342 | if (remainder <= 0) { | 
| Mark Salyzyn | 9ceadcb | 2017-03-20 08:15:40 -0700 | [diff] [blame] | 343 | ALOGE("%s len is corrupted", conf[which][dir]); | 
| Mark Salyzyn | 6865114 | 2015-04-01 09:24:22 -0700 | [diff] [blame] | 344 | break; | 
|  | 345 | } | 
| Mark Salyzyn | 163ecc6 | 2017-05-02 08:56:15 -0700 | [diff] [blame] | 346 | prefix = static_cast<char*>(calloc(1, remainder)); | 
| Mark Salyzyn | 7977cc6 | 2015-04-15 19:27:39 -0700 | [diff] [blame] | 347 | if (!prefix) { | 
| Mark Salyzyn | 9ceadcb | 2017-03-20 08:15:40 -0700 | [diff] [blame] | 348 | ALOGE("%s out of memory", conf[which][dir]); | 
| Mark Salyzyn | 7977cc6 | 2015-04-15 19:27:39 -0700 | [diff] [blame] | 349 | break; | 
| Mark Salyzyn | 6865114 | 2015-04-01 09:24:22 -0700 | [diff] [blame] | 350 | } | 
| Mark Salyzyn | 7977cc6 | 2015-04-15 19:27:39 -0700 | [diff] [blame] | 351 | if (TEMP_FAILURE_RETRY(read(fd, prefix, remainder)) != remainder) { | 
|  | 352 | free(prefix); | 
| Mark Salyzyn | 9ceadcb | 2017-03-20 08:15:40 -0700 | [diff] [blame] | 353 | ALOGE("%s prefix is truncated", conf[which][dir]); | 
| Mark Salyzyn | 7977cc6 | 2015-04-15 19:27:39 -0700 | [diff] [blame] | 354 | break; | 
|  | 355 | } | 
|  | 356 | len = strnlen(prefix, remainder); | 
| Mark Salyzyn | 163ecc6 | 2017-05-02 08:56:15 -0700 | [diff] [blame] | 357 | if (len >= remainder) {  // missing a terminating null | 
| Mark Salyzyn | 7977cc6 | 2015-04-15 19:27:39 -0700 | [diff] [blame] | 358 | free(prefix); | 
| Mark Salyzyn | 9ceadcb | 2017-03-20 08:15:40 -0700 | [diff] [blame] | 359 | ALOGE("%s is corrupted", conf[which][dir]); | 
| Mark Salyzyn | 6865114 | 2015-04-01 09:24:22 -0700 | [diff] [blame] | 360 | break; | 
|  | 361 | } | 
| Mark Salyzyn | 7977cc6 | 2015-04-15 19:27:39 -0700 | [diff] [blame] | 362 | if (fs_config_cmp(dir, prefix, len, path, plen)) { | 
|  | 363 | free(prefix); | 
|  | 364 | close(fd); | 
| Tom Cherry | 5550d4e | 2019-06-17 14:28:37 -0700 | [diff] [blame] | 365 | *uid = header.uid; | 
|  | 366 | *gid = header.gid; | 
|  | 367 | *mode = (*mode & (~07777)) | header.mode; | 
|  | 368 | *capabilities = header.capabilities; | 
| Mark Salyzyn | 7977cc6 | 2015-04-15 19:27:39 -0700 | [diff] [blame] | 369 | return; | 
| Mark Salyzyn | 6865114 | 2015-04-01 09:24:22 -0700 | [diff] [blame] | 370 | } | 
| Mark Salyzyn | 7977cc6 | 2015-04-15 19:27:39 -0700 | [diff] [blame] | 371 | free(prefix); | 
| Mark Salyzyn | 7e82633 | 2015-04-15 22:30:30 +0000 | [diff] [blame] | 372 | } | 
| Mark Salyzyn | 6865114 | 2015-04-01 09:24:22 -0700 | [diff] [blame] | 373 | close(fd); | 
| Mark Salyzyn | 6865114 | 2015-04-01 09:24:22 -0700 | [diff] [blame] | 374 | } | 
|  | 375 |  | 
| Mark Salyzyn | 166e24c | 2017-03-20 08:15:40 -0700 | [diff] [blame] | 376 | for (pc = dir ? android_dirs : android_files; pc->prefix; pc++) { | 
| Mark Salyzyn | 6865114 | 2015-04-01 09:24:22 -0700 | [diff] [blame] | 377 | if (fs_config_cmp(dir, pc->prefix, strlen(pc->prefix), path, plen)) { | 
|  | 378 | break; | 
| Mark Salyzyn | 8b2c7de | 2015-04-01 07:42:01 -0700 | [diff] [blame] | 379 | } | 
|  | 380 | } | 
|  | 381 | *uid = pc->uid; | 
|  | 382 | *gid = pc->gid; | 
|  | 383 | *mode = (*mode & (~07777)) | pc->mode; | 
|  | 384 | *capabilities = pc->capabilities; | 
|  | 385 | } |