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" |
Ryan Mitchell | a707013 | 2020-05-13 14:17:52 -0700 | [diff] [blame] | 36 | #include "idmap2/Result.h" |
Mårten Kongstad | 4cbb007 | 2018-11-30 16:22:05 +0100 | [diff] [blame] | 37 | #include "idmap2/SysTrace.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; |
Ryan Mitchell | a707013 | 2020-05-13 14:17:52 -0700 | [diff] [blame] | 41 | using android::base::StringPrintf; |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 42 | using android::binder::Status; |
| 43 | using android::idmap2::BinaryStreamVisitor; |
| 44 | using android::idmap2::Idmap; |
| 45 | using android::idmap2::IdmapHeader; |
Ryan Mitchell | 2ed8bfa | 2021-01-08 13:34:28 -0800 | [diff] [blame^] | 46 | using android::idmap2::OverlayResourceContainer; |
| 47 | using android::idmap2::TargetResourceContainer; |
Mårten Kongstad | 1da49dc | 2019-01-14 10:03:53 +0100 | [diff] [blame] | 48 | using android::idmap2::utils::kIdmapCacheDir; |
Mårten Kongstad | b877902 | 2018-11-29 09:53:17 +0100 | [diff] [blame] | 49 | using android::idmap2::utils::kIdmapFilePermissionMask; |
Mårten Kongstad | 1da49dc | 2019-01-14 10:03:53 +0100 | [diff] [blame] | 50 | using android::idmap2::utils::UidHasWriteAccessToPath; |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 51 | |
Winson | 62ac8b5 | 2019-12-04 08:36:48 -0800 | [diff] [blame] | 52 | using PolicyBitmask = android::ResTable_overlayable_policy_header::PolicyBitmask; |
| 53 | |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 54 | namespace { |
| 55 | |
Ryan Mitchell | 7d53f19 | 2020-04-24 17:45:25 -0700 | [diff] [blame] | 56 | constexpr const char* kFrameworkPath = "/system/framework/framework-res.apk"; |
| 57 | |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 58 | Status ok() { |
| 59 | return Status::ok(); |
| 60 | } |
| 61 | |
| 62 | Status error(const std::string& msg) { |
| 63 | LOG(ERROR) << msg; |
| 64 | return Status::fromExceptionCode(Status::EX_NONE, msg.c_str()); |
| 65 | } |
| 66 | |
Mårten Kongstad | d10d06d | 2019-01-07 17:26:25 -0800 | [diff] [blame] | 67 | PolicyBitmask ConvertAidlArgToPolicyBitmask(int32_t arg) { |
| 68 | return static_cast<PolicyBitmask>(arg); |
| 69 | } |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 70 | } // namespace |
| 71 | |
Mårten Kongstad | 0eba72a | 2018-11-29 08:23:14 +0100 | [diff] [blame] | 72 | namespace android::os { |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 73 | |
Ryan Mitchell | 2ed8bfa | 2021-01-08 13:34:28 -0800 | [diff] [blame^] | 74 | Status Idmap2Service::getIdmapPath(const std::string& overlay_path, |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 75 | int32_t user_id ATTRIBUTE_UNUSED, std::string* _aidl_return) { |
| 76 | assert(_aidl_return); |
Ryan Mitchell | 2ed8bfa | 2021-01-08 13:34:28 -0800 | [diff] [blame^] | 77 | SYSTRACE << "Idmap2Service::getIdmapPath " << overlay_path; |
| 78 | *_aidl_return = Idmap::CanonicalIdmapPathFor(kIdmapCacheDir, overlay_path); |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 79 | return ok(); |
| 80 | } |
| 81 | |
Ryan Mitchell | 2ed8bfa | 2021-01-08 13:34:28 -0800 | [diff] [blame^] | 82 | Status Idmap2Service::removeIdmap(const std::string& overlay_path, int32_t user_id ATTRIBUTE_UNUSED, |
| 83 | bool* _aidl_return) { |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 84 | assert(_aidl_return); |
Ryan Mitchell | 2ed8bfa | 2021-01-08 13:34:28 -0800 | [diff] [blame^] | 85 | SYSTRACE << "Idmap2Service::removeIdmap " << overlay_path; |
Mårten Kongstad | 1da49dc | 2019-01-14 10:03:53 +0100 | [diff] [blame] | 86 | const uid_t uid = IPCThreadState::self()->getCallingUid(); |
Ryan Mitchell | 2ed8bfa | 2021-01-08 13:34:28 -0800 | [diff] [blame^] | 87 | const std::string idmap_path = Idmap::CanonicalIdmapPathFor(kIdmapCacheDir, overlay_path); |
Mårten Kongstad | 1da49dc | 2019-01-14 10:03:53 +0100 | [diff] [blame] | 88 | if (!UidHasWriteAccessToPath(uid, idmap_path)) { |
| 89 | *_aidl_return = false; |
| 90 | return error(base::StringPrintf("failed to unlink %s: calling uid %d lacks write access", |
| 91 | idmap_path.c_str(), uid)); |
| 92 | } |
Mårten Kongstad | b877902 | 2018-11-29 09:53:17 +0100 | [diff] [blame] | 93 | if (unlink(idmap_path.c_str()) != 0) { |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 94 | *_aidl_return = false; |
| 95 | return error("failed to unlink " + idmap_path + ": " + strerror(errno)); |
| 96 | } |
Mårten Kongstad | b877902 | 2018-11-29 09:53:17 +0100 | [diff] [blame] | 97 | *_aidl_return = true; |
| 98 | return ok(); |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 99 | } |
| 100 | |
Ryan Mitchell | 2ed8bfa | 2021-01-08 13:34:28 -0800 | [diff] [blame^] | 101 | Status Idmap2Service::verifyIdmap(const std::string& target_path, const std::string& overlay_path, |
| 102 | int32_t fulfilled_policies, bool enforce_overlayable, |
| 103 | int32_t user_id ATTRIBUTE_UNUSED, bool* _aidl_return) { |
| 104 | SYSTRACE << "Idmap2Service::verifyIdmap " << overlay_path; |
Mårten Kongstad | ef0695d | 2018-12-04 14:36:48 +0100 | [diff] [blame] | 105 | assert(_aidl_return); |
Ryan Mitchell | 038a284 | 2020-06-08 14:41:07 -0700 | [diff] [blame] | 106 | |
Ryan Mitchell | 2ed8bfa | 2021-01-08 13:34:28 -0800 | [diff] [blame^] | 107 | const std::string idmap_path = Idmap::CanonicalIdmapPathFor(kIdmapCacheDir, overlay_path); |
Mårten Kongstad | ef0695d | 2018-12-04 14:36:48 +0100 | [diff] [blame] | 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; |
Ryan Mitchell | 2ed8bfa | 2021-01-08 13:34:28 -0800 | [diff] [blame^] | 113 | LOG(WARNING) << "failed to parse idmap header of '" << idmap_path << "'"; |
| 114 | return ok(); |
hg.choi | a3a6813 | 2020-01-15 17:12:48 +0900 | [diff] [blame] | 115 | } |
| 116 | |
Ryan Mitchell | 2ed8bfa | 2021-01-08 13:34:28 -0800 | [diff] [blame^] | 117 | const auto target = GetTargetContainer(target_path); |
| 118 | if (!target) { |
Ryan Mitchell | a707013 | 2020-05-13 14:17:52 -0700 | [diff] [blame] | 119 | *_aidl_return = false; |
Ryan Mitchell | 2ed8bfa | 2021-01-08 13:34:28 -0800 | [diff] [blame^] | 120 | LOG(WARNING) << "failed to load target '" << target_path << "'"; |
| 121 | return ok(); |
| 122 | } |
| 123 | |
| 124 | const auto overlay = OverlayResourceContainer::FromPath(overlay_path); |
| 125 | if (!overlay) { |
| 126 | *_aidl_return = false; |
| 127 | LOG(WARNING) << "failed to load overlay '" << overlay_path << "'"; |
| 128 | return ok(); |
Ryan Mitchell | a707013 | 2020-05-13 14:17:52 -0700 | [diff] [blame] | 129 | } |
| 130 | |
Ryan Mitchell | 30dc2e0 | 2020-12-02 11:43:18 -0800 | [diff] [blame] | 131 | // TODO(162841629): Support passing overlay name to idmap2d verify |
Ryan Mitchell | a707013 | 2020-05-13 14:17:52 -0700 | [diff] [blame] | 132 | auto up_to_date = |
Ryan Mitchell | 2ed8bfa | 2021-01-08 13:34:28 -0800 | [diff] [blame^] | 133 | header->IsUpToDate(*GetPointer(*target), **overlay, "", |
Ryan Mitchell | 038a284 | 2020-06-08 14:41:07 -0700 | [diff] [blame] | 134 | ConvertAidlArgToPolicyBitmask(fulfilled_policies), enforce_overlayable); |
Ryan Mitchell | a707013 | 2020-05-13 14:17:52 -0700 | [diff] [blame] | 135 | |
Ryan Mitchell | 038a284 | 2020-06-08 14:41:07 -0700 | [diff] [blame] | 136 | *_aidl_return = static_cast<bool>(up_to_date); |
Ryan Mitchell | 2ed8bfa | 2021-01-08 13:34:28 -0800 | [diff] [blame^] | 137 | if (!up_to_date) { |
| 138 | LOG(WARNING) << "idmap '" << idmap_path |
| 139 | << "' not up to date : " << up_to_date.GetErrorMessage(); |
| 140 | } |
| 141 | return ok(); |
Mårten Kongstad | ef0695d | 2018-12-04 14:36:48 +0100 | [diff] [blame] | 142 | } |
| 143 | |
Ryan Mitchell | 2ed8bfa | 2021-01-08 13:34:28 -0800 | [diff] [blame^] | 144 | Status Idmap2Service::createIdmap(const std::string& target_path, const std::string& overlay_path, |
| 145 | int32_t fulfilled_policies, bool enforce_overlayable, |
| 146 | int32_t user_id ATTRIBUTE_UNUSED, |
Jooyung Han | 16bac85 | 2020-08-10 12:53:14 +0900 | [diff] [blame] | 147 | std::optional<std::string>* _aidl_return) { |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 148 | assert(_aidl_return); |
Ryan Mitchell | 2ed8bfa | 2021-01-08 13:34:28 -0800 | [diff] [blame^] | 149 | SYSTRACE << "Idmap2Service::createIdmap " << target_path << " " << overlay_path; |
Jooyung Han | 605e39f | 2020-01-23 13:29:22 +0900 | [diff] [blame] | 150 | _aidl_return->reset(); |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 151 | |
Mårten Kongstad | d10d06d | 2019-01-07 17:26:25 -0800 | [diff] [blame] | 152 | const PolicyBitmask policy_bitmask = ConvertAidlArgToPolicyBitmask(fulfilled_policies); |
| 153 | |
Ryan Mitchell | 2ed8bfa | 2021-01-08 13:34:28 -0800 | [diff] [blame^] | 154 | const std::string idmap_path = Idmap::CanonicalIdmapPathFor(kIdmapCacheDir, overlay_path); |
Mårten Kongstad | 1da49dc | 2019-01-14 10:03:53 +0100 | [diff] [blame] | 155 | const uid_t uid = IPCThreadState::self()->getCallingUid(); |
| 156 | if (!UidHasWriteAccessToPath(uid, idmap_path)) { |
| 157 | return error(base::StringPrintf("will not write to %s: calling uid %d lacks write accesss", |
| 158 | idmap_path.c_str(), uid)); |
| 159 | } |
| 160 | |
Ryan Mitchell | 2ed8bfa | 2021-01-08 13:34:28 -0800 | [diff] [blame^] | 161 | const auto target = GetTargetContainer(target_path); |
| 162 | if (!target) { |
| 163 | return error("failed to load target '%s'" + target_path); |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 164 | } |
| 165 | |
Ryan Mitchell | 2ed8bfa | 2021-01-08 13:34:28 -0800 | [diff] [blame^] | 166 | const auto overlay = OverlayResourceContainer::FromPath(overlay_path); |
| 167 | if (!overlay) { |
| 168 | return error("failed to load apk overlay '%s'" + overlay_path); |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 169 | } |
| 170 | |
Ryan Mitchell | 30dc2e0 | 2020-12-02 11:43:18 -0800 | [diff] [blame] | 171 | // TODO(162841629): Support passing overlay name to idmap2d create |
Ryan Mitchell | 2ed8bfa | 2021-01-08 13:34:28 -0800 | [diff] [blame^] | 172 | const auto idmap = Idmap::FromContainers(*GetPointer(*target), **overlay, "", policy_bitmask, |
| 173 | enforce_overlayable); |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 174 | if (!idmap) { |
Mårten Kongstad | ce42490 | 2019-03-01 08:35:37 +0100 | [diff] [blame] | 175 | return error(idmap.GetErrorMessage()); |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 176 | } |
| 177 | |
Ryan Mitchell | a909305 | 2020-03-26 17:15:01 -0700 | [diff] [blame] | 178 | // idmap files are mapped with mmap in libandroidfw. Deleting and recreating the idmap guarantees |
| 179 | // that existing memory maps will continue to be valid and unaffected. |
| 180 | unlink(idmap_path.c_str()); |
| 181 | |
Mårten Kongstad | b877902 | 2018-11-29 09:53:17 +0100 | [diff] [blame] | 182 | umask(kIdmapFilePermissionMask); |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 183 | std::ofstream fout(idmap_path); |
| 184 | if (fout.fail()) { |
| 185 | return error("failed to open idmap path " + idmap_path); |
| 186 | } |
Ryan Mitchell | a909305 | 2020-03-26 17:15:01 -0700 | [diff] [blame] | 187 | |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 188 | BinaryStreamVisitor visitor(fout); |
Mårten Kongstad | ce42490 | 2019-03-01 08:35:37 +0100 | [diff] [blame] | 189 | (*idmap)->accept(&visitor); |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 190 | fout.close(); |
| 191 | if (fout.fail()) { |
Ryan Mitchell | a909305 | 2020-03-26 17:15:01 -0700 | [diff] [blame] | 192 | unlink(idmap_path.c_str()); |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 193 | return error("failed to write to idmap path " + idmap_path); |
| 194 | } |
| 195 | |
Jooyung Han | 16bac85 | 2020-08-10 12:53:14 +0900 | [diff] [blame] | 196 | *_aidl_return = idmap_path; |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 197 | return ok(); |
| 198 | } |
| 199 | |
Ryan Mitchell | 2ed8bfa | 2021-01-08 13:34:28 -0800 | [diff] [blame^] | 200 | idmap2::Result<Idmap2Service::TargetResourceContainerPtr> Idmap2Service::GetTargetContainer( |
| 201 | const std::string& target_path) { |
| 202 | if (target_path == kFrameworkPath) { |
| 203 | if (framework_apk_cache_ == nullptr) { |
| 204 | // Initialize the framework APK cache. |
| 205 | auto target = TargetResourceContainer::FromPath(target_path); |
| 206 | if (!target) { |
| 207 | return target.GetError(); |
| 208 | } |
| 209 | framework_apk_cache_ = std::move(*target); |
| 210 | } |
| 211 | return {framework_apk_cache_.get()}; |
| 212 | } |
| 213 | |
| 214 | auto target = TargetResourceContainer::FromPath(target_path); |
| 215 | if (!target) { |
| 216 | return target.GetError(); |
| 217 | } |
| 218 | return {std::move(*target)}; |
| 219 | } |
| 220 | |
Mårten Kongstad | 0eba72a | 2018-11-29 08:23:14 +0100 | [diff] [blame] | 221 | } // namespace android::os |