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, |
Mathew Inwood | a8503d9 | 2018-04-05 16:10:25 +0100 | [diff] [blame] | 36 | kJustWarn = 1, // keep checks enabled, but allow everything (enables logging) |
Mathew Inwood | 597d7f6 | 2018-03-22 11:36:47 +0000 | [diff] [blame] | 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 { |
Mathew Inwood | 1724520 | 2018-04-12 13:56:37 +0100 | [diff] [blame^] | 56 | kNone, // internal test that does not correspond to an actual access by app |
| 57 | kReflection, |
| 58 | kJNI, |
| 59 | kLinking, |
Mathew Inwood | 1fd97f2 | 2018-04-03 15:32:32 +0100 | [diff] [blame] | 60 | }; |
| 61 | |
| 62 | // Do not change the values of items in this enum, as they are written to the |
| 63 | // event log for offline analysis. Any changes will interfere with that analysis. |
| 64 | enum AccessContextFlags { |
| 65 | // Accessed member is a field if this bit is set, else a method |
| 66 | kMemberIsField = 1 << 0, |
| 67 | // Indicates if access was denied to the member, instead of just printing a warning. |
| 68 | kAccessDenied = 1 << 1, |
David Brazdil | 068d68d | 2018-02-12 13:04:17 -0800 | [diff] [blame] | 69 | }; |
| 70 | |
Narayan Kamath | f5f1f80 | 2018-04-03 15:23:46 +0100 | [diff] [blame] | 71 | inline Action GetActionFromAccessFlags(uint32_t access_flags) { |
Mathew Inwood | 597d7f6 | 2018-03-22 11:36:47 +0000 | [diff] [blame] | 72 | EnforcementPolicy policy = Runtime::Current()->GetHiddenApiEnforcementPolicy(); |
| 73 | if (policy == EnforcementPolicy::kNoChecks) { |
| 74 | // Exit early. Nothing to enforce. |
| 75 | return kAllow; |
| 76 | } |
| 77 | |
| 78 | HiddenApiAccessFlags::ApiList api_list = HiddenApiAccessFlags::DecodeFromRuntime(access_flags); |
| 79 | if (api_list == HiddenApiAccessFlags::kWhitelist) { |
| 80 | return kAllow; |
| 81 | } |
Mathew Inwood | a8503d9 | 2018-04-05 16:10:25 +0100 | [diff] [blame] | 82 | // if policy is "just warn", always warn. We returned above for whitelist APIs. |
| 83 | if (policy == EnforcementPolicy::kJustWarn) { |
| 84 | return kAllowButWarn; |
| 85 | } |
| 86 | DCHECK(policy >= EnforcementPolicy::kDarkGreyAndBlackList); |
Mathew Inwood | 597d7f6 | 2018-03-22 11:36:47 +0000 | [diff] [blame] | 87 | // 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] | 88 | // HiddenApiAccessFlags::ApiList, and their ordering. Assertions are in hidden_api.cc. |
Mathew Inwood | 597d7f6 | 2018-03-22 11:36:47 +0000 | [diff] [blame] | 89 | if (static_cast<int>(policy) > static_cast<int>(api_list)) { |
| 90 | return api_list == HiddenApiAccessFlags::kDarkGreylist |
| 91 | ? kAllowButWarnAndToast |
| 92 | : kAllowButWarn; |
| 93 | } else { |
| 94 | return kDeny; |
David Brazdil | 5a61bb7 | 2018-01-19 16:59:46 +0000 | [diff] [blame] | 95 | } |
| 96 | } |
| 97 | |
Andreas Gampe | aa12001 | 2018-03-28 16:23:24 -0700 | [diff] [blame] | 98 | // Implementation details. DO NOT ACCESS DIRECTLY. |
| 99 | namespace detail { |
David Brazdil | ee7d2fd | 2018-01-20 17:25:23 +0000 | [diff] [blame] | 100 | |
Mathew Inwood | 7d74ef5 | 2018-03-16 14:18:33 +0000 | [diff] [blame] | 101 | // Class to encapsulate the signature of a member (ArtField or ArtMethod). This |
| 102 | // is used as a helper when matching prefixes, and when logging the signature. |
| 103 | class MemberSignature { |
| 104 | private: |
Mathew Inwood | 1fd97f2 | 2018-04-03 15:32:32 +0100 | [diff] [blame] | 105 | enum MemberType { |
| 106 | kField, |
| 107 | kMethod, |
| 108 | }; |
| 109 | |
| 110 | std::string class_name_; |
| 111 | std::string member_name_; |
| 112 | std::string type_signature_; |
Mathew Inwood | 7d74ef5 | 2018-03-16 14:18:33 +0000 | [diff] [blame] | 113 | std::string tmp_; |
Mathew Inwood | 1fd97f2 | 2018-04-03 15:32:32 +0100 | [diff] [blame] | 114 | MemberType type_; |
| 115 | |
| 116 | inline std::vector<const char*> GetSignatureParts() const; |
Mathew Inwood | 7d74ef5 | 2018-03-16 14:18:33 +0000 | [diff] [blame] | 117 | |
| 118 | public: |
Andreas Gampe | aa12001 | 2018-03-28 16:23:24 -0700 | [diff] [blame] | 119 | explicit MemberSignature(ArtField* field) REQUIRES_SHARED(Locks::mutator_lock_); |
| 120 | explicit MemberSignature(ArtMethod* method) REQUIRES_SHARED(Locks::mutator_lock_); |
Mathew Inwood | 7d74ef5 | 2018-03-16 14:18:33 +0000 | [diff] [blame] | 121 | |
Andreas Gampe | aa12001 | 2018-03-28 16:23:24 -0700 | [diff] [blame] | 122 | void Dump(std::ostream& os) const; |
Mathew Inwood | 7d74ef5 | 2018-03-16 14:18:33 +0000 | [diff] [blame] | 123 | |
Mathew Inwood | 7d74ef5 | 2018-03-16 14:18:33 +0000 | [diff] [blame] | 124 | // Performs prefix match on this member. Since the full member signature is |
| 125 | // composed of several parts, we match each part in turn (rather than |
| 126 | // building the entire thing in memory and performing a simple prefix match) |
Andreas Gampe | aa12001 | 2018-03-28 16:23:24 -0700 | [diff] [blame] | 127 | bool DoesPrefixMatch(const std::string& prefix) const; |
Mathew Inwood | 7d74ef5 | 2018-03-16 14:18:33 +0000 | [diff] [blame] | 128 | |
Andreas Gampe | aa12001 | 2018-03-28 16:23:24 -0700 | [diff] [blame] | 129 | bool IsExempted(const std::vector<std::string>& exemptions); |
Mathew Inwood | 7d74ef5 | 2018-03-16 14:18:33 +0000 | [diff] [blame] | 130 | |
Andreas Gampe | aa12001 | 2018-03-28 16:23:24 -0700 | [diff] [blame] | 131 | void WarnAboutAccess(AccessMethod access_method, HiddenApiAccessFlags::ApiList list); |
Mathew Inwood | 1fd97f2 | 2018-04-03 15:32:32 +0100 | [diff] [blame] | 132 | |
| 133 | void LogAccessToEventLog(AccessMethod access_method, Action action_taken); |
Mathew Inwood | 7d74ef5 | 2018-03-16 14:18:33 +0000 | [diff] [blame] | 134 | }; |
David Brazdil | ee7d2fd | 2018-01-20 17:25:23 +0000 | [diff] [blame] | 135 | |
Andreas Gampe | aa12001 | 2018-03-28 16:23:24 -0700 | [diff] [blame] | 136 | template<typename T> |
Narayan Kamath | f5f1f80 | 2018-04-03 15:23:46 +0100 | [diff] [blame] | 137 | Action GetMemberActionImpl(T* member, Action action, AccessMethod access_method) |
Andreas Gampe | aa12001 | 2018-03-28 16:23:24 -0700 | [diff] [blame] | 138 | REQUIRES_SHARED(Locks::mutator_lock_); |
| 139 | |
| 140 | // Returns true if the caller is either loaded by the boot strap class loader or comes from |
| 141 | // a dex file located in ${ANDROID_ROOT}/framework/. |
| 142 | ALWAYS_INLINE |
| 143 | inline bool IsCallerInPlatformDex(ObjPtr<mirror::ClassLoader> caller_class_loader, |
| 144 | ObjPtr<mirror::DexCache> caller_dex_cache) |
| 145 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 146 | if (caller_class_loader.IsNull()) { |
| 147 | return true; |
| 148 | } else if (caller_dex_cache.IsNull()) { |
| 149 | return false; |
| 150 | } else { |
| 151 | const DexFile* caller_dex_file = caller_dex_cache->GetDexFile(); |
| 152 | return caller_dex_file != nullptr && caller_dex_file->IsPlatformDexFile(); |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | } // namespace detail |
| 157 | |
David Brazdil | a02cb11 | 2018-01-31 11:36:39 +0000 | [diff] [blame] | 158 | // 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] | 159 | // reflective query. The decision is based on whether the caller is in the |
| 160 | // platform or not. Because different users of this function determine this |
| 161 | // in a different way, `fn_caller_in_platform(self)` is called and should |
| 162 | // return true if the caller is located in the platform. |
David Brazdil | 8ce3bfa | 2018-03-12 18:01:18 +0000 | [diff] [blame] | 163 | // 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] | 164 | template<typename T> |
Narayan Kamath | f5f1f80 | 2018-04-03 15:23:46 +0100 | [diff] [blame] | 165 | inline Action GetMemberAction(T* member, |
| 166 | Thread* self, |
| 167 | std::function<bool(Thread*)> fn_caller_in_platform, |
| 168 | AccessMethod access_method) |
David Brazdil | ee7d2fd | 2018-01-20 17:25:23 +0000 | [diff] [blame] | 169 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 170 | DCHECK(member != nullptr); |
David Brazdil | ee7d2fd | 2018-01-20 17:25:23 +0000 | [diff] [blame] | 171 | |
Narayan Kamath | f5f1f80 | 2018-04-03 15:23:46 +0100 | [diff] [blame] | 172 | Action action = GetActionFromAccessFlags(member->GetAccessFlags()); |
David Brazdil | a02cb11 | 2018-01-31 11:36:39 +0000 | [diff] [blame] | 173 | if (action == kAllow) { |
| 174 | // Nothing to do. |
Narayan Kamath | f5f1f80 | 2018-04-03 15:23:46 +0100 | [diff] [blame] | 175 | return action; |
David Brazdil | a02cb11 | 2018-01-31 11:36:39 +0000 | [diff] [blame] | 176 | } |
| 177 | |
David Brazdil | 8e1a7cb | 2018-03-27 08:14:25 +0000 | [diff] [blame] | 178 | // 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] | 179 | // This can be *very* expensive. Save it for last. |
David Brazdil | 8e1a7cb | 2018-03-27 08:14:25 +0000 | [diff] [blame] | 180 | if (fn_caller_in_platform(self)) { |
| 181 | // Caller in the platform. Exit. |
Narayan Kamath | f5f1f80 | 2018-04-03 15:23:46 +0100 | [diff] [blame] | 182 | return kAllow; |
David Brazdil | a02cb11 | 2018-01-31 11:36:39 +0000 | [diff] [blame] | 183 | } |
| 184 | |
David Brazdil | 8e1a7cb | 2018-03-27 08:14:25 +0000 | [diff] [blame] | 185 | // Member is hidden and caller is not in the platform. |
Narayan Kamath | f5f1f80 | 2018-04-03 15:23:46 +0100 | [diff] [blame] | 186 | return detail::GetMemberActionImpl(member, action, access_method); |
David Brazdil | 8e1a7cb | 2018-03-27 08:14:25 +0000 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | inline bool IsCallerInPlatformDex(ObjPtr<mirror::Class> caller) |
| 190 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Andreas Gampe | aa12001 | 2018-03-28 16:23:24 -0700 | [diff] [blame] | 191 | return !caller.IsNull() && |
| 192 | detail::IsCallerInPlatformDex(caller->GetClassLoader(), caller->GetDexCache()); |
David Brazdil | 8e1a7cb | 2018-03-27 08:14:25 +0000 | [diff] [blame] | 193 | } |
| 194 | |
David Brazdil | 8ce3bfa | 2018-03-12 18:01:18 +0000 | [diff] [blame] | 195 | // Returns true if access to `member` should be denied to a caller loaded with |
| 196 | // `caller_class_loader`. |
| 197 | // This function might print warnings into the log if the member is hidden. |
| 198 | template<typename T> |
Narayan Kamath | f5f1f80 | 2018-04-03 15:23:46 +0100 | [diff] [blame] | 199 | inline Action GetMemberAction(T* member, |
| 200 | ObjPtr<mirror::ClassLoader> caller_class_loader, |
| 201 | ObjPtr<mirror::DexCache> caller_dex_cache, |
| 202 | AccessMethod access_method) |
David Brazdil | ee7d2fd | 2018-01-20 17:25:23 +0000 | [diff] [blame] | 203 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Andreas Gampe | aa12001 | 2018-03-28 16:23:24 -0700 | [diff] [blame] | 204 | bool caller_in_platform = detail::IsCallerInPlatformDex(caller_class_loader, caller_dex_cache); |
Narayan Kamath | f5f1f80 | 2018-04-03 15:23:46 +0100 | [diff] [blame] | 205 | return GetMemberAction(member, |
| 206 | /* thread */ nullptr, |
| 207 | [caller_in_platform] (Thread*) { return caller_in_platform; }, |
| 208 | access_method); |
David Brazdil | ee7d2fd | 2018-01-20 17:25:23 +0000 | [diff] [blame] | 209 | } |
| 210 | |
Narayan Kamath | f5f1f80 | 2018-04-03 15:23:46 +0100 | [diff] [blame] | 211 | // Calls back into managed code to notify VMRuntime.nonSdkApiUsageConsumer that |
| 212 | // |member| was accessed. This is usually called when an API is on the black, |
| 213 | // dark grey or light grey lists. Given that the callback can execute arbitrary |
| 214 | // code, a call to this method can result in thread suspension. |
| 215 | template<typename T> void NotifyHiddenApiListener(T* member) |
| 216 | REQUIRES_SHARED(Locks::mutator_lock_); |
| 217 | |
| 218 | |
David Brazdil | 5a61bb7 | 2018-01-19 16:59:46 +0000 | [diff] [blame] | 219 | } // namespace hiddenapi |
| 220 | } // namespace art |
| 221 | |
| 222 | #endif // ART_RUNTIME_HIDDEN_API_H_ |