blob: 8e21fd3b8fbafa1d06e0baa4f823d5f5542c4752 [file] [log] [blame]
David Brazdil5a61bb72018-01-19 16:59:46 +00001/*
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#ifndef ART_RUNTIME_HIDDEN_API_H_
18#define ART_RUNTIME_HIDDEN_API_H_
19
David Brazdil8ce3bfa2018-03-12 18:01:18 +000020#include "art_field-inl.h"
21#include "art_method-inl.h"
Andreas Gampeaa120012018-03-28 16:23:24 -070022#include "base/mutex.h"
David Sehr67bf42e2018-02-26 16:43:04 -080023#include "dex/hidden_api_access_flags.h"
David Brazdil8ce3bfa2018-03-12 18:01:18 +000024#include "mirror/class-inl.h"
David Brazdil5a61bb72018-01-19 16:59:46 +000025#include "reflection.h"
26#include "runtime.h"
27
28namespace art {
29namespace hiddenapi {
30
Mathew Inwood597d7f62018-03-22 11:36:47 +000031// Hidden API enforcement policy
32// This must be kept in sync with ApplicationInfo.ApiEnforcementPolicy in
33// frameworks/base/core/java/android/content/pm/ApplicationInfo.java
34enum class EnforcementPolicy {
35 kNoChecks = 0,
Mathew Inwooda8503d92018-04-05 16:10:25 +010036 kJustWarn = 1, // keep checks enabled, but allow everything (enables logging)
Mathew Inwood597d7f62018-03-22 11:36:47 +000037 kDarkGreyAndBlackList = 2, // ban dark grey & blacklist
38 kBlacklistOnly = 3, // ban blacklist violations only
39 kMax = kBlacklistOnly,
40};
41
42inline EnforcementPolicy EnforcementPolicyFromInt(int api_policy_int) {
43 DCHECK_GE(api_policy_int, 0);
44 DCHECK_LE(api_policy_int, static_cast<int>(EnforcementPolicy::kMax));
45 return static_cast<EnforcementPolicy>(api_policy_int);
46}
47
David Brazdila02cb112018-01-31 11:36:39 +000048enum Action {
49 kAllow,
50 kAllowButWarn,
David Brazdil92265222018-02-02 11:21:40 +000051 kAllowButWarnAndToast,
David Brazdila02cb112018-01-31 11:36:39 +000052 kDeny
53};
David Brazdil5a61bb72018-01-19 16:59:46 +000054
David Brazdil068d68d2018-02-12 13:04:17 -080055enum AccessMethod {
Mathew Inwood17245202018-04-12 13:56:37 +010056 kNone, // internal test that does not correspond to an actual access by app
57 kReflection,
58 kJNI,
59 kLinking,
Mathew Inwood1fd97f22018-04-03 15:32:32 +010060};
61
62// Do not change the values of items in this enum, as they are written to the
63// event log for offline analysis. Any changes will interfere with that analysis.
64enum AccessContextFlags {
65 // Accessed member is a field if this bit is set, else a method
66 kMemberIsField = 1 << 0,
67 // Indicates if access was denied to the member, instead of just printing a warning.
68 kAccessDenied = 1 << 1,
David Brazdil068d68d2018-02-12 13:04:17 -080069};
70
David Brazdil166546c2018-04-23 13:50:38 +010071inline Action GetActionFromAccessFlags(HiddenApiAccessFlags::ApiList api_list) {
72 if (api_list == HiddenApiAccessFlags::kWhitelist) {
73 return kAllow;
74 }
75
Mathew Inwood597d7f62018-03-22 11:36:47 +000076 EnforcementPolicy policy = Runtime::Current()->GetHiddenApiEnforcementPolicy();
77 if (policy == EnforcementPolicy::kNoChecks) {
78 // Exit early. Nothing to enforce.
79 return kAllow;
80 }
81
Mathew Inwooda8503d92018-04-05 16:10:25 +010082 // if policy is "just warn", always warn. We returned above for whitelist APIs.
83 if (policy == EnforcementPolicy::kJustWarn) {
84 return kAllowButWarn;
85 }
86 DCHECK(policy >= EnforcementPolicy::kDarkGreyAndBlackList);
Mathew Inwood597d7f62018-03-22 11:36:47 +000087 // The logic below relies on equality of values in the enums EnforcementPolicy and
Andreas Gampeaa120012018-03-28 16:23:24 -070088 // HiddenApiAccessFlags::ApiList, and their ordering. Assertions are in hidden_api.cc.
Mathew Inwood597d7f62018-03-22 11:36:47 +000089 if (static_cast<int>(policy) > static_cast<int>(api_list)) {
90 return api_list == HiddenApiAccessFlags::kDarkGreylist
91 ? kAllowButWarnAndToast
92 : kAllowButWarn;
93 } else {
94 return kDeny;
David Brazdil5a61bb72018-01-19 16:59:46 +000095 }
96}
97
Andreas Gampeaa120012018-03-28 16:23:24 -070098// Implementation details. DO NOT ACCESS DIRECTLY.
99namespace detail {
David Brazdilee7d2fd2018-01-20 17:25:23 +0000100
Mathew Inwood7d74ef52018-03-16 14:18:33 +0000101// Class to encapsulate the signature of a member (ArtField or ArtMethod). This
102// is used as a helper when matching prefixes, and when logging the signature.
103class MemberSignature {
104 private:
Mathew Inwood1fd97f22018-04-03 15:32:32 +0100105 enum MemberType {
106 kField,
107 kMethod,
108 };
109
110 std::string class_name_;
111 std::string member_name_;
112 std::string type_signature_;
Mathew Inwood7d74ef52018-03-16 14:18:33 +0000113 std::string tmp_;
Mathew Inwood1fd97f22018-04-03 15:32:32 +0100114 MemberType type_;
115
116 inline std::vector<const char*> GetSignatureParts() const;
Mathew Inwood7d74ef52018-03-16 14:18:33 +0000117
118 public:
Andreas Gampeaa120012018-03-28 16:23:24 -0700119 explicit MemberSignature(ArtField* field) REQUIRES_SHARED(Locks::mutator_lock_);
120 explicit MemberSignature(ArtMethod* method) REQUIRES_SHARED(Locks::mutator_lock_);
Mathew Inwood7d74ef52018-03-16 14:18:33 +0000121
Andreas Gampeaa120012018-03-28 16:23:24 -0700122 void Dump(std::ostream& os) const;
Mathew Inwood7d74ef52018-03-16 14:18:33 +0000123
Mathew Inwood7d74ef52018-03-16 14:18:33 +0000124 // Performs prefix match on this member. Since the full member signature is
125 // composed of several parts, we match each part in turn (rather than
126 // building the entire thing in memory and performing a simple prefix match)
Andreas Gampeaa120012018-03-28 16:23:24 -0700127 bool DoesPrefixMatch(const std::string& prefix) const;
Mathew Inwood7d74ef52018-03-16 14:18:33 +0000128
Andreas Gampeaa120012018-03-28 16:23:24 -0700129 bool IsExempted(const std::vector<std::string>& exemptions);
Mathew Inwood7d74ef52018-03-16 14:18:33 +0000130
Andreas Gampeaa120012018-03-28 16:23:24 -0700131 void WarnAboutAccess(AccessMethod access_method, HiddenApiAccessFlags::ApiList list);
Mathew Inwood1fd97f22018-04-03 15:32:32 +0100132
133 void LogAccessToEventLog(AccessMethod access_method, Action action_taken);
Mathew Inwood7d74ef52018-03-16 14:18:33 +0000134};
David Brazdilee7d2fd2018-01-20 17:25:23 +0000135
Andreas Gampeaa120012018-03-28 16:23:24 -0700136template<typename T>
David Brazdil39512f52018-04-23 13:51:16 +0100137Action GetMemberActionImpl(T* member,
138 HiddenApiAccessFlags::ApiList api_list,
139 Action action,
140 AccessMethod access_method)
Andreas Gampeaa120012018-03-28 16:23:24 -0700141 REQUIRES_SHARED(Locks::mutator_lock_);
142
143// Returns true if the caller is either loaded by the boot strap class loader or comes from
144// a dex file located in ${ANDROID_ROOT}/framework/.
145ALWAYS_INLINE
David Brazdil59a49a62018-05-09 10:58:27 +0100146inline bool IsCallerTrusted(ObjPtr<mirror::Class> caller,
147 ObjPtr<mirror::ClassLoader> caller_class_loader,
148 ObjPtr<mirror::DexCache> caller_dex_cache)
Andreas Gampeaa120012018-03-28 16:23:24 -0700149 REQUIRES_SHARED(Locks::mutator_lock_) {
150 if (caller_class_loader.IsNull()) {
David Brazdil59a49a62018-05-09 10:58:27 +0100151 // Boot class loader.
Andreas Gampeaa120012018-03-28 16:23:24 -0700152 return true;
Andreas Gampeaa120012018-03-28 16:23:24 -0700153 }
David Brazdil59a49a62018-05-09 10:58:27 +0100154
155 if (!caller_dex_cache.IsNull()) {
156 const DexFile* caller_dex_file = caller_dex_cache->GetDexFile();
157 if (caller_dex_file != nullptr && caller_dex_file->IsPlatformDexFile()) {
158 // Caller is in a platform dex file.
159 return true;
160 }
161 }
162
163 if (!caller.IsNull() &&
164 caller->ShouldSkipHiddenApiChecks() &&
165 Runtime::Current()->IsJavaDebuggable()) {
166 // We are in debuggable mode and this caller has been marked trusted.
167 return true;
168 }
169
170 return false;
Andreas Gampeaa120012018-03-28 16:23:24 -0700171}
172
173} // namespace detail
174
David Brazdila02cb112018-01-31 11:36:39 +0000175// Returns true if access to `member` should be denied to the caller of the
David Brazdil59a49a62018-05-09 10:58:27 +0100176// reflective query. The decision is based on whether the caller is trusted or
177// not. Because different users of this function determine this in a different
178// way, `fn_caller_is_trusted(self)` is called and should return true if the
179// caller is allowed to access the platform.
David Brazdil8ce3bfa2018-03-12 18:01:18 +0000180// This function might print warnings into the log if the member is hidden.
David Brazdilee7d2fd2018-01-20 17:25:23 +0000181template<typename T>
Narayan Kamathf5f1f802018-04-03 15:23:46 +0100182inline Action GetMemberAction(T* member,
183 Thread* self,
David Brazdil59a49a62018-05-09 10:58:27 +0100184 std::function<bool(Thread*)> fn_caller_is_trusted,
Narayan Kamathf5f1f802018-04-03 15:23:46 +0100185 AccessMethod access_method)
David Brazdilee7d2fd2018-01-20 17:25:23 +0000186 REQUIRES_SHARED(Locks::mutator_lock_) {
187 DCHECK(member != nullptr);
David Brazdilee7d2fd2018-01-20 17:25:23 +0000188
David Brazdil166546c2018-04-23 13:50:38 +0100189 // Decode hidden API access flags.
190 // NB Multiple threads might try to access (and overwrite) these simultaneously,
191 // causing a race. We only do that if access has not been denied, so the race
192 // cannot change Java semantics. We should, however, decode the access flags
193 // once and use it throughout this function, otherwise we may get inconsistent
194 // results, e.g. print whitelist warnings (b/78327881).
195 HiddenApiAccessFlags::ApiList api_list = member->GetHiddenApiAccessFlags();
196
197 Action action = GetActionFromAccessFlags(member->GetHiddenApiAccessFlags());
David Brazdila02cb112018-01-31 11:36:39 +0000198 if (action == kAllow) {
199 // Nothing to do.
Narayan Kamathf5f1f802018-04-03 15:23:46 +0100200 return action;
David Brazdila02cb112018-01-31 11:36:39 +0000201 }
202
David Brazdil8e1a7cb2018-03-27 08:14:25 +0000203 // Member is hidden. Invoke `fn_caller_in_platform` and find the origin of the access.
David Brazdila02cb112018-01-31 11:36:39 +0000204 // This can be *very* expensive. Save it for last.
David Brazdil59a49a62018-05-09 10:58:27 +0100205 if (fn_caller_is_trusted(self)) {
206 // Caller is trusted. Exit.
Narayan Kamathf5f1f802018-04-03 15:23:46 +0100207 return kAllow;
David Brazdila02cb112018-01-31 11:36:39 +0000208 }
209
David Brazdil8e1a7cb2018-03-27 08:14:25 +0000210 // Member is hidden and caller is not in the platform.
David Brazdil39512f52018-04-23 13:51:16 +0100211 return detail::GetMemberActionImpl(member, api_list, action, access_method);
David Brazdil8e1a7cb2018-03-27 08:14:25 +0000212}
213
David Brazdil59a49a62018-05-09 10:58:27 +0100214inline bool IsCallerTrusted(ObjPtr<mirror::Class> caller) REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampeaa120012018-03-28 16:23:24 -0700215 return !caller.IsNull() &&
David Brazdil59a49a62018-05-09 10:58:27 +0100216 detail::IsCallerTrusted(caller, caller->GetClassLoader(), caller->GetDexCache());
David Brazdil8e1a7cb2018-03-27 08:14:25 +0000217}
218
David Brazdil8ce3bfa2018-03-12 18:01:18 +0000219// Returns true if access to `member` should be denied to a caller loaded with
220// `caller_class_loader`.
221// This function might print warnings into the log if the member is hidden.
222template<typename T>
Narayan Kamathf5f1f802018-04-03 15:23:46 +0100223inline Action GetMemberAction(T* member,
224 ObjPtr<mirror::ClassLoader> caller_class_loader,
225 ObjPtr<mirror::DexCache> caller_dex_cache,
226 AccessMethod access_method)
David Brazdilee7d2fd2018-01-20 17:25:23 +0000227 REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil59a49a62018-05-09 10:58:27 +0100228 bool is_caller_trusted =
229 detail::IsCallerTrusted(/* caller */ nullptr, caller_class_loader, caller_dex_cache);
Narayan Kamathf5f1f802018-04-03 15:23:46 +0100230 return GetMemberAction(member,
231 /* thread */ nullptr,
David Brazdil59a49a62018-05-09 10:58:27 +0100232 [is_caller_trusted] (Thread*) { return is_caller_trusted; },
Narayan Kamathf5f1f802018-04-03 15:23:46 +0100233 access_method);
David Brazdilee7d2fd2018-01-20 17:25:23 +0000234}
235
Narayan Kamathf5f1f802018-04-03 15:23:46 +0100236// Calls back into managed code to notify VMRuntime.nonSdkApiUsageConsumer that
237// |member| was accessed. This is usually called when an API is on the black,
238// dark grey or light grey lists. Given that the callback can execute arbitrary
239// code, a call to this method can result in thread suspension.
240template<typename T> void NotifyHiddenApiListener(T* member)
241 REQUIRES_SHARED(Locks::mutator_lock_);
242
243
David Brazdil5a61bb72018-01-19 16:59:46 +0000244} // namespace hiddenapi
245} // namespace art
246
247#endif // ART_RUNTIME_HIDDEN_API_H_