blob: 4325496ca369f64ebf9c0e763f72af933f7e2a89 [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
Mathew Inwood1fd97f22018-04-03 15:32:32 +010055// Do not change the values of items in this enum, as they are written to the
56// event log for offline analysis. Any changes will interfere with that analysis.
David Brazdil068d68d2018-02-12 13:04:17 -080057enum AccessMethod {
Mathew Inwood1fd97f22018-04-03 15:32:32 +010058 kNone = 0, // internal test that does not correspond to an actual access by app
59 kReflection = 1,
60 kJNI = 2,
61 kLinking = 3,
62};
63
64// Do not change the values of items in this enum, as they are written to the
65// event log for offline analysis. Any changes will interfere with that analysis.
66enum AccessContextFlags {
67 // Accessed member is a field if this bit is set, else a method
68 kMemberIsField = 1 << 0,
69 // Indicates if access was denied to the member, instead of just printing a warning.
70 kAccessDenied = 1 << 1,
David Brazdil068d68d2018-02-12 13:04:17 -080071};
72
Narayan Kamathf5f1f802018-04-03 15:23:46 +010073inline Action GetActionFromAccessFlags(uint32_t access_flags) {
Mathew Inwood597d7f62018-03-22 11:36:47 +000074 EnforcementPolicy policy = Runtime::Current()->GetHiddenApiEnforcementPolicy();
75 if (policy == EnforcementPolicy::kNoChecks) {
76 // Exit early. Nothing to enforce.
77 return kAllow;
78 }
79
80 HiddenApiAccessFlags::ApiList api_list = HiddenApiAccessFlags::DecodeFromRuntime(access_flags);
81 if (api_list == HiddenApiAccessFlags::kWhitelist) {
82 return kAllow;
83 }
Mathew Inwooda8503d92018-04-05 16:10:25 +010084 // if policy is "just warn", always warn. We returned above for whitelist APIs.
85 if (policy == EnforcementPolicy::kJustWarn) {
86 return kAllowButWarn;
87 }
88 DCHECK(policy >= EnforcementPolicy::kDarkGreyAndBlackList);
Mathew Inwood597d7f62018-03-22 11:36:47 +000089 // The logic below relies on equality of values in the enums EnforcementPolicy and
Andreas Gampeaa120012018-03-28 16:23:24 -070090 // HiddenApiAccessFlags::ApiList, and their ordering. Assertions are in hidden_api.cc.
Mathew Inwood597d7f62018-03-22 11:36:47 +000091 if (static_cast<int>(policy) > static_cast<int>(api_list)) {
92 return api_list == HiddenApiAccessFlags::kDarkGreylist
93 ? kAllowButWarnAndToast
94 : kAllowButWarn;
95 } else {
96 return kDeny;
David Brazdil5a61bb72018-01-19 16:59:46 +000097 }
98}
99
Andreas Gampeaa120012018-03-28 16:23:24 -0700100// Implementation details. DO NOT ACCESS DIRECTLY.
101namespace detail {
David Brazdilee7d2fd2018-01-20 17:25:23 +0000102
Mathew Inwood7d74ef52018-03-16 14:18:33 +0000103// Class to encapsulate the signature of a member (ArtField or ArtMethod). This
104// is used as a helper when matching prefixes, and when logging the signature.
105class MemberSignature {
106 private:
Mathew Inwood1fd97f22018-04-03 15:32:32 +0100107 enum MemberType {
108 kField,
109 kMethod,
110 };
111
112 std::string class_name_;
113 std::string member_name_;
114 std::string type_signature_;
Mathew Inwood7d74ef52018-03-16 14:18:33 +0000115 std::string tmp_;
Mathew Inwood1fd97f22018-04-03 15:32:32 +0100116 MemberType type_;
117
118 inline std::vector<const char*> GetSignatureParts() const;
Mathew Inwood7d74ef52018-03-16 14:18:33 +0000119
120 public:
Andreas Gampeaa120012018-03-28 16:23:24 -0700121 explicit MemberSignature(ArtField* field) REQUIRES_SHARED(Locks::mutator_lock_);
122 explicit MemberSignature(ArtMethod* method) REQUIRES_SHARED(Locks::mutator_lock_);
Mathew Inwood7d74ef52018-03-16 14:18:33 +0000123
Andreas Gampeaa120012018-03-28 16:23:24 -0700124 void Dump(std::ostream& os) const;
Mathew Inwood7d74ef52018-03-16 14:18:33 +0000125
Mathew Inwood7d74ef52018-03-16 14:18:33 +0000126 // Performs prefix match on this member. Since the full member signature is
127 // composed of several parts, we match each part in turn (rather than
128 // building the entire thing in memory and performing a simple prefix match)
Andreas Gampeaa120012018-03-28 16:23:24 -0700129 bool DoesPrefixMatch(const std::string& prefix) const;
Mathew Inwood7d74ef52018-03-16 14:18:33 +0000130
Andreas Gampeaa120012018-03-28 16:23:24 -0700131 bool IsExempted(const std::vector<std::string>& exemptions);
Mathew Inwood7d74ef52018-03-16 14:18:33 +0000132
Andreas Gampeaa120012018-03-28 16:23:24 -0700133 void WarnAboutAccess(AccessMethod access_method, HiddenApiAccessFlags::ApiList list);
Mathew Inwood1fd97f22018-04-03 15:32:32 +0100134
135 void LogAccessToEventLog(AccessMethod access_method, Action action_taken);
Mathew Inwood7d74ef52018-03-16 14:18:33 +0000136};
David Brazdilee7d2fd2018-01-20 17:25:23 +0000137
Andreas Gampeaa120012018-03-28 16:23:24 -0700138template<typename T>
Narayan Kamathf5f1f802018-04-03 15:23:46 +0100139Action GetMemberActionImpl(T* member, Action action, AccessMethod access_method)
Andreas Gampeaa120012018-03-28 16:23:24 -0700140 REQUIRES_SHARED(Locks::mutator_lock_);
141
142// Returns true if the caller is either loaded by the boot strap class loader or comes from
143// a dex file located in ${ANDROID_ROOT}/framework/.
144ALWAYS_INLINE
145inline bool IsCallerInPlatformDex(ObjPtr<mirror::ClassLoader> caller_class_loader,
146 ObjPtr<mirror::DexCache> caller_dex_cache)
147 REQUIRES_SHARED(Locks::mutator_lock_) {
148 if (caller_class_loader.IsNull()) {
149 return true;
150 } else if (caller_dex_cache.IsNull()) {
151 return false;
152 } else {
153 const DexFile* caller_dex_file = caller_dex_cache->GetDexFile();
154 return caller_dex_file != nullptr && caller_dex_file->IsPlatformDexFile();
155 }
156}
157
158} // namespace detail
159
David Brazdila02cb112018-01-31 11:36:39 +0000160// Returns true if access to `member` should be denied to the caller of the
David Brazdil8e1a7cb2018-03-27 08:14:25 +0000161// reflective query. The decision is based on whether the caller is in the
162// platform or not. Because different users of this function determine this
163// in a different way, `fn_caller_in_platform(self)` is called and should
164// return true if the caller is located in the platform.
David Brazdil8ce3bfa2018-03-12 18:01:18 +0000165// This function might print warnings into the log if the member is hidden.
David Brazdilee7d2fd2018-01-20 17:25:23 +0000166template<typename T>
Narayan Kamathf5f1f802018-04-03 15:23:46 +0100167inline Action GetMemberAction(T* member,
168 Thread* self,
169 std::function<bool(Thread*)> fn_caller_in_platform,
170 AccessMethod access_method)
David Brazdilee7d2fd2018-01-20 17:25:23 +0000171 REQUIRES_SHARED(Locks::mutator_lock_) {
172 DCHECK(member != nullptr);
David Brazdilee7d2fd2018-01-20 17:25:23 +0000173
Narayan Kamathf5f1f802018-04-03 15:23:46 +0100174 Action action = GetActionFromAccessFlags(member->GetAccessFlags());
David Brazdila02cb112018-01-31 11:36:39 +0000175 if (action == kAllow) {
176 // Nothing to do.
Narayan Kamathf5f1f802018-04-03 15:23:46 +0100177 return action;
David Brazdila02cb112018-01-31 11:36:39 +0000178 }
179
David Brazdil8e1a7cb2018-03-27 08:14:25 +0000180 // Member is hidden. Invoke `fn_caller_in_platform` and find the origin of the access.
David Brazdila02cb112018-01-31 11:36:39 +0000181 // This can be *very* expensive. Save it for last.
David Brazdil8e1a7cb2018-03-27 08:14:25 +0000182 if (fn_caller_in_platform(self)) {
183 // Caller in the platform. Exit.
Narayan Kamathf5f1f802018-04-03 15:23:46 +0100184 return kAllow;
David Brazdila02cb112018-01-31 11:36:39 +0000185 }
186
David Brazdil8e1a7cb2018-03-27 08:14:25 +0000187 // Member is hidden and caller is not in the platform.
Narayan Kamathf5f1f802018-04-03 15:23:46 +0100188 return detail::GetMemberActionImpl(member, action, access_method);
David Brazdil8e1a7cb2018-03-27 08:14:25 +0000189}
190
191inline bool IsCallerInPlatformDex(ObjPtr<mirror::Class> caller)
192 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampeaa120012018-03-28 16:23:24 -0700193 return !caller.IsNull() &&
194 detail::IsCallerInPlatformDex(caller->GetClassLoader(), caller->GetDexCache());
David Brazdil8e1a7cb2018-03-27 08:14:25 +0000195}
196
David Brazdil8ce3bfa2018-03-12 18:01:18 +0000197// Returns true if access to `member` should be denied to a caller loaded with
198// `caller_class_loader`.
199// This function might print warnings into the log if the member is hidden.
200template<typename T>
Narayan Kamathf5f1f802018-04-03 15:23:46 +0100201inline Action GetMemberAction(T* member,
202 ObjPtr<mirror::ClassLoader> caller_class_loader,
203 ObjPtr<mirror::DexCache> caller_dex_cache,
204 AccessMethod access_method)
David Brazdilee7d2fd2018-01-20 17:25:23 +0000205 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampeaa120012018-03-28 16:23:24 -0700206 bool caller_in_platform = detail::IsCallerInPlatformDex(caller_class_loader, caller_dex_cache);
Narayan Kamathf5f1f802018-04-03 15:23:46 +0100207 return GetMemberAction(member,
208 /* thread */ nullptr,
209 [caller_in_platform] (Thread*) { return caller_in_platform; },
210 access_method);
David Brazdilee7d2fd2018-01-20 17:25:23 +0000211}
212
Narayan Kamathf5f1f802018-04-03 15:23:46 +0100213// Calls back into managed code to notify VMRuntime.nonSdkApiUsageConsumer that
214// |member| was accessed. This is usually called when an API is on the black,
215// dark grey or light grey lists. Given that the callback can execute arbitrary
216// code, a call to this method can result in thread suspension.
217template<typename T> void NotifyHiddenApiListener(T* member)
218 REQUIRES_SHARED(Locks::mutator_lock_);
219
220
David Brazdil5a61bb72018-01-19 16:59:46 +0000221} // namespace hiddenapi
222} // namespace art
223
224#endif // ART_RUNTIME_HIDDEN_API_H_