Andreas Gampe | 80f5fe5 | 2018-03-28 16:23:24 -0700 | [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 | #include "hidden_api.h" |
| 18 | |
Narayan Kamath | e453a8d | 2018-04-03 15:23:46 +0100 | [diff] [blame] | 19 | #include <nativehelper/scoped_local_ref.h> |
Andrei Onea | 3098e36 | 2021-01-26 18:30:54 +0000 | [diff] [blame] | 20 | #include <atomic> |
Narayan Kamath | e453a8d | 2018-04-03 15:23:46 +0100 | [diff] [blame] | 21 | |
David Brazdil | 8586569 | 2018-10-30 17:26:20 +0000 | [diff] [blame] | 22 | #include "art_field-inl.h" |
| 23 | #include "art_method-inl.h" |
Sorin Basca | 633ac3c | 2021-10-06 14:00:29 +0000 | [diff] [blame] | 24 | #include "class_root-inl.h" |
Andrei Onea | 037d282 | 2020-11-19 00:20:04 +0000 | [diff] [blame] | 25 | #include "compat_framework.h" |
Andreas Gampe | 80f5fe5 | 2018-03-28 16:23:24 -0700 | [diff] [blame] | 26 | #include "base/dumpable.h" |
David Brazdil | a5c3a80 | 2019-03-08 14:59:41 +0000 | [diff] [blame] | 27 | #include "base/file_utils.h" |
David Brazdil | 8586569 | 2018-10-30 17:26:20 +0000 | [diff] [blame] | 28 | #include "dex/class_accessor-inl.h" |
David Brazdil | 1a65863 | 2018-12-01 17:54:26 +0000 | [diff] [blame] | 29 | #include "dex/dex_file_loader.h" |
| 30 | #include "mirror/class_ext.h" |
Sorin Basca | 633ac3c | 2021-10-06 14:00:29 +0000 | [diff] [blame] | 31 | #include "mirror/proxy.h" |
David Brazdil | a5c3a80 | 2019-03-08 14:59:41 +0000 | [diff] [blame] | 32 | #include "oat_file.h" |
David Brazdil | 8586569 | 2018-10-30 17:26:20 +0000 | [diff] [blame] | 33 | #include "scoped_thread_state_change.h" |
Sorin Basca | 633ac3c | 2021-10-06 14:00:29 +0000 | [diff] [blame] | 34 | #include "stack.h" |
David Brazdil | 8586569 | 2018-10-30 17:26:20 +0000 | [diff] [blame] | 35 | #include "thread-inl.h" |
Narayan Kamath | e453a8d | 2018-04-03 15:23:46 +0100 | [diff] [blame] | 36 | #include "well_known_classes.h" |
Andreas Gampe | 80f5fe5 | 2018-03-28 16:23:24 -0700 | [diff] [blame] | 37 | |
| 38 | namespace art { |
| 39 | namespace hiddenapi { |
| 40 | |
Pedro Loureiro | f7fcb98 | 2020-11-24 16:44:12 +0000 | [diff] [blame] | 41 | // Should be the same as dalvik.system.VMRuntime.HIDE_MAXTARGETSDK_P_HIDDEN_APIS, |
| 42 | // dalvik.system.VMRuntime.HIDE_MAXTARGETSDK_Q_HIDDEN_APIS, and |
Pedro Loureiro | 2807839 | 2021-01-21 17:09:55 +0000 | [diff] [blame] | 43 | // dalvik.system.VMRuntime.ALLOW_TEST_API_ACCESS. |
atrost | 2dea079 | 2020-02-25 20:11:47 +0000 | [diff] [blame] | 44 | // Corresponds to bug ids. |
| 45 | static constexpr uint64_t kHideMaxtargetsdkPHiddenApis = 149997251; |
| 46 | static constexpr uint64_t kHideMaxtargetsdkQHiddenApis = 149994052; |
Pedro Loureiro | f7fcb98 | 2020-11-24 16:44:12 +0000 | [diff] [blame] | 47 | static constexpr uint64_t kAllowTestApiAccess = 166236554; |
atrost | 2dea079 | 2020-02-25 20:11:47 +0000 | [diff] [blame] | 48 | |
Andrei Onea | 3098e36 | 2021-01-26 18:30:54 +0000 | [diff] [blame] | 49 | static constexpr uint64_t kMaxLogWarnings = 100; |
| 50 | |
Sorin Basca | 633ac3c | 2021-10-06 14:00:29 +0000 | [diff] [blame] | 51 | // Should be the same as dalvik.system.VMRuntime.PREVENT_META_REFLECTION_BLOCKLIST_ACCESS. |
| 52 | // Corresponds to a bug id. |
| 53 | static constexpr uint64_t kPreventMetaReflectionBlocklistAccess = 142365358; |
| 54 | |
Mathew Inwood | 27199e6 | 2018-04-11 16:08:21 +0100 | [diff] [blame] | 55 | // Set to true if we should always print a warning in logcat for all hidden API accesses, not just |
Andrei Onea | fc12a6c | 2020-07-29 19:52:34 +0100 | [diff] [blame] | 56 | // conditionally and unconditionally blocked. This can be set to true for developer preview / beta |
| 57 | // builds, but should be false for public release builds. |
Mathew Inwood | 6d6012e | 2018-04-12 15:43:11 +0100 | [diff] [blame] | 58 | // Note that when flipping this flag, you must also update the expectations of test 674-hiddenapi |
Andrei Onea | fc12a6c | 2020-07-29 19:52:34 +0100 | [diff] [blame] | 59 | // as it affects whether or not we warn for unsupported APIs that have been added to the exemptions |
Mathew Inwood | 6d6012e | 2018-04-12 15:43:11 +0100 | [diff] [blame] | 60 | // list. |
Mathew Inwood | 015a7ec | 2018-05-16 11:18:10 +0100 | [diff] [blame] | 61 | static constexpr bool kLogAllAccesses = false; |
Mathew Inwood | 27199e6 | 2018-04-11 16:08:21 +0100 | [diff] [blame] | 62 | |
Artur Satayev | 4a1e4dd | 2020-04-23 22:28:59 +0100 | [diff] [blame] | 63 | // Exemptions for logcat warning. Following signatures do not produce a warning as app developers |
Andrei Onea | fc12a6c | 2020-07-29 19:52:34 +0100 | [diff] [blame] | 64 | // should not be alerted on the usage of these unsupported APIs. See b/154851649. |
Artur Satayev | 4a1e4dd | 2020-04-23 22:28:59 +0100 | [diff] [blame] | 65 | static const std::vector<std::string> kWarningExemptions = { |
| 66 | "Ljava/nio/Buffer;", |
| 67 | "Llibcore/io/Memory;", |
| 68 | "Lsun/misc/Unsafe;", |
| 69 | }; |
| 70 | |
Andreas Gampe | 80f5fe5 | 2018-03-28 16:23:24 -0700 | [diff] [blame] | 71 | static inline std::ostream& operator<<(std::ostream& os, AccessMethod value) { |
| 72 | switch (value) { |
David Brazdil | f50ac10 | 2018-10-17 18:00:06 +0100 | [diff] [blame] | 73 | case AccessMethod::kNone: |
David Brazdil | 54a99cf | 2018-04-05 16:57:32 +0100 | [diff] [blame] | 74 | LOG(FATAL) << "Internal access to hidden API should not be logged"; |
| 75 | UNREACHABLE(); |
David Brazdil | f50ac10 | 2018-10-17 18:00:06 +0100 | [diff] [blame] | 76 | case AccessMethod::kReflection: |
Andreas Gampe | 80f5fe5 | 2018-03-28 16:23:24 -0700 | [diff] [blame] | 77 | os << "reflection"; |
| 78 | break; |
David Brazdil | f50ac10 | 2018-10-17 18:00:06 +0100 | [diff] [blame] | 79 | case AccessMethod::kJNI: |
Andreas Gampe | 80f5fe5 | 2018-03-28 16:23:24 -0700 | [diff] [blame] | 80 | os << "JNI"; |
| 81 | break; |
David Brazdil | f50ac10 | 2018-10-17 18:00:06 +0100 | [diff] [blame] | 82 | case AccessMethod::kLinking: |
Andreas Gampe | 80f5fe5 | 2018-03-28 16:23:24 -0700 | [diff] [blame] | 83 | os << "linking"; |
| 84 | break; |
| 85 | } |
| 86 | return os; |
| 87 | } |
| 88 | |
David Brazdil | e768182 | 2018-12-14 16:25:33 +0000 | [diff] [blame] | 89 | static inline std::ostream& operator<<(std::ostream& os, const AccessContext& value) |
| 90 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 91 | if (!value.GetClass().IsNull()) { |
| 92 | std::string tmp; |
| 93 | os << value.GetClass()->GetDescriptor(&tmp); |
| 94 | } else if (value.GetDexFile() != nullptr) { |
| 95 | os << value.GetDexFile()->GetLocation(); |
| 96 | } else { |
| 97 | os << "<unknown_caller>"; |
| 98 | } |
| 99 | return os; |
| 100 | } |
| 101 | |
Nicolas Geoffray | e3e0f70 | 2019-03-12 07:02:02 +0000 | [diff] [blame] | 102 | static Domain DetermineDomainFromLocation(const std::string& dex_location, |
| 103 | ObjPtr<mirror::ClassLoader> class_loader) { |
David Brazdil | bfaba28 | 2019-03-15 11:35:51 +0000 | [diff] [blame] | 104 | // If running with APEX, check `path` against known APEX locations. |
Martin Stjernholm | e58624f | 2019-09-20 15:53:40 +0100 | [diff] [blame] | 105 | // These checks will be skipped on target buildbots where ANDROID_ART_ROOT |
David Brazdil | bfaba28 | 2019-03-15 11:35:51 +0000 | [diff] [blame] | 106 | // is set to "/system". |
Martin Stjernholm | e58624f | 2019-09-20 15:53:40 +0100 | [diff] [blame] | 107 | if (ArtModuleRootDistinctFromAndroidRoot()) { |
| 108 | if (LocationIsOnArtModule(dex_location.c_str()) || |
Victor Chang | d20e51d | 2020-05-05 16:01:19 +0100 | [diff] [blame] | 109 | LocationIsOnConscryptModule(dex_location.c_str()) || |
| 110 | LocationIsOnI18nModule(dex_location.c_str())) { |
Nicolas Geoffray | e3e0f70 | 2019-03-12 07:02:02 +0000 | [diff] [blame] | 111 | return Domain::kCorePlatform; |
David Brazdil | bfaba28 | 2019-03-15 11:35:51 +0000 | [diff] [blame] | 112 | } |
| 113 | |
Nicolas Geoffray | e3e0f70 | 2019-03-12 07:02:02 +0000 | [diff] [blame] | 114 | if (LocationIsOnApex(dex_location.c_str())) { |
David Brazdil | bfaba28 | 2019-03-15 11:35:51 +0000 | [diff] [blame] | 115 | return Domain::kPlatform; |
| 116 | } |
| 117 | } |
| 118 | |
Nicolas Geoffray | e3e0f70 | 2019-03-12 07:02:02 +0000 | [diff] [blame] | 119 | if (LocationIsOnSystemFramework(dex_location.c_str())) { |
David Brazdil | a5c3a80 | 2019-03-08 14:59:41 +0000 | [diff] [blame] | 120 | return Domain::kPlatform; |
| 121 | } |
| 122 | |
Chris Gross | 5477b8e | 2020-04-24 09:36:45 -0700 | [diff] [blame] | 123 | if (LocationIsOnSystemExtFramework(dex_location.c_str())) { |
| 124 | return Domain::kPlatform; |
| 125 | } |
| 126 | |
David Brazdil | a5c3a80 | 2019-03-08 14:59:41 +0000 | [diff] [blame] | 127 | if (class_loader.IsNull()) { |
Nicolas Geoffray | 5158d4a | 2020-05-27 08:50:06 +0100 | [diff] [blame] | 128 | if (kIsTargetBuild && !kIsTargetLinux) { |
| 129 | // This is unexpected only when running on Android. |
| 130 | LOG(WARNING) << "DexFile " << dex_location |
| 131 | << " is in boot class path but is not in a known location"; |
| 132 | } |
David Brazdil | a5c3a80 | 2019-03-08 14:59:41 +0000 | [diff] [blame] | 133 | return Domain::kPlatform; |
| 134 | } |
| 135 | |
| 136 | return Domain::kApplication; |
| 137 | } |
| 138 | |
David Brazdil | a5c3a80 | 2019-03-08 14:59:41 +0000 | [diff] [blame] | 139 | void InitializeDexFileDomain(const DexFile& dex_file, ObjPtr<mirror::ClassLoader> class_loader) { |
Nicolas Geoffray | e3e0f70 | 2019-03-12 07:02:02 +0000 | [diff] [blame] | 140 | Domain dex_domain = DetermineDomainFromLocation(dex_file.GetLocation(), class_loader); |
David Brazdil | a5c3a80 | 2019-03-08 14:59:41 +0000 | [diff] [blame] | 141 | |
| 142 | // Assign the domain unless a more permissive domain has already been assigned. |
| 143 | // This may happen when DexFile is initialized as trusted. |
| 144 | if (IsDomainMoreTrustedThan(dex_domain, dex_file.GetHiddenapiDomain())) { |
| 145 | dex_file.SetHiddenapiDomain(dex_domain); |
| 146 | } |
| 147 | } |
| 148 | |
Orion Hodson | 814b928 | 2020-02-19 16:37:11 +0000 | [diff] [blame] | 149 | void InitializeCorePlatformApiPrivateFields() { |
| 150 | // The following fields in WellKnownClasses correspond to private fields in the Core Platform |
| 151 | // API that cannot be otherwise expressed and propagated through tooling (b/144502743). |
| 152 | jfieldID private_core_platform_api_fields[] = { |
Orion Hodson | eb070f0 | 2020-03-10 14:00:18 +0000 | [diff] [blame] | 153 | WellKnownClasses::java_io_FileDescriptor_descriptor, |
Orion Hodson | 814b928 | 2020-02-19 16:37:11 +0000 | [diff] [blame] | 154 | WellKnownClasses::java_nio_Buffer_address, |
| 155 | WellKnownClasses::java_nio_Buffer_elementSizeShift, |
| 156 | WellKnownClasses::java_nio_Buffer_limit, |
| 157 | WellKnownClasses::java_nio_Buffer_position, |
| 158 | }; |
| 159 | |
| 160 | ScopedObjectAccess soa(Thread::Current()); |
| 161 | for (const auto private_core_platform_api_field : private_core_platform_api_fields) { |
| 162 | ArtField* field = jni::DecodeArtField(private_core_platform_api_field); |
| 163 | const uint32_t access_flags = field->GetAccessFlags(); |
| 164 | uint32_t new_access_flags = access_flags | kAccCorePlatformApi; |
| 165 | DCHECK(new_access_flags != access_flags); |
| 166 | field->SetAccessFlags(new_access_flags); |
| 167 | } |
| 168 | } |
| 169 | |
Sorin Basca | 633ac3c | 2021-10-06 14:00:29 +0000 | [diff] [blame] | 170 | hiddenapi::AccessContext GetReflectionCallerAccessContext(Thread* self) |
| 171 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 172 | // Walk the stack and find the first frame not from java.lang.Class, |
| 173 | // java.lang.invoke or java.lang.reflect. This is very expensive. |
| 174 | // Save this till the last. |
| 175 | struct FirstExternalCallerVisitor : public StackVisitor { |
| 176 | explicit FirstExternalCallerVisitor(Thread* thread) |
| 177 | : StackVisitor(thread, nullptr, StackVisitor::StackWalkKind::kIncludeInlinedFrames), |
| 178 | caller(nullptr) { |
| 179 | } |
| 180 | |
| 181 | bool VisitFrame() override REQUIRES_SHARED(Locks::mutator_lock_) { |
| 182 | ArtMethod *m = GetMethod(); |
| 183 | if (m == nullptr) { |
| 184 | // Attached native thread. Assume this is *not* boot class path. |
| 185 | caller = nullptr; |
| 186 | return false; |
| 187 | } else if (m->IsRuntimeMethod()) { |
| 188 | // Internal runtime method, continue walking the stack. |
| 189 | return true; |
| 190 | } |
| 191 | |
| 192 | ObjPtr<mirror::Class> declaring_class = m->GetDeclaringClass(); |
| 193 | if (declaring_class->IsBootStrapClassLoaded()) { |
| 194 | if (declaring_class->IsClassClass()) { |
| 195 | return true; |
| 196 | } |
| 197 | // Check classes in the java.lang.invoke package. At the time of writing, the |
| 198 | // classes of interest are MethodHandles and MethodHandles.Lookup, but this |
| 199 | // is subject to change so conservatively cover the entire package. |
| 200 | // NB Static initializers within java.lang.invoke are permitted and do not |
| 201 | // need further stack inspection. |
| 202 | ObjPtr<mirror::Class> lookup_class = GetClassRoot<mirror::MethodHandlesLookup>(); |
| 203 | if ((declaring_class == lookup_class || declaring_class->IsInSamePackage(lookup_class)) |
| 204 | && !m->IsClassInitializer()) { |
| 205 | return true; |
| 206 | } |
| 207 | // Check for classes in the java.lang.reflect package, except for java.lang.reflect.Proxy. |
| 208 | // java.lang.reflect.Proxy does its own hidden api checks (https://r.android.com/915496), |
| 209 | // and walking over this frame would cause a null pointer dereference |
| 210 | // (e.g. in 691-hiddenapi-proxy). |
| 211 | ObjPtr<mirror::Class> proxy_class = GetClassRoot<mirror::Proxy>(); |
| 212 | CompatFramework& compat_framework = Runtime::Current()->GetCompatFramework(); |
| 213 | if (declaring_class->IsInSamePackage(proxy_class) && declaring_class != proxy_class) { |
| 214 | if (compat_framework.IsChangeEnabled(kPreventMetaReflectionBlocklistAccess)) { |
| 215 | return true; |
| 216 | } |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | caller = m; |
| 221 | return false; |
| 222 | } |
| 223 | |
| 224 | ArtMethod* caller; |
| 225 | }; |
| 226 | |
| 227 | FirstExternalCallerVisitor visitor(self); |
| 228 | visitor.WalkStack(); |
| 229 | |
| 230 | // Construct AccessContext from the calling class found on the stack. |
| 231 | // If the calling class cannot be determined, e.g. unattached threads, |
| 232 | // we conservatively assume the caller is trusted. |
| 233 | ObjPtr<mirror::Class> caller = (visitor.caller == nullptr) |
| 234 | ? nullptr : visitor.caller->GetDeclaringClass(); |
| 235 | return caller.IsNull() ? AccessContext(/* is_trusted= */ true) |
| 236 | : AccessContext(caller); |
| 237 | } |
| 238 | |
Andreas Gampe | 80f5fe5 | 2018-03-28 16:23:24 -0700 | [diff] [blame] | 239 | namespace detail { |
| 240 | |
David Brazdil | f50ac10 | 2018-10-17 18:00:06 +0100 | [diff] [blame] | 241 | // Do not change the values of items in this enum, as they are written to the |
| 242 | // event log for offline analysis. Any changes will interfere with that analysis. |
| 243 | enum AccessContextFlags { |
| 244 | // Accessed member is a field if this bit is set, else a method |
| 245 | kMemberIsField = 1 << 0, |
| 246 | // Indicates if access was denied to the member, instead of just printing a warning. |
| 247 | kAccessDenied = 1 << 1, |
| 248 | }; |
| 249 | |
Andreas Gampe | 80f5fe5 | 2018-03-28 16:23:24 -0700 | [diff] [blame] | 250 | MemberSignature::MemberSignature(ArtField* field) { |
Mathew Inwood | 73ddda4 | 2018-04-03 15:32:32 +0100 | [diff] [blame] | 251 | class_name_ = field->GetDeclaringClass()->GetDescriptor(&tmp_); |
| 252 | member_name_ = field->GetName(); |
| 253 | type_signature_ = field->GetTypeDescriptor(); |
| 254 | type_ = kField; |
Andreas Gampe | 80f5fe5 | 2018-03-28 16:23:24 -0700 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | MemberSignature::MemberSignature(ArtMethod* method) { |
David Brazdil | 6a1dab4 | 2019-02-28 18:45:15 +0000 | [diff] [blame] | 258 | DCHECK(method == method->GetInterfaceMethodIfProxy(kRuntimePointerSize)) |
| 259 | << "Caller should have replaced proxy method with interface method"; |
Mathew Inwood | 73ddda4 | 2018-04-03 15:32:32 +0100 | [diff] [blame] | 260 | class_name_ = method->GetDeclaringClass()->GetDescriptor(&tmp_); |
| 261 | member_name_ = method->GetName(); |
| 262 | type_signature_ = method->GetSignature().ToString(); |
| 263 | type_ = kMethod; |
| 264 | } |
| 265 | |
David Brazdil | 1a65863 | 2018-12-01 17:54:26 +0000 | [diff] [blame] | 266 | MemberSignature::MemberSignature(const ClassAccessor::Field& field) { |
| 267 | const DexFile& dex_file = field.GetDexFile(); |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 268 | const dex::FieldId& field_id = dex_file.GetFieldId(field.GetIndex()); |
David Brazdil | 1a65863 | 2018-12-01 17:54:26 +0000 | [diff] [blame] | 269 | class_name_ = dex_file.GetFieldDeclaringClassDescriptor(field_id); |
| 270 | member_name_ = dex_file.GetFieldName(field_id); |
| 271 | type_signature_ = dex_file.GetFieldTypeDescriptor(field_id); |
| 272 | type_ = kField; |
| 273 | } |
| 274 | |
| 275 | MemberSignature::MemberSignature(const ClassAccessor::Method& method) { |
| 276 | const DexFile& dex_file = method.GetDexFile(); |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 277 | const dex::MethodId& method_id = dex_file.GetMethodId(method.GetIndex()); |
David Brazdil | 1a65863 | 2018-12-01 17:54:26 +0000 | [diff] [blame] | 278 | class_name_ = dex_file.GetMethodDeclaringClassDescriptor(method_id); |
| 279 | member_name_ = dex_file.GetMethodName(method_id); |
| 280 | type_signature_ = dex_file.GetMethodSignature(method_id).ToString(); |
| 281 | type_ = kMethod; |
| 282 | } |
| 283 | |
Mathew Inwood | 73ddda4 | 2018-04-03 15:32:32 +0100 | [diff] [blame] | 284 | inline std::vector<const char*> MemberSignature::GetSignatureParts() const { |
| 285 | if (type_ == kField) { |
| 286 | return { class_name_.c_str(), "->", member_name_.c_str(), ":", type_signature_.c_str() }; |
| 287 | } else { |
| 288 | DCHECK_EQ(type_, kMethod); |
| 289 | return { class_name_.c_str(), "->", member_name_.c_str(), type_signature_.c_str() }; |
| 290 | } |
Andreas Gampe | 80f5fe5 | 2018-03-28 16:23:24 -0700 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | bool MemberSignature::DoesPrefixMatch(const std::string& prefix) const { |
| 294 | size_t pos = 0; |
Mathew Inwood | 73ddda4 | 2018-04-03 15:32:32 +0100 | [diff] [blame] | 295 | for (const char* part : GetSignatureParts()) { |
| 296 | size_t count = std::min(prefix.length() - pos, strlen(part)); |
Andreas Gampe | 80f5fe5 | 2018-03-28 16:23:24 -0700 | [diff] [blame] | 297 | if (prefix.compare(pos, count, part, 0, count) == 0) { |
| 298 | pos += count; |
| 299 | } else { |
| 300 | return false; |
| 301 | } |
| 302 | } |
| 303 | // We have a complete match if all parts match (we exit the loop without |
| 304 | // returning) AND we've matched the whole prefix. |
| 305 | return pos == prefix.length(); |
| 306 | } |
| 307 | |
Artur Satayev | 4a1e4dd | 2020-04-23 22:28:59 +0100 | [diff] [blame] | 308 | bool MemberSignature::DoesPrefixMatchAny(const std::vector<std::string>& exemptions) { |
Andreas Gampe | 80f5fe5 | 2018-03-28 16:23:24 -0700 | [diff] [blame] | 309 | for (const std::string& exemption : exemptions) { |
| 310 | if (DoesPrefixMatch(exemption)) { |
| 311 | return true; |
| 312 | } |
| 313 | } |
| 314 | return false; |
| 315 | } |
| 316 | |
| 317 | void MemberSignature::Dump(std::ostream& os) const { |
Mathew Inwood | 73ddda4 | 2018-04-03 15:32:32 +0100 | [diff] [blame] | 318 | for (const char* part : GetSignatureParts()) { |
Andreas Gampe | 80f5fe5 | 2018-03-28 16:23:24 -0700 | [diff] [blame] | 319 | os << part; |
| 320 | } |
| 321 | } |
| 322 | |
David Brazdil | 4de9bb6 | 2019-04-03 13:06:17 +0100 | [diff] [blame] | 323 | void MemberSignature::WarnAboutAccess(AccessMethod access_method, |
| 324 | hiddenapi::ApiList list, |
| 325 | bool access_denied) { |
Andrei Onea | 3098e36 | 2021-01-26 18:30:54 +0000 | [diff] [blame] | 326 | static std::atomic<uint64_t> log_warning_count_ = 0; |
| 327 | if (log_warning_count_ > kMaxLogWarnings) { |
| 328 | return; |
| 329 | } |
Mathew Inwood | 73ddda4 | 2018-04-03 15:32:32 +0100 | [diff] [blame] | 330 | LOG(WARNING) << "Accessing hidden " << (type_ == kField ? "field " : "method ") |
David Brazdil | 4de9bb6 | 2019-04-03 13:06:17 +0100 | [diff] [blame] | 331 | << Dumpable<MemberSignature>(*this) << " (" << list << ", " << access_method |
| 332 | << (access_denied ? ", denied)" : ", allowed)"); |
Pedro Loureiro | 2807839 | 2021-01-21 17:09:55 +0000 | [diff] [blame] | 333 | if (access_denied && list.IsTestApi()) { |
| 334 | // see b/177047045 for more details about test api access getting denied |
| 335 | LOG(WARNING) << "If this is a platform test consider enabling " |
| 336 | << "VMRuntime.ALLOW_TEST_API_ACCESS change id for this package."; |
| 337 | } |
Andrei Onea | 3098e36 | 2021-01-26 18:30:54 +0000 | [diff] [blame] | 338 | if (log_warning_count_ >= kMaxLogWarnings) { |
| 339 | LOG(WARNING) << "Reached maximum number of hidden api access warnings."; |
| 340 | } |
| 341 | ++log_warning_count_; |
Mathew Inwood | 73ddda4 | 2018-04-03 15:32:32 +0100 | [diff] [blame] | 342 | } |
David Brazdil | f50ac10 | 2018-10-17 18:00:06 +0100 | [diff] [blame] | 343 | |
David Brazdil | 1a65863 | 2018-12-01 17:54:26 +0000 | [diff] [blame] | 344 | bool MemberSignature::Equals(const MemberSignature& other) { |
| 345 | return type_ == other.type_ && |
| 346 | class_name_ == other.class_name_ && |
| 347 | member_name_ == other.member_name_ && |
| 348 | type_signature_ == other.type_signature_; |
| 349 | } |
| 350 | |
| 351 | bool MemberSignature::MemberNameAndTypeMatch(const MemberSignature& other) { |
| 352 | return member_name_ == other.member_name_ && type_signature_ == other.type_signature_; |
| 353 | } |
| 354 | |
Andrei Onea | 6ad020d | 2019-02-18 12:15:51 +0000 | [diff] [blame] | 355 | void MemberSignature::LogAccessToEventLog(uint32_t sampled_value, |
| 356 | AccessMethod access_method, |
| 357 | bool access_denied) { |
Nicolas Geoffray | 8a22907 | 2018-05-10 16:34:14 +0100 | [diff] [blame] | 358 | #ifdef ART_TARGET_ANDROID |
David Brazdil | f50ac10 | 2018-10-17 18:00:06 +0100 | [diff] [blame] | 359 | if (access_method == AccessMethod::kLinking || access_method == AccessMethod::kNone) { |
Mathew Inwood | 73ddda4 | 2018-04-03 15:32:32 +0100 | [diff] [blame] | 360 | // Linking warnings come from static analysis/compilation of the bytecode |
| 361 | // and can contain false positives (i.e. code that is never run). We choose |
| 362 | // not to log these in the event log. |
Mathew Inwood | f59ca61 | 2018-05-03 11:30:01 +0100 | [diff] [blame] | 363 | // None does not correspond to actual access, so should also be ignored. |
Mathew Inwood | 73ddda4 | 2018-04-03 15:32:32 +0100 | [diff] [blame] | 364 | return; |
| 365 | } |
Andrei Onea | a2d2bc2 | 2019-01-25 16:18:53 +0000 | [diff] [blame] | 366 | Runtime* runtime = Runtime::Current(); |
| 367 | if (runtime->IsAotCompiler()) { |
| 368 | return; |
Mathew Inwood | 73ddda4 | 2018-04-03 15:32:32 +0100 | [diff] [blame] | 369 | } |
Andrei Onea | a2d2bc2 | 2019-01-25 16:18:53 +0000 | [diff] [blame] | 370 | JNIEnvExt* env = Thread::Current()->GetJniEnv(); |
Mathew Inwood | 5bcef17 | 2018-05-01 14:40:12 +0100 | [diff] [blame] | 371 | const std::string& package_name = Runtime::Current()->GetProcessPackageName(); |
Andrei Onea | a2d2bc2 | 2019-01-25 16:18:53 +0000 | [diff] [blame] | 372 | ScopedLocalRef<jstring> package_str(env, env->NewStringUTF(package_name.c_str())); |
| 373 | if (env->ExceptionCheck()) { |
| 374 | env->ExceptionClear(); |
| 375 | LOG(ERROR) << "Unable to allocate string for package name which called hidden api"; |
Mathew Inwood | 5bcef17 | 2018-05-01 14:40:12 +0100 | [diff] [blame] | 376 | } |
Mathew Inwood | 2d4d62f | 2018-04-12 13:56:37 +0100 | [diff] [blame] | 377 | std::ostringstream signature_str; |
| 378 | Dump(signature_str); |
Andrei Onea | a2d2bc2 | 2019-01-25 16:18:53 +0000 | [diff] [blame] | 379 | ScopedLocalRef<jstring> signature_jstr(env, |
| 380 | env->NewStringUTF(signature_str.str().c_str())); |
| 381 | if (env->ExceptionCheck()) { |
| 382 | env->ExceptionClear(); |
| 383 | LOG(ERROR) << "Unable to allocate string for hidden api method signature"; |
| 384 | } |
| 385 | env->CallStaticVoidMethod(WellKnownClasses::dalvik_system_VMRuntime, |
Andrei Onea | 6ad020d | 2019-02-18 12:15:51 +0000 | [diff] [blame] | 386 | WellKnownClasses::dalvik_system_VMRuntime_hiddenApiUsed, |
| 387 | sampled_value, |
| 388 | package_str.get(), |
| 389 | signature_jstr.get(), |
| 390 | static_cast<jint>(access_method), |
| 391 | access_denied); |
Andrei Onea | a2d2bc2 | 2019-01-25 16:18:53 +0000 | [diff] [blame] | 392 | if (env->ExceptionCheck()) { |
| 393 | env->ExceptionClear(); |
| 394 | LOG(ERROR) << "Unable to report hidden api usage"; |
| 395 | } |
Nicolas Geoffray | 8a22907 | 2018-05-10 16:34:14 +0100 | [diff] [blame] | 396 | #else |
Andrei Onea | 6ad020d | 2019-02-18 12:15:51 +0000 | [diff] [blame] | 397 | UNUSED(sampled_value); |
Nicolas Geoffray | 8a22907 | 2018-05-10 16:34:14 +0100 | [diff] [blame] | 398 | UNUSED(access_method); |
David Brazdil | f50ac10 | 2018-10-17 18:00:06 +0100 | [diff] [blame] | 399 | UNUSED(access_denied); |
Nicolas Geoffray | 8a22907 | 2018-05-10 16:34:14 +0100 | [diff] [blame] | 400 | #endif |
Andreas Gampe | 80f5fe5 | 2018-03-28 16:23:24 -0700 | [diff] [blame] | 401 | } |
| 402 | |
David Brazdil | f50ac10 | 2018-10-17 18:00:06 +0100 | [diff] [blame] | 403 | void MemberSignature::NotifyHiddenApiListener(AccessMethod access_method) { |
| 404 | if (access_method != AccessMethod::kReflection && access_method != AccessMethod::kJNI) { |
| 405 | // We can only up-call into Java during reflection and JNI down-calls. |
| 406 | return; |
| 407 | } |
| 408 | |
| 409 | Runtime* runtime = Runtime::Current(); |
| 410 | if (!runtime->IsAotCompiler()) { |
| 411 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
| 412 | |
| 413 | ScopedLocalRef<jobject> consumer_object(soa.Env(), |
| 414 | soa.Env()->GetStaticObjectField( |
| 415 | WellKnownClasses::dalvik_system_VMRuntime, |
| 416 | WellKnownClasses::dalvik_system_VMRuntime_nonSdkApiUsageConsumer)); |
| 417 | // If the consumer is non-null, we call back to it to let it know that we |
| 418 | // have encountered an API that's in one of our lists. |
| 419 | if (consumer_object != nullptr) { |
| 420 | std::ostringstream member_signature_str; |
| 421 | Dump(member_signature_str); |
| 422 | |
| 423 | ScopedLocalRef<jobject> signature_str( |
| 424 | soa.Env(), |
| 425 | soa.Env()->NewStringUTF(member_signature_str.str().c_str())); |
| 426 | |
| 427 | // Call through to Consumer.accept(String memberSignature); |
| 428 | soa.Env()->CallVoidMethod(consumer_object.get(), |
| 429 | WellKnownClasses::java_util_function_Consumer_accept, |
| 430 | signature_str.get()); |
| 431 | } |
| 432 | } |
| 433 | } |
| 434 | |
David Brazdil | 8586569 | 2018-10-30 17:26:20 +0000 | [diff] [blame] | 435 | static ALWAYS_INLINE bool CanUpdateRuntimeFlags(ArtField*) { |
David Brazdil | 8a6b2f3 | 2018-04-26 16:52:11 +0100 | [diff] [blame] | 436 | return true; |
| 437 | } |
| 438 | |
David Brazdil | 8586569 | 2018-10-30 17:26:20 +0000 | [diff] [blame] | 439 | static ALWAYS_INLINE bool CanUpdateRuntimeFlags(ArtMethod* method) { |
David Brazdil | 8a6b2f3 | 2018-04-26 16:52:11 +0100 | [diff] [blame] | 440 | return !method->IsIntrinsic(); |
| 441 | } |
| 442 | |
| 443 | template<typename T> |
David Brazdil | d51e574 | 2019-02-28 14:47:32 +0000 | [diff] [blame] | 444 | static ALWAYS_INLINE void MaybeUpdateAccessFlags(Runtime* runtime, T* member, uint32_t flag) |
David Brazdil | 8a6b2f3 | 2018-04-26 16:52:11 +0100 | [diff] [blame] | 445 | REQUIRES_SHARED(Locks::mutator_lock_) { |
David Brazdil | 0039bc5 | 2019-04-10 10:22:26 +0100 | [diff] [blame] | 446 | // Update the access flags unless: |
| 447 | // (a) `member` is an intrinsic |
| 448 | // (b) this is AOT compiler, as we do not want the updated access flags in the boot/app image |
| 449 | // (c) deduping warnings has been explicitly switched off. |
| 450 | if (CanUpdateRuntimeFlags(member) && |
| 451 | !runtime->IsAotCompiler() && |
| 452 | runtime->ShouldDedupeHiddenApiWarnings()) { |
David Brazdil | d51e574 | 2019-02-28 14:47:32 +0000 | [diff] [blame] | 453 | member->SetAccessFlags(member->GetAccessFlags() | flag); |
David Brazdil | 8a6b2f3 | 2018-04-26 16:52:11 +0100 | [diff] [blame] | 454 | } |
| 455 | } |
| 456 | |
David Brazdil | 1a65863 | 2018-12-01 17:54:26 +0000 | [diff] [blame] | 457 | static ALWAYS_INLINE uint32_t GetMemberDexIndex(ArtField* field) { |
| 458 | return field->GetDexFieldIndex(); |
David Brazdil | 8586569 | 2018-10-30 17:26:20 +0000 | [diff] [blame] | 459 | } |
| 460 | |
David Brazdil | 1a65863 | 2018-12-01 17:54:26 +0000 | [diff] [blame] | 461 | static ALWAYS_INLINE uint32_t GetMemberDexIndex(ArtMethod* method) |
| 462 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 463 | // Use the non-obsolete method to avoid DexFile mismatch between |
| 464 | // the method index and the declaring class. |
| 465 | return method->GetNonObsoleteMethod()->GetDexMethodIndex(); |
| 466 | } |
David Brazdil | 8586569 | 2018-10-30 17:26:20 +0000 | [diff] [blame] | 467 | |
David Brazdil | 1a65863 | 2018-12-01 17:54:26 +0000 | [diff] [blame] | 468 | static void VisitMembers(const DexFile& dex_file, |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 469 | const dex::ClassDef& class_def, |
David Brazdil | 1a65863 | 2018-12-01 17:54:26 +0000 | [diff] [blame] | 470 | const std::function<void(const ClassAccessor::Field&)>& fn_visit) { |
| 471 | ClassAccessor accessor(dex_file, class_def, /* parse_hiddenapi_class_data= */ true); |
| 472 | accessor.VisitFields(fn_visit, fn_visit); |
| 473 | } |
| 474 | |
| 475 | static void VisitMembers(const DexFile& dex_file, |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 476 | const dex::ClassDef& class_def, |
David Brazdil | 1a65863 | 2018-12-01 17:54:26 +0000 | [diff] [blame] | 477 | const std::function<void(const ClassAccessor::Method&)>& fn_visit) { |
| 478 | ClassAccessor accessor(dex_file, class_def, /* parse_hiddenapi_class_data= */ true); |
| 479 | accessor.VisitMethods(fn_visit, fn_visit); |
| 480 | } |
| 481 | |
| 482 | template<typename T> |
| 483 | uint32_t GetDexFlags(T* member) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 484 | static_assert(std::is_same<T, ArtField>::value || std::is_same<T, ArtMethod>::value); |
David Brazdil | 6a1dab4 | 2019-02-28 18:45:15 +0000 | [diff] [blame] | 485 | constexpr bool kMemberIsField = std::is_same<T, ArtField>::value; |
David Brazdil | 1a65863 | 2018-12-01 17:54:26 +0000 | [diff] [blame] | 486 | using AccessorType = typename std::conditional<std::is_same<T, ArtField>::value, |
| 487 | ClassAccessor::Field, ClassAccessor::Method>::type; |
| 488 | |
| 489 | ObjPtr<mirror::Class> declaring_class = member->GetDeclaringClass(); |
David Brazdil | 90faceb | 2018-12-14 14:36:15 +0000 | [diff] [blame] | 490 | DCHECK(!declaring_class.IsNull()) << "Attempting to access a runtime method"; |
David Brazdil | 8586569 | 2018-10-30 17:26:20 +0000 | [diff] [blame] | 491 | |
David Brazdil | 90faceb | 2018-12-14 14:36:15 +0000 | [diff] [blame] | 492 | ApiList flags; |
| 493 | DCHECK(!flags.IsValid()); |
David Brazdil | 8586569 | 2018-10-30 17:26:20 +0000 | [diff] [blame] | 494 | |
David Brazdil | 1a65863 | 2018-12-01 17:54:26 +0000 | [diff] [blame] | 495 | // Check if the declaring class has ClassExt allocated. If it does, check if |
| 496 | // the pre-JVMTI redefine dex file has been set to determine if the declaring |
| 497 | // class has been JVMTI-redefined. |
| 498 | ObjPtr<mirror::ClassExt> ext(declaring_class->GetExtData()); |
| 499 | const DexFile* original_dex = ext.IsNull() ? nullptr : ext->GetPreRedefineDexFile(); |
| 500 | if (LIKELY(original_dex == nullptr)) { |
| 501 | // Class is not redefined. Find the class def, iterate over its members and |
| 502 | // find the entry corresponding to this `member`. |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 503 | const dex::ClassDef* class_def = declaring_class->GetClassDef(); |
David Brazdil | 6a1dab4 | 2019-02-28 18:45:15 +0000 | [diff] [blame] | 504 | if (class_def == nullptr) { |
| 505 | // ClassDef is not set for proxy classes. Only their fields can ever be inspected. |
| 506 | DCHECK(declaring_class->IsProxyClass()) |
| 507 | << "Only proxy classes are expected not to have a class def"; |
| 508 | DCHECK(kMemberIsField) |
| 509 | << "Interface methods should be inspected instead of proxy class methods"; |
Andrei Onea | fc12a6c | 2020-07-29 19:52:34 +0100 | [diff] [blame] | 510 | flags = ApiList::Unsupported(); |
David Brazdil | 6a1dab4 | 2019-02-28 18:45:15 +0000 | [diff] [blame] | 511 | } else { |
| 512 | uint32_t member_index = GetMemberDexIndex(member); |
| 513 | auto fn_visit = [&](const AccessorType& dex_member) { |
| 514 | if (dex_member.GetIndex() == member_index) { |
| 515 | flags = ApiList(dex_member.GetHiddenapiFlags()); |
| 516 | } |
| 517 | }; |
| 518 | VisitMembers(declaring_class->GetDexFile(), *class_def, fn_visit); |
| 519 | } |
David Brazdil | 1a65863 | 2018-12-01 17:54:26 +0000 | [diff] [blame] | 520 | } else { |
| 521 | // Class was redefined using JVMTI. We have a pointer to the original dex file |
| 522 | // and the class def index of this class in that dex file, but the field/method |
| 523 | // indices are lost. Iterate over all members of the class def and find the one |
| 524 | // corresponding to this `member` by name and type string comparison. |
| 525 | // This is obviously very slow, but it is only used when non-exempt code tries |
| 526 | // to access a hidden member of a JVMTI-redefined class. |
| 527 | uint16_t class_def_idx = ext->GetPreRedefineClassDefIndex(); |
| 528 | DCHECK_NE(class_def_idx, DexFile::kDexNoIndex16); |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 529 | const dex::ClassDef& original_class_def = original_dex->GetClassDef(class_def_idx); |
David Brazdil | 1a65863 | 2018-12-01 17:54:26 +0000 | [diff] [blame] | 530 | MemberSignature member_signature(member); |
| 531 | auto fn_visit = [&](const AccessorType& dex_member) { |
| 532 | MemberSignature cur_signature(dex_member); |
| 533 | if (member_signature.MemberNameAndTypeMatch(cur_signature)) { |
| 534 | DCHECK(member_signature.Equals(cur_signature)); |
David Brazdil | 90faceb | 2018-12-14 14:36:15 +0000 | [diff] [blame] | 535 | flags = ApiList(dex_member.GetHiddenapiFlags()); |
David Brazdil | 1a65863 | 2018-12-01 17:54:26 +0000 | [diff] [blame] | 536 | } |
| 537 | }; |
| 538 | VisitMembers(*original_dex, original_class_def, fn_visit); |
| 539 | } |
David Brazdil | 8586569 | 2018-10-30 17:26:20 +0000 | [diff] [blame] | 540 | |
David Brazdil | 90faceb | 2018-12-14 14:36:15 +0000 | [diff] [blame] | 541 | CHECK(flags.IsValid()) << "Could not find hiddenapi flags for " |
David Brazdil | 1a65863 | 2018-12-01 17:54:26 +0000 | [diff] [blame] | 542 | << Dumpable<MemberSignature>(MemberSignature(member)); |
David Brazdil | 90faceb | 2018-12-14 14:36:15 +0000 | [diff] [blame] | 543 | return flags.GetDexFlags(); |
David Brazdil | 8586569 | 2018-10-30 17:26:20 +0000 | [diff] [blame] | 544 | } |
| 545 | |
Andreas Gampe | 80f5fe5 | 2018-03-28 16:23:24 -0700 | [diff] [blame] | 546 | template<typename T> |
David Brazdil | d51e574 | 2019-02-28 14:47:32 +0000 | [diff] [blame] | 547 | bool HandleCorePlatformApiViolation(T* member, |
| 548 | const AccessContext& caller_context, |
| 549 | AccessMethod access_method, |
| 550 | EnforcementPolicy policy) { |
| 551 | DCHECK(policy != EnforcementPolicy::kDisabled) |
| 552 | << "Should never enter this function when access checks are completely disabled"; |
| 553 | |
David Brazdil | e768182 | 2018-12-14 16:25:33 +0000 | [diff] [blame] | 554 | if (access_method != AccessMethod::kNone) { |
David Brazdil | d51e574 | 2019-02-28 14:47:32 +0000 | [diff] [blame] | 555 | LOG(WARNING) << "Core platform API violation: " |
| 556 | << Dumpable<MemberSignature>(MemberSignature(member)) |
| 557 | << " from " << caller_context << " using " << access_method; |
| 558 | |
| 559 | // If policy is set to just warn, add kAccCorePlatformApi to access flags of |
| 560 | // `member` to avoid reporting the violation again next time. |
| 561 | if (policy == EnforcementPolicy::kJustWarn) { |
| 562 | MaybeUpdateAccessFlags(Runtime::Current(), member, kAccCorePlatformApi); |
| 563 | } |
David Brazdil | e768182 | 2018-12-14 16:25:33 +0000 | [diff] [blame] | 564 | } |
David Brazdil | d51e574 | 2019-02-28 14:47:32 +0000 | [diff] [blame] | 565 | |
| 566 | // Deny access if enforcement is enabled. |
| 567 | return policy == EnforcementPolicy::kEnabled; |
David Brazdil | e768182 | 2018-12-14 16:25:33 +0000 | [diff] [blame] | 568 | } |
| 569 | |
| 570 | template<typename T> |
| 571 | bool ShouldDenyAccessToMemberImpl(T* member, ApiList api_list, AccessMethod access_method) { |
David Brazdil | f50ac10 | 2018-10-17 18:00:06 +0100 | [diff] [blame] | 572 | DCHECK(member != nullptr); |
Andreas Gampe | 80f5fe5 | 2018-03-28 16:23:24 -0700 | [diff] [blame] | 573 | Runtime* runtime = Runtime::Current(); |
Andrei Onea | 037d282 | 2020-11-19 00:20:04 +0000 | [diff] [blame] | 574 | CompatFramework& compatFramework = runtime->GetCompatFramework(); |
David Brazdil | c5a96e4 | 2019-01-09 10:04:45 +0000 | [diff] [blame] | 575 | |
Artur Satayev | 267366c | 2019-10-31 14:59:26 +0000 | [diff] [blame] | 576 | EnforcementPolicy hiddenApiPolicy = runtime->GetHiddenApiEnforcementPolicy(); |
| 577 | DCHECK(hiddenApiPolicy != EnforcementPolicy::kDisabled) |
David Brazdil | c5a96e4 | 2019-01-09 10:04:45 +0000 | [diff] [blame] | 578 | << "Should never enter this function when access checks are completely disabled"; |
David Brazdil | f50ac10 | 2018-10-17 18:00:06 +0100 | [diff] [blame] | 579 | |
David Brazdil | f50ac10 | 2018-10-17 18:00:06 +0100 | [diff] [blame] | 580 | MemberSignature member_signature(member); |
Andreas Gampe | 80f5fe5 | 2018-03-28 16:23:24 -0700 | [diff] [blame] | 581 | |
Andrei Onea | fc12a6c | 2020-07-29 19:52:34 +0100 | [diff] [blame] | 582 | // Check for an exemption first. Exempted APIs are treated as SDK. |
Artur Satayev | 4a1e4dd | 2020-04-23 22:28:59 +0100 | [diff] [blame] | 583 | if (member_signature.DoesPrefixMatchAny(runtime->GetHiddenApiExemptions())) { |
David Brazdil | f50ac10 | 2018-10-17 18:00:06 +0100 | [diff] [blame] | 584 | // Avoid re-examining the exemption list next time. |
| 585 | // Note this results in no warning for the member, which seems like what one would expect. |
Andrei Onea | fc12a6c | 2020-07-29 19:52:34 +0100 | [diff] [blame] | 586 | // Exemptions effectively adds new members to the public API list. |
David Brazdil | d51e574 | 2019-02-28 14:47:32 +0000 | [diff] [blame] | 587 | MaybeUpdateAccessFlags(runtime, member, kAccPublicApi); |
David Brazdil | f50ac10 | 2018-10-17 18:00:06 +0100 | [diff] [blame] | 588 | return false; |
| 589 | } |
Andreas Gampe | 80f5fe5 | 2018-03-28 16:23:24 -0700 | [diff] [blame] | 590 | |
satayev | 09efc16 | 2021-02-22 17:24:57 +0000 | [diff] [blame] | 591 | EnforcementPolicy testApiPolicy = runtime->GetTestApiEnforcementPolicy(); |
| 592 | |
Artur Satayev | 267366c | 2019-10-31 14:59:26 +0000 | [diff] [blame] | 593 | bool deny_access = false; |
| 594 | if (hiddenApiPolicy == EnforcementPolicy::kEnabled) { |
satayev | 09efc16 | 2021-02-22 17:24:57 +0000 | [diff] [blame] | 595 | if (api_list.IsTestApi() && |
| 596 | (testApiPolicy == EnforcementPolicy::kDisabled || |
| 597 | compatFramework.IsChangeEnabled(kAllowTestApiAccess))) { |
Artur Satayev | 267366c | 2019-10-31 14:59:26 +0000 | [diff] [blame] | 598 | deny_access = false; |
| 599 | } else { |
atrost | 2dea079 | 2020-02-25 20:11:47 +0000 | [diff] [blame] | 600 | switch (api_list.GetMaxAllowedSdkVersion()) { |
| 601 | case SdkVersion::kP: |
Andrei Onea | 037d282 | 2020-11-19 00:20:04 +0000 | [diff] [blame] | 602 | deny_access = compatFramework.IsChangeEnabled(kHideMaxtargetsdkPHiddenApis); |
atrost | 2dea079 | 2020-02-25 20:11:47 +0000 | [diff] [blame] | 603 | break; |
| 604 | case SdkVersion::kQ: |
Andrei Onea | 037d282 | 2020-11-19 00:20:04 +0000 | [diff] [blame] | 605 | deny_access = compatFramework.IsChangeEnabled(kHideMaxtargetsdkQHiddenApis); |
atrost | 2dea079 | 2020-02-25 20:11:47 +0000 | [diff] [blame] | 606 | break; |
| 607 | default: |
| 608 | deny_access = IsSdkVersionSetAndMoreThan(runtime->GetTargetSdkVersion(), |
Nicolas Geoffray | fedff51 | 2021-02-07 21:36:33 +0000 | [diff] [blame] | 609 | api_list.GetMaxAllowedSdkVersion()); |
atrost | 2dea079 | 2020-02-25 20:11:47 +0000 | [diff] [blame] | 610 | } |
Artur Satayev | 267366c | 2019-10-31 14:59:26 +0000 | [diff] [blame] | 611 | } |
| 612 | } |
| 613 | |
David Brazdil | f50ac10 | 2018-10-17 18:00:06 +0100 | [diff] [blame] | 614 | if (access_method != AccessMethod::kNone) { |
Andrei Onea | fc12a6c | 2020-07-29 19:52:34 +0100 | [diff] [blame] | 615 | // Warn if blocked signature is being accessed or it is not exempted. |
Artur Satayev | 4a1e4dd | 2020-04-23 22:28:59 +0100 | [diff] [blame] | 616 | if (deny_access || !member_signature.DoesPrefixMatchAny(kWarningExemptions)) { |
| 617 | // Print a log message with information about this class member access. |
| 618 | // We do this if we're about to deny access, or the app is debuggable. |
| 619 | if (kLogAllAccesses || deny_access || runtime->IsJavaDebuggable()) { |
| 620 | member_signature.WarnAboutAccess(access_method, api_list, deny_access); |
| 621 | } |
Andreas Gampe | 80f5fe5 | 2018-03-28 16:23:24 -0700 | [diff] [blame] | 622 | |
Artur Satayev | 4a1e4dd | 2020-04-23 22:28:59 +0100 | [diff] [blame] | 623 | // If there is a StrictMode listener, notify it about this violation. |
| 624 | member_signature.NotifyHiddenApiListener(access_method); |
| 625 | } |
David Brazdil | f50ac10 | 2018-10-17 18:00:06 +0100 | [diff] [blame] | 626 | |
| 627 | // If event log sampling is enabled, report this violation. |
| 628 | if (kIsTargetBuild && !kIsTargetLinux) { |
| 629 | uint32_t eventLogSampleRate = runtime->GetHiddenApiEventLogSampleRate(); |
| 630 | // Assert that RAND_MAX is big enough, to ensure sampling below works as expected. |
| 631 | static_assert(RAND_MAX >= 0xffff, "RAND_MAX too small"); |
Andrei Onea | 6ad020d | 2019-02-18 12:15:51 +0000 | [diff] [blame] | 632 | if (eventLogSampleRate != 0) { |
| 633 | const uint32_t sampled_value = static_cast<uint32_t>(std::rand()) & 0xffff; |
| 634 | if (sampled_value < eventLogSampleRate) { |
| 635 | member_signature.LogAccessToEventLog(sampled_value, access_method, deny_access); |
| 636 | } |
David Brazdil | f50ac10 | 2018-10-17 18:00:06 +0100 | [diff] [blame] | 637 | } |
| 638 | } |
| 639 | |
Andrei Onea | fc12a6c | 2020-07-29 19:52:34 +0100 | [diff] [blame] | 640 | // If this access was not denied, flag member as SDK and skip |
Nicolas Geoffray | a126a71 | 2022-02-25 10:07:17 +0000 | [diff] [blame] | 641 | // the warning the next time the member is accessed. Don't update for |
| 642 | // non-debuggable apps as this has a memory cost. |
| 643 | if (!deny_access && runtime->IsJavaDebuggable()) { |
David Brazdil | d51e574 | 2019-02-28 14:47:32 +0000 | [diff] [blame] | 644 | MaybeUpdateAccessFlags(runtime, member, kAccPublicApi); |
Mathew Inwood | 73ddda4 | 2018-04-03 15:32:32 +0100 | [diff] [blame] | 645 | } |
| 646 | } |
| 647 | |
David Brazdil | f50ac10 | 2018-10-17 18:00:06 +0100 | [diff] [blame] | 648 | return deny_access; |
Andreas Gampe | 80f5fe5 | 2018-03-28 16:23:24 -0700 | [diff] [blame] | 649 | } |
| 650 | |
David Brazdil | e768182 | 2018-12-14 16:25:33 +0000 | [diff] [blame] | 651 | // Need to instantiate these. |
David Brazdil | 1a65863 | 2018-12-01 17:54:26 +0000 | [diff] [blame] | 652 | template uint32_t GetDexFlags<ArtField>(ArtField* member); |
| 653 | template uint32_t GetDexFlags<ArtMethod>(ArtMethod* member); |
David Brazdil | d51e574 | 2019-02-28 14:47:32 +0000 | [diff] [blame] | 654 | template bool HandleCorePlatformApiViolation(ArtField* member, |
| 655 | const AccessContext& caller_context, |
| 656 | AccessMethod access_method, |
| 657 | EnforcementPolicy policy); |
| 658 | template bool HandleCorePlatformApiViolation(ArtMethod* member, |
| 659 | const AccessContext& caller_context, |
| 660 | AccessMethod access_method, |
| 661 | EnforcementPolicy policy); |
David Brazdil | f50ac10 | 2018-10-17 18:00:06 +0100 | [diff] [blame] | 662 | template bool ShouldDenyAccessToMemberImpl<ArtField>(ArtField* member, |
David Brazdil | e768182 | 2018-12-14 16:25:33 +0000 | [diff] [blame] | 663 | ApiList api_list, |
David Brazdil | f50ac10 | 2018-10-17 18:00:06 +0100 | [diff] [blame] | 664 | AccessMethod access_method); |
| 665 | template bool ShouldDenyAccessToMemberImpl<ArtMethod>(ArtMethod* member, |
David Brazdil | e768182 | 2018-12-14 16:25:33 +0000 | [diff] [blame] | 666 | ApiList api_list, |
David Brazdil | f50ac10 | 2018-10-17 18:00:06 +0100 | [diff] [blame] | 667 | AccessMethod access_method); |
Andreas Gampe | 80f5fe5 | 2018-03-28 16:23:24 -0700 | [diff] [blame] | 668 | } // namespace detail |
Narayan Kamath | e453a8d | 2018-04-03 15:23:46 +0100 | [diff] [blame] | 669 | |
Vladimir Marko | 492649b | 2021-10-11 14:21:22 +0100 | [diff] [blame] | 670 | template<typename T> |
| 671 | bool ShouldDenyAccessToMember(T* member, |
| 672 | const std::function<AccessContext()>& fn_get_access_context, |
| 673 | AccessMethod access_method) { |
| 674 | DCHECK(member != nullptr); |
| 675 | |
| 676 | // First check if we have an explicit sdk checker installed that should be used to |
| 677 | // verify access. If so, make the decision based on it. |
| 678 | // |
| 679 | // This is used during off-device AOT compilation which may want to generate verification |
| 680 | // metadata only for a specific list of public SDKs. Note that the check here is made |
| 681 | // based on descriptor equality and it's aim to further restrict a symbol that would |
| 682 | // otherwise be resolved. |
| 683 | // |
| 684 | // The check only applies to boot classpaths dex files. |
| 685 | Runtime* runtime = Runtime::Current(); |
| 686 | if (UNLIKELY(runtime->IsAotCompiler())) { |
| 687 | if (member->GetDeclaringClass()->GetClassLoader() == nullptr && |
| 688 | runtime->GetClassLinker()->DenyAccessBasedOnPublicSdk(member)) { |
| 689 | return true; |
| 690 | } |
| 691 | } |
| 692 | |
| 693 | // Get the runtime flags encoded in member's access flags. |
| 694 | // Note: this works for proxy methods because they inherit access flags from their |
| 695 | // respective interface methods. |
| 696 | const uint32_t runtime_flags = GetRuntimeFlags(member); |
| 697 | |
| 698 | // Exit early if member is public API. This flag is also set for non-boot class |
| 699 | // path fields/methods. |
| 700 | if ((runtime_flags & kAccPublicApi) != 0) { |
| 701 | return false; |
| 702 | } |
| 703 | |
| 704 | // Determine which domain the caller and callee belong to. |
| 705 | // This can be *very* expensive. This is why ShouldDenyAccessToMember |
| 706 | // should not be called on every individual access. |
| 707 | const AccessContext caller_context = fn_get_access_context(); |
| 708 | const AccessContext callee_context(member->GetDeclaringClass()); |
| 709 | |
| 710 | // Non-boot classpath callers should have exited early. |
| 711 | DCHECK(!callee_context.IsApplicationDomain()); |
| 712 | |
| 713 | // Check if the caller is always allowed to access members in the callee context. |
| 714 | if (caller_context.CanAlwaysAccess(callee_context)) { |
| 715 | return false; |
| 716 | } |
| 717 | |
| 718 | // Check if this is platform accessing core platform. We may warn if `member` is |
| 719 | // not part of core platform API. |
| 720 | switch (caller_context.GetDomain()) { |
| 721 | case Domain::kApplication: { |
| 722 | DCHECK(!callee_context.IsApplicationDomain()); |
| 723 | |
| 724 | // Exit early if access checks are completely disabled. |
| 725 | EnforcementPolicy policy = runtime->GetHiddenApiEnforcementPolicy(); |
| 726 | if (policy == EnforcementPolicy::kDisabled) { |
| 727 | return false; |
| 728 | } |
| 729 | |
| 730 | // If this is a proxy method, look at the interface method instead. |
| 731 | member = detail::GetInterfaceMemberIfProxy(member); |
| 732 | |
| 733 | // Decode hidden API access flags from the dex file. |
| 734 | // This is an O(N) operation scaling with the number of fields/methods |
| 735 | // in the class. Only do this on slow path and only do it once. |
| 736 | ApiList api_list(detail::GetDexFlags(member)); |
| 737 | DCHECK(api_list.IsValid()); |
| 738 | |
| 739 | // Member is hidden and caller is not exempted. Enter slow path. |
| 740 | return detail::ShouldDenyAccessToMemberImpl(member, api_list, access_method); |
| 741 | } |
| 742 | |
| 743 | case Domain::kPlatform: { |
| 744 | DCHECK(callee_context.GetDomain() == Domain::kCorePlatform); |
| 745 | |
| 746 | // Member is part of core platform API. Accessing it is allowed. |
| 747 | if ((runtime_flags & kAccCorePlatformApi) != 0) { |
| 748 | return false; |
| 749 | } |
| 750 | |
| 751 | // Allow access if access checks are disabled. |
| 752 | EnforcementPolicy policy = Runtime::Current()->GetCorePlatformApiEnforcementPolicy(); |
| 753 | if (policy == EnforcementPolicy::kDisabled) { |
| 754 | return false; |
| 755 | } |
| 756 | |
| 757 | // If this is a proxy method, look at the interface method instead. |
| 758 | member = detail::GetInterfaceMemberIfProxy(member); |
| 759 | |
| 760 | // Access checks are not disabled, report the violation. |
| 761 | // This may also add kAccCorePlatformApi to the access flags of `member` |
| 762 | // so as to not warn again on next access. |
| 763 | return detail::HandleCorePlatformApiViolation(member, |
| 764 | caller_context, |
| 765 | access_method, |
| 766 | policy); |
| 767 | } |
| 768 | |
| 769 | case Domain::kCorePlatform: { |
| 770 | LOG(FATAL) << "CorePlatform domain should be allowed to access all domains"; |
| 771 | UNREACHABLE(); |
| 772 | } |
| 773 | } |
| 774 | } |
| 775 | |
| 776 | // Need to instantiate these. |
| 777 | template bool ShouldDenyAccessToMember<ArtField>( |
| 778 | ArtField* member, |
| 779 | const std::function<AccessContext()>& fn_get_access_context, |
| 780 | AccessMethod access_method); |
| 781 | template bool ShouldDenyAccessToMember<ArtMethod>( |
| 782 | ArtMethod* member, |
| 783 | const std::function<AccessContext()>& fn_get_access_context, |
| 784 | AccessMethod access_method); |
| 785 | |
Andreas Gampe | 80f5fe5 | 2018-03-28 16:23:24 -0700 | [diff] [blame] | 786 | } // namespace hiddenapi |
| 787 | } // namespace art |