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 | 8586569 | 2018-10-30 17:26:20 +0000 | [diff] [blame] | 20 | #include "art_field.h" |
| 21 | #include "art_method.h" |
David Brazdil | dcfa89b | 2018-10-31 11:04:10 +0000 | [diff] [blame] | 22 | #include "base/hiddenapi_flags.h" |
Andreas Gampe | 7fbc4a5 | 2018-11-28 08:26:47 -0800 | [diff] [blame] | 23 | #include "base/locks.h" |
David Brazdil | 8586569 | 2018-10-30 17:26:20 +0000 | [diff] [blame] | 24 | #include "intrinsics_enum.h" |
David Brazdil | 8ce3bfa | 2018-03-12 18:01:18 +0000 | [diff] [blame] | 25 | #include "mirror/class-inl.h" |
David Brazdil | 5a61bb7 | 2018-01-19 16:59:46 +0000 | [diff] [blame] | 26 | #include "reflection.h" |
| 27 | #include "runtime.h" |
| 28 | |
| 29 | namespace art { |
| 30 | namespace hiddenapi { |
| 31 | |
Mathew Inwood | 597d7f6 | 2018-03-22 11:36:47 +0000 | [diff] [blame] | 32 | // Hidden API enforcement policy |
| 33 | // This must be kept in sync with ApplicationInfo.ApiEnforcementPolicy in |
| 34 | // frameworks/base/core/java/android/content/pm/ApplicationInfo.java |
| 35 | enum class EnforcementPolicy { |
David Brazdil | f50ac10 | 2018-10-17 18:00:06 +0100 | [diff] [blame] | 36 | kDisabled = 0, |
Mathew Inwood | a8503d9 | 2018-04-05 16:10:25 +0100 | [diff] [blame] | 37 | kJustWarn = 1, // keep checks enabled, but allow everything (enables logging) |
David Brazdil | f50ac10 | 2018-10-17 18:00:06 +0100 | [diff] [blame] | 38 | kEnabled = 2, // ban dark grey & blacklist |
| 39 | kMax = kEnabled, |
Mathew Inwood | 597d7f6 | 2018-03-22 11:36:47 +0000 | [diff] [blame] | 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 | |
Andrei Onea | a2d2bc2 | 2019-01-25 16:18:53 +0000 | [diff] [blame] | 48 | // Hidden API access method |
| 49 | // Thist must be kept in sync with VMRuntime.HiddenApiUsageLogger.ACCESS_METHOD_* |
David Brazdil | f50ac10 | 2018-10-17 18:00:06 +0100 | [diff] [blame] | 50 | enum class AccessMethod { |
Andrei Onea | a2d2bc2 | 2019-01-25 16:18:53 +0000 | [diff] [blame] | 51 | kNone = 0, // internal test that does not correspond to an actual access by app |
| 52 | kReflection = 1, |
| 53 | kJNI = 2, |
| 54 | kLinking = 3, |
Mathew Inwood | 1fd97f2 | 2018-04-03 15:32:32 +0100 | [diff] [blame] | 55 | }; |
| 56 | |
David Brazdil | e768182 | 2018-12-14 16:25:33 +0000 | [diff] [blame] | 57 | // Represents the API domain of a caller/callee. |
| 58 | class AccessContext { |
David Brazdil | f50ac10 | 2018-10-17 18:00:06 +0100 | [diff] [blame] | 59 | public: |
David Brazdil | e768182 | 2018-12-14 16:25:33 +0000 | [diff] [blame] | 60 | // Initialize to either the fully-trusted or fully-untrusted domain. |
| 61 | explicit AccessContext(bool is_trusted) |
| 62 | : klass_(nullptr), |
| 63 | dex_file_(nullptr), |
| 64 | domain_(ComputeDomain(is_trusted)) {} |
David Brazdil | f50ac10 | 2018-10-17 18:00:06 +0100 | [diff] [blame] | 65 | |
David Brazdil | e768182 | 2018-12-14 16:25:33 +0000 | [diff] [blame] | 66 | // Initialize from class loader and dex file (via dex cache). |
David Brazdil | f50ac10 | 2018-10-17 18:00:06 +0100 | [diff] [blame] | 67 | AccessContext(ObjPtr<mirror::ClassLoader> class_loader, ObjPtr<mirror::DexCache> dex_cache) |
David Brazdil | e768182 | 2018-12-14 16:25:33 +0000 | [diff] [blame] | 68 | REQUIRES_SHARED(Locks::mutator_lock_) |
| 69 | : klass_(nullptr), |
| 70 | dex_file_(GetDexFileFromDexCache(dex_cache)), |
| 71 | domain_(ComputeDomain(class_loader, dex_file_)) {} |
David Brazdil | f50ac10 | 2018-10-17 18:00:06 +0100 | [diff] [blame] | 72 | |
David Brazdil | e768182 | 2018-12-14 16:25:33 +0000 | [diff] [blame] | 73 | // Initialize from Class. |
| 74 | explicit AccessContext(ObjPtr<mirror::Class> klass) |
| 75 | REQUIRES_SHARED(Locks::mutator_lock_) |
| 76 | : klass_(klass), |
| 77 | dex_file_(GetDexFileFromDexCache(klass->GetDexCache())), |
| 78 | domain_(ComputeDomain(klass, dex_file_)) {} |
| 79 | |
| 80 | ObjPtr<mirror::Class> GetClass() const { return klass_; } |
| 81 | const DexFile* GetDexFile() const { return dex_file_; } |
| 82 | Domain GetDomain() const { return domain_; } |
David Brazdil | d51e574 | 2019-02-28 14:47:32 +0000 | [diff] [blame^] | 83 | bool IsApplicationDomain() const { return domain_ == Domain::kApplication; } |
David Brazdil | e768182 | 2018-12-14 16:25:33 +0000 | [diff] [blame] | 84 | |
| 85 | // Returns true if this domain is always allowed to access the domain of `callee`. |
| 86 | bool CanAlwaysAccess(const AccessContext& callee) const { |
| 87 | return IsDomainMoreTrustedThan(domain_, callee.domain_); |
| 88 | } |
David Brazdil | f50ac10 | 2018-10-17 18:00:06 +0100 | [diff] [blame] | 89 | |
| 90 | private: |
David Brazdil | e768182 | 2018-12-14 16:25:33 +0000 | [diff] [blame] | 91 | static const DexFile* GetDexFileFromDexCache(ObjPtr<mirror::DexCache> dex_cache) |
David Brazdil | f50ac10 | 2018-10-17 18:00:06 +0100 | [diff] [blame] | 92 | REQUIRES_SHARED(Locks::mutator_lock_) { |
David Brazdil | e768182 | 2018-12-14 16:25:33 +0000 | [diff] [blame] | 93 | return dex_cache.IsNull() ? nullptr : dex_cache->GetDexFile(); |
David Brazdil | f50ac10 | 2018-10-17 18:00:06 +0100 | [diff] [blame] | 94 | } |
| 95 | |
David Brazdil | e768182 | 2018-12-14 16:25:33 +0000 | [diff] [blame] | 96 | static Domain ComputeDomain(bool is_trusted) { |
| 97 | return is_trusted ? Domain::kCorePlatform : Domain::kApplication; |
| 98 | } |
David Brazdil | f50ac10 | 2018-10-17 18:00:06 +0100 | [diff] [blame] | 99 | |
David Brazdil | e768182 | 2018-12-14 16:25:33 +0000 | [diff] [blame] | 100 | static Domain ComputeDomain(ObjPtr<mirror::ClassLoader> class_loader, const DexFile* dex_file) |
| 101 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 102 | if (dex_file == nullptr) { |
| 103 | return ComputeDomain(/* is_trusted= */ class_loader.IsNull()); |
David Brazdil | f50ac10 | 2018-10-17 18:00:06 +0100 | [diff] [blame] | 104 | } |
| 105 | |
David Brazdil | e768182 | 2018-12-14 16:25:33 +0000 | [diff] [blame] | 106 | Domain dex_domain = dex_file->GetHiddenapiDomain(); |
| 107 | if (class_loader.IsNull() && dex_domain == Domain::kApplication) { |
David Brazdil | e7e26d1 | 2019-02-28 15:04:14 +0000 | [diff] [blame] | 108 | LOG(WARNING) << "DexFile " << dex_file->GetLocation() |
| 109 | << " is in boot classpath but is assigned the application domain"; |
| 110 | dex_file->SetHiddenapiDomain(Domain::kPlatform); |
David Brazdil | e768182 | 2018-12-14 16:25:33 +0000 | [diff] [blame] | 111 | dex_domain = Domain::kPlatform; |
| 112 | } |
| 113 | return dex_domain; |
| 114 | } |
| 115 | |
| 116 | static Domain ComputeDomain(ObjPtr<mirror::Class> klass, const DexFile* dex_file) |
| 117 | REQUIRES_SHARED(Locks::mutator_lock_) { |
David Brazdil | f50ac10 | 2018-10-17 18:00:06 +0100 | [diff] [blame] | 118 | // Check other aspects of the context. |
David Brazdil | e768182 | 2018-12-14 16:25:33 +0000 | [diff] [blame] | 119 | Domain domain = ComputeDomain(klass->GetClassLoader(), dex_file); |
| 120 | |
| 121 | if (domain == Domain::kApplication && |
| 122 | klass->ShouldSkipHiddenApiChecks() && |
| 123 | Runtime::Current()->IsJavaDebuggable()) { |
| 124 | // Class is known, it is marked trusted and we are in debuggable mode. |
| 125 | domain = ComputeDomain(/* is_trusted= */ true); |
| 126 | } |
| 127 | |
| 128 | return domain; |
David Brazdil | f50ac10 | 2018-10-17 18:00:06 +0100 | [diff] [blame] | 129 | } |
| 130 | |
David Brazdil | e768182 | 2018-12-14 16:25:33 +0000 | [diff] [blame] | 131 | // Pointer to declaring class of the caller/callee (null if not provided). |
| 132 | // This is not safe across GC but we're only using this class for passing |
| 133 | // information about the caller to the access check logic and never retain |
| 134 | // the AccessContext instance beyond that. |
| 135 | const ObjPtr<mirror::Class> klass_; |
| 136 | |
| 137 | // DexFile of the caller/callee (null if not provided). |
| 138 | const DexFile* const dex_file_; |
| 139 | |
| 140 | // Computed domain of the caller/callee. |
| 141 | const Domain domain_; |
David Brazdil | 068d68d | 2018-02-12 13:04:17 -0800 | [diff] [blame] | 142 | }; |
| 143 | |
David Brazdil | 32bde99 | 2018-05-14 15:24:34 +0100 | [diff] [blame] | 144 | class ScopedHiddenApiEnforcementPolicySetting { |
| 145 | public: |
| 146 | explicit ScopedHiddenApiEnforcementPolicySetting(EnforcementPolicy new_policy) |
| 147 | : initial_policy_(Runtime::Current()->GetHiddenApiEnforcementPolicy()) { |
| 148 | Runtime::Current()->SetHiddenApiEnforcementPolicy(new_policy); |
| 149 | } |
| 150 | |
| 151 | ~ScopedHiddenApiEnforcementPolicySetting() { |
| 152 | Runtime::Current()->SetHiddenApiEnforcementPolicy(initial_policy_); |
| 153 | } |
| 154 | |
| 155 | private: |
| 156 | const EnforcementPolicy initial_policy_; |
| 157 | DISALLOW_COPY_AND_ASSIGN(ScopedHiddenApiEnforcementPolicySetting); |
| 158 | }; |
| 159 | |
Andreas Gampe | aa12001 | 2018-03-28 16:23:24 -0700 | [diff] [blame] | 160 | // Implementation details. DO NOT ACCESS DIRECTLY. |
| 161 | namespace detail { |
David Brazdil | ee7d2fd | 2018-01-20 17:25:23 +0000 | [diff] [blame] | 162 | |
Mathew Inwood | 7d74ef5 | 2018-03-16 14:18:33 +0000 | [diff] [blame] | 163 | // Class to encapsulate the signature of a member (ArtField or ArtMethod). This |
| 164 | // is used as a helper when matching prefixes, and when logging the signature. |
| 165 | class MemberSignature { |
| 166 | private: |
Mathew Inwood | 1fd97f2 | 2018-04-03 15:32:32 +0100 | [diff] [blame] | 167 | enum MemberType { |
| 168 | kField, |
| 169 | kMethod, |
| 170 | }; |
| 171 | |
| 172 | std::string class_name_; |
| 173 | std::string member_name_; |
| 174 | std::string type_signature_; |
Mathew Inwood | 7d74ef5 | 2018-03-16 14:18:33 +0000 | [diff] [blame] | 175 | std::string tmp_; |
Mathew Inwood | 1fd97f2 | 2018-04-03 15:32:32 +0100 | [diff] [blame] | 176 | MemberType type_; |
| 177 | |
| 178 | inline std::vector<const char*> GetSignatureParts() const; |
Mathew Inwood | 7d74ef5 | 2018-03-16 14:18:33 +0000 | [diff] [blame] | 179 | |
| 180 | public: |
Andreas Gampe | aa12001 | 2018-03-28 16:23:24 -0700 | [diff] [blame] | 181 | explicit MemberSignature(ArtField* field) REQUIRES_SHARED(Locks::mutator_lock_); |
| 182 | explicit MemberSignature(ArtMethod* method) REQUIRES_SHARED(Locks::mutator_lock_); |
David Brazdil | 1a65863 | 2018-12-01 17:54:26 +0000 | [diff] [blame] | 183 | explicit MemberSignature(const ClassAccessor::Field& field); |
| 184 | explicit MemberSignature(const ClassAccessor::Method& method); |
Mathew Inwood | 7d74ef5 | 2018-03-16 14:18:33 +0000 | [diff] [blame] | 185 | |
Andreas Gampe | aa12001 | 2018-03-28 16:23:24 -0700 | [diff] [blame] | 186 | void Dump(std::ostream& os) const; |
Mathew Inwood | 7d74ef5 | 2018-03-16 14:18:33 +0000 | [diff] [blame] | 187 | |
David Brazdil | 1a65863 | 2018-12-01 17:54:26 +0000 | [diff] [blame] | 188 | bool Equals(const MemberSignature& other); |
| 189 | bool MemberNameAndTypeMatch(const MemberSignature& other); |
| 190 | |
Mathew Inwood | 7d74ef5 | 2018-03-16 14:18:33 +0000 | [diff] [blame] | 191 | // Performs prefix match on this member. Since the full member signature is |
| 192 | // composed of several parts, we match each part in turn (rather than |
| 193 | // building the entire thing in memory and performing a simple prefix match) |
Andreas Gampe | aa12001 | 2018-03-28 16:23:24 -0700 | [diff] [blame] | 194 | bool DoesPrefixMatch(const std::string& prefix) const; |
Mathew Inwood | 7d74ef5 | 2018-03-16 14:18:33 +0000 | [diff] [blame] | 195 | |
Andreas Gampe | aa12001 | 2018-03-28 16:23:24 -0700 | [diff] [blame] | 196 | bool IsExempted(const std::vector<std::string>& exemptions); |
Mathew Inwood | 7d74ef5 | 2018-03-16 14:18:33 +0000 | [diff] [blame] | 197 | |
David Brazdil | 47cd272 | 2018-10-23 12:50:02 +0100 | [diff] [blame] | 198 | void WarnAboutAccess(AccessMethod access_method, ApiList list); |
Mathew Inwood | 1fd97f2 | 2018-04-03 15:32:32 +0100 | [diff] [blame] | 199 | |
Andrei Onea | 6ad020d | 2019-02-18 12:15:51 +0000 | [diff] [blame] | 200 | void LogAccessToEventLog(uint32_t sampled_value, AccessMethod access_method, bool access_denied); |
David Brazdil | f50ac10 | 2018-10-17 18:00:06 +0100 | [diff] [blame] | 201 | |
| 202 | // Calls back into managed code to notify VMRuntime.nonSdkApiUsageConsumer that |
| 203 | // |member| was accessed. This is usually called when an API is on the black, |
| 204 | // dark grey or light grey lists. Given that the callback can execute arbitrary |
| 205 | // code, a call to this method can result in thread suspension. |
| 206 | void NotifyHiddenApiListener(AccessMethod access_method); |
Mathew Inwood | 7d74ef5 | 2018-03-16 14:18:33 +0000 | [diff] [blame] | 207 | }; |
David Brazdil | ee7d2fd | 2018-01-20 17:25:23 +0000 | [diff] [blame] | 208 | |
David Brazdil | 8586569 | 2018-10-30 17:26:20 +0000 | [diff] [blame] | 209 | // Locates hiddenapi flags for `field` in the corresponding dex file. |
| 210 | // NB: This is an O(N) operation, linear with the number of members in the class def. |
David Brazdil | 1a65863 | 2018-12-01 17:54:26 +0000 | [diff] [blame] | 211 | template<typename T> |
| 212 | uint32_t GetDexFlags(T* member) REQUIRES_SHARED(Locks::mutator_lock_); |
David Brazdil | 8586569 | 2018-10-30 17:26:20 +0000 | [diff] [blame] | 213 | |
David Brazdil | d51e574 | 2019-02-28 14:47:32 +0000 | [diff] [blame^] | 214 | // Handler of detected core platform API violations. Returns true if access to |
| 215 | // `member` should be denied. |
Andreas Gampe | aa12001 | 2018-03-28 16:23:24 -0700 | [diff] [blame] | 216 | template<typename T> |
David Brazdil | d51e574 | 2019-02-28 14:47:32 +0000 | [diff] [blame^] | 217 | bool HandleCorePlatformApiViolation(T* member, |
| 218 | const AccessContext& caller_context, |
| 219 | AccessMethod access_method, |
| 220 | EnforcementPolicy policy) |
David Brazdil | e768182 | 2018-12-14 16:25:33 +0000 | [diff] [blame] | 221 | REQUIRES_SHARED(Locks::mutator_lock_); |
| 222 | |
| 223 | template<typename T> |
David Brazdil | f50ac10 | 2018-10-17 18:00:06 +0100 | [diff] [blame] | 224 | bool ShouldDenyAccessToMemberImpl(T* member, ApiList api_list, AccessMethod access_method) |
Andreas Gampe | aa12001 | 2018-03-28 16:23:24 -0700 | [diff] [blame] | 225 | REQUIRES_SHARED(Locks::mutator_lock_); |
| 226 | |
David Brazdil | 6a1dab4 | 2019-02-28 18:45:15 +0000 | [diff] [blame] | 227 | inline ArtField* GetInterfaceMemberIfProxy(ArtField* field) { return field; } |
| 228 | |
| 229 | inline ArtMethod* GetInterfaceMemberIfProxy(ArtMethod* method) |
| 230 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 231 | return method->GetInterfaceMethodIfProxy(kRuntimePointerSize); |
| 232 | } |
| 233 | |
Andreas Gampe | aa12001 | 2018-03-28 16:23:24 -0700 | [diff] [blame] | 234 | } // namespace detail |
| 235 | |
David Brazdil | 8586569 | 2018-10-30 17:26:20 +0000 | [diff] [blame] | 236 | // Returns access flags for the runtime representation of a class member (ArtField/ArtMember). |
| 237 | ALWAYS_INLINE inline uint32_t CreateRuntimeFlags(const ClassAccessor::BaseItem& member) { |
| 238 | uint32_t runtime_flags = 0u; |
| 239 | |
David Brazdil | 90faceb | 2018-12-14 14:36:15 +0000 | [diff] [blame] | 240 | ApiList api_list(member.GetHiddenapiFlags()); |
| 241 | DCHECK(api_list.IsValid()); |
David Brazdil | 8586569 | 2018-10-30 17:26:20 +0000 | [diff] [blame] | 242 | |
David Brazdil | 90faceb | 2018-12-14 14:36:15 +0000 | [diff] [blame] | 243 | if (api_list.Contains(ApiList::Whitelist())) { |
David Brazdil | 8586569 | 2018-10-30 17:26:20 +0000 | [diff] [blame] | 244 | runtime_flags |= kAccPublicApi; |
David Brazdil | 90faceb | 2018-12-14 14:36:15 +0000 | [diff] [blame] | 245 | } else { |
| 246 | // Only add domain-specific flags for non-public API members. |
| 247 | // This simplifies hardcoded values for intrinsics. |
| 248 | if (api_list.Contains(ApiList::CorePlatformApi())) { |
| 249 | runtime_flags |= kAccCorePlatformApi; |
| 250 | } |
David Brazdil | 8586569 | 2018-10-30 17:26:20 +0000 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | DCHECK_EQ(runtime_flags & kAccHiddenapiBits, runtime_flags) |
| 254 | << "Runtime flags not in reserved access flags bits"; |
| 255 | return runtime_flags; |
| 256 | } |
| 257 | |
| 258 | // Extracts hiddenapi runtime flags from access flags of ArtField. |
| 259 | ALWAYS_INLINE inline uint32_t GetRuntimeFlags(ArtField* field) |
| 260 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 261 | return field->GetAccessFlags() & kAccHiddenapiBits; |
| 262 | } |
| 263 | |
| 264 | // Extracts hiddenapi runtime flags from access flags of ArtMethod. |
| 265 | // Uses hardcoded values for intrinsics. |
| 266 | ALWAYS_INLINE inline uint32_t GetRuntimeFlags(ArtMethod* method) |
| 267 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 268 | if (UNLIKELY(method->IsIntrinsic())) { |
| 269 | switch (static_cast<Intrinsics>(method->GetIntrinsic())) { |
| 270 | case Intrinsics::kSystemArrayCopyChar: |
| 271 | case Intrinsics::kStringGetCharsNoCheck: |
| 272 | case Intrinsics::kReferenceGetReferent: |
| 273 | case Intrinsics::kMemoryPeekByte: |
| 274 | case Intrinsics::kMemoryPokeByte: |
| 275 | case Intrinsics::kUnsafeCASInt: |
| 276 | case Intrinsics::kUnsafeCASLong: |
| 277 | case Intrinsics::kUnsafeCASObject: |
| 278 | case Intrinsics::kUnsafeGet: |
| 279 | case Intrinsics::kUnsafeGetAndAddInt: |
| 280 | case Intrinsics::kUnsafeGetAndAddLong: |
| 281 | case Intrinsics::kUnsafeGetAndSetInt: |
| 282 | case Intrinsics::kUnsafeGetAndSetLong: |
| 283 | case Intrinsics::kUnsafeGetAndSetObject: |
| 284 | case Intrinsics::kUnsafeGetLong: |
| 285 | case Intrinsics::kUnsafeGetLongVolatile: |
| 286 | case Intrinsics::kUnsafeGetObject: |
| 287 | case Intrinsics::kUnsafeGetObjectVolatile: |
| 288 | case Intrinsics::kUnsafeGetVolatile: |
| 289 | case Intrinsics::kUnsafePut: |
| 290 | case Intrinsics::kUnsafePutLong: |
| 291 | case Intrinsics::kUnsafePutLongOrdered: |
| 292 | case Intrinsics::kUnsafePutLongVolatile: |
| 293 | case Intrinsics::kUnsafePutObject: |
| 294 | case Intrinsics::kUnsafePutObjectOrdered: |
| 295 | case Intrinsics::kUnsafePutObjectVolatile: |
| 296 | case Intrinsics::kUnsafePutOrdered: |
| 297 | case Intrinsics::kUnsafePutVolatile: |
| 298 | case Intrinsics::kUnsafeLoadFence: |
| 299 | case Intrinsics::kUnsafeStoreFence: |
| 300 | case Intrinsics::kUnsafeFullFence: |
| 301 | case Intrinsics::kCRC32Update: |
Evgeny Astigeevich | 15c5b97 | 2018-11-20 13:41:40 +0000 | [diff] [blame] | 302 | case Intrinsics::kCRC32UpdateBytes: |
Evgeny Astigeevich | 776a7c2 | 2018-12-17 11:40:34 +0000 | [diff] [blame] | 303 | case Intrinsics::kCRC32UpdateByteBuffer: |
David Brazdil | 8586569 | 2018-10-30 17:26:20 +0000 | [diff] [blame] | 304 | case Intrinsics::kStringNewStringFromBytes: |
| 305 | case Intrinsics::kStringNewStringFromChars: |
| 306 | case Intrinsics::kStringNewStringFromString: |
| 307 | case Intrinsics::kMemoryPeekIntNative: |
| 308 | case Intrinsics::kMemoryPeekLongNative: |
| 309 | case Intrinsics::kMemoryPeekShortNative: |
| 310 | case Intrinsics::kMemoryPokeIntNative: |
| 311 | case Intrinsics::kMemoryPokeLongNative: |
| 312 | case Intrinsics::kMemoryPokeShortNative: |
| 313 | case Intrinsics::kVarHandleFullFence: |
| 314 | case Intrinsics::kVarHandleAcquireFence: |
| 315 | case Intrinsics::kVarHandleReleaseFence: |
| 316 | case Intrinsics::kVarHandleLoadLoadFence: |
| 317 | case Intrinsics::kVarHandleStoreStoreFence: |
| 318 | case Intrinsics::kVarHandleCompareAndExchange: |
| 319 | case Intrinsics::kVarHandleCompareAndExchangeAcquire: |
| 320 | case Intrinsics::kVarHandleCompareAndExchangeRelease: |
| 321 | case Intrinsics::kVarHandleCompareAndSet: |
| 322 | case Intrinsics::kVarHandleGet: |
| 323 | case Intrinsics::kVarHandleGetAcquire: |
| 324 | case Intrinsics::kVarHandleGetAndAdd: |
| 325 | case Intrinsics::kVarHandleGetAndAddAcquire: |
| 326 | case Intrinsics::kVarHandleGetAndAddRelease: |
| 327 | case Intrinsics::kVarHandleGetAndBitwiseAnd: |
| 328 | case Intrinsics::kVarHandleGetAndBitwiseAndAcquire: |
| 329 | case Intrinsics::kVarHandleGetAndBitwiseAndRelease: |
| 330 | case Intrinsics::kVarHandleGetAndBitwiseOr: |
| 331 | case Intrinsics::kVarHandleGetAndBitwiseOrAcquire: |
| 332 | case Intrinsics::kVarHandleGetAndBitwiseOrRelease: |
| 333 | case Intrinsics::kVarHandleGetAndBitwiseXor: |
| 334 | case Intrinsics::kVarHandleGetAndBitwiseXorAcquire: |
| 335 | case Intrinsics::kVarHandleGetAndBitwiseXorRelease: |
| 336 | case Intrinsics::kVarHandleGetAndSet: |
| 337 | case Intrinsics::kVarHandleGetAndSetAcquire: |
| 338 | case Intrinsics::kVarHandleGetAndSetRelease: |
| 339 | case Intrinsics::kVarHandleGetOpaque: |
| 340 | case Intrinsics::kVarHandleGetVolatile: |
| 341 | case Intrinsics::kVarHandleSet: |
| 342 | case Intrinsics::kVarHandleSetOpaque: |
| 343 | case Intrinsics::kVarHandleSetRelease: |
| 344 | case Intrinsics::kVarHandleSetVolatile: |
| 345 | case Intrinsics::kVarHandleWeakCompareAndSet: |
| 346 | case Intrinsics::kVarHandleWeakCompareAndSetAcquire: |
| 347 | case Intrinsics::kVarHandleWeakCompareAndSetPlain: |
| 348 | case Intrinsics::kVarHandleWeakCompareAndSetRelease: |
| 349 | return 0u; |
| 350 | default: |
| 351 | // Remaining intrinsics are public API. We DCHECK that in SetIntrinsic(). |
| 352 | return kAccPublicApi; |
| 353 | } |
| 354 | } else { |
| 355 | return method->GetAccessFlags() & kAccHiddenapiBits; |
| 356 | } |
| 357 | } |
| 358 | |
David Brazdil | f50ac10 | 2018-10-17 18:00:06 +0100 | [diff] [blame] | 359 | // Returns true if access to `member` should be denied in the given context. |
| 360 | // The decision is based on whether the caller is in a trusted context or not. |
| 361 | // Because determining the access context can be expensive, a lambda function |
| 362 | // "fn_get_access_context" is lazily invoked after other criteria have been |
| 363 | // considered. |
David Brazdil | 8ce3bfa | 2018-03-12 18:01:18 +0000 | [diff] [blame] | 364 | // 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] | 365 | template<typename T> |
David Brazdil | f50ac10 | 2018-10-17 18:00:06 +0100 | [diff] [blame] | 366 | inline bool ShouldDenyAccessToMember(T* member, |
David Brazdil | 4bcd657 | 2019-02-02 20:08:44 +0000 | [diff] [blame] | 367 | const std::function<AccessContext()>& fn_get_access_context, |
David Brazdil | f50ac10 | 2018-10-17 18:00:06 +0100 | [diff] [blame] | 368 | AccessMethod access_method) |
David Brazdil | ee7d2fd | 2018-01-20 17:25:23 +0000 | [diff] [blame] | 369 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 370 | DCHECK(member != nullptr); |
David Brazdil | 6a1dab4 | 2019-02-28 18:45:15 +0000 | [diff] [blame] | 371 | |
| 372 | // Get the runtime flags encoded in member's access flags. |
| 373 | // Note: this works for proxy methods because they inherit access flags from their |
| 374 | // respective interface methods. |
David Brazdil | e768182 | 2018-12-14 16:25:33 +0000 | [diff] [blame] | 375 | const uint32_t runtime_flags = GetRuntimeFlags(member); |
David Brazdil | ee7d2fd | 2018-01-20 17:25:23 +0000 | [diff] [blame] | 376 | |
David Brazdil | 8586569 | 2018-10-30 17:26:20 +0000 | [diff] [blame] | 377 | // Exit early if member is public API. This flag is also set for non-boot class |
| 378 | // path fields/methods. |
David Brazdil | e768182 | 2018-12-14 16:25:33 +0000 | [diff] [blame] | 379 | if ((runtime_flags & kAccPublicApi) != 0) { |
David Brazdil | f50ac10 | 2018-10-17 18:00:06 +0100 | [diff] [blame] | 380 | return false; |
David Brazdil | a02cb11 | 2018-01-31 11:36:39 +0000 | [diff] [blame] | 381 | } |
| 382 | |
David Brazdil | e768182 | 2018-12-14 16:25:33 +0000 | [diff] [blame] | 383 | // Determine which domain the caller and callee belong to. |
| 384 | // This can be *very* expensive. This is why ShouldDenyAccessToMember |
| 385 | // should not be called on every individual access. |
| 386 | const AccessContext caller_context = fn_get_access_context(); |
| 387 | const AccessContext callee_context(member->GetDeclaringClass()); |
| 388 | |
| 389 | // Non-boot classpath callers should have exited early. |
David Brazdil | d51e574 | 2019-02-28 14:47:32 +0000 | [diff] [blame^] | 390 | DCHECK(!callee_context.IsApplicationDomain()); |
David Brazdil | e768182 | 2018-12-14 16:25:33 +0000 | [diff] [blame] | 391 | |
| 392 | // Check if the caller is always allowed to access members in the callee context. |
| 393 | if (caller_context.CanAlwaysAccess(callee_context)) { |
David Brazdil | c5a96e4 | 2019-01-09 10:04:45 +0000 | [diff] [blame] | 394 | return false; |
| 395 | } |
| 396 | |
David Brazdil | e768182 | 2018-12-14 16:25:33 +0000 | [diff] [blame] | 397 | // Check if this is platform accessing core platform. We may warn if `member` is |
| 398 | // not part of core platform API. |
| 399 | switch (caller_context.GetDomain()) { |
| 400 | case Domain::kApplication: { |
David Brazdil | d51e574 | 2019-02-28 14:47:32 +0000 | [diff] [blame^] | 401 | DCHECK(!callee_context.IsApplicationDomain()); |
David Brazdil | e768182 | 2018-12-14 16:25:33 +0000 | [diff] [blame] | 402 | |
| 403 | // Exit early if access checks are completely disabled. |
| 404 | EnforcementPolicy policy = Runtime::Current()->GetHiddenApiEnforcementPolicy(); |
| 405 | if (policy == EnforcementPolicy::kDisabled) { |
| 406 | return false; |
| 407 | } |
| 408 | |
David Brazdil | 6a1dab4 | 2019-02-28 18:45:15 +0000 | [diff] [blame] | 409 | // If this is a proxy method, look at the interface method instead. |
| 410 | member = detail::GetInterfaceMemberIfProxy(member); |
| 411 | |
David Brazdil | e768182 | 2018-12-14 16:25:33 +0000 | [diff] [blame] | 412 | // Decode hidden API access flags from the dex file. |
| 413 | // This is an O(N) operation scaling with the number of fields/methods |
| 414 | // in the class. Only do this on slow path and only do it once. |
| 415 | ApiList api_list(detail::GetDexFlags(member)); |
| 416 | DCHECK(api_list.IsValid()); |
| 417 | |
| 418 | // Member is hidden and caller is not exempted. Enter slow path. |
| 419 | return detail::ShouldDenyAccessToMemberImpl(member, api_list, access_method); |
| 420 | } |
| 421 | |
| 422 | case Domain::kPlatform: { |
| 423 | DCHECK(callee_context.GetDomain() == Domain::kCorePlatform); |
| 424 | |
| 425 | // Member is part of core platform API. Accessing it is allowed. |
| 426 | if ((runtime_flags & kAccCorePlatformApi) != 0) { |
| 427 | return false; |
| 428 | } |
| 429 | |
| 430 | // Allow access if access checks are disabled. |
| 431 | EnforcementPolicy policy = Runtime::Current()->GetCorePlatformApiEnforcementPolicy(); |
| 432 | if (policy == EnforcementPolicy::kDisabled) { |
| 433 | return false; |
| 434 | } |
| 435 | |
David Brazdil | 6a1dab4 | 2019-02-28 18:45:15 +0000 | [diff] [blame] | 436 | // If this is a proxy method, look at the interface method instead. |
| 437 | member = detail::GetInterfaceMemberIfProxy(member); |
| 438 | |
David Brazdil | e768182 | 2018-12-14 16:25:33 +0000 | [diff] [blame] | 439 | // Access checks are not disabled, report the violation. |
David Brazdil | d51e574 | 2019-02-28 14:47:32 +0000 | [diff] [blame^] | 440 | // This may also add kAccCorePlatformApi to the access flags of `member` |
| 441 | // so as to not warn again on next access. |
| 442 | return detail::HandleCorePlatformApiViolation(member, |
| 443 | caller_context, |
| 444 | access_method, |
| 445 | policy); |
David Brazdil | e768182 | 2018-12-14 16:25:33 +0000 | [diff] [blame] | 446 | } |
| 447 | |
| 448 | case Domain::kCorePlatform: { |
| 449 | LOG(FATAL) << "CorePlatform domain should be allowed to access all domains"; |
| 450 | UNREACHABLE(); |
| 451 | } |
David Brazdil | a02cb11 | 2018-01-31 11:36:39 +0000 | [diff] [blame] | 452 | } |
David Brazdil | 8e1a7cb | 2018-03-27 08:14:25 +0000 | [diff] [blame] | 453 | } |
| 454 | |
David Brazdil | f50ac10 | 2018-10-17 18:00:06 +0100 | [diff] [blame] | 455 | // Helper method for callers where access context can be determined beforehand. |
| 456 | // Wraps AccessContext in a lambda and passes it to the real ShouldDenyAccessToMember. |
David Brazdil | 8ce3bfa | 2018-03-12 18:01:18 +0000 | [diff] [blame] | 457 | template<typename T> |
David Brazdil | f50ac10 | 2018-10-17 18:00:06 +0100 | [diff] [blame] | 458 | inline bool ShouldDenyAccessToMember(T* member, |
David Brazdil | e768182 | 2018-12-14 16:25:33 +0000 | [diff] [blame] | 459 | const AccessContext& access_context, |
David Brazdil | f50ac10 | 2018-10-17 18:00:06 +0100 | [diff] [blame] | 460 | AccessMethod access_method) |
David Brazdil | ee7d2fd | 2018-01-20 17:25:23 +0000 | [diff] [blame] | 461 | REQUIRES_SHARED(Locks::mutator_lock_) { |
David Brazdil | e768182 | 2018-12-14 16:25:33 +0000 | [diff] [blame] | 462 | return ShouldDenyAccessToMember(member, [&]() { return access_context; }, access_method); |
David Brazdil | ee7d2fd | 2018-01-20 17:25:23 +0000 | [diff] [blame] | 463 | } |
| 464 | |
David Brazdil | 5a61bb7 | 2018-01-19 16:59:46 +0000 | [diff] [blame] | 465 | } // namespace hiddenapi |
| 466 | } // namespace art |
| 467 | |
| 468 | #endif // ART_RUNTIME_HIDDEN_API_H_ |