David Brazdil | 5a61bb7 | 2018-01-19 16:59:46 +0000 | [diff] [blame] | 1 | /* |
| 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 Brazdil | 8ce3bfa | 2018-03-12 18:01:18 +0000 | [diff] [blame] | 20 | #include "art_field-inl.h" |
| 21 | #include "art_method-inl.h" |
David Sehr | 67bf42e | 2018-02-26 16:43:04 -0800 | [diff] [blame] | 22 | #include "dex/hidden_api_access_flags.h" |
David Brazdil | 8ce3bfa | 2018-03-12 18:01:18 +0000 | [diff] [blame] | 23 | #include "mirror/class-inl.h" |
David Brazdil | 5a61bb7 | 2018-01-19 16:59:46 +0000 | [diff] [blame] | 24 | #include "reflection.h" |
| 25 | #include "runtime.h" |
| 26 | |
| 27 | namespace art { |
| 28 | namespace hiddenapi { |
| 29 | |
Mathew Inwood | 597d7f6 | 2018-03-22 11:36:47 +0000 | [diff] [blame] | 30 | // Hidden API enforcement policy |
| 31 | // This must be kept in sync with ApplicationInfo.ApiEnforcementPolicy in |
| 32 | // frameworks/base/core/java/android/content/pm/ApplicationInfo.java |
| 33 | enum class EnforcementPolicy { |
| 34 | kNoChecks = 0, |
| 35 | kAllLists = 1, // ban anything but whitelist |
| 36 | kDarkGreyAndBlackList = 2, // ban dark grey & blacklist |
| 37 | kBlacklistOnly = 3, // ban blacklist violations only |
| 38 | kMax = kBlacklistOnly, |
| 39 | }; |
| 40 | |
| 41 | inline 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 Brazdil | a02cb11 | 2018-01-31 11:36:39 +0000 | [diff] [blame] | 47 | enum Action { |
| 48 | kAllow, |
| 49 | kAllowButWarn, |
David Brazdil | 9226522 | 2018-02-02 11:21:40 +0000 | [diff] [blame] | 50 | kAllowButWarnAndToast, |
David Brazdil | a02cb11 | 2018-01-31 11:36:39 +0000 | [diff] [blame] | 51 | kDeny |
| 52 | }; |
David Brazdil | 5a61bb7 | 2018-01-19 16:59:46 +0000 | [diff] [blame] | 53 | |
David Brazdil | 068d68d | 2018-02-12 13:04:17 -0800 | [diff] [blame] | 54 | enum AccessMethod { |
| 55 | kReflection, |
David Brazdil | 8ce3bfa | 2018-03-12 18:01:18 +0000 | [diff] [blame] | 56 | kJNI, |
| 57 | kLinking, |
David Brazdil | 068d68d | 2018-02-12 13:04:17 -0800 | [diff] [blame] | 58 | }; |
| 59 | |
| 60 | inline std::ostream& operator<<(std::ostream& os, AccessMethod value) { |
| 61 | switch (value) { |
| 62 | case kReflection: |
| 63 | os << "reflection"; |
| 64 | break; |
| 65 | case kJNI: |
| 66 | os << "JNI"; |
| 67 | break; |
David Brazdil | 8ce3bfa | 2018-03-12 18:01:18 +0000 | [diff] [blame] | 68 | case kLinking: |
| 69 | os << "linking"; |
| 70 | break; |
David Brazdil | 068d68d | 2018-02-12 13:04:17 -0800 | [diff] [blame] | 71 | } |
| 72 | return os; |
| 73 | } |
| 74 | |
Mathew Inwood | 597d7f6 | 2018-03-22 11:36:47 +0000 | [diff] [blame] | 75 | static constexpr bool EnumsEqual(EnforcementPolicy policy, HiddenApiAccessFlags::ApiList apiList) { |
| 76 | return static_cast<int>(policy) == static_cast<int>(apiList); |
| 77 | } |
| 78 | |
David Brazdil | a02cb11 | 2018-01-31 11:36:39 +0000 | [diff] [blame] | 79 | inline Action GetMemberAction(uint32_t access_flags) { |
Mathew Inwood | 597d7f6 | 2018-03-22 11:36:47 +0000 | [diff] [blame] | 80 | EnforcementPolicy policy = Runtime::Current()->GetHiddenApiEnforcementPolicy(); |
| 81 | if (policy == EnforcementPolicy::kNoChecks) { |
| 82 | // Exit early. Nothing to enforce. |
| 83 | return kAllow; |
| 84 | } |
| 85 | |
| 86 | HiddenApiAccessFlags::ApiList api_list = HiddenApiAccessFlags::DecodeFromRuntime(access_flags); |
| 87 | if (api_list == HiddenApiAccessFlags::kWhitelist) { |
| 88 | return kAllow; |
| 89 | } |
| 90 | // The logic below relies on equality of values in the enums EnforcementPolicy and |
| 91 | // HiddenApiAccessFlags::ApiList, and their ordering. Assert that this is as expected. |
| 92 | static_assert( |
| 93 | EnumsEqual(EnforcementPolicy::kAllLists, HiddenApiAccessFlags::kLightGreylist) && |
| 94 | EnumsEqual(EnforcementPolicy::kDarkGreyAndBlackList, HiddenApiAccessFlags::kDarkGreylist) && |
| 95 | EnumsEqual(EnforcementPolicy::kBlacklistOnly, HiddenApiAccessFlags::kBlacklist), |
| 96 | "Mismatch between EnforcementPolicy and ApiList enums"); |
| 97 | static_assert( |
| 98 | EnforcementPolicy::kAllLists < EnforcementPolicy::kDarkGreyAndBlackList && |
| 99 | EnforcementPolicy::kDarkGreyAndBlackList < EnforcementPolicy::kBlacklistOnly, |
| 100 | "EnforcementPolicy values ordering not correct"); |
| 101 | if (static_cast<int>(policy) > static_cast<int>(api_list)) { |
| 102 | return api_list == HiddenApiAccessFlags::kDarkGreylist |
| 103 | ? kAllowButWarnAndToast |
| 104 | : kAllowButWarn; |
| 105 | } else { |
| 106 | return kDeny; |
David Brazdil | 5a61bb7 | 2018-01-19 16:59:46 +0000 | [diff] [blame] | 107 | } |
| 108 | } |
| 109 | |
David Brazdil | ee7d2fd | 2018-01-20 17:25:23 +0000 | [diff] [blame] | 110 | // Issue a warning about field access. |
David Brazdil | 068d68d | 2018-02-12 13:04:17 -0800 | [diff] [blame] | 111 | inline void WarnAboutMemberAccess(ArtField* field, AccessMethod access_method) |
| 112 | REQUIRES_SHARED(Locks::mutator_lock_) { |
David Brazdil | 1077bbc | 2018-01-31 14:33:08 +0000 | [diff] [blame] | 113 | std::string tmp; |
| 114 | LOG(WARNING) << "Accessing hidden field " |
| 115 | << field->GetDeclaringClass()->GetDescriptor(&tmp) << "->" |
David Brazdil | 068d68d | 2018-02-12 13:04:17 -0800 | [diff] [blame] | 116 | << field->GetName() << ":" << field->GetTypeDescriptor() |
| 117 | << " (" << HiddenApiAccessFlags::DecodeFromRuntime(field->GetAccessFlags()) |
| 118 | << ", " << access_method << ")"; |
David Brazdil | ee7d2fd | 2018-01-20 17:25:23 +0000 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | // Issue a warning about method access. |
David Brazdil | 068d68d | 2018-02-12 13:04:17 -0800 | [diff] [blame] | 122 | inline void WarnAboutMemberAccess(ArtMethod* method, AccessMethod access_method) |
| 123 | REQUIRES_SHARED(Locks::mutator_lock_) { |
David Brazdil | 1077bbc | 2018-01-31 14:33:08 +0000 | [diff] [blame] | 124 | std::string tmp; |
| 125 | LOG(WARNING) << "Accessing hidden method " |
| 126 | << method->GetDeclaringClass()->GetDescriptor(&tmp) << "->" |
David Brazdil | 068d68d | 2018-02-12 13:04:17 -0800 | [diff] [blame] | 127 | << method->GetName() << method->GetSignature().ToString() |
| 128 | << " (" << HiddenApiAccessFlags::DecodeFromRuntime(method->GetAccessFlags()) |
| 129 | << ", " << access_method << ")"; |
David Brazdil | ee7d2fd | 2018-01-20 17:25:23 +0000 | [diff] [blame] | 130 | } |
| 131 | |
David Brazdil | a02cb11 | 2018-01-31 11:36:39 +0000 | [diff] [blame] | 132 | // Returns true if access to `member` should be denied to the caller of the |
Nicolas Geoffray | 0127b71 | 2018-03-26 19:31:41 +0000 | [diff] [blame] | 133 | // reflective query. The decision is based on whether the caller is in boot |
| 134 | // class path or not. Because different users of this function determine this |
| 135 | // in a different way, `fn_caller_in_boot(self)` is called and should return |
| 136 | // true if the caller is in boot class path. |
David Brazdil | 8ce3bfa | 2018-03-12 18:01:18 +0000 | [diff] [blame] | 137 | // This function might print warnings into the log if the member is hidden. |
David Brazdil | ee7d2fd | 2018-01-20 17:25:23 +0000 | [diff] [blame] | 138 | template<typename T> |
David Brazdil | a02cb11 | 2018-01-31 11:36:39 +0000 | [diff] [blame] | 139 | inline bool ShouldBlockAccessToMember(T* member, |
| 140 | Thread* self, |
Nicolas Geoffray | 0127b71 | 2018-03-26 19:31:41 +0000 | [diff] [blame] | 141 | std::function<bool(Thread*)> fn_caller_in_boot, |
David Brazdil | 068d68d | 2018-02-12 13:04:17 -0800 | [diff] [blame] | 142 | AccessMethod access_method) |
David Brazdil | ee7d2fd | 2018-01-20 17:25:23 +0000 | [diff] [blame] | 143 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 144 | DCHECK(member != nullptr); |
David Brazdil | ee7d2fd | 2018-01-20 17:25:23 +0000 | [diff] [blame] | 145 | |
David Brazdil | a02cb11 | 2018-01-31 11:36:39 +0000 | [diff] [blame] | 146 | Action action = GetMemberAction(member->GetAccessFlags()); |
| 147 | if (action == kAllow) { |
| 148 | // Nothing to do. |
| 149 | return false; |
| 150 | } |
| 151 | |
Nicolas Geoffray | 0127b71 | 2018-03-26 19:31:41 +0000 | [diff] [blame] | 152 | // Member is hidden. Walk the stack to find the caller. |
David Brazdil | a02cb11 | 2018-01-31 11:36:39 +0000 | [diff] [blame] | 153 | // This can be *very* expensive. Save it for last. |
Nicolas Geoffray | 0127b71 | 2018-03-26 19:31:41 +0000 | [diff] [blame] | 154 | if (fn_caller_in_boot(self)) { |
| 155 | // Caller in boot class path. Exit. |
David Brazdil | a02cb11 | 2018-01-31 11:36:39 +0000 | [diff] [blame] | 156 | return false; |
| 157 | } |
| 158 | |
Nicolas Geoffray | 0127b71 | 2018-03-26 19:31:41 +0000 | [diff] [blame] | 159 | // Member is hidden and we are not in the boot class path. |
David Brazdil | 068d68d | 2018-02-12 13:04:17 -0800 | [diff] [blame] | 160 | |
| 161 | // Print a log message with information about this class member access. |
| 162 | // We do this regardless of whether we block the access or not. |
| 163 | WarnAboutMemberAccess(member, access_method); |
| 164 | |
David Brazdil | 9226522 | 2018-02-02 11:21:40 +0000 | [diff] [blame] | 165 | if (action == kDeny) { |
Mathew Inwood | 597d7f6 | 2018-03-22 11:36:47 +0000 | [diff] [blame] | 166 | // Block access |
David Brazdil | 9226522 | 2018-02-02 11:21:40 +0000 | [diff] [blame] | 167 | return true; |
David Brazdil | a02cb11 | 2018-01-31 11:36:39 +0000 | [diff] [blame] | 168 | } |
David Brazdil | 068d68d | 2018-02-12 13:04:17 -0800 | [diff] [blame] | 169 | |
| 170 | // Allow access to this member but print a warning. |
| 171 | DCHECK(action == kAllowButWarn || action == kAllowButWarnAndToast); |
| 172 | |
Mathew Inwood | 597d7f6 | 2018-03-22 11:36:47 +0000 | [diff] [blame] | 173 | Runtime* runtime = Runtime::Current(); |
| 174 | |
David Brazdil | 068d68d | 2018-02-12 13:04:17 -0800 | [diff] [blame] | 175 | // Depending on a runtime flag, we might move the member into whitelist and |
| 176 | // skip the warning the next time the member is accessed. |
| 177 | if (runtime->ShouldDedupeHiddenApiWarnings()) { |
| 178 | member->SetAccessFlags(HiddenApiAccessFlags::EncodeForRuntime( |
| 179 | member->GetAccessFlags(), HiddenApiAccessFlags::kWhitelist)); |
| 180 | } |
| 181 | |
| 182 | // If this action requires a UI warning, set the appropriate flag. |
| 183 | if (action == kAllowButWarnAndToast || runtime->ShouldAlwaysSetHiddenApiWarningFlag()) { |
Mathew Inwood | 597d7f6 | 2018-03-22 11:36:47 +0000 | [diff] [blame] | 184 | runtime->SetPendingHiddenApiWarning(true); |
David Brazdil | 068d68d | 2018-02-12 13:04:17 -0800 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | return false; |
David Brazdil | ee7d2fd | 2018-01-20 17:25:23 +0000 | [diff] [blame] | 188 | } |
| 189 | |
David Brazdil | 8ce3bfa | 2018-03-12 18:01:18 +0000 | [diff] [blame] | 190 | // Returns true if access to `member` should be denied to a caller loaded with |
| 191 | // `caller_class_loader`. |
| 192 | // This function might print warnings into the log if the member is hidden. |
| 193 | template<typename T> |
| 194 | inline bool ShouldBlockAccessToMember(T* member, |
| 195 | ObjPtr<mirror::ClassLoader> caller_class_loader, |
| 196 | AccessMethod access_method) |
David Brazdil | ee7d2fd | 2018-01-20 17:25:23 +0000 | [diff] [blame] | 197 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Nicolas Geoffray | 0127b71 | 2018-03-26 19:31:41 +0000 | [diff] [blame] | 198 | bool caller_in_boot = (caller_class_loader.IsNull()); |
David Brazdil | 8ce3bfa | 2018-03-12 18:01:18 +0000 | [diff] [blame] | 199 | return ShouldBlockAccessToMember(member, |
| 200 | /* thread */ nullptr, |
Nicolas Geoffray | 0127b71 | 2018-03-26 19:31:41 +0000 | [diff] [blame] | 201 | [caller_in_boot] (Thread*) { return caller_in_boot; }, |
David Brazdil | 8ce3bfa | 2018-03-12 18:01:18 +0000 | [diff] [blame] | 202 | access_method); |
David Brazdil | ee7d2fd | 2018-01-20 17:25:23 +0000 | [diff] [blame] | 203 | } |
| 204 | |
David Brazdil | 5a61bb7 | 2018-01-19 16:59:46 +0000 | [diff] [blame] | 205 | } // namespace hiddenapi |
| 206 | } // namespace art |
| 207 | |
| 208 | #endif // ART_RUNTIME_HIDDEN_API_H_ |