| 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> |
| Roland Levillain | c19c604 | 2018-12-18 12:15:12 +0000 | [diff] [blame] | 28 | #include <selinux/android.h> |
| 29 | |
| 30 | #include <apexd.h> |
| Andreas Gampe | 01ad598 | 2016-03-09 16:27:29 -0800 | [diff] [blame] | 31 | |
| Jeff Sharkey | 0274c97 | 2016-12-06 09:32:04 -0700 | [diff] [blame] | 32 | #include "installd_constants.h" |
| 33 | #include "otapreopt_utils.h" |
| Andreas Gampe | 548bdb9 | 2016-06-02 17:56:45 -0700 | [diff] [blame] | 34 | |
| Andreas Gampe | 01ad598 | 2016-03-09 16:27:29 -0800 | [diff] [blame] | 35 | #ifndef LOG_TAG |
| 36 | #define LOG_TAG "otapreopt" |
| 37 | #endif |
| 38 | |
| 39 | using android::base::StringPrintf; |
| 40 | |
| 41 | namespace android { |
| 42 | namespace installd { |
| 43 | |
| Roland Levillain | c3265ae | 2019-01-31 18:30:17 +0000 | [diff] [blame] | 44 | // Configuration for bind-mounted Bionic artifacts. |
| 45 | |
| 46 | static constexpr const char* kLinkerMountPoint = "/bionic/bin/linker"; |
| 47 | static constexpr const char* kRuntimeLinkerPath = "/apex/com.android.runtime/bin/linker"; |
| 48 | |
| 49 | static constexpr const char* kBionicLibsMountPointDir = "/bionic/lib/"; |
| 50 | static constexpr const char* kRuntimeBionicLibsDir = "/apex/com.android.runtime/lib/bionic/"; |
| 51 | |
| 52 | static constexpr const char* kLinkerMountPoint64 = "/bionic/bin/linker64"; |
| 53 | static constexpr const char* kRuntimeLinkerPath64 = "/apex/com.android.runtime/bin/linker64"; |
| 54 | |
| 55 | static constexpr const char* kBionicLibsMountPointDir64 = "/bionic/lib64/"; |
| 56 | static constexpr const char* kRuntimeBionicLibsDir64 = "/apex/com.android.runtime/lib64/bionic/"; |
| 57 | |
| 58 | static const std::vector<std::string> kBionicLibFileNames = {"libc.so", "libm.so", "libdl.so"}; |
| 59 | |
| 60 | |
| Andreas Gampe | d089ca1 | 2016-06-27 14:25:30 -0700 | [diff] [blame] | 61 | static void CloseDescriptor(int fd) { |
| 62 | if (fd >= 0) { |
| 63 | int result = close(fd); |
| 64 | UNUSED(result); // Ignore result. Printing to logcat will open a new descriptor |
| 65 | // that we do *not* want. |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | static void CloseDescriptor(const char* descriptor_string) { |
| 70 | int fd = -1; |
| 71 | std::istringstream stream(descriptor_string); |
| 72 | stream >> fd; |
| 73 | if (!stream.fail()) { |
| 74 | CloseDescriptor(fd); |
| 75 | } |
| 76 | } |
| 77 | |
| Roland Levillain | 6520cca | 2019-02-01 13:15:58 +0000 | [diff] [blame] | 78 | static std::vector<apex::ApexFile> ActivateApexPackages() { |
| 79 | // The logic here is (partially) copied and adapted from |
| 80 | // system/apex/apexd/apexd_main.cpp. |
| 81 | // |
| 82 | // Only scan the APEX directory under /system (within the chroot dir). |
| Roland Levillain | 6520cca | 2019-02-01 13:15:58 +0000 | [diff] [blame] | 83 | apex::scanPackagesDirAndActivate(apex::kApexPackageSystemDir); |
| 84 | return apex::getActivePackages(); |
| 85 | } |
| 86 | |
| 87 | static void DeactivateApexPackages(const std::vector<apex::ApexFile>& active_packages) { |
| 88 | for (const apex::ApexFile& apex_file : active_packages) { |
| 89 | const std::string& package_path = apex_file.GetPath(); |
| 90 | apex::Status status = apex::deactivatePackage(package_path); |
| 91 | if (!status.Ok()) { |
| 92 | LOG(ERROR) << "Failed to deactivate " << package_path << ": " << status.ErrorMessage(); |
| 93 | } |
| 94 | } |
| 95 | } |
| 96 | |
| Roland Levillain | c3265ae | 2019-01-31 18:30:17 +0000 | [diff] [blame] | 97 | // Copied from system/core/init/mount_namespace.cpp. |
| 98 | static bool BindMount(const std::string& source, const std::string& mount_point, |
| 99 | bool recursive = false) { |
| 100 | unsigned long mountflags = MS_BIND; |
| 101 | if (recursive) { |
| 102 | mountflags |= MS_REC; |
| 103 | } |
| 104 | if (mount(source.c_str(), mount_point.c_str(), nullptr, mountflags, nullptr) == -1) { |
| 105 | PLOG(ERROR) << "Could not bind-mount " << source << " to " << mount_point; |
| 106 | return false; |
| 107 | } |
| 108 | return true; |
| 109 | } |
| 110 | |
| 111 | // Copied from system/core/init/mount_namespace.cpp and and adjusted (bind |
| 112 | // mounts are not made private, as the /postinstall is already private (see |
| 113 | // `android::installd::otapreopt_chroot`). |
| 114 | static bool BindMountBionic(const std::string& linker_source, const std::string& lib_dir_source, |
| 115 | const std::string& linker_mount_point, |
| 116 | const std::string& lib_mount_dir) { |
| 117 | if (access(linker_source.c_str(), F_OK) != 0) { |
| 118 | PLOG(INFO) << linker_source << " does not exist. Skipping mounting Bionic there."; |
| 119 | return true; |
| 120 | } |
| 121 | if (!BindMount(linker_source, linker_mount_point)) { |
| 122 | return false; |
| 123 | } |
| 124 | for (const auto& libname : kBionicLibFileNames) { |
| 125 | std::string mount_point = lib_mount_dir + libname; |
| 126 | std::string source = lib_dir_source + libname; |
| 127 | if (!BindMount(source, mount_point)) { |
| 128 | return false; |
| 129 | } |
| 130 | } |
| 131 | return true; |
| 132 | } |
| 133 | |
| Andreas Gampe | d089ca1 | 2016-06-27 14:25:30 -0700 | [diff] [blame] | 134 | // Entry for otapreopt_chroot. Expected parameters are: |
| 135 | // [cmd] [status-fd] [target-slot] "dexopt" [dexopt-params] |
| 136 | // The file descriptor denoted by status-fd will be closed. The rest of the parameters will |
| 137 | // be passed on to otapreopt in the chroot. |
| Andreas Gampe | 01ad598 | 2016-03-09 16:27:29 -0800 | [diff] [blame] | 138 | static int otapreopt_chroot(const int argc, char **arg) { |
| Zach Riggle | 318853a | 2018-01-18 18:34:04 -0600 | [diff] [blame] | 139 | // Validate arguments |
| 140 | // We need the command, status channel and target slot, at a minimum. |
| 141 | if(argc < 3) { |
| 142 | PLOG(ERROR) << "Not enough arguments."; |
| 143 | exit(208); |
| 144 | } |
| Andreas Gampe | d089ca1 | 2016-06-27 14:25:30 -0700 | [diff] [blame] | 145 | // Close all file descriptors. They are coming from the caller, we do not want to pass them |
| 146 | // on across our fork/exec into a different domain. |
| 147 | // 1) Default descriptors. |
| 148 | CloseDescriptor(STDIN_FILENO); |
| 149 | CloseDescriptor(STDOUT_FILENO); |
| 150 | CloseDescriptor(STDERR_FILENO); |
| 151 | // 2) The status channel. |
| 152 | CloseDescriptor(arg[1]); |
| 153 | |
| Andreas Gampe | 01ad598 | 2016-03-09 16:27:29 -0800 | [diff] [blame] | 154 | // We need to run the otapreopt tool from the postinstall partition. As such, set up a |
| 155 | // mount namespace and change root. |
| 156 | |
| 157 | // Create our own mount namespace. |
| 158 | if (unshare(CLONE_NEWNS) != 0) { |
| 159 | PLOG(ERROR) << "Failed to unshare() for otapreopt."; |
| 160 | exit(200); |
| 161 | } |
| 162 | |
| 163 | // Make postinstall private, so that our changes don't propagate. |
| 164 | if (mount("", "/postinstall", nullptr, MS_PRIVATE, nullptr) != 0) { |
| 165 | PLOG(ERROR) << "Failed to mount private."; |
| 166 | exit(201); |
| 167 | } |
| 168 | |
| 169 | // Bind mount necessary directories. |
| 170 | constexpr const char* kBindMounts[] = { |
| 171 | "/data", "/dev", "/proc", "/sys" |
| 172 | }; |
| 173 | for (size_t i = 0; i < arraysize(kBindMounts); ++i) { |
| 174 | std::string trg = StringPrintf("/postinstall%s", kBindMounts[i]); |
| 175 | if (mount(kBindMounts[i], trg.c_str(), nullptr, MS_BIND, nullptr) != 0) { |
| 176 | PLOG(ERROR) << "Failed to bind-mount " << kBindMounts[i]; |
| 177 | exit(202); |
| 178 | } |
| 179 | } |
| 180 | |
| Andreas Gampe | fd12eda | 2016-07-12 09:47:17 -0700 | [diff] [blame] | 181 | // Try to mount the vendor partition. update_engine doesn't do this for us, but we |
| 182 | // want it for vendor APKs. |
| 183 | // Notes: |
| 184 | // 1) We pretty much guess a name here and hope to find the partition by name. |
| 185 | // It is just as complicated and brittle to scan /proc/mounts. But this requires |
| 186 | // validating the target-slot so as not to try to mount some totally random path. |
| 187 | // 2) We're in a mount namespace here, so when we die, this will be cleaned up. |
| 188 | // 3) Ignore errors. Printing anything at this stage will open a file descriptor |
| 189 | // for logging. |
| 190 | if (!ValidateTargetSlotSuffix(arg[2])) { |
| 191 | LOG(ERROR) << "Target slot suffix not legal: " << arg[2]; |
| 192 | exit(207); |
| 193 | } |
| Andreas Gampe | b87a1c7 | 2018-04-13 17:42:23 -0700 | [diff] [blame] | 194 | { |
| Bowgo Tsai | 19d2d08 | 2018-05-31 10:16:46 +0800 | [diff] [blame] | 195 | std::string vendor_partition = StringPrintf("/dev/block/by-name/vendor%s", |
| Andreas Gampe | b87a1c7 | 2018-04-13 17:42:23 -0700 | [diff] [blame] | 196 | arg[2]); |
| 197 | int vendor_result = mount(vendor_partition.c_str(), |
| 198 | "/postinstall/vendor", |
| 199 | "ext4", |
| 200 | MS_RDONLY, |
| 201 | /* data */ nullptr); |
| 202 | UNUSED(vendor_result); |
| 203 | } |
| 204 | |
| 205 | // Try to mount the product partition. update_engine doesn't do this for us, but we |
| 206 | // want it for product APKs. Same notes as vendor above. |
| 207 | { |
| Bowgo Tsai | 19d2d08 | 2018-05-31 10:16:46 +0800 | [diff] [blame] | 208 | std::string product_partition = StringPrintf("/dev/block/by-name/product%s", |
| Andreas Gampe | b87a1c7 | 2018-04-13 17:42:23 -0700 | [diff] [blame] | 209 | arg[2]); |
| 210 | int product_result = mount(product_partition.c_str(), |
| 211 | "/postinstall/product", |
| 212 | "ext4", |
| 213 | MS_RDONLY, |
| 214 | /* data */ nullptr); |
| 215 | UNUSED(product_result); |
| 216 | } |
| Andreas Gampe | fd12eda | 2016-07-12 09:47:17 -0700 | [diff] [blame] | 217 | |
| Roland Levillain | c19c604 | 2018-12-18 12:15:12 +0000 | [diff] [blame] | 218 | // Setup APEX mount point and its security context. |
| 219 | static constexpr const char* kPostinstallApexDir = "/postinstall/apex"; |
| 220 | // The following logic is similar to the one in system/core/rootdir/init.rc: |
| 221 | // |
| 222 | // mount tmpfs tmpfs /apex nodev noexec nosuid |
| 223 | // chmod 0755 /apex |
| 224 | // chown root root /apex |
| 225 | // restorecon /apex |
| 226 | // |
| Roland Levillain | 8d27681 | 2019-01-24 10:51:30 +0000 | [diff] [blame] | 227 | // except we perform the `restorecon` step just after mounting the tmpfs |
| 228 | // filesystem in /postinstall/apex, so that this directory is correctly |
| 229 | // labeled (with type `postinstall_apex_mnt_dir`) and may be manipulated in |
| 230 | // following operations (`chmod`, `chown`, etc.) following policies |
| 231 | // restricted to `postinstall_apex_mnt_dir`: |
| 232 | // |
| 233 | // mount tmpfs tmpfs /postinstall/apex nodev noexec nosuid |
| 234 | // restorecon /postinstall/apex |
| 235 | // chmod 0755 /postinstall/apex |
| 236 | // chown root root /postinstall/apex |
| 237 | // |
| Roland Levillain | c19c604 | 2018-12-18 12:15:12 +0000 | [diff] [blame] | 238 | if (mount("tmpfs", kPostinstallApexDir, "tmpfs", MS_NODEV | MS_NOEXEC | MS_NOSUID, nullptr) |
| 239 | != 0) { |
| 240 | PLOG(ERROR) << "Failed to mount tmpfs in " << kPostinstallApexDir; |
| 241 | exit(209); |
| 242 | } |
| Roland Levillain | 8d27681 | 2019-01-24 10:51:30 +0000 | [diff] [blame] | 243 | if (selinux_android_restorecon(kPostinstallApexDir, 0) < 0) { |
| 244 | PLOG(ERROR) << "Failed to restorecon " << kPostinstallApexDir; |
| 245 | exit(214); |
| 246 | } |
| Roland Levillain | c19c604 | 2018-12-18 12:15:12 +0000 | [diff] [blame] | 247 | if (chmod(kPostinstallApexDir, 0755) != 0) { |
| 248 | PLOG(ERROR) << "Failed to chmod " << kPostinstallApexDir << " to 0755"; |
| 249 | exit(210); |
| 250 | } |
| 251 | if (chown(kPostinstallApexDir, 0, 0) != 0) { |
| 252 | PLOG(ERROR) << "Failed to chown " << kPostinstallApexDir << " to root:root"; |
| 253 | exit(211); |
| 254 | } |
| Roland Levillain | c19c604 | 2018-12-18 12:15:12 +0000 | [diff] [blame] | 255 | |
| Andreas Gampe | 01ad598 | 2016-03-09 16:27:29 -0800 | [diff] [blame] | 256 | // Chdir into /postinstall. |
| 257 | if (chdir("/postinstall") != 0) { |
| 258 | PLOG(ERROR) << "Unable to chdir into /postinstall."; |
| 259 | exit(203); |
| 260 | } |
| 261 | |
| 262 | // Make /postinstall the root in our mount namespace. |
| 263 | if (chroot(".") != 0) { |
| 264 | PLOG(ERROR) << "Failed to chroot"; |
| 265 | exit(204); |
| 266 | } |
| 267 | |
| 268 | if (chdir("/") != 0) { |
| 269 | PLOG(ERROR) << "Unable to chdir into /."; |
| 270 | exit(205); |
| 271 | } |
| 272 | |
| Roland Levillain | c19c604 | 2018-12-18 12:15:12 +0000 | [diff] [blame] | 273 | // Try to mount APEX packages in "/apex" in the chroot dir. We need at least |
| 274 | // the Android Runtime APEX, as it is required by otapreopt to run dex2oat. |
| Roland Levillain | 6520cca | 2019-02-01 13:15:58 +0000 | [diff] [blame] | 275 | std::vector<apex::ApexFile> active_packages = ActivateApexPackages(); |
| Roland Levillain | c19c604 | 2018-12-18 12:15:12 +0000 | [diff] [blame] | 276 | |
| Roland Levillain | c3265ae | 2019-01-31 18:30:17 +0000 | [diff] [blame] | 277 | // Bind-mount Bionic artifacts from the Runtime APEX. |
| 278 | // This logic is copied and adapted from system/core/init/mount_namespace.cpp. |
| 279 | if (!BindMountBionic(kRuntimeLinkerPath, kRuntimeBionicLibsDir, kLinkerMountPoint, |
| 280 | kBionicLibsMountPointDir)) { |
| 281 | LOG(ERROR) << "Failed to mount 32-bit Bionic artifacts from the Runtime APEX."; |
| 282 | // Clean up and exit. |
| 283 | DeactivateApexPackages(active_packages); |
| 284 | exit(215); |
| 285 | } |
| 286 | if (!BindMountBionic(kRuntimeLinkerPath64, kRuntimeBionicLibsDir64, kLinkerMountPoint64, |
| 287 | kBionicLibsMountPointDir64)) { |
| 288 | LOG(ERROR) << "Failed to mount 64-bit Bionic artifacts from the Runtime APEX."; |
| 289 | // Clean up and exit. |
| 290 | DeactivateApexPackages(active_packages); |
| 291 | exit(216); |
| 292 | } |
| 293 | |
| Andreas Gampe | 01ad598 | 2016-03-09 16:27:29 -0800 | [diff] [blame] | 294 | // Now go on and run otapreopt. |
| 295 | |
| Roland Levillain | 94b4180 | 2019-01-18 11:56:50 +0000 | [diff] [blame] | 296 | // Incoming: cmd + status-fd + target-slot + cmd... | Incoming | = argc |
| 297 | // Outgoing: cmd + target-slot + cmd... | Outgoing | = argc - 1 |
| 298 | std::vector<std::string> cmd; |
| 299 | cmd.reserve(argc); |
| 300 | cmd.push_back("/system/bin/otapreopt"); |
| Andreas Gampe | d089ca1 | 2016-06-27 14:25:30 -0700 | [diff] [blame] | 301 | |
| 302 | // The first parameter is the status file descriptor, skip. |
| Roland Levillain | 94b4180 | 2019-01-18 11:56:50 +0000 | [diff] [blame] | 303 | for (size_t i = 2; i < static_cast<size_t>(argc); ++i) { |
| 304 | cmd.push_back(arg[i]); |
| Andreas Gampe | d089ca1 | 2016-06-27 14:25:30 -0700 | [diff] [blame] | 305 | } |
| Andreas Gampe | 01ad598 | 2016-03-09 16:27:29 -0800 | [diff] [blame] | 306 | |
| Roland Levillain | 94b4180 | 2019-01-18 11:56:50 +0000 | [diff] [blame] | 307 | // Fork and execute otapreopt in its own process. |
| 308 | std::string error_msg; |
| 309 | bool exec_result = Exec(cmd, &error_msg); |
| 310 | if (!exec_result) { |
| 311 | LOG(ERROR) << "Running otapreopt failed: " << error_msg; |
| 312 | } |
| 313 | |
| Roland Levillain | 6520cca | 2019-02-01 13:15:58 +0000 | [diff] [blame] | 314 | // Tear down the work down by the apexd logic. (i.e. deactivate packages). |
| 315 | DeactivateApexPackages(active_packages); |
| Roland Levillain | 94b4180 | 2019-01-18 11:56:50 +0000 | [diff] [blame] | 316 | |
| 317 | if (!exec_result) { |
| 318 | exit(213); |
| 319 | } |
| 320 | |
| 321 | return 0; |
| Andreas Gampe | 01ad598 | 2016-03-09 16:27:29 -0800 | [diff] [blame] | 322 | } |
| 323 | |
| 324 | } // namespace installd |
| 325 | } // namespace android |
| 326 | |
| 327 | int main(const int argc, char *argv[]) { |
| 328 | return android::installd::otapreopt_chroot(argc, argv); |
| 329 | } |