The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2005 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 | |
Mathias Agopian | c5b2c0b | 2009-05-19 19:08:10 -0700 | [diff] [blame] | 17 | #include <binder/Binder.h> |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 18 | |
Bailey Forrest | 6913c46 | 2015-08-18 17:15:10 -0700 | [diff] [blame] | 19 | #include <atomic> |
Yifan Hong | 8b89085 | 2021-06-10 13:44:09 -0700 | [diff] [blame] | 20 | #include <set> |
Yifan Hong | 84bedeb | 2021-04-21 21:37:17 -0700 | [diff] [blame] | 21 | |
| 22 | #include <android-base/unique_fd.h> |
Mathias Agopian | c5b2c0b | 2009-05-19 19:08:10 -0700 | [diff] [blame] | 23 | #include <binder/BpBinder.h> |
| 24 | #include <binder/IInterface.h> |
Yifan Hong | 84bedeb | 2021-04-21 21:37:17 -0700 | [diff] [blame] | 25 | #include <binder/IPCThreadState.h> |
Dianne Hackborn | 23eb1e2 | 2015-10-07 17:35:27 -0700 | [diff] [blame] | 26 | #include <binder/IResultReceiver.h> |
Dianne Hackborn | 1941a40 | 2016-08-29 12:30:43 -0700 | [diff] [blame] | 27 | #include <binder/IShellCallback.h> |
Mathias Agopian | c5b2c0b | 2009-05-19 19:08:10 -0700 | [diff] [blame] | 28 | #include <binder/Parcel.h> |
Fabián Cañas | 356a022 | 2023-01-13 16:32:53 -0500 | [diff] [blame] | 29 | #include <binder/RecordedTransaction.h> |
Yifan Hong | 84bedeb | 2021-04-21 21:37:17 -0700 | [diff] [blame] | 30 | #include <binder/RpcServer.h> |
Sahil Somani | 2522b07 | 2022-08-08 14:07:47 -0700 | [diff] [blame] | 31 | #include <pthread.h> |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 32 | |
Yifan Hong | 84bedeb | 2021-04-21 21:37:17 -0700 | [diff] [blame] | 33 | #include <inttypes.h> |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 34 | #include <stdio.h> |
| 35 | |
Andrei Homescu | f55d688 | 2022-04-30 00:50:20 +0000 | [diff] [blame] | 36 | #ifdef __linux__ |
| 37 | #include <linux/sched.h> |
| 38 | #endif |
| 39 | |
Steven Moreland | 3215028 | 2021-11-12 22:54:53 +0000 | [diff] [blame] | 40 | #include "BuildFlags.h" |
Tomasz Wasilczyk | 346f104 | 2023-10-09 17:02:46 +0000 | [diff] [blame] | 41 | #include "OS.h" |
Yifan Hong | 84bedeb | 2021-04-21 21:37:17 -0700 | [diff] [blame] | 42 | #include "RpcState.h" |
| 43 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 44 | namespace android { |
| 45 | |
Tomasz Wasilczyk | b8ebcca | 2023-10-09 19:54:20 +0000 | [diff] [blame] | 46 | constexpr uid_t kUidRoot = 0; |
| 47 | |
Steven Moreland | 90c1f9a | 2021-05-03 18:27:24 +0000 | [diff] [blame] | 48 | // Service implementations inherit from BBinder and IBinder, and this is frozen |
| 49 | // in prebuilts. |
| 50 | #ifdef __LP64__ |
| 51 | static_assert(sizeof(IBinder) == 24); |
| 52 | static_assert(sizeof(BBinder) == 40); |
| 53 | #else |
| 54 | static_assert(sizeof(IBinder) == 12); |
| 55 | static_assert(sizeof(BBinder) == 20); |
| 56 | #endif |
| 57 | |
Steven Moreland | d9a7400 | 2022-06-02 00:03:18 +0000 | [diff] [blame] | 58 | // global b/c b/230079120 - consistent symbol table |
Yifan Hong | 84bedeb | 2021-04-21 21:37:17 -0700 | [diff] [blame] | 59 | #ifdef BINDER_RPC_DEV_SERVERS |
Steven Moreland | 5255c97 | 2023-05-19 22:53:41 +0000 | [diff] [blame] | 60 | constexpr bool kEnableRpcDevServers = true; |
Yifan Hong | 84bedeb | 2021-04-21 21:37:17 -0700 | [diff] [blame] | 61 | #else |
Steven Moreland | 5255c97 | 2023-05-19 22:53:41 +0000 | [diff] [blame] | 62 | constexpr bool kEnableRpcDevServers = false; |
Yifan Hong | 84bedeb | 2021-04-21 21:37:17 -0700 | [diff] [blame] | 63 | #endif |
| 64 | |
Sahil Somani | 2522b07 | 2022-08-08 14:07:47 -0700 | [diff] [blame] | 65 | #ifdef BINDER_ENABLE_RECORDING |
Steven Moreland | 5255c97 | 2023-05-19 22:53:41 +0000 | [diff] [blame] | 66 | constexpr bool kEnableRecording = true; |
Sahil Somani | 2522b07 | 2022-08-08 14:07:47 -0700 | [diff] [blame] | 67 | #else |
Steven Moreland | 5255c97 | 2023-05-19 22:53:41 +0000 | [diff] [blame] | 68 | constexpr bool kEnableRecording = false; |
Sahil Somani | 2522b07 | 2022-08-08 14:07:47 -0700 | [diff] [blame] | 69 | #endif |
| 70 | |
Martijn Coenen | 1cad19c | 2021-10-04 09:19:01 +0200 | [diff] [blame] | 71 | // Log any reply transactions for which the data exceeds this size |
| 72 | #define LOG_REPLIES_OVER_SIZE (300 * 1024) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 73 | // --------------------------------------------------------------------------- |
| 74 | |
Mathias Agopian | 83c0446 | 2009-05-22 19:00:22 -0700 | [diff] [blame] | 75 | IBinder::IBinder() |
| 76 | : RefBase() |
| 77 | { |
| 78 | } |
| 79 | |
| 80 | IBinder::~IBinder() |
| 81 | { |
| 82 | } |
| 83 | |
| 84 | // --------------------------------------------------------------------------- |
| 85 | |
Colin Cross | 6f4f3ab | 2014-02-05 17:42:44 -0800 | [diff] [blame] | 86 | sp<IInterface> IBinder::queryLocalInterface(const String16& /*descriptor*/) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 87 | { |
Yi Kong | fdd8da9 | 2018-06-07 17:52:27 -0700 | [diff] [blame] | 88 | return nullptr; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | BBinder* IBinder::localBinder() |
| 92 | { |
Yi Kong | fdd8da9 | 2018-06-07 17:52:27 -0700 | [diff] [blame] | 93 | return nullptr; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | BpBinder* IBinder::remoteBinder() |
| 97 | { |
Yi Kong | fdd8da9 | 2018-06-07 17:52:27 -0700 | [diff] [blame] | 98 | return nullptr; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | bool IBinder::checkSubclass(const void* /*subclassID*/) const |
| 102 | { |
| 103 | return false; |
| 104 | } |
| 105 | |
Dianne Hackborn | 23eb1e2 | 2015-10-07 17:35:27 -0700 | [diff] [blame] | 106 | |
Dianne Hackborn | f2bf93b | 2015-10-14 15:13:02 -0700 | [diff] [blame] | 107 | status_t IBinder::shellCommand(const sp<IBinder>& target, int in, int out, int err, |
Dianne Hackborn | 1941a40 | 2016-08-29 12:30:43 -0700 | [diff] [blame] | 108 | Vector<String16>& args, const sp<IShellCallback>& callback, |
| 109 | const sp<IResultReceiver>& resultReceiver) |
Dianne Hackborn | 23eb1e2 | 2015-10-07 17:35:27 -0700 | [diff] [blame] | 110 | { |
Dianne Hackborn | f2bf93b | 2015-10-14 15:13:02 -0700 | [diff] [blame] | 111 | Parcel send; |
| 112 | Parcel reply; |
| 113 | send.writeFileDescriptor(in); |
| 114 | send.writeFileDescriptor(out); |
| 115 | send.writeFileDescriptor(err); |
| 116 | const size_t numArgs = args.size(); |
| 117 | send.writeInt32(numArgs); |
| 118 | for (size_t i = 0; i < numArgs; i++) { |
| 119 | send.writeString16(args[i]); |
Dianne Hackborn | 23eb1e2 | 2015-10-07 17:35:27 -0700 | [diff] [blame] | 120 | } |
Yi Kong | fdd8da9 | 2018-06-07 17:52:27 -0700 | [diff] [blame] | 121 | send.writeStrongBinder(callback != nullptr ? IInterface::asBinder(callback) : nullptr); |
| 122 | send.writeStrongBinder(resultReceiver != nullptr ? IInterface::asBinder(resultReceiver) : nullptr); |
Dianne Hackborn | f2bf93b | 2015-10-14 15:13:02 -0700 | [diff] [blame] | 123 | return target->transact(SHELL_COMMAND_TRANSACTION, send, &reply); |
Dianne Hackborn | 23eb1e2 | 2015-10-07 17:35:27 -0700 | [diff] [blame] | 124 | } |
| 125 | |
Steven Moreland | b8ad08d | 2019-08-09 14:42:56 -0700 | [diff] [blame] | 126 | status_t IBinder::getExtension(sp<IBinder>* out) { |
| 127 | BBinder* local = this->localBinder(); |
| 128 | if (local != nullptr) { |
| 129 | *out = local->getExtension(); |
| 130 | return OK; |
| 131 | } |
| 132 | |
| 133 | BpBinder* proxy = this->remoteBinder(); |
| 134 | LOG_ALWAYS_FATAL_IF(proxy == nullptr); |
| 135 | |
| 136 | Parcel data; |
| 137 | Parcel reply; |
| 138 | status_t status = transact(EXTENSION_TRANSACTION, data, &reply); |
| 139 | if (status != OK) return status; |
| 140 | |
| 141 | return reply.readNullableStrongBinder(out); |
| 142 | } |
| 143 | |
Steven Moreland | 86080b8 | 2019-09-23 15:41:18 -0700 | [diff] [blame] | 144 | status_t IBinder::getDebugPid(pid_t* out) { |
| 145 | BBinder* local = this->localBinder(); |
| 146 | if (local != nullptr) { |
| 147 | *out = local->getDebugPid(); |
| 148 | return OK; |
| 149 | } |
| 150 | |
| 151 | BpBinder* proxy = this->remoteBinder(); |
| 152 | LOG_ALWAYS_FATAL_IF(proxy == nullptr); |
| 153 | |
| 154 | Parcel data; |
| 155 | Parcel reply; |
| 156 | status_t status = transact(DEBUG_PID_TRANSACTION, data, &reply); |
| 157 | if (status != OK) return status; |
| 158 | |
| 159 | int32_t pid; |
| 160 | status = reply.readInt32(&pid); |
| 161 | if (status != OK) return status; |
| 162 | |
| 163 | if (pid < 0 || pid > std::numeric_limits<pid_t>::max()) { |
| 164 | return BAD_VALUE; |
| 165 | } |
| 166 | *out = pid; |
| 167 | return OK; |
| 168 | } |
| 169 | |
Yifan Hong | 02530ec | 2021-06-10 13:38:38 -0700 | [diff] [blame] | 170 | status_t IBinder::setRpcClientDebug(android::base::unique_fd socketFd, |
| 171 | const sp<IBinder>& keepAliveBinder) { |
Steven Moreland | d9a7400 | 2022-06-02 00:03:18 +0000 | [diff] [blame] | 172 | if (!kEnableRpcDevServers) { |
Yifan Hong | 84bedeb | 2021-04-21 21:37:17 -0700 | [diff] [blame] | 173 | ALOGW("setRpcClientDebug disallowed because RPC is not enabled"); |
| 174 | return INVALID_OPERATION; |
| 175 | } |
Steven Moreland | 3215028 | 2021-11-12 22:54:53 +0000 | [diff] [blame] | 176 | if (!kEnableKernelIpc) { |
| 177 | ALOGW("setRpcClientDebug disallowed because kernel binder is not enabled"); |
| 178 | return INVALID_OPERATION; |
| 179 | } |
Yifan Hong | 84bedeb | 2021-04-21 21:37:17 -0700 | [diff] [blame] | 180 | |
| 181 | BBinder* local = this->localBinder(); |
| 182 | if (local != nullptr) { |
Yifan Hong | 02530ec | 2021-06-10 13:38:38 -0700 | [diff] [blame] | 183 | return local->BBinder::setRpcClientDebug(std::move(socketFd), keepAliveBinder); |
Yifan Hong | 84bedeb | 2021-04-21 21:37:17 -0700 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | BpBinder* proxy = this->remoteBinder(); |
| 187 | LOG_ALWAYS_FATAL_IF(proxy == nullptr, "binder object must be either local or remote"); |
| 188 | |
| 189 | Parcel data; |
| 190 | Parcel reply; |
| 191 | status_t status; |
| 192 | if (status = data.writeBool(socketFd.ok()); status != OK) return status; |
| 193 | if (socketFd.ok()) { |
| 194 | // writeUniqueFileDescriptor currently makes an unnecessary dup(). |
| 195 | status = data.writeFileDescriptor(socketFd.release(), true /* own */); |
| 196 | if (status != OK) return status; |
| 197 | } |
Yifan Hong | 02530ec | 2021-06-10 13:38:38 -0700 | [diff] [blame] | 198 | if (status = data.writeStrongBinder(keepAliveBinder); status != OK) return status; |
Yifan Hong | 84bedeb | 2021-04-21 21:37:17 -0700 | [diff] [blame] | 199 | return transact(SET_RPC_CLIENT_TRANSACTION, data, &reply); |
| 200 | } |
| 201 | |
Steven Moreland | 9e759e8 | 2021-06-25 21:30:23 +0000 | [diff] [blame] | 202 | void IBinder::withLock(const std::function<void()>& doWithLock) { |
| 203 | BBinder* local = localBinder(); |
| 204 | if (local) { |
| 205 | local->withLock(doWithLock); |
| 206 | return; |
| 207 | } |
| 208 | BpBinder* proxy = this->remoteBinder(); |
| 209 | LOG_ALWAYS_FATAL_IF(proxy == nullptr, "binder object must be either local or remote"); |
| 210 | proxy->withLock(doWithLock); |
| 211 | } |
| 212 | |
Devin Moore | 3faaa00 | 2022-07-27 15:54:06 +0000 | [diff] [blame] | 213 | sp<IBinder> IBinder::lookupOrCreateWeak(const void* objectID, object_make_func make, |
| 214 | const void* makeArgs) { |
| 215 | BBinder* local = localBinder(); |
| 216 | if (local) { |
| 217 | return local->lookupOrCreateWeak(objectID, make, makeArgs); |
| 218 | } |
| 219 | BpBinder* proxy = this->remoteBinder(); |
| 220 | LOG_ALWAYS_FATAL_IF(proxy == nullptr, "binder object must be either local or remote"); |
| 221 | return proxy->lookupOrCreateWeak(objectID, make, makeArgs); |
| 222 | } |
| 223 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 224 | // --------------------------------------------------------------------------- |
| 225 | |
Yifan Hong | 8b89085 | 2021-06-10 13:44:09 -0700 | [diff] [blame] | 226 | class BBinder::RpcServerLink : public IBinder::DeathRecipient { |
| 227 | public: |
| 228 | // On binder died, calls RpcServer::shutdown on @a rpcServer, and removes itself from @a binder. |
| 229 | RpcServerLink(const sp<RpcServer>& rpcServer, const sp<IBinder>& keepAliveBinder, |
| 230 | const wp<BBinder>& binder) |
| 231 | : mRpcServer(rpcServer), mKeepAliveBinder(keepAliveBinder), mBinder(binder) {} |
Steven Moreland | d9a7400 | 2022-06-02 00:03:18 +0000 | [diff] [blame] | 232 | virtual ~RpcServerLink(); |
Yifan Hong | 8b89085 | 2021-06-10 13:44:09 -0700 | [diff] [blame] | 233 | void binderDied(const wp<IBinder>&) override { |
Steven Moreland | f6957b7 | 2022-09-24 03:12:16 +0000 | [diff] [blame] | 234 | auto promoted = mBinder.promote(); |
| 235 | ALOGI("RpcBinder: binder died, shutting down RpcServer for %s", |
| 236 | promoted ? String8(promoted->getInterfaceDescriptor()).c_str() : "<NULL>"); |
| 237 | |
Yifan Hong | 8b89085 | 2021-06-10 13:44:09 -0700 | [diff] [blame] | 238 | if (mRpcServer == nullptr) { |
| 239 | ALOGW("RpcServerLink: Unable to shut down RpcServer because it does not exist."); |
| 240 | } else { |
| 241 | ALOGW_IF(!mRpcServer->shutdown(), |
| 242 | "RpcServerLink: RpcServer did not shut down properly. Not started?"); |
| 243 | } |
| 244 | mRpcServer.clear(); |
| 245 | |
Steven Moreland | f6957b7 | 2022-09-24 03:12:16 +0000 | [diff] [blame] | 246 | if (promoted) { |
Yifan Hong | 8b89085 | 2021-06-10 13:44:09 -0700 | [diff] [blame] | 247 | promoted->removeRpcServerLink(sp<RpcServerLink>::fromExisting(this)); |
| 248 | } |
| 249 | mBinder.clear(); |
| 250 | } |
| 251 | |
| 252 | private: |
| 253 | sp<RpcServer> mRpcServer; |
| 254 | sp<IBinder> mKeepAliveBinder; // hold to avoid automatically unlinking |
| 255 | wp<BBinder> mBinder; |
| 256 | }; |
Steven Moreland | d9a7400 | 2022-06-02 00:03:18 +0000 | [diff] [blame] | 257 | BBinder::RpcServerLink::~RpcServerLink() {} |
Yifan Hong | 8b89085 | 2021-06-10 13:44:09 -0700 | [diff] [blame] | 258 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 259 | class BBinder::Extras |
| 260 | { |
| 261 | public: |
Steven Moreland | f021200 | 2018-12-26 13:59:23 -0800 | [diff] [blame] | 262 | // unlocked objects |
Steven Moreland | b8ad08d | 2019-08-09 14:42:56 -0700 | [diff] [blame] | 263 | sp<IBinder> mExtension; |
Andrei Homescu | f55d688 | 2022-04-30 00:50:20 +0000 | [diff] [blame] | 264 | #ifdef __linux__ |
Steven Moreland | bf1915b | 2020-07-16 22:43:02 +0000 | [diff] [blame] | 265 | int mPolicy = SCHED_NORMAL; |
| 266 | int mPriority = 0; |
Andrei Homescu | f55d688 | 2022-04-30 00:50:20 +0000 | [diff] [blame] | 267 | #endif |
| 268 | bool mRequestingSid = false; |
| 269 | bool mInheritRt = false; |
Steven Moreland | f021200 | 2018-12-26 13:59:23 -0800 | [diff] [blame] | 270 | |
| 271 | // for below objects |
Tomasz Wasilczyk | 66ee192 | 2023-10-26 14:53:49 -0700 | [diff] [blame] | 272 | RpcMutex mLock; |
Yifan Hong | 8b89085 | 2021-06-10 13:44:09 -0700 | [diff] [blame] | 273 | std::set<sp<RpcServerLink>> mRpcServerLinks; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 274 | BpBinder::ObjectManager mObjects; |
Sahil Somani | 2522b07 | 2022-08-08 14:07:47 -0700 | [diff] [blame] | 275 | |
| 276 | android::base::unique_fd mRecordingFd; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 277 | }; |
| 278 | |
| 279 | // --------------------------------------------------------------------------- |
| 280 | |
Sahil Somani | 2522b07 | 2022-08-08 14:07:47 -0700 | [diff] [blame] | 281 | BBinder::BBinder() : mExtras(nullptr), mStability(0), mParceled(false), mRecordingOn(false) {} |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 282 | |
| 283 | bool BBinder::isBinderAlive() const |
| 284 | { |
| 285 | return true; |
| 286 | } |
| 287 | |
| 288 | status_t BBinder::pingBinder() |
| 289 | { |
| 290 | return NO_ERROR; |
| 291 | } |
| 292 | |
Sahil Somani | 2522b07 | 2022-08-08 14:07:47 -0700 | [diff] [blame] | 293 | status_t BBinder::startRecordingTransactions(const Parcel& data) { |
| 294 | if (!kEnableRecording) { |
| 295 | ALOGW("Binder recording disallowed because recording is not enabled"); |
| 296 | return INVALID_OPERATION; |
| 297 | } |
| 298 | if (!kEnableKernelIpc) { |
| 299 | ALOGW("Binder recording disallowed because kernel binder is not enabled"); |
| 300 | return INVALID_OPERATION; |
| 301 | } |
| 302 | uid_t uid = IPCThreadState::self()->getCallingUid(); |
Tomasz Wasilczyk | b8ebcca | 2023-10-09 19:54:20 +0000 | [diff] [blame] | 303 | if (uid != kUidRoot) { |
Sahil Somani | 2522b07 | 2022-08-08 14:07:47 -0700 | [diff] [blame] | 304 | ALOGE("Binder recording not allowed because client %" PRIu32 " is not root", uid); |
| 305 | return PERMISSION_DENIED; |
| 306 | } |
| 307 | Extras* e = getOrCreateExtras(); |
Tomasz Wasilczyk | 66ee192 | 2023-10-26 14:53:49 -0700 | [diff] [blame] | 308 | RpcMutexUniqueLock lock(e->mLock); |
Sahil Somani | 2522b07 | 2022-08-08 14:07:47 -0700 | [diff] [blame] | 309 | if (mRecordingOn) { |
Tomasz Wasilczyk | e3de880 | 2023-11-01 11:05:27 -0700 | [diff] [blame^] | 310 | ALOGI("Could not start Binder recording. Another is already in progress."); |
Sahil Somani | 2522b07 | 2022-08-08 14:07:47 -0700 | [diff] [blame] | 311 | return INVALID_OPERATION; |
| 312 | } else { |
| 313 | status_t readStatus = data.readUniqueFileDescriptor(&(e->mRecordingFd)); |
| 314 | if (readStatus != OK) { |
| 315 | return readStatus; |
| 316 | } |
| 317 | mRecordingOn = true; |
Tomasz Wasilczyk | e3de880 | 2023-11-01 11:05:27 -0700 | [diff] [blame^] | 318 | ALOGI("Started Binder recording."); |
Sahil Somani | 2522b07 | 2022-08-08 14:07:47 -0700 | [diff] [blame] | 319 | return NO_ERROR; |
| 320 | } |
| 321 | } |
| 322 | |
| 323 | status_t BBinder::stopRecordingTransactions() { |
| 324 | if (!kEnableRecording) { |
| 325 | ALOGW("Binder recording disallowed because recording is not enabled"); |
| 326 | return INVALID_OPERATION; |
| 327 | } |
| 328 | if (!kEnableKernelIpc) { |
| 329 | ALOGW("Binder recording disallowed because kernel binder is not enabled"); |
| 330 | return INVALID_OPERATION; |
| 331 | } |
| 332 | uid_t uid = IPCThreadState::self()->getCallingUid(); |
Tomasz Wasilczyk | b8ebcca | 2023-10-09 19:54:20 +0000 | [diff] [blame] | 333 | if (uid != kUidRoot) { |
Sahil Somani | 2522b07 | 2022-08-08 14:07:47 -0700 | [diff] [blame] | 334 | ALOGE("Binder recording not allowed because client %" PRIu32 " is not root", uid); |
| 335 | return PERMISSION_DENIED; |
| 336 | } |
| 337 | Extras* e = getOrCreateExtras(); |
Tomasz Wasilczyk | 66ee192 | 2023-10-26 14:53:49 -0700 | [diff] [blame] | 338 | RpcMutexUniqueLock lock(e->mLock); |
Sahil Somani | 2522b07 | 2022-08-08 14:07:47 -0700 | [diff] [blame] | 339 | if (mRecordingOn) { |
| 340 | e->mRecordingFd.reset(); |
| 341 | mRecordingOn = false; |
Tomasz Wasilczyk | e3de880 | 2023-11-01 11:05:27 -0700 | [diff] [blame^] | 342 | ALOGI("Stopped Binder recording."); |
Sahil Somani | 2522b07 | 2022-08-08 14:07:47 -0700 | [diff] [blame] | 343 | return NO_ERROR; |
| 344 | } else { |
Tomasz Wasilczyk | e3de880 | 2023-11-01 11:05:27 -0700 | [diff] [blame^] | 345 | ALOGI("Could not stop Binder recording. One is not in progress."); |
Sahil Somani | 2522b07 | 2022-08-08 14:07:47 -0700 | [diff] [blame] | 346 | return INVALID_OPERATION; |
| 347 | } |
| 348 | } |
| 349 | |
Mathias Agopian | 83c0446 | 2009-05-22 19:00:22 -0700 | [diff] [blame] | 350 | const String16& BBinder::getInterfaceDescriptor() const |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 351 | { |
Steven Moreland | 1ef0eb6 | 2022-08-18 20:09:30 +0000 | [diff] [blame] | 352 | static StaticString16 sBBinder(u"BBinder"); |
| 353 | ALOGW("Reached BBinder::getInterfaceDescriptor (this=%p). Override?", this); |
| 354 | return sBBinder; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 355 | } |
| 356 | |
Jiyong Park | b86c866 | 2018-10-29 23:01:57 +0900 | [diff] [blame] | 357 | // NOLINTNEXTLINE(google-default-arguments) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 358 | status_t BBinder::transact( |
| 359 | uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) |
| 360 | { |
| 361 | data.setDataPosition(0); |
| 362 | |
Steven Moreland | f183fdd | 2020-10-27 00:12:12 +0000 | [diff] [blame] | 363 | if (reply != nullptr && (flags & FLAG_CLEAR_BUF)) { |
| 364 | reply->markSensitive(); |
| 365 | } |
| 366 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 367 | status_t err = NO_ERROR; |
| 368 | switch (code) { |
| 369 | case PING_TRANSACTION: |
Steven Moreland | 6e69d65 | 2019-07-10 14:17:55 -0700 | [diff] [blame] | 370 | err = pingBinder(); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 371 | break; |
Sahil Somani | 2522b07 | 2022-08-08 14:07:47 -0700 | [diff] [blame] | 372 | case START_RECORDING_TRANSACTION: |
| 373 | err = startRecordingTransactions(data); |
| 374 | break; |
| 375 | case STOP_RECORDING_TRANSACTION: |
| 376 | err = stopRecordingTransactions(); |
| 377 | break; |
Steven Moreland | b8ad08d | 2019-08-09 14:42:56 -0700 | [diff] [blame] | 378 | case EXTENSION_TRANSACTION: |
Tomasz Wasilczyk | e3de880 | 2023-11-01 11:05:27 -0700 | [diff] [blame^] | 379 | LOG_ALWAYS_FATAL_IF(reply == nullptr, "reply == nullptr"); |
Steven Moreland | b8ad08d | 2019-08-09 14:42:56 -0700 | [diff] [blame] | 380 | err = reply->writeStrongBinder(getExtension()); |
| 381 | break; |
Steven Moreland | 86080b8 | 2019-09-23 15:41:18 -0700 | [diff] [blame] | 382 | case DEBUG_PID_TRANSACTION: |
Tomasz Wasilczyk | e3de880 | 2023-11-01 11:05:27 -0700 | [diff] [blame^] | 383 | LOG_ALWAYS_FATAL_IF(reply == nullptr, "reply == nullptr"); |
Steven Moreland | 86080b8 | 2019-09-23 15:41:18 -0700 | [diff] [blame] | 384 | err = reply->writeInt32(getDebugPid()); |
| 385 | break; |
Yifan Hong | 84bedeb | 2021-04-21 21:37:17 -0700 | [diff] [blame] | 386 | case SET_RPC_CLIENT_TRANSACTION: { |
| 387 | err = setRpcClientDebug(data); |
| 388 | break; |
| 389 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 390 | default: |
| 391 | err = onTransact(code, data, reply, flags); |
| 392 | break; |
| 393 | } |
| 394 | |
Steven Moreland | a86a356 | 2019-08-01 23:28:34 +0000 | [diff] [blame] | 395 | // In case this is being transacted on in the same process. |
Yi Kong | fdd8da9 | 2018-06-07 17:52:27 -0700 | [diff] [blame] | 396 | if (reply != nullptr) { |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 397 | reply->setDataPosition(0); |
Martijn Coenen | 1cad19c | 2021-10-04 09:19:01 +0200 | [diff] [blame] | 398 | if (reply->dataSize() > LOG_REPLIES_OVER_SIZE) { |
| 399 | ALOGW("Large reply transaction of %zu bytes, interface descriptor %s, code %d", |
| 400 | reply->dataSize(), String8(getInterfaceDescriptor()).c_str(), code); |
| 401 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 402 | } |
| 403 | |
Tomasz Wasilczyk | 32746ae | 2023-10-09 19:57:58 +0000 | [diff] [blame] | 404 | if (kEnableKernelIpc && mRecordingOn && code != START_RECORDING_TRANSACTION) [[unlikely]] { |
Sahil Somani | 2522b07 | 2022-08-08 14:07:47 -0700 | [diff] [blame] | 405 | Extras* e = mExtras.load(std::memory_order_acquire); |
Tomasz Wasilczyk | 66ee192 | 2023-10-26 14:53:49 -0700 | [diff] [blame] | 406 | RpcMutexUniqueLock lock(e->mLock); |
Sahil Somani | 2522b07 | 2022-08-08 14:07:47 -0700 | [diff] [blame] | 407 | if (mRecordingOn) { |
| 408 | Parcel emptyReply; |
Fabián Cañas | 68dbcfe | 2023-01-06 15:37:39 -0500 | [diff] [blame] | 409 | timespec ts; |
| 410 | timespec_get(&ts, TIME_UTC); |
Fabián Cañas | 6c0c963 | 2023-01-13 15:21:16 -0500 | [diff] [blame] | 411 | auto transaction = android::binder::debug::RecordedTransaction:: |
| 412 | fromDetails(getInterfaceDescriptor(), code, flags, ts, data, |
| 413 | reply ? *reply : emptyReply, err); |
Sahil Somani | 2522b07 | 2022-08-08 14:07:47 -0700 | [diff] [blame] | 414 | if (transaction) { |
| 415 | if (status_t err = transaction->dumpToFile(e->mRecordingFd); err != NO_ERROR) { |
Tomasz Wasilczyk | e3de880 | 2023-11-01 11:05:27 -0700 | [diff] [blame^] | 416 | ALOGI("Failed to dump RecordedTransaction to file with error %d", err); |
Sahil Somani | 2522b07 | 2022-08-08 14:07:47 -0700 | [diff] [blame] | 417 | } |
| 418 | } else { |
Tomasz Wasilczyk | e3de880 | 2023-11-01 11:05:27 -0700 | [diff] [blame^] | 419 | ALOGI("Failed to create RecordedTransaction object."); |
Sahil Somani | 2522b07 | 2022-08-08 14:07:47 -0700 | [diff] [blame] | 420 | } |
| 421 | } |
| 422 | } |
| 423 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 424 | return err; |
| 425 | } |
| 426 | |
Jiyong Park | b86c866 | 2018-10-29 23:01:57 +0900 | [diff] [blame] | 427 | // NOLINTNEXTLINE(google-default-arguments) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 428 | status_t BBinder::linkToDeath( |
Colin Cross | 6f4f3ab | 2014-02-05 17:42:44 -0800 | [diff] [blame] | 429 | const sp<DeathRecipient>& /*recipient*/, void* /*cookie*/, |
| 430 | uint32_t /*flags*/) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 431 | { |
| 432 | return INVALID_OPERATION; |
| 433 | } |
| 434 | |
Jiyong Park | b86c866 | 2018-10-29 23:01:57 +0900 | [diff] [blame] | 435 | // NOLINTNEXTLINE(google-default-arguments) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 436 | status_t BBinder::unlinkToDeath( |
Colin Cross | 6f4f3ab | 2014-02-05 17:42:44 -0800 | [diff] [blame] | 437 | const wp<DeathRecipient>& /*recipient*/, void* /*cookie*/, |
| 438 | uint32_t /*flags*/, wp<DeathRecipient>* /*outRecipient*/) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 439 | { |
| 440 | return INVALID_OPERATION; |
| 441 | } |
| 442 | |
Dianne Hackborn | 23eb1e2 | 2015-10-07 17:35:27 -0700 | [diff] [blame] | 443 | status_t BBinder::dump(int /*fd*/, const Vector<String16>& /*args*/) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 444 | { |
| 445 | return NO_ERROR; |
| 446 | } |
| 447 | |
Steven Moreland | 63a2d51 | 2021-06-25 01:10:15 +0000 | [diff] [blame] | 448 | void* BBinder::attachObject(const void* objectID, void* object, void* cleanupCookie, |
| 449 | object_cleanup_func func) { |
Steven Moreland | f021200 | 2018-12-26 13:59:23 -0800 | [diff] [blame] | 450 | Extras* e = getOrCreateExtras(); |
Steven Moreland | 5ce7ca5 | 2021-06-25 21:59:50 +0000 | [diff] [blame] | 451 | LOG_ALWAYS_FATAL_IF(!e, "no memory"); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 452 | |
Tomasz Wasilczyk | 66ee192 | 2023-10-26 14:53:49 -0700 | [diff] [blame] | 453 | RpcMutexUniqueLock _l(e->mLock); |
Steven Moreland | 63a2d51 | 2021-06-25 01:10:15 +0000 | [diff] [blame] | 454 | return e->mObjects.attach(objectID, object, cleanupCookie, func); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 455 | } |
| 456 | |
| 457 | void* BBinder::findObject(const void* objectID) const |
| 458 | { |
Bailey Forrest | 6913c46 | 2015-08-18 17:15:10 -0700 | [diff] [blame] | 459 | Extras* e = mExtras.load(std::memory_order_acquire); |
Yi Kong | fdd8da9 | 2018-06-07 17:52:27 -0700 | [diff] [blame] | 460 | if (!e) return nullptr; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 461 | |
Tomasz Wasilczyk | 66ee192 | 2023-10-26 14:53:49 -0700 | [diff] [blame] | 462 | RpcMutexUniqueLock _l(e->mLock); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 463 | return e->mObjects.find(objectID); |
| 464 | } |
| 465 | |
Steven Moreland | 63a2d51 | 2021-06-25 01:10:15 +0000 | [diff] [blame] | 466 | void* BBinder::detachObject(const void* objectID) { |
Bailey Forrest | 6913c46 | 2015-08-18 17:15:10 -0700 | [diff] [blame] | 467 | Extras* e = mExtras.load(std::memory_order_acquire); |
Steven Moreland | 63a2d51 | 2021-06-25 01:10:15 +0000 | [diff] [blame] | 468 | if (!e) return nullptr; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 469 | |
Tomasz Wasilczyk | 66ee192 | 2023-10-26 14:53:49 -0700 | [diff] [blame] | 470 | RpcMutexUniqueLock _l(e->mLock); |
Steven Moreland | 63a2d51 | 2021-06-25 01:10:15 +0000 | [diff] [blame] | 471 | return e->mObjects.detach(objectID); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 472 | } |
| 473 | |
Steven Moreland | 9e759e8 | 2021-06-25 21:30:23 +0000 | [diff] [blame] | 474 | void BBinder::withLock(const std::function<void()>& doWithLock) { |
| 475 | Extras* e = getOrCreateExtras(); |
| 476 | LOG_ALWAYS_FATAL_IF(!e, "no memory"); |
| 477 | |
Tomasz Wasilczyk | 66ee192 | 2023-10-26 14:53:49 -0700 | [diff] [blame] | 478 | RpcMutexUniqueLock _l(e->mLock); |
Steven Moreland | 9e759e8 | 2021-06-25 21:30:23 +0000 | [diff] [blame] | 479 | doWithLock(); |
| 480 | } |
| 481 | |
Devin Moore | 3faaa00 | 2022-07-27 15:54:06 +0000 | [diff] [blame] | 482 | sp<IBinder> BBinder::lookupOrCreateWeak(const void* objectID, object_make_func make, |
| 483 | const void* makeArgs) { |
| 484 | Extras* e = getOrCreateExtras(); |
| 485 | LOG_ALWAYS_FATAL_IF(!e, "no memory"); |
Tomasz Wasilczyk | 66ee192 | 2023-10-26 14:53:49 -0700 | [diff] [blame] | 486 | RpcMutexUniqueLock _l(e->mLock); |
Devin Moore | 3faaa00 | 2022-07-27 15:54:06 +0000 | [diff] [blame] | 487 | return e->mObjects.lookupOrCreateWeak(objectID, make, makeArgs); |
| 488 | } |
| 489 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 490 | BBinder* BBinder::localBinder() |
| 491 | { |
| 492 | return this; |
| 493 | } |
| 494 | |
Steven Moreland | f021200 | 2018-12-26 13:59:23 -0800 | [diff] [blame] | 495 | bool BBinder::isRequestingSid() |
| 496 | { |
| 497 | Extras* e = mExtras.load(std::memory_order_acquire); |
| 498 | |
| 499 | return e && e->mRequestingSid; |
| 500 | } |
| 501 | |
| 502 | void BBinder::setRequestingSid(bool requestingSid) |
| 503 | { |
Steven Moreland | d37e6dd | 2021-06-16 22:07:51 +0000 | [diff] [blame] | 504 | LOG_ALWAYS_FATAL_IF(mParceled, |
| 505 | "setRequestingSid() should not be called after a binder object " |
| 506 | "is parceled/sent to another process"); |
Kalesh Singh | d67c8e8 | 2020-12-29 15:46:25 -0500 | [diff] [blame] | 507 | |
Steven Moreland | f021200 | 2018-12-26 13:59:23 -0800 | [diff] [blame] | 508 | Extras* e = mExtras.load(std::memory_order_acquire); |
| 509 | |
| 510 | if (!e) { |
| 511 | // default is false. Most things don't need sids, so avoiding allocations when possible. |
| 512 | if (!requestingSid) { |
| 513 | return; |
| 514 | } |
| 515 | |
| 516 | e = getOrCreateExtras(); |
| 517 | if (!e) return; // out of memory |
| 518 | } |
| 519 | |
Steven Moreland | 3668be6 | 2019-02-08 17:56:55 -0800 | [diff] [blame] | 520 | e->mRequestingSid = requestingSid; |
Steven Moreland | f021200 | 2018-12-26 13:59:23 -0800 | [diff] [blame] | 521 | } |
| 522 | |
Steven Moreland | b8ad08d | 2019-08-09 14:42:56 -0700 | [diff] [blame] | 523 | sp<IBinder> BBinder::getExtension() { |
| 524 | Extras* e = mExtras.load(std::memory_order_acquire); |
| 525 | if (e == nullptr) return nullptr; |
| 526 | return e->mExtension; |
| 527 | } |
| 528 | |
Andrei Homescu | f55d688 | 2022-04-30 00:50:20 +0000 | [diff] [blame] | 529 | #ifdef __linux__ |
Steven Moreland | bf1915b | 2020-07-16 22:43:02 +0000 | [diff] [blame] | 530 | void BBinder::setMinSchedulerPolicy(int policy, int priority) { |
Steven Moreland | d37e6dd | 2021-06-16 22:07:51 +0000 | [diff] [blame] | 531 | LOG_ALWAYS_FATAL_IF(mParceled, |
| 532 | "setMinSchedulerPolicy() should not be called after a binder object " |
| 533 | "is parceled/sent to another process"); |
Kalesh Singh | d67c8e8 | 2020-12-29 15:46:25 -0500 | [diff] [blame] | 534 | |
Steven Moreland | bf1915b | 2020-07-16 22:43:02 +0000 | [diff] [blame] | 535 | switch (policy) { |
| 536 | case SCHED_NORMAL: |
| 537 | LOG_ALWAYS_FATAL_IF(priority < -20 || priority > 19, "Invalid priority for SCHED_NORMAL: %d", priority); |
| 538 | break; |
| 539 | case SCHED_RR: |
| 540 | case SCHED_FIFO: |
| 541 | LOG_ALWAYS_FATAL_IF(priority < 1 || priority > 99, "Invalid priority for sched %d: %d", policy, priority); |
| 542 | break; |
| 543 | default: |
| 544 | LOG_ALWAYS_FATAL("Unrecognized scheduling policy: %d", policy); |
| 545 | } |
| 546 | |
| 547 | Extras* e = mExtras.load(std::memory_order_acquire); |
| 548 | |
| 549 | if (e == nullptr) { |
| 550 | // Avoid allocations if called with default. |
| 551 | if (policy == SCHED_NORMAL && priority == 0) { |
| 552 | return; |
| 553 | } |
| 554 | |
| 555 | e = getOrCreateExtras(); |
| 556 | if (!e) return; // out of memory |
| 557 | } |
| 558 | |
| 559 | e->mPolicy = policy; |
| 560 | e->mPriority = priority; |
| 561 | } |
| 562 | |
| 563 | int BBinder::getMinSchedulerPolicy() { |
| 564 | Extras* e = mExtras.load(std::memory_order_acquire); |
| 565 | if (e == nullptr) return SCHED_NORMAL; |
| 566 | return e->mPolicy; |
| 567 | } |
| 568 | |
| 569 | int BBinder::getMinSchedulerPriority() { |
| 570 | Extras* e = mExtras.load(std::memory_order_acquire); |
| 571 | if (e == nullptr) return 0; |
| 572 | return e->mPriority; |
| 573 | } |
Andrei Homescu | f55d688 | 2022-04-30 00:50:20 +0000 | [diff] [blame] | 574 | #endif // __linux__ |
Steven Moreland | bf1915b | 2020-07-16 22:43:02 +0000 | [diff] [blame] | 575 | |
Steven Moreland | cf03cf1 | 2020-12-04 02:58:40 +0000 | [diff] [blame] | 576 | bool BBinder::isInheritRt() { |
| 577 | Extras* e = mExtras.load(std::memory_order_acquire); |
| 578 | |
| 579 | return e && e->mInheritRt; |
| 580 | } |
| 581 | |
| 582 | void BBinder::setInheritRt(bool inheritRt) { |
Steven Moreland | d37e6dd | 2021-06-16 22:07:51 +0000 | [diff] [blame] | 583 | LOG_ALWAYS_FATAL_IF(mParceled, |
| 584 | "setInheritRt() should not be called after a binder object " |
| 585 | "is parceled/sent to another process"); |
Kalesh Singh | d67c8e8 | 2020-12-29 15:46:25 -0500 | [diff] [blame] | 586 | |
Steven Moreland | cf03cf1 | 2020-12-04 02:58:40 +0000 | [diff] [blame] | 587 | Extras* e = mExtras.load(std::memory_order_acquire); |
| 588 | |
| 589 | if (!e) { |
| 590 | if (!inheritRt) { |
| 591 | return; |
| 592 | } |
| 593 | |
| 594 | e = getOrCreateExtras(); |
| 595 | if (!e) return; // out of memory |
| 596 | } |
| 597 | |
| 598 | e->mInheritRt = inheritRt; |
| 599 | } |
| 600 | |
Steven Moreland | 86080b8 | 2019-09-23 15:41:18 -0700 | [diff] [blame] | 601 | pid_t BBinder::getDebugPid() { |
Andrei Homescu | f55d688 | 2022-04-30 00:50:20 +0000 | [diff] [blame] | 602 | #ifdef __linux__ |
Steven Moreland | 86080b8 | 2019-09-23 15:41:18 -0700 | [diff] [blame] | 603 | return getpid(); |
Andrei Homescu | f55d688 | 2022-04-30 00:50:20 +0000 | [diff] [blame] | 604 | #else |
| 605 | // TODO: handle other OSes |
| 606 | return 0; |
| 607 | #endif // __linux__ |
Steven Moreland | 86080b8 | 2019-09-23 15:41:18 -0700 | [diff] [blame] | 608 | } |
| 609 | |
Steven Moreland | b8ad08d | 2019-08-09 14:42:56 -0700 | [diff] [blame] | 610 | void BBinder::setExtension(const sp<IBinder>& extension) { |
Steven Moreland | d37e6dd | 2021-06-16 22:07:51 +0000 | [diff] [blame] | 611 | LOG_ALWAYS_FATAL_IF(mParceled, |
| 612 | "setExtension() should not be called after a binder object " |
| 613 | "is parceled/sent to another process"); |
Kalesh Singh | d67c8e8 | 2020-12-29 15:46:25 -0500 | [diff] [blame] | 614 | |
Steven Moreland | b8ad08d | 2019-08-09 14:42:56 -0700 | [diff] [blame] | 615 | Extras* e = getOrCreateExtras(); |
| 616 | e->mExtension = extension; |
| 617 | } |
| 618 | |
Kalesh Singh | d67c8e8 | 2020-12-29 15:46:25 -0500 | [diff] [blame] | 619 | bool BBinder::wasParceled() { |
| 620 | return mParceled; |
| 621 | } |
| 622 | |
| 623 | void BBinder::setParceled() { |
| 624 | mParceled = true; |
| 625 | } |
| 626 | |
Yifan Hong | 84bedeb | 2021-04-21 21:37:17 -0700 | [diff] [blame] | 627 | status_t BBinder::setRpcClientDebug(const Parcel& data) { |
Steven Moreland | d9a7400 | 2022-06-02 00:03:18 +0000 | [diff] [blame] | 628 | if (!kEnableRpcDevServers) { |
Yifan Hong | 84bedeb | 2021-04-21 21:37:17 -0700 | [diff] [blame] | 629 | ALOGW("%s: disallowed because RPC is not enabled", __PRETTY_FUNCTION__); |
| 630 | return INVALID_OPERATION; |
| 631 | } |
Steven Moreland | 3215028 | 2021-11-12 22:54:53 +0000 | [diff] [blame] | 632 | if (!kEnableKernelIpc) { |
| 633 | ALOGW("setRpcClientDebug disallowed because kernel binder is not enabled"); |
| 634 | return INVALID_OPERATION; |
| 635 | } |
Yifan Hong | 84bedeb | 2021-04-21 21:37:17 -0700 | [diff] [blame] | 636 | uid_t uid = IPCThreadState::self()->getCallingUid(); |
Tomasz Wasilczyk | b8ebcca | 2023-10-09 19:54:20 +0000 | [diff] [blame] | 637 | if (uid != kUidRoot) { |
Yifan Hong | 84bedeb | 2021-04-21 21:37:17 -0700 | [diff] [blame] | 638 | ALOGE("%s: not allowed because client %" PRIu32 " is not root", __PRETTY_FUNCTION__, uid); |
| 639 | return PERMISSION_DENIED; |
| 640 | } |
| 641 | status_t status; |
| 642 | bool hasSocketFd; |
| 643 | android::base::unique_fd clientFd; |
Yifan Hong | 84bedeb | 2021-04-21 21:37:17 -0700 | [diff] [blame] | 644 | |
| 645 | if (status = data.readBool(&hasSocketFd); status != OK) return status; |
| 646 | if (hasSocketFd) { |
| 647 | if (status = data.readUniqueFileDescriptor(&clientFd); status != OK) return status; |
| 648 | } |
Yifan Hong | 02530ec | 2021-06-10 13:38:38 -0700 | [diff] [blame] | 649 | sp<IBinder> keepAliveBinder; |
| 650 | if (status = data.readNullableStrongBinder(&keepAliveBinder); status != OK) return status; |
Yifan Hong | 84bedeb | 2021-04-21 21:37:17 -0700 | [diff] [blame] | 651 | |
Yifan Hong | 02530ec | 2021-06-10 13:38:38 -0700 | [diff] [blame] | 652 | return setRpcClientDebug(std::move(clientFd), keepAliveBinder); |
Yifan Hong | 84bedeb | 2021-04-21 21:37:17 -0700 | [diff] [blame] | 653 | } |
| 654 | |
Yifan Hong | 02530ec | 2021-06-10 13:38:38 -0700 | [diff] [blame] | 655 | status_t BBinder::setRpcClientDebug(android::base::unique_fd socketFd, |
| 656 | const sp<IBinder>& keepAliveBinder) { |
Steven Moreland | d9a7400 | 2022-06-02 00:03:18 +0000 | [diff] [blame] | 657 | if (!kEnableRpcDevServers) { |
Yifan Hong | 84bedeb | 2021-04-21 21:37:17 -0700 | [diff] [blame] | 658 | ALOGW("%s: disallowed because RPC is not enabled", __PRETTY_FUNCTION__); |
| 659 | return INVALID_OPERATION; |
| 660 | } |
Steven Moreland | 3215028 | 2021-11-12 22:54:53 +0000 | [diff] [blame] | 661 | if (!kEnableKernelIpc) { |
| 662 | ALOGW("setRpcClientDebug disallowed because kernel binder is not enabled"); |
| 663 | return INVALID_OPERATION; |
| 664 | } |
Yifan Hong | 84bedeb | 2021-04-21 21:37:17 -0700 | [diff] [blame] | 665 | |
| 666 | const int socketFdForPrint = socketFd.get(); |
Yifan Hong | 3482323 | 2021-06-07 17:23:00 -0700 | [diff] [blame] | 667 | LOG_RPC_DETAIL("%s(fd=%d)", __PRETTY_FUNCTION__, socketFdForPrint); |
Yifan Hong | 84bedeb | 2021-04-21 21:37:17 -0700 | [diff] [blame] | 668 | |
| 669 | if (!socketFd.ok()) { |
| 670 | ALOGE("%s: No socket FD provided.", __PRETTY_FUNCTION__); |
| 671 | return BAD_VALUE; |
| 672 | } |
Yifan Hong | 84bedeb | 2021-04-21 21:37:17 -0700 | [diff] [blame] | 673 | |
Yifan Hong | 02530ec | 2021-06-10 13:38:38 -0700 | [diff] [blame] | 674 | if (keepAliveBinder == nullptr) { |
| 675 | ALOGE("%s: No keepAliveBinder provided.", __PRETTY_FUNCTION__); |
| 676 | return UNEXPECTED_NULL; |
| 677 | } |
| 678 | |
Elie Kheirallah | 47431c1 | 2022-04-21 23:46:17 +0000 | [diff] [blame] | 679 | size_t binderThreadPoolMaxCount = ProcessState::self()->getThreadPoolMaxTotalThreadCount(); |
Yifan Hong | 84bedeb | 2021-04-21 21:37:17 -0700 | [diff] [blame] | 680 | if (binderThreadPoolMaxCount <= 1) { |
| 681 | ALOGE("%s: ProcessState thread pool max count is %zu. RPC is disabled for this service " |
| 682 | "because RPC requires the service to support multithreading.", |
| 683 | __PRETTY_FUNCTION__, binderThreadPoolMaxCount); |
| 684 | return INVALID_OPERATION; |
| 685 | } |
| 686 | |
Yifan Hong | 8b89085 | 2021-06-10 13:44:09 -0700 | [diff] [blame] | 687 | // Weak ref to avoid circular dependency: |
| 688 | // BBinder -> RpcServerLink ----> RpcServer -X-> BBinder |
| 689 | // `-X-> BBinder |
| 690 | auto weakThis = wp<BBinder>::fromExisting(this); |
| 691 | |
Yifan Hong | 84bedeb | 2021-04-21 21:37:17 -0700 | [diff] [blame] | 692 | Extras* e = getOrCreateExtras(); |
Tomasz Wasilczyk | 66ee192 | 2023-10-26 14:53:49 -0700 | [diff] [blame] | 693 | RpcMutexUniqueLock _l(e->mLock); |
Yifan Hong | 8b89085 | 2021-06-10 13:44:09 -0700 | [diff] [blame] | 694 | auto rpcServer = RpcServer::make(); |
| 695 | LOG_ALWAYS_FATAL_IF(rpcServer == nullptr, "RpcServer::make returns null"); |
Yifan Hong | 8b89085 | 2021-06-10 13:44:09 -0700 | [diff] [blame] | 696 | auto link = sp<RpcServerLink>::make(rpcServer, keepAliveBinder, weakThis); |
| 697 | if (auto status = keepAliveBinder->linkToDeath(link, nullptr, 0); status != OK) { |
| 698 | ALOGE("%s: keepAliveBinder->linkToDeath returns %s", __PRETTY_FUNCTION__, |
| 699 | statusToString(status).c_str()); |
| 700 | return status; |
Yifan Hong | 84bedeb | 2021-04-21 21:37:17 -0700 | [diff] [blame] | 701 | } |
Yifan Hong | 8b89085 | 2021-06-10 13:44:09 -0700 | [diff] [blame] | 702 | rpcServer->setRootObjectWeak(weakThis); |
Steven Moreland | 2372f9d | 2021-08-05 15:42:01 -0700 | [diff] [blame] | 703 | if (auto status = rpcServer->setupExternalServer(std::move(socketFd)); status != OK) { |
| 704 | return status; |
| 705 | } |
Yifan Hong | 8b89085 | 2021-06-10 13:44:09 -0700 | [diff] [blame] | 706 | rpcServer->setMaxThreads(binderThreadPoolMaxCount); |
Tomasz Wasilczyk | e3de880 | 2023-11-01 11:05:27 -0700 | [diff] [blame^] | 707 | ALOGI("RpcBinder: Started Binder debug on %s", String8(getInterfaceDescriptor()).c_str()); |
Yifan Hong | 8b89085 | 2021-06-10 13:44:09 -0700 | [diff] [blame] | 708 | rpcServer->start(); |
| 709 | e->mRpcServerLinks.emplace(link); |
Yifan Hong | 3482323 | 2021-06-07 17:23:00 -0700 | [diff] [blame] | 710 | LOG_RPC_DETAIL("%s(fd=%d) successful", __PRETTY_FUNCTION__, socketFdForPrint); |
Yifan Hong | 84bedeb | 2021-04-21 21:37:17 -0700 | [diff] [blame] | 711 | return OK; |
| 712 | } |
| 713 | |
Yifan Hong | 8b89085 | 2021-06-10 13:44:09 -0700 | [diff] [blame] | 714 | void BBinder::removeRpcServerLink(const sp<RpcServerLink>& link) { |
| 715 | Extras* e = mExtras.load(std::memory_order_acquire); |
| 716 | if (!e) return; |
Tomasz Wasilczyk | 66ee192 | 2023-10-26 14:53:49 -0700 | [diff] [blame] | 717 | RpcMutexUniqueLock _l(e->mLock); |
Yifan Hong | 8b89085 | 2021-06-10 13:44:09 -0700 | [diff] [blame] | 718 | (void)e->mRpcServerLinks.erase(link); |
| 719 | } |
| 720 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 721 | BBinder::~BBinder() |
| 722 | { |
Steven Moreland | 1bd2bc7 | 2022-07-29 20:29:40 +0000 | [diff] [blame] | 723 | if (!wasParceled()) { |
| 724 | if (getExtension()) { |
Tomasz Wasilczyk | e3de880 | 2023-11-01 11:05:27 -0700 | [diff] [blame^] | 725 | ALOGW("Binder %p destroyed with extension attached before being parceled.", this); |
Steven Moreland | 1bd2bc7 | 2022-07-29 20:29:40 +0000 | [diff] [blame] | 726 | } |
| 727 | if (isRequestingSid()) { |
Tomasz Wasilczyk | e3de880 | 2023-11-01 11:05:27 -0700 | [diff] [blame^] | 728 | ALOGW("Binder %p destroyed when requesting SID before being parceled.", this); |
Steven Moreland | 1bd2bc7 | 2022-07-29 20:29:40 +0000 | [diff] [blame] | 729 | } |
| 730 | if (isInheritRt()) { |
Tomasz Wasilczyk | e3de880 | 2023-11-01 11:05:27 -0700 | [diff] [blame^] | 731 | ALOGW("Binder %p destroyed after setInheritRt before being parceled.", this); |
Steven Moreland | 1bd2bc7 | 2022-07-29 20:29:40 +0000 | [diff] [blame] | 732 | } |
Andrei Homescu | 4052550 | 2022-08-02 01:23:22 +0000 | [diff] [blame] | 733 | #ifdef __linux__ |
Steven Moreland | 1bd2bc7 | 2022-07-29 20:29:40 +0000 | [diff] [blame] | 734 | if (getMinSchedulerPolicy() != SCHED_NORMAL) { |
Tomasz Wasilczyk | e3de880 | 2023-11-01 11:05:27 -0700 | [diff] [blame^] | 735 | ALOGW("Binder %p destroyed after setMinSchedulerPolicy before being parceled.", this); |
Steven Moreland | 1bd2bc7 | 2022-07-29 20:29:40 +0000 | [diff] [blame] | 736 | } |
| 737 | if (getMinSchedulerPriority() != 0) { |
Tomasz Wasilczyk | e3de880 | 2023-11-01 11:05:27 -0700 | [diff] [blame^] | 738 | ALOGW("Binder %p destroyed after setMinSchedulerPolicy before being parceled.", this); |
Steven Moreland | 1bd2bc7 | 2022-07-29 20:29:40 +0000 | [diff] [blame] | 739 | } |
Andrei Homescu | 4052550 | 2022-08-02 01:23:22 +0000 | [diff] [blame] | 740 | #endif // __linux__ |
Steven Moreland | 55a1254 | 2022-03-31 21:53:11 +0000 | [diff] [blame] | 741 | } |
| 742 | |
Bailey Forrest | 6913c46 | 2015-08-18 17:15:10 -0700 | [diff] [blame] | 743 | Extras* e = mExtras.load(std::memory_order_relaxed); |
Hans Boehm | 3effaba | 2014-08-12 22:56:00 +0000 | [diff] [blame] | 744 | if (e) delete e; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 745 | } |
| 746 | |
| 747 | |
Jiyong Park | b86c866 | 2018-10-29 23:01:57 +0900 | [diff] [blame] | 748 | // NOLINTNEXTLINE(google-default-arguments) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 749 | status_t BBinder::onTransact( |
Colin Cross | 6f4f3ab | 2014-02-05 17:42:44 -0800 | [diff] [blame] | 750 | uint32_t code, const Parcel& data, Parcel* reply, uint32_t /*flags*/) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 751 | { |
| 752 | switch (code) { |
| 753 | case INTERFACE_TRANSACTION: |
Tomasz Wasilczyk | e3de880 | 2023-11-01 11:05:27 -0700 | [diff] [blame^] | 754 | LOG_ALWAYS_FATAL_IF(reply == nullptr, "reply == nullptr"); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 755 | reply->writeString16(getInterfaceDescriptor()); |
| 756 | return NO_ERROR; |
| 757 | |
| 758 | case DUMP_TRANSACTION: { |
| 759 | int fd = data.readFileDescriptor(); |
| 760 | int argc = data.readInt32(); |
| 761 | Vector<String16> args; |
| 762 | for (int i = 0; i < argc && data.dataAvail() > 0; i++) { |
| 763 | args.add(data.readString16()); |
| 764 | } |
| 765 | return dump(fd, args); |
| 766 | } |
Dianne Hackborn | 555f89d | 2012-05-08 18:54:22 -0700 | [diff] [blame] | 767 | |
Dianne Hackborn | 23eb1e2 | 2015-10-07 17:35:27 -0700 | [diff] [blame] | 768 | case SHELL_COMMAND_TRANSACTION: { |
| 769 | int in = data.readFileDescriptor(); |
| 770 | int out = data.readFileDescriptor(); |
| 771 | int err = data.readFileDescriptor(); |
| 772 | int argc = data.readInt32(); |
| 773 | Vector<String16> args; |
| 774 | for (int i = 0; i < argc && data.dataAvail() > 0; i++) { |
| 775 | args.add(data.readString16()); |
| 776 | } |
Andrei Homescu | a543a84 | 2022-07-07 22:17:55 +0000 | [diff] [blame] | 777 | sp<IBinder> shellCallbackBinder = data.readStrongBinder(); |
Dianne Hackborn | 23eb1e2 | 2015-10-07 17:35:27 -0700 | [diff] [blame] | 778 | sp<IResultReceiver> resultReceiver = IResultReceiver::asInterface( |
| 779 | data.readStrongBinder()); |
| 780 | |
Dianne Hackborn | f2bf93b | 2015-10-14 15:13:02 -0700 | [diff] [blame] | 781 | // XXX can't add virtuals until binaries are updated. |
Andrei Homescu | a543a84 | 2022-07-07 22:17:55 +0000 | [diff] [blame] | 782 | // sp<IShellCallback> shellCallback = IShellCallback::asInterface( |
| 783 | // shellCallbackBinder); |
| 784 | // return shellCommand(in, out, err, args, resultReceiver); |
Christopher Wiley | 0a9a1c1 | 2016-07-20 08:28:14 -0700 | [diff] [blame] | 785 | (void)in; |
| 786 | (void)out; |
| 787 | (void)err; |
| 788 | |
Yi Kong | fdd8da9 | 2018-06-07 17:52:27 -0700 | [diff] [blame] | 789 | if (resultReceiver != nullptr) { |
Dianne Hackborn | f2bf93b | 2015-10-14 15:13:02 -0700 | [diff] [blame] | 790 | resultReceiver->send(INVALID_OPERATION); |
| 791 | } |
Martijn Coenen | aa6ee99 | 2017-08-17 15:38:08 +0200 | [diff] [blame] | 792 | |
| 793 | return NO_ERROR; |
Dianne Hackborn | 23eb1e2 | 2015-10-07 17:35:27 -0700 | [diff] [blame] | 794 | } |
| 795 | |
Dianne Hackborn | 555f89d | 2012-05-08 18:54:22 -0700 | [diff] [blame] | 796 | case SYSPROPS_TRANSACTION: { |
Tomasz Wasilczyk | 346f104 | 2023-10-09 17:02:46 +0000 | [diff] [blame] | 797 | if (!binder::os::report_sysprop_change()) return INVALID_OPERATION; |
Dianne Hackborn | 555f89d | 2012-05-08 18:54:22 -0700 | [diff] [blame] | 798 | return NO_ERROR; |
| 799 | } |
| 800 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 801 | default: |
| 802 | return UNKNOWN_TRANSACTION; |
| 803 | } |
| 804 | } |
| 805 | |
Steven Moreland | f021200 | 2018-12-26 13:59:23 -0800 | [diff] [blame] | 806 | BBinder::Extras* BBinder::getOrCreateExtras() |
| 807 | { |
| 808 | Extras* e = mExtras.load(std::memory_order_acquire); |
| 809 | |
| 810 | if (!e) { |
| 811 | e = new Extras; |
| 812 | Extras* expected = nullptr; |
| 813 | if (!mExtras.compare_exchange_strong(expected, e, |
| 814 | std::memory_order_release, |
| 815 | std::memory_order_acquire)) { |
| 816 | delete e; |
| 817 | e = expected; // Filled in by CAS |
| 818 | } |
| 819 | if (e == nullptr) return nullptr; // out of memory |
| 820 | } |
| 821 | |
| 822 | return e; |
| 823 | } |
| 824 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 825 | // --------------------------------------------------------------------------- |
| 826 | |
| 827 | enum { |
| 828 | // This is used to transfer ownership of the remote binder from |
| 829 | // the BpRefBase object holding it (when it is constructed), to the |
| 830 | // owner of the BpRefBase object when it first acquires that BpRefBase. |
| 831 | kRemoteAcquired = 0x00000001 |
| 832 | }; |
| 833 | |
| 834 | BpRefBase::BpRefBase(const sp<IBinder>& o) |
Yi Kong | fdd8da9 | 2018-06-07 17:52:27 -0700 | [diff] [blame] | 835 | : mRemote(o.get()), mRefs(nullptr), mState(0) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 836 | { |
| 837 | extendObjectLifetime(OBJECT_LIFETIME_WEAK); |
| 838 | |
| 839 | if (mRemote) { |
| 840 | mRemote->incStrong(this); // Removed on first IncStrong(). |
| 841 | mRefs = mRemote->createWeak(this); // Held for our entire lifetime. |
| 842 | } |
| 843 | } |
| 844 | |
| 845 | BpRefBase::~BpRefBase() |
| 846 | { |
| 847 | if (mRemote) { |
Bailey Forrest | 6913c46 | 2015-08-18 17:15:10 -0700 | [diff] [blame] | 848 | if (!(mState.load(std::memory_order_relaxed)&kRemoteAcquired)) { |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 849 | mRemote->decStrong(this); |
| 850 | } |
| 851 | mRefs->decWeak(this); |
| 852 | } |
| 853 | } |
| 854 | |
| 855 | void BpRefBase::onFirstRef() |
| 856 | { |
Bailey Forrest | 6913c46 | 2015-08-18 17:15:10 -0700 | [diff] [blame] | 857 | mState.fetch_or(kRemoteAcquired, std::memory_order_relaxed); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 858 | } |
| 859 | |
Colin Cross | 6f4f3ab | 2014-02-05 17:42:44 -0800 | [diff] [blame] | 860 | void BpRefBase::onLastStrongRef(const void* /*id*/) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 861 | { |
| 862 | if (mRemote) { |
| 863 | mRemote->decStrong(this); |
| 864 | } |
| 865 | } |
| 866 | |
Colin Cross | 6f4f3ab | 2014-02-05 17:42:44 -0800 | [diff] [blame] | 867 | bool BpRefBase::onIncStrongAttempted(uint32_t /*flags*/, const void* /*id*/) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 868 | { |
| 869 | return mRemote ? mRefs->attemptIncStrong(this) : false; |
| 870 | } |
| 871 | |
| 872 | // --------------------------------------------------------------------------- |
| 873 | |
Steven Moreland | 61ff849 | 2019-09-26 16:05:45 -0700 | [diff] [blame] | 874 | } // namespace android |