| 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 | 
|  | 39 | //    are the sha256 hashes of the parts of the SEPolicy on /system and /product that were used to | 
|  | 40 | //    compile this precompiled policy.  The system partition contains a similar sha256 of the parts | 
|  | 41 | //    of the SEPolicy that it currently contains.  Symmetrically, product paritition contains a | 
|  | 42 | //    sha256 of its SEPolicy.  System loads this precompiled_sepolicy directly if and only if hashes | 
|  | 43 | //    for system policy match and hashes for product policy match. | 
|  | 44 | // 2) If these hashes do not match, then either /system or /product (or both) have been updated out | 
|  | 45 | //    of sync with /vendor and the init needs to compile the SEPolicy.  /system contains the | 
|  | 46 | //    SEPolicy compiler, secilc, and it is used by the LoadSplitPolicy() function below to compile | 
|  | 47 | //    the SEPolicy to a temp directory and load it.  That function contains even more documentation | 
|  | 48 | //    with the specific implementation details of how the SEPolicy is compiled if needed. | 
| Tom Cherry | c317009 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 49 |  | 
|  | 50 | #include "selinux.h" | 
|  | 51 |  | 
| Tom Cherry | 40acb37 | 2018-08-01 13:41:12 -0700 | [diff] [blame] | 52 | #include <android/api-level.h> | 
| Tom Cherry | c317009 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 53 | #include <fcntl.h> | 
| Tom Cherry | c317009 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 54 | #include <stdlib.h> | 
|  | 55 | #include <sys/wait.h> | 
|  | 56 | #include <unistd.h> | 
|  | 57 |  | 
|  | 58 | #include <android-base/chrono_utils.h> | 
|  | 59 | #include <android-base/file.h> | 
|  | 60 | #include <android-base/logging.h> | 
| Logan Chien | 837b2a4 | 2018-05-03 14:33:52 +0800 | [diff] [blame] | 61 | #include <android-base/parseint.h> | 
| Tom Cherry | c317009 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 62 | #include <android-base/unique_fd.h> | 
| Tom Cherry | 7bfea3d | 2018-11-06 14:12:05 -0800 | [diff] [blame] | 63 | #include <cutils/android_reboot.h> | 
| Tom Cherry | c317009 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 64 | #include <selinux/android.h> | 
|  | 65 |  | 
| Tom Cherry | 7bfea3d | 2018-11-06 14:12:05 -0800 | [diff] [blame] | 66 | #include "reboot_utils.h" | 
| Tom Cherry | c317009 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 67 | #include "util.h" | 
|  | 68 |  | 
| Logan Chien | 837b2a4 | 2018-05-03 14:33:52 +0800 | [diff] [blame] | 69 | using android::base::ParseInt; | 
| Tom Cherry | c317009 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 70 | using android::base::Timer; | 
|  | 71 | using android::base::unique_fd; | 
|  | 72 |  | 
|  | 73 | namespace android { | 
|  | 74 | namespace init { | 
|  | 75 |  | 
| Tom Cherry | c317009 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 76 | namespace { | 
|  | 77 |  | 
| Tom Cherry | 94f3bcd | 2017-08-17 16:52:10 -0700 | [diff] [blame] | 78 | selabel_handle* sehandle = nullptr; | 
|  | 79 |  | 
| Tom Cherry | c317009 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 80 | enum EnforcingStatus { SELINUX_PERMISSIVE, SELINUX_ENFORCING }; | 
|  | 81 |  | 
|  | 82 | EnforcingStatus StatusFromCmdline() { | 
|  | 83 | EnforcingStatus status = SELINUX_ENFORCING; | 
|  | 84 |  | 
|  | 85 | import_kernel_cmdline(false, | 
|  | 86 | [&](const std::string& key, const std::string& value, bool in_qemu) { | 
|  | 87 | if (key == "androidboot.selinux" && value == "permissive") { | 
|  | 88 | status = SELINUX_PERMISSIVE; | 
|  | 89 | } | 
|  | 90 | }); | 
|  | 91 |  | 
|  | 92 | return status; | 
|  | 93 | } | 
|  | 94 |  | 
|  | 95 | bool IsEnforcing() { | 
|  | 96 | if (ALLOW_PERMISSIVE_SELINUX) { | 
|  | 97 | return StatusFromCmdline() == SELINUX_ENFORCING; | 
|  | 98 | } | 
|  | 99 | return true; | 
|  | 100 | } | 
|  | 101 |  | 
|  | 102 | // Forks, executes the provided program in the child, and waits for the completion in the parent. | 
|  | 103 | // Child's stderr is captured and logged using LOG(ERROR). | 
|  | 104 | bool ForkExecveAndWaitForCompletion(const char* filename, char* const argv[]) { | 
|  | 105 | // Create a pipe used for redirecting child process's output. | 
|  | 106 | // * pipe_fds[0] is the FD the parent will use for reading. | 
|  | 107 | // * pipe_fds[1] is the FD the child will use for writing. | 
|  | 108 | int pipe_fds[2]; | 
|  | 109 | if (pipe(pipe_fds) == -1) { | 
|  | 110 | PLOG(ERROR) << "Failed to create pipe"; | 
|  | 111 | return false; | 
|  | 112 | } | 
|  | 113 |  | 
|  | 114 | pid_t child_pid = fork(); | 
|  | 115 | if (child_pid == -1) { | 
|  | 116 | PLOG(ERROR) << "Failed to fork for " << filename; | 
|  | 117 | return false; | 
|  | 118 | } | 
|  | 119 |  | 
|  | 120 | if (child_pid == 0) { | 
|  | 121 | // fork succeeded -- this is executing in the child process | 
|  | 122 |  | 
|  | 123 | // Close the pipe FD not used by this process | 
| Nick Kralevich | 3d118e7 | 2017-10-24 10:45:48 -0700 | [diff] [blame] | 124 | close(pipe_fds[0]); | 
| Tom Cherry | c317009 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 125 |  | 
|  | 126 | // Redirect stderr to the pipe FD provided by the parent | 
|  | 127 | if (TEMP_FAILURE_RETRY(dup2(pipe_fds[1], STDERR_FILENO)) == -1) { | 
|  | 128 | PLOG(ERROR) << "Failed to redirect stderr of " << filename; | 
|  | 129 | _exit(127); | 
|  | 130 | return false; | 
|  | 131 | } | 
| Nick Kralevich | 3d118e7 | 2017-10-24 10:45:48 -0700 | [diff] [blame] | 132 | close(pipe_fds[1]); | 
| Tom Cherry | c317009 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 133 |  | 
| Tom Cherry | 6de21f1 | 2017-08-22 15:41:03 -0700 | [diff] [blame] | 134 | if (execv(filename, argv) == -1) { | 
| Tom Cherry | c317009 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 135 | PLOG(ERROR) << "Failed to execve " << filename; | 
|  | 136 | return false; | 
|  | 137 | } | 
|  | 138 | // Unreachable because execve will have succeeded and replaced this code | 
|  | 139 | // with child process's code. | 
|  | 140 | _exit(127); | 
|  | 141 | return false; | 
|  | 142 | } else { | 
|  | 143 | // fork succeeded -- this is executing in the original/parent process | 
|  | 144 |  | 
|  | 145 | // Close the pipe FD not used by this process | 
| Nick Kralevich | 3d118e7 | 2017-10-24 10:45:48 -0700 | [diff] [blame] | 146 | close(pipe_fds[1]); | 
| Tom Cherry | c317009 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 147 |  | 
|  | 148 | // Log the redirected output of the child process. | 
|  | 149 | // It's unfortunate that there's no standard way to obtain an istream for a file descriptor. | 
|  | 150 | // As a result, we're buffering all output and logging it in one go at the end of the | 
|  | 151 | // invocation, instead of logging it as it comes in. | 
|  | 152 | const int child_out_fd = pipe_fds[0]; | 
|  | 153 | std::string child_output; | 
|  | 154 | if (!android::base::ReadFdToString(child_out_fd, &child_output)) { | 
|  | 155 | PLOG(ERROR) << "Failed to capture full output of " << filename; | 
|  | 156 | } | 
| Nick Kralevich | 3d118e7 | 2017-10-24 10:45:48 -0700 | [diff] [blame] | 157 | close(child_out_fd); | 
| Tom Cherry | c317009 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 158 | if (!child_output.empty()) { | 
|  | 159 | // Log captured output, line by line, because LOG expects to be invoked for each line | 
|  | 160 | std::istringstream in(child_output); | 
|  | 161 | std::string line; | 
|  | 162 | while (std::getline(in, line)) { | 
|  | 163 | LOG(ERROR) << filename << ": " << line; | 
|  | 164 | } | 
|  | 165 | } | 
|  | 166 |  | 
|  | 167 | // Wait for child to terminate | 
|  | 168 | int status; | 
|  | 169 | if (TEMP_FAILURE_RETRY(waitpid(child_pid, &status, 0)) != child_pid) { | 
|  | 170 | PLOG(ERROR) << "Failed to wait for " << filename; | 
|  | 171 | return false; | 
|  | 172 | } | 
|  | 173 |  | 
|  | 174 | if (WIFEXITED(status)) { | 
|  | 175 | int status_code = WEXITSTATUS(status); | 
|  | 176 | if (status_code == 0) { | 
|  | 177 | return true; | 
|  | 178 | } else { | 
|  | 179 | LOG(ERROR) << filename << " exited with status " << status_code; | 
|  | 180 | } | 
|  | 181 | } else if (WIFSIGNALED(status)) { | 
|  | 182 | LOG(ERROR) << filename << " killed by signal " << WTERMSIG(status); | 
|  | 183 | } else if (WIFSTOPPED(status)) { | 
|  | 184 | LOG(ERROR) << filename << " stopped by signal " << WSTOPSIG(status); | 
|  | 185 | } else { | 
|  | 186 | LOG(ERROR) << "waitpid for " << filename << " returned unexpected status: " << status; | 
|  | 187 | } | 
|  | 188 |  | 
|  | 189 | return false; | 
|  | 190 | } | 
|  | 191 | } | 
|  | 192 |  | 
|  | 193 | bool ReadFirstLine(const char* file, std::string* line) { | 
|  | 194 | line->clear(); | 
|  | 195 |  | 
|  | 196 | std::string contents; | 
|  | 197 | if (!android::base::ReadFileToString(file, &contents, true /* follow symlinks */)) { | 
|  | 198 | return false; | 
|  | 199 | } | 
|  | 200 | std::istringstream in(contents); | 
|  | 201 | std::getline(in, *line); | 
|  | 202 | return true; | 
|  | 203 | } | 
|  | 204 |  | 
|  | 205 | bool FindPrecompiledSplitPolicy(std::string* file) { | 
|  | 206 | file->clear(); | 
| kaichieh | eef4cd7 | 2017-08-31 22:07:19 +0800 | [diff] [blame] | 207 | // If there is an odm partition, precompiled_sepolicy will be in | 
|  | 208 | // odm/etc/selinux. Otherwise it will be in vendor/etc/selinux. | 
|  | 209 | static constexpr const char vendor_precompiled_sepolicy[] = | 
|  | 210 | "/vendor/etc/selinux/precompiled_sepolicy"; | 
|  | 211 | static constexpr const char odm_precompiled_sepolicy[] = | 
|  | 212 | "/odm/etc/selinux/precompiled_sepolicy"; | 
|  | 213 | if (access(odm_precompiled_sepolicy, R_OK) == 0) { | 
|  | 214 | *file = odm_precompiled_sepolicy; | 
|  | 215 | } else if (access(vendor_precompiled_sepolicy, R_OK) == 0) { | 
|  | 216 | *file = vendor_precompiled_sepolicy; | 
|  | 217 | } else { | 
|  | 218 | PLOG(INFO) << "No precompiled sepolicy"; | 
| Tom Cherry | c317009 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 219 | return false; | 
|  | 220 | } | 
|  | 221 | std::string actual_plat_id; | 
| Tri Vo | c8137f9 | 2019-01-22 18:22:25 -0800 | [diff] [blame] | 222 | 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] | 223 | PLOG(INFO) << "Failed to read " | 
| Tri Vo | c8137f9 | 2019-01-22 18:22:25 -0800 | [diff] [blame] | 224 | "/system/etc/selinux/plat_sepolicy_and_mapping.sha256"; | 
|  | 225 | return false; | 
|  | 226 | } | 
|  | 227 | std::string actual_product_id; | 
|  | 228 | if (!ReadFirstLine("/product/etc/selinux/product_sepolicy_and_mapping.sha256", | 
|  | 229 | &actual_product_id)) { | 
|  | 230 | PLOG(INFO) << "Failed to read " | 
|  | 231 | "/product/etc/selinux/product_sepolicy_and_mapping.sha256"; | 
| Tom Cherry | c317009 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 232 | return false; | 
|  | 233 | } | 
| kaichieh | eef4cd7 | 2017-08-31 22:07:19 +0800 | [diff] [blame] | 234 |  | 
| Tom Cherry | c317009 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 235 | std::string precompiled_plat_id; | 
| Tri Vo | c8137f9 | 2019-01-22 18:22:25 -0800 | [diff] [blame] | 236 | std::string precompiled_plat_sha256 = *file + ".plat_sepolicy_and_mapping.sha256"; | 
|  | 237 | if (!ReadFirstLine(precompiled_plat_sha256.c_str(), &precompiled_plat_id)) { | 
|  | 238 | PLOG(INFO) << "Failed to read " << precompiled_plat_sha256; | 
| kaichieh | eef4cd7 | 2017-08-31 22:07:19 +0800 | [diff] [blame] | 239 | file->clear(); | 
| Tom Cherry | c317009 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 240 | return false; | 
|  | 241 | } | 
| Tri Vo | c8137f9 | 2019-01-22 18:22:25 -0800 | [diff] [blame] | 242 | std::string precompiled_product_id; | 
|  | 243 | std::string precompiled_product_sha256 = *file + ".product_sepolicy_and_mapping.sha256"; | 
|  | 244 | if (!ReadFirstLine(precompiled_product_sha256.c_str(), &precompiled_product_id)) { | 
|  | 245 | PLOG(INFO) << "Failed to read " << precompiled_product_sha256; | 
|  | 246 | file->clear(); | 
|  | 247 | return false; | 
|  | 248 | } | 
|  | 249 | if (actual_plat_id.empty() || actual_plat_id != precompiled_plat_id || | 
|  | 250 | actual_product_id.empty() || actual_product_id != precompiled_product_id) { | 
| kaichieh | eef4cd7 | 2017-08-31 22:07:19 +0800 | [diff] [blame] | 251 | file->clear(); | 
| Tom Cherry | c317009 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 252 | return false; | 
|  | 253 | } | 
| Tom Cherry | c317009 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 254 | return true; | 
|  | 255 | } | 
|  | 256 |  | 
|  | 257 | bool GetVendorMappingVersion(std::string* plat_vers) { | 
|  | 258 | if (!ReadFirstLine("/vendor/etc/selinux/plat_sepolicy_vers.txt", plat_vers)) { | 
|  | 259 | PLOG(ERROR) << "Failed to read /vendor/etc/selinux/plat_sepolicy_vers.txt"; | 
|  | 260 | return false; | 
|  | 261 | } | 
|  | 262 | if (plat_vers->empty()) { | 
|  | 263 | LOG(ERROR) << "No version present in plat_sepolicy_vers.txt"; | 
|  | 264 | return false; | 
|  | 265 | } | 
|  | 266 | return true; | 
|  | 267 | } | 
|  | 268 |  | 
|  | 269 | constexpr const char plat_policy_cil_file[] = "/system/etc/selinux/plat_sepolicy.cil"; | 
|  | 270 |  | 
|  | 271 | bool IsSplitPolicyDevice() { | 
|  | 272 | return access(plat_policy_cil_file, R_OK) != -1; | 
|  | 273 | } | 
|  | 274 |  | 
|  | 275 | bool LoadSplitPolicy() { | 
|  | 276 | // IMPLEMENTATION NOTE: Split policy consists of three CIL files: | 
|  | 277 | // * platform -- policy needed due to logic contained in the system image, | 
|  | 278 | // * non-platform -- policy needed due to logic contained in the vendor image, | 
|  | 279 | // * mapping -- mapping policy which helps preserve forward-compatibility of non-platform policy | 
|  | 280 | //   with newer versions of platform policy. | 
|  | 281 | // | 
|  | 282 | // secilc is invoked to compile the above three policy files into a single monolithic policy | 
|  | 283 | // file. This file is then loaded into the kernel. | 
|  | 284 |  | 
|  | 285 | // Load precompiled policy from vendor image, if a matching policy is found there. The policy | 
|  | 286 | // must match the platform policy on the system image. | 
|  | 287 | std::string precompiled_sepolicy_file; | 
|  | 288 | if (FindPrecompiledSplitPolicy(&precompiled_sepolicy_file)) { | 
|  | 289 | unique_fd fd(open(precompiled_sepolicy_file.c_str(), O_RDONLY | O_CLOEXEC | O_BINARY)); | 
|  | 290 | if (fd != -1) { | 
|  | 291 | if (selinux_android_load_policy_from_fd(fd, precompiled_sepolicy_file.c_str()) < 0) { | 
|  | 292 | LOG(ERROR) << "Failed to load SELinux policy from " << precompiled_sepolicy_file; | 
|  | 293 | return false; | 
|  | 294 | } | 
|  | 295 | return true; | 
|  | 296 | } | 
|  | 297 | } | 
|  | 298 | // No suitable precompiled policy could be loaded | 
|  | 299 |  | 
|  | 300 | LOG(INFO) << "Compiling SELinux policy"; | 
|  | 301 |  | 
|  | 302 | // Determine the highest policy language version supported by the kernel | 
|  | 303 | set_selinuxmnt("/sys/fs/selinux"); | 
|  | 304 | int max_policy_version = security_policyvers(); | 
|  | 305 | if (max_policy_version == -1) { | 
|  | 306 | PLOG(ERROR) << "Failed to determine highest policy version supported by kernel"; | 
|  | 307 | return false; | 
|  | 308 | } | 
|  | 309 |  | 
|  | 310 | // We store the output of the compilation on /dev because this is the most convenient tmpfs | 
|  | 311 | // storage mount available this early in the boot sequence. | 
|  | 312 | char compiled_sepolicy[] = "/dev/sepolicy.XXXXXX"; | 
|  | 313 | unique_fd compiled_sepolicy_fd(mkostemp(compiled_sepolicy, O_CLOEXEC)); | 
|  | 314 | if (compiled_sepolicy_fd < 0) { | 
|  | 315 | PLOG(ERROR) << "Failed to create temporary file " << compiled_sepolicy; | 
|  | 316 | return false; | 
|  | 317 | } | 
|  | 318 |  | 
|  | 319 | // Determine which mapping file to include | 
|  | 320 | std::string vend_plat_vers; | 
|  | 321 | if (!GetVendorMappingVersion(&vend_plat_vers)) { | 
|  | 322 | return false; | 
|  | 323 | } | 
| Tri Vo | 503f185 | 2019-01-16 11:57:19 -0800 | [diff] [blame] | 324 | std::string plat_mapping_file("/system/etc/selinux/mapping/" + vend_plat_vers + ".cil"); | 
| kaichieh | eef4cd7 | 2017-08-31 22:07:19 +0800 | [diff] [blame] | 325 |  | 
| Tri Vo | d3518cf | 2018-12-14 14:25:08 -0800 | [diff] [blame] | 326 | std::string product_policy_cil_file("/product/etc/selinux/product_sepolicy.cil"); | 
|  | 327 | if (access(product_policy_cil_file.c_str(), F_OK) == -1) { | 
|  | 328 | product_policy_cil_file.clear(); | 
|  | 329 | } | 
|  | 330 |  | 
| Tri Vo | 503f185 | 2019-01-16 11:57:19 -0800 | [diff] [blame] | 331 | std::string product_mapping_file("/product/etc/selinux/mapping/" + vend_plat_vers + ".cil"); | 
|  | 332 | if (access(product_mapping_file.c_str(), F_OK) == -1) { | 
|  | 333 | product_mapping_file.clear(); | 
|  | 334 | } | 
|  | 335 |  | 
| Bowgo Tsai | 069ab5b | 2017-10-18 17:03:20 +0800 | [diff] [blame] | 336 | // 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] | 337 | // nonplat_sepolicy.cil. | 
| Bowgo Tsai | 069ab5b | 2017-10-18 17:03:20 +0800 | [diff] [blame] | 338 | 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] | 339 | std::string vendor_policy_cil_file("/vendor/etc/selinux/vendor_sepolicy.cil"); | 
|  | 340 |  | 
|  | 341 | if (access(vendor_policy_cil_file.c_str(), F_OK) == -1) { | 
|  | 342 | // For backward compatibility. | 
|  | 343 | // TODO: remove this after no device is using nonplat_sepolicy.cil. | 
|  | 344 | vendor_policy_cil_file = "/vendor/etc/selinux/nonplat_sepolicy.cil"; | 
| Bowgo Tsai | 069ab5b | 2017-10-18 17:03:20 +0800 | [diff] [blame] | 345 | plat_pub_versioned_cil_file.clear(); | 
|  | 346 | } else if (access(plat_pub_versioned_cil_file.c_str(), F_OK) == -1) { | 
|  | 347 | LOG(ERROR) << "Missing " << plat_pub_versioned_cil_file; | 
| kaichieh | eef4cd7 | 2017-08-31 22:07:19 +0800 | [diff] [blame] | 348 | return false; | 
|  | 349 | } | 
|  | 350 |  | 
|  | 351 | // odm_sepolicy.cil is default but optional. | 
|  | 352 | std::string odm_policy_cil_file("/odm/etc/selinux/odm_sepolicy.cil"); | 
|  | 353 | if (access(odm_policy_cil_file.c_str(), F_OK) == -1) { | 
|  | 354 | odm_policy_cil_file.clear(); | 
|  | 355 | } | 
| Andreas Huber | c41b838 | 2017-08-18 14:43:52 -0700 | [diff] [blame] | 356 | const std::string version_as_string = std::to_string(max_policy_version); | 
|  | 357 |  | 
| Tom Cherry | c317009 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 358 | // clang-format off | 
| kaichieh | eef4cd7 | 2017-08-31 22:07:19 +0800 | [diff] [blame] | 359 | std::vector<const char*> compile_args { | 
| Tom Cherry | c317009 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 360 | "/system/bin/secilc", | 
|  | 361 | plat_policy_cil_file, | 
| Jeff Vander Stoep | 5e9ba3c | 2017-10-06 17:03:45 -0700 | [diff] [blame] | 362 | "-m", "-M", "true", "-G", "-N", | 
| Tom Cherry | c317009 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 363 | // Target the highest policy language version supported by the kernel | 
| Andreas Huber | c41b838 | 2017-08-18 14:43:52 -0700 | [diff] [blame] | 364 | "-c", version_as_string.c_str(), | 
| Tri Vo | 503f185 | 2019-01-16 11:57:19 -0800 | [diff] [blame] | 365 | plat_mapping_file.c_str(), | 
| Tom Cherry | c317009 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 366 | "-o", compiled_sepolicy, | 
|  | 367 | // We don't care about file_contexts output by the compiler | 
|  | 368 | "-f", "/sys/fs/selinux/null",  // /dev/null is not yet available | 
| kaichieh | eef4cd7 | 2017-08-31 22:07:19 +0800 | [diff] [blame] | 369 | }; | 
| Tom Cherry | c317009 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 370 | // clang-format on | 
|  | 371 |  | 
| Tri Vo | d3518cf | 2018-12-14 14:25:08 -0800 | [diff] [blame] | 372 | if (!product_policy_cil_file.empty()) { | 
|  | 373 | compile_args.push_back(product_policy_cil_file.c_str()); | 
|  | 374 | } | 
| Tri Vo | 503f185 | 2019-01-16 11:57:19 -0800 | [diff] [blame] | 375 | if (!product_mapping_file.empty()) { | 
|  | 376 | compile_args.push_back(product_mapping_file.c_str()); | 
|  | 377 | } | 
| Bowgo Tsai | 069ab5b | 2017-10-18 17:03:20 +0800 | [diff] [blame] | 378 | if (!plat_pub_versioned_cil_file.empty()) { | 
|  | 379 | compile_args.push_back(plat_pub_versioned_cil_file.c_str()); | 
| kaichieh | eef4cd7 | 2017-08-31 22:07:19 +0800 | [diff] [blame] | 380 | } | 
|  | 381 | if (!vendor_policy_cil_file.empty()) { | 
|  | 382 | compile_args.push_back(vendor_policy_cil_file.c_str()); | 
|  | 383 | } | 
|  | 384 | if (!odm_policy_cil_file.empty()) { | 
|  | 385 | compile_args.push_back(odm_policy_cil_file.c_str()); | 
|  | 386 | } | 
|  | 387 | compile_args.push_back(nullptr); | 
|  | 388 |  | 
|  | 389 | if (!ForkExecveAndWaitForCompletion(compile_args[0], (char**)compile_args.data())) { | 
| Tom Cherry | c317009 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 390 | unlink(compiled_sepolicy); | 
|  | 391 | return false; | 
|  | 392 | } | 
|  | 393 | unlink(compiled_sepolicy); | 
|  | 394 |  | 
|  | 395 | LOG(INFO) << "Loading compiled SELinux policy"; | 
|  | 396 | if (selinux_android_load_policy_from_fd(compiled_sepolicy_fd, compiled_sepolicy) < 0) { | 
|  | 397 | LOG(ERROR) << "Failed to load SELinux policy from " << compiled_sepolicy; | 
|  | 398 | return false; | 
|  | 399 | } | 
|  | 400 |  | 
|  | 401 | return true; | 
|  | 402 | } | 
|  | 403 |  | 
|  | 404 | bool LoadMonolithicPolicy() { | 
|  | 405 | LOG(VERBOSE) << "Loading SELinux policy from monolithic file"; | 
|  | 406 | if (selinux_android_load_policy() < 0) { | 
|  | 407 | PLOG(ERROR) << "Failed to load monolithic SELinux policy"; | 
|  | 408 | return false; | 
|  | 409 | } | 
|  | 410 | return true; | 
|  | 411 | } | 
|  | 412 |  | 
|  | 413 | bool LoadPolicy() { | 
|  | 414 | return IsSplitPolicyDevice() ? LoadSplitPolicy() : LoadMonolithicPolicy(); | 
|  | 415 | } | 
|  | 416 |  | 
| Tom Cherry | c317009 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 417 | void SelinuxInitialize() { | 
|  | 418 | Timer t; | 
|  | 419 |  | 
|  | 420 | LOG(INFO) << "Loading SELinux policy"; | 
|  | 421 | if (!LoadPolicy()) { | 
| Tom Cherry | d8db7ab | 2017-08-17 17:28:30 -0700 | [diff] [blame] | 422 | LOG(FATAL) << "Unable to load SELinux policy"; | 
| Tom Cherry | c317009 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 423 | } | 
|  | 424 |  | 
|  | 425 | bool kernel_enforcing = (security_getenforce() == 1); | 
|  | 426 | bool is_enforcing = IsEnforcing(); | 
|  | 427 | if (kernel_enforcing != is_enforcing) { | 
|  | 428 | if (security_setenforce(is_enforcing)) { | 
| Tom Cherry | d8db7ab | 2017-08-17 17:28:30 -0700 | [diff] [blame] | 429 | PLOG(FATAL) << "security_setenforce(%s) failed" << (is_enforcing ? "true" : "false"); | 
| Tom Cherry | c317009 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 430 | } | 
|  | 431 | } | 
|  | 432 |  | 
| Tom Cherry | 11a3aee | 2017-08-03 12:54:07 -0700 | [diff] [blame] | 433 | if (auto result = WriteFile("/sys/fs/selinux/checkreqprot", "0"); !result) { | 
| Tom Cherry | d8db7ab | 2017-08-17 17:28:30 -0700 | [diff] [blame] | 434 | LOG(FATAL) << "Unable to write to /sys/fs/selinux/checkreqprot: " << result.error(); | 
| Tom Cherry | c317009 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 435 | } | 
|  | 436 |  | 
|  | 437 | // init's first stage can't set properties, so pass the time to the second stage. | 
|  | 438 | setenv("INIT_SELINUX_TOOK", std::to_string(t.duration().count()).c_str(), 1); | 
|  | 439 | } | 
|  | 440 |  | 
| Tom Cherry | 7bfea3d | 2018-11-06 14:12:05 -0800 | [diff] [blame] | 441 | }  // namespace | 
|  | 442 |  | 
| Tom Cherry | c317009 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 443 | // The files and directories that were created before initial sepolicy load or | 
|  | 444 | // files on ramdisk need to have their security context restored to the proper | 
|  | 445 | // value. This must happen before /dev is populated by ueventd. | 
|  | 446 | void SelinuxRestoreContext() { | 
|  | 447 | LOG(INFO) << "Running restorecon..."; | 
|  | 448 | selinux_android_restorecon("/dev", 0); | 
|  | 449 | selinux_android_restorecon("/dev/kmsg", 0); | 
|  | 450 | if constexpr (WORLD_WRITABLE_KMSG) { | 
|  | 451 | selinux_android_restorecon("/dev/kmsg_debug", 0); | 
|  | 452 | } | 
| Tom Cherry | 81ae075 | 2018-07-30 16:23:49 -0700 | [diff] [blame] | 453 | selinux_android_restorecon("/dev/null", 0); | 
|  | 454 | selinux_android_restorecon("/dev/ptmx", 0); | 
| Tom Cherry | c317009 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 455 | selinux_android_restorecon("/dev/socket", 0); | 
|  | 456 | selinux_android_restorecon("/dev/random", 0); | 
|  | 457 | selinux_android_restorecon("/dev/urandom", 0); | 
|  | 458 | selinux_android_restorecon("/dev/__properties__", 0); | 
|  | 459 |  | 
| Tom Cherry | c317009 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 460 | selinux_android_restorecon("/dev/block", SELINUX_ANDROID_RESTORECON_RECURSE); | 
|  | 461 | selinux_android_restorecon("/dev/device-mapper", 0); | 
| Tom Cherry | c317009 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 462 | } | 
|  | 463 |  | 
| Tom Cherry | 74069d1 | 2018-07-20 15:26:25 -0700 | [diff] [blame] | 464 | int SelinuxKlogCallback(int type, const char* fmt, ...) { | 
|  | 465 | android::base::LogSeverity severity = android::base::ERROR; | 
|  | 466 | if (type == SELINUX_WARNING) { | 
|  | 467 | severity = android::base::WARNING; | 
|  | 468 | } else if (type == SELINUX_INFO) { | 
|  | 469 | severity = android::base::INFO; | 
|  | 470 | } | 
|  | 471 | char buf[1024]; | 
|  | 472 | va_list ap; | 
|  | 473 | va_start(ap, fmt); | 
|  | 474 | vsnprintf(buf, sizeof(buf), fmt, ap); | 
|  | 475 | va_end(ap); | 
|  | 476 | android::base::KernelLogger(android::base::MAIN, severity, "selinux", nullptr, 0, buf); | 
|  | 477 | return 0; | 
|  | 478 | } | 
|  | 479 |  | 
| Tom Cherry | c317009 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 480 | // This function sets up SELinux logging to be written to kmsg, to match init's logging. | 
|  | 481 | void SelinuxSetupKernelLogging() { | 
|  | 482 | selinux_callback cb; | 
| Tom Cherry | 74069d1 | 2018-07-20 15:26:25 -0700 | [diff] [blame] | 483 | cb.func_log = SelinuxKlogCallback; | 
| Tom Cherry | c317009 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 484 | selinux_set_callback(SELINUX_CB_LOG, cb); | 
|  | 485 | } | 
|  | 486 |  | 
| Tom Cherry | 40acb37 | 2018-08-01 13:41:12 -0700 | [diff] [blame] | 487 | // This function returns the Android version with which the vendor SEPolicy was compiled. | 
|  | 488 | // It is used for version checks such as whether or not vendor_init should be used | 
|  | 489 | int SelinuxGetVendorAndroidVersion() { | 
| Logan Chien | 837b2a4 | 2018-05-03 14:33:52 +0800 | [diff] [blame] | 490 | if (!IsSplitPolicyDevice()) { | 
| Tom Cherry | 40acb37 | 2018-08-01 13:41:12 -0700 | [diff] [blame] | 491 | // If this device does not split sepolicy files, it's not a Treble device and therefore, | 
|  | 492 | // we assume it's always on the latest platform. | 
|  | 493 | return __ANDROID_API_FUTURE__; | 
| Logan Chien | 837b2a4 | 2018-05-03 14:33:52 +0800 | [diff] [blame] | 494 | } | 
|  | 495 |  | 
|  | 496 | std::string version; | 
|  | 497 | if (!GetVendorMappingVersion(&version)) { | 
| Tom Cherry | 40acb37 | 2018-08-01 13:41:12 -0700 | [diff] [blame] | 498 | LOG(FATAL) << "Could not read vendor SELinux version"; | 
| Logan Chien | 837b2a4 | 2018-05-03 14:33:52 +0800 | [diff] [blame] | 499 | } | 
|  | 500 |  | 
|  | 501 | int major_version; | 
|  | 502 | std::string major_version_str(version, 0, version.find('.')); | 
|  | 503 | if (!ParseInt(major_version_str, &major_version)) { | 
| Tom Cherry | 40acb37 | 2018-08-01 13:41:12 -0700 | [diff] [blame] | 504 | PLOG(FATAL) << "Failed to parse the vendor sepolicy major version " << major_version_str; | 
| Logan Chien | 837b2a4 | 2018-05-03 14:33:52 +0800 | [diff] [blame] | 505 | } | 
|  | 506 |  | 
| Tom Cherry | 40acb37 | 2018-08-01 13:41:12 -0700 | [diff] [blame] | 507 | return major_version; | 
| Logan Chien | 837b2a4 | 2018-05-03 14:33:52 +0800 | [diff] [blame] | 508 | } | 
|  | 509 |  | 
| Tom Cherry | 7bfea3d | 2018-11-06 14:12:05 -0800 | [diff] [blame] | 510 | // This function initializes SELinux then execs init to run in the init SELinux context. | 
|  | 511 | int SetupSelinux(char** argv) { | 
|  | 512 | android::base::InitLogging(argv, &android::base::KernelLogger, [](const char*) { | 
|  | 513 | RebootSystem(ANDROID_RB_RESTART2, "bootloader"); | 
|  | 514 | }); | 
|  | 515 |  | 
|  | 516 | if (REBOOT_BOOTLOADER_ON_PANIC) { | 
|  | 517 | InstallRebootSignalHandlers(); | 
|  | 518 | } | 
|  | 519 |  | 
|  | 520 | // Set up SELinux, loading the SELinux policy. | 
|  | 521 | SelinuxSetupKernelLogging(); | 
|  | 522 | SelinuxInitialize(); | 
|  | 523 |  | 
|  | 524 | // We're in the kernel domain and want to transition to the init domain.  File systems that | 
|  | 525 | // store SELabels in their xattrs, such as ext4 do not need an explicit restorecon here, | 
|  | 526 | // but other file systems do.  In particular, this is needed for ramdisks such as the | 
|  | 527 | // recovery image for A/B devices. | 
|  | 528 | if (selinux_android_restorecon("/system/bin/init", 0) == -1) { | 
|  | 529 | PLOG(FATAL) << "restorecon failed of /system/bin/init failed"; | 
|  | 530 | } | 
|  | 531 |  | 
|  | 532 | const char* path = "/system/bin/init"; | 
|  | 533 | const char* args[] = {path, "second_stage", nullptr}; | 
|  | 534 | execv(path, const_cast<char**>(args)); | 
|  | 535 |  | 
|  | 536 | // execv() only returns if an error happened, in which case we | 
|  | 537 | // panic and never return from this function. | 
|  | 538 | PLOG(FATAL) << "execv(\"" << path << "\") failed"; | 
|  | 539 |  | 
|  | 540 | return 1; | 
|  | 541 | } | 
|  | 542 |  | 
| Tom Cherry | c317009 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 543 | // selinux_android_file_context_handle() takes on the order of 10+ms to run, so we want to cache | 
|  | 544 | // its value.  selinux_android_restorecon() also needs an sehandle for file context look up.  It | 
|  | 545 | // will create and store its own copy, but selinux_android_set_sehandle() can be used to provide | 
|  | 546 | // one, thus eliminating an extra call to selinux_android_file_context_handle(). | 
|  | 547 | void SelabelInitialize() { | 
|  | 548 | sehandle = selinux_android_file_context_handle(); | 
|  | 549 | selinux_android_set_sehandle(sehandle); | 
|  | 550 | } | 
|  | 551 |  | 
|  | 552 | // A C++ wrapper around selabel_lookup() using the cached sehandle. | 
|  | 553 | // If sehandle is null, this returns success with an empty context. | 
|  | 554 | bool SelabelLookupFileContext(const std::string& key, int type, std::string* result) { | 
|  | 555 | result->clear(); | 
|  | 556 |  | 
|  | 557 | if (!sehandle) return true; | 
|  | 558 |  | 
|  | 559 | char* context; | 
|  | 560 | if (selabel_lookup(sehandle, &context, key.c_str(), type) != 0) { | 
|  | 561 | return false; | 
|  | 562 | } | 
|  | 563 | *result = context; | 
|  | 564 | free(context); | 
|  | 565 | return true; | 
|  | 566 | } | 
|  | 567 |  | 
|  | 568 | // A C++ wrapper around selabel_lookup_best_match() using the cached sehandle. | 
|  | 569 | // If sehandle is null, this returns success with an empty context. | 
|  | 570 | bool SelabelLookupFileContextBestMatch(const std::string& key, | 
|  | 571 | const std::vector<std::string>& aliases, int type, | 
|  | 572 | std::string* result) { | 
|  | 573 | result->clear(); | 
|  | 574 |  | 
|  | 575 | if (!sehandle) return true; | 
|  | 576 |  | 
|  | 577 | std::vector<const char*> c_aliases; | 
|  | 578 | for (const auto& alias : aliases) { | 
|  | 579 | c_aliases.emplace_back(alias.c_str()); | 
|  | 580 | } | 
|  | 581 | c_aliases.emplace_back(nullptr); | 
|  | 582 |  | 
|  | 583 | char* context; | 
|  | 584 | if (selabel_lookup_best_match(sehandle, &context, key.c_str(), &c_aliases[0], type) != 0) { | 
|  | 585 | return false; | 
|  | 586 | } | 
|  | 587 | *result = context; | 
|  | 588 | free(context); | 
|  | 589 | return true; | 
|  | 590 | } | 
|  | 591 |  | 
|  | 592 | }  // namespace init | 
|  | 593 | }  // namespace android |