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 { |
David Brazdil | f50ac10 | 2018-10-17 18:00:06 +0100 | [diff] [blame^] | 35 | kDisabled = 0, |
Mathew Inwood | a8503d9 | 2018-04-05 16:10:25 +0100 | [diff] [blame] | 36 | kJustWarn = 1, // keep checks enabled, but allow everything (enables logging) |
David Brazdil | f50ac10 | 2018-10-17 18:00:06 +0100 | [diff] [blame^] | 37 | kEnabled = 2, // ban dark grey & blacklist |
| 38 | kMax = kEnabled, |
Mathew Inwood | 597d7f6 | 2018-03-22 11:36:47 +0000 | [diff] [blame] | 39 | }; |
| 40 | |
| 41 | inline EnforcementPolicy EnforcementPolicyFromInt(int api_policy_int) { |
| 42 | DCHECK_GE(api_policy_int, 0); |
| 43 | DCHECK_LE(api_policy_int, static_cast<int>(EnforcementPolicy::kMax)); |
| 44 | return static_cast<EnforcementPolicy>(api_policy_int); |
| 45 | } |
| 46 | |
David Brazdil | f50ac10 | 2018-10-17 18:00:06 +0100 | [diff] [blame^] | 47 | enum class AccessMethod { |
Mathew Inwood | 1724520 | 2018-04-12 13:56:37 +0100 | [diff] [blame] | 48 | kNone, // internal test that does not correspond to an actual access by app |
| 49 | kReflection, |
| 50 | kJNI, |
| 51 | kLinking, |
Mathew Inwood | 1fd97f2 | 2018-04-03 15:32:32 +0100 | [diff] [blame] | 52 | }; |
| 53 | |
David Brazdil | f50ac10 | 2018-10-17 18:00:06 +0100 | [diff] [blame^] | 54 | struct AccessContext { |
| 55 | public: |
| 56 | explicit AccessContext(bool is_trusted) : is_trusted_(is_trusted) {} |
| 57 | |
| 58 | explicit AccessContext(ObjPtr<mirror::Class> klass) : is_trusted_(GetIsTrusted(klass)) {} |
| 59 | |
| 60 | AccessContext(ObjPtr<mirror::ClassLoader> class_loader, ObjPtr<mirror::DexCache> dex_cache) |
| 61 | : is_trusted_(GetIsTrusted(class_loader, dex_cache)) {} |
| 62 | |
| 63 | bool IsTrusted() const { return is_trusted_; } |
| 64 | |
| 65 | private: |
| 66 | static bool GetIsTrusted(ObjPtr<mirror::ClassLoader> class_loader, |
| 67 | ObjPtr<mirror::DexCache> dex_cache) |
| 68 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 69 | // Trust if the caller is in is boot class loader. |
| 70 | if (class_loader.IsNull()) { |
| 71 | return true; |
| 72 | } |
| 73 | |
| 74 | // Trust if caller is in a platform dex file. |
| 75 | if (!dex_cache.IsNull()) { |
| 76 | const DexFile* dex_file = dex_cache->GetDexFile(); |
| 77 | if (dex_file != nullptr && dex_file->IsPlatformDexFile()) { |
| 78 | return true; |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | return false; |
| 83 | } |
| 84 | |
| 85 | static bool GetIsTrusted(ObjPtr<mirror::Class> klass) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 86 | DCHECK(!klass.IsNull()); |
| 87 | |
| 88 | if (klass->ShouldSkipHiddenApiChecks() && Runtime::Current()->IsJavaDebuggable()) { |
| 89 | // Class is known, it is marked trusted and we are in debuggable mode. |
| 90 | return true; |
| 91 | } |
| 92 | |
| 93 | // Check other aspects of the context. |
| 94 | return GetIsTrusted(klass->GetClassLoader(), klass->GetDexCache()); |
| 95 | } |
| 96 | |
| 97 | bool is_trusted_; |
David Brazdil | 068d68d | 2018-02-12 13:04:17 -0800 | [diff] [blame] | 98 | }; |
| 99 | |
David Brazdil | 32bde99 | 2018-05-14 15:24:34 +0100 | [diff] [blame] | 100 | class ScopedHiddenApiEnforcementPolicySetting { |
| 101 | public: |
| 102 | explicit ScopedHiddenApiEnforcementPolicySetting(EnforcementPolicy new_policy) |
| 103 | : initial_policy_(Runtime::Current()->GetHiddenApiEnforcementPolicy()) { |
| 104 | Runtime::Current()->SetHiddenApiEnforcementPolicy(new_policy); |
| 105 | } |
| 106 | |
| 107 | ~ScopedHiddenApiEnforcementPolicySetting() { |
| 108 | Runtime::Current()->SetHiddenApiEnforcementPolicy(initial_policy_); |
| 109 | } |
| 110 | |
| 111 | private: |
| 112 | const EnforcementPolicy initial_policy_; |
| 113 | DISALLOW_COPY_AND_ASSIGN(ScopedHiddenApiEnforcementPolicySetting); |
| 114 | }; |
| 115 | |
Andreas Gampe | aa12001 | 2018-03-28 16:23:24 -0700 | [diff] [blame] | 116 | // Implementation details. DO NOT ACCESS DIRECTLY. |
| 117 | namespace detail { |
David Brazdil | ee7d2fd | 2018-01-20 17:25:23 +0000 | [diff] [blame] | 118 | |
David Brazdil | f50ac10 | 2018-10-17 18:00:06 +0100 | [diff] [blame^] | 119 | enum class SdkCodes { |
| 120 | kVersionNone = std::numeric_limits<int32_t>::min(), |
| 121 | kVersionUnlimited = std::numeric_limits<int32_t>::max(), |
| 122 | kVersionO_MR1 = 27, |
| 123 | kVersionP = 28, |
| 124 | }; |
| 125 | |
Mathew Inwood | 7d74ef5 | 2018-03-16 14:18:33 +0000 | [diff] [blame] | 126 | // Class to encapsulate the signature of a member (ArtField or ArtMethod). This |
| 127 | // is used as a helper when matching prefixes, and when logging the signature. |
| 128 | class MemberSignature { |
| 129 | private: |
Mathew Inwood | 1fd97f2 | 2018-04-03 15:32:32 +0100 | [diff] [blame] | 130 | enum MemberType { |
| 131 | kField, |
| 132 | kMethod, |
| 133 | }; |
| 134 | |
| 135 | std::string class_name_; |
| 136 | std::string member_name_; |
| 137 | std::string type_signature_; |
Mathew Inwood | 7d74ef5 | 2018-03-16 14:18:33 +0000 | [diff] [blame] | 138 | std::string tmp_; |
Mathew Inwood | 1fd97f2 | 2018-04-03 15:32:32 +0100 | [diff] [blame] | 139 | MemberType type_; |
| 140 | |
| 141 | inline std::vector<const char*> GetSignatureParts() const; |
Mathew Inwood | 7d74ef5 | 2018-03-16 14:18:33 +0000 | [diff] [blame] | 142 | |
| 143 | public: |
Andreas Gampe | aa12001 | 2018-03-28 16:23:24 -0700 | [diff] [blame] | 144 | explicit MemberSignature(ArtField* field) REQUIRES_SHARED(Locks::mutator_lock_); |
| 145 | explicit MemberSignature(ArtMethod* method) REQUIRES_SHARED(Locks::mutator_lock_); |
Mathew Inwood | 7d74ef5 | 2018-03-16 14:18:33 +0000 | [diff] [blame] | 146 | |
Andreas Gampe | aa12001 | 2018-03-28 16:23:24 -0700 | [diff] [blame] | 147 | void Dump(std::ostream& os) const; |
Mathew Inwood | 7d74ef5 | 2018-03-16 14:18:33 +0000 | [diff] [blame] | 148 | |
Mathew Inwood | 7d74ef5 | 2018-03-16 14:18:33 +0000 | [diff] [blame] | 149 | // Performs prefix match on this member. Since the full member signature is |
| 150 | // composed of several parts, we match each part in turn (rather than |
| 151 | // building the entire thing in memory and performing a simple prefix match) |
Andreas Gampe | aa12001 | 2018-03-28 16:23:24 -0700 | [diff] [blame] | 152 | bool DoesPrefixMatch(const std::string& prefix) const; |
Mathew Inwood | 7d74ef5 | 2018-03-16 14:18:33 +0000 | [diff] [blame] | 153 | |
Andreas Gampe | aa12001 | 2018-03-28 16:23:24 -0700 | [diff] [blame] | 154 | bool IsExempted(const std::vector<std::string>& exemptions); |
Mathew Inwood | 7d74ef5 | 2018-03-16 14:18:33 +0000 | [diff] [blame] | 155 | |
David Brazdil | 47cd272 | 2018-10-23 12:50:02 +0100 | [diff] [blame] | 156 | void WarnAboutAccess(AccessMethod access_method, ApiList list); |
Mathew Inwood | 1fd97f2 | 2018-04-03 15:32:32 +0100 | [diff] [blame] | 157 | |
David Brazdil | f50ac10 | 2018-10-17 18:00:06 +0100 | [diff] [blame^] | 158 | void LogAccessToEventLog(AccessMethod access_method, bool access_denied); |
| 159 | |
| 160 | // Calls back into managed code to notify VMRuntime.nonSdkApiUsageConsumer that |
| 161 | // |member| was accessed. This is usually called when an API is on the black, |
| 162 | // dark grey or light grey lists. Given that the callback can execute arbitrary |
| 163 | // code, a call to this method can result in thread suspension. |
| 164 | void NotifyHiddenApiListener(AccessMethod access_method); |
Mathew Inwood | 7d74ef5 | 2018-03-16 14:18:33 +0000 | [diff] [blame] | 165 | }; |
David Brazdil | ee7d2fd | 2018-01-20 17:25:23 +0000 | [diff] [blame] | 166 | |
Andreas Gampe | aa12001 | 2018-03-28 16:23:24 -0700 | [diff] [blame] | 167 | template<typename T> |
David Brazdil | f50ac10 | 2018-10-17 18:00:06 +0100 | [diff] [blame^] | 168 | bool ShouldDenyAccessToMemberImpl(T* member, ApiList api_list, AccessMethod access_method) |
Andreas Gampe | aa12001 | 2018-03-28 16:23:24 -0700 | [diff] [blame] | 169 | REQUIRES_SHARED(Locks::mutator_lock_); |
| 170 | |
Andreas Gampe | aa12001 | 2018-03-28 16:23:24 -0700 | [diff] [blame] | 171 | } // namespace detail |
| 172 | |
David Brazdil | f50ac10 | 2018-10-17 18:00:06 +0100 | [diff] [blame^] | 173 | // Returns true if access to `member` should be denied in the given context. |
| 174 | // The decision is based on whether the caller is in a trusted context or not. |
| 175 | // Because determining the access context can be expensive, a lambda function |
| 176 | // "fn_get_access_context" is lazily invoked after other criteria have been |
| 177 | // considered. |
David Brazdil | 8ce3bfa | 2018-03-12 18:01:18 +0000 | [diff] [blame] | 178 | // 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] | 179 | template<typename T> |
David Brazdil | f50ac10 | 2018-10-17 18:00:06 +0100 | [diff] [blame^] | 180 | inline bool ShouldDenyAccessToMember(T* member, |
| 181 | std::function<AccessContext()> fn_get_access_context, |
| 182 | AccessMethod access_method) |
David Brazdil | ee7d2fd | 2018-01-20 17:25:23 +0000 | [diff] [blame] | 183 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 184 | DCHECK(member != nullptr); |
David Brazdil | ee7d2fd | 2018-01-20 17:25:23 +0000 | [diff] [blame] | 185 | |
David Brazdil | 166546c | 2018-04-23 13:50:38 +0100 | [diff] [blame] | 186 | // Decode hidden API access flags. |
| 187 | // NB Multiple threads might try to access (and overwrite) these simultaneously, |
| 188 | // causing a race. We only do that if access has not been denied, so the race |
| 189 | // cannot change Java semantics. We should, however, decode the access flags |
| 190 | // once and use it throughout this function, otherwise we may get inconsistent |
| 191 | // results, e.g. print whitelist warnings (b/78327881). |
David Brazdil | 47cd272 | 2018-10-23 12:50:02 +0100 | [diff] [blame] | 192 | ApiList api_list = member->GetHiddenApiAccessFlags(); |
David Brazdil | 166546c | 2018-04-23 13:50:38 +0100 | [diff] [blame] | 193 | |
David Brazdil | f50ac10 | 2018-10-17 18:00:06 +0100 | [diff] [blame^] | 194 | // Exit early if member is on the whitelist. |
| 195 | if (api_list == ApiList::kWhitelist) { |
| 196 | return false; |
David Brazdil | a02cb11 | 2018-01-31 11:36:39 +0000 | [diff] [blame] | 197 | } |
| 198 | |
David Brazdil | f50ac10 | 2018-10-17 18:00:06 +0100 | [diff] [blame^] | 199 | // Check if caller is exempted from access checks. |
David Brazdil | a02cb11 | 2018-01-31 11:36:39 +0000 | [diff] [blame] | 200 | // This can be *very* expensive. Save it for last. |
David Brazdil | f50ac10 | 2018-10-17 18:00:06 +0100 | [diff] [blame^] | 201 | if (fn_get_access_context().IsTrusted()) { |
| 202 | return false; |
David Brazdil | a02cb11 | 2018-01-31 11:36:39 +0000 | [diff] [blame] | 203 | } |
| 204 | |
David Brazdil | f50ac10 | 2018-10-17 18:00:06 +0100 | [diff] [blame^] | 205 | // Member is hidden and caller is not exempted. Enter slow path. |
| 206 | return detail::ShouldDenyAccessToMemberImpl(member, api_list, access_method); |
David Brazdil | 8e1a7cb | 2018-03-27 08:14:25 +0000 | [diff] [blame] | 207 | } |
| 208 | |
David Brazdil | f50ac10 | 2018-10-17 18:00:06 +0100 | [diff] [blame^] | 209 | // Helper method for callers where access context can be determined beforehand. |
| 210 | // Wraps AccessContext in a lambda and passes it to the real ShouldDenyAccessToMember. |
David Brazdil | 8ce3bfa | 2018-03-12 18:01:18 +0000 | [diff] [blame] | 211 | template<typename T> |
David Brazdil | f50ac10 | 2018-10-17 18:00:06 +0100 | [diff] [blame^] | 212 | inline bool ShouldDenyAccessToMember(T* member, |
| 213 | AccessContext access_context, |
| 214 | AccessMethod access_method) |
David Brazdil | ee7d2fd | 2018-01-20 17:25:23 +0000 | [diff] [blame] | 215 | REQUIRES_SHARED(Locks::mutator_lock_) { |
David Brazdil | f50ac10 | 2018-10-17 18:00:06 +0100 | [diff] [blame^] | 216 | return ShouldDenyAccessToMember( |
| 217 | member, |
| 218 | [&] () REQUIRES_SHARED(Locks::mutator_lock_) { return access_context; }, |
| 219 | access_method); |
David Brazdil | ee7d2fd | 2018-01-20 17:25:23 +0000 | [diff] [blame] | 220 | } |
| 221 | |
David Brazdil | 5a61bb7 | 2018-01-19 16:59:46 +0000 | [diff] [blame] | 222 | } // namespace hiddenapi |
| 223 | } // namespace art |
| 224 | |
| 225 | #endif // ART_RUNTIME_HIDDEN_API_H_ |