blob: a3c752718ee26825da3d3e0e8c8e45ee74389440 [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 Kongstad02751232018-04-27 13:16:32 +020030#include "utils/String8.h"
31#include "utils/Trace.h"
32
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 Kongstad02751232018-04-27 13:16:32 +020038
39#include "idmap2d/Idmap2Service.h"
40
41using android::binder::Status;
42using android::idmap2::BinaryStreamVisitor;
43using android::idmap2::Idmap;
44using android::idmap2::IdmapHeader;
Mårten Kongstadd10d06d2019-01-07 17:26:25 -080045using android::idmap2::PolicyBitmask;
46using android::idmap2::Result;
Mårten Kongstadb8779022018-11-29 09:53:17 +010047using android::idmap2::utils::kIdmapFilePermissionMask;
Mårten Kongstad02751232018-04-27 13:16:32 +020048
49namespace {
50
Mårten Kongstadb8779022018-11-29 09:53:17 +010051constexpr const char* kIdmapCacheDir = "/data/resource-cache";
Mårten Kongstad02751232018-04-27 13:16:32 +020052
53Status 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);
73 *_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);
80 const std::string idmap_path = Idmap::CanonicalIdmapPathFor(kIdmapCacheDir, overlay_apk_path);
Mårten Kongstadb8779022018-11-29 09:53:17 +010081 if (unlink(idmap_path.c_str()) != 0) {
Mårten Kongstad02751232018-04-27 13:16:32 +020082 *_aidl_return = false;
83 return error("failed to unlink " + idmap_path + ": " + strerror(errno));
84 }
Mårten Kongstadb8779022018-11-29 09:53:17 +010085 *_aidl_return = true;
86 return ok();
Mårten Kongstad02751232018-04-27 13:16:32 +020087}
88
Mårten Kongstadef0695d2018-12-04 14:36:48 +010089Status Idmap2Service::verifyIdmap(const std::string& overlay_apk_path,
Mårten Kongstadd10d06d2019-01-07 17:26:25 -080090 int32_t fulfilled_policies ATTRIBUTE_UNUSED,
91 bool enforce_overlayable ATTRIBUTE_UNUSED,
Mårten Kongstadef0695d2018-12-04 14:36:48 +010092 int32_t user_id ATTRIBUTE_UNUSED, bool* _aidl_return) {
93 assert(_aidl_return);
94 const std::string idmap_path = Idmap::CanonicalIdmapPathFor(kIdmapCacheDir, overlay_apk_path);
95 std::ifstream fin(idmap_path);
96 const std::unique_ptr<const IdmapHeader> header = IdmapHeader::FromBinaryStream(fin);
97 fin.close();
98 std::stringstream dev_null;
99 *_aidl_return = header && header->IsUpToDate(dev_null);
Mårten Kongstadd10d06d2019-01-07 17:26:25 -0800100
101 // TODO(b/119328308): Check that the set of fulfilled policies of the overlay has not changed
102
Mårten Kongstadef0695d2018-12-04 14:36:48 +0100103 return ok();
104}
105
Mårten Kongstad02751232018-04-27 13:16:32 +0200106Status Idmap2Service::createIdmap(const std::string& target_apk_path,
Mårten Kongstadd10d06d2019-01-07 17:26:25 -0800107 const std::string& overlay_apk_path, int32_t fulfilled_policies,
108 bool enforce_overlayable, int32_t user_id,
Mårten Kongstad02751232018-04-27 13:16:32 +0200109 std::unique_ptr<std::string>* _aidl_return) {
110 assert(_aidl_return);
111 std::stringstream trace;
112 trace << __FUNCTION__ << " " << target_apk_path << " " << overlay_apk_path << " "
113 << std::to_string(user_id);
114 ATRACE_NAME(trace.str().c_str());
115 std::cout << trace.str() << std::endl;
116
117 _aidl_return->reset(nullptr);
118
Mårten Kongstadd10d06d2019-01-07 17:26:25 -0800119 const PolicyBitmask policy_bitmask = ConvertAidlArgToPolicyBitmask(fulfilled_policies);
120
Mårten Kongstad02751232018-04-27 13:16:32 +0200121 const std::unique_ptr<const ApkAssets> target_apk = ApkAssets::Load(target_apk_path);
122 if (!target_apk) {
123 return error("failed to load apk " + target_apk_path);
124 }
125
126 const std::unique_ptr<const ApkAssets> overlay_apk = ApkAssets::Load(overlay_apk_path);
127 if (!overlay_apk) {
128 return error("failed to load apk " + overlay_apk_path);
129 }
130
131 std::stringstream err;
132 const std::unique_ptr<const Idmap> idmap =
Mårten Kongstadd10d06d2019-01-07 17:26:25 -0800133 Idmap::FromApkAssets(target_apk_path, *target_apk, overlay_apk_path, *overlay_apk,
134 policy_bitmask, enforce_overlayable, err);
Mårten Kongstad02751232018-04-27 13:16:32 +0200135 if (!idmap) {
136 return error(err.str());
137 }
138
Mårten Kongstadb8779022018-11-29 09:53:17 +0100139 umask(kIdmapFilePermissionMask);
Mårten Kongstadef0695d2018-12-04 14:36:48 +0100140 const std::string idmap_path = Idmap::CanonicalIdmapPathFor(kIdmapCacheDir, overlay_apk_path);
Mårten Kongstad02751232018-04-27 13:16:32 +0200141 std::ofstream fout(idmap_path);
142 if (fout.fail()) {
143 return error("failed to open idmap path " + idmap_path);
144 }
145 BinaryStreamVisitor visitor(fout);
146 idmap->accept(&visitor);
147 fout.close();
148 if (fout.fail()) {
149 return error("failed to write to idmap path " + idmap_path);
150 }
151
Mårten Kongstad0eba72a2018-11-29 08:23:14 +0100152 *_aidl_return = std::make_unique<std::string>(idmap_path);
Mårten Kongstad02751232018-04-27 13:16:32 +0200153 return ok();
154}
155
Mårten Kongstad0eba72a2018-11-29 08:23:14 +0100156} // namespace android::os