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