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 | |
| 29 | // The split policy is for supporting treble devices. It splits the SEPolicy across files on |
Jeff Vander Stoep | 5effda4 | 2021-11-05 09:03:11 +0100 | [diff] [blame^] | 30 | // /system/etc/selinux (the 'plat' portion of the policy) and /vendor/etc/selinux (the 'vendor' |
Tom Cherry | c317009 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 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. |
| 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 |
Bowgo Tsai | f016f25 | 2019-08-28 17:56:51 +0800 | [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 |
| 43 | // precompiled_sepolicy directly if and only if the hashes along with the precompiled SEPolicy on |
| 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 |
David Anderson | 491e4da | 2020-12-08 00:21:20 -0800 | [diff] [blame] | 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> |
Bowgo Tsai | 1dacd42 | 2019-03-04 17:53:34 +0800 | [diff] [blame] | 69 | #include <fs_avb/fs_avb.h> |
David Anderson | d0ce530 | 2020-03-20 21:47:10 -0700 | [diff] [blame] | 70 | #include <fs_mgr.h> |
Bowgo Tsai | 196cc58 | 2020-01-20 18:03:58 +0800 | [diff] [blame] | 71 | #include <libgsi/libgsi.h> |
Yifan Hong | d91998f | 2020-02-20 17:54:57 -0800 | [diff] [blame] | 72 | #include <libsnapshot/snapshot.h> |
Tom Cherry | c317009 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 73 | #include <selinux/android.h> |
| 74 | |
David Anderson | d0ce530 | 2020-03-20 21:47:10 -0700 | [diff] [blame] | 75 | #include "block_dev_initializer.h" |
Bowgo Tsai | 30afda7 | 2019-04-11 23:57:24 +0800 | [diff] [blame] | 76 | #include "debug_ramdisk.h" |
Tom Cherry | 7bfea3d | 2018-11-06 14:12:05 -0800 | [diff] [blame] | 77 | #include "reboot_utils.h" |
David Anderson | 491e4da | 2020-12-08 00:21:20 -0800 | [diff] [blame] | 78 | #include "snapuserd_transition.h" |
Tom Cherry | c317009 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 79 | #include "util.h" |
| 80 | |
Bowgo Tsai | 1dacd42 | 2019-03-04 17:53:34 +0800 | [diff] [blame] | 81 | using namespace std::string_literals; |
| 82 | |
Logan Chien | 837b2a4 | 2018-05-03 14:33:52 +0800 | [diff] [blame] | 83 | using android::base::ParseInt; |
Tom Cherry | c317009 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 84 | using android::base::Timer; |
| 85 | using android::base::unique_fd; |
Bowgo Tsai | 1dacd42 | 2019-03-04 17:53:34 +0800 | [diff] [blame] | 86 | using android::fs_mgr::AvbHandle; |
Yifan Hong | d91998f | 2020-02-20 17:54:57 -0800 | [diff] [blame] | 87 | using android::snapshot::SnapshotManager; |
Tom Cherry | c317009 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 88 | |
| 89 | namespace android { |
| 90 | namespace init { |
| 91 | |
Tom Cherry | c317009 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 92 | namespace { |
| 93 | |
| 94 | enum EnforcingStatus { SELINUX_PERMISSIVE, SELINUX_ENFORCING }; |
| 95 | |
Alistair Delva | 63594a4 | 2021-03-09 11:04:23 -0800 | [diff] [blame] | 96 | EnforcingStatus StatusFromProperty() { |
Tom Cherry | c317009 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 97 | EnforcingStatus status = SELINUX_ENFORCING; |
| 98 | |
Tom Cherry | c88d8f9 | 2019-08-19 15:21:25 -0700 | [diff] [blame] | 99 | ImportKernelCmdline([&](const std::string& key, const std::string& value) { |
| 100 | if (key == "androidboot.selinux" && value == "permissive") { |
| 101 | status = SELINUX_PERMISSIVE; |
| 102 | } |
| 103 | }); |
Tom Cherry | c317009 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 104 | |
Alistair Delva | 63594a4 | 2021-03-09 11:04:23 -0800 | [diff] [blame] | 105 | if (status == SELINUX_ENFORCING) { |
| 106 | ImportBootconfig([&](const std::string& key, const std::string& value) { |
| 107 | if (key == "androidboot.selinux" && value == "permissive") { |
| 108 | status = SELINUX_PERMISSIVE; |
| 109 | } |
| 110 | }); |
| 111 | } |
| 112 | |
Tom Cherry | c317009 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 113 | return status; |
| 114 | } |
| 115 | |
| 116 | bool IsEnforcing() { |
| 117 | if (ALLOW_PERMISSIVE_SELINUX) { |
Alistair Delva | 63594a4 | 2021-03-09 11:04:23 -0800 | [diff] [blame] | 118 | return StatusFromProperty() == SELINUX_ENFORCING; |
Tom Cherry | c317009 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 119 | } |
| 120 | return true; |
| 121 | } |
| 122 | |
| 123 | // Forks, executes the provided program in the child, and waits for the completion in the parent. |
| 124 | // Child's stderr is captured and logged using LOG(ERROR). |
| 125 | bool ForkExecveAndWaitForCompletion(const char* filename, char* const argv[]) { |
| 126 | // Create a pipe used for redirecting child process's output. |
| 127 | // * pipe_fds[0] is the FD the parent will use for reading. |
| 128 | // * pipe_fds[1] is the FD the child will use for writing. |
| 129 | int pipe_fds[2]; |
| 130 | if (pipe(pipe_fds) == -1) { |
| 131 | PLOG(ERROR) << "Failed to create pipe"; |
| 132 | return false; |
| 133 | } |
| 134 | |
| 135 | pid_t child_pid = fork(); |
| 136 | if (child_pid == -1) { |
| 137 | PLOG(ERROR) << "Failed to fork for " << filename; |
| 138 | return false; |
| 139 | } |
| 140 | |
| 141 | if (child_pid == 0) { |
| 142 | // fork succeeded -- this is executing in the child process |
| 143 | |
| 144 | // Close the pipe FD not used by this process |
Nick Kralevich | 3d118e7 | 2017-10-24 10:45:48 -0700 | [diff] [blame] | 145 | close(pipe_fds[0]); |
Tom Cherry | c317009 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 146 | |
| 147 | // Redirect stderr to the pipe FD provided by the parent |
| 148 | if (TEMP_FAILURE_RETRY(dup2(pipe_fds[1], STDERR_FILENO)) == -1) { |
| 149 | PLOG(ERROR) << "Failed to redirect stderr of " << filename; |
| 150 | _exit(127); |
| 151 | return false; |
| 152 | } |
Nick Kralevich | 3d118e7 | 2017-10-24 10:45:48 -0700 | [diff] [blame] | 153 | close(pipe_fds[1]); |
Tom Cherry | c317009 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 154 | |
Tom Cherry | 6de21f1 | 2017-08-22 15:41:03 -0700 | [diff] [blame] | 155 | if (execv(filename, argv) == -1) { |
Tom Cherry | c317009 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 156 | PLOG(ERROR) << "Failed to execve " << filename; |
| 157 | return false; |
| 158 | } |
| 159 | // Unreachable because execve will have succeeded and replaced this code |
| 160 | // with child process's code. |
| 161 | _exit(127); |
| 162 | return false; |
| 163 | } else { |
| 164 | // fork succeeded -- this is executing in the original/parent process |
| 165 | |
| 166 | // Close the pipe FD not used by this process |
Nick Kralevich | 3d118e7 | 2017-10-24 10:45:48 -0700 | [diff] [blame] | 167 | close(pipe_fds[1]); |
Tom Cherry | c317009 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 168 | |
| 169 | // Log the redirected output of the child process. |
| 170 | // It's unfortunate that there's no standard way to obtain an istream for a file descriptor. |
| 171 | // As a result, we're buffering all output and logging it in one go at the end of the |
| 172 | // invocation, instead of logging it as it comes in. |
| 173 | const int child_out_fd = pipe_fds[0]; |
| 174 | std::string child_output; |
| 175 | if (!android::base::ReadFdToString(child_out_fd, &child_output)) { |
| 176 | PLOG(ERROR) << "Failed to capture full output of " << filename; |
| 177 | } |
Nick Kralevich | 3d118e7 | 2017-10-24 10:45:48 -0700 | [diff] [blame] | 178 | close(child_out_fd); |
Tom Cherry | c317009 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 179 | if (!child_output.empty()) { |
| 180 | // Log captured output, line by line, because LOG expects to be invoked for each line |
| 181 | std::istringstream in(child_output); |
| 182 | std::string line; |
| 183 | while (std::getline(in, line)) { |
| 184 | LOG(ERROR) << filename << ": " << line; |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | // Wait for child to terminate |
| 189 | int status; |
| 190 | if (TEMP_FAILURE_RETRY(waitpid(child_pid, &status, 0)) != child_pid) { |
| 191 | PLOG(ERROR) << "Failed to wait for " << filename; |
| 192 | return false; |
| 193 | } |
| 194 | |
| 195 | if (WIFEXITED(status)) { |
| 196 | int status_code = WEXITSTATUS(status); |
| 197 | if (status_code == 0) { |
| 198 | return true; |
| 199 | } else { |
| 200 | LOG(ERROR) << filename << " exited with status " << status_code; |
| 201 | } |
| 202 | } else if (WIFSIGNALED(status)) { |
| 203 | LOG(ERROR) << filename << " killed by signal " << WTERMSIG(status); |
| 204 | } else if (WIFSTOPPED(status)) { |
| 205 | LOG(ERROR) << filename << " stopped by signal " << WSTOPSIG(status); |
| 206 | } else { |
| 207 | LOG(ERROR) << "waitpid for " << filename << " returned unexpected status: " << status; |
| 208 | } |
| 209 | |
| 210 | return false; |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | bool ReadFirstLine(const char* file, std::string* line) { |
| 215 | line->clear(); |
| 216 | |
| 217 | std::string contents; |
| 218 | if (!android::base::ReadFileToString(file, &contents, true /* follow symlinks */)) { |
| 219 | return false; |
| 220 | } |
| 221 | std::istringstream in(contents); |
| 222 | std::getline(in, *line); |
| 223 | return true; |
| 224 | } |
| 225 | |
Inseob Kim | d99d977 | 2021-03-11 17:58:26 +0900 | [diff] [blame] | 226 | Result<std::string> FindPrecompiledSplitPolicy() { |
| 227 | std::string precompiled_sepolicy; |
kaichieh | eef4cd7 | 2017-08-31 22:07:19 +0800 | [diff] [blame] | 228 | // If there is an odm partition, precompiled_sepolicy will be in |
| 229 | // odm/etc/selinux. Otherwise it will be in vendor/etc/selinux. |
| 230 | static constexpr const char vendor_precompiled_sepolicy[] = |
| 231 | "/vendor/etc/selinux/precompiled_sepolicy"; |
| 232 | static constexpr const char odm_precompiled_sepolicy[] = |
| 233 | "/odm/etc/selinux/precompiled_sepolicy"; |
| 234 | if (access(odm_precompiled_sepolicy, R_OK) == 0) { |
Inseob Kim | d99d977 | 2021-03-11 17:58:26 +0900 | [diff] [blame] | 235 | precompiled_sepolicy = odm_precompiled_sepolicy; |
kaichieh | eef4cd7 | 2017-08-31 22:07:19 +0800 | [diff] [blame] | 236 | } else if (access(vendor_precompiled_sepolicy, R_OK) == 0) { |
Inseob Kim | d99d977 | 2021-03-11 17:58:26 +0900 | [diff] [blame] | 237 | precompiled_sepolicy = vendor_precompiled_sepolicy; |
kaichieh | eef4cd7 | 2017-08-31 22:07:19 +0800 | [diff] [blame] | 238 | } else { |
Inseob Kim | d99d977 | 2021-03-11 17:58:26 +0900 | [diff] [blame] | 239 | return ErrnoError() << "No precompiled sepolicy at " << vendor_precompiled_sepolicy; |
Tom Cherry | c317009 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 240 | } |
kaichieh | eef4cd7 | 2017-08-31 22:07:19 +0800 | [diff] [blame] | 241 | |
Inseob Kim | d99d977 | 2021-03-11 17:58:26 +0900 | [diff] [blame] | 242 | // Use precompiled sepolicy only when all corresponding hashes are equal. |
Inseob Kim | d99d977 | 2021-03-11 17:58:26 +0900 | [diff] [blame] | 243 | std::vector<std::pair<std::string, std::string>> sepolicy_hashes{ |
| 244 | {"/system/etc/selinux/plat_sepolicy_and_mapping.sha256", |
| 245 | precompiled_sepolicy + ".plat_sepolicy_and_mapping.sha256"}, |
Inseob Kim | 28fdb67 | 2021-04-29 19:48:27 +0900 | [diff] [blame] | 246 | {"/system_ext/etc/selinux/system_ext_sepolicy_and_mapping.sha256", |
| 247 | precompiled_sepolicy + ".system_ext_sepolicy_and_mapping.sha256"}, |
| 248 | {"/product/etc/selinux/product_sepolicy_and_mapping.sha256", |
| 249 | precompiled_sepolicy + ".product_sepolicy_and_mapping.sha256"}, |
Inseob Kim | d99d977 | 2021-03-11 17:58:26 +0900 | [diff] [blame] | 250 | }; |
| 251 | |
Inseob Kim | d99d977 | 2021-03-11 17:58:26 +0900 | [diff] [blame] | 252 | for (const auto& [actual_id_path, precompiled_id_path] : sepolicy_hashes) { |
Inseob Kim | 28fdb67 | 2021-04-29 19:48:27 +0900 | [diff] [blame] | 253 | // Both of them should exist or both of them shouldn't exist. |
| 254 | if (access(actual_id_path.c_str(), R_OK) != 0) { |
| 255 | if (access(precompiled_id_path.c_str(), R_OK) == 0) { |
| 256 | return Error() << precompiled_id_path << " exists but " << actual_id_path |
| 257 | << " doesn't"; |
| 258 | } |
| 259 | continue; |
| 260 | } |
| 261 | |
Inseob Kim | d99d977 | 2021-03-11 17:58:26 +0900 | [diff] [blame] | 262 | std::string actual_id; |
| 263 | if (!ReadFirstLine(actual_id_path.c_str(), &actual_id)) { |
| 264 | return ErrnoError() << "Failed to read " << actual_id_path; |
| 265 | } |
| 266 | |
| 267 | std::string precompiled_id; |
| 268 | if (!ReadFirstLine(precompiled_id_path.c_str(), &precompiled_id)) { |
| 269 | return ErrnoError() << "Failed to read " << precompiled_id_path; |
| 270 | } |
| 271 | |
| 272 | if (actual_id.empty() || actual_id != precompiled_id) { |
| 273 | return Error() << actual_id_path << " and " << precompiled_id_path << " differ"; |
| 274 | } |
Tri Vo | c8137f9 | 2019-01-22 18:22:25 -0800 | [diff] [blame] | 275 | } |
Inseob Kim | d99d977 | 2021-03-11 17:58:26 +0900 | [diff] [blame] | 276 | |
| 277 | return precompiled_sepolicy; |
Tom Cherry | c317009 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | bool GetVendorMappingVersion(std::string* plat_vers) { |
| 281 | if (!ReadFirstLine("/vendor/etc/selinux/plat_sepolicy_vers.txt", plat_vers)) { |
| 282 | PLOG(ERROR) << "Failed to read /vendor/etc/selinux/plat_sepolicy_vers.txt"; |
| 283 | return false; |
| 284 | } |
| 285 | if (plat_vers->empty()) { |
| 286 | LOG(ERROR) << "No version present in plat_sepolicy_vers.txt"; |
| 287 | return false; |
| 288 | } |
| 289 | return true; |
| 290 | } |
| 291 | |
| 292 | constexpr const char plat_policy_cil_file[] = "/system/etc/selinux/plat_sepolicy.cil"; |
| 293 | |
| 294 | bool IsSplitPolicyDevice() { |
| 295 | return access(plat_policy_cil_file, R_OK) != -1; |
| 296 | } |
| 297 | |
Yi-Yo Chiang | bb77c54 | 2021-09-23 14:14:16 +0000 | [diff] [blame] | 298 | std::optional<const char*> GetUserdebugPlatformPolicyFile() { |
| 299 | // See if we need to load userdebug_plat_sepolicy.cil instead of plat_sepolicy.cil. |
| 300 | const char* force_debuggable_env = getenv("INIT_FORCE_DEBUGGABLE"); |
| 301 | if (force_debuggable_env && "true"s == force_debuggable_env && AvbHandle::IsDeviceUnlocked()) { |
| 302 | const std::vector<const char*> debug_policy_candidates = { |
| 303 | #if INSTALL_DEBUG_POLICY_TO_SYSTEM_EXT == 1 |
| 304 | "/system_ext/etc/selinux/userdebug_plat_sepolicy.cil", |
| 305 | #endif |
| 306 | kDebugRamdiskSEPolicy, |
| 307 | }; |
| 308 | for (const char* debug_policy : debug_policy_candidates) { |
| 309 | if (access(debug_policy, F_OK) == 0) { |
| 310 | return debug_policy; |
| 311 | } |
| 312 | } |
| 313 | } |
| 314 | return std::nullopt; |
| 315 | } |
| 316 | |
David Anderson | 491e4da | 2020-12-08 00:21:20 -0800 | [diff] [blame] | 317 | struct PolicyFile { |
| 318 | unique_fd fd; |
| 319 | std::string path; |
| 320 | }; |
| 321 | |
| 322 | bool OpenSplitPolicy(PolicyFile* policy_file) { |
Jeff Vander Stoep | 5effda4 | 2021-11-05 09:03:11 +0100 | [diff] [blame^] | 323 | // IMPLEMENTATION NOTE: Split policy consists of three or more CIL files: |
Tom Cherry | c317009 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 324 | // * platform -- policy needed due to logic contained in the system image, |
Jeff Vander Stoep | 5effda4 | 2021-11-05 09:03:11 +0100 | [diff] [blame^] | 325 | // * vendor -- policy needed due to logic contained in the vendor image, |
Tom Cherry | c317009 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 326 | // * mapping -- mapping policy which helps preserve forward-compatibility of non-platform policy |
| 327 | // with newer versions of platform policy. |
Jeff Vander Stoep | 5effda4 | 2021-11-05 09:03:11 +0100 | [diff] [blame^] | 328 | // * (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] | 329 | // secilc is invoked to compile the above three policy files into a single monolithic policy |
| 330 | // file. This file is then loaded into the kernel. |
| 331 | |
Yi-Yo Chiang | bb77c54 | 2021-09-23 14:14:16 +0000 | [diff] [blame] | 332 | const auto userdebug_plat_sepolicy = GetUserdebugPlatformPolicyFile(); |
| 333 | const bool use_userdebug_policy = userdebug_plat_sepolicy.has_value(); |
Bowgo Tsai | 1dacd42 | 2019-03-04 17:53:34 +0800 | [diff] [blame] | 334 | if (use_userdebug_policy) { |
Yi-Yo Chiang | bb77c54 | 2021-09-23 14:14:16 +0000 | [diff] [blame] | 335 | LOG(INFO) << "Using userdebug system sepolicy " << *userdebug_plat_sepolicy; |
Bowgo Tsai | 1dacd42 | 2019-03-04 17:53:34 +0800 | [diff] [blame] | 336 | } |
| 337 | |
Tom Cherry | c317009 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 338 | // Load precompiled policy from vendor image, if a matching policy is found there. The policy |
| 339 | // must match the platform policy on the system image. |
Bowgo Tsai | 1dacd42 | 2019-03-04 17:53:34 +0800 | [diff] [blame] | 340 | // use_userdebug_policy requires compiling sepolicy with userdebug_plat_sepolicy.cil. |
| 341 | // Thus it cannot use the precompiled policy from vendor image. |
Inseob Kim | d99d977 | 2021-03-11 17:58:26 +0900 | [diff] [blame] | 342 | if (!use_userdebug_policy) { |
| 343 | if (auto res = FindPrecompiledSplitPolicy(); res.ok()) { |
| 344 | unique_fd fd(open(res->c_str(), O_RDONLY | O_CLOEXEC | O_BINARY)); |
| 345 | if (fd != -1) { |
| 346 | policy_file->fd = std::move(fd); |
| 347 | policy_file->path = std::move(*res); |
| 348 | return true; |
| 349 | } |
| 350 | } else { |
| 351 | LOG(INFO) << res.error(); |
Tom Cherry | c317009 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 352 | } |
| 353 | } |
| 354 | // No suitable precompiled policy could be loaded |
| 355 | |
| 356 | LOG(INFO) << "Compiling SELinux policy"; |
| 357 | |
Tom Cherry | c317009 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 358 | // We store the output of the compilation on /dev because this is the most convenient tmpfs |
| 359 | // storage mount available this early in the boot sequence. |
| 360 | char compiled_sepolicy[] = "/dev/sepolicy.XXXXXX"; |
| 361 | unique_fd compiled_sepolicy_fd(mkostemp(compiled_sepolicy, O_CLOEXEC)); |
| 362 | if (compiled_sepolicy_fd < 0) { |
| 363 | PLOG(ERROR) << "Failed to create temporary file " << compiled_sepolicy; |
| 364 | return false; |
| 365 | } |
| 366 | |
| 367 | // Determine which mapping file to include |
| 368 | std::string vend_plat_vers; |
| 369 | if (!GetVendorMappingVersion(&vend_plat_vers)) { |
| 370 | return false; |
| 371 | } |
Tri Vo | 503f185 | 2019-01-16 11:57:19 -0800 | [diff] [blame] | 372 | std::string plat_mapping_file("/system/etc/selinux/mapping/" + vend_plat_vers + ".cil"); |
kaichieh | eef4cd7 | 2017-08-31 22:07:19 +0800 | [diff] [blame] | 373 | |
Jeff Vander Stoep | 0ac51cf | 2019-05-02 14:05:18 -0700 | [diff] [blame] | 374 | std::string plat_compat_cil_file("/system/etc/selinux/mapping/" + vend_plat_vers + |
| 375 | ".compat.cil"); |
| 376 | if (access(plat_compat_cil_file.c_str(), F_OK) == -1) { |
| 377 | plat_compat_cil_file.clear(); |
| 378 | } |
| 379 | |
Bowgo Tsai | f016f25 | 2019-08-28 17:56:51 +0800 | [diff] [blame] | 380 | std::string system_ext_policy_cil_file("/system_ext/etc/selinux/system_ext_sepolicy.cil"); |
| 381 | if (access(system_ext_policy_cil_file.c_str(), F_OK) == -1) { |
| 382 | system_ext_policy_cil_file.clear(); |
| 383 | } |
| 384 | |
| 385 | std::string system_ext_mapping_file("/system_ext/etc/selinux/mapping/" + vend_plat_vers + |
| 386 | ".cil"); |
| 387 | if (access(system_ext_mapping_file.c_str(), F_OK) == -1) { |
| 388 | system_ext_mapping_file.clear(); |
| 389 | } |
| 390 | |
Yi-Yo Chiang | 731d247 | 2021-03-23 22:11:13 +0800 | [diff] [blame] | 391 | std::string system_ext_compat_cil_file("/system_ext/etc/selinux/mapping/" + vend_plat_vers + |
| 392 | ".compat.cil"); |
| 393 | if (access(system_ext_compat_cil_file.c_str(), F_OK) == -1) { |
| 394 | system_ext_compat_cil_file.clear(); |
| 395 | } |
| 396 | |
Tri Vo | d3518cf | 2018-12-14 14:25:08 -0800 | [diff] [blame] | 397 | std::string product_policy_cil_file("/product/etc/selinux/product_sepolicy.cil"); |
| 398 | if (access(product_policy_cil_file.c_str(), F_OK) == -1) { |
| 399 | product_policy_cil_file.clear(); |
| 400 | } |
| 401 | |
Tri Vo | 503f185 | 2019-01-16 11:57:19 -0800 | [diff] [blame] | 402 | std::string product_mapping_file("/product/etc/selinux/mapping/" + vend_plat_vers + ".cil"); |
| 403 | if (access(product_mapping_file.c_str(), F_OK) == -1) { |
| 404 | product_mapping_file.clear(); |
| 405 | } |
| 406 | |
kaichieh | eef4cd7 | 2017-08-31 22:07:19 +0800 | [diff] [blame] | 407 | std::string vendor_policy_cil_file("/vendor/etc/selinux/vendor_sepolicy.cil"); |
kaichieh | eef4cd7 | 2017-08-31 22:07:19 +0800 | [diff] [blame] | 408 | if (access(vendor_policy_cil_file.c_str(), F_OK) == -1) { |
Jeff Vander Stoep | 5effda4 | 2021-11-05 09:03:11 +0100 | [diff] [blame^] | 409 | LOG(ERROR) << "Missing " << vendor_policy_cil_file; |
| 410 | return false; |
| 411 | } |
| 412 | |
| 413 | std::string plat_pub_versioned_cil_file("/vendor/etc/selinux/plat_pub_versioned.cil"); |
| 414 | if (access(plat_pub_versioned_cil_file.c_str(), F_OK) == -1) { |
Bowgo Tsai | 069ab5b | 2017-10-18 17:03:20 +0800 | [diff] [blame] | 415 | LOG(ERROR) << "Missing " << plat_pub_versioned_cil_file; |
kaichieh | eef4cd7 | 2017-08-31 22:07:19 +0800 | [diff] [blame] | 416 | return false; |
| 417 | } |
| 418 | |
| 419 | // odm_sepolicy.cil is default but optional. |
| 420 | std::string odm_policy_cil_file("/odm/etc/selinux/odm_sepolicy.cil"); |
| 421 | if (access(odm_policy_cil_file.c_str(), F_OK) == -1) { |
| 422 | odm_policy_cil_file.clear(); |
| 423 | } |
Jeff Vander Stoep | 724eda5 | 2019-02-15 12:13:38 -0800 | [diff] [blame] | 424 | const std::string version_as_string = std::to_string(SEPOLICY_VERSION); |
Andreas Huber | c41b838 | 2017-08-18 14:43:52 -0700 | [diff] [blame] | 425 | |
Tom Cherry | c317009 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 426 | // clang-format off |
kaichieh | eef4cd7 | 2017-08-31 22:07:19 +0800 | [diff] [blame] | 427 | std::vector<const char*> compile_args { |
Tom Cherry | c317009 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 428 | "/system/bin/secilc", |
Yi-Yo Chiang | bb77c54 | 2021-09-23 14:14:16 +0000 | [diff] [blame] | 429 | use_userdebug_policy ? *userdebug_plat_sepolicy : plat_policy_cil_file, |
Jeff Vander Stoep | 5e9ba3c | 2017-10-06 17:03:45 -0700 | [diff] [blame] | 430 | "-m", "-M", "true", "-G", "-N", |
Andreas Huber | c41b838 | 2017-08-18 14:43:52 -0700 | [diff] [blame] | 431 | "-c", version_as_string.c_str(), |
Tri Vo | 503f185 | 2019-01-16 11:57:19 -0800 | [diff] [blame] | 432 | plat_mapping_file.c_str(), |
Tom Cherry | c317009 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 433 | "-o", compiled_sepolicy, |
| 434 | // We don't care about file_contexts output by the compiler |
| 435 | "-f", "/sys/fs/selinux/null", // /dev/null is not yet available |
kaichieh | eef4cd7 | 2017-08-31 22:07:19 +0800 | [diff] [blame] | 436 | }; |
Tom Cherry | c317009 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 437 | // clang-format on |
| 438 | |
Jeff Vander Stoep | 0ac51cf | 2019-05-02 14:05:18 -0700 | [diff] [blame] | 439 | if (!plat_compat_cil_file.empty()) { |
| 440 | compile_args.push_back(plat_compat_cil_file.c_str()); |
| 441 | } |
Bowgo Tsai | f016f25 | 2019-08-28 17:56:51 +0800 | [diff] [blame] | 442 | if (!system_ext_policy_cil_file.empty()) { |
| 443 | compile_args.push_back(system_ext_policy_cil_file.c_str()); |
| 444 | } |
| 445 | if (!system_ext_mapping_file.empty()) { |
| 446 | compile_args.push_back(system_ext_mapping_file.c_str()); |
| 447 | } |
Yi-Yo Chiang | 731d247 | 2021-03-23 22:11:13 +0800 | [diff] [blame] | 448 | if (!system_ext_compat_cil_file.empty()) { |
| 449 | compile_args.push_back(system_ext_compat_cil_file.c_str()); |
| 450 | } |
Tri Vo | d3518cf | 2018-12-14 14:25:08 -0800 | [diff] [blame] | 451 | if (!product_policy_cil_file.empty()) { |
| 452 | compile_args.push_back(product_policy_cil_file.c_str()); |
| 453 | } |
Tri Vo | 503f185 | 2019-01-16 11:57:19 -0800 | [diff] [blame] | 454 | if (!product_mapping_file.empty()) { |
| 455 | compile_args.push_back(product_mapping_file.c_str()); |
| 456 | } |
Bowgo Tsai | 069ab5b | 2017-10-18 17:03:20 +0800 | [diff] [blame] | 457 | if (!plat_pub_versioned_cil_file.empty()) { |
| 458 | compile_args.push_back(plat_pub_versioned_cil_file.c_str()); |
kaichieh | eef4cd7 | 2017-08-31 22:07:19 +0800 | [diff] [blame] | 459 | } |
| 460 | if (!vendor_policy_cil_file.empty()) { |
| 461 | compile_args.push_back(vendor_policy_cil_file.c_str()); |
| 462 | } |
| 463 | if (!odm_policy_cil_file.empty()) { |
| 464 | compile_args.push_back(odm_policy_cil_file.c_str()); |
| 465 | } |
| 466 | compile_args.push_back(nullptr); |
| 467 | |
| 468 | if (!ForkExecveAndWaitForCompletion(compile_args[0], (char**)compile_args.data())) { |
Tom Cherry | c317009 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 469 | unlink(compiled_sepolicy); |
| 470 | return false; |
| 471 | } |
| 472 | unlink(compiled_sepolicy); |
| 473 | |
David Anderson | 491e4da | 2020-12-08 00:21:20 -0800 | [diff] [blame] | 474 | policy_file->fd = std::move(compiled_sepolicy_fd); |
| 475 | policy_file->path = compiled_sepolicy; |
Tom Cherry | c317009 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 476 | return true; |
| 477 | } |
| 478 | |
David Anderson | 491e4da | 2020-12-08 00:21:20 -0800 | [diff] [blame] | 479 | bool OpenMonolithicPolicy(PolicyFile* policy_file) { |
| 480 | static constexpr char kSepolicyFile[] = "/sepolicy"; |
| 481 | |
| 482 | LOG(VERBOSE) << "Opening SELinux policy from monolithic file"; |
| 483 | policy_file->fd.reset(open(kSepolicyFile, O_RDONLY | O_CLOEXEC | O_NOFOLLOW)); |
| 484 | if (policy_file->fd < 0) { |
| 485 | PLOG(ERROR) << "Failed to open monolithic SELinux policy"; |
Tom Cherry | c317009 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 486 | return false; |
| 487 | } |
David Anderson | 491e4da | 2020-12-08 00:21:20 -0800 | [diff] [blame] | 488 | policy_file->path = kSepolicyFile; |
Tom Cherry | c317009 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 489 | return true; |
| 490 | } |
| 491 | |
David Anderson | 491e4da | 2020-12-08 00:21:20 -0800 | [diff] [blame] | 492 | void ReadPolicy(std::string* policy) { |
| 493 | PolicyFile policy_file; |
Tom Cherry | c317009 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 494 | |
David Anderson | 491e4da | 2020-12-08 00:21:20 -0800 | [diff] [blame] | 495 | bool ok = IsSplitPolicyDevice() ? OpenSplitPolicy(&policy_file) |
| 496 | : OpenMonolithicPolicy(&policy_file); |
| 497 | if (!ok) { |
| 498 | LOG(FATAL) << "Unable to open SELinux policy"; |
Tom Cherry | c317009 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 499 | } |
| 500 | |
David Anderson | 491e4da | 2020-12-08 00:21:20 -0800 | [diff] [blame] | 501 | if (!android::base::ReadFdToString(policy_file.fd, policy)) { |
| 502 | PLOG(FATAL) << "Failed to read policy file: " << policy_file.path; |
| 503 | } |
| 504 | } |
| 505 | |
| 506 | void SelinuxSetEnforcement() { |
Tom Cherry | c317009 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 507 | bool kernel_enforcing = (security_getenforce() == 1); |
| 508 | bool is_enforcing = IsEnforcing(); |
| 509 | if (kernel_enforcing != is_enforcing) { |
| 510 | if (security_setenforce(is_enforcing)) { |
Paul Lawrence | b2c2d69 | 2019-08-30 11:11:44 -0700 | [diff] [blame] | 511 | PLOG(FATAL) << "security_setenforce(" << (is_enforcing ? "true" : "false") |
| 512 | << ") failed"; |
Tom Cherry | c317009 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 513 | } |
| 514 | } |
| 515 | |
Bernie Innocenti | cecebbb | 2020-02-06 03:49:33 +0900 | [diff] [blame] | 516 | if (auto result = WriteFile("/sys/fs/selinux/checkreqprot", "0"); !result.ok()) { |
Tom Cherry | d8db7ab | 2017-08-17 17:28:30 -0700 | [diff] [blame] | 517 | LOG(FATAL) << "Unable to write to /sys/fs/selinux/checkreqprot: " << result.error(); |
Tom Cherry | c317009 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 518 | } |
Tom Cherry | c317009 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 519 | } |
| 520 | |
Tom Cherry | 8180b48 | 2019-08-26 13:57:51 -0700 | [diff] [blame] | 521 | constexpr size_t kKlogMessageSize = 1024; |
| 522 | |
| 523 | void SelinuxAvcLog(char* buf, size_t buf_len) { |
| 524 | CHECK_GT(buf_len, 0u); |
| 525 | |
| 526 | size_t str_len = strnlen(buf, buf_len); |
| 527 | // trim newline at end of string |
| 528 | if (buf[str_len - 1] == '\n') { |
| 529 | buf[str_len - 1] = '\0'; |
| 530 | } |
| 531 | |
| 532 | struct NetlinkMessage { |
| 533 | nlmsghdr hdr; |
| 534 | char buf[kKlogMessageSize]; |
| 535 | } request = {}; |
| 536 | |
| 537 | request.hdr.nlmsg_flags = NLM_F_REQUEST; |
| 538 | request.hdr.nlmsg_type = AUDIT_USER_AVC; |
| 539 | request.hdr.nlmsg_len = sizeof(request); |
| 540 | strlcpy(request.buf, buf, sizeof(request.buf)); |
| 541 | |
| 542 | auto fd = unique_fd{socket(PF_NETLINK, SOCK_RAW | SOCK_CLOEXEC, NETLINK_AUDIT)}; |
| 543 | if (!fd.ok()) { |
| 544 | return; |
| 545 | } |
| 546 | |
| 547 | TEMP_FAILURE_RETRY(send(fd, &request, sizeof(request), 0)); |
| 548 | } |
| 549 | |
Tom Cherry | 7bfea3d | 2018-11-06 14:12:05 -0800 | [diff] [blame] | 550 | } // namespace |
| 551 | |
Tom Cherry | c317009 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 552 | void SelinuxRestoreContext() { |
| 553 | LOG(INFO) << "Running restorecon..."; |
| 554 | selinux_android_restorecon("/dev", 0); |
| 555 | selinux_android_restorecon("/dev/kmsg", 0); |
| 556 | if constexpr (WORLD_WRITABLE_KMSG) { |
| 557 | selinux_android_restorecon("/dev/kmsg_debug", 0); |
| 558 | } |
Tom Cherry | 81ae075 | 2018-07-30 16:23:49 -0700 | [diff] [blame] | 559 | selinux_android_restorecon("/dev/null", 0); |
| 560 | selinux_android_restorecon("/dev/ptmx", 0); |
Tom Cherry | c317009 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 561 | selinux_android_restorecon("/dev/socket", 0); |
| 562 | selinux_android_restorecon("/dev/random", 0); |
| 563 | selinux_android_restorecon("/dev/urandom", 0); |
| 564 | selinux_android_restorecon("/dev/__properties__", 0); |
| 565 | |
Tom Cherry | c317009 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 566 | selinux_android_restorecon("/dev/block", SELINUX_ANDROID_RESTORECON_RECURSE); |
David Anderson | 1ff7581 | 2020-11-13 00:31:47 -0800 | [diff] [blame] | 567 | selinux_android_restorecon("/dev/dm-user", SELINUX_ANDROID_RESTORECON_RECURSE); |
Tom Cherry | c317009 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 568 | selinux_android_restorecon("/dev/device-mapper", 0); |
Jiyong Park | 4ba548d | 2019-02-22 16:04:35 +0900 | [diff] [blame] | 569 | |
| 570 | selinux_android_restorecon("/apex", 0); |
Kiyoung Kim | 99df54b | 2019-11-22 16:14:10 +0900 | [diff] [blame] | 571 | |
| 572 | selinux_android_restorecon("/linkerconfig", 0); |
Bowgo Tsai | 196cc58 | 2020-01-20 18:03:58 +0800 | [diff] [blame] | 573 | |
David Anderson | c991f34 | 2020-02-21 17:11:07 -0800 | [diff] [blame] | 574 | // adb remount, snapshot-based updates, and DSUs all create files during |
| 575 | // first-stage init. |
Yifan Hong | d91998f | 2020-02-20 17:54:57 -0800 | [diff] [blame] | 576 | selinux_android_restorecon(SnapshotManager::GetGlobalRollbackIndicatorPath().c_str(), 0); |
David Anderson | 4bb500f | 2020-03-06 18:14:19 -0800 | [diff] [blame] | 577 | selinux_android_restorecon("/metadata/gsi", SELINUX_ANDROID_RESTORECON_RECURSE | |
| 578 | SELINUX_ANDROID_RESTORECON_SKIP_SEHASH); |
Tom Cherry | c317009 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 579 | } |
| 580 | |
Tom Cherry | 74069d1 | 2018-07-20 15:26:25 -0700 | [diff] [blame] | 581 | int SelinuxKlogCallback(int type, const char* fmt, ...) { |
| 582 | android::base::LogSeverity severity = android::base::ERROR; |
| 583 | if (type == SELINUX_WARNING) { |
| 584 | severity = android::base::WARNING; |
| 585 | } else if (type == SELINUX_INFO) { |
| 586 | severity = android::base::INFO; |
| 587 | } |
Tom Cherry | 8180b48 | 2019-08-26 13:57:51 -0700 | [diff] [blame] | 588 | char buf[kKlogMessageSize]; |
Tom Cherry | 74069d1 | 2018-07-20 15:26:25 -0700 | [diff] [blame] | 589 | va_list ap; |
| 590 | va_start(ap, fmt); |
Tom Cherry | 8180b48 | 2019-08-26 13:57:51 -0700 | [diff] [blame] | 591 | int length_written = vsnprintf(buf, sizeof(buf), fmt, ap); |
Tom Cherry | 74069d1 | 2018-07-20 15:26:25 -0700 | [diff] [blame] | 592 | va_end(ap); |
Tom Cherry | 8180b48 | 2019-08-26 13:57:51 -0700 | [diff] [blame] | 593 | if (length_written <= 0) { |
| 594 | return 0; |
| 595 | } |
| 596 | if (type == SELINUX_AVC) { |
| 597 | SelinuxAvcLog(buf, sizeof(buf)); |
| 598 | } else { |
| 599 | android::base::KernelLogger(android::base::MAIN, severity, "selinux", nullptr, 0, buf); |
| 600 | } |
Tom Cherry | 74069d1 | 2018-07-20 15:26:25 -0700 | [diff] [blame] | 601 | return 0; |
| 602 | } |
| 603 | |
Tom Cherry | c317009 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 604 | void SelinuxSetupKernelLogging() { |
| 605 | selinux_callback cb; |
Tom Cherry | 74069d1 | 2018-07-20 15:26:25 -0700 | [diff] [blame] | 606 | cb.func_log = SelinuxKlogCallback; |
Tom Cherry | c317009 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 607 | selinux_set_callback(SELINUX_CB_LOG, cb); |
| 608 | } |
| 609 | |
Tom Cherry | 40acb37 | 2018-08-01 13:41:12 -0700 | [diff] [blame] | 610 | int SelinuxGetVendorAndroidVersion() { |
Tom Cherry | c5cf85d | 2019-07-31 13:59:15 -0700 | [diff] [blame] | 611 | static int vendor_android_version = [] { |
| 612 | if (!IsSplitPolicyDevice()) { |
| 613 | // If this device does not split sepolicy files, it's not a Treble device and therefore, |
| 614 | // we assume it's always on the latest platform. |
| 615 | return __ANDROID_API_FUTURE__; |
| 616 | } |
Logan Chien | 837b2a4 | 2018-05-03 14:33:52 +0800 | [diff] [blame] | 617 | |
Tom Cherry | c5cf85d | 2019-07-31 13:59:15 -0700 | [diff] [blame] | 618 | std::string version; |
| 619 | if (!GetVendorMappingVersion(&version)) { |
| 620 | LOG(FATAL) << "Could not read vendor SELinux version"; |
| 621 | } |
Logan Chien | 837b2a4 | 2018-05-03 14:33:52 +0800 | [diff] [blame] | 622 | |
Tom Cherry | c5cf85d | 2019-07-31 13:59:15 -0700 | [diff] [blame] | 623 | int major_version; |
| 624 | std::string major_version_str(version, 0, version.find('.')); |
| 625 | if (!ParseInt(major_version_str, &major_version)) { |
| 626 | PLOG(FATAL) << "Failed to parse the vendor sepolicy major version " |
| 627 | << major_version_str; |
| 628 | } |
Logan Chien | 837b2a4 | 2018-05-03 14:33:52 +0800 | [diff] [blame] | 629 | |
Tom Cherry | c5cf85d | 2019-07-31 13:59:15 -0700 | [diff] [blame] | 630 | return major_version; |
| 631 | }(); |
| 632 | return vendor_android_version; |
Logan Chien | 837b2a4 | 2018-05-03 14:33:52 +0800 | [diff] [blame] | 633 | } |
| 634 | |
David Anderson | d0ce530 | 2020-03-20 21:47:10 -0700 | [diff] [blame] | 635 | // This is for R system.img/system_ext.img to work on old vendor.img as system_ext.img |
| 636 | // is introduced in R. We mount system_ext in second stage init because the first-stage |
| 637 | // init in boot.img won't be updated in the system-only OTA scenario. |
| 638 | void MountMissingSystemPartitions() { |
| 639 | android::fs_mgr::Fstab fstab; |
| 640 | if (!ReadDefaultFstab(&fstab)) { |
| 641 | LOG(ERROR) << "Could not read default fstab"; |
| 642 | } |
| 643 | |
| 644 | android::fs_mgr::Fstab mounts; |
| 645 | if (!ReadFstabFromFile("/proc/mounts", &mounts)) { |
| 646 | LOG(ERROR) << "Could not read /proc/mounts"; |
| 647 | } |
| 648 | |
| 649 | static const std::vector<std::string> kPartitionNames = {"system_ext", "product"}; |
| 650 | |
| 651 | android::fs_mgr::Fstab extra_fstab; |
| 652 | for (const auto& name : kPartitionNames) { |
| 653 | if (GetEntryForMountPoint(&mounts, "/"s + name)) { |
| 654 | // The partition is already mounted. |
| 655 | continue; |
| 656 | } |
| 657 | |
| 658 | auto system_entry = GetEntryForMountPoint(&fstab, "/system"); |
| 659 | if (!system_entry) { |
| 660 | LOG(ERROR) << "Could not find mount entry for /system"; |
| 661 | break; |
| 662 | } |
| 663 | if (!system_entry->fs_mgr_flags.logical) { |
| 664 | LOG(INFO) << "Skipping mount of " << name << ", system is not dynamic."; |
| 665 | break; |
| 666 | } |
| 667 | |
| 668 | auto entry = *system_entry; |
| 669 | auto partition_name = name + fs_mgr_get_slot_suffix(); |
| 670 | auto replace_name = "system"s + fs_mgr_get_slot_suffix(); |
| 671 | |
| 672 | entry.mount_point = "/"s + name; |
| 673 | entry.blk_device = |
| 674 | android::base::StringReplace(entry.blk_device, replace_name, partition_name, false); |
| 675 | if (!fs_mgr_update_logical_partition(&entry)) { |
| 676 | LOG(ERROR) << "Could not update logical partition"; |
| 677 | continue; |
| 678 | } |
| 679 | |
| 680 | extra_fstab.emplace_back(std::move(entry)); |
| 681 | } |
| 682 | |
Yi-Yo Chiang | 2057901 | 2021-04-01 20:14:54 +0800 | [diff] [blame] | 683 | SkipMountingPartitions(&extra_fstab, true /* verbose */); |
David Anderson | d0ce530 | 2020-03-20 21:47:10 -0700 | [diff] [blame] | 684 | if (extra_fstab.empty()) { |
| 685 | return; |
| 686 | } |
| 687 | |
| 688 | BlockDevInitializer block_dev_init; |
| 689 | for (auto& entry : extra_fstab) { |
| 690 | if (access(entry.blk_device.c_str(), F_OK) != 0) { |
| 691 | auto block_dev = android::base::Basename(entry.blk_device); |
| 692 | if (!block_dev_init.InitDmDevice(block_dev)) { |
| 693 | LOG(ERROR) << "Failed to find device-mapper node: " << block_dev; |
| 694 | continue; |
| 695 | } |
| 696 | } |
| 697 | if (fs_mgr_do_mount_one(entry)) { |
| 698 | LOG(ERROR) << "Could not mount " << entry.mount_point; |
| 699 | } |
| 700 | } |
| 701 | } |
| 702 | |
David Anderson | 491e4da | 2020-12-08 00:21:20 -0800 | [diff] [blame] | 703 | static void LoadSelinuxPolicy(std::string& policy) { |
| 704 | LOG(INFO) << "Loading SELinux policy"; |
| 705 | |
| 706 | set_selinuxmnt("/sys/fs/selinux"); |
| 707 | if (security_load_policy(policy.data(), policy.size()) < 0) { |
| 708 | PLOG(FATAL) << "SELinux: Could not load policy"; |
| 709 | } |
| 710 | } |
| 711 | |
| 712 | // The SELinux setup process is carefully orchestrated around snapuserd. Policy |
| 713 | // must be loaded off dynamic partitions, and during an OTA, those partitions |
| 714 | // cannot be read without snapuserd. But, with kernel-privileged snapuserd |
| 715 | // running, loading the policy will immediately trigger audits. |
| 716 | // |
| 717 | // We use a five-step process to address this: |
| 718 | // (1) Read the policy into a string, with snapuserd running. |
| 719 | // (2) Rewrite the snapshot device-mapper tables, to generate new dm-user |
| 720 | // devices and to flush I/O. |
| 721 | // (3) Kill snapuserd, which no longer has any dm-user devices to attach to. |
| 722 | // (4) Load the sepolicy and issue critical restorecons in /dev, carefully |
| 723 | // avoiding anything that would read from /system. |
| 724 | // (5) Re-launch snapuserd and attach it to the dm-user devices from step (2). |
| 725 | // |
| 726 | // After this sequence, it is safe to enable enforcing mode and continue booting. |
Tom Cherry | 7bfea3d | 2018-11-06 14:12:05 -0800 | [diff] [blame] | 727 | int SetupSelinux(char** argv) { |
Mark Salyzyn | beb6abe | 2019-07-29 09:35:18 -0700 | [diff] [blame] | 728 | SetStdioToDevNull(argv); |
Tom Cherry | 59656fb | 2019-05-28 10:19:44 -0700 | [diff] [blame] | 729 | InitKernelLogging(argv); |
Tom Cherry | 7bfea3d | 2018-11-06 14:12:05 -0800 | [diff] [blame] | 730 | |
| 731 | if (REBOOT_BOOTLOADER_ON_PANIC) { |
| 732 | InstallRebootSignalHandlers(); |
| 733 | } |
| 734 | |
Mark Salyzyn | 10377df | 2019-03-27 08:10:41 -0700 | [diff] [blame] | 735 | boot_clock::time_point start_time = boot_clock::now(); |
| 736 | |
David Anderson | d0ce530 | 2020-03-20 21:47:10 -0700 | [diff] [blame] | 737 | MountMissingSystemPartitions(); |
| 738 | |
Tom Cherry | 7bfea3d | 2018-11-06 14:12:05 -0800 | [diff] [blame] | 739 | SelinuxSetupKernelLogging(); |
David Anderson | 491e4da | 2020-12-08 00:21:20 -0800 | [diff] [blame] | 740 | |
| 741 | LOG(INFO) << "Opening SELinux policy"; |
| 742 | |
| 743 | // Read the policy before potentially killing snapuserd. |
| 744 | std::string policy; |
| 745 | ReadPolicy(&policy); |
| 746 | |
| 747 | auto snapuserd_helper = SnapuserdSelinuxHelper::CreateIfNeeded(); |
| 748 | if (snapuserd_helper) { |
| 749 | // Kill the old snapused to avoid audit messages. After this we cannot |
| 750 | // read from /system (or other dynamic partitions) until we call |
| 751 | // FinishTransition(). |
| 752 | snapuserd_helper->StartTransition(); |
| 753 | } |
| 754 | |
| 755 | LoadSelinuxPolicy(policy); |
| 756 | |
| 757 | if (snapuserd_helper) { |
| 758 | // Before enforcing, finish the pending snapuserd transition. |
| 759 | snapuserd_helper->FinishTransition(); |
| 760 | snapuserd_helper = nullptr; |
| 761 | } |
| 762 | |
| 763 | SelinuxSetEnforcement(); |
Tom Cherry | 7bfea3d | 2018-11-06 14:12:05 -0800 | [diff] [blame] | 764 | |
| 765 | // We're in the kernel domain and want to transition to the init domain. File systems that |
| 766 | // store SELabels in their xattrs, such as ext4 do not need an explicit restorecon here, |
| 767 | // but other file systems do. In particular, this is needed for ramdisks such as the |
| 768 | // recovery image for A/B devices. |
| 769 | if (selinux_android_restorecon("/system/bin/init", 0) == -1) { |
| 770 | PLOG(FATAL) << "restorecon failed of /system/bin/init failed"; |
| 771 | } |
| 772 | |
Mark Salyzyn | 44505ec | 2019-05-08 12:44:50 -0700 | [diff] [blame] | 773 | 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] | 774 | |
Tom Cherry | 7bfea3d | 2018-11-06 14:12:05 -0800 | [diff] [blame] | 775 | const char* path = "/system/bin/init"; |
| 776 | const char* args[] = {path, "second_stage", nullptr}; |
| 777 | execv(path, const_cast<char**>(args)); |
| 778 | |
| 779 | // execv() only returns if an error happened, in which case we |
| 780 | // panic and never return from this function. |
| 781 | PLOG(FATAL) << "execv(\"" << path << "\") failed"; |
| 782 | |
| 783 | return 1; |
| 784 | } |
| 785 | |
Tom Cherry | c317009 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 786 | } // namespace init |
| 787 | } // namespace android |