blob: 0e4bd89e355ccf7aa8537822758c99878bc2a231 [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"
37#include "idmap2/Result.h"
Mårten Kongstad4cbb0072018-11-30 16:22:05 +010038#include "idmap2/SysTrace.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);
Mårten Kongstad4cbb0072018-11-30 16:22:05 +010075 SYSTRACE << "Idmap2Service::getIdmapPath " << overlay_apk_path;
Mårten Kongstad02751232018-04-27 13:16:32 +020076 *_aidl_return = Idmap::CanonicalIdmapPathFor(kIdmapCacheDir, overlay_apk_path);
77 return ok();
78}
79
80Status Idmap2Service::removeIdmap(const std::string& overlay_apk_path,
81 int32_t user_id ATTRIBUTE_UNUSED, bool* _aidl_return) {
82 assert(_aidl_return);
Mårten Kongstad4cbb0072018-11-30 16:22:05 +010083 SYSTRACE << "Idmap2Service::removeIdmap " << overlay_apk_path;
Mårten Kongstad1da49dc2019-01-14 10:03:53 +010084 const uid_t uid = IPCThreadState::self()->getCallingUid();
Mårten Kongstad02751232018-04-27 13:16:32 +020085 const std::string idmap_path = Idmap::CanonicalIdmapPathFor(kIdmapCacheDir, overlay_apk_path);
Mårten Kongstad1da49dc2019-01-14 10:03:53 +010086 if (!UidHasWriteAccessToPath(uid, idmap_path)) {
87 *_aidl_return = false;
88 return error(base::StringPrintf("failed to unlink %s: calling uid %d lacks write access",
89 idmap_path.c_str(), uid));
90 }
Mårten Kongstadb8779022018-11-29 09:53:17 +010091 if (unlink(idmap_path.c_str()) != 0) {
Mårten Kongstad02751232018-04-27 13:16:32 +020092 *_aidl_return = false;
93 return error("failed to unlink " + idmap_path + ": " + strerror(errno));
94 }
Mårten Kongstadb8779022018-11-29 09:53:17 +010095 *_aidl_return = true;
96 return ok();
Mårten Kongstad02751232018-04-27 13:16:32 +020097}
98
Mårten Kongstadef0695d2018-12-04 14:36:48 +010099Status Idmap2Service::verifyIdmap(const std::string& overlay_apk_path,
Mårten Kongstadd10d06d2019-01-07 17:26:25 -0800100 int32_t fulfilled_policies ATTRIBUTE_UNUSED,
101 bool enforce_overlayable ATTRIBUTE_UNUSED,
Mårten Kongstadef0695d2018-12-04 14:36:48 +0100102 int32_t user_id ATTRIBUTE_UNUSED, bool* _aidl_return) {
Mårten Kongstad4cbb0072018-11-30 16:22:05 +0100103 SYSTRACE << "Idmap2Service::verifyIdmap " << overlay_apk_path;
Mårten Kongstadef0695d2018-12-04 14:36:48 +0100104 assert(_aidl_return);
105 const std::string idmap_path = Idmap::CanonicalIdmapPathFor(kIdmapCacheDir, overlay_apk_path);
106 std::ifstream fin(idmap_path);
107 const std::unique_ptr<const IdmapHeader> header = IdmapHeader::FromBinaryStream(fin);
108 fin.close();
109 std::stringstream dev_null;
110 *_aidl_return = header && header->IsUpToDate(dev_null);
Mårten Kongstadd10d06d2019-01-07 17:26:25 -0800111
112 // TODO(b/119328308): Check that the set of fulfilled policies of the overlay has not changed
113
Mårten Kongstadef0695d2018-12-04 14:36:48 +0100114 return ok();
115}
116
Mårten Kongstad02751232018-04-27 13:16:32 +0200117Status Idmap2Service::createIdmap(const std::string& target_apk_path,
Mårten Kongstadd10d06d2019-01-07 17:26:25 -0800118 const std::string& overlay_apk_path, int32_t fulfilled_policies,
Mårten Kongstad4cbb0072018-11-30 16:22:05 +0100119 bool enforce_overlayable, int32_t user_id ATTRIBUTE_UNUSED,
Mårten Kongstad02751232018-04-27 13:16:32 +0200120 std::unique_ptr<std::string>* _aidl_return) {
121 assert(_aidl_return);
Mårten Kongstad4cbb0072018-11-30 16:22:05 +0100122 SYSTRACE << "Idmap2Service::createIdmap " << target_apk_path << " " << overlay_apk_path;
Mårten Kongstad02751232018-04-27 13:16:32 +0200123 _aidl_return->reset(nullptr);
124
Mårten Kongstadd10d06d2019-01-07 17:26:25 -0800125 const PolicyBitmask policy_bitmask = ConvertAidlArgToPolicyBitmask(fulfilled_policies);
126
Mårten Kongstad1da49dc2019-01-14 10:03:53 +0100127 const std::string idmap_path = Idmap::CanonicalIdmapPathFor(kIdmapCacheDir, overlay_apk_path);
128 const uid_t uid = IPCThreadState::self()->getCallingUid();
129 if (!UidHasWriteAccessToPath(uid, idmap_path)) {
130 return error(base::StringPrintf("will not write to %s: calling uid %d lacks write accesss",
131 idmap_path.c_str(), uid));
132 }
133
Mårten Kongstad02751232018-04-27 13:16:32 +0200134 const std::unique_ptr<const ApkAssets> target_apk = ApkAssets::Load(target_apk_path);
135 if (!target_apk) {
136 return error("failed to load apk " + target_apk_path);
137 }
138
139 const std::unique_ptr<const ApkAssets> overlay_apk = ApkAssets::Load(overlay_apk_path);
140 if (!overlay_apk) {
141 return error("failed to load apk " + overlay_apk_path);
142 }
143
144 std::stringstream err;
145 const std::unique_ptr<const Idmap> idmap =
Mårten Kongstadd10d06d2019-01-07 17:26:25 -0800146 Idmap::FromApkAssets(target_apk_path, *target_apk, overlay_apk_path, *overlay_apk,
147 policy_bitmask, enforce_overlayable, err);
Mårten Kongstad02751232018-04-27 13:16:32 +0200148 if (!idmap) {
149 return error(err.str());
150 }
151
Mårten Kongstadb8779022018-11-29 09:53:17 +0100152 umask(kIdmapFilePermissionMask);
Mårten Kongstad02751232018-04-27 13:16:32 +0200153 std::ofstream fout(idmap_path);
154 if (fout.fail()) {
155 return error("failed to open idmap path " + idmap_path);
156 }
157 BinaryStreamVisitor visitor(fout);
158 idmap->accept(&visitor);
159 fout.close();
160 if (fout.fail()) {
161 return error("failed to write to idmap path " + idmap_path);
162 }
163
Mårten Kongstad0eba72a2018-11-29 08:23:14 +0100164 *_aidl_return = std::make_unique<std::string>(idmap_path);
Mårten Kongstad02751232018-04-27 13:16:32 +0200165 return ok();
166}
167
Mårten Kongstad0eba72a2018-11-29 08:23:14 +0100168} // namespace android::os