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