blob: 481d704f1cebd5a7877b33c6a7b9b4564f8a12b2 [file] [log] [blame]
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001/*
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 Agopianc5b2c0b2009-05-19 19:08:10 -070017#include <binder/Binder.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080018
Bailey Forrest6913c462015-08-18 17:15:10 -070019#include <atomic>
Yifan Hong8b890852021-06-10 13:44:09 -070020#include <set>
Yifan Hong84bedeb2021-04-21 21:37:17 -070021
Jiyong Park5970d0a2022-03-08 16:56:13 +090022#include <android-base/logging.h>
Yifan Hong84bedeb2021-04-21 21:37:17 -070023#include <android-base/unique_fd.h>
Sahil Somani2522b072022-08-08 14:07:47 -070024#include <binder/BinderRecordReplay.h>
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070025#include <binder/BpBinder.h>
26#include <binder/IInterface.h>
Yifan Hong84bedeb2021-04-21 21:37:17 -070027#include <binder/IPCThreadState.h>
Dianne Hackborn23eb1e22015-10-07 17:35:27 -070028#include <binder/IResultReceiver.h>
Dianne Hackborn1941a402016-08-29 12:30:43 -070029#include <binder/IShellCallback.h>
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070030#include <binder/Parcel.h>
Yifan Hong84bedeb2021-04-21 21:37:17 -070031#include <binder/RpcServer.h>
Sahil Somani2522b072022-08-08 14:07:47 -070032#include <cutils/compiler.h>
Yifan Hong84bedeb2021-04-21 21:37:17 -070033#include <private/android_filesystem_config.h>
Sahil Somani2522b072022-08-08 14:07:47 -070034#include <pthread.h>
Yifan Hong84bedeb2021-04-21 21:37:17 -070035#include <utils/misc.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080036
Yifan Hong84bedeb2021-04-21 21:37:17 -070037#include <inttypes.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080038#include <stdio.h>
39
Andrei Homescuf55d6882022-04-30 00:50:20 +000040#ifdef __linux__
41#include <linux/sched.h>
42#endif
43
Steven Moreland32150282021-11-12 22:54:53 +000044#include "BuildFlags.h"
Yifan Hong84bedeb2021-04-21 21:37:17 -070045#include "RpcState.h"
46
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080047namespace android {
48
Steven Moreland90c1f9a2021-05-03 18:27:24 +000049// Service implementations inherit from BBinder and IBinder, and this is frozen
50// in prebuilts.
51#ifdef __LP64__
52static_assert(sizeof(IBinder) == 24);
53static_assert(sizeof(BBinder) == 40);
54#else
55static_assert(sizeof(IBinder) == 12);
56static_assert(sizeof(BBinder) == 20);
57#endif
58
Steven Morelandd9a74002022-06-02 00:03:18 +000059// global b/c b/230079120 - consistent symbol table
Yifan Hong84bedeb2021-04-21 21:37:17 -070060#ifdef BINDER_RPC_DEV_SERVERS
Steven Morelandd9a74002022-06-02 00:03:18 +000061bool kEnableRpcDevServers = true;
Yifan Hong84bedeb2021-04-21 21:37:17 -070062#else
Steven Morelandd9a74002022-06-02 00:03:18 +000063bool kEnableRpcDevServers = false;
Yifan Hong84bedeb2021-04-21 21:37:17 -070064#endif
65
Sahil Somani2522b072022-08-08 14:07:47 -070066#ifdef BINDER_ENABLE_RECORDING
67bool kEnableRecording = true;
68#else
69bool kEnableRecording = false;
70#endif
71
Martijn Coenen1cad19c2021-10-04 09:19:01 +020072// Log any reply transactions for which the data exceeds this size
73#define LOG_REPLIES_OVER_SIZE (300 * 1024)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080074// ---------------------------------------------------------------------------
75
Mathias Agopian83c04462009-05-22 19:00:22 -070076IBinder::IBinder()
77 : RefBase()
78{
79}
80
81IBinder::~IBinder()
82{
83}
84
85// ---------------------------------------------------------------------------
86
Colin Cross6f4f3ab2014-02-05 17:42:44 -080087sp<IInterface> IBinder::queryLocalInterface(const String16& /*descriptor*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080088{
Yi Kongfdd8da92018-06-07 17:52:27 -070089 return nullptr;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080090}
91
92BBinder* IBinder::localBinder()
93{
Yi Kongfdd8da92018-06-07 17:52:27 -070094 return nullptr;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080095}
96
97BpBinder* IBinder::remoteBinder()
98{
Yi Kongfdd8da92018-06-07 17:52:27 -070099 return nullptr;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800100}
101
102bool IBinder::checkSubclass(const void* /*subclassID*/) const
103{
104 return false;
105}
106
Dianne Hackborn23eb1e22015-10-07 17:35:27 -0700107
Dianne Hackbornf2bf93b2015-10-14 15:13:02 -0700108status_t IBinder::shellCommand(const sp<IBinder>& target, int in, int out, int err,
Dianne Hackborn1941a402016-08-29 12:30:43 -0700109 Vector<String16>& args, const sp<IShellCallback>& callback,
110 const sp<IResultReceiver>& resultReceiver)
Dianne Hackborn23eb1e22015-10-07 17:35:27 -0700111{
Dianne Hackbornf2bf93b2015-10-14 15:13:02 -0700112 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 Hackborn23eb1e22015-10-07 17:35:27 -0700121 }
Yi Kongfdd8da92018-06-07 17:52:27 -0700122 send.writeStrongBinder(callback != nullptr ? IInterface::asBinder(callback) : nullptr);
123 send.writeStrongBinder(resultReceiver != nullptr ? IInterface::asBinder(resultReceiver) : nullptr);
Dianne Hackbornf2bf93b2015-10-14 15:13:02 -0700124 return target->transact(SHELL_COMMAND_TRANSACTION, send, &reply);
Dianne Hackborn23eb1e22015-10-07 17:35:27 -0700125}
126
Steven Morelandb8ad08d2019-08-09 14:42:56 -0700127status_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 Moreland86080b82019-09-23 15:41:18 -0700145status_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 Hong02530ec2021-06-10 13:38:38 -0700171status_t IBinder::setRpcClientDebug(android::base::unique_fd socketFd,
172 const sp<IBinder>& keepAliveBinder) {
Steven Morelandd9a74002022-06-02 00:03:18 +0000173 if (!kEnableRpcDevServers) {
Yifan Hong84bedeb2021-04-21 21:37:17 -0700174 ALOGW("setRpcClientDebug disallowed because RPC is not enabled");
175 return INVALID_OPERATION;
176 }
Steven Moreland32150282021-11-12 22:54:53 +0000177 if (!kEnableKernelIpc) {
178 ALOGW("setRpcClientDebug disallowed because kernel binder is not enabled");
179 return INVALID_OPERATION;
180 }
Yifan Hong84bedeb2021-04-21 21:37:17 -0700181
182 BBinder* local = this->localBinder();
183 if (local != nullptr) {
Yifan Hong02530ec2021-06-10 13:38:38 -0700184 return local->BBinder::setRpcClientDebug(std::move(socketFd), keepAliveBinder);
Yifan Hong84bedeb2021-04-21 21:37:17 -0700185 }
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 Hong02530ec2021-06-10 13:38:38 -0700199 if (status = data.writeStrongBinder(keepAliveBinder); status != OK) return status;
Yifan Hong84bedeb2021-04-21 21:37:17 -0700200 return transact(SET_RPC_CLIENT_TRANSACTION, data, &reply);
201}
202
Steven Moreland9e759e82021-06-25 21:30:23 +0000203void 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 Moore3faaa002022-07-27 15:54:06 +0000214sp<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 Projectedbf3b62009-03-03 19:31:44 -0800225// ---------------------------------------------------------------------------
226
Yifan Hong8b890852021-06-10 13:44:09 -0700227class BBinder::RpcServerLink : public IBinder::DeathRecipient {
228public:
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 Morelandd9a74002022-06-02 00:03:18 +0000233 virtual ~RpcServerLink();
Yifan Hong8b890852021-06-10 13:44:09 -0700234 void binderDied(const wp<IBinder>&) override {
235 LOG_RPC_DETAIL("RpcServerLink: binder died, shutting down RpcServer");
236 if (mRpcServer == nullptr) {
237 ALOGW("RpcServerLink: Unable to shut down RpcServer because it does not exist.");
238 } else {
239 ALOGW_IF(!mRpcServer->shutdown(),
240 "RpcServerLink: RpcServer did not shut down properly. Not started?");
241 }
242 mRpcServer.clear();
243
244 auto promoted = mBinder.promote();
245 if (promoted == nullptr) {
246 ALOGW("RpcServerLink: Unable to remove link from parent binder object because parent "
247 "binder object is gone.");
248 } else {
249 promoted->removeRpcServerLink(sp<RpcServerLink>::fromExisting(this));
250 }
251 mBinder.clear();
252 }
253
254private:
255 sp<RpcServer> mRpcServer;
256 sp<IBinder> mKeepAliveBinder; // hold to avoid automatically unlinking
257 wp<BBinder> mBinder;
258};
Steven Morelandd9a74002022-06-02 00:03:18 +0000259BBinder::RpcServerLink::~RpcServerLink() {}
Yifan Hong8b890852021-06-10 13:44:09 -0700260
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800261class BBinder::Extras
262{
263public:
Steven Morelandf0212002018-12-26 13:59:23 -0800264 // unlocked objects
Steven Morelandb8ad08d2019-08-09 14:42:56 -0700265 sp<IBinder> mExtension;
Andrei Homescuf55d6882022-04-30 00:50:20 +0000266#ifdef __linux__
Steven Morelandbf1915b2020-07-16 22:43:02 +0000267 int mPolicy = SCHED_NORMAL;
268 int mPriority = 0;
Andrei Homescuf55d6882022-04-30 00:50:20 +0000269#endif
270 bool mRequestingSid = false;
271 bool mInheritRt = false;
Steven Morelandf0212002018-12-26 13:59:23 -0800272
273 // for below objects
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800274 Mutex mLock;
Yifan Hong8b890852021-06-10 13:44:09 -0700275 std::set<sp<RpcServerLink>> mRpcServerLinks;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800276 BpBinder::ObjectManager mObjects;
Sahil Somani2522b072022-08-08 14:07:47 -0700277
278 android::base::unique_fd mRecordingFd;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800279};
280
281// ---------------------------------------------------------------------------
282
Sahil Somani2522b072022-08-08 14:07:47 -0700283BBinder::BBinder() : mExtras(nullptr), mStability(0), mParceled(false), mRecordingOn(false) {}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800284
285bool BBinder::isBinderAlive() const
286{
287 return true;
288}
289
290status_t BBinder::pingBinder()
291{
292 return NO_ERROR;
293}
294
Sahil Somani2522b072022-08-08 14:07:47 -0700295status_t BBinder::startRecordingTransactions(const Parcel& data) {
296 if (!kEnableRecording) {
297 ALOGW("Binder recording disallowed because recording is not enabled");
298 return INVALID_OPERATION;
299 }
300 if (!kEnableKernelIpc) {
301 ALOGW("Binder recording disallowed because kernel binder is not enabled");
302 return INVALID_OPERATION;
303 }
304 uid_t uid = IPCThreadState::self()->getCallingUid();
305 if (uid != AID_ROOT) {
306 ALOGE("Binder recording not allowed because client %" PRIu32 " is not root", uid);
307 return PERMISSION_DENIED;
308 }
309 Extras* e = getOrCreateExtras();
310 AutoMutex lock(e->mLock);
311 if (mRecordingOn) {
312 LOG(INFO) << "Could not start Binder recording. Another is already in progress.";
313 return INVALID_OPERATION;
314 } else {
315 status_t readStatus = data.readUniqueFileDescriptor(&(e->mRecordingFd));
316 if (readStatus != OK) {
317 return readStatus;
318 }
319 mRecordingOn = true;
320 LOG(INFO) << "Started Binder recording.";
321 return NO_ERROR;
322 }
323}
324
325status_t BBinder::stopRecordingTransactions() {
326 if (!kEnableRecording) {
327 ALOGW("Binder recording disallowed because recording is not enabled");
328 return INVALID_OPERATION;
329 }
330 if (!kEnableKernelIpc) {
331 ALOGW("Binder recording disallowed because kernel binder is not enabled");
332 return INVALID_OPERATION;
333 }
334 uid_t uid = IPCThreadState::self()->getCallingUid();
335 if (uid != AID_ROOT) {
336 ALOGE("Binder recording not allowed because client %" PRIu32 " is not root", uid);
337 return PERMISSION_DENIED;
338 }
339 Extras* e = getOrCreateExtras();
340 AutoMutex lock(e->mLock);
341 if (mRecordingOn) {
342 e->mRecordingFd.reset();
343 mRecordingOn = false;
344 LOG(INFO) << "Stopped Binder recording.";
345 return NO_ERROR;
346 } else {
347 LOG(INFO) << "Could not stop Binder recording. One is not in progress.";
348 return INVALID_OPERATION;
349 }
350}
351
Mathias Agopian83c04462009-05-22 19:00:22 -0700352const String16& BBinder::getInterfaceDescriptor() const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800353{
Steven Moreland1ef0eb62022-08-18 20:09:30 +0000354 static StaticString16 sBBinder(u"BBinder");
355 ALOGW("Reached BBinder::getInterfaceDescriptor (this=%p). Override?", this);
356 return sBBinder;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800357}
358
Jiyong Parkb86c8662018-10-29 23:01:57 +0900359// NOLINTNEXTLINE(google-default-arguments)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800360status_t BBinder::transact(
361 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
362{
363 data.setDataPosition(0);
364
Steven Morelandf183fdd2020-10-27 00:12:12 +0000365 if (reply != nullptr && (flags & FLAG_CLEAR_BUF)) {
366 reply->markSensitive();
367 }
368
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800369 status_t err = NO_ERROR;
370 switch (code) {
371 case PING_TRANSACTION:
Steven Moreland6e69d652019-07-10 14:17:55 -0700372 err = pingBinder();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800373 break;
Sahil Somani2522b072022-08-08 14:07:47 -0700374 case START_RECORDING_TRANSACTION:
375 err = startRecordingTransactions(data);
376 break;
377 case STOP_RECORDING_TRANSACTION:
378 err = stopRecordingTransactions();
379 break;
Steven Morelandb8ad08d2019-08-09 14:42:56 -0700380 case EXTENSION_TRANSACTION:
Jiyong Park5970d0a2022-03-08 16:56:13 +0900381 CHECK(reply != nullptr);
Steven Morelandb8ad08d2019-08-09 14:42:56 -0700382 err = reply->writeStrongBinder(getExtension());
383 break;
Steven Moreland86080b82019-09-23 15:41:18 -0700384 case DEBUG_PID_TRANSACTION:
Jiyong Park5970d0a2022-03-08 16:56:13 +0900385 CHECK(reply != nullptr);
Steven Moreland86080b82019-09-23 15:41:18 -0700386 err = reply->writeInt32(getDebugPid());
387 break;
Yifan Hong84bedeb2021-04-21 21:37:17 -0700388 case SET_RPC_CLIENT_TRANSACTION: {
389 err = setRpcClientDebug(data);
390 break;
391 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800392 default:
393 err = onTransact(code, data, reply, flags);
394 break;
395 }
396
Steven Morelanda86a3562019-08-01 23:28:34 +0000397 // In case this is being transacted on in the same process.
Yi Kongfdd8da92018-06-07 17:52:27 -0700398 if (reply != nullptr) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800399 reply->setDataPosition(0);
Martijn Coenen1cad19c2021-10-04 09:19:01 +0200400 if (reply->dataSize() > LOG_REPLIES_OVER_SIZE) {
401 ALOGW("Large reply transaction of %zu bytes, interface descriptor %s, code %d",
402 reply->dataSize(), String8(getInterfaceDescriptor()).c_str(), code);
403 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800404 }
405
Sahil Somani2522b072022-08-08 14:07:47 -0700406 if (CC_UNLIKELY(kEnableKernelIpc && mRecordingOn && code != START_RECORDING_TRANSACTION)) {
407 Extras* e = mExtras.load(std::memory_order_acquire);
408 AutoMutex lock(e->mLock);
409 if (mRecordingOn) {
410 Parcel emptyReply;
411 auto transaction =
412 android::binder::debug::RecordedTransaction::fromDetails(code, flags, data,
413 reply ? *reply
414 : emptyReply,
415 err);
416 if (transaction) {
417 if (status_t err = transaction->dumpToFile(e->mRecordingFd); err != NO_ERROR) {
418 LOG(INFO) << "Failed to dump RecordedTransaction to file with error " << err;
419 }
420 } else {
421 LOG(INFO) << "Failed to create RecordedTransaction object.";
422 }
423 }
424 }
425
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800426 return err;
427}
428
Jiyong Parkb86c8662018-10-29 23:01:57 +0900429// NOLINTNEXTLINE(google-default-arguments)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800430status_t BBinder::linkToDeath(
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800431 const sp<DeathRecipient>& /*recipient*/, void* /*cookie*/,
432 uint32_t /*flags*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800433{
434 return INVALID_OPERATION;
435}
436
Jiyong Parkb86c8662018-10-29 23:01:57 +0900437// NOLINTNEXTLINE(google-default-arguments)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800438status_t BBinder::unlinkToDeath(
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800439 const wp<DeathRecipient>& /*recipient*/, void* /*cookie*/,
440 uint32_t /*flags*/, wp<DeathRecipient>* /*outRecipient*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800441{
442 return INVALID_OPERATION;
443}
444
Dianne Hackborn23eb1e22015-10-07 17:35:27 -0700445status_t BBinder::dump(int /*fd*/, const Vector<String16>& /*args*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800446{
447 return NO_ERROR;
448}
449
Steven Moreland63a2d512021-06-25 01:10:15 +0000450void* BBinder::attachObject(const void* objectID, void* object, void* cleanupCookie,
451 object_cleanup_func func) {
Steven Morelandf0212002018-12-26 13:59:23 -0800452 Extras* e = getOrCreateExtras();
Steven Moreland5ce7ca52021-06-25 21:59:50 +0000453 LOG_ALWAYS_FATAL_IF(!e, "no memory");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800454
455 AutoMutex _l(e->mLock);
Steven Moreland63a2d512021-06-25 01:10:15 +0000456 return e->mObjects.attach(objectID, object, cleanupCookie, func);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800457}
458
459void* BBinder::findObject(const void* objectID) const
460{
Bailey Forrest6913c462015-08-18 17:15:10 -0700461 Extras* e = mExtras.load(std::memory_order_acquire);
Yi Kongfdd8da92018-06-07 17:52:27 -0700462 if (!e) return nullptr;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800463
464 AutoMutex _l(e->mLock);
465 return e->mObjects.find(objectID);
466}
467
Steven Moreland63a2d512021-06-25 01:10:15 +0000468void* BBinder::detachObject(const void* objectID) {
Bailey Forrest6913c462015-08-18 17:15:10 -0700469 Extras* e = mExtras.load(std::memory_order_acquire);
Steven Moreland63a2d512021-06-25 01:10:15 +0000470 if (!e) return nullptr;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800471
472 AutoMutex _l(e->mLock);
Steven Moreland63a2d512021-06-25 01:10:15 +0000473 return e->mObjects.detach(objectID);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800474}
475
Steven Moreland9e759e82021-06-25 21:30:23 +0000476void BBinder::withLock(const std::function<void()>& doWithLock) {
477 Extras* e = getOrCreateExtras();
478 LOG_ALWAYS_FATAL_IF(!e, "no memory");
479
480 AutoMutex _l(e->mLock);
481 doWithLock();
482}
483
Devin Moore3faaa002022-07-27 15:54:06 +0000484sp<IBinder> BBinder::lookupOrCreateWeak(const void* objectID, object_make_func make,
485 const void* makeArgs) {
486 Extras* e = getOrCreateExtras();
487 LOG_ALWAYS_FATAL_IF(!e, "no memory");
488 AutoMutex _l(e->mLock);
489 return e->mObjects.lookupOrCreateWeak(objectID, make, makeArgs);
490}
491
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800492BBinder* BBinder::localBinder()
493{
494 return this;
495}
496
Steven Morelandf0212002018-12-26 13:59:23 -0800497bool BBinder::isRequestingSid()
498{
499 Extras* e = mExtras.load(std::memory_order_acquire);
500
501 return e && e->mRequestingSid;
502}
503
504void BBinder::setRequestingSid(bool requestingSid)
505{
Steven Morelandd37e6dd2021-06-16 22:07:51 +0000506 LOG_ALWAYS_FATAL_IF(mParceled,
507 "setRequestingSid() should not be called after a binder object "
508 "is parceled/sent to another process");
Kalesh Singhd67c8e82020-12-29 15:46:25 -0500509
Steven Morelandf0212002018-12-26 13:59:23 -0800510 Extras* e = mExtras.load(std::memory_order_acquire);
511
512 if (!e) {
513 // default is false. Most things don't need sids, so avoiding allocations when possible.
514 if (!requestingSid) {
515 return;
516 }
517
518 e = getOrCreateExtras();
519 if (!e) return; // out of memory
520 }
521
Steven Moreland3668be62019-02-08 17:56:55 -0800522 e->mRequestingSid = requestingSid;
Steven Morelandf0212002018-12-26 13:59:23 -0800523}
524
Steven Morelandb8ad08d2019-08-09 14:42:56 -0700525sp<IBinder> BBinder::getExtension() {
526 Extras* e = mExtras.load(std::memory_order_acquire);
527 if (e == nullptr) return nullptr;
528 return e->mExtension;
529}
530
Andrei Homescuf55d6882022-04-30 00:50:20 +0000531#ifdef __linux__
Steven Morelandbf1915b2020-07-16 22:43:02 +0000532void BBinder::setMinSchedulerPolicy(int policy, int priority) {
Steven Morelandd37e6dd2021-06-16 22:07:51 +0000533 LOG_ALWAYS_FATAL_IF(mParceled,
534 "setMinSchedulerPolicy() should not be called after a binder object "
535 "is parceled/sent to another process");
Kalesh Singhd67c8e82020-12-29 15:46:25 -0500536
Steven Morelandbf1915b2020-07-16 22:43:02 +0000537 switch (policy) {
538 case SCHED_NORMAL:
539 LOG_ALWAYS_FATAL_IF(priority < -20 || priority > 19, "Invalid priority for SCHED_NORMAL: %d", priority);
540 break;
541 case SCHED_RR:
542 case SCHED_FIFO:
543 LOG_ALWAYS_FATAL_IF(priority < 1 || priority > 99, "Invalid priority for sched %d: %d", policy, priority);
544 break;
545 default:
546 LOG_ALWAYS_FATAL("Unrecognized scheduling policy: %d", policy);
547 }
548
549 Extras* e = mExtras.load(std::memory_order_acquire);
550
551 if (e == nullptr) {
552 // Avoid allocations if called with default.
553 if (policy == SCHED_NORMAL && priority == 0) {
554 return;
555 }
556
557 e = getOrCreateExtras();
558 if (!e) return; // out of memory
559 }
560
561 e->mPolicy = policy;
562 e->mPriority = priority;
563}
564
565int BBinder::getMinSchedulerPolicy() {
566 Extras* e = mExtras.load(std::memory_order_acquire);
567 if (e == nullptr) return SCHED_NORMAL;
568 return e->mPolicy;
569}
570
571int BBinder::getMinSchedulerPriority() {
572 Extras* e = mExtras.load(std::memory_order_acquire);
573 if (e == nullptr) return 0;
574 return e->mPriority;
575}
Andrei Homescuf55d6882022-04-30 00:50:20 +0000576#endif // __linux__
Steven Morelandbf1915b2020-07-16 22:43:02 +0000577
Steven Morelandcf03cf12020-12-04 02:58:40 +0000578bool BBinder::isInheritRt() {
579 Extras* e = mExtras.load(std::memory_order_acquire);
580
581 return e && e->mInheritRt;
582}
583
584void BBinder::setInheritRt(bool inheritRt) {
Steven Morelandd37e6dd2021-06-16 22:07:51 +0000585 LOG_ALWAYS_FATAL_IF(mParceled,
586 "setInheritRt() should not be called after a binder object "
587 "is parceled/sent to another process");
Kalesh Singhd67c8e82020-12-29 15:46:25 -0500588
Steven Morelandcf03cf12020-12-04 02:58:40 +0000589 Extras* e = mExtras.load(std::memory_order_acquire);
590
591 if (!e) {
592 if (!inheritRt) {
593 return;
594 }
595
596 e = getOrCreateExtras();
597 if (!e) return; // out of memory
598 }
599
600 e->mInheritRt = inheritRt;
601}
602
Steven Moreland86080b82019-09-23 15:41:18 -0700603pid_t BBinder::getDebugPid() {
Andrei Homescuf55d6882022-04-30 00:50:20 +0000604#ifdef __linux__
Steven Moreland86080b82019-09-23 15:41:18 -0700605 return getpid();
Andrei Homescuf55d6882022-04-30 00:50:20 +0000606#else
607 // TODO: handle other OSes
608 return 0;
609#endif // __linux__
Steven Moreland86080b82019-09-23 15:41:18 -0700610}
611
Steven Morelandb8ad08d2019-08-09 14:42:56 -0700612void BBinder::setExtension(const sp<IBinder>& extension) {
Steven Morelandd37e6dd2021-06-16 22:07:51 +0000613 LOG_ALWAYS_FATAL_IF(mParceled,
614 "setExtension() should not be called after a binder object "
615 "is parceled/sent to another process");
Kalesh Singhd67c8e82020-12-29 15:46:25 -0500616
Steven Morelandb8ad08d2019-08-09 14:42:56 -0700617 Extras* e = getOrCreateExtras();
618 e->mExtension = extension;
619}
620
Kalesh Singhd67c8e82020-12-29 15:46:25 -0500621bool BBinder::wasParceled() {
622 return mParceled;
623}
624
625void BBinder::setParceled() {
626 mParceled = true;
627}
628
Yifan Hong84bedeb2021-04-21 21:37:17 -0700629status_t BBinder::setRpcClientDebug(const Parcel& data) {
Steven Morelandd9a74002022-06-02 00:03:18 +0000630 if (!kEnableRpcDevServers) {
Yifan Hong84bedeb2021-04-21 21:37:17 -0700631 ALOGW("%s: disallowed because RPC is not enabled", __PRETTY_FUNCTION__);
632 return INVALID_OPERATION;
633 }
Steven Moreland32150282021-11-12 22:54:53 +0000634 if (!kEnableKernelIpc) {
635 ALOGW("setRpcClientDebug disallowed because kernel binder is not enabled");
636 return INVALID_OPERATION;
637 }
Yifan Hong84bedeb2021-04-21 21:37:17 -0700638 uid_t uid = IPCThreadState::self()->getCallingUid();
639 if (uid != AID_ROOT) {
640 ALOGE("%s: not allowed because client %" PRIu32 " is not root", __PRETTY_FUNCTION__, uid);
641 return PERMISSION_DENIED;
642 }
643 status_t status;
644 bool hasSocketFd;
645 android::base::unique_fd clientFd;
Yifan Hong84bedeb2021-04-21 21:37:17 -0700646
647 if (status = data.readBool(&hasSocketFd); status != OK) return status;
648 if (hasSocketFd) {
649 if (status = data.readUniqueFileDescriptor(&clientFd); status != OK) return status;
650 }
Yifan Hong02530ec2021-06-10 13:38:38 -0700651 sp<IBinder> keepAliveBinder;
652 if (status = data.readNullableStrongBinder(&keepAliveBinder); status != OK) return status;
Yifan Hong84bedeb2021-04-21 21:37:17 -0700653
Yifan Hong02530ec2021-06-10 13:38:38 -0700654 return setRpcClientDebug(std::move(clientFd), keepAliveBinder);
Yifan Hong84bedeb2021-04-21 21:37:17 -0700655}
656
Yifan Hong02530ec2021-06-10 13:38:38 -0700657status_t BBinder::setRpcClientDebug(android::base::unique_fd socketFd,
658 const sp<IBinder>& keepAliveBinder) {
Steven Morelandd9a74002022-06-02 00:03:18 +0000659 if (!kEnableRpcDevServers) {
Yifan Hong84bedeb2021-04-21 21:37:17 -0700660 ALOGW("%s: disallowed because RPC is not enabled", __PRETTY_FUNCTION__);
661 return INVALID_OPERATION;
662 }
Steven Moreland32150282021-11-12 22:54:53 +0000663 if (!kEnableKernelIpc) {
664 ALOGW("setRpcClientDebug disallowed because kernel binder is not enabled");
665 return INVALID_OPERATION;
666 }
Yifan Hong84bedeb2021-04-21 21:37:17 -0700667
668 const int socketFdForPrint = socketFd.get();
Yifan Hong34823232021-06-07 17:23:00 -0700669 LOG_RPC_DETAIL("%s(fd=%d)", __PRETTY_FUNCTION__, socketFdForPrint);
Yifan Hong84bedeb2021-04-21 21:37:17 -0700670
671 if (!socketFd.ok()) {
672 ALOGE("%s: No socket FD provided.", __PRETTY_FUNCTION__);
673 return BAD_VALUE;
674 }
Yifan Hong84bedeb2021-04-21 21:37:17 -0700675
Yifan Hong02530ec2021-06-10 13:38:38 -0700676 if (keepAliveBinder == nullptr) {
677 ALOGE("%s: No keepAliveBinder provided.", __PRETTY_FUNCTION__);
678 return UNEXPECTED_NULL;
679 }
680
Elie Kheirallah47431c12022-04-21 23:46:17 +0000681 size_t binderThreadPoolMaxCount = ProcessState::self()->getThreadPoolMaxTotalThreadCount();
Yifan Hong84bedeb2021-04-21 21:37:17 -0700682 if (binderThreadPoolMaxCount <= 1) {
683 ALOGE("%s: ProcessState thread pool max count is %zu. RPC is disabled for this service "
684 "because RPC requires the service to support multithreading.",
685 __PRETTY_FUNCTION__, binderThreadPoolMaxCount);
686 return INVALID_OPERATION;
687 }
688
Yifan Hong8b890852021-06-10 13:44:09 -0700689 // Weak ref to avoid circular dependency:
690 // BBinder -> RpcServerLink ----> RpcServer -X-> BBinder
691 // `-X-> BBinder
692 auto weakThis = wp<BBinder>::fromExisting(this);
693
Yifan Hong84bedeb2021-04-21 21:37:17 -0700694 Extras* e = getOrCreateExtras();
695 AutoMutex _l(e->mLock);
Yifan Hong8b890852021-06-10 13:44:09 -0700696 auto rpcServer = RpcServer::make();
697 LOG_ALWAYS_FATAL_IF(rpcServer == nullptr, "RpcServer::make returns null");
Yifan Hong8b890852021-06-10 13:44:09 -0700698 auto link = sp<RpcServerLink>::make(rpcServer, keepAliveBinder, weakThis);
699 if (auto status = keepAliveBinder->linkToDeath(link, nullptr, 0); status != OK) {
700 ALOGE("%s: keepAliveBinder->linkToDeath returns %s", __PRETTY_FUNCTION__,
701 statusToString(status).c_str());
702 return status;
Yifan Hong84bedeb2021-04-21 21:37:17 -0700703 }
Yifan Hong8b890852021-06-10 13:44:09 -0700704 rpcServer->setRootObjectWeak(weakThis);
Steven Moreland2372f9d2021-08-05 15:42:01 -0700705 if (auto status = rpcServer->setupExternalServer(std::move(socketFd)); status != OK) {
706 return status;
707 }
Yifan Hong8b890852021-06-10 13:44:09 -0700708 rpcServer->setMaxThreads(binderThreadPoolMaxCount);
709 rpcServer->start();
710 e->mRpcServerLinks.emplace(link);
Yifan Hong34823232021-06-07 17:23:00 -0700711 LOG_RPC_DETAIL("%s(fd=%d) successful", __PRETTY_FUNCTION__, socketFdForPrint);
Yifan Hong84bedeb2021-04-21 21:37:17 -0700712 return OK;
713}
714
Yifan Hong8b890852021-06-10 13:44:09 -0700715void BBinder::removeRpcServerLink(const sp<RpcServerLink>& link) {
716 Extras* e = mExtras.load(std::memory_order_acquire);
717 if (!e) return;
718 AutoMutex _l(e->mLock);
719 (void)e->mRpcServerLinks.erase(link);
720}
721
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800722BBinder::~BBinder()
723{
Steven Moreland1bd2bc72022-07-29 20:29:40 +0000724 if (!wasParceled()) {
725 if (getExtension()) {
726 ALOGW("Binder %p destroyed with extension attached before being parceled.", this);
727 }
728 if (isRequestingSid()) {
729 ALOGW("Binder %p destroyed when requesting SID before being parceled.", this);
730 }
731 if (isInheritRt()) {
732 ALOGW("Binder %p destroyed after setInheritRt before being parceled.", this);
733 }
Andrei Homescu40525502022-08-02 01:23:22 +0000734#ifdef __linux__
Steven Moreland1bd2bc72022-07-29 20:29:40 +0000735 if (getMinSchedulerPolicy() != SCHED_NORMAL) {
736 ALOGW("Binder %p destroyed after setMinSchedulerPolicy before being parceled.", this);
737 }
738 if (getMinSchedulerPriority() != 0) {
739 ALOGW("Binder %p destroyed after setMinSchedulerPolicy before being parceled.", this);
740 }
Andrei Homescu40525502022-08-02 01:23:22 +0000741#endif // __linux__
Steven Moreland55a12542022-03-31 21:53:11 +0000742 }
743
Bailey Forrest6913c462015-08-18 17:15:10 -0700744 Extras* e = mExtras.load(std::memory_order_relaxed);
Hans Boehm3effaba2014-08-12 22:56:00 +0000745 if (e) delete e;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800746}
747
748
Jiyong Parkb86c8662018-10-29 23:01:57 +0900749// NOLINTNEXTLINE(google-default-arguments)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800750status_t BBinder::onTransact(
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800751 uint32_t code, const Parcel& data, Parcel* reply, uint32_t /*flags*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800752{
753 switch (code) {
754 case INTERFACE_TRANSACTION:
Jiyong Park5970d0a2022-03-08 16:56:13 +0900755 CHECK(reply != nullptr);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800756 reply->writeString16(getInterfaceDescriptor());
757 return NO_ERROR;
758
759 case DUMP_TRANSACTION: {
760 int fd = data.readFileDescriptor();
761 int argc = data.readInt32();
762 Vector<String16> args;
763 for (int i = 0; i < argc && data.dataAvail() > 0; i++) {
764 args.add(data.readString16());
765 }
766 return dump(fd, args);
767 }
Dianne Hackborn555f89d2012-05-08 18:54:22 -0700768
Dianne Hackborn23eb1e22015-10-07 17:35:27 -0700769 case SHELL_COMMAND_TRANSACTION: {
770 int in = data.readFileDescriptor();
771 int out = data.readFileDescriptor();
772 int err = data.readFileDescriptor();
773 int argc = data.readInt32();
774 Vector<String16> args;
775 for (int i = 0; i < argc && data.dataAvail() > 0; i++) {
776 args.add(data.readString16());
777 }
Andrei Homescua543a842022-07-07 22:17:55 +0000778 sp<IBinder> shellCallbackBinder = data.readStrongBinder();
Dianne Hackborn23eb1e22015-10-07 17:35:27 -0700779 sp<IResultReceiver> resultReceiver = IResultReceiver::asInterface(
780 data.readStrongBinder());
781
Dianne Hackbornf2bf93b2015-10-14 15:13:02 -0700782 // XXX can't add virtuals until binaries are updated.
Andrei Homescua543a842022-07-07 22:17:55 +0000783 // sp<IShellCallback> shellCallback = IShellCallback::asInterface(
784 // shellCallbackBinder);
785 // return shellCommand(in, out, err, args, resultReceiver);
Christopher Wiley0a9a1c12016-07-20 08:28:14 -0700786 (void)in;
787 (void)out;
788 (void)err;
789
Yi Kongfdd8da92018-06-07 17:52:27 -0700790 if (resultReceiver != nullptr) {
Dianne Hackbornf2bf93b2015-10-14 15:13:02 -0700791 resultReceiver->send(INVALID_OPERATION);
792 }
Martijn Coenenaa6ee992017-08-17 15:38:08 +0200793
794 return NO_ERROR;
Dianne Hackborn23eb1e22015-10-07 17:35:27 -0700795 }
796
Dianne Hackborn555f89d2012-05-08 18:54:22 -0700797 case SYSPROPS_TRANSACTION: {
798 report_sysprop_change();
799 return NO_ERROR;
800 }
801
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800802 default:
803 return UNKNOWN_TRANSACTION;
804 }
805}
806
Steven Morelandf0212002018-12-26 13:59:23 -0800807BBinder::Extras* BBinder::getOrCreateExtras()
808{
809 Extras* e = mExtras.load(std::memory_order_acquire);
810
811 if (!e) {
812 e = new Extras;
813 Extras* expected = nullptr;
814 if (!mExtras.compare_exchange_strong(expected, e,
815 std::memory_order_release,
816 std::memory_order_acquire)) {
817 delete e;
818 e = expected; // Filled in by CAS
819 }
820 if (e == nullptr) return nullptr; // out of memory
821 }
822
823 return e;
824}
825
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800826// ---------------------------------------------------------------------------
827
828enum {
829 // This is used to transfer ownership of the remote binder from
830 // the BpRefBase object holding it (when it is constructed), to the
831 // owner of the BpRefBase object when it first acquires that BpRefBase.
832 kRemoteAcquired = 0x00000001
833};
834
835BpRefBase::BpRefBase(const sp<IBinder>& o)
Yi Kongfdd8da92018-06-07 17:52:27 -0700836 : mRemote(o.get()), mRefs(nullptr), mState(0)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800837{
838 extendObjectLifetime(OBJECT_LIFETIME_WEAK);
839
840 if (mRemote) {
841 mRemote->incStrong(this); // Removed on first IncStrong().
842 mRefs = mRemote->createWeak(this); // Held for our entire lifetime.
843 }
844}
845
846BpRefBase::~BpRefBase()
847{
848 if (mRemote) {
Bailey Forrest6913c462015-08-18 17:15:10 -0700849 if (!(mState.load(std::memory_order_relaxed)&kRemoteAcquired)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800850 mRemote->decStrong(this);
851 }
852 mRefs->decWeak(this);
853 }
854}
855
856void BpRefBase::onFirstRef()
857{
Bailey Forrest6913c462015-08-18 17:15:10 -0700858 mState.fetch_or(kRemoteAcquired, std::memory_order_relaxed);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800859}
860
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800861void BpRefBase::onLastStrongRef(const void* /*id*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800862{
863 if (mRemote) {
864 mRemote->decStrong(this);
865 }
866}
867
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800868bool BpRefBase::onIncStrongAttempted(uint32_t /*flags*/, const void* /*id*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800869{
870 return mRemote ? mRefs->attemptIncStrong(this) : false;
871}
872
873// ---------------------------------------------------------------------------
874
Steven Moreland61ff8492019-09-26 16:05:45 -0700875} // namespace android