blob: 3ff9d118067e3b0808083c847e5fd46de84cd7c9 [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
31#include <apexd.h>
Andreas Gampe01ad5982016-03-09 16:27:29 -080032
Jeff Sharkey0274c972016-12-06 09:32:04 -070033#include "installd_constants.h"
34#include "otapreopt_utils.h"
Andreas Gampe548bdb92016-06-02 17:56:45 -070035
Andreas Gampe01ad5982016-03-09 16:27:29 -080036#ifndef LOG_TAG
37#define LOG_TAG "otapreopt"
38#endif
39
40using android::base::StringPrintf;
41
42namespace android {
43namespace installd {
44
Andreas Gamped089ca12016-06-27 14:25:30 -070045static void CloseDescriptor(int fd) {
46 if (fd >= 0) {
47 int result = close(fd);
48 UNUSED(result); // Ignore result. Printing to logcat will open a new descriptor
49 // that we do *not* want.
50 }
51}
52
53static void CloseDescriptor(const char* descriptor_string) {
54 int fd = -1;
55 std::istringstream stream(descriptor_string);
56 stream >> fd;
57 if (!stream.fail()) {
58 CloseDescriptor(fd);
59 }
60}
61
Roland Levillain6520cca2019-02-01 13:15:58 +000062static std::vector<apex::ApexFile> ActivateApexPackages() {
63 // The logic here is (partially) copied and adapted from
64 // system/apex/apexd/apexd_main.cpp.
65 //
66 // Only scan the APEX directory under /system (within the chroot dir).
Nikita Ioffebeec6272019-06-24 03:00:35 +010067 // Cast call to void to suppress warn_unused_result.
68 static_cast<void>(apex::scanPackagesDirAndActivate(apex::kApexPackageSystemDir));
Roland Levillain6520cca2019-02-01 13:15:58 +000069 return apex::getActivePackages();
70}
71
72static void DeactivateApexPackages(const std::vector<apex::ApexFile>& active_packages) {
73 for (const apex::ApexFile& apex_file : active_packages) {
74 const std::string& package_path = apex_file.GetPath();
Mohammad Samiul Islam7dcfd342019-06-25 10:25:30 +010075 base::Result<void> status = apex::deactivatePackage(package_path);
76 if (!status) {
77 LOG(ERROR) << "Failed to deactivate " << package_path << ": "
78 << status.error();
Roland Levillain6520cca2019-02-01 13:15:58 +000079 }
80 }
81}
82
Andreas Gampe54b62c92019-03-21 11:34:19 -070083static void TryExtraMount(const char* name, const char* slot, const char* target) {
Andreas Gampee90127d2019-03-22 11:21:34 -070084 std::string partition_name = StringPrintf("%s%s", name, slot);
85
86 // See whether update_engine mounted a logical partition.
87 {
88 auto& dm = dm::DeviceMapper::Instance();
89 if (dm.GetState(partition_name) != dm::DmDeviceState::INVALID) {
90 std::string path;
91 if (dm.GetDmDevicePathByName(partition_name, &path)) {
92 int mount_result = mount(path.c_str(),
93 target,
94 "ext4",
95 MS_RDONLY,
96 /* data */ nullptr);
97 if (mount_result == 0) {
98 return;
99 }
100 }
101 }
102 }
103
104 // Fall back and attempt a direct mount.
105 std::string block_device = StringPrintf("/dev/block/by-name/%s", partition_name.c_str());
Andreas Gampe54b62c92019-03-21 11:34:19 -0700106 int mount_result = mount(block_device.c_str(),
107 target,
108 "ext4",
109 MS_RDONLY,
110 /* data */ nullptr);
111 UNUSED(mount_result);
112}
113
Andreas Gamped089ca12016-06-27 14:25:30 -0700114// Entry for otapreopt_chroot. Expected parameters are:
115// [cmd] [status-fd] [target-slot] "dexopt" [dexopt-params]
116// The file descriptor denoted by status-fd will be closed. The rest of the parameters will
117// be passed on to otapreopt in the chroot.
Andreas Gampe01ad5982016-03-09 16:27:29 -0800118static int otapreopt_chroot(const int argc, char **arg) {
Zach Riggle318853a2018-01-18 18:34:04 -0600119 // Validate arguments
120 // We need the command, status channel and target slot, at a minimum.
121 if(argc < 3) {
122 PLOG(ERROR) << "Not enough arguments.";
123 exit(208);
124 }
Andreas Gamped089ca12016-06-27 14:25:30 -0700125 // Close all file descriptors. They are coming from the caller, we do not want to pass them
126 // on across our fork/exec into a different domain.
127 // 1) Default descriptors.
128 CloseDescriptor(STDIN_FILENO);
129 CloseDescriptor(STDOUT_FILENO);
130 CloseDescriptor(STDERR_FILENO);
131 // 2) The status channel.
132 CloseDescriptor(arg[1]);
133
Andreas Gampe01ad5982016-03-09 16:27:29 -0800134 // We need to run the otapreopt tool from the postinstall partition. As such, set up a
135 // mount namespace and change root.
136
137 // Create our own mount namespace.
138 if (unshare(CLONE_NEWNS) != 0) {
139 PLOG(ERROR) << "Failed to unshare() for otapreopt.";
140 exit(200);
141 }
142
143 // Make postinstall private, so that our changes don't propagate.
144 if (mount("", "/postinstall", nullptr, MS_PRIVATE, nullptr) != 0) {
145 PLOG(ERROR) << "Failed to mount private.";
146 exit(201);
147 }
148
149 // Bind mount necessary directories.
150 constexpr const char* kBindMounts[] = {
151 "/data", "/dev", "/proc", "/sys"
152 };
153 for (size_t i = 0; i < arraysize(kBindMounts); ++i) {
154 std::string trg = StringPrintf("/postinstall%s", kBindMounts[i]);
155 if (mount(kBindMounts[i], trg.c_str(), nullptr, MS_BIND, nullptr) != 0) {
156 PLOG(ERROR) << "Failed to bind-mount " << kBindMounts[i];
157 exit(202);
158 }
159 }
160
Andreas Gampefd12eda2016-07-12 09:47:17 -0700161 // Try to mount the vendor partition. update_engine doesn't do this for us, but we
162 // want it for vendor APKs.
163 // Notes:
164 // 1) We pretty much guess a name here and hope to find the partition by name.
165 // It is just as complicated and brittle to scan /proc/mounts. But this requires
166 // validating the target-slot so as not to try to mount some totally random path.
167 // 2) We're in a mount namespace here, so when we die, this will be cleaned up.
168 // 3) Ignore errors. Printing anything at this stage will open a file descriptor
169 // for logging.
170 if (!ValidateTargetSlotSuffix(arg[2])) {
171 LOG(ERROR) << "Target slot suffix not legal: " << arg[2];
172 exit(207);
173 }
Andreas Gampe54b62c92019-03-21 11:34:19 -0700174 TryExtraMount("vendor", arg[2], "/postinstall/vendor");
Andreas Gampeb87a1c72018-04-13 17:42:23 -0700175
176 // Try to mount the product partition. update_engine doesn't do this for us, but we
177 // want it for product APKs. Same notes as vendor above.
Andreas Gampe54b62c92019-03-21 11:34:19 -0700178 TryExtraMount("product", arg[2], "/postinstall/product");
Andreas Gampefd12eda2016-07-12 09:47:17 -0700179
Roland Levillainc19c6042018-12-18 12:15:12 +0000180 // Setup APEX mount point and its security context.
181 static constexpr const char* kPostinstallApexDir = "/postinstall/apex";
182 // The following logic is similar to the one in system/core/rootdir/init.rc:
183 //
184 // mount tmpfs tmpfs /apex nodev noexec nosuid
185 // chmod 0755 /apex
186 // chown root root /apex
187 // restorecon /apex
188 //
Roland Levillain8d276812019-01-24 10:51:30 +0000189 // except we perform the `restorecon` step just after mounting the tmpfs
190 // filesystem in /postinstall/apex, so that this directory is correctly
191 // labeled (with type `postinstall_apex_mnt_dir`) and may be manipulated in
192 // following operations (`chmod`, `chown`, etc.) following policies
193 // restricted to `postinstall_apex_mnt_dir`:
194 //
195 // mount tmpfs tmpfs /postinstall/apex nodev noexec nosuid
196 // restorecon /postinstall/apex
197 // chmod 0755 /postinstall/apex
198 // chown root root /postinstall/apex
199 //
Roland Levillainc19c6042018-12-18 12:15:12 +0000200 if (mount("tmpfs", kPostinstallApexDir, "tmpfs", MS_NODEV | MS_NOEXEC | MS_NOSUID, nullptr)
201 != 0) {
202 PLOG(ERROR) << "Failed to mount tmpfs in " << kPostinstallApexDir;
203 exit(209);
204 }
Roland Levillain8d276812019-01-24 10:51:30 +0000205 if (selinux_android_restorecon(kPostinstallApexDir, 0) < 0) {
206 PLOG(ERROR) << "Failed to restorecon " << kPostinstallApexDir;
207 exit(214);
208 }
Roland Levillainc19c6042018-12-18 12:15:12 +0000209 if (chmod(kPostinstallApexDir, 0755) != 0) {
210 PLOG(ERROR) << "Failed to chmod " << kPostinstallApexDir << " to 0755";
211 exit(210);
212 }
213 if (chown(kPostinstallApexDir, 0, 0) != 0) {
214 PLOG(ERROR) << "Failed to chown " << kPostinstallApexDir << " to root:root";
215 exit(211);
216 }
Roland Levillainc19c6042018-12-18 12:15:12 +0000217
Andreas Gampe01ad5982016-03-09 16:27:29 -0800218 // Chdir into /postinstall.
219 if (chdir("/postinstall") != 0) {
220 PLOG(ERROR) << "Unable to chdir into /postinstall.";
221 exit(203);
222 }
223
224 // Make /postinstall the root in our mount namespace.
225 if (chroot(".") != 0) {
226 PLOG(ERROR) << "Failed to chroot";
227 exit(204);
228 }
229
230 if (chdir("/") != 0) {
231 PLOG(ERROR) << "Unable to chdir into /.";
232 exit(205);
233 }
234
Roland Levillainc19c6042018-12-18 12:15:12 +0000235 // Try to mount APEX packages in "/apex" in the chroot dir. We need at least
Martin Stjernholmf4caaa02019-07-17 22:14:14 +0100236 // the ART APEX, as it is required by otapreopt to run dex2oat.
Roland Levillain6520cca2019-02-01 13:15:58 +0000237 std::vector<apex::ApexFile> active_packages = ActivateApexPackages();
Roland Levillainc19c6042018-12-18 12:15:12 +0000238
Martin Stjernholmf4caaa02019-07-17 22:14:14 +0100239 // Check that an ART APEX has been activated; clean up and exit
Roland Levillain8a8ca152019-07-11 18:48:05 +0100240 // early otherwise.
241 if (std::none_of(active_packages.begin(),
242 active_packages.end(),
243 [](const apex::ApexFile& package){
Martin Stjernholmf4caaa02019-07-17 22:14:14 +0100244 return package.GetManifest().name() == "com.android.art";
Roland Levillain8a8ca152019-07-11 18:48:05 +0100245 })) {
Martin Stjernholmf4caaa02019-07-17 22:14:14 +0100246 LOG(FATAL_WITHOUT_ABORT) << "No activated com.android.art APEX package.";
Roland Levillain8a8ca152019-07-11 18:48:05 +0100247 DeactivateApexPackages(active_packages);
248 exit(217);
249 }
250
Andreas Gampe01ad5982016-03-09 16:27:29 -0800251 // Now go on and run otapreopt.
252
Roland Levillain94b41802019-01-18 11:56:50 +0000253 // Incoming: cmd + status-fd + target-slot + cmd... | Incoming | = argc
254 // Outgoing: cmd + target-slot + cmd... | Outgoing | = argc - 1
255 std::vector<std::string> cmd;
256 cmd.reserve(argc);
257 cmd.push_back("/system/bin/otapreopt");
Andreas Gamped089ca12016-06-27 14:25:30 -0700258
259 // The first parameter is the status file descriptor, skip.
Roland Levillain94b41802019-01-18 11:56:50 +0000260 for (size_t i = 2; i < static_cast<size_t>(argc); ++i) {
261 cmd.push_back(arg[i]);
Andreas Gamped089ca12016-06-27 14:25:30 -0700262 }
Andreas Gampe01ad5982016-03-09 16:27:29 -0800263
Roland Levillain94b41802019-01-18 11:56:50 +0000264 // Fork and execute otapreopt in its own process.
265 std::string error_msg;
266 bool exec_result = Exec(cmd, &error_msg);
267 if (!exec_result) {
268 LOG(ERROR) << "Running otapreopt failed: " << error_msg;
269 }
270
Roland Levillain6520cca2019-02-01 13:15:58 +0000271 // Tear down the work down by the apexd logic. (i.e. deactivate packages).
272 DeactivateApexPackages(active_packages);
Roland Levillain94b41802019-01-18 11:56:50 +0000273
274 if (!exec_result) {
275 exit(213);
276 }
277
278 return 0;
Andreas Gampe01ad5982016-03-09 16:27:29 -0800279}
280
281} // namespace installd
282} // namespace android
283
284int main(const int argc, char *argv[]) {
285 return android::installd::otapreopt_chroot(argc, argv);
286}