Tom Cherry | c317009 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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 | // This file contains the functions that initialize SELinux during boot as well as helper functions |
| 18 | // for SELinux operation for init. |
| 19 | |
| 20 | // When the system boots, there is no SEPolicy present and init is running in the kernel domain. |
Tom Cherry | 7bfea3d | 2018-11-06 14:12:05 -0800 | [diff] [blame] | 21 | // Init loads the SEPolicy from the file system, restores the context of /system/bin/init based on |
| 22 | // this SEPolicy, and finally exec()'s itself to run in the proper domain. |
Tom Cherry | c317009 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 23 | |
| 24 | // The SEPolicy on Android comes in two variants: monolithic and split. |
| 25 | |
| 26 | // The monolithic policy variant is for legacy non-treble devices that contain a single SEPolicy |
| 27 | // file located at /sepolicy and is directly loaded into the kernel SELinux subsystem. |
| 28 | |
Thiébaud Weksteen | 50f03fd | 2023-09-06 10:52:49 +1000 | [diff] [blame] | 29 | // The split policy is for supporting treble devices. It splits the SEPolicy across files on |
| 30 | // /system/etc/selinux (the 'plat' portion of the policy) and /vendor/etc/selinux (the 'vendor' |
| 31 | // portion of the policy). This is necessary to allow the system image to be updated independently |
| 32 | // of the vendor image, while maintaining contributions from both partitions in the SEPolicy. This |
| 33 | // is especially important for VTS testing, where the SEPolicy on the Google System Image may not be |
| 34 | // identical to the system image shipped on a vendor's device. |
Tom Cherry | c317009 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 35 | |
| 36 | // The split SEPolicy is loaded as described below: |
Tri Vo | c8137f9 | 2019-01-22 18:22:25 -0800 | [diff] [blame] | 37 | // 1) There is a precompiled SEPolicy located at either /vendor/etc/selinux/precompiled_sepolicy or |
| 38 | // /odm/etc/selinux/precompiled_sepolicy if odm parition is present. Stored along with this file |
Thiébaud Weksteen | 50f03fd | 2023-09-06 10:52:49 +1000 | [diff] [blame] | 39 | // are the sha256 hashes of the parts of the SEPolicy on /system, /system_ext and /product that |
| 40 | // were used to compile this precompiled policy. The system partition contains a similar sha256 |
| 41 | // of the parts of the SEPolicy that it currently contains. Symmetrically, system_ext and |
| 42 | // product paritition contain sha256 hashes of their SEPolicy. The init loads this |
Bowgo Tsai | f016f25 | 2019-08-28 17:56:51 +0800 | [diff] [blame] | 43 | // precompiled_sepolicy directly if and only if the hashes along with the precompiled SEPolicy on |
Thiébaud Weksteen | 50f03fd | 2023-09-06 10:52:49 +1000 | [diff] [blame] | 44 | // /vendor or /odm match the hashes for system, system_ext and product SEPolicy, respectively. |
| 45 | // 2) If these hashes do not match, then either /system or /system_ext or /product (or some of them) |
| 46 | // have been updated out of sync with /vendor (or /odm if it is present) and the init needs to |
| 47 | // compile the SEPolicy. /system contains the SEPolicy compiler, secilc, and it is used by the |
| 48 | // OpenSplitPolicy() function below to compile the SEPolicy to a temp directory and load it. |
Bowgo Tsai | f016f25 | 2019-08-28 17:56:51 +0800 | [diff] [blame] | 49 | // That function contains even more documentation with the specific implementation details of how |
| 50 | // the SEPolicy is compiled if needed. |
Tom Cherry | c317009 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 51 | |
| 52 | #include "selinux.h" |
| 53 | |
Tom Cherry | 40acb37 | 2018-08-01 13:41:12 -0700 | [diff] [blame] | 54 | #include <android/api-level.h> |
Tom Cherry | c317009 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 55 | #include <fcntl.h> |
Tom Cherry | 8180b48 | 2019-08-26 13:57:51 -0700 | [diff] [blame] | 56 | #include <linux/audit.h> |
| 57 | #include <linux/netlink.h> |
Tom Cherry | c317009 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 58 | #include <stdlib.h> |
| 59 | #include <sys/wait.h> |
| 60 | #include <unistd.h> |
| 61 | |
| 62 | #include <android-base/chrono_utils.h> |
| 63 | #include <android-base/file.h> |
| 64 | #include <android-base/logging.h> |
Logan Chien | 837b2a4 | 2018-05-03 14:33:52 +0800 | [diff] [blame] | 65 | #include <android-base/parseint.h> |
Inseob Kim | d99d977 | 2021-03-11 17:58:26 +0900 | [diff] [blame] | 66 | #include <android-base/result.h> |
David Anderson | d0ce530 | 2020-03-20 21:47:10 -0700 | [diff] [blame] | 67 | #include <android-base/strings.h> |
Tom Cherry | c317009 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 68 | #include <android-base/unique_fd.h> |
Nikita Ioffe | feb7e0e | 2024-03-28 00:32:36 +0000 | [diff] [blame] | 69 | #include <android/avf_cc_flags.h> |
Bowgo Tsai | 1dacd42 | 2019-03-04 17:53:34 +0800 | [diff] [blame] | 70 | #include <fs_avb/fs_avb.h> |
David Anderson | d0ce530 | 2020-03-20 21:47:10 -0700 | [diff] [blame] | 71 | #include <fs_mgr.h> |
Paul Lawrence | 6bb5289 | 2025-01-29 19:43:48 -0800 | [diff] [blame] | 72 | #include <fs_mgr_overlayfs.h> |
Inseob Kim | e2efde3 | 2024-11-20 17:56:20 +0900 | [diff] [blame] | 73 | #include <genfslabelsversion.h> |
Bowgo Tsai | 196cc58 | 2020-01-20 18:03:58 +0800 | [diff] [blame] | 74 | #include <libgsi/libgsi.h> |
Yifan Hong | d91998f | 2020-02-20 17:54:57 -0800 | [diff] [blame] | 75 | #include <libsnapshot/snapshot.h> |
Tom Cherry | c317009 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 76 | #include <selinux/android.h> |
| 77 | |
David Anderson | d0ce530 | 2020-03-20 21:47:10 -0700 | [diff] [blame] | 78 | #include "block_dev_initializer.h" |
Bowgo Tsai | 30afda7 | 2019-04-11 23:57:24 +0800 | [diff] [blame] | 79 | #include "debug_ramdisk.h" |
Tom Cherry | 7bfea3d | 2018-11-06 14:12:05 -0800 | [diff] [blame] | 80 | #include "reboot_utils.h" |
Paul Lawrence | 6bb5289 | 2025-01-29 19:43:48 -0800 | [diff] [blame] | 81 | #include "second_stage_resources.h" |
David Anderson | 491e4da | 2020-12-08 00:21:20 -0800 | [diff] [blame] | 82 | #include "snapuserd_transition.h" |
Tom Cherry | c317009 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 83 | #include "util.h" |
| 84 | |
Bowgo Tsai | 1dacd42 | 2019-03-04 17:53:34 +0800 | [diff] [blame] | 85 | using namespace std::string_literals; |
| 86 | |
Logan Chien | 837b2a4 | 2018-05-03 14:33:52 +0800 | [diff] [blame] | 87 | using android::base::ParseInt; |
Tom Cherry | c317009 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 88 | using android::base::Timer; |
| 89 | using android::base::unique_fd; |
Bowgo Tsai | 1dacd42 | 2019-03-04 17:53:34 +0800 | [diff] [blame] | 90 | using android::fs_mgr::AvbHandle; |
Yifan Hong | d91998f | 2020-02-20 17:54:57 -0800 | [diff] [blame] | 91 | using android::snapshot::SnapshotManager; |
Tom Cherry | c317009 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 92 | |
| 93 | namespace android { |
| 94 | namespace init { |
| 95 | |
Tom Cherry | c317009 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 96 | namespace { |
| 97 | |
| 98 | enum EnforcingStatus { SELINUX_PERMISSIVE, SELINUX_ENFORCING }; |
| 99 | |
Alistair Delva | 63594a4 | 2021-03-09 11:04:23 -0800 | [diff] [blame] | 100 | EnforcingStatus StatusFromProperty() { |
Yi-Yo Chiang | da5323e | 2023-08-02 01:08:23 +0800 | [diff] [blame] | 101 | std::string value; |
| 102 | if (android::fs_mgr::GetKernelCmdline("androidboot.selinux", &value) && value == "permissive") { |
| 103 | return SELINUX_PERMISSIVE; |
Alistair Delva | 63594a4 | 2021-03-09 11:04:23 -0800 | [diff] [blame] | 104 | } |
Yi-Yo Chiang | da5323e | 2023-08-02 01:08:23 +0800 | [diff] [blame] | 105 | if (android::fs_mgr::GetBootconfig("androidboot.selinux", &value) && value == "permissive") { |
| 106 | return SELINUX_PERMISSIVE; |
| 107 | } |
| 108 | return SELINUX_ENFORCING; |
Tom Cherry | c317009 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | bool IsEnforcing() { |
| 112 | if (ALLOW_PERMISSIVE_SELINUX) { |
Alistair Delva | 63594a4 | 2021-03-09 11:04:23 -0800 | [diff] [blame] | 113 | return StatusFromProperty() == SELINUX_ENFORCING; |
Tom Cherry | c317009 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 114 | } |
| 115 | return true; |
| 116 | } |
| 117 | |
Tom Cherry | c317009 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 118 | bool ReadFirstLine(const char* file, std::string* line) { |
| 119 | line->clear(); |
| 120 | |
| 121 | std::string contents; |
| 122 | if (!android::base::ReadFileToString(file, &contents, true /* follow symlinks */)) { |
| 123 | return false; |
| 124 | } |
| 125 | std::istringstream in(contents); |
| 126 | std::getline(in, *line); |
| 127 | return true; |
| 128 | } |
| 129 | |
Inseob Kim | d99d977 | 2021-03-11 17:58:26 +0900 | [diff] [blame] | 130 | Result<std::string> FindPrecompiledSplitPolicy() { |
| 131 | std::string precompiled_sepolicy; |
kaichieh | eef4cd7 | 2017-08-31 22:07:19 +0800 | [diff] [blame] | 132 | // If there is an odm partition, precompiled_sepolicy will be in |
| 133 | // odm/etc/selinux. Otherwise it will be in vendor/etc/selinux. |
| 134 | static constexpr const char vendor_precompiled_sepolicy[] = |
Sandro | 1120f7f | 2022-09-05 15:56:38 +0000 | [diff] [blame] | 135 | "/vendor/etc/selinux/precompiled_sepolicy"; |
kaichieh | eef4cd7 | 2017-08-31 22:07:19 +0800 | [diff] [blame] | 136 | static constexpr const char odm_precompiled_sepolicy[] = |
Sandro | 1120f7f | 2022-09-05 15:56:38 +0000 | [diff] [blame] | 137 | "/odm/etc/selinux/precompiled_sepolicy"; |
kaichieh | eef4cd7 | 2017-08-31 22:07:19 +0800 | [diff] [blame] | 138 | if (access(odm_precompiled_sepolicy, R_OK) == 0) { |
Inseob Kim | d99d977 | 2021-03-11 17:58:26 +0900 | [diff] [blame] | 139 | precompiled_sepolicy = odm_precompiled_sepolicy; |
kaichieh | eef4cd7 | 2017-08-31 22:07:19 +0800 | [diff] [blame] | 140 | } else if (access(vendor_precompiled_sepolicy, R_OK) == 0) { |
Inseob Kim | d99d977 | 2021-03-11 17:58:26 +0900 | [diff] [blame] | 141 | precompiled_sepolicy = vendor_precompiled_sepolicy; |
kaichieh | eef4cd7 | 2017-08-31 22:07:19 +0800 | [diff] [blame] | 142 | } else { |
Inseob Kim | d99d977 | 2021-03-11 17:58:26 +0900 | [diff] [blame] | 143 | return ErrnoError() << "No precompiled sepolicy at " << vendor_precompiled_sepolicy; |
Tom Cherry | c317009 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 144 | } |
kaichieh | eef4cd7 | 2017-08-31 22:07:19 +0800 | [diff] [blame] | 145 | |
Inseob Kim | d99d977 | 2021-03-11 17:58:26 +0900 | [diff] [blame] | 146 | // Use precompiled sepolicy only when all corresponding hashes are equal. |
Inseob Kim | d99d977 | 2021-03-11 17:58:26 +0900 | [diff] [blame] | 147 | std::vector<std::pair<std::string, std::string>> sepolicy_hashes{ |
| 148 | {"/system/etc/selinux/plat_sepolicy_and_mapping.sha256", |
| 149 | precompiled_sepolicy + ".plat_sepolicy_and_mapping.sha256"}, |
Inseob Kim | 28fdb67 | 2021-04-29 19:48:27 +0900 | [diff] [blame] | 150 | {"/system_ext/etc/selinux/system_ext_sepolicy_and_mapping.sha256", |
| 151 | precompiled_sepolicy + ".system_ext_sepolicy_and_mapping.sha256"}, |
| 152 | {"/product/etc/selinux/product_sepolicy_and_mapping.sha256", |
| 153 | precompiled_sepolicy + ".product_sepolicy_and_mapping.sha256"}, |
Inseob Kim | d99d977 | 2021-03-11 17:58:26 +0900 | [diff] [blame] | 154 | }; |
| 155 | |
Inseob Kim | d99d977 | 2021-03-11 17:58:26 +0900 | [diff] [blame] | 156 | for (const auto& [actual_id_path, precompiled_id_path] : sepolicy_hashes) { |
Inseob Kim | 28fdb67 | 2021-04-29 19:48:27 +0900 | [diff] [blame] | 157 | // Both of them should exist or both of them shouldn't exist. |
| 158 | if (access(actual_id_path.c_str(), R_OK) != 0) { |
| 159 | if (access(precompiled_id_path.c_str(), R_OK) == 0) { |
| 160 | return Error() << precompiled_id_path << " exists but " << actual_id_path |
| 161 | << " doesn't"; |
| 162 | } |
| 163 | continue; |
| 164 | } |
| 165 | |
Inseob Kim | d99d977 | 2021-03-11 17:58:26 +0900 | [diff] [blame] | 166 | std::string actual_id; |
| 167 | if (!ReadFirstLine(actual_id_path.c_str(), &actual_id)) { |
| 168 | return ErrnoError() << "Failed to read " << actual_id_path; |
| 169 | } |
| 170 | |
| 171 | std::string precompiled_id; |
| 172 | if (!ReadFirstLine(precompiled_id_path.c_str(), &precompiled_id)) { |
| 173 | return ErrnoError() << "Failed to read " << precompiled_id_path; |
| 174 | } |
| 175 | |
| 176 | if (actual_id.empty() || actual_id != precompiled_id) { |
| 177 | return Error() << actual_id_path << " and " << precompiled_id_path << " differ"; |
| 178 | } |
Tri Vo | c8137f9 | 2019-01-22 18:22:25 -0800 | [diff] [blame] | 179 | } |
Inseob Kim | d99d977 | 2021-03-11 17:58:26 +0900 | [diff] [blame] | 180 | |
| 181 | return precompiled_sepolicy; |
Tom Cherry | c317009 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | bool GetVendorMappingVersion(std::string* plat_vers) { |
| 185 | if (!ReadFirstLine("/vendor/etc/selinux/plat_sepolicy_vers.txt", plat_vers)) { |
| 186 | PLOG(ERROR) << "Failed to read /vendor/etc/selinux/plat_sepolicy_vers.txt"; |
| 187 | return false; |
| 188 | } |
| 189 | if (plat_vers->empty()) { |
| 190 | LOG(ERROR) << "No version present in plat_sepolicy_vers.txt"; |
| 191 | return false; |
| 192 | } |
| 193 | return true; |
| 194 | } |
| 195 | |
| 196 | constexpr const char plat_policy_cil_file[] = "/system/etc/selinux/plat_sepolicy.cil"; |
| 197 | |
| 198 | bool IsSplitPolicyDevice() { |
| 199 | return access(plat_policy_cil_file, R_OK) != -1; |
| 200 | } |
| 201 | |
Yi-Yo Chiang | bb77c54 | 2021-09-23 14:14:16 +0000 | [diff] [blame] | 202 | std::optional<const char*> GetUserdebugPlatformPolicyFile() { |
| 203 | // See if we need to load userdebug_plat_sepolicy.cil instead of plat_sepolicy.cil. |
| 204 | const char* force_debuggable_env = getenv("INIT_FORCE_DEBUGGABLE"); |
| 205 | if (force_debuggable_env && "true"s == force_debuggable_env && AvbHandle::IsDeviceUnlocked()) { |
| 206 | const std::vector<const char*> debug_policy_candidates = { |
| 207 | #if INSTALL_DEBUG_POLICY_TO_SYSTEM_EXT == 1 |
| 208 | "/system_ext/etc/selinux/userdebug_plat_sepolicy.cil", |
| 209 | #endif |
| 210 | kDebugRamdiskSEPolicy, |
| 211 | }; |
| 212 | for (const char* debug_policy : debug_policy_candidates) { |
| 213 | if (access(debug_policy, F_OK) == 0) { |
| 214 | return debug_policy; |
| 215 | } |
| 216 | } |
| 217 | } |
| 218 | return std::nullopt; |
| 219 | } |
| 220 | |
David Anderson | 491e4da | 2020-12-08 00:21:20 -0800 | [diff] [blame] | 221 | struct PolicyFile { |
| 222 | unique_fd fd; |
| 223 | std::string path; |
| 224 | }; |
| 225 | |
| 226 | bool OpenSplitPolicy(PolicyFile* policy_file) { |
Jeff Vander Stoep | 5effda4 | 2021-11-05 09:03:11 +0100 | [diff] [blame] | 227 | // IMPLEMENTATION NOTE: Split policy consists of three or more CIL files: |
Tom Cherry | c317009 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 228 | // * platform -- policy needed due to logic contained in the system image, |
Jeff Vander Stoep | 5effda4 | 2021-11-05 09:03:11 +0100 | [diff] [blame] | 229 | // * vendor -- policy needed due to logic contained in the vendor image, |
Tom Cherry | c317009 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 230 | // * mapping -- mapping policy which helps preserve forward-compatibility of non-platform policy |
| 231 | // with newer versions of platform policy. |
Thiébaud Weksteen | 50f03fd | 2023-09-06 10:52:49 +1000 | [diff] [blame] | 232 | // * (optional) policy needed due to logic on product, system_ext, or odm images. |
Tom Cherry | c317009 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 233 | // secilc is invoked to compile the above three policy files into a single monolithic policy |
| 234 | // file. This file is then loaded into the kernel. |
| 235 | |
Yi-Yo Chiang | bb77c54 | 2021-09-23 14:14:16 +0000 | [diff] [blame] | 236 | const auto userdebug_plat_sepolicy = GetUserdebugPlatformPolicyFile(); |
| 237 | const bool use_userdebug_policy = userdebug_plat_sepolicy.has_value(); |
Bowgo Tsai | 1dacd42 | 2019-03-04 17:53:34 +0800 | [diff] [blame] | 238 | if (use_userdebug_policy) { |
Yi-Yo Chiang | bb77c54 | 2021-09-23 14:14:16 +0000 | [diff] [blame] | 239 | LOG(INFO) << "Using userdebug system sepolicy " << *userdebug_plat_sepolicy; |
Bowgo Tsai | 1dacd42 | 2019-03-04 17:53:34 +0800 | [diff] [blame] | 240 | } |
| 241 | |
Tom Cherry | c317009 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 242 | // Load precompiled policy from vendor image, if a matching policy is found there. The policy |
| 243 | // must match the platform policy on the system image. |
Bowgo Tsai | 1dacd42 | 2019-03-04 17:53:34 +0800 | [diff] [blame] | 244 | // use_userdebug_policy requires compiling sepolicy with userdebug_plat_sepolicy.cil. |
| 245 | // Thus it cannot use the precompiled policy from vendor image. |
Inseob Kim | d99d977 | 2021-03-11 17:58:26 +0900 | [diff] [blame] | 246 | if (!use_userdebug_policy) { |
| 247 | if (auto res = FindPrecompiledSplitPolicy(); res.ok()) { |
| 248 | unique_fd fd(open(res->c_str(), O_RDONLY | O_CLOEXEC | O_BINARY)); |
| 249 | if (fd != -1) { |
| 250 | policy_file->fd = std::move(fd); |
| 251 | policy_file->path = std::move(*res); |
| 252 | return true; |
| 253 | } |
| 254 | } else { |
| 255 | LOG(INFO) << res.error(); |
Tom Cherry | c317009 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 256 | } |
| 257 | } |
| 258 | // No suitable precompiled policy could be loaded |
| 259 | |
| 260 | LOG(INFO) << "Compiling SELinux policy"; |
| 261 | |
Tom Cherry | c317009 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 262 | // We store the output of the compilation on /dev because this is the most convenient tmpfs |
| 263 | // storage mount available this early in the boot sequence. |
| 264 | char compiled_sepolicy[] = "/dev/sepolicy.XXXXXX"; |
| 265 | unique_fd compiled_sepolicy_fd(mkostemp(compiled_sepolicy, O_CLOEXEC)); |
| 266 | if (compiled_sepolicy_fd < 0) { |
| 267 | PLOG(ERROR) << "Failed to create temporary file " << compiled_sepolicy; |
| 268 | return false; |
| 269 | } |
| 270 | |
| 271 | // Determine which mapping file to include |
| 272 | std::string vend_plat_vers; |
| 273 | if (!GetVendorMappingVersion(&vend_plat_vers)) { |
| 274 | return false; |
| 275 | } |
Tri Vo | 503f185 | 2019-01-16 11:57:19 -0800 | [diff] [blame] | 276 | std::string plat_mapping_file("/system/etc/selinux/mapping/" + vend_plat_vers + ".cil"); |
kaichieh | eef4cd7 | 2017-08-31 22:07:19 +0800 | [diff] [blame] | 277 | |
Jeff Vander Stoep | 0ac51cf | 2019-05-02 14:05:18 -0700 | [diff] [blame] | 278 | std::string plat_compat_cil_file("/system/etc/selinux/mapping/" + vend_plat_vers + |
| 279 | ".compat.cil"); |
| 280 | if (access(plat_compat_cil_file.c_str(), F_OK) == -1) { |
| 281 | plat_compat_cil_file.clear(); |
| 282 | } |
| 283 | |
Bowgo Tsai | f016f25 | 2019-08-28 17:56:51 +0800 | [diff] [blame] | 284 | std::string system_ext_policy_cil_file("/system_ext/etc/selinux/system_ext_sepolicy.cil"); |
| 285 | if (access(system_ext_policy_cil_file.c_str(), F_OK) == -1) { |
| 286 | system_ext_policy_cil_file.clear(); |
| 287 | } |
| 288 | |
| 289 | std::string system_ext_mapping_file("/system_ext/etc/selinux/mapping/" + vend_plat_vers + |
| 290 | ".cil"); |
| 291 | if (access(system_ext_mapping_file.c_str(), F_OK) == -1) { |
| 292 | system_ext_mapping_file.clear(); |
| 293 | } |
| 294 | |
Yi-Yo Chiang | 731d247 | 2021-03-23 22:11:13 +0800 | [diff] [blame] | 295 | std::string system_ext_compat_cil_file("/system_ext/etc/selinux/mapping/" + vend_plat_vers + |
| 296 | ".compat.cil"); |
| 297 | if (access(system_ext_compat_cil_file.c_str(), F_OK) == -1) { |
| 298 | system_ext_compat_cil_file.clear(); |
| 299 | } |
| 300 | |
Tri Vo | d3518cf | 2018-12-14 14:25:08 -0800 | [diff] [blame] | 301 | std::string product_policy_cil_file("/product/etc/selinux/product_sepolicy.cil"); |
| 302 | if (access(product_policy_cil_file.c_str(), F_OK) == -1) { |
| 303 | product_policy_cil_file.clear(); |
| 304 | } |
| 305 | |
Tri Vo | 503f185 | 2019-01-16 11:57:19 -0800 | [diff] [blame] | 306 | std::string product_mapping_file("/product/etc/selinux/mapping/" + vend_plat_vers + ".cil"); |
| 307 | if (access(product_mapping_file.c_str(), F_OK) == -1) { |
| 308 | product_mapping_file.clear(); |
| 309 | } |
| 310 | |
kaichieh | eef4cd7 | 2017-08-31 22:07:19 +0800 | [diff] [blame] | 311 | std::string vendor_policy_cil_file("/vendor/etc/selinux/vendor_sepolicy.cil"); |
kaichieh | eef4cd7 | 2017-08-31 22:07:19 +0800 | [diff] [blame] | 312 | if (access(vendor_policy_cil_file.c_str(), F_OK) == -1) { |
Jeff Vander Stoep | 5effda4 | 2021-11-05 09:03:11 +0100 | [diff] [blame] | 313 | LOG(ERROR) << "Missing " << vendor_policy_cil_file; |
| 314 | return false; |
| 315 | } |
| 316 | |
| 317 | std::string plat_pub_versioned_cil_file("/vendor/etc/selinux/plat_pub_versioned.cil"); |
| 318 | if (access(plat_pub_versioned_cil_file.c_str(), F_OK) == -1) { |
Bowgo Tsai | 069ab5b | 2017-10-18 17:03:20 +0800 | [diff] [blame] | 319 | LOG(ERROR) << "Missing " << plat_pub_versioned_cil_file; |
kaichieh | eef4cd7 | 2017-08-31 22:07:19 +0800 | [diff] [blame] | 320 | return false; |
| 321 | } |
| 322 | |
| 323 | // odm_sepolicy.cil is default but optional. |
| 324 | std::string odm_policy_cil_file("/odm/etc/selinux/odm_sepolicy.cil"); |
| 325 | if (access(odm_policy_cil_file.c_str(), F_OK) == -1) { |
| 326 | odm_policy_cil_file.clear(); |
| 327 | } |
Jeff Vander Stoep | 724eda5 | 2019-02-15 12:13:38 -0800 | [diff] [blame] | 328 | const std::string version_as_string = std::to_string(SEPOLICY_VERSION); |
Andreas Huber | c41b838 | 2017-08-18 14:43:52 -0700 | [diff] [blame] | 329 | |
Inseob Kim | 76afb4a | 2024-11-06 17:07:04 +0900 | [diff] [blame] | 330 | std::vector<std::string> genfs_cil_files; |
| 331 | |
Inseob Kim | e2efde3 | 2024-11-20 17:56:20 +0900 | [diff] [blame] | 332 | int vendor_genfs_version = get_genfs_labels_version(); |
Inseob Kim | 76afb4a | 2024-11-06 17:07:04 +0900 | [diff] [blame] | 333 | std::string genfs_cil_file = |
| 334 | std::format("/system/etc/selinux/plat_sepolicy_genfs_{}.cil", vendor_genfs_version); |
| 335 | if (access(genfs_cil_file.c_str(), F_OK) != 0) { |
Inseob Kim | e2efde3 | 2024-11-20 17:56:20 +0900 | [diff] [blame] | 336 | LOG(INFO) << "Missing " << genfs_cil_file << "; skipping"; |
Inseob Kim | 76afb4a | 2024-11-06 17:07:04 +0900 | [diff] [blame] | 337 | genfs_cil_file.clear(); |
Inseob Kim | e2efde3 | 2024-11-20 17:56:20 +0900 | [diff] [blame] | 338 | } else { |
| 339 | LOG(INFO) << "Using " << genfs_cil_file << " for genfs labels"; |
Inseob Kim | 76afb4a | 2024-11-06 17:07:04 +0900 | [diff] [blame] | 340 | } |
| 341 | |
Tom Cherry | c317009 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 342 | // clang-format off |
kaichieh | eef4cd7 | 2017-08-31 22:07:19 +0800 | [diff] [blame] | 343 | std::vector<const char*> compile_args { |
Tom Cherry | c317009 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 344 | "/system/bin/secilc", |
Yi-Yo Chiang | bb77c54 | 2021-09-23 14:14:16 +0000 | [diff] [blame] | 345 | use_userdebug_policy ? *userdebug_plat_sepolicy : plat_policy_cil_file, |
Jeff Vander Stoep | 5e9ba3c | 2017-10-06 17:03:45 -0700 | [diff] [blame] | 346 | "-m", "-M", "true", "-G", "-N", |
Andreas Huber | c41b838 | 2017-08-18 14:43:52 -0700 | [diff] [blame] | 347 | "-c", version_as_string.c_str(), |
Tri Vo | 503f185 | 2019-01-16 11:57:19 -0800 | [diff] [blame] | 348 | plat_mapping_file.c_str(), |
Tom Cherry | c317009 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 349 | "-o", compiled_sepolicy, |
| 350 | // We don't care about file_contexts output by the compiler |
| 351 | "-f", "/sys/fs/selinux/null", // /dev/null is not yet available |
kaichieh | eef4cd7 | 2017-08-31 22:07:19 +0800 | [diff] [blame] | 352 | }; |
Tom Cherry | c317009 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 353 | // clang-format on |
| 354 | |
Jeff Vander Stoep | 0ac51cf | 2019-05-02 14:05:18 -0700 | [diff] [blame] | 355 | if (!plat_compat_cil_file.empty()) { |
| 356 | compile_args.push_back(plat_compat_cil_file.c_str()); |
| 357 | } |
Bowgo Tsai | f016f25 | 2019-08-28 17:56:51 +0800 | [diff] [blame] | 358 | if (!system_ext_policy_cil_file.empty()) { |
| 359 | compile_args.push_back(system_ext_policy_cil_file.c_str()); |
| 360 | } |
| 361 | if (!system_ext_mapping_file.empty()) { |
| 362 | compile_args.push_back(system_ext_mapping_file.c_str()); |
| 363 | } |
Yi-Yo Chiang | 731d247 | 2021-03-23 22:11:13 +0800 | [diff] [blame] | 364 | if (!system_ext_compat_cil_file.empty()) { |
| 365 | compile_args.push_back(system_ext_compat_cil_file.c_str()); |
| 366 | } |
Tri Vo | d3518cf | 2018-12-14 14:25:08 -0800 | [diff] [blame] | 367 | if (!product_policy_cil_file.empty()) { |
| 368 | compile_args.push_back(product_policy_cil_file.c_str()); |
| 369 | } |
Tri Vo | 503f185 | 2019-01-16 11:57:19 -0800 | [diff] [blame] | 370 | if (!product_mapping_file.empty()) { |
| 371 | compile_args.push_back(product_mapping_file.c_str()); |
| 372 | } |
Bowgo Tsai | 069ab5b | 2017-10-18 17:03:20 +0800 | [diff] [blame] | 373 | if (!plat_pub_versioned_cil_file.empty()) { |
| 374 | compile_args.push_back(plat_pub_versioned_cil_file.c_str()); |
kaichieh | eef4cd7 | 2017-08-31 22:07:19 +0800 | [diff] [blame] | 375 | } |
| 376 | if (!vendor_policy_cil_file.empty()) { |
| 377 | compile_args.push_back(vendor_policy_cil_file.c_str()); |
| 378 | } |
| 379 | if (!odm_policy_cil_file.empty()) { |
| 380 | compile_args.push_back(odm_policy_cil_file.c_str()); |
| 381 | } |
Inseob Kim | 76afb4a | 2024-11-06 17:07:04 +0900 | [diff] [blame] | 382 | if (!genfs_cil_file.empty()) { |
| 383 | compile_args.push_back(genfs_cil_file.c_str()); |
| 384 | } |
kaichieh | eef4cd7 | 2017-08-31 22:07:19 +0800 | [diff] [blame] | 385 | compile_args.push_back(nullptr); |
| 386 | |
| 387 | if (!ForkExecveAndWaitForCompletion(compile_args[0], (char**)compile_args.data())) { |
Tom Cherry | c317009 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 388 | unlink(compiled_sepolicy); |
| 389 | return false; |
| 390 | } |
| 391 | unlink(compiled_sepolicy); |
| 392 | |
David Anderson | 491e4da | 2020-12-08 00:21:20 -0800 | [diff] [blame] | 393 | policy_file->fd = std::move(compiled_sepolicy_fd); |
| 394 | policy_file->path = compiled_sepolicy; |
Tom Cherry | c317009 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 395 | return true; |
| 396 | } |
| 397 | |
David Anderson | 491e4da | 2020-12-08 00:21:20 -0800 | [diff] [blame] | 398 | bool OpenMonolithicPolicy(PolicyFile* policy_file) { |
| 399 | static constexpr char kSepolicyFile[] = "/sepolicy"; |
| 400 | |
Nikita Ioffe | a66adf4 | 2023-06-26 14:55:31 +0100 | [diff] [blame] | 401 | LOG(INFO) << "Opening SELinux policy from monolithic file " << kSepolicyFile; |
| 402 | policy_file->fd.reset(open(kSepolicyFile, O_RDONLY | O_CLOEXEC | O_NOFOLLOW)); |
David Anderson | 491e4da | 2020-12-08 00:21:20 -0800 | [diff] [blame] | 403 | if (policy_file->fd < 0) { |
| 404 | PLOG(ERROR) << "Failed to open monolithic SELinux policy"; |
Tom Cherry | c317009 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 405 | return false; |
| 406 | } |
Nikita Ioffe | a66adf4 | 2023-06-26 14:55:31 +0100 | [diff] [blame] | 407 | policy_file->path = kSepolicyFile; |
Tom Cherry | c317009 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 408 | return true; |
| 409 | } |
| 410 | |
David Anderson | 491e4da | 2020-12-08 00:21:20 -0800 | [diff] [blame] | 411 | void ReadPolicy(std::string* policy) { |
| 412 | PolicyFile policy_file; |
Tom Cherry | c317009 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 413 | |
David Anderson | 491e4da | 2020-12-08 00:21:20 -0800 | [diff] [blame] | 414 | bool ok = IsSplitPolicyDevice() ? OpenSplitPolicy(&policy_file) |
| 415 | : OpenMonolithicPolicy(&policy_file); |
| 416 | if (!ok) { |
| 417 | LOG(FATAL) << "Unable to open SELinux policy"; |
Tom Cherry | c317009 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 418 | } |
| 419 | |
David Anderson | 491e4da | 2020-12-08 00:21:20 -0800 | [diff] [blame] | 420 | if (!android::base::ReadFdToString(policy_file.fd, policy)) { |
| 421 | PLOG(FATAL) << "Failed to read policy file: " << policy_file.path; |
| 422 | } |
| 423 | } |
| 424 | |
| 425 | void SelinuxSetEnforcement() { |
Tom Cherry | c317009 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 426 | bool kernel_enforcing = (security_getenforce() == 1); |
| 427 | bool is_enforcing = IsEnforcing(); |
| 428 | if (kernel_enforcing != is_enforcing) { |
| 429 | if (security_setenforce(is_enforcing)) { |
Paul Lawrence | b2c2d69 | 2019-08-30 11:11:44 -0700 | [diff] [blame] | 430 | PLOG(FATAL) << "security_setenforce(" << (is_enforcing ? "true" : "false") |
| 431 | << ") failed"; |
Tom Cherry | c317009 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 432 | } |
| 433 | } |
Tom Cherry | c317009 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 434 | } |
| 435 | |
Tom Cherry | 8180b48 | 2019-08-26 13:57:51 -0700 | [diff] [blame] | 436 | constexpr size_t kKlogMessageSize = 1024; |
| 437 | |
Thiébaud Weksteen | f03dde8 | 2023-04-03 16:53:12 +1000 | [diff] [blame] | 438 | void SelinuxAvcLog(char* buf) { |
Tom Cherry | 8180b48 | 2019-08-26 13:57:51 -0700 | [diff] [blame] | 439 | struct NetlinkMessage { |
| 440 | nlmsghdr hdr; |
| 441 | char buf[kKlogMessageSize]; |
| 442 | } request = {}; |
| 443 | |
| 444 | request.hdr.nlmsg_flags = NLM_F_REQUEST; |
| 445 | request.hdr.nlmsg_type = AUDIT_USER_AVC; |
| 446 | request.hdr.nlmsg_len = sizeof(request); |
| 447 | strlcpy(request.buf, buf, sizeof(request.buf)); |
| 448 | |
| 449 | auto fd = unique_fd{socket(PF_NETLINK, SOCK_RAW | SOCK_CLOEXEC, NETLINK_AUDIT)}; |
| 450 | if (!fd.ok()) { |
| 451 | return; |
| 452 | } |
| 453 | |
Bart Van Assche | aee2ec8 | 2022-12-02 18:48:15 -0800 | [diff] [blame] | 454 | TEMP_FAILURE_RETRY(send(fd.get(), &request, sizeof(request), 0)); |
Tom Cherry | 8180b48 | 2019-08-26 13:57:51 -0700 | [diff] [blame] | 455 | } |
| 456 | |
Eric Biggers | 141c6a8 | 2023-08-11 21:05:14 +0000 | [diff] [blame] | 457 | int RestoreconIfExists(const char* path, unsigned int flags) { |
| 458 | if (access(path, F_OK) != 0 && errno == ENOENT) { |
| 459 | // Avoid error message for path that is expected to not always exist. |
| 460 | return 0; |
| 461 | } |
| 462 | return selinux_android_restorecon(path, flags); |
| 463 | } |
| 464 | |
Tom Cherry | 7bfea3d | 2018-11-06 14:12:05 -0800 | [diff] [blame] | 465 | } // namespace |
| 466 | |
Tom Cherry | c317009 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 467 | void SelinuxRestoreContext() { |
| 468 | LOG(INFO) << "Running restorecon..."; |
| 469 | selinux_android_restorecon("/dev", 0); |
Inseob Kim | 89d6913 | 2022-03-22 21:51:07 +0900 | [diff] [blame] | 470 | selinux_android_restorecon("/dev/console", 0); |
Tom Cherry | c317009 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 471 | selinux_android_restorecon("/dev/kmsg", 0); |
| 472 | if constexpr (WORLD_WRITABLE_KMSG) { |
| 473 | selinux_android_restorecon("/dev/kmsg_debug", 0); |
| 474 | } |
Tom Cherry | 81ae075 | 2018-07-30 16:23:49 -0700 | [diff] [blame] | 475 | selinux_android_restorecon("/dev/null", 0); |
| 476 | selinux_android_restorecon("/dev/ptmx", 0); |
Tom Cherry | c317009 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 477 | selinux_android_restorecon("/dev/socket", 0); |
| 478 | selinux_android_restorecon("/dev/random", 0); |
| 479 | selinux_android_restorecon("/dev/urandom", 0); |
| 480 | selinux_android_restorecon("/dev/__properties__", 0); |
| 481 | |
Tom Cherry | c317009 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 482 | selinux_android_restorecon("/dev/block", SELINUX_ANDROID_RESTORECON_RECURSE); |
David Anderson | 1ff7581 | 2020-11-13 00:31:47 -0800 | [diff] [blame] | 483 | selinux_android_restorecon("/dev/dm-user", SELINUX_ANDROID_RESTORECON_RECURSE); |
Tom Cherry | c317009 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 484 | selinux_android_restorecon("/dev/device-mapper", 0); |
Jiyong Park | 4ba548d | 2019-02-22 16:04:35 +0900 | [diff] [blame] | 485 | |
| 486 | selinux_android_restorecon("/apex", 0); |
Jooyung Han | 566c652 | 2023-08-09 07:05:31 +0000 | [diff] [blame] | 487 | selinux_android_restorecon("/bootstrap-apex", 0); |
Kiyoung Kim | 99df54b | 2019-11-22 16:14:10 +0900 | [diff] [blame] | 488 | selinux_android_restorecon("/linkerconfig", 0); |
Bowgo Tsai | 196cc58 | 2020-01-20 18:03:58 +0800 | [diff] [blame] | 489 | |
David Anderson | c991f34 | 2020-02-21 17:11:07 -0800 | [diff] [blame] | 490 | // adb remount, snapshot-based updates, and DSUs all create files during |
| 491 | // first-stage init. |
Eric Biggers | 141c6a8 | 2023-08-11 21:05:14 +0000 | [diff] [blame] | 492 | RestoreconIfExists(SnapshotManager::GetGlobalRollbackIndicatorPath().c_str(), 0); |
| 493 | RestoreconIfExists("/metadata/gsi", |
| 494 | SELINUX_ANDROID_RESTORECON_RECURSE | SELINUX_ANDROID_RESTORECON_SKIP_SEHASH); |
Tom Cherry | c317009 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 495 | } |
| 496 | |
Tom Cherry | 74069d1 | 2018-07-20 15:26:25 -0700 | [diff] [blame] | 497 | int SelinuxKlogCallback(int type, const char* fmt, ...) { |
| 498 | android::base::LogSeverity severity = android::base::ERROR; |
| 499 | if (type == SELINUX_WARNING) { |
| 500 | severity = android::base::WARNING; |
| 501 | } else if (type == SELINUX_INFO) { |
| 502 | severity = android::base::INFO; |
| 503 | } |
Tom Cherry | 8180b48 | 2019-08-26 13:57:51 -0700 | [diff] [blame] | 504 | char buf[kKlogMessageSize]; |
Tom Cherry | 74069d1 | 2018-07-20 15:26:25 -0700 | [diff] [blame] | 505 | va_list ap; |
| 506 | va_start(ap, fmt); |
Tom Cherry | 8180b48 | 2019-08-26 13:57:51 -0700 | [diff] [blame] | 507 | int length_written = vsnprintf(buf, sizeof(buf), fmt, ap); |
Tom Cherry | 74069d1 | 2018-07-20 15:26:25 -0700 | [diff] [blame] | 508 | va_end(ap); |
Tom Cherry | 8180b48 | 2019-08-26 13:57:51 -0700 | [diff] [blame] | 509 | if (length_written <= 0) { |
| 510 | return 0; |
| 511 | } |
Thiébaud Weksteen | f03dde8 | 2023-04-03 16:53:12 +1000 | [diff] [blame] | 512 | |
| 513 | // libselinux log messages usually contain a new line character, while |
| 514 | // Android LOG() does not expect it. Remove it to avoid empty lines in |
| 515 | // the log buffers. |
| 516 | size_t str_len = strlen(buf); |
| 517 | if (buf[str_len - 1] == '\n') { |
| 518 | buf[str_len - 1] = '\0'; |
| 519 | } |
| 520 | |
Tom Cherry | 8180b48 | 2019-08-26 13:57:51 -0700 | [diff] [blame] | 521 | if (type == SELINUX_AVC) { |
Thiébaud Weksteen | f03dde8 | 2023-04-03 16:53:12 +1000 | [diff] [blame] | 522 | SelinuxAvcLog(buf); |
Tom Cherry | 8180b48 | 2019-08-26 13:57:51 -0700 | [diff] [blame] | 523 | } else { |
| 524 | android::base::KernelLogger(android::base::MAIN, severity, "selinux", nullptr, 0, buf); |
| 525 | } |
Tom Cherry | 74069d1 | 2018-07-20 15:26:25 -0700 | [diff] [blame] | 526 | return 0; |
| 527 | } |
| 528 | |
Tom Cherry | c317009 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 529 | void SelinuxSetupKernelLogging() { |
| 530 | selinux_callback cb; |
Tom Cherry | 74069d1 | 2018-07-20 15:26:25 -0700 | [diff] [blame] | 531 | cb.func_log = SelinuxKlogCallback; |
Tom Cherry | c317009 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 532 | selinux_set_callback(SELINUX_CB_LOG, cb); |
| 533 | } |
| 534 | |
Tom Cherry | 40acb37 | 2018-08-01 13:41:12 -0700 | [diff] [blame] | 535 | int SelinuxGetVendorAndroidVersion() { |
Nikita Ioffe | a66adf4 | 2023-06-26 14:55:31 +0100 | [diff] [blame] | 536 | if (IsMicrodroid()) { |
| 537 | // As of now Microdroid doesn't have any vendor code. |
| 538 | return __ANDROID_API_FUTURE__; |
| 539 | } |
Tom Cherry | c5cf85d | 2019-07-31 13:59:15 -0700 | [diff] [blame] | 540 | static int vendor_android_version = [] { |
| 541 | if (!IsSplitPolicyDevice()) { |
| 542 | // If this device does not split sepolicy files, it's not a Treble device and therefore, |
| 543 | // we assume it's always on the latest platform. |
| 544 | return __ANDROID_API_FUTURE__; |
| 545 | } |
Logan Chien | 837b2a4 | 2018-05-03 14:33:52 +0800 | [diff] [blame] | 546 | |
Tom Cherry | c5cf85d | 2019-07-31 13:59:15 -0700 | [diff] [blame] | 547 | std::string version; |
| 548 | if (!GetVendorMappingVersion(&version)) { |
| 549 | LOG(FATAL) << "Could not read vendor SELinux version"; |
| 550 | } |
Logan Chien | 837b2a4 | 2018-05-03 14:33:52 +0800 | [diff] [blame] | 551 | |
Tom Cherry | c5cf85d | 2019-07-31 13:59:15 -0700 | [diff] [blame] | 552 | int major_version; |
| 553 | std::string major_version_str(version, 0, version.find('.')); |
| 554 | if (!ParseInt(major_version_str, &major_version)) { |
| 555 | PLOG(FATAL) << "Failed to parse the vendor sepolicy major version " |
| 556 | << major_version_str; |
| 557 | } |
Logan Chien | 837b2a4 | 2018-05-03 14:33:52 +0800 | [diff] [blame] | 558 | |
Tom Cherry | c5cf85d | 2019-07-31 13:59:15 -0700 | [diff] [blame] | 559 | return major_version; |
| 560 | }(); |
| 561 | return vendor_android_version; |
Logan Chien | 837b2a4 | 2018-05-03 14:33:52 +0800 | [diff] [blame] | 562 | } |
| 563 | |
David Anderson | d0ce530 | 2020-03-20 21:47:10 -0700 | [diff] [blame] | 564 | // This is for R system.img/system_ext.img to work on old vendor.img as system_ext.img |
| 565 | // is introduced in R. We mount system_ext in second stage init because the first-stage |
| 566 | // init in boot.img won't be updated in the system-only OTA scenario. |
| 567 | void MountMissingSystemPartitions() { |
| 568 | android::fs_mgr::Fstab fstab; |
| 569 | if (!ReadDefaultFstab(&fstab)) { |
| 570 | LOG(ERROR) << "Could not read default fstab"; |
| 571 | } |
| 572 | |
| 573 | android::fs_mgr::Fstab mounts; |
| 574 | if (!ReadFstabFromFile("/proc/mounts", &mounts)) { |
| 575 | LOG(ERROR) << "Could not read /proc/mounts"; |
| 576 | } |
| 577 | |
| 578 | static const std::vector<std::string> kPartitionNames = {"system_ext", "product"}; |
| 579 | |
| 580 | android::fs_mgr::Fstab extra_fstab; |
| 581 | for (const auto& name : kPartitionNames) { |
| 582 | if (GetEntryForMountPoint(&mounts, "/"s + name)) { |
| 583 | // The partition is already mounted. |
| 584 | continue; |
| 585 | } |
| 586 | |
Lianjun Huang | ccd094c | 2023-02-17 20:22:37 +0800 | [diff] [blame] | 587 | auto system_entries = GetEntriesForMountPoint(&fstab, "/system"); |
| 588 | for (auto& system_entry : system_entries) { |
| 589 | if (!system_entry) { |
| 590 | LOG(ERROR) << "Could not find mount entry for /system"; |
| 591 | break; |
| 592 | } |
| 593 | if (!system_entry->fs_mgr_flags.logical) { |
| 594 | LOG(INFO) << "Skipping mount of " << name << ", system is not dynamic."; |
| 595 | break; |
| 596 | } |
David Anderson | d0ce530 | 2020-03-20 21:47:10 -0700 | [diff] [blame] | 597 | |
Lianjun Huang | ccd094c | 2023-02-17 20:22:37 +0800 | [diff] [blame] | 598 | auto entry = *system_entry; |
| 599 | auto partition_name = name + fs_mgr_get_slot_suffix(); |
| 600 | auto replace_name = "system"s + fs_mgr_get_slot_suffix(); |
David Anderson | d0ce530 | 2020-03-20 21:47:10 -0700 | [diff] [blame] | 601 | |
Lianjun Huang | ccd094c | 2023-02-17 20:22:37 +0800 | [diff] [blame] | 602 | entry.mount_point = "/"s + name; |
| 603 | entry.blk_device = |
David Anderson | d0ce530 | 2020-03-20 21:47:10 -0700 | [diff] [blame] | 604 | android::base::StringReplace(entry.blk_device, replace_name, partition_name, false); |
Lianjun Huang | ccd094c | 2023-02-17 20:22:37 +0800 | [diff] [blame] | 605 | if (!fs_mgr_update_logical_partition(&entry)) { |
| 606 | LOG(ERROR) << "Could not update logical partition"; |
| 607 | continue; |
| 608 | } |
David Anderson | d0ce530 | 2020-03-20 21:47:10 -0700 | [diff] [blame] | 609 | |
Lianjun Huang | ccd094c | 2023-02-17 20:22:37 +0800 | [diff] [blame] | 610 | extra_fstab.emplace_back(std::move(entry)); |
| 611 | } |
David Anderson | d0ce530 | 2020-03-20 21:47:10 -0700 | [diff] [blame] | 612 | } |
| 613 | |
Yi-Yo Chiang | 2057901 | 2021-04-01 20:14:54 +0800 | [diff] [blame] | 614 | SkipMountingPartitions(&extra_fstab, true /* verbose */); |
David Anderson | d0ce530 | 2020-03-20 21:47:10 -0700 | [diff] [blame] | 615 | if (extra_fstab.empty()) { |
| 616 | return; |
| 617 | } |
| 618 | |
| 619 | BlockDevInitializer block_dev_init; |
| 620 | for (auto& entry : extra_fstab) { |
| 621 | if (access(entry.blk_device.c_str(), F_OK) != 0) { |
| 622 | auto block_dev = android::base::Basename(entry.blk_device); |
| 623 | if (!block_dev_init.InitDmDevice(block_dev)) { |
| 624 | LOG(ERROR) << "Failed to find device-mapper node: " << block_dev; |
| 625 | continue; |
| 626 | } |
| 627 | } |
| 628 | if (fs_mgr_do_mount_one(entry)) { |
| 629 | LOG(ERROR) << "Could not mount " << entry.mount_point; |
| 630 | } |
| 631 | } |
| 632 | } |
| 633 | |
David Anderson | 491e4da | 2020-12-08 00:21:20 -0800 | [diff] [blame] | 634 | static void LoadSelinuxPolicy(std::string& policy) { |
| 635 | LOG(INFO) << "Loading SELinux policy"; |
| 636 | |
| 637 | set_selinuxmnt("/sys/fs/selinux"); |
| 638 | if (security_load_policy(policy.data(), policy.size()) < 0) { |
| 639 | PLOG(FATAL) << "SELinux: Could not load policy"; |
| 640 | } |
| 641 | } |
| 642 | |
Nikita Ioffe | a66adf4 | 2023-06-26 14:55:31 +0100 | [diff] [blame] | 643 | // Encapsulates steps to load SELinux policy in Microdroid. |
| 644 | // So far the process is very straightforward - just load the precompiled policy from /system. |
| 645 | void LoadSelinuxPolicyMicrodroid() { |
| 646 | constexpr const char kMicrodroidPrecompiledSepolicy[] = |
| 647 | "/system/etc/selinux/microdroid_precompiled_sepolicy"; |
| 648 | |
| 649 | LOG(INFO) << "Opening SELinux policy from " << kMicrodroidPrecompiledSepolicy; |
| 650 | unique_fd policy_fd(open(kMicrodroidPrecompiledSepolicy, O_RDONLY | O_CLOEXEC | O_NOFOLLOW)); |
| 651 | if (policy_fd < 0) { |
| 652 | PLOG(FATAL) << "Failed to open " << kMicrodroidPrecompiledSepolicy; |
| 653 | } |
| 654 | |
| 655 | std::string policy; |
| 656 | if (!android::base::ReadFdToString(policy_fd, &policy)) { |
| 657 | PLOG(FATAL) << "Failed to read policy file: " << kMicrodroidPrecompiledSepolicy; |
| 658 | } |
| 659 | |
| 660 | LoadSelinuxPolicy(policy); |
| 661 | } |
| 662 | |
David Anderson | 491e4da | 2020-12-08 00:21:20 -0800 | [diff] [blame] | 663 | // The SELinux setup process is carefully orchestrated around snapuserd. Policy |
| 664 | // must be loaded off dynamic partitions, and during an OTA, those partitions |
| 665 | // cannot be read without snapuserd. But, with kernel-privileged snapuserd |
| 666 | // running, loading the policy will immediately trigger audits. |
| 667 | // |
| 668 | // We use a five-step process to address this: |
| 669 | // (1) Read the policy into a string, with snapuserd running. |
| 670 | // (2) Rewrite the snapshot device-mapper tables, to generate new dm-user |
| 671 | // devices and to flush I/O. |
| 672 | // (3) Kill snapuserd, which no longer has any dm-user devices to attach to. |
| 673 | // (4) Load the sepolicy and issue critical restorecons in /dev, carefully |
| 674 | // avoiding anything that would read from /system. |
| 675 | // (5) Re-launch snapuserd and attach it to the dm-user devices from step (2). |
| 676 | // |
| 677 | // After this sequence, it is safe to enable enforcing mode and continue booting. |
Nikita Ioffe | a66adf4 | 2023-06-26 14:55:31 +0100 | [diff] [blame] | 678 | void LoadSelinuxPolicyAndroid() { |
David Anderson | d0ce530 | 2020-03-20 21:47:10 -0700 | [diff] [blame] | 679 | MountMissingSystemPartitions(); |
| 680 | |
David Anderson | 491e4da | 2020-12-08 00:21:20 -0800 | [diff] [blame] | 681 | LOG(INFO) << "Opening SELinux policy"; |
| 682 | |
| 683 | // Read the policy before potentially killing snapuserd. |
| 684 | std::string policy; |
| 685 | ReadPolicy(&policy); |
| 686 | |
| 687 | auto snapuserd_helper = SnapuserdSelinuxHelper::CreateIfNeeded(); |
| 688 | if (snapuserd_helper) { |
Nikita Ioffe | a66adf4 | 2023-06-26 14:55:31 +0100 | [diff] [blame] | 689 | // Kill the old snapused to avoid audit messages. After this we cannot read from /system |
| 690 | // (or other dynamic partitions) until we call FinishTransition(). |
David Anderson | 491e4da | 2020-12-08 00:21:20 -0800 | [diff] [blame] | 691 | snapuserd_helper->StartTransition(); |
| 692 | } |
| 693 | |
| 694 | LoadSelinuxPolicy(policy); |
| 695 | |
| 696 | if (snapuserd_helper) { |
| 697 | // Before enforcing, finish the pending snapuserd transition. |
| 698 | snapuserd_helper->FinishTransition(); |
| 699 | snapuserd_helper = nullptr; |
| 700 | } |
Nikita Ioffe | a66adf4 | 2023-06-26 14:55:31 +0100 | [diff] [blame] | 701 | } |
| 702 | |
Paul Lawrence | 6bb5289 | 2025-01-29 19:43:48 -0800 | [diff] [blame] | 703 | #ifdef ALLOW_REMOUNT_OVERLAYS |
| 704 | void SetupOverlays() { |
| 705 | if (android::fs_mgr::use_override_creds) return; |
| 706 | |
| 707 | bool has_overlays = false; |
| 708 | std::string contents; |
| 709 | auto result = android::base::ReadFileToString("/proc/mounts", &contents, true); |
| 710 | |
| 711 | auto lines = android::base::Split(contents, "\n"); |
| 712 | for (auto const& line : lines) |
| 713 | if (android::base::StartsWith(line, "overlay")) { |
| 714 | has_overlays = true; |
| 715 | break; |
| 716 | } |
| 717 | |
| 718 | if (!has_overlays) return; |
| 719 | |
| 720 | // After adb remount, we mount all r/o volumes with overlayfs to allow writing. |
| 721 | // However, since overlayfs performs its file operations in the context of the |
| 722 | // mounting process, this will not work as is - init is in the kernel domain in |
| 723 | // first stage, which has very limited permissions. |
| 724 | |
| 725 | // In order to fix this, we need to unmount remount all these volumes from a process |
| 726 | // with sufficient privileges to be able to perform these operations. The |
| 727 | // overlay_remounter domain has those privileges on debuggable devices. |
| 728 | // We will call overlay_remounter which will do the unmounts/mounts. |
| 729 | // But for that to work, the volumes must not be busy, so we need to copy |
| 730 | // overlay_remounter from system to a ramdisk and run it from there. |
| 731 | |
| 732 | const char* kOverlayRemounter = "overlay_remounter"; |
| 733 | auto or_src = std::filesystem::path("/system/xbin/") / kOverlayRemounter; |
| 734 | auto or_dest = std::filesystem::path(kSecondStageRes) / kOverlayRemounter; |
| 735 | std::error_code ec; |
| 736 | std::filesystem::copy(or_src, or_dest, ec); |
| 737 | if (ec) { |
| 738 | LOG(FATAL) << "Failed to copy " << or_src << " to " << or_dest << " " << ec.message(); |
| 739 | } |
| 740 | |
| 741 | if (selinux_android_restorecon(or_dest.c_str(), 0) == -1) { |
| 742 | PLOG(FATAL) << "restorecon of " << or_dest << " failed"; |
| 743 | } |
| 744 | auto dest = unique_fd(open(or_dest.c_str(), O_RDONLY | O_CLOEXEC)); |
| 745 | if (dest.get() == -1) { |
| 746 | PLOG(FATAL) << "Failed to reopen " << or_dest; |
| 747 | } |
| 748 | if (unlink(or_dest.c_str()) == -1) { |
| 749 | PLOG(FATAL) << "Failed to unlink " << or_dest; |
| 750 | } |
| 751 | const char* args[] = {or_dest.c_str(), nullptr}; |
Paul Lawrence | 7bcc189 | 2025-02-21 06:31:48 -0800 | [diff] [blame] | 752 | fexecve(dest.get(), const_cast<char**>(args), environ); |
Paul Lawrence | 6bb5289 | 2025-01-29 19:43:48 -0800 | [diff] [blame] | 753 | |
| 754 | // execv() only returns if an error happened, in which case we |
| 755 | // panic and never return from this function. |
| 756 | PLOG(FATAL) << "execv(\"" << or_dest << "\") failed"; |
| 757 | } |
| 758 | #else |
| 759 | void SetupOverlays() {} |
| 760 | #endif |
| 761 | |
Nikita Ioffe | a66adf4 | 2023-06-26 14:55:31 +0100 | [diff] [blame] | 762 | int SetupSelinux(char** argv) { |
| 763 | SetStdioToDevNull(argv); |
| 764 | InitKernelLogging(argv); |
| 765 | |
| 766 | if (REBOOT_BOOTLOADER_ON_PANIC) { |
| 767 | InstallRebootSignalHandlers(); |
| 768 | } |
| 769 | |
| 770 | boot_clock::time_point start_time = boot_clock::now(); |
| 771 | |
| 772 | SelinuxSetupKernelLogging(); |
| 773 | |
| 774 | // TODO(b/287206497): refactor into different headers to only include what we need. |
| 775 | if (IsMicrodroid()) { |
| 776 | LoadSelinuxPolicyMicrodroid(); |
| 777 | } else { |
| 778 | LoadSelinuxPolicyAndroid(); |
| 779 | } |
Jeffrey Vander Stoep | baeece6 | 2022-02-08 12:42:33 +0000 | [diff] [blame] | 780 | |
David Anderson | 491e4da | 2020-12-08 00:21:20 -0800 | [diff] [blame] | 781 | SelinuxSetEnforcement(); |
Tom Cherry | 7bfea3d | 2018-11-06 14:12:05 -0800 | [diff] [blame] | 782 | |
Nikita Ioffe | feb7e0e | 2024-03-28 00:32:36 +0000 | [diff] [blame] | 783 | if (IsMicrodroid() && android::virtualization::IsOpenDiceChangesFlagEnabled()) { |
| 784 | // We run restorecon of /microdroid_resources while we are still in kernel context to avoid |
| 785 | // granting init `tmpfs:file relabelfrom` capability. |
| 786 | const int flags = SELINUX_ANDROID_RESTORECON_RECURSE; |
| 787 | if (selinux_android_restorecon("/microdroid_resources", flags) == -1) { |
| 788 | PLOG(FATAL) << "restorecon of /microdroid_resources failed"; |
| 789 | } |
| 790 | } |
| 791 | |
Tom Cherry | 7bfea3d | 2018-11-06 14:12:05 -0800 | [diff] [blame] | 792 | // We're in the kernel domain and want to transition to the init domain. File systems that |
| 793 | // store SELabels in their xattrs, such as ext4 do not need an explicit restorecon here, |
| 794 | // but other file systems do. In particular, this is needed for ramdisks such as the |
| 795 | // recovery image for A/B devices. |
| 796 | if (selinux_android_restorecon("/system/bin/init", 0) == -1) { |
| 797 | PLOG(FATAL) << "restorecon failed of /system/bin/init failed"; |
| 798 | } |
| 799 | |
Mark Salyzyn | 44505ec | 2019-05-08 12:44:50 -0700 | [diff] [blame] | 800 | setenv(kEnvSelinuxStartedAt, std::to_string(start_time.time_since_epoch().count()).c_str(), 1); |
Mark Salyzyn | 10377df | 2019-03-27 08:10:41 -0700 | [diff] [blame] | 801 | |
Paul Lawrence | 6bb5289 | 2025-01-29 19:43:48 -0800 | [diff] [blame] | 802 | // SetupOverlays does not return if overlays exist, instead it execs overlay_remounter |
| 803 | // which then execs second stage init |
| 804 | SetupOverlays(); |
| 805 | |
Tom Cherry | 7bfea3d | 2018-11-06 14:12:05 -0800 | [diff] [blame] | 806 | const char* path = "/system/bin/init"; |
| 807 | const char* args[] = {path, "second_stage", nullptr}; |
| 808 | execv(path, const_cast<char**>(args)); |
| 809 | |
| 810 | // execv() only returns if an error happened, in which case we |
| 811 | // panic and never return from this function. |
| 812 | PLOG(FATAL) << "execv(\"" << path << "\") failed"; |
| 813 | |
| 814 | return 1; |
| 815 | } |
| 816 | |
Tom Cherry | c317009 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 817 | } // namespace init |
| 818 | } // namespace android |