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 | 1da49dc | 2019-01-14 10:03:53 +0100 | [diff] [blame] | 30 | #include "binder/IPCThreadState.h" |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 31 | #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 Kongstad | d10d06d | 2019-01-07 17:26:25 -0800 | [diff] [blame] | 37 | #include "idmap2/Policies.h" |
| 38 | #include "idmap2/Result.h" |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 39 | |
| 40 | #include "idmap2d/Idmap2Service.h" |
| 41 | |
Mårten Kongstad | 1da49dc | 2019-01-14 10:03:53 +0100 | [diff] [blame] | 42 | using android::IPCThreadState; |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 43 | using android::binder::Status; |
| 44 | using android::idmap2::BinaryStreamVisitor; |
| 45 | using android::idmap2::Idmap; |
| 46 | using android::idmap2::IdmapHeader; |
Mårten Kongstad | d10d06d | 2019-01-07 17:26:25 -0800 | [diff] [blame] | 47 | using android::idmap2::PolicyBitmask; |
| 48 | using android::idmap2::Result; |
Mårten Kongstad | 1da49dc | 2019-01-14 10:03:53 +0100 | [diff] [blame] | 49 | using android::idmap2::utils::kIdmapCacheDir; |
Mårten Kongstad | b877902 | 2018-11-29 09:53:17 +0100 | [diff] [blame] | 50 | using android::idmap2::utils::kIdmapFilePermissionMask; |
Mårten Kongstad | 1da49dc | 2019-01-14 10:03:53 +0100 | [diff] [blame] | 51 | using android::idmap2::utils::UidHasWriteAccessToPath; |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 52 | |
| 53 | namespace { |
| 54 | |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 55 | Status ok() { |
| 56 | return Status::ok(); |
| 57 | } |
| 58 | |
| 59 | Status error(const std::string& msg) { |
| 60 | LOG(ERROR) << msg; |
| 61 | return Status::fromExceptionCode(Status::EX_NONE, msg.c_str()); |
| 62 | } |
| 63 | |
Mårten Kongstad | d10d06d | 2019-01-07 17:26:25 -0800 | [diff] [blame] | 64 | PolicyBitmask ConvertAidlArgToPolicyBitmask(int32_t arg) { |
| 65 | return static_cast<PolicyBitmask>(arg); |
| 66 | } |
| 67 | |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 68 | } // namespace |
| 69 | |
Mårten Kongstad | 0eba72a | 2018-11-29 08:23:14 +0100 | [diff] [blame] | 70 | namespace android::os { |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 71 | |
| 72 | Status 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 | |
| 79 | Status Idmap2Service::removeIdmap(const std::string& overlay_apk_path, |
| 80 | int32_t user_id ATTRIBUTE_UNUSED, bool* _aidl_return) { |
| 81 | assert(_aidl_return); |
Mårten Kongstad | 1da49dc | 2019-01-14 10:03:53 +0100 | [diff] [blame] | 82 | const uid_t uid = IPCThreadState::self()->getCallingUid(); |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 83 | const std::string idmap_path = Idmap::CanonicalIdmapPathFor(kIdmapCacheDir, overlay_apk_path); |
Mårten Kongstad | 1da49dc | 2019-01-14 10:03:53 +0100 | [diff] [blame] | 84 | 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 Kongstad | b877902 | 2018-11-29 09:53:17 +0100 | [diff] [blame] | 89 | if (unlink(idmap_path.c_str()) != 0) { |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 90 | *_aidl_return = false; |
| 91 | return error("failed to unlink " + idmap_path + ": " + strerror(errno)); |
| 92 | } |
Mårten Kongstad | b877902 | 2018-11-29 09:53:17 +0100 | [diff] [blame] | 93 | *_aidl_return = true; |
| 94 | return ok(); |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 95 | } |
| 96 | |
Mårten Kongstad | ef0695d | 2018-12-04 14:36:48 +0100 | [diff] [blame] | 97 | Status Idmap2Service::verifyIdmap(const std::string& overlay_apk_path, |
Mårten Kongstad | d10d06d | 2019-01-07 17:26:25 -0800 | [diff] [blame] | 98 | int32_t fulfilled_policies ATTRIBUTE_UNUSED, |
| 99 | bool enforce_overlayable ATTRIBUTE_UNUSED, |
Mårten Kongstad | ef0695d | 2018-12-04 14:36:48 +0100 | [diff] [blame] | 100 | 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 Kongstad | d10d06d | 2019-01-07 17:26:25 -0800 | [diff] [blame] | 108 | |
| 109 | // TODO(b/119328308): Check that the set of fulfilled policies of the overlay has not changed |
| 110 | |
Mårten Kongstad | ef0695d | 2018-12-04 14:36:48 +0100 | [diff] [blame] | 111 | return ok(); |
| 112 | } |
| 113 | |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 114 | Status Idmap2Service::createIdmap(const std::string& target_apk_path, |
Mårten Kongstad | d10d06d | 2019-01-07 17:26:25 -0800 | [diff] [blame] | 115 | const std::string& overlay_apk_path, int32_t fulfilled_policies, |
| 116 | bool enforce_overlayable, int32_t user_id, |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 117 | 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 Kongstad | d10d06d | 2019-01-07 17:26:25 -0800 | [diff] [blame] | 127 | const PolicyBitmask policy_bitmask = ConvertAidlArgToPolicyBitmask(fulfilled_policies); |
| 128 | |
Mårten Kongstad | 1da49dc | 2019-01-14 10:03:53 +0100 | [diff] [blame] | 129 | 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 Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 136 | 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 Kongstad | d10d06d | 2019-01-07 17:26:25 -0800 | [diff] [blame] | 148 | Idmap::FromApkAssets(target_apk_path, *target_apk, overlay_apk_path, *overlay_apk, |
| 149 | policy_bitmask, enforce_overlayable, err); |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 150 | if (!idmap) { |
| 151 | return error(err.str()); |
| 152 | } |
| 153 | |
Mårten Kongstad | b877902 | 2018-11-29 09:53:17 +0100 | [diff] [blame] | 154 | umask(kIdmapFilePermissionMask); |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 155 | 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 Kongstad | 0eba72a | 2018-11-29 08:23:14 +0100 | [diff] [blame] | 166 | *_aidl_return = std::make_unique<std::string>(idmap_path); |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 167 | return ok(); |
| 168 | } |
| 169 | |
Mårten Kongstad | 0eba72a | 2018-11-29 08:23:14 +0100 | [diff] [blame] | 170 | } // namespace android::os |