blob: 4b7af4573976d2bff05778824c6435f9e43f1998 [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
Tomasz Wasilczyk1d1e9ff2023-10-25 16:17:54 -070020#include <endian.h>
Mark Salyzynabed7f72016-01-27 08:02:48 -080021#include <errno.h>
Mark Salyzyn70f36652016-02-02 10:27:03 -080022#include <fcntl.h>
Mark Salyzynabed7f72016-01-27 08:02:48 -080023#include <inttypes.h>
Mark Salyzyn70f36652016-02-02 10:27:03 -080024#include <pthread.h>
Mark Salyzynabed7f72016-01-27 08:02:48 -080025#include <stdint.h>
26#include <stdio.h>
27#include <stdlib.h>
28#include <sys/mman.h>
Andrei Homescua9cca9c2022-05-03 04:48:01 +000029#include <sys/resource.h>
Mark Salyzyneab2afc2016-01-27 08:02:48 -080030#include <sys/stat.h>
31#include <sys/types.h>
32#include <unistd.h>
Benjamin Lerman38f51272024-04-22 14:27:36 +020033#include <algorithm>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070034
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070035#include <binder/Binder.h>
36#include <binder/BpBinder.h>
Tomasz Wasilczyk1de48a22023-10-30 14:19:19 +000037#include <binder/Functional.h>
Mark Salyzynabed7f72016-01-27 08:02:48 -080038#include <binder/IPCThreadState.h>
39#include <binder/Parcel.h>
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070040#include <binder/ProcessState.h>
Steven Moreland6e5a7752019-08-05 20:30:14 -070041#include <binder/Stability.h>
Christopher Wiley09eb7492015-11-09 15:06:15 -080042#include <binder/Status.h>
Mathias Agopian002e1e52013-05-06 20:20:50 -070043#include <binder/TextOutput.h>
44
Tomasz Wasilczykbca8f942023-10-09 17:42:37 +000045#ifndef BINDER_DISABLE_BLOB
Mark Salyzynabed7f72016-01-27 08:02:48 -080046#include <cutils/ashmem.h>
Tomasz Wasilczykbca8f942023-10-09 17:42:37 +000047#endif
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070048#include <utils/String16.h>
Steven Moreland3af936a2021-03-26 03:05:38 +000049#include <utils/String8.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
Tomasz Wasilczyk1de48a22023-10-30 14:19:19 +0000100using namespace android::binder::impl;
Tomasz Wasilczyk639490b2023-11-01 13:49:41 -0700101using binder::borrowed_fd;
102using binder::unique_fd;
Tomasz Wasilczyk1de48a22023-10-30 14:19:19 +0000103
Steven Moreland7b102262019-08-01 15:48:43 -0700104// many things compile this into prebuilts on the stack
Steven Moreland90c1f9a2021-05-03 18:27:24 +0000105#ifdef __LP64__
106static_assert(sizeof(Parcel) == 120);
107#else
108static_assert(sizeof(Parcel) == 60);
109#endif
Steven Moreland7b102262019-08-01 15:48:43 -0700110
Jeff Sharkey8994c182020-09-11 12:07:10 -0600111static std::atomic<size_t> gParcelGlobalAllocCount;
112static std::atomic<size_t> gParcelGlobalAllocSize;
Dianne Hackborna4cff882014-11-13 17:07:40 -0800113
Andrei Homescu1519b982022-06-09 02:04:44 +0000114// Maximum number of file descriptors per Parcel.
115constexpr size_t kMaxFds = 1024;
Christopher Tatee4e0ae82016-03-24 16:03:44 -0700116
Jeff Brown13b16042014-11-11 16:44:25 -0800117// Maximum size of a blob to transfer in-place.
Tomasz Wasilczyk83780132023-11-29 21:39:29 -0800118[[maybe_unused]] static const size_t BLOB_INPLACE_LIMIT = 16 * 1024;
Jeff Brown13b16042014-11-11 16:44:25 -0800119
Steven Moreland02f736f2023-06-22 23:03:57 +0000120#if defined(__BIONIC__)
121static void FdTag(int fd, const void* old_addr, const void* new_addr) {
122 if (android_fdsan_exchange_owner_tag) {
123 uint64_t old_tag = android_fdsan_create_owner_tag(ANDROID_FDSAN_OWNER_TYPE_PARCEL,
124 reinterpret_cast<uint64_t>(old_addr));
125 uint64_t new_tag = android_fdsan_create_owner_tag(ANDROID_FDSAN_OWNER_TYPE_PARCEL,
126 reinterpret_cast<uint64_t>(new_addr));
127 android_fdsan_exchange_owner_tag(fd, old_tag, new_tag);
128 }
129}
130static void FdTagClose(int fd, const void* addr) {
131 if (android_fdsan_close_with_tag) {
132 uint64_t tag = android_fdsan_create_owner_tag(ANDROID_FDSAN_OWNER_TYPE_PARCEL,
133 reinterpret_cast<uint64_t>(addr));
134 android_fdsan_close_with_tag(fd, tag);
135 } else {
136 close(fd);
137 }
138}
139#else
140static void FdTag(int fd, const void* old_addr, const void* new_addr) {
141 (void)fd;
142 (void)old_addr;
143 (void)new_addr;
144}
145static void FdTagClose(int fd, const void* addr) {
146 (void)addr;
147 close(fd);
148}
149#endif
150
Jeff Brown13b16042014-11-11 16:44:25 -0800151enum {
152 BLOB_INPLACE = 0,
153 BLOB_ASHMEM_IMMUTABLE = 1,
154 BLOB_ASHMEM_MUTABLE = 2,
155};
156
Andrei Homescua9cca9c2022-05-03 04:48:01 +0000157#ifdef BINDER_WITH_KERNEL_IPC
Steven Morelandc673f1f2021-10-07 18:23:35 -0700158static void acquire_object(const sp<ProcessState>& proc, const flat_binder_object& obj,
159 const void* who) {
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700160 switch (obj.hdr.type) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700161 case BINDER_TYPE_BINDER:
162 if (obj.binder) {
yuxic05af3b2021-08-24 02:52:15 +0000163 LOG_REFS("Parcel %p acquiring reference on local %llu", who, obj.cookie);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800164 reinterpret_cast<IBinder*>(obj.cookie)->incStrong(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700165 }
166 return;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700167 case BINDER_TYPE_HANDLE: {
168 const sp<IBinder> b = proc->getStrongProxyForHandle(obj.handle);
Yi Kong91635562018-06-07 14:38:36 -0700169 if (b != nullptr) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700170 LOG_REFS("Parcel %p acquiring reference on remote %p", who, b.get());
171 b->incStrong(who);
172 }
173 return;
174 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700175 case BINDER_TYPE_FD: {
Steven Moreland02f736f2023-06-22 23:03:57 +0000176 if (obj.cookie != 0) { // owned
177 FdTag(obj.handle, nullptr, who);
178 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700179 return;
180 }
181 }
182
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700183 ALOGD("Invalid object type 0x%08x", obj.hdr.type);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700184}
185
Steven Morelandc673f1f2021-10-07 18:23:35 -0700186static void release_object(const sp<ProcessState>& proc, const flat_binder_object& obj,
187 const void* who) {
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700188 switch (obj.hdr.type) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700189 case BINDER_TYPE_BINDER:
190 if (obj.binder) {
yuxic05af3b2021-08-24 02:52:15 +0000191 LOG_REFS("Parcel %p releasing reference on local %llu", who, obj.cookie);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800192 reinterpret_cast<IBinder*>(obj.cookie)->decStrong(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700193 }
194 return;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700195 case BINDER_TYPE_HANDLE: {
196 const sp<IBinder> b = proc->getStrongProxyForHandle(obj.handle);
Yi Kong91635562018-06-07 14:38:36 -0700197 if (b != nullptr) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700198 LOG_REFS("Parcel %p releasing reference on remote %p", who, b.get());
199 b->decStrong(who);
200 }
201 return;
202 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700203 case BINDER_TYPE_FD: {
Steven Moreland02f736f2023-06-22 23:03:57 +0000204 // note: this path is not used when mOwner, so the tag is also released
205 // in 'closeFileDescriptors'
Mark Salyzynb454d8f2016-01-27 08:02:48 -0800206 if (obj.cookie != 0) { // owned
Steven Moreland02f736f2023-06-22 23:03:57 +0000207 FdTagClose(obj.handle, who);
Adrian Rooscbf37262015-10-22 16:12:53 -0700208 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700209 return;
210 }
211 }
212
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700213 ALOGE("Invalid object type 0x%08x", obj.hdr.type);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700214}
Andrei Homescua9cca9c2022-05-03 04:48:01 +0000215#endif // BINDER_WITH_KERNEL_IPC
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700216
Tomasz Wasilczyk639490b2023-11-01 13:49:41 -0700217static int toRawFd(const std::variant<unique_fd, borrowed_fd>& v) {
Frederick Mayle69a0c992022-05-26 20:38:39 +0000218 return std::visit([](const auto& fd) { return fd.get(); }, v);
219}
220
Frederick Mayled3c595a2022-05-09 23:02:54 +0000221Parcel::RpcFields::RpcFields(const sp<RpcSession>& session) : mSession(session) {
222 LOG_ALWAYS_FATAL_IF(mSession == nullptr);
223}
224
Steven Moreland34b48cb2020-12-01 22:45:38 +0000225status_t Parcel::finishFlattenBinder(const sp<IBinder>& binder)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700226{
Steven Moreland6e5a7752019-08-05 20:30:14 -0700227 internal::Stability::tryMarkCompilationUnit(binder.get());
Steven Moreland16a41062021-07-23 13:35:25 -0700228 int16_t rep = internal::Stability::getRepr(binder.get());
Steven Moreland14e4cfa2021-06-03 21:40:45 +0000229 return writeInt32(rep);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700230}
231
Steven Morelanda86a3562019-08-01 23:28:34 +0000232status_t Parcel::finishUnflattenBinder(
233 const sp<IBinder>& binder, sp<IBinder>* out) const
234{
235 int32_t stability;
236 status_t status = readInt32(&stability);
237 if (status != OK) return status;
238
Steven Moreland14e4cfa2021-06-03 21:40:45 +0000239 status = internal::Stability::setRepr(binder.get(), static_cast<int16_t>(stability),
240 true /*log*/);
Steven Morelanda86a3562019-08-01 23:28:34 +0000241 if (status != OK) return status;
242
243 *out = binder;
244 return OK;
245}
246
Andrei Homescua9cca9c2022-05-03 04:48:01 +0000247#ifdef BINDER_WITH_KERNEL_IPC
Steven Morelandbf1915b2020-07-16 22:43:02 +0000248static constexpr inline int schedPolicyMask(int policy, int priority) {
249 return (priority & FLAT_BINDER_FLAG_PRIORITY_MASK) | ((policy & 3) << FLAT_BINDER_FLAG_SCHED_POLICY_SHIFT);
250}
Andrei Homescua9cca9c2022-05-03 04:48:01 +0000251#endif // BINDER_WITH_KERNEL_IPC
Steven Morelandbf1915b2020-07-16 22:43:02 +0000252
Kalesh Singhd67c8e82020-12-29 15:46:25 -0500253status_t Parcel::flattenBinder(const sp<IBinder>& binder) {
254 BBinder* local = nullptr;
255 if (binder) local = binder->localBinder();
256 if (local) local->setParceled();
257
Frederick Mayled3c595a2022-05-09 23:02:54 +0000258 if (const auto* rpcFields = maybeRpcFields()) {
Steven Moreland5553ac42020-11-11 02:14:45 +0000259 if (binder) {
sandeepbandaruaa9d3a32024-02-22 17:16:53 +0000260 status_t status = writeInt32(RpcFields::TYPE_BINDER); // non-null
Steven Moreland5553ac42020-11-11 02:14:45 +0000261 if (status != OK) return status;
Steven Moreland5623d1a2021-09-10 15:45:34 -0700262 uint64_t address;
Steven Morelanda5036f02021-06-08 02:26:57 +0000263 // TODO(b/167966510): need to undo this if the Parcel is not sent
Frederick Mayled3c595a2022-05-09 23:02:54 +0000264 status = rpcFields->mSession->state()->onBinderLeaving(rpcFields->mSession, binder,
265 &address);
Steven Moreland5553ac42020-11-11 02:14:45 +0000266 if (status != OK) return status;
Steven Moreland5623d1a2021-09-10 15:45:34 -0700267 status = writeUint64(address);
Steven Moreland5553ac42020-11-11 02:14:45 +0000268 if (status != OK) return status;
269 } else {
sandeepbandaruaa9d3a32024-02-22 17:16:53 +0000270 status_t status = writeInt32(RpcFields::TYPE_BINDER_NULL); // null
Steven Moreland5553ac42020-11-11 02:14:45 +0000271 if (status != OK) return status;
272 }
273 return finishFlattenBinder(binder);
274 }
275
Andrei Homescua9cca9c2022-05-03 04:48:01 +0000276#ifdef BINDER_WITH_KERNEL_IPC
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700277 flat_binder_object obj;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700278
Steven Morelandbf1915b2020-07-16 22:43:02 +0000279 int schedBits = 0;
280 if (!IPCThreadState::self()->backgroundSchedulingDisabled()) {
281 schedBits = schedPolicyMask(SCHED_NORMAL, 19);
Martijn Coenen2b631742017-05-05 11:16:59 -0700282 }
283
Yi Kong91635562018-06-07 14:38:36 -0700284 if (binder != nullptr) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700285 if (!local) {
286 BpBinder *proxy = binder->remoteBinder();
Yi Kong91635562018-06-07 14:38:36 -0700287 if (proxy == nullptr) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000288 ALOGE("null proxy");
Steven Moreland5553ac42020-11-11 02:14:45 +0000289 } else {
290 if (proxy->isRpcBinder()) {
Steven Morelanda9231112021-09-22 10:08:14 -0700291 ALOGE("Sending a socket binder over kernel binder is prohibited");
Steven Moreland5553ac42020-11-11 02:14:45 +0000292 return INVALID_OPERATION;
293 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700294 }
Steven Moreland99157622021-09-13 16:27:34 -0700295 const int32_t handle = proxy ? proxy->getPrivateAccessor().binderHandle() : 0;
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700296 obj.hdr.type = BINDER_TYPE_HANDLE;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -0800297 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
Steven Moreland49c27532022-03-01 10:21:07 +0000298 obj.flags = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700299 obj.handle = handle;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800300 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700301 } else {
Steven Morelandbf1915b2020-07-16 22:43:02 +0000302 int policy = local->getMinSchedulerPolicy();
303 int priority = local->getMinSchedulerPriority();
304
305 if (policy != 0 || priority != 0) {
306 // override value, since it is set explicitly
307 schedBits = schedPolicyMask(policy, priority);
308 }
Steven Moreland49c27532022-03-01 10:21:07 +0000309 obj.flags = FLAT_BINDER_FLAG_ACCEPTS_FDS;
Steven Morelandf0212002018-12-26 13:59:23 -0800310 if (local->isRequestingSid()) {
311 obj.flags |= FLAT_BINDER_FLAG_TXN_SECURITY_CTX;
312 }
Steven Morelandcf03cf12020-12-04 02:58:40 +0000313 if (local->isInheritRt()) {
314 obj.flags |= FLAT_BINDER_FLAG_INHERIT_RT;
315 }
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700316 obj.hdr.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800317 obj.binder = reinterpret_cast<uintptr_t>(local->getWeakRefs());
318 obj.cookie = reinterpret_cast<uintptr_t>(local);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700319 }
320 } else {
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700321 obj.hdr.type = BINDER_TYPE_BINDER;
Steven Moreland49c27532022-03-01 10:21:07 +0000322 obj.flags = 0;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800323 obj.binder = 0;
324 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700325 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700326
Steven Morelandbf1915b2020-07-16 22:43:02 +0000327 obj.flags |= schedBits;
328
Steven Moreland34b48cb2020-12-01 22:45:38 +0000329 status_t status = writeObject(obj, false);
330 if (status != OK) return status;
331
332 return finishFlattenBinder(binder);
Andrei Homescua9cca9c2022-05-03 04:48:01 +0000333#else // BINDER_WITH_KERNEL_IPC
334 LOG_ALWAYS_FATAL("Binder kernel driver disabled at build time");
335 return INVALID_OPERATION;
336#endif // BINDER_WITH_KERNEL_IPC
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700337}
338
Steven Morelanda86a3562019-08-01 23:28:34 +0000339status_t Parcel::unflattenBinder(sp<IBinder>* out) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700340{
Frederick Mayled3c595a2022-05-09 23:02:54 +0000341 if (const auto* rpcFields = maybeRpcFields()) {
Steven Moreland5623d1a2021-09-10 15:45:34 -0700342 int32_t isPresent;
343 status_t status = readInt32(&isPresent);
Steven Moreland5553ac42020-11-11 02:14:45 +0000344 if (status != OK) return status;
345
346 sp<IBinder> binder;
347
Steven Moreland5623d1a2021-09-10 15:45:34 -0700348 if (isPresent & 1) {
349 uint64_t addr;
350 if (status_t status = readUint64(&addr); status != OK) return status;
Frederick Mayled3c595a2022-05-09 23:02:54 +0000351 if (status_t status =
352 rpcFields->mSession->state()->onBinderEntering(rpcFields->mSession, addr,
353 &binder);
Steven Moreland7227c8a2021-06-02 00:24:32 +0000354 status != OK)
355 return status;
Frederick Mayled3c595a2022-05-09 23:02:54 +0000356 if (status_t status =
357 rpcFields->mSession->state()->flushExcessBinderRefs(rpcFields->mSession,
358 addr, binder);
Steven Morelandd8083312021-09-22 13:37:10 -0700359 status != OK)
360 return status;
Steven Moreland5553ac42020-11-11 02:14:45 +0000361 }
362
363 return finishUnflattenBinder(binder, out);
364 }
365
Andrei Homescua9cca9c2022-05-03 04:48:01 +0000366#ifdef BINDER_WITH_KERNEL_IPC
Steven Morelanda86a3562019-08-01 23:28:34 +0000367 const flat_binder_object* flat = readObject(false);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700368
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700369 if (flat) {
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700370 switch (flat->hdr.type) {
Steven Morelanda86a3562019-08-01 23:28:34 +0000371 case BINDER_TYPE_BINDER: {
Steven Moreland1a3a8ef2021-04-02 02:52:46 +0000372 sp<IBinder> binder =
373 sp<IBinder>::fromExisting(reinterpret_cast<IBinder*>(flat->cookie));
Steven Morelanda86a3562019-08-01 23:28:34 +0000374 return finishUnflattenBinder(binder, out);
375 }
376 case BINDER_TYPE_HANDLE: {
377 sp<IBinder> binder =
378 ProcessState::self()->getStrongProxyForHandle(flat->handle);
379 return finishUnflattenBinder(binder, out);
380 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700381 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700382 }
383 return BAD_TYPE;
Andrei Homescua9cca9c2022-05-03 04:48:01 +0000384#else // BINDER_WITH_KERNEL_IPC
385 LOG_ALWAYS_FATAL("Binder kernel driver disabled at build time");
386 return INVALID_OPERATION;
387#endif // BINDER_WITH_KERNEL_IPC
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700388}
389
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700390// ---------------------------------------------------------------------------
391
392Parcel::Parcel()
393{
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800394 LOG_ALLOC("Parcel %p: constructing", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700395 initState();
396}
397
398Parcel::~Parcel()
399{
400 freeDataNoInit();
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800401 LOG_ALLOC("Parcel %p: destroyed", this);
402}
403
404size_t Parcel::getGlobalAllocSize() {
Jeff Sharkey8994c182020-09-11 12:07:10 -0600405 return gParcelGlobalAllocSize.load();
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800406}
407
408size_t Parcel::getGlobalAllocCount() {
Jeff Sharkey8994c182020-09-11 12:07:10 -0600409 return gParcelGlobalAllocCount.load();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700410}
411
412const uint8_t* Parcel::data() const
413{
414 return mData;
415}
416
417size_t Parcel::dataSize() const
418{
419 return (mDataSize > mDataPos ? mDataSize : mDataPos);
420}
421
Pawan Waghd886a442023-01-21 02:16:38 +0000422size_t Parcel::dataBufferSize() const {
423 return mDataSize;
424}
425
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700426size_t Parcel::dataAvail() const
427{
Nick Kralevichcfe27de2015-09-16 09:49:15 -0700428 size_t result = dataSize() - dataPosition();
429 if (result > INT32_MAX) {
Steven Moreland6adf33c2019-09-25 13:18:09 -0700430 LOG_ALWAYS_FATAL("result too big: %zu", result);
Nick Kralevichcfe27de2015-09-16 09:49:15 -0700431 }
432 return result;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700433}
434
435size_t Parcel::dataPosition() const
436{
437 return mDataPos;
438}
439
440size_t Parcel::dataCapacity() const
441{
442 return mDataCapacity;
443}
444
445status_t Parcel::setDataSize(size_t size)
446{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700447 if (size > INT32_MAX) {
448 // don't accept size_t values which may have come from an
449 // inadvertent conversion from a negative int.
450 return BAD_VALUE;
451 }
452
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700453 status_t err;
454 err = continueWrite(size);
455 if (err == NO_ERROR) {
456 mDataSize = size;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700457 ALOGV("setDataSize Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700458 }
459 return err;
460}
461
462void Parcel::setDataPosition(size_t pos) const
463{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700464 if (pos > INT32_MAX) {
465 // don't accept size_t values which may have come from an
466 // inadvertent conversion from a negative int.
Steven Moreland6adf33c2019-09-25 13:18:09 -0700467 LOG_ALWAYS_FATAL("pos too big: %zu", pos);
Nick Kralevichb6b14232015-04-02 09:36:02 -0700468 }
469
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700470 mDataPos = pos;
Frederick Mayled3c595a2022-05-09 23:02:54 +0000471 if (const auto* kernelFields = maybeKernelFields()) {
472 kernelFields->mNextObjectHint = 0;
473 kernelFields->mObjectsSorted = false;
474 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700475}
476
477status_t Parcel::setDataCapacity(size_t size)
478{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700479 if (size > INT32_MAX) {
480 // don't accept size_t values which may have come from an
481 // inadvertent conversion from a negative int.
482 return BAD_VALUE;
483 }
484
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -0700485 if (size > mDataCapacity) return continueWrite(size);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700486 return NO_ERROR;
487}
488
489status_t Parcel::setData(const uint8_t* buffer, size_t len)
490{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700491 if (len > INT32_MAX) {
492 // don't accept size_t values which may have come from an
493 // inadvertent conversion from a negative int.
494 return BAD_VALUE;
495 }
496
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700497 status_t err = restartWrite(len);
498 if (err == NO_ERROR) {
499 memcpy(const_cast<uint8_t*>(data()), buffer, len);
500 mDataSize = len;
Frederick Mayled3c595a2022-05-09 23:02:54 +0000501 if (auto* kernelFields = maybeKernelFields()) {
502 kernelFields->mFdsKnown = false;
503 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700504 }
505 return err;
506}
507
Frederick Mayled3c595a2022-05-09 23:02:54 +0000508status_t Parcel::appendFrom(const Parcel* parcel, size_t offset, size_t len) {
509 if (isForRpc() != parcel->isForRpc()) {
Steven Moreland2034eff2021-10-13 11:24:35 -0700510 ALOGE("Cannot append Parcel from one context to another. They may be different formats, "
511 "and objects are specific to a context.");
Steven Moreland67753c32021-04-02 18:45:19 +0000512 return BAD_TYPE;
513 }
Frederick Mayled3c595a2022-05-09 23:02:54 +0000514 if (isForRpc() && maybeRpcFields()->mSession != parcel->maybeRpcFields()->mSession) {
515 ALOGE("Cannot append Parcels from different sessions");
516 return BAD_TYPE;
517 }
Steven Moreland67753c32021-04-02 18:45:19 +0000518
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700519 status_t err;
Frederick Mayled3c595a2022-05-09 23:02:54 +0000520 const uint8_t* data = parcel->mData;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700521 int startPos = mDataPos;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700522
523 if (len == 0) {
524 return NO_ERROR;
525 }
526
Nick Kralevichb6b14232015-04-02 09:36:02 -0700527 if (len > INT32_MAX) {
528 // don't accept size_t values which may have come from an
529 // inadvertent conversion from a negative int.
530 return BAD_VALUE;
531 }
532
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700533 // range checks against the source parcel size
534 if ((offset > parcel->mDataSize)
535 || (len > parcel->mDataSize)
536 || (offset + len > parcel->mDataSize)) {
537 return BAD_VALUE;
538 }
539
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -0700540 if ((mDataSize+len) > mDataCapacity) {
541 // grow data
542 err = growData(len);
543 if (err != NO_ERROR) {
544 return err;
545 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700546 }
547
548 // append data
549 memcpy(mData + mDataPos, data + offset, len);
550 mDataPos += len;
551 mDataSize += len;
552
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400553 err = NO_ERROR;
554
Frederick Mayled3c595a2022-05-09 23:02:54 +0000555 if (auto* kernelFields = maybeKernelFields()) {
Andrei Homescu88012462022-07-07 04:30:05 +0000556#ifdef BINDER_WITH_KERNEL_IPC
Frederick Mayled3c595a2022-05-09 23:02:54 +0000557 auto* otherKernelFields = parcel->maybeKernelFields();
558 LOG_ALWAYS_FATAL_IF(otherKernelFields == nullptr);
559
560 const binder_size_t* objects = otherKernelFields->mObjects;
561 size_t size = otherKernelFields->mObjectsSize;
562 // Count objects in range
563 int firstIndex = -1, lastIndex = -2;
564 for (int i = 0; i < (int)size; i++) {
565 size_t off = objects[i];
566 if ((off >= offset) && (off + sizeof(flat_binder_object) <= offset + len)) {
567 if (firstIndex == -1) {
568 firstIndex = i;
569 }
570 lastIndex = i;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700571 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700572 }
Frederick Mayled3c595a2022-05-09 23:02:54 +0000573 int numObjects = lastIndex - firstIndex + 1;
574 if (numObjects > 0) {
575 const sp<ProcessState> proc(ProcessState::self());
576 // grow objects
577 if (kernelFields->mObjectsCapacity < kernelFields->mObjectsSize + numObjects) {
578 if ((size_t)numObjects > SIZE_MAX - kernelFields->mObjectsSize)
579 return NO_MEMORY; // overflow
580 if (kernelFields->mObjectsSize + numObjects > SIZE_MAX / 3)
581 return NO_MEMORY; // overflow
582 size_t newSize = ((kernelFields->mObjectsSize + numObjects) * 3) / 2;
583 if (newSize > SIZE_MAX / sizeof(binder_size_t)) return NO_MEMORY; // overflow
584 binder_size_t* objects = (binder_size_t*)realloc(kernelFields->mObjects,
585 newSize * sizeof(binder_size_t));
586 if (objects == (binder_size_t*)nullptr) {
587 return NO_MEMORY;
588 }
589 kernelFields->mObjects = objects;
590 kernelFields->mObjectsCapacity = newSize;
591 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700592
Frederick Mayled3c595a2022-05-09 23:02:54 +0000593 // append and acquire objects
594 int idx = kernelFields->mObjectsSize;
595 for (int i = firstIndex; i <= lastIndex; i++) {
596 size_t off = objects[i] - offset + startPos;
597 kernelFields->mObjects[idx++] = off;
598 kernelFields->mObjectsSize++;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700599
Frederick Mayled3c595a2022-05-09 23:02:54 +0000600 flat_binder_object* flat = reinterpret_cast<flat_binder_object*>(mData + off);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700601
Frederick Mayled3c595a2022-05-09 23:02:54 +0000602 if (flat->hdr.type == BINDER_TYPE_FD) {
603 // If this is a file descriptor, we need to dup it so the
604 // new Parcel now owns its own fd, and can declare that we
605 // officially know we have fds.
606 flat->handle = fcntl(flat->handle, F_DUPFD_CLOEXEC, 0);
607 flat->cookie = 1;
608 kernelFields->mHasFds = kernelFields->mFdsKnown = true;
609 if (!mAllowFds) {
610 err = FDS_NOT_ALLOWED;
611 }
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400612 }
Steven Moreland02f736f2023-06-22 23:03:57 +0000613
614 acquire_object(proc, *flat, this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700615 }
616 }
Andrei Homescu88012462022-07-07 04:30:05 +0000617#else
618 LOG_ALWAYS_FATAL("Binder kernel driver disabled at build time");
619 return INVALID_OPERATION;
620#endif // BINDER_WITH_KERNEL_IPC
Frederick Mayle69a0c992022-05-26 20:38:39 +0000621 } else {
622 auto* rpcFields = maybeRpcFields();
623 LOG_ALWAYS_FATAL_IF(rpcFields == nullptr);
624 auto* otherRpcFields = parcel->maybeRpcFields();
625 if (otherRpcFields == nullptr) {
626 return BAD_TYPE;
627 }
628 if (rpcFields->mSession != otherRpcFields->mSession) {
629 return BAD_TYPE;
630 }
631
632 const size_t savedDataPos = mDataPos;
Tomasz Wasilczyk1de48a22023-10-30 14:19:19 +0000633 auto scopeGuard = make_scope_guard([&]() { mDataPos = savedDataPos; });
Frederick Mayle69a0c992022-05-26 20:38:39 +0000634
635 rpcFields->mObjectPositions.reserve(otherRpcFields->mObjectPositions.size());
636 if (otherRpcFields->mFds != nullptr) {
637 if (rpcFields->mFds == nullptr) {
638 rpcFields->mFds = std::make_unique<decltype(rpcFields->mFds)::element_type>();
639 }
640 rpcFields->mFds->reserve(otherRpcFields->mFds->size());
641 }
642 for (size_t i = 0; i < otherRpcFields->mObjectPositions.size(); i++) {
643 const binder_size_t objPos = otherRpcFields->mObjectPositions[i];
644 if (offset <= objPos && objPos < offset + len) {
645 size_t newDataPos = objPos - offset + startPos;
646 rpcFields->mObjectPositions.push_back(newDataPos);
647
648 mDataPos = newDataPos;
649 int32_t objectType;
650 if (status_t status = readInt32(&objectType); status != OK) {
651 return status;
652 }
653 if (objectType != RpcFields::TYPE_NATIVE_FILE_DESCRIPTOR) {
654 continue;
655 }
656
657 if (!mAllowFds) {
658 return FDS_NOT_ALLOWED;
659 }
660
661 // Read FD, duplicate, and add to list.
662 int32_t fdIndex;
663 if (status_t status = readInt32(&fdIndex); status != OK) {
664 return status;
665 }
Andrei Homescufc221502022-10-08 03:51:17 +0000666 int oldFd = toRawFd(otherRpcFields->mFds->at(fdIndex));
Frederick Mayle69a0c992022-05-26 20:38:39 +0000667 // To match kernel binder behavior, we always dup, even if the
668 // FD was unowned in the source parcel.
Andrei Homescufc221502022-10-08 03:51:17 +0000669 int newFd = -1;
Tomasz Wasilczyk0d9dec22023-10-06 20:28:49 +0000670 if (status_t status = binder::os::dupFileDescriptor(oldFd, &newFd); status != OK) {
Andrei Homescufc221502022-10-08 03:51:17 +0000671 ALOGW("Failed to duplicate file descriptor %d: %s", oldFd, strerror(-status));
672 }
Tomasz Wasilczyk639490b2023-11-01 13:49:41 -0700673 rpcFields->mFds->emplace_back(unique_fd(newFd));
Frederick Mayle69a0c992022-05-26 20:38:39 +0000674 // Fixup the index in the data.
675 mDataPos = newDataPos + 4;
676 if (status_t status = writeInt32(rpcFields->mFds->size() - 1); status != OK) {
677 return status;
678 }
679 }
680 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700681 }
682
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400683 return err;
684}
685
Dianne Hackborn15feb9b2017-04-10 15:34:35 -0700686int Parcel::compareData(const Parcel& other) {
687 size_t size = dataSize();
688 if (size != other.dataSize()) {
689 return size < other.dataSize() ? -1 : 1;
690 }
691 return memcmp(data(), other.data(), size);
692}
693
Bernardo Rufino897b1652021-10-08 10:30:20 +0100694status_t Parcel::compareDataInRange(size_t thisOffset, const Parcel& other, size_t otherOffset,
695 size_t len, int* result) const {
696 if (len > INT32_MAX || thisOffset > INT32_MAX || otherOffset > INT32_MAX) {
697 // Don't accept size_t values which may have come from an inadvertent conversion from a
698 // negative int.
699 return BAD_VALUE;
700 }
701 size_t thisLimit;
702 if (__builtin_add_overflow(thisOffset, len, &thisLimit) || thisLimit > mDataSize) {
703 return BAD_VALUE;
704 }
705 size_t otherLimit;
706 if (__builtin_add_overflow(otherOffset, len, &otherLimit) || otherLimit > other.mDataSize) {
707 return BAD_VALUE;
708 }
709 *result = memcmp(data() + thisOffset, other.data() + otherOffset, len);
710 return NO_ERROR;
711}
712
Jeff Brown13b16042014-11-11 16:44:25 -0800713bool Parcel::allowFds() const
714{
715 return mAllowFds;
716}
717
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700718bool Parcel::pushAllowFds(bool allowFds)
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400719{
720 const bool origValue = mAllowFds;
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700721 if (!allowFds) {
722 mAllowFds = false;
723 }
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400724 return origValue;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700725}
726
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700727void Parcel::restoreAllowFds(bool lastValue)
728{
729 mAllowFds = lastValue;
730}
731
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700732bool Parcel::hasFileDescriptors() const
733{
Frederick Mayled3c595a2022-05-09 23:02:54 +0000734 if (const auto* rpcFields = maybeRpcFields()) {
Frederick Mayle69a0c992022-05-26 20:38:39 +0000735 return rpcFields->mFds != nullptr && !rpcFields->mFds->empty();
Frederick Mayled3c595a2022-05-09 23:02:54 +0000736 }
737 auto* kernelFields = maybeKernelFields();
738 if (!kernelFields->mFdsKnown) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700739 scanForFds();
740 }
Frederick Mayled3c595a2022-05-09 23:02:54 +0000741 return kernelFields->mHasFds;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700742}
743
sandeepbandaruaa9d3a32024-02-22 17:16:53 +0000744status_t Parcel::hasBinders(bool* result) const {
745 status_t status = hasBindersInRange(0, dataSize(), result);
746 ALOGE_IF(status != NO_ERROR, "Error %d calling hasBindersInRange()", status);
747 return status;
748}
749
Steven Moreland4b2f18d2022-03-24 00:32:25 +0000750std::vector<sp<IBinder>> Parcel::debugReadAllStrongBinders() const {
751 std::vector<sp<IBinder>> ret;
752
Andrei Homescua9cca9c2022-05-03 04:48:01 +0000753#ifdef BINDER_WITH_KERNEL_IPC
Frederick Mayled3c595a2022-05-09 23:02:54 +0000754 const auto* kernelFields = maybeKernelFields();
755 if (kernelFields == nullptr) {
756 return ret;
757 }
758
Steven Moreland4b2f18d2022-03-24 00:32:25 +0000759 size_t initPosition = dataPosition();
Frederick Mayled3c595a2022-05-09 23:02:54 +0000760 for (size_t i = 0; i < kernelFields->mObjectsSize; i++) {
761 binder_size_t offset = kernelFields->mObjects[i];
Steven Moreland4b2f18d2022-03-24 00:32:25 +0000762 const flat_binder_object* flat =
763 reinterpret_cast<const flat_binder_object*>(mData + offset);
764 if (flat->hdr.type != BINDER_TYPE_BINDER) continue;
765
766 setDataPosition(offset);
767
768 sp<IBinder> binder = readStrongBinder();
769 if (binder != nullptr) ret.push_back(binder);
770 }
771
772 setDataPosition(initPosition);
Andrei Homescua9cca9c2022-05-03 04:48:01 +0000773#endif // BINDER_WITH_KERNEL_IPC
774
Steven Moreland4b2f18d2022-03-24 00:32:25 +0000775 return ret;
776}
777
778std::vector<int> Parcel::debugReadAllFileDescriptors() const {
779 std::vector<int> ret;
780
Frederick Mayle69a0c992022-05-26 20:38:39 +0000781 if (const auto* kernelFields = maybeKernelFields()) {
Andrei Homescua9cca9c2022-05-03 04:48:01 +0000782#ifdef BINDER_WITH_KERNEL_IPC
Frederick Mayle69a0c992022-05-26 20:38:39 +0000783 size_t initPosition = dataPosition();
784 for (size_t i = 0; i < kernelFields->mObjectsSize; i++) {
785 binder_size_t offset = kernelFields->mObjects[i];
786 const flat_binder_object* flat =
787 reinterpret_cast<const flat_binder_object*>(mData + offset);
788 if (flat->hdr.type != BINDER_TYPE_FD) continue;
789
790 setDataPosition(offset);
791
792 int fd = readFileDescriptor();
793 LOG_ALWAYS_FATAL_IF(fd == -1);
794 ret.push_back(fd);
795 }
796 setDataPosition(initPosition);
Andrei Homescua9cca9c2022-05-03 04:48:01 +0000797#else
798 LOG_ALWAYS_FATAL("Binder kernel driver disabled at build time");
799#endif
Frederick Mayle69a0c992022-05-26 20:38:39 +0000800 } else if (const auto* rpcFields = maybeRpcFields(); rpcFields && rpcFields->mFds) {
801 for (const auto& fd : *rpcFields->mFds) {
802 ret.push_back(toRawFd(fd));
803 }
Frederick Mayled3c595a2022-05-09 23:02:54 +0000804 }
805
Steven Moreland4b2f18d2022-03-24 00:32:25 +0000806 return ret;
807}
808
sandeepbandaruaa9d3a32024-02-22 17:16:53 +0000809status_t Parcel::hasBindersInRange(size_t offset, size_t len, bool* result) const {
810 if (len > INT32_MAX || offset > INT32_MAX) {
811 // Don't accept size_t values which may have come from an inadvertent conversion from a
812 // negative int.
813 return BAD_VALUE;
814 }
815 size_t limit;
816 if (__builtin_add_overflow(offset, len, &limit) || limit > mDataSize) {
817 return BAD_VALUE;
818 }
819 *result = false;
820 if (const auto* kernelFields = maybeKernelFields()) {
821#ifdef BINDER_WITH_KERNEL_IPC
822 for (size_t i = 0; i < kernelFields->mObjectsSize; i++) {
823 size_t pos = kernelFields->mObjects[i];
824 if (pos < offset) continue;
825 if (pos + sizeof(flat_binder_object) > offset + len) {
826 if (kernelFields->mObjectsSorted) {
827 break;
828 } else {
829 continue;
830 }
831 }
832 const flat_binder_object* flat =
833 reinterpret_cast<const flat_binder_object*>(mData + pos);
834 if (flat->hdr.type == BINDER_TYPE_BINDER || flat->hdr.type == BINDER_TYPE_HANDLE) {
835 *result = true;
836 break;
837 }
838 }
839#else
840 LOG_ALWAYS_FATAL("Binder kernel driver disabled at build time");
841 return INVALID_OPERATION;
842#endif // BINDER_WITH_KERNEL_IPC
843 } else if (const auto* rpcFields = maybeRpcFields()) {
844 return INVALID_OPERATION;
845 }
846 return NO_ERROR;
847}
848
Bernardo Rufinobbbd88d2021-10-15 14:54:30 +0100849status_t Parcel::hasFileDescriptorsInRange(size_t offset, size_t len, bool* result) const {
Bernardo Rufino22092af2021-10-07 14:09:24 +0100850 if (len > INT32_MAX || offset > INT32_MAX) {
851 // Don't accept size_t values which may have come from an inadvertent conversion from a
852 // negative int.
853 return BAD_VALUE;
854 }
Bernardo Rufinobbbd88d2021-10-15 14:54:30 +0100855 size_t limit;
856 if (__builtin_add_overflow(offset, len, &limit) || limit > mDataSize) {
Bernardo Rufino22092af2021-10-07 14:09:24 +0100857 return BAD_VALUE;
858 }
Bernardo Rufinobbbd88d2021-10-15 14:54:30 +0100859 *result = false;
Frederick Mayle69a0c992022-05-26 20:38:39 +0000860 if (const auto* kernelFields = maybeKernelFields()) {
Andrei Homescua9cca9c2022-05-03 04:48:01 +0000861#ifdef BINDER_WITH_KERNEL_IPC
Frederick Mayle69a0c992022-05-26 20:38:39 +0000862 for (size_t i = 0; i < kernelFields->mObjectsSize; i++) {
863 size_t pos = kernelFields->mObjects[i];
864 if (pos < offset) continue;
865 if (pos + sizeof(flat_binder_object) > offset + len) {
866 if (kernelFields->mObjectsSorted) {
867 break;
868 } else {
869 continue;
870 }
871 }
872 const flat_binder_object* flat =
873 reinterpret_cast<const flat_binder_object*>(mData + pos);
874 if (flat->hdr.type == BINDER_TYPE_FD) {
875 *result = true;
Frederick Mayled3c595a2022-05-09 23:02:54 +0000876 break;
Frederick Mayled3c595a2022-05-09 23:02:54 +0000877 }
Bernardo Rufino22092af2021-10-07 14:09:24 +0100878 }
Andrei Homescua9cca9c2022-05-03 04:48:01 +0000879#else
880 LOG_ALWAYS_FATAL("Binder kernel driver disabled at build time");
881 return INVALID_OPERATION;
882#endif // BINDER_WITH_KERNEL_IPC
Frederick Mayle69a0c992022-05-26 20:38:39 +0000883 } else if (const auto* rpcFields = maybeRpcFields()) {
884 for (uint32_t pos : rpcFields->mObjectPositions) {
885 if (offset <= pos && pos < limit) {
886 const auto* type = reinterpret_cast<const RpcFields::ObjectType*>(mData + pos);
887 if (*type == RpcFields::TYPE_NATIVE_FILE_DESCRIPTOR) {
888 *result = true;
889 break;
890 }
891 }
Bernardo Rufino22092af2021-10-07 14:09:24 +0100892 }
893 }
Bernardo Rufinobbbd88d2021-10-15 14:54:30 +0100894 return NO_ERROR;
Bernardo Rufino22092af2021-10-07 14:09:24 +0100895}
896
Steven Morelandf183fdd2020-10-27 00:12:12 +0000897void Parcel::markSensitive() const
898{
899 mDeallocZero = true;
900}
901
Steven Moreland5553ac42020-11-11 02:14:45 +0000902void Parcel::markForBinder(const sp<IBinder>& binder) {
Steven Moreland1fda67b2021-04-02 18:35:50 +0000903 LOG_ALWAYS_FATAL_IF(mData != nullptr, "format must be set before data is written");
904
Steven Moreland5553ac42020-11-11 02:14:45 +0000905 if (binder && binder->remoteBinder() && binder->remoteBinder()->isRpcBinder()) {
Steven Moreland99157622021-09-13 16:27:34 -0700906 markForRpc(binder->remoteBinder()->getPrivateAccessor().rpcSession());
Steven Moreland5553ac42020-11-11 02:14:45 +0000907 }
908}
909
Steven Morelandc9939062021-05-05 17:57:41 +0000910void Parcel::markForRpc(const sp<RpcSession>& session) {
Steven Moreland1fda67b2021-04-02 18:35:50 +0000911 LOG_ALWAYS_FATAL_IF(mData != nullptr && mOwner == nullptr,
912 "format must be set before data is written OR on IPC data");
913
Frederick Mayled3c595a2022-05-09 23:02:54 +0000914 mVariantFields.emplace<RpcFields>(session);
Steven Moreland5553ac42020-11-11 02:14:45 +0000915}
916
917bool Parcel::isForRpc() const {
Frederick Mayled3c595a2022-05-09 23:02:54 +0000918 return std::holds_alternative<RpcFields>(mVariantFields);
Steven Moreland5553ac42020-11-11 02:14:45 +0000919}
920
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000921void Parcel::updateWorkSourceRequestHeaderPosition() const {
Frederick Mayled3c595a2022-05-09 23:02:54 +0000922 auto* kernelFields = maybeKernelFields();
923 if (kernelFields == nullptr) {
924 return;
925 }
926
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000927 // Only update the request headers once. We only want to point
928 // to the first headers read/written.
Frederick Mayled3c595a2022-05-09 23:02:54 +0000929 if (!kernelFields->mRequestHeaderPresent) {
930 kernelFields->mWorkSourceRequestHeaderPosition = dataPosition();
931 kernelFields->mRequestHeaderPresent = true;
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000932 }
933}
934
Andrei Homescua9cca9c2022-05-03 04:48:01 +0000935#ifdef BINDER_WITH_KERNEL_IPC
Steven Moreland94e63e12023-12-05 20:29:50 +0000936
937#if defined(__ANDROID__)
938
Steven Morelandb6c7e222021-02-18 19:20:14 +0000939#if defined(__ANDROID_VNDK__)
Steven Morelandd70160f2019-07-23 10:20:38 -0700940constexpr int32_t kHeader = B_PACK_CHARS('V', 'N', 'D', 'R');
Yifan Hong70786532021-10-20 21:41:18 -0700941#elif defined(__ANDROID_RECOVERY__)
942constexpr int32_t kHeader = B_PACK_CHARS('R', 'E', 'C', 'O');
Steven Morelandd70160f2019-07-23 10:20:38 -0700943#else
944constexpr int32_t kHeader = B_PACK_CHARS('S', 'Y', 'S', 'T');
945#endif
Steven Moreland94e63e12023-12-05 20:29:50 +0000946
947#else // ANDROID not defined
948
949// If kernel binder is used in new environments, we need to make sure it's separated
950// out and has a separate header.
951constexpr int32_t kHeader = B_PACK_CHARS('U', 'N', 'K', 'N');
952#endif
953
Andrei Homescua9cca9c2022-05-03 04:48:01 +0000954#endif // BINDER_WITH_KERNEL_IPC
Steven Morelandd70160f2019-07-23 10:20:38 -0700955
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700956// Write RPC headers. (previously just the interface token)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700957status_t Parcel::writeInterfaceToken(const String16& interface)
958{
Tomasz Wasilczyk0bfea2d2023-08-11 00:06:51 +0000959 return writeInterfaceToken(interface.c_str(), interface.size());
Steven Morelanddbc76c72020-10-01 18:02:48 +0000960}
961
962status_t Parcel::writeInterfaceToken(const char16_t* str, size_t len) {
Frederick Mayled3c595a2022-05-09 23:02:54 +0000963 if (auto* kernelFields = maybeKernelFields()) {
Andrei Homescua9cca9c2022-05-03 04:48:01 +0000964#ifdef BINDER_WITH_KERNEL_IPC
Steven Moreland3af936a2021-03-26 03:05:38 +0000965 const IPCThreadState* threadState = IPCThreadState::self();
966 writeInt32(threadState->getStrictModePolicy() | STRICT_MODE_PENALTY_GATHER);
967 updateWorkSourceRequestHeaderPosition();
968 writeInt32(threadState->shouldPropagateWorkSource() ? threadState->getCallingWorkSourceUid()
969 : IPCThreadState::kUnsetWorkSource);
970 writeInt32(kHeader);
Andrei Homescua9cca9c2022-05-03 04:48:01 +0000971#else // BINDER_WITH_KERNEL_IPC
972 LOG_ALWAYS_FATAL("Binder kernel driver disabled at build time");
973 return INVALID_OPERATION;
974#endif // BINDER_WITH_KERNEL_IPC
Steven Moreland3af936a2021-03-26 03:05:38 +0000975 }
Steven Morelanddbc76c72020-10-01 18:02:48 +0000976
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700977 // currently the interface identification token is just its name as a string
Steven Morelanddbc76c72020-10-01 18:02:48 +0000978 return writeString16(str, len);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700979}
980
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000981bool Parcel::replaceCallingWorkSourceUid(uid_t uid)
982{
Frederick Mayled3c595a2022-05-09 23:02:54 +0000983 auto* kernelFields = maybeKernelFields();
984 if (kernelFields == nullptr) {
985 return false;
986 }
987 if (!kernelFields->mRequestHeaderPresent) {
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000988 return false;
989 }
990
991 const size_t initialPosition = dataPosition();
Frederick Mayled3c595a2022-05-09 23:02:54 +0000992 setDataPosition(kernelFields->mWorkSourceRequestHeaderPosition);
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000993 status_t err = writeInt32(uid);
994 setDataPosition(initialPosition);
995 return err == NO_ERROR;
996}
997
Steven Morelandf1b1e492019-05-06 15:05:13 -0700998uid_t Parcel::readCallingWorkSourceUid() const
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000999{
Frederick Mayled3c595a2022-05-09 23:02:54 +00001000 auto* kernelFields = maybeKernelFields();
1001 if (kernelFields == nullptr) {
1002 return false;
1003 }
1004 if (!kernelFields->mRequestHeaderPresent) {
Olivier Gaillarddc848a02019-01-30 17:10:44 +00001005 return IPCThreadState::kUnsetWorkSource;
1006 }
1007
1008 const size_t initialPosition = dataPosition();
Frederick Mayled3c595a2022-05-09 23:02:54 +00001009 setDataPosition(kernelFields->mWorkSourceRequestHeaderPosition);
Olivier Gaillarddc848a02019-01-30 17:10:44 +00001010 uid_t uid = readInt32();
1011 setDataPosition(initialPosition);
1012 return uid;
1013}
1014
Mathias Agopian83c04462009-05-22 19:00:22 -07001015bool Parcel::checkInterface(IBinder* binder) const
1016{
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -07001017 return enforceInterface(binder->getInterfaceDescriptor());
Mathias Agopian83c04462009-05-22 19:00:22 -07001018}
1019
Brad Fitzpatricka877cd82010-07-07 16:06:39 -07001020bool Parcel::enforceInterface(const String16& interface,
Brad Fitzpatrick70081a12010-07-27 09:49:11 -07001021 IPCThreadState* threadState) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001022{
Tomasz Wasilczyk0bfea2d2023-08-11 00:06:51 +00001023 return enforceInterface(interface.c_str(), interface.size(), threadState);
Daniel Colascione0bb330d2019-10-29 16:44:19 -07001024}
1025
1026bool Parcel::enforceInterface(const char16_t* interface,
1027 size_t len,
1028 IPCThreadState* threadState) const
1029{
Frederick Mayled3c595a2022-05-09 23:02:54 +00001030 if (auto* kernelFields = maybeKernelFields()) {
Andrei Homescua9cca9c2022-05-03 04:48:01 +00001031#ifdef BINDER_WITH_KERNEL_IPC
Steven Moreland3af936a2021-03-26 03:05:38 +00001032 // StrictModePolicy.
1033 int32_t strictPolicy = readInt32();
1034 if (threadState == nullptr) {
1035 threadState = IPCThreadState::self();
1036 }
1037 if ((threadState->getLastTransactionBinderFlags() & IBinder::FLAG_ONEWAY) != 0) {
1038 // For one-way calls, the callee is running entirely
1039 // disconnected from the caller, so disable StrictMode entirely.
1040 // Not only does disk/network usage not impact the caller, but
1041 // there's no way to communicate back violations anyway.
1042 threadState->setStrictModePolicy(0);
1043 } else {
1044 threadState->setStrictModePolicy(strictPolicy);
1045 }
1046 // WorkSource.
1047 updateWorkSourceRequestHeaderPosition();
1048 int32_t workSource = readInt32();
1049 threadState->setCallingWorkSourceUidWithoutPropagation(workSource);
1050 // vendor header
1051 int32_t header = readInt32();
Steven Moreland3a667492023-06-02 21:41:39 +00001052
1053 // fuzzers skip this check, because it is for protecting the underlying ABI, but
1054 // we don't want it to reduce our coverage
1055 if (header != kHeader && !mServiceFuzzing) {
Steven Moreland3af936a2021-03-26 03:05:38 +00001056 ALOGE("Expecting header 0x%x but found 0x%x. Mixing copies of libbinder?", kHeader,
1057 header);
1058 return false;
1059 }
Andrei Homescua9cca9c2022-05-03 04:48:01 +00001060#else // BINDER_WITH_KERNEL_IPC
1061 LOG_ALWAYS_FATAL("Binder kernel driver disabled at build time");
1062 (void)threadState;
1063 return false;
1064#endif // BINDER_WITH_KERNEL_IPC
Brad Fitzpatricka877cd82010-07-07 16:06:39 -07001065 }
Steven Moreland3af936a2021-03-26 03:05:38 +00001066
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +01001067 // Interface descriptor.
Daniel Colascione0bb330d2019-10-29 16:44:19 -07001068 size_t parcel_interface_len;
1069 const char16_t* parcel_interface = readString16Inplace(&parcel_interface_len);
1070 if (len == parcel_interface_len &&
1071 (!len || !memcmp(parcel_interface, interface, len * sizeof (char16_t)))) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001072 return true;
1073 } else {
Steven Morelande99d8822023-06-02 21:56:39 +00001074 if (mServiceFuzzing) {
1075 // ignore. Theoretically, this could cause a few false positives, because
1076 // people could assume things about getInterfaceDescriptor if they pass
1077 // this point, but it would be extremely fragile. It's more important that
1078 // we fuzz with the above things read from the Parcel.
1079 return true;
1080 } else {
Steven Moreland3a667492023-06-02 21:41:39 +00001081 ALOGW("**** enforceInterface() expected '%s' but read '%s'",
Tomasz Wasilczyk0bfea2d2023-08-11 00:06:51 +00001082 String8(interface, len).c_str(),
1083 String8(parcel_interface, parcel_interface_len).c_str());
Steven Morelande99d8822023-06-02 21:56:39 +00001084 return false;
Steven Moreland3a667492023-06-02 21:41:39 +00001085 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001086 }
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -07001087}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001088
Pawan Wagh104654a2022-11-15 21:18:42 +00001089void Parcel::setEnforceNoDataAvail(bool enforceNoDataAvail) {
1090 mEnforceNoDataAvail = enforceNoDataAvail;
1091}
1092
Steven Moreland3a667492023-06-02 21:41:39 +00001093void Parcel::setServiceFuzzing() {
1094 mServiceFuzzing = true;
1095}
1096
Steven Moreland418914a2023-06-28 21:47:14 +00001097bool Parcel::isServiceFuzzing() const {
1098 return mServiceFuzzing;
1099}
1100
Jooyung Hand23f9502021-12-23 14:39:57 +09001101binder::Status Parcel::enforceNoDataAvail() const {
Pawan Wagh104654a2022-11-15 21:18:42 +00001102 if (!mEnforceNoDataAvail) {
1103 return binder::Status::ok();
1104 }
1105
Jooyung Hand23f9502021-12-23 14:39:57 +09001106 const auto n = dataAvail();
1107 if (n == 0) {
1108 return binder::Status::ok();
1109 }
1110 return binder::Status::
1111 fromExceptionCode(binder::Status::Exception::EX_BAD_PARCELABLE,
1112 String8::format("Parcel data not fully consumed, unread size: %zu",
1113 n));
1114}
1115
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001116size_t Parcel::objectsCount() const
1117{
Frederick Mayled3c595a2022-05-09 23:02:54 +00001118 if (const auto* kernelFields = maybeKernelFields()) {
1119 return kernelFields->mObjectsSize;
1120 }
1121 return 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001122}
1123
1124status_t Parcel::errorCheck() const
1125{
1126 return mError;
1127}
1128
1129void Parcel::setError(status_t err)
1130{
1131 mError = err;
1132}
1133
1134status_t Parcel::finishWrite(size_t len)
1135{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001136 if (len > INT32_MAX) {
1137 // don't accept size_t values which may have come from an
1138 // inadvertent conversion from a negative int.
1139 return BAD_VALUE;
1140 }
1141
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001142 //printf("Finish write of %d\n", len);
1143 mDataPos += len;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001144 ALOGV("finishWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001145 if (mDataPos > mDataSize) {
1146 mDataSize = mDataPos;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001147 ALOGV("finishWrite Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001148 }
1149 //printf("New pos=%d, size=%d\n", mDataPos, mDataSize);
1150 return NO_ERROR;
1151}
1152
1153status_t Parcel::writeUnpadded(const void* data, size_t len)
1154{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001155 if (len > INT32_MAX) {
1156 // don't accept size_t values which may have come from an
1157 // inadvertent conversion from a negative int.
1158 return BAD_VALUE;
1159 }
1160
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001161 size_t end = mDataPos + len;
1162 if (end < mDataPos) {
1163 // integer overflow
1164 return BAD_VALUE;
1165 }
1166
1167 if (end <= mDataCapacity) {
1168restart_write:
1169 memcpy(mData+mDataPos, data, len);
1170 return finishWrite(len);
1171 }
1172
1173 status_t err = growData(len);
1174 if (err == NO_ERROR) goto restart_write;
1175 return err;
1176}
1177
1178status_t Parcel::write(const void* data, size_t len)
1179{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001180 if (len > INT32_MAX) {
1181 // don't accept size_t values which may have come from an
1182 // inadvertent conversion from a negative int.
1183 return BAD_VALUE;
1184 }
1185
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001186 void* const d = writeInplace(len);
1187 if (d) {
1188 memcpy(d, data, len);
1189 return NO_ERROR;
1190 }
1191 return mError;
1192}
1193
1194void* Parcel::writeInplace(size_t len)
1195{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001196 if (len > INT32_MAX) {
1197 // don't accept size_t values which may have come from an
1198 // inadvertent conversion from a negative int.
Yi Kong91635562018-06-07 14:38:36 -07001199 return nullptr;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001200 }
1201
1202 const size_t padded = pad_size(len);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001203
Khalid Ramadanb3d817b2021-11-18 07:02:50 +00001204 // check for integer overflow
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001205 if (mDataPos+padded < mDataPos) {
Yi Kong91635562018-06-07 14:38:36 -07001206 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001207 }
1208
1209 if ((mDataPos+padded) <= mDataCapacity) {
1210restart_write:
1211 //printf("Writing %ld bytes, padded to %ld\n", len, padded);
1212 uint8_t* const data = mData+mDataPos;
1213
1214 // Need to pad at end?
1215 if (padded != len) {
1216#if BYTE_ORDER == BIG_ENDIAN
1217 static const uint32_t mask[4] = {
1218 0x00000000, 0xffffff00, 0xffff0000, 0xff000000
1219 };
1220#endif
1221#if BYTE_ORDER == LITTLE_ENDIAN
1222 static const uint32_t mask[4] = {
1223 0x00000000, 0x00ffffff, 0x0000ffff, 0x000000ff
1224 };
1225#endif
1226 //printf("Applying pad mask: %p to %p\n", (void*)mask[padded-len],
1227 // *reinterpret_cast<void**>(data+padded-4));
1228 *reinterpret_cast<uint32_t*>(data+padded-4) &= mask[padded-len];
1229 }
1230
1231 finishWrite(padded);
1232 return data;
1233 }
1234
1235 status_t err = growData(padded);
1236 if (err == NO_ERROR) goto restart_write;
Yi Kong91635562018-06-07 14:38:36 -07001237 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001238}
1239
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001240status_t Parcel::writeUtf8AsUtf16(const std::string& str) {
1241 const uint8_t* strData = (uint8_t*)str.data();
1242 const size_t strLen= str.length();
1243 const ssize_t utf16Len = utf8_to_utf16_length(strData, strLen);
Sergio Girof4607432016-07-21 14:46:35 +01001244 if (utf16Len < 0 || utf16Len > std::numeric_limits<int32_t>::max()) {
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001245 return BAD_VALUE;
1246 }
1247
1248 status_t err = writeInt32(utf16Len);
1249 if (err) {
1250 return err;
1251 }
1252
1253 // Allocate enough bytes to hold our converted string and its terminating NULL.
1254 void* dst = writeInplace((utf16Len + 1) * sizeof(char16_t));
1255 if (!dst) {
1256 return NO_MEMORY;
1257 }
1258
Sergio Girof4607432016-07-21 14:46:35 +01001259 utf8_to_utf16(strData, strLen, (char16_t*)dst, (size_t) utf16Len + 1);
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001260
1261 return NO_ERROR;
1262}
1263
Jooyung Han9fcc4ef2020-01-23 12:45:10 +09001264
Andy Hung49198cf2020-11-18 11:02:39 -08001265status_t Parcel::writeUtf8AsUtf16(const std::optional<std::string>& str) { return writeData(str); }
1266status_t Parcel::writeUtf8AsUtf16(const std::unique_ptr<std::string>& str) { return writeData(str); }
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001267
Andy Hung49198cf2020-11-18 11:02:39 -08001268status_t Parcel::writeString16(const std::optional<String16>& str) { return writeData(str); }
1269status_t Parcel::writeString16(const std::unique_ptr<String16>& str) { return writeData(str); }
Casey Dahlin451ff582015-10-19 18:12:18 -07001270
Andy Hung49198cf2020-11-18 11:02:39 -08001271status_t Parcel::writeByteVector(const std::vector<int8_t>& val) { return writeData(val); }
1272status_t Parcel::writeByteVector(const std::optional<std::vector<int8_t>>& val) { return writeData(val); }
1273status_t Parcel::writeByteVector(const std::unique_ptr<std::vector<int8_t>>& val) { return writeData(val); }
1274status_t Parcel::writeByteVector(const std::vector<uint8_t>& val) { return writeData(val); }
1275status_t Parcel::writeByteVector(const std::optional<std::vector<uint8_t>>& val) { return writeData(val); }
1276status_t Parcel::writeByteVector(const std::unique_ptr<std::vector<uint8_t>>& val){ return writeData(val); }
1277status_t Parcel::writeInt32Vector(const std::vector<int32_t>& val) { return writeData(val); }
1278status_t Parcel::writeInt32Vector(const std::optional<std::vector<int32_t>>& val) { return writeData(val); }
1279status_t Parcel::writeInt32Vector(const std::unique_ptr<std::vector<int32_t>>& val) { return writeData(val); }
1280status_t Parcel::writeInt64Vector(const std::vector<int64_t>& val) { return writeData(val); }
1281status_t Parcel::writeInt64Vector(const std::optional<std::vector<int64_t>>& val) { return writeData(val); }
1282status_t Parcel::writeInt64Vector(const std::unique_ptr<std::vector<int64_t>>& val) { return writeData(val); }
1283status_t Parcel::writeUint64Vector(const std::vector<uint64_t>& val) { return writeData(val); }
1284status_t Parcel::writeUint64Vector(const std::optional<std::vector<uint64_t>>& val) { return writeData(val); }
1285status_t Parcel::writeUint64Vector(const std::unique_ptr<std::vector<uint64_t>>& val) { return writeData(val); }
1286status_t Parcel::writeFloatVector(const std::vector<float>& val) { return writeData(val); }
1287status_t Parcel::writeFloatVector(const std::optional<std::vector<float>>& val) { return writeData(val); }
1288status_t Parcel::writeFloatVector(const std::unique_ptr<std::vector<float>>& val) { return writeData(val); }
1289status_t Parcel::writeDoubleVector(const std::vector<double>& val) { return writeData(val); }
1290status_t Parcel::writeDoubleVector(const std::optional<std::vector<double>>& val) { return writeData(val); }
1291status_t Parcel::writeDoubleVector(const std::unique_ptr<std::vector<double>>& val) { return writeData(val); }
1292status_t Parcel::writeBoolVector(const std::vector<bool>& val) { return writeData(val); }
1293status_t Parcel::writeBoolVector(const std::optional<std::vector<bool>>& val) { return writeData(val); }
1294status_t Parcel::writeBoolVector(const std::unique_ptr<std::vector<bool>>& val) { return writeData(val); }
1295status_t Parcel::writeCharVector(const std::vector<char16_t>& val) { return writeData(val); }
1296status_t Parcel::writeCharVector(const std::optional<std::vector<char16_t>>& val) { return writeData(val); }
1297status_t Parcel::writeCharVector(const std::unique_ptr<std::vector<char16_t>>& val) { return writeData(val); }
Casey Dahlin451ff582015-10-19 18:12:18 -07001298
Andy Hung49198cf2020-11-18 11:02:39 -08001299status_t Parcel::writeString16Vector(const std::vector<String16>& val) { return writeData(val); }
Casey Dahlinb9872622015-11-25 15:09:45 -08001300status_t Parcel::writeString16Vector(
Andy Hung49198cf2020-11-18 11:02:39 -08001301 const std::optional<std::vector<std::optional<String16>>>& val) { return writeData(val); }
Jooyung Han9fcc4ef2020-01-23 12:45:10 +09001302status_t Parcel::writeString16Vector(
Andy Hung49198cf2020-11-18 11:02:39 -08001303 const std::unique_ptr<std::vector<std::unique_ptr<String16>>>& val) { return writeData(val); }
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001304status_t Parcel::writeUtf8VectorAsUtf16Vector(
Andy Hung49198cf2020-11-18 11:02:39 -08001305 const std::optional<std::vector<std::optional<std::string>>>& val) { return writeData(val); }
Jooyung Han9fcc4ef2020-01-23 12:45:10 +09001306status_t Parcel::writeUtf8VectorAsUtf16Vector(
Andy Hung49198cf2020-11-18 11:02:39 -08001307 const std::unique_ptr<std::vector<std::unique_ptr<std::string>>>& val) { return writeData(val); }
1308status_t Parcel::writeUtf8VectorAsUtf16Vector(const std::vector<std::string>& val) { return writeData(val); }
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001309
Tomasz Wasilczyk639490b2023-11-01 13:49:41 -07001310status_t Parcel::writeUniqueFileDescriptorVector(const std::vector<unique_fd>& val) {
1311 return writeData(val);
1312}
1313status_t Parcel::writeUniqueFileDescriptorVector(const std::optional<std::vector<unique_fd>>& val) {
1314 return writeData(val);
1315}
1316status_t Parcel::writeUniqueFileDescriptorVector(
1317 const std::unique_ptr<std::vector<unique_fd>>& val) {
1318 return writeData(val);
1319}
Andy Hung49198cf2020-11-18 11:02:39 -08001320
1321status_t Parcel::writeStrongBinderVector(const std::vector<sp<IBinder>>& val) { return writeData(val); }
1322status_t Parcel::writeStrongBinderVector(const std::optional<std::vector<sp<IBinder>>>& val) { return writeData(val); }
1323status_t Parcel::writeStrongBinderVector(const std::unique_ptr<std::vector<sp<IBinder>>>& val) { return writeData(val); }
1324
1325status_t Parcel::writeParcelable(const Parcelable& parcelable) { return writeData(parcelable); }
1326
1327status_t Parcel::readUtf8FromUtf16(std::optional<std::string>* str) const { return readData(str); }
1328status_t Parcel::readUtf8FromUtf16(std::unique_ptr<std::string>* str) const { return readData(str); }
1329
1330status_t Parcel::readString16(std::optional<String16>* pArg) const { return readData(pArg); }
1331status_t Parcel::readString16(std::unique_ptr<String16>* pArg) const { return readData(pArg); }
1332
1333status_t Parcel::readByteVector(std::vector<int8_t>* val) const { return readData(val); }
1334status_t Parcel::readByteVector(std::vector<uint8_t>* val) const { return readData(val); }
1335status_t Parcel::readByteVector(std::optional<std::vector<int8_t>>* val) const { return readData(val); }
1336status_t Parcel::readByteVector(std::unique_ptr<std::vector<int8_t>>* val) const { return readData(val); }
1337status_t Parcel::readByteVector(std::optional<std::vector<uint8_t>>* val) const { return readData(val); }
1338status_t Parcel::readByteVector(std::unique_ptr<std::vector<uint8_t>>* val) const { return readData(val); }
1339status_t Parcel::readInt32Vector(std::optional<std::vector<int32_t>>* val) const { return readData(val); }
1340status_t Parcel::readInt32Vector(std::unique_ptr<std::vector<int32_t>>* val) const { return readData(val); }
1341status_t Parcel::readInt32Vector(std::vector<int32_t>* val) const { return readData(val); }
1342status_t Parcel::readInt64Vector(std::optional<std::vector<int64_t>>* val) const { return readData(val); }
1343status_t Parcel::readInt64Vector(std::unique_ptr<std::vector<int64_t>>* val) const { return readData(val); }
1344status_t Parcel::readInt64Vector(std::vector<int64_t>* val) const { return readData(val); }
1345status_t Parcel::readUint64Vector(std::optional<std::vector<uint64_t>>* val) const { return readData(val); }
1346status_t Parcel::readUint64Vector(std::unique_ptr<std::vector<uint64_t>>* val) const { return readData(val); }
1347status_t Parcel::readUint64Vector(std::vector<uint64_t>* val) const { return readData(val); }
1348status_t Parcel::readFloatVector(std::optional<std::vector<float>>* val) const { return readData(val); }
1349status_t Parcel::readFloatVector(std::unique_ptr<std::vector<float>>* val) const { return readData(val); }
1350status_t Parcel::readFloatVector(std::vector<float>* val) const { return readData(val); }
1351status_t Parcel::readDoubleVector(std::optional<std::vector<double>>* val) const { return readData(val); }
1352status_t Parcel::readDoubleVector(std::unique_ptr<std::vector<double>>* val) const { return readData(val); }
1353status_t Parcel::readDoubleVector(std::vector<double>* val) const { return readData(val); }
1354status_t Parcel::readBoolVector(std::optional<std::vector<bool>>* val) const { return readData(val); }
1355status_t Parcel::readBoolVector(std::unique_ptr<std::vector<bool>>* val) const { return readData(val); }
1356status_t Parcel::readBoolVector(std::vector<bool>* val) const { return readData(val); }
1357status_t Parcel::readCharVector(std::optional<std::vector<char16_t>>* val) const { return readData(val); }
1358status_t Parcel::readCharVector(std::unique_ptr<std::vector<char16_t>>* val) const { return readData(val); }
1359status_t Parcel::readCharVector(std::vector<char16_t>* val) const { return readData(val); }
1360
1361status_t Parcel::readString16Vector(
1362 std::optional<std::vector<std::optional<String16>>>* val) const { return readData(val); }
1363status_t Parcel::readString16Vector(
1364 std::unique_ptr<std::vector<std::unique_ptr<String16>>>* val) const { return readData(val); }
1365status_t Parcel::readString16Vector(std::vector<String16>* val) const { return readData(val); }
1366status_t Parcel::readUtf8VectorFromUtf16Vector(
1367 std::optional<std::vector<std::optional<std::string>>>* val) const { return readData(val); }
1368status_t Parcel::readUtf8VectorFromUtf16Vector(
1369 std::unique_ptr<std::vector<std::unique_ptr<std::string>>>* val) const { return readData(val); }
1370status_t Parcel::readUtf8VectorFromUtf16Vector(std::vector<std::string>* val) const { return readData(val); }
1371
Tomasz Wasilczyk639490b2023-11-01 13:49:41 -07001372status_t Parcel::readUniqueFileDescriptorVector(std::optional<std::vector<unique_fd>>* val) const {
1373 return readData(val);
1374}
1375status_t Parcel::readUniqueFileDescriptorVector(
1376 std::unique_ptr<std::vector<unique_fd>>* val) const {
1377 return readData(val);
1378}
1379status_t Parcel::readUniqueFileDescriptorVector(std::vector<unique_fd>* val) const {
1380 return readData(val);
1381}
Andy Hung49198cf2020-11-18 11:02:39 -08001382
1383status_t Parcel::readStrongBinderVector(std::optional<std::vector<sp<IBinder>>>* val) const { return readData(val); }
1384status_t Parcel::readStrongBinderVector(std::unique_ptr<std::vector<sp<IBinder>>>* val) const { return readData(val); }
1385status_t Parcel::readStrongBinderVector(std::vector<sp<IBinder>>* val) const { return readData(val); }
1386
1387status_t Parcel::readParcelable(Parcelable* parcelable) const { return readData(parcelable); }
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001388
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001389status_t Parcel::writeInt32(int32_t val)
1390{
Andreas Huber84a6d042009-08-17 13:33:27 -07001391 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001392}
Dan Stoza41a0f2f2014-12-01 10:01:10 -08001393
1394status_t Parcel::writeUint32(uint32_t val)
1395{
1396 return writeAligned(val);
1397}
1398
Marco Nelissen5c0106e2013-10-16 10:57:51 -07001399status_t Parcel::writeInt32Array(size_t len, const int32_t *val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001400 if (len > INT32_MAX) {
1401 // don't accept size_t values which may have come from an
1402 // inadvertent conversion from a negative int.
1403 return BAD_VALUE;
1404 }
1405
Marco Nelissen5c0106e2013-10-16 10:57:51 -07001406 if (!val) {
Chad Brubakere59cb432015-06-30 14:03:55 -07001407 return writeInt32(-1);
Marco Nelissen5c0106e2013-10-16 10:57:51 -07001408 }
Chad Brubakere59cb432015-06-30 14:03:55 -07001409 status_t ret = writeInt32(static_cast<uint32_t>(len));
Marco Nelissen5c0106e2013-10-16 10:57:51 -07001410 if (ret == NO_ERROR) {
1411 ret = write(val, len * sizeof(*val));
1412 }
1413 return ret;
1414}
Marco Nelissenf0190bf2014-03-13 14:17:40 -07001415status_t Parcel::writeByteArray(size_t len, const uint8_t *val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001416 if (len > INT32_MAX) {
1417 // don't accept size_t values which may have come from an
1418 // inadvertent conversion from a negative int.
1419 return BAD_VALUE;
1420 }
1421
Marco Nelissenf0190bf2014-03-13 14:17:40 -07001422 if (!val) {
Chad Brubakere59cb432015-06-30 14:03:55 -07001423 return writeInt32(-1);
Marco Nelissenf0190bf2014-03-13 14:17:40 -07001424 }
Chad Brubakere59cb432015-06-30 14:03:55 -07001425 status_t ret = writeInt32(static_cast<uint32_t>(len));
Marco Nelissenf0190bf2014-03-13 14:17:40 -07001426 if (ret == NO_ERROR) {
1427 ret = write(val, len * sizeof(*val));
1428 }
1429 return ret;
1430}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001431
Casey Dahlind6848f52015-10-15 15:44:59 -07001432status_t Parcel::writeBool(bool val)
1433{
1434 return writeInt32(int32_t(val));
1435}
1436
1437status_t Parcel::writeChar(char16_t val)
1438{
1439 return writeInt32(int32_t(val));
1440}
1441
1442status_t Parcel::writeByte(int8_t val)
1443{
1444 return writeInt32(int32_t(val));
1445}
1446
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001447status_t Parcel::writeInt64(int64_t val)
1448{
Andreas Huber84a6d042009-08-17 13:33:27 -07001449 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001450}
1451
Ronghua Wu2d13afd2015-03-16 11:11:07 -07001452status_t Parcel::writeUint64(uint64_t val)
1453{
1454 return writeAligned(val);
1455}
1456
Serban Constantinescuf683e012013-11-05 16:53:55 +00001457status_t Parcel::writePointer(uintptr_t val)
1458{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001459 return writeAligned<binder_uintptr_t>(val);
Serban Constantinescuf683e012013-11-05 16:53:55 +00001460}
1461
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001462status_t Parcel::writeFloat(float val)
1463{
Andreas Huber84a6d042009-08-17 13:33:27 -07001464 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001465}
1466
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001467#if defined(__mips__) && defined(__mips_hard_float)
1468
1469status_t Parcel::writeDouble(double val)
1470{
1471 union {
1472 double d;
1473 unsigned long long ll;
1474 } u;
1475 u.d = val;
1476 return writeAligned(u.ll);
1477}
1478
1479#else
1480
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001481status_t Parcel::writeDouble(double val)
1482{
Andreas Huber84a6d042009-08-17 13:33:27 -07001483 return writeAligned(val);
1484}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001485
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001486#endif
1487
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001488status_t Parcel::writeCString(const char* str)
1489{
1490 return write(str, strlen(str)+1);
1491}
1492
1493status_t Parcel::writeString8(const String8& str)
1494{
Tomasz Wasilczyk0bfea2d2023-08-11 00:06:51 +00001495 return writeString8(str.c_str(), str.size());
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06001496}
1497
1498status_t Parcel::writeString8(const char* str, size_t len)
1499{
1500 if (str == nullptr) return writeInt32(-1);
1501
Jeff Sharkey18220902020-11-05 08:36:20 -07001502 // NOTE: Keep this logic in sync with android_os_Parcel.cpp
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06001503 status_t err = writeInt32(len);
1504 if (err == NO_ERROR) {
1505 uint8_t* data = (uint8_t*)writeInplace(len+sizeof(char));
1506 if (data) {
1507 memcpy(data, str, len);
1508 *reinterpret_cast<char*>(data+len) = 0;
1509 return NO_ERROR;
1510 }
1511 err = mError;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001512 }
1513 return err;
1514}
1515
1516status_t Parcel::writeString16(const String16& str)
1517{
Tomasz Wasilczyk0bfea2d2023-08-11 00:06:51 +00001518 return writeString16(str.c_str(), str.size());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001519}
1520
1521status_t Parcel::writeString16(const char16_t* str, size_t len)
1522{
Yi Kong91635562018-06-07 14:38:36 -07001523 if (str == nullptr) return writeInt32(-1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001524
Jeff Sharkey18220902020-11-05 08:36:20 -07001525 // NOTE: Keep this logic in sync with android_os_Parcel.cpp
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001526 status_t err = writeInt32(len);
1527 if (err == NO_ERROR) {
1528 len *= sizeof(char16_t);
1529 uint8_t* data = (uint8_t*)writeInplace(len+sizeof(char16_t));
1530 if (data) {
1531 memcpy(data, str, len);
1532 *reinterpret_cast<char16_t*>(data+len) = 0;
1533 return NO_ERROR;
1534 }
1535 err = mError;
1536 }
1537 return err;
1538}
1539
1540status_t Parcel::writeStrongBinder(const sp<IBinder>& val)
1541{
Steven Morelanda86a3562019-08-01 23:28:34 +00001542 return flattenBinder(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001543}
1544
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001545
Casey Dahlinb9872622015-11-25 15:09:45 -08001546status_t Parcel::writeRawNullableParcelable(const Parcelable* parcelable) {
1547 if (!parcelable) {
1548 return writeInt32(0);
1549 }
1550
1551 return writeParcelable(*parcelable);
1552}
1553
Tomasz Wasilczyk232d0b32023-10-09 17:37:16 +00001554#ifndef BINDER_DISABLE_NATIVE_HANDLE
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001555status_t Parcel::writeNativeHandle(const native_handle* handle)
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001556{
Mathias Agopian1d0a95b2009-07-31 16:12:13 -07001557 if (!handle || handle->version != sizeof(native_handle))
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001558 return BAD_TYPE;
1559
1560 status_t err;
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001561 err = writeInt32(handle->numFds);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001562 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001563
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001564 err = writeInt32(handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001565 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001566
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001567 for (int i=0 ; err==NO_ERROR && i<handle->numFds ; i++)
1568 err = writeDupFileDescriptor(handle->data[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001569
1570 if (err != NO_ERROR) {
Steve Block9d453682011-12-20 16:23:08 +00001571 ALOGD("write native handle, write dup fd failed");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001572 return err;
1573 }
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001574 err = write(handle->data + handle->numFds, sizeof(int)*handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001575 return err;
1576}
Tomasz Wasilczyk232d0b32023-10-09 17:37:16 +00001577#endif
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001578
Frederick Mayle69a0c992022-05-26 20:38:39 +00001579status_t Parcel::writeFileDescriptor(int fd, bool takeOwnership) {
1580 if (auto* rpcFields = maybeRpcFields()) {
Tomasz Wasilczyk639490b2023-11-01 13:49:41 -07001581 std::variant<unique_fd, borrowed_fd> fdVariant;
Frederick Mayle69a0c992022-05-26 20:38:39 +00001582 if (takeOwnership) {
Tomasz Wasilczyk639490b2023-11-01 13:49:41 -07001583 fdVariant = unique_fd(fd);
Frederick Mayle69a0c992022-05-26 20:38:39 +00001584 } else {
Tomasz Wasilczyk639490b2023-11-01 13:49:41 -07001585 fdVariant = borrowed_fd(fd);
Frederick Mayle69a0c992022-05-26 20:38:39 +00001586 }
1587 if (!mAllowFds) {
Devin Mooref4c9b482024-07-01 20:22:23 +00001588 ALOGE("FDs are not allowed in this parcel. Both the service and the client must set "
1589 "the FileDescriptorTransportMode and agree on the support.");
Frederick Mayle69a0c992022-05-26 20:38:39 +00001590 return FDS_NOT_ALLOWED;
1591 }
1592 switch (rpcFields->mSession->getFileDescriptorTransportMode()) {
1593 case RpcSession::FileDescriptorTransportMode::NONE: {
Devin Mooref4c9b482024-07-01 20:22:23 +00001594 ALOGE("FDs are not allowed in this RpcSession. Both the service and the client "
1595 "must set "
1596 "the FileDescriptorTransportMode and agree on the support.");
Frederick Mayle69a0c992022-05-26 20:38:39 +00001597 return FDS_NOT_ALLOWED;
1598 }
Andrei Homescu1c18a802022-08-17 04:59:01 +00001599 case RpcSession::FileDescriptorTransportMode::UNIX:
1600 case RpcSession::FileDescriptorTransportMode::TRUSTY: {
Frederick Mayle69a0c992022-05-26 20:38:39 +00001601 if (rpcFields->mFds == nullptr) {
1602 rpcFields->mFds = std::make_unique<decltype(rpcFields->mFds)::element_type>();
1603 }
1604 size_t dataPos = mDataPos;
1605 if (dataPos > UINT32_MAX) {
1606 return NO_MEMORY;
1607 }
1608 if (status_t err = writeInt32(RpcFields::TYPE_NATIVE_FILE_DESCRIPTOR); err != OK) {
1609 return err;
1610 }
1611 if (status_t err = writeInt32(rpcFields->mFds->size()); err != OK) {
1612 return err;
1613 }
1614 rpcFields->mObjectPositions.push_back(dataPos);
1615 rpcFields->mFds->push_back(std::move(fdVariant));
1616 return OK;
1617 }
1618 }
Steven Moreland5553ac42020-11-11 02:14:45 +00001619 }
1620
Andrei Homescua9cca9c2022-05-03 04:48:01 +00001621#ifdef BINDER_WITH_KERNEL_IPC
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001622 flat_binder_object obj;
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07001623 obj.hdr.type = BINDER_TYPE_FD;
T.J. Mercierca0c2cb2022-12-19 21:56:35 +00001624 obj.flags = 0;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -08001625 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001626 obj.handle = fd;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001627 obj.cookie = takeOwnership ? 1 : 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001628 return writeObject(obj, true);
Andrei Homescua9cca9c2022-05-03 04:48:01 +00001629#else // BINDER_WITH_KERNEL_IPC
1630 LOG_ALWAYS_FATAL("Binder kernel driver disabled at build time");
1631 (void)fd;
1632 (void)takeOwnership;
1633 return INVALID_OPERATION;
1634#endif // BINDER_WITH_KERNEL_IPC
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001635}
1636
1637status_t Parcel::writeDupFileDescriptor(int fd)
1638{
Andrei Homescu24ad36e2022-08-04 01:33:33 +00001639 int dupFd;
Tomasz Wasilczyk0d9dec22023-10-06 20:28:49 +00001640 if (status_t err = binder::os::dupFileDescriptor(fd, &dupFd); err != OK) {
Andrei Homescu24ad36e2022-08-04 01:33:33 +00001641 return err;
Jeff Brownd341c712011-11-04 20:19:33 -07001642 }
1643 status_t err = writeFileDescriptor(dupFd, true /*takeOwnership*/);
Casey Dahlin06673e32015-11-23 13:24:23 -08001644 if (err != OK) {
Jeff Brownd341c712011-11-04 20:19:33 -07001645 close(dupFd);
1646 }
1647 return err;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001648}
1649
Dianne Hackborn1941a402016-08-29 12:30:43 -07001650status_t Parcel::writeParcelFileDescriptor(int fd, bool takeOwnership)
1651{
1652 writeInt32(0);
1653 return writeFileDescriptor(fd, takeOwnership);
1654}
1655
Ryo Hashimotobf551892018-05-31 16:58:35 +09001656status_t Parcel::writeDupParcelFileDescriptor(int fd)
1657{
Andrei Homescu24ad36e2022-08-04 01:33:33 +00001658 int dupFd;
Tomasz Wasilczyk0d9dec22023-10-06 20:28:49 +00001659 if (status_t err = binder::os::dupFileDescriptor(fd, &dupFd); err != OK) {
Andrei Homescu24ad36e2022-08-04 01:33:33 +00001660 return err;
Ryo Hashimotobf551892018-05-31 16:58:35 +09001661 }
1662 status_t err = writeParcelFileDescriptor(dupFd, true /*takeOwnership*/);
1663 if (err != OK) {
1664 close(dupFd);
1665 }
1666 return err;
1667}
1668
Tomasz Wasilczyk639490b2023-11-01 13:49:41 -07001669status_t Parcel::writeUniqueFileDescriptor(const unique_fd& fd) {
Casey Dahlin06673e32015-11-23 13:24:23 -08001670 return writeDupFileDescriptor(fd.get());
1671}
1672
Jeff Brown13b16042014-11-11 16:44:25 -08001673status_t Parcel::writeBlob(size_t len, bool mutableCopy, WritableBlob* outBlob)
Jeff Brown5707dbf2011-09-23 21:17:56 -07001674{
Tomasz Wasilczykbca8f942023-10-09 17:42:37 +00001675#ifdef BINDER_DISABLE_BLOB
1676 (void)len;
1677 (void)mutableCopy;
1678 (void)outBlob;
1679 return INVALID_OPERATION;
1680#else
Nick Kralevichb6b14232015-04-02 09:36:02 -07001681 if (len > INT32_MAX) {
1682 // don't accept size_t values which may have come from an
1683 // inadvertent conversion from a negative int.
1684 return BAD_VALUE;
1685 }
1686
Jeff Brown13b16042014-11-11 16:44:25 -08001687 status_t status;
1688 if (!mAllowFds || len <= BLOB_INPLACE_LIMIT) {
Steve Block6807e592011-10-20 11:56:00 +01001689 ALOGV("writeBlob: write in place");
Jeff Brown13b16042014-11-11 16:44:25 -08001690 status = writeInt32(BLOB_INPLACE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001691 if (status) return status;
1692
1693 void* ptr = writeInplace(len);
1694 if (!ptr) return NO_MEMORY;
1695
Jeff Brown13b16042014-11-11 16:44:25 -08001696 outBlob->init(-1, ptr, len, false);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001697 return NO_ERROR;
1698 }
1699
Steve Block6807e592011-10-20 11:56:00 +01001700 ALOGV("writeBlob: write to ashmem");
Jeff Brown5707dbf2011-09-23 21:17:56 -07001701 int fd = ashmem_create_region("Parcel Blob", len);
1702 if (fd < 0) return NO_MEMORY;
1703
1704 int result = ashmem_set_prot_region(fd, PROT_READ | PROT_WRITE);
1705 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -07001706 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001707 } else {
Yi Kong91635562018-06-07 14:38:36 -07001708 void* ptr = ::mmap(nullptr, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001709 if (ptr == MAP_FAILED) {
1710 status = -errno;
1711 } else {
Jeff Brown13b16042014-11-11 16:44:25 -08001712 if (!mutableCopy) {
1713 result = ashmem_set_prot_region(fd, PROT_READ);
1714 }
Jeff Brown5707dbf2011-09-23 21:17:56 -07001715 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -07001716 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001717 } else {
Jeff Brown13b16042014-11-11 16:44:25 -08001718 status = writeInt32(mutableCopy ? BLOB_ASHMEM_MUTABLE : BLOB_ASHMEM_IMMUTABLE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001719 if (!status) {
Jeff Brown93ff1f92011-11-04 19:01:44 -07001720 status = writeFileDescriptor(fd, true /*takeOwnership*/);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001721 if (!status) {
Jeff Brown13b16042014-11-11 16:44:25 -08001722 outBlob->init(fd, ptr, len, mutableCopy);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001723 return NO_ERROR;
1724 }
1725 }
1726 }
1727 }
Mike McTernan733610f2024-08-27 11:48:30 +01001728 if (::munmap(ptr, len) == -1) {
1729 ALOGW("munmap() failed: %s", strerror(errno));
1730 }
Jeff Brown5707dbf2011-09-23 21:17:56 -07001731 }
1732 ::close(fd);
1733 return status;
Tomasz Wasilczykbca8f942023-10-09 17:42:37 +00001734#endif
Jeff Brown5707dbf2011-09-23 21:17:56 -07001735}
1736
Jeff Brown13b16042014-11-11 16:44:25 -08001737status_t Parcel::writeDupImmutableBlobFileDescriptor(int fd)
1738{
1739 // Must match up with what's done in writeBlob.
1740 if (!mAllowFds) return FDS_NOT_ALLOWED;
1741 status_t status = writeInt32(BLOB_ASHMEM_IMMUTABLE);
1742 if (status) return status;
1743 return writeDupFileDescriptor(fd);
1744}
1745
Mathias Agopiane1424282013-07-29 21:24:40 -07001746status_t Parcel::write(const FlattenableHelperInterface& val)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001747{
1748 status_t err;
1749
1750 // size if needed
Mathias Agopiane1424282013-07-29 21:24:40 -07001751 const size_t len = val.getFlattenedSize();
1752 const size_t fd_count = val.getFdCount();
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001753
Andrei Homescu1519b982022-06-09 02:04:44 +00001754 if ((len > INT32_MAX) || (fd_count > kMaxFds)) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001755 // don't accept size_t values which may have come from an
1756 // inadvertent conversion from a negative int.
1757 return BAD_VALUE;
1758 }
1759
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001760 err = this->writeInt32(len);
1761 if (err) return err;
1762
1763 err = this->writeInt32(fd_count);
1764 if (err) return err;
1765
1766 // payload
Martijn Coenenf8542382018-04-04 11:46:56 +02001767 void* const buf = this->writeInplace(len);
Yi Kong91635562018-06-07 14:38:36 -07001768 if (buf == nullptr)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001769 return BAD_VALUE;
1770
Yi Kong91635562018-06-07 14:38:36 -07001771 int* fds = nullptr;
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001772 if (fd_count) {
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001773 fds = new (std::nothrow) int[fd_count];
1774 if (fds == nullptr) {
1775 ALOGE("write: failed to allocate requested %zu fds", fd_count);
1776 return BAD_VALUE;
1777 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001778 }
1779
1780 err = val.flatten(buf, len, fds, fd_count);
1781 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
1782 err = this->writeDupFileDescriptor( fds[i] );
1783 }
1784
1785 if (fd_count) {
1786 delete [] fds;
1787 }
1788
1789 return err;
1790}
1791
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001792status_t Parcel::writeObject(const flat_binder_object& val, bool nullMetaData)
1793{
Frederick Mayled3c595a2022-05-09 23:02:54 +00001794 auto* kernelFields = maybeKernelFields();
1795 LOG_ALWAYS_FATAL_IF(kernelFields == nullptr, "Can't write flat_binder_object to RPC Parcel");
1796
Andrei Homescua9cca9c2022-05-03 04:48:01 +00001797#ifdef BINDER_WITH_KERNEL_IPC
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001798 const bool enoughData = (mDataPos+sizeof(val)) <= mDataCapacity;
Frederick Mayled3c595a2022-05-09 23:02:54 +00001799 const bool enoughObjects = kernelFields->mObjectsSize < kernelFields->mObjectsCapacity;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001800 if (enoughData && enoughObjects) {
1801restart_write:
1802 *reinterpret_cast<flat_binder_object*>(mData+mDataPos) = val;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001803
Christopher Tate98e67d32015-06-03 18:44:15 -07001804 // remember if it's a file descriptor
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07001805 if (val.hdr.type == BINDER_TYPE_FD) {
Christopher Tate98e67d32015-06-03 18:44:15 -07001806 if (!mAllowFds) {
1807 // fail before modifying our object index
1808 return FDS_NOT_ALLOWED;
1809 }
Frederick Mayled3c595a2022-05-09 23:02:54 +00001810 kernelFields->mHasFds = kernelFields->mFdsKnown = true;
Christopher Tate98e67d32015-06-03 18:44:15 -07001811 }
1812
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001813 // Need to write meta-data?
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001814 if (nullMetaData || val.binder != 0) {
Frederick Mayled3c595a2022-05-09 23:02:54 +00001815 kernelFields->mObjects[kernelFields->mObjectsSize] = mDataPos;
Steven Morelandc673f1f2021-10-07 18:23:35 -07001816 acquire_object(ProcessState::self(), val, this);
Frederick Mayled3c595a2022-05-09 23:02:54 +00001817 kernelFields->mObjectsSize++;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001818 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001819
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001820 return finishWrite(sizeof(flat_binder_object));
1821 }
1822
1823 if (!enoughData) {
1824 const status_t err = growData(sizeof(val));
1825 if (err != NO_ERROR) return err;
1826 }
1827 if (!enoughObjects) {
Frederick Mayled3c595a2022-05-09 23:02:54 +00001828 if (kernelFields->mObjectsSize > SIZE_MAX - 2) return NO_MEMORY; // overflow
1829 if ((kernelFields->mObjectsSize + 2) > SIZE_MAX / 3) return NO_MEMORY; // overflow
1830 size_t newSize = ((kernelFields->mObjectsSize + 2) * 3) / 2;
Martijn Coenen93fe5182020-01-22 10:46:25 +01001831 if (newSize > SIZE_MAX / sizeof(binder_size_t)) return NO_MEMORY; // overflow
Frederick Mayled3c595a2022-05-09 23:02:54 +00001832 binder_size_t* objects =
1833 (binder_size_t*)realloc(kernelFields->mObjects, newSize * sizeof(binder_size_t));
Yi Kong91635562018-06-07 14:38:36 -07001834 if (objects == nullptr) return NO_MEMORY;
Frederick Mayled3c595a2022-05-09 23:02:54 +00001835 kernelFields->mObjects = objects;
1836 kernelFields->mObjectsCapacity = newSize;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001837 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001838
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001839 goto restart_write;
Andrei Homescua9cca9c2022-05-03 04:48:01 +00001840#else // BINDER_WITH_KERNEL_IPC
1841 LOG_ALWAYS_FATAL("Binder kernel driver disabled at build time");
1842 (void)val;
1843 (void)nullMetaData;
1844 return INVALID_OPERATION;
1845#endif // BINDER_WITH_KERNEL_IPC
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001846}
1847
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001848status_t Parcel::writeNoException()
1849{
Christopher Wiley09eb7492015-11-09 15:06:15 -08001850 binder::Status status;
1851 return status.writeToParcel(this);
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001852}
1853
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001854status_t Parcel::validateReadData(size_t upperBound) const
1855{
Frederick Mayled3c595a2022-05-09 23:02:54 +00001856 const auto* kernelFields = maybeKernelFields();
1857 if (kernelFields == nullptr) {
1858 // Can't validate RPC Parcel reads because the location of binder
1859 // objects is unknown.
1860 return OK;
1861 }
1862
Andrei Homescua9cca9c2022-05-03 04:48:01 +00001863#ifdef BINDER_WITH_KERNEL_IPC
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001864 // Don't allow non-object reads on object data
Frederick Mayled3c595a2022-05-09 23:02:54 +00001865 if (kernelFields->mObjectsSorted || kernelFields->mObjectsSize <= 1) {
1866 data_sorted:
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001867 // Expect to check only against the next object
Frederick Mayled3c595a2022-05-09 23:02:54 +00001868 if (kernelFields->mNextObjectHint < kernelFields->mObjectsSize &&
1869 upperBound > kernelFields->mObjects[kernelFields->mNextObjectHint]) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001870 // For some reason the current read position is greater than the next object
1871 // hint. Iterate until we find the right object
Frederick Mayled3c595a2022-05-09 23:02:54 +00001872 size_t nextObject = kernelFields->mNextObjectHint;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001873 do {
Frederick Mayled3c595a2022-05-09 23:02:54 +00001874 if (mDataPos < kernelFields->mObjects[nextObject] + sizeof(flat_binder_object)) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001875 // Requested info overlaps with an object
Steven Moreland3a667492023-06-02 21:41:39 +00001876 if (!mServiceFuzzing) {
1877 ALOGE("Attempt to read from protected data in Parcel %p", this);
1878 }
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001879 return PERMISSION_DENIED;
1880 }
1881 nextObject++;
Frederick Mayled3c595a2022-05-09 23:02:54 +00001882 } while (nextObject < kernelFields->mObjectsSize &&
1883 upperBound > kernelFields->mObjects[nextObject]);
1884 kernelFields->mNextObjectHint = nextObject;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001885 }
1886 return NO_ERROR;
1887 }
1888 // Quickly determine if mObjects is sorted.
Frederick Mayled3c595a2022-05-09 23:02:54 +00001889 binder_size_t* currObj = kernelFields->mObjects + kernelFields->mObjectsSize - 1;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001890 binder_size_t* prevObj = currObj;
Frederick Mayled3c595a2022-05-09 23:02:54 +00001891 while (currObj > kernelFields->mObjects) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001892 prevObj--;
1893 if(*prevObj > *currObj) {
1894 goto data_unsorted;
1895 }
1896 currObj--;
1897 }
Frederick Mayled3c595a2022-05-09 23:02:54 +00001898 kernelFields->mObjectsSorted = true;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001899 goto data_sorted;
1900
1901data_unsorted:
1902 // Insertion Sort mObjects
1903 // Great for mostly sorted lists. If randomly sorted or reverse ordered mObjects become common,
1904 // switch to std::sort(mObjects, mObjects + mObjectsSize);
Frederick Mayled3c595a2022-05-09 23:02:54 +00001905 for (binder_size_t* iter0 = kernelFields->mObjects + 1;
1906 iter0 < kernelFields->mObjects + kernelFields->mObjectsSize; iter0++) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001907 binder_size_t temp = *iter0;
1908 binder_size_t* iter1 = iter0 - 1;
Frederick Mayled3c595a2022-05-09 23:02:54 +00001909 while (iter1 >= kernelFields->mObjects && *iter1 > temp) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001910 *(iter1 + 1) = *iter1;
1911 iter1--;
1912 }
1913 *(iter1 + 1) = temp;
1914 }
Frederick Mayled3c595a2022-05-09 23:02:54 +00001915 kernelFields->mNextObjectHint = 0;
1916 kernelFields->mObjectsSorted = true;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001917 goto data_sorted;
Andrei Homescua9cca9c2022-05-03 04:48:01 +00001918#else // BINDER_WITH_KERNEL_IPC
1919 (void)upperBound;
1920 return NO_ERROR;
1921#endif // BINDER_WITH_KERNEL_IPC
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001922}
1923
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001924status_t Parcel::read(void* outData, size_t len) const
1925{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001926 if (len > INT32_MAX) {
1927 // don't accept size_t values which may have come from an
1928 // inadvertent conversion from a negative int.
1929 return BAD_VALUE;
1930 }
1931
1932 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize
1933 && len <= pad_size(len)) {
Frederick Mayled3c595a2022-05-09 23:02:54 +00001934 const auto* kernelFields = maybeKernelFields();
1935 if (kernelFields != nullptr && kernelFields->mObjectsSize > 0) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001936 status_t err = validateReadData(mDataPos + pad_size(len));
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001937 if(err != NO_ERROR) {
1938 // Still increment the data position by the expected length
1939 mDataPos += pad_size(len);
1940 ALOGV("read Setting data pos of %p to %zu", this, mDataPos);
1941 return err;
1942 }
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001943 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001944 memcpy(outData, mData+mDataPos, len);
Nick Kralevichb6b14232015-04-02 09:36:02 -07001945 mDataPos += pad_size(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001946 ALOGV("read Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001947 return NO_ERROR;
1948 }
1949 return NOT_ENOUGH_DATA;
1950}
1951
1952const void* Parcel::readInplace(size_t len) const
1953{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001954 if (len > INT32_MAX) {
1955 // don't accept size_t values which may have come from an
1956 // inadvertent conversion from a negative int.
Yi Kong91635562018-06-07 14:38:36 -07001957 return nullptr;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001958 }
1959
1960 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize
1961 && len <= pad_size(len)) {
Frederick Mayled3c595a2022-05-09 23:02:54 +00001962 const auto* kernelFields = maybeKernelFields();
1963 if (kernelFields != nullptr && kernelFields->mObjectsSize > 0) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001964 status_t err = validateReadData(mDataPos + pad_size(len));
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001965 if(err != NO_ERROR) {
1966 // Still increment the data position by the expected length
1967 mDataPos += pad_size(len);
1968 ALOGV("readInplace Setting data pos of %p to %zu", this, mDataPos);
Yi Kong91635562018-06-07 14:38:36 -07001969 return nullptr;
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001970 }
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001971 }
1972
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001973 const void* data = mData+mDataPos;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001974 mDataPos += pad_size(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001975 ALOGV("readInplace Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001976 return data;
1977 }
Yi Kong91635562018-06-07 14:38:36 -07001978 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001979}
1980
Steven Morelandd4f73fb2021-05-14 19:50:52 +00001981status_t Parcel::readOutVectorSizeWithCheck(size_t elmSize, int32_t* size) const {
1982 if (status_t status = readInt32(size); status != OK) return status;
1983 if (*size < 0) return OK; // may be null, client to handle
1984
1985 LOG_ALWAYS_FATAL_IF(elmSize > INT32_MAX, "Cannot have element as big as %zu", elmSize);
1986
1987 // approximation, can't know max element size (e.g. if it makes heap
1988 // allocations)
1989 static_assert(sizeof(int) == sizeof(int32_t), "Android is LP64");
1990 int32_t allocationSize;
1991 if (__builtin_smul_overflow(elmSize, *size, &allocationSize)) return NO_MEMORY;
1992
1993 // High limit of 1MB since something this big could never be returned. Could
1994 // probably scope this down, but might impact very specific usecases.
1995 constexpr int32_t kMaxAllocationSize = 1 * 1000 * 1000;
1996
1997 if (allocationSize >= kMaxAllocationSize) {
1998 return NO_MEMORY;
1999 }
2000
2001 return OK;
2002}
2003
Andreas Huber84a6d042009-08-17 13:33:27 -07002004template<class T>
2005status_t Parcel::readAligned(T *pArg) const {
Elliott Hughes42a9b942020-08-17 15:53:31 -07002006 static_assert(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));
Andrei Homescue33238e2022-03-29 22:50:00 +00002007 static_assert(std::is_trivially_copyable_v<T>);
Andreas Huber84a6d042009-08-17 13:33:27 -07002008
2009 if ((mDataPos+sizeof(T)) <= mDataSize) {
Frederick Mayled3c595a2022-05-09 23:02:54 +00002010 const auto* kernelFields = maybeKernelFields();
2011 if (kernelFields != nullptr && kernelFields->mObjectsSize > 0) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002012 status_t err = validateReadData(mDataPos + sizeof(T));
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07002013 if(err != NO_ERROR) {
2014 // Still increment the data position by the expected length
2015 mDataPos += sizeof(T);
2016 return err;
2017 }
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002018 }
2019
Andrei Homescue33238e2022-03-29 22:50:00 +00002020 memcpy(pArg, mData + mDataPos, sizeof(T));
Andreas Huber84a6d042009-08-17 13:33:27 -07002021 mDataPos += sizeof(T);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002022 return NO_ERROR;
2023 } else {
2024 return NOT_ENOUGH_DATA;
2025 }
2026}
2027
Andreas Huber84a6d042009-08-17 13:33:27 -07002028template<class T>
2029T Parcel::readAligned() const {
2030 T result;
2031 if (readAligned(&result) != NO_ERROR) {
2032 result = 0;
2033 }
2034
2035 return result;
2036}
2037
2038template<class T>
2039status_t Parcel::writeAligned(T val) {
Elliott Hughes42a9b942020-08-17 15:53:31 -07002040 static_assert(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));
Andrei Homescue33238e2022-03-29 22:50:00 +00002041 static_assert(std::is_trivially_copyable_v<T>);
Andreas Huber84a6d042009-08-17 13:33:27 -07002042
2043 if ((mDataPos+sizeof(val)) <= mDataCapacity) {
2044restart_write:
Andrei Homescue33238e2022-03-29 22:50:00 +00002045 memcpy(mData + mDataPos, &val, sizeof(val));
Andreas Huber84a6d042009-08-17 13:33:27 -07002046 return finishWrite(sizeof(val));
2047 }
2048
2049 status_t err = growData(sizeof(val));
2050 if (err == NO_ERROR) goto restart_write;
2051 return err;
2052}
2053
2054status_t Parcel::readInt32(int32_t *pArg) const
2055{
2056 return readAligned(pArg);
2057}
2058
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002059int32_t Parcel::readInt32() const
2060{
Andreas Huber84a6d042009-08-17 13:33:27 -07002061 return readAligned<int32_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002062}
2063
Dan Stoza41a0f2f2014-12-01 10:01:10 -08002064status_t Parcel::readUint32(uint32_t *pArg) const
2065{
2066 return readAligned(pArg);
2067}
2068
2069uint32_t Parcel::readUint32() const
2070{
2071 return readAligned<uint32_t>();
2072}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002073
2074status_t Parcel::readInt64(int64_t *pArg) const
2075{
Andreas Huber84a6d042009-08-17 13:33:27 -07002076 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002077}
2078
2079
2080int64_t Parcel::readInt64() const
2081{
Andreas Huber84a6d042009-08-17 13:33:27 -07002082 return readAligned<int64_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002083}
2084
Ronghua Wu2d13afd2015-03-16 11:11:07 -07002085status_t Parcel::readUint64(uint64_t *pArg) const
2086{
2087 return readAligned(pArg);
2088}
2089
2090uint64_t Parcel::readUint64() const
2091{
2092 return readAligned<uint64_t>();
2093}
2094
Serban Constantinescuf683e012013-11-05 16:53:55 +00002095status_t Parcel::readPointer(uintptr_t *pArg) const
2096{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002097 status_t ret;
2098 binder_uintptr_t ptr;
2099 ret = readAligned(&ptr);
2100 if (!ret)
2101 *pArg = ptr;
2102 return ret;
Serban Constantinescuf683e012013-11-05 16:53:55 +00002103}
2104
2105uintptr_t Parcel::readPointer() const
2106{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002107 return readAligned<binder_uintptr_t>();
Serban Constantinescuf683e012013-11-05 16:53:55 +00002108}
2109
2110
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002111status_t Parcel::readFloat(float *pArg) const
2112{
Andreas Huber84a6d042009-08-17 13:33:27 -07002113 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002114}
2115
2116
2117float Parcel::readFloat() const
2118{
Andreas Huber84a6d042009-08-17 13:33:27 -07002119 return readAligned<float>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002120}
2121
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08002122#if defined(__mips__) && defined(__mips_hard_float)
2123
2124status_t Parcel::readDouble(double *pArg) const
2125{
2126 union {
2127 double d;
2128 unsigned long long ll;
2129 } u;
Narayan Kamath2c68d382014-06-04 15:04:29 +01002130 u.d = 0;
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08002131 status_t status;
2132 status = readAligned(&u.ll);
2133 *pArg = u.d;
2134 return status;
2135}
2136
2137double Parcel::readDouble() const
2138{
2139 union {
2140 double d;
2141 unsigned long long ll;
2142 } u;
2143 u.ll = readAligned<unsigned long long>();
2144 return u.d;
2145}
2146
2147#else
2148
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002149status_t Parcel::readDouble(double *pArg) const
2150{
Andreas Huber84a6d042009-08-17 13:33:27 -07002151 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002152}
2153
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002154double Parcel::readDouble() const
2155{
Andreas Huber84a6d042009-08-17 13:33:27 -07002156 return readAligned<double>();
2157}
2158
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08002159#endif
2160
Casey Dahlind6848f52015-10-15 15:44:59 -07002161status_t Parcel::readBool(bool *pArg) const
2162{
Manoj Gupta6eb62052017-07-12 10:29:15 -07002163 int32_t tmp = 0;
Casey Dahlind6848f52015-10-15 15:44:59 -07002164 status_t ret = readInt32(&tmp);
2165 *pArg = (tmp != 0);
2166 return ret;
2167}
2168
2169bool Parcel::readBool() const
2170{
2171 return readInt32() != 0;
2172}
2173
2174status_t Parcel::readChar(char16_t *pArg) const
2175{
Manoj Gupta6eb62052017-07-12 10:29:15 -07002176 int32_t tmp = 0;
Casey Dahlind6848f52015-10-15 15:44:59 -07002177 status_t ret = readInt32(&tmp);
2178 *pArg = char16_t(tmp);
2179 return ret;
2180}
2181
2182char16_t Parcel::readChar() const
2183{
2184 return char16_t(readInt32());
2185}
2186
2187status_t Parcel::readByte(int8_t *pArg) const
2188{
Manoj Gupta6eb62052017-07-12 10:29:15 -07002189 int32_t tmp = 0;
Casey Dahlind6848f52015-10-15 15:44:59 -07002190 status_t ret = readInt32(&tmp);
2191 *pArg = int8_t(tmp);
2192 return ret;
2193}
2194
2195int8_t Parcel::readByte() const
2196{
2197 return int8_t(readInt32());
2198}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002199
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08002200status_t Parcel::readUtf8FromUtf16(std::string* str) const {
2201 size_t utf16Size = 0;
2202 const char16_t* src = readString16Inplace(&utf16Size);
2203 if (!src) {
2204 return UNEXPECTED_NULL;
2205 }
2206
2207 // Save ourselves the trouble, we're done.
2208 if (utf16Size == 0u) {
2209 str->clear();
2210 return NO_ERROR;
2211 }
2212
Sergio Giro9b39ebe2016-06-28 18:19:33 +01002213 // Allow for closing '\0'
2214 ssize_t utf8Size = utf16_to_utf8_length(src, utf16Size) + 1;
2215 if (utf8Size < 1) {
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08002216 return BAD_VALUE;
2217 }
2218 // Note that while it is probably safe to assume string::resize keeps a
Sergio Giro9b39ebe2016-06-28 18:19:33 +01002219 // spare byte around for the trailing null, we still pass the size including the trailing null
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08002220 str->resize(utf8Size);
Sergio Giro9b39ebe2016-06-28 18:19:33 +01002221 utf16_to_utf8(src, utf16Size, &((*str)[0]), utf8Size);
2222 str->resize(utf8Size - 1);
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08002223 return NO_ERROR;
2224}
2225
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002226const char* Parcel::readCString() const
2227{
Steven Morelandd0d4b582019-05-17 13:14:06 -07002228 if (mDataPos < mDataSize) {
2229 const size_t avail = mDataSize-mDataPos;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002230 const char* str = reinterpret_cast<const char*>(mData+mDataPos);
2231 // is the string's trailing NUL within the parcel's valid bounds?
2232 const char* eos = reinterpret_cast<const char*>(memchr(str, 0, avail));
2233 if (eos) {
2234 const size_t len = eos - str;
Nick Kralevichb6b14232015-04-02 09:36:02 -07002235 mDataPos += pad_size(len+1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002236 ALOGV("readCString Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002237 return str;
2238 }
2239 }
Yi Kong91635562018-06-07 14:38:36 -07002240 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002241}
2242
2243String8 Parcel::readString8() const
2244{
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06002245 size_t len;
2246 const char* str = readString8Inplace(&len);
2247 if (str) return String8(str, len);
Steven Moreland3a667492023-06-02 21:41:39 +00002248
2249 if (!mServiceFuzzing) {
2250 ALOGE("Reading a NULL string not supported here.");
2251 }
2252
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06002253 return String8();
Roshan Pius87b64d22016-07-18 12:51:02 -07002254}
2255
2256status_t Parcel::readString8(String8* pArg) const
2257{
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06002258 size_t len;
2259 const char* str = readString8Inplace(&len);
2260 if (str) {
2261 pArg->setTo(str, len);
2262 return 0;
2263 } else {
Roshan Pius87b64d22016-07-18 12:51:02 -07002264 *pArg = String8();
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06002265 return UNEXPECTED_NULL;
Roshan Pius87b64d22016-07-18 12:51:02 -07002266 }
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06002267}
2268
2269const char* Parcel::readString8Inplace(size_t* outLen) const
2270{
2271 int32_t size = readInt32();
2272 // watch for potential int overflow from size+1
2273 if (size >= 0 && size < INT32_MAX) {
2274 *outLen = size;
2275 const char* str = (const char*)readInplace(size+1);
Steven Moreland61d0f842020-12-04 21:13:03 +00002276 if (str != nullptr) {
2277 if (str[size] == '\0') {
2278 return str;
2279 }
2280 android_errorWriteLog(0x534e4554, "172655291");
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06002281 }
Roshan Pius87b64d22016-07-18 12:51:02 -07002282 }
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06002283 *outLen = 0;
2284 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002285}
2286
2287String16 Parcel::readString16() const
2288{
2289 size_t len;
2290 const char16_t* str = readString16Inplace(&len);
2291 if (str) return String16(str, len);
Steven Moreland3a667492023-06-02 21:41:39 +00002292
2293 if (!mServiceFuzzing) {
2294 ALOGE("Reading a NULL string not supported here.");
2295 }
2296
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002297 return String16();
2298}
2299
Casey Dahlinb9872622015-11-25 15:09:45 -08002300
Casey Dahlin451ff582015-10-19 18:12:18 -07002301status_t Parcel::readString16(String16* pArg) const
2302{
2303 size_t len;
2304 const char16_t* str = readString16Inplace(&len);
2305 if (str) {
Casey Dahlin1515ea12015-10-20 16:26:23 -07002306 pArg->setTo(str, len);
Casey Dahlin451ff582015-10-19 18:12:18 -07002307 return 0;
2308 } else {
2309 *pArg = String16();
Christopher Wiley4db672d2015-11-10 09:44:30 -08002310 return UNEXPECTED_NULL;
Casey Dahlin451ff582015-10-19 18:12:18 -07002311 }
2312}
2313
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002314const char16_t* Parcel::readString16Inplace(size_t* outLen) const
2315{
2316 int32_t size = readInt32();
2317 // watch for potential int overflow from size+1
2318 if (size >= 0 && size < INT32_MAX) {
2319 *outLen = size;
2320 const char16_t* str = (const char16_t*)readInplace((size+1)*sizeof(char16_t));
Steven Moreland61d0f842020-12-04 21:13:03 +00002321 if (str != nullptr) {
2322 if (str[size] == u'\0') {
2323 return str;
2324 }
2325 android_errorWriteLog(0x534e4554, "172655291");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002326 }
2327 }
2328 *outLen = 0;
Yi Kong91635562018-06-07 14:38:36 -07002329 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002330}
2331
Casey Dahlinf0c13772015-10-27 18:33:56 -07002332status_t Parcel::readStrongBinder(sp<IBinder>* val) const
2333{
Christopher Wiley35d77ca2016-03-08 10:49:51 -08002334 status_t status = readNullableStrongBinder(val);
2335 if (status == OK && !val->get()) {
Steven Moreland3a667492023-06-02 21:41:39 +00002336 if (!mServiceFuzzing) {
2337 ALOGW("Expecting binder but got null!");
2338 }
Christopher Wiley35d77ca2016-03-08 10:49:51 -08002339 status = UNEXPECTED_NULL;
2340 }
2341 return status;
2342}
2343
2344status_t Parcel::readNullableStrongBinder(sp<IBinder>* val) const
2345{
Steven Morelanda86a3562019-08-01 23:28:34 +00002346 return unflattenBinder(val);
Casey Dahlinf0c13772015-10-27 18:33:56 -07002347}
2348
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002349sp<IBinder> Parcel::readStrongBinder() const
2350{
2351 sp<IBinder> val;
Christopher Wiley35d77ca2016-03-08 10:49:51 -08002352 // Note that a lot of code in Android reads binders by hand with this
2353 // method, and that code has historically been ok with getting nullptr
2354 // back (while ignoring error codes).
2355 readNullableStrongBinder(&val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002356 return val;
2357}
2358
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07002359int32_t Parcel::readExceptionCode() const
2360{
Christopher Wiley09eb7492015-11-09 15:06:15 -08002361 binder::Status status;
2362 status.readFromParcel(*this);
2363 return status.exceptionCode();
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07002364}
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002365
Tomasz Wasilczyk232d0b32023-10-09 17:37:16 +00002366#ifndef BINDER_DISABLE_NATIVE_HANDLE
Mathias Agopiana47f02a2009-05-21 16:29:38 -07002367native_handle* Parcel::readNativeHandle() const
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002368{
2369 int numFds, numInts;
2370 status_t err;
2371 err = readInt32(&numFds);
Yi Kong91635562018-06-07 14:38:36 -07002372 if (err != NO_ERROR) return nullptr;
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002373 err = readInt32(&numInts);
Yi Kong91635562018-06-07 14:38:36 -07002374 if (err != NO_ERROR) return nullptr;
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002375
Mathias Agopiana47f02a2009-05-21 16:29:38 -07002376 native_handle* h = native_handle_create(numFds, numInts);
Adam Lesinskieaac99a2015-05-12 17:35:48 -07002377 if (!h) {
Yi Kong91635562018-06-07 14:38:36 -07002378 return nullptr;
Adam Lesinskieaac99a2015-05-12 17:35:48 -07002379 }
2380
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002381 for (int i=0 ; err==NO_ERROR && i<numFds ; i++) {
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002382 h->data[i] = fcntl(readFileDescriptor(), F_DUPFD_CLOEXEC, 0);
Marco Nelissen1de79662016-04-26 08:44:09 -07002383 if (h->data[i] < 0) {
2384 for (int j = 0; j < i; j++) {
2385 close(h->data[j]);
2386 }
2387 native_handle_delete(h);
Yi Kong91635562018-06-07 14:38:36 -07002388 return nullptr;
Marco Nelissen1de79662016-04-26 08:44:09 -07002389 }
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002390 }
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002391 err = read(h->data + numFds, sizeof(int)*numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002392 if (err != NO_ERROR) {
Mathias Agopiana47f02a2009-05-21 16:29:38 -07002393 native_handle_close(h);
2394 native_handle_delete(h);
Yi Kong91635562018-06-07 14:38:36 -07002395 h = nullptr;
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002396 }
2397 return h;
2398}
Tomasz Wasilczyk232d0b32023-10-09 17:37:16 +00002399#endif
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002400
Frederick Mayle69a0c992022-05-26 20:38:39 +00002401int Parcel::readFileDescriptor() const {
2402 if (const auto* rpcFields = maybeRpcFields()) {
2403 if (!std::binary_search(rpcFields->mObjectPositions.begin(),
2404 rpcFields->mObjectPositions.end(), mDataPos)) {
Steven Moreland3a667492023-06-02 21:41:39 +00002405 if (!mServiceFuzzing) {
2406 ALOGW("Attempt to read file descriptor from Parcel %p at offset %zu that is not in "
2407 "the object list",
2408 this, mDataPos);
2409 }
Frederick Mayle69a0c992022-05-26 20:38:39 +00002410 return BAD_TYPE;
2411 }
2412
2413 int32_t objectType = readInt32();
2414 if (objectType != RpcFields::TYPE_NATIVE_FILE_DESCRIPTOR) {
2415 return BAD_TYPE;
2416 }
2417
2418 int32_t fdIndex = readInt32();
2419 if (rpcFields->mFds == nullptr || fdIndex < 0 ||
2420 static_cast<size_t>(fdIndex) >= rpcFields->mFds->size()) {
2421 ALOGE("RPC Parcel contains invalid file descriptor index. index=%d fd_count=%zu",
2422 fdIndex, rpcFields->mFds ? rpcFields->mFds->size() : 0);
2423 return BAD_VALUE;
2424 }
2425 return toRawFd(rpcFields->mFds->at(fdIndex));
2426 }
2427
Andrei Homescua9cca9c2022-05-03 04:48:01 +00002428#ifdef BINDER_WITH_KERNEL_IPC
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002429 const flat_binder_object* flat = readObject(true);
Casey Dahlin06673e32015-11-23 13:24:23 -08002430
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002431 if (flat && flat->hdr.type == BINDER_TYPE_FD) {
Casey Dahlin06673e32015-11-23 13:24:23 -08002432 return flat->handle;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002433 }
Casey Dahlin06673e32015-11-23 13:24:23 -08002434
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002435 return BAD_TYPE;
Andrei Homescua9cca9c2022-05-03 04:48:01 +00002436#else // BINDER_WITH_KERNEL_IPC
2437 LOG_ALWAYS_FATAL("Binder kernel driver disabled at build time");
2438 return INVALID_OPERATION;
2439#endif // BINDER_WITH_KERNEL_IPC
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002440}
2441
Frederick Mayle69a0c992022-05-26 20:38:39 +00002442int Parcel::readParcelFileDescriptor() const {
Dianne Hackborn1941a402016-08-29 12:30:43 -07002443 int32_t hasComm = readInt32();
2444 int fd = readFileDescriptor();
2445 if (hasComm != 0) {
Steven Morelandb73806a2018-11-12 19:35:47 -08002446 // detach (owned by the binder driver)
2447 int comm = readFileDescriptor();
2448
2449 // warning: this must be kept in sync with:
2450 // frameworks/base/core/java/android/os/ParcelFileDescriptor.java
2451 enum ParcelFileDescriptorStatus {
2452 DETACHED = 2,
2453 };
2454
2455#if BYTE_ORDER == BIG_ENDIAN
2456 const int32_t message = ParcelFileDescriptorStatus::DETACHED;
2457#endif
2458#if BYTE_ORDER == LITTLE_ENDIAN
2459 const int32_t message = __builtin_bswap32(ParcelFileDescriptorStatus::DETACHED);
2460#endif
2461
2462 ssize_t written = TEMP_FAILURE_RETRY(
2463 ::write(comm, &message, sizeof(message)));
2464
Krzysztof Kosińskia8406892021-02-02 17:59:43 -08002465 if (written != sizeof(message)) {
Steven Morelandb73806a2018-11-12 19:35:47 -08002466 ALOGW("Failed to detach ParcelFileDescriptor written: %zd err: %s",
2467 written, strerror(errno));
2468 return BAD_TYPE;
2469 }
Dianne Hackborn1941a402016-08-29 12:30:43 -07002470 }
2471 return fd;
2472}
2473
Tomasz Wasilczyk639490b2023-11-01 13:49:41 -07002474status_t Parcel::readUniqueFileDescriptor(unique_fd* val) const {
Casey Dahlin06673e32015-11-23 13:24:23 -08002475 int got = readFileDescriptor();
2476
2477 if (got == BAD_TYPE) {
2478 return BAD_TYPE;
2479 }
2480
Andrei Homescu24ad36e2022-08-04 01:33:33 +00002481 int dupFd;
Tomasz Wasilczyk0d9dec22023-10-06 20:28:49 +00002482 if (status_t err = binder::os::dupFileDescriptor(got, &dupFd); err != OK) {
Andrei Homescu24ad36e2022-08-04 01:33:33 +00002483 return BAD_VALUE;
2484 }
2485
2486 val->reset(dupFd);
Casey Dahlin06673e32015-11-23 13:24:23 -08002487
2488 if (val->get() < 0) {
2489 return BAD_VALUE;
2490 }
2491
2492 return OK;
2493}
2494
Tomasz Wasilczyk639490b2023-11-01 13:49:41 -07002495status_t Parcel::readUniqueParcelFileDescriptor(unique_fd* val) const {
Ryo Hashimotobf551892018-05-31 16:58:35 +09002496 int got = readParcelFileDescriptor();
2497
2498 if (got == BAD_TYPE) {
2499 return BAD_TYPE;
2500 }
2501
Andrei Homescu24ad36e2022-08-04 01:33:33 +00002502 int dupFd;
Tomasz Wasilczyk0d9dec22023-10-06 20:28:49 +00002503 if (status_t err = binder::os::dupFileDescriptor(got, &dupFd); err != OK) {
Andrei Homescu24ad36e2022-08-04 01:33:33 +00002504 return BAD_VALUE;
2505 }
2506
2507 val->reset(dupFd);
Ryo Hashimotobf551892018-05-31 16:58:35 +09002508
2509 if (val->get() < 0) {
2510 return BAD_VALUE;
2511 }
2512
2513 return OK;
2514}
Casey Dahlin06673e32015-11-23 13:24:23 -08002515
Jeff Brown5707dbf2011-09-23 21:17:56 -07002516status_t Parcel::readBlob(size_t len, ReadableBlob* outBlob) const
2517{
Tomasz Wasilczykbca8f942023-10-09 17:42:37 +00002518#ifdef BINDER_DISABLE_BLOB
2519 (void)len;
2520 (void)outBlob;
2521 return INVALID_OPERATION;
2522#else
Jeff Brown13b16042014-11-11 16:44:25 -08002523 int32_t blobType;
2524 status_t status = readInt32(&blobType);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002525 if (status) return status;
2526
Jeff Brown13b16042014-11-11 16:44:25 -08002527 if (blobType == BLOB_INPLACE) {
Steve Block6807e592011-10-20 11:56:00 +01002528 ALOGV("readBlob: read in place");
Jeff Brown5707dbf2011-09-23 21:17:56 -07002529 const void* ptr = readInplace(len);
2530 if (!ptr) return BAD_VALUE;
2531
Jeff Brown13b16042014-11-11 16:44:25 -08002532 outBlob->init(-1, const_cast<void*>(ptr), len, false);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002533 return NO_ERROR;
2534 }
2535
Steve Block6807e592011-10-20 11:56:00 +01002536 ALOGV("readBlob: read from ashmem");
Jeff Brown13b16042014-11-11 16:44:25 -08002537 bool isMutable = (blobType == BLOB_ASHMEM_MUTABLE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002538 int fd = readFileDescriptor();
2539 if (fd == int(BAD_TYPE)) return BAD_VALUE;
2540
Jorim Jaggi150b4ef2018-07-13 11:18:30 +00002541 if (!ashmem_valid(fd)) {
2542 ALOGE("invalid fd");
2543 return BAD_VALUE;
2544 }
Marco Nelissen7a96ec42018-06-06 07:37:46 -07002545 int size = ashmem_get_size_region(fd);
2546 if (size < 0 || size_t(size) < len) {
Jorim Jaggi150b4ef2018-07-13 11:18:30 +00002547 ALOGE("request size %zu does not match fd size %d", len, size);
Marco Nelissen7a96ec42018-06-06 07:37:46 -07002548 return BAD_VALUE;
2549 }
Yi Kong91635562018-06-07 14:38:36 -07002550 void* ptr = ::mmap(nullptr, len, isMutable ? PROT_READ | PROT_WRITE : PROT_READ,
Jeff Brown13b16042014-11-11 16:44:25 -08002551 MAP_SHARED, fd, 0);
Narayan Kamath9ea09752014-10-08 17:35:45 +01002552 if (ptr == MAP_FAILED) return NO_MEMORY;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002553
Jeff Brown13b16042014-11-11 16:44:25 -08002554 outBlob->init(fd, ptr, len, isMutable);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002555 return NO_ERROR;
Tomasz Wasilczykbca8f942023-10-09 17:42:37 +00002556#endif
Jeff Brown5707dbf2011-09-23 21:17:56 -07002557}
2558
Mathias Agopiane1424282013-07-29 21:24:40 -07002559status_t Parcel::read(FlattenableHelperInterface& val) const
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002560{
2561 // size
2562 const size_t len = this->readInt32();
2563 const size_t fd_count = this->readInt32();
2564
Andrei Homescu1519b982022-06-09 02:04:44 +00002565 if ((len > INT32_MAX) || (fd_count > kMaxFds)) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07002566 // don't accept size_t values which may have come from an
2567 // inadvertent conversion from a negative int.
2568 return BAD_VALUE;
2569 }
2570
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002571 // payload
Nick Kralevichb6b14232015-04-02 09:36:02 -07002572 void const* const buf = this->readInplace(pad_size(len));
Yi Kong91635562018-06-07 14:38:36 -07002573 if (buf == nullptr)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002574 return BAD_VALUE;
2575
Yi Kong91635562018-06-07 14:38:36 -07002576 int* fds = nullptr;
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002577 if (fd_count) {
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002578 fds = new (std::nothrow) int[fd_count];
2579 if (fds == nullptr) {
2580 ALOGE("read: failed to allocate requested %zu fds", fd_count);
2581 return BAD_VALUE;
2582 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002583 }
2584
2585 status_t err = NO_ERROR;
2586 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
Fabien Sanglardd84ff312016-10-21 10:58:26 -07002587 int fd = this->readFileDescriptor();
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002588 if (fd < 0 || ((fds[i] = fcntl(fd, F_DUPFD_CLOEXEC, 0)) < 0)) {
Jun Jiangabf8a2c2014-04-29 14:22:10 +08002589 err = BAD_VALUE;
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002590 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 -07002591 i, fds[i], fd_count, strerror(fd < 0 ? -fd : errno));
2592 // Close all the file descriptors that were dup-ed.
2593 for (size_t j=0; j<i ;j++) {
2594 close(fds[j]);
2595 }
Jun Jiangabf8a2c2014-04-29 14:22:10 +08002596 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002597 }
2598
2599 if (err == NO_ERROR) {
2600 err = val.unflatten(buf, len, fds, fd_count);
2601 }
2602
2603 if (fd_count) {
2604 delete [] fds;
2605 }
2606
2607 return err;
2608}
Andrei Homescua9cca9c2022-05-03 04:48:01 +00002609
2610#ifdef BINDER_WITH_KERNEL_IPC
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002611const flat_binder_object* Parcel::readObject(bool nullMetaData) const
2612{
Frederick Mayled3c595a2022-05-09 23:02:54 +00002613 const auto* kernelFields = maybeKernelFields();
2614 if (kernelFields == nullptr) {
2615 return nullptr;
2616 }
2617
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002618 const size_t DPOS = mDataPos;
2619 if ((DPOS+sizeof(flat_binder_object)) <= mDataSize) {
2620 const flat_binder_object* obj
2621 = reinterpret_cast<const flat_binder_object*>(mData+DPOS);
2622 mDataPos = DPOS + sizeof(flat_binder_object);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002623 if (!nullMetaData && (obj->cookie == 0 && obj->binder == 0)) {
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002624 // When transferring a NULL object, we don't write it into
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002625 // the object list, so we don't want to check for it when
2626 // reading.
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002627 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002628 return obj;
2629 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002630
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002631 // Ensure that this object is valid...
Frederick Mayled3c595a2022-05-09 23:02:54 +00002632 binder_size_t* const OBJS = kernelFields->mObjects;
2633 const size_t N = kernelFields->mObjectsSize;
2634 size_t opos = kernelFields->mNextObjectHint;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002635
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002636 if (N > 0) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002637 ALOGV("Parcel %p looking for obj at %zu, hint=%zu",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002638 this, DPOS, opos);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002639
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002640 // Start at the current hint position, looking for an object at
2641 // the current data position.
2642 if (opos < N) {
2643 while (opos < (N-1) && OBJS[opos] < DPOS) {
2644 opos++;
2645 }
2646 } else {
2647 opos = N-1;
2648 }
2649 if (OBJS[opos] == DPOS) {
2650 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002651 ALOGV("Parcel %p found obj %zu at index %zu with forward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002652 this, DPOS, opos);
Frederick Mayled3c595a2022-05-09 23:02:54 +00002653 kernelFields->mNextObjectHint = opos + 1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002654 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002655 return obj;
2656 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002657
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002658 // Look backwards for it...
2659 while (opos > 0 && OBJS[opos] > DPOS) {
2660 opos--;
2661 }
2662 if (OBJS[opos] == DPOS) {
2663 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002664 ALOGV("Parcel %p found obj %zu at index %zu with backward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002665 this, DPOS, opos);
Frederick Mayled3c595a2022-05-09 23:02:54 +00002666 kernelFields->mNextObjectHint = opos + 1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002667 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002668 return obj;
2669 }
2670 }
Steven Moreland3a667492023-06-02 21:41:39 +00002671 if (!mServiceFuzzing) {
2672 ALOGW("Attempt to read object from Parcel %p at offset %zu that is not in the object "
2673 "list",
2674 this, DPOS);
2675 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002676 }
Yi Kong91635562018-06-07 14:38:36 -07002677 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002678}
Andrei Homescua9cca9c2022-05-03 04:48:01 +00002679#endif // BINDER_WITH_KERNEL_IPC
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002680
Frederick Mayled3c595a2022-05-09 23:02:54 +00002681void Parcel::closeFileDescriptors() {
Frederick Mayle69a0c992022-05-26 20:38:39 +00002682 if (auto* kernelFields = maybeKernelFields()) {
Andrei Homescua9cca9c2022-05-03 04:48:01 +00002683#ifdef BINDER_WITH_KERNEL_IPC
Frederick Mayle69a0c992022-05-26 20:38:39 +00002684 size_t i = kernelFields->mObjectsSize;
2685 if (i > 0) {
2686 // ALOGI("Closing file descriptors for %zu objects...", i);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002687 }
Frederick Mayle69a0c992022-05-26 20:38:39 +00002688 while (i > 0) {
2689 i--;
2690 const flat_binder_object* flat =
2691 reinterpret_cast<flat_binder_object*>(mData + kernelFields->mObjects[i]);
2692 if (flat->hdr.type == BINDER_TYPE_FD) {
2693 // ALOGI("Closing fd: %ld", flat->handle);
Steven Moreland02f736f2023-06-22 23:03:57 +00002694 // FDs from the kernel are always owned
2695 FdTagClose(flat->handle, this);
Frederick Mayle69a0c992022-05-26 20:38:39 +00002696 }
2697 }
Andrei Homescua9cca9c2022-05-03 04:48:01 +00002698#else // BINDER_WITH_KERNEL_IPC
2699 LOG_ALWAYS_FATAL("Binder kernel driver disabled at build time");
2700#endif // BINDER_WITH_KERNEL_IPC
Frederick Mayle69a0c992022-05-26 20:38:39 +00002701 } else if (auto* rpcFields = maybeRpcFields()) {
2702 rpcFields->mFds.reset();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002703 }
2704}
2705
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002706uintptr_t Parcel::ipcData() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002707{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002708 return reinterpret_cast<uintptr_t>(mData);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002709}
2710
2711size_t Parcel::ipcDataSize() const
2712{
2713 return (mDataSize > mDataPos ? mDataSize : mDataPos);
2714}
2715
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002716uintptr_t Parcel::ipcObjects() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002717{
Frederick Mayled3c595a2022-05-09 23:02:54 +00002718 if (const auto* kernelFields = maybeKernelFields()) {
2719 return reinterpret_cast<uintptr_t>(kernelFields->mObjects);
2720 }
2721 return 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002722}
2723
2724size_t Parcel::ipcObjectsCount() const
2725{
Frederick Mayled3c595a2022-05-09 23:02:54 +00002726 if (const auto* kernelFields = maybeKernelFields()) {
2727 return kernelFields->mObjectsSize;
2728 }
2729 return 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002730}
2731
Frederick Mayled3c595a2022-05-09 23:02:54 +00002732void Parcel::ipcSetDataReference(const uint8_t* data, size_t dataSize, const binder_size_t* objects,
2733 size_t objectsCount, release_func relFunc) {
Steven Moreland438cce82021-04-02 18:04:08 +00002734 // this code uses 'mOwner == nullptr' to understand whether it owns memory
2735 LOG_ALWAYS_FATAL_IF(relFunc == nullptr, "must provide cleanup function");
2736
Steven Morelandceed9bb2020-12-17 01:01:06 +00002737 freeData();
2738
Frederick Mayled3c595a2022-05-09 23:02:54 +00002739 auto* kernelFields = maybeKernelFields();
2740 LOG_ALWAYS_FATAL_IF(kernelFields == nullptr); // guaranteed by freeData.
2741
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002742 mData = const_cast<uint8_t*>(data);
2743 mDataSize = mDataCapacity = dataSize;
Frederick Mayled3c595a2022-05-09 23:02:54 +00002744 kernelFields->mObjects = const_cast<binder_size_t*>(objects);
2745 kernelFields->mObjectsSize = kernelFields->mObjectsCapacity = objectsCount;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002746 mOwner = relFunc;
Steven Morelandceed9bb2020-12-17 01:01:06 +00002747
Andrei Homescua9cca9c2022-05-03 04:48:01 +00002748#ifdef BINDER_WITH_KERNEL_IPC
Steven Morelandceed9bb2020-12-17 01:01:06 +00002749 binder_size_t minOffset = 0;
Frederick Mayled3c595a2022-05-09 23:02:54 +00002750 for (size_t i = 0; i < kernelFields->mObjectsSize; i++) {
2751 binder_size_t offset = kernelFields->mObjects[i];
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002752 if (offset < minOffset) {
Dan Albert3bdc5b82014-11-20 11:50:23 -08002753 ALOGE("%s: bad object offset %" PRIu64 " < %" PRIu64 "\n",
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002754 __func__, (uint64_t)offset, (uint64_t)minOffset);
Frederick Mayled3c595a2022-05-09 23:02:54 +00002755 kernelFields->mObjectsSize = 0;
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002756 break;
2757 }
Martijn Coenen82c75312019-07-24 15:18:30 +02002758 const flat_binder_object* flat
2759 = reinterpret_cast<const flat_binder_object*>(mData + offset);
2760 uint32_t type = flat->hdr.type;
2761 if (!(type == BINDER_TYPE_BINDER || type == BINDER_TYPE_HANDLE ||
2762 type == BINDER_TYPE_FD)) {
2763 // We should never receive other types (eg BINDER_TYPE_FDA) as long as we don't support
2764 // them in libbinder. If we do receive them, it probably means a kernel bug; try to
Steven Morelandf2e0a952021-11-01 18:17:23 -07002765 // recover gracefully by clearing out the objects.
Martijn Coenen82c75312019-07-24 15:18:30 +02002766 android_errorWriteLog(0x534e4554, "135930648");
Steven Morelandf2e0a952021-11-01 18:17:23 -07002767 android_errorWriteLog(0x534e4554, "203847542");
Martijn Coenen82c75312019-07-24 15:18:30 +02002768 ALOGE("%s: unsupported type object (%" PRIu32 ") at offset %" PRIu64 "\n",
2769 __func__, type, (uint64_t)offset);
Steven Morelandf2e0a952021-11-01 18:17:23 -07002770
2771 // WARNING: callers of ipcSetDataReference need to make sure they
2772 // don't rely on mObjectsSize in their release_func.
Frederick Mayled3c595a2022-05-09 23:02:54 +00002773 kernelFields->mObjectsSize = 0;
Martijn Coenen82c75312019-07-24 15:18:30 +02002774 break;
2775 }
Steven Moreland02f736f2023-06-22 23:03:57 +00002776 if (type == BINDER_TYPE_FD) {
2777 // FDs from the kernel are always owned
Tomasz Wasilczyke97f3a82024-04-30 10:37:32 -07002778 FdTag(flat->handle, nullptr, this);
Steven Moreland02f736f2023-06-22 23:03:57 +00002779 }
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002780 minOffset = offset + sizeof(flat_binder_object);
2781 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002782 scanForFds();
Andrei Homescua9cca9c2022-05-03 04:48:01 +00002783#else // BINDER_WITH_KERNEL_IPC
2784 LOG_ALWAYS_FATAL_IF(objectsCount != 0,
2785 "Non-zero objects count passed to Parcel with kernel driver disabled");
2786#endif // BINDER_WITH_KERNEL_IPC
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002787}
2788
Frederick Mayleffe9ac22022-06-30 02:07:36 +00002789status_t Parcel::rpcSetDataReference(
2790 const sp<RpcSession>& session, const uint8_t* data, size_t dataSize,
2791 const uint32_t* objectTable, size_t objectTableSize,
Tomasz Wasilczyk639490b2023-11-01 13:49:41 -07002792 std::vector<std::variant<unique_fd, borrowed_fd>>&& ancillaryFds, release_func relFunc) {
Frederick Mayled3c595a2022-05-09 23:02:54 +00002793 // this code uses 'mOwner == nullptr' to understand whether it owns memory
2794 LOG_ALWAYS_FATAL_IF(relFunc == nullptr, "must provide cleanup function");
2795
2796 LOG_ALWAYS_FATAL_IF(session == nullptr);
2797
Frederick Mayle694e9732022-07-15 00:24:58 +00002798 if (objectTableSize != ancillaryFds.size()) {
2799 ALOGE("objectTableSize=%zu ancillaryFds.size=%zu", objectTableSize, ancillaryFds.size());
2800 relFunc(data, dataSize, nullptr, 0);
2801 return BAD_VALUE;
2802 }
2803 for (size_t i = 0; i < objectTableSize; i++) {
2804 uint32_t minObjectEnd;
2805 if (__builtin_add_overflow(objectTable[i], sizeof(RpcFields::ObjectType), &minObjectEnd) ||
2806 minObjectEnd >= dataSize) {
2807 ALOGE("received out of range object position: %" PRIu32 " (parcel size is %zu)",
2808 objectTable[i], dataSize);
2809 relFunc(data, dataSize, nullptr, 0);
2810 return BAD_VALUE;
2811 }
2812 }
2813
Frederick Mayled3c595a2022-05-09 23:02:54 +00002814 freeData();
2815 markForRpc(session);
2816
Frederick Mayle69a0c992022-05-26 20:38:39 +00002817 auto* rpcFields = maybeRpcFields();
2818 LOG_ALWAYS_FATAL_IF(rpcFields == nullptr); // guaranteed by markForRpc.
2819
Frederick Mayled3c595a2022-05-09 23:02:54 +00002820 mData = const_cast<uint8_t*>(data);
2821 mDataSize = mDataCapacity = dataSize;
2822 mOwner = relFunc;
Frederick Mayle69a0c992022-05-26 20:38:39 +00002823
Frederick Mayle69a0c992022-05-26 20:38:39 +00002824 rpcFields->mObjectPositions.reserve(objectTableSize);
2825 for (size_t i = 0; i < objectTableSize; i++) {
2826 rpcFields->mObjectPositions.push_back(objectTable[i]);
2827 }
2828 if (!ancillaryFds.empty()) {
2829 rpcFields->mFds = std::make_unique<decltype(rpcFields->mFds)::element_type>();
Frederick Mayleffe9ac22022-06-30 02:07:36 +00002830 *rpcFields->mFds = std::move(ancillaryFds);
Frederick Mayle69a0c992022-05-26 20:38:39 +00002831 }
2832
2833 return OK;
Frederick Mayled3c595a2022-05-09 23:02:54 +00002834}
2835
Pawan Wagh7063b522022-09-28 18:52:26 +00002836void Parcel::print(std::ostream& to, uint32_t /*flags*/) const {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002837 to << "Parcel(";
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002838
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002839 if (errorCheck() != NO_ERROR) {
2840 const status_t err = errorCheck();
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002841 to << "Error: " << (void*)(intptr_t)err << " \"" << strerror(-err) << "\"";
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002842 } else if (dataSize() > 0) {
2843 const uint8_t* DATA = data();
Pawan Wagh7063b522022-09-28 18:52:26 +00002844 to << "\t" << HexDump(DATA, dataSize());
Andrei Homescua9cca9c2022-05-03 04:48:01 +00002845#ifdef BINDER_WITH_KERNEL_IPC
Frederick Mayled3c595a2022-05-09 23:02:54 +00002846 if (const auto* kernelFields = maybeKernelFields()) {
2847 const binder_size_t* OBJS = kernelFields->mObjects;
2848 const size_t N = objectsCount();
2849 for (size_t i = 0; i < N; i++) {
2850 const flat_binder_object* flat =
2851 reinterpret_cast<const flat_binder_object*>(DATA + OBJS[i]);
Pawan Wagh7063b522022-09-28 18:52:26 +00002852 to << "Object #" << i << " @ " << (void*)OBJS[i] << ": "
Frederick Mayled3c595a2022-05-09 23:02:54 +00002853 << TypeCode(flat->hdr.type & 0x7f7f7f00) << " = " << flat->binder;
2854 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002855 }
Andrei Homescua9cca9c2022-05-03 04:48:01 +00002856#endif // BINDER_WITH_KERNEL_IPC
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002857 } else {
2858 to << "NULL";
2859 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002860
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002861 to << ")";
2862}
2863
2864void Parcel::releaseObjects()
2865{
Frederick Mayled3c595a2022-05-09 23:02:54 +00002866 auto* kernelFields = maybeKernelFields();
2867 if (kernelFields == nullptr) {
2868 return;
2869 }
2870
Andrei Homescua9cca9c2022-05-03 04:48:01 +00002871#ifdef BINDER_WITH_KERNEL_IPC
Frederick Mayled3c595a2022-05-09 23:02:54 +00002872 size_t i = kernelFields->mObjectsSize;
Martijn Coenen69390d42018-10-22 15:18:10 +02002873 if (i == 0) {
2874 return;
2875 }
2876 sp<ProcessState> proc(ProcessState::self());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002877 uint8_t* const data = mData;
Frederick Mayled3c595a2022-05-09 23:02:54 +00002878 binder_size_t* const objects = kernelFields->mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002879 while (i > 0) {
2880 i--;
Frederick Mayled3c595a2022-05-09 23:02:54 +00002881 const flat_binder_object* flat = reinterpret_cast<flat_binder_object*>(data + objects[i]);
Steven Morelandc673f1f2021-10-07 18:23:35 -07002882 release_object(proc, *flat, this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002883 }
Andrei Homescua9cca9c2022-05-03 04:48:01 +00002884#endif // BINDER_WITH_KERNEL_IPC
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002885}
2886
2887void Parcel::acquireObjects()
2888{
Frederick Mayled3c595a2022-05-09 23:02:54 +00002889 auto* kernelFields = maybeKernelFields();
2890 if (kernelFields == nullptr) {
2891 return;
2892 }
2893
Andrei Homescua9cca9c2022-05-03 04:48:01 +00002894#ifdef BINDER_WITH_KERNEL_IPC
Frederick Mayled3c595a2022-05-09 23:02:54 +00002895 size_t i = kernelFields->mObjectsSize;
Martijn Coenen69390d42018-10-22 15:18:10 +02002896 if (i == 0) {
2897 return;
2898 }
2899 const sp<ProcessState> proc(ProcessState::self());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002900 uint8_t* const data = mData;
Frederick Mayled3c595a2022-05-09 23:02:54 +00002901 binder_size_t* const objects = kernelFields->mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002902 while (i > 0) {
2903 i--;
Frederick Mayled3c595a2022-05-09 23:02:54 +00002904 const flat_binder_object* flat = reinterpret_cast<flat_binder_object*>(data + objects[i]);
Steven Morelandc673f1f2021-10-07 18:23:35 -07002905 acquire_object(proc, *flat, this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002906 }
Andrei Homescua9cca9c2022-05-03 04:48:01 +00002907#endif // BINDER_WITH_KERNEL_IPC
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002908}
2909
2910void Parcel::freeData()
2911{
2912 freeDataNoInit();
2913 initState();
2914}
2915
2916void Parcel::freeDataNoInit()
2917{
2918 if (mOwner) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002919 LOG_ALLOC("Parcel %p: freeing other owner data", this);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002920 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
Frederick Mayled3c595a2022-05-09 23:02:54 +00002921 auto* kernelFields = maybeKernelFields();
Frederick Mayle53b6ffe2022-07-15 20:14:01 +00002922 // Close FDs before freeing, otherwise they will leak for kernel binder.
2923 closeFileDescriptors();
2924 mOwner(mData, mDataSize, kernelFields ? kernelFields->mObjects : nullptr,
Frederick Mayled3c595a2022-05-09 23:02:54 +00002925 kernelFields ? kernelFields->mObjectsSize : 0);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002926 } else {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002927 LOG_ALLOC("Parcel %p: freeing allocated data", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002928 releaseObjects();
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002929 if (mData) {
2930 LOG_ALLOC("Parcel %p: freeing with %zu capacity", this, mDataCapacity);
Jeff Sharkey8994c182020-09-11 12:07:10 -06002931 gParcelGlobalAllocSize -= mDataCapacity;
2932 gParcelGlobalAllocCount--;
Steven Morelandf183fdd2020-10-27 00:12:12 +00002933 if (mDeallocZero) {
2934 zeroMemory(mData, mDataSize);
2935 }
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002936 free(mData);
2937 }
Frederick Mayled3c595a2022-05-09 23:02:54 +00002938 auto* kernelFields = maybeKernelFields();
2939 if (kernelFields && kernelFields->mObjects) free(kernelFields->mObjects);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002940 }
2941}
2942
2943status_t Parcel::growData(size_t len)
2944{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002945 if (len > INT32_MAX) {
2946 // don't accept size_t values which may have come from an
2947 // inadvertent conversion from a negative int.
2948 return BAD_VALUE;
2949 }
2950
Martijn Coenen93fe5182020-01-22 10:46:25 +01002951 if (len > SIZE_MAX - mDataSize) return NO_MEMORY; // overflow
2952 if (mDataSize + len > SIZE_MAX / 3) return NO_MEMORY; // overflow
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002953 size_t newSize = ((mDataSize+len)*3)/2;
2954 return (newSize <= mDataSize)
2955 ? (status_t) NO_MEMORY
Steven Moreland042ae822020-05-27 17:45:17 +00002956 : continueWrite(std::max(newSize, (size_t) 128));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002957}
2958
Steven Morelandf183fdd2020-10-27 00:12:12 +00002959static uint8_t* reallocZeroFree(uint8_t* data, size_t oldCapacity, size_t newCapacity, bool zero) {
2960 if (!zero) {
2961 return (uint8_t*)realloc(data, newCapacity);
2962 }
2963 uint8_t* newData = (uint8_t*)malloc(newCapacity);
2964 if (!newData) {
2965 return nullptr;
2966 }
2967
2968 memcpy(newData, data, std::min(oldCapacity, newCapacity));
2969 zeroMemory(data, oldCapacity);
2970 free(data);
2971 return newData;
2972}
2973
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002974status_t Parcel::restartWrite(size_t desired)
2975{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002976 if (desired > INT32_MAX) {
2977 // don't accept size_t values which may have come from an
2978 // inadvertent conversion from a negative int.
2979 return BAD_VALUE;
2980 }
2981
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002982 if (mOwner) {
2983 freeData();
2984 return continueWrite(desired);
2985 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002986
Steven Morelandc6b0dfa2024-03-05 09:10:02 +00002987 releaseObjects();
2988
Steven Morelandf183fdd2020-10-27 00:12:12 +00002989 uint8_t* data = reallocZeroFree(mData, mDataCapacity, desired, mDeallocZero);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002990 if (!data && desired > mDataCapacity) {
Steven Moreland47d7f1e2024-03-08 23:43:21 +00002991 LOG_ALWAYS_FATAL("out of memory");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002992 mError = NO_MEMORY;
2993 return NO_MEMORY;
2994 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002995
Devin Moore4a0a55e2020-06-04 13:23:10 -07002996 if (data || desired == 0) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002997 LOG_ALLOC("Parcel %p: restart from %zu to %zu capacity", this, mDataCapacity, desired);
Jeff Sharkey8994c182020-09-11 12:07:10 -06002998 if (mDataCapacity > desired) {
2999 gParcelGlobalAllocSize -= (mDataCapacity - desired);
3000 } else {
3001 gParcelGlobalAllocSize += (desired - mDataCapacity);
3002 }
3003
Colin Cross83ec65e2015-12-08 17:15:50 -08003004 if (!mData) {
3005 gParcelGlobalAllocCount++;
3006 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003007 mData = data;
3008 mDataCapacity = desired;
3009 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07003010
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003011 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07003012 ALOGV("restartWrite Setting data size of %p to %zu", this, mDataSize);
3013 ALOGV("restartWrite Setting data pos of %p to %zu", this, mDataPos);
3014
Frederick Mayled3c595a2022-05-09 23:02:54 +00003015 if (auto* kernelFields = maybeKernelFields()) {
3016 free(kernelFields->mObjects);
3017 kernelFields->mObjects = nullptr;
3018 kernelFields->mObjectsSize = kernelFields->mObjectsCapacity = 0;
3019 kernelFields->mNextObjectHint = 0;
3020 kernelFields->mObjectsSorted = false;
3021 kernelFields->mHasFds = false;
3022 kernelFields->mFdsKnown = true;
Frederick Mayle69a0c992022-05-26 20:38:39 +00003023 } else if (auto* rpcFields = maybeRpcFields()) {
3024 rpcFields->mObjectPositions.clear();
3025 rpcFields->mFds.reset();
Frederick Mayled3c595a2022-05-09 23:02:54 +00003026 }
Dianne Hackborn8938ed22011-09-28 23:19:47 -04003027 mAllowFds = true;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07003028
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003029 return NO_ERROR;
3030}
3031
3032status_t Parcel::continueWrite(size_t desired)
3033{
Nick Kralevichb6b14232015-04-02 09:36:02 -07003034 if (desired > INT32_MAX) {
3035 // don't accept size_t values which may have come from an
3036 // inadvertent conversion from a negative int.
3037 return BAD_VALUE;
3038 }
3039
Frederick Mayled3c595a2022-05-09 23:02:54 +00003040 auto* kernelFields = maybeKernelFields();
Frederick Mayle69a0c992022-05-26 20:38:39 +00003041 auto* rpcFields = maybeRpcFields();
Frederick Mayled3c595a2022-05-09 23:02:54 +00003042
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003043 // If shrinking, first adjust for any objects that appear
3044 // after the new data size.
Frederick Mayle69a0c992022-05-26 20:38:39 +00003045 size_t objectsSize =
3046 kernelFields ? kernelFields->mObjectsSize : rpcFields->mObjectPositions.size();
3047 if (desired < mDataSize) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003048 if (desired == 0) {
3049 objectsSize = 0;
3050 } else {
Frederick Mayle69a0c992022-05-26 20:38:39 +00003051 if (kernelFields) {
3052 while (objectsSize > 0) {
3053 if (kernelFields->mObjects[objectsSize - 1] < desired) break;
3054 objectsSize--;
3055 }
3056 } else {
3057 while (objectsSize > 0) {
3058 if (rpcFields->mObjectPositions[objectsSize - 1] < desired) break;
3059 objectsSize--;
3060 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003061 }
3062 }
3063 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07003064
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003065 if (mOwner) {
3066 // If the size is going to zero, just release the owner's data.
3067 if (desired == 0) {
3068 freeData();
3069 return NO_ERROR;
3070 }
3071
3072 // If there is a different owner, we need to take
3073 // posession.
3074 uint8_t* data = (uint8_t*)malloc(desired);
3075 if (!data) {
3076 mError = NO_MEMORY;
3077 return NO_MEMORY;
3078 }
Yi Kong91635562018-06-07 14:38:36 -07003079 binder_size_t* objects = nullptr;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07003080
Frederick Mayle69a0c992022-05-26 20:38:39 +00003081 if (kernelFields && objectsSize) {
Nick Kraleviche9881a32015-04-28 16:21:30 -07003082 objects = (binder_size_t*)calloc(objectsSize, sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003083 if (!objects) {
Hyejin Kim3f727c02013-03-09 11:28:54 +09003084 free(data);
3085
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003086 mError = NO_MEMORY;
3087 return NO_MEMORY;
3088 }
3089
3090 // Little hack to only acquire references on objects
3091 // we will be keeping.
Frederick Mayled3c595a2022-05-09 23:02:54 +00003092 size_t oldObjectsSize = kernelFields->mObjectsSize;
3093 kernelFields->mObjectsSize = objectsSize;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003094 acquireObjects();
Frederick Mayled3c595a2022-05-09 23:02:54 +00003095 kernelFields->mObjectsSize = oldObjectsSize;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003096 }
Frederick Mayle69a0c992022-05-26 20:38:39 +00003097 if (rpcFields) {
3098 if (status_t status = truncateRpcObjects(objectsSize); status != OK) {
3099 free(data);
3100 return status;
3101 }
3102 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07003103
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003104 if (mData) {
3105 memcpy(data, mData, mDataSize < desired ? mDataSize : desired);
3106 }
Frederick Mayled3c595a2022-05-09 23:02:54 +00003107 if (objects && kernelFields && kernelFields->mObjects) {
3108 memcpy(objects, kernelFields->mObjects, objectsSize * sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003109 }
Frederick Mayle53b6ffe2022-07-15 20:14:01 +00003110 // ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
3111 if (kernelFields) {
3112 // TODO(b/239222407): This seems wrong. We should only free FDs when
3113 // they are in a truncated section of the parcel.
3114 closeFileDescriptors();
3115 }
3116 mOwner(mData, mDataSize, kernelFields ? kernelFields->mObjects : nullptr,
Frederick Mayled3c595a2022-05-09 23:02:54 +00003117 kernelFields ? kernelFields->mObjectsSize : 0);
Yi Kong91635562018-06-07 14:38:36 -07003118 mOwner = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003119
Dianne Hackborn7e790af2014-11-11 12:22:53 -08003120 LOG_ALLOC("Parcel %p: taking ownership of %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;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003125 mDataSize = (mDataSize < desired) ? mDataSize : desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07003126 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003127 mDataCapacity = desired;
Frederick Mayled3c595a2022-05-09 23:02:54 +00003128 if (kernelFields) {
3129 kernelFields->mObjects = objects;
3130 kernelFields->mObjectsSize = kernelFields->mObjectsCapacity = objectsSize;
3131 kernelFields->mNextObjectHint = 0;
3132 kernelFields->mObjectsSorted = false;
3133 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003134
3135 } else if (mData) {
Frederick Mayled3c595a2022-05-09 23:02:54 +00003136 if (kernelFields && objectsSize < kernelFields->mObjectsSize) {
Andrei Homescua9cca9c2022-05-03 04:48:01 +00003137#ifdef BINDER_WITH_KERNEL_IPC
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003138 // Need to release refs on any objects we are dropping.
3139 const sp<ProcessState> proc(ProcessState::self());
Frederick Mayled3c595a2022-05-09 23:02:54 +00003140 for (size_t i = objectsSize; i < kernelFields->mObjectsSize; i++) {
3141 const flat_binder_object* flat =
3142 reinterpret_cast<flat_binder_object*>(mData + kernelFields->mObjects[i]);
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07003143 if (flat->hdr.type == BINDER_TYPE_FD) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003144 // will need to rescan because we may have lopped off the only FDs
Frederick Mayled3c595a2022-05-09 23:02:54 +00003145 kernelFields->mFdsKnown = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003146 }
Steven Morelandc673f1f2021-10-07 18:23:35 -07003147 release_object(proc, *flat, this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003148 }
Michael Wachenschwanz6af27a82019-06-03 17:24:51 -07003149
3150 if (objectsSize == 0) {
Frederick Mayled3c595a2022-05-09 23:02:54 +00003151 free(kernelFields->mObjects);
3152 kernelFields->mObjects = nullptr;
3153 kernelFields->mObjectsCapacity = 0;
Michael Wachenschwanz6af27a82019-06-03 17:24:51 -07003154 } else {
3155 binder_size_t* objects =
Frederick Mayled3c595a2022-05-09 23:02:54 +00003156 (binder_size_t*)realloc(kernelFields->mObjects,
3157 objectsSize * sizeof(binder_size_t));
Michael Wachenschwanz6af27a82019-06-03 17:24:51 -07003158 if (objects) {
Frederick Mayled3c595a2022-05-09 23:02:54 +00003159 kernelFields->mObjects = objects;
3160 kernelFields->mObjectsCapacity = objectsSize;
Michael Wachenschwanz6af27a82019-06-03 17:24:51 -07003161 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003162 }
Frederick Mayled3c595a2022-05-09 23:02:54 +00003163 kernelFields->mObjectsSize = objectsSize;
3164 kernelFields->mNextObjectHint = 0;
3165 kernelFields->mObjectsSorted = false;
Andrei Homescua9cca9c2022-05-03 04:48:01 +00003166#else // BINDER_WITH_KERNEL_IPC
3167 LOG_ALWAYS_FATAL("Non-zero numObjects for RPC Parcel");
3168#endif // BINDER_WITH_KERNEL_IPC
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003169 }
Frederick Mayle69a0c992022-05-26 20:38:39 +00003170 if (rpcFields) {
3171 if (status_t status = truncateRpcObjects(objectsSize); status != OK) {
3172 return status;
3173 }
3174 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003175
3176 // We own the data, so we can just do a realloc().
3177 if (desired > mDataCapacity) {
Steven Morelandf183fdd2020-10-27 00:12:12 +00003178 uint8_t* data = reallocZeroFree(mData, mDataCapacity, desired, mDeallocZero);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003179 if (data) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08003180 LOG_ALLOC("Parcel %p: continue from %zu to %zu capacity", this, mDataCapacity,
3181 desired);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08003182 gParcelGlobalAllocSize += desired;
3183 gParcelGlobalAllocSize -= mDataCapacity;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003184 mData = data;
3185 mDataCapacity = desired;
Ganesh Mahendranade89892017-09-28 16:56:03 +08003186 } else {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003187 mError = NO_MEMORY;
3188 return NO_MEMORY;
3189 }
3190 } else {
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07003191 if (mDataSize > desired) {
3192 mDataSize = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07003193 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07003194 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003195 if (mDataPos > desired) {
3196 mDataPos = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07003197 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003198 }
3199 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07003200
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003201 } else {
3202 // This is the first data. Easy!
3203 uint8_t* data = (uint8_t*)malloc(desired);
3204 if (!data) {
3205 mError = NO_MEMORY;
3206 return NO_MEMORY;
3207 }
Hyejin Kim3f727c02013-03-09 11:28:54 +09003208
Frederick Mayled3c595a2022-05-09 23:02:54 +00003209 if (!(mDataCapacity == 0 &&
3210 (kernelFields == nullptr ||
3211 (kernelFields->mObjects == nullptr && kernelFields->mObjectsCapacity == 0)))) {
3212 ALOGE("continueWrite: %zu/%p/%zu/%zu", mDataCapacity,
3213 kernelFields ? kernelFields->mObjects : nullptr,
3214 kernelFields ? kernelFields->mObjectsCapacity : 0, desired);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003215 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07003216
Dianne Hackborn7e790af2014-11-11 12:22:53 -08003217 LOG_ALLOC("Parcel %p: allocating with %zu capacity", this, desired);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08003218 gParcelGlobalAllocSize += desired;
3219 gParcelGlobalAllocCount++;
Dianne Hackborn7e790af2014-11-11 12:22:53 -08003220
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003221 mData = data;
3222 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07003223 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
3224 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003225 mDataCapacity = desired;
3226 }
3227
3228 return NO_ERROR;
3229}
3230
Frederick Mayle69a0c992022-05-26 20:38:39 +00003231status_t Parcel::truncateRpcObjects(size_t newObjectsSize) {
3232 auto* rpcFields = maybeRpcFields();
3233 if (newObjectsSize == 0) {
3234 rpcFields->mObjectPositions.clear();
3235 if (rpcFields->mFds) {
3236 rpcFields->mFds->clear();
3237 }
3238 return OK;
3239 }
3240 while (rpcFields->mObjectPositions.size() > newObjectsSize) {
3241 uint32_t pos = rpcFields->mObjectPositions.back();
3242 rpcFields->mObjectPositions.pop_back();
3243 const auto type = *reinterpret_cast<const RpcFields::ObjectType*>(mData + pos);
3244 if (type == RpcFields::TYPE_NATIVE_FILE_DESCRIPTOR) {
3245 const auto fdIndex =
3246 *reinterpret_cast<const int32_t*>(mData + pos + sizeof(RpcFields::ObjectType));
3247 if (rpcFields->mFds == nullptr || fdIndex < 0 ||
3248 static_cast<size_t>(fdIndex) >= rpcFields->mFds->size()) {
3249 ALOGE("RPC Parcel contains invalid file descriptor index. index=%d fd_count=%zu",
3250 fdIndex, rpcFields->mFds ? rpcFields->mFds->size() : 0);
3251 return BAD_VALUE;
3252 }
3253 // In practice, this always removes the last element.
3254 rpcFields->mFds->erase(rpcFields->mFds->begin() + fdIndex);
3255 }
3256 }
3257 return OK;
3258}
3259
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003260void Parcel::initState()
3261{
Dianne Hackborn7e790af2014-11-11 12:22:53 -08003262 LOG_ALLOC("Parcel %p: initState", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003263 mError = NO_ERROR;
Yi Kong91635562018-06-07 14:38:36 -07003264 mData = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003265 mDataSize = 0;
3266 mDataCapacity = 0;
3267 mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07003268 ALOGV("initState Setting data size of %p to %zu", this, mDataSize);
3269 ALOGV("initState Setting data pos of %p to %zu", this, mDataPos);
Frederick Mayled3c595a2022-05-09 23:02:54 +00003270 mVariantFields.emplace<KernelFields>();
Steven Moreland6e5a7752019-08-05 20:30:14 -07003271 mAllowFds = true;
Steven Morelandf183fdd2020-10-27 00:12:12 +00003272 mDeallocZero = false;
Yi Kong91635562018-06-07 14:38:36 -07003273 mOwner = nullptr;
Pawan Wagh104654a2022-11-15 21:18:42 +00003274 mEnforceNoDataAvail = true;
Steven Moreland3a667492023-06-02 21:41:39 +00003275 mServiceFuzzing = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003276}
3277
Bernardo Rufinobbbd88d2021-10-15 14:54:30 +01003278void Parcel::scanForFds() const {
Frederick Mayled3c595a2022-05-09 23:02:54 +00003279 auto* kernelFields = maybeKernelFields();
3280 if (kernelFields == nullptr) {
3281 return;
3282 }
3283 status_t status = hasFileDescriptorsInRange(0, dataSize(), &kernelFields->mHasFds);
Bernardo Rufinobbbd88d2021-10-15 14:54:30 +01003284 ALOGE_IF(status != NO_ERROR, "Error %d calling hasFileDescriptorsInRange()", status);
Frederick Mayled3c595a2022-05-09 23:02:54 +00003285 kernelFields->mFdsKnown = true;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003286}
3287
Andrei Homescua9cca9c2022-05-03 04:48:01 +00003288#ifdef BINDER_WITH_KERNEL_IPC
Dan Sandleraa5c2342015-04-10 10:08:45 -04003289size_t Parcel::getBlobAshmemSize() const
3290{
Adrian Roos6bb31142015-10-22 16:46:12 -07003291 // This used to return the size of all blobs that were written to ashmem, now we're returning
3292 // the ashmem currently referenced by this Parcel, which should be equivalent.
Steven Morelandc673f1f2021-10-07 18:23:35 -07003293 // TODO(b/202029388): Remove method once ABI can be changed.
3294 return getOpenAshmemSize();
Dan Sandleraa5c2342015-04-10 10:08:45 -04003295}
3296
Adrian Rooscbf37262015-10-22 16:12:53 -07003297size_t Parcel::getOpenAshmemSize() const
3298{
Frederick Mayled3c595a2022-05-09 23:02:54 +00003299 auto* kernelFields = maybeKernelFields();
3300 if (kernelFields == nullptr) {
3301 return 0;
3302 }
3303
Steven Morelandc673f1f2021-10-07 18:23:35 -07003304 size_t openAshmemSize = 0;
Tomasz Wasilczykbca8f942023-10-09 17:42:37 +00003305#ifndef BINDER_DISABLE_BLOB
Frederick Mayled3c595a2022-05-09 23:02:54 +00003306 for (size_t i = 0; i < kernelFields->mObjectsSize; i++) {
Steven Morelandc673f1f2021-10-07 18:23:35 -07003307 const flat_binder_object* flat =
Frederick Mayled3c595a2022-05-09 23:02:54 +00003308 reinterpret_cast<const flat_binder_object*>(mData + kernelFields->mObjects[i]);
Steven Morelandc673f1f2021-10-07 18:23:35 -07003309
3310 // cookie is compared against zero for historical reasons
3311 // > obj.cookie = takeOwnership ? 1 : 0;
3312 if (flat->hdr.type == BINDER_TYPE_FD && flat->cookie != 0 && ashmem_valid(flat->handle)) {
3313 int size = ashmem_get_size_region(flat->handle);
3314 if (__builtin_add_overflow(openAshmemSize, size, &openAshmemSize)) {
3315 ALOGE("Overflow when computing ashmem size.");
3316 return SIZE_MAX;
3317 }
3318 }
3319 }
Tomasz Wasilczykbca8f942023-10-09 17:42:37 +00003320#endif
Steven Morelandc673f1f2021-10-07 18:23:35 -07003321 return openAshmemSize;
Jeff Brown5707dbf2011-09-23 21:17:56 -07003322}
Andrei Homescua9cca9c2022-05-03 04:48:01 +00003323#endif // BINDER_WITH_KERNEL_IPC
Jeff Brown5707dbf2011-09-23 21:17:56 -07003324
3325// --- Parcel::Blob ---
3326
3327Parcel::Blob::Blob() :
Yi Kong91635562018-06-07 14:38:36 -07003328 mFd(-1), mData(nullptr), mSize(0), mMutable(false) {
Jeff Brown5707dbf2011-09-23 21:17:56 -07003329}
3330
3331Parcel::Blob::~Blob() {
3332 release();
3333}
3334
3335void Parcel::Blob::release() {
Jeff Brown13b16042014-11-11 16:44:25 -08003336 if (mFd != -1 && mData) {
Mike McTernan733610f2024-08-27 11:48:30 +01003337 if (::munmap(mData, mSize) == -1) {
3338 ALOGW("munmap() failed: %s", strerror(errno));
3339 }
Jeff Brown5707dbf2011-09-23 21:17:56 -07003340 }
3341 clear();
3342}
3343
Jeff Brown13b16042014-11-11 16:44:25 -08003344void Parcel::Blob::init(int fd, void* data, size_t size, bool isMutable) {
3345 mFd = fd;
Jeff Brown5707dbf2011-09-23 21:17:56 -07003346 mData = data;
3347 mSize = size;
Jeff Brown13b16042014-11-11 16:44:25 -08003348 mMutable = isMutable;
Jeff Brown5707dbf2011-09-23 21:17:56 -07003349}
3350
3351void Parcel::Blob::clear() {
Jeff Brown13b16042014-11-11 16:44:25 -08003352 mFd = -1;
Yi Kong91635562018-06-07 14:38:36 -07003353 mData = nullptr;
Jeff Brown5707dbf2011-09-23 21:17:56 -07003354 mSize = 0;
Jeff Brown13b16042014-11-11 16:44:25 -08003355 mMutable = false;
Jeff Brown5707dbf2011-09-23 21:17:56 -07003356}
3357
Steven Moreland61ff8492019-09-26 16:05:45 -07003358} // namespace android