blob: 609ddaf9d7bf358f04f1ee4e7073799471d89e9c [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>
Roland Levillainc19c6042018-12-18 12:15:12 +000028#include <selinux/android.h>
29
30#include <apexd.h>
Andreas Gampe01ad5982016-03-09 16:27:29 -080031
Jeff Sharkey0274c972016-12-06 09:32:04 -070032#include "installd_constants.h"
33#include "otapreopt_utils.h"
Andreas Gampe548bdb92016-06-02 17:56:45 -070034
Andreas Gampe01ad5982016-03-09 16:27:29 -080035#ifndef LOG_TAG
36#define LOG_TAG "otapreopt"
37#endif
38
39using android::base::StringPrintf;
40
41namespace android {
42namespace installd {
43
Roland Levillainc3265ae2019-01-31 18:30:17 +000044// Configuration for bind-mounted Bionic artifacts.
45
46static constexpr const char* kLinkerMountPoint = "/bionic/bin/linker";
47static constexpr const char* kRuntimeLinkerPath = "/apex/com.android.runtime/bin/linker";
48
49static constexpr const char* kBionicLibsMountPointDir = "/bionic/lib/";
50static constexpr const char* kRuntimeBionicLibsDir = "/apex/com.android.runtime/lib/bionic/";
51
52static constexpr const char* kLinkerMountPoint64 = "/bionic/bin/linker64";
53static constexpr const char* kRuntimeLinkerPath64 = "/apex/com.android.runtime/bin/linker64";
54
55static constexpr const char* kBionicLibsMountPointDir64 = "/bionic/lib64/";
56static constexpr const char* kRuntimeBionicLibsDir64 = "/apex/com.android.runtime/lib64/bionic/";
57
58static const std::vector<std::string> kBionicLibFileNames = {"libc.so", "libm.so", "libdl.so"};
59
60
Andreas Gamped089ca12016-06-27 14:25:30 -070061static void CloseDescriptor(int fd) {
62 if (fd >= 0) {
63 int result = close(fd);
64 UNUSED(result); // Ignore result. Printing to logcat will open a new descriptor
65 // that we do *not* want.
66 }
67}
68
69static void CloseDescriptor(const char* descriptor_string) {
70 int fd = -1;
71 std::istringstream stream(descriptor_string);
72 stream >> fd;
73 if (!stream.fail()) {
74 CloseDescriptor(fd);
75 }
76}
77
Roland Levillain6520cca2019-02-01 13:15:58 +000078static std::vector<apex::ApexFile> ActivateApexPackages() {
79 // The logic here is (partially) copied and adapted from
80 // system/apex/apexd/apexd_main.cpp.
81 //
82 // Only scan the APEX directory under /system (within the chroot dir).
83 // Note that this leaves around the loop devices created and used by
84 // libapexd's code, but this is fine, as we expect to reboot soon after.
85 apex::scanPackagesDirAndActivate(apex::kApexPackageSystemDir);
86 return apex::getActivePackages();
87}
88
89static void DeactivateApexPackages(const std::vector<apex::ApexFile>& active_packages) {
90 for (const apex::ApexFile& apex_file : active_packages) {
91 const std::string& package_path = apex_file.GetPath();
92 apex::Status status = apex::deactivatePackage(package_path);
93 if (!status.Ok()) {
94 LOG(ERROR) << "Failed to deactivate " << package_path << ": " << status.ErrorMessage();
95 }
96 }
97}
98
Roland Levillainc3265ae2019-01-31 18:30:17 +000099// Copied from system/core/init/mount_namespace.cpp.
100static bool BindMount(const std::string& source, const std::string& mount_point,
101 bool recursive = false) {
102 unsigned long mountflags = MS_BIND;
103 if (recursive) {
104 mountflags |= MS_REC;
105 }
106 if (mount(source.c_str(), mount_point.c_str(), nullptr, mountflags, nullptr) == -1) {
107 PLOG(ERROR) << "Could not bind-mount " << source << " to " << mount_point;
108 return false;
109 }
110 return true;
111}
112
113// Copied from system/core/init/mount_namespace.cpp and and adjusted (bind
114// mounts are not made private, as the /postinstall is already private (see
115// `android::installd::otapreopt_chroot`).
116static bool BindMountBionic(const std::string& linker_source, const std::string& lib_dir_source,
117 const std::string& linker_mount_point,
118 const std::string& lib_mount_dir) {
119 if (access(linker_source.c_str(), F_OK) != 0) {
120 PLOG(INFO) << linker_source << " does not exist. Skipping mounting Bionic there.";
121 return true;
122 }
123 if (!BindMount(linker_source, linker_mount_point)) {
124 return false;
125 }
126 for (const auto& libname : kBionicLibFileNames) {
127 std::string mount_point = lib_mount_dir + libname;
128 std::string source = lib_dir_source + libname;
129 if (!BindMount(source, mount_point)) {
130 return false;
131 }
132 }
133 return true;
134}
135
Andreas Gamped089ca12016-06-27 14:25:30 -0700136// Entry for otapreopt_chroot. Expected parameters are:
137// [cmd] [status-fd] [target-slot] "dexopt" [dexopt-params]
138// The file descriptor denoted by status-fd will be closed. The rest of the parameters will
139// be passed on to otapreopt in the chroot.
Andreas Gampe01ad5982016-03-09 16:27:29 -0800140static int otapreopt_chroot(const int argc, char **arg) {
Zach Riggle318853a2018-01-18 18:34:04 -0600141 // Validate arguments
142 // We need the command, status channel and target slot, at a minimum.
143 if(argc < 3) {
144 PLOG(ERROR) << "Not enough arguments.";
145 exit(208);
146 }
Andreas Gamped089ca12016-06-27 14:25:30 -0700147 // Close all file descriptors. They are coming from the caller, we do not want to pass them
148 // on across our fork/exec into a different domain.
149 // 1) Default descriptors.
150 CloseDescriptor(STDIN_FILENO);
151 CloseDescriptor(STDOUT_FILENO);
152 CloseDescriptor(STDERR_FILENO);
153 // 2) The status channel.
154 CloseDescriptor(arg[1]);
155
Andreas Gampe01ad5982016-03-09 16:27:29 -0800156 // We need to run the otapreopt tool from the postinstall partition. As such, set up a
157 // mount namespace and change root.
158
159 // Create our own mount namespace.
160 if (unshare(CLONE_NEWNS) != 0) {
161 PLOG(ERROR) << "Failed to unshare() for otapreopt.";
162 exit(200);
163 }
164
165 // Make postinstall private, so that our changes don't propagate.
166 if (mount("", "/postinstall", nullptr, MS_PRIVATE, nullptr) != 0) {
167 PLOG(ERROR) << "Failed to mount private.";
168 exit(201);
169 }
170
171 // Bind mount necessary directories.
172 constexpr const char* kBindMounts[] = {
173 "/data", "/dev", "/proc", "/sys"
174 };
175 for (size_t i = 0; i < arraysize(kBindMounts); ++i) {
176 std::string trg = StringPrintf("/postinstall%s", kBindMounts[i]);
177 if (mount(kBindMounts[i], trg.c_str(), nullptr, MS_BIND, nullptr) != 0) {
178 PLOG(ERROR) << "Failed to bind-mount " << kBindMounts[i];
179 exit(202);
180 }
181 }
182
Andreas Gampefd12eda2016-07-12 09:47:17 -0700183 // Try to mount the vendor partition. update_engine doesn't do this for us, but we
184 // want it for vendor APKs.
185 // Notes:
186 // 1) We pretty much guess a name here and hope to find the partition by name.
187 // It is just as complicated and brittle to scan /proc/mounts. But this requires
188 // validating the target-slot so as not to try to mount some totally random path.
189 // 2) We're in a mount namespace here, so when we die, this will be cleaned up.
190 // 3) Ignore errors. Printing anything at this stage will open a file descriptor
191 // for logging.
192 if (!ValidateTargetSlotSuffix(arg[2])) {
193 LOG(ERROR) << "Target slot suffix not legal: " << arg[2];
194 exit(207);
195 }
Andreas Gampeb87a1c72018-04-13 17:42:23 -0700196 {
Bowgo Tsai19d2d082018-05-31 10:16:46 +0800197 std::string vendor_partition = StringPrintf("/dev/block/by-name/vendor%s",
Andreas Gampeb87a1c72018-04-13 17:42:23 -0700198 arg[2]);
199 int vendor_result = mount(vendor_partition.c_str(),
200 "/postinstall/vendor",
201 "ext4",
202 MS_RDONLY,
203 /* data */ nullptr);
204 UNUSED(vendor_result);
205 }
206
207 // Try to mount the product partition. update_engine doesn't do this for us, but we
208 // want it for product APKs. Same notes as vendor above.
209 {
Bowgo Tsai19d2d082018-05-31 10:16:46 +0800210 std::string product_partition = StringPrintf("/dev/block/by-name/product%s",
Andreas Gampeb87a1c72018-04-13 17:42:23 -0700211 arg[2]);
212 int product_result = mount(product_partition.c_str(),
213 "/postinstall/product",
214 "ext4",
215 MS_RDONLY,
216 /* data */ nullptr);
217 UNUSED(product_result);
218 }
Andreas Gampefd12eda2016-07-12 09:47:17 -0700219
Roland Levillainc19c6042018-12-18 12:15:12 +0000220 // Setup APEX mount point and its security context.
221 static constexpr const char* kPostinstallApexDir = "/postinstall/apex";
222 // The following logic is similar to the one in system/core/rootdir/init.rc:
223 //
224 // mount tmpfs tmpfs /apex nodev noexec nosuid
225 // chmod 0755 /apex
226 // chown root root /apex
227 // restorecon /apex
228 //
Roland Levillain8d276812019-01-24 10:51:30 +0000229 // except we perform the `restorecon` step just after mounting the tmpfs
230 // filesystem in /postinstall/apex, so that this directory is correctly
231 // labeled (with type `postinstall_apex_mnt_dir`) and may be manipulated in
232 // following operations (`chmod`, `chown`, etc.) following policies
233 // restricted to `postinstall_apex_mnt_dir`:
234 //
235 // mount tmpfs tmpfs /postinstall/apex nodev noexec nosuid
236 // restorecon /postinstall/apex
237 // chmod 0755 /postinstall/apex
238 // chown root root /postinstall/apex
239 //
Roland Levillainc19c6042018-12-18 12:15:12 +0000240 if (mount("tmpfs", kPostinstallApexDir, "tmpfs", MS_NODEV | MS_NOEXEC | MS_NOSUID, nullptr)
241 != 0) {
242 PLOG(ERROR) << "Failed to mount tmpfs in " << kPostinstallApexDir;
243 exit(209);
244 }
Roland Levillain8d276812019-01-24 10:51:30 +0000245 if (selinux_android_restorecon(kPostinstallApexDir, 0) < 0) {
246 PLOG(ERROR) << "Failed to restorecon " << kPostinstallApexDir;
247 exit(214);
248 }
Roland Levillainc19c6042018-12-18 12:15:12 +0000249 if (chmod(kPostinstallApexDir, 0755) != 0) {
250 PLOG(ERROR) << "Failed to chmod " << kPostinstallApexDir << " to 0755";
251 exit(210);
252 }
253 if (chown(kPostinstallApexDir, 0, 0) != 0) {
254 PLOG(ERROR) << "Failed to chown " << kPostinstallApexDir << " to root:root";
255 exit(211);
256 }
Roland Levillainc19c6042018-12-18 12:15:12 +0000257
Andreas Gampe01ad5982016-03-09 16:27:29 -0800258 // Chdir into /postinstall.
259 if (chdir("/postinstall") != 0) {
260 PLOG(ERROR) << "Unable to chdir into /postinstall.";
261 exit(203);
262 }
263
264 // Make /postinstall the root in our mount namespace.
265 if (chroot(".") != 0) {
266 PLOG(ERROR) << "Failed to chroot";
267 exit(204);
268 }
269
270 if (chdir("/") != 0) {
271 PLOG(ERROR) << "Unable to chdir into /.";
272 exit(205);
273 }
274
Roland Levillainc19c6042018-12-18 12:15:12 +0000275 // Try to mount APEX packages in "/apex" in the chroot dir. We need at least
276 // the Android Runtime APEX, as it is required by otapreopt to run dex2oat.
Roland Levillain6520cca2019-02-01 13:15:58 +0000277 std::vector<apex::ApexFile> active_packages = ActivateApexPackages();
Roland Levillainc19c6042018-12-18 12:15:12 +0000278
Roland Levillainc3265ae2019-01-31 18:30:17 +0000279 // Bind-mount Bionic artifacts from the Runtime APEX.
280 // This logic is copied and adapted from system/core/init/mount_namespace.cpp.
281 if (!BindMountBionic(kRuntimeLinkerPath, kRuntimeBionicLibsDir, kLinkerMountPoint,
282 kBionicLibsMountPointDir)) {
283 LOG(ERROR) << "Failed to mount 32-bit Bionic artifacts from the Runtime APEX.";
284 // Clean up and exit.
285 DeactivateApexPackages(active_packages);
286 exit(215);
287 }
288 if (!BindMountBionic(kRuntimeLinkerPath64, kRuntimeBionicLibsDir64, kLinkerMountPoint64,
289 kBionicLibsMountPointDir64)) {
290 LOG(ERROR) << "Failed to mount 64-bit Bionic artifacts from the Runtime APEX.";
291 // Clean up and exit.
292 DeactivateApexPackages(active_packages);
293 exit(216);
294 }
295
Andreas Gampe01ad5982016-03-09 16:27:29 -0800296 // Now go on and run otapreopt.
297
Roland Levillain94b41802019-01-18 11:56:50 +0000298 // Incoming: cmd + status-fd + target-slot + cmd... | Incoming | = argc
299 // Outgoing: cmd + target-slot + cmd... | Outgoing | = argc - 1
300 std::vector<std::string> cmd;
301 cmd.reserve(argc);
302 cmd.push_back("/system/bin/otapreopt");
Andreas Gamped089ca12016-06-27 14:25:30 -0700303
304 // The first parameter is the status file descriptor, skip.
Roland Levillain94b41802019-01-18 11:56:50 +0000305 for (size_t i = 2; i < static_cast<size_t>(argc); ++i) {
306 cmd.push_back(arg[i]);
Andreas Gamped089ca12016-06-27 14:25:30 -0700307 }
Andreas Gampe01ad5982016-03-09 16:27:29 -0800308
Roland Levillain94b41802019-01-18 11:56:50 +0000309 // Fork and execute otapreopt in its own process.
310 std::string error_msg;
311 bool exec_result = Exec(cmd, &error_msg);
312 if (!exec_result) {
313 LOG(ERROR) << "Running otapreopt failed: " << error_msg;
314 }
315
Roland Levillain6520cca2019-02-01 13:15:58 +0000316 // Tear down the work down by the apexd logic. (i.e. deactivate packages).
317 DeactivateApexPackages(active_packages);
Roland Levillain94b41802019-01-18 11:56:50 +0000318
319 if (!exec_result) {
320 exit(213);
321 }
322
323 return 0;
Andreas Gampe01ad5982016-03-09 16:27:29 -0800324}
325
326} // namespace installd
327} // namespace android
328
329int main(const int argc, char *argv[]) {
330 return android::installd::otapreopt_chroot(argc, argv);
331}