blob: 75fc7f714ce325ef5122c197af78f677500ce05e [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"
Mårten Kongstad4cbb0072018-11-30 16:22:05 +010036#include "idmap2/SysTrace.h"
Ryan Mitchell52e1f7a2019-04-12 12:31:42 -070037#include "utils/String8.h"
Mårten Kongstad02751232018-04-27 13:16:32 +020038
Mårten Kongstad1da49dc2019-01-14 10:03:53 +010039using android::IPCThreadState;
Mårten Kongstad02751232018-04-27 13:16:32 +020040using android::binder::Status;
41using android::idmap2::BinaryStreamVisitor;
42using android::idmap2::Idmap;
43using android::idmap2::IdmapHeader;
Mårten Kongstad1da49dc2019-01-14 10:03:53 +010044using android::idmap2::utils::kIdmapCacheDir;
Mårten Kongstadb8779022018-11-29 09:53:17 +010045using android::idmap2::utils::kIdmapFilePermissionMask;
Mårten Kongstad1da49dc2019-01-14 10:03:53 +010046using android::idmap2::utils::UidHasWriteAccessToPath;
Mårten Kongstad02751232018-04-27 13:16:32 +020047
Winson62ac8b52019-12-04 08:36:48 -080048using PolicyBitmask = android::ResTable_overlayable_policy_header::PolicyBitmask;
49
Mårten Kongstad02751232018-04-27 13:16:32 +020050namespace {
51
Mårten Kongstad02751232018-04-27 13:16:32 +020052Status ok() {
53 return Status::ok();
54}
55
56Status error(const std::string& msg) {
57 LOG(ERROR) << msg;
58 return Status::fromExceptionCode(Status::EX_NONE, msg.c_str());
59}
60
Mårten Kongstadd10d06d2019-01-07 17:26:25 -080061PolicyBitmask ConvertAidlArgToPolicyBitmask(int32_t arg) {
62 return static_cast<PolicyBitmask>(arg);
63}
64
Mårten Kongstad02751232018-04-27 13:16:32 +020065} // namespace
66
Mårten Kongstad0eba72a2018-11-29 08:23:14 +010067namespace android::os {
Mårten Kongstad02751232018-04-27 13:16:32 +020068
69Status Idmap2Service::getIdmapPath(const std::string& overlay_apk_path,
70 int32_t user_id ATTRIBUTE_UNUSED, std::string* _aidl_return) {
71 assert(_aidl_return);
Mårten Kongstad4cbb0072018-11-30 16:22:05 +010072 SYSTRACE << "Idmap2Service::getIdmapPath " << overlay_apk_path;
Mårten Kongstad02751232018-04-27 13:16:32 +020073 *_aidl_return = Idmap::CanonicalIdmapPathFor(kIdmapCacheDir, overlay_apk_path);
74 return ok();
75}
76
77Status Idmap2Service::removeIdmap(const std::string& overlay_apk_path,
78 int32_t user_id ATTRIBUTE_UNUSED, bool* _aidl_return) {
79 assert(_aidl_return);
Mårten Kongstad4cbb0072018-11-30 16:22:05 +010080 SYSTRACE << "Idmap2Service::removeIdmap " << overlay_apk_path;
Mårten Kongstad1da49dc2019-01-14 10:03:53 +010081 const uid_t uid = IPCThreadState::self()->getCallingUid();
Mårten Kongstad02751232018-04-27 13:16:32 +020082 const std::string idmap_path = Idmap::CanonicalIdmapPathFor(kIdmapCacheDir, overlay_apk_path);
Mårten Kongstad1da49dc2019-01-14 10:03:53 +010083 if (!UidHasWriteAccessToPath(uid, idmap_path)) {
84 *_aidl_return = false;
85 return error(base::StringPrintf("failed to unlink %s: calling uid %d lacks write access",
86 idmap_path.c_str(), uid));
87 }
Mårten Kongstadb8779022018-11-29 09:53:17 +010088 if (unlink(idmap_path.c_str()) != 0) {
Mårten Kongstad02751232018-04-27 13:16:32 +020089 *_aidl_return = false;
90 return error("failed to unlink " + idmap_path + ": " + strerror(errno));
91 }
Mårten Kongstadb8779022018-11-29 09:53:17 +010092 *_aidl_return = true;
93 return ok();
Mårten Kongstad02751232018-04-27 13:16:32 +020094}
95
hg.choia3a68132020-01-15 17:12:48 +090096Status Idmap2Service::verifyIdmap(const std::string& target_apk_path,
97 const std::string& overlay_apk_path,
Mårten Kongstadd10d06d2019-01-07 17:26:25 -080098 int32_t fulfilled_policies ATTRIBUTE_UNUSED,
99 bool enforce_overlayable ATTRIBUTE_UNUSED,
Mårten Kongstadef0695d2018-12-04 14:36:48 +0100100 int32_t user_id ATTRIBUTE_UNUSED, bool* _aidl_return) {
Mårten Kongstad4cbb0072018-11-30 16:22:05 +0100101 SYSTRACE << "Idmap2Service::verifyIdmap " << overlay_apk_path;
Mårten Kongstadef0695d2018-12-04 14:36:48 +0100102 assert(_aidl_return);
103 const std::string idmap_path = Idmap::CanonicalIdmapPathFor(kIdmapCacheDir, overlay_apk_path);
104 std::ifstream fin(idmap_path);
105 const std::unique_ptr<const IdmapHeader> header = IdmapHeader::FromBinaryStream(fin);
106 fin.close();
hg.choia3a68132020-01-15 17:12:48 +0900107 if (!header) {
108 *_aidl_return = false;
109 return error("failed to parse idmap header");
110 }
111
112 *_aidl_return =
113 strcmp(header->GetTargetPath().data(), target_apk_path.data()) == 0 && header->IsUpToDate();
Mårten Kongstadd10d06d2019-01-07 17:26:25 -0800114
115 // TODO(b/119328308): Check that the set of fulfilled policies of the overlay has not changed
Mårten Kongstadef0695d2018-12-04 14:36:48 +0100116 return ok();
117}
118
Mårten Kongstad02751232018-04-27 13:16:32 +0200119Status Idmap2Service::createIdmap(const std::string& target_apk_path,
Mårten Kongstadd10d06d2019-01-07 17:26:25 -0800120 const std::string& overlay_apk_path, int32_t fulfilled_policies,
Mårten Kongstad4cbb0072018-11-30 16:22:05 +0100121 bool enforce_overlayable, int32_t user_id ATTRIBUTE_UNUSED,
Jooyung Han66c567a2020-03-07 21:47:09 +0900122 aidl::nullable<std::string>* _aidl_return) {
Mårten Kongstad02751232018-04-27 13:16:32 +0200123 assert(_aidl_return);
Mårten Kongstad4cbb0072018-11-30 16:22:05 +0100124 SYSTRACE << "Idmap2Service::createIdmap " << target_apk_path << " " << overlay_apk_path;
Mårten Kongstad02751232018-04-27 13:16:32 +0200125 _aidl_return->reset(nullptr);
126
Mårten Kongstadd10d06d2019-01-07 17:26:25 -0800127 const PolicyBitmask policy_bitmask = ConvertAidlArgToPolicyBitmask(fulfilled_policies);
128
Mårten Kongstad1da49dc2019-01-14 10:03:53 +0100129 const std::string idmap_path = Idmap::CanonicalIdmapPathFor(kIdmapCacheDir, overlay_apk_path);
130 const uid_t uid = IPCThreadState::self()->getCallingUid();
131 if (!UidHasWriteAccessToPath(uid, idmap_path)) {
132 return error(base::StringPrintf("will not write to %s: calling uid %d lacks write accesss",
133 idmap_path.c_str(), uid));
134 }
135
Mårten Kongstad02751232018-04-27 13:16:32 +0200136 const std::unique_ptr<const ApkAssets> target_apk = ApkAssets::Load(target_apk_path);
137 if (!target_apk) {
138 return error("failed to load apk " + target_apk_path);
139 }
140
141 const std::unique_ptr<const ApkAssets> overlay_apk = ApkAssets::Load(overlay_apk_path);
142 if (!overlay_apk) {
143 return error("failed to load apk " + overlay_apk_path);
144 }
145
Ryan Mitchell9e4f52b2019-09-19 12:15:52 -0700146 const auto idmap =
147 Idmap::FromApkAssets(*target_apk, *overlay_apk, policy_bitmask, enforce_overlayable);
Mårten Kongstad02751232018-04-27 13:16:32 +0200148 if (!idmap) {
Mårten Kongstadce424902019-03-01 08:35:37 +0100149 return error(idmap.GetErrorMessage());
Mårten Kongstad02751232018-04-27 13:16:32 +0200150 }
151
Ryan Mitchella9093052020-03-26 17:15:01 -0700152 // idmap files are mapped with mmap in libandroidfw. Deleting and recreating the idmap guarantees
153 // that existing memory maps will continue to be valid and unaffected.
154 unlink(idmap_path.c_str());
155
Mårten Kongstadb8779022018-11-29 09:53:17 +0100156 umask(kIdmapFilePermissionMask);
Mårten Kongstad02751232018-04-27 13:16:32 +0200157 std::ofstream fout(idmap_path);
158 if (fout.fail()) {
159 return error("failed to open idmap path " + idmap_path);
160 }
Ryan Mitchella9093052020-03-26 17:15:01 -0700161
Mårten Kongstad02751232018-04-27 13:16:32 +0200162 BinaryStreamVisitor visitor(fout);
Mårten Kongstadce424902019-03-01 08:35:37 +0100163 (*idmap)->accept(&visitor);
Mårten Kongstad02751232018-04-27 13:16:32 +0200164 fout.close();
165 if (fout.fail()) {
Ryan Mitchella9093052020-03-26 17:15:01 -0700166 unlink(idmap_path.c_str());
Mårten Kongstad02751232018-04-27 13:16:32 +0200167 return error("failed to write to idmap path " + idmap_path);
168 }
169
Jooyung Han66c567a2020-03-07 21:47:09 +0900170 *_aidl_return = aidl::make_nullable<std::string>(idmap_path);
Mårten Kongstad02751232018-04-27 13:16:32 +0200171 return ok();
172}
173
Mårten Kongstad0eba72a2018-11-29 08:23:14 +0100174} // namespace android::os