Steven Moreland | dea3cf9 | 2019-07-16 18:06:55 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 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 | */ |
Steven Moreland | 89ddfc5 | 2020-11-13 02:39:26 +0000 | [diff] [blame] | 16 | #define LOG_TAG "Stability" |
| 17 | |
Steven Moreland | dea3cf9 | 2019-07-16 18:06:55 -0700 | [diff] [blame] | 18 | #include <binder/Stability.h> |
| 19 | |
Steven Moreland | a7fb018 | 2020-02-26 16:02:08 -0800 | [diff] [blame] | 20 | #include <binder/BpBinder.h> |
| 21 | #include <binder/Binder.h> |
| 22 | |
Steven Moreland | dea3cf9 | 2019-07-16 18:06:55 -0700 | [diff] [blame] | 23 | namespace android { |
| 24 | namespace internal { |
| 25 | |
Kalesh Singh | 8b80291 | 2021-03-31 12:13:56 -0400 | [diff] [blame] | 26 | void Stability::forceDowngradeToStability(const sp<IBinder>& binder, Level level) { |
Steven Moreland | e35fef3 | 2021-03-23 01:38:24 +0000 | [diff] [blame] | 27 | // Downgrading a remote binder would require also copying the version from |
| 28 | // the binder sent here. In practice though, we don't need to downgrade the |
| 29 | // stability of a remote binder, since this would as an effect only restrict |
| 30 | // what we can do to it. |
| 31 | LOG_ALWAYS_FATAL_IF(!binder || !binder->localBinder(), "Can only downgrade local binder"); |
| 32 | |
Steven Moreland | 16a4106 | 2021-07-23 13:35:25 -0700 | [diff] [blame] | 33 | status_t result = setRepr(binder.get(), level, REPR_LOG | REPR_ALLOW_DOWNGRADE); |
Steven Moreland | e35fef3 | 2021-03-23 01:38:24 +0000 | [diff] [blame] | 34 | LOG_ALWAYS_FATAL_IF(result != OK, "Should only mark known object."); |
| 35 | } |
| 36 | |
Kalesh Singh | 8b80291 | 2021-03-31 12:13:56 -0400 | [diff] [blame] | 37 | void Stability::forceDowngradeToLocalStability(const sp<IBinder>& binder) { |
| 38 | forceDowngradeToStability(binder, getLocalLevel()); |
| 39 | } |
| 40 | |
| 41 | void Stability::forceDowngradeToSystemStability(const sp<IBinder>& binder) { |
| 42 | forceDowngradeToStability(binder, Level::SYSTEM); |
| 43 | } |
| 44 | |
| 45 | void Stability::forceDowngradeToVendorStability(const sp<IBinder>& binder) { |
| 46 | forceDowngradeToStability(binder, Level::VENDOR); |
| 47 | } |
| 48 | |
Steven Moreland | dea3cf9 | 2019-07-16 18:06:55 -0700 | [diff] [blame] | 49 | void Stability::markCompilationUnit(IBinder* binder) { |
Steven Moreland | 16a4106 | 2021-07-23 13:35:25 -0700 | [diff] [blame] | 50 | status_t result = setRepr(binder, getLocalLevel(), REPR_LOG); |
Steven Moreland | dea3cf9 | 2019-07-16 18:06:55 -0700 | [diff] [blame] | 51 | LOG_ALWAYS_FATAL_IF(result != OK, "Should only mark known object."); |
| 52 | } |
| 53 | |
| 54 | void Stability::markVintf(IBinder* binder) { |
Steven Moreland | 16a4106 | 2021-07-23 13:35:25 -0700 | [diff] [blame] | 55 | status_t result = setRepr(binder, Level::VINTF, REPR_LOG); |
Steven Moreland | dea3cf9 | 2019-07-16 18:06:55 -0700 | [diff] [blame] | 56 | LOG_ALWAYS_FATAL_IF(result != OK, "Should only mark known object."); |
| 57 | } |
| 58 | |
Steven Moreland | e5a6a87 | 2021-05-19 22:11:37 +0000 | [diff] [blame] | 59 | std::string Stability::debugToString(const sp<IBinder>& binder) { |
Steven Moreland | 16a4106 | 2021-07-23 13:35:25 -0700 | [diff] [blame] | 60 | return levelString(getRepr(binder.get())); |
Steven Moreland | 64ae917 | 2019-08-02 20:45:15 -0700 | [diff] [blame] | 61 | } |
| 62 | |
Steven Moreland | c709dd8 | 2019-08-05 20:30:14 -0700 | [diff] [blame] | 63 | void Stability::markVndk(IBinder* binder) { |
Steven Moreland | 16a4106 | 2021-07-23 13:35:25 -0700 | [diff] [blame] | 64 | status_t result = setRepr(binder, Level::VENDOR, REPR_LOG); |
Steven Moreland | c709dd8 | 2019-08-05 20:30:14 -0700 | [diff] [blame] | 65 | LOG_ALWAYS_FATAL_IF(result != OK, "Should only mark known object."); |
| 66 | } |
| 67 | |
Steven Moreland | 86a17f8 | 2019-09-10 10:18:00 -0700 | [diff] [blame] | 68 | bool Stability::requiresVintfDeclaration(const sp<IBinder>& binder) { |
Steven Moreland | 16a4106 | 2021-07-23 13:35:25 -0700 | [diff] [blame] | 69 | return check(getRepr(binder.get()), Level::VINTF); |
Steven Moreland | 86a17f8 | 2019-09-10 10:18:00 -0700 | [diff] [blame] | 70 | } |
| 71 | |
Steven Moreland | 2a9f32f | 2019-07-31 17:51:25 -0700 | [diff] [blame] | 72 | void Stability::tryMarkCompilationUnit(IBinder* binder) { |
Steven Moreland | 16a4106 | 2021-07-23 13:35:25 -0700 | [diff] [blame] | 73 | (void)setRepr(binder, getLocalLevel(), REPR_NONE); |
Steven Moreland | 2f2c4e0 | 2020-07-28 18:11:21 +0000 | [diff] [blame] | 74 | } |
| 75 | |
Steven Moreland | 89ddfc5 | 2020-11-13 02:39:26 +0000 | [diff] [blame] | 76 | Stability::Level Stability::getLocalLevel() { |
Steven Moreland | b6c7e22 | 2021-02-18 19:20:14 +0000 | [diff] [blame] | 77 | #ifdef __ANDROID_APEX__ |
| 78 | #error APEX can't use libbinder (must use libbinder_ndk) |
| 79 | #endif |
| 80 | |
Steven Moreland | 2f2c4e0 | 2020-07-28 18:11:21 +0000 | [diff] [blame] | 81 | #ifdef __ANDROID_VNDK__ |
Steven Moreland | b6c7e22 | 2021-02-18 19:20:14 +0000 | [diff] [blame] | 82 | return Level::VENDOR; |
Steven Moreland | 2f2c4e0 | 2020-07-28 18:11:21 +0000 | [diff] [blame] | 83 | #else |
| 84 | // TODO(b/139325195): split up stability levels for system/APEX. |
| 85 | return Level::SYSTEM; |
| 86 | #endif |
Steven Moreland | 2a9f32f | 2019-07-31 17:51:25 -0700 | [diff] [blame] | 87 | } |
| 88 | |
Steven Moreland | 16a4106 | 2021-07-23 13:35:25 -0700 | [diff] [blame] | 89 | status_t Stability::setRepr(IBinder* binder, int32_t setting, uint32_t flags) { |
Steven Moreland | e35fef3 | 2021-03-23 01:38:24 +0000 | [diff] [blame] | 90 | bool log = flags & REPR_LOG; |
| 91 | bool allowDowngrade = flags & REPR_ALLOW_DOWNGRADE; |
| 92 | |
Steven Moreland | 16a4106 | 2021-07-23 13:35:25 -0700 | [diff] [blame] | 93 | int16_t current = getRepr(binder); |
Steven Moreland | dea3cf9 | 2019-07-16 18:06:55 -0700 | [diff] [blame] | 94 | |
| 95 | // null binder is always written w/ 'UNDECLARED' stability |
| 96 | if (binder == nullptr) { |
Steven Moreland | 16a4106 | 2021-07-23 13:35:25 -0700 | [diff] [blame] | 97 | if (setting == UNDECLARED) { |
Steven Moreland | dea3cf9 | 2019-07-16 18:06:55 -0700 | [diff] [blame] | 98 | return OK; |
| 99 | } else { |
Steven Moreland | 2a9f32f | 2019-07-31 17:51:25 -0700 | [diff] [blame] | 100 | if (log) { |
Steven Moreland | 16a4106 | 2021-07-23 13:35:25 -0700 | [diff] [blame] | 101 | ALOGE("Null binder written with stability %s.", levelString(setting).c_str()); |
Steven Moreland | 2a9f32f | 2019-07-31 17:51:25 -0700 | [diff] [blame] | 102 | } |
Steven Moreland | dea3cf9 | 2019-07-16 18:06:55 -0700 | [diff] [blame] | 103 | return BAD_TYPE; |
| 104 | } |
| 105 | } |
| 106 | |
Steven Moreland | 16a4106 | 2021-07-23 13:35:25 -0700 | [diff] [blame] | 107 | if (!isDeclaredLevel(setting)) { |
Steven Moreland | 2a9f32f | 2019-07-31 17:51:25 -0700 | [diff] [blame] | 108 | if (log) { |
Steven Moreland | 16a4106 | 2021-07-23 13:35:25 -0700 | [diff] [blame] | 109 | ALOGE("Can only set known stability, not %d.", setting); |
Steven Moreland | dea3cf9 | 2019-07-16 18:06:55 -0700 | [diff] [blame] | 110 | } |
Steven Moreland | 2a9f32f | 2019-07-31 17:51:25 -0700 | [diff] [blame] | 111 | return BAD_TYPE; |
Steven Moreland | dea3cf9 | 2019-07-16 18:06:55 -0700 | [diff] [blame] | 112 | } |
Steven Moreland | 16a4106 | 2021-07-23 13:35:25 -0700 | [diff] [blame] | 113 | Level levelSetting = static_cast<Level>(setting); |
Steven Moreland | dea3cf9 | 2019-07-16 18:06:55 -0700 | [diff] [blame] | 114 | |
Steven Moreland | e35fef3 | 2021-03-23 01:38:24 +0000 | [diff] [blame] | 115 | if (current == setting) return OK; |
| 116 | |
Steven Moreland | 16a4106 | 2021-07-23 13:35:25 -0700 | [diff] [blame] | 117 | bool hasAlreadyBeenSet = current != Level::UNDECLARED; |
| 118 | bool isAllowedDowngrade = allowDowngrade && check(current, levelSetting); |
Steven Moreland | e35fef3 | 2021-03-23 01:38:24 +0000 | [diff] [blame] | 119 | if (hasAlreadyBeenSet && !isAllowedDowngrade) { |
Steven Moreland | 2a9f32f | 2019-07-31 17:51:25 -0700 | [diff] [blame] | 120 | if (log) { |
Steven Moreland | 89ddfc5 | 2020-11-13 02:39:26 +0000 | [diff] [blame] | 121 | ALOGE("Interface being set with %s but it is already marked as %s", |
Steven Moreland | 16a4106 | 2021-07-23 13:35:25 -0700 | [diff] [blame] | 122 | levelString(setting).c_str(), levelString(current).c_str()); |
Steven Moreland | 2a9f32f | 2019-07-31 17:51:25 -0700 | [diff] [blame] | 123 | } |
Steven Moreland | dea3cf9 | 2019-07-16 18:06:55 -0700 | [diff] [blame] | 124 | return BAD_TYPE; |
| 125 | } |
| 126 | |
Steven Moreland | e35fef3 | 2021-03-23 01:38:24 +0000 | [diff] [blame] | 127 | if (isAllowedDowngrade) { |
Steven Moreland | 16a4106 | 2021-07-23 13:35:25 -0700 | [diff] [blame] | 128 | ALOGI("Interface set with %s downgraded to %s stability", levelString(current).c_str(), |
| 129 | levelString(setting).c_str()); |
Steven Moreland | e35fef3 | 2021-03-23 01:38:24 +0000 | [diff] [blame] | 130 | } |
Steven Moreland | dea3cf9 | 2019-07-16 18:06:55 -0700 | [diff] [blame] | 131 | |
Steven Moreland | a7fb018 | 2020-02-26 16:02:08 -0800 | [diff] [blame] | 132 | BBinder* local = binder->localBinder(); |
| 133 | if (local != nullptr) { |
Steven Moreland | 16a4106 | 2021-07-23 13:35:25 -0700 | [diff] [blame] | 134 | local->mStability = setting; |
Steven Moreland | a7fb018 | 2020-02-26 16:02:08 -0800 | [diff] [blame] | 135 | } else { |
Steven Moreland | 16a4106 | 2021-07-23 13:35:25 -0700 | [diff] [blame] | 136 | binder->remoteBinder()->mStability = setting; |
Steven Moreland | a7fb018 | 2020-02-26 16:02:08 -0800 | [diff] [blame] | 137 | } |
Steven Moreland | dea3cf9 | 2019-07-16 18:06:55 -0700 | [diff] [blame] | 138 | |
| 139 | return OK; |
| 140 | } |
| 141 | |
Steven Moreland | 16a4106 | 2021-07-23 13:35:25 -0700 | [diff] [blame] | 142 | int16_t Stability::getRepr(IBinder* binder) { |
Steven Moreland | 89ddfc5 | 2020-11-13 02:39:26 +0000 | [diff] [blame] | 143 | if (binder == nullptr) { |
Steven Moreland | 16a4106 | 2021-07-23 13:35:25 -0700 | [diff] [blame] | 144 | return Level::UNDECLARED; |
Steven Moreland | 89ddfc5 | 2020-11-13 02:39:26 +0000 | [diff] [blame] | 145 | } |
Steven Moreland | dea3cf9 | 2019-07-16 18:06:55 -0700 | [diff] [blame] | 146 | |
Steven Moreland | a7fb018 | 2020-02-26 16:02:08 -0800 | [diff] [blame] | 147 | BBinder* local = binder->localBinder(); |
| 148 | if (local != nullptr) { |
Steven Moreland | 16a4106 | 2021-07-23 13:35:25 -0700 | [diff] [blame] | 149 | return local->mStability; |
Steven Moreland | a7fb018 | 2020-02-26 16:02:08 -0800 | [diff] [blame] | 150 | } |
| 151 | |
Steven Moreland | 16a4106 | 2021-07-23 13:35:25 -0700 | [diff] [blame] | 152 | return binder->remoteBinder()->mStability; |
Steven Moreland | dea3cf9 | 2019-07-16 18:06:55 -0700 | [diff] [blame] | 153 | } |
| 154 | |
Steven Moreland | 16a4106 | 2021-07-23 13:35:25 -0700 | [diff] [blame] | 155 | bool Stability::check(int16_t provided, Level required) { |
| 156 | bool stable = (provided & required) == required; |
Steven Moreland | dea3cf9 | 2019-07-16 18:06:55 -0700 | [diff] [blame] | 157 | |
Steven Moreland | 16a4106 | 2021-07-23 13:35:25 -0700 | [diff] [blame] | 158 | if (provided != UNDECLARED && !isDeclaredLevel(provided)) { |
| 159 | ALOGE("Unknown stability when checking interface stability %d.", provided); |
Steven Moreland | dea3cf9 | 2019-07-16 18:06:55 -0700 | [diff] [blame] | 160 | |
| 161 | stable = false; |
| 162 | } |
| 163 | |
Steven Moreland | dea3cf9 | 2019-07-16 18:06:55 -0700 | [diff] [blame] | 164 | return stable; |
| 165 | } |
| 166 | |
Steven Moreland | 16a4106 | 2021-07-23 13:35:25 -0700 | [diff] [blame] | 167 | bool Stability::isDeclaredLevel(int32_t stability) { |
Steven Moreland | dea3cf9 | 2019-07-16 18:06:55 -0700 | [diff] [blame] | 168 | return stability == VENDOR || stability == SYSTEM || stability == VINTF; |
| 169 | } |
| 170 | |
Steven Moreland | 16a4106 | 2021-07-23 13:35:25 -0700 | [diff] [blame] | 171 | std::string Stability::levelString(int32_t level) { |
Steven Moreland | 89ddfc5 | 2020-11-13 02:39:26 +0000 | [diff] [blame] | 172 | switch (level) { |
Steven Moreland | dea3cf9 | 2019-07-16 18:06:55 -0700 | [diff] [blame] | 173 | case Level::UNDECLARED: return "undeclared stability"; |
| 174 | case Level::VENDOR: return "vendor stability"; |
| 175 | case Level::SYSTEM: return "system stability"; |
| 176 | case Level::VINTF: return "vintf stability"; |
| 177 | } |
Steven Moreland | 89ddfc5 | 2020-11-13 02:39:26 +0000 | [diff] [blame] | 178 | return "unknown stability " + std::to_string(level); |
Steven Moreland | dea3cf9 | 2019-07-16 18:06:55 -0700 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | } // namespace internal |
Steven Moreland | 86a17f8 | 2019-09-10 10:18:00 -0700 | [diff] [blame] | 182 | } // namespace stability |