blob: c04b558e2de20c63aa1af6b99212500a1d13ebf3 [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
Alex Light53e94382021-03-09 16:39:55 -080023#include <array>
Alex Lightcf7e6de2021-03-04 10:20:18 -080024#include <fstream>
Andreas Gamped089ca12016-06-27 14:25:30 -070025#include <sstream>
26
Alex Lightcf7e6de2021-03-04 10:20:18 -080027#include <android-base/file.h>
Andreas Gampe01ad5982016-03-09 16:27:29 -080028#include <android-base/logging.h>
29#include <android-base/macros.h>
30#include <android-base/stringprintf.h>
Alex Lightcf7e6de2021-03-04 10:20:18 -080031#include <android-base/unique_fd.h>
Andreas Gampee90127d2019-03-22 11:21:34 -070032#include <libdm/dm.h>
Roland Levillainc19c6042018-12-18 12:15:12 +000033#include <selinux/android.h>
34
Jeff Sharkey0274c972016-12-06 09:32:04 -070035#include "installd_constants.h"
36#include "otapreopt_utils.h"
Andreas Gampe548bdb92016-06-02 17:56:45 -070037
Andreas Gampe01ad5982016-03-09 16:27:29 -080038#ifndef LOG_TAG
39#define LOG_TAG "otapreopt"
40#endif
41
42using android::base::StringPrintf;
43
44namespace android {
45namespace installd {
46
Andreas Gamped089ca12016-06-27 14:25:30 -070047static void CloseDescriptor(int fd) {
48 if (fd >= 0) {
49 int result = close(fd);
50 UNUSED(result); // Ignore result. Printing to logcat will open a new descriptor
51 // that we do *not* want.
52 }
53}
54
55static void CloseDescriptor(const char* descriptor_string) {
56 int fd = -1;
57 std::istringstream stream(descriptor_string);
58 stream >> fd;
59 if (!stream.fail()) {
60 CloseDescriptor(fd);
61 }
62}
63
Alex Light53e94382021-03-09 16:39:55 -080064static void ActivateApexPackages() {
65 std::vector<std::string> apexd_cmd{"/system/bin/apexd", "--otachroot-bootstrap"};
66 std::string apexd_error_msg;
Roland Levillain6520cca2019-02-01 13:15:58 +000067
Alex Light53e94382021-03-09 16:39:55 -080068 bool exec_result = Exec(apexd_cmd, &apexd_error_msg);
69 if (!exec_result) {
70 PLOG(ERROR) << "Running otapreopt failed: " << apexd_error_msg;
71 exit(220);
Roland Levillain6520cca2019-02-01 13:15:58 +000072 }
73}
74
Andreas Gampe54b62c92019-03-21 11:34:19 -070075static void TryExtraMount(const char* name, const char* slot, const char* target) {
Andreas Gampee90127d2019-03-22 11:21:34 -070076 std::string partition_name = StringPrintf("%s%s", name, slot);
77
78 // See whether update_engine mounted a logical partition.
79 {
80 auto& dm = dm::DeviceMapper::Instance();
81 if (dm.GetState(partition_name) != dm::DmDeviceState::INVALID) {
82 std::string path;
83 if (dm.GetDmDevicePathByName(partition_name, &path)) {
84 int mount_result = mount(path.c_str(),
85 target,
86 "ext4",
87 MS_RDONLY,
88 /* data */ nullptr);
89 if (mount_result == 0) {
90 return;
91 }
92 }
93 }
94 }
95
96 // Fall back and attempt a direct mount.
97 std::string block_device = StringPrintf("/dev/block/by-name/%s", partition_name.c_str());
Andreas Gampe54b62c92019-03-21 11:34:19 -070098 int mount_result = mount(block_device.c_str(),
99 target,
100 "ext4",
101 MS_RDONLY,
102 /* data */ nullptr);
103 UNUSED(mount_result);
104}
105
Andreas Gamped089ca12016-06-27 14:25:30 -0700106// Entry for otapreopt_chroot. Expected parameters are:
107// [cmd] [status-fd] [target-slot] "dexopt" [dexopt-params]
108// The file descriptor denoted by status-fd will be closed. The rest of the parameters will
109// be passed on to otapreopt in the chroot.
Andreas Gampe01ad5982016-03-09 16:27:29 -0800110static int otapreopt_chroot(const int argc, char **arg) {
Zach Riggle318853a2018-01-18 18:34:04 -0600111 // Validate arguments
112 // We need the command, status channel and target slot, at a minimum.
113 if(argc < 3) {
114 PLOG(ERROR) << "Not enough arguments.";
115 exit(208);
116 }
Andreas Gamped089ca12016-06-27 14:25:30 -0700117 // Close all file descriptors. They are coming from the caller, we do not want to pass them
118 // on across our fork/exec into a different domain.
119 // 1) Default descriptors.
120 CloseDescriptor(STDIN_FILENO);
121 CloseDescriptor(STDOUT_FILENO);
122 CloseDescriptor(STDERR_FILENO);
123 // 2) The status channel.
124 CloseDescriptor(arg[1]);
125
Andreas Gampe01ad5982016-03-09 16:27:29 -0800126 // We need to run the otapreopt tool from the postinstall partition. As such, set up a
127 // mount namespace and change root.
128
129 // Create our own mount namespace.
130 if (unshare(CLONE_NEWNS) != 0) {
131 PLOG(ERROR) << "Failed to unshare() for otapreopt.";
132 exit(200);
133 }
134
135 // Make postinstall private, so that our changes don't propagate.
136 if (mount("", "/postinstall", nullptr, MS_PRIVATE, nullptr) != 0) {
137 PLOG(ERROR) << "Failed to mount private.";
138 exit(201);
139 }
140
141 // Bind mount necessary directories.
142 constexpr const char* kBindMounts[] = {
143 "/data", "/dev", "/proc", "/sys"
144 };
145 for (size_t i = 0; i < arraysize(kBindMounts); ++i) {
146 std::string trg = StringPrintf("/postinstall%s", kBindMounts[i]);
147 if (mount(kBindMounts[i], trg.c_str(), nullptr, MS_BIND, nullptr) != 0) {
148 PLOG(ERROR) << "Failed to bind-mount " << kBindMounts[i];
149 exit(202);
150 }
151 }
152
Andreas Gampefd12eda2016-07-12 09:47:17 -0700153 // Try to mount the vendor partition. update_engine doesn't do this for us, but we
154 // want it for vendor APKs.
155 // Notes:
156 // 1) We pretty much guess a name here and hope to find the partition by name.
157 // It is just as complicated and brittle to scan /proc/mounts. But this requires
158 // validating the target-slot so as not to try to mount some totally random path.
159 // 2) We're in a mount namespace here, so when we die, this will be cleaned up.
160 // 3) Ignore errors. Printing anything at this stage will open a file descriptor
161 // for logging.
162 if (!ValidateTargetSlotSuffix(arg[2])) {
163 LOG(ERROR) << "Target slot suffix not legal: " << arg[2];
164 exit(207);
165 }
Andreas Gampe54b62c92019-03-21 11:34:19 -0700166 TryExtraMount("vendor", arg[2], "/postinstall/vendor");
Andreas Gampeb87a1c72018-04-13 17:42:23 -0700167
168 // Try to mount the product partition. update_engine doesn't do this for us, but we
169 // want it for product APKs. Same notes as vendor above.
Andreas Gampe54b62c92019-03-21 11:34:19 -0700170 TryExtraMount("product", arg[2], "/postinstall/product");
Andreas Gampefd12eda2016-07-12 09:47:17 -0700171
Alex Light2b307a02021-03-03 18:35:47 -0800172 constexpr const char* kPostInstallLinkerconfig = "/postinstall/linkerconfig";
173 // Try to mount /postinstall/linkerconfig. we will set it up after performing the chroot
174 if (mount("tmpfs", kPostInstallLinkerconfig, "tmpfs", 0, nullptr) != 0) {
175 PLOG(ERROR) << "Failed to mount a tmpfs for " << kPostInstallLinkerconfig;
176 exit(215);
177 }
178
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
Martin Stjernholmf4caaa02019-07-17 22:14:14 +0100235 // the ART APEX, as it is required by otapreopt to run dex2oat.
Alex Light53e94382021-03-09 16:39:55 -0800236 ActivateApexPackages();
Roland Levillainc19c6042018-12-18 12:15:12 +0000237
Martin Stjernholmf4caaa02019-07-17 22:14:14 +0100238 // Check that an ART APEX has been activated; clean up and exit
Roland Levillain8a8ca152019-07-11 18:48:05 +0100239 // early otherwise.
Alex Light2b307a02021-03-03 18:35:47 -0800240 static constexpr const std::string_view kRequiredApexs[] = {
241 "com.android.art",
242 "com.android.runtime",
243 };
Alex Light53e94382021-03-09 16:39:55 -0800244 std::array<bool, arraysize(kRequiredApexs)> found_apexs{ false, false };
245 DIR* apex_dir = opendir("/apex");
246 if (apex_dir == nullptr) {
247 PLOG(ERROR) << "unable to open /apex";
248 exit(220);
249 }
250 for (dirent* entry = readdir(apex_dir); entry != nullptr; entry = readdir(apex_dir)) {
251 for (int i = 0; i < found_apexs.size(); i++) {
252 if (kRequiredApexs[i] == std::string_view(entry->d_name)) {
253 found_apexs[i] = true;
254 break;
255 }
Alex Light2b307a02021-03-03 18:35:47 -0800256 }
257 }
Alex Light53e94382021-03-09 16:39:55 -0800258 closedir(apex_dir);
259 auto it = std::find(found_apexs.cbegin(), found_apexs.cend(), false);
260 if (it != found_apexs.cend()) {
261 LOG(ERROR) << "No activated " << kRequiredApexs[std::distance(found_apexs.cbegin(), it)]
262 << " package!";
263 exit(221);
264 }
Alex Light2b307a02021-03-03 18:35:47 -0800265
266 // Setup /linkerconfig. Doing it after the chroot means it doesn't need its own category
267 if (selinux_android_restorecon("/linkerconfig", 0) < 0) {
268 PLOG(ERROR) << "Failed to restorecon /linkerconfig";
269 exit(219);
270 }
271 std::vector<std::string> linkerconfig_cmd{"/apex/com.android.runtime/bin/linkerconfig",
272 "--target", "/linkerconfig"};
273 std::string linkerconfig_error_msg;
274 bool linkerconfig_exec_result = Exec(linkerconfig_cmd, &linkerconfig_error_msg);
275 if (!linkerconfig_exec_result) {
276 LOG(ERROR) << "Running linkerconfig failed: " << linkerconfig_error_msg;
277 exit(218);
Roland Levillain8a8ca152019-07-11 18:48:05 +0100278 }
279
Andreas Gampe01ad5982016-03-09 16:27:29 -0800280 // Now go on and run otapreopt.
281
Roland Levillain94b41802019-01-18 11:56:50 +0000282 // Incoming: cmd + status-fd + target-slot + cmd... | Incoming | = argc
283 // Outgoing: cmd + target-slot + cmd... | Outgoing | = argc - 1
284 std::vector<std::string> cmd;
285 cmd.reserve(argc);
286 cmd.push_back("/system/bin/otapreopt");
Andreas Gamped089ca12016-06-27 14:25:30 -0700287
288 // The first parameter is the status file descriptor, skip.
Roland Levillain94b41802019-01-18 11:56:50 +0000289 for (size_t i = 2; i < static_cast<size_t>(argc); ++i) {
290 cmd.push_back(arg[i]);
Andreas Gamped089ca12016-06-27 14:25:30 -0700291 }
Andreas Gampe01ad5982016-03-09 16:27:29 -0800292
Roland Levillain94b41802019-01-18 11:56:50 +0000293 // Fork and execute otapreopt in its own process.
294 std::string error_msg;
295 bool exec_result = Exec(cmd, &error_msg);
296 if (!exec_result) {
297 LOG(ERROR) << "Running otapreopt failed: " << error_msg;
298 }
299
Roland Levillain94b41802019-01-18 11:56:50 +0000300 if (!exec_result) {
301 exit(213);
302 }
303
304 return 0;
Andreas Gampe01ad5982016-03-09 16:27:29 -0800305}
306
307} // namespace installd
308} // namespace android
309
310int main(const int argc, char *argv[]) {
311 return android::installd::otapreopt_chroot(argc, argv);
312}