blob: fb07840730fabd236b65bdf057bae1dd8750d6c3 [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
Roland Levillainc19c6042018-12-18 12:15:12 +0000188 // Setup APEX mount point and its security context.
189 static constexpr const char* kPostinstallApexDir = "/postinstall/apex";
190 // The following logic is similar to the one in system/core/rootdir/init.rc:
191 //
192 // mount tmpfs tmpfs /apex nodev noexec nosuid
193 // chmod 0755 /apex
194 // chown root root /apex
195 // restorecon /apex
196 //
Roland Levillain8d276812019-01-24 10:51:30 +0000197 // except we perform the `restorecon` step just after mounting the tmpfs
198 // filesystem in /postinstall/apex, so that this directory is correctly
199 // labeled (with type `postinstall_apex_mnt_dir`) and may be manipulated in
200 // following operations (`chmod`, `chown`, etc.) following policies
201 // restricted to `postinstall_apex_mnt_dir`:
202 //
203 // mount tmpfs tmpfs /postinstall/apex nodev noexec nosuid
204 // restorecon /postinstall/apex
205 // chmod 0755 /postinstall/apex
206 // chown root root /postinstall/apex
207 //
Roland Levillainc19c6042018-12-18 12:15:12 +0000208 if (mount("tmpfs", kPostinstallApexDir, "tmpfs", MS_NODEV | MS_NOEXEC | MS_NOSUID, nullptr)
209 != 0) {
210 PLOG(ERROR) << "Failed to mount tmpfs in " << kPostinstallApexDir;
211 exit(209);
212 }
Roland Levillain8d276812019-01-24 10:51:30 +0000213 if (selinux_android_restorecon(kPostinstallApexDir, 0) < 0) {
214 PLOG(ERROR) << "Failed to restorecon " << kPostinstallApexDir;
215 exit(214);
216 }
Roland Levillainc19c6042018-12-18 12:15:12 +0000217 if (chmod(kPostinstallApexDir, 0755) != 0) {
218 PLOG(ERROR) << "Failed to chmod " << kPostinstallApexDir << " to 0755";
219 exit(210);
220 }
221 if (chown(kPostinstallApexDir, 0, 0) != 0) {
222 PLOG(ERROR) << "Failed to chown " << kPostinstallApexDir << " to root:root";
223 exit(211);
224 }
Roland Levillainc19c6042018-12-18 12:15:12 +0000225
Andreas Gampe01ad5982016-03-09 16:27:29 -0800226 // Chdir into /postinstall.
227 if (chdir("/postinstall") != 0) {
228 PLOG(ERROR) << "Unable to chdir into /postinstall.";
229 exit(203);
230 }
231
232 // Make /postinstall the root in our mount namespace.
233 if (chroot(".") != 0) {
234 PLOG(ERROR) << "Failed to chroot";
235 exit(204);
236 }
237
238 if (chdir("/") != 0) {
239 PLOG(ERROR) << "Unable to chdir into /.";
240 exit(205);
241 }
242
Roland Levillainc19c6042018-12-18 12:15:12 +0000243 // Try to mount APEX packages in "/apex" in the chroot dir. We need at least
Martin Stjernholmf4caaa02019-07-17 22:14:14 +0100244 // the ART APEX, as it is required by otapreopt to run dex2oat.
Roland Levillain6520cca2019-02-01 13:15:58 +0000245 std::vector<apex::ApexFile> active_packages = ActivateApexPackages();
Roland Levillainc19c6042018-12-18 12:15:12 +0000246
Martin Stjernholmf4caaa02019-07-17 22:14:14 +0100247 // Check that an ART APEX has been activated; clean up and exit
Roland Levillain8a8ca152019-07-11 18:48:05 +0100248 // early otherwise.
249 if (std::none_of(active_packages.begin(),
250 active_packages.end(),
251 [](const apex::ApexFile& package){
Martin Stjernholmf4caaa02019-07-17 22:14:14 +0100252 return package.GetManifest().name() == "com.android.art";
Roland Levillain8a8ca152019-07-11 18:48:05 +0100253 })) {
Martin Stjernholmf4caaa02019-07-17 22:14:14 +0100254 LOG(FATAL_WITHOUT_ABORT) << "No activated com.android.art APEX package.";
Roland Levillain8a8ca152019-07-11 18:48:05 +0100255 DeactivateApexPackages(active_packages);
256 exit(217);
257 }
258
Andreas Gampe01ad5982016-03-09 16:27:29 -0800259 // Now go on and run otapreopt.
260
Roland Levillain94b41802019-01-18 11:56:50 +0000261 // Incoming: cmd + status-fd + target-slot + cmd... | Incoming | = argc
262 // Outgoing: cmd + target-slot + cmd... | Outgoing | = argc - 1
263 std::vector<std::string> cmd;
264 cmd.reserve(argc);
265 cmd.push_back("/system/bin/otapreopt");
Andreas Gamped089ca12016-06-27 14:25:30 -0700266
267 // The first parameter is the status file descriptor, skip.
Roland Levillain94b41802019-01-18 11:56:50 +0000268 for (size_t i = 2; i < static_cast<size_t>(argc); ++i) {
269 cmd.push_back(arg[i]);
Andreas Gamped089ca12016-06-27 14:25:30 -0700270 }
Andreas Gampe01ad5982016-03-09 16:27:29 -0800271
Roland Levillain94b41802019-01-18 11:56:50 +0000272 // Fork and execute otapreopt in its own process.
273 std::string error_msg;
274 bool exec_result = Exec(cmd, &error_msg);
275 if (!exec_result) {
276 LOG(ERROR) << "Running otapreopt failed: " << error_msg;
277 }
278
Roland Levillain6520cca2019-02-01 13:15:58 +0000279 // Tear down the work down by the apexd logic. (i.e. deactivate packages).
280 DeactivateApexPackages(active_packages);
Roland Levillain94b41802019-01-18 11:56:50 +0000281
282 if (!exec_result) {
283 exit(213);
284 }
285
286 return 0;
Andreas Gampe01ad5982016-03-09 16:27:29 -0800287}
288
289} // namespace installd
290} // namespace android
291
292int main(const int argc, char *argv[]) {
293 return android::installd::otapreopt_chroot(argc, argv);
294}