1 /* 2 * Copyright (C) 2018 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 #define LOG_TAG "apexd" 18 19 #include "apexd_prepostinstall.h" 20 21 #include <algorithm> 22 #include <vector> 23 24 #include <fcntl.h> 25 #include <sys/mount.h> 26 #include <sys/stat.h> 27 #include <sys/types.h> 28 #include <sys/wait.h> 29 #include <unistd.h> 30 31 #include <android-base/logging.h> 32 #include <android-base/macros.h> 33 #include <android-base/scopeguard.h> 34 #include <android-base/strings.h> 35 36 #include "apex_database.h" 37 #include "apex_file.h" 38 #include "apex_manifest.h" 39 #include "apexd.h" 40 #include "apexd_private.h" 41 #include "apexd_utils.h" 42 #include "string_log.h" 43 44 using android::base::Error; 45 using android::base::Result; 46 using ::apex::proto::ApexManifest; 47 48 namespace android { 49 namespace apex { 50 51 namespace { 52 53 using MountedApexData = MountedApexDatabase::MountedApexData; 54 55 void CloseSTDDescriptors() { 56 // exec()d process will reopen STD* file descriptors as 57 // /dev/null 58 close(STDIN_FILENO); 59 close(STDOUT_FILENO); 60 close(STDERR_FILENO); 61 } 62 63 template <typename Fn> 64 Result<void> StageFnInstall(const std::vector<ApexFile>& apexes, 65 const std::vector<std::string>& mount_points, Fn fn, 66 const char* arg, const char* name) { 67 // TODO(b/158470023): consider supporting a session with more than one 68 // pre-install hook. 69 int hook_idx = -1; 70 for (size_t i = 0; i < apexes.size(); i++) { 71 if (!(apexes[i].GetManifest().*fn)().empty()) { 72 if (hook_idx != -1) { 73 return Error() << "Missing support for multiple " << name << " hooks"; 74 } 75 hook_idx = i; 76 } 77 } 78 CHECK(hook_idx != -1); 79 LOG(VERBOSE) << name << " for " << apexes[hook_idx].GetPath(); 80 81 // Create invocation args. 82 std::vector<std::string> args{ 83 "/system/bin/apexd", arg, 84 mount_points[hook_idx] // Make the APEX with hook first. 85 }; 86 for (size_t i = 0; i < mount_points.size(); i++) { 87 if ((int)i != hook_idx) { 88 args.push_back(mount_points[i]); 89 } 90 } 91 92 return ForkAndRun(args); 93 } 94 95 template <typename Fn> 96 int RunFnInstall(char** in_argv, Fn fn, const char* name) { 97 std::vector<std::string> activation_dirs; 98 auto preinstall_guard = android::base::make_scope_guard([&]() { 99 for (const std::string& active_point : activation_dirs) { 100 if (0 != rmdir(active_point.c_str())) { 101 PLOG(ERROR) << "Could not delete temporary active point " 102 << active_point; 103 } 104 } 105 }); 106 107 // 1) Unshare. 108 if (unshare(CLONE_NEWNS) != 0) { 109 PLOG(ERROR) << "Failed to unshare() for apex " << name; 110 _exit(200); 111 } 112 113 // 2) Make everything private, so that our (and hook's) changes do not 114 // propagate. 115 if (mount(nullptr, "/", nullptr, MS_PRIVATE | MS_REC, nullptr) == -1) { 116 PLOG(ERROR) << "Failed to mount private."; 117 _exit(201); 118 } 119 120 std::string hook_path; 121 { 122 auto bind_fn = [&fn, name, 123 activation_dirs](const std::string& mount_point) mutable { 124 std::string hook; 125 std::string active_point; 126 { 127 Result<ApexManifest> manifest_or = 128 ReadManifest(mount_point + "/" + kManifestFilenamePb); 129 if (!manifest_or.ok()) { 130 LOG(ERROR) << "Could not read manifest from " << mount_point << "/" 131 << kManifestFilenamePb << " for " << name << ": " 132 << manifest_or.error(); 133 // Fallback to Json manifest if present. 134 LOG(ERROR) << "Trying to find a JSON manifest"; 135 manifest_or = ReadManifest(mount_point + "/" + kManifestFilenameJson); 136 if (!manifest_or.ok()) { 137 LOG(ERROR) << "Could not read manifest from " << mount_point << "/" 138 << kManifestFilenameJson << " for " << name << ": " 139 << manifest_or.error(); 140 _exit(202); 141 } 142 } 143 const auto& manifest = *manifest_or; 144 hook = (manifest.*fn)(); 145 active_point = apexd_private::GetActiveMountPoint(manifest); 146 // Ensure there is an activation point. If not, create one and delete 147 // later. 148 if (0 == mkdir(active_point.c_str(), kMkdirMode)) { 149 activation_dirs.push_back(active_point); 150 } else if (errno != EEXIST) { 151 PLOG(ERROR) << "Unable to create mount point " << active_point; 152 _exit(205); 153 } 154 } 155 156 // 3) Activate the new apex. 157 Result<void> bind_status = 158 apexd_private::BindMount(active_point, mount_point); 159 if (!bind_status.ok()) { 160 LOG(ERROR) << "Failed to bind-mount " << mount_point << " to " 161 << active_point << ": " << bind_status.error(); 162 _exit(203); 163 } 164 165 return std::make_pair(active_point, hook); 166 }; 167 168 // First/main APEX. 169 auto [active_point, hook] = bind_fn(in_argv[2]); 170 hook_path = active_point + "/" + hook; 171 172 for (size_t i = 3;; ++i) { 173 if (in_argv[i] == nullptr) { 174 break; 175 } 176 bind_fn(in_argv[i]); // Ignore result, hook will be empty. 177 } 178 } 179 180 // 4) Run the hook. 181 182 // For now, just run sh. But this probably needs to run the new linker. 183 std::vector<std::string> args{ 184 hook_path, 185 }; 186 std::vector<const char*> argv; 187 argv.resize(args.size() + 1, nullptr); 188 std::transform(args.begin(), args.end(), argv.begin(), 189 [](const std::string& in) { return in.c_str(); }); 190 191 LOG(ERROR) << "execv of " << android::base::Join(args, " "); 192 193 // Close all file descriptors. They are coming from the caller, we do not 194 // want to pass them on across our fork/exec into a different domain. 195 CloseSTDDescriptors(); 196 197 execv(argv[0], const_cast<char**>(argv.data())); 198 PLOG(ERROR) << "execv of " << android::base::Join(args, " ") << " failed"; 199 _exit(204); 200 } 201 202 } // namespace 203 204 Result<void> StagePreInstall(const std::vector<ApexFile>& apexes, 205 const std::vector<std::string>& mount_points) { 206 return StageFnInstall(apexes, mount_points, &ApexManifest::preinstallhook, 207 "--pre-install", "pre-install"); 208 } 209 210 int RunPreInstall(char** in_argv) { 211 return RunFnInstall(in_argv, &ApexManifest::preinstallhook, "pre-install"); 212 } 213 214 Result<void> StagePostInstall(const std::vector<ApexFile>& apexes, 215 const std::vector<std::string>& mount_points) { 216 return StageFnInstall(apexes, mount_points, &ApexManifest::postinstallhook, 217 "--post-install", "post-install"); 218 } 219 220 int RunPostInstall(char** in_argv) { 221 return RunFnInstall(in_argv, &ApexManifest::postinstallhook, "post-install"); 222 } 223 224 } // namespace apex 225 } // namespace android 226