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