blob: 817e0fc756d55e76bde28bdaa080ec3a67719eac [file] [log] [blame]
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001/*
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
17#define LOG_TAG "Parcel"
18//#define LOG_NDEBUG 0
19
Mark Salyzynabed7f72016-01-27 08:02:48 -080020#include <errno.h>
Mark Salyzyn70f36652016-02-02 10:27:03 -080021#include <fcntl.h>
Mark Salyzynabed7f72016-01-27 08:02:48 -080022#include <inttypes.h>
Mark Salyzyn70f36652016-02-02 10:27:03 -080023#include <pthread.h>
Mark Salyzynabed7f72016-01-27 08:02:48 -080024#include <stdint.h>
25#include <stdio.h>
26#include <stdlib.h>
27#include <sys/mman.h>
Andrei Homescua9cca9c2022-05-03 04:48:01 +000028#include <sys/resource.h>
Mark Salyzyneab2afc2016-01-27 08:02:48 -080029#include <sys/stat.h>
30#include <sys/types.h>
31#include <unistd.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070032
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070033#include <binder/Binder.h>
34#include <binder/BpBinder.h>
Mark Salyzynabed7f72016-01-27 08:02:48 -080035#include <binder/IPCThreadState.h>
36#include <binder/Parcel.h>
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070037#include <binder/ProcessState.h>
Steven Moreland6e5a7752019-08-05 20:30:14 -070038#include <binder/Stability.h>
Christopher Wiley09eb7492015-11-09 15:06:15 -080039#include <binder/Status.h>
Mathias Agopian002e1e52013-05-06 20:20:50 -070040#include <binder/TextOutput.h>
41
Frederick Mayle69a0c992022-05-26 20:38:39 +000042#include <android-base/scopeguard.h>
Mark Salyzynabed7f72016-01-27 08:02:48 -080043#include <cutils/ashmem.h>
Steven Moreland3af936a2021-03-26 03:05:38 +000044#include <cutils/compiler.h>
Mark Salyzynabed7f72016-01-27 08:02:48 -080045#include <utils/Flattenable.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070046#include <utils/Log.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070047#include <utils/String16.h>
Steven Moreland3af936a2021-03-26 03:05:38 +000048#include <utils/String8.h>
49#include <utils/misc.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070050
Andrei Homescu24ad36e2022-08-04 01:33:33 +000051#include "OS.h"
Steven Moreland5553ac42020-11-11 02:14:45 +000052#include "RpcState.h"
Steven Morelanda4853cd2019-07-12 15:44:37 -070053#include "Static.h"
Steven Morelandf183fdd2020-10-27 00:12:12 +000054#include "Utils.h"
Andrei Homescua9cca9c2022-05-03 04:48:01 +000055
56// A lot of code in this file uses definitions from the
57// Linux kernel header for Binder <linux/android/binder.h>
58// which is included indirectly via "binder_module.h".
59// Non-Linux OSes do not have that header, so libbinder should be
60// built for those targets without kernel binder support, i.e.,
61// without BINDER_WITH_KERNEL_IPC. For this reason, all code in this
62// file that depends on kernel binder, including the header itself,
63// is conditional on BINDER_WITH_KERNEL_IPC.
64#ifdef BINDER_WITH_KERNEL_IPC
65#include <linux/sched.h>
Steven Moreland6ba5a252021-05-04 22:49:00 +000066#include "binder_module.h"
Andrei Homescua9cca9c2022-05-03 04:48:01 +000067#else // BINDER_WITH_KERNEL_IPC
68// Needed by {read,write}Pointer
69typedef uintptr_t binder_uintptr_t;
70#endif // BINDER_WITH_KERNEL_IPC
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070071
Steven Moreland02f736f2023-06-22 23:03:57 +000072#ifdef __BIONIC__
73#include <android/fdsan.h>
74#endif
75
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070076#define LOG_REFS(...)
Andrei Homescua9cca9c2022-05-03 04:48:01 +000077// #define LOG_REFS(...) ALOG(LOG_DEBUG, LOG_TAG, __VA_ARGS__)
Dianne Hackborn7e790af2014-11-11 12:22:53 -080078#define LOG_ALLOC(...)
Andrei Homescua9cca9c2022-05-03 04:48:01 +000079// #define LOG_ALLOC(...) ALOG(LOG_DEBUG, LOG_TAG, __VA_ARGS__)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070080
81// ---------------------------------------------------------------------------
82
Nick Kralevichb6b14232015-04-02 09:36:02 -070083// This macro should never be used at runtime, as a too large value
84// of s could cause an integer overflow. Instead, you should always
85// use the wrapper function pad_size()
Andrei Homescuf7f2c172022-03-08 22:52:49 +000086#define PAD_SIZE_UNSAFE(s) (((s) + 3) & ~3UL)
Nick Kralevichb6b14232015-04-02 09:36:02 -070087
88static size_t pad_size(size_t s) {
Steven Moreland28723ae2019-04-01 18:52:30 -070089 if (s > (std::numeric_limits<size_t>::max() - 3)) {
Steven Moreland6adf33c2019-09-25 13:18:09 -070090 LOG_ALWAYS_FATAL("pad size too big %zu", s);
Nick Kralevichb6b14232015-04-02 09:36:02 -070091 }
92 return PAD_SIZE_UNSAFE(s);
93}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070094
Brad Fitzpatricka877cd82010-07-07 16:06:39 -070095// Note: must be kept in sync with android/os/StrictMode.java's PENALTY_GATHER
Jeff Sharkey05827be2018-06-26 10:52:38 -060096#define STRICT_MODE_PENALTY_GATHER (1 << 31)
Brad Fitzpatricka877cd82010-07-07 16:06:39 -070097
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070098namespace android {
99
Steven Moreland7b102262019-08-01 15:48:43 -0700100// many things compile this into prebuilts on the stack
Steven Moreland90c1f9a2021-05-03 18:27:24 +0000101#ifdef __LP64__
102static_assert(sizeof(Parcel) == 120);
103#else
104static_assert(sizeof(Parcel) == 60);
105#endif
Steven Moreland7b102262019-08-01 15:48:43 -0700106
Jeff Sharkey8994c182020-09-11 12:07:10 -0600107static std::atomic<size_t> gParcelGlobalAllocCount;
108static std::atomic<size_t> gParcelGlobalAllocSize;
Dianne Hackborna4cff882014-11-13 17:07:40 -0800109
Andrei Homescu1519b982022-06-09 02:04:44 +0000110// Maximum number of file descriptors per Parcel.
111constexpr size_t kMaxFds = 1024;
Christopher Tatee4e0ae82016-03-24 16:03:44 -0700112
Jeff Brown13b16042014-11-11 16:44:25 -0800113// Maximum size of a blob to transfer in-place.
114static const size_t BLOB_INPLACE_LIMIT = 16 * 1024;
115
Steven Moreland02f736f2023-06-22 23:03:57 +0000116#if defined(__BIONIC__)
117static void FdTag(int fd, const void* old_addr, const void* new_addr) {
118 if (android_fdsan_exchange_owner_tag) {
119 uint64_t old_tag = android_fdsan_create_owner_tag(ANDROID_FDSAN_OWNER_TYPE_PARCEL,
120 reinterpret_cast<uint64_t>(old_addr));
121 uint64_t new_tag = android_fdsan_create_owner_tag(ANDROID_FDSAN_OWNER_TYPE_PARCEL,
122 reinterpret_cast<uint64_t>(new_addr));
123 android_fdsan_exchange_owner_tag(fd, old_tag, new_tag);
124 }
125}
126static void FdTagClose(int fd, const void* addr) {
127 if (android_fdsan_close_with_tag) {
128 uint64_t tag = android_fdsan_create_owner_tag(ANDROID_FDSAN_OWNER_TYPE_PARCEL,
129 reinterpret_cast<uint64_t>(addr));
130 android_fdsan_close_with_tag(fd, tag);
131 } else {
132 close(fd);
133 }
134}
135#else
136static void FdTag(int fd, const void* old_addr, const void* new_addr) {
137 (void)fd;
138 (void)old_addr;
139 (void)new_addr;
140}
141static void FdTagClose(int fd, const void* addr) {
142 (void)addr;
143 close(fd);
144}
145#endif
146
Jeff Brown13b16042014-11-11 16:44:25 -0800147enum {
148 BLOB_INPLACE = 0,
149 BLOB_ASHMEM_IMMUTABLE = 1,
150 BLOB_ASHMEM_MUTABLE = 2,
151};
152
Andrei Homescua9cca9c2022-05-03 04:48:01 +0000153#ifdef BINDER_WITH_KERNEL_IPC
Steven Morelandc673f1f2021-10-07 18:23:35 -0700154static void acquire_object(const sp<ProcessState>& proc, const flat_binder_object& obj,
155 const void* who) {
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700156 switch (obj.hdr.type) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700157 case BINDER_TYPE_BINDER:
158 if (obj.binder) {
yuxic05af3b2021-08-24 02:52:15 +0000159 LOG_REFS("Parcel %p acquiring reference on local %llu", who, obj.cookie);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800160 reinterpret_cast<IBinder*>(obj.cookie)->incStrong(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700161 }
162 return;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700163 case BINDER_TYPE_HANDLE: {
164 const sp<IBinder> b = proc->getStrongProxyForHandle(obj.handle);
Yi Kong91635562018-06-07 14:38:36 -0700165 if (b != nullptr) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700166 LOG_REFS("Parcel %p acquiring reference on remote %p", who, b.get());
167 b->incStrong(who);
168 }
169 return;
170 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700171 case BINDER_TYPE_FD: {
Steven Moreland02f736f2023-06-22 23:03:57 +0000172 if (obj.cookie != 0) { // owned
173 FdTag(obj.handle, nullptr, who);
174 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700175 return;
176 }
177 }
178
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700179 ALOGD("Invalid object type 0x%08x", obj.hdr.type);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700180}
181
Steven Morelandc673f1f2021-10-07 18:23:35 -0700182static void release_object(const sp<ProcessState>& proc, const flat_binder_object& obj,
183 const void* who) {
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700184 switch (obj.hdr.type) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700185 case BINDER_TYPE_BINDER:
186 if (obj.binder) {
yuxic05af3b2021-08-24 02:52:15 +0000187 LOG_REFS("Parcel %p releasing reference on local %llu", who, obj.cookie);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800188 reinterpret_cast<IBinder*>(obj.cookie)->decStrong(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700189 }
190 return;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700191 case BINDER_TYPE_HANDLE: {
192 const sp<IBinder> b = proc->getStrongProxyForHandle(obj.handle);
Yi Kong91635562018-06-07 14:38:36 -0700193 if (b != nullptr) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700194 LOG_REFS("Parcel %p releasing reference on remote %p", who, b.get());
195 b->decStrong(who);
196 }
197 return;
198 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700199 case BINDER_TYPE_FD: {
Steven Moreland02f736f2023-06-22 23:03:57 +0000200 // note: this path is not used when mOwner, so the tag is also released
201 // in 'closeFileDescriptors'
Mark Salyzynb454d8f2016-01-27 08:02:48 -0800202 if (obj.cookie != 0) { // owned
Steven Moreland02f736f2023-06-22 23:03:57 +0000203 FdTagClose(obj.handle, who);
Adrian Rooscbf37262015-10-22 16:12:53 -0700204 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700205 return;
206 }
207 }
208
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700209 ALOGE("Invalid object type 0x%08x", obj.hdr.type);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700210}
Andrei Homescua9cca9c2022-05-03 04:48:01 +0000211#endif // BINDER_WITH_KERNEL_IPC
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700212
Frederick Mayle69a0c992022-05-26 20:38:39 +0000213static int toRawFd(const std::variant<base::unique_fd, base::borrowed_fd>& v) {
214 return std::visit([](const auto& fd) { return fd.get(); }, v);
215}
216
Frederick Mayled3c595a2022-05-09 23:02:54 +0000217Parcel::RpcFields::RpcFields(const sp<RpcSession>& session) : mSession(session) {
218 LOG_ALWAYS_FATAL_IF(mSession == nullptr);
219}
220
Steven Moreland34b48cb2020-12-01 22:45:38 +0000221status_t Parcel::finishFlattenBinder(const sp<IBinder>& binder)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700222{
Steven Moreland6e5a7752019-08-05 20:30:14 -0700223 internal::Stability::tryMarkCompilationUnit(binder.get());
Steven Moreland16a41062021-07-23 13:35:25 -0700224 int16_t rep = internal::Stability::getRepr(binder.get());
Steven Moreland14e4cfa2021-06-03 21:40:45 +0000225 return writeInt32(rep);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700226}
227
Steven Morelanda86a3562019-08-01 23:28:34 +0000228status_t Parcel::finishUnflattenBinder(
229 const sp<IBinder>& binder, sp<IBinder>* out) const
230{
231 int32_t stability;
232 status_t status = readInt32(&stability);
233 if (status != OK) return status;
234
Steven Moreland14e4cfa2021-06-03 21:40:45 +0000235 status = internal::Stability::setRepr(binder.get(), static_cast<int16_t>(stability),
236 true /*log*/);
Steven Morelanda86a3562019-08-01 23:28:34 +0000237 if (status != OK) return status;
238
239 *out = binder;
240 return OK;
241}
242
Andrei Homescua9cca9c2022-05-03 04:48:01 +0000243#ifdef BINDER_WITH_KERNEL_IPC
Steven Morelandbf1915b2020-07-16 22:43:02 +0000244static constexpr inline int schedPolicyMask(int policy, int priority) {
245 return (priority & FLAT_BINDER_FLAG_PRIORITY_MASK) | ((policy & 3) << FLAT_BINDER_FLAG_SCHED_POLICY_SHIFT);
246}
Andrei Homescua9cca9c2022-05-03 04:48:01 +0000247#endif // BINDER_WITH_KERNEL_IPC
Steven Morelandbf1915b2020-07-16 22:43:02 +0000248
Kalesh Singhd67c8e82020-12-29 15:46:25 -0500249status_t Parcel::flattenBinder(const sp<IBinder>& binder) {
250 BBinder* local = nullptr;
251 if (binder) local = binder->localBinder();
252 if (local) local->setParceled();
253
Frederick Mayled3c595a2022-05-09 23:02:54 +0000254 if (const auto* rpcFields = maybeRpcFields()) {
Steven Moreland5553ac42020-11-11 02:14:45 +0000255 if (binder) {
256 status_t status = writeInt32(1); // non-null
257 if (status != OK) return status;
Steven Moreland5623d1a2021-09-10 15:45:34 -0700258 uint64_t address;
Steven Morelanda5036f02021-06-08 02:26:57 +0000259 // TODO(b/167966510): need to undo this if the Parcel is not sent
Frederick Mayled3c595a2022-05-09 23:02:54 +0000260 status = rpcFields->mSession->state()->onBinderLeaving(rpcFields->mSession, binder,
261 &address);
Steven Moreland5553ac42020-11-11 02:14:45 +0000262 if (status != OK) return status;
Steven Moreland5623d1a2021-09-10 15:45:34 -0700263 status = writeUint64(address);
Steven Moreland5553ac42020-11-11 02:14:45 +0000264 if (status != OK) return status;
265 } else {
266 status_t status = writeInt32(0); // null
267 if (status != OK) return status;
268 }
269 return finishFlattenBinder(binder);
270 }
271
Andrei Homescua9cca9c2022-05-03 04:48:01 +0000272#ifdef BINDER_WITH_KERNEL_IPC
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700273 flat_binder_object obj;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700274
Steven Morelandbf1915b2020-07-16 22:43:02 +0000275 int schedBits = 0;
276 if (!IPCThreadState::self()->backgroundSchedulingDisabled()) {
277 schedBits = schedPolicyMask(SCHED_NORMAL, 19);
Martijn Coenen2b631742017-05-05 11:16:59 -0700278 }
279
Yi Kong91635562018-06-07 14:38:36 -0700280 if (binder != nullptr) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700281 if (!local) {
282 BpBinder *proxy = binder->remoteBinder();
Yi Kong91635562018-06-07 14:38:36 -0700283 if (proxy == nullptr) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000284 ALOGE("null proxy");
Steven Moreland5553ac42020-11-11 02:14:45 +0000285 } else {
286 if (proxy->isRpcBinder()) {
Steven Morelanda9231112021-09-22 10:08:14 -0700287 ALOGE("Sending a socket binder over kernel binder is prohibited");
Steven Moreland5553ac42020-11-11 02:14:45 +0000288 return INVALID_OPERATION;
289 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700290 }
Steven Moreland99157622021-09-13 16:27:34 -0700291 const int32_t handle = proxy ? proxy->getPrivateAccessor().binderHandle() : 0;
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700292 obj.hdr.type = BINDER_TYPE_HANDLE;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -0800293 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
Steven Moreland49c27532022-03-01 10:21:07 +0000294 obj.flags = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700295 obj.handle = handle;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800296 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700297 } else {
Steven Morelandbf1915b2020-07-16 22:43:02 +0000298 int policy = local->getMinSchedulerPolicy();
299 int priority = local->getMinSchedulerPriority();
300
301 if (policy != 0 || priority != 0) {
302 // override value, since it is set explicitly
303 schedBits = schedPolicyMask(policy, priority);
304 }
Steven Moreland49c27532022-03-01 10:21:07 +0000305 obj.flags = FLAT_BINDER_FLAG_ACCEPTS_FDS;
Steven Morelandf0212002018-12-26 13:59:23 -0800306 if (local->isRequestingSid()) {
307 obj.flags |= FLAT_BINDER_FLAG_TXN_SECURITY_CTX;
308 }
Steven Morelandcf03cf12020-12-04 02:58:40 +0000309 if (local->isInheritRt()) {
310 obj.flags |= FLAT_BINDER_FLAG_INHERIT_RT;
311 }
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700312 obj.hdr.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800313 obj.binder = reinterpret_cast<uintptr_t>(local->getWeakRefs());
314 obj.cookie = reinterpret_cast<uintptr_t>(local);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700315 }
316 } else {
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700317 obj.hdr.type = BINDER_TYPE_BINDER;
Steven Moreland49c27532022-03-01 10:21:07 +0000318 obj.flags = 0;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800319 obj.binder = 0;
320 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700321 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700322
Steven Morelandbf1915b2020-07-16 22:43:02 +0000323 obj.flags |= schedBits;
324
Steven Moreland34b48cb2020-12-01 22:45:38 +0000325 status_t status = writeObject(obj, false);
326 if (status != OK) return status;
327
328 return finishFlattenBinder(binder);
Andrei Homescua9cca9c2022-05-03 04:48:01 +0000329#else // BINDER_WITH_KERNEL_IPC
330 LOG_ALWAYS_FATAL("Binder kernel driver disabled at build time");
331 return INVALID_OPERATION;
332#endif // BINDER_WITH_KERNEL_IPC
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700333}
334
Steven Morelanda86a3562019-08-01 23:28:34 +0000335status_t Parcel::unflattenBinder(sp<IBinder>* out) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700336{
Frederick Mayled3c595a2022-05-09 23:02:54 +0000337 if (const auto* rpcFields = maybeRpcFields()) {
Steven Moreland5623d1a2021-09-10 15:45:34 -0700338 int32_t isPresent;
339 status_t status = readInt32(&isPresent);
Steven Moreland5553ac42020-11-11 02:14:45 +0000340 if (status != OK) return status;
341
342 sp<IBinder> binder;
343
Steven Moreland5623d1a2021-09-10 15:45:34 -0700344 if (isPresent & 1) {
345 uint64_t addr;
346 if (status_t status = readUint64(&addr); status != OK) return status;
Frederick Mayled3c595a2022-05-09 23:02:54 +0000347 if (status_t status =
348 rpcFields->mSession->state()->onBinderEntering(rpcFields->mSession, addr,
349 &binder);
Steven Moreland7227c8a2021-06-02 00:24:32 +0000350 status != OK)
351 return status;
Frederick Mayled3c595a2022-05-09 23:02:54 +0000352 if (status_t status =
353 rpcFields->mSession->state()->flushExcessBinderRefs(rpcFields->mSession,
354 addr, binder);
Steven Morelandd8083312021-09-22 13:37:10 -0700355 status != OK)
356 return status;
Steven Moreland5553ac42020-11-11 02:14:45 +0000357 }
358
359 return finishUnflattenBinder(binder, out);
360 }
361
Andrei Homescua9cca9c2022-05-03 04:48:01 +0000362#ifdef BINDER_WITH_KERNEL_IPC
Steven Morelanda86a3562019-08-01 23:28:34 +0000363 const flat_binder_object* flat = readObject(false);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700364
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700365 if (flat) {
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700366 switch (flat->hdr.type) {
Steven Morelanda86a3562019-08-01 23:28:34 +0000367 case BINDER_TYPE_BINDER: {
Steven Moreland1a3a8ef2021-04-02 02:52:46 +0000368 sp<IBinder> binder =
369 sp<IBinder>::fromExisting(reinterpret_cast<IBinder*>(flat->cookie));
Steven Morelanda86a3562019-08-01 23:28:34 +0000370 return finishUnflattenBinder(binder, out);
371 }
372 case BINDER_TYPE_HANDLE: {
373 sp<IBinder> binder =
374 ProcessState::self()->getStrongProxyForHandle(flat->handle);
375 return finishUnflattenBinder(binder, out);
376 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700377 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700378 }
379 return BAD_TYPE;
Andrei Homescua9cca9c2022-05-03 04:48:01 +0000380#else // BINDER_WITH_KERNEL_IPC
381 LOG_ALWAYS_FATAL("Binder kernel driver disabled at build time");
382 return INVALID_OPERATION;
383#endif // BINDER_WITH_KERNEL_IPC
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700384}
385
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700386// ---------------------------------------------------------------------------
387
388Parcel::Parcel()
389{
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800390 LOG_ALLOC("Parcel %p: constructing", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700391 initState();
392}
393
394Parcel::~Parcel()
395{
396 freeDataNoInit();
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800397 LOG_ALLOC("Parcel %p: destroyed", this);
398}
399
400size_t Parcel::getGlobalAllocSize() {
Jeff Sharkey8994c182020-09-11 12:07:10 -0600401 return gParcelGlobalAllocSize.load();
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800402}
403
404size_t Parcel::getGlobalAllocCount() {
Jeff Sharkey8994c182020-09-11 12:07:10 -0600405 return gParcelGlobalAllocCount.load();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700406}
407
408const uint8_t* Parcel::data() const
409{
410 return mData;
411}
412
413size_t Parcel::dataSize() const
414{
415 return (mDataSize > mDataPos ? mDataSize : mDataPos);
416}
417
Pawan Waghd886a442023-01-21 02:16:38 +0000418size_t Parcel::dataBufferSize() const {
419 return mDataSize;
420}
421
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700422size_t Parcel::dataAvail() const
423{
Nick Kralevichcfe27de2015-09-16 09:49:15 -0700424 size_t result = dataSize() - dataPosition();
425 if (result > INT32_MAX) {
Steven Moreland6adf33c2019-09-25 13:18:09 -0700426 LOG_ALWAYS_FATAL("result too big: %zu", result);
Nick Kralevichcfe27de2015-09-16 09:49:15 -0700427 }
428 return result;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700429}
430
431size_t Parcel::dataPosition() const
432{
433 return mDataPos;
434}
435
436size_t Parcel::dataCapacity() const
437{
438 return mDataCapacity;
439}
440
441status_t Parcel::setDataSize(size_t size)
442{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700443 if (size > INT32_MAX) {
444 // don't accept size_t values which may have come from an
445 // inadvertent conversion from a negative int.
446 return BAD_VALUE;
447 }
448
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700449 status_t err;
450 err = continueWrite(size);
451 if (err == NO_ERROR) {
452 mDataSize = size;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700453 ALOGV("setDataSize Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700454 }
455 return err;
456}
457
458void Parcel::setDataPosition(size_t pos) const
459{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700460 if (pos > INT32_MAX) {
461 // don't accept size_t values which may have come from an
462 // inadvertent conversion from a negative int.
Steven Moreland6adf33c2019-09-25 13:18:09 -0700463 LOG_ALWAYS_FATAL("pos too big: %zu", pos);
Nick Kralevichb6b14232015-04-02 09:36:02 -0700464 }
465
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700466 mDataPos = pos;
Frederick Mayled3c595a2022-05-09 23:02:54 +0000467 if (const auto* kernelFields = maybeKernelFields()) {
468 kernelFields->mNextObjectHint = 0;
469 kernelFields->mObjectsSorted = false;
470 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700471}
472
473status_t Parcel::setDataCapacity(size_t size)
474{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700475 if (size > INT32_MAX) {
476 // don't accept size_t values which may have come from an
477 // inadvertent conversion from a negative int.
478 return BAD_VALUE;
479 }
480
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -0700481 if (size > mDataCapacity) return continueWrite(size);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700482 return NO_ERROR;
483}
484
485status_t Parcel::setData(const uint8_t* buffer, size_t len)
486{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700487 if (len > INT32_MAX) {
488 // don't accept size_t values which may have come from an
489 // inadvertent conversion from a negative int.
490 return BAD_VALUE;
491 }
492
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700493 status_t err = restartWrite(len);
494 if (err == NO_ERROR) {
495 memcpy(const_cast<uint8_t*>(data()), buffer, len);
496 mDataSize = len;
Frederick Mayled3c595a2022-05-09 23:02:54 +0000497 if (auto* kernelFields = maybeKernelFields()) {
498 kernelFields->mFdsKnown = false;
499 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700500 }
501 return err;
502}
503
Frederick Mayled3c595a2022-05-09 23:02:54 +0000504status_t Parcel::appendFrom(const Parcel* parcel, size_t offset, size_t len) {
505 if (isForRpc() != parcel->isForRpc()) {
Steven Moreland2034eff2021-10-13 11:24:35 -0700506 ALOGE("Cannot append Parcel from one context to another. They may be different formats, "
507 "and objects are specific to a context.");
Steven Moreland67753c32021-04-02 18:45:19 +0000508 return BAD_TYPE;
509 }
Frederick Mayled3c595a2022-05-09 23:02:54 +0000510 if (isForRpc() && maybeRpcFields()->mSession != parcel->maybeRpcFields()->mSession) {
511 ALOGE("Cannot append Parcels from different sessions");
512 return BAD_TYPE;
513 }
Steven Moreland67753c32021-04-02 18:45:19 +0000514
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700515 status_t err;
Frederick Mayled3c595a2022-05-09 23:02:54 +0000516 const uint8_t* data = parcel->mData;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700517 int startPos = mDataPos;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700518
519 if (len == 0) {
520 return NO_ERROR;
521 }
522
Nick Kralevichb6b14232015-04-02 09:36:02 -0700523 if (len > INT32_MAX) {
524 // don't accept size_t values which may have come from an
525 // inadvertent conversion from a negative int.
526 return BAD_VALUE;
527 }
528
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700529 // range checks against the source parcel size
530 if ((offset > parcel->mDataSize)
531 || (len > parcel->mDataSize)
532 || (offset + len > parcel->mDataSize)) {
533 return BAD_VALUE;
534 }
535
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -0700536 if ((mDataSize+len) > mDataCapacity) {
537 // grow data
538 err = growData(len);
539 if (err != NO_ERROR) {
540 return err;
541 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700542 }
543
544 // append data
545 memcpy(mData + mDataPos, data + offset, len);
546 mDataPos += len;
547 mDataSize += len;
548
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400549 err = NO_ERROR;
550
Frederick Mayled3c595a2022-05-09 23:02:54 +0000551 if (auto* kernelFields = maybeKernelFields()) {
Andrei Homescu88012462022-07-07 04:30:05 +0000552#ifdef BINDER_WITH_KERNEL_IPC
Frederick Mayled3c595a2022-05-09 23:02:54 +0000553 auto* otherKernelFields = parcel->maybeKernelFields();
554 LOG_ALWAYS_FATAL_IF(otherKernelFields == nullptr);
555
556 const binder_size_t* objects = otherKernelFields->mObjects;
557 size_t size = otherKernelFields->mObjectsSize;
558 // Count objects in range
559 int firstIndex = -1, lastIndex = -2;
560 for (int i = 0; i < (int)size; i++) {
561 size_t off = objects[i];
562 if ((off >= offset) && (off + sizeof(flat_binder_object) <= offset + len)) {
563 if (firstIndex == -1) {
564 firstIndex = i;
565 }
566 lastIndex = i;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700567 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700568 }
Frederick Mayled3c595a2022-05-09 23:02:54 +0000569 int numObjects = lastIndex - firstIndex + 1;
570 if (numObjects > 0) {
571 const sp<ProcessState> proc(ProcessState::self());
572 // grow objects
573 if (kernelFields->mObjectsCapacity < kernelFields->mObjectsSize + numObjects) {
574 if ((size_t)numObjects > SIZE_MAX - kernelFields->mObjectsSize)
575 return NO_MEMORY; // overflow
576 if (kernelFields->mObjectsSize + numObjects > SIZE_MAX / 3)
577 return NO_MEMORY; // overflow
578 size_t newSize = ((kernelFields->mObjectsSize + numObjects) * 3) / 2;
579 if (newSize > SIZE_MAX / sizeof(binder_size_t)) return NO_MEMORY; // overflow
580 binder_size_t* objects = (binder_size_t*)realloc(kernelFields->mObjects,
581 newSize * sizeof(binder_size_t));
582 if (objects == (binder_size_t*)nullptr) {
583 return NO_MEMORY;
584 }
585 kernelFields->mObjects = objects;
586 kernelFields->mObjectsCapacity = newSize;
587 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700588
Frederick Mayled3c595a2022-05-09 23:02:54 +0000589 // append and acquire objects
590 int idx = kernelFields->mObjectsSize;
591 for (int i = firstIndex; i <= lastIndex; i++) {
592 size_t off = objects[i] - offset + startPos;
593 kernelFields->mObjects[idx++] = off;
594 kernelFields->mObjectsSize++;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700595
Frederick Mayled3c595a2022-05-09 23:02:54 +0000596 flat_binder_object* flat = reinterpret_cast<flat_binder_object*>(mData + off);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700597
Frederick Mayled3c595a2022-05-09 23:02:54 +0000598 if (flat->hdr.type == BINDER_TYPE_FD) {
599 // If this is a file descriptor, we need to dup it so the
600 // new Parcel now owns its own fd, and can declare that we
601 // officially know we have fds.
602 flat->handle = fcntl(flat->handle, F_DUPFD_CLOEXEC, 0);
603 flat->cookie = 1;
604 kernelFields->mHasFds = kernelFields->mFdsKnown = true;
605 if (!mAllowFds) {
606 err = FDS_NOT_ALLOWED;
607 }
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400608 }
Steven Moreland02f736f2023-06-22 23:03:57 +0000609
610 acquire_object(proc, *flat, this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700611 }
612 }
Andrei Homescu88012462022-07-07 04:30:05 +0000613#else
614 LOG_ALWAYS_FATAL("Binder kernel driver disabled at build time");
615 return INVALID_OPERATION;
616#endif // BINDER_WITH_KERNEL_IPC
Frederick Mayle69a0c992022-05-26 20:38:39 +0000617 } else {
618 auto* rpcFields = maybeRpcFields();
619 LOG_ALWAYS_FATAL_IF(rpcFields == nullptr);
620 auto* otherRpcFields = parcel->maybeRpcFields();
621 if (otherRpcFields == nullptr) {
622 return BAD_TYPE;
623 }
624 if (rpcFields->mSession != otherRpcFields->mSession) {
625 return BAD_TYPE;
626 }
627
628 const size_t savedDataPos = mDataPos;
629 base::ScopeGuard scopeGuard = [&]() { mDataPos = savedDataPos; };
630
631 rpcFields->mObjectPositions.reserve(otherRpcFields->mObjectPositions.size());
632 if (otherRpcFields->mFds != nullptr) {
633 if (rpcFields->mFds == nullptr) {
634 rpcFields->mFds = std::make_unique<decltype(rpcFields->mFds)::element_type>();
635 }
636 rpcFields->mFds->reserve(otherRpcFields->mFds->size());
637 }
638 for (size_t i = 0; i < otherRpcFields->mObjectPositions.size(); i++) {
639 const binder_size_t objPos = otherRpcFields->mObjectPositions[i];
640 if (offset <= objPos && objPos < offset + len) {
641 size_t newDataPos = objPos - offset + startPos;
642 rpcFields->mObjectPositions.push_back(newDataPos);
643
644 mDataPos = newDataPos;
645 int32_t objectType;
646 if (status_t status = readInt32(&objectType); status != OK) {
647 return status;
648 }
649 if (objectType != RpcFields::TYPE_NATIVE_FILE_DESCRIPTOR) {
650 continue;
651 }
652
653 if (!mAllowFds) {
654 return FDS_NOT_ALLOWED;
655 }
656
657 // Read FD, duplicate, and add to list.
658 int32_t fdIndex;
659 if (status_t status = readInt32(&fdIndex); status != OK) {
660 return status;
661 }
Andrei Homescufc221502022-10-08 03:51:17 +0000662 int oldFd = toRawFd(otherRpcFields->mFds->at(fdIndex));
Frederick Mayle69a0c992022-05-26 20:38:39 +0000663 // To match kernel binder behavior, we always dup, even if the
664 // FD was unowned in the source parcel.
Andrei Homescufc221502022-10-08 03:51:17 +0000665 int newFd = -1;
666 if (status_t status = dupFileDescriptor(oldFd, &newFd); status != OK) {
667 ALOGW("Failed to duplicate file descriptor %d: %s", oldFd, strerror(-status));
668 }
669 rpcFields->mFds->emplace_back(base::unique_fd(newFd));
Frederick Mayle69a0c992022-05-26 20:38:39 +0000670 // Fixup the index in the data.
671 mDataPos = newDataPos + 4;
672 if (status_t status = writeInt32(rpcFields->mFds->size() - 1); status != OK) {
673 return status;
674 }
675 }
676 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700677 }
678
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400679 return err;
680}
681
Dianne Hackborn15feb9b2017-04-10 15:34:35 -0700682int Parcel::compareData(const Parcel& other) {
683 size_t size = dataSize();
684 if (size != other.dataSize()) {
685 return size < other.dataSize() ? -1 : 1;
686 }
687 return memcmp(data(), other.data(), size);
688}
689
Bernardo Rufino897b1652021-10-08 10:30:20 +0100690status_t Parcel::compareDataInRange(size_t thisOffset, const Parcel& other, size_t otherOffset,
691 size_t len, int* result) const {
692 if (len > INT32_MAX || thisOffset > INT32_MAX || otherOffset > INT32_MAX) {
693 // Don't accept size_t values which may have come from an inadvertent conversion from a
694 // negative int.
695 return BAD_VALUE;
696 }
697 size_t thisLimit;
698 if (__builtin_add_overflow(thisOffset, len, &thisLimit) || thisLimit > mDataSize) {
699 return BAD_VALUE;
700 }
701 size_t otherLimit;
702 if (__builtin_add_overflow(otherOffset, len, &otherLimit) || otherLimit > other.mDataSize) {
703 return BAD_VALUE;
704 }
705 *result = memcmp(data() + thisOffset, other.data() + otherOffset, len);
706 return NO_ERROR;
707}
708
Jeff Brown13b16042014-11-11 16:44:25 -0800709bool Parcel::allowFds() const
710{
711 return mAllowFds;
712}
713
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700714bool Parcel::pushAllowFds(bool allowFds)
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400715{
716 const bool origValue = mAllowFds;
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700717 if (!allowFds) {
718 mAllowFds = false;
719 }
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400720 return origValue;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700721}
722
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700723void Parcel::restoreAllowFds(bool lastValue)
724{
725 mAllowFds = lastValue;
726}
727
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700728bool Parcel::hasFileDescriptors() const
729{
Frederick Mayled3c595a2022-05-09 23:02:54 +0000730 if (const auto* rpcFields = maybeRpcFields()) {
Frederick Mayle69a0c992022-05-26 20:38:39 +0000731 return rpcFields->mFds != nullptr && !rpcFields->mFds->empty();
Frederick Mayled3c595a2022-05-09 23:02:54 +0000732 }
733 auto* kernelFields = maybeKernelFields();
734 if (!kernelFields->mFdsKnown) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700735 scanForFds();
736 }
Frederick Mayled3c595a2022-05-09 23:02:54 +0000737 return kernelFields->mHasFds;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700738}
739
Steven Moreland4b2f18d2022-03-24 00:32:25 +0000740std::vector<sp<IBinder>> Parcel::debugReadAllStrongBinders() const {
741 std::vector<sp<IBinder>> ret;
742
Andrei Homescua9cca9c2022-05-03 04:48:01 +0000743#ifdef BINDER_WITH_KERNEL_IPC
Frederick Mayled3c595a2022-05-09 23:02:54 +0000744 const auto* kernelFields = maybeKernelFields();
745 if (kernelFields == nullptr) {
746 return ret;
747 }
748
Steven Moreland4b2f18d2022-03-24 00:32:25 +0000749 size_t initPosition = dataPosition();
Frederick Mayled3c595a2022-05-09 23:02:54 +0000750 for (size_t i = 0; i < kernelFields->mObjectsSize; i++) {
751 binder_size_t offset = kernelFields->mObjects[i];
Steven Moreland4b2f18d2022-03-24 00:32:25 +0000752 const flat_binder_object* flat =
753 reinterpret_cast<const flat_binder_object*>(mData + offset);
754 if (flat->hdr.type != BINDER_TYPE_BINDER) continue;
755
756 setDataPosition(offset);
757
758 sp<IBinder> binder = readStrongBinder();
759 if (binder != nullptr) ret.push_back(binder);
760 }
761
762 setDataPosition(initPosition);
Andrei Homescua9cca9c2022-05-03 04:48:01 +0000763#endif // BINDER_WITH_KERNEL_IPC
764
Steven Moreland4b2f18d2022-03-24 00:32:25 +0000765 return ret;
766}
767
768std::vector<int> Parcel::debugReadAllFileDescriptors() const {
769 std::vector<int> ret;
770
Frederick Mayle69a0c992022-05-26 20:38:39 +0000771 if (const auto* kernelFields = maybeKernelFields()) {
Andrei Homescua9cca9c2022-05-03 04:48:01 +0000772#ifdef BINDER_WITH_KERNEL_IPC
Frederick Mayle69a0c992022-05-26 20:38:39 +0000773 size_t initPosition = dataPosition();
774 for (size_t i = 0; i < kernelFields->mObjectsSize; i++) {
775 binder_size_t offset = kernelFields->mObjects[i];
776 const flat_binder_object* flat =
777 reinterpret_cast<const flat_binder_object*>(mData + offset);
778 if (flat->hdr.type != BINDER_TYPE_FD) continue;
779
780 setDataPosition(offset);
781
782 int fd = readFileDescriptor();
783 LOG_ALWAYS_FATAL_IF(fd == -1);
784 ret.push_back(fd);
785 }
786 setDataPosition(initPosition);
Andrei Homescua9cca9c2022-05-03 04:48:01 +0000787#else
788 LOG_ALWAYS_FATAL("Binder kernel driver disabled at build time");
789#endif
Frederick Mayle69a0c992022-05-26 20:38:39 +0000790 } else if (const auto* rpcFields = maybeRpcFields(); rpcFields && rpcFields->mFds) {
791 for (const auto& fd : *rpcFields->mFds) {
792 ret.push_back(toRawFd(fd));
793 }
Frederick Mayled3c595a2022-05-09 23:02:54 +0000794 }
795
Steven Moreland4b2f18d2022-03-24 00:32:25 +0000796 return ret;
797}
798
Bernardo Rufinobbbd88d2021-10-15 14:54:30 +0100799status_t Parcel::hasFileDescriptorsInRange(size_t offset, size_t len, bool* result) const {
Bernardo Rufino22092af2021-10-07 14:09:24 +0100800 if (len > INT32_MAX || offset > INT32_MAX) {
801 // Don't accept size_t values which may have come from an inadvertent conversion from a
802 // negative int.
803 return BAD_VALUE;
804 }
Bernardo Rufinobbbd88d2021-10-15 14:54:30 +0100805 size_t limit;
806 if (__builtin_add_overflow(offset, len, &limit) || limit > mDataSize) {
Bernardo Rufino22092af2021-10-07 14:09:24 +0100807 return BAD_VALUE;
808 }
Bernardo Rufinobbbd88d2021-10-15 14:54:30 +0100809 *result = false;
Frederick Mayle69a0c992022-05-26 20:38:39 +0000810 if (const auto* kernelFields = maybeKernelFields()) {
Andrei Homescua9cca9c2022-05-03 04:48:01 +0000811#ifdef BINDER_WITH_KERNEL_IPC
Frederick Mayle69a0c992022-05-26 20:38:39 +0000812 for (size_t i = 0; i < kernelFields->mObjectsSize; i++) {
813 size_t pos = kernelFields->mObjects[i];
814 if (pos < offset) continue;
815 if (pos + sizeof(flat_binder_object) > offset + len) {
816 if (kernelFields->mObjectsSorted) {
817 break;
818 } else {
819 continue;
820 }
821 }
822 const flat_binder_object* flat =
823 reinterpret_cast<const flat_binder_object*>(mData + pos);
824 if (flat->hdr.type == BINDER_TYPE_FD) {
825 *result = true;
Frederick Mayled3c595a2022-05-09 23:02:54 +0000826 break;
Frederick Mayled3c595a2022-05-09 23:02:54 +0000827 }
Bernardo Rufino22092af2021-10-07 14:09:24 +0100828 }
Andrei Homescua9cca9c2022-05-03 04:48:01 +0000829#else
830 LOG_ALWAYS_FATAL("Binder kernel driver disabled at build time");
831 return INVALID_OPERATION;
832#endif // BINDER_WITH_KERNEL_IPC
Frederick Mayle69a0c992022-05-26 20:38:39 +0000833 } else if (const auto* rpcFields = maybeRpcFields()) {
834 for (uint32_t pos : rpcFields->mObjectPositions) {
835 if (offset <= pos && pos < limit) {
836 const auto* type = reinterpret_cast<const RpcFields::ObjectType*>(mData + pos);
837 if (*type == RpcFields::TYPE_NATIVE_FILE_DESCRIPTOR) {
838 *result = true;
839 break;
840 }
841 }
Bernardo Rufino22092af2021-10-07 14:09:24 +0100842 }
843 }
Bernardo Rufinobbbd88d2021-10-15 14:54:30 +0100844 return NO_ERROR;
Bernardo Rufino22092af2021-10-07 14:09:24 +0100845}
846
Steven Morelandf183fdd2020-10-27 00:12:12 +0000847void Parcel::markSensitive() const
848{
849 mDeallocZero = true;
850}
851
Steven Moreland5553ac42020-11-11 02:14:45 +0000852void Parcel::markForBinder(const sp<IBinder>& binder) {
Steven Moreland1fda67b2021-04-02 18:35:50 +0000853 LOG_ALWAYS_FATAL_IF(mData != nullptr, "format must be set before data is written");
854
Steven Moreland5553ac42020-11-11 02:14:45 +0000855 if (binder && binder->remoteBinder() && binder->remoteBinder()->isRpcBinder()) {
Steven Moreland99157622021-09-13 16:27:34 -0700856 markForRpc(binder->remoteBinder()->getPrivateAccessor().rpcSession());
Steven Moreland5553ac42020-11-11 02:14:45 +0000857 }
858}
859
Steven Morelandc9939062021-05-05 17:57:41 +0000860void Parcel::markForRpc(const sp<RpcSession>& session) {
Steven Moreland1fda67b2021-04-02 18:35:50 +0000861 LOG_ALWAYS_FATAL_IF(mData != nullptr && mOwner == nullptr,
862 "format must be set before data is written OR on IPC data");
863
Frederick Mayled3c595a2022-05-09 23:02:54 +0000864 mVariantFields.emplace<RpcFields>(session);
Steven Moreland5553ac42020-11-11 02:14:45 +0000865}
866
867bool Parcel::isForRpc() const {
Frederick Mayled3c595a2022-05-09 23:02:54 +0000868 return std::holds_alternative<RpcFields>(mVariantFields);
Steven Moreland5553ac42020-11-11 02:14:45 +0000869}
870
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000871void Parcel::updateWorkSourceRequestHeaderPosition() const {
Frederick Mayled3c595a2022-05-09 23:02:54 +0000872 auto* kernelFields = maybeKernelFields();
873 if (kernelFields == nullptr) {
874 return;
875 }
876
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000877 // Only update the request headers once. We only want to point
878 // to the first headers read/written.
Frederick Mayled3c595a2022-05-09 23:02:54 +0000879 if (!kernelFields->mRequestHeaderPresent) {
880 kernelFields->mWorkSourceRequestHeaderPosition = dataPosition();
881 kernelFields->mRequestHeaderPresent = true;
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000882 }
883}
884
Andrei Homescua9cca9c2022-05-03 04:48:01 +0000885#ifdef BINDER_WITH_KERNEL_IPC
Steven Morelandb6c7e222021-02-18 19:20:14 +0000886#if defined(__ANDROID_VNDK__)
Steven Morelandd70160f2019-07-23 10:20:38 -0700887constexpr int32_t kHeader = B_PACK_CHARS('V', 'N', 'D', 'R');
Yifan Hong70786532021-10-20 21:41:18 -0700888#elif defined(__ANDROID_RECOVERY__)
889constexpr int32_t kHeader = B_PACK_CHARS('R', 'E', 'C', 'O');
Steven Morelandd70160f2019-07-23 10:20:38 -0700890#else
891constexpr int32_t kHeader = B_PACK_CHARS('S', 'Y', 'S', 'T');
892#endif
Andrei Homescua9cca9c2022-05-03 04:48:01 +0000893#endif // BINDER_WITH_KERNEL_IPC
Steven Morelandd70160f2019-07-23 10:20:38 -0700894
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700895// Write RPC headers. (previously just the interface token)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700896status_t Parcel::writeInterfaceToken(const String16& interface)
897{
Tomasz Wasilczyk0bfea2d2023-08-11 00:06:51 +0000898 return writeInterfaceToken(interface.c_str(), interface.size());
Steven Morelanddbc76c72020-10-01 18:02:48 +0000899}
900
901status_t Parcel::writeInterfaceToken(const char16_t* str, size_t len) {
Frederick Mayled3c595a2022-05-09 23:02:54 +0000902 if (auto* kernelFields = maybeKernelFields()) {
Andrei Homescua9cca9c2022-05-03 04:48:01 +0000903#ifdef BINDER_WITH_KERNEL_IPC
Steven Moreland3af936a2021-03-26 03:05:38 +0000904 const IPCThreadState* threadState = IPCThreadState::self();
905 writeInt32(threadState->getStrictModePolicy() | STRICT_MODE_PENALTY_GATHER);
906 updateWorkSourceRequestHeaderPosition();
907 writeInt32(threadState->shouldPropagateWorkSource() ? threadState->getCallingWorkSourceUid()
908 : IPCThreadState::kUnsetWorkSource);
909 writeInt32(kHeader);
Andrei Homescua9cca9c2022-05-03 04:48:01 +0000910#else // BINDER_WITH_KERNEL_IPC
911 LOG_ALWAYS_FATAL("Binder kernel driver disabled at build time");
912 return INVALID_OPERATION;
913#endif // BINDER_WITH_KERNEL_IPC
Steven Moreland3af936a2021-03-26 03:05:38 +0000914 }
Steven Morelanddbc76c72020-10-01 18:02:48 +0000915
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700916 // currently the interface identification token is just its name as a string
Steven Morelanddbc76c72020-10-01 18:02:48 +0000917 return writeString16(str, len);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700918}
919
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000920bool Parcel::replaceCallingWorkSourceUid(uid_t uid)
921{
Frederick Mayled3c595a2022-05-09 23:02:54 +0000922 auto* kernelFields = maybeKernelFields();
923 if (kernelFields == nullptr) {
924 return false;
925 }
926 if (!kernelFields->mRequestHeaderPresent) {
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000927 return false;
928 }
929
930 const size_t initialPosition = dataPosition();
Frederick Mayled3c595a2022-05-09 23:02:54 +0000931 setDataPosition(kernelFields->mWorkSourceRequestHeaderPosition);
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000932 status_t err = writeInt32(uid);
933 setDataPosition(initialPosition);
934 return err == NO_ERROR;
935}
936
Steven Morelandf1b1e492019-05-06 15:05:13 -0700937uid_t Parcel::readCallingWorkSourceUid() const
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000938{
Frederick Mayled3c595a2022-05-09 23:02:54 +0000939 auto* kernelFields = maybeKernelFields();
940 if (kernelFields == nullptr) {
941 return false;
942 }
943 if (!kernelFields->mRequestHeaderPresent) {
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000944 return IPCThreadState::kUnsetWorkSource;
945 }
946
947 const size_t initialPosition = dataPosition();
Frederick Mayled3c595a2022-05-09 23:02:54 +0000948 setDataPosition(kernelFields->mWorkSourceRequestHeaderPosition);
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000949 uid_t uid = readInt32();
950 setDataPosition(initialPosition);
951 return uid;
952}
953
Mathias Agopian83c04462009-05-22 19:00:22 -0700954bool Parcel::checkInterface(IBinder* binder) const
955{
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700956 return enforceInterface(binder->getInterfaceDescriptor());
Mathias Agopian83c04462009-05-22 19:00:22 -0700957}
958
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700959bool Parcel::enforceInterface(const String16& interface,
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700960 IPCThreadState* threadState) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700961{
Tomasz Wasilczyk0bfea2d2023-08-11 00:06:51 +0000962 return enforceInterface(interface.c_str(), interface.size(), threadState);
Daniel Colascione0bb330d2019-10-29 16:44:19 -0700963}
964
965bool Parcel::enforceInterface(const char16_t* interface,
966 size_t len,
967 IPCThreadState* threadState) const
968{
Frederick Mayled3c595a2022-05-09 23:02:54 +0000969 if (auto* kernelFields = maybeKernelFields()) {
Andrei Homescua9cca9c2022-05-03 04:48:01 +0000970#ifdef BINDER_WITH_KERNEL_IPC
Steven Moreland3af936a2021-03-26 03:05:38 +0000971 // StrictModePolicy.
972 int32_t strictPolicy = readInt32();
973 if (threadState == nullptr) {
974 threadState = IPCThreadState::self();
975 }
976 if ((threadState->getLastTransactionBinderFlags() & IBinder::FLAG_ONEWAY) != 0) {
977 // For one-way calls, the callee is running entirely
978 // disconnected from the caller, so disable StrictMode entirely.
979 // Not only does disk/network usage not impact the caller, but
980 // there's no way to communicate back violations anyway.
981 threadState->setStrictModePolicy(0);
982 } else {
983 threadState->setStrictModePolicy(strictPolicy);
984 }
985 // WorkSource.
986 updateWorkSourceRequestHeaderPosition();
987 int32_t workSource = readInt32();
988 threadState->setCallingWorkSourceUidWithoutPropagation(workSource);
989 // vendor header
990 int32_t header = readInt32();
Steven Moreland3a667492023-06-02 21:41:39 +0000991
992 // fuzzers skip this check, because it is for protecting the underlying ABI, but
993 // we don't want it to reduce our coverage
994 if (header != kHeader && !mServiceFuzzing) {
Steven Moreland3af936a2021-03-26 03:05:38 +0000995 ALOGE("Expecting header 0x%x but found 0x%x. Mixing copies of libbinder?", kHeader,
996 header);
997 return false;
998 }
Andrei Homescua9cca9c2022-05-03 04:48:01 +0000999#else // BINDER_WITH_KERNEL_IPC
1000 LOG_ALWAYS_FATAL("Binder kernel driver disabled at build time");
1001 (void)threadState;
1002 return false;
1003#endif // BINDER_WITH_KERNEL_IPC
Brad Fitzpatricka877cd82010-07-07 16:06:39 -07001004 }
Steven Moreland3af936a2021-03-26 03:05:38 +00001005
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +01001006 // Interface descriptor.
Daniel Colascione0bb330d2019-10-29 16:44:19 -07001007 size_t parcel_interface_len;
1008 const char16_t* parcel_interface = readString16Inplace(&parcel_interface_len);
1009 if (len == parcel_interface_len &&
1010 (!len || !memcmp(parcel_interface, interface, len * sizeof (char16_t)))) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001011 return true;
1012 } else {
Steven Morelande99d8822023-06-02 21:56:39 +00001013 if (mServiceFuzzing) {
1014 // ignore. Theoretically, this could cause a few false positives, because
1015 // people could assume things about getInterfaceDescriptor if they pass
1016 // this point, but it would be extremely fragile. It's more important that
1017 // we fuzz with the above things read from the Parcel.
1018 return true;
1019 } else {
Steven Moreland3a667492023-06-02 21:41:39 +00001020 ALOGW("**** enforceInterface() expected '%s' but read '%s'",
Tomasz Wasilczyk0bfea2d2023-08-11 00:06:51 +00001021 String8(interface, len).c_str(),
1022 String8(parcel_interface, parcel_interface_len).c_str());
Steven Morelande99d8822023-06-02 21:56:39 +00001023 return false;
Steven Moreland3a667492023-06-02 21:41:39 +00001024 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001025 }
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -07001026}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001027
Pawan Wagh104654a2022-11-15 21:18:42 +00001028void Parcel::setEnforceNoDataAvail(bool enforceNoDataAvail) {
1029 mEnforceNoDataAvail = enforceNoDataAvail;
1030}
1031
Steven Moreland3a667492023-06-02 21:41:39 +00001032void Parcel::setServiceFuzzing() {
1033 mServiceFuzzing = true;
1034}
1035
Steven Moreland418914a2023-06-28 21:47:14 +00001036bool Parcel::isServiceFuzzing() const {
1037 return mServiceFuzzing;
1038}
1039
Jooyung Hand23f9502021-12-23 14:39:57 +09001040binder::Status Parcel::enforceNoDataAvail() const {
Pawan Wagh104654a2022-11-15 21:18:42 +00001041 if (!mEnforceNoDataAvail) {
1042 return binder::Status::ok();
1043 }
1044
Jooyung Hand23f9502021-12-23 14:39:57 +09001045 const auto n = dataAvail();
1046 if (n == 0) {
1047 return binder::Status::ok();
1048 }
1049 return binder::Status::
1050 fromExceptionCode(binder::Status::Exception::EX_BAD_PARCELABLE,
1051 String8::format("Parcel data not fully consumed, unread size: %zu",
1052 n));
1053}
1054
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001055size_t Parcel::objectsCount() const
1056{
Frederick Mayled3c595a2022-05-09 23:02:54 +00001057 if (const auto* kernelFields = maybeKernelFields()) {
1058 return kernelFields->mObjectsSize;
1059 }
1060 return 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001061}
1062
1063status_t Parcel::errorCheck() const
1064{
1065 return mError;
1066}
1067
1068void Parcel::setError(status_t err)
1069{
1070 mError = err;
1071}
1072
1073status_t Parcel::finishWrite(size_t len)
1074{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001075 if (len > INT32_MAX) {
1076 // don't accept size_t values which may have come from an
1077 // inadvertent conversion from a negative int.
1078 return BAD_VALUE;
1079 }
1080
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001081 //printf("Finish write of %d\n", len);
1082 mDataPos += len;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001083 ALOGV("finishWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001084 if (mDataPos > mDataSize) {
1085 mDataSize = mDataPos;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001086 ALOGV("finishWrite Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001087 }
1088 //printf("New pos=%d, size=%d\n", mDataPos, mDataSize);
1089 return NO_ERROR;
1090}
1091
1092status_t Parcel::writeUnpadded(const void* data, size_t len)
1093{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001094 if (len > INT32_MAX) {
1095 // don't accept size_t values which may have come from an
1096 // inadvertent conversion from a negative int.
1097 return BAD_VALUE;
1098 }
1099
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001100 size_t end = mDataPos + len;
1101 if (end < mDataPos) {
1102 // integer overflow
1103 return BAD_VALUE;
1104 }
1105
1106 if (end <= mDataCapacity) {
1107restart_write:
1108 memcpy(mData+mDataPos, data, len);
1109 return finishWrite(len);
1110 }
1111
1112 status_t err = growData(len);
1113 if (err == NO_ERROR) goto restart_write;
1114 return err;
1115}
1116
1117status_t Parcel::write(const void* data, size_t len)
1118{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001119 if (len > INT32_MAX) {
1120 // don't accept size_t values which may have come from an
1121 // inadvertent conversion from a negative int.
1122 return BAD_VALUE;
1123 }
1124
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001125 void* const d = writeInplace(len);
1126 if (d) {
1127 memcpy(d, data, len);
1128 return NO_ERROR;
1129 }
1130 return mError;
1131}
1132
1133void* Parcel::writeInplace(size_t len)
1134{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001135 if (len > INT32_MAX) {
1136 // don't accept size_t values which may have come from an
1137 // inadvertent conversion from a negative int.
Yi Kong91635562018-06-07 14:38:36 -07001138 return nullptr;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001139 }
1140
1141 const size_t padded = pad_size(len);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001142
Khalid Ramadanb3d817b2021-11-18 07:02:50 +00001143 // check for integer overflow
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001144 if (mDataPos+padded < mDataPos) {
Yi Kong91635562018-06-07 14:38:36 -07001145 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001146 }
1147
1148 if ((mDataPos+padded) <= mDataCapacity) {
1149restart_write:
1150 //printf("Writing %ld bytes, padded to %ld\n", len, padded);
1151 uint8_t* const data = mData+mDataPos;
1152
1153 // Need to pad at end?
1154 if (padded != len) {
1155#if BYTE_ORDER == BIG_ENDIAN
1156 static const uint32_t mask[4] = {
1157 0x00000000, 0xffffff00, 0xffff0000, 0xff000000
1158 };
1159#endif
1160#if BYTE_ORDER == LITTLE_ENDIAN
1161 static const uint32_t mask[4] = {
1162 0x00000000, 0x00ffffff, 0x0000ffff, 0x000000ff
1163 };
1164#endif
1165 //printf("Applying pad mask: %p to %p\n", (void*)mask[padded-len],
1166 // *reinterpret_cast<void**>(data+padded-4));
1167 *reinterpret_cast<uint32_t*>(data+padded-4) &= mask[padded-len];
1168 }
1169
1170 finishWrite(padded);
1171 return data;
1172 }
1173
1174 status_t err = growData(padded);
1175 if (err == NO_ERROR) goto restart_write;
Yi Kong91635562018-06-07 14:38:36 -07001176 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001177}
1178
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001179status_t Parcel::writeUtf8AsUtf16(const std::string& str) {
1180 const uint8_t* strData = (uint8_t*)str.data();
1181 const size_t strLen= str.length();
1182 const ssize_t utf16Len = utf8_to_utf16_length(strData, strLen);
Sergio Girof4607432016-07-21 14:46:35 +01001183 if (utf16Len < 0 || utf16Len > std::numeric_limits<int32_t>::max()) {
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001184 return BAD_VALUE;
1185 }
1186
1187 status_t err = writeInt32(utf16Len);
1188 if (err) {
1189 return err;
1190 }
1191
1192 // Allocate enough bytes to hold our converted string and its terminating NULL.
1193 void* dst = writeInplace((utf16Len + 1) * sizeof(char16_t));
1194 if (!dst) {
1195 return NO_MEMORY;
1196 }
1197
Sergio Girof4607432016-07-21 14:46:35 +01001198 utf8_to_utf16(strData, strLen, (char16_t*)dst, (size_t) utf16Len + 1);
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001199
1200 return NO_ERROR;
1201}
1202
Jooyung Han9fcc4ef2020-01-23 12:45:10 +09001203
Andy Hung49198cf2020-11-18 11:02:39 -08001204status_t Parcel::writeUtf8AsUtf16(const std::optional<std::string>& str) { return writeData(str); }
1205status_t Parcel::writeUtf8AsUtf16(const std::unique_ptr<std::string>& str) { return writeData(str); }
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001206
Andy Hung49198cf2020-11-18 11:02:39 -08001207status_t Parcel::writeString16(const std::optional<String16>& str) { return writeData(str); }
1208status_t Parcel::writeString16(const std::unique_ptr<String16>& str) { return writeData(str); }
Casey Dahlin451ff582015-10-19 18:12:18 -07001209
Andy Hung49198cf2020-11-18 11:02:39 -08001210status_t Parcel::writeByteVector(const std::vector<int8_t>& val) { return writeData(val); }
1211status_t Parcel::writeByteVector(const std::optional<std::vector<int8_t>>& val) { return writeData(val); }
1212status_t Parcel::writeByteVector(const std::unique_ptr<std::vector<int8_t>>& val) { return writeData(val); }
1213status_t Parcel::writeByteVector(const std::vector<uint8_t>& val) { return writeData(val); }
1214status_t Parcel::writeByteVector(const std::optional<std::vector<uint8_t>>& val) { return writeData(val); }
1215status_t Parcel::writeByteVector(const std::unique_ptr<std::vector<uint8_t>>& val){ return writeData(val); }
1216status_t Parcel::writeInt32Vector(const std::vector<int32_t>& val) { return writeData(val); }
1217status_t Parcel::writeInt32Vector(const std::optional<std::vector<int32_t>>& val) { return writeData(val); }
1218status_t Parcel::writeInt32Vector(const std::unique_ptr<std::vector<int32_t>>& val) { return writeData(val); }
1219status_t Parcel::writeInt64Vector(const std::vector<int64_t>& val) { return writeData(val); }
1220status_t Parcel::writeInt64Vector(const std::optional<std::vector<int64_t>>& val) { return writeData(val); }
1221status_t Parcel::writeInt64Vector(const std::unique_ptr<std::vector<int64_t>>& val) { return writeData(val); }
1222status_t Parcel::writeUint64Vector(const std::vector<uint64_t>& val) { return writeData(val); }
1223status_t Parcel::writeUint64Vector(const std::optional<std::vector<uint64_t>>& val) { return writeData(val); }
1224status_t Parcel::writeUint64Vector(const std::unique_ptr<std::vector<uint64_t>>& val) { return writeData(val); }
1225status_t Parcel::writeFloatVector(const std::vector<float>& val) { return writeData(val); }
1226status_t Parcel::writeFloatVector(const std::optional<std::vector<float>>& val) { return writeData(val); }
1227status_t Parcel::writeFloatVector(const std::unique_ptr<std::vector<float>>& val) { return writeData(val); }
1228status_t Parcel::writeDoubleVector(const std::vector<double>& val) { return writeData(val); }
1229status_t Parcel::writeDoubleVector(const std::optional<std::vector<double>>& val) { return writeData(val); }
1230status_t Parcel::writeDoubleVector(const std::unique_ptr<std::vector<double>>& val) { return writeData(val); }
1231status_t Parcel::writeBoolVector(const std::vector<bool>& val) { return writeData(val); }
1232status_t Parcel::writeBoolVector(const std::optional<std::vector<bool>>& val) { return writeData(val); }
1233status_t Parcel::writeBoolVector(const std::unique_ptr<std::vector<bool>>& val) { return writeData(val); }
1234status_t Parcel::writeCharVector(const std::vector<char16_t>& val) { return writeData(val); }
1235status_t Parcel::writeCharVector(const std::optional<std::vector<char16_t>>& val) { return writeData(val); }
1236status_t Parcel::writeCharVector(const std::unique_ptr<std::vector<char16_t>>& val) { return writeData(val); }
Casey Dahlin451ff582015-10-19 18:12:18 -07001237
Andy Hung49198cf2020-11-18 11:02:39 -08001238status_t Parcel::writeString16Vector(const std::vector<String16>& val) { return writeData(val); }
Casey Dahlinb9872622015-11-25 15:09:45 -08001239status_t Parcel::writeString16Vector(
Andy Hung49198cf2020-11-18 11:02:39 -08001240 const std::optional<std::vector<std::optional<String16>>>& val) { return writeData(val); }
Jooyung Han9fcc4ef2020-01-23 12:45:10 +09001241status_t Parcel::writeString16Vector(
Andy Hung49198cf2020-11-18 11:02:39 -08001242 const std::unique_ptr<std::vector<std::unique_ptr<String16>>>& val) { return writeData(val); }
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001243status_t Parcel::writeUtf8VectorAsUtf16Vector(
Andy Hung49198cf2020-11-18 11:02:39 -08001244 const std::optional<std::vector<std::optional<std::string>>>& val) { return writeData(val); }
Jooyung Han9fcc4ef2020-01-23 12:45:10 +09001245status_t Parcel::writeUtf8VectorAsUtf16Vector(
Andy Hung49198cf2020-11-18 11:02:39 -08001246 const std::unique_ptr<std::vector<std::unique_ptr<std::string>>>& val) { return writeData(val); }
1247status_t Parcel::writeUtf8VectorAsUtf16Vector(const std::vector<std::string>& val) { return writeData(val); }
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001248
Andy Hung49198cf2020-11-18 11:02:39 -08001249status_t Parcel::writeUniqueFileDescriptorVector(const std::vector<base::unique_fd>& val) { return writeData(val); }
1250status_t Parcel::writeUniqueFileDescriptorVector(const std::optional<std::vector<base::unique_fd>>& val) { return writeData(val); }
1251status_t Parcel::writeUniqueFileDescriptorVector(const std::unique_ptr<std::vector<base::unique_fd>>& val) { return writeData(val); }
1252
1253status_t Parcel::writeStrongBinderVector(const std::vector<sp<IBinder>>& val) { return writeData(val); }
1254status_t Parcel::writeStrongBinderVector(const std::optional<std::vector<sp<IBinder>>>& val) { return writeData(val); }
1255status_t Parcel::writeStrongBinderVector(const std::unique_ptr<std::vector<sp<IBinder>>>& val) { return writeData(val); }
1256
1257status_t Parcel::writeParcelable(const Parcelable& parcelable) { return writeData(parcelable); }
1258
1259status_t Parcel::readUtf8FromUtf16(std::optional<std::string>* str) const { return readData(str); }
1260status_t Parcel::readUtf8FromUtf16(std::unique_ptr<std::string>* str) const { return readData(str); }
1261
1262status_t Parcel::readString16(std::optional<String16>* pArg) const { return readData(pArg); }
1263status_t Parcel::readString16(std::unique_ptr<String16>* pArg) const { return readData(pArg); }
1264
1265status_t Parcel::readByteVector(std::vector<int8_t>* val) const { return readData(val); }
1266status_t Parcel::readByteVector(std::vector<uint8_t>* val) const { return readData(val); }
1267status_t Parcel::readByteVector(std::optional<std::vector<int8_t>>* val) const { return readData(val); }
1268status_t Parcel::readByteVector(std::unique_ptr<std::vector<int8_t>>* val) const { return readData(val); }
1269status_t Parcel::readByteVector(std::optional<std::vector<uint8_t>>* val) const { return readData(val); }
1270status_t Parcel::readByteVector(std::unique_ptr<std::vector<uint8_t>>* val) const { return readData(val); }
1271status_t Parcel::readInt32Vector(std::optional<std::vector<int32_t>>* val) const { return readData(val); }
1272status_t Parcel::readInt32Vector(std::unique_ptr<std::vector<int32_t>>* val) const { return readData(val); }
1273status_t Parcel::readInt32Vector(std::vector<int32_t>* val) const { return readData(val); }
1274status_t Parcel::readInt64Vector(std::optional<std::vector<int64_t>>* val) const { return readData(val); }
1275status_t Parcel::readInt64Vector(std::unique_ptr<std::vector<int64_t>>* val) const { return readData(val); }
1276status_t Parcel::readInt64Vector(std::vector<int64_t>* val) const { return readData(val); }
1277status_t Parcel::readUint64Vector(std::optional<std::vector<uint64_t>>* val) const { return readData(val); }
1278status_t Parcel::readUint64Vector(std::unique_ptr<std::vector<uint64_t>>* val) const { return readData(val); }
1279status_t Parcel::readUint64Vector(std::vector<uint64_t>* val) const { return readData(val); }
1280status_t Parcel::readFloatVector(std::optional<std::vector<float>>* val) const { return readData(val); }
1281status_t Parcel::readFloatVector(std::unique_ptr<std::vector<float>>* val) const { return readData(val); }
1282status_t Parcel::readFloatVector(std::vector<float>* val) const { return readData(val); }
1283status_t Parcel::readDoubleVector(std::optional<std::vector<double>>* val) const { return readData(val); }
1284status_t Parcel::readDoubleVector(std::unique_ptr<std::vector<double>>* val) const { return readData(val); }
1285status_t Parcel::readDoubleVector(std::vector<double>* val) const { return readData(val); }
1286status_t Parcel::readBoolVector(std::optional<std::vector<bool>>* val) const { return readData(val); }
1287status_t Parcel::readBoolVector(std::unique_ptr<std::vector<bool>>* val) const { return readData(val); }
1288status_t Parcel::readBoolVector(std::vector<bool>* val) const { return readData(val); }
1289status_t Parcel::readCharVector(std::optional<std::vector<char16_t>>* val) const { return readData(val); }
1290status_t Parcel::readCharVector(std::unique_ptr<std::vector<char16_t>>* val) const { return readData(val); }
1291status_t Parcel::readCharVector(std::vector<char16_t>* val) const { return readData(val); }
1292
1293status_t Parcel::readString16Vector(
1294 std::optional<std::vector<std::optional<String16>>>* val) const { return readData(val); }
1295status_t Parcel::readString16Vector(
1296 std::unique_ptr<std::vector<std::unique_ptr<String16>>>* val) const { return readData(val); }
1297status_t Parcel::readString16Vector(std::vector<String16>* val) const { return readData(val); }
1298status_t Parcel::readUtf8VectorFromUtf16Vector(
1299 std::optional<std::vector<std::optional<std::string>>>* val) const { return readData(val); }
1300status_t Parcel::readUtf8VectorFromUtf16Vector(
1301 std::unique_ptr<std::vector<std::unique_ptr<std::string>>>* val) const { return readData(val); }
1302status_t Parcel::readUtf8VectorFromUtf16Vector(std::vector<std::string>* val) const { return readData(val); }
1303
1304status_t Parcel::readUniqueFileDescriptorVector(std::optional<std::vector<base::unique_fd>>* val) const { return readData(val); }
1305status_t Parcel::readUniqueFileDescriptorVector(std::unique_ptr<std::vector<base::unique_fd>>* val) const { return readData(val); }
1306status_t Parcel::readUniqueFileDescriptorVector(std::vector<base::unique_fd>* val) const { return readData(val); }
1307
1308status_t Parcel::readStrongBinderVector(std::optional<std::vector<sp<IBinder>>>* val) const { return readData(val); }
1309status_t Parcel::readStrongBinderVector(std::unique_ptr<std::vector<sp<IBinder>>>* val) const { return readData(val); }
1310status_t Parcel::readStrongBinderVector(std::vector<sp<IBinder>>* val) const { return readData(val); }
1311
1312status_t Parcel::readParcelable(Parcelable* parcelable) const { return readData(parcelable); }
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001313
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001314status_t Parcel::writeInt32(int32_t val)
1315{
Andreas Huber84a6d042009-08-17 13:33:27 -07001316 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001317}
Dan Stoza41a0f2f2014-12-01 10:01:10 -08001318
1319status_t Parcel::writeUint32(uint32_t val)
1320{
1321 return writeAligned(val);
1322}
1323
Marco Nelissen5c0106e2013-10-16 10:57:51 -07001324status_t Parcel::writeInt32Array(size_t len, const int32_t *val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001325 if (len > INT32_MAX) {
1326 // don't accept size_t values which may have come from an
1327 // inadvertent conversion from a negative int.
1328 return BAD_VALUE;
1329 }
1330
Marco Nelissen5c0106e2013-10-16 10:57:51 -07001331 if (!val) {
Chad Brubakere59cb432015-06-30 14:03:55 -07001332 return writeInt32(-1);
Marco Nelissen5c0106e2013-10-16 10:57:51 -07001333 }
Chad Brubakere59cb432015-06-30 14:03:55 -07001334 status_t ret = writeInt32(static_cast<uint32_t>(len));
Marco Nelissen5c0106e2013-10-16 10:57:51 -07001335 if (ret == NO_ERROR) {
1336 ret = write(val, len * sizeof(*val));
1337 }
1338 return ret;
1339}
Marco Nelissenf0190bf2014-03-13 14:17:40 -07001340status_t Parcel::writeByteArray(size_t len, const uint8_t *val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001341 if (len > INT32_MAX) {
1342 // don't accept size_t values which may have come from an
1343 // inadvertent conversion from a negative int.
1344 return BAD_VALUE;
1345 }
1346
Marco Nelissenf0190bf2014-03-13 14:17:40 -07001347 if (!val) {
Chad Brubakere59cb432015-06-30 14:03:55 -07001348 return writeInt32(-1);
Marco Nelissenf0190bf2014-03-13 14:17:40 -07001349 }
Chad Brubakere59cb432015-06-30 14:03:55 -07001350 status_t ret = writeInt32(static_cast<uint32_t>(len));
Marco Nelissenf0190bf2014-03-13 14:17:40 -07001351 if (ret == NO_ERROR) {
1352 ret = write(val, len * sizeof(*val));
1353 }
1354 return ret;
1355}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001356
Casey Dahlind6848f52015-10-15 15:44:59 -07001357status_t Parcel::writeBool(bool val)
1358{
1359 return writeInt32(int32_t(val));
1360}
1361
1362status_t Parcel::writeChar(char16_t val)
1363{
1364 return writeInt32(int32_t(val));
1365}
1366
1367status_t Parcel::writeByte(int8_t val)
1368{
1369 return writeInt32(int32_t(val));
1370}
1371
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001372status_t Parcel::writeInt64(int64_t val)
1373{
Andreas Huber84a6d042009-08-17 13:33:27 -07001374 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001375}
1376
Ronghua Wu2d13afd2015-03-16 11:11:07 -07001377status_t Parcel::writeUint64(uint64_t val)
1378{
1379 return writeAligned(val);
1380}
1381
Serban Constantinescuf683e012013-11-05 16:53:55 +00001382status_t Parcel::writePointer(uintptr_t val)
1383{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001384 return writeAligned<binder_uintptr_t>(val);
Serban Constantinescuf683e012013-11-05 16:53:55 +00001385}
1386
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001387status_t Parcel::writeFloat(float val)
1388{
Andreas Huber84a6d042009-08-17 13:33:27 -07001389 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001390}
1391
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001392#if defined(__mips__) && defined(__mips_hard_float)
1393
1394status_t Parcel::writeDouble(double val)
1395{
1396 union {
1397 double d;
1398 unsigned long long ll;
1399 } u;
1400 u.d = val;
1401 return writeAligned(u.ll);
1402}
1403
1404#else
1405
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001406status_t Parcel::writeDouble(double val)
1407{
Andreas Huber84a6d042009-08-17 13:33:27 -07001408 return writeAligned(val);
1409}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001410
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001411#endif
1412
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001413status_t Parcel::writeCString(const char* str)
1414{
1415 return write(str, strlen(str)+1);
1416}
1417
1418status_t Parcel::writeString8(const String8& str)
1419{
Tomasz Wasilczyk0bfea2d2023-08-11 00:06:51 +00001420 return writeString8(str.c_str(), str.size());
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06001421}
1422
1423status_t Parcel::writeString8(const char* str, size_t len)
1424{
1425 if (str == nullptr) return writeInt32(-1);
1426
Jeff Sharkey18220902020-11-05 08:36:20 -07001427 // NOTE: Keep this logic in sync with android_os_Parcel.cpp
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06001428 status_t err = writeInt32(len);
1429 if (err == NO_ERROR) {
1430 uint8_t* data = (uint8_t*)writeInplace(len+sizeof(char));
1431 if (data) {
1432 memcpy(data, str, len);
1433 *reinterpret_cast<char*>(data+len) = 0;
1434 return NO_ERROR;
1435 }
1436 err = mError;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001437 }
1438 return err;
1439}
1440
1441status_t Parcel::writeString16(const String16& str)
1442{
Tomasz Wasilczyk0bfea2d2023-08-11 00:06:51 +00001443 return writeString16(str.c_str(), str.size());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001444}
1445
1446status_t Parcel::writeString16(const char16_t* str, size_t len)
1447{
Yi Kong91635562018-06-07 14:38:36 -07001448 if (str == nullptr) return writeInt32(-1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001449
Jeff Sharkey18220902020-11-05 08:36:20 -07001450 // NOTE: Keep this logic in sync with android_os_Parcel.cpp
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001451 status_t err = writeInt32(len);
1452 if (err == NO_ERROR) {
1453 len *= sizeof(char16_t);
1454 uint8_t* data = (uint8_t*)writeInplace(len+sizeof(char16_t));
1455 if (data) {
1456 memcpy(data, str, len);
1457 *reinterpret_cast<char16_t*>(data+len) = 0;
1458 return NO_ERROR;
1459 }
1460 err = mError;
1461 }
1462 return err;
1463}
1464
1465status_t Parcel::writeStrongBinder(const sp<IBinder>& val)
1466{
Steven Morelanda86a3562019-08-01 23:28:34 +00001467 return flattenBinder(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001468}
1469
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001470
Casey Dahlinb9872622015-11-25 15:09:45 -08001471status_t Parcel::writeRawNullableParcelable(const Parcelable* parcelable) {
1472 if (!parcelable) {
1473 return writeInt32(0);
1474 }
1475
1476 return writeParcelable(*parcelable);
1477}
1478
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001479status_t Parcel::writeNativeHandle(const native_handle* handle)
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001480{
Mathias Agopian1d0a95b2009-07-31 16:12:13 -07001481 if (!handle || handle->version != sizeof(native_handle))
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001482 return BAD_TYPE;
1483
1484 status_t err;
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001485 err = writeInt32(handle->numFds);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001486 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001487
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001488 err = writeInt32(handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001489 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001490
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001491 for (int i=0 ; err==NO_ERROR && i<handle->numFds ; i++)
1492 err = writeDupFileDescriptor(handle->data[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001493
1494 if (err != NO_ERROR) {
Steve Block9d453682011-12-20 16:23:08 +00001495 ALOGD("write native handle, write dup fd failed");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001496 return err;
1497 }
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001498 err = write(handle->data + handle->numFds, sizeof(int)*handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001499 return err;
1500}
1501
Frederick Mayle69a0c992022-05-26 20:38:39 +00001502status_t Parcel::writeFileDescriptor(int fd, bool takeOwnership) {
1503 if (auto* rpcFields = maybeRpcFields()) {
1504 std::variant<base::unique_fd, base::borrowed_fd> fdVariant;
1505 if (takeOwnership) {
1506 fdVariant = base::unique_fd(fd);
1507 } else {
1508 fdVariant = base::borrowed_fd(fd);
1509 }
1510 if (!mAllowFds) {
1511 return FDS_NOT_ALLOWED;
1512 }
1513 switch (rpcFields->mSession->getFileDescriptorTransportMode()) {
1514 case RpcSession::FileDescriptorTransportMode::NONE: {
1515 return FDS_NOT_ALLOWED;
1516 }
Andrei Homescu1c18a802022-08-17 04:59:01 +00001517 case RpcSession::FileDescriptorTransportMode::UNIX:
1518 case RpcSession::FileDescriptorTransportMode::TRUSTY: {
Frederick Mayle69a0c992022-05-26 20:38:39 +00001519 if (rpcFields->mFds == nullptr) {
1520 rpcFields->mFds = std::make_unique<decltype(rpcFields->mFds)::element_type>();
1521 }
1522 size_t dataPos = mDataPos;
1523 if (dataPos > UINT32_MAX) {
1524 return NO_MEMORY;
1525 }
1526 if (status_t err = writeInt32(RpcFields::TYPE_NATIVE_FILE_DESCRIPTOR); err != OK) {
1527 return err;
1528 }
1529 if (status_t err = writeInt32(rpcFields->mFds->size()); err != OK) {
1530 return err;
1531 }
1532 rpcFields->mObjectPositions.push_back(dataPos);
1533 rpcFields->mFds->push_back(std::move(fdVariant));
1534 return OK;
1535 }
1536 }
Steven Moreland5553ac42020-11-11 02:14:45 +00001537 }
1538
Andrei Homescua9cca9c2022-05-03 04:48:01 +00001539#ifdef BINDER_WITH_KERNEL_IPC
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001540 flat_binder_object obj;
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07001541 obj.hdr.type = BINDER_TYPE_FD;
T.J. Mercierca0c2cb2022-12-19 21:56:35 +00001542 obj.flags = 0;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -08001543 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001544 obj.handle = fd;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001545 obj.cookie = takeOwnership ? 1 : 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001546 return writeObject(obj, true);
Andrei Homescua9cca9c2022-05-03 04:48:01 +00001547#else // BINDER_WITH_KERNEL_IPC
1548 LOG_ALWAYS_FATAL("Binder kernel driver disabled at build time");
1549 (void)fd;
1550 (void)takeOwnership;
1551 return INVALID_OPERATION;
1552#endif // BINDER_WITH_KERNEL_IPC
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001553}
1554
1555status_t Parcel::writeDupFileDescriptor(int fd)
1556{
Andrei Homescu24ad36e2022-08-04 01:33:33 +00001557 int dupFd;
1558 if (status_t err = dupFileDescriptor(fd, &dupFd); err != OK) {
1559 return err;
Jeff Brownd341c712011-11-04 20:19:33 -07001560 }
1561 status_t err = writeFileDescriptor(dupFd, true /*takeOwnership*/);
Casey Dahlin06673e32015-11-23 13:24:23 -08001562 if (err != OK) {
Jeff Brownd341c712011-11-04 20:19:33 -07001563 close(dupFd);
1564 }
1565 return err;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001566}
1567
Dianne Hackborn1941a402016-08-29 12:30:43 -07001568status_t Parcel::writeParcelFileDescriptor(int fd, bool takeOwnership)
1569{
1570 writeInt32(0);
1571 return writeFileDescriptor(fd, takeOwnership);
1572}
1573
Ryo Hashimotobf551892018-05-31 16:58:35 +09001574status_t Parcel::writeDupParcelFileDescriptor(int fd)
1575{
Andrei Homescu24ad36e2022-08-04 01:33:33 +00001576 int dupFd;
1577 if (status_t err = dupFileDescriptor(fd, &dupFd); err != OK) {
1578 return err;
Ryo Hashimotobf551892018-05-31 16:58:35 +09001579 }
1580 status_t err = writeParcelFileDescriptor(dupFd, true /*takeOwnership*/);
1581 if (err != OK) {
1582 close(dupFd);
1583 }
1584 return err;
1585}
1586
Christopher Wiley2cf19952016-04-11 11:09:37 -07001587status_t Parcel::writeUniqueFileDescriptor(const base::unique_fd& fd) {
Casey Dahlin06673e32015-11-23 13:24:23 -08001588 return writeDupFileDescriptor(fd.get());
1589}
1590
Jeff Brown13b16042014-11-11 16:44:25 -08001591status_t Parcel::writeBlob(size_t len, bool mutableCopy, WritableBlob* outBlob)
Jeff Brown5707dbf2011-09-23 21:17:56 -07001592{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001593 if (len > INT32_MAX) {
1594 // don't accept size_t values which may have come from an
1595 // inadvertent conversion from a negative int.
1596 return BAD_VALUE;
1597 }
1598
Jeff Brown13b16042014-11-11 16:44:25 -08001599 status_t status;
1600 if (!mAllowFds || len <= BLOB_INPLACE_LIMIT) {
Steve Block6807e592011-10-20 11:56:00 +01001601 ALOGV("writeBlob: write in place");
Jeff Brown13b16042014-11-11 16:44:25 -08001602 status = writeInt32(BLOB_INPLACE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001603 if (status) return status;
1604
1605 void* ptr = writeInplace(len);
1606 if (!ptr) return NO_MEMORY;
1607
Jeff Brown13b16042014-11-11 16:44:25 -08001608 outBlob->init(-1, ptr, len, false);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001609 return NO_ERROR;
1610 }
1611
Steve Block6807e592011-10-20 11:56:00 +01001612 ALOGV("writeBlob: write to ashmem");
Jeff Brown5707dbf2011-09-23 21:17:56 -07001613 int fd = ashmem_create_region("Parcel Blob", len);
1614 if (fd < 0) return NO_MEMORY;
1615
1616 int result = ashmem_set_prot_region(fd, PROT_READ | PROT_WRITE);
1617 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -07001618 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001619 } else {
Yi Kong91635562018-06-07 14:38:36 -07001620 void* ptr = ::mmap(nullptr, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001621 if (ptr == MAP_FAILED) {
1622 status = -errno;
1623 } else {
Jeff Brown13b16042014-11-11 16:44:25 -08001624 if (!mutableCopy) {
1625 result = ashmem_set_prot_region(fd, PROT_READ);
1626 }
Jeff Brown5707dbf2011-09-23 21:17:56 -07001627 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -07001628 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001629 } else {
Jeff Brown13b16042014-11-11 16:44:25 -08001630 status = writeInt32(mutableCopy ? BLOB_ASHMEM_MUTABLE : BLOB_ASHMEM_IMMUTABLE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001631 if (!status) {
Jeff Brown93ff1f92011-11-04 19:01:44 -07001632 status = writeFileDescriptor(fd, true /*takeOwnership*/);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001633 if (!status) {
Jeff Brown13b16042014-11-11 16:44:25 -08001634 outBlob->init(fd, ptr, len, mutableCopy);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001635 return NO_ERROR;
1636 }
1637 }
1638 }
1639 }
1640 ::munmap(ptr, len);
1641 }
1642 ::close(fd);
1643 return status;
1644}
1645
Jeff Brown13b16042014-11-11 16:44:25 -08001646status_t Parcel::writeDupImmutableBlobFileDescriptor(int fd)
1647{
1648 // Must match up with what's done in writeBlob.
1649 if (!mAllowFds) return FDS_NOT_ALLOWED;
1650 status_t status = writeInt32(BLOB_ASHMEM_IMMUTABLE);
1651 if (status) return status;
1652 return writeDupFileDescriptor(fd);
1653}
1654
Mathias Agopiane1424282013-07-29 21:24:40 -07001655status_t Parcel::write(const FlattenableHelperInterface& val)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001656{
1657 status_t err;
1658
1659 // size if needed
Mathias Agopiane1424282013-07-29 21:24:40 -07001660 const size_t len = val.getFlattenedSize();
1661 const size_t fd_count = val.getFdCount();
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001662
Andrei Homescu1519b982022-06-09 02:04:44 +00001663 if ((len > INT32_MAX) || (fd_count > kMaxFds)) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001664 // don't accept size_t values which may have come from an
1665 // inadvertent conversion from a negative int.
1666 return BAD_VALUE;
1667 }
1668
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001669 err = this->writeInt32(len);
1670 if (err) return err;
1671
1672 err = this->writeInt32(fd_count);
1673 if (err) return err;
1674
1675 // payload
Martijn Coenenf8542382018-04-04 11:46:56 +02001676 void* const buf = this->writeInplace(len);
Yi Kong91635562018-06-07 14:38:36 -07001677 if (buf == nullptr)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001678 return BAD_VALUE;
1679
Yi Kong91635562018-06-07 14:38:36 -07001680 int* fds = nullptr;
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001681 if (fd_count) {
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001682 fds = new (std::nothrow) int[fd_count];
1683 if (fds == nullptr) {
1684 ALOGE("write: failed to allocate requested %zu fds", fd_count);
1685 return BAD_VALUE;
1686 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001687 }
1688
1689 err = val.flatten(buf, len, fds, fd_count);
1690 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
1691 err = this->writeDupFileDescriptor( fds[i] );
1692 }
1693
1694 if (fd_count) {
1695 delete [] fds;
1696 }
1697
1698 return err;
1699}
1700
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001701status_t Parcel::writeObject(const flat_binder_object& val, bool nullMetaData)
1702{
Frederick Mayled3c595a2022-05-09 23:02:54 +00001703 auto* kernelFields = maybeKernelFields();
1704 LOG_ALWAYS_FATAL_IF(kernelFields == nullptr, "Can't write flat_binder_object to RPC Parcel");
1705
Andrei Homescua9cca9c2022-05-03 04:48:01 +00001706#ifdef BINDER_WITH_KERNEL_IPC
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001707 const bool enoughData = (mDataPos+sizeof(val)) <= mDataCapacity;
Frederick Mayled3c595a2022-05-09 23:02:54 +00001708 const bool enoughObjects = kernelFields->mObjectsSize < kernelFields->mObjectsCapacity;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001709 if (enoughData && enoughObjects) {
1710restart_write:
1711 *reinterpret_cast<flat_binder_object*>(mData+mDataPos) = val;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001712
Christopher Tate98e67d32015-06-03 18:44:15 -07001713 // remember if it's a file descriptor
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07001714 if (val.hdr.type == BINDER_TYPE_FD) {
Christopher Tate98e67d32015-06-03 18:44:15 -07001715 if (!mAllowFds) {
1716 // fail before modifying our object index
1717 return FDS_NOT_ALLOWED;
1718 }
Frederick Mayled3c595a2022-05-09 23:02:54 +00001719 kernelFields->mHasFds = kernelFields->mFdsKnown = true;
Christopher Tate98e67d32015-06-03 18:44:15 -07001720 }
1721
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001722 // Need to write meta-data?
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001723 if (nullMetaData || val.binder != 0) {
Frederick Mayled3c595a2022-05-09 23:02:54 +00001724 kernelFields->mObjects[kernelFields->mObjectsSize] = mDataPos;
Steven Morelandc673f1f2021-10-07 18:23:35 -07001725 acquire_object(ProcessState::self(), val, this);
Frederick Mayled3c595a2022-05-09 23:02:54 +00001726 kernelFields->mObjectsSize++;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001727 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001728
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001729 return finishWrite(sizeof(flat_binder_object));
1730 }
1731
1732 if (!enoughData) {
1733 const status_t err = growData(sizeof(val));
1734 if (err != NO_ERROR) return err;
1735 }
1736 if (!enoughObjects) {
Frederick Mayled3c595a2022-05-09 23:02:54 +00001737 if (kernelFields->mObjectsSize > SIZE_MAX - 2) return NO_MEMORY; // overflow
1738 if ((kernelFields->mObjectsSize + 2) > SIZE_MAX / 3) return NO_MEMORY; // overflow
1739 size_t newSize = ((kernelFields->mObjectsSize + 2) * 3) / 2;
Martijn Coenen93fe5182020-01-22 10:46:25 +01001740 if (newSize > SIZE_MAX / sizeof(binder_size_t)) return NO_MEMORY; // overflow
Frederick Mayled3c595a2022-05-09 23:02:54 +00001741 binder_size_t* objects =
1742 (binder_size_t*)realloc(kernelFields->mObjects, newSize * sizeof(binder_size_t));
Yi Kong91635562018-06-07 14:38:36 -07001743 if (objects == nullptr) return NO_MEMORY;
Frederick Mayled3c595a2022-05-09 23:02:54 +00001744 kernelFields->mObjects = objects;
1745 kernelFields->mObjectsCapacity = newSize;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001746 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001747
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001748 goto restart_write;
Andrei Homescua9cca9c2022-05-03 04:48:01 +00001749#else // BINDER_WITH_KERNEL_IPC
1750 LOG_ALWAYS_FATAL("Binder kernel driver disabled at build time");
1751 (void)val;
1752 (void)nullMetaData;
1753 return INVALID_OPERATION;
1754#endif // BINDER_WITH_KERNEL_IPC
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001755}
1756
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001757status_t Parcel::writeNoException()
1758{
Christopher Wiley09eb7492015-11-09 15:06:15 -08001759 binder::Status status;
1760 return status.writeToParcel(this);
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001761}
1762
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001763status_t Parcel::validateReadData(size_t upperBound) const
1764{
Frederick Mayled3c595a2022-05-09 23:02:54 +00001765 const auto* kernelFields = maybeKernelFields();
1766 if (kernelFields == nullptr) {
1767 // Can't validate RPC Parcel reads because the location of binder
1768 // objects is unknown.
1769 return OK;
1770 }
1771
Andrei Homescua9cca9c2022-05-03 04:48:01 +00001772#ifdef BINDER_WITH_KERNEL_IPC
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001773 // Don't allow non-object reads on object data
Frederick Mayled3c595a2022-05-09 23:02:54 +00001774 if (kernelFields->mObjectsSorted || kernelFields->mObjectsSize <= 1) {
1775 data_sorted:
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001776 // Expect to check only against the next object
Frederick Mayled3c595a2022-05-09 23:02:54 +00001777 if (kernelFields->mNextObjectHint < kernelFields->mObjectsSize &&
1778 upperBound > kernelFields->mObjects[kernelFields->mNextObjectHint]) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001779 // For some reason the current read position is greater than the next object
1780 // hint. Iterate until we find the right object
Frederick Mayled3c595a2022-05-09 23:02:54 +00001781 size_t nextObject = kernelFields->mNextObjectHint;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001782 do {
Frederick Mayled3c595a2022-05-09 23:02:54 +00001783 if (mDataPos < kernelFields->mObjects[nextObject] + sizeof(flat_binder_object)) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001784 // Requested info overlaps with an object
Steven Moreland3a667492023-06-02 21:41:39 +00001785 if (!mServiceFuzzing) {
1786 ALOGE("Attempt to read from protected data in Parcel %p", this);
1787 }
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001788 return PERMISSION_DENIED;
1789 }
1790 nextObject++;
Frederick Mayled3c595a2022-05-09 23:02:54 +00001791 } while (nextObject < kernelFields->mObjectsSize &&
1792 upperBound > kernelFields->mObjects[nextObject]);
1793 kernelFields->mNextObjectHint = nextObject;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001794 }
1795 return NO_ERROR;
1796 }
1797 // Quickly determine if mObjects is sorted.
Frederick Mayled3c595a2022-05-09 23:02:54 +00001798 binder_size_t* currObj = kernelFields->mObjects + kernelFields->mObjectsSize - 1;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001799 binder_size_t* prevObj = currObj;
Frederick Mayled3c595a2022-05-09 23:02:54 +00001800 while (currObj > kernelFields->mObjects) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001801 prevObj--;
1802 if(*prevObj > *currObj) {
1803 goto data_unsorted;
1804 }
1805 currObj--;
1806 }
Frederick Mayled3c595a2022-05-09 23:02:54 +00001807 kernelFields->mObjectsSorted = true;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001808 goto data_sorted;
1809
1810data_unsorted:
1811 // Insertion Sort mObjects
1812 // Great for mostly sorted lists. If randomly sorted or reverse ordered mObjects become common,
1813 // switch to std::sort(mObjects, mObjects + mObjectsSize);
Frederick Mayled3c595a2022-05-09 23:02:54 +00001814 for (binder_size_t* iter0 = kernelFields->mObjects + 1;
1815 iter0 < kernelFields->mObjects + kernelFields->mObjectsSize; iter0++) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001816 binder_size_t temp = *iter0;
1817 binder_size_t* iter1 = iter0 - 1;
Frederick Mayled3c595a2022-05-09 23:02:54 +00001818 while (iter1 >= kernelFields->mObjects && *iter1 > temp) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001819 *(iter1 + 1) = *iter1;
1820 iter1--;
1821 }
1822 *(iter1 + 1) = temp;
1823 }
Frederick Mayled3c595a2022-05-09 23:02:54 +00001824 kernelFields->mNextObjectHint = 0;
1825 kernelFields->mObjectsSorted = true;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001826 goto data_sorted;
Andrei Homescua9cca9c2022-05-03 04:48:01 +00001827#else // BINDER_WITH_KERNEL_IPC
1828 (void)upperBound;
1829 return NO_ERROR;
1830#endif // BINDER_WITH_KERNEL_IPC
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001831}
1832
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001833status_t Parcel::read(void* outData, size_t len) const
1834{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001835 if (len > INT32_MAX) {
1836 // don't accept size_t values which may have come from an
1837 // inadvertent conversion from a negative int.
1838 return BAD_VALUE;
1839 }
1840
1841 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize
1842 && len <= pad_size(len)) {
Frederick Mayled3c595a2022-05-09 23:02:54 +00001843 const auto* kernelFields = maybeKernelFields();
1844 if (kernelFields != nullptr && kernelFields->mObjectsSize > 0) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001845 status_t err = validateReadData(mDataPos + pad_size(len));
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001846 if(err != NO_ERROR) {
1847 // Still increment the data position by the expected length
1848 mDataPos += pad_size(len);
1849 ALOGV("read Setting data pos of %p to %zu", this, mDataPos);
1850 return err;
1851 }
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001852 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001853 memcpy(outData, mData+mDataPos, len);
Nick Kralevichb6b14232015-04-02 09:36:02 -07001854 mDataPos += pad_size(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001855 ALOGV("read Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001856 return NO_ERROR;
1857 }
1858 return NOT_ENOUGH_DATA;
1859}
1860
1861const void* Parcel::readInplace(size_t len) const
1862{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001863 if (len > INT32_MAX) {
1864 // don't accept size_t values which may have come from an
1865 // inadvertent conversion from a negative int.
Yi Kong91635562018-06-07 14:38:36 -07001866 return nullptr;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001867 }
1868
1869 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize
1870 && len <= pad_size(len)) {
Frederick Mayled3c595a2022-05-09 23:02:54 +00001871 const auto* kernelFields = maybeKernelFields();
1872 if (kernelFields != nullptr && kernelFields->mObjectsSize > 0) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001873 status_t err = validateReadData(mDataPos + pad_size(len));
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001874 if(err != NO_ERROR) {
1875 // Still increment the data position by the expected length
1876 mDataPos += pad_size(len);
1877 ALOGV("readInplace Setting data pos of %p to %zu", this, mDataPos);
Yi Kong91635562018-06-07 14:38:36 -07001878 return nullptr;
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001879 }
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001880 }
1881
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001882 const void* data = mData+mDataPos;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001883 mDataPos += pad_size(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001884 ALOGV("readInplace Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001885 return data;
1886 }
Yi Kong91635562018-06-07 14:38:36 -07001887 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001888}
1889
Steven Morelandd4f73fb2021-05-14 19:50:52 +00001890status_t Parcel::readOutVectorSizeWithCheck(size_t elmSize, int32_t* size) const {
1891 if (status_t status = readInt32(size); status != OK) return status;
1892 if (*size < 0) return OK; // may be null, client to handle
1893
1894 LOG_ALWAYS_FATAL_IF(elmSize > INT32_MAX, "Cannot have element as big as %zu", elmSize);
1895
1896 // approximation, can't know max element size (e.g. if it makes heap
1897 // allocations)
1898 static_assert(sizeof(int) == sizeof(int32_t), "Android is LP64");
1899 int32_t allocationSize;
1900 if (__builtin_smul_overflow(elmSize, *size, &allocationSize)) return NO_MEMORY;
1901
1902 // High limit of 1MB since something this big could never be returned. Could
1903 // probably scope this down, but might impact very specific usecases.
1904 constexpr int32_t kMaxAllocationSize = 1 * 1000 * 1000;
1905
1906 if (allocationSize >= kMaxAllocationSize) {
1907 return NO_MEMORY;
1908 }
1909
1910 return OK;
1911}
1912
Andreas Huber84a6d042009-08-17 13:33:27 -07001913template<class T>
1914status_t Parcel::readAligned(T *pArg) const {
Elliott Hughes42a9b942020-08-17 15:53:31 -07001915 static_assert(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));
Andrei Homescue33238e2022-03-29 22:50:00 +00001916 static_assert(std::is_trivially_copyable_v<T>);
Andreas Huber84a6d042009-08-17 13:33:27 -07001917
1918 if ((mDataPos+sizeof(T)) <= mDataSize) {
Frederick Mayled3c595a2022-05-09 23:02:54 +00001919 const auto* kernelFields = maybeKernelFields();
1920 if (kernelFields != nullptr && kernelFields->mObjectsSize > 0) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001921 status_t err = validateReadData(mDataPos + sizeof(T));
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001922 if(err != NO_ERROR) {
1923 // Still increment the data position by the expected length
1924 mDataPos += sizeof(T);
1925 return err;
1926 }
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001927 }
1928
Andrei Homescue33238e2022-03-29 22:50:00 +00001929 memcpy(pArg, mData + mDataPos, sizeof(T));
Andreas Huber84a6d042009-08-17 13:33:27 -07001930 mDataPos += sizeof(T);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001931 return NO_ERROR;
1932 } else {
1933 return NOT_ENOUGH_DATA;
1934 }
1935}
1936
Andreas Huber84a6d042009-08-17 13:33:27 -07001937template<class T>
1938T Parcel::readAligned() const {
1939 T result;
1940 if (readAligned(&result) != NO_ERROR) {
1941 result = 0;
1942 }
1943
1944 return result;
1945}
1946
1947template<class T>
1948status_t Parcel::writeAligned(T val) {
Elliott Hughes42a9b942020-08-17 15:53:31 -07001949 static_assert(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));
Andrei Homescue33238e2022-03-29 22:50:00 +00001950 static_assert(std::is_trivially_copyable_v<T>);
Andreas Huber84a6d042009-08-17 13:33:27 -07001951
1952 if ((mDataPos+sizeof(val)) <= mDataCapacity) {
1953restart_write:
Andrei Homescue33238e2022-03-29 22:50:00 +00001954 memcpy(mData + mDataPos, &val, sizeof(val));
Andreas Huber84a6d042009-08-17 13:33:27 -07001955 return finishWrite(sizeof(val));
1956 }
1957
1958 status_t err = growData(sizeof(val));
1959 if (err == NO_ERROR) goto restart_write;
1960 return err;
1961}
1962
1963status_t Parcel::readInt32(int32_t *pArg) const
1964{
1965 return readAligned(pArg);
1966}
1967
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001968int32_t Parcel::readInt32() const
1969{
Andreas Huber84a6d042009-08-17 13:33:27 -07001970 return readAligned<int32_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001971}
1972
Dan Stoza41a0f2f2014-12-01 10:01:10 -08001973status_t Parcel::readUint32(uint32_t *pArg) const
1974{
1975 return readAligned(pArg);
1976}
1977
1978uint32_t Parcel::readUint32() const
1979{
1980 return readAligned<uint32_t>();
1981}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001982
1983status_t Parcel::readInt64(int64_t *pArg) const
1984{
Andreas Huber84a6d042009-08-17 13:33:27 -07001985 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001986}
1987
1988
1989int64_t Parcel::readInt64() const
1990{
Andreas Huber84a6d042009-08-17 13:33:27 -07001991 return readAligned<int64_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001992}
1993
Ronghua Wu2d13afd2015-03-16 11:11:07 -07001994status_t Parcel::readUint64(uint64_t *pArg) const
1995{
1996 return readAligned(pArg);
1997}
1998
1999uint64_t Parcel::readUint64() const
2000{
2001 return readAligned<uint64_t>();
2002}
2003
Serban Constantinescuf683e012013-11-05 16:53:55 +00002004status_t Parcel::readPointer(uintptr_t *pArg) const
2005{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002006 status_t ret;
2007 binder_uintptr_t ptr;
2008 ret = readAligned(&ptr);
2009 if (!ret)
2010 *pArg = ptr;
2011 return ret;
Serban Constantinescuf683e012013-11-05 16:53:55 +00002012}
2013
2014uintptr_t Parcel::readPointer() const
2015{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002016 return readAligned<binder_uintptr_t>();
Serban Constantinescuf683e012013-11-05 16:53:55 +00002017}
2018
2019
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002020status_t Parcel::readFloat(float *pArg) const
2021{
Andreas Huber84a6d042009-08-17 13:33:27 -07002022 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002023}
2024
2025
2026float Parcel::readFloat() const
2027{
Andreas Huber84a6d042009-08-17 13:33:27 -07002028 return readAligned<float>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002029}
2030
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08002031#if defined(__mips__) && defined(__mips_hard_float)
2032
2033status_t Parcel::readDouble(double *pArg) const
2034{
2035 union {
2036 double d;
2037 unsigned long long ll;
2038 } u;
Narayan Kamath2c68d382014-06-04 15:04:29 +01002039 u.d = 0;
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08002040 status_t status;
2041 status = readAligned(&u.ll);
2042 *pArg = u.d;
2043 return status;
2044}
2045
2046double Parcel::readDouble() const
2047{
2048 union {
2049 double d;
2050 unsigned long long ll;
2051 } u;
2052 u.ll = readAligned<unsigned long long>();
2053 return u.d;
2054}
2055
2056#else
2057
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002058status_t Parcel::readDouble(double *pArg) const
2059{
Andreas Huber84a6d042009-08-17 13:33:27 -07002060 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002061}
2062
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002063double Parcel::readDouble() const
2064{
Andreas Huber84a6d042009-08-17 13:33:27 -07002065 return readAligned<double>();
2066}
2067
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08002068#endif
2069
Casey Dahlind6848f52015-10-15 15:44:59 -07002070status_t Parcel::readBool(bool *pArg) const
2071{
Manoj Gupta6eb62052017-07-12 10:29:15 -07002072 int32_t tmp = 0;
Casey Dahlind6848f52015-10-15 15:44:59 -07002073 status_t ret = readInt32(&tmp);
2074 *pArg = (tmp != 0);
2075 return ret;
2076}
2077
2078bool Parcel::readBool() const
2079{
2080 return readInt32() != 0;
2081}
2082
2083status_t Parcel::readChar(char16_t *pArg) const
2084{
Manoj Gupta6eb62052017-07-12 10:29:15 -07002085 int32_t tmp = 0;
Casey Dahlind6848f52015-10-15 15:44:59 -07002086 status_t ret = readInt32(&tmp);
2087 *pArg = char16_t(tmp);
2088 return ret;
2089}
2090
2091char16_t Parcel::readChar() const
2092{
2093 return char16_t(readInt32());
2094}
2095
2096status_t Parcel::readByte(int8_t *pArg) const
2097{
Manoj Gupta6eb62052017-07-12 10:29:15 -07002098 int32_t tmp = 0;
Casey Dahlind6848f52015-10-15 15:44:59 -07002099 status_t ret = readInt32(&tmp);
2100 *pArg = int8_t(tmp);
2101 return ret;
2102}
2103
2104int8_t Parcel::readByte() const
2105{
2106 return int8_t(readInt32());
2107}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002108
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08002109status_t Parcel::readUtf8FromUtf16(std::string* str) const {
2110 size_t utf16Size = 0;
2111 const char16_t* src = readString16Inplace(&utf16Size);
2112 if (!src) {
2113 return UNEXPECTED_NULL;
2114 }
2115
2116 // Save ourselves the trouble, we're done.
2117 if (utf16Size == 0u) {
2118 str->clear();
2119 return NO_ERROR;
2120 }
2121
Sergio Giro9b39ebe2016-06-28 18:19:33 +01002122 // Allow for closing '\0'
2123 ssize_t utf8Size = utf16_to_utf8_length(src, utf16Size) + 1;
2124 if (utf8Size < 1) {
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08002125 return BAD_VALUE;
2126 }
2127 // Note that while it is probably safe to assume string::resize keeps a
Sergio Giro9b39ebe2016-06-28 18:19:33 +01002128 // spare byte around for the trailing null, we still pass the size including the trailing null
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08002129 str->resize(utf8Size);
Sergio Giro9b39ebe2016-06-28 18:19:33 +01002130 utf16_to_utf8(src, utf16Size, &((*str)[0]), utf8Size);
2131 str->resize(utf8Size - 1);
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08002132 return NO_ERROR;
2133}
2134
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002135const char* Parcel::readCString() const
2136{
Steven Morelandd0d4b582019-05-17 13:14:06 -07002137 if (mDataPos < mDataSize) {
2138 const size_t avail = mDataSize-mDataPos;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002139 const char* str = reinterpret_cast<const char*>(mData+mDataPos);
2140 // is the string's trailing NUL within the parcel's valid bounds?
2141 const char* eos = reinterpret_cast<const char*>(memchr(str, 0, avail));
2142 if (eos) {
2143 const size_t len = eos - str;
Nick Kralevichb6b14232015-04-02 09:36:02 -07002144 mDataPos += pad_size(len+1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002145 ALOGV("readCString Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002146 return str;
2147 }
2148 }
Yi Kong91635562018-06-07 14:38:36 -07002149 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002150}
2151
2152String8 Parcel::readString8() const
2153{
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06002154 size_t len;
2155 const char* str = readString8Inplace(&len);
2156 if (str) return String8(str, len);
Steven Moreland3a667492023-06-02 21:41:39 +00002157
2158 if (!mServiceFuzzing) {
2159 ALOGE("Reading a NULL string not supported here.");
2160 }
2161
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06002162 return String8();
Roshan Pius87b64d22016-07-18 12:51:02 -07002163}
2164
2165status_t Parcel::readString8(String8* pArg) const
2166{
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06002167 size_t len;
2168 const char* str = readString8Inplace(&len);
2169 if (str) {
2170 pArg->setTo(str, len);
2171 return 0;
2172 } else {
Roshan Pius87b64d22016-07-18 12:51:02 -07002173 *pArg = String8();
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06002174 return UNEXPECTED_NULL;
Roshan Pius87b64d22016-07-18 12:51:02 -07002175 }
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06002176}
2177
2178const char* Parcel::readString8Inplace(size_t* outLen) const
2179{
2180 int32_t size = readInt32();
2181 // watch for potential int overflow from size+1
2182 if (size >= 0 && size < INT32_MAX) {
2183 *outLen = size;
2184 const char* str = (const char*)readInplace(size+1);
Steven Moreland61d0f842020-12-04 21:13:03 +00002185 if (str != nullptr) {
2186 if (str[size] == '\0') {
2187 return str;
2188 }
2189 android_errorWriteLog(0x534e4554, "172655291");
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06002190 }
Roshan Pius87b64d22016-07-18 12:51:02 -07002191 }
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06002192 *outLen = 0;
2193 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002194}
2195
2196String16 Parcel::readString16() const
2197{
2198 size_t len;
2199 const char16_t* str = readString16Inplace(&len);
2200 if (str) return String16(str, len);
Steven Moreland3a667492023-06-02 21:41:39 +00002201
2202 if (!mServiceFuzzing) {
2203 ALOGE("Reading a NULL string not supported here.");
2204 }
2205
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002206 return String16();
2207}
2208
Casey Dahlinb9872622015-11-25 15:09:45 -08002209
Casey Dahlin451ff582015-10-19 18:12:18 -07002210status_t Parcel::readString16(String16* pArg) const
2211{
2212 size_t len;
2213 const char16_t* str = readString16Inplace(&len);
2214 if (str) {
Casey Dahlin1515ea12015-10-20 16:26:23 -07002215 pArg->setTo(str, len);
Casey Dahlin451ff582015-10-19 18:12:18 -07002216 return 0;
2217 } else {
2218 *pArg = String16();
Christopher Wiley4db672d2015-11-10 09:44:30 -08002219 return UNEXPECTED_NULL;
Casey Dahlin451ff582015-10-19 18:12:18 -07002220 }
2221}
2222
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002223const char16_t* Parcel::readString16Inplace(size_t* outLen) const
2224{
2225 int32_t size = readInt32();
2226 // watch for potential int overflow from size+1
2227 if (size >= 0 && size < INT32_MAX) {
2228 *outLen = size;
2229 const char16_t* str = (const char16_t*)readInplace((size+1)*sizeof(char16_t));
Steven Moreland61d0f842020-12-04 21:13:03 +00002230 if (str != nullptr) {
2231 if (str[size] == u'\0') {
2232 return str;
2233 }
2234 android_errorWriteLog(0x534e4554, "172655291");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002235 }
2236 }
2237 *outLen = 0;
Yi Kong91635562018-06-07 14:38:36 -07002238 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002239}
2240
Casey Dahlinf0c13772015-10-27 18:33:56 -07002241status_t Parcel::readStrongBinder(sp<IBinder>* val) const
2242{
Christopher Wiley35d77ca2016-03-08 10:49:51 -08002243 status_t status = readNullableStrongBinder(val);
2244 if (status == OK && !val->get()) {
Steven Moreland3a667492023-06-02 21:41:39 +00002245 if (!mServiceFuzzing) {
2246 ALOGW("Expecting binder but got null!");
2247 }
Christopher Wiley35d77ca2016-03-08 10:49:51 -08002248 status = UNEXPECTED_NULL;
2249 }
2250 return status;
2251}
2252
2253status_t Parcel::readNullableStrongBinder(sp<IBinder>* val) const
2254{
Steven Morelanda86a3562019-08-01 23:28:34 +00002255 return unflattenBinder(val);
Casey Dahlinf0c13772015-10-27 18:33:56 -07002256}
2257
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002258sp<IBinder> Parcel::readStrongBinder() const
2259{
2260 sp<IBinder> val;
Christopher Wiley35d77ca2016-03-08 10:49:51 -08002261 // Note that a lot of code in Android reads binders by hand with this
2262 // method, and that code has historically been ok with getting nullptr
2263 // back (while ignoring error codes).
2264 readNullableStrongBinder(&val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002265 return val;
2266}
2267
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07002268int32_t Parcel::readExceptionCode() const
2269{
Christopher Wiley09eb7492015-11-09 15:06:15 -08002270 binder::Status status;
2271 status.readFromParcel(*this);
2272 return status.exceptionCode();
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07002273}
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002274
Mathias Agopiana47f02a2009-05-21 16:29:38 -07002275native_handle* Parcel::readNativeHandle() const
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002276{
2277 int numFds, numInts;
2278 status_t err;
2279 err = readInt32(&numFds);
Yi Kong91635562018-06-07 14:38:36 -07002280 if (err != NO_ERROR) return nullptr;
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002281 err = readInt32(&numInts);
Yi Kong91635562018-06-07 14:38:36 -07002282 if (err != NO_ERROR) return nullptr;
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002283
Mathias Agopiana47f02a2009-05-21 16:29:38 -07002284 native_handle* h = native_handle_create(numFds, numInts);
Adam Lesinskieaac99a2015-05-12 17:35:48 -07002285 if (!h) {
Yi Kong91635562018-06-07 14:38:36 -07002286 return nullptr;
Adam Lesinskieaac99a2015-05-12 17:35:48 -07002287 }
2288
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002289 for (int i=0 ; err==NO_ERROR && i<numFds ; i++) {
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002290 h->data[i] = fcntl(readFileDescriptor(), F_DUPFD_CLOEXEC, 0);
Marco Nelissen1de79662016-04-26 08:44:09 -07002291 if (h->data[i] < 0) {
2292 for (int j = 0; j < i; j++) {
2293 close(h->data[j]);
2294 }
2295 native_handle_delete(h);
Yi Kong91635562018-06-07 14:38:36 -07002296 return nullptr;
Marco Nelissen1de79662016-04-26 08:44:09 -07002297 }
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002298 }
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002299 err = read(h->data + numFds, sizeof(int)*numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002300 if (err != NO_ERROR) {
Mathias Agopiana47f02a2009-05-21 16:29:38 -07002301 native_handle_close(h);
2302 native_handle_delete(h);
Yi Kong91635562018-06-07 14:38:36 -07002303 h = nullptr;
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002304 }
2305 return h;
2306}
2307
Frederick Mayle69a0c992022-05-26 20:38:39 +00002308int Parcel::readFileDescriptor() const {
2309 if (const auto* rpcFields = maybeRpcFields()) {
2310 if (!std::binary_search(rpcFields->mObjectPositions.begin(),
2311 rpcFields->mObjectPositions.end(), mDataPos)) {
Steven Moreland3a667492023-06-02 21:41:39 +00002312 if (!mServiceFuzzing) {
2313 ALOGW("Attempt to read file descriptor from Parcel %p at offset %zu that is not in "
2314 "the object list",
2315 this, mDataPos);
2316 }
Frederick Mayle69a0c992022-05-26 20:38:39 +00002317 return BAD_TYPE;
2318 }
2319
2320 int32_t objectType = readInt32();
2321 if (objectType != RpcFields::TYPE_NATIVE_FILE_DESCRIPTOR) {
2322 return BAD_TYPE;
2323 }
2324
2325 int32_t fdIndex = readInt32();
2326 if (rpcFields->mFds == nullptr || fdIndex < 0 ||
2327 static_cast<size_t>(fdIndex) >= rpcFields->mFds->size()) {
2328 ALOGE("RPC Parcel contains invalid file descriptor index. index=%d fd_count=%zu",
2329 fdIndex, rpcFields->mFds ? rpcFields->mFds->size() : 0);
2330 return BAD_VALUE;
2331 }
2332 return toRawFd(rpcFields->mFds->at(fdIndex));
2333 }
2334
Andrei Homescua9cca9c2022-05-03 04:48:01 +00002335#ifdef BINDER_WITH_KERNEL_IPC
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002336 const flat_binder_object* flat = readObject(true);
Casey Dahlin06673e32015-11-23 13:24:23 -08002337
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002338 if (flat && flat->hdr.type == BINDER_TYPE_FD) {
Casey Dahlin06673e32015-11-23 13:24:23 -08002339 return flat->handle;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002340 }
Casey Dahlin06673e32015-11-23 13:24:23 -08002341
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002342 return BAD_TYPE;
Andrei Homescua9cca9c2022-05-03 04:48:01 +00002343#else // BINDER_WITH_KERNEL_IPC
2344 LOG_ALWAYS_FATAL("Binder kernel driver disabled at build time");
2345 return INVALID_OPERATION;
2346#endif // BINDER_WITH_KERNEL_IPC
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002347}
2348
Frederick Mayle69a0c992022-05-26 20:38:39 +00002349int Parcel::readParcelFileDescriptor() const {
Dianne Hackborn1941a402016-08-29 12:30:43 -07002350 int32_t hasComm = readInt32();
2351 int fd = readFileDescriptor();
2352 if (hasComm != 0) {
Steven Morelandb73806a2018-11-12 19:35:47 -08002353 // detach (owned by the binder driver)
2354 int comm = readFileDescriptor();
2355
2356 // warning: this must be kept in sync with:
2357 // frameworks/base/core/java/android/os/ParcelFileDescriptor.java
2358 enum ParcelFileDescriptorStatus {
2359 DETACHED = 2,
2360 };
2361
2362#if BYTE_ORDER == BIG_ENDIAN
2363 const int32_t message = ParcelFileDescriptorStatus::DETACHED;
2364#endif
2365#if BYTE_ORDER == LITTLE_ENDIAN
2366 const int32_t message = __builtin_bswap32(ParcelFileDescriptorStatus::DETACHED);
2367#endif
2368
2369 ssize_t written = TEMP_FAILURE_RETRY(
2370 ::write(comm, &message, sizeof(message)));
2371
Krzysztof Kosińskia8406892021-02-02 17:59:43 -08002372 if (written != sizeof(message)) {
Steven Morelandb73806a2018-11-12 19:35:47 -08002373 ALOGW("Failed to detach ParcelFileDescriptor written: %zd err: %s",
2374 written, strerror(errno));
2375 return BAD_TYPE;
2376 }
Dianne Hackborn1941a402016-08-29 12:30:43 -07002377 }
2378 return fd;
2379}
2380
Christopher Wiley2cf19952016-04-11 11:09:37 -07002381status_t Parcel::readUniqueFileDescriptor(base::unique_fd* val) const
Casey Dahlin06673e32015-11-23 13:24:23 -08002382{
2383 int got = readFileDescriptor();
2384
2385 if (got == BAD_TYPE) {
2386 return BAD_TYPE;
2387 }
2388
Andrei Homescu24ad36e2022-08-04 01:33:33 +00002389 int dupFd;
2390 if (status_t err = dupFileDescriptor(got, &dupFd); err != OK) {
2391 return BAD_VALUE;
2392 }
2393
2394 val->reset(dupFd);
Casey Dahlin06673e32015-11-23 13:24:23 -08002395
2396 if (val->get() < 0) {
2397 return BAD_VALUE;
2398 }
2399
2400 return OK;
2401}
2402
Ryo Hashimotobf551892018-05-31 16:58:35 +09002403status_t Parcel::readUniqueParcelFileDescriptor(base::unique_fd* val) const
2404{
2405 int got = readParcelFileDescriptor();
2406
2407 if (got == BAD_TYPE) {
2408 return BAD_TYPE;
2409 }
2410
Andrei Homescu24ad36e2022-08-04 01:33:33 +00002411 int dupFd;
2412 if (status_t err = dupFileDescriptor(got, &dupFd); err != OK) {
2413 return BAD_VALUE;
2414 }
2415
2416 val->reset(dupFd);
Ryo Hashimotobf551892018-05-31 16:58:35 +09002417
2418 if (val->get() < 0) {
2419 return BAD_VALUE;
2420 }
2421
2422 return OK;
2423}
Casey Dahlin06673e32015-11-23 13:24:23 -08002424
Jeff Brown5707dbf2011-09-23 21:17:56 -07002425status_t Parcel::readBlob(size_t len, ReadableBlob* outBlob) const
2426{
Jeff Brown13b16042014-11-11 16:44:25 -08002427 int32_t blobType;
2428 status_t status = readInt32(&blobType);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002429 if (status) return status;
2430
Jeff Brown13b16042014-11-11 16:44:25 -08002431 if (blobType == BLOB_INPLACE) {
Steve Block6807e592011-10-20 11:56:00 +01002432 ALOGV("readBlob: read in place");
Jeff Brown5707dbf2011-09-23 21:17:56 -07002433 const void* ptr = readInplace(len);
2434 if (!ptr) return BAD_VALUE;
2435
Jeff Brown13b16042014-11-11 16:44:25 -08002436 outBlob->init(-1, const_cast<void*>(ptr), len, false);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002437 return NO_ERROR;
2438 }
2439
Steve Block6807e592011-10-20 11:56:00 +01002440 ALOGV("readBlob: read from ashmem");
Jeff Brown13b16042014-11-11 16:44:25 -08002441 bool isMutable = (blobType == BLOB_ASHMEM_MUTABLE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002442 int fd = readFileDescriptor();
2443 if (fd == int(BAD_TYPE)) return BAD_VALUE;
2444
Jorim Jaggi150b4ef2018-07-13 11:18:30 +00002445 if (!ashmem_valid(fd)) {
2446 ALOGE("invalid fd");
2447 return BAD_VALUE;
2448 }
Marco Nelissen7a96ec42018-06-06 07:37:46 -07002449 int size = ashmem_get_size_region(fd);
2450 if (size < 0 || size_t(size) < len) {
Jorim Jaggi150b4ef2018-07-13 11:18:30 +00002451 ALOGE("request size %zu does not match fd size %d", len, size);
Marco Nelissen7a96ec42018-06-06 07:37:46 -07002452 return BAD_VALUE;
2453 }
Yi Kong91635562018-06-07 14:38:36 -07002454 void* ptr = ::mmap(nullptr, len, isMutable ? PROT_READ | PROT_WRITE : PROT_READ,
Jeff Brown13b16042014-11-11 16:44:25 -08002455 MAP_SHARED, fd, 0);
Narayan Kamath9ea09752014-10-08 17:35:45 +01002456 if (ptr == MAP_FAILED) return NO_MEMORY;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002457
Jeff Brown13b16042014-11-11 16:44:25 -08002458 outBlob->init(fd, ptr, len, isMutable);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002459 return NO_ERROR;
2460}
2461
Mathias Agopiane1424282013-07-29 21:24:40 -07002462status_t Parcel::read(FlattenableHelperInterface& val) const
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002463{
2464 // size
2465 const size_t len = this->readInt32();
2466 const size_t fd_count = this->readInt32();
2467
Andrei Homescu1519b982022-06-09 02:04:44 +00002468 if ((len > INT32_MAX) || (fd_count > kMaxFds)) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07002469 // don't accept size_t values which may have come from an
2470 // inadvertent conversion from a negative int.
2471 return BAD_VALUE;
2472 }
2473
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002474 // payload
Nick Kralevichb6b14232015-04-02 09:36:02 -07002475 void const* const buf = this->readInplace(pad_size(len));
Yi Kong91635562018-06-07 14:38:36 -07002476 if (buf == nullptr)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002477 return BAD_VALUE;
2478
Yi Kong91635562018-06-07 14:38:36 -07002479 int* fds = nullptr;
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002480 if (fd_count) {
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002481 fds = new (std::nothrow) int[fd_count];
2482 if (fds == nullptr) {
2483 ALOGE("read: failed to allocate requested %zu fds", fd_count);
2484 return BAD_VALUE;
2485 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002486 }
2487
2488 status_t err = NO_ERROR;
2489 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
Fabien Sanglardd84ff312016-10-21 10:58:26 -07002490 int fd = this->readFileDescriptor();
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002491 if (fd < 0 || ((fds[i] = fcntl(fd, F_DUPFD_CLOEXEC, 0)) < 0)) {
Jun Jiangabf8a2c2014-04-29 14:22:10 +08002492 err = BAD_VALUE;
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002493 ALOGE("fcntl(F_DUPFD_CLOEXEC) failed in Parcel::read, i is %zu, fds[i] is %d, fd_count is %zu, error: %s",
Fabien Sanglardd84ff312016-10-21 10:58:26 -07002494 i, fds[i], fd_count, strerror(fd < 0 ? -fd : errno));
2495 // Close all the file descriptors that were dup-ed.
2496 for (size_t j=0; j<i ;j++) {
2497 close(fds[j]);
2498 }
Jun Jiangabf8a2c2014-04-29 14:22:10 +08002499 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002500 }
2501
2502 if (err == NO_ERROR) {
2503 err = val.unflatten(buf, len, fds, fd_count);
2504 }
2505
2506 if (fd_count) {
2507 delete [] fds;
2508 }
2509
2510 return err;
2511}
Andrei Homescua9cca9c2022-05-03 04:48:01 +00002512
2513#ifdef BINDER_WITH_KERNEL_IPC
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002514const flat_binder_object* Parcel::readObject(bool nullMetaData) const
2515{
Frederick Mayled3c595a2022-05-09 23:02:54 +00002516 const auto* kernelFields = maybeKernelFields();
2517 if (kernelFields == nullptr) {
2518 return nullptr;
2519 }
2520
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002521 const size_t DPOS = mDataPos;
2522 if ((DPOS+sizeof(flat_binder_object)) <= mDataSize) {
2523 const flat_binder_object* obj
2524 = reinterpret_cast<const flat_binder_object*>(mData+DPOS);
2525 mDataPos = DPOS + sizeof(flat_binder_object);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002526 if (!nullMetaData && (obj->cookie == 0 && obj->binder == 0)) {
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002527 // When transferring a NULL object, we don't write it into
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002528 // the object list, so we don't want to check for it when
2529 // reading.
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002530 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002531 return obj;
2532 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002533
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002534 // Ensure that this object is valid...
Frederick Mayled3c595a2022-05-09 23:02:54 +00002535 binder_size_t* const OBJS = kernelFields->mObjects;
2536 const size_t N = kernelFields->mObjectsSize;
2537 size_t opos = kernelFields->mNextObjectHint;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002538
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002539 if (N > 0) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002540 ALOGV("Parcel %p looking for obj at %zu, hint=%zu",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002541 this, DPOS, opos);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002542
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002543 // Start at the current hint position, looking for an object at
2544 // the current data position.
2545 if (opos < N) {
2546 while (opos < (N-1) && OBJS[opos] < DPOS) {
2547 opos++;
2548 }
2549 } else {
2550 opos = N-1;
2551 }
2552 if (OBJS[opos] == DPOS) {
2553 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002554 ALOGV("Parcel %p found obj %zu at index %zu with forward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002555 this, DPOS, opos);
Frederick Mayled3c595a2022-05-09 23:02:54 +00002556 kernelFields->mNextObjectHint = opos + 1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002557 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002558 return obj;
2559 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002560
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002561 // Look backwards for it...
2562 while (opos > 0 && OBJS[opos] > DPOS) {
2563 opos--;
2564 }
2565 if (OBJS[opos] == DPOS) {
2566 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002567 ALOGV("Parcel %p found obj %zu at index %zu with backward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002568 this, DPOS, opos);
Frederick Mayled3c595a2022-05-09 23:02:54 +00002569 kernelFields->mNextObjectHint = opos + 1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002570 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002571 return obj;
2572 }
2573 }
Steven Moreland3a667492023-06-02 21:41:39 +00002574 if (!mServiceFuzzing) {
2575 ALOGW("Attempt to read object from Parcel %p at offset %zu that is not in the object "
2576 "list",
2577 this, DPOS);
2578 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002579 }
Yi Kong91635562018-06-07 14:38:36 -07002580 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002581}
Andrei Homescua9cca9c2022-05-03 04:48:01 +00002582#endif // BINDER_WITH_KERNEL_IPC
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002583
Frederick Mayled3c595a2022-05-09 23:02:54 +00002584void Parcel::closeFileDescriptors() {
Frederick Mayle69a0c992022-05-26 20:38:39 +00002585 if (auto* kernelFields = maybeKernelFields()) {
Andrei Homescua9cca9c2022-05-03 04:48:01 +00002586#ifdef BINDER_WITH_KERNEL_IPC
Frederick Mayle69a0c992022-05-26 20:38:39 +00002587 size_t i = kernelFields->mObjectsSize;
2588 if (i > 0) {
2589 // ALOGI("Closing file descriptors for %zu objects...", i);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002590 }
Frederick Mayle69a0c992022-05-26 20:38:39 +00002591 while (i > 0) {
2592 i--;
2593 const flat_binder_object* flat =
2594 reinterpret_cast<flat_binder_object*>(mData + kernelFields->mObjects[i]);
2595 if (flat->hdr.type == BINDER_TYPE_FD) {
2596 // ALOGI("Closing fd: %ld", flat->handle);
Steven Moreland02f736f2023-06-22 23:03:57 +00002597 // FDs from the kernel are always owned
2598 FdTagClose(flat->handle, this);
Frederick Mayle69a0c992022-05-26 20:38:39 +00002599 }
2600 }
Andrei Homescua9cca9c2022-05-03 04:48:01 +00002601#else // BINDER_WITH_KERNEL_IPC
2602 LOG_ALWAYS_FATAL("Binder kernel driver disabled at build time");
2603#endif // BINDER_WITH_KERNEL_IPC
Frederick Mayle69a0c992022-05-26 20:38:39 +00002604 } else if (auto* rpcFields = maybeRpcFields()) {
2605 rpcFields->mFds.reset();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002606 }
2607}
2608
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002609uintptr_t Parcel::ipcData() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002610{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002611 return reinterpret_cast<uintptr_t>(mData);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002612}
2613
2614size_t Parcel::ipcDataSize() const
2615{
2616 return (mDataSize > mDataPos ? mDataSize : mDataPos);
2617}
2618
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002619uintptr_t Parcel::ipcObjects() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002620{
Frederick Mayled3c595a2022-05-09 23:02:54 +00002621 if (const auto* kernelFields = maybeKernelFields()) {
2622 return reinterpret_cast<uintptr_t>(kernelFields->mObjects);
2623 }
2624 return 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002625}
2626
2627size_t Parcel::ipcObjectsCount() const
2628{
Frederick Mayled3c595a2022-05-09 23:02:54 +00002629 if (const auto* kernelFields = maybeKernelFields()) {
2630 return kernelFields->mObjectsSize;
2631 }
2632 return 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002633}
2634
Frederick Mayled3c595a2022-05-09 23:02:54 +00002635void Parcel::ipcSetDataReference(const uint8_t* data, size_t dataSize, const binder_size_t* objects,
2636 size_t objectsCount, release_func relFunc) {
Steven Moreland438cce82021-04-02 18:04:08 +00002637 // this code uses 'mOwner == nullptr' to understand whether it owns memory
2638 LOG_ALWAYS_FATAL_IF(relFunc == nullptr, "must provide cleanup function");
2639
Steven Morelandceed9bb2020-12-17 01:01:06 +00002640 freeData();
2641
Frederick Mayled3c595a2022-05-09 23:02:54 +00002642 auto* kernelFields = maybeKernelFields();
2643 LOG_ALWAYS_FATAL_IF(kernelFields == nullptr); // guaranteed by freeData.
2644
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002645 mData = const_cast<uint8_t*>(data);
2646 mDataSize = mDataCapacity = dataSize;
Frederick Mayled3c595a2022-05-09 23:02:54 +00002647 kernelFields->mObjects = const_cast<binder_size_t*>(objects);
2648 kernelFields->mObjectsSize = kernelFields->mObjectsCapacity = objectsCount;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002649 mOwner = relFunc;
Steven Morelandceed9bb2020-12-17 01:01:06 +00002650
Andrei Homescua9cca9c2022-05-03 04:48:01 +00002651#ifdef BINDER_WITH_KERNEL_IPC
Steven Morelandceed9bb2020-12-17 01:01:06 +00002652 binder_size_t minOffset = 0;
Frederick Mayled3c595a2022-05-09 23:02:54 +00002653 for (size_t i = 0; i < kernelFields->mObjectsSize; i++) {
2654 binder_size_t offset = kernelFields->mObjects[i];
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002655 if (offset < minOffset) {
Dan Albert3bdc5b82014-11-20 11:50:23 -08002656 ALOGE("%s: bad object offset %" PRIu64 " < %" PRIu64 "\n",
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002657 __func__, (uint64_t)offset, (uint64_t)minOffset);
Frederick Mayled3c595a2022-05-09 23:02:54 +00002658 kernelFields->mObjectsSize = 0;
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002659 break;
2660 }
Martijn Coenen82c75312019-07-24 15:18:30 +02002661 const flat_binder_object* flat
2662 = reinterpret_cast<const flat_binder_object*>(mData + offset);
2663 uint32_t type = flat->hdr.type;
2664 if (!(type == BINDER_TYPE_BINDER || type == BINDER_TYPE_HANDLE ||
2665 type == BINDER_TYPE_FD)) {
2666 // We should never receive other types (eg BINDER_TYPE_FDA) as long as we don't support
2667 // them in libbinder. If we do receive them, it probably means a kernel bug; try to
Steven Morelandf2e0a952021-11-01 18:17:23 -07002668 // recover gracefully by clearing out the objects.
Martijn Coenen82c75312019-07-24 15:18:30 +02002669 android_errorWriteLog(0x534e4554, "135930648");
Steven Morelandf2e0a952021-11-01 18:17:23 -07002670 android_errorWriteLog(0x534e4554, "203847542");
Martijn Coenen82c75312019-07-24 15:18:30 +02002671 ALOGE("%s: unsupported type object (%" PRIu32 ") at offset %" PRIu64 "\n",
2672 __func__, type, (uint64_t)offset);
Steven Morelandf2e0a952021-11-01 18:17:23 -07002673
2674 // WARNING: callers of ipcSetDataReference need to make sure they
2675 // don't rely on mObjectsSize in their release_func.
Frederick Mayled3c595a2022-05-09 23:02:54 +00002676 kernelFields->mObjectsSize = 0;
Martijn Coenen82c75312019-07-24 15:18:30 +02002677 break;
2678 }
Steven Moreland02f736f2023-06-22 23:03:57 +00002679 if (type == BINDER_TYPE_FD) {
2680 // FDs from the kernel are always owned
2681 FdTag(flat->handle, 0, this);
2682 }
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002683 minOffset = offset + sizeof(flat_binder_object);
2684 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002685 scanForFds();
Andrei Homescua9cca9c2022-05-03 04:48:01 +00002686#else // BINDER_WITH_KERNEL_IPC
2687 LOG_ALWAYS_FATAL_IF(objectsCount != 0,
2688 "Non-zero objects count passed to Parcel with kernel driver disabled");
2689#endif // BINDER_WITH_KERNEL_IPC
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002690}
2691
Frederick Mayleffe9ac22022-06-30 02:07:36 +00002692status_t Parcel::rpcSetDataReference(
2693 const sp<RpcSession>& session, const uint8_t* data, size_t dataSize,
2694 const uint32_t* objectTable, size_t objectTableSize,
2695 std::vector<std::variant<base::unique_fd, base::borrowed_fd>>&& ancillaryFds,
2696 release_func relFunc) {
Frederick Mayled3c595a2022-05-09 23:02:54 +00002697 // this code uses 'mOwner == nullptr' to understand whether it owns memory
2698 LOG_ALWAYS_FATAL_IF(relFunc == nullptr, "must provide cleanup function");
2699
2700 LOG_ALWAYS_FATAL_IF(session == nullptr);
2701
Frederick Mayle694e9732022-07-15 00:24:58 +00002702 if (objectTableSize != ancillaryFds.size()) {
2703 ALOGE("objectTableSize=%zu ancillaryFds.size=%zu", objectTableSize, ancillaryFds.size());
2704 relFunc(data, dataSize, nullptr, 0);
2705 return BAD_VALUE;
2706 }
2707 for (size_t i = 0; i < objectTableSize; i++) {
2708 uint32_t minObjectEnd;
2709 if (__builtin_add_overflow(objectTable[i], sizeof(RpcFields::ObjectType), &minObjectEnd) ||
2710 minObjectEnd >= dataSize) {
2711 ALOGE("received out of range object position: %" PRIu32 " (parcel size is %zu)",
2712 objectTable[i], dataSize);
2713 relFunc(data, dataSize, nullptr, 0);
2714 return BAD_VALUE;
2715 }
2716 }
2717
Frederick Mayled3c595a2022-05-09 23:02:54 +00002718 freeData();
2719 markForRpc(session);
2720
Frederick Mayle69a0c992022-05-26 20:38:39 +00002721 auto* rpcFields = maybeRpcFields();
2722 LOG_ALWAYS_FATAL_IF(rpcFields == nullptr); // guaranteed by markForRpc.
2723
Frederick Mayled3c595a2022-05-09 23:02:54 +00002724 mData = const_cast<uint8_t*>(data);
2725 mDataSize = mDataCapacity = dataSize;
2726 mOwner = relFunc;
Frederick Mayle69a0c992022-05-26 20:38:39 +00002727
Frederick Mayle69a0c992022-05-26 20:38:39 +00002728 rpcFields->mObjectPositions.reserve(objectTableSize);
2729 for (size_t i = 0; i < objectTableSize; i++) {
2730 rpcFields->mObjectPositions.push_back(objectTable[i]);
2731 }
2732 if (!ancillaryFds.empty()) {
2733 rpcFields->mFds = std::make_unique<decltype(rpcFields->mFds)::element_type>();
Frederick Mayleffe9ac22022-06-30 02:07:36 +00002734 *rpcFields->mFds = std::move(ancillaryFds);
Frederick Mayle69a0c992022-05-26 20:38:39 +00002735 }
2736
2737 return OK;
Frederick Mayled3c595a2022-05-09 23:02:54 +00002738}
2739
Pawan Wagh7063b522022-09-28 18:52:26 +00002740void Parcel::print(std::ostream& to, uint32_t /*flags*/) const {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002741 to << "Parcel(";
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002742
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002743 if (errorCheck() != NO_ERROR) {
2744 const status_t err = errorCheck();
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002745 to << "Error: " << (void*)(intptr_t)err << " \"" << strerror(-err) << "\"";
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002746 } else if (dataSize() > 0) {
2747 const uint8_t* DATA = data();
Pawan Wagh7063b522022-09-28 18:52:26 +00002748 to << "\t" << HexDump(DATA, dataSize());
Andrei Homescua9cca9c2022-05-03 04:48:01 +00002749#ifdef BINDER_WITH_KERNEL_IPC
Frederick Mayled3c595a2022-05-09 23:02:54 +00002750 if (const auto* kernelFields = maybeKernelFields()) {
2751 const binder_size_t* OBJS = kernelFields->mObjects;
2752 const size_t N = objectsCount();
2753 for (size_t i = 0; i < N; i++) {
2754 const flat_binder_object* flat =
2755 reinterpret_cast<const flat_binder_object*>(DATA + OBJS[i]);
Pawan Wagh7063b522022-09-28 18:52:26 +00002756 to << "Object #" << i << " @ " << (void*)OBJS[i] << ": "
Frederick Mayled3c595a2022-05-09 23:02:54 +00002757 << TypeCode(flat->hdr.type & 0x7f7f7f00) << " = " << flat->binder;
2758 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002759 }
Andrei Homescua9cca9c2022-05-03 04:48:01 +00002760#endif // BINDER_WITH_KERNEL_IPC
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002761 } else {
2762 to << "NULL";
2763 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002764
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002765 to << ")";
2766}
2767
2768void Parcel::releaseObjects()
2769{
Frederick Mayled3c595a2022-05-09 23:02:54 +00002770 auto* kernelFields = maybeKernelFields();
2771 if (kernelFields == nullptr) {
2772 return;
2773 }
2774
Andrei Homescua9cca9c2022-05-03 04:48:01 +00002775#ifdef BINDER_WITH_KERNEL_IPC
Frederick Mayled3c595a2022-05-09 23:02:54 +00002776 size_t i = kernelFields->mObjectsSize;
Martijn Coenen69390d42018-10-22 15:18:10 +02002777 if (i == 0) {
2778 return;
2779 }
2780 sp<ProcessState> proc(ProcessState::self());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002781 uint8_t* const data = mData;
Frederick Mayled3c595a2022-05-09 23:02:54 +00002782 binder_size_t* const objects = kernelFields->mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002783 while (i > 0) {
2784 i--;
Frederick Mayled3c595a2022-05-09 23:02:54 +00002785 const flat_binder_object* flat = reinterpret_cast<flat_binder_object*>(data + objects[i]);
Steven Morelandc673f1f2021-10-07 18:23:35 -07002786 release_object(proc, *flat, this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002787 }
Andrei Homescua9cca9c2022-05-03 04:48:01 +00002788#endif // BINDER_WITH_KERNEL_IPC
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002789}
2790
2791void Parcel::acquireObjects()
2792{
Frederick Mayled3c595a2022-05-09 23:02:54 +00002793 auto* kernelFields = maybeKernelFields();
2794 if (kernelFields == nullptr) {
2795 return;
2796 }
2797
Andrei Homescua9cca9c2022-05-03 04:48:01 +00002798#ifdef BINDER_WITH_KERNEL_IPC
Frederick Mayled3c595a2022-05-09 23:02:54 +00002799 size_t i = kernelFields->mObjectsSize;
Martijn Coenen69390d42018-10-22 15:18:10 +02002800 if (i == 0) {
2801 return;
2802 }
2803 const sp<ProcessState> proc(ProcessState::self());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002804 uint8_t* const data = mData;
Frederick Mayled3c595a2022-05-09 23:02:54 +00002805 binder_size_t* const objects = kernelFields->mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002806 while (i > 0) {
2807 i--;
Frederick Mayled3c595a2022-05-09 23:02:54 +00002808 const flat_binder_object* flat = reinterpret_cast<flat_binder_object*>(data + objects[i]);
Steven Morelandc673f1f2021-10-07 18:23:35 -07002809 acquire_object(proc, *flat, this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002810 }
Andrei Homescua9cca9c2022-05-03 04:48:01 +00002811#endif // BINDER_WITH_KERNEL_IPC
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002812}
2813
2814void Parcel::freeData()
2815{
2816 freeDataNoInit();
2817 initState();
2818}
2819
2820void Parcel::freeDataNoInit()
2821{
2822 if (mOwner) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002823 LOG_ALLOC("Parcel %p: freeing other owner data", this);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002824 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
Frederick Mayled3c595a2022-05-09 23:02:54 +00002825 auto* kernelFields = maybeKernelFields();
Frederick Mayle53b6ffe2022-07-15 20:14:01 +00002826 // Close FDs before freeing, otherwise they will leak for kernel binder.
2827 closeFileDescriptors();
2828 mOwner(mData, mDataSize, kernelFields ? kernelFields->mObjects : nullptr,
Frederick Mayled3c595a2022-05-09 23:02:54 +00002829 kernelFields ? kernelFields->mObjectsSize : 0);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002830 } else {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002831 LOG_ALLOC("Parcel %p: freeing allocated data", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002832 releaseObjects();
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002833 if (mData) {
2834 LOG_ALLOC("Parcel %p: freeing with %zu capacity", this, mDataCapacity);
Jeff Sharkey8994c182020-09-11 12:07:10 -06002835 gParcelGlobalAllocSize -= mDataCapacity;
2836 gParcelGlobalAllocCount--;
Steven Morelandf183fdd2020-10-27 00:12:12 +00002837 if (mDeallocZero) {
2838 zeroMemory(mData, mDataSize);
2839 }
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002840 free(mData);
2841 }
Frederick Mayled3c595a2022-05-09 23:02:54 +00002842 auto* kernelFields = maybeKernelFields();
2843 if (kernelFields && kernelFields->mObjects) free(kernelFields->mObjects);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002844 }
2845}
2846
2847status_t Parcel::growData(size_t len)
2848{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002849 if (len > INT32_MAX) {
2850 // don't accept size_t values which may have come from an
2851 // inadvertent conversion from a negative int.
2852 return BAD_VALUE;
2853 }
2854
Martijn Coenen93fe5182020-01-22 10:46:25 +01002855 if (len > SIZE_MAX - mDataSize) return NO_MEMORY; // overflow
2856 if (mDataSize + len > SIZE_MAX / 3) return NO_MEMORY; // overflow
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002857 size_t newSize = ((mDataSize+len)*3)/2;
2858 return (newSize <= mDataSize)
2859 ? (status_t) NO_MEMORY
Steven Moreland042ae822020-05-27 17:45:17 +00002860 : continueWrite(std::max(newSize, (size_t) 128));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002861}
2862
Steven Morelandf183fdd2020-10-27 00:12:12 +00002863static uint8_t* reallocZeroFree(uint8_t* data, size_t oldCapacity, size_t newCapacity, bool zero) {
2864 if (!zero) {
2865 return (uint8_t*)realloc(data, newCapacity);
2866 }
2867 uint8_t* newData = (uint8_t*)malloc(newCapacity);
2868 if (!newData) {
2869 return nullptr;
2870 }
2871
2872 memcpy(newData, data, std::min(oldCapacity, newCapacity));
2873 zeroMemory(data, oldCapacity);
2874 free(data);
2875 return newData;
2876}
2877
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002878status_t Parcel::restartWrite(size_t desired)
2879{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002880 if (desired > INT32_MAX) {
2881 // don't accept size_t values which may have come from an
2882 // inadvertent conversion from a negative int.
2883 return BAD_VALUE;
2884 }
2885
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002886 if (mOwner) {
2887 freeData();
2888 return continueWrite(desired);
2889 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002890
Steven Morelandf183fdd2020-10-27 00:12:12 +00002891 uint8_t* data = reallocZeroFree(mData, mDataCapacity, desired, mDeallocZero);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002892 if (!data && desired > mDataCapacity) {
2893 mError = NO_MEMORY;
2894 return NO_MEMORY;
2895 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002896
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002897 releaseObjects();
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002898
Devin Moore4a0a55e2020-06-04 13:23:10 -07002899 if (data || desired == 0) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002900 LOG_ALLOC("Parcel %p: restart from %zu to %zu capacity", this, mDataCapacity, desired);
Jeff Sharkey8994c182020-09-11 12:07:10 -06002901 if (mDataCapacity > desired) {
2902 gParcelGlobalAllocSize -= (mDataCapacity - desired);
2903 } else {
2904 gParcelGlobalAllocSize += (desired - mDataCapacity);
2905 }
2906
Colin Cross83ec65e2015-12-08 17:15:50 -08002907 if (!mData) {
2908 gParcelGlobalAllocCount++;
2909 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002910 mData = data;
2911 mDataCapacity = desired;
2912 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002913
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002914 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002915 ALOGV("restartWrite Setting data size of %p to %zu", this, mDataSize);
2916 ALOGV("restartWrite Setting data pos of %p to %zu", this, mDataPos);
2917
Frederick Mayled3c595a2022-05-09 23:02:54 +00002918 if (auto* kernelFields = maybeKernelFields()) {
2919 free(kernelFields->mObjects);
2920 kernelFields->mObjects = nullptr;
2921 kernelFields->mObjectsSize = kernelFields->mObjectsCapacity = 0;
2922 kernelFields->mNextObjectHint = 0;
2923 kernelFields->mObjectsSorted = false;
2924 kernelFields->mHasFds = false;
2925 kernelFields->mFdsKnown = true;
Frederick Mayle69a0c992022-05-26 20:38:39 +00002926 } else if (auto* rpcFields = maybeRpcFields()) {
2927 rpcFields->mObjectPositions.clear();
2928 rpcFields->mFds.reset();
Frederick Mayled3c595a2022-05-09 23:02:54 +00002929 }
Dianne Hackborn8938ed22011-09-28 23:19:47 -04002930 mAllowFds = true;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002931
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002932 return NO_ERROR;
2933}
2934
2935status_t Parcel::continueWrite(size_t desired)
2936{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002937 if (desired > INT32_MAX) {
2938 // don't accept size_t values which may have come from an
2939 // inadvertent conversion from a negative int.
2940 return BAD_VALUE;
2941 }
2942
Frederick Mayled3c595a2022-05-09 23:02:54 +00002943 auto* kernelFields = maybeKernelFields();
Frederick Mayle69a0c992022-05-26 20:38:39 +00002944 auto* rpcFields = maybeRpcFields();
Frederick Mayled3c595a2022-05-09 23:02:54 +00002945
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002946 // If shrinking, first adjust for any objects that appear
2947 // after the new data size.
Frederick Mayle69a0c992022-05-26 20:38:39 +00002948 size_t objectsSize =
2949 kernelFields ? kernelFields->mObjectsSize : rpcFields->mObjectPositions.size();
2950 if (desired < mDataSize) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002951 if (desired == 0) {
2952 objectsSize = 0;
2953 } else {
Frederick Mayle69a0c992022-05-26 20:38:39 +00002954 if (kernelFields) {
2955 while (objectsSize > 0) {
2956 if (kernelFields->mObjects[objectsSize - 1] < desired) break;
2957 objectsSize--;
2958 }
2959 } else {
2960 while (objectsSize > 0) {
2961 if (rpcFields->mObjectPositions[objectsSize - 1] < desired) break;
2962 objectsSize--;
2963 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002964 }
2965 }
2966 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002967
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002968 if (mOwner) {
2969 // If the size is going to zero, just release the owner's data.
2970 if (desired == 0) {
2971 freeData();
2972 return NO_ERROR;
2973 }
2974
2975 // If there is a different owner, we need to take
2976 // posession.
2977 uint8_t* data = (uint8_t*)malloc(desired);
2978 if (!data) {
2979 mError = NO_MEMORY;
2980 return NO_MEMORY;
2981 }
Yi Kong91635562018-06-07 14:38:36 -07002982 binder_size_t* objects = nullptr;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002983
Frederick Mayle69a0c992022-05-26 20:38:39 +00002984 if (kernelFields && objectsSize) {
Nick Kraleviche9881a32015-04-28 16:21:30 -07002985 objects = (binder_size_t*)calloc(objectsSize, sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002986 if (!objects) {
Hyejin Kim3f727c02013-03-09 11:28:54 +09002987 free(data);
2988
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002989 mError = NO_MEMORY;
2990 return NO_MEMORY;
2991 }
2992
2993 // Little hack to only acquire references on objects
2994 // we will be keeping.
Frederick Mayled3c595a2022-05-09 23:02:54 +00002995 size_t oldObjectsSize = kernelFields->mObjectsSize;
2996 kernelFields->mObjectsSize = objectsSize;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002997 acquireObjects();
Frederick Mayled3c595a2022-05-09 23:02:54 +00002998 kernelFields->mObjectsSize = oldObjectsSize;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002999 }
Frederick Mayle69a0c992022-05-26 20:38:39 +00003000 if (rpcFields) {
3001 if (status_t status = truncateRpcObjects(objectsSize); status != OK) {
3002 free(data);
3003 return status;
3004 }
3005 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07003006
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003007 if (mData) {
3008 memcpy(data, mData, mDataSize < desired ? mDataSize : desired);
3009 }
Frederick Mayled3c595a2022-05-09 23:02:54 +00003010 if (objects && kernelFields && kernelFields->mObjects) {
3011 memcpy(objects, kernelFields->mObjects, objectsSize * sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003012 }
Frederick Mayle53b6ffe2022-07-15 20:14:01 +00003013 // ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
3014 if (kernelFields) {
3015 // TODO(b/239222407): This seems wrong. We should only free FDs when
3016 // they are in a truncated section of the parcel.
3017 closeFileDescriptors();
3018 }
3019 mOwner(mData, mDataSize, kernelFields ? kernelFields->mObjects : nullptr,
Frederick Mayled3c595a2022-05-09 23:02:54 +00003020 kernelFields ? kernelFields->mObjectsSize : 0);
Yi Kong91635562018-06-07 14:38:36 -07003021 mOwner = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003022
Dianne Hackborn7e790af2014-11-11 12:22:53 -08003023 LOG_ALLOC("Parcel %p: taking ownership of %zu capacity", this, desired);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08003024 gParcelGlobalAllocSize += desired;
3025 gParcelGlobalAllocCount++;
Dianne Hackborn7e790af2014-11-11 12:22:53 -08003026
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003027 mData = data;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003028 mDataSize = (mDataSize < desired) ? mDataSize : desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07003029 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003030 mDataCapacity = desired;
Frederick Mayled3c595a2022-05-09 23:02:54 +00003031 if (kernelFields) {
3032 kernelFields->mObjects = objects;
3033 kernelFields->mObjectsSize = kernelFields->mObjectsCapacity = objectsSize;
3034 kernelFields->mNextObjectHint = 0;
3035 kernelFields->mObjectsSorted = false;
3036 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003037
3038 } else if (mData) {
Frederick Mayled3c595a2022-05-09 23:02:54 +00003039 if (kernelFields && objectsSize < kernelFields->mObjectsSize) {
Andrei Homescua9cca9c2022-05-03 04:48:01 +00003040#ifdef BINDER_WITH_KERNEL_IPC
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003041 // Need to release refs on any objects we are dropping.
3042 const sp<ProcessState> proc(ProcessState::self());
Frederick Mayled3c595a2022-05-09 23:02:54 +00003043 for (size_t i = objectsSize; i < kernelFields->mObjectsSize; i++) {
3044 const flat_binder_object* flat =
3045 reinterpret_cast<flat_binder_object*>(mData + kernelFields->mObjects[i]);
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07003046 if (flat->hdr.type == BINDER_TYPE_FD) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003047 // will need to rescan because we may have lopped off the only FDs
Frederick Mayled3c595a2022-05-09 23:02:54 +00003048 kernelFields->mFdsKnown = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003049 }
Steven Morelandc673f1f2021-10-07 18:23:35 -07003050 release_object(proc, *flat, this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003051 }
Michael Wachenschwanz6af27a82019-06-03 17:24:51 -07003052
3053 if (objectsSize == 0) {
Frederick Mayled3c595a2022-05-09 23:02:54 +00003054 free(kernelFields->mObjects);
3055 kernelFields->mObjects = nullptr;
3056 kernelFields->mObjectsCapacity = 0;
Michael Wachenschwanz6af27a82019-06-03 17:24:51 -07003057 } else {
3058 binder_size_t* objects =
Frederick Mayled3c595a2022-05-09 23:02:54 +00003059 (binder_size_t*)realloc(kernelFields->mObjects,
3060 objectsSize * sizeof(binder_size_t));
Michael Wachenschwanz6af27a82019-06-03 17:24:51 -07003061 if (objects) {
Frederick Mayled3c595a2022-05-09 23:02:54 +00003062 kernelFields->mObjects = objects;
3063 kernelFields->mObjectsCapacity = objectsSize;
Michael Wachenschwanz6af27a82019-06-03 17:24:51 -07003064 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003065 }
Frederick Mayled3c595a2022-05-09 23:02:54 +00003066 kernelFields->mObjectsSize = objectsSize;
3067 kernelFields->mNextObjectHint = 0;
3068 kernelFields->mObjectsSorted = false;
Andrei Homescua9cca9c2022-05-03 04:48:01 +00003069#else // BINDER_WITH_KERNEL_IPC
3070 LOG_ALWAYS_FATAL("Non-zero numObjects for RPC Parcel");
3071#endif // BINDER_WITH_KERNEL_IPC
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003072 }
Frederick Mayle69a0c992022-05-26 20:38:39 +00003073 if (rpcFields) {
3074 if (status_t status = truncateRpcObjects(objectsSize); status != OK) {
3075 return status;
3076 }
3077 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003078
3079 // We own the data, so we can just do a realloc().
3080 if (desired > mDataCapacity) {
Steven Morelandf183fdd2020-10-27 00:12:12 +00003081 uint8_t* data = reallocZeroFree(mData, mDataCapacity, desired, mDeallocZero);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003082 if (data) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08003083 LOG_ALLOC("Parcel %p: continue from %zu to %zu capacity", this, mDataCapacity,
3084 desired);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08003085 gParcelGlobalAllocSize += desired;
3086 gParcelGlobalAllocSize -= mDataCapacity;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003087 mData = data;
3088 mDataCapacity = desired;
Ganesh Mahendranade89892017-09-28 16:56:03 +08003089 } else {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003090 mError = NO_MEMORY;
3091 return NO_MEMORY;
3092 }
3093 } else {
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07003094 if (mDataSize > desired) {
3095 mDataSize = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07003096 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07003097 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003098 if (mDataPos > desired) {
3099 mDataPos = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07003100 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003101 }
3102 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07003103
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003104 } else {
3105 // This is the first data. Easy!
3106 uint8_t* data = (uint8_t*)malloc(desired);
3107 if (!data) {
3108 mError = NO_MEMORY;
3109 return NO_MEMORY;
3110 }
Hyejin Kim3f727c02013-03-09 11:28:54 +09003111
Frederick Mayled3c595a2022-05-09 23:02:54 +00003112 if (!(mDataCapacity == 0 &&
3113 (kernelFields == nullptr ||
3114 (kernelFields->mObjects == nullptr && kernelFields->mObjectsCapacity == 0)))) {
3115 ALOGE("continueWrite: %zu/%p/%zu/%zu", mDataCapacity,
3116 kernelFields ? kernelFields->mObjects : nullptr,
3117 kernelFields ? kernelFields->mObjectsCapacity : 0, desired);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003118 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07003119
Dianne Hackborn7e790af2014-11-11 12:22:53 -08003120 LOG_ALLOC("Parcel %p: allocating with %zu capacity", this, desired);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08003121 gParcelGlobalAllocSize += desired;
3122 gParcelGlobalAllocCount++;
Dianne Hackborn7e790af2014-11-11 12:22:53 -08003123
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003124 mData = data;
3125 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07003126 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
3127 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003128 mDataCapacity = desired;
3129 }
3130
3131 return NO_ERROR;
3132}
3133
Frederick Mayle69a0c992022-05-26 20:38:39 +00003134status_t Parcel::truncateRpcObjects(size_t newObjectsSize) {
3135 auto* rpcFields = maybeRpcFields();
3136 if (newObjectsSize == 0) {
3137 rpcFields->mObjectPositions.clear();
3138 if (rpcFields->mFds) {
3139 rpcFields->mFds->clear();
3140 }
3141 return OK;
3142 }
3143 while (rpcFields->mObjectPositions.size() > newObjectsSize) {
3144 uint32_t pos = rpcFields->mObjectPositions.back();
3145 rpcFields->mObjectPositions.pop_back();
3146 const auto type = *reinterpret_cast<const RpcFields::ObjectType*>(mData + pos);
3147 if (type == RpcFields::TYPE_NATIVE_FILE_DESCRIPTOR) {
3148 const auto fdIndex =
3149 *reinterpret_cast<const int32_t*>(mData + pos + sizeof(RpcFields::ObjectType));
3150 if (rpcFields->mFds == nullptr || fdIndex < 0 ||
3151 static_cast<size_t>(fdIndex) >= rpcFields->mFds->size()) {
3152 ALOGE("RPC Parcel contains invalid file descriptor index. index=%d fd_count=%zu",
3153 fdIndex, rpcFields->mFds ? rpcFields->mFds->size() : 0);
3154 return BAD_VALUE;
3155 }
3156 // In practice, this always removes the last element.
3157 rpcFields->mFds->erase(rpcFields->mFds->begin() + fdIndex);
3158 }
3159 }
3160 return OK;
3161}
3162
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003163void Parcel::initState()
3164{
Dianne Hackborn7e790af2014-11-11 12:22:53 -08003165 LOG_ALLOC("Parcel %p: initState", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003166 mError = NO_ERROR;
Yi Kong91635562018-06-07 14:38:36 -07003167 mData = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003168 mDataSize = 0;
3169 mDataCapacity = 0;
3170 mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07003171 ALOGV("initState Setting data size of %p to %zu", this, mDataSize);
3172 ALOGV("initState Setting data pos of %p to %zu", this, mDataPos);
Frederick Mayled3c595a2022-05-09 23:02:54 +00003173 mVariantFields.emplace<KernelFields>();
Steven Moreland6e5a7752019-08-05 20:30:14 -07003174 mAllowFds = true;
Steven Morelandf183fdd2020-10-27 00:12:12 +00003175 mDeallocZero = false;
Yi Kong91635562018-06-07 14:38:36 -07003176 mOwner = nullptr;
Pawan Wagh104654a2022-11-15 21:18:42 +00003177 mEnforceNoDataAvail = true;
Steven Moreland3a667492023-06-02 21:41:39 +00003178 mServiceFuzzing = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003179}
3180
Bernardo Rufinobbbd88d2021-10-15 14:54:30 +01003181void Parcel::scanForFds() const {
Frederick Mayled3c595a2022-05-09 23:02:54 +00003182 auto* kernelFields = maybeKernelFields();
3183 if (kernelFields == nullptr) {
3184 return;
3185 }
3186 status_t status = hasFileDescriptorsInRange(0, dataSize(), &kernelFields->mHasFds);
Bernardo Rufinobbbd88d2021-10-15 14:54:30 +01003187 ALOGE_IF(status != NO_ERROR, "Error %d calling hasFileDescriptorsInRange()", status);
Frederick Mayled3c595a2022-05-09 23:02:54 +00003188 kernelFields->mFdsKnown = true;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003189}
3190
Andrei Homescua9cca9c2022-05-03 04:48:01 +00003191#ifdef BINDER_WITH_KERNEL_IPC
Dan Sandleraa5c2342015-04-10 10:08:45 -04003192size_t Parcel::getBlobAshmemSize() const
3193{
Adrian Roos6bb31142015-10-22 16:46:12 -07003194 // This used to return the size of all blobs that were written to ashmem, now we're returning
3195 // the ashmem currently referenced by this Parcel, which should be equivalent.
Steven Morelandc673f1f2021-10-07 18:23:35 -07003196 // TODO(b/202029388): Remove method once ABI can be changed.
3197 return getOpenAshmemSize();
Dan Sandleraa5c2342015-04-10 10:08:45 -04003198}
3199
Adrian Rooscbf37262015-10-22 16:12:53 -07003200size_t Parcel::getOpenAshmemSize() const
3201{
Frederick Mayled3c595a2022-05-09 23:02:54 +00003202 auto* kernelFields = maybeKernelFields();
3203 if (kernelFields == nullptr) {
3204 return 0;
3205 }
3206
Steven Morelandc673f1f2021-10-07 18:23:35 -07003207 size_t openAshmemSize = 0;
Frederick Mayled3c595a2022-05-09 23:02:54 +00003208 for (size_t i = 0; i < kernelFields->mObjectsSize; i++) {
Steven Morelandc673f1f2021-10-07 18:23:35 -07003209 const flat_binder_object* flat =
Frederick Mayled3c595a2022-05-09 23:02:54 +00003210 reinterpret_cast<const flat_binder_object*>(mData + kernelFields->mObjects[i]);
Steven Morelandc673f1f2021-10-07 18:23:35 -07003211
3212 // cookie is compared against zero for historical reasons
3213 // > obj.cookie = takeOwnership ? 1 : 0;
3214 if (flat->hdr.type == BINDER_TYPE_FD && flat->cookie != 0 && ashmem_valid(flat->handle)) {
3215 int size = ashmem_get_size_region(flat->handle);
3216 if (__builtin_add_overflow(openAshmemSize, size, &openAshmemSize)) {
3217 ALOGE("Overflow when computing ashmem size.");
3218 return SIZE_MAX;
3219 }
3220 }
3221 }
3222 return openAshmemSize;
Jeff Brown5707dbf2011-09-23 21:17:56 -07003223}
Andrei Homescua9cca9c2022-05-03 04:48:01 +00003224#endif // BINDER_WITH_KERNEL_IPC
Jeff Brown5707dbf2011-09-23 21:17:56 -07003225
3226// --- Parcel::Blob ---
3227
3228Parcel::Blob::Blob() :
Yi Kong91635562018-06-07 14:38:36 -07003229 mFd(-1), mData(nullptr), mSize(0), mMutable(false) {
Jeff Brown5707dbf2011-09-23 21:17:56 -07003230}
3231
3232Parcel::Blob::~Blob() {
3233 release();
3234}
3235
3236void Parcel::Blob::release() {
Jeff Brown13b16042014-11-11 16:44:25 -08003237 if (mFd != -1 && mData) {
Jeff Brown5707dbf2011-09-23 21:17:56 -07003238 ::munmap(mData, mSize);
3239 }
3240 clear();
3241}
3242
Jeff Brown13b16042014-11-11 16:44:25 -08003243void Parcel::Blob::init(int fd, void* data, size_t size, bool isMutable) {
3244 mFd = fd;
Jeff Brown5707dbf2011-09-23 21:17:56 -07003245 mData = data;
3246 mSize = size;
Jeff Brown13b16042014-11-11 16:44:25 -08003247 mMutable = isMutable;
Jeff Brown5707dbf2011-09-23 21:17:56 -07003248}
3249
3250void Parcel::Blob::clear() {
Jeff Brown13b16042014-11-11 16:44:25 -08003251 mFd = -1;
Yi Kong91635562018-06-07 14:38:36 -07003252 mData = nullptr;
Jeff Brown5707dbf2011-09-23 21:17:56 -07003253 mSize = 0;
Jeff Brown13b16042014-11-11 16:44:25 -08003254 mMutable = false;
Jeff Brown5707dbf2011-09-23 21:17:56 -07003255}
3256
Steven Moreland61ff8492019-09-26 16:05:45 -07003257} // namespace android