blob: c2fd4ee3298bfd12f3522999d9b5679ab284db1e [file] [log] [blame]
Andreas Gampe80f5fe52018-03-28 16:23:24 -07001/*
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 Inwood73ddda42018-04-03 15:32:32 +010017#include <log/log_event_list.h>
18
Andreas Gampe80f5fe52018-03-28 16:23:24 -070019#include "hidden_api.h"
20
Narayan Kamathe453a8d2018-04-03 15:23:46 +010021#include <nativehelper/scoped_local_ref.h>
22
Andreas Gampe80f5fe52018-03-28 16:23:24 -070023#include "base/dumpable.h"
Narayan Kamathe453a8d2018-04-03 15:23:46 +010024#include "thread-current-inl.h"
25#include "well_known_classes.h"
Andreas Gampe80f5fe52018-03-28 16:23:24 -070026
27namespace art {
28namespace hiddenapi {
29
Mathew Inwood27199e62018-04-11 16:08:21 +010030// Set to true if we should always print a warning in logcat for all hidden API accesses, not just
31// dark grey and black. This can be set to true for developer preview / beta builds, but should be
32// false for public release builds.
33static constexpr bool kLogAllAccesses = true;
34
Andreas Gampe80f5fe52018-03-28 16:23:24 -070035static inline std::ostream& operator<<(std::ostream& os, AccessMethod value) {
36 switch (value) {
David Brazdil54a99cf2018-04-05 16:57:32 +010037 case kNone:
38 LOG(FATAL) << "Internal access to hidden API should not be logged";
39 UNREACHABLE();
Andreas Gampe80f5fe52018-03-28 16:23:24 -070040 case kReflection:
41 os << "reflection";
42 break;
43 case kJNI:
44 os << "JNI";
45 break;
46 case kLinking:
47 os << "linking";
48 break;
49 }
50 return os;
51}
52
53static constexpr bool EnumsEqual(EnforcementPolicy policy, HiddenApiAccessFlags::ApiList apiList) {
54 return static_cast<int>(policy) == static_cast<int>(apiList);
55}
56
57// GetMemberAction-related static_asserts.
58static_assert(
Andreas Gampe80f5fe52018-03-28 16:23:24 -070059 EnumsEqual(EnforcementPolicy::kDarkGreyAndBlackList, HiddenApiAccessFlags::kDarkGreylist) &&
60 EnumsEqual(EnforcementPolicy::kBlacklistOnly, HiddenApiAccessFlags::kBlacklist),
61 "Mismatch between EnforcementPolicy and ApiList enums");
62static_assert(
Mathew Inwood68693692018-04-05 16:10:25 +010063 EnforcementPolicy::kJustWarn < EnforcementPolicy::kDarkGreyAndBlackList &&
Andreas Gampe80f5fe52018-03-28 16:23:24 -070064 EnforcementPolicy::kDarkGreyAndBlackList < EnforcementPolicy::kBlacklistOnly,
65 "EnforcementPolicy values ordering not correct");
66
67namespace detail {
68
Mathew Inwood73ddda42018-04-03 15:32:32 +010069// This is the ID of the event log event. It is duplicated from
70// system/core/logcat/event.logtags
71constexpr int EVENT_LOG_TAG_art_hidden_api_access = 20004;
72
Andreas Gampe80f5fe52018-03-28 16:23:24 -070073MemberSignature::MemberSignature(ArtField* field) {
Mathew Inwood73ddda42018-04-03 15:32:32 +010074 class_name_ = field->GetDeclaringClass()->GetDescriptor(&tmp_);
75 member_name_ = field->GetName();
76 type_signature_ = field->GetTypeDescriptor();
77 type_ = kField;
Andreas Gampe80f5fe52018-03-28 16:23:24 -070078}
79
80MemberSignature::MemberSignature(ArtMethod* method) {
Mathew Inwood73ddda42018-04-03 15:32:32 +010081 class_name_ = method->GetDeclaringClass()->GetDescriptor(&tmp_);
82 member_name_ = method->GetName();
83 type_signature_ = method->GetSignature().ToString();
84 type_ = kMethod;
85}
86
87inline std::vector<const char*> MemberSignature::GetSignatureParts() const {
88 if (type_ == kField) {
89 return { class_name_.c_str(), "->", member_name_.c_str(), ":", type_signature_.c_str() };
90 } else {
91 DCHECK_EQ(type_, kMethod);
92 return { class_name_.c_str(), "->", member_name_.c_str(), type_signature_.c_str() };
93 }
Andreas Gampe80f5fe52018-03-28 16:23:24 -070094}
95
96bool MemberSignature::DoesPrefixMatch(const std::string& prefix) const {
97 size_t pos = 0;
Mathew Inwood73ddda42018-04-03 15:32:32 +010098 for (const char* part : GetSignatureParts()) {
99 size_t count = std::min(prefix.length() - pos, strlen(part));
Andreas Gampe80f5fe52018-03-28 16:23:24 -0700100 if (prefix.compare(pos, count, part, 0, count) == 0) {
101 pos += count;
102 } else {
103 return false;
104 }
105 }
106 // We have a complete match if all parts match (we exit the loop without
107 // returning) AND we've matched the whole prefix.
108 return pos == prefix.length();
109}
110
111bool MemberSignature::IsExempted(const std::vector<std::string>& exemptions) {
112 for (const std::string& exemption : exemptions) {
113 if (DoesPrefixMatch(exemption)) {
114 return true;
115 }
116 }
117 return false;
118}
119
120void MemberSignature::Dump(std::ostream& os) const {
Mathew Inwood73ddda42018-04-03 15:32:32 +0100121 for (const char* part : GetSignatureParts()) {
Andreas Gampe80f5fe52018-03-28 16:23:24 -0700122 os << part;
123 }
124}
125
126void MemberSignature::WarnAboutAccess(AccessMethod access_method,
127 HiddenApiAccessFlags::ApiList list) {
Mathew Inwood73ddda42018-04-03 15:32:32 +0100128 LOG(WARNING) << "Accessing hidden " << (type_ == kField ? "field " : "method ")
129 << Dumpable<MemberSignature>(*this) << " (" << list << ", " << access_method << ")";
130}
131
132void MemberSignature::LogAccessToEventLog(AccessMethod access_method, Action action_taken) {
133 if (access_method == kLinking) {
134 // Linking warnings come from static analysis/compilation of the bytecode
135 // and can contain false positives (i.e. code that is never run). We choose
136 // not to log these in the event log.
137 return;
138 }
139 uint32_t flags = 0;
140 if (action_taken == kDeny) {
141 flags |= kAccessDenied;
142 }
143 if (type_ == kField) {
144 flags |= kMemberIsField;
145 }
146 android_log_event_list ctx(EVENT_LOG_TAG_art_hidden_api_access);
147 ctx << access_method;
148 ctx << flags;
149 ctx << class_name_;
150 ctx << member_name_;
151 ctx << type_signature_;
152 ctx << LOG_ID_EVENTS;
Andreas Gampe80f5fe52018-03-28 16:23:24 -0700153}
154
155template<typename T>
Narayan Kamathe453a8d2018-04-03 15:23:46 +0100156Action GetMemberActionImpl(T* member, Action action, AccessMethod access_method) {
David Brazdil54a99cf2018-04-05 16:57:32 +0100157 DCHECK_NE(action, kAllow);
158
Andreas Gampe80f5fe52018-03-28 16:23:24 -0700159 // Get the signature, we need it later.
160 MemberSignature member_signature(member);
161
162 Runtime* runtime = Runtime::Current();
163
Mathew Inwoodc8ce5f52018-04-05 13:58:55 +0100164 // Check for an exemption first. Exempted APIs are treated as white list.
165 // We only do this if we're about to deny, or if the app is debuggable. This is because:
166 // - we only print a warning for light greylist violations for debuggable apps
167 // - for non-debuggable apps, there is no distinction between light grey & whitelisted APIs.
168 // - we want to avoid the overhead of checking for exemptions for light greylisted APIs whenever
169 // possible.
Mathew Inwood27199e62018-04-11 16:08:21 +0100170 if (kLogAllAccesses || action == kDeny || runtime->IsJavaDebuggable()) {
Andreas Gampe80f5fe52018-03-28 16:23:24 -0700171 if (member_signature.IsExempted(runtime->GetHiddenApiExemptions())) {
Mathew Inwoodc8ce5f52018-04-05 13:58:55 +0100172 action = kAllow;
Andreas Gampe80f5fe52018-03-28 16:23:24 -0700173 // Avoid re-examining the exemption list next time.
Mathew Inwoodc8ce5f52018-04-05 13:58:55 +0100174 // Note this results in no warning for the member, which seems like what one would expect.
175 // Exemptions effectively adds new members to the whitelist.
Mathew Inwood64ee8ae2018-04-09 12:24:55 +0100176 if (runtime->ShouldDedupeHiddenApiWarnings()) {
177 member->SetAccessFlags(HiddenApiAccessFlags::EncodeForRuntime(
178 member->GetAccessFlags(), HiddenApiAccessFlags::kWhitelist));
179 }
Mathew Inwoodc8ce5f52018-04-05 13:58:55 +0100180 return kAllow;
Andreas Gampe80f5fe52018-03-28 16:23:24 -0700181 }
Andreas Gampe80f5fe52018-03-28 16:23:24 -0700182
Mathew Inwoodc8ce5f52018-04-05 13:58:55 +0100183 if (access_method != kNone) {
184 // Print a log message with information about this class member access.
185 // We do this if we're about to block access, or the app is debuggable.
186 member_signature.WarnAboutAccess(access_method,
187 HiddenApiAccessFlags::DecodeFromRuntime(member->GetAccessFlags()));
188 }
David Brazdil54a99cf2018-04-05 16:57:32 +0100189 }
Andreas Gampe80f5fe52018-03-28 16:23:24 -0700190
Mathew Inwood73ddda42018-04-03 15:32:32 +0100191 if (kIsTargetBuild) {
192 uint32_t eventLogSampleRate = runtime->GetHiddenApiEventLogSampleRate();
193 // Assert that RAND_MAX is big enough, to ensure sampling below works as expected.
194 static_assert(RAND_MAX >= 0xffff, "RAND_MAX too small");
195 if (eventLogSampleRate != 0 &&
196 (static_cast<uint32_t>(std::rand()) & 0xffff) < eventLogSampleRate) {
197 member_signature.LogAccessToEventLog(access_method, action);
198 }
199 }
200
Andreas Gampe80f5fe52018-03-28 16:23:24 -0700201 if (action == kDeny) {
202 // Block access
Narayan Kamathe453a8d2018-04-03 15:23:46 +0100203 return action;
Andreas Gampe80f5fe52018-03-28 16:23:24 -0700204 }
205
206 // Allow access to this member but print a warning.
207 DCHECK(action == kAllowButWarn || action == kAllowButWarnAndToast);
208
David Brazdil54a99cf2018-04-05 16:57:32 +0100209 if (access_method != kNone) {
210 // Depending on a runtime flag, we might move the member into whitelist and
211 // skip the warning the next time the member is accessed.
212 if (runtime->ShouldDedupeHiddenApiWarnings()) {
213 member->SetAccessFlags(HiddenApiAccessFlags::EncodeForRuntime(
214 member->GetAccessFlags(), HiddenApiAccessFlags::kWhitelist));
215 }
Andreas Gampe80f5fe52018-03-28 16:23:24 -0700216
David Brazdil54a99cf2018-04-05 16:57:32 +0100217 // If this action requires a UI warning, set the appropriate flag.
218 if (action == kAllowButWarnAndToast || runtime->ShouldAlwaysSetHiddenApiWarningFlag()) {
219 runtime->SetPendingHiddenApiWarning(true);
220 }
Andreas Gampe80f5fe52018-03-28 16:23:24 -0700221 }
222
Narayan Kamathe453a8d2018-04-03 15:23:46 +0100223 return action;
Andreas Gampe80f5fe52018-03-28 16:23:24 -0700224}
225
226// Need to instantiate this.
Narayan Kamathe453a8d2018-04-03 15:23:46 +0100227template Action GetMemberActionImpl<ArtField>(ArtField* member,
228 Action action,
229 AccessMethod access_method);
230template Action GetMemberActionImpl<ArtMethod>(ArtMethod* member,
231 Action action,
232 AccessMethod access_method);
Andreas Gampe80f5fe52018-03-28 16:23:24 -0700233} // namespace detail
Narayan Kamathe453a8d2018-04-03 15:23:46 +0100234
235template<typename T>
236void NotifyHiddenApiListener(T* member) {
237 Runtime* runtime = Runtime::Current();
238 if (!runtime->IsAotCompiler()) {
239 ScopedObjectAccessUnchecked soa(Thread::Current());
240
241 ScopedLocalRef<jobject> consumer_object(soa.Env(),
242 soa.Env()->GetStaticObjectField(
243 WellKnownClasses::dalvik_system_VMRuntime,
244 WellKnownClasses::dalvik_system_VMRuntime_nonSdkApiUsageConsumer));
245 // If the consumer is non-null, we call back to it to let it know that we
246 // have encountered an API that's in one of our lists.
247 if (consumer_object != nullptr) {
248 detail::MemberSignature member_signature(member);
249 std::ostringstream member_signature_str;
250 member_signature.Dump(member_signature_str);
251
252 ScopedLocalRef<jobject> signature_str(
253 soa.Env(),
254 soa.Env()->NewStringUTF(member_signature_str.str().c_str()));
255
256 // Call through to Consumer.accept(String memberSignature);
257 soa.Env()->CallVoidMethod(consumer_object.get(),
258 WellKnownClasses::java_util_function_Consumer_accept,
259 signature_str.get());
260 }
261 }
262}
263
264template void NotifyHiddenApiListener<ArtMethod>(ArtMethod* member);
265template void NotifyHiddenApiListener<ArtField>(ArtField* member);
266
Andreas Gampe80f5fe52018-03-28 16:23:24 -0700267} // namespace hiddenapi
268} // namespace art