blob: d2c71a7d3557a8b9620eb3eb1b8c87624e209eae [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 {
David Brazdil4525e0b2018-04-05 16:57:32 +010056 kNone, // internal test that does not correspond to an actual access by app
David Brazdil068d68d2018-02-12 13:04:17 -080057 kReflection,
David Brazdil8ce3bfa2018-03-12 18:01:18 +000058 kJNI,
59 kLinking,
David Brazdil068d68d2018-02-12 13:04:17 -080060};
61
Narayan Kamathf5f1f802018-04-03 15:23:46 +010062inline Action GetActionFromAccessFlags(uint32_t access_flags) {
Mathew Inwood597d7f62018-03-22 11:36:47 +000063 EnforcementPolicy policy = Runtime::Current()->GetHiddenApiEnforcementPolicy();
64 if (policy == EnforcementPolicy::kNoChecks) {
65 // Exit early. Nothing to enforce.
66 return kAllow;
67 }
68
69 HiddenApiAccessFlags::ApiList api_list = HiddenApiAccessFlags::DecodeFromRuntime(access_flags);
70 if (api_list == HiddenApiAccessFlags::kWhitelist) {
71 return kAllow;
72 }
Mathew Inwooda8503d92018-04-05 16:10:25 +010073 // if policy is "just warn", always warn. We returned above for whitelist APIs.
74 if (policy == EnforcementPolicy::kJustWarn) {
75 return kAllowButWarn;
76 }
77 DCHECK(policy >= EnforcementPolicy::kDarkGreyAndBlackList);
Mathew Inwood597d7f62018-03-22 11:36:47 +000078 // The logic below relies on equality of values in the enums EnforcementPolicy and
Andreas Gampeaa120012018-03-28 16:23:24 -070079 // HiddenApiAccessFlags::ApiList, and their ordering. Assertions are in hidden_api.cc.
Mathew Inwood597d7f62018-03-22 11:36:47 +000080 if (static_cast<int>(policy) > static_cast<int>(api_list)) {
81 return api_list == HiddenApiAccessFlags::kDarkGreylist
82 ? kAllowButWarnAndToast
83 : kAllowButWarn;
84 } else {
85 return kDeny;
David Brazdil5a61bb72018-01-19 16:59:46 +000086 }
87}
88
Andreas Gampeaa120012018-03-28 16:23:24 -070089// Implementation details. DO NOT ACCESS DIRECTLY.
90namespace detail {
David Brazdilee7d2fd2018-01-20 17:25:23 +000091
Mathew Inwood7d74ef52018-03-16 14:18:33 +000092// Class to encapsulate the signature of a member (ArtField or ArtMethod). This
93// is used as a helper when matching prefixes, and when logging the signature.
94class MemberSignature {
95 private:
96 std::string member_type_;
97 std::vector<std::string> signature_parts_;
98 std::string tmp_;
99
100 public:
Andreas Gampeaa120012018-03-28 16:23:24 -0700101 explicit MemberSignature(ArtField* field) REQUIRES_SHARED(Locks::mutator_lock_);
102 explicit MemberSignature(ArtMethod* method) REQUIRES_SHARED(Locks::mutator_lock_);
Mathew Inwood7d74ef52018-03-16 14:18:33 +0000103
Andreas Gampeaa120012018-03-28 16:23:24 -0700104 void Dump(std::ostream& os) const;
Mathew Inwood7d74ef52018-03-16 14:18:33 +0000105
Mathew Inwood7d74ef52018-03-16 14:18:33 +0000106 // Performs prefix match on this member. Since the full member signature is
107 // composed of several parts, we match each part in turn (rather than
108 // building the entire thing in memory and performing a simple prefix match)
Andreas Gampeaa120012018-03-28 16:23:24 -0700109 bool DoesPrefixMatch(const std::string& prefix) const;
Mathew Inwood7d74ef52018-03-16 14:18:33 +0000110
Andreas Gampeaa120012018-03-28 16:23:24 -0700111 bool IsExempted(const std::vector<std::string>& exemptions);
Mathew Inwood7d74ef52018-03-16 14:18:33 +0000112
Andreas Gampeaa120012018-03-28 16:23:24 -0700113 void WarnAboutAccess(AccessMethod access_method, HiddenApiAccessFlags::ApiList list);
Mathew Inwood7d74ef52018-03-16 14:18:33 +0000114};
David Brazdilee7d2fd2018-01-20 17:25:23 +0000115
Andreas Gampeaa120012018-03-28 16:23:24 -0700116template<typename T>
Narayan Kamathf5f1f802018-04-03 15:23:46 +0100117Action GetMemberActionImpl(T* member, Action action, AccessMethod access_method)
Andreas Gampeaa120012018-03-28 16:23:24 -0700118 REQUIRES_SHARED(Locks::mutator_lock_);
119
120// Returns true if the caller is either loaded by the boot strap class loader or comes from
121// a dex file located in ${ANDROID_ROOT}/framework/.
122ALWAYS_INLINE
123inline bool IsCallerInPlatformDex(ObjPtr<mirror::ClassLoader> caller_class_loader,
124 ObjPtr<mirror::DexCache> caller_dex_cache)
125 REQUIRES_SHARED(Locks::mutator_lock_) {
126 if (caller_class_loader.IsNull()) {
127 return true;
128 } else if (caller_dex_cache.IsNull()) {
129 return false;
130 } else {
131 const DexFile* caller_dex_file = caller_dex_cache->GetDexFile();
132 return caller_dex_file != nullptr && caller_dex_file->IsPlatformDexFile();
133 }
134}
135
136} // namespace detail
137
David Brazdila02cb112018-01-31 11:36:39 +0000138// Returns true if access to `member` should be denied to the caller of the
David Brazdil8e1a7cb2018-03-27 08:14:25 +0000139// reflective query. The decision is based on whether the caller is in the
140// platform or not. Because different users of this function determine this
141// in a different way, `fn_caller_in_platform(self)` is called and should
142// return true if the caller is located in the platform.
David Brazdil8ce3bfa2018-03-12 18:01:18 +0000143// This function might print warnings into the log if the member is hidden.
David Brazdilee7d2fd2018-01-20 17:25:23 +0000144template<typename T>
Narayan Kamathf5f1f802018-04-03 15:23:46 +0100145inline Action GetMemberAction(T* member,
146 Thread* self,
147 std::function<bool(Thread*)> fn_caller_in_platform,
148 AccessMethod access_method)
David Brazdilee7d2fd2018-01-20 17:25:23 +0000149 REQUIRES_SHARED(Locks::mutator_lock_) {
150 DCHECK(member != nullptr);
David Brazdilee7d2fd2018-01-20 17:25:23 +0000151
Narayan Kamathf5f1f802018-04-03 15:23:46 +0100152 Action action = GetActionFromAccessFlags(member->GetAccessFlags());
David Brazdila02cb112018-01-31 11:36:39 +0000153 if (action == kAllow) {
154 // Nothing to do.
Narayan Kamathf5f1f802018-04-03 15:23:46 +0100155 return action;
David Brazdila02cb112018-01-31 11:36:39 +0000156 }
157
David Brazdil8e1a7cb2018-03-27 08:14:25 +0000158 // Member is hidden. Invoke `fn_caller_in_platform` and find the origin of the access.
David Brazdila02cb112018-01-31 11:36:39 +0000159 // This can be *very* expensive. Save it for last.
David Brazdil8e1a7cb2018-03-27 08:14:25 +0000160 if (fn_caller_in_platform(self)) {
161 // Caller in the platform. Exit.
Narayan Kamathf5f1f802018-04-03 15:23:46 +0100162 return kAllow;
David Brazdila02cb112018-01-31 11:36:39 +0000163 }
164
David Brazdil8e1a7cb2018-03-27 08:14:25 +0000165 // Member is hidden and caller is not in the platform.
Narayan Kamathf5f1f802018-04-03 15:23:46 +0100166 return detail::GetMemberActionImpl(member, action, access_method);
David Brazdil8e1a7cb2018-03-27 08:14:25 +0000167}
168
169inline bool IsCallerInPlatformDex(ObjPtr<mirror::Class> caller)
170 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampeaa120012018-03-28 16:23:24 -0700171 return !caller.IsNull() &&
172 detail::IsCallerInPlatformDex(caller->GetClassLoader(), caller->GetDexCache());
David Brazdil8e1a7cb2018-03-27 08:14:25 +0000173}
174
David Brazdil8ce3bfa2018-03-12 18:01:18 +0000175// Returns true if access to `member` should be denied to a caller loaded with
176// `caller_class_loader`.
177// This function might print warnings into the log if the member is hidden.
178template<typename T>
Narayan Kamathf5f1f802018-04-03 15:23:46 +0100179inline Action GetMemberAction(T* member,
180 ObjPtr<mirror::ClassLoader> caller_class_loader,
181 ObjPtr<mirror::DexCache> caller_dex_cache,
182 AccessMethod access_method)
David Brazdilee7d2fd2018-01-20 17:25:23 +0000183 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampeaa120012018-03-28 16:23:24 -0700184 bool caller_in_platform = detail::IsCallerInPlatformDex(caller_class_loader, caller_dex_cache);
Narayan Kamathf5f1f802018-04-03 15:23:46 +0100185 return GetMemberAction(member,
186 /* thread */ nullptr,
187 [caller_in_platform] (Thread*) { return caller_in_platform; },
188 access_method);
David Brazdilee7d2fd2018-01-20 17:25:23 +0000189}
190
Narayan Kamathf5f1f802018-04-03 15:23:46 +0100191// Calls back into managed code to notify VMRuntime.nonSdkApiUsageConsumer that
192// |member| was accessed. This is usually called when an API is on the black,
193// dark grey or light grey lists. Given that the callback can execute arbitrary
194// code, a call to this method can result in thread suspension.
195template<typename T> void NotifyHiddenApiListener(T* member)
196 REQUIRES_SHARED(Locks::mutator_lock_);
197
198
David Brazdil5a61bb72018-01-19 16:59:46 +0000199} // namespace hiddenapi
200} // namespace art
201
202#endif // ART_RUNTIME_HIDDEN_API_H_