David Brazdil | 5a61bb7 | 2018-01-19 16:59:46 +0000 | [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 | #ifndef ART_RUNTIME_HIDDEN_API_H_ |
| 18 | #define ART_RUNTIME_HIDDEN_API_H_ |
| 19 | |
David Brazdil | 8ce3bfa | 2018-03-12 18:01:18 +0000 | [diff] [blame] | 20 | #include "art_field-inl.h" |
| 21 | #include "art_method-inl.h" |
Andreas Gampe | aa12001 | 2018-03-28 16:23:24 -0700 | [diff] [blame^] | 22 | #include "base/mutex.h" |
David Sehr | 67bf42e | 2018-02-26 16:43:04 -0800 | [diff] [blame] | 23 | #include "dex/hidden_api_access_flags.h" |
David Brazdil | 8ce3bfa | 2018-03-12 18:01:18 +0000 | [diff] [blame] | 24 | #include "mirror/class-inl.h" |
David Brazdil | 5a61bb7 | 2018-01-19 16:59:46 +0000 | [diff] [blame] | 25 | #include "reflection.h" |
| 26 | #include "runtime.h" |
| 27 | |
| 28 | namespace art { |
| 29 | namespace hiddenapi { |
| 30 | |
Mathew Inwood | 597d7f6 | 2018-03-22 11:36:47 +0000 | [diff] [blame] | 31 | // Hidden API enforcement policy |
| 32 | // This must be kept in sync with ApplicationInfo.ApiEnforcementPolicy in |
| 33 | // frameworks/base/core/java/android/content/pm/ApplicationInfo.java |
| 34 | enum class EnforcementPolicy { |
| 35 | kNoChecks = 0, |
| 36 | kAllLists = 1, // ban anything but whitelist |
| 37 | kDarkGreyAndBlackList = 2, // ban dark grey & blacklist |
| 38 | kBlacklistOnly = 3, // ban blacklist violations only |
| 39 | kMax = kBlacklistOnly, |
| 40 | }; |
| 41 | |
| 42 | inline EnforcementPolicy EnforcementPolicyFromInt(int api_policy_int) { |
| 43 | DCHECK_GE(api_policy_int, 0); |
| 44 | DCHECK_LE(api_policy_int, static_cast<int>(EnforcementPolicy::kMax)); |
| 45 | return static_cast<EnforcementPolicy>(api_policy_int); |
| 46 | } |
| 47 | |
David Brazdil | a02cb11 | 2018-01-31 11:36:39 +0000 | [diff] [blame] | 48 | enum Action { |
| 49 | kAllow, |
| 50 | kAllowButWarn, |
David Brazdil | 9226522 | 2018-02-02 11:21:40 +0000 | [diff] [blame] | 51 | kAllowButWarnAndToast, |
David Brazdil | a02cb11 | 2018-01-31 11:36:39 +0000 | [diff] [blame] | 52 | kDeny |
| 53 | }; |
David Brazdil | 5a61bb7 | 2018-01-19 16:59:46 +0000 | [diff] [blame] | 54 | |
David Brazdil | 068d68d | 2018-02-12 13:04:17 -0800 | [diff] [blame] | 55 | enum AccessMethod { |
| 56 | kReflection, |
David Brazdil | 8ce3bfa | 2018-03-12 18:01:18 +0000 | [diff] [blame] | 57 | kJNI, |
| 58 | kLinking, |
David Brazdil | 068d68d | 2018-02-12 13:04:17 -0800 | [diff] [blame] | 59 | }; |
| 60 | |
David Brazdil | a02cb11 | 2018-01-31 11:36:39 +0000 | [diff] [blame] | 61 | inline Action GetMemberAction(uint32_t access_flags) { |
Mathew Inwood | 597d7f6 | 2018-03-22 11:36:47 +0000 | [diff] [blame] | 62 | EnforcementPolicy policy = Runtime::Current()->GetHiddenApiEnforcementPolicy(); |
| 63 | if (policy == EnforcementPolicy::kNoChecks) { |
| 64 | // Exit early. Nothing to enforce. |
| 65 | return kAllow; |
| 66 | } |
| 67 | |
| 68 | HiddenApiAccessFlags::ApiList api_list = HiddenApiAccessFlags::DecodeFromRuntime(access_flags); |
| 69 | if (api_list == HiddenApiAccessFlags::kWhitelist) { |
| 70 | return kAllow; |
| 71 | } |
| 72 | // The logic below relies on equality of values in the enums EnforcementPolicy and |
Andreas Gampe | aa12001 | 2018-03-28 16:23:24 -0700 | [diff] [blame^] | 73 | // HiddenApiAccessFlags::ApiList, and their ordering. Assertions are in hidden_api.cc. |
Mathew Inwood | 597d7f6 | 2018-03-22 11:36:47 +0000 | [diff] [blame] | 74 | if (static_cast<int>(policy) > static_cast<int>(api_list)) { |
| 75 | return api_list == HiddenApiAccessFlags::kDarkGreylist |
| 76 | ? kAllowButWarnAndToast |
| 77 | : kAllowButWarn; |
| 78 | } else { |
| 79 | return kDeny; |
David Brazdil | 5a61bb7 | 2018-01-19 16:59:46 +0000 | [diff] [blame] | 80 | } |
| 81 | } |
| 82 | |
Andreas Gampe | aa12001 | 2018-03-28 16:23:24 -0700 | [diff] [blame^] | 83 | // Implementation details. DO NOT ACCESS DIRECTLY. |
| 84 | namespace detail { |
David Brazdil | ee7d2fd | 2018-01-20 17:25:23 +0000 | [diff] [blame] | 85 | |
Mathew Inwood | 7d74ef5 | 2018-03-16 14:18:33 +0000 | [diff] [blame] | 86 | // Class to encapsulate the signature of a member (ArtField or ArtMethod). This |
| 87 | // is used as a helper when matching prefixes, and when logging the signature. |
| 88 | class MemberSignature { |
| 89 | private: |
| 90 | std::string member_type_; |
| 91 | std::vector<std::string> signature_parts_; |
| 92 | std::string tmp_; |
| 93 | |
| 94 | public: |
Andreas Gampe | aa12001 | 2018-03-28 16:23:24 -0700 | [diff] [blame^] | 95 | explicit MemberSignature(ArtField* field) REQUIRES_SHARED(Locks::mutator_lock_); |
| 96 | explicit MemberSignature(ArtMethod* method) REQUIRES_SHARED(Locks::mutator_lock_); |
Mathew Inwood | 7d74ef5 | 2018-03-16 14:18:33 +0000 | [diff] [blame] | 97 | |
Andreas Gampe | aa12001 | 2018-03-28 16:23:24 -0700 | [diff] [blame^] | 98 | void Dump(std::ostream& os) const; |
Mathew Inwood | 7d74ef5 | 2018-03-16 14:18:33 +0000 | [diff] [blame] | 99 | |
Mathew Inwood | 7d74ef5 | 2018-03-16 14:18:33 +0000 | [diff] [blame] | 100 | // Performs prefix match on this member. Since the full member signature is |
| 101 | // composed of several parts, we match each part in turn (rather than |
| 102 | // building the entire thing in memory and performing a simple prefix match) |
Andreas Gampe | aa12001 | 2018-03-28 16:23:24 -0700 | [diff] [blame^] | 103 | bool DoesPrefixMatch(const std::string& prefix) const; |
Mathew Inwood | 7d74ef5 | 2018-03-16 14:18:33 +0000 | [diff] [blame] | 104 | |
Andreas Gampe | aa12001 | 2018-03-28 16:23:24 -0700 | [diff] [blame^] | 105 | bool IsExempted(const std::vector<std::string>& exemptions); |
Mathew Inwood | 7d74ef5 | 2018-03-16 14:18:33 +0000 | [diff] [blame] | 106 | |
Andreas Gampe | aa12001 | 2018-03-28 16:23:24 -0700 | [diff] [blame^] | 107 | void WarnAboutAccess(AccessMethod access_method, HiddenApiAccessFlags::ApiList list); |
Mathew Inwood | 7d74ef5 | 2018-03-16 14:18:33 +0000 | [diff] [blame] | 108 | }; |
David Brazdil | ee7d2fd | 2018-01-20 17:25:23 +0000 | [diff] [blame] | 109 | |
Andreas Gampe | aa12001 | 2018-03-28 16:23:24 -0700 | [diff] [blame^] | 110 | template<typename T> |
| 111 | bool ShouldBlockAccessToMemberImpl(T* member, |
| 112 | Action action, |
| 113 | AccessMethod access_method) |
| 114 | REQUIRES_SHARED(Locks::mutator_lock_); |
| 115 | |
| 116 | // Returns true if the caller is either loaded by the boot strap class loader or comes from |
| 117 | // a dex file located in ${ANDROID_ROOT}/framework/. |
| 118 | ALWAYS_INLINE |
| 119 | inline bool IsCallerInPlatformDex(ObjPtr<mirror::ClassLoader> caller_class_loader, |
| 120 | ObjPtr<mirror::DexCache> caller_dex_cache) |
| 121 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 122 | if (caller_class_loader.IsNull()) { |
| 123 | return true; |
| 124 | } else if (caller_dex_cache.IsNull()) { |
| 125 | return false; |
| 126 | } else { |
| 127 | const DexFile* caller_dex_file = caller_dex_cache->GetDexFile(); |
| 128 | return caller_dex_file != nullptr && caller_dex_file->IsPlatformDexFile(); |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | } // namespace detail |
| 133 | |
David Brazdil | a02cb11 | 2018-01-31 11:36:39 +0000 | [diff] [blame] | 134 | // Returns true if access to `member` should be denied to the caller of the |
David Brazdil | 8e1a7cb | 2018-03-27 08:14:25 +0000 | [diff] [blame] | 135 | // reflective query. The decision is based on whether the caller is in the |
| 136 | // platform or not. Because different users of this function determine this |
| 137 | // in a different way, `fn_caller_in_platform(self)` is called and should |
| 138 | // return true if the caller is located in the platform. |
David Brazdil | 8ce3bfa | 2018-03-12 18:01:18 +0000 | [diff] [blame] | 139 | // This function might print warnings into the log if the member is hidden. |
David Brazdil | ee7d2fd | 2018-01-20 17:25:23 +0000 | [diff] [blame] | 140 | template<typename T> |
David Brazdil | a02cb11 | 2018-01-31 11:36:39 +0000 | [diff] [blame] | 141 | inline bool ShouldBlockAccessToMember(T* member, |
| 142 | Thread* self, |
David Brazdil | 8e1a7cb | 2018-03-27 08:14:25 +0000 | [diff] [blame] | 143 | std::function<bool(Thread*)> fn_caller_in_platform, |
David Brazdil | 068d68d | 2018-02-12 13:04:17 -0800 | [diff] [blame] | 144 | AccessMethod access_method) |
David Brazdil | ee7d2fd | 2018-01-20 17:25:23 +0000 | [diff] [blame] | 145 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 146 | DCHECK(member != nullptr); |
David Brazdil | ee7d2fd | 2018-01-20 17:25:23 +0000 | [diff] [blame] | 147 | |
David Brazdil | a02cb11 | 2018-01-31 11:36:39 +0000 | [diff] [blame] | 148 | Action action = GetMemberAction(member->GetAccessFlags()); |
| 149 | if (action == kAllow) { |
| 150 | // Nothing to do. |
| 151 | return false; |
| 152 | } |
| 153 | |
David Brazdil | 8e1a7cb | 2018-03-27 08:14:25 +0000 | [diff] [blame] | 154 | // Member is hidden. Invoke `fn_caller_in_platform` and find the origin of the access. |
David Brazdil | a02cb11 | 2018-01-31 11:36:39 +0000 | [diff] [blame] | 155 | // This can be *very* expensive. Save it for last. |
David Brazdil | 8e1a7cb | 2018-03-27 08:14:25 +0000 | [diff] [blame] | 156 | if (fn_caller_in_platform(self)) { |
| 157 | // Caller in the platform. Exit. |
David Brazdil | a02cb11 | 2018-01-31 11:36:39 +0000 | [diff] [blame] | 158 | return false; |
| 159 | } |
| 160 | |
David Brazdil | 8e1a7cb | 2018-03-27 08:14:25 +0000 | [diff] [blame] | 161 | // Member is hidden and caller is not in the platform. |
Andreas Gampe | aa12001 | 2018-03-28 16:23:24 -0700 | [diff] [blame^] | 162 | return detail::ShouldBlockAccessToMemberImpl(member, action, access_method); |
David Brazdil | 8e1a7cb | 2018-03-27 08:14:25 +0000 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | inline bool IsCallerInPlatformDex(ObjPtr<mirror::Class> caller) |
| 166 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Andreas Gampe | aa12001 | 2018-03-28 16:23:24 -0700 | [diff] [blame^] | 167 | return !caller.IsNull() && |
| 168 | detail::IsCallerInPlatformDex(caller->GetClassLoader(), caller->GetDexCache()); |
David Brazdil | 8e1a7cb | 2018-03-27 08:14:25 +0000 | [diff] [blame] | 169 | } |
| 170 | |
David Brazdil | 8ce3bfa | 2018-03-12 18:01:18 +0000 | [diff] [blame] | 171 | // Returns true if access to `member` should be denied to a caller loaded with |
| 172 | // `caller_class_loader`. |
| 173 | // This function might print warnings into the log if the member is hidden. |
| 174 | template<typename T> |
| 175 | inline bool ShouldBlockAccessToMember(T* member, |
| 176 | ObjPtr<mirror::ClassLoader> caller_class_loader, |
David Brazdil | 8e1a7cb | 2018-03-27 08:14:25 +0000 | [diff] [blame] | 177 | ObjPtr<mirror::DexCache> caller_dex_cache, |
David Brazdil | 8ce3bfa | 2018-03-12 18:01:18 +0000 | [diff] [blame] | 178 | AccessMethod access_method) |
David Brazdil | ee7d2fd | 2018-01-20 17:25:23 +0000 | [diff] [blame] | 179 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Andreas Gampe | aa12001 | 2018-03-28 16:23:24 -0700 | [diff] [blame^] | 180 | bool caller_in_platform = detail::IsCallerInPlatformDex(caller_class_loader, caller_dex_cache); |
David Brazdil | 8ce3bfa | 2018-03-12 18:01:18 +0000 | [diff] [blame] | 181 | return ShouldBlockAccessToMember(member, |
| 182 | /* thread */ nullptr, |
David Brazdil | 8e1a7cb | 2018-03-27 08:14:25 +0000 | [diff] [blame] | 183 | [caller_in_platform] (Thread*) { return caller_in_platform; }, |
David Brazdil | 8ce3bfa | 2018-03-12 18:01:18 +0000 | [diff] [blame] | 184 | access_method); |
David Brazdil | ee7d2fd | 2018-01-20 17:25:23 +0000 | [diff] [blame] | 185 | } |
| 186 | |
David Brazdil | 5a61bb7 | 2018-01-19 16:59:46 +0000 | [diff] [blame] | 187 | } // namespace hiddenapi |
| 188 | } // namespace art |
| 189 | |
| 190 | #endif // ART_RUNTIME_HIDDEN_API_H_ |