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 17 #include <fcntl.h> 18 #include <linux/unistd.h> 19 #include <sys/mount.h> 20 #include <sys/wait.h> 21 22 #include <sstream> 23 24 #include <android-base/logging.h> 25 #include <android-base/macros.h> 26 #include <android-base/stringprintf.h> 27 28 #include "installd_constants.h" 29 #include "otapreopt_utils.h" 30 31 #ifndef LOG_TAG 32 #define LOG_TAG "otapreopt" 33 #endif 34 35 using android::base::StringPrintf; 36 37 namespace android { 38 namespace installd { 39 40 static void CloseDescriptor(int fd) { 41 if (fd >= 0) { 42 int result = close(fd); 43 UNUSED(result); // Ignore result. Printing to logcat will open a new descriptor 44 // that we do *not* want. 45 } 46 } 47 48 static void CloseDescriptor(const char* descriptor_string) { 49 int fd = -1; 50 std::istringstream stream(descriptor_string); 51 stream >> fd; 52 if (!stream.fail()) { 53 CloseDescriptor(fd); 54 } 55 } 56 57 // Entry for otapreopt_chroot. Expected parameters are: 58 // [cmd] [status-fd] [target-slot] "dexopt" [dexopt-params] 59 // The file descriptor denoted by status-fd will be closed. The rest of the parameters will 60 // be passed on to otapreopt in the chroot. 61 static int otapreopt_chroot(const int argc, char **arg) { 62 // Validate arguments 63 // We need the command, status channel and target slot, at a minimum. 64 if(argc < 3) { 65 PLOG(ERROR) << "Not enough arguments."; 66 exit(208); 67 } 68 // Close all file descriptors. They are coming from the caller, we do not want to pass them 69 // on across our fork/exec into a different domain. 70 // 1) Default descriptors. 71 CloseDescriptor(STDIN_FILENO); 72 CloseDescriptor(STDOUT_FILENO); 73 CloseDescriptor(STDERR_FILENO); 74 // 2) The status channel. 75 CloseDescriptor(arg[1]); 76 77 // We need to run the otapreopt tool from the postinstall partition. As such, set up a 78 // mount namespace and change root. 79 80 // Create our own mount namespace. 81 if (unshare(CLONE_NEWNS) != 0) { 82 PLOG(ERROR) << "Failed to unshare() for otapreopt."; 83 exit(200); 84 } 85 86 // Make postinstall private, so that our changes don't propagate. 87 if (mount("", "/postinstall", nullptr, MS_PRIVATE, nullptr) != 0) { 88 PLOG(ERROR) << "Failed to mount private."; 89 exit(201); 90 } 91 92 // Bind mount necessary directories. 93 constexpr const char* kBindMounts[] = { 94 "/data", "/dev", "/proc", "/sys" 95 }; 96 for (size_t i = 0; i < arraysize(kBindMounts); ++i) { 97 std::string trg = StringPrintf("/postinstall%s", kBindMounts[i]); 98 if (mount(kBindMounts[i], trg.c_str(), nullptr, MS_BIND, nullptr) != 0) { 99 PLOG(ERROR) << "Failed to bind-mount " << kBindMounts[i]; 100 exit(202); 101 } 102 } 103 104 // Try to mount the vendor partition. update_engine doesn't do this for us, but we 105 // want it for vendor APKs. 106 // Notes: 107 // 1) We pretty much guess a name here and hope to find the partition by name. 108 // It is just as complicated and brittle to scan /proc/mounts. But this requires 109 // validating the target-slot so as not to try to mount some totally random path. 110 // 2) We're in a mount namespace here, so when we die, this will be cleaned up. 111 // 3) Ignore errors. Printing anything at this stage will open a file descriptor 112 // for logging. 113 if (!ValidateTargetSlotSuffix(arg[2])) { 114 LOG(ERROR) << "Target slot suffix not legal: " << arg[2]; 115 exit(207); 116 } 117 { 118 std::string vendor_partition = StringPrintf("/dev/block/by-name/vendor%s", 119 arg[2]); 120 int vendor_result = mount(vendor_partition.c_str(), 121 "/postinstall/vendor", 122 "ext4", 123 MS_RDONLY, 124 /* data */ nullptr); 125 UNUSED(vendor_result); 126 } 127 128 // Try to mount the product partition. update_engine doesn't do this for us, but we 129 // want it for product APKs. Same notes as vendor above. 130 { 131 std::string product_partition = StringPrintf("/dev/block/by-name/product%s", 132 arg[2]); 133 int product_result = mount(product_partition.c_str(), 134 "/postinstall/product", 135 "ext4", 136 MS_RDONLY, 137 /* data */ nullptr); 138 UNUSED(product_result); 139 } 140 141 // Chdir into /postinstall. 142 if (chdir("/postinstall") != 0) { 143 PLOG(ERROR) << "Unable to chdir into /postinstall."; 144 exit(203); 145 } 146 147 // Make /postinstall the root in our mount namespace. 148 if (chroot(".") != 0) { 149 PLOG(ERROR) << "Failed to chroot"; 150 exit(204); 151 } 152 153 if (chdir("/") != 0) { 154 PLOG(ERROR) << "Unable to chdir into /."; 155 exit(205); 156 } 157 158 // Now go on and run otapreopt. 159 160 // Incoming: cmd + status-fd + target-slot + cmd... + null | Incoming | = argc + 1 161 // Outgoing: cmd + target-slot + cmd... + null | Outgoing | = argc 162 const char** argv = new const char*[argc]; 163 164 argv[0] = "/system/bin/otapreopt"; 165 166 // The first parameter is the status file descriptor, skip. 167 for (size_t i = 2; i <= static_cast<size_t>(argc); ++i) { 168 argv[i - 1] = arg[i]; 169 } 170 171 execv(argv[0], static_cast<char * const *>(const_cast<char**>(argv))); 172 PLOG(ERROR) << "execv(OTAPREOPT) failed."; 173 exit(99); 174 } 175 176 } // namespace installd 177 } // namespace android 178 179 int main(const int argc, char *argv[]) { 180 return android::installd::otapreopt_chroot(argc, argv); 181 } 182