101ad5984dd202311a7e301c8c771a5d4b7c76136Andreas Gampe/*
201ad5984dd202311a7e301c8c771a5d4b7c76136Andreas Gampe ** Copyright 2016, The Android Open Source Project
301ad5984dd202311a7e301c8c771a5d4b7c76136Andreas Gampe **
401ad5984dd202311a7e301c8c771a5d4b7c76136Andreas Gampe ** Licensed under the Apache License, Version 2.0 (the "License");
501ad5984dd202311a7e301c8c771a5d4b7c76136Andreas Gampe ** you may not use this file except in compliance with the License.
601ad5984dd202311a7e301c8c771a5d4b7c76136Andreas Gampe ** You may obtain a copy of the License at
701ad5984dd202311a7e301c8c771a5d4b7c76136Andreas Gampe **
801ad5984dd202311a7e301c8c771a5d4b7c76136Andreas Gampe **     http://www.apache.org/licenses/LICENSE-2.0
901ad5984dd202311a7e301c8c771a5d4b7c76136Andreas Gampe **
1001ad5984dd202311a7e301c8c771a5d4b7c76136Andreas Gampe ** Unless required by applicable law or agreed to in writing, software
1101ad5984dd202311a7e301c8c771a5d4b7c76136Andreas Gampe ** distributed under the License is distributed on an "AS IS" BASIS,
1201ad5984dd202311a7e301c8c771a5d4b7c76136Andreas Gampe ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1301ad5984dd202311a7e301c8c771a5d4b7c76136Andreas Gampe ** See the License for the specific language governing permissions and
1401ad5984dd202311a7e301c8c771a5d4b7c76136Andreas Gampe ** limitations under the License.
1501ad5984dd202311a7e301c8c771a5d4b7c76136Andreas Gampe */
1601ad5984dd202311a7e301c8c771a5d4b7c76136Andreas Gampe
17d089ca1703769854356a263ca640d3e07ab8548dAndreas Gampe#include <fcntl.h>
1801ad5984dd202311a7e301c8c771a5d4b7c76136Andreas Gampe#include <linux/unistd.h>
1901ad5984dd202311a7e301c8c771a5d4b7c76136Andreas Gampe#include <sys/mount.h>
2001ad5984dd202311a7e301c8c771a5d4b7c76136Andreas Gampe#include <sys/wait.h>
2101ad5984dd202311a7e301c8c771a5d4b7c76136Andreas Gampe
22d089ca1703769854356a263ca640d3e07ab8548dAndreas Gampe#include <sstream>
23d089ca1703769854356a263ca640d3e07ab8548dAndreas Gampe
2401ad5984dd202311a7e301c8c771a5d4b7c76136Andreas Gampe#include <android-base/logging.h>
2501ad5984dd202311a7e301c8c771a5d4b7c76136Andreas Gampe#include <android-base/macros.h>
2601ad5984dd202311a7e301c8c771a5d4b7c76136Andreas Gampe#include <android-base/stringprintf.h>
2701ad5984dd202311a7e301c8c771a5d4b7c76136Andreas Gampe
280274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey#include "installd_constants.h"
290274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey#include "otapreopt_utils.h"
30548bdb930895d8fe6651eaada0bc5739ee5248c8Andreas Gampe
3101ad5984dd202311a7e301c8c771a5d4b7c76136Andreas Gampe#ifndef LOG_TAG
3201ad5984dd202311a7e301c8c771a5d4b7c76136Andreas Gampe#define LOG_TAG "otapreopt"
3301ad5984dd202311a7e301c8c771a5d4b7c76136Andreas Gampe#endif
3401ad5984dd202311a7e301c8c771a5d4b7c76136Andreas Gampe
3501ad5984dd202311a7e301c8c771a5d4b7c76136Andreas Gampeusing android::base::StringPrintf;
3601ad5984dd202311a7e301c8c771a5d4b7c76136Andreas Gampe
3701ad5984dd202311a7e301c8c771a5d4b7c76136Andreas Gampenamespace android {
3801ad5984dd202311a7e301c8c771a5d4b7c76136Andreas Gampenamespace installd {
3901ad5984dd202311a7e301c8c771a5d4b7c76136Andreas Gampe
40d089ca1703769854356a263ca640d3e07ab8548dAndreas Gampestatic void CloseDescriptor(int fd) {
41d089ca1703769854356a263ca640d3e07ab8548dAndreas Gampe    if (fd >= 0) {
42d089ca1703769854356a263ca640d3e07ab8548dAndreas Gampe        int result = close(fd);
43d089ca1703769854356a263ca640d3e07ab8548dAndreas Gampe        UNUSED(result);  // Ignore result. Printing to logcat will open a new descriptor
44d089ca1703769854356a263ca640d3e07ab8548dAndreas Gampe                         // that we do *not* want.
45d089ca1703769854356a263ca640d3e07ab8548dAndreas Gampe    }
46d089ca1703769854356a263ca640d3e07ab8548dAndreas Gampe}
47d089ca1703769854356a263ca640d3e07ab8548dAndreas Gampe
48d089ca1703769854356a263ca640d3e07ab8548dAndreas Gampestatic void CloseDescriptor(const char* descriptor_string) {
49d089ca1703769854356a263ca640d3e07ab8548dAndreas Gampe    int fd = -1;
50d089ca1703769854356a263ca640d3e07ab8548dAndreas Gampe    std::istringstream stream(descriptor_string);
51d089ca1703769854356a263ca640d3e07ab8548dAndreas Gampe    stream >> fd;
52d089ca1703769854356a263ca640d3e07ab8548dAndreas Gampe    if (!stream.fail()) {
53d089ca1703769854356a263ca640d3e07ab8548dAndreas Gampe        CloseDescriptor(fd);
54d089ca1703769854356a263ca640d3e07ab8548dAndreas Gampe    }
55d089ca1703769854356a263ca640d3e07ab8548dAndreas Gampe}
56d089ca1703769854356a263ca640d3e07ab8548dAndreas Gampe
57d089ca1703769854356a263ca640d3e07ab8548dAndreas Gampe// Entry for otapreopt_chroot. Expected parameters are:
58d089ca1703769854356a263ca640d3e07ab8548dAndreas Gampe//   [cmd] [status-fd] [target-slot] "dexopt" [dexopt-params]
59d089ca1703769854356a263ca640d3e07ab8548dAndreas Gampe// The file descriptor denoted by status-fd will be closed. The rest of the parameters will
60d089ca1703769854356a263ca640d3e07ab8548dAndreas Gampe// be passed on to otapreopt in the chroot.
6101ad5984dd202311a7e301c8c771a5d4b7c76136Andreas Gampestatic int otapreopt_chroot(const int argc, char **arg) {
62d089ca1703769854356a263ca640d3e07ab8548dAndreas Gampe    // Close all file descriptors. They are coming from the caller, we do not want to pass them
63d089ca1703769854356a263ca640d3e07ab8548dAndreas Gampe    // on across our fork/exec into a different domain.
64d089ca1703769854356a263ca640d3e07ab8548dAndreas Gampe    // 1) Default descriptors.
65d089ca1703769854356a263ca640d3e07ab8548dAndreas Gampe    CloseDescriptor(STDIN_FILENO);
66d089ca1703769854356a263ca640d3e07ab8548dAndreas Gampe    CloseDescriptor(STDOUT_FILENO);
67d089ca1703769854356a263ca640d3e07ab8548dAndreas Gampe    CloseDescriptor(STDERR_FILENO);
68d089ca1703769854356a263ca640d3e07ab8548dAndreas Gampe    // 2) The status channel.
69d089ca1703769854356a263ca640d3e07ab8548dAndreas Gampe    CloseDescriptor(arg[1]);
70d089ca1703769854356a263ca640d3e07ab8548dAndreas Gampe
7101ad5984dd202311a7e301c8c771a5d4b7c76136Andreas Gampe    // We need to run the otapreopt tool from the postinstall partition. As such, set up a
7201ad5984dd202311a7e301c8c771a5d4b7c76136Andreas Gampe    // mount namespace and change root.
7301ad5984dd202311a7e301c8c771a5d4b7c76136Andreas Gampe
7401ad5984dd202311a7e301c8c771a5d4b7c76136Andreas Gampe    // Create our own mount namespace.
7501ad5984dd202311a7e301c8c771a5d4b7c76136Andreas Gampe    if (unshare(CLONE_NEWNS) != 0) {
7601ad5984dd202311a7e301c8c771a5d4b7c76136Andreas Gampe        PLOG(ERROR) << "Failed to unshare() for otapreopt.";
7701ad5984dd202311a7e301c8c771a5d4b7c76136Andreas Gampe        exit(200);
7801ad5984dd202311a7e301c8c771a5d4b7c76136Andreas Gampe    }
7901ad5984dd202311a7e301c8c771a5d4b7c76136Andreas Gampe
8001ad5984dd202311a7e301c8c771a5d4b7c76136Andreas Gampe    // Make postinstall private, so that our changes don't propagate.
8101ad5984dd202311a7e301c8c771a5d4b7c76136Andreas Gampe    if (mount("", "/postinstall", nullptr, MS_PRIVATE, nullptr) != 0) {
8201ad5984dd202311a7e301c8c771a5d4b7c76136Andreas Gampe        PLOG(ERROR) << "Failed to mount private.";
8301ad5984dd202311a7e301c8c771a5d4b7c76136Andreas Gampe        exit(201);
8401ad5984dd202311a7e301c8c771a5d4b7c76136Andreas Gampe    }
8501ad5984dd202311a7e301c8c771a5d4b7c76136Andreas Gampe
8601ad5984dd202311a7e301c8c771a5d4b7c76136Andreas Gampe    // Bind mount necessary directories.
8701ad5984dd202311a7e301c8c771a5d4b7c76136Andreas Gampe    constexpr const char* kBindMounts[] = {
8801ad5984dd202311a7e301c8c771a5d4b7c76136Andreas Gampe            "/data", "/dev", "/proc", "/sys"
8901ad5984dd202311a7e301c8c771a5d4b7c76136Andreas Gampe    };
9001ad5984dd202311a7e301c8c771a5d4b7c76136Andreas Gampe    for (size_t i = 0; i < arraysize(kBindMounts); ++i) {
9101ad5984dd202311a7e301c8c771a5d4b7c76136Andreas Gampe        std::string trg = StringPrintf("/postinstall%s", kBindMounts[i]);
9201ad5984dd202311a7e301c8c771a5d4b7c76136Andreas Gampe        if (mount(kBindMounts[i], trg.c_str(), nullptr, MS_BIND, nullptr) != 0) {
9301ad5984dd202311a7e301c8c771a5d4b7c76136Andreas Gampe            PLOG(ERROR) << "Failed to bind-mount " << kBindMounts[i];
9401ad5984dd202311a7e301c8c771a5d4b7c76136Andreas Gampe            exit(202);
9501ad5984dd202311a7e301c8c771a5d4b7c76136Andreas Gampe        }
9601ad5984dd202311a7e301c8c771a5d4b7c76136Andreas Gampe    }
9701ad5984dd202311a7e301c8c771a5d4b7c76136Andreas Gampe
98fd12edaeab839f3f1f087cc75bd18b4d8af5b192Andreas Gampe    // Try to mount the vendor partition. update_engine doesn't do this for us, but we
99fd12edaeab839f3f1f087cc75bd18b4d8af5b192Andreas Gampe    // want it for vendor APKs.
100fd12edaeab839f3f1f087cc75bd18b4d8af5b192Andreas Gampe    // Notes:
101fd12edaeab839f3f1f087cc75bd18b4d8af5b192Andreas Gampe    //  1) We pretty much guess a name here and hope to find the partition by name.
102fd12edaeab839f3f1f087cc75bd18b4d8af5b192Andreas Gampe    //     It is just as complicated and brittle to scan /proc/mounts. But this requires
103fd12edaeab839f3f1f087cc75bd18b4d8af5b192Andreas Gampe    //     validating the target-slot so as not to try to mount some totally random path.
104fd12edaeab839f3f1f087cc75bd18b4d8af5b192Andreas Gampe    //  2) We're in a mount namespace here, so when we die, this will be cleaned up.
105fd12edaeab839f3f1f087cc75bd18b4d8af5b192Andreas Gampe    //  3) Ignore errors. Printing anything at this stage will open a file descriptor
106fd12edaeab839f3f1f087cc75bd18b4d8af5b192Andreas Gampe    //     for logging.
107fd12edaeab839f3f1f087cc75bd18b4d8af5b192Andreas Gampe    if (!ValidateTargetSlotSuffix(arg[2])) {
108fd12edaeab839f3f1f087cc75bd18b4d8af5b192Andreas Gampe        LOG(ERROR) << "Target slot suffix not legal: " << arg[2];
109fd12edaeab839f3f1f087cc75bd18b4d8af5b192Andreas Gampe        exit(207);
110fd12edaeab839f3f1f087cc75bd18b4d8af5b192Andreas Gampe    }
111fd12edaeab839f3f1f087cc75bd18b4d8af5b192Andreas Gampe    std::string vendor_partition = StringPrintf("/dev/block/bootdevice/by-name/vendor%s",
112fd12edaeab839f3f1f087cc75bd18b4d8af5b192Andreas Gampe                                                arg[2]);
113fd12edaeab839f3f1f087cc75bd18b4d8af5b192Andreas Gampe    int vendor_result = mount(vendor_partition.c_str(),
114fd12edaeab839f3f1f087cc75bd18b4d8af5b192Andreas Gampe                              "/postinstall/vendor",
115fd12edaeab839f3f1f087cc75bd18b4d8af5b192Andreas Gampe                              "ext4",
116fd12edaeab839f3f1f087cc75bd18b4d8af5b192Andreas Gampe                              MS_RDONLY,
117fd12edaeab839f3f1f087cc75bd18b4d8af5b192Andreas Gampe                              /* data */ nullptr);
118fd12edaeab839f3f1f087cc75bd18b4d8af5b192Andreas Gampe    UNUSED(vendor_result);
119fd12edaeab839f3f1f087cc75bd18b4d8af5b192Andreas Gampe
12001ad5984dd202311a7e301c8c771a5d4b7c76136Andreas Gampe    // Chdir into /postinstall.
12101ad5984dd202311a7e301c8c771a5d4b7c76136Andreas Gampe    if (chdir("/postinstall") != 0) {
12201ad5984dd202311a7e301c8c771a5d4b7c76136Andreas Gampe        PLOG(ERROR) << "Unable to chdir into /postinstall.";
12301ad5984dd202311a7e301c8c771a5d4b7c76136Andreas Gampe        exit(203);
12401ad5984dd202311a7e301c8c771a5d4b7c76136Andreas Gampe    }
12501ad5984dd202311a7e301c8c771a5d4b7c76136Andreas Gampe
12601ad5984dd202311a7e301c8c771a5d4b7c76136Andreas Gampe    // Make /postinstall the root in our mount namespace.
12701ad5984dd202311a7e301c8c771a5d4b7c76136Andreas Gampe    if (chroot(".")  != 0) {
12801ad5984dd202311a7e301c8c771a5d4b7c76136Andreas Gampe        PLOG(ERROR) << "Failed to chroot";
12901ad5984dd202311a7e301c8c771a5d4b7c76136Andreas Gampe        exit(204);
13001ad5984dd202311a7e301c8c771a5d4b7c76136Andreas Gampe    }
13101ad5984dd202311a7e301c8c771a5d4b7c76136Andreas Gampe
13201ad5984dd202311a7e301c8c771a5d4b7c76136Andreas Gampe    if (chdir("/") != 0) {
13301ad5984dd202311a7e301c8c771a5d4b7c76136Andreas Gampe        PLOG(ERROR) << "Unable to chdir into /.";
13401ad5984dd202311a7e301c8c771a5d4b7c76136Andreas Gampe        exit(205);
13501ad5984dd202311a7e301c8c771a5d4b7c76136Andreas Gampe    }
13601ad5984dd202311a7e301c8c771a5d4b7c76136Andreas Gampe
13701ad5984dd202311a7e301c8c771a5d4b7c76136Andreas Gampe    // Now go on and run otapreopt.
13801ad5984dd202311a7e301c8c771a5d4b7c76136Andreas Gampe
139c093d3a68143f73b0ad89678874086e2b84a5675Andreas Gampe    // Incoming:  cmd + status-fd + target-slot + cmd... + null      | Incoming | = argc + 1
140c093d3a68143f73b0ad89678874086e2b84a5675Andreas Gampe    // Outgoing:  cmd             + target-slot + cmd... + null      | Outgoing | = argc
141c093d3a68143f73b0ad89678874086e2b84a5675Andreas Gampe    const char** argv = new const char*[argc];
142c093d3a68143f73b0ad89678874086e2b84a5675Andreas Gampe
14301ad5984dd202311a7e301c8c771a5d4b7c76136Andreas Gampe    argv[0] = "/system/bin/otapreopt";
144d089ca1703769854356a263ca640d3e07ab8548dAndreas Gampe
145d089ca1703769854356a263ca640d3e07ab8548dAndreas Gampe    // The first parameter is the status file descriptor, skip.
146c093d3a68143f73b0ad89678874086e2b84a5675Andreas Gampe    for (size_t i = 2; i <= static_cast<size_t>(argc); ++i) {
147c093d3a68143f73b0ad89678874086e2b84a5675Andreas Gampe        argv[i - 1] = arg[i];
14801ad5984dd202311a7e301c8c771a5d4b7c76136Andreas Gampe    }
14901ad5984dd202311a7e301c8c771a5d4b7c76136Andreas Gampe
150c093d3a68143f73b0ad89678874086e2b84a5675Andreas Gampe    execv(argv[0], static_cast<char * const *>(const_cast<char**>(argv)));
15101ad5984dd202311a7e301c8c771a5d4b7c76136Andreas Gampe    PLOG(ERROR) << "execv(OTAPREOPT) failed.";
15201ad5984dd202311a7e301c8c771a5d4b7c76136Andreas Gampe    exit(99);
15301ad5984dd202311a7e301c8c771a5d4b7c76136Andreas Gampe}
15401ad5984dd202311a7e301c8c771a5d4b7c76136Andreas Gampe
15501ad5984dd202311a7e301c8c771a5d4b7c76136Andreas Gampe}  // namespace installd
15601ad5984dd202311a7e301c8c771a5d4b7c76136Andreas Gampe}  // namespace android
15701ad5984dd202311a7e301c8c771a5d4b7c76136Andreas Gampe
15801ad5984dd202311a7e301c8c771a5d4b7c76136Andreas Gampeint main(const int argc, char *argv[]) {
15901ad5984dd202311a7e301c8c771a5d4b7c76136Andreas Gampe    return android::installd::otapreopt_chroot(argc, argv);
16001ad5984dd202311a7e301c8c771a5d4b7c76136Andreas Gampe}
161