Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 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 | |
Ryan Mitchell | 52e1f7a | 2019-04-12 12:31:42 -0700 | [diff] [blame] | 17 | #include "idmap2d/Idmap2Service.h" |
| 18 | |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 19 | #include <sys/stat.h> // umask |
| 20 | #include <sys/types.h> // umask |
| 21 | #include <unistd.h> |
| 22 | |
| 23 | #include <cerrno> |
| 24 | #include <cstring> |
| 25 | #include <fstream> |
| 26 | #include <memory> |
| 27 | #include <ostream> |
| 28 | #include <string> |
| 29 | |
| 30 | #include "android-base/macros.h" |
Mårten Kongstad | d10d06d | 2019-01-07 17:26:25 -0800 | [diff] [blame] | 31 | #include "android-base/stringprintf.h" |
Mårten Kongstad | 1da49dc | 2019-01-14 10:03:53 +0100 | [diff] [blame] | 32 | #include "binder/IPCThreadState.h" |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 33 | #include "idmap2/BinaryStreamVisitor.h" |
| 34 | #include "idmap2/FileUtils.h" |
| 35 | #include "idmap2/Idmap.h" |
Mårten Kongstad | 4cbb007 | 2018-11-30 16:22:05 +0100 | [diff] [blame] | 36 | #include "idmap2/SysTrace.h" |
Ryan Mitchell | 7d53f19 | 2020-04-24 17:45:25 -0700 | [diff] [blame^] | 37 | #include "idmap2/ZipFile.h" |
Ryan Mitchell | 52e1f7a | 2019-04-12 12:31:42 -0700 | [diff] [blame] | 38 | #include "utils/String8.h" |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 39 | |
Mårten Kongstad | 1da49dc | 2019-01-14 10:03:53 +0100 | [diff] [blame] | 40 | using android::IPCThreadState; |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 41 | using android::binder::Status; |
| 42 | using android::idmap2::BinaryStreamVisitor; |
Ryan Mitchell | 7d53f19 | 2020-04-24 17:45:25 -0700 | [diff] [blame^] | 43 | using android::idmap2::GetPackageCrc; |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 44 | using android::idmap2::Idmap; |
| 45 | using android::idmap2::IdmapHeader; |
Mårten Kongstad | 1da49dc | 2019-01-14 10:03:53 +0100 | [diff] [blame] | 46 | using android::idmap2::utils::kIdmapCacheDir; |
Mårten Kongstad | b877902 | 2018-11-29 09:53:17 +0100 | [diff] [blame] | 47 | using android::idmap2::utils::kIdmapFilePermissionMask; |
Mårten Kongstad | 1da49dc | 2019-01-14 10:03:53 +0100 | [diff] [blame] | 48 | using android::idmap2::utils::UidHasWriteAccessToPath; |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 49 | |
Winson | 62ac8b5 | 2019-12-04 08:36:48 -0800 | [diff] [blame] | 50 | using PolicyBitmask = android::ResTable_overlayable_policy_header::PolicyBitmask; |
| 51 | |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 52 | namespace { |
| 53 | |
Ryan Mitchell | 7d53f19 | 2020-04-24 17:45:25 -0700 | [diff] [blame^] | 54 | constexpr const char* kFrameworkPath = "/system/framework/framework-res.apk"; |
| 55 | |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 56 | Status ok() { |
| 57 | return Status::ok(); |
| 58 | } |
| 59 | |
| 60 | Status error(const std::string& msg) { |
| 61 | LOG(ERROR) << msg; |
| 62 | return Status::fromExceptionCode(Status::EX_NONE, msg.c_str()); |
| 63 | } |
| 64 | |
Mårten Kongstad | d10d06d | 2019-01-07 17:26:25 -0800 | [diff] [blame] | 65 | PolicyBitmask ConvertAidlArgToPolicyBitmask(int32_t arg) { |
| 66 | return static_cast<PolicyBitmask>(arg); |
| 67 | } |
| 68 | |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 69 | } // namespace |
| 70 | |
Mårten Kongstad | 0eba72a | 2018-11-29 08:23:14 +0100 | [diff] [blame] | 71 | namespace android::os { |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 72 | |
| 73 | Status Idmap2Service::getIdmapPath(const std::string& overlay_apk_path, |
| 74 | int32_t user_id ATTRIBUTE_UNUSED, std::string* _aidl_return) { |
| 75 | assert(_aidl_return); |
Mårten Kongstad | 4cbb007 | 2018-11-30 16:22:05 +0100 | [diff] [blame] | 76 | SYSTRACE << "Idmap2Service::getIdmapPath " << overlay_apk_path; |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 77 | *_aidl_return = Idmap::CanonicalIdmapPathFor(kIdmapCacheDir, overlay_apk_path); |
| 78 | return ok(); |
| 79 | } |
| 80 | |
| 81 | Status Idmap2Service::removeIdmap(const std::string& overlay_apk_path, |
| 82 | int32_t user_id ATTRIBUTE_UNUSED, bool* _aidl_return) { |
| 83 | assert(_aidl_return); |
Mårten Kongstad | 4cbb007 | 2018-11-30 16:22:05 +0100 | [diff] [blame] | 84 | SYSTRACE << "Idmap2Service::removeIdmap " << overlay_apk_path; |
Mårten Kongstad | 1da49dc | 2019-01-14 10:03:53 +0100 | [diff] [blame] | 85 | const uid_t uid = IPCThreadState::self()->getCallingUid(); |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 86 | const std::string idmap_path = Idmap::CanonicalIdmapPathFor(kIdmapCacheDir, overlay_apk_path); |
Mårten Kongstad | 1da49dc | 2019-01-14 10:03:53 +0100 | [diff] [blame] | 87 | if (!UidHasWriteAccessToPath(uid, idmap_path)) { |
| 88 | *_aidl_return = false; |
| 89 | return error(base::StringPrintf("failed to unlink %s: calling uid %d lacks write access", |
| 90 | idmap_path.c_str(), uid)); |
| 91 | } |
Mårten Kongstad | b877902 | 2018-11-29 09:53:17 +0100 | [diff] [blame] | 92 | if (unlink(idmap_path.c_str()) != 0) { |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 93 | *_aidl_return = false; |
| 94 | return error("failed to unlink " + idmap_path + ": " + strerror(errno)); |
| 95 | } |
Mårten Kongstad | b877902 | 2018-11-29 09:53:17 +0100 | [diff] [blame] | 96 | *_aidl_return = true; |
| 97 | return ok(); |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 98 | } |
| 99 | |
hg.choi | a3a6813 | 2020-01-15 17:12:48 +0900 | [diff] [blame] | 100 | Status Idmap2Service::verifyIdmap(const std::string& target_apk_path, |
| 101 | const std::string& overlay_apk_path, |
Mårten Kongstad | d10d06d | 2019-01-07 17:26:25 -0800 | [diff] [blame] | 102 | int32_t fulfilled_policies ATTRIBUTE_UNUSED, |
| 103 | bool enforce_overlayable ATTRIBUTE_UNUSED, |
Mårten Kongstad | ef0695d | 2018-12-04 14:36:48 +0100 | [diff] [blame] | 104 | int32_t user_id ATTRIBUTE_UNUSED, bool* _aidl_return) { |
Mårten Kongstad | 4cbb007 | 2018-11-30 16:22:05 +0100 | [diff] [blame] | 105 | SYSTRACE << "Idmap2Service::verifyIdmap " << overlay_apk_path; |
Mårten Kongstad | ef0695d | 2018-12-04 14:36:48 +0100 | [diff] [blame] | 106 | assert(_aidl_return); |
| 107 | const std::string idmap_path = Idmap::CanonicalIdmapPathFor(kIdmapCacheDir, overlay_apk_path); |
| 108 | std::ifstream fin(idmap_path); |
| 109 | const std::unique_ptr<const IdmapHeader> header = IdmapHeader::FromBinaryStream(fin); |
| 110 | fin.close(); |
hg.choi | a3a6813 | 2020-01-15 17:12:48 +0900 | [diff] [blame] | 111 | if (!header) { |
| 112 | *_aidl_return = false; |
| 113 | return error("failed to parse idmap header"); |
| 114 | } |
| 115 | |
Ryan Mitchell | 7d53f19 | 2020-04-24 17:45:25 -0700 | [diff] [blame^] | 116 | if (strcmp(header->GetTargetPath().data(), target_apk_path.data()) != 0) { |
| 117 | *_aidl_return = false; |
| 118 | return ok(); |
| 119 | } |
| 120 | |
| 121 | if (target_apk_path != kFrameworkPath) { |
| 122 | *_aidl_return = (bool) header->IsUpToDate(); |
| 123 | } else { |
| 124 | if (!android_crc_) { |
| 125 | // Loading the framework zip can take several milliseconds. Cache the crc of the framework |
| 126 | // resource APK to reduce repeated work during boot. |
| 127 | const auto target_zip = idmap2::ZipFile::Open(target_apk_path); |
| 128 | if (!target_zip) { |
| 129 | return error(base::StringPrintf("failed to open target %s", target_apk_path.c_str())); |
| 130 | } |
| 131 | |
| 132 | const auto target_crc = GetPackageCrc(*target_zip); |
| 133 | if (!target_crc) { |
| 134 | return error(target_crc.GetErrorMessage()); |
| 135 | } |
| 136 | |
| 137 | android_crc_ = *target_crc; |
| 138 | } |
| 139 | |
| 140 | *_aidl_return = (bool) header->IsUpToDate(android_crc_.value()); |
| 141 | } |
Mårten Kongstad | d10d06d | 2019-01-07 17:26:25 -0800 | [diff] [blame] | 142 | |
| 143 | // TODO(b/119328308): Check that the set of fulfilled policies of the overlay has not changed |
Mårten Kongstad | ef0695d | 2018-12-04 14:36:48 +0100 | [diff] [blame] | 144 | return ok(); |
| 145 | } |
| 146 | |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 147 | Status Idmap2Service::createIdmap(const std::string& target_apk_path, |
Mårten Kongstad | d10d06d | 2019-01-07 17:26:25 -0800 | [diff] [blame] | 148 | const std::string& overlay_apk_path, int32_t fulfilled_policies, |
Mårten Kongstad | 4cbb007 | 2018-11-30 16:22:05 +0100 | [diff] [blame] | 149 | bool enforce_overlayable, int32_t user_id ATTRIBUTE_UNUSED, |
Jooyung Han | 66c567a | 2020-03-07 21:47:09 +0900 | [diff] [blame] | 150 | aidl::nullable<std::string>* _aidl_return) { |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 151 | assert(_aidl_return); |
Mårten Kongstad | 4cbb007 | 2018-11-30 16:22:05 +0100 | [diff] [blame] | 152 | SYSTRACE << "Idmap2Service::createIdmap " << target_apk_path << " " << overlay_apk_path; |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 153 | _aidl_return->reset(nullptr); |
| 154 | |
Mårten Kongstad | d10d06d | 2019-01-07 17:26:25 -0800 | [diff] [blame] | 155 | const PolicyBitmask policy_bitmask = ConvertAidlArgToPolicyBitmask(fulfilled_policies); |
| 156 | |
Mårten Kongstad | 1da49dc | 2019-01-14 10:03:53 +0100 | [diff] [blame] | 157 | const std::string idmap_path = Idmap::CanonicalIdmapPathFor(kIdmapCacheDir, overlay_apk_path); |
| 158 | const uid_t uid = IPCThreadState::self()->getCallingUid(); |
| 159 | if (!UidHasWriteAccessToPath(uid, idmap_path)) { |
| 160 | return error(base::StringPrintf("will not write to %s: calling uid %d lacks write accesss", |
| 161 | idmap_path.c_str(), uid)); |
| 162 | } |
| 163 | |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 164 | const std::unique_ptr<const ApkAssets> target_apk = ApkAssets::Load(target_apk_path); |
| 165 | if (!target_apk) { |
| 166 | return error("failed to load apk " + target_apk_path); |
| 167 | } |
| 168 | |
| 169 | const std::unique_ptr<const ApkAssets> overlay_apk = ApkAssets::Load(overlay_apk_path); |
| 170 | if (!overlay_apk) { |
| 171 | return error("failed to load apk " + overlay_apk_path); |
| 172 | } |
| 173 | |
Ryan Mitchell | 9e4f52b | 2019-09-19 12:15:52 -0700 | [diff] [blame] | 174 | const auto idmap = |
| 175 | Idmap::FromApkAssets(*target_apk, *overlay_apk, policy_bitmask, enforce_overlayable); |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 176 | if (!idmap) { |
Mårten Kongstad | ce42490 | 2019-03-01 08:35:37 +0100 | [diff] [blame] | 177 | return error(idmap.GetErrorMessage()); |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 178 | } |
| 179 | |
Ryan Mitchell | a909305 | 2020-03-26 17:15:01 -0700 | [diff] [blame] | 180 | // idmap files are mapped with mmap in libandroidfw. Deleting and recreating the idmap guarantees |
| 181 | // that existing memory maps will continue to be valid and unaffected. |
| 182 | unlink(idmap_path.c_str()); |
| 183 | |
Mårten Kongstad | b877902 | 2018-11-29 09:53:17 +0100 | [diff] [blame] | 184 | umask(kIdmapFilePermissionMask); |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 185 | std::ofstream fout(idmap_path); |
| 186 | if (fout.fail()) { |
| 187 | return error("failed to open idmap path " + idmap_path); |
| 188 | } |
Ryan Mitchell | a909305 | 2020-03-26 17:15:01 -0700 | [diff] [blame] | 189 | |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 190 | BinaryStreamVisitor visitor(fout); |
Mårten Kongstad | ce42490 | 2019-03-01 08:35:37 +0100 | [diff] [blame] | 191 | (*idmap)->accept(&visitor); |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 192 | fout.close(); |
| 193 | if (fout.fail()) { |
Ryan Mitchell | a909305 | 2020-03-26 17:15:01 -0700 | [diff] [blame] | 194 | unlink(idmap_path.c_str()); |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 195 | return error("failed to write to idmap path " + idmap_path); |
| 196 | } |
| 197 | |
Jooyung Han | 66c567a | 2020-03-07 21:47:09 +0900 | [diff] [blame] | 198 | *_aidl_return = aidl::make_nullable<std::string>(idmap_path); |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 199 | return ok(); |
| 200 | } |
| 201 | |
Mårten Kongstad | 0eba72a | 2018-11-29 08:23:14 +0100 | [diff] [blame] | 202 | } // namespace android::os |