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 | |
Jeffrey Vander Stoep | baeece6 | 2022-02-08 12:42:33 +0000 | [diff] [blame] | 29 | // The split policy is for supporting treble devices and updateable apexes. It splits the SEPolicy |
| 30 | // across files on /system/etc/selinux (the 'plat' portion of the policy), /vendor/etc/selinux |
| 31 | // (the 'vendor' portion of the policy), /system_ext/etc/selinux, /product/etc/selinux, |
| 32 | // /odm/etc/selinux, and /dev/selinux (the apex portion of policy). This is necessary to allow |
| 33 | // images to be updated independently of the vendor image, while maintaining contributions from |
| 34 | // multiple partitions in the SEPolicy. This is especially important for VTS testing, where the |
| 35 | // SEPolicy on the Google System Image may not be identical to the system image shipped on a |
| 36 | // vendor's device. |
Tom Cherry | c317009 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 37 | |
| 38 | // The split SEPolicy is loaded as described below: |
Tri Vo | c8137f9 | 2019-01-22 18:22:25 -0800 | [diff] [blame] | 39 | // 1) There is a precompiled SEPolicy located at either /vendor/etc/selinux/precompiled_sepolicy or |
| 40 | // /odm/etc/selinux/precompiled_sepolicy if odm parition is present. Stored along with this file |
Jeffrey Vander Stoep | baeece6 | 2022-02-08 12:42:33 +0000 | [diff] [blame] | 41 | // are the sha256 hashes of the parts of the SEPolicy on /system, /system_ext, /product, and apex |
| 42 | // that were used to compile this precompiled policy. The system partition contains a similar |
| 43 | // sha256 of the parts of the SEPolicy that it currently contains. Symmetrically, system_ext, |
| 44 | // product, and apex contain sha256 hashes of their SEPolicy. Init loads this |
Bowgo Tsai | f016f25 | 2019-08-28 17:56:51 +0800 | [diff] [blame] | 45 | // precompiled_sepolicy directly if and only if the hashes along with the precompiled SEPolicy on |
Jeffrey Vander Stoep | baeece6 | 2022-02-08 12:42:33 +0000 | [diff] [blame] | 46 | // /vendor or /odm match the hashes for system, system_ext, product, and apex SEPolicy, |
| 47 | // respectively. |
| 48 | // 2) If these hashes do not match, then either /system or /system_ext /product, or apex (or some of |
| 49 | // them) have been updated out of sync with /vendor (or /odm if it is present) and the init needs |
| 50 | // to compile the SEPolicy. /system contains the SEPolicy compiler, secilc, and it is used by |
| 51 | // the OpenSplitPolicy() function below to compile the SEPolicy to a temp directory and load it. |
Bowgo Tsai | f016f25 | 2019-08-28 17:56:51 +0800 | [diff] [blame] | 52 | // That function contains even more documentation with the specific implementation details of how |
| 53 | // the SEPolicy is compiled if needed. |
Tom Cherry | c317009 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 54 | |
| 55 | #include "selinux.h" |
| 56 | |
Tom Cherry | 40acb37 | 2018-08-01 13:41:12 -0700 | [diff] [blame] | 57 | #include <android/api-level.h> |
Tom Cherry | c317009 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 58 | #include <fcntl.h> |
Tom Cherry | 8180b48 | 2019-08-26 13:57:51 -0700 | [diff] [blame] | 59 | #include <linux/audit.h> |
| 60 | #include <linux/netlink.h> |
Tom Cherry | c317009 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 61 | #include <stdlib.h> |
| 62 | #include <sys/wait.h> |
| 63 | #include <unistd.h> |
Jeffrey Vander Stoep | baeece6 | 2022-02-08 12:42:33 +0000 | [diff] [blame] | 64 | #include <fstream> |
Tom Cherry | c317009 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 65 | |
Jeffrey Vander Stoep | baeece6 | 2022-02-08 12:42:33 +0000 | [diff] [blame] | 66 | #include <CertUtils.h> |
Tom Cherry | c317009 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 67 | #include <android-base/chrono_utils.h> |
| 68 | #include <android-base/file.h> |
| 69 | #include <android-base/logging.h> |
Logan Chien | 837b2a4 | 2018-05-03 14:33:52 +0800 | [diff] [blame] | 70 | #include <android-base/parseint.h> |
Inseob Kim | d99d977 | 2021-03-11 17:58:26 +0900 | [diff] [blame] | 71 | #include <android-base/result.h> |
Jeffrey Vander Stoep | baeece6 | 2022-02-08 12:42:33 +0000 | [diff] [blame] | 72 | #include <android-base/scopeguard.h> |
David Anderson | d0ce530 | 2020-03-20 21:47:10 -0700 | [diff] [blame] | 73 | #include <android-base/strings.h> |
Tom Cherry | c317009 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 74 | #include <android-base/unique_fd.h> |
Bowgo Tsai | 1dacd42 | 2019-03-04 17:53:34 +0800 | [diff] [blame] | 75 | #include <fs_avb/fs_avb.h> |
David Anderson | d0ce530 | 2020-03-20 21:47:10 -0700 | [diff] [blame] | 76 | #include <fs_mgr.h> |
Bowgo Tsai | 196cc58 | 2020-01-20 18:03:58 +0800 | [diff] [blame] | 77 | #include <libgsi/libgsi.h> |
Yifan Hong | d91998f | 2020-02-20 17:54:57 -0800 | [diff] [blame] | 78 | #include <libsnapshot/snapshot.h> |
Tom Cherry | c317009 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 79 | #include <selinux/android.h> |
Jeffrey Vander Stoep | baeece6 | 2022-02-08 12:42:33 +0000 | [diff] [blame] | 80 | #include <ziparchive/zip_archive.h> |
Tom Cherry | c317009 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 81 | |
David Anderson | d0ce530 | 2020-03-20 21:47:10 -0700 | [diff] [blame] | 82 | #include "block_dev_initializer.h" |
Bowgo Tsai | 30afda7 | 2019-04-11 23:57:24 +0800 | [diff] [blame] | 83 | #include "debug_ramdisk.h" |
Tom Cherry | 7bfea3d | 2018-11-06 14:12:05 -0800 | [diff] [blame] | 84 | #include "reboot_utils.h" |
David Anderson | 491e4da | 2020-12-08 00:21:20 -0800 | [diff] [blame] | 85 | #include "snapuserd_transition.h" |
Tom Cherry | c317009 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 86 | #include "util.h" |
| 87 | |
Bowgo Tsai | 1dacd42 | 2019-03-04 17:53:34 +0800 | [diff] [blame] | 88 | using namespace std::string_literals; |
| 89 | |
Logan Chien | 837b2a4 | 2018-05-03 14:33:52 +0800 | [diff] [blame] | 90 | using android::base::ParseInt; |
Tom Cherry | c317009 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 91 | using android::base::Timer; |
| 92 | using android::base::unique_fd; |
Bowgo Tsai | 1dacd42 | 2019-03-04 17:53:34 +0800 | [diff] [blame] | 93 | using android::fs_mgr::AvbHandle; |
Yifan Hong | d91998f | 2020-02-20 17:54:57 -0800 | [diff] [blame] | 94 | using android::snapshot::SnapshotManager; |
Tom Cherry | c317009 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 95 | |
| 96 | namespace android { |
| 97 | namespace init { |
| 98 | |
Tom Cherry | c317009 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 99 | namespace { |
| 100 | |
| 101 | enum EnforcingStatus { SELINUX_PERMISSIVE, SELINUX_ENFORCING }; |
| 102 | |
Alistair Delva | 63594a4 | 2021-03-09 11:04:23 -0800 | [diff] [blame] | 103 | EnforcingStatus StatusFromProperty() { |
Tom Cherry | c317009 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 104 | EnforcingStatus status = SELINUX_ENFORCING; |
| 105 | |
Tom Cherry | c88d8f9 | 2019-08-19 15:21:25 -0700 | [diff] [blame] | 106 | ImportKernelCmdline([&](const std::string& key, const std::string& value) { |
| 107 | if (key == "androidboot.selinux" && value == "permissive") { |
| 108 | status = SELINUX_PERMISSIVE; |
| 109 | } |
| 110 | }); |
Tom Cherry | c317009 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 111 | |
Alistair Delva | 63594a4 | 2021-03-09 11:04:23 -0800 | [diff] [blame] | 112 | if (status == SELINUX_ENFORCING) { |
| 113 | ImportBootconfig([&](const std::string& key, const std::string& value) { |
| 114 | if (key == "androidboot.selinux" && value == "permissive") { |
| 115 | status = SELINUX_PERMISSIVE; |
| 116 | } |
| 117 | }); |
| 118 | } |
| 119 | |
Tom Cherry | c317009 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 120 | return status; |
| 121 | } |
| 122 | |
| 123 | bool IsEnforcing() { |
| 124 | if (ALLOW_PERMISSIVE_SELINUX) { |
Alistair Delva | 63594a4 | 2021-03-09 11:04:23 -0800 | [diff] [blame] | 125 | return StatusFromProperty() == SELINUX_ENFORCING; |
Tom Cherry | c317009 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 126 | } |
| 127 | return true; |
| 128 | } |
| 129 | |
| 130 | // Forks, executes the provided program in the child, and waits for the completion in the parent. |
| 131 | // Child's stderr is captured and logged using LOG(ERROR). |
| 132 | bool ForkExecveAndWaitForCompletion(const char* filename, char* const argv[]) { |
| 133 | // Create a pipe used for redirecting child process's output. |
| 134 | // * pipe_fds[0] is the FD the parent will use for reading. |
| 135 | // * pipe_fds[1] is the FD the child will use for writing. |
| 136 | int pipe_fds[2]; |
| 137 | if (pipe(pipe_fds) == -1) { |
| 138 | PLOG(ERROR) << "Failed to create pipe"; |
| 139 | return false; |
| 140 | } |
| 141 | |
| 142 | pid_t child_pid = fork(); |
| 143 | if (child_pid == -1) { |
| 144 | PLOG(ERROR) << "Failed to fork for " << filename; |
| 145 | return false; |
| 146 | } |
| 147 | |
| 148 | if (child_pid == 0) { |
| 149 | // fork succeeded -- this is executing in the child 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[0]); |
Tom Cherry | c317009 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 153 | |
| 154 | // Redirect stderr to the pipe FD provided by the parent |
| 155 | if (TEMP_FAILURE_RETRY(dup2(pipe_fds[1], STDERR_FILENO)) == -1) { |
| 156 | PLOG(ERROR) << "Failed to redirect stderr of " << filename; |
| 157 | _exit(127); |
| 158 | return false; |
| 159 | } |
Nick Kralevich | 3d118e7 | 2017-10-24 10:45:48 -0700 | [diff] [blame] | 160 | close(pipe_fds[1]); |
Tom Cherry | c317009 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 161 | |
Tom Cherry | 6de21f1 | 2017-08-22 15:41:03 -0700 | [diff] [blame] | 162 | if (execv(filename, argv) == -1) { |
Tom Cherry | c317009 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 163 | PLOG(ERROR) << "Failed to execve " << filename; |
| 164 | return false; |
| 165 | } |
| 166 | // Unreachable because execve will have succeeded and replaced this code |
| 167 | // with child process's code. |
| 168 | _exit(127); |
| 169 | return false; |
| 170 | } else { |
| 171 | // fork succeeded -- this is executing in the original/parent process |
| 172 | |
| 173 | // Close the pipe FD not used by this process |
Nick Kralevich | 3d118e7 | 2017-10-24 10:45:48 -0700 | [diff] [blame] | 174 | close(pipe_fds[1]); |
Tom Cherry | c317009 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 175 | |
| 176 | // Log the redirected output of the child process. |
| 177 | // It's unfortunate that there's no standard way to obtain an istream for a file descriptor. |
| 178 | // As a result, we're buffering all output and logging it in one go at the end of the |
| 179 | // invocation, instead of logging it as it comes in. |
| 180 | const int child_out_fd = pipe_fds[0]; |
| 181 | std::string child_output; |
| 182 | if (!android::base::ReadFdToString(child_out_fd, &child_output)) { |
| 183 | PLOG(ERROR) << "Failed to capture full output of " << filename; |
| 184 | } |
Nick Kralevich | 3d118e7 | 2017-10-24 10:45:48 -0700 | [diff] [blame] | 185 | close(child_out_fd); |
Tom Cherry | c317009 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 186 | if (!child_output.empty()) { |
| 187 | // Log captured output, line by line, because LOG expects to be invoked for each line |
| 188 | std::istringstream in(child_output); |
| 189 | std::string line; |
| 190 | while (std::getline(in, line)) { |
| 191 | LOG(ERROR) << filename << ": " << line; |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | // Wait for child to terminate |
| 196 | int status; |
| 197 | if (TEMP_FAILURE_RETRY(waitpid(child_pid, &status, 0)) != child_pid) { |
| 198 | PLOG(ERROR) << "Failed to wait for " << filename; |
| 199 | return false; |
| 200 | } |
| 201 | |
| 202 | if (WIFEXITED(status)) { |
| 203 | int status_code = WEXITSTATUS(status); |
| 204 | if (status_code == 0) { |
| 205 | return true; |
| 206 | } else { |
| 207 | LOG(ERROR) << filename << " exited with status " << status_code; |
| 208 | } |
| 209 | } else if (WIFSIGNALED(status)) { |
| 210 | LOG(ERROR) << filename << " killed by signal " << WTERMSIG(status); |
| 211 | } else if (WIFSTOPPED(status)) { |
| 212 | LOG(ERROR) << filename << " stopped by signal " << WSTOPSIG(status); |
| 213 | } else { |
| 214 | LOG(ERROR) << "waitpid for " << filename << " returned unexpected status: " << status; |
| 215 | } |
| 216 | |
| 217 | return false; |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | bool ReadFirstLine(const char* file, std::string* line) { |
| 222 | line->clear(); |
| 223 | |
| 224 | std::string contents; |
| 225 | if (!android::base::ReadFileToString(file, &contents, true /* follow symlinks */)) { |
| 226 | return false; |
| 227 | } |
| 228 | std::istringstream in(contents); |
| 229 | std::getline(in, *line); |
| 230 | return true; |
| 231 | } |
| 232 | |
Inseob Kim | d99d977 | 2021-03-11 17:58:26 +0900 | [diff] [blame] | 233 | Result<std::string> FindPrecompiledSplitPolicy() { |
| 234 | std::string precompiled_sepolicy; |
kaichieh | eef4cd7 | 2017-08-31 22:07:19 +0800 | [diff] [blame] | 235 | // If there is an odm partition, precompiled_sepolicy will be in |
| 236 | // odm/etc/selinux. Otherwise it will be in vendor/etc/selinux. |
| 237 | static constexpr const char vendor_precompiled_sepolicy[] = |
Sandro | 1120f7f | 2022-09-05 15:56:38 +0000 | [diff] [blame] | 238 | "/vendor/etc/selinux/precompiled_sepolicy"; |
kaichieh | eef4cd7 | 2017-08-31 22:07:19 +0800 | [diff] [blame] | 239 | static constexpr const char odm_precompiled_sepolicy[] = |
Sandro | 1120f7f | 2022-09-05 15:56:38 +0000 | [diff] [blame] | 240 | "/odm/etc/selinux/precompiled_sepolicy"; |
kaichieh | eef4cd7 | 2017-08-31 22:07:19 +0800 | [diff] [blame] | 241 | if (access(odm_precompiled_sepolicy, R_OK) == 0) { |
Inseob Kim | d99d977 | 2021-03-11 17:58:26 +0900 | [diff] [blame] | 242 | precompiled_sepolicy = odm_precompiled_sepolicy; |
kaichieh | eef4cd7 | 2017-08-31 22:07:19 +0800 | [diff] [blame] | 243 | } else if (access(vendor_precompiled_sepolicy, R_OK) == 0) { |
Inseob Kim | d99d977 | 2021-03-11 17:58:26 +0900 | [diff] [blame] | 244 | precompiled_sepolicy = vendor_precompiled_sepolicy; |
kaichieh | eef4cd7 | 2017-08-31 22:07:19 +0800 | [diff] [blame] | 245 | } else { |
Inseob Kim | d99d977 | 2021-03-11 17:58:26 +0900 | [diff] [blame] | 246 | return ErrnoError() << "No precompiled sepolicy at " << vendor_precompiled_sepolicy; |
Tom Cherry | c317009 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 247 | } |
kaichieh | eef4cd7 | 2017-08-31 22:07:19 +0800 | [diff] [blame] | 248 | |
Inseob Kim | d99d977 | 2021-03-11 17:58:26 +0900 | [diff] [blame] | 249 | // Use precompiled sepolicy only when all corresponding hashes are equal. |
Inseob Kim | d99d977 | 2021-03-11 17:58:26 +0900 | [diff] [blame] | 250 | std::vector<std::pair<std::string, std::string>> sepolicy_hashes{ |
| 251 | {"/system/etc/selinux/plat_sepolicy_and_mapping.sha256", |
| 252 | precompiled_sepolicy + ".plat_sepolicy_and_mapping.sha256"}, |
Inseob Kim | 28fdb67 | 2021-04-29 19:48:27 +0900 | [diff] [blame] | 253 | {"/system_ext/etc/selinux/system_ext_sepolicy_and_mapping.sha256", |
| 254 | precompiled_sepolicy + ".system_ext_sepolicy_and_mapping.sha256"}, |
| 255 | {"/product/etc/selinux/product_sepolicy_and_mapping.sha256", |
| 256 | precompiled_sepolicy + ".product_sepolicy_and_mapping.sha256"}, |
Jeffrey Vander Stoep | baeece6 | 2022-02-08 12:42:33 +0000 | [diff] [blame] | 257 | {"/dev/selinux/apex_sepolicy.sha256", precompiled_sepolicy + ".apex_sepolicy.sha256"}, |
Inseob Kim | d99d977 | 2021-03-11 17:58:26 +0900 | [diff] [blame] | 258 | }; |
| 259 | |
Inseob Kim | d99d977 | 2021-03-11 17:58:26 +0900 | [diff] [blame] | 260 | for (const auto& [actual_id_path, precompiled_id_path] : sepolicy_hashes) { |
Inseob Kim | 28fdb67 | 2021-04-29 19:48:27 +0900 | [diff] [blame] | 261 | // Both of them should exist or both of them shouldn't exist. |
| 262 | if (access(actual_id_path.c_str(), R_OK) != 0) { |
| 263 | if (access(precompiled_id_path.c_str(), R_OK) == 0) { |
| 264 | return Error() << precompiled_id_path << " exists but " << actual_id_path |
| 265 | << " doesn't"; |
| 266 | } |
| 267 | continue; |
| 268 | } |
| 269 | |
Inseob Kim | d99d977 | 2021-03-11 17:58:26 +0900 | [diff] [blame] | 270 | std::string actual_id; |
| 271 | if (!ReadFirstLine(actual_id_path.c_str(), &actual_id)) { |
| 272 | return ErrnoError() << "Failed to read " << actual_id_path; |
| 273 | } |
| 274 | |
| 275 | std::string precompiled_id; |
| 276 | if (!ReadFirstLine(precompiled_id_path.c_str(), &precompiled_id)) { |
| 277 | return ErrnoError() << "Failed to read " << precompiled_id_path; |
| 278 | } |
| 279 | |
| 280 | if (actual_id.empty() || actual_id != precompiled_id) { |
| 281 | return Error() << actual_id_path << " and " << precompiled_id_path << " differ"; |
| 282 | } |
Tri Vo | c8137f9 | 2019-01-22 18:22:25 -0800 | [diff] [blame] | 283 | } |
Inseob Kim | d99d977 | 2021-03-11 17:58:26 +0900 | [diff] [blame] | 284 | |
| 285 | return precompiled_sepolicy; |
Tom Cherry | c317009 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | bool GetVendorMappingVersion(std::string* plat_vers) { |
| 289 | if (!ReadFirstLine("/vendor/etc/selinux/plat_sepolicy_vers.txt", plat_vers)) { |
| 290 | PLOG(ERROR) << "Failed to read /vendor/etc/selinux/plat_sepolicy_vers.txt"; |
| 291 | return false; |
| 292 | } |
| 293 | if (plat_vers->empty()) { |
| 294 | LOG(ERROR) << "No version present in plat_sepolicy_vers.txt"; |
| 295 | return false; |
| 296 | } |
| 297 | return true; |
| 298 | } |
| 299 | |
| 300 | constexpr const char plat_policy_cil_file[] = "/system/etc/selinux/plat_sepolicy.cil"; |
| 301 | |
| 302 | bool IsSplitPolicyDevice() { |
| 303 | return access(plat_policy_cil_file, R_OK) != -1; |
| 304 | } |
| 305 | |
Yi-Yo Chiang | bb77c54 | 2021-09-23 14:14:16 +0000 | [diff] [blame] | 306 | std::optional<const char*> GetUserdebugPlatformPolicyFile() { |
| 307 | // See if we need to load userdebug_plat_sepolicy.cil instead of plat_sepolicy.cil. |
| 308 | const char* force_debuggable_env = getenv("INIT_FORCE_DEBUGGABLE"); |
| 309 | if (force_debuggable_env && "true"s == force_debuggable_env && AvbHandle::IsDeviceUnlocked()) { |
| 310 | const std::vector<const char*> debug_policy_candidates = { |
| 311 | #if INSTALL_DEBUG_POLICY_TO_SYSTEM_EXT == 1 |
| 312 | "/system_ext/etc/selinux/userdebug_plat_sepolicy.cil", |
| 313 | #endif |
| 314 | kDebugRamdiskSEPolicy, |
| 315 | }; |
| 316 | for (const char* debug_policy : debug_policy_candidates) { |
| 317 | if (access(debug_policy, F_OK) == 0) { |
| 318 | return debug_policy; |
| 319 | } |
| 320 | } |
| 321 | } |
| 322 | return std::nullopt; |
| 323 | } |
| 324 | |
David Anderson | 491e4da | 2020-12-08 00:21:20 -0800 | [diff] [blame] | 325 | struct PolicyFile { |
| 326 | unique_fd fd; |
| 327 | std::string path; |
| 328 | }; |
| 329 | |
| 330 | bool OpenSplitPolicy(PolicyFile* policy_file) { |
Jeff Vander Stoep | 5effda4 | 2021-11-05 09:03:11 +0100 | [diff] [blame] | 331 | // IMPLEMENTATION NOTE: Split policy consists of three or more CIL files: |
Tom Cherry | c317009 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 332 | // * platform -- policy needed due to logic contained in the system image, |
Jeff Vander Stoep | 5effda4 | 2021-11-05 09:03:11 +0100 | [diff] [blame] | 333 | // * vendor -- policy needed due to logic contained in the vendor image, |
Tom Cherry | c317009 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 334 | // * mapping -- mapping policy which helps preserve forward-compatibility of non-platform policy |
| 335 | // with newer versions of platform policy. |
Jeffrey Vander Stoep | baeece6 | 2022-02-08 12:42:33 +0000 | [diff] [blame] | 336 | // * (optional) policy needed due to logic on product, system_ext, odm, or apex. |
Tom Cherry | c317009 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 337 | // secilc is invoked to compile the above three policy files into a single monolithic policy |
| 338 | // file. This file is then loaded into the kernel. |
| 339 | |
Yi-Yo Chiang | bb77c54 | 2021-09-23 14:14:16 +0000 | [diff] [blame] | 340 | const auto userdebug_plat_sepolicy = GetUserdebugPlatformPolicyFile(); |
| 341 | const bool use_userdebug_policy = userdebug_plat_sepolicy.has_value(); |
Bowgo Tsai | 1dacd42 | 2019-03-04 17:53:34 +0800 | [diff] [blame] | 342 | if (use_userdebug_policy) { |
Yi-Yo Chiang | bb77c54 | 2021-09-23 14:14:16 +0000 | [diff] [blame] | 343 | LOG(INFO) << "Using userdebug system sepolicy " << *userdebug_plat_sepolicy; |
Bowgo Tsai | 1dacd42 | 2019-03-04 17:53:34 +0800 | [diff] [blame] | 344 | } |
| 345 | |
Tom Cherry | c317009 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 346 | // Load precompiled policy from vendor image, if a matching policy is found there. The policy |
| 347 | // must match the platform policy on the system image. |
Bowgo Tsai | 1dacd42 | 2019-03-04 17:53:34 +0800 | [diff] [blame] | 348 | // use_userdebug_policy requires compiling sepolicy with userdebug_plat_sepolicy.cil. |
| 349 | // Thus it cannot use the precompiled policy from vendor image. |
Inseob Kim | d99d977 | 2021-03-11 17:58:26 +0900 | [diff] [blame] | 350 | if (!use_userdebug_policy) { |
| 351 | if (auto res = FindPrecompiledSplitPolicy(); res.ok()) { |
| 352 | unique_fd fd(open(res->c_str(), O_RDONLY | O_CLOEXEC | O_BINARY)); |
| 353 | if (fd != -1) { |
| 354 | policy_file->fd = std::move(fd); |
| 355 | policy_file->path = std::move(*res); |
| 356 | return true; |
| 357 | } |
| 358 | } else { |
| 359 | LOG(INFO) << res.error(); |
Tom Cherry | c317009 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 360 | } |
| 361 | } |
| 362 | // No suitable precompiled policy could be loaded |
| 363 | |
| 364 | LOG(INFO) << "Compiling SELinux policy"; |
| 365 | |
Tom Cherry | c317009 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 366 | // We store the output of the compilation on /dev because this is the most convenient tmpfs |
| 367 | // storage mount available this early in the boot sequence. |
| 368 | char compiled_sepolicy[] = "/dev/sepolicy.XXXXXX"; |
| 369 | unique_fd compiled_sepolicy_fd(mkostemp(compiled_sepolicy, O_CLOEXEC)); |
| 370 | if (compiled_sepolicy_fd < 0) { |
| 371 | PLOG(ERROR) << "Failed to create temporary file " << compiled_sepolicy; |
| 372 | return false; |
| 373 | } |
| 374 | |
| 375 | // Determine which mapping file to include |
| 376 | std::string vend_plat_vers; |
| 377 | if (!GetVendorMappingVersion(&vend_plat_vers)) { |
| 378 | return false; |
| 379 | } |
Tri Vo | 503f185 | 2019-01-16 11:57:19 -0800 | [diff] [blame] | 380 | std::string plat_mapping_file("/system/etc/selinux/mapping/" + vend_plat_vers + ".cil"); |
kaichieh | eef4cd7 | 2017-08-31 22:07:19 +0800 | [diff] [blame] | 381 | |
Jeff Vander Stoep | 0ac51cf | 2019-05-02 14:05:18 -0700 | [diff] [blame] | 382 | std::string plat_compat_cil_file("/system/etc/selinux/mapping/" + vend_plat_vers + |
| 383 | ".compat.cil"); |
| 384 | if (access(plat_compat_cil_file.c_str(), F_OK) == -1) { |
| 385 | plat_compat_cil_file.clear(); |
| 386 | } |
| 387 | |
Bowgo Tsai | f016f25 | 2019-08-28 17:56:51 +0800 | [diff] [blame] | 388 | std::string system_ext_policy_cil_file("/system_ext/etc/selinux/system_ext_sepolicy.cil"); |
| 389 | if (access(system_ext_policy_cil_file.c_str(), F_OK) == -1) { |
| 390 | system_ext_policy_cil_file.clear(); |
| 391 | } |
| 392 | |
| 393 | std::string system_ext_mapping_file("/system_ext/etc/selinux/mapping/" + vend_plat_vers + |
| 394 | ".cil"); |
| 395 | if (access(system_ext_mapping_file.c_str(), F_OK) == -1) { |
| 396 | system_ext_mapping_file.clear(); |
| 397 | } |
| 398 | |
Yi-Yo Chiang | 731d247 | 2021-03-23 22:11:13 +0800 | [diff] [blame] | 399 | std::string system_ext_compat_cil_file("/system_ext/etc/selinux/mapping/" + vend_plat_vers + |
| 400 | ".compat.cil"); |
| 401 | if (access(system_ext_compat_cil_file.c_str(), F_OK) == -1) { |
| 402 | system_ext_compat_cil_file.clear(); |
| 403 | } |
| 404 | |
Tri Vo | d3518cf | 2018-12-14 14:25:08 -0800 | [diff] [blame] | 405 | std::string product_policy_cil_file("/product/etc/selinux/product_sepolicy.cil"); |
| 406 | if (access(product_policy_cil_file.c_str(), F_OK) == -1) { |
| 407 | product_policy_cil_file.clear(); |
| 408 | } |
| 409 | |
Tri Vo | 503f185 | 2019-01-16 11:57:19 -0800 | [diff] [blame] | 410 | std::string product_mapping_file("/product/etc/selinux/mapping/" + vend_plat_vers + ".cil"); |
| 411 | if (access(product_mapping_file.c_str(), F_OK) == -1) { |
| 412 | product_mapping_file.clear(); |
| 413 | } |
| 414 | |
kaichieh | eef4cd7 | 2017-08-31 22:07:19 +0800 | [diff] [blame] | 415 | std::string vendor_policy_cil_file("/vendor/etc/selinux/vendor_sepolicy.cil"); |
kaichieh | eef4cd7 | 2017-08-31 22:07:19 +0800 | [diff] [blame] | 416 | if (access(vendor_policy_cil_file.c_str(), F_OK) == -1) { |
Jeff Vander Stoep | 5effda4 | 2021-11-05 09:03:11 +0100 | [diff] [blame] | 417 | LOG(ERROR) << "Missing " << vendor_policy_cil_file; |
| 418 | return false; |
| 419 | } |
| 420 | |
| 421 | std::string plat_pub_versioned_cil_file("/vendor/etc/selinux/plat_pub_versioned.cil"); |
| 422 | if (access(plat_pub_versioned_cil_file.c_str(), F_OK) == -1) { |
Bowgo Tsai | 069ab5b | 2017-10-18 17:03:20 +0800 | [diff] [blame] | 423 | LOG(ERROR) << "Missing " << plat_pub_versioned_cil_file; |
kaichieh | eef4cd7 | 2017-08-31 22:07:19 +0800 | [diff] [blame] | 424 | return false; |
| 425 | } |
| 426 | |
| 427 | // odm_sepolicy.cil is default but optional. |
| 428 | std::string odm_policy_cil_file("/odm/etc/selinux/odm_sepolicy.cil"); |
| 429 | if (access(odm_policy_cil_file.c_str(), F_OK) == -1) { |
| 430 | odm_policy_cil_file.clear(); |
| 431 | } |
Jeffrey Vander Stoep | baeece6 | 2022-02-08 12:42:33 +0000 | [diff] [blame] | 432 | |
| 433 | // apex_sepolicy.cil is default but optional. |
| 434 | std::string apex_policy_cil_file("/dev/selinux/apex_sepolicy.cil"); |
| 435 | if (access(apex_policy_cil_file.c_str(), F_OK) == -1) { |
| 436 | apex_policy_cil_file.clear(); |
| 437 | } |
Jeff Vander Stoep | 724eda5 | 2019-02-15 12:13:38 -0800 | [diff] [blame] | 438 | const std::string version_as_string = std::to_string(SEPOLICY_VERSION); |
Andreas Huber | c41b838 | 2017-08-18 14:43:52 -0700 | [diff] [blame] | 439 | |
Tom Cherry | c317009 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 440 | // clang-format off |
kaichieh | eef4cd7 | 2017-08-31 22:07:19 +0800 | [diff] [blame] | 441 | std::vector<const char*> compile_args { |
Tom Cherry | c317009 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 442 | "/system/bin/secilc", |
Yi-Yo Chiang | bb77c54 | 2021-09-23 14:14:16 +0000 | [diff] [blame] | 443 | use_userdebug_policy ? *userdebug_plat_sepolicy : plat_policy_cil_file, |
Jeff Vander Stoep | 5e9ba3c | 2017-10-06 17:03:45 -0700 | [diff] [blame] | 444 | "-m", "-M", "true", "-G", "-N", |
Andreas Huber | c41b838 | 2017-08-18 14:43:52 -0700 | [diff] [blame] | 445 | "-c", version_as_string.c_str(), |
Tri Vo | 503f185 | 2019-01-16 11:57:19 -0800 | [diff] [blame] | 446 | plat_mapping_file.c_str(), |
Tom Cherry | c317009 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 447 | "-o", compiled_sepolicy, |
| 448 | // We don't care about file_contexts output by the compiler |
| 449 | "-f", "/sys/fs/selinux/null", // /dev/null is not yet available |
kaichieh | eef4cd7 | 2017-08-31 22:07:19 +0800 | [diff] [blame] | 450 | }; |
Tom Cherry | c317009 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 451 | // clang-format on |
| 452 | |
Jeff Vander Stoep | 0ac51cf | 2019-05-02 14:05:18 -0700 | [diff] [blame] | 453 | if (!plat_compat_cil_file.empty()) { |
| 454 | compile_args.push_back(plat_compat_cil_file.c_str()); |
| 455 | } |
Bowgo Tsai | f016f25 | 2019-08-28 17:56:51 +0800 | [diff] [blame] | 456 | if (!system_ext_policy_cil_file.empty()) { |
| 457 | compile_args.push_back(system_ext_policy_cil_file.c_str()); |
| 458 | } |
| 459 | if (!system_ext_mapping_file.empty()) { |
| 460 | compile_args.push_back(system_ext_mapping_file.c_str()); |
| 461 | } |
Yi-Yo Chiang | 731d247 | 2021-03-23 22:11:13 +0800 | [diff] [blame] | 462 | if (!system_ext_compat_cil_file.empty()) { |
| 463 | compile_args.push_back(system_ext_compat_cil_file.c_str()); |
| 464 | } |
Tri Vo | d3518cf | 2018-12-14 14:25:08 -0800 | [diff] [blame] | 465 | if (!product_policy_cil_file.empty()) { |
| 466 | compile_args.push_back(product_policy_cil_file.c_str()); |
| 467 | } |
Tri Vo | 503f185 | 2019-01-16 11:57:19 -0800 | [diff] [blame] | 468 | if (!product_mapping_file.empty()) { |
| 469 | compile_args.push_back(product_mapping_file.c_str()); |
| 470 | } |
Bowgo Tsai | 069ab5b | 2017-10-18 17:03:20 +0800 | [diff] [blame] | 471 | if (!plat_pub_versioned_cil_file.empty()) { |
| 472 | compile_args.push_back(plat_pub_versioned_cil_file.c_str()); |
kaichieh | eef4cd7 | 2017-08-31 22:07:19 +0800 | [diff] [blame] | 473 | } |
| 474 | if (!vendor_policy_cil_file.empty()) { |
| 475 | compile_args.push_back(vendor_policy_cil_file.c_str()); |
| 476 | } |
| 477 | if (!odm_policy_cil_file.empty()) { |
| 478 | compile_args.push_back(odm_policy_cil_file.c_str()); |
| 479 | } |
Jeffrey Vander Stoep | baeece6 | 2022-02-08 12:42:33 +0000 | [diff] [blame] | 480 | if (!apex_policy_cil_file.empty()) { |
| 481 | compile_args.push_back(apex_policy_cil_file.c_str()); |
| 482 | } |
kaichieh | eef4cd7 | 2017-08-31 22:07:19 +0800 | [diff] [blame] | 483 | compile_args.push_back(nullptr); |
| 484 | |
| 485 | if (!ForkExecveAndWaitForCompletion(compile_args[0], (char**)compile_args.data())) { |
Tom Cherry | c317009 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 486 | unlink(compiled_sepolicy); |
| 487 | return false; |
| 488 | } |
| 489 | unlink(compiled_sepolicy); |
| 490 | |
David Anderson | 491e4da | 2020-12-08 00:21:20 -0800 | [diff] [blame] | 491 | policy_file->fd = std::move(compiled_sepolicy_fd); |
| 492 | policy_file->path = compiled_sepolicy; |
Tom Cherry | c317009 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 493 | return true; |
| 494 | } |
| 495 | |
David Anderson | 491e4da | 2020-12-08 00:21:20 -0800 | [diff] [blame] | 496 | bool OpenMonolithicPolicy(PolicyFile* policy_file) { |
| 497 | static constexpr char kSepolicyFile[] = "/sepolicy"; |
| 498 | |
Nikita Ioffe | a66adf4 | 2023-06-26 14:55:31 +0100 | [diff] [blame] | 499 | LOG(INFO) << "Opening SELinux policy from monolithic file " << kSepolicyFile; |
| 500 | policy_file->fd.reset(open(kSepolicyFile, O_RDONLY | O_CLOEXEC | O_NOFOLLOW)); |
David Anderson | 491e4da | 2020-12-08 00:21:20 -0800 | [diff] [blame] | 501 | if (policy_file->fd < 0) { |
| 502 | PLOG(ERROR) << "Failed to open monolithic SELinux policy"; |
Tom Cherry | c317009 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 503 | return false; |
| 504 | } |
Nikita Ioffe | a66adf4 | 2023-06-26 14:55:31 +0100 | [diff] [blame] | 505 | policy_file->path = kSepolicyFile; |
Tom Cherry | c317009 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 506 | return true; |
| 507 | } |
| 508 | |
Jeffrey Vander Stoep | baeece6 | 2022-02-08 12:42:33 +0000 | [diff] [blame] | 509 | constexpr const char* kSigningCertRelease = |
| 510 | "/system/etc/selinux/com.android.sepolicy.cert-release.der"; |
Jeffrey Vander Stoep | baeece6 | 2022-02-08 12:42:33 +0000 | [diff] [blame] | 511 | const std::string kSepolicyApexMetadataDir = "/metadata/sepolicy/"; |
| 512 | const std::string kSepolicyApexSystemDir = "/system/etc/selinux/apex/"; |
| 513 | const std::string kSepolicyZip = "SEPolicy.zip"; |
| 514 | const std::string kSepolicySignature = "SEPolicy.zip.sig"; |
| 515 | |
| 516 | const std::string kTmpfsDir = "/dev/selinux/"; |
| 517 | |
| 518 | // Files that are deleted after policy is compiled/loaded. |
| 519 | const std::vector<std::string> kApexSepolicyTmp{"apex_sepolicy.cil", "apex_sepolicy.sha256"}; |
| 520 | // Files that need to persist because they are used by userspace processes. |
| 521 | const std::vector<std::string> kApexSepolicy{"apex_file_contexts", "apex_property_contexts", |
| 522 | "apex_service_contexts", "apex_seapp_contexts", |
| 523 | "apex_test"}; |
| 524 | |
Sandro | d019210 | 2022-09-07 14:56:39 +0000 | [diff] [blame] | 525 | Result<void> CreateTmpfsDir() { |
Sandro | 1120f7f | 2022-09-05 15:56:38 +0000 | [diff] [blame] | 526 | mode_t mode = 0744; |
| 527 | struct stat stat_data; |
| 528 | if (stat(kTmpfsDir.c_str(), &stat_data) != 0) { |
| 529 | if (errno != ENOENT) { |
| 530 | return ErrnoError() << "Could not stat " << kTmpfsDir; |
| 531 | } |
| 532 | if (mkdir(kTmpfsDir.c_str(), mode) != 0) { |
| 533 | return ErrnoError() << "Could not mkdir " << kTmpfsDir; |
| 534 | } |
| 535 | } else { |
| 536 | if (!S_ISDIR(stat_data.st_mode)) { |
| 537 | return Error() << kTmpfsDir << " exists and is not a directory."; |
| 538 | } |
Sandro | d019210 | 2022-09-07 14:56:39 +0000 | [diff] [blame] | 539 | LOG(WARNING) << "Directory " << kTmpfsDir << " already exists"; |
Sandro | 1120f7f | 2022-09-05 15:56:38 +0000 | [diff] [blame] | 540 | } |
| 541 | |
| 542 | // Need to manually call chmod because mkdir will create a folder with |
| 543 | // permissions mode & ~umask. |
| 544 | if (chmod(kTmpfsDir.c_str(), mode) != 0) { |
| 545 | return ErrnoError() << "Could not chmod " << kTmpfsDir; |
| 546 | } |
| 547 | |
| 548 | return {}; |
| 549 | } |
| 550 | |
Jeffrey Vander Stoep | baeece6 | 2022-02-08 12:42:33 +0000 | [diff] [blame] | 551 | Result<void> PutFileInTmpfs(ZipArchiveHandle archive, const std::string& fileName) { |
| 552 | ZipEntry entry; |
| 553 | std::string dstPath = kTmpfsDir + fileName; |
| 554 | |
| 555 | int ret = FindEntry(archive, fileName, &entry); |
| 556 | if (ret != 0) { |
| 557 | // All files are optional. If a file doesn't exist, return without error. |
| 558 | return {}; |
| 559 | } |
| 560 | |
| 561 | unique_fd fd(TEMP_FAILURE_RETRY( |
| 562 | open(dstPath.c_str(), O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, S_IRUSR | S_IWUSR))); |
| 563 | if (fd == -1) { |
Sandro | 1120f7f | 2022-09-05 15:56:38 +0000 | [diff] [blame] | 564 | return ErrnoError() << "Failed to open " << dstPath; |
Jeffrey Vander Stoep | baeece6 | 2022-02-08 12:42:33 +0000 | [diff] [blame] | 565 | } |
| 566 | |
Bart Van Assche | aee2ec8 | 2022-12-02 18:48:15 -0800 | [diff] [blame] | 567 | ret = ExtractEntryToFile(archive, &entry, fd.get()); |
Jeffrey Vander Stoep | baeece6 | 2022-02-08 12:42:33 +0000 | [diff] [blame] | 568 | if (ret != 0) { |
| 569 | return Error() << "Failed to extract entry \"" << fileName << "\" (" |
| 570 | << entry.uncompressed_length << " bytes) to \"" << dstPath |
| 571 | << "\": " << ErrorCodeString(ret); |
| 572 | } |
| 573 | |
| 574 | return {}; |
| 575 | } |
| 576 | |
| 577 | Result<void> GetPolicyFromApex(const std::string& dir) { |
| 578 | LOG(INFO) << "Loading APEX Sepolicy from " << dir + kSepolicyZip; |
| 579 | unique_fd fd(open((dir + kSepolicyZip).c_str(), O_RDONLY | O_BINARY | O_CLOEXEC)); |
| 580 | if (fd < 0) { |
| 581 | return ErrnoError() << "Failed to open package " << dir + kSepolicyZip; |
| 582 | } |
| 583 | |
| 584 | ZipArchiveHandle handle; |
| 585 | int ret = OpenArchiveFd(fd.get(), (dir + kSepolicyZip).c_str(), &handle, |
| 586 | /*assume_ownership=*/false); |
| 587 | if (ret < 0) { |
| 588 | return Error() << "Failed to open package " << dir + kSepolicyZip << ": " |
| 589 | << ErrorCodeString(ret); |
| 590 | } |
| 591 | |
| 592 | auto handle_guard = android::base::make_scope_guard([&handle] { CloseArchive(handle); }); |
| 593 | |
Sandro | d019210 | 2022-09-07 14:56:39 +0000 | [diff] [blame] | 594 | auto create = CreateTmpfsDir(); |
Sandro | 1120f7f | 2022-09-05 15:56:38 +0000 | [diff] [blame] | 595 | if (!create.ok()) { |
| 596 | return create.error(); |
| 597 | } |
| 598 | |
Jeffrey Vander Stoep | baeece6 | 2022-02-08 12:42:33 +0000 | [diff] [blame] | 599 | for (const auto& file : kApexSepolicy) { |
| 600 | auto extract = PutFileInTmpfs(handle, file); |
| 601 | if (!extract.ok()) { |
| 602 | return extract.error(); |
| 603 | } |
| 604 | } |
| 605 | for (const auto& file : kApexSepolicyTmp) { |
| 606 | auto extract = PutFileInTmpfs(handle, file); |
| 607 | if (!extract.ok()) { |
| 608 | return extract.error(); |
| 609 | } |
| 610 | } |
| 611 | return {}; |
| 612 | } |
| 613 | |
Jeffrey Vander Stoep | baeece6 | 2022-02-08 12:42:33 +0000 | [diff] [blame] | 614 | Result<void> SepolicyCheckSignature(const std::string& dir) { |
| 615 | std::string signature; |
| 616 | if (!android::base::ReadFileToString(dir + kSepolicySignature, &signature)) { |
| 617 | return ErrnoError() << "Failed to read " << kSepolicySignature; |
| 618 | } |
| 619 | |
| 620 | std::fstream sepolicyZip(dir + kSepolicyZip, std::ios::in | std::ios::binary); |
| 621 | if (!sepolicyZip) { |
| 622 | return Error() << "Failed to open " << kSepolicyZip; |
| 623 | } |
| 624 | sepolicyZip.seekg(0); |
| 625 | std::string sepolicyStr((std::istreambuf_iterator<char>(sepolicyZip)), |
| 626 | std::istreambuf_iterator<char>()); |
| 627 | |
| 628 | auto releaseKey = extractPublicKeyFromX509(kSigningCertRelease); |
| 629 | if (!releaseKey.ok()) { |
| 630 | return releaseKey.error(); |
| 631 | } |
| 632 | |
| 633 | return verifySignature(sepolicyStr, signature, *releaseKey); |
| 634 | } |
| 635 | |
Eric Biggers | 0b2c5cd | 2023-07-05 18:02:09 +0000 | [diff] [blame] | 636 | Result<void> SepolicyVerify(const std::string& dir) { |
Jeffrey Vander Stoep | baeece6 | 2022-02-08 12:42:33 +0000 | [diff] [blame] | 637 | auto sepolicySignature = SepolicyCheckSignature(dir); |
| 638 | if (!sepolicySignature.ok()) { |
| 639 | return Error() << "Apex SEPolicy failed signature check"; |
| 640 | } |
| 641 | return {}; |
| 642 | } |
| 643 | |
| 644 | void CleanupApexSepolicy() { |
| 645 | for (const auto& file : kApexSepolicyTmp) { |
| 646 | std::string path = kTmpfsDir + file; |
| 647 | unlink(path.c_str()); |
| 648 | } |
| 649 | } |
| 650 | |
| 651 | // Updatable sepolicy is shipped within an zip within an APEX. Because |
| 652 | // it needs to be available before Apexes are mounted, apexd copies |
| 653 | // the zip from the APEX and stores it in /metadata/sepolicy. If there is |
| 654 | // no updatable sepolicy in /metadata/sepolicy, then the updatable policy is |
| 655 | // loaded from /system/etc/selinux/apex. Init performs the following |
| 656 | // steps on boot: |
| 657 | // |
| 658 | // 1. Validates the zip by checking its signature against a public key that is |
| 659 | // stored in /system/etc/selinux. |
| 660 | // 2. Extracts files from zip and stores them in /dev/selinux. |
| 661 | // 3. Checks if the apex_sepolicy.sha256 matches the sha256 of precompiled_sepolicy. |
| 662 | // if so, the precompiled sepolicy is used. Otherwise, an on-device compile of the policy |
| 663 | // is used. This is the same flow as on-device compilation of policy for Treble. |
| 664 | // 4. Cleans up files in /dev/selinux which are no longer needed. |
| 665 | // 5. Restorecons the remaining files in /dev/selinux. |
| 666 | // 6. Sets selinux into enforcing mode and continues normal booting. |
| 667 | // |
| 668 | void PrepareApexSepolicy() { |
Jeffrey Vander Stoep | baeece6 | 2022-02-08 12:42:33 +0000 | [diff] [blame] | 669 | // If apex sepolicy zip exists in /metadata/sepolicy, use that, otherwise use version on |
Eric Biggers | 53ed745 | 2023-07-17 20:45:44 +0000 | [diff] [blame^] | 670 | // /system. If neither exists, do nothing. |
| 671 | std::string dir; |
| 672 | if (access((kSepolicyApexMetadataDir + kSepolicyZip).c_str(), F_OK) == 0) { |
| 673 | dir = kSepolicyApexMetadataDir; |
| 674 | } else if (access((kSepolicyApexSystemDir + kSepolicyZip).c_str(), F_OK) == 0) { |
| 675 | dir = kSepolicyApexSystemDir; |
| 676 | } else { |
| 677 | LOG(INFO) << "APEX Sepolicy not found"; |
| 678 | return; |
| 679 | } |
Jeffrey Vander Stoep | baeece6 | 2022-02-08 12:42:33 +0000 | [diff] [blame] | 680 | |
Eric Biggers | 0b2c5cd | 2023-07-05 18:02:09 +0000 | [diff] [blame] | 681 | auto sepolicyVerify = SepolicyVerify(dir); |
Jeffrey Vander Stoep | baeece6 | 2022-02-08 12:42:33 +0000 | [diff] [blame] | 682 | if (!sepolicyVerify.ok()) { |
| 683 | LOG(INFO) << "Error: " << sepolicyVerify.error(); |
| 684 | // If signature verification fails, fall back to version on /system. |
| 685 | // This file doesn't need to be verified because it lives on the system partition which |
| 686 | // is signed and protected by verified boot. |
| 687 | dir = kSepolicyApexSystemDir; |
| 688 | } |
| 689 | |
| 690 | auto apex = GetPolicyFromApex(dir); |
| 691 | if (!apex.ok()) { |
| 692 | // TODO(b/199914227) Make failure fatal. For now continue booting with non-apex sepolicy. |
| 693 | LOG(ERROR) << apex.error(); |
| 694 | } |
| 695 | } |
| 696 | |
David Anderson | 491e4da | 2020-12-08 00:21:20 -0800 | [diff] [blame] | 697 | void ReadPolicy(std::string* policy) { |
| 698 | PolicyFile policy_file; |
Tom Cherry | c317009 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 699 | |
David Anderson | 491e4da | 2020-12-08 00:21:20 -0800 | [diff] [blame] | 700 | bool ok = IsSplitPolicyDevice() ? OpenSplitPolicy(&policy_file) |
| 701 | : OpenMonolithicPolicy(&policy_file); |
| 702 | if (!ok) { |
| 703 | LOG(FATAL) << "Unable to open SELinux policy"; |
Tom Cherry | c317009 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 704 | } |
| 705 | |
David Anderson | 491e4da | 2020-12-08 00:21:20 -0800 | [diff] [blame] | 706 | if (!android::base::ReadFdToString(policy_file.fd, policy)) { |
| 707 | PLOG(FATAL) << "Failed to read policy file: " << policy_file.path; |
| 708 | } |
| 709 | } |
| 710 | |
| 711 | void SelinuxSetEnforcement() { |
Tom Cherry | c317009 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 712 | bool kernel_enforcing = (security_getenforce() == 1); |
| 713 | bool is_enforcing = IsEnforcing(); |
| 714 | if (kernel_enforcing != is_enforcing) { |
| 715 | if (security_setenforce(is_enforcing)) { |
Paul Lawrence | b2c2d69 | 2019-08-30 11:11:44 -0700 | [diff] [blame] | 716 | PLOG(FATAL) << "security_setenforce(" << (is_enforcing ? "true" : "false") |
| 717 | << ") failed"; |
Tom Cherry | c317009 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 718 | } |
| 719 | } |
| 720 | |
Bernie Innocenti | cecebbb | 2020-02-06 03:49:33 +0900 | [diff] [blame] | 721 | if (auto result = WriteFile("/sys/fs/selinux/checkreqprot", "0"); !result.ok()) { |
Tom Cherry | d8db7ab | 2017-08-17 17:28:30 -0700 | [diff] [blame] | 722 | LOG(FATAL) << "Unable to write to /sys/fs/selinux/checkreqprot: " << result.error(); |
Tom Cherry | c317009 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 723 | } |
Tom Cherry | c317009 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 724 | } |
| 725 | |
Tom Cherry | 8180b48 | 2019-08-26 13:57:51 -0700 | [diff] [blame] | 726 | constexpr size_t kKlogMessageSize = 1024; |
| 727 | |
Thiébaud Weksteen | f03dde8 | 2023-04-03 16:53:12 +1000 | [diff] [blame] | 728 | void SelinuxAvcLog(char* buf) { |
Tom Cherry | 8180b48 | 2019-08-26 13:57:51 -0700 | [diff] [blame] | 729 | struct NetlinkMessage { |
| 730 | nlmsghdr hdr; |
| 731 | char buf[kKlogMessageSize]; |
| 732 | } request = {}; |
| 733 | |
| 734 | request.hdr.nlmsg_flags = NLM_F_REQUEST; |
| 735 | request.hdr.nlmsg_type = AUDIT_USER_AVC; |
| 736 | request.hdr.nlmsg_len = sizeof(request); |
| 737 | strlcpy(request.buf, buf, sizeof(request.buf)); |
| 738 | |
| 739 | auto fd = unique_fd{socket(PF_NETLINK, SOCK_RAW | SOCK_CLOEXEC, NETLINK_AUDIT)}; |
| 740 | if (!fd.ok()) { |
| 741 | return; |
| 742 | } |
| 743 | |
Bart Van Assche | aee2ec8 | 2022-12-02 18:48:15 -0800 | [diff] [blame] | 744 | TEMP_FAILURE_RETRY(send(fd.get(), &request, sizeof(request), 0)); |
Tom Cherry | 8180b48 | 2019-08-26 13:57:51 -0700 | [diff] [blame] | 745 | } |
| 746 | |
Tom Cherry | 7bfea3d | 2018-11-06 14:12:05 -0800 | [diff] [blame] | 747 | } // namespace |
| 748 | |
Tom Cherry | c317009 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 749 | void SelinuxRestoreContext() { |
| 750 | LOG(INFO) << "Running restorecon..."; |
| 751 | selinux_android_restorecon("/dev", 0); |
Inseob Kim | 89d6913 | 2022-03-22 21:51:07 +0900 | [diff] [blame] | 752 | selinux_android_restorecon("/dev/console", 0); |
Tom Cherry | c317009 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 753 | selinux_android_restorecon("/dev/kmsg", 0); |
| 754 | if constexpr (WORLD_WRITABLE_KMSG) { |
| 755 | selinux_android_restorecon("/dev/kmsg_debug", 0); |
| 756 | } |
Tom Cherry | 81ae075 | 2018-07-30 16:23:49 -0700 | [diff] [blame] | 757 | selinux_android_restorecon("/dev/null", 0); |
| 758 | selinux_android_restorecon("/dev/ptmx", 0); |
Tom Cherry | c317009 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 759 | selinux_android_restorecon("/dev/socket", 0); |
| 760 | selinux_android_restorecon("/dev/random", 0); |
| 761 | selinux_android_restorecon("/dev/urandom", 0); |
| 762 | selinux_android_restorecon("/dev/__properties__", 0); |
| 763 | |
Tom Cherry | c317009 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 764 | selinux_android_restorecon("/dev/block", SELINUX_ANDROID_RESTORECON_RECURSE); |
David Anderson | 1ff7581 | 2020-11-13 00:31:47 -0800 | [diff] [blame] | 765 | selinux_android_restorecon("/dev/dm-user", SELINUX_ANDROID_RESTORECON_RECURSE); |
Tom Cherry | c317009 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 766 | selinux_android_restorecon("/dev/device-mapper", 0); |
Jiyong Park | 4ba548d | 2019-02-22 16:04:35 +0900 | [diff] [blame] | 767 | |
| 768 | selinux_android_restorecon("/apex", 0); |
Kiyoung Kim | 99df54b | 2019-11-22 16:14:10 +0900 | [diff] [blame] | 769 | |
| 770 | selinux_android_restorecon("/linkerconfig", 0); |
Bowgo Tsai | 196cc58 | 2020-01-20 18:03:58 +0800 | [diff] [blame] | 771 | |
David Anderson | c991f34 | 2020-02-21 17:11:07 -0800 | [diff] [blame] | 772 | // adb remount, snapshot-based updates, and DSUs all create files during |
| 773 | // first-stage init. |
Yifan Hong | d91998f | 2020-02-20 17:54:57 -0800 | [diff] [blame] | 774 | selinux_android_restorecon(SnapshotManager::GetGlobalRollbackIndicatorPath().c_str(), 0); |
David Anderson | 4bb500f | 2020-03-06 18:14:19 -0800 | [diff] [blame] | 775 | selinux_android_restorecon("/metadata/gsi", SELINUX_ANDROID_RESTORECON_RECURSE | |
| 776 | SELINUX_ANDROID_RESTORECON_SKIP_SEHASH); |
Tom Cherry | c317009 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 777 | } |
| 778 | |
Tom Cherry | 74069d1 | 2018-07-20 15:26:25 -0700 | [diff] [blame] | 779 | int SelinuxKlogCallback(int type, const char* fmt, ...) { |
| 780 | android::base::LogSeverity severity = android::base::ERROR; |
| 781 | if (type == SELINUX_WARNING) { |
| 782 | severity = android::base::WARNING; |
| 783 | } else if (type == SELINUX_INFO) { |
| 784 | severity = android::base::INFO; |
| 785 | } |
Tom Cherry | 8180b48 | 2019-08-26 13:57:51 -0700 | [diff] [blame] | 786 | char buf[kKlogMessageSize]; |
Tom Cherry | 74069d1 | 2018-07-20 15:26:25 -0700 | [diff] [blame] | 787 | va_list ap; |
| 788 | va_start(ap, fmt); |
Tom Cherry | 8180b48 | 2019-08-26 13:57:51 -0700 | [diff] [blame] | 789 | int length_written = vsnprintf(buf, sizeof(buf), fmt, ap); |
Tom Cherry | 74069d1 | 2018-07-20 15:26:25 -0700 | [diff] [blame] | 790 | va_end(ap); |
Tom Cherry | 8180b48 | 2019-08-26 13:57:51 -0700 | [diff] [blame] | 791 | if (length_written <= 0) { |
| 792 | return 0; |
| 793 | } |
Thiébaud Weksteen | f03dde8 | 2023-04-03 16:53:12 +1000 | [diff] [blame] | 794 | |
| 795 | // libselinux log messages usually contain a new line character, while |
| 796 | // Android LOG() does not expect it. Remove it to avoid empty lines in |
| 797 | // the log buffers. |
| 798 | size_t str_len = strlen(buf); |
| 799 | if (buf[str_len - 1] == '\n') { |
| 800 | buf[str_len - 1] = '\0'; |
| 801 | } |
| 802 | |
Tom Cherry | 8180b48 | 2019-08-26 13:57:51 -0700 | [diff] [blame] | 803 | if (type == SELINUX_AVC) { |
Thiébaud Weksteen | f03dde8 | 2023-04-03 16:53:12 +1000 | [diff] [blame] | 804 | SelinuxAvcLog(buf); |
Tom Cherry | 8180b48 | 2019-08-26 13:57:51 -0700 | [diff] [blame] | 805 | } else { |
| 806 | android::base::KernelLogger(android::base::MAIN, severity, "selinux", nullptr, 0, buf); |
| 807 | } |
Tom Cherry | 74069d1 | 2018-07-20 15:26:25 -0700 | [diff] [blame] | 808 | return 0; |
| 809 | } |
| 810 | |
Tom Cherry | c317009 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 811 | void SelinuxSetupKernelLogging() { |
| 812 | selinux_callback cb; |
Tom Cherry | 74069d1 | 2018-07-20 15:26:25 -0700 | [diff] [blame] | 813 | cb.func_log = SelinuxKlogCallback; |
Tom Cherry | c317009 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 814 | selinux_set_callback(SELINUX_CB_LOG, cb); |
| 815 | } |
| 816 | |
Tom Cherry | 40acb37 | 2018-08-01 13:41:12 -0700 | [diff] [blame] | 817 | int SelinuxGetVendorAndroidVersion() { |
Nikita Ioffe | a66adf4 | 2023-06-26 14:55:31 +0100 | [diff] [blame] | 818 | if (IsMicrodroid()) { |
| 819 | // As of now Microdroid doesn't have any vendor code. |
| 820 | return __ANDROID_API_FUTURE__; |
| 821 | } |
Tom Cherry | c5cf85d | 2019-07-31 13:59:15 -0700 | [diff] [blame] | 822 | static int vendor_android_version = [] { |
| 823 | if (!IsSplitPolicyDevice()) { |
| 824 | // If this device does not split sepolicy files, it's not a Treble device and therefore, |
| 825 | // we assume it's always on the latest platform. |
| 826 | return __ANDROID_API_FUTURE__; |
| 827 | } |
Logan Chien | 837b2a4 | 2018-05-03 14:33:52 +0800 | [diff] [blame] | 828 | |
Tom Cherry | c5cf85d | 2019-07-31 13:59:15 -0700 | [diff] [blame] | 829 | std::string version; |
| 830 | if (!GetVendorMappingVersion(&version)) { |
| 831 | LOG(FATAL) << "Could not read vendor SELinux version"; |
| 832 | } |
Logan Chien | 837b2a4 | 2018-05-03 14:33:52 +0800 | [diff] [blame] | 833 | |
Tom Cherry | c5cf85d | 2019-07-31 13:59:15 -0700 | [diff] [blame] | 834 | int major_version; |
| 835 | std::string major_version_str(version, 0, version.find('.')); |
| 836 | if (!ParseInt(major_version_str, &major_version)) { |
| 837 | PLOG(FATAL) << "Failed to parse the vendor sepolicy major version " |
| 838 | << major_version_str; |
| 839 | } |
Logan Chien | 837b2a4 | 2018-05-03 14:33:52 +0800 | [diff] [blame] | 840 | |
Tom Cherry | c5cf85d | 2019-07-31 13:59:15 -0700 | [diff] [blame] | 841 | return major_version; |
| 842 | }(); |
| 843 | return vendor_android_version; |
Logan Chien | 837b2a4 | 2018-05-03 14:33:52 +0800 | [diff] [blame] | 844 | } |
| 845 | |
David Anderson | d0ce530 | 2020-03-20 21:47:10 -0700 | [diff] [blame] | 846 | // This is for R system.img/system_ext.img to work on old vendor.img as system_ext.img |
| 847 | // is introduced in R. We mount system_ext in second stage init because the first-stage |
| 848 | // init in boot.img won't be updated in the system-only OTA scenario. |
| 849 | void MountMissingSystemPartitions() { |
| 850 | android::fs_mgr::Fstab fstab; |
| 851 | if (!ReadDefaultFstab(&fstab)) { |
| 852 | LOG(ERROR) << "Could not read default fstab"; |
| 853 | } |
| 854 | |
| 855 | android::fs_mgr::Fstab mounts; |
| 856 | if (!ReadFstabFromFile("/proc/mounts", &mounts)) { |
| 857 | LOG(ERROR) << "Could not read /proc/mounts"; |
| 858 | } |
| 859 | |
| 860 | static const std::vector<std::string> kPartitionNames = {"system_ext", "product"}; |
| 861 | |
| 862 | android::fs_mgr::Fstab extra_fstab; |
| 863 | for (const auto& name : kPartitionNames) { |
| 864 | if (GetEntryForMountPoint(&mounts, "/"s + name)) { |
| 865 | // The partition is already mounted. |
| 866 | continue; |
| 867 | } |
| 868 | |
Lianjun Huang | ccd094c | 2023-02-17 20:22:37 +0800 | [diff] [blame] | 869 | auto system_entries = GetEntriesForMountPoint(&fstab, "/system"); |
| 870 | for (auto& system_entry : system_entries) { |
| 871 | if (!system_entry) { |
| 872 | LOG(ERROR) << "Could not find mount entry for /system"; |
| 873 | break; |
| 874 | } |
| 875 | if (!system_entry->fs_mgr_flags.logical) { |
| 876 | LOG(INFO) << "Skipping mount of " << name << ", system is not dynamic."; |
| 877 | break; |
| 878 | } |
David Anderson | d0ce530 | 2020-03-20 21:47:10 -0700 | [diff] [blame] | 879 | |
Lianjun Huang | ccd094c | 2023-02-17 20:22:37 +0800 | [diff] [blame] | 880 | auto entry = *system_entry; |
| 881 | auto partition_name = name + fs_mgr_get_slot_suffix(); |
| 882 | auto replace_name = "system"s + fs_mgr_get_slot_suffix(); |
David Anderson | d0ce530 | 2020-03-20 21:47:10 -0700 | [diff] [blame] | 883 | |
Lianjun Huang | ccd094c | 2023-02-17 20:22:37 +0800 | [diff] [blame] | 884 | entry.mount_point = "/"s + name; |
| 885 | entry.blk_device = |
David Anderson | d0ce530 | 2020-03-20 21:47:10 -0700 | [diff] [blame] | 886 | android::base::StringReplace(entry.blk_device, replace_name, partition_name, false); |
Lianjun Huang | ccd094c | 2023-02-17 20:22:37 +0800 | [diff] [blame] | 887 | if (!fs_mgr_update_logical_partition(&entry)) { |
| 888 | LOG(ERROR) << "Could not update logical partition"; |
| 889 | continue; |
| 890 | } |
David Anderson | d0ce530 | 2020-03-20 21:47:10 -0700 | [diff] [blame] | 891 | |
Lianjun Huang | ccd094c | 2023-02-17 20:22:37 +0800 | [diff] [blame] | 892 | extra_fstab.emplace_back(std::move(entry)); |
| 893 | } |
David Anderson | d0ce530 | 2020-03-20 21:47:10 -0700 | [diff] [blame] | 894 | } |
| 895 | |
Yi-Yo Chiang | 2057901 | 2021-04-01 20:14:54 +0800 | [diff] [blame] | 896 | SkipMountingPartitions(&extra_fstab, true /* verbose */); |
David Anderson | d0ce530 | 2020-03-20 21:47:10 -0700 | [diff] [blame] | 897 | if (extra_fstab.empty()) { |
| 898 | return; |
| 899 | } |
| 900 | |
| 901 | BlockDevInitializer block_dev_init; |
| 902 | for (auto& entry : extra_fstab) { |
| 903 | if (access(entry.blk_device.c_str(), F_OK) != 0) { |
| 904 | auto block_dev = android::base::Basename(entry.blk_device); |
| 905 | if (!block_dev_init.InitDmDevice(block_dev)) { |
| 906 | LOG(ERROR) << "Failed to find device-mapper node: " << block_dev; |
| 907 | continue; |
| 908 | } |
| 909 | } |
| 910 | if (fs_mgr_do_mount_one(entry)) { |
| 911 | LOG(ERROR) << "Could not mount " << entry.mount_point; |
| 912 | } |
| 913 | } |
| 914 | } |
| 915 | |
David Anderson | 491e4da | 2020-12-08 00:21:20 -0800 | [diff] [blame] | 916 | static void LoadSelinuxPolicy(std::string& policy) { |
| 917 | LOG(INFO) << "Loading SELinux policy"; |
| 918 | |
| 919 | set_selinuxmnt("/sys/fs/selinux"); |
| 920 | if (security_load_policy(policy.data(), policy.size()) < 0) { |
| 921 | PLOG(FATAL) << "SELinux: Could not load policy"; |
| 922 | } |
| 923 | } |
| 924 | |
Nikita Ioffe | a66adf4 | 2023-06-26 14:55:31 +0100 | [diff] [blame] | 925 | // Encapsulates steps to load SELinux policy in Microdroid. |
| 926 | // So far the process is very straightforward - just load the precompiled policy from /system. |
| 927 | void LoadSelinuxPolicyMicrodroid() { |
| 928 | constexpr const char kMicrodroidPrecompiledSepolicy[] = |
| 929 | "/system/etc/selinux/microdroid_precompiled_sepolicy"; |
| 930 | |
| 931 | LOG(INFO) << "Opening SELinux policy from " << kMicrodroidPrecompiledSepolicy; |
| 932 | unique_fd policy_fd(open(kMicrodroidPrecompiledSepolicy, O_RDONLY | O_CLOEXEC | O_NOFOLLOW)); |
| 933 | if (policy_fd < 0) { |
| 934 | PLOG(FATAL) << "Failed to open " << kMicrodroidPrecompiledSepolicy; |
| 935 | } |
| 936 | |
| 937 | std::string policy; |
| 938 | if (!android::base::ReadFdToString(policy_fd, &policy)) { |
| 939 | PLOG(FATAL) << "Failed to read policy file: " << kMicrodroidPrecompiledSepolicy; |
| 940 | } |
| 941 | |
| 942 | LoadSelinuxPolicy(policy); |
| 943 | } |
| 944 | |
David Anderson | 491e4da | 2020-12-08 00:21:20 -0800 | [diff] [blame] | 945 | // The SELinux setup process is carefully orchestrated around snapuserd. Policy |
| 946 | // must be loaded off dynamic partitions, and during an OTA, those partitions |
| 947 | // cannot be read without snapuserd. But, with kernel-privileged snapuserd |
| 948 | // running, loading the policy will immediately trigger audits. |
| 949 | // |
| 950 | // We use a five-step process to address this: |
| 951 | // (1) Read the policy into a string, with snapuserd running. |
| 952 | // (2) Rewrite the snapshot device-mapper tables, to generate new dm-user |
| 953 | // devices and to flush I/O. |
| 954 | // (3) Kill snapuserd, which no longer has any dm-user devices to attach to. |
| 955 | // (4) Load the sepolicy and issue critical restorecons in /dev, carefully |
| 956 | // avoiding anything that would read from /system. |
| 957 | // (5) Re-launch snapuserd and attach it to the dm-user devices from step (2). |
| 958 | // |
| 959 | // After this sequence, it is safe to enable enforcing mode and continue booting. |
Nikita Ioffe | a66adf4 | 2023-06-26 14:55:31 +0100 | [diff] [blame] | 960 | void LoadSelinuxPolicyAndroid() { |
David Anderson | d0ce530 | 2020-03-20 21:47:10 -0700 | [diff] [blame] | 961 | MountMissingSystemPartitions(); |
| 962 | |
David Anderson | 491e4da | 2020-12-08 00:21:20 -0800 | [diff] [blame] | 963 | LOG(INFO) << "Opening SELinux policy"; |
| 964 | |
Jeffrey Vander Stoep | baeece6 | 2022-02-08 12:42:33 +0000 | [diff] [blame] | 965 | PrepareApexSepolicy(); |
| 966 | |
David Anderson | 491e4da | 2020-12-08 00:21:20 -0800 | [diff] [blame] | 967 | // Read the policy before potentially killing snapuserd. |
| 968 | std::string policy; |
| 969 | ReadPolicy(&policy); |
Jeffrey Vander Stoep | baeece6 | 2022-02-08 12:42:33 +0000 | [diff] [blame] | 970 | CleanupApexSepolicy(); |
David Anderson | 491e4da | 2020-12-08 00:21:20 -0800 | [diff] [blame] | 971 | |
| 972 | auto snapuserd_helper = SnapuserdSelinuxHelper::CreateIfNeeded(); |
| 973 | if (snapuserd_helper) { |
Nikita Ioffe | a66adf4 | 2023-06-26 14:55:31 +0100 | [diff] [blame] | 974 | // Kill the old snapused to avoid audit messages. After this we cannot read from /system |
| 975 | // (or other dynamic partitions) until we call FinishTransition(). |
David Anderson | 491e4da | 2020-12-08 00:21:20 -0800 | [diff] [blame] | 976 | snapuserd_helper->StartTransition(); |
| 977 | } |
| 978 | |
| 979 | LoadSelinuxPolicy(policy); |
| 980 | |
| 981 | if (snapuserd_helper) { |
| 982 | // Before enforcing, finish the pending snapuserd transition. |
| 983 | snapuserd_helper->FinishTransition(); |
| 984 | snapuserd_helper = nullptr; |
| 985 | } |
| 986 | |
Jeffrey Vander Stoep | baeece6 | 2022-02-08 12:42:33 +0000 | [diff] [blame] | 987 | // This restorecon is intentionally done before SelinuxSetEnforcement because the permissions |
| 988 | // needed to transition files from tmpfs to *_contexts_file context should not be granted to |
| 989 | // any process after selinux is set into enforcing mode. |
| 990 | if (selinux_android_restorecon("/dev/selinux/", SELINUX_ANDROID_RESTORECON_RECURSE) == -1) { |
| 991 | PLOG(FATAL) << "restorecon failed of /dev/selinux failed"; |
| 992 | } |
Nikita Ioffe | a66adf4 | 2023-06-26 14:55:31 +0100 | [diff] [blame] | 993 | } |
| 994 | |
| 995 | int SetupSelinux(char** argv) { |
| 996 | SetStdioToDevNull(argv); |
| 997 | InitKernelLogging(argv); |
| 998 | |
| 999 | if (REBOOT_BOOTLOADER_ON_PANIC) { |
| 1000 | InstallRebootSignalHandlers(); |
| 1001 | } |
| 1002 | |
| 1003 | boot_clock::time_point start_time = boot_clock::now(); |
| 1004 | |
| 1005 | SelinuxSetupKernelLogging(); |
| 1006 | |
| 1007 | // TODO(b/287206497): refactor into different headers to only include what we need. |
| 1008 | if (IsMicrodroid()) { |
| 1009 | LoadSelinuxPolicyMicrodroid(); |
| 1010 | } else { |
| 1011 | LoadSelinuxPolicyAndroid(); |
| 1012 | } |
Jeffrey Vander Stoep | baeece6 | 2022-02-08 12:42:33 +0000 | [diff] [blame] | 1013 | |
David Anderson | 491e4da | 2020-12-08 00:21:20 -0800 | [diff] [blame] | 1014 | SelinuxSetEnforcement(); |
Tom Cherry | 7bfea3d | 2018-11-06 14:12:05 -0800 | [diff] [blame] | 1015 | |
| 1016 | // We're in the kernel domain and want to transition to the init domain. File systems that |
| 1017 | // store SELabels in their xattrs, such as ext4 do not need an explicit restorecon here, |
| 1018 | // but other file systems do. In particular, this is needed for ramdisks such as the |
| 1019 | // recovery image for A/B devices. |
| 1020 | if (selinux_android_restorecon("/system/bin/init", 0) == -1) { |
| 1021 | PLOG(FATAL) << "restorecon failed of /system/bin/init failed"; |
| 1022 | } |
| 1023 | |
Mark Salyzyn | 44505ec | 2019-05-08 12:44:50 -0700 | [diff] [blame] | 1024 | 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] | 1025 | |
Tom Cherry | 7bfea3d | 2018-11-06 14:12:05 -0800 | [diff] [blame] | 1026 | const char* path = "/system/bin/init"; |
| 1027 | const char* args[] = {path, "second_stage", nullptr}; |
| 1028 | execv(path, const_cast<char**>(args)); |
| 1029 | |
| 1030 | // execv() only returns if an error happened, in which case we |
| 1031 | // panic and never return from this function. |
| 1032 | PLOG(FATAL) << "execv(\"" << path << "\") failed"; |
| 1033 | |
| 1034 | return 1; |
| 1035 | } |
| 1036 | |
Tom Cherry | c317009 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 1037 | } // namespace init |
| 1038 | } // namespace android |