blob: 21b8650a97e0f7b79d90b48a05902c1afc37c03e [file] [log] [blame]
Andreas Gampee1a40392018-11-30 09:47:17 -08001/*
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
Andreas Gampef7663552019-01-03 09:22:11 -080019#include "apexd_prepostinstall.h"
Andreas Gampee1a40392018-11-30 09:47:17 -080020
21#include <algorithm>
22#include <vector>
23
24#include <fcntl.h>
25#include <sys/mount.h>
Andreas Gampeb48122e2018-12-11 15:48:42 -080026#include <sys/stat.h>
Andreas Gampee1a40392018-11-30 09:47:17 -080027#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
Nikita Ioffe264c4212019-09-13 16:30:17 +010036#include "apex_database.h"
Andreas Gampee1a40392018-11-30 09:47:17 -080037#include "apex_file.h"
38#include "apexd.h"
39#include "apexd_private.h"
40#include "apexd_utils.h"
41#include "string_log.h"
42
Mohammad Samiul Islambd6ab0f2019-06-20 15:55:27 +010043using android::base::Error;
44using android::base::Result;
45
Andreas Gampee1a40392018-11-30 09:47:17 -080046namespace android {
47namespace apex {
48
49namespace {
50
Nikita Ioffe264c4212019-09-13 16:30:17 +010051using MountedApexData = MountedApexDatabase::MountedApexData;
52
Nick Kralevich3501c7b2019-02-28 10:20:44 -080053void CloseSTDDescriptors() {
54 // exec()d process will reopen STD* file descriptors as
55 // /dev/null
56 close(STDIN_FILENO);
57 close(STDOUT_FILENO);
58 close(STDERR_FILENO);
Andreas Gampee1a40392018-11-30 09:47:17 -080059}
60
Nikita Ioffe264c4212019-09-13 16:30:17 +010061// Instead of temp mounting inside this fuction, we can make a caller do it.
62// This will align with the plan of extending temp mounting to provide a
63// way to run additional pre-reboot verification of an APEX.
64// TODO(ioffe): pass mount points instead of apex files.
Andreas Gampef7663552019-01-03 09:22:11 -080065template <typename Fn>
Mohammad Samiul Islambd6ab0f2019-06-20 15:55:27 +010066Result<void> StageFnInstall(const std::vector<ApexFile>& apexes, Fn fn,
67 const char* arg, const char* name) {
Andreas Gampee44b5792018-12-13 15:48:45 -080068 // TODO: Support a session with more than one pre-install hook.
Nikita Ioffe264c4212019-09-13 16:30:17 +010069 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) {
Mohammad Samiul Islambd6ab0f2019-06-20 15:55:27 +010073 return Error() << "Missing support for multiple " << name << " hooks";
Andreas Gampee44b5792018-12-13 15:48:45 -080074 }
Nikita Ioffe264c4212019-09-13 16:30:17 +010075 hook_idx = i;
Andreas Gampee44b5792018-12-13 15:48:45 -080076 }
Andreas Gampea00c5452018-12-10 13:38:33 -080077 }
Nikita Ioffe264c4212019-09-13 16:30:17 +010078 CHECK(hook_idx != -1);
79 LOG(VERBOSE) << name << " for " << apexes[hook_idx].GetPath();
Andreas Gampea00c5452018-12-10 13:38:33 -080080
Nikita Ioffe264c4212019-09-13 16:30:17 +010081 std::vector<MountedApexData> mounted_apexes;
Andreas Gampee44b5792018-12-13 15:48:45 -080082 std::vector<std::string> activation_dirs;
83 auto preinstall_guard = android::base::make_scope_guard([&]() {
Nikita Ioffe264c4212019-09-13 16:30:17 +010084 for (const auto& mount : mounted_apexes) {
85 Result<void> st = apexd_private::Unmount(mount);
Bernie Innocentid04d5d02020-02-06 22:01:51 +090086 if (!st.ok()) {
Nikita Ioffe264c4212019-09-13 16:30:17 +010087 LOG(ERROR) << "Failed to unmount " << mount.full_path << " from "
88 << mount.mount_point << " after " << name << ": "
89 << st.error();
Andreas Gampee44b5792018-12-13 15:48:45 -080090 }
91 }
92 for (const std::string& active_point : activation_dirs) {
93 if (0 != rmdir(active_point.c_str())) {
94 PLOG(ERROR) << "Could not delete temporary active point "
95 << active_point;
96 }
Andreas Gampe4510d492018-12-12 15:56:05 -080097 }
Andreas Gampee1a40392018-11-30 09:47:17 -080098 });
99
Andreas Gampee44b5792018-12-13 15:48:45 -0800100 for (const ApexFile& apex : apexes) {
Nikita Ioffe264c4212019-09-13 16:30:17 +0100101 // 1) Mount the package.
Andreas Gampee44b5792018-12-13 15:48:45 -0800102 std::string mount_point =
Nikita Ioffe264c4212019-09-13 16:30:17 +0100103 apexd_private::GetPackageTempMountPoint(apex.GetManifest());
Andreas Gampee1a40392018-11-30 09:47:17 -0800104
Nikita Ioffe264c4212019-09-13 16:30:17 +0100105 auto mount_data = apexd_private::TempMountPackage(apex, mount_point);
Bernie Innocentid04d5d02020-02-06 22:01:51 +0900106 if (!mount_data.ok()) {
Nikita Ioffe264c4212019-09-13 16:30:17 +0100107 return mount_data.error();
Andreas Gampee1a40392018-11-30 09:47:17 -0800108 }
Nikita Ioffe264c4212019-09-13 16:30:17 +0100109 mounted_apexes.push_back(std::move(*mount_data));
Andreas Gampee1a40392018-11-30 09:47:17 -0800110
Nikita Ioffe264c4212019-09-13 16:30:17 +0100111 // Given the fact, that we only allow updates of existing APEXes, all the
112 // activation points will always be already created. Only scenario, when it
113 // won't be the case might be apexservice_test. But even then, it might be
114 // safer to move active_point creation logic to run after unshare.
115 // TODO(ioffe): move creation of activation points inside RunFnInstall?
Andreas Gampee44b5792018-12-13 15:48:45 -0800116 // 2) Ensure there is an activation point, and we will clean it up.
117 std::string active_point =
118 apexd_private::GetActiveMountPoint(apex.GetManifest());
119 if (0 == mkdir(active_point.c_str(), kMkdirMode)) {
120 activation_dirs.emplace_back(std::move(active_point));
121 } else {
Andreas Gampeb48122e2018-12-11 15:48:42 -0800122 int saved_errno = errno;
Andreas Gampeb48122e2018-12-11 15:48:42 -0800123 if (saved_errno != EEXIST) {
Mohammad Samiul Islambd6ab0f2019-06-20 15:55:27 +0100124 return Error() << "Unable to create mount point" << active_point << ": "
125 << strerror(saved_errno);
Andreas Gampeb48122e2018-12-11 15:48:42 -0800126 }
127 }
128 }
129
130 // 3) Create invocation args.
Andreas Gampee1a40392018-11-30 09:47:17 -0800131 std::vector<std::string> args{
Andreas Gampef7663552019-01-03 09:22:11 -0800132 "/system/bin/apexd", arg,
Nikita Ioffe264c4212019-09-13 16:30:17 +0100133 mounted_apexes[hook_idx].mount_point, // Make the APEX with hook first.
Andreas Gampee1a40392018-11-30 09:47:17 -0800134 };
Nikita Ioffe264c4212019-09-13 16:30:17 +0100135 for (size_t i = 0; i < mounted_apexes.size(); i++) {
136 if ((int)i != hook_idx) {
137 args.push_back(mounted_apexes[i].mount_point);
Andreas Gampee44b5792018-12-13 15:48:45 -0800138 }
139 }
140
Andreas Gampee1a40392018-11-30 09:47:17 -0800141 std::string error_msg;
142 int res = ForkAndRun(args, &error_msg);
Mohammad Samiul Islambd6ab0f2019-06-20 15:55:27 +0100143 return res == 0 ? Result<void>{} : Error() << error_msg;
Andreas Gampee1a40392018-11-30 09:47:17 -0800144}
145
Andreas Gampef7663552019-01-03 09:22:11 -0800146template <typename Fn>
147int RunFnInstall(char** in_argv, Fn fn, const char* name) {
Nick Kralevich3501c7b2019-02-28 10:20:44 -0800148 // 1) Unshare.
Andreas Gampee1a40392018-11-30 09:47:17 -0800149 if (unshare(CLONE_NEWNS) != 0) {
Andreas Gampef7663552019-01-03 09:22:11 -0800150 PLOG(ERROR) << "Failed to unshare() for apex " << name;
Andreas Gampee1a40392018-11-30 09:47:17 -0800151 _exit(200);
152 }
153
Nick Kralevich3501c7b2019-02-28 10:20:44 -0800154 // 2) Make everything private, so that our (and hook's) changes do not
Andreas Gampe4ead9492019-02-08 13:08:43 -0800155 // propagate.
156 if (mount(nullptr, "/", nullptr, MS_PRIVATE | MS_REC, nullptr) == -1) {
Andreas Gampee1a40392018-11-30 09:47:17 -0800157 PLOG(ERROR) << "Failed to mount private.";
158 _exit(201);
159 }
160
Andreas Gampee44b5792018-12-13 15:48:45 -0800161 std::string hook_path;
Andreas Gampee1a40392018-11-30 09:47:17 -0800162 {
Nikita Ioffe264c4212019-09-13 16:30:17 +0100163 auto bind_fn = [&fn, name](const std::string& mount_point) {
Andreas Gampef7663552019-01-03 09:22:11 -0800164 std::string hook;
Andreas Gampee44b5792018-12-13 15:48:45 -0800165 std::string active_point;
166 {
Nikita Ioffe264c4212019-09-13 16:30:17 +0100167 Result<ApexManifest> manifest_or =
Dario Frenia277bdf2019-11-05 22:37:49 +0000168 ReadManifest(mount_point + "/" + kManifestFilenamePb);
Bernie Innocentid04d5d02020-02-06 22:01:51 +0900169 if (!manifest_or.ok()) {
Dario Frenia277bdf2019-11-05 22:37:49 +0000170 LOG(ERROR) << "Could not read manifest from " << mount_point << "/"
171 << kManifestFilenamePb << " for " << name << ": "
172 << manifest_or.error();
173 // Fallback to Json manifest if present.
174 LOG(ERROR) << "Trying to find a JSON manifest";
175 manifest_or = ReadManifest(mount_point + "/" + kManifestFilenameJson);
Bernie Innocentid04d5d02020-02-06 22:01:51 +0900176 if (!manifest_or.ok()) {
Dario Frenia277bdf2019-11-05 22:37:49 +0000177 LOG(ERROR) << "Could not read manifest from " << mount_point << "/"
178 << kManifestFilenameJson << " for " << name << ": "
179 << manifest_or.error();
180 _exit(202);
181 }
Andreas Gampee44b5792018-12-13 15:48:45 -0800182 }
Nikita Ioffe264c4212019-09-13 16:30:17 +0100183 const auto& manifest = *manifest_or;
Andreas Gampef7663552019-01-03 09:22:11 -0800184 hook = (manifest.*fn)();
Andreas Gampee44b5792018-12-13 15:48:45 -0800185 active_point = apexd_private::GetActiveMountPoint(manifest);
186 }
187
Nick Kralevich3501c7b2019-02-28 10:20:44 -0800188 // 3) Activate the new apex.
Mohammad Samiul Islambd6ab0f2019-06-20 15:55:27 +0100189 Result<void> bind_status =
190 apexd_private::BindMount(active_point, mount_point);
Bernie Innocentid04d5d02020-02-06 22:01:51 +0900191 if (!bind_status.ok()) {
Andreas Gampee44b5792018-12-13 15:48:45 -0800192 LOG(ERROR) << "Failed to bind-mount " << mount_point << " to "
Mohammad Samiul Islambd6ab0f2019-06-20 15:55:27 +0100193 << active_point << ": " << bind_status.error();
Andreas Gampee44b5792018-12-13 15:48:45 -0800194 _exit(203);
195 }
196
Andreas Gampef7663552019-01-03 09:22:11 -0800197 return std::make_pair(active_point, hook);
Andreas Gampee44b5792018-12-13 15:48:45 -0800198 };
199
200 // First/main APEX.
Andreas Gampef7663552019-01-03 09:22:11 -0800201 auto [active_point, hook] = bind_fn(in_argv[2]);
202 hook_path = active_point + "/" + hook;
Andreas Gampee44b5792018-12-13 15:48:45 -0800203
204 for (size_t i = 3;; ++i) {
205 if (in_argv[i] == nullptr) {
206 break;
207 }
208 bind_fn(in_argv[i]); // Ignore result, hook will be empty.
Andreas Gampee1a40392018-11-30 09:47:17 -0800209 }
Andreas Gampee1a40392018-11-30 09:47:17 -0800210 }
211
Nick Kralevich3501c7b2019-02-28 10:20:44 -0800212 // 4) Run the hook.
Andreas Gampee1a40392018-11-30 09:47:17 -0800213
214 // For now, just run sh. But this probably needs to run the new linker.
215 std::vector<std::string> args{
Andreas Gampee44b5792018-12-13 15:48:45 -0800216 hook_path,
Andreas Gampee1a40392018-11-30 09:47:17 -0800217 };
218 std::vector<const char*> argv;
219 argv.resize(args.size() + 1, nullptr);
220 std::transform(args.begin(), args.end(), argv.begin(),
221 [](const std::string& in) { return in.c_str(); });
222
223 LOG(ERROR) << "execv of " << android::base::Join(args, " ");
224
Nick Kralevich3501c7b2019-02-28 10:20:44 -0800225 // Close all file descriptors. They are coming from the caller, we do not
226 // want to pass them on across our fork/exec into a different domain.
227 CloseSTDDescriptors();
228
Andreas Gampee1a40392018-11-30 09:47:17 -0800229 execv(argv[0], const_cast<char**>(argv.data()));
230 PLOG(ERROR) << "execv of " << android::base::Join(args, " ") << " failed";
231 _exit(204);
232}
233
Andreas Gampef7663552019-01-03 09:22:11 -0800234} // namespace
235
Mohammad Samiul Islambd6ab0f2019-06-20 15:55:27 +0100236Result<void> StagePreInstall(const std::vector<ApexFile>& apexes) {
Abhijeet Kaur216e36c2019-01-04 10:15:01 +0000237 return StageFnInstall(apexes, &ApexManifest::preinstallhook, "--pre-install",
238 "pre-install");
Andreas Gampef7663552019-01-03 09:22:11 -0800239}
240
241int RunPreInstall(char** in_argv) {
Abhijeet Kaur216e36c2019-01-04 10:15:01 +0000242 return RunFnInstall(in_argv, &ApexManifest::preinstallhook, "pre-install");
Andreas Gampef7663552019-01-03 09:22:11 -0800243}
244
Mohammad Samiul Islambd6ab0f2019-06-20 15:55:27 +0100245Result<void> StagePostInstall(const std::vector<ApexFile>& apexes) {
Abhijeet Kaur216e36c2019-01-04 10:15:01 +0000246 return StageFnInstall(apexes, &ApexManifest::postinstallhook,
Andreas Gampef7663552019-01-03 09:22:11 -0800247 "--post-install", "post-install");
248}
249
250int RunPostInstall(char** in_argv) {
Abhijeet Kaur216e36c2019-01-04 10:15:01 +0000251 return RunFnInstall(in_argv, &ApexManifest::postinstallhook, "post-install");
Andreas Gampef7663552019-01-03 09:22:11 -0800252}
253
Andreas Gampee1a40392018-11-30 09:47:17 -0800254} // namespace apex
255} // namespace android