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 | |
Mathew Inwood | 73ddda4 | 2018-04-03 15:32:32 +0100 | [diff] [blame^] | 17 | #include <log/log_event_list.h> |
| 18 | |
Andreas Gampe | 80f5fe5 | 2018-03-28 16:23:24 -0700 | [diff] [blame] | 19 | #include "hidden_api.h" |
| 20 | |
Narayan Kamath | e453a8d | 2018-04-03 15:23:46 +0100 | [diff] [blame] | 21 | #include <nativehelper/scoped_local_ref.h> |
| 22 | |
Andreas Gampe | 80f5fe5 | 2018-03-28 16:23:24 -0700 | [diff] [blame] | 23 | #include "base/dumpable.h" |
Narayan Kamath | e453a8d | 2018-04-03 15:23:46 +0100 | [diff] [blame] | 24 | #include "thread-current-inl.h" |
| 25 | #include "well_known_classes.h" |
Andreas Gampe | 80f5fe5 | 2018-03-28 16:23:24 -0700 | [diff] [blame] | 26 | |
| 27 | namespace art { |
| 28 | namespace hiddenapi { |
| 29 | |
| 30 | static inline std::ostream& operator<<(std::ostream& os, AccessMethod value) { |
| 31 | switch (value) { |
David Brazdil | 54a99cf | 2018-04-05 16:57:32 +0100 | [diff] [blame] | 32 | case kNone: |
| 33 | LOG(FATAL) << "Internal access to hidden API should not be logged"; |
| 34 | UNREACHABLE(); |
Andreas Gampe | 80f5fe5 | 2018-03-28 16:23:24 -0700 | [diff] [blame] | 35 | case kReflection: |
| 36 | os << "reflection"; |
| 37 | break; |
| 38 | case kJNI: |
| 39 | os << "JNI"; |
| 40 | break; |
| 41 | case kLinking: |
| 42 | os << "linking"; |
| 43 | break; |
| 44 | } |
| 45 | return os; |
| 46 | } |
| 47 | |
| 48 | static constexpr bool EnumsEqual(EnforcementPolicy policy, HiddenApiAccessFlags::ApiList apiList) { |
| 49 | return static_cast<int>(policy) == static_cast<int>(apiList); |
| 50 | } |
| 51 | |
| 52 | // GetMemberAction-related static_asserts. |
| 53 | static_assert( |
Andreas Gampe | 80f5fe5 | 2018-03-28 16:23:24 -0700 | [diff] [blame] | 54 | EnumsEqual(EnforcementPolicy::kDarkGreyAndBlackList, HiddenApiAccessFlags::kDarkGreylist) && |
| 55 | EnumsEqual(EnforcementPolicy::kBlacklistOnly, HiddenApiAccessFlags::kBlacklist), |
| 56 | "Mismatch between EnforcementPolicy and ApiList enums"); |
| 57 | static_assert( |
Mathew Inwood | 6869369 | 2018-04-05 16:10:25 +0100 | [diff] [blame] | 58 | EnforcementPolicy::kJustWarn < EnforcementPolicy::kDarkGreyAndBlackList && |
Andreas Gampe | 80f5fe5 | 2018-03-28 16:23:24 -0700 | [diff] [blame] | 59 | EnforcementPolicy::kDarkGreyAndBlackList < EnforcementPolicy::kBlacklistOnly, |
| 60 | "EnforcementPolicy values ordering not correct"); |
| 61 | |
| 62 | namespace detail { |
| 63 | |
Mathew Inwood | 73ddda4 | 2018-04-03 15:32:32 +0100 | [diff] [blame^] | 64 | // This is the ID of the event log event. It is duplicated from |
| 65 | // system/core/logcat/event.logtags |
| 66 | constexpr int EVENT_LOG_TAG_art_hidden_api_access = 20004; |
| 67 | |
Andreas Gampe | 80f5fe5 | 2018-03-28 16:23:24 -0700 | [diff] [blame] | 68 | MemberSignature::MemberSignature(ArtField* field) { |
Mathew Inwood | 73ddda4 | 2018-04-03 15:32:32 +0100 | [diff] [blame^] | 69 | class_name_ = field->GetDeclaringClass()->GetDescriptor(&tmp_); |
| 70 | member_name_ = field->GetName(); |
| 71 | type_signature_ = field->GetTypeDescriptor(); |
| 72 | type_ = kField; |
Andreas Gampe | 80f5fe5 | 2018-03-28 16:23:24 -0700 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | MemberSignature::MemberSignature(ArtMethod* method) { |
Mathew Inwood | 73ddda4 | 2018-04-03 15:32:32 +0100 | [diff] [blame^] | 76 | class_name_ = method->GetDeclaringClass()->GetDescriptor(&tmp_); |
| 77 | member_name_ = method->GetName(); |
| 78 | type_signature_ = method->GetSignature().ToString(); |
| 79 | type_ = kMethod; |
| 80 | } |
| 81 | |
| 82 | inline std::vector<const char*> MemberSignature::GetSignatureParts() const { |
| 83 | if (type_ == kField) { |
| 84 | return { class_name_.c_str(), "->", member_name_.c_str(), ":", type_signature_.c_str() }; |
| 85 | } else { |
| 86 | DCHECK_EQ(type_, kMethod); |
| 87 | return { class_name_.c_str(), "->", member_name_.c_str(), type_signature_.c_str() }; |
| 88 | } |
Andreas Gampe | 80f5fe5 | 2018-03-28 16:23:24 -0700 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | bool MemberSignature::DoesPrefixMatch(const std::string& prefix) const { |
| 92 | size_t pos = 0; |
Mathew Inwood | 73ddda4 | 2018-04-03 15:32:32 +0100 | [diff] [blame^] | 93 | for (const char* part : GetSignatureParts()) { |
| 94 | size_t count = std::min(prefix.length() - pos, strlen(part)); |
Andreas Gampe | 80f5fe5 | 2018-03-28 16:23:24 -0700 | [diff] [blame] | 95 | if (prefix.compare(pos, count, part, 0, count) == 0) { |
| 96 | pos += count; |
| 97 | } else { |
| 98 | return false; |
| 99 | } |
| 100 | } |
| 101 | // We have a complete match if all parts match (we exit the loop without |
| 102 | // returning) AND we've matched the whole prefix. |
| 103 | return pos == prefix.length(); |
| 104 | } |
| 105 | |
| 106 | bool MemberSignature::IsExempted(const std::vector<std::string>& exemptions) { |
| 107 | for (const std::string& exemption : exemptions) { |
| 108 | if (DoesPrefixMatch(exemption)) { |
| 109 | return true; |
| 110 | } |
| 111 | } |
| 112 | return false; |
| 113 | } |
| 114 | |
| 115 | void MemberSignature::Dump(std::ostream& os) const { |
Mathew Inwood | 73ddda4 | 2018-04-03 15:32:32 +0100 | [diff] [blame^] | 116 | for (const char* part : GetSignatureParts()) { |
Andreas Gampe | 80f5fe5 | 2018-03-28 16:23:24 -0700 | [diff] [blame] | 117 | os << part; |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | void MemberSignature::WarnAboutAccess(AccessMethod access_method, |
| 122 | HiddenApiAccessFlags::ApiList list) { |
Mathew Inwood | 73ddda4 | 2018-04-03 15:32:32 +0100 | [diff] [blame^] | 123 | LOG(WARNING) << "Accessing hidden " << (type_ == kField ? "field " : "method ") |
| 124 | << Dumpable<MemberSignature>(*this) << " (" << list << ", " << access_method << ")"; |
| 125 | } |
| 126 | |
| 127 | void MemberSignature::LogAccessToEventLog(AccessMethod access_method, Action action_taken) { |
| 128 | if (access_method == kLinking) { |
| 129 | // Linking warnings come from static analysis/compilation of the bytecode |
| 130 | // and can contain false positives (i.e. code that is never run). We choose |
| 131 | // not to log these in the event log. |
| 132 | return; |
| 133 | } |
| 134 | uint32_t flags = 0; |
| 135 | if (action_taken == kDeny) { |
| 136 | flags |= kAccessDenied; |
| 137 | } |
| 138 | if (type_ == kField) { |
| 139 | flags |= kMemberIsField; |
| 140 | } |
| 141 | android_log_event_list ctx(EVENT_LOG_TAG_art_hidden_api_access); |
| 142 | ctx << access_method; |
| 143 | ctx << flags; |
| 144 | ctx << class_name_; |
| 145 | ctx << member_name_; |
| 146 | ctx << type_signature_; |
| 147 | ctx << LOG_ID_EVENTS; |
Andreas Gampe | 80f5fe5 | 2018-03-28 16:23:24 -0700 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | template<typename T> |
Narayan Kamath | e453a8d | 2018-04-03 15:23:46 +0100 | [diff] [blame] | 151 | Action GetMemberActionImpl(T* member, Action action, AccessMethod access_method) { |
David Brazdil | 54a99cf | 2018-04-05 16:57:32 +0100 | [diff] [blame] | 152 | DCHECK_NE(action, kAllow); |
| 153 | |
Andreas Gampe | 80f5fe5 | 2018-03-28 16:23:24 -0700 | [diff] [blame] | 154 | // Get the signature, we need it later. |
| 155 | MemberSignature member_signature(member); |
| 156 | |
| 157 | Runtime* runtime = Runtime::Current(); |
| 158 | |
Mathew Inwood | c8ce5f5 | 2018-04-05 13:58:55 +0100 | [diff] [blame] | 159 | // Check for an exemption first. Exempted APIs are treated as white list. |
| 160 | // We only do this if we're about to deny, or if the app is debuggable. This is because: |
| 161 | // - we only print a warning for light greylist violations for debuggable apps |
| 162 | // - for non-debuggable apps, there is no distinction between light grey & whitelisted APIs. |
| 163 | // - we want to avoid the overhead of checking for exemptions for light greylisted APIs whenever |
| 164 | // possible. |
| 165 | if (action == kDeny || runtime->IsJavaDebuggable()) { |
Andreas Gampe | 80f5fe5 | 2018-03-28 16:23:24 -0700 | [diff] [blame] | 166 | if (member_signature.IsExempted(runtime->GetHiddenApiExemptions())) { |
Mathew Inwood | c8ce5f5 | 2018-04-05 13:58:55 +0100 | [diff] [blame] | 167 | action = kAllow; |
Andreas Gampe | 80f5fe5 | 2018-03-28 16:23:24 -0700 | [diff] [blame] | 168 | // Avoid re-examining the exemption list next time. |
Mathew Inwood | c8ce5f5 | 2018-04-05 13:58:55 +0100 | [diff] [blame] | 169 | // Note this results in no warning for the member, which seems like what one would expect. |
| 170 | // Exemptions effectively adds new members to the whitelist. |
Andreas Gampe | 80f5fe5 | 2018-03-28 16:23:24 -0700 | [diff] [blame] | 171 | member->SetAccessFlags(HiddenApiAccessFlags::EncodeForRuntime( |
Mathew Inwood | c8ce5f5 | 2018-04-05 13:58:55 +0100 | [diff] [blame] | 172 | member->GetAccessFlags(), HiddenApiAccessFlags::kWhitelist)); |
| 173 | return kAllow; |
Andreas Gampe | 80f5fe5 | 2018-03-28 16:23:24 -0700 | [diff] [blame] | 174 | } |
Andreas Gampe | 80f5fe5 | 2018-03-28 16:23:24 -0700 | [diff] [blame] | 175 | |
Mathew Inwood | c8ce5f5 | 2018-04-05 13:58:55 +0100 | [diff] [blame] | 176 | if (access_method != kNone) { |
| 177 | // Print a log message with information about this class member access. |
| 178 | // We do this if we're about to block access, or the app is debuggable. |
| 179 | member_signature.WarnAboutAccess(access_method, |
| 180 | HiddenApiAccessFlags::DecodeFromRuntime(member->GetAccessFlags())); |
| 181 | } |
David Brazdil | 54a99cf | 2018-04-05 16:57:32 +0100 | [diff] [blame] | 182 | } |
Andreas Gampe | 80f5fe5 | 2018-03-28 16:23:24 -0700 | [diff] [blame] | 183 | |
Mathew Inwood | 73ddda4 | 2018-04-03 15:32:32 +0100 | [diff] [blame^] | 184 | if (kIsTargetBuild) { |
| 185 | uint32_t eventLogSampleRate = runtime->GetHiddenApiEventLogSampleRate(); |
| 186 | // Assert that RAND_MAX is big enough, to ensure sampling below works as expected. |
| 187 | static_assert(RAND_MAX >= 0xffff, "RAND_MAX too small"); |
| 188 | if (eventLogSampleRate != 0 && |
| 189 | (static_cast<uint32_t>(std::rand()) & 0xffff) < eventLogSampleRate) { |
| 190 | member_signature.LogAccessToEventLog(access_method, action); |
| 191 | } |
| 192 | } |
| 193 | |
Andreas Gampe | 80f5fe5 | 2018-03-28 16:23:24 -0700 | [diff] [blame] | 194 | if (action == kDeny) { |
| 195 | // Block access |
Narayan Kamath | e453a8d | 2018-04-03 15:23:46 +0100 | [diff] [blame] | 196 | return action; |
Andreas Gampe | 80f5fe5 | 2018-03-28 16:23:24 -0700 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | // Allow access to this member but print a warning. |
| 200 | DCHECK(action == kAllowButWarn || action == kAllowButWarnAndToast); |
| 201 | |
David Brazdil | 54a99cf | 2018-04-05 16:57:32 +0100 | [diff] [blame] | 202 | if (access_method != kNone) { |
| 203 | // Depending on a runtime flag, we might move the member into whitelist and |
| 204 | // skip the warning the next time the member is accessed. |
| 205 | if (runtime->ShouldDedupeHiddenApiWarnings()) { |
| 206 | member->SetAccessFlags(HiddenApiAccessFlags::EncodeForRuntime( |
| 207 | member->GetAccessFlags(), HiddenApiAccessFlags::kWhitelist)); |
| 208 | } |
Andreas Gampe | 80f5fe5 | 2018-03-28 16:23:24 -0700 | [diff] [blame] | 209 | |
David Brazdil | 54a99cf | 2018-04-05 16:57:32 +0100 | [diff] [blame] | 210 | // If this action requires a UI warning, set the appropriate flag. |
| 211 | if (action == kAllowButWarnAndToast || runtime->ShouldAlwaysSetHiddenApiWarningFlag()) { |
| 212 | runtime->SetPendingHiddenApiWarning(true); |
| 213 | } |
Andreas Gampe | 80f5fe5 | 2018-03-28 16:23:24 -0700 | [diff] [blame] | 214 | } |
| 215 | |
Narayan Kamath | e453a8d | 2018-04-03 15:23:46 +0100 | [diff] [blame] | 216 | return action; |
Andreas Gampe | 80f5fe5 | 2018-03-28 16:23:24 -0700 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | // Need to instantiate this. |
Narayan Kamath | e453a8d | 2018-04-03 15:23:46 +0100 | [diff] [blame] | 220 | template Action GetMemberActionImpl<ArtField>(ArtField* member, |
| 221 | Action action, |
| 222 | AccessMethod access_method); |
| 223 | template Action GetMemberActionImpl<ArtMethod>(ArtMethod* member, |
| 224 | Action action, |
| 225 | AccessMethod access_method); |
Andreas Gampe | 80f5fe5 | 2018-03-28 16:23:24 -0700 | [diff] [blame] | 226 | } // namespace detail |
Narayan Kamath | e453a8d | 2018-04-03 15:23:46 +0100 | [diff] [blame] | 227 | |
| 228 | template<typename T> |
| 229 | void NotifyHiddenApiListener(T* member) { |
| 230 | Runtime* runtime = Runtime::Current(); |
| 231 | if (!runtime->IsAotCompiler()) { |
| 232 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
| 233 | |
| 234 | ScopedLocalRef<jobject> consumer_object(soa.Env(), |
| 235 | soa.Env()->GetStaticObjectField( |
| 236 | WellKnownClasses::dalvik_system_VMRuntime, |
| 237 | WellKnownClasses::dalvik_system_VMRuntime_nonSdkApiUsageConsumer)); |
| 238 | // If the consumer is non-null, we call back to it to let it know that we |
| 239 | // have encountered an API that's in one of our lists. |
| 240 | if (consumer_object != nullptr) { |
| 241 | detail::MemberSignature member_signature(member); |
| 242 | std::ostringstream member_signature_str; |
| 243 | member_signature.Dump(member_signature_str); |
| 244 | |
| 245 | ScopedLocalRef<jobject> signature_str( |
| 246 | soa.Env(), |
| 247 | soa.Env()->NewStringUTF(member_signature_str.str().c_str())); |
| 248 | |
| 249 | // Call through to Consumer.accept(String memberSignature); |
| 250 | soa.Env()->CallVoidMethod(consumer_object.get(), |
| 251 | WellKnownClasses::java_util_function_Consumer_accept, |
| 252 | signature_str.get()); |
| 253 | } |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | template void NotifyHiddenApiListener<ArtMethod>(ArtMethod* member); |
| 258 | template void NotifyHiddenApiListener<ArtField>(ArtField* member); |
| 259 | |
Andreas Gampe | 80f5fe5 | 2018-03-28 16:23:24 -0700 | [diff] [blame] | 260 | } // namespace hiddenapi |
| 261 | } // namespace art |