blob: d1ff90d9e7a2e96c4d1ba440ed43b8c85e9a5e3d [file] [log] [blame]
Nikita Ioffe4db13a52019-03-14 23:26:08 +00001/*
2 * Copyright (C) 2019 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
17#include "apex_shim.h"
18
Nikita Ioffe6d6c74f2019-03-18 14:54:38 +000019#include <android-base/file.h>
Nikita Ioffe4db13a52019-03-14 23:26:08 +000020#include <android-base/logging.h>
Nikita Ioffe6d6c74f2019-03-18 14:54:38 +000021#include <android-base/stringprintf.h>
22#include <android-base/strings.h>
23#include <openssl/sha.h>
Nikita Ioffe2cbca0e2019-03-27 19:12:11 +000024#include <filesystem>
Nikita Ioffe6d6c74f2019-03-18 14:54:38 +000025#include <fstream>
26#include <sstream>
Nikita Ioffe4db13a52019-03-14 23:26:08 +000027#include <unordered_set>
28
Nikita Ioffec367f312019-07-27 15:57:35 +010029#include "apex_constants.h"
Nikita Ioffe4db13a52019-03-14 23:26:08 +000030#include "apex_file.h"
Nikita Ioffe4db13a52019-03-14 23:26:08 +000031#include "string_log.h"
32
Mohammad Samiul Islambd6ab0f2019-06-20 15:55:27 +010033using android::base::ErrnoError;
34using android::base::Error;
Mohammad Samiul Islambd6ab0f2019-06-20 15:55:27 +010035using android::base::Result;
36
Nikita Ioffe4db13a52019-03-14 23:26:08 +000037namespace android {
38namespace apex {
39namespace shim {
40
Nikita Ioffe2cbca0e2019-03-27 19:12:11 +000041namespace fs = std::filesystem;
42
Nikita Ioffe4db13a52019-03-14 23:26:08 +000043namespace {
44
Nikita Ioffed9a25d42019-03-26 01:37:03 +000045static constexpr const char* kApexCtsShimPackage = "com.android.apex.cts.shim";
Dario Freni9b813b52019-11-18 21:52:14 +000046static constexpr const char* kHashFilePath = "etc/hash.txt";
Nikita Ioffe6d6c74f2019-03-18 14:54:38 +000047static constexpr const int kBufSize = 1024;
Dario Freni9b813b52019-11-18 21:52:14 +000048static constexpr const fs::perms kForbiddenFilePermissions =
Nikita Ioffe2cbca0e2019-03-27 19:12:11 +000049 fs::perms::owner_exec | fs::perms::group_exec | fs::perms::others_exec;
Dario Freni9b813b52019-11-18 21:52:14 +000050static constexpr const char* kExpectedCtsShimFiles[] = {
51 "apex_manifest.json",
52 "apex_manifest.pb",
53 "etc/hash.txt",
Dario Freni3975cc72020-04-23 12:34:30 +010054 "app/CtsShim/CtsShim.apk",
55 "app/CtsShimTargetPSdk/CtsShimTargetPSdk.apk",
56 "priv-app/CtsShimPriv/CtsShimPriv.apk",
Dario Freni9b813b52019-11-18 21:52:14 +000057};
Nikita Ioffe4db13a52019-03-14 23:26:08 +000058
Mohammad Samiul Islambd6ab0f2019-06-20 15:55:27 +010059Result<std::string> CalculateSha512(const std::string& path) {
Nikita Ioffe6d6c74f2019-03-18 14:54:38 +000060 LOG(DEBUG) << "Calculating SHA512 of " << path;
61 SHA512_CTX ctx;
62 SHA512_Init(&ctx);
63 std::ifstream apex(path, std::ios::binary);
64 if (apex.bad()) {
Mohammad Samiul Islambd6ab0f2019-06-20 15:55:27 +010065 return Error() << "Failed to open " << path;
Nikita Ioffe6d6c74f2019-03-18 14:54:38 +000066 }
67 char buf[kBufSize];
68 while (!apex.eof()) {
69 apex.read(buf, kBufSize);
70 if (apex.bad()) {
Mohammad Samiul Islambd6ab0f2019-06-20 15:55:27 +010071 return Error() << "Failed to read " << path;
Nikita Ioffe6d6c74f2019-03-18 14:54:38 +000072 }
73 int bytes_read = apex.gcount();
74 SHA512_Update(&ctx, buf, bytes_read);
75 }
76 uint8_t hash[SHA512_DIGEST_LENGTH];
77 SHA512_Final(hash, &ctx);
78 std::stringstream ss;
79 ss << std::hex;
80 for (int i = 0; i < SHA512_DIGEST_LENGTH; i++) {
81 ss << std::setw(2) << std::setfill('0') << static_cast<int>(hash[i]);
82 }
Mohammad Samiul Islambd6ab0f2019-06-20 15:55:27 +010083 return ss.str();
Nikita Ioffe6d6c74f2019-03-18 14:54:38 +000084}
85
Nikita Ioffec367f312019-07-27 15:57:35 +010086Result<std::vector<std::string>> GetAllowedHashes(const std::string& path) {
Nikita Ioffe2cbca0e2019-03-27 19:12:11 +000087 using android::base::ReadFileToString;
88 using android::base::StringPrintf;
Nikita Ioffe2cbca0e2019-03-27 19:12:11 +000089 const std::string& file_path =
Dario Freni9b813b52019-11-18 21:52:14 +000090 StringPrintf("%s/%s", path.c_str(), kHashFilePath);
Nikita Ioffe6d6c74f2019-03-18 14:54:38 +000091 LOG(DEBUG) << "Reading SHA512 from " << file_path;
92 std::string hash;
Nikita Ioffe2cbca0e2019-03-27 19:12:11 +000093 if (!ReadFileToString(file_path, &hash, false /* follows symlinks */)) {
Mohammad Samiul Islambd6ab0f2019-06-20 15:55:27 +010094 return ErrnoError() << "Failed to read " << file_path;
Nikita Ioffe6d6c74f2019-03-18 14:54:38 +000095 }
Nikita Ioffec367f312019-07-27 15:57:35 +010096 std::vector<std::string> allowed_hashes = android::base::Split(hash, "\n");
97 auto system_shim_hash = CalculateSha512(
98 StringPrintf("%s/%s", kApexPackageSystemDir, shim::kSystemShimApexName));
Bernie Innocentid04d5d02020-02-06 22:01:51 +090099 if (!system_shim_hash.ok()) {
Nikita Ioffec367f312019-07-27 15:57:35 +0100100 return system_shim_hash.error();
101 }
102 allowed_hashes.push_back(std::move(*system_shim_hash));
103 return allowed_hashes;
Nikita Ioffe6d6c74f2019-03-18 14:54:38 +0000104}
Nikita Ioffe4db13a52019-03-14 23:26:08 +0000105} // namespace
106
107bool IsShimApex(const ApexFile& apex_file) {
108 return apex_file.GetManifest().name() == kApexCtsShimPackage;
109}
110
Mohammad Samiul Islambd6ab0f2019-06-20 15:55:27 +0100111Result<void> ValidateShimApex(const std::string& mount_point,
112 const ApexFile& apex_file) {
Nikita Ioffe2cbca0e2019-03-27 19:12:11 +0000113 LOG(DEBUG) << "Validating shim apex " << mount_point;
Nikita Ioffe69a1bc52019-03-29 14:55:29 +0000114 const ApexManifest& manifest = apex_file.GetManifest();
115 if (!manifest.preinstallhook().empty() ||
116 !manifest.postinstallhook().empty()) {
Mohammad Samiul Islambd6ab0f2019-06-20 15:55:27 +0100117 return Errorf("Shim apex is not allowed to have pre or post install hooks");
Nikita Ioffe69a1bc52019-03-29 14:55:29 +0000118 }
Nikita Ioffe2cbca0e2019-03-27 19:12:11 +0000119 std::error_code ec;
Dario Freni9b813b52019-11-18 21:52:14 +0000120 std::unordered_set<std::string> expected_files;
121 for (auto file : kExpectedCtsShimFiles) {
122 expected_files.insert(file);
Nikita Ioffe2cbca0e2019-03-27 19:12:11 +0000123 }
Dario Freni9b813b52019-11-18 21:52:14 +0000124
125 auto iter = fs::recursive_directory_iterator(mount_point, ec);
126 // Unfortunately fs::recursive_directory_iterator::operator++ can throw an
127 // exception, which means that it's impossible to use range-based for loop
128 // here.
Nikita Ioffe2cbca0e2019-03-27 19:12:11 +0000129 // TODO: wrap into a non-throwing iterator to support range-based for loop.
130 while (iter != fs::end(iter)) {
Dario Freni9b813b52019-11-18 21:52:14 +0000131 auto path = iter->path();
132 // Resolve the mount point to ensure any trailing slash is removed.
133 auto resolved_mount_point = fs::path(mount_point).string();
134 auto local_path = path.string().substr(resolved_mount_point.length() + 1);
135 fs::file_status status = iter->status(ec);
136
137 if (fs::is_symlink(status)) {
138 return Error()
139 << "Shim apex is not allowed to contain symbolic links, found "
140 << path;
141 } else if (fs::is_regular_file(status)) {
142 if ((status.permissions() & kForbiddenFilePermissions) !=
143 fs::perms::none) {
144 return Error() << path << " has illegal permissions";
145 }
146 auto ex = expected_files.find(local_path);
147 if (ex != expected_files.end()) {
148 expected_files.erase(local_path);
149 } else {
150 return Error() << path << " is an unexpected file inside the shim apex";
151 }
152 } else if (!fs::is_directory(status)) {
153 // If this is not a symlink, a file or a directory, fail.
154 return Error() << "Unexpected file entry in shim apex: " << iter->path();
Nikita Ioffe2cbca0e2019-03-27 19:12:11 +0000155 }
156 iter = iter.increment(ec);
157 if (ec) {
Mohammad Samiul Islambd6ab0f2019-06-20 15:55:27 +0100158 return Error() << "Failed to scan " << mount_point << " : "
159 << ec.message();
Nikita Ioffe2cbca0e2019-03-27 19:12:11 +0000160 }
161 }
Dario Freni9b813b52019-11-18 21:52:14 +0000162
Mohammad Samiul Islambd6ab0f2019-06-20 15:55:27 +0100163 return {};
Nikita Ioffe4db13a52019-03-14 23:26:08 +0000164}
165
Mohammad Samiul Islambd6ab0f2019-06-20 15:55:27 +0100166Result<void> ValidateUpdate(const std::string& system_apex_path,
167 const std::string& new_apex_path) {
Nikita Ioffedcb96692019-04-11 16:09:48 +0100168 LOG(DEBUG) << "Validating update of shim apex to " << new_apex_path
169 << " using system shim apex " << system_apex_path;
Nikita Ioffec367f312019-07-27 15:57:35 +0100170 auto allowed = GetAllowedHashes(system_apex_path);
Bernie Innocentid04d5d02020-02-06 22:01:51 +0900171 if (!allowed.ok()) {
Mohammad Samiul Islambd6ab0f2019-06-20 15:55:27 +0100172 return allowed.error();
Nikita Ioffe6d6c74f2019-03-18 14:54:38 +0000173 }
Nikita Ioffe98054d72019-04-03 14:18:22 +0100174 auto actual = CalculateSha512(new_apex_path);
Bernie Innocentid04d5d02020-02-06 22:01:51 +0900175 if (!actual.ok()) {
Mohammad Samiul Islambd6ab0f2019-06-20 15:55:27 +0100176 return actual.error();
Nikita Ioffe6d6c74f2019-03-18 14:54:38 +0000177 }
Nikita Ioffe98054d72019-04-03 14:18:22 +0100178 auto it = std::find(allowed->begin(), allowed->end(), *actual);
179 if (it == allowed->end()) {
Mohammad Samiul Islambd6ab0f2019-06-20 15:55:27 +0100180 return Error() << new_apex_path << " has unexpected SHA512 hash "
181 << *actual;
Nikita Ioffe6d6c74f2019-03-18 14:54:38 +0000182 }
Mohammad Samiul Islambd6ab0f2019-06-20 15:55:27 +0100183 return {};
Nikita Ioffe4db13a52019-03-14 23:26:08 +0000184}
185
186} // namespace shim
187} // namespace apex
188} // namespace android