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