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