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