blob: 15e22a3410cfa825fb30af84f7a8947d42580b70 [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 Mitchell7d53f192020-04-24 17:45:25 -070038#include "idmap2/ZipFile.h"
Ryan Mitchell52e1f7a2019-04-12 12:31:42 -070039#include "utils/String8.h"
Mårten Kongstad02751232018-04-27 13:16:32 +020040
Mårten Kongstad1da49dc2019-01-14 10:03:53 +010041using android::IPCThreadState;
Ryan Mitchella7070132020-05-13 14:17:52 -070042using android::base::StringPrintf;
Mårten Kongstad02751232018-04-27 13:16:32 +020043using android::binder::Status;
44using android::idmap2::BinaryStreamVisitor;
Ryan Mitchell7d53f192020-04-24 17:45:25 -070045using android::idmap2::GetPackageCrc;
Mårten Kongstad02751232018-04-27 13:16:32 +020046using android::idmap2::Idmap;
47using android::idmap2::IdmapHeader;
Ryan Mitchella7070132020-05-13 14:17:52 -070048using android::idmap2::ZipFile;
Mårten Kongstad1da49dc2019-01-14 10:03:53 +010049using android::idmap2::utils::kIdmapCacheDir;
Mårten Kongstadb8779022018-11-29 09:53:17 +010050using android::idmap2::utils::kIdmapFilePermissionMask;
Mårten Kongstad1da49dc2019-01-14 10:03:53 +010051using android::idmap2::utils::UidHasWriteAccessToPath;
Mårten Kongstad02751232018-04-27 13:16:32 +020052
Winson62ac8b52019-12-04 08:36:48 -080053using PolicyBitmask = android::ResTable_overlayable_policy_header::PolicyBitmask;
54
Mårten Kongstad02751232018-04-27 13:16:32 +020055namespace {
56
Ryan Mitchell7d53f192020-04-24 17:45:25 -070057constexpr const char* kFrameworkPath = "/system/framework/framework-res.apk";
58
Mårten Kongstad02751232018-04-27 13:16:32 +020059Status ok() {
60 return Status::ok();
61}
62
63Status error(const std::string& msg) {
64 LOG(ERROR) << msg;
65 return Status::fromExceptionCode(Status::EX_NONE, msg.c_str());
66}
67
Mårten Kongstadd10d06d2019-01-07 17:26:25 -080068PolicyBitmask ConvertAidlArgToPolicyBitmask(int32_t arg) {
69 return static_cast<PolicyBitmask>(arg);
70}
71
Ryan Mitchella7070132020-05-13 14:17:52 -070072Status GetCrc(const std::string& apk_path, uint32_t* out_crc) {
Ryan Mitchell038a2842020-06-08 14:41:07 -070073 const auto zip = ZipFile::Open(apk_path);
74 if (!zip) {
Ryan Mitchella7070132020-05-13 14:17:52 -070075 return error(StringPrintf("failed to open apk %s", apk_path.c_str()));
76 }
77
Ryan Mitchell038a2842020-06-08 14:41:07 -070078 const auto crc = GetPackageCrc(*zip);
Ryan Mitchella7070132020-05-13 14:17:52 -070079 if (!crc) {
80 return error(crc.GetErrorMessage());
81 }
82
83 *out_crc = *crc;
84 return ok();
85}
86
Mårten Kongstad02751232018-04-27 13:16:32 +020087} // namespace
88
Mårten Kongstad0eba72a2018-11-29 08:23:14 +010089namespace android::os {
Mårten Kongstad02751232018-04-27 13:16:32 +020090
91Status Idmap2Service::getIdmapPath(const std::string& overlay_apk_path,
92 int32_t user_id ATTRIBUTE_UNUSED, std::string* _aidl_return) {
93 assert(_aidl_return);
Mårten Kongstad4cbb0072018-11-30 16:22:05 +010094 SYSTRACE << "Idmap2Service::getIdmapPath " << overlay_apk_path;
Mårten Kongstad02751232018-04-27 13:16:32 +020095 *_aidl_return = Idmap::CanonicalIdmapPathFor(kIdmapCacheDir, overlay_apk_path);
96 return ok();
97}
98
99Status Idmap2Service::removeIdmap(const std::string& overlay_apk_path,
100 int32_t user_id ATTRIBUTE_UNUSED, bool* _aidl_return) {
101 assert(_aidl_return);
Mårten Kongstad4cbb0072018-11-30 16:22:05 +0100102 SYSTRACE << "Idmap2Service::removeIdmap " << overlay_apk_path;
Mårten Kongstad1da49dc2019-01-14 10:03:53 +0100103 const uid_t uid = IPCThreadState::self()->getCallingUid();
Mårten Kongstad02751232018-04-27 13:16:32 +0200104 const std::string idmap_path = Idmap::CanonicalIdmapPathFor(kIdmapCacheDir, overlay_apk_path);
Mårten Kongstad1da49dc2019-01-14 10:03:53 +0100105 if (!UidHasWriteAccessToPath(uid, idmap_path)) {
106 *_aidl_return = false;
107 return error(base::StringPrintf("failed to unlink %s: calling uid %d lacks write access",
108 idmap_path.c_str(), uid));
109 }
Mårten Kongstadb8779022018-11-29 09:53:17 +0100110 if (unlink(idmap_path.c_str()) != 0) {
Mårten Kongstad02751232018-04-27 13:16:32 +0200111 *_aidl_return = false;
112 return error("failed to unlink " + idmap_path + ": " + strerror(errno));
113 }
Mårten Kongstadb8779022018-11-29 09:53:17 +0100114 *_aidl_return = true;
115 return ok();
Mårten Kongstad02751232018-04-27 13:16:32 +0200116}
117
hg.choia3a68132020-01-15 17:12:48 +0900118Status Idmap2Service::verifyIdmap(const std::string& target_apk_path,
Ryan Mitchella7070132020-05-13 14:17:52 -0700119 const std::string& overlay_apk_path, int32_t fulfilled_policies,
120 bool enforce_overlayable, int32_t user_id ATTRIBUTE_UNUSED,
121 bool* _aidl_return) {
Mårten Kongstad4cbb0072018-11-30 16:22:05 +0100122 SYSTRACE << "Idmap2Service::verifyIdmap " << overlay_apk_path;
Mårten Kongstadef0695d2018-12-04 14:36:48 +0100123 assert(_aidl_return);
Ryan Mitchell038a2842020-06-08 14:41:07 -0700124
Mårten Kongstadef0695d2018-12-04 14:36:48 +0100125 const std::string idmap_path = Idmap::CanonicalIdmapPathFor(kIdmapCacheDir, overlay_apk_path);
126 std::ifstream fin(idmap_path);
127 const std::unique_ptr<const IdmapHeader> header = IdmapHeader::FromBinaryStream(fin);
128 fin.close();
hg.choia3a68132020-01-15 17:12:48 +0900129 if (!header) {
130 *_aidl_return = false;
131 return error("failed to parse idmap header");
132 }
Mårten Kongstadd10d06d2019-01-07 17:26:25 -0800133
Ryan Mitchella7070132020-05-13 14:17:52 -0700134 uint32_t target_crc;
135 if (target_apk_path == kFrameworkPath && android_crc_) {
136 target_crc = *android_crc_;
Ryan Mitchell7d53f192020-04-24 17:45:25 -0700137 } else {
Ryan Mitchella7070132020-05-13 14:17:52 -0700138 auto target_crc_status = GetCrc(target_apk_path, &target_crc);
139 if (!target_crc_status.isOk()) {
140 *_aidl_return = false;
141 return target_crc_status;
Ryan Mitchell7d53f192020-04-24 17:45:25 -0700142 }
Mårten Kongstadd10d06d2019-01-07 17:26:25 -0800143
Ryan Mitchella7070132020-05-13 14:17:52 -0700144 // Loading the framework zip can take several milliseconds. Cache the crc of the framework
145 // resource APK to reduce repeated work during boot.
146 if (target_apk_path == kFrameworkPath) {
147 android_crc_ = target_crc;
148 }
Ryan Mitchell7d53f192020-04-24 17:45:25 -0700149 }
Mårten Kongstadef0695d2018-12-04 14:36:48 +0100150
Ryan Mitchella7070132020-05-13 14:17:52 -0700151 uint32_t overlay_crc;
152 auto overlay_crc_status = GetCrc(overlay_apk_path, &overlay_crc);
153 if (!overlay_crc_status.isOk()) {
154 *_aidl_return = false;
155 return overlay_crc_status;
156 }
157
158 auto up_to_date =
159 header->IsUpToDate(target_apk_path.c_str(), overlay_apk_path.c_str(), target_crc, overlay_crc,
Ryan Mitchell038a2842020-06-08 14:41:07 -0700160 ConvertAidlArgToPolicyBitmask(fulfilled_policies), enforce_overlayable);
Ryan Mitchella7070132020-05-13 14:17:52 -0700161
Ryan Mitchell038a2842020-06-08 14:41:07 -0700162 *_aidl_return = static_cast<bool>(up_to_date);
163 return *_aidl_return ? ok() : error(up_to_date.GetErrorMessage());
Mårten Kongstadef0695d2018-12-04 14:36:48 +0100164}
165
Mårten Kongstad02751232018-04-27 13:16:32 +0200166Status Idmap2Service::createIdmap(const std::string& target_apk_path,
Mårten Kongstadd10d06d2019-01-07 17:26:25 -0800167 const std::string& overlay_apk_path, int32_t fulfilled_policies,
Mårten Kongstad4cbb0072018-11-30 16:22:05 +0100168 bool enforce_overlayable, int32_t user_id ATTRIBUTE_UNUSED,
Jooyung Hanf4529682020-01-23 13:29:22 +0900169 std::optional<std::string>* _aidl_return) {
Mårten Kongstad02751232018-04-27 13:16:32 +0200170 assert(_aidl_return);
Mårten Kongstad4cbb0072018-11-30 16:22:05 +0100171 SYSTRACE << "Idmap2Service::createIdmap " << target_apk_path << " " << overlay_apk_path;
Jooyung Hanf4529682020-01-23 13:29:22 +0900172 _aidl_return->reset();
Mårten Kongstad02751232018-04-27 13:16:32 +0200173
Mårten Kongstadd10d06d2019-01-07 17:26:25 -0800174 const PolicyBitmask policy_bitmask = ConvertAidlArgToPolicyBitmask(fulfilled_policies);
175
Mårten Kongstad1da49dc2019-01-14 10:03:53 +0100176 const std::string idmap_path = Idmap::CanonicalIdmapPathFor(kIdmapCacheDir, overlay_apk_path);
177 const uid_t uid = IPCThreadState::self()->getCallingUid();
178 if (!UidHasWriteAccessToPath(uid, idmap_path)) {
179 return error(base::StringPrintf("will not write to %s: calling uid %d lacks write accesss",
180 idmap_path.c_str(), uid));
181 }
182
Mårten Kongstad02751232018-04-27 13:16:32 +0200183 const std::unique_ptr<const ApkAssets> target_apk = ApkAssets::Load(target_apk_path);
184 if (!target_apk) {
185 return error("failed to load apk " + target_apk_path);
186 }
187
188 const std::unique_ptr<const ApkAssets> overlay_apk = ApkAssets::Load(overlay_apk_path);
189 if (!overlay_apk) {
190 return error("failed to load apk " + overlay_apk_path);
191 }
192
Ryan Mitchell9e4f52b2019-09-19 12:15:52 -0700193 const auto idmap =
194 Idmap::FromApkAssets(*target_apk, *overlay_apk, policy_bitmask, enforce_overlayable);
Mårten Kongstad02751232018-04-27 13:16:32 +0200195 if (!idmap) {
Mårten Kongstadce424902019-03-01 08:35:37 +0100196 return error(idmap.GetErrorMessage());
Mårten Kongstad02751232018-04-27 13:16:32 +0200197 }
198
Ryan Mitchella9093052020-03-26 17:15:01 -0700199 // idmap files are mapped with mmap in libandroidfw. Deleting and recreating the idmap guarantees
200 // that existing memory maps will continue to be valid and unaffected.
201 unlink(idmap_path.c_str());
202
Mårten Kongstadb8779022018-11-29 09:53:17 +0100203 umask(kIdmapFilePermissionMask);
Mårten Kongstad02751232018-04-27 13:16:32 +0200204 std::ofstream fout(idmap_path);
205 if (fout.fail()) {
206 return error("failed to open idmap path " + idmap_path);
207 }
Ryan Mitchella9093052020-03-26 17:15:01 -0700208
Mårten Kongstad02751232018-04-27 13:16:32 +0200209 BinaryStreamVisitor visitor(fout);
Mårten Kongstadce424902019-03-01 08:35:37 +0100210 (*idmap)->accept(&visitor);
Mårten Kongstad02751232018-04-27 13:16:32 +0200211 fout.close();
212 if (fout.fail()) {
Ryan Mitchella9093052020-03-26 17:15:01 -0700213 unlink(idmap_path.c_str());
Mårten Kongstad02751232018-04-27 13:16:32 +0200214 return error("failed to write to idmap path " + idmap_path);
215 }
216
Jooyung Hanf4529682020-01-23 13:29:22 +0900217 *_aidl_return = idmap_path;
Mårten Kongstad02751232018-04-27 13:16:32 +0200218 return ok();
219}
220
Mårten Kongstad0eba72a2018-11-29 08:23:14 +0100221} // namespace android::os