Andreas Gampe | 01ad598 | 2016-03-09 16:27:29 -0800 | [diff] [blame] | 1 | /* |
| 2 | ** Copyright 2016, 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 | |
Andreas Gampe | d089ca1 | 2016-06-27 14:25:30 -0700 | [diff] [blame] | 17 | #include <fcntl.h> |
Andreas Gampe | 01ad598 | 2016-03-09 16:27:29 -0800 | [diff] [blame] | 18 | #include <linux/unistd.h> |
| 19 | #include <sys/mount.h> |
Roland Levillain | c19c604 | 2018-12-18 12:15:12 +0000 | [diff] [blame] | 20 | #include <sys/stat.h> |
Andreas Gampe | 01ad598 | 2016-03-09 16:27:29 -0800 | [diff] [blame] | 21 | #include <sys/wait.h> |
| 22 | |
Alex Light | 53e9438 | 2021-03-09 16:39:55 -0800 | [diff] [blame^] | 23 | #include <array> |
Alex Light | cf7e6de | 2021-03-04 10:20:18 -0800 | [diff] [blame] | 24 | #include <fstream> |
Andreas Gampe | d089ca1 | 2016-06-27 14:25:30 -0700 | [diff] [blame] | 25 | #include <sstream> |
| 26 | |
Alex Light | cf7e6de | 2021-03-04 10:20:18 -0800 | [diff] [blame] | 27 | #include <android-base/file.h> |
Andreas Gampe | 01ad598 | 2016-03-09 16:27:29 -0800 | [diff] [blame] | 28 | #include <android-base/logging.h> |
| 29 | #include <android-base/macros.h> |
| 30 | #include <android-base/stringprintf.h> |
Alex Light | cf7e6de | 2021-03-04 10:20:18 -0800 | [diff] [blame] | 31 | #include <android-base/unique_fd.h> |
Andreas Gampe | e90127d | 2019-03-22 11:21:34 -0700 | [diff] [blame] | 32 | #include <libdm/dm.h> |
Roland Levillain | c19c604 | 2018-12-18 12:15:12 +0000 | [diff] [blame] | 33 | #include <selinux/android.h> |
| 34 | |
Jeff Sharkey | 0274c97 | 2016-12-06 09:32:04 -0700 | [diff] [blame] | 35 | #include "installd_constants.h" |
| 36 | #include "otapreopt_utils.h" |
Andreas Gampe | 548bdb9 | 2016-06-02 17:56:45 -0700 | [diff] [blame] | 37 | |
Andreas Gampe | 01ad598 | 2016-03-09 16:27:29 -0800 | [diff] [blame] | 38 | #ifndef LOG_TAG |
| 39 | #define LOG_TAG "otapreopt" |
| 40 | #endif |
| 41 | |
| 42 | using android::base::StringPrintf; |
| 43 | |
| 44 | namespace android { |
| 45 | namespace installd { |
| 46 | |
Andreas Gampe | d089ca1 | 2016-06-27 14:25:30 -0700 | [diff] [blame] | 47 | static void CloseDescriptor(int fd) { |
| 48 | if (fd >= 0) { |
| 49 | int result = close(fd); |
| 50 | UNUSED(result); // Ignore result. Printing to logcat will open a new descriptor |
| 51 | // that we do *not* want. |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | static void CloseDescriptor(const char* descriptor_string) { |
| 56 | int fd = -1; |
| 57 | std::istringstream stream(descriptor_string); |
| 58 | stream >> fd; |
| 59 | if (!stream.fail()) { |
| 60 | CloseDescriptor(fd); |
| 61 | } |
| 62 | } |
| 63 | |
Alex Light | 53e9438 | 2021-03-09 16:39:55 -0800 | [diff] [blame^] | 64 | static void ActivateApexPackages() { |
| 65 | std::vector<std::string> apexd_cmd{"/system/bin/apexd", "--otachroot-bootstrap"}; |
| 66 | std::string apexd_error_msg; |
Roland Levillain | 6520cca | 2019-02-01 13:15:58 +0000 | [diff] [blame] | 67 | |
Alex Light | 53e9438 | 2021-03-09 16:39:55 -0800 | [diff] [blame^] | 68 | bool exec_result = Exec(apexd_cmd, &apexd_error_msg); |
| 69 | if (!exec_result) { |
| 70 | PLOG(ERROR) << "Running otapreopt failed: " << apexd_error_msg; |
| 71 | exit(220); |
Roland Levillain | 6520cca | 2019-02-01 13:15:58 +0000 | [diff] [blame] | 72 | } |
| 73 | } |
| 74 | |
Andreas Gampe | 54b62c9 | 2019-03-21 11:34:19 -0700 | [diff] [blame] | 75 | static void TryExtraMount(const char* name, const char* slot, const char* target) { |
Andreas Gampe | e90127d | 2019-03-22 11:21:34 -0700 | [diff] [blame] | 76 | std::string partition_name = StringPrintf("%s%s", name, slot); |
| 77 | |
| 78 | // See whether update_engine mounted a logical partition. |
| 79 | { |
| 80 | auto& dm = dm::DeviceMapper::Instance(); |
| 81 | if (dm.GetState(partition_name) != dm::DmDeviceState::INVALID) { |
| 82 | std::string path; |
| 83 | if (dm.GetDmDevicePathByName(partition_name, &path)) { |
| 84 | int mount_result = mount(path.c_str(), |
| 85 | target, |
| 86 | "ext4", |
| 87 | MS_RDONLY, |
| 88 | /* data */ nullptr); |
| 89 | if (mount_result == 0) { |
| 90 | return; |
| 91 | } |
| 92 | } |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | // Fall back and attempt a direct mount. |
| 97 | std::string block_device = StringPrintf("/dev/block/by-name/%s", partition_name.c_str()); |
Andreas Gampe | 54b62c9 | 2019-03-21 11:34:19 -0700 | [diff] [blame] | 98 | int mount_result = mount(block_device.c_str(), |
| 99 | target, |
| 100 | "ext4", |
| 101 | MS_RDONLY, |
| 102 | /* data */ nullptr); |
| 103 | UNUSED(mount_result); |
| 104 | } |
| 105 | |
Andreas Gampe | d089ca1 | 2016-06-27 14:25:30 -0700 | [diff] [blame] | 106 | // Entry for otapreopt_chroot. Expected parameters are: |
| 107 | // [cmd] [status-fd] [target-slot] "dexopt" [dexopt-params] |
| 108 | // The file descriptor denoted by status-fd will be closed. The rest of the parameters will |
| 109 | // be passed on to otapreopt in the chroot. |
Andreas Gampe | 01ad598 | 2016-03-09 16:27:29 -0800 | [diff] [blame] | 110 | static int otapreopt_chroot(const int argc, char **arg) { |
Zach Riggle | 318853a | 2018-01-18 18:34:04 -0600 | [diff] [blame] | 111 | // Validate arguments |
| 112 | // We need the command, status channel and target slot, at a minimum. |
| 113 | if(argc < 3) { |
| 114 | PLOG(ERROR) << "Not enough arguments."; |
| 115 | exit(208); |
| 116 | } |
Andreas Gampe | d089ca1 | 2016-06-27 14:25:30 -0700 | [diff] [blame] | 117 | // Close all file descriptors. They are coming from the caller, we do not want to pass them |
| 118 | // on across our fork/exec into a different domain. |
| 119 | // 1) Default descriptors. |
| 120 | CloseDescriptor(STDIN_FILENO); |
| 121 | CloseDescriptor(STDOUT_FILENO); |
| 122 | CloseDescriptor(STDERR_FILENO); |
| 123 | // 2) The status channel. |
| 124 | CloseDescriptor(arg[1]); |
| 125 | |
Andreas Gampe | 01ad598 | 2016-03-09 16:27:29 -0800 | [diff] [blame] | 126 | // We need to run the otapreopt tool from the postinstall partition. As such, set up a |
| 127 | // mount namespace and change root. |
| 128 | |
| 129 | // Create our own mount namespace. |
| 130 | if (unshare(CLONE_NEWNS) != 0) { |
| 131 | PLOG(ERROR) << "Failed to unshare() for otapreopt."; |
| 132 | exit(200); |
| 133 | } |
| 134 | |
| 135 | // Make postinstall private, so that our changes don't propagate. |
| 136 | if (mount("", "/postinstall", nullptr, MS_PRIVATE, nullptr) != 0) { |
| 137 | PLOG(ERROR) << "Failed to mount private."; |
| 138 | exit(201); |
| 139 | } |
| 140 | |
| 141 | // Bind mount necessary directories. |
| 142 | constexpr const char* kBindMounts[] = { |
| 143 | "/data", "/dev", "/proc", "/sys" |
| 144 | }; |
| 145 | for (size_t i = 0; i < arraysize(kBindMounts); ++i) { |
| 146 | std::string trg = StringPrintf("/postinstall%s", kBindMounts[i]); |
| 147 | if (mount(kBindMounts[i], trg.c_str(), nullptr, MS_BIND, nullptr) != 0) { |
| 148 | PLOG(ERROR) << "Failed to bind-mount " << kBindMounts[i]; |
| 149 | exit(202); |
| 150 | } |
| 151 | } |
| 152 | |
Andreas Gampe | fd12eda | 2016-07-12 09:47:17 -0700 | [diff] [blame] | 153 | // Try to mount the vendor partition. update_engine doesn't do this for us, but we |
| 154 | // want it for vendor APKs. |
| 155 | // Notes: |
| 156 | // 1) We pretty much guess a name here and hope to find the partition by name. |
| 157 | // It is just as complicated and brittle to scan /proc/mounts. But this requires |
| 158 | // validating the target-slot so as not to try to mount some totally random path. |
| 159 | // 2) We're in a mount namespace here, so when we die, this will be cleaned up. |
| 160 | // 3) Ignore errors. Printing anything at this stage will open a file descriptor |
| 161 | // for logging. |
| 162 | if (!ValidateTargetSlotSuffix(arg[2])) { |
| 163 | LOG(ERROR) << "Target slot suffix not legal: " << arg[2]; |
| 164 | exit(207); |
| 165 | } |
Andreas Gampe | 54b62c9 | 2019-03-21 11:34:19 -0700 | [diff] [blame] | 166 | TryExtraMount("vendor", arg[2], "/postinstall/vendor"); |
Andreas Gampe | b87a1c7 | 2018-04-13 17:42:23 -0700 | [diff] [blame] | 167 | |
| 168 | // Try to mount the product partition. update_engine doesn't do this for us, but we |
| 169 | // want it for product APKs. Same notes as vendor above. |
Andreas Gampe | 54b62c9 | 2019-03-21 11:34:19 -0700 | [diff] [blame] | 170 | TryExtraMount("product", arg[2], "/postinstall/product"); |
Andreas Gampe | fd12eda | 2016-07-12 09:47:17 -0700 | [diff] [blame] | 171 | |
Alex Light | 2b307a0 | 2021-03-03 18:35:47 -0800 | [diff] [blame] | 172 | constexpr const char* kPostInstallLinkerconfig = "/postinstall/linkerconfig"; |
| 173 | // Try to mount /postinstall/linkerconfig. we will set it up after performing the chroot |
| 174 | if (mount("tmpfs", kPostInstallLinkerconfig, "tmpfs", 0, nullptr) != 0) { |
| 175 | PLOG(ERROR) << "Failed to mount a tmpfs for " << kPostInstallLinkerconfig; |
| 176 | exit(215); |
| 177 | } |
| 178 | |
Roland Levillain | c19c604 | 2018-12-18 12:15:12 +0000 | [diff] [blame] | 179 | // Setup APEX mount point and its security context. |
| 180 | static constexpr const char* kPostinstallApexDir = "/postinstall/apex"; |
| 181 | // The following logic is similar to the one in system/core/rootdir/init.rc: |
| 182 | // |
| 183 | // mount tmpfs tmpfs /apex nodev noexec nosuid |
| 184 | // chmod 0755 /apex |
| 185 | // chown root root /apex |
| 186 | // restorecon /apex |
| 187 | // |
Roland Levillain | 8d27681 | 2019-01-24 10:51:30 +0000 | [diff] [blame] | 188 | // except we perform the `restorecon` step just after mounting the tmpfs |
| 189 | // filesystem in /postinstall/apex, so that this directory is correctly |
| 190 | // labeled (with type `postinstall_apex_mnt_dir`) and may be manipulated in |
| 191 | // following operations (`chmod`, `chown`, etc.) following policies |
| 192 | // restricted to `postinstall_apex_mnt_dir`: |
| 193 | // |
| 194 | // mount tmpfs tmpfs /postinstall/apex nodev noexec nosuid |
| 195 | // restorecon /postinstall/apex |
| 196 | // chmod 0755 /postinstall/apex |
| 197 | // chown root root /postinstall/apex |
| 198 | // |
Roland Levillain | c19c604 | 2018-12-18 12:15:12 +0000 | [diff] [blame] | 199 | if (mount("tmpfs", kPostinstallApexDir, "tmpfs", MS_NODEV | MS_NOEXEC | MS_NOSUID, nullptr) |
| 200 | != 0) { |
| 201 | PLOG(ERROR) << "Failed to mount tmpfs in " << kPostinstallApexDir; |
| 202 | exit(209); |
| 203 | } |
Roland Levillain | 8d27681 | 2019-01-24 10:51:30 +0000 | [diff] [blame] | 204 | if (selinux_android_restorecon(kPostinstallApexDir, 0) < 0) { |
| 205 | PLOG(ERROR) << "Failed to restorecon " << kPostinstallApexDir; |
| 206 | exit(214); |
| 207 | } |
Roland Levillain | c19c604 | 2018-12-18 12:15:12 +0000 | [diff] [blame] | 208 | if (chmod(kPostinstallApexDir, 0755) != 0) { |
| 209 | PLOG(ERROR) << "Failed to chmod " << kPostinstallApexDir << " to 0755"; |
| 210 | exit(210); |
| 211 | } |
| 212 | if (chown(kPostinstallApexDir, 0, 0) != 0) { |
| 213 | PLOG(ERROR) << "Failed to chown " << kPostinstallApexDir << " to root:root"; |
| 214 | exit(211); |
| 215 | } |
Roland Levillain | c19c604 | 2018-12-18 12:15:12 +0000 | [diff] [blame] | 216 | |
Andreas Gampe | 01ad598 | 2016-03-09 16:27:29 -0800 | [diff] [blame] | 217 | // Chdir into /postinstall. |
| 218 | if (chdir("/postinstall") != 0) { |
| 219 | PLOG(ERROR) << "Unable to chdir into /postinstall."; |
| 220 | exit(203); |
| 221 | } |
| 222 | |
| 223 | // Make /postinstall the root in our mount namespace. |
| 224 | if (chroot(".") != 0) { |
| 225 | PLOG(ERROR) << "Failed to chroot"; |
| 226 | exit(204); |
| 227 | } |
| 228 | |
| 229 | if (chdir("/") != 0) { |
| 230 | PLOG(ERROR) << "Unable to chdir into /."; |
| 231 | exit(205); |
| 232 | } |
| 233 | |
Roland Levillain | c19c604 | 2018-12-18 12:15:12 +0000 | [diff] [blame] | 234 | // Try to mount APEX packages in "/apex" in the chroot dir. We need at least |
Martin Stjernholm | f4caaa0 | 2019-07-17 22:14:14 +0100 | [diff] [blame] | 235 | // the ART APEX, as it is required by otapreopt to run dex2oat. |
Alex Light | 53e9438 | 2021-03-09 16:39:55 -0800 | [diff] [blame^] | 236 | ActivateApexPackages(); |
Roland Levillain | c19c604 | 2018-12-18 12:15:12 +0000 | [diff] [blame] | 237 | |
Martin Stjernholm | f4caaa0 | 2019-07-17 22:14:14 +0100 | [diff] [blame] | 238 | // Check that an ART APEX has been activated; clean up and exit |
Roland Levillain | 8a8ca15 | 2019-07-11 18:48:05 +0100 | [diff] [blame] | 239 | // early otherwise. |
Alex Light | 2b307a0 | 2021-03-03 18:35:47 -0800 | [diff] [blame] | 240 | static constexpr const std::string_view kRequiredApexs[] = { |
| 241 | "com.android.art", |
| 242 | "com.android.runtime", |
| 243 | }; |
Alex Light | 53e9438 | 2021-03-09 16:39:55 -0800 | [diff] [blame^] | 244 | std::array<bool, arraysize(kRequiredApexs)> found_apexs{ false, false }; |
| 245 | DIR* apex_dir = opendir("/apex"); |
| 246 | if (apex_dir == nullptr) { |
| 247 | PLOG(ERROR) << "unable to open /apex"; |
| 248 | exit(220); |
| 249 | } |
| 250 | for (dirent* entry = readdir(apex_dir); entry != nullptr; entry = readdir(apex_dir)) { |
| 251 | for (int i = 0; i < found_apexs.size(); i++) { |
| 252 | if (kRequiredApexs[i] == std::string_view(entry->d_name)) { |
| 253 | found_apexs[i] = true; |
| 254 | break; |
| 255 | } |
Alex Light | 2b307a0 | 2021-03-03 18:35:47 -0800 | [diff] [blame] | 256 | } |
| 257 | } |
Alex Light | 53e9438 | 2021-03-09 16:39:55 -0800 | [diff] [blame^] | 258 | closedir(apex_dir); |
| 259 | auto it = std::find(found_apexs.cbegin(), found_apexs.cend(), false); |
| 260 | if (it != found_apexs.cend()) { |
| 261 | LOG(ERROR) << "No activated " << kRequiredApexs[std::distance(found_apexs.cbegin(), it)] |
| 262 | << " package!"; |
| 263 | exit(221); |
| 264 | } |
Alex Light | 2b307a0 | 2021-03-03 18:35:47 -0800 | [diff] [blame] | 265 | |
| 266 | // Setup /linkerconfig. Doing it after the chroot means it doesn't need its own category |
| 267 | if (selinux_android_restorecon("/linkerconfig", 0) < 0) { |
| 268 | PLOG(ERROR) << "Failed to restorecon /linkerconfig"; |
| 269 | exit(219); |
| 270 | } |
| 271 | std::vector<std::string> linkerconfig_cmd{"/apex/com.android.runtime/bin/linkerconfig", |
| 272 | "--target", "/linkerconfig"}; |
| 273 | std::string linkerconfig_error_msg; |
| 274 | bool linkerconfig_exec_result = Exec(linkerconfig_cmd, &linkerconfig_error_msg); |
| 275 | if (!linkerconfig_exec_result) { |
| 276 | LOG(ERROR) << "Running linkerconfig failed: " << linkerconfig_error_msg; |
| 277 | exit(218); |
Roland Levillain | 8a8ca15 | 2019-07-11 18:48:05 +0100 | [diff] [blame] | 278 | } |
| 279 | |
Andreas Gampe | 01ad598 | 2016-03-09 16:27:29 -0800 | [diff] [blame] | 280 | // Now go on and run otapreopt. |
| 281 | |
Roland Levillain | 94b4180 | 2019-01-18 11:56:50 +0000 | [diff] [blame] | 282 | // Incoming: cmd + status-fd + target-slot + cmd... | Incoming | = argc |
| 283 | // Outgoing: cmd + target-slot + cmd... | Outgoing | = argc - 1 |
| 284 | std::vector<std::string> cmd; |
| 285 | cmd.reserve(argc); |
| 286 | cmd.push_back("/system/bin/otapreopt"); |
Andreas Gampe | d089ca1 | 2016-06-27 14:25:30 -0700 | [diff] [blame] | 287 | |
| 288 | // The first parameter is the status file descriptor, skip. |
Roland Levillain | 94b4180 | 2019-01-18 11:56:50 +0000 | [diff] [blame] | 289 | for (size_t i = 2; i < static_cast<size_t>(argc); ++i) { |
| 290 | cmd.push_back(arg[i]); |
Andreas Gampe | d089ca1 | 2016-06-27 14:25:30 -0700 | [diff] [blame] | 291 | } |
Andreas Gampe | 01ad598 | 2016-03-09 16:27:29 -0800 | [diff] [blame] | 292 | |
Roland Levillain | 94b4180 | 2019-01-18 11:56:50 +0000 | [diff] [blame] | 293 | // Fork and execute otapreopt in its own process. |
| 294 | std::string error_msg; |
| 295 | bool exec_result = Exec(cmd, &error_msg); |
| 296 | if (!exec_result) { |
| 297 | LOG(ERROR) << "Running otapreopt failed: " << error_msg; |
| 298 | } |
| 299 | |
Roland Levillain | 94b4180 | 2019-01-18 11:56:50 +0000 | [diff] [blame] | 300 | if (!exec_result) { |
| 301 | exit(213); |
| 302 | } |
| 303 | |
| 304 | return 0; |
Andreas Gampe | 01ad598 | 2016-03-09 16:27:29 -0800 | [diff] [blame] | 305 | } |
| 306 | |
| 307 | } // namespace installd |
| 308 | } // namespace android |
| 309 | |
| 310 | int main(const int argc, char *argv[]) { |
| 311 | return android::installd::otapreopt_chroot(argc, argv); |
| 312 | } |