Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 1 | /* |
| 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 Kongstad | d10d06d | 2019-01-07 17:26:25 -0800 | [diff] [blame^] | 29 | #include "android-base/stringprintf.h" |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 30 | #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 Kongstad | d10d06d | 2019-01-07 17:26:25 -0800 | [diff] [blame^] | 36 | #include "idmap2/Policies.h" |
| 37 | #include "idmap2/Result.h" |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 38 | |
| 39 | #include "idmap2d/Idmap2Service.h" |
| 40 | |
| 41 | using android::binder::Status; |
| 42 | using android::idmap2::BinaryStreamVisitor; |
| 43 | using android::idmap2::Idmap; |
| 44 | using android::idmap2::IdmapHeader; |
Mårten Kongstad | d10d06d | 2019-01-07 17:26:25 -0800 | [diff] [blame^] | 45 | using android::idmap2::PolicyBitmask; |
| 46 | using android::idmap2::Result; |
Mårten Kongstad | b877902 | 2018-11-29 09:53:17 +0100 | [diff] [blame] | 47 | using android::idmap2::utils::kIdmapFilePermissionMask; |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 48 | |
| 49 | namespace { |
| 50 | |
Mårten Kongstad | b877902 | 2018-11-29 09:53:17 +0100 | [diff] [blame] | 51 | constexpr const char* kIdmapCacheDir = "/data/resource-cache"; |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 52 | |
| 53 | Status ok() { |
| 54 | return Status::ok(); |
| 55 | } |
| 56 | |
| 57 | Status error(const std::string& msg) { |
| 58 | LOG(ERROR) << msg; |
| 59 | return Status::fromExceptionCode(Status::EX_NONE, msg.c_str()); |
| 60 | } |
| 61 | |
Mårten Kongstad | d10d06d | 2019-01-07 17:26:25 -0800 | [diff] [blame^] | 62 | PolicyBitmask ConvertAidlArgToPolicyBitmask(int32_t arg) { |
| 63 | return static_cast<PolicyBitmask>(arg); |
| 64 | } |
| 65 | |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 66 | } // namespace |
| 67 | |
Mårten Kongstad | 0eba72a | 2018-11-29 08:23:14 +0100 | [diff] [blame] | 68 | namespace android::os { |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 69 | |
| 70 | Status 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 | |
| 77 | Status 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 Kongstad | b877902 | 2018-11-29 09:53:17 +0100 | [diff] [blame] | 81 | if (unlink(idmap_path.c_str()) != 0) { |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 82 | *_aidl_return = false; |
| 83 | return error("failed to unlink " + idmap_path + ": " + strerror(errno)); |
| 84 | } |
Mårten Kongstad | b877902 | 2018-11-29 09:53:17 +0100 | [diff] [blame] | 85 | *_aidl_return = true; |
| 86 | return ok(); |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 87 | } |
| 88 | |
Mårten Kongstad | ef0695d | 2018-12-04 14:36:48 +0100 | [diff] [blame] | 89 | Status Idmap2Service::verifyIdmap(const std::string& overlay_apk_path, |
Mårten Kongstad | d10d06d | 2019-01-07 17:26:25 -0800 | [diff] [blame^] | 90 | int32_t fulfilled_policies ATTRIBUTE_UNUSED, |
| 91 | bool enforce_overlayable ATTRIBUTE_UNUSED, |
Mårten Kongstad | ef0695d | 2018-12-04 14:36:48 +0100 | [diff] [blame] | 92 | 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 Kongstad | d10d06d | 2019-01-07 17:26:25 -0800 | [diff] [blame^] | 100 | |
| 101 | // TODO(b/119328308): Check that the set of fulfilled policies of the overlay has not changed |
| 102 | |
Mårten Kongstad | ef0695d | 2018-12-04 14:36:48 +0100 | [diff] [blame] | 103 | return ok(); |
| 104 | } |
| 105 | |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 106 | Status Idmap2Service::createIdmap(const std::string& target_apk_path, |
Mårten Kongstad | d10d06d | 2019-01-07 17:26:25 -0800 | [diff] [blame^] | 107 | const std::string& overlay_apk_path, int32_t fulfilled_policies, |
| 108 | bool enforce_overlayable, int32_t user_id, |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 109 | 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 Kongstad | d10d06d | 2019-01-07 17:26:25 -0800 | [diff] [blame^] | 119 | const PolicyBitmask policy_bitmask = ConvertAidlArgToPolicyBitmask(fulfilled_policies); |
| 120 | |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 121 | 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 Kongstad | d10d06d | 2019-01-07 17:26:25 -0800 | [diff] [blame^] | 133 | Idmap::FromApkAssets(target_apk_path, *target_apk, overlay_apk_path, *overlay_apk, |
| 134 | policy_bitmask, enforce_overlayable, err); |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 135 | if (!idmap) { |
| 136 | return error(err.str()); |
| 137 | } |
| 138 | |
Mårten Kongstad | b877902 | 2018-11-29 09:53:17 +0100 | [diff] [blame] | 139 | umask(kIdmapFilePermissionMask); |
Mårten Kongstad | ef0695d | 2018-12-04 14:36:48 +0100 | [diff] [blame] | 140 | const std::string idmap_path = Idmap::CanonicalIdmapPathFor(kIdmapCacheDir, overlay_apk_path); |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 141 | 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 Kongstad | 0eba72a | 2018-11-29 08:23:14 +0100 | [diff] [blame] | 152 | *_aidl_return = std::make_unique<std::string>(idmap_path); |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 153 | return ok(); |
| 154 | } |
| 155 | |
Mårten Kongstad | 0eba72a | 2018-11-29 08:23:14 +0100 | [diff] [blame] | 156 | } // namespace android::os |