blob: fa944143e40846b405013e40566f94e3b08c2031 [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
17#include <sys/stat.h> // umask
18#include <sys/types.h> // umask
19#include <unistd.h>
20
21#include <cerrno>
22#include <cstring>
23#include <fstream>
24#include <memory>
25#include <ostream>
26#include <string>
27
28#include "android-base/macros.h"
Mårten Kongstadd10d06d2019-01-07 17:26:25 -080029#include "android-base/stringprintf.h"
Mårten Kongstad1da49dc2019-01-14 10:03:53 +010030#include "binder/IPCThreadState.h"
Mårten Kongstad02751232018-04-27 13:16:32 +020031#include "utils/String8.h"
Mårten Kongstad02751232018-04-27 13:16:32 +020032
33#include "idmap2/BinaryStreamVisitor.h"
34#include "idmap2/FileUtils.h"
35#include "idmap2/Idmap.h"
Mårten Kongstadd10d06d2019-01-07 17:26:25 -080036#include "idmap2/Policies.h"
Mårten Kongstad4cbb0072018-11-30 16:22:05 +010037#include "idmap2/SysTrace.h"
Mårten Kongstad02751232018-04-27 13:16:32 +020038
39#include "idmap2d/Idmap2Service.h"
40
Mårten Kongstad1da49dc2019-01-14 10:03:53 +010041using android::IPCThreadState;
Mårten Kongstad02751232018-04-27 13:16:32 +020042using android::binder::Status;
43using android::idmap2::BinaryStreamVisitor;
44using android::idmap2::Idmap;
45using android::idmap2::IdmapHeader;
Mårten Kongstadd10d06d2019-01-07 17:26:25 -080046using android::idmap2::PolicyBitmask;
Mårten Kongstad1da49dc2019-01-14 10:03:53 +010047using android::idmap2::utils::kIdmapCacheDir;
Mårten Kongstadb8779022018-11-29 09:53:17 +010048using android::idmap2::utils::kIdmapFilePermissionMask;
Mårten Kongstad1da49dc2019-01-14 10:03:53 +010049using android::idmap2::utils::UidHasWriteAccessToPath;
Mårten Kongstad02751232018-04-27 13:16:32 +020050
51namespace {
52
Mårten Kongstad02751232018-04-27 13:16:32 +020053Status ok() {
54 return Status::ok();
55}
56
57Status error(const std::string& msg) {
58 LOG(ERROR) << msg;
59 return Status::fromExceptionCode(Status::EX_NONE, msg.c_str());
60}
61
Mårten Kongstadd10d06d2019-01-07 17:26:25 -080062PolicyBitmask ConvertAidlArgToPolicyBitmask(int32_t arg) {
63 return static_cast<PolicyBitmask>(arg);
64}
65
Mårten Kongstad02751232018-04-27 13:16:32 +020066} // namespace
67
Mårten Kongstad0eba72a2018-11-29 08:23:14 +010068namespace android::os {
Mårten Kongstad02751232018-04-27 13:16:32 +020069
70Status Idmap2Service::getIdmapPath(const std::string& overlay_apk_path,
71 int32_t user_id ATTRIBUTE_UNUSED, std::string* _aidl_return) {
72 assert(_aidl_return);
Mårten Kongstad4cbb0072018-11-30 16:22:05 +010073 SYSTRACE << "Idmap2Service::getIdmapPath " << overlay_apk_path;
Mårten Kongstad02751232018-04-27 13:16:32 +020074 *_aidl_return = Idmap::CanonicalIdmapPathFor(kIdmapCacheDir, overlay_apk_path);
75 return ok();
76}
77
78Status Idmap2Service::removeIdmap(const std::string& overlay_apk_path,
79 int32_t user_id ATTRIBUTE_UNUSED, bool* _aidl_return) {
80 assert(_aidl_return);
Mårten Kongstad4cbb0072018-11-30 16:22:05 +010081 SYSTRACE << "Idmap2Service::removeIdmap " << overlay_apk_path;
Mårten Kongstad1da49dc2019-01-14 10:03:53 +010082 const uid_t uid = IPCThreadState::self()->getCallingUid();
Mårten Kongstad02751232018-04-27 13:16:32 +020083 const std::string idmap_path = Idmap::CanonicalIdmapPathFor(kIdmapCacheDir, overlay_apk_path);
Mårten Kongstad1da49dc2019-01-14 10:03:53 +010084 if (!UidHasWriteAccessToPath(uid, idmap_path)) {
85 *_aidl_return = false;
86 return error(base::StringPrintf("failed to unlink %s: calling uid %d lacks write access",
87 idmap_path.c_str(), uid));
88 }
Mårten Kongstadb8779022018-11-29 09:53:17 +010089 if (unlink(idmap_path.c_str()) != 0) {
Mårten Kongstad02751232018-04-27 13:16:32 +020090 *_aidl_return = false;
91 return error("failed to unlink " + idmap_path + ": " + strerror(errno));
92 }
Mårten Kongstadb8779022018-11-29 09:53:17 +010093 *_aidl_return = true;
94 return ok();
Mårten Kongstad02751232018-04-27 13:16:32 +020095}
96
Mårten Kongstadef0695d2018-12-04 14:36:48 +010097Status Idmap2Service::verifyIdmap(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();
107 std::stringstream dev_null;
108 *_aidl_return = header && header->IsUpToDate(dev_null);
Mårten Kongstadd10d06d2019-01-07 17:26:25 -0800109
110 // TODO(b/119328308): Check that the set of fulfilled policies of the overlay has not changed
111
Mårten Kongstadef0695d2018-12-04 14:36:48 +0100112 return ok();
113}
114
Mårten Kongstad02751232018-04-27 13:16:32 +0200115Status Idmap2Service::createIdmap(const std::string& target_apk_path,
Mårten Kongstadd10d06d2019-01-07 17:26:25 -0800116 const std::string& overlay_apk_path, int32_t fulfilled_policies,
Mårten Kongstad4cbb0072018-11-30 16:22:05 +0100117 bool enforce_overlayable, int32_t user_id ATTRIBUTE_UNUSED,
Mårten Kongstad02751232018-04-27 13:16:32 +0200118 std::unique_ptr<std::string>* _aidl_return) {
119 assert(_aidl_return);
Mårten Kongstad4cbb0072018-11-30 16:22:05 +0100120 SYSTRACE << "Idmap2Service::createIdmap " << target_apk_path << " " << overlay_apk_path;
Mårten Kongstad02751232018-04-27 13:16:32 +0200121 _aidl_return->reset(nullptr);
122
Mårten Kongstadd10d06d2019-01-07 17:26:25 -0800123 const PolicyBitmask policy_bitmask = ConvertAidlArgToPolicyBitmask(fulfilled_policies);
124
Mårten Kongstad1da49dc2019-01-14 10:03:53 +0100125 const std::string idmap_path = Idmap::CanonicalIdmapPathFor(kIdmapCacheDir, overlay_apk_path);
126 const uid_t uid = IPCThreadState::self()->getCallingUid();
127 if (!UidHasWriteAccessToPath(uid, idmap_path)) {
128 return error(base::StringPrintf("will not write to %s: calling uid %d lacks write accesss",
129 idmap_path.c_str(), uid));
130 }
131
Mårten Kongstad02751232018-04-27 13:16:32 +0200132 const std::unique_ptr<const ApkAssets> target_apk = ApkAssets::Load(target_apk_path);
133 if (!target_apk) {
134 return error("failed to load apk " + target_apk_path);
135 }
136
137 const std::unique_ptr<const ApkAssets> overlay_apk = ApkAssets::Load(overlay_apk_path);
138 if (!overlay_apk) {
139 return error("failed to load apk " + overlay_apk_path);
140 }
141
142 std::stringstream err;
143 const std::unique_ptr<const Idmap> idmap =
Mårten Kongstadd10d06d2019-01-07 17:26:25 -0800144 Idmap::FromApkAssets(target_apk_path, *target_apk, overlay_apk_path, *overlay_apk,
145 policy_bitmask, enforce_overlayable, err);
Mårten Kongstad02751232018-04-27 13:16:32 +0200146 if (!idmap) {
147 return error(err.str());
148 }
149
Mårten Kongstadb8779022018-11-29 09:53:17 +0100150 umask(kIdmapFilePermissionMask);
Mårten Kongstad02751232018-04-27 13:16:32 +0200151 std::ofstream fout(idmap_path);
152 if (fout.fail()) {
153 return error("failed to open idmap path " + idmap_path);
154 }
155 BinaryStreamVisitor visitor(fout);
156 idmap->accept(&visitor);
157 fout.close();
158 if (fout.fail()) {
159 return error("failed to write to idmap path " + idmap_path);
160 }
161
Mårten Kongstad0eba72a2018-11-29 08:23:14 +0100162 *_aidl_return = std::make_unique<std::string>(idmap_path);
Mårten Kongstad02751232018-04-27 13:16:32 +0200163 return ok();
164}
165
Mårten Kongstad0eba72a2018-11-29 08:23:14 +0100166} // namespace android::os