blob: 0fdc9d6cbb221244379fc173ee6bd0c41561e228 [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
Andreas Gamped089ca12016-06-27 14:25:30 -070044static void CloseDescriptor(int fd) {
45 if (fd >= 0) {
46 int result = close(fd);
47 UNUSED(result); // Ignore result. Printing to logcat will open a new descriptor
48 // that we do *not* want.
49 }
50}
51
52static void CloseDescriptor(const char* descriptor_string) {
53 int fd = -1;
54 std::istringstream stream(descriptor_string);
55 stream >> fd;
56 if (!stream.fail()) {
57 CloseDescriptor(fd);
58 }
59}
60
Roland Levillain6520cca2019-02-01 13:15:58 +000061static std::vector<apex::ApexFile> ActivateApexPackages() {
62 // The logic here is (partially) copied and adapted from
63 // system/apex/apexd/apexd_main.cpp.
64 //
65 // Only scan the APEX directory under /system (within the chroot dir).
Roland Levillain6520cca2019-02-01 13:15:58 +000066 apex::scanPackagesDirAndActivate(apex::kApexPackageSystemDir);
67 return apex::getActivePackages();
68}
69
70static void DeactivateApexPackages(const std::vector<apex::ApexFile>& active_packages) {
71 for (const apex::ApexFile& apex_file : active_packages) {
72 const std::string& package_path = apex_file.GetPath();
73 apex::Status status = apex::deactivatePackage(package_path);
74 if (!status.Ok()) {
75 LOG(ERROR) << "Failed to deactivate " << package_path << ": " << status.ErrorMessage();
76 }
77 }
78}
79
Andreas Gamped089ca12016-06-27 14:25:30 -070080// Entry for otapreopt_chroot. Expected parameters are:
81// [cmd] [status-fd] [target-slot] "dexopt" [dexopt-params]
82// The file descriptor denoted by status-fd will be closed. The rest of the parameters will
83// be passed on to otapreopt in the chroot.
Andreas Gampe01ad5982016-03-09 16:27:29 -080084static int otapreopt_chroot(const int argc, char **arg) {
Zach Riggle318853a2018-01-18 18:34:04 -060085 // Validate arguments
86 // We need the command, status channel and target slot, at a minimum.
87 if(argc < 3) {
88 PLOG(ERROR) << "Not enough arguments.";
89 exit(208);
90 }
Andreas Gamped089ca12016-06-27 14:25:30 -070091 // Close all file descriptors. They are coming from the caller, we do not want to pass them
92 // on across our fork/exec into a different domain.
93 // 1) Default descriptors.
94 CloseDescriptor(STDIN_FILENO);
95 CloseDescriptor(STDOUT_FILENO);
96 CloseDescriptor(STDERR_FILENO);
97 // 2) The status channel.
98 CloseDescriptor(arg[1]);
99
Andreas Gampe01ad5982016-03-09 16:27:29 -0800100 // We need to run the otapreopt tool from the postinstall partition. As such, set up a
101 // mount namespace and change root.
102
103 // Create our own mount namespace.
104 if (unshare(CLONE_NEWNS) != 0) {
105 PLOG(ERROR) << "Failed to unshare() for otapreopt.";
106 exit(200);
107 }
108
109 // Make postinstall private, so that our changes don't propagate.
110 if (mount("", "/postinstall", nullptr, MS_PRIVATE, nullptr) != 0) {
111 PLOG(ERROR) << "Failed to mount private.";
112 exit(201);
113 }
114
115 // Bind mount necessary directories.
116 constexpr const char* kBindMounts[] = {
117 "/data", "/dev", "/proc", "/sys"
118 };
119 for (size_t i = 0; i < arraysize(kBindMounts); ++i) {
120 std::string trg = StringPrintf("/postinstall%s", kBindMounts[i]);
121 if (mount(kBindMounts[i], trg.c_str(), nullptr, MS_BIND, nullptr) != 0) {
122 PLOG(ERROR) << "Failed to bind-mount " << kBindMounts[i];
123 exit(202);
124 }
125 }
126
Andreas Gampefd12eda2016-07-12 09:47:17 -0700127 // Try to mount the vendor partition. update_engine doesn't do this for us, but we
128 // want it for vendor APKs.
129 // Notes:
130 // 1) We pretty much guess a name here and hope to find the partition by name.
131 // It is just as complicated and brittle to scan /proc/mounts. But this requires
132 // validating the target-slot so as not to try to mount some totally random path.
133 // 2) We're in a mount namespace here, so when we die, this will be cleaned up.
134 // 3) Ignore errors. Printing anything at this stage will open a file descriptor
135 // for logging.
136 if (!ValidateTargetSlotSuffix(arg[2])) {
137 LOG(ERROR) << "Target slot suffix not legal: " << arg[2];
138 exit(207);
139 }
Andreas Gampeb87a1c72018-04-13 17:42:23 -0700140 {
Bowgo Tsai19d2d082018-05-31 10:16:46 +0800141 std::string vendor_partition = StringPrintf("/dev/block/by-name/vendor%s",
Andreas Gampeb87a1c72018-04-13 17:42:23 -0700142 arg[2]);
143 int vendor_result = mount(vendor_partition.c_str(),
144 "/postinstall/vendor",
145 "ext4",
146 MS_RDONLY,
147 /* data */ nullptr);
148 UNUSED(vendor_result);
149 }
150
151 // Try to mount the product partition. update_engine doesn't do this for us, but we
152 // want it for product APKs. Same notes as vendor above.
153 {
Bowgo Tsai19d2d082018-05-31 10:16:46 +0800154 std::string product_partition = StringPrintf("/dev/block/by-name/product%s",
Andreas Gampeb87a1c72018-04-13 17:42:23 -0700155 arg[2]);
156 int product_result = mount(product_partition.c_str(),
157 "/postinstall/product",
158 "ext4",
159 MS_RDONLY,
160 /* data */ nullptr);
161 UNUSED(product_result);
162 }
Andreas Gampefd12eda2016-07-12 09:47:17 -0700163
Roland Levillainc19c6042018-12-18 12:15:12 +0000164 // Setup APEX mount point and its security context.
165 static constexpr const char* kPostinstallApexDir = "/postinstall/apex";
166 // The following logic is similar to the one in system/core/rootdir/init.rc:
167 //
168 // mount tmpfs tmpfs /apex nodev noexec nosuid
169 // chmod 0755 /apex
170 // chown root root /apex
171 // restorecon /apex
172 //
Roland Levillain8d276812019-01-24 10:51:30 +0000173 // except we perform the `restorecon` step just after mounting the tmpfs
174 // filesystem in /postinstall/apex, so that this directory is correctly
175 // labeled (with type `postinstall_apex_mnt_dir`) and may be manipulated in
176 // following operations (`chmod`, `chown`, etc.) following policies
177 // restricted to `postinstall_apex_mnt_dir`:
178 //
179 // mount tmpfs tmpfs /postinstall/apex nodev noexec nosuid
180 // restorecon /postinstall/apex
181 // chmod 0755 /postinstall/apex
182 // chown root root /postinstall/apex
183 //
Roland Levillainc19c6042018-12-18 12:15:12 +0000184 if (mount("tmpfs", kPostinstallApexDir, "tmpfs", MS_NODEV | MS_NOEXEC | MS_NOSUID, nullptr)
185 != 0) {
186 PLOG(ERROR) << "Failed to mount tmpfs in " << kPostinstallApexDir;
187 exit(209);
188 }
Roland Levillain8d276812019-01-24 10:51:30 +0000189 if (selinux_android_restorecon(kPostinstallApexDir, 0) < 0) {
190 PLOG(ERROR) << "Failed to restorecon " << kPostinstallApexDir;
191 exit(214);
192 }
Roland Levillainc19c6042018-12-18 12:15:12 +0000193 if (chmod(kPostinstallApexDir, 0755) != 0) {
194 PLOG(ERROR) << "Failed to chmod " << kPostinstallApexDir << " to 0755";
195 exit(210);
196 }
197 if (chown(kPostinstallApexDir, 0, 0) != 0) {
198 PLOG(ERROR) << "Failed to chown " << kPostinstallApexDir << " to root:root";
199 exit(211);
200 }
Roland Levillainc19c6042018-12-18 12:15:12 +0000201
Andreas Gampe01ad5982016-03-09 16:27:29 -0800202 // Chdir into /postinstall.
203 if (chdir("/postinstall") != 0) {
204 PLOG(ERROR) << "Unable to chdir into /postinstall.";
205 exit(203);
206 }
207
208 // Make /postinstall the root in our mount namespace.
209 if (chroot(".") != 0) {
210 PLOG(ERROR) << "Failed to chroot";
211 exit(204);
212 }
213
214 if (chdir("/") != 0) {
215 PLOG(ERROR) << "Unable to chdir into /.";
216 exit(205);
217 }
218
Roland Levillainc19c6042018-12-18 12:15:12 +0000219 // Try to mount APEX packages in "/apex" in the chroot dir. We need at least
220 // the Android Runtime APEX, as it is required by otapreopt to run dex2oat.
Roland Levillain6520cca2019-02-01 13:15:58 +0000221 std::vector<apex::ApexFile> active_packages = ActivateApexPackages();
Roland Levillainc19c6042018-12-18 12:15:12 +0000222
Andreas Gampe01ad5982016-03-09 16:27:29 -0800223 // Now go on and run otapreopt.
224
Roland Levillain94b41802019-01-18 11:56:50 +0000225 // Incoming: cmd + status-fd + target-slot + cmd... | Incoming | = argc
226 // Outgoing: cmd + target-slot + cmd... | Outgoing | = argc - 1
227 std::vector<std::string> cmd;
228 cmd.reserve(argc);
229 cmd.push_back("/system/bin/otapreopt");
Andreas Gamped089ca12016-06-27 14:25:30 -0700230
231 // The first parameter is the status file descriptor, skip.
Roland Levillain94b41802019-01-18 11:56:50 +0000232 for (size_t i = 2; i < static_cast<size_t>(argc); ++i) {
233 cmd.push_back(arg[i]);
Andreas Gamped089ca12016-06-27 14:25:30 -0700234 }
Andreas Gampe01ad5982016-03-09 16:27:29 -0800235
Roland Levillain94b41802019-01-18 11:56:50 +0000236 // Fork and execute otapreopt in its own process.
237 std::string error_msg;
238 bool exec_result = Exec(cmd, &error_msg);
239 if (!exec_result) {
240 LOG(ERROR) << "Running otapreopt failed: " << error_msg;
241 }
242
Roland Levillain6520cca2019-02-01 13:15:58 +0000243 // Tear down the work down by the apexd logic. (i.e. deactivate packages).
244 DeactivateApexPackages(active_packages);
Roland Levillain94b41802019-01-18 11:56:50 +0000245
246 if (!exec_result) {
247 exit(213);
248 }
249
250 return 0;
Andreas Gampe01ad5982016-03-09 16:27:29 -0800251}
252
253} // namespace installd
254} // namespace android
255
256int main(const int argc, char *argv[]) {
257 return android::installd::otapreopt_chroot(argc, argv);
258}