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