blob: c5d09cfa7767eadab978ef9bb8921dc9a75d537a [file] [log] [blame]
Andreas Gampeaa120012018-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 Inwood1fd97f22018-04-03 15:32:32 +010017#include <log/log_event_list.h>
18
Andreas Gampeaa120012018-03-28 16:23:24 -070019#include "hidden_api.h"
20
Narayan Kamathf5f1f802018-04-03 15:23:46 +010021#include <nativehelper/scoped_local_ref.h>
22
Andreas Gampeaa120012018-03-28 16:23:24 -070023#include "base/dumpable.h"
Narayan Kamathf5f1f802018-04-03 15:23:46 +010024#include "thread-current-inl.h"
25#include "well_known_classes.h"
Andreas Gampeaa120012018-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 Brazdil4525e0b2018-04-05 16:57:32 +010032 case kNone:
33 LOG(FATAL) << "Internal access to hidden API should not be logged";
34 UNREACHABLE();
Andreas Gampeaa120012018-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 Gampeaa120012018-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 Inwooda8503d92018-04-05 16:10:25 +010058 EnforcementPolicy::kJustWarn < EnforcementPolicy::kDarkGreyAndBlackList &&
Andreas Gampeaa120012018-03-28 16:23:24 -070059 EnforcementPolicy::kDarkGreyAndBlackList < EnforcementPolicy::kBlacklistOnly,
60 "EnforcementPolicy values ordering not correct");
61
62namespace detail {
63
Mathew Inwood1fd97f22018-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 Gampeaa120012018-03-28 16:23:24 -070068MemberSignature::MemberSignature(ArtField* field) {
Mathew Inwood1fd97f22018-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 Gampeaa120012018-03-28 16:23:24 -070073}
74
75MemberSignature::MemberSignature(ArtMethod* method) {
Mathew Inwood1fd97f22018-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 Gampeaa120012018-03-28 16:23:24 -070089}
90
91bool MemberSignature::DoesPrefixMatch(const std::string& prefix) const {
92 size_t pos = 0;
Mathew Inwood1fd97f22018-04-03 15:32:32 +010093 for (const char* part : GetSignatureParts()) {
94 size_t count = std::min(prefix.length() - pos, strlen(part));
Andreas Gampeaa120012018-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 Inwood1fd97f22018-04-03 15:32:32 +0100116 for (const char* part : GetSignatureParts()) {
Andreas Gampeaa120012018-03-28 16:23:24 -0700117 os << part;
118 }
119}
120
121void MemberSignature::WarnAboutAccess(AccessMethod access_method,
122 HiddenApiAccessFlags::ApiList list) {
Mathew Inwood1fd97f22018-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 Gampeaa120012018-03-28 16:23:24 -0700148}
149
150template<typename T>
Narayan Kamathf5f1f802018-04-03 15:23:46 +0100151Action GetMemberActionImpl(T* member, Action action, AccessMethod access_method) {
David Brazdil4525e0b2018-04-05 16:57:32 +0100152 DCHECK_NE(action, kAllow);
153
Andreas Gampeaa120012018-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 Inwood9a819452018-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 Gampeaa120012018-03-28 16:23:24 -0700166 if (member_signature.IsExempted(runtime->GetHiddenApiExemptions())) {
Mathew Inwood9a819452018-04-05 13:58:55 +0100167 action = kAllow;
Andreas Gampeaa120012018-03-28 16:23:24 -0700168 // Avoid re-examining the exemption list next time.
Mathew Inwood9a819452018-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.
Mathew Inwood9a13d422018-04-09 12:24:55 +0100171 if (runtime->ShouldDedupeHiddenApiWarnings()) {
172 member->SetAccessFlags(HiddenApiAccessFlags::EncodeForRuntime(
173 member->GetAccessFlags(), HiddenApiAccessFlags::kWhitelist));
174 }
Mathew Inwood9a819452018-04-05 13:58:55 +0100175 return kAllow;
Andreas Gampeaa120012018-03-28 16:23:24 -0700176 }
Andreas Gampeaa120012018-03-28 16:23:24 -0700177
Mathew Inwood9a819452018-04-05 13:58:55 +0100178 if (access_method != kNone) {
179 // Print a log message with information about this class member access.
180 // We do this if we're about to block access, or the app is debuggable.
181 member_signature.WarnAboutAccess(access_method,
182 HiddenApiAccessFlags::DecodeFromRuntime(member->GetAccessFlags()));
183 }
David Brazdil4525e0b2018-04-05 16:57:32 +0100184 }
Andreas Gampeaa120012018-03-28 16:23:24 -0700185
Mathew Inwood1fd97f22018-04-03 15:32:32 +0100186 if (kIsTargetBuild) {
187 uint32_t eventLogSampleRate = runtime->GetHiddenApiEventLogSampleRate();
188 // Assert that RAND_MAX is big enough, to ensure sampling below works as expected.
189 static_assert(RAND_MAX >= 0xffff, "RAND_MAX too small");
190 if (eventLogSampleRate != 0 &&
191 (static_cast<uint32_t>(std::rand()) & 0xffff) < eventLogSampleRate) {
192 member_signature.LogAccessToEventLog(access_method, action);
193 }
194 }
195
Andreas Gampeaa120012018-03-28 16:23:24 -0700196 if (action == kDeny) {
197 // Block access
Narayan Kamathf5f1f802018-04-03 15:23:46 +0100198 return action;
Andreas Gampeaa120012018-03-28 16:23:24 -0700199 }
200
201 // Allow access to this member but print a warning.
202 DCHECK(action == kAllowButWarn || action == kAllowButWarnAndToast);
203
David Brazdil4525e0b2018-04-05 16:57:32 +0100204 if (access_method != kNone) {
205 // Depending on a runtime flag, we might move the member into whitelist and
206 // skip the warning the next time the member is accessed.
207 if (runtime->ShouldDedupeHiddenApiWarnings()) {
208 member->SetAccessFlags(HiddenApiAccessFlags::EncodeForRuntime(
209 member->GetAccessFlags(), HiddenApiAccessFlags::kWhitelist));
210 }
Andreas Gampeaa120012018-03-28 16:23:24 -0700211
David Brazdil4525e0b2018-04-05 16:57:32 +0100212 // If this action requires a UI warning, set the appropriate flag.
213 if (action == kAllowButWarnAndToast || runtime->ShouldAlwaysSetHiddenApiWarningFlag()) {
214 runtime->SetPendingHiddenApiWarning(true);
215 }
Andreas Gampeaa120012018-03-28 16:23:24 -0700216 }
217
Narayan Kamathf5f1f802018-04-03 15:23:46 +0100218 return action;
Andreas Gampeaa120012018-03-28 16:23:24 -0700219}
220
221// Need to instantiate this.
Narayan Kamathf5f1f802018-04-03 15:23:46 +0100222template Action GetMemberActionImpl<ArtField>(ArtField* member,
223 Action action,
224 AccessMethod access_method);
225template Action GetMemberActionImpl<ArtMethod>(ArtMethod* member,
226 Action action,
227 AccessMethod access_method);
Andreas Gampeaa120012018-03-28 16:23:24 -0700228} // namespace detail
Narayan Kamathf5f1f802018-04-03 15:23:46 +0100229
230template<typename T>
231void NotifyHiddenApiListener(T* member) {
232 Runtime* runtime = Runtime::Current();
233 if (!runtime->IsAotCompiler()) {
234 ScopedObjectAccessUnchecked soa(Thread::Current());
235
236 ScopedLocalRef<jobject> consumer_object(soa.Env(),
237 soa.Env()->GetStaticObjectField(
238 WellKnownClasses::dalvik_system_VMRuntime,
239 WellKnownClasses::dalvik_system_VMRuntime_nonSdkApiUsageConsumer));
240 // If the consumer is non-null, we call back to it to let it know that we
241 // have encountered an API that's in one of our lists.
242 if (consumer_object != nullptr) {
243 detail::MemberSignature member_signature(member);
244 std::ostringstream member_signature_str;
245 member_signature.Dump(member_signature_str);
246
247 ScopedLocalRef<jobject> signature_str(
248 soa.Env(),
249 soa.Env()->NewStringUTF(member_signature_str.str().c_str()));
250
251 // Call through to Consumer.accept(String memberSignature);
252 soa.Env()->CallVoidMethod(consumer_object.get(),
253 WellKnownClasses::java_util_function_Consumer_accept,
254 signature_str.get());
255 }
256 }
257}
258
259template void NotifyHiddenApiListener<ArtMethod>(ArtMethod* member);
260template void NotifyHiddenApiListener<ArtField>(ArtField* member);
261
Andreas Gampeaa120012018-03-28 16:23:24 -0700262} // namespace hiddenapi
263} // namespace art