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