blob: d600f3139becaead7fa922b81e127c0187d36c11 [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
30static inline std::ostream& operator<<(std::ostream& os, AccessMethod value) {
31 switch (value) {
David Brazdil54a99cf2018-04-05 16:57:32 +010032 case kNone:
33 LOG(FATAL) << "Internal access to hidden API should not be logged";
34 UNREACHABLE();
Andreas Gampe80f5fe52018-03-28 16:23:24 -070035 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
48static 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.
53static_assert(
Andreas Gampe80f5fe52018-03-28 16:23:24 -070054 EnumsEqual(EnforcementPolicy::kDarkGreyAndBlackList, HiddenApiAccessFlags::kDarkGreylist) &&
55 EnumsEqual(EnforcementPolicy::kBlacklistOnly, HiddenApiAccessFlags::kBlacklist),
56 "Mismatch between EnforcementPolicy and ApiList enums");
57static_assert(
Mathew Inwood68693692018-04-05 16:10:25 +010058 EnforcementPolicy::kJustWarn < EnforcementPolicy::kDarkGreyAndBlackList &&
Andreas Gampe80f5fe52018-03-28 16:23:24 -070059 EnforcementPolicy::kDarkGreyAndBlackList < EnforcementPolicy::kBlacklistOnly,
60 "EnforcementPolicy values ordering not correct");
61
62namespace detail {
63
Mathew Inwood73ddda42018-04-03 15:32:32 +010064// This is the ID of the event log event. It is duplicated from
65// system/core/logcat/event.logtags
66constexpr int EVENT_LOG_TAG_art_hidden_api_access = 20004;
67
Andreas Gampe80f5fe52018-03-28 16:23:24 -070068MemberSignature::MemberSignature(ArtField* field) {
Mathew Inwood73ddda42018-04-03 15:32:32 +010069 class_name_ = field->GetDeclaringClass()->GetDescriptor(&tmp_);
70 member_name_ = field->GetName();
71 type_signature_ = field->GetTypeDescriptor();
72 type_ = kField;
Andreas Gampe80f5fe52018-03-28 16:23:24 -070073}
74
75MemberSignature::MemberSignature(ArtMethod* method) {
Mathew Inwood73ddda42018-04-03 15:32:32 +010076 class_name_ = method->GetDeclaringClass()->GetDescriptor(&tmp_);
77 member_name_ = method->GetName();
78 type_signature_ = method->GetSignature().ToString();
79 type_ = kMethod;
80}
81
82inline 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 Gampe80f5fe52018-03-28 16:23:24 -070089}
90
91bool MemberSignature::DoesPrefixMatch(const std::string& prefix) const {
92 size_t pos = 0;
Mathew Inwood73ddda42018-04-03 15:32:32 +010093 for (const char* part : GetSignatureParts()) {
94 size_t count = std::min(prefix.length() - pos, strlen(part));
Andreas Gampe80f5fe52018-03-28 16:23:24 -070095 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
106bool 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
115void MemberSignature::Dump(std::ostream& os) const {
Mathew Inwood73ddda42018-04-03 15:32:32 +0100116 for (const char* part : GetSignatureParts()) {
Andreas Gampe80f5fe52018-03-28 16:23:24 -0700117 os << part;
118 }
119}
120
121void MemberSignature::WarnAboutAccess(AccessMethod access_method,
122 HiddenApiAccessFlags::ApiList list) {
Mathew Inwood73ddda42018-04-03 15:32:32 +0100123 LOG(WARNING) << "Accessing hidden " << (type_ == kField ? "field " : "method ")
124 << Dumpable<MemberSignature>(*this) << " (" << list << ", " << access_method << ")";
125}
126
127void 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 Gampe80f5fe52018-03-28 16:23:24 -0700148}
149
150template<typename T>
Narayan Kamathe453a8d2018-04-03 15:23:46 +0100151Action GetMemberActionImpl(T* member, Action action, AccessMethod access_method) {
David Brazdil54a99cf2018-04-05 16:57:32 +0100152 DCHECK_NE(action, kAllow);
153
Andreas Gampe80f5fe52018-03-28 16:23:24 -0700154 // Get the signature, we need it later.
155 MemberSignature member_signature(member);
156
157 Runtime* runtime = Runtime::Current();
158
Mathew Inwoodc8ce5f52018-04-05 13:58:55 +0100159 // 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 Gampe80f5fe52018-03-28 16:23:24 -0700166 if (member_signature.IsExempted(runtime->GetHiddenApiExemptions())) {
Mathew Inwoodc8ce5f52018-04-05 13:58:55 +0100167 action = kAllow;
Andreas Gampe80f5fe52018-03-28 16:23:24 -0700168 // Avoid re-examining the exemption list next time.
Mathew Inwoodc8ce5f52018-04-05 13:58:55 +0100169 // 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 Gampe80f5fe52018-03-28 16:23:24 -0700171 member->SetAccessFlags(HiddenApiAccessFlags::EncodeForRuntime(
Mathew Inwoodc8ce5f52018-04-05 13:58:55 +0100172 member->GetAccessFlags(), HiddenApiAccessFlags::kWhitelist));
173 return kAllow;
Andreas Gampe80f5fe52018-03-28 16:23:24 -0700174 }
Andreas Gampe80f5fe52018-03-28 16:23:24 -0700175
Mathew Inwoodc8ce5f52018-04-05 13:58:55 +0100176 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 Brazdil54a99cf2018-04-05 16:57:32 +0100182 }
Andreas Gampe80f5fe52018-03-28 16:23:24 -0700183
Mathew Inwood73ddda42018-04-03 15:32:32 +0100184 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 Gampe80f5fe52018-03-28 16:23:24 -0700194 if (action == kDeny) {
195 // Block access
Narayan Kamathe453a8d2018-04-03 15:23:46 +0100196 return action;
Andreas Gampe80f5fe52018-03-28 16:23:24 -0700197 }
198
199 // Allow access to this member but print a warning.
200 DCHECK(action == kAllowButWarn || action == kAllowButWarnAndToast);
201
David Brazdil54a99cf2018-04-05 16:57:32 +0100202 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 Gampe80f5fe52018-03-28 16:23:24 -0700209
David Brazdil54a99cf2018-04-05 16:57:32 +0100210 // If this action requires a UI warning, set the appropriate flag.
211 if (action == kAllowButWarnAndToast || runtime->ShouldAlwaysSetHiddenApiWarningFlag()) {
212 runtime->SetPendingHiddenApiWarning(true);
213 }
Andreas Gampe80f5fe52018-03-28 16:23:24 -0700214 }
215
Narayan Kamathe453a8d2018-04-03 15:23:46 +0100216 return action;
Andreas Gampe80f5fe52018-03-28 16:23:24 -0700217}
218
219// Need to instantiate this.
Narayan Kamathe453a8d2018-04-03 15:23:46 +0100220template Action GetMemberActionImpl<ArtField>(ArtField* member,
221 Action action,
222 AccessMethod access_method);
223template Action GetMemberActionImpl<ArtMethod>(ArtMethod* member,
224 Action action,
225 AccessMethod access_method);
Andreas Gampe80f5fe52018-03-28 16:23:24 -0700226} // namespace detail
Narayan Kamathe453a8d2018-04-03 15:23:46 +0100227
228template<typename T>
229void 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
257template void NotifyHiddenApiListener<ArtMethod>(ArtMethod* member);
258template void NotifyHiddenApiListener<ArtField>(ArtField* member);
259
Andreas Gampe80f5fe52018-03-28 16:23:24 -0700260} // namespace hiddenapi
261} // namespace art