blob: f62630e702d41188ac3f56c80eb824c7171b1096 [file] [log] [blame]
Mårten Kongstad02751232018-04-27 13:16:32 +02001/*
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 Mitchell52e1f7a2019-04-12 12:31:42 -070017#include "idmap2d/Idmap2Service.h"
18
Mårten Kongstad02751232018-04-27 13:16:32 +020019#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 Kongstadd10d06d2019-01-07 17:26:25 -080031#include "android-base/stringprintf.h"
Mårten Kongstad1da49dc2019-01-14 10:03:53 +010032#include "binder/IPCThreadState.h"
Mårten Kongstad02751232018-04-27 13:16:32 +020033#include "idmap2/BinaryStreamVisitor.h"
34#include "idmap2/FileUtils.h"
35#include "idmap2/Idmap.h"
Ryan Mitchella7070132020-05-13 14:17:52 -070036#include "idmap2/Result.h"
Mårten Kongstad4cbb0072018-11-30 16:22:05 +010037#include "idmap2/SysTrace.h"
Ryan Mitchell52e1f7a2019-04-12 12:31:42 -070038#include "utils/String8.h"
Mårten Kongstad02751232018-04-27 13:16:32 +020039
Mårten Kongstad1da49dc2019-01-14 10:03:53 +010040using android::IPCThreadState;
Ryan Mitchella7070132020-05-13 14:17:52 -070041using android::base::StringPrintf;
Mårten Kongstad02751232018-04-27 13:16:32 +020042using android::binder::Status;
43using android::idmap2::BinaryStreamVisitor;
44using android::idmap2::Idmap;
45using android::idmap2::IdmapHeader;
Ryan Mitchell2ed8bfa2021-01-08 13:34:28 -080046using android::idmap2::OverlayResourceContainer;
47using android::idmap2::TargetResourceContainer;
Mårten Kongstad1da49dc2019-01-14 10:03:53 +010048using android::idmap2::utils::kIdmapCacheDir;
Mårten Kongstadb8779022018-11-29 09:53:17 +010049using android::idmap2::utils::kIdmapFilePermissionMask;
Mårten Kongstad1da49dc2019-01-14 10:03:53 +010050using android::idmap2::utils::UidHasWriteAccessToPath;
Mårten Kongstad02751232018-04-27 13:16:32 +020051
Winson62ac8b52019-12-04 08:36:48 -080052using PolicyBitmask = android::ResTable_overlayable_policy_header::PolicyBitmask;
53
Mårten Kongstad02751232018-04-27 13:16:32 +020054namespace {
55
Ryan Mitchell7d53f192020-04-24 17:45:25 -070056constexpr const char* kFrameworkPath = "/system/framework/framework-res.apk";
57
Mårten Kongstad02751232018-04-27 13:16:32 +020058Status ok() {
59 return Status::ok();
60}
61
62Status error(const std::string& msg) {
63 LOG(ERROR) << msg;
64 return Status::fromExceptionCode(Status::EX_NONE, msg.c_str());
65}
66
Mårten Kongstadd10d06d2019-01-07 17:26:25 -080067PolicyBitmask ConvertAidlArgToPolicyBitmask(int32_t arg) {
68 return static_cast<PolicyBitmask>(arg);
69}
Mårten Kongstad02751232018-04-27 13:16:32 +020070} // namespace
71
Mårten Kongstad0eba72a2018-11-29 08:23:14 +010072namespace android::os {
Mårten Kongstad02751232018-04-27 13:16:32 +020073
Ryan Mitchell2ed8bfa2021-01-08 13:34:28 -080074Status Idmap2Service::getIdmapPath(const std::string& overlay_path,
Mårten Kongstad02751232018-04-27 13:16:32 +020075 int32_t user_id ATTRIBUTE_UNUSED, std::string* _aidl_return) {
76 assert(_aidl_return);
Ryan Mitchell2ed8bfa2021-01-08 13:34:28 -080077 SYSTRACE << "Idmap2Service::getIdmapPath " << overlay_path;
78 *_aidl_return = Idmap::CanonicalIdmapPathFor(kIdmapCacheDir, overlay_path);
Mårten Kongstad02751232018-04-27 13:16:32 +020079 return ok();
80}
81
Ryan Mitchell2ed8bfa2021-01-08 13:34:28 -080082Status Idmap2Service::removeIdmap(const std::string& overlay_path, int32_t user_id ATTRIBUTE_UNUSED,
83 bool* _aidl_return) {
Mårten Kongstad02751232018-04-27 13:16:32 +020084 assert(_aidl_return);
Ryan Mitchell2ed8bfa2021-01-08 13:34:28 -080085 SYSTRACE << "Idmap2Service::removeIdmap " << overlay_path;
Mårten Kongstad1da49dc2019-01-14 10:03:53 +010086 const uid_t uid = IPCThreadState::self()->getCallingUid();
Ryan Mitchell2ed8bfa2021-01-08 13:34:28 -080087 const std::string idmap_path = Idmap::CanonicalIdmapPathFor(kIdmapCacheDir, overlay_path);
Mårten Kongstad1da49dc2019-01-14 10:03:53 +010088 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 Kongstadb8779022018-11-29 09:53:17 +010093 if (unlink(idmap_path.c_str()) != 0) {
Mårten Kongstad02751232018-04-27 13:16:32 +020094 *_aidl_return = false;
95 return error("failed to unlink " + idmap_path + ": " + strerror(errno));
96 }
Mårten Kongstadb8779022018-11-29 09:53:17 +010097 *_aidl_return = true;
98 return ok();
Mårten Kongstad02751232018-04-27 13:16:32 +020099}
100
Ryan Mitchell2ed8bfa2021-01-08 13:34:28 -0800101Status 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 Kongstadef0695d2018-12-04 14:36:48 +0100105 assert(_aidl_return);
Ryan Mitchell038a2842020-06-08 14:41:07 -0700106
Ryan Mitchell2ed8bfa2021-01-08 13:34:28 -0800107 const std::string idmap_path = Idmap::CanonicalIdmapPathFor(kIdmapCacheDir, overlay_path);
Mårten Kongstadef0695d2018-12-04 14:36:48 +0100108 std::ifstream fin(idmap_path);
109 const std::unique_ptr<const IdmapHeader> header = IdmapHeader::FromBinaryStream(fin);
110 fin.close();
hg.choia3a68132020-01-15 17:12:48 +0900111 if (!header) {
112 *_aidl_return = false;
Ryan Mitchell2ed8bfa2021-01-08 13:34:28 -0800113 LOG(WARNING) << "failed to parse idmap header of '" << idmap_path << "'";
114 return ok();
hg.choia3a68132020-01-15 17:12:48 +0900115 }
116
Ryan Mitchell2ed8bfa2021-01-08 13:34:28 -0800117 const auto target = GetTargetContainer(target_path);
118 if (!target) {
Ryan Mitchella7070132020-05-13 14:17:52 -0700119 *_aidl_return = false;
Ryan Mitchell2ed8bfa2021-01-08 13:34:28 -0800120 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 Mitchella7070132020-05-13 14:17:52 -0700129 }
130
Ryan Mitchell30dc2e02020-12-02 11:43:18 -0800131 // TODO(162841629): Support passing overlay name to idmap2d verify
Ryan Mitchella7070132020-05-13 14:17:52 -0700132 auto up_to_date =
Ryan Mitchell2ed8bfa2021-01-08 13:34:28 -0800133 header->IsUpToDate(*GetPointer(*target), **overlay, "",
Ryan Mitchell038a2842020-06-08 14:41:07 -0700134 ConvertAidlArgToPolicyBitmask(fulfilled_policies), enforce_overlayable);
Ryan Mitchella7070132020-05-13 14:17:52 -0700135
Ryan Mitchell038a2842020-06-08 14:41:07 -0700136 *_aidl_return = static_cast<bool>(up_to_date);
Ryan Mitchell2ed8bfa2021-01-08 13:34:28 -0800137 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 Kongstadef0695d2018-12-04 14:36:48 +0100142}
143
Ryan Mitchell2ed8bfa2021-01-08 13:34:28 -0800144Status 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 Han16bac852020-08-10 12:53:14 +0900147 std::optional<std::string>* _aidl_return) {
Mårten Kongstad02751232018-04-27 13:16:32 +0200148 assert(_aidl_return);
Ryan Mitchell2ed8bfa2021-01-08 13:34:28 -0800149 SYSTRACE << "Idmap2Service::createIdmap " << target_path << " " << overlay_path;
Jooyung Han605e39f2020-01-23 13:29:22 +0900150 _aidl_return->reset();
Mårten Kongstad02751232018-04-27 13:16:32 +0200151
Mårten Kongstadd10d06d2019-01-07 17:26:25 -0800152 const PolicyBitmask policy_bitmask = ConvertAidlArgToPolicyBitmask(fulfilled_policies);
153
Ryan Mitchell2ed8bfa2021-01-08 13:34:28 -0800154 const std::string idmap_path = Idmap::CanonicalIdmapPathFor(kIdmapCacheDir, overlay_path);
Mårten Kongstad1da49dc2019-01-14 10:03:53 +0100155 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 Mitchell2ed8bfa2021-01-08 13:34:28 -0800161 const auto target = GetTargetContainer(target_path);
162 if (!target) {
163 return error("failed to load target '%s'" + target_path);
Mårten Kongstad02751232018-04-27 13:16:32 +0200164 }
165
Ryan Mitchell2ed8bfa2021-01-08 13:34:28 -0800166 const auto overlay = OverlayResourceContainer::FromPath(overlay_path);
167 if (!overlay) {
168 return error("failed to load apk overlay '%s'" + overlay_path);
Mårten Kongstad02751232018-04-27 13:16:32 +0200169 }
170
Ryan Mitchell30dc2e02020-12-02 11:43:18 -0800171 // TODO(162841629): Support passing overlay name to idmap2d create
Ryan Mitchell2ed8bfa2021-01-08 13:34:28 -0800172 const auto idmap = Idmap::FromContainers(*GetPointer(*target), **overlay, "", policy_bitmask,
173 enforce_overlayable);
Mårten Kongstad02751232018-04-27 13:16:32 +0200174 if (!idmap) {
Mårten Kongstadce424902019-03-01 08:35:37 +0100175 return error(idmap.GetErrorMessage());
Mårten Kongstad02751232018-04-27 13:16:32 +0200176 }
177
Ryan Mitchella9093052020-03-26 17:15:01 -0700178 // 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 Kongstadb8779022018-11-29 09:53:17 +0100182 umask(kIdmapFilePermissionMask);
Mårten Kongstad02751232018-04-27 13:16:32 +0200183 std::ofstream fout(idmap_path);
184 if (fout.fail()) {
185 return error("failed to open idmap path " + idmap_path);
186 }
Ryan Mitchella9093052020-03-26 17:15:01 -0700187
Mårten Kongstad02751232018-04-27 13:16:32 +0200188 BinaryStreamVisitor visitor(fout);
Mårten Kongstadce424902019-03-01 08:35:37 +0100189 (*idmap)->accept(&visitor);
Mårten Kongstad02751232018-04-27 13:16:32 +0200190 fout.close();
191 if (fout.fail()) {
Ryan Mitchella9093052020-03-26 17:15:01 -0700192 unlink(idmap_path.c_str());
Mårten Kongstad02751232018-04-27 13:16:32 +0200193 return error("failed to write to idmap path " + idmap_path);
194 }
195
Jooyung Han16bac852020-08-10 12:53:14 +0900196 *_aidl_return = idmap_path;
Mårten Kongstad02751232018-04-27 13:16:32 +0200197 return ok();
198}
199
Ryan Mitchell2ed8bfa2021-01-08 13:34:28 -0800200idmap2::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 Kongstad0eba72a2018-11-29 08:23:14 +0100221} // namespace android::os