blob: f30ce9b08d6e3bd4dc4fedc096864a6818248fb5 [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"
32#include "utils/Trace.h"
33
34#include "idmap2/BinaryStreamVisitor.h"
35#include "idmap2/FileUtils.h"
36#include "idmap2/Idmap.h"
Mårten Kongstadd10d06d2019-01-07 17:26:25 -080037#include "idmap2/Policies.h"
38#include "idmap2/Result.h"
Mårten Kongstad02751232018-04-27 13:16:32 +020039
40#include "idmap2d/Idmap2Service.h"
41
Mårten Kongstad1da49dc2019-01-14 10:03:53 +010042using android::IPCThreadState;
Mårten Kongstad02751232018-04-27 13:16:32 +020043using android::binder::Status;
44using android::idmap2::BinaryStreamVisitor;
45using android::idmap2::Idmap;
46using android::idmap2::IdmapHeader;
Mårten Kongstadd10d06d2019-01-07 17:26:25 -080047using android::idmap2::PolicyBitmask;
48using android::idmap2::Result;
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
53namespace {
54
Mårten Kongstad02751232018-04-27 13:16:32 +020055Status ok() {
56 return Status::ok();
57}
58
59Status error(const std::string& msg) {
60 LOG(ERROR) << msg;
61 return Status::fromExceptionCode(Status::EX_NONE, msg.c_str());
62}
63
Mårten Kongstadd10d06d2019-01-07 17:26:25 -080064PolicyBitmask ConvertAidlArgToPolicyBitmask(int32_t arg) {
65 return static_cast<PolicyBitmask>(arg);
66}
67
Mårten Kongstad02751232018-04-27 13:16:32 +020068} // namespace
69
Mårten Kongstad0eba72a2018-11-29 08:23:14 +010070namespace android::os {
Mårten Kongstad02751232018-04-27 13:16:32 +020071
72Status Idmap2Service::getIdmapPath(const std::string& overlay_apk_path,
73 int32_t user_id ATTRIBUTE_UNUSED, std::string* _aidl_return) {
74 assert(_aidl_return);
75 *_aidl_return = Idmap::CanonicalIdmapPathFor(kIdmapCacheDir, overlay_apk_path);
76 return ok();
77}
78
79Status Idmap2Service::removeIdmap(const std::string& overlay_apk_path,
80 int32_t user_id ATTRIBUTE_UNUSED, bool* _aidl_return) {
81 assert(_aidl_return);
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) {
101 assert(_aidl_return);
102 const std::string idmap_path = Idmap::CanonicalIdmapPathFor(kIdmapCacheDir, overlay_apk_path);
103 std::ifstream fin(idmap_path);
104 const std::unique_ptr<const IdmapHeader> header = IdmapHeader::FromBinaryStream(fin);
105 fin.close();
106 std::stringstream dev_null;
107 *_aidl_return = header && header->IsUpToDate(dev_null);
Mårten Kongstadd10d06d2019-01-07 17:26:25 -0800108
109 // TODO(b/119328308): Check that the set of fulfilled policies of the overlay has not changed
110
Mårten Kongstadef0695d2018-12-04 14:36:48 +0100111 return ok();
112}
113
Mårten Kongstad02751232018-04-27 13:16:32 +0200114Status Idmap2Service::createIdmap(const std::string& target_apk_path,
Mårten Kongstadd10d06d2019-01-07 17:26:25 -0800115 const std::string& overlay_apk_path, int32_t fulfilled_policies,
116 bool enforce_overlayable, int32_t user_id,
Mårten Kongstad02751232018-04-27 13:16:32 +0200117 std::unique_ptr<std::string>* _aidl_return) {
118 assert(_aidl_return);
119 std::stringstream trace;
120 trace << __FUNCTION__ << " " << target_apk_path << " " << overlay_apk_path << " "
121 << std::to_string(user_id);
122 ATRACE_NAME(trace.str().c_str());
123 std::cout << trace.str() << std::endl;
124
125 _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
146 std::stringstream err;
147 const std::unique_ptr<const Idmap> idmap =
Mårten Kongstadd10d06d2019-01-07 17:26:25 -0800148 Idmap::FromApkAssets(target_apk_path, *target_apk, overlay_apk_path, *overlay_apk,
149 policy_bitmask, enforce_overlayable, err);
Mårten Kongstad02751232018-04-27 13:16:32 +0200150 if (!idmap) {
151 return error(err.str());
152 }
153
Mårten Kongstadb8779022018-11-29 09:53:17 +0100154 umask(kIdmapFilePermissionMask);
Mårten Kongstad02751232018-04-27 13:16:32 +0200155 std::ofstream fout(idmap_path);
156 if (fout.fail()) {
157 return error("failed to open idmap path " + idmap_path);
158 }
159 BinaryStreamVisitor visitor(fout);
160 idmap->accept(&visitor);
161 fout.close();
162 if (fout.fail()) {
163 return error("failed to write to idmap path " + idmap_path);
164 }
165
Mårten Kongstad0eba72a2018-11-29 08:23:14 +0100166 *_aidl_return = std::make_unique<std::string>(idmap_path);
Mårten Kongstad02751232018-04-27 13:16:32 +0200167 return ok();
168}
169
Mårten Kongstad0eba72a2018-11-29 08:23:14 +0100170} // namespace android::os