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