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