| 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 |  | 
| Steven Moreland | 89ddfc5 | 2020-11-13 02:39:26 +0000 | [diff] [blame] | 26 | // the libbinder parcel format is currently unstable | 
|  | 27 |  | 
|  | 28 | // oldest version which is supported | 
|  | 29 | constexpr uint8_t kBinderWireFormatOldest = 1; | 
|  | 30 | // current version | 
|  | 31 | constexpr uint8_t kBinderWireFormatVersion = 1; | 
|  | 32 |  | 
|  | 33 | Stability::Category Stability::Category::currentFromLevel(Level level) { | 
|  | 34 | return { | 
|  | 35 | .version = kBinderWireFormatVersion, | 
| Steven Moreland | 89ddfc5 | 2020-11-13 02:39:26 +0000 | [diff] [blame] | 36 | .level = level, | 
|  | 37 | }; | 
|  | 38 | } | 
|  | 39 |  | 
| Kalesh Singh | 8b80291 | 2021-03-31 12:13:56 -0400 | [diff] [blame] | 40 | void Stability::forceDowngradeToStability(const sp<IBinder>& binder, Level level) { | 
| Steven Moreland | e35fef3 | 2021-03-23 01:38:24 +0000 | [diff] [blame] | 41 | // Downgrading a remote binder would require also copying the version from | 
|  | 42 | // the binder sent here. In practice though, we don't need to downgrade the | 
|  | 43 | // stability of a remote binder, since this would as an effect only restrict | 
|  | 44 | // what we can do to it. | 
|  | 45 | LOG_ALWAYS_FATAL_IF(!binder || !binder->localBinder(), "Can only downgrade local binder"); | 
|  | 46 |  | 
| Kalesh Singh | 8b80291 | 2021-03-31 12:13:56 -0400 | [diff] [blame] | 47 | auto stability = Category::currentFromLevel(level); | 
| Steven Moreland | e35fef3 | 2021-03-23 01:38:24 +0000 | [diff] [blame] | 48 | status_t result = setRepr(binder.get(), stability.repr(), REPR_LOG | REPR_ALLOW_DOWNGRADE); | 
|  | 49 | LOG_ALWAYS_FATAL_IF(result != OK, "Should only mark known object."); | 
|  | 50 | } | 
|  | 51 |  | 
| Kalesh Singh | 8b80291 | 2021-03-31 12:13:56 -0400 | [diff] [blame] | 52 | void Stability::forceDowngradeToLocalStability(const sp<IBinder>& binder) { | 
|  | 53 | forceDowngradeToStability(binder, getLocalLevel()); | 
|  | 54 | } | 
|  | 55 |  | 
|  | 56 | void Stability::forceDowngradeToSystemStability(const sp<IBinder>& binder) { | 
|  | 57 | forceDowngradeToStability(binder, Level::SYSTEM); | 
|  | 58 | } | 
|  | 59 |  | 
|  | 60 | void Stability::forceDowngradeToVendorStability(const sp<IBinder>& binder) { | 
|  | 61 | forceDowngradeToStability(binder, Level::VENDOR); | 
|  | 62 | } | 
|  | 63 |  | 
| Steven Moreland | 89ddfc5 | 2020-11-13 02:39:26 +0000 | [diff] [blame] | 64 | std::string Stability::Category::debugString() { | 
|  | 65 | return levelString(level) + " wire protocol version " | 
|  | 66 | + std::to_string(version); | 
|  | 67 | } | 
|  | 68 |  | 
| Steven Moreland | dea3cf9 | 2019-07-16 18:06:55 -0700 | [diff] [blame] | 69 | void Stability::markCompilationUnit(IBinder* binder) { | 
| Steven Moreland | 89ddfc5 | 2020-11-13 02:39:26 +0000 | [diff] [blame] | 70 | auto stability = Category::currentFromLevel(getLocalLevel()); | 
| Steven Moreland | e35fef3 | 2021-03-23 01:38:24 +0000 | [diff] [blame] | 71 | status_t result = setRepr(binder, stability.repr(), REPR_LOG); | 
| Steven Moreland | dea3cf9 | 2019-07-16 18:06:55 -0700 | [diff] [blame] | 72 | LOG_ALWAYS_FATAL_IF(result != OK, "Should only mark known object."); | 
|  | 73 | } | 
|  | 74 |  | 
|  | 75 | void Stability::markVintf(IBinder* binder) { | 
| Steven Moreland | 89ddfc5 | 2020-11-13 02:39:26 +0000 | [diff] [blame] | 76 | auto stability = Category::currentFromLevel(Level::VINTF); | 
| Steven Moreland | e35fef3 | 2021-03-23 01:38:24 +0000 | [diff] [blame] | 77 | status_t result = setRepr(binder, stability.repr(), REPR_LOG); | 
| Steven Moreland | dea3cf9 | 2019-07-16 18:06:55 -0700 | [diff] [blame] | 78 | LOG_ALWAYS_FATAL_IF(result != OK, "Should only mark known object."); | 
|  | 79 | } | 
|  | 80 |  | 
| Steven Moreland | e5a6a87 | 2021-05-19 22:11:37 +0000 | [diff] [blame] | 81 | std::string Stability::debugToString(const sp<IBinder>& binder) { | 
| Steven Moreland | 89ddfc5 | 2020-11-13 02:39:26 +0000 | [diff] [blame] | 82 | auto stability = getCategory(binder.get()); | 
| Steven Moreland | e5a6a87 | 2021-05-19 22:11:37 +0000 | [diff] [blame] | 83 | return stability.debugString(); | 
| Steven Moreland | 64ae917 | 2019-08-02 20:45:15 -0700 | [diff] [blame] | 84 | } | 
|  | 85 |  | 
| Steven Moreland | c709dd8 | 2019-08-05 20:30:14 -0700 | [diff] [blame] | 86 | void Stability::markVndk(IBinder* binder) { | 
| Steven Moreland | 89ddfc5 | 2020-11-13 02:39:26 +0000 | [diff] [blame] | 87 | auto stability = Category::currentFromLevel(Level::VENDOR); | 
| Steven Moreland | e35fef3 | 2021-03-23 01:38:24 +0000 | [diff] [blame] | 88 | status_t result = setRepr(binder, stability.repr(), REPR_LOG); | 
| Steven Moreland | c709dd8 | 2019-08-05 20:30:14 -0700 | [diff] [blame] | 89 | LOG_ALWAYS_FATAL_IF(result != OK, "Should only mark known object."); | 
|  | 90 | } | 
|  | 91 |  | 
| Steven Moreland | 86a17f8 | 2019-09-10 10:18:00 -0700 | [diff] [blame] | 92 | bool Stability::requiresVintfDeclaration(const sp<IBinder>& binder) { | 
| Steven Moreland | 89ddfc5 | 2020-11-13 02:39:26 +0000 | [diff] [blame] | 93 | return check(getCategory(binder.get()), Level::VINTF); | 
| Steven Moreland | 86a17f8 | 2019-09-10 10:18:00 -0700 | [diff] [blame] | 94 | } | 
|  | 95 |  | 
| Steven Moreland | 2a9f32f | 2019-07-31 17:51:25 -0700 | [diff] [blame] | 96 | void Stability::tryMarkCompilationUnit(IBinder* binder) { | 
| Steven Moreland | 89ddfc5 | 2020-11-13 02:39:26 +0000 | [diff] [blame] | 97 | auto stability = Category::currentFromLevel(getLocalLevel()); | 
| Steven Moreland | e35fef3 | 2021-03-23 01:38:24 +0000 | [diff] [blame] | 98 | (void) setRepr(binder, stability.repr(), REPR_NONE); | 
| Steven Moreland | 2f2c4e0 | 2020-07-28 18:11:21 +0000 | [diff] [blame] | 99 | } | 
|  | 100 |  | 
| Steven Moreland | 89ddfc5 | 2020-11-13 02:39:26 +0000 | [diff] [blame] | 101 | Stability::Level Stability::getLocalLevel() { | 
| Steven Moreland | b6c7e22 | 2021-02-18 19:20:14 +0000 | [diff] [blame] | 102 | #ifdef __ANDROID_APEX__ | 
|  | 103 | #error APEX can't use libbinder (must use libbinder_ndk) | 
|  | 104 | #endif | 
|  | 105 |  | 
| Steven Moreland | 2f2c4e0 | 2020-07-28 18:11:21 +0000 | [diff] [blame] | 106 | #ifdef __ANDROID_VNDK__ | 
| Steven Moreland | b6c7e22 | 2021-02-18 19:20:14 +0000 | [diff] [blame] | 107 | return Level::VENDOR; | 
| Steven Moreland | 2f2c4e0 | 2020-07-28 18:11:21 +0000 | [diff] [blame] | 108 | #else | 
|  | 109 | // TODO(b/139325195): split up stability levels for system/APEX. | 
|  | 110 | return Level::SYSTEM; | 
|  | 111 | #endif | 
| Steven Moreland | 2a9f32f | 2019-07-31 17:51:25 -0700 | [diff] [blame] | 112 | } | 
|  | 113 |  | 
| Steven Moreland | e35fef3 | 2021-03-23 01:38:24 +0000 | [diff] [blame] | 114 | status_t Stability::setRepr(IBinder* binder, int32_t representation, uint32_t flags) { | 
|  | 115 | bool log = flags & REPR_LOG; | 
|  | 116 | bool allowDowngrade = flags & REPR_ALLOW_DOWNGRADE; | 
|  | 117 |  | 
| Steven Moreland | 89ddfc5 | 2020-11-13 02:39:26 +0000 | [diff] [blame] | 118 | auto current = getCategory(binder); | 
|  | 119 | auto setting = Category::fromRepr(representation); | 
|  | 120 |  | 
|  | 121 | // If we have ahold of a binder with a newer declared version, then it | 
|  | 122 | // should support older versions, and we will simply write our parcels with | 
|  | 123 | // the current wire parcel format. | 
|  | 124 | if (setting.version < kBinderWireFormatOldest) { | 
|  | 125 | // always log, because this shouldn't happen | 
|  | 126 | ALOGE("Cannot accept binder with older binder wire protocol version " | 
|  | 127 | "%u. Versions less than %u are unsupported.", setting.version, | 
|  | 128 | kBinderWireFormatOldest); | 
|  | 129 | return BAD_TYPE; | 
|  | 130 | } | 
| Steven Moreland | dea3cf9 | 2019-07-16 18:06:55 -0700 | [diff] [blame] | 131 |  | 
|  | 132 | // null binder is always written w/ 'UNDECLARED' stability | 
|  | 133 | if (binder == nullptr) { | 
| Steven Moreland | 89ddfc5 | 2020-11-13 02:39:26 +0000 | [diff] [blame] | 134 | if (setting.level == UNDECLARED) { | 
| Steven Moreland | dea3cf9 | 2019-07-16 18:06:55 -0700 | [diff] [blame] | 135 | return OK; | 
|  | 136 | } else { | 
| Steven Moreland | 2a9f32f | 2019-07-31 17:51:25 -0700 | [diff] [blame] | 137 | if (log) { | 
|  | 138 | ALOGE("Null binder written with stability %s.", | 
| Steven Moreland | 89ddfc5 | 2020-11-13 02:39:26 +0000 | [diff] [blame] | 139 | levelString(setting.level).c_str()); | 
| Steven Moreland | 2a9f32f | 2019-07-31 17:51:25 -0700 | [diff] [blame] | 140 | } | 
| Steven Moreland | dea3cf9 | 2019-07-16 18:06:55 -0700 | [diff] [blame] | 141 | return BAD_TYPE; | 
|  | 142 | } | 
|  | 143 | } | 
|  | 144 |  | 
| Steven Moreland | 89ddfc5 | 2020-11-13 02:39:26 +0000 | [diff] [blame] | 145 | if (!isDeclaredLevel(setting.level)) { | 
| Steven Moreland | 2a9f32f | 2019-07-31 17:51:25 -0700 | [diff] [blame] | 146 | if (log) { | 
| Steven Moreland | 89ddfc5 | 2020-11-13 02:39:26 +0000 | [diff] [blame] | 147 | ALOGE("Can only set known stability, not %u.", setting.level); | 
| Steven Moreland | dea3cf9 | 2019-07-16 18:06:55 -0700 | [diff] [blame] | 148 | } | 
| Steven Moreland | 2a9f32f | 2019-07-31 17:51:25 -0700 | [diff] [blame] | 149 | return BAD_TYPE; | 
| Steven Moreland | dea3cf9 | 2019-07-16 18:06:55 -0700 | [diff] [blame] | 150 | } | 
|  | 151 |  | 
| Steven Moreland | e35fef3 | 2021-03-23 01:38:24 +0000 | [diff] [blame] | 152 | if (current == setting) return OK; | 
|  | 153 |  | 
|  | 154 | bool hasAlreadyBeenSet = current.repr() != 0; | 
|  | 155 | bool isAllowedDowngrade = allowDowngrade && check(current, setting.level); | 
|  | 156 | if (hasAlreadyBeenSet && !isAllowedDowngrade) { | 
| Steven Moreland | 2a9f32f | 2019-07-31 17:51:25 -0700 | [diff] [blame] | 157 | if (log) { | 
| Steven Moreland | 89ddfc5 | 2020-11-13 02:39:26 +0000 | [diff] [blame] | 158 | ALOGE("Interface being set with %s but it is already marked as %s", | 
|  | 159 | setting.debugString().c_str(), | 
|  | 160 | current.debugString().c_str()); | 
| Steven Moreland | 2a9f32f | 2019-07-31 17:51:25 -0700 | [diff] [blame] | 161 | } | 
| Steven Moreland | dea3cf9 | 2019-07-16 18:06:55 -0700 | [diff] [blame] | 162 | return BAD_TYPE; | 
|  | 163 | } | 
|  | 164 |  | 
| Steven Moreland | e35fef3 | 2021-03-23 01:38:24 +0000 | [diff] [blame] | 165 | if (isAllowedDowngrade) { | 
|  | 166 | ALOGI("Interface set with %s downgraded to %s stability", | 
|  | 167 | current.debugString().c_str(), | 
|  | 168 | setting.debugString().c_str()); | 
|  | 169 | } | 
| Steven Moreland | dea3cf9 | 2019-07-16 18:06:55 -0700 | [diff] [blame] | 170 |  | 
| Steven Moreland | a7fb018 | 2020-02-26 16:02:08 -0800 | [diff] [blame] | 171 | BBinder* local = binder->localBinder(); | 
|  | 172 | if (local != nullptr) { | 
| Steven Moreland | 89ddfc5 | 2020-11-13 02:39:26 +0000 | [diff] [blame] | 173 | local->mStability = setting.repr(); | 
| Steven Moreland | a7fb018 | 2020-02-26 16:02:08 -0800 | [diff] [blame] | 174 | } else { | 
| Steven Moreland | 89ddfc5 | 2020-11-13 02:39:26 +0000 | [diff] [blame] | 175 | binder->remoteBinder()->mStability = setting.repr(); | 
| Steven Moreland | a7fb018 | 2020-02-26 16:02:08 -0800 | [diff] [blame] | 176 | } | 
| Steven Moreland | dea3cf9 | 2019-07-16 18:06:55 -0700 | [diff] [blame] | 177 |  | 
|  | 178 | return OK; | 
|  | 179 | } | 
|  | 180 |  | 
| Steven Moreland | 89ddfc5 | 2020-11-13 02:39:26 +0000 | [diff] [blame] | 181 | Stability::Category Stability::getCategory(IBinder* binder) { | 
|  | 182 | if (binder == nullptr) { | 
|  | 183 | return Category::currentFromLevel(Level::UNDECLARED); | 
|  | 184 | } | 
| Steven Moreland | dea3cf9 | 2019-07-16 18:06:55 -0700 | [diff] [blame] | 185 |  | 
| Steven Moreland | a7fb018 | 2020-02-26 16:02:08 -0800 | [diff] [blame] | 186 | BBinder* local = binder->localBinder(); | 
|  | 187 | if (local != nullptr) { | 
| Steven Moreland | 89ddfc5 | 2020-11-13 02:39:26 +0000 | [diff] [blame] | 188 | return Category::fromRepr(local->mStability); | 
| Steven Moreland | a7fb018 | 2020-02-26 16:02:08 -0800 | [diff] [blame] | 189 | } | 
|  | 190 |  | 
| Steven Moreland | 89ddfc5 | 2020-11-13 02:39:26 +0000 | [diff] [blame] | 191 | return Category::fromRepr(binder->remoteBinder()->mStability); | 
| Steven Moreland | dea3cf9 | 2019-07-16 18:06:55 -0700 | [diff] [blame] | 192 | } | 
|  | 193 |  | 
| Steven Moreland | 89ddfc5 | 2020-11-13 02:39:26 +0000 | [diff] [blame] | 194 | bool Stability::check(Category provided, Level required) { | 
|  | 195 | bool stable = (provided.level & required) == required; | 
| Steven Moreland | dea3cf9 | 2019-07-16 18:06:55 -0700 | [diff] [blame] | 196 |  | 
| Steven Moreland | 89ddfc5 | 2020-11-13 02:39:26 +0000 | [diff] [blame] | 197 | if (provided.level != UNDECLARED && !isDeclaredLevel(provided.level)) { | 
|  | 198 | ALOGE("Unknown stability when checking interface stability %d.", | 
|  | 199 | provided.level); | 
| Steven Moreland | dea3cf9 | 2019-07-16 18:06:55 -0700 | [diff] [blame] | 200 |  | 
|  | 201 | stable = false; | 
|  | 202 | } | 
|  | 203 |  | 
| Steven Moreland | dea3cf9 | 2019-07-16 18:06:55 -0700 | [diff] [blame] | 204 | return stable; | 
|  | 205 | } | 
|  | 206 |  | 
| Steven Moreland | 89ddfc5 | 2020-11-13 02:39:26 +0000 | [diff] [blame] | 207 | bool Stability::isDeclaredLevel(Level stability) { | 
| Steven Moreland | dea3cf9 | 2019-07-16 18:06:55 -0700 | [diff] [blame] | 208 | return stability == VENDOR || stability == SYSTEM || stability == VINTF; | 
|  | 209 | } | 
|  | 210 |  | 
| Steven Moreland | 89ddfc5 | 2020-11-13 02:39:26 +0000 | [diff] [blame] | 211 | std::string Stability::levelString(Level level) { | 
|  | 212 | switch (level) { | 
| Steven Moreland | dea3cf9 | 2019-07-16 18:06:55 -0700 | [diff] [blame] | 213 | case Level::UNDECLARED: return "undeclared stability"; | 
|  | 214 | case Level::VENDOR: return "vendor stability"; | 
|  | 215 | case Level::SYSTEM: return "system stability"; | 
|  | 216 | case Level::VINTF: return "vintf stability"; | 
|  | 217 | } | 
| Steven Moreland | 89ddfc5 | 2020-11-13 02:39:26 +0000 | [diff] [blame] | 218 | return "unknown stability " + std::to_string(level); | 
| Steven Moreland | dea3cf9 | 2019-07-16 18:06:55 -0700 | [diff] [blame] | 219 | } | 
|  | 220 |  | 
|  | 221 | }  // namespace internal | 
| Steven Moreland | 86a17f8 | 2019-09-10 10:18:00 -0700 | [diff] [blame] | 222 | }  // namespace stability |