blob: ed00e2a89271e1ed76c8bfc15f6dc60a67623668 [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 {
David Brazdilf50ac102018-10-17 18:00:06 +010035 kDisabled = 0,
Mathew Inwooda8503d92018-04-05 16:10:25 +010036 kJustWarn = 1, // keep checks enabled, but allow everything (enables logging)
David Brazdilf50ac102018-10-17 18:00:06 +010037 kEnabled = 2, // ban dark grey & blacklist
38 kMax = kEnabled,
Mathew Inwood597d7f62018-03-22 11:36:47 +000039};
40
41inline EnforcementPolicy EnforcementPolicyFromInt(int api_policy_int) {
42 DCHECK_GE(api_policy_int, 0);
43 DCHECK_LE(api_policy_int, static_cast<int>(EnforcementPolicy::kMax));
44 return static_cast<EnforcementPolicy>(api_policy_int);
45}
46
David Brazdilf50ac102018-10-17 18:00:06 +010047enum class AccessMethod {
Mathew Inwood17245202018-04-12 13:56:37 +010048 kNone, // internal test that does not correspond to an actual access by app
49 kReflection,
50 kJNI,
51 kLinking,
Mathew Inwood1fd97f22018-04-03 15:32:32 +010052};
53
David Brazdilf50ac102018-10-17 18:00:06 +010054struct AccessContext {
55 public:
56 explicit AccessContext(bool is_trusted) : is_trusted_(is_trusted) {}
57
58 explicit AccessContext(ObjPtr<mirror::Class> klass) : is_trusted_(GetIsTrusted(klass)) {}
59
60 AccessContext(ObjPtr<mirror::ClassLoader> class_loader, ObjPtr<mirror::DexCache> dex_cache)
61 : is_trusted_(GetIsTrusted(class_loader, dex_cache)) {}
62
63 bool IsTrusted() const { return is_trusted_; }
64
65 private:
66 static bool GetIsTrusted(ObjPtr<mirror::ClassLoader> class_loader,
67 ObjPtr<mirror::DexCache> dex_cache)
68 REQUIRES_SHARED(Locks::mutator_lock_) {
69 // Trust if the caller is in is boot class loader.
70 if (class_loader.IsNull()) {
71 return true;
72 }
73
74 // Trust if caller is in a platform dex file.
75 if (!dex_cache.IsNull()) {
76 const DexFile* dex_file = dex_cache->GetDexFile();
77 if (dex_file != nullptr && dex_file->IsPlatformDexFile()) {
78 return true;
79 }
80 }
81
82 return false;
83 }
84
85 static bool GetIsTrusted(ObjPtr<mirror::Class> klass) REQUIRES_SHARED(Locks::mutator_lock_) {
86 DCHECK(!klass.IsNull());
87
88 if (klass->ShouldSkipHiddenApiChecks() && Runtime::Current()->IsJavaDebuggable()) {
89 // Class is known, it is marked trusted and we are in debuggable mode.
90 return true;
91 }
92
93 // Check other aspects of the context.
94 return GetIsTrusted(klass->GetClassLoader(), klass->GetDexCache());
95 }
96
97 bool is_trusted_;
David Brazdil068d68d2018-02-12 13:04:17 -080098};
99
David Brazdil32bde992018-05-14 15:24:34 +0100100class ScopedHiddenApiEnforcementPolicySetting {
101 public:
102 explicit ScopedHiddenApiEnforcementPolicySetting(EnforcementPolicy new_policy)
103 : initial_policy_(Runtime::Current()->GetHiddenApiEnforcementPolicy()) {
104 Runtime::Current()->SetHiddenApiEnforcementPolicy(new_policy);
105 }
106
107 ~ScopedHiddenApiEnforcementPolicySetting() {
108 Runtime::Current()->SetHiddenApiEnforcementPolicy(initial_policy_);
109 }
110
111 private:
112 const EnforcementPolicy initial_policy_;
113 DISALLOW_COPY_AND_ASSIGN(ScopedHiddenApiEnforcementPolicySetting);
114};
115
Andreas Gampeaa120012018-03-28 16:23:24 -0700116// Implementation details. DO NOT ACCESS DIRECTLY.
117namespace detail {
David Brazdilee7d2fd2018-01-20 17:25:23 +0000118
David Brazdilf50ac102018-10-17 18:00:06 +0100119enum class SdkCodes {
120 kVersionNone = std::numeric_limits<int32_t>::min(),
121 kVersionUnlimited = std::numeric_limits<int32_t>::max(),
122 kVersionO_MR1 = 27,
123 kVersionP = 28,
124};
125
Mathew Inwood7d74ef52018-03-16 14:18:33 +0000126// Class to encapsulate the signature of a member (ArtField or ArtMethod). This
127// is used as a helper when matching prefixes, and when logging the signature.
128class MemberSignature {
129 private:
Mathew Inwood1fd97f22018-04-03 15:32:32 +0100130 enum MemberType {
131 kField,
132 kMethod,
133 };
134
135 std::string class_name_;
136 std::string member_name_;
137 std::string type_signature_;
Mathew Inwood7d74ef52018-03-16 14:18:33 +0000138 std::string tmp_;
Mathew Inwood1fd97f22018-04-03 15:32:32 +0100139 MemberType type_;
140
141 inline std::vector<const char*> GetSignatureParts() const;
Mathew Inwood7d74ef52018-03-16 14:18:33 +0000142
143 public:
Andreas Gampeaa120012018-03-28 16:23:24 -0700144 explicit MemberSignature(ArtField* field) REQUIRES_SHARED(Locks::mutator_lock_);
145 explicit MemberSignature(ArtMethod* method) REQUIRES_SHARED(Locks::mutator_lock_);
Mathew Inwood7d74ef52018-03-16 14:18:33 +0000146
Andreas Gampeaa120012018-03-28 16:23:24 -0700147 void Dump(std::ostream& os) const;
Mathew Inwood7d74ef52018-03-16 14:18:33 +0000148
Mathew Inwood7d74ef52018-03-16 14:18:33 +0000149 // Performs prefix match on this member. Since the full member signature is
150 // composed of several parts, we match each part in turn (rather than
151 // building the entire thing in memory and performing a simple prefix match)
Andreas Gampeaa120012018-03-28 16:23:24 -0700152 bool DoesPrefixMatch(const std::string& prefix) const;
Mathew Inwood7d74ef52018-03-16 14:18:33 +0000153
Andreas Gampeaa120012018-03-28 16:23:24 -0700154 bool IsExempted(const std::vector<std::string>& exemptions);
Mathew Inwood7d74ef52018-03-16 14:18:33 +0000155
David Brazdil47cd2722018-10-23 12:50:02 +0100156 void WarnAboutAccess(AccessMethod access_method, ApiList list);
Mathew Inwood1fd97f22018-04-03 15:32:32 +0100157
David Brazdilf50ac102018-10-17 18:00:06 +0100158 void LogAccessToEventLog(AccessMethod access_method, bool access_denied);
159
160 // Calls back into managed code to notify VMRuntime.nonSdkApiUsageConsumer that
161 // |member| was accessed. This is usually called when an API is on the black,
162 // dark grey or light grey lists. Given that the callback can execute arbitrary
163 // code, a call to this method can result in thread suspension.
164 void NotifyHiddenApiListener(AccessMethod access_method);
Mathew Inwood7d74ef52018-03-16 14:18:33 +0000165};
David Brazdilee7d2fd2018-01-20 17:25:23 +0000166
Andreas Gampeaa120012018-03-28 16:23:24 -0700167template<typename T>
David Brazdilf50ac102018-10-17 18:00:06 +0100168bool ShouldDenyAccessToMemberImpl(T* member, ApiList api_list, AccessMethod access_method)
Andreas Gampeaa120012018-03-28 16:23:24 -0700169 REQUIRES_SHARED(Locks::mutator_lock_);
170
Andreas Gampeaa120012018-03-28 16:23:24 -0700171} // namespace detail
172
David Brazdilf50ac102018-10-17 18:00:06 +0100173// Returns true if access to `member` should be denied in the given context.
174// The decision is based on whether the caller is in a trusted context or not.
175// Because determining the access context can be expensive, a lambda function
176// "fn_get_access_context" is lazily invoked after other criteria have been
177// considered.
David Brazdil8ce3bfa2018-03-12 18:01:18 +0000178// This function might print warnings into the log if the member is hidden.
David Brazdilee7d2fd2018-01-20 17:25:23 +0000179template<typename T>
David Brazdilf50ac102018-10-17 18:00:06 +0100180inline bool ShouldDenyAccessToMember(T* member,
181 std::function<AccessContext()> fn_get_access_context,
182 AccessMethod access_method)
David Brazdilee7d2fd2018-01-20 17:25:23 +0000183 REQUIRES_SHARED(Locks::mutator_lock_) {
184 DCHECK(member != nullptr);
David Brazdilee7d2fd2018-01-20 17:25:23 +0000185
David Brazdil166546c2018-04-23 13:50:38 +0100186 // Decode hidden API access flags.
187 // NB Multiple threads might try to access (and overwrite) these simultaneously,
188 // causing a race. We only do that if access has not been denied, so the race
189 // cannot change Java semantics. We should, however, decode the access flags
190 // once and use it throughout this function, otherwise we may get inconsistent
191 // results, e.g. print whitelist warnings (b/78327881).
David Brazdil47cd2722018-10-23 12:50:02 +0100192 ApiList api_list = member->GetHiddenApiAccessFlags();
David Brazdil166546c2018-04-23 13:50:38 +0100193
David Brazdilf50ac102018-10-17 18:00:06 +0100194 // Exit early if member is on the whitelist.
195 if (api_list == ApiList::kWhitelist) {
196 return false;
David Brazdila02cb112018-01-31 11:36:39 +0000197 }
198
David Brazdilf50ac102018-10-17 18:00:06 +0100199 // Check if caller is exempted from access checks.
David Brazdila02cb112018-01-31 11:36:39 +0000200 // This can be *very* expensive. Save it for last.
David Brazdilf50ac102018-10-17 18:00:06 +0100201 if (fn_get_access_context().IsTrusted()) {
202 return false;
David Brazdila02cb112018-01-31 11:36:39 +0000203 }
204
David Brazdilf50ac102018-10-17 18:00:06 +0100205 // Member is hidden and caller is not exempted. Enter slow path.
206 return detail::ShouldDenyAccessToMemberImpl(member, api_list, access_method);
David Brazdil8e1a7cb2018-03-27 08:14:25 +0000207}
208
David Brazdilf50ac102018-10-17 18:00:06 +0100209// Helper method for callers where access context can be determined beforehand.
210// Wraps AccessContext in a lambda and passes it to the real ShouldDenyAccessToMember.
David Brazdil8ce3bfa2018-03-12 18:01:18 +0000211template<typename T>
David Brazdilf50ac102018-10-17 18:00:06 +0100212inline bool ShouldDenyAccessToMember(T* member,
213 AccessContext access_context,
214 AccessMethod access_method)
David Brazdilee7d2fd2018-01-20 17:25:23 +0000215 REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdilf50ac102018-10-17 18:00:06 +0100216 return ShouldDenyAccessToMember(
217 member,
218 [&] () REQUIRES_SHARED(Locks::mutator_lock_) { return access_context; },
219 access_method);
David Brazdilee7d2fd2018-01-20 17:25:23 +0000220}
221
David Brazdil5a61bb72018-01-19 16:59:46 +0000222} // namespace hiddenapi
223} // namespace art
224
225#endif // ART_RUNTIME_HIDDEN_API_H_