blob: 46c3fd7aa726ea54ea8ecc4c73bc4a234f85bf74 [file] [log] [blame]
Andreas Gampe01ad5982016-03-09 16:27:29 -08001/*
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 Gamped089ca12016-06-27 14:25:30 -070017#include <fcntl.h>
Andreas Gampe01ad5982016-03-09 16:27:29 -080018#include <linux/unistd.h>
19#include <sys/mount.h>
Roland Levillainc19c6042018-12-18 12:15:12 +000020#include <sys/stat.h>
Andreas Gampe01ad5982016-03-09 16:27:29 -080021#include <sys/wait.h>
22
Andreas Gamped089ca12016-06-27 14:25:30 -070023#include <sstream>
24
Andreas Gampe01ad5982016-03-09 16:27:29 -080025#include <android-base/logging.h>
26#include <android-base/macros.h>
27#include <android-base/stringprintf.h>
Andreas Gampee90127d2019-03-22 11:21:34 -070028#include <libdm/dm.h>
Roland Levillainc19c6042018-12-18 12:15:12 +000029#include <selinux/android.h>
30
Nikita Ioffe35d7d4c2021-03-03 19:21:06 +000031#include <apex_file_repository.h>
Roland Levillainc19c6042018-12-18 12:15:12 +000032#include <apexd.h>
Andreas Gampe01ad5982016-03-09 16:27:29 -080033
Jeff Sharkey0274c972016-12-06 09:32:04 -070034#include "installd_constants.h"
35#include "otapreopt_utils.h"
Andreas Gampe548bdb92016-06-02 17:56:45 -070036
Andreas Gampe01ad5982016-03-09 16:27:29 -080037#ifndef LOG_TAG
38#define LOG_TAG "otapreopt"
39#endif
40
41using android::base::StringPrintf;
42
43namespace android {
44namespace installd {
45
Andreas Gamped089ca12016-06-27 14:25:30 -070046static void CloseDescriptor(int fd) {
47 if (fd >= 0) {
48 int result = close(fd);
49 UNUSED(result); // Ignore result. Printing to logcat will open a new descriptor
50 // that we do *not* want.
51 }
52}
53
54static void CloseDescriptor(const char* descriptor_string) {
55 int fd = -1;
56 std::istringstream stream(descriptor_string);
57 stream >> fd;
58 if (!stream.fail()) {
59 CloseDescriptor(fd);
60 }
61}
62
Roland Levillain6520cca2019-02-01 13:15:58 +000063static std::vector<apex::ApexFile> ActivateApexPackages() {
64 // The logic here is (partially) copied and adapted from
Jooyung Han361210e2019-12-05 18:14:39 +090065 // system/apex/apexd/apexd.cpp.
Roland Levillain6520cca2019-02-01 13:15:58 +000066 //
Jooyung Han361210e2019-12-05 18:14:39 +090067 // Only scan the APEX directory under /system, /system_ext and /vendor (within the chroot dir).
Nikita Ioffe35d7d4c2021-03-03 19:21:06 +000068 std::vector<std::string> apex_dirs{apex::kApexPackageSystemDir, apex::kApexPackageSystemExtDir,
Jooyung Han361210e2019-12-05 18:14:39 +090069 apex::kApexPackageVendorDir};
Nikita Ioffe35d7d4c2021-03-03 19:21:06 +000070 // Initialize ApexFileRepository used internally in ScanPackagesDirAndActivate.
71 // This is a quick fix to fix apex activation in otapreopt_chroot.
72 apex::ApexFileRepository::GetInstance().AddPreInstalledApex(apex_dirs);
Jooyung Han361210e2019-12-05 18:14:39 +090073 for (const auto& dir : apex_dirs) {
74 // Cast call to void to suppress warn_unused_result.
Nikita Ioffe35d7d4c2021-03-03 19:21:06 +000075 static_cast<void>(apex::ScanPackagesDirAndActivate(dir.c_str()));
Jooyung Han361210e2019-12-05 18:14:39 +090076 }
Nikita Ioffeb2342d82020-12-22 20:23:34 +000077 return apex::GetActivePackages();
Roland Levillain6520cca2019-02-01 13:15:58 +000078}
79
80static void DeactivateApexPackages(const std::vector<apex::ApexFile>& active_packages) {
81 for (const apex::ApexFile& apex_file : active_packages) {
82 const std::string& package_path = apex_file.GetPath();
Nikita Ioffeb2342d82020-12-22 20:23:34 +000083 base::Result<void> status = apex::DeactivatePackage(package_path);
Bernie Innocenti018138f2020-02-06 21:39:04 +090084 if (!status.ok()) {
Mohammad Samiul Islam7dcfd342019-06-25 10:25:30 +010085 LOG(ERROR) << "Failed to deactivate " << package_path << ": "
86 << status.error();
Roland Levillain6520cca2019-02-01 13:15:58 +000087 }
88 }
89}
90
Andreas Gampe54b62c92019-03-21 11:34:19 -070091static void TryExtraMount(const char* name, const char* slot, const char* target) {
Andreas Gampee90127d2019-03-22 11:21:34 -070092 std::string partition_name = StringPrintf("%s%s", name, slot);
93
94 // See whether update_engine mounted a logical partition.
95 {
96 auto& dm = dm::DeviceMapper::Instance();
97 if (dm.GetState(partition_name) != dm::DmDeviceState::INVALID) {
98 std::string path;
99 if (dm.GetDmDevicePathByName(partition_name, &path)) {
100 int mount_result = mount(path.c_str(),
101 target,
102 "ext4",
103 MS_RDONLY,
104 /* data */ nullptr);
105 if (mount_result == 0) {
106 return;
107 }
108 }
109 }
110 }
111
112 // Fall back and attempt a direct mount.
113 std::string block_device = StringPrintf("/dev/block/by-name/%s", partition_name.c_str());
Andreas Gampe54b62c92019-03-21 11:34:19 -0700114 int mount_result = mount(block_device.c_str(),
115 target,
116 "ext4",
117 MS_RDONLY,
118 /* data */ nullptr);
119 UNUSED(mount_result);
120}
121
Andreas Gamped089ca12016-06-27 14:25:30 -0700122// Entry for otapreopt_chroot. Expected parameters are:
123// [cmd] [status-fd] [target-slot] "dexopt" [dexopt-params]
124// The file descriptor denoted by status-fd will be closed. The rest of the parameters will
125// be passed on to otapreopt in the chroot.
Andreas Gampe01ad5982016-03-09 16:27:29 -0800126static int otapreopt_chroot(const int argc, char **arg) {
Zach Riggle318853a2018-01-18 18:34:04 -0600127 // Validate arguments
128 // We need the command, status channel and target slot, at a minimum.
129 if(argc < 3) {
130 PLOG(ERROR) << "Not enough arguments.";
131 exit(208);
132 }
Andreas Gamped089ca12016-06-27 14:25:30 -0700133 // Close all file descriptors. They are coming from the caller, we do not want to pass them
134 // on across our fork/exec into a different domain.
135 // 1) Default descriptors.
136 CloseDescriptor(STDIN_FILENO);
137 CloseDescriptor(STDOUT_FILENO);
138 CloseDescriptor(STDERR_FILENO);
139 // 2) The status channel.
140 CloseDescriptor(arg[1]);
141
Andreas Gampe01ad5982016-03-09 16:27:29 -0800142 // We need to run the otapreopt tool from the postinstall partition. As such, set up a
143 // mount namespace and change root.
144
145 // Create our own mount namespace.
146 if (unshare(CLONE_NEWNS) != 0) {
147 PLOG(ERROR) << "Failed to unshare() for otapreopt.";
148 exit(200);
149 }
150
151 // Make postinstall private, so that our changes don't propagate.
152 if (mount("", "/postinstall", nullptr, MS_PRIVATE, nullptr) != 0) {
153 PLOG(ERROR) << "Failed to mount private.";
154 exit(201);
155 }
156
157 // Bind mount necessary directories.
158 constexpr const char* kBindMounts[] = {
159 "/data", "/dev", "/proc", "/sys"
160 };
161 for (size_t i = 0; i < arraysize(kBindMounts); ++i) {
162 std::string trg = StringPrintf("/postinstall%s", kBindMounts[i]);
163 if (mount(kBindMounts[i], trg.c_str(), nullptr, MS_BIND, nullptr) != 0) {
164 PLOG(ERROR) << "Failed to bind-mount " << kBindMounts[i];
165 exit(202);
166 }
167 }
168
Andreas Gampefd12eda2016-07-12 09:47:17 -0700169 // Try to mount the vendor partition. update_engine doesn't do this for us, but we
170 // want it for vendor APKs.
171 // Notes:
172 // 1) We pretty much guess a name here and hope to find the partition by name.
173 // It is just as complicated and brittle to scan /proc/mounts. But this requires
174 // validating the target-slot so as not to try to mount some totally random path.
175 // 2) We're in a mount namespace here, so when we die, this will be cleaned up.
176 // 3) Ignore errors. Printing anything at this stage will open a file descriptor
177 // for logging.
178 if (!ValidateTargetSlotSuffix(arg[2])) {
179 LOG(ERROR) << "Target slot suffix not legal: " << arg[2];
180 exit(207);
181 }
Andreas Gampe54b62c92019-03-21 11:34:19 -0700182 TryExtraMount("vendor", arg[2], "/postinstall/vendor");
Andreas Gampeb87a1c72018-04-13 17:42:23 -0700183
184 // Try to mount the product partition. update_engine doesn't do this for us, but we
185 // want it for product APKs. Same notes as vendor above.
Andreas Gampe54b62c92019-03-21 11:34:19 -0700186 TryExtraMount("product", arg[2], "/postinstall/product");
Andreas Gampefd12eda2016-07-12 09:47:17 -0700187
Alex Light2b307a02021-03-03 18:35:47 -0800188 constexpr const char* kPostInstallLinkerconfig = "/postinstall/linkerconfig";
189 // Try to mount /postinstall/linkerconfig. we will set it up after performing the chroot
190 if (mount("tmpfs", kPostInstallLinkerconfig, "tmpfs", 0, nullptr) != 0) {
191 PLOG(ERROR) << "Failed to mount a tmpfs for " << kPostInstallLinkerconfig;
192 exit(215);
193 }
194
Roland Levillainc19c6042018-12-18 12:15:12 +0000195 // Setup APEX mount point and its security context.
196 static constexpr const char* kPostinstallApexDir = "/postinstall/apex";
197 // The following logic is similar to the one in system/core/rootdir/init.rc:
198 //
199 // mount tmpfs tmpfs /apex nodev noexec nosuid
200 // chmod 0755 /apex
201 // chown root root /apex
202 // restorecon /apex
203 //
Roland Levillain8d276812019-01-24 10:51:30 +0000204 // except we perform the `restorecon` step just after mounting the tmpfs
205 // filesystem in /postinstall/apex, so that this directory is correctly
206 // labeled (with type `postinstall_apex_mnt_dir`) and may be manipulated in
207 // following operations (`chmod`, `chown`, etc.) following policies
208 // restricted to `postinstall_apex_mnt_dir`:
209 //
210 // mount tmpfs tmpfs /postinstall/apex nodev noexec nosuid
211 // restorecon /postinstall/apex
212 // chmod 0755 /postinstall/apex
213 // chown root root /postinstall/apex
214 //
Roland Levillainc19c6042018-12-18 12:15:12 +0000215 if (mount("tmpfs", kPostinstallApexDir, "tmpfs", MS_NODEV | MS_NOEXEC | MS_NOSUID, nullptr)
216 != 0) {
217 PLOG(ERROR) << "Failed to mount tmpfs in " << kPostinstallApexDir;
218 exit(209);
219 }
Roland Levillain8d276812019-01-24 10:51:30 +0000220 if (selinux_android_restorecon(kPostinstallApexDir, 0) < 0) {
221 PLOG(ERROR) << "Failed to restorecon " << kPostinstallApexDir;
222 exit(214);
223 }
Roland Levillainc19c6042018-12-18 12:15:12 +0000224 if (chmod(kPostinstallApexDir, 0755) != 0) {
225 PLOG(ERROR) << "Failed to chmod " << kPostinstallApexDir << " to 0755";
226 exit(210);
227 }
228 if (chown(kPostinstallApexDir, 0, 0) != 0) {
229 PLOG(ERROR) << "Failed to chown " << kPostinstallApexDir << " to root:root";
230 exit(211);
231 }
Roland Levillainc19c6042018-12-18 12:15:12 +0000232
Andreas Gampe01ad5982016-03-09 16:27:29 -0800233 // Chdir into /postinstall.
234 if (chdir("/postinstall") != 0) {
235 PLOG(ERROR) << "Unable to chdir into /postinstall.";
236 exit(203);
237 }
238
239 // Make /postinstall the root in our mount namespace.
240 if (chroot(".") != 0) {
241 PLOG(ERROR) << "Failed to chroot";
242 exit(204);
243 }
244
245 if (chdir("/") != 0) {
246 PLOG(ERROR) << "Unable to chdir into /.";
247 exit(205);
248 }
249
Roland Levillainc19c6042018-12-18 12:15:12 +0000250 // Try to mount APEX packages in "/apex" in the chroot dir. We need at least
Martin Stjernholmf4caaa02019-07-17 22:14:14 +0100251 // the ART APEX, as it is required by otapreopt to run dex2oat.
Roland Levillain6520cca2019-02-01 13:15:58 +0000252 std::vector<apex::ApexFile> active_packages = ActivateApexPackages();
Roland Levillainc19c6042018-12-18 12:15:12 +0000253
Martin Stjernholmf4caaa02019-07-17 22:14:14 +0100254 // Check that an ART APEX has been activated; clean up and exit
Roland Levillain8a8ca152019-07-11 18:48:05 +0100255 // early otherwise.
Alex Light2b307a02021-03-03 18:35:47 -0800256 static constexpr const std::string_view kRequiredApexs[] = {
257 "com.android.art",
258 "com.android.runtime",
259 };
260 for (std::string_view apex : kRequiredApexs) {
261 if (std::none_of(active_packages.begin(), active_packages.end(),
262 [&](const apex::ApexFile& package) {
263 return package.GetManifest().name() == apex;
264 })) {
265 LOG(FATAL_WITHOUT_ABORT) << "No activated " << apex << " APEX package.";
266 DeactivateApexPackages(active_packages);
267 exit(217);
268 }
269 }
270
271 // Setup /linkerconfig. Doing it after the chroot means it doesn't need its own category
272 if (selinux_android_restorecon("/linkerconfig", 0) < 0) {
273 PLOG(ERROR) << "Failed to restorecon /linkerconfig";
274 exit(219);
275 }
276 std::vector<std::string> linkerconfig_cmd{"/apex/com.android.runtime/bin/linkerconfig",
277 "--target", "/linkerconfig"};
278 std::string linkerconfig_error_msg;
279 bool linkerconfig_exec_result = Exec(linkerconfig_cmd, &linkerconfig_error_msg);
280 if (!linkerconfig_exec_result) {
281 LOG(ERROR) << "Running linkerconfig failed: " << linkerconfig_error_msg;
282 exit(218);
Roland Levillain8a8ca152019-07-11 18:48:05 +0100283 }
284
Andreas Gampe01ad5982016-03-09 16:27:29 -0800285 // Now go on and run otapreopt.
286
Roland Levillain94b41802019-01-18 11:56:50 +0000287 // Incoming: cmd + status-fd + target-slot + cmd... | Incoming | = argc
288 // Outgoing: cmd + target-slot + cmd... | Outgoing | = argc - 1
289 std::vector<std::string> cmd;
290 cmd.reserve(argc);
291 cmd.push_back("/system/bin/otapreopt");
Andreas Gamped089ca12016-06-27 14:25:30 -0700292
293 // The first parameter is the status file descriptor, skip.
Roland Levillain94b41802019-01-18 11:56:50 +0000294 for (size_t i = 2; i < static_cast<size_t>(argc); ++i) {
295 cmd.push_back(arg[i]);
Andreas Gamped089ca12016-06-27 14:25:30 -0700296 }
Andreas Gampe01ad5982016-03-09 16:27:29 -0800297
Roland Levillain94b41802019-01-18 11:56:50 +0000298 // Fork and execute otapreopt in its own process.
299 std::string error_msg;
300 bool exec_result = Exec(cmd, &error_msg);
301 if (!exec_result) {
302 LOG(ERROR) << "Running otapreopt failed: " << error_msg;
303 }
304
Roland Levillain6520cca2019-02-01 13:15:58 +0000305 // Tear down the work down by the apexd logic. (i.e. deactivate packages).
306 DeactivateApexPackages(active_packages);
Roland Levillain94b41802019-01-18 11:56:50 +0000307
308 if (!exec_result) {
309 exit(213);
310 }
311
312 return 0;
Andreas Gampe01ad5982016-03-09 16:27:29 -0800313}
314
315} // namespace installd
316} // namespace android
317
318int main(const int argc, char *argv[]) {
319 return android::installd::otapreopt_chroot(argc, argv);
320}