| 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 |  | 
| Andreas Gampe | d089ca1 | 2016-06-27 14:25:30 -0700 | [diff] [blame] | 44 | static void CloseDescriptor(int fd) { | 
 | 45 |     if (fd >= 0) { | 
 | 46 |         int result = close(fd); | 
 | 47 |         UNUSED(result);  // Ignore result. Printing to logcat will open a new descriptor | 
 | 48 |                          // that we do *not* want. | 
 | 49 |     } | 
 | 50 | } | 
 | 51 |  | 
 | 52 | static void CloseDescriptor(const char* descriptor_string) { | 
 | 53 |     int fd = -1; | 
 | 54 |     std::istringstream stream(descriptor_string); | 
 | 55 |     stream >> fd; | 
 | 56 |     if (!stream.fail()) { | 
 | 57 |         CloseDescriptor(fd); | 
 | 58 |     } | 
 | 59 | } | 
 | 60 |  | 
| Roland Levillain | 6520cca | 2019-02-01 13:15:58 +0000 | [diff] [blame] | 61 | static std::vector<apex::ApexFile> ActivateApexPackages() { | 
 | 62 |     // The logic here is (partially) copied and adapted from | 
 | 63 |     // system/apex/apexd/apexd_main.cpp. | 
 | 64 |     // | 
 | 65 |     // Only scan the APEX directory under /system (within the chroot dir). | 
 | 66 |     // Note that this leaves around the loop devices created and used by | 
 | 67 |     // libapexd's code, but this is fine, as we expect to reboot soon after. | 
 | 68 |     apex::scanPackagesDirAndActivate(apex::kApexPackageSystemDir); | 
 | 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(); | 
 | 75 |         apex::Status status = apex::deactivatePackage(package_path); | 
 | 76 |         if (!status.Ok()) { | 
 | 77 |             LOG(ERROR) << "Failed to deactivate " << package_path << ": " << status.ErrorMessage(); | 
 | 78 |         } | 
 | 79 |     } | 
 | 80 | } | 
 | 81 |  | 
| Andreas Gampe | d089ca1 | 2016-06-27 14:25:30 -0700 | [diff] [blame] | 82 | // Entry for otapreopt_chroot. Expected parameters are: | 
 | 83 | //   [cmd] [status-fd] [target-slot] "dexopt" [dexopt-params] | 
 | 84 | // The file descriptor denoted by status-fd will be closed. The rest of the parameters will | 
 | 85 | // be passed on to otapreopt in the chroot. | 
| Andreas Gampe | 01ad598 | 2016-03-09 16:27:29 -0800 | [diff] [blame] | 86 | static int otapreopt_chroot(const int argc, char **arg) { | 
| Zach Riggle | 318853a | 2018-01-18 18:34:04 -0600 | [diff] [blame] | 87 |     // Validate arguments | 
 | 88 |     // We need the command, status channel and target slot, at a minimum. | 
 | 89 |     if(argc < 3) { | 
 | 90 |         PLOG(ERROR) << "Not enough arguments."; | 
 | 91 |         exit(208); | 
 | 92 |     } | 
| Andreas Gampe | d089ca1 | 2016-06-27 14:25:30 -0700 | [diff] [blame] | 93 |     // Close all file descriptors. They are coming from the caller, we do not want to pass them | 
 | 94 |     // on across our fork/exec into a different domain. | 
 | 95 |     // 1) Default descriptors. | 
 | 96 |     CloseDescriptor(STDIN_FILENO); | 
 | 97 |     CloseDescriptor(STDOUT_FILENO); | 
 | 98 |     CloseDescriptor(STDERR_FILENO); | 
 | 99 |     // 2) The status channel. | 
 | 100 |     CloseDescriptor(arg[1]); | 
 | 101 |  | 
| Andreas Gampe | 01ad598 | 2016-03-09 16:27:29 -0800 | [diff] [blame] | 102 |     // We need to run the otapreopt tool from the postinstall partition. As such, set up a | 
 | 103 |     // mount namespace and change root. | 
 | 104 |  | 
 | 105 |     // Create our own mount namespace. | 
 | 106 |     if (unshare(CLONE_NEWNS) != 0) { | 
 | 107 |         PLOG(ERROR) << "Failed to unshare() for otapreopt."; | 
 | 108 |         exit(200); | 
 | 109 |     } | 
 | 110 |  | 
 | 111 |     // Make postinstall private, so that our changes don't propagate. | 
 | 112 |     if (mount("", "/postinstall", nullptr, MS_PRIVATE, nullptr) != 0) { | 
 | 113 |         PLOG(ERROR) << "Failed to mount private."; | 
 | 114 |         exit(201); | 
 | 115 |     } | 
 | 116 |  | 
 | 117 |     // Bind mount necessary directories. | 
 | 118 |     constexpr const char* kBindMounts[] = { | 
 | 119 |             "/data", "/dev", "/proc", "/sys" | 
 | 120 |     }; | 
 | 121 |     for (size_t i = 0; i < arraysize(kBindMounts); ++i) { | 
 | 122 |         std::string trg = StringPrintf("/postinstall%s", kBindMounts[i]); | 
 | 123 |         if (mount(kBindMounts[i], trg.c_str(), nullptr, MS_BIND, nullptr) != 0) { | 
 | 124 |             PLOG(ERROR) << "Failed to bind-mount " << kBindMounts[i]; | 
 | 125 |             exit(202); | 
 | 126 |         } | 
 | 127 |     } | 
 | 128 |  | 
| Andreas Gampe | fd12eda | 2016-07-12 09:47:17 -0700 | [diff] [blame] | 129 |     // Try to mount the vendor partition. update_engine doesn't do this for us, but we | 
 | 130 |     // want it for vendor APKs. | 
 | 131 |     // Notes: | 
 | 132 |     //  1) We pretty much guess a name here and hope to find the partition by name. | 
 | 133 |     //     It is just as complicated and brittle to scan /proc/mounts. But this requires | 
 | 134 |     //     validating the target-slot so as not to try to mount some totally random path. | 
 | 135 |     //  2) We're in a mount namespace here, so when we die, this will be cleaned up. | 
 | 136 |     //  3) Ignore errors. Printing anything at this stage will open a file descriptor | 
 | 137 |     //     for logging. | 
 | 138 |     if (!ValidateTargetSlotSuffix(arg[2])) { | 
 | 139 |         LOG(ERROR) << "Target slot suffix not legal: " << arg[2]; | 
 | 140 |         exit(207); | 
 | 141 |     } | 
| Andreas Gampe | b87a1c7 | 2018-04-13 17:42:23 -0700 | [diff] [blame] | 142 |     { | 
| Bowgo Tsai | 19d2d08 | 2018-05-31 10:16:46 +0800 | [diff] [blame] | 143 |       std::string vendor_partition = StringPrintf("/dev/block/by-name/vendor%s", | 
| Andreas Gampe | b87a1c7 | 2018-04-13 17:42:23 -0700 | [diff] [blame] | 144 |                                                   arg[2]); | 
 | 145 |       int vendor_result = mount(vendor_partition.c_str(), | 
 | 146 |                                 "/postinstall/vendor", | 
 | 147 |                                 "ext4", | 
 | 148 |                                 MS_RDONLY, | 
 | 149 |                                 /* data */ nullptr); | 
 | 150 |       UNUSED(vendor_result); | 
 | 151 |     } | 
 | 152 |  | 
 | 153 |     // Try to mount the product partition. update_engine doesn't do this for us, but we | 
 | 154 |     // want it for product APKs. Same notes as vendor above. | 
 | 155 |     { | 
| Bowgo Tsai | 19d2d08 | 2018-05-31 10:16:46 +0800 | [diff] [blame] | 156 |       std::string product_partition = StringPrintf("/dev/block/by-name/product%s", | 
| Andreas Gampe | b87a1c7 | 2018-04-13 17:42:23 -0700 | [diff] [blame] | 157 |                                                    arg[2]); | 
 | 158 |       int product_result = mount(product_partition.c_str(), | 
 | 159 |                                  "/postinstall/product", | 
 | 160 |                                  "ext4", | 
 | 161 |                                  MS_RDONLY, | 
 | 162 |                                  /* data */ nullptr); | 
 | 163 |       UNUSED(product_result); | 
 | 164 |     } | 
| Andreas Gampe | fd12eda | 2016-07-12 09:47:17 -0700 | [diff] [blame] | 165 |  | 
| Roland Levillain | c19c604 | 2018-12-18 12:15:12 +0000 | [diff] [blame] | 166 |     // Setup APEX mount point and its security context. | 
 | 167 |     static constexpr const char* kPostinstallApexDir = "/postinstall/apex"; | 
 | 168 |     // The following logic is similar to the one in system/core/rootdir/init.rc: | 
 | 169 |     // | 
 | 170 |     //   mount tmpfs tmpfs /apex nodev noexec nosuid | 
 | 171 |     //   chmod 0755 /apex | 
 | 172 |     //   chown root root /apex | 
 | 173 |     //   restorecon /apex | 
 | 174 |     // | 
| Roland Levillain | 8d27681 | 2019-01-24 10:51:30 +0000 | [diff] [blame] | 175 |     // except we perform the `restorecon` step just after mounting the tmpfs | 
 | 176 |     // filesystem in /postinstall/apex, so that this directory is correctly | 
 | 177 |     // labeled (with type `postinstall_apex_mnt_dir`) and may be manipulated in | 
 | 178 |     // following operations (`chmod`, `chown`, etc.) following policies | 
 | 179 |     // restricted to `postinstall_apex_mnt_dir`: | 
 | 180 |     // | 
 | 181 |     //   mount tmpfs tmpfs /postinstall/apex nodev noexec nosuid | 
 | 182 |     //   restorecon /postinstall/apex | 
 | 183 |     //   chmod 0755 /postinstall/apex | 
 | 184 |     //   chown root root /postinstall/apex | 
 | 185 |     // | 
| Roland Levillain | c19c604 | 2018-12-18 12:15:12 +0000 | [diff] [blame] | 186 |     if (mount("tmpfs", kPostinstallApexDir, "tmpfs", MS_NODEV | MS_NOEXEC | MS_NOSUID, nullptr) | 
 | 187 |         != 0) { | 
 | 188 |         PLOG(ERROR) << "Failed to mount tmpfs in " << kPostinstallApexDir; | 
 | 189 |         exit(209); | 
 | 190 |     } | 
| Roland Levillain | 8d27681 | 2019-01-24 10:51:30 +0000 | [diff] [blame] | 191 |     if (selinux_android_restorecon(kPostinstallApexDir, 0) < 0) { | 
 | 192 |         PLOG(ERROR) << "Failed to restorecon " << kPostinstallApexDir; | 
 | 193 |         exit(214); | 
 | 194 |     } | 
| Roland Levillain | c19c604 | 2018-12-18 12:15:12 +0000 | [diff] [blame] | 195 |     if (chmod(kPostinstallApexDir, 0755) != 0) { | 
 | 196 |         PLOG(ERROR) << "Failed to chmod " << kPostinstallApexDir << " to 0755"; | 
 | 197 |         exit(210); | 
 | 198 |     } | 
 | 199 |     if (chown(kPostinstallApexDir, 0, 0) != 0) { | 
 | 200 |         PLOG(ERROR) << "Failed to chown " << kPostinstallApexDir << " to root:root"; | 
 | 201 |         exit(211); | 
 | 202 |     } | 
| Roland Levillain | c19c604 | 2018-12-18 12:15:12 +0000 | [diff] [blame] | 203 |  | 
| Andreas Gampe | 01ad598 | 2016-03-09 16:27:29 -0800 | [diff] [blame] | 204 |     // Chdir into /postinstall. | 
 | 205 |     if (chdir("/postinstall") != 0) { | 
 | 206 |         PLOG(ERROR) << "Unable to chdir into /postinstall."; | 
 | 207 |         exit(203); | 
 | 208 |     } | 
 | 209 |  | 
 | 210 |     // Make /postinstall the root in our mount namespace. | 
 | 211 |     if (chroot(".")  != 0) { | 
 | 212 |         PLOG(ERROR) << "Failed to chroot"; | 
 | 213 |         exit(204); | 
 | 214 |     } | 
 | 215 |  | 
 | 216 |     if (chdir("/") != 0) { | 
 | 217 |         PLOG(ERROR) << "Unable to chdir into /."; | 
 | 218 |         exit(205); | 
 | 219 |     } | 
 | 220 |  | 
| Roland Levillain | c19c604 | 2018-12-18 12:15:12 +0000 | [diff] [blame] | 221 |     // Try to mount APEX packages in "/apex" in the chroot dir. We need at least | 
 | 222 |     // 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] | 223 |     std::vector<apex::ApexFile> active_packages = ActivateApexPackages(); | 
| Roland Levillain | c19c604 | 2018-12-18 12:15:12 +0000 | [diff] [blame] | 224 |  | 
| Andreas Gampe | 01ad598 | 2016-03-09 16:27:29 -0800 | [diff] [blame] | 225 |     // Now go on and run otapreopt. | 
 | 226 |  | 
| Roland Levillain | 94b4180 | 2019-01-18 11:56:50 +0000 | [diff] [blame] | 227 |     // Incoming:  cmd + status-fd + target-slot + cmd...      | Incoming | = argc | 
 | 228 |     // Outgoing:  cmd             + target-slot + cmd...      | Outgoing | = argc - 1 | 
 | 229 |     std::vector<std::string> cmd; | 
 | 230 |     cmd.reserve(argc); | 
 | 231 |     cmd.push_back("/system/bin/otapreopt"); | 
| Andreas Gampe | d089ca1 | 2016-06-27 14:25:30 -0700 | [diff] [blame] | 232 |  | 
 | 233 |     // The first parameter is the status file descriptor, skip. | 
| Roland Levillain | 94b4180 | 2019-01-18 11:56:50 +0000 | [diff] [blame] | 234 |     for (size_t i = 2; i < static_cast<size_t>(argc); ++i) { | 
 | 235 |         cmd.push_back(arg[i]); | 
| Andreas Gampe | d089ca1 | 2016-06-27 14:25:30 -0700 | [diff] [blame] | 236 |     } | 
| Andreas Gampe | 01ad598 | 2016-03-09 16:27:29 -0800 | [diff] [blame] | 237 |  | 
| Roland Levillain | 94b4180 | 2019-01-18 11:56:50 +0000 | [diff] [blame] | 238 |     // Fork and execute otapreopt in its own process. | 
 | 239 |     std::string error_msg; | 
 | 240 |     bool exec_result = Exec(cmd, &error_msg); | 
 | 241 |     if (!exec_result) { | 
 | 242 |         LOG(ERROR) << "Running otapreopt failed: " << error_msg; | 
 | 243 |     } | 
 | 244 |  | 
| Roland Levillain | 6520cca | 2019-02-01 13:15:58 +0000 | [diff] [blame] | 245 |     // Tear down the work down by the apexd logic. (i.e. deactivate packages). | 
 | 246 |     DeactivateApexPackages(active_packages); | 
| Roland Levillain | 94b4180 | 2019-01-18 11:56:50 +0000 | [diff] [blame] | 247 |  | 
 | 248 |     if (!exec_result) { | 
 | 249 |         exit(213); | 
 | 250 |     } | 
 | 251 |  | 
 | 252 |     return 0; | 
| Andreas Gampe | 01ad598 | 2016-03-09 16:27:29 -0800 | [diff] [blame] | 253 | } | 
 | 254 |  | 
 | 255 | }  // namespace installd | 
 | 256 | }  // namespace android | 
 | 257 |  | 
 | 258 | int main(const int argc, char *argv[]) { | 
 | 259 |     return android::installd::otapreopt_chroot(argc, argv); | 
 | 260 | } |