blob: e8f513aebb10491a23b1a23bcdbf7c89903b3ac6 [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();
75 apex::Status status = apex::deactivatePackage(package_path);
76 if (!status.Ok()) {
77 LOG(ERROR) << "Failed to deactivate " << package_path << ": " << status.ErrorMessage();
78 }
79 }
80}
81
Andreas Gampe54b62c92019-03-21 11:34:19 -070082static void TryExtraMount(const char* name, const char* slot, const char* target) {
Andreas Gampee90127d2019-03-22 11:21:34 -070083 std::string partition_name = StringPrintf("%s%s", name, slot);
84
85 // See whether update_engine mounted a logical partition.
86 {
87 auto& dm = dm::DeviceMapper::Instance();
88 if (dm.GetState(partition_name) != dm::DmDeviceState::INVALID) {
89 std::string path;
90 if (dm.GetDmDevicePathByName(partition_name, &path)) {
91 int mount_result = mount(path.c_str(),
92 target,
93 "ext4",
94 MS_RDONLY,
95 /* data */ nullptr);
96 if (mount_result == 0) {
97 return;
98 }
99 }
100 }
101 }
102
103 // Fall back and attempt a direct mount.
104 std::string block_device = StringPrintf("/dev/block/by-name/%s", partition_name.c_str());
Andreas Gampe54b62c92019-03-21 11:34:19 -0700105 int mount_result = mount(block_device.c_str(),
106 target,
107 "ext4",
108 MS_RDONLY,
109 /* data */ nullptr);
110 UNUSED(mount_result);
111}
112
Andreas Gamped089ca12016-06-27 14:25:30 -0700113// Entry for otapreopt_chroot. Expected parameters are:
114// [cmd] [status-fd] [target-slot] "dexopt" [dexopt-params]
115// The file descriptor denoted by status-fd will be closed. The rest of the parameters will
116// be passed on to otapreopt in the chroot.
Andreas Gampe01ad5982016-03-09 16:27:29 -0800117static int otapreopt_chroot(const int argc, char **arg) {
Zach Riggle318853a2018-01-18 18:34:04 -0600118 // Validate arguments
119 // We need the command, status channel and target slot, at a minimum.
120 if(argc < 3) {
121 PLOG(ERROR) << "Not enough arguments.";
122 exit(208);
123 }
Andreas Gamped089ca12016-06-27 14:25:30 -0700124 // Close all file descriptors. They are coming from the caller, we do not want to pass them
125 // on across our fork/exec into a different domain.
126 // 1) Default descriptors.
127 CloseDescriptor(STDIN_FILENO);
128 CloseDescriptor(STDOUT_FILENO);
129 CloseDescriptor(STDERR_FILENO);
130 // 2) The status channel.
131 CloseDescriptor(arg[1]);
132
Andreas Gampe01ad5982016-03-09 16:27:29 -0800133 // We need to run the otapreopt tool from the postinstall partition. As such, set up a
134 // mount namespace and change root.
135
136 // Create our own mount namespace.
137 if (unshare(CLONE_NEWNS) != 0) {
138 PLOG(ERROR) << "Failed to unshare() for otapreopt.";
139 exit(200);
140 }
141
142 // Make postinstall private, so that our changes don't propagate.
143 if (mount("", "/postinstall", nullptr, MS_PRIVATE, nullptr) != 0) {
144 PLOG(ERROR) << "Failed to mount private.";
145 exit(201);
146 }
147
148 // Bind mount necessary directories.
149 constexpr const char* kBindMounts[] = {
150 "/data", "/dev", "/proc", "/sys"
151 };
152 for (size_t i = 0; i < arraysize(kBindMounts); ++i) {
153 std::string trg = StringPrintf("/postinstall%s", kBindMounts[i]);
154 if (mount(kBindMounts[i], trg.c_str(), nullptr, MS_BIND, nullptr) != 0) {
155 PLOG(ERROR) << "Failed to bind-mount " << kBindMounts[i];
156 exit(202);
157 }
158 }
159
Andreas Gampefd12eda2016-07-12 09:47:17 -0700160 // Try to mount the vendor partition. update_engine doesn't do this for us, but we
161 // want it for vendor APKs.
162 // Notes:
163 // 1) We pretty much guess a name here and hope to find the partition by name.
164 // It is just as complicated and brittle to scan /proc/mounts. But this requires
165 // validating the target-slot so as not to try to mount some totally random path.
166 // 2) We're in a mount namespace here, so when we die, this will be cleaned up.
167 // 3) Ignore errors. Printing anything at this stage will open a file descriptor
168 // for logging.
169 if (!ValidateTargetSlotSuffix(arg[2])) {
170 LOG(ERROR) << "Target slot suffix not legal: " << arg[2];
171 exit(207);
172 }
Andreas Gampe54b62c92019-03-21 11:34:19 -0700173 TryExtraMount("vendor", arg[2], "/postinstall/vendor");
Andreas Gampeb87a1c72018-04-13 17:42:23 -0700174
175 // Try to mount the product partition. update_engine doesn't do this for us, but we
176 // want it for product APKs. Same notes as vendor above.
Andreas Gampe54b62c92019-03-21 11:34:19 -0700177 TryExtraMount("product", arg[2], "/postinstall/product");
Andreas Gampefd12eda2016-07-12 09:47:17 -0700178
Roland Levillainc19c6042018-12-18 12:15:12 +0000179 // Setup APEX mount point and its security context.
180 static constexpr const char* kPostinstallApexDir = "/postinstall/apex";
181 // The following logic is similar to the one in system/core/rootdir/init.rc:
182 //
183 // mount tmpfs tmpfs /apex nodev noexec nosuid
184 // chmod 0755 /apex
185 // chown root root /apex
186 // restorecon /apex
187 //
Roland Levillain8d276812019-01-24 10:51:30 +0000188 // except we perform the `restorecon` step just after mounting the tmpfs
189 // filesystem in /postinstall/apex, so that this directory is correctly
190 // labeled (with type `postinstall_apex_mnt_dir`) and may be manipulated in
191 // following operations (`chmod`, `chown`, etc.) following policies
192 // restricted to `postinstall_apex_mnt_dir`:
193 //
194 // mount tmpfs tmpfs /postinstall/apex nodev noexec nosuid
195 // restorecon /postinstall/apex
196 // chmod 0755 /postinstall/apex
197 // chown root root /postinstall/apex
198 //
Roland Levillainc19c6042018-12-18 12:15:12 +0000199 if (mount("tmpfs", kPostinstallApexDir, "tmpfs", MS_NODEV | MS_NOEXEC | MS_NOSUID, nullptr)
200 != 0) {
201 PLOG(ERROR) << "Failed to mount tmpfs in " << kPostinstallApexDir;
202 exit(209);
203 }
Roland Levillain8d276812019-01-24 10:51:30 +0000204 if (selinux_android_restorecon(kPostinstallApexDir, 0) < 0) {
205 PLOG(ERROR) << "Failed to restorecon " << kPostinstallApexDir;
206 exit(214);
207 }
Roland Levillainc19c6042018-12-18 12:15:12 +0000208 if (chmod(kPostinstallApexDir, 0755) != 0) {
209 PLOG(ERROR) << "Failed to chmod " << kPostinstallApexDir << " to 0755";
210 exit(210);
211 }
212 if (chown(kPostinstallApexDir, 0, 0) != 0) {
213 PLOG(ERROR) << "Failed to chown " << kPostinstallApexDir << " to root:root";
214 exit(211);
215 }
Roland Levillainc19c6042018-12-18 12:15:12 +0000216
Andreas Gampe01ad5982016-03-09 16:27:29 -0800217 // Chdir into /postinstall.
218 if (chdir("/postinstall") != 0) {
219 PLOG(ERROR) << "Unable to chdir into /postinstall.";
220 exit(203);
221 }
222
223 // Make /postinstall the root in our mount namespace.
224 if (chroot(".") != 0) {
225 PLOG(ERROR) << "Failed to chroot";
226 exit(204);
227 }
228
229 if (chdir("/") != 0) {
230 PLOG(ERROR) << "Unable to chdir into /.";
231 exit(205);
232 }
233
Roland Levillainc19c6042018-12-18 12:15:12 +0000234 // Try to mount APEX packages in "/apex" in the chroot dir. We need at least
235 // the Android Runtime APEX, as it is required by otapreopt to run dex2oat.
Roland Levillain6520cca2019-02-01 13:15:58 +0000236 std::vector<apex::ApexFile> active_packages = ActivateApexPackages();
Roland Levillainc19c6042018-12-18 12:15:12 +0000237
Andreas Gampe01ad5982016-03-09 16:27:29 -0800238 // Now go on and run otapreopt.
239
Roland Levillain94b41802019-01-18 11:56:50 +0000240 // Incoming: cmd + status-fd + target-slot + cmd... | Incoming | = argc
241 // Outgoing: cmd + target-slot + cmd... | Outgoing | = argc - 1
242 std::vector<std::string> cmd;
243 cmd.reserve(argc);
244 cmd.push_back("/system/bin/otapreopt");
Andreas Gamped089ca12016-06-27 14:25:30 -0700245
246 // The first parameter is the status file descriptor, skip.
Roland Levillain94b41802019-01-18 11:56:50 +0000247 for (size_t i = 2; i < static_cast<size_t>(argc); ++i) {
248 cmd.push_back(arg[i]);
Andreas Gamped089ca12016-06-27 14:25:30 -0700249 }
Andreas Gampe01ad5982016-03-09 16:27:29 -0800250
Roland Levillain94b41802019-01-18 11:56:50 +0000251 // Fork and execute otapreopt in its own process.
252 std::string error_msg;
253 bool exec_result = Exec(cmd, &error_msg);
254 if (!exec_result) {
255 LOG(ERROR) << "Running otapreopt failed: " << error_msg;
256 }
257
Roland Levillain6520cca2019-02-01 13:15:58 +0000258 // Tear down the work down by the apexd logic. (i.e. deactivate packages).
259 DeactivateApexPackages(active_packages);
Roland Levillain94b41802019-01-18 11:56:50 +0000260
261 if (!exec_result) {
262 exit(213);
263 }
264
265 return 0;
Andreas Gampe01ad5982016-03-09 16:27:29 -0800266}
267
268} // namespace installd
269} // namespace android
270
271int main(const int argc, char *argv[]) {
272 return android::installd::otapreopt_chroot(argc, argv);
273}