blob: a97d111b979e452588ca3e2f5fcaf1cd46e25a43 [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) {
Steven Morelandc4fe2462024-10-22 00:01:25 +0000671 ALOGW("Failed to duplicate file descriptor %d: %s", oldFd,
672 statusToString(status).c_str());
Andrei Homescufc221502022-10-08 03:51:17 +0000673 }
Tomasz Wasilczyk639490b2023-11-01 13:49:41 -0700674 rpcFields->mFds->emplace_back(unique_fd(newFd));
Frederick Mayle69a0c992022-05-26 20:38:39 +0000675 // Fixup the index in the data.
676 mDataPos = newDataPos + 4;
677 if (status_t status = writeInt32(rpcFields->mFds->size() - 1); status != OK) {
678 return status;
679 }
680 }
681 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700682 }
683
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400684 return err;
685}
686
Steven Morelande96c2b42024-09-30 23:30:28 +0000687int Parcel::compareData(const Parcel& other) const {
Dianne Hackborn15feb9b2017-04-10 15:34:35 -0700688 size_t size = dataSize();
689 if (size != other.dataSize()) {
690 return size < other.dataSize() ? -1 : 1;
691 }
692 return memcmp(data(), other.data(), size);
693}
694
Bernardo Rufino897b1652021-10-08 10:30:20 +0100695status_t Parcel::compareDataInRange(size_t thisOffset, const Parcel& other, size_t otherOffset,
696 size_t len, int* result) const {
697 if (len > INT32_MAX || thisOffset > INT32_MAX || otherOffset > INT32_MAX) {
698 // Don't accept size_t values which may have come from an inadvertent conversion from a
699 // negative int.
700 return BAD_VALUE;
701 }
702 size_t thisLimit;
703 if (__builtin_add_overflow(thisOffset, len, &thisLimit) || thisLimit > mDataSize) {
704 return BAD_VALUE;
705 }
706 size_t otherLimit;
707 if (__builtin_add_overflow(otherOffset, len, &otherLimit) || otherLimit > other.mDataSize) {
708 return BAD_VALUE;
709 }
710 *result = memcmp(data() + thisOffset, other.data() + otherOffset, len);
711 return NO_ERROR;
712}
713
Jeff Brown13b16042014-11-11 16:44:25 -0800714bool Parcel::allowFds() const
715{
716 return mAllowFds;
717}
718
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700719bool Parcel::pushAllowFds(bool allowFds)
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400720{
721 const bool origValue = mAllowFds;
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700722 if (!allowFds) {
723 mAllowFds = false;
724 }
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400725 return origValue;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700726}
727
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700728void Parcel::restoreAllowFds(bool lastValue)
729{
730 mAllowFds = lastValue;
731}
732
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700733bool Parcel::hasFileDescriptors() const
734{
Frederick Mayled3c595a2022-05-09 23:02:54 +0000735 if (const auto* rpcFields = maybeRpcFields()) {
Frederick Mayle69a0c992022-05-26 20:38:39 +0000736 return rpcFields->mFds != nullptr && !rpcFields->mFds->empty();
Frederick Mayled3c595a2022-05-09 23:02:54 +0000737 }
738 auto* kernelFields = maybeKernelFields();
739 if (!kernelFields->mFdsKnown) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700740 scanForFds();
741 }
Frederick Mayled3c595a2022-05-09 23:02:54 +0000742 return kernelFields->mHasFds;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700743}
744
sandeepbandaruaa9d3a32024-02-22 17:16:53 +0000745status_t Parcel::hasBinders(bool* result) const {
746 status_t status = hasBindersInRange(0, dataSize(), result);
747 ALOGE_IF(status != NO_ERROR, "Error %d calling hasBindersInRange()", status);
748 return status;
749}
750
Steven Moreland4b2f18d2022-03-24 00:32:25 +0000751std::vector<sp<IBinder>> Parcel::debugReadAllStrongBinders() const {
752 std::vector<sp<IBinder>> ret;
753
Andrei Homescua9cca9c2022-05-03 04:48:01 +0000754#ifdef BINDER_WITH_KERNEL_IPC
Frederick Mayled3c595a2022-05-09 23:02:54 +0000755 const auto* kernelFields = maybeKernelFields();
756 if (kernelFields == nullptr) {
757 return ret;
758 }
759
Steven Moreland4b2f18d2022-03-24 00:32:25 +0000760 size_t initPosition = dataPosition();
Frederick Mayled3c595a2022-05-09 23:02:54 +0000761 for (size_t i = 0; i < kernelFields->mObjectsSize; i++) {
762 binder_size_t offset = kernelFields->mObjects[i];
Steven Moreland4b2f18d2022-03-24 00:32:25 +0000763 const flat_binder_object* flat =
764 reinterpret_cast<const flat_binder_object*>(mData + offset);
765 if (flat->hdr.type != BINDER_TYPE_BINDER) continue;
766
767 setDataPosition(offset);
768
769 sp<IBinder> binder = readStrongBinder();
770 if (binder != nullptr) ret.push_back(binder);
771 }
772
773 setDataPosition(initPosition);
Andrei Homescua9cca9c2022-05-03 04:48:01 +0000774#endif // BINDER_WITH_KERNEL_IPC
775
Steven Moreland4b2f18d2022-03-24 00:32:25 +0000776 return ret;
777}
778
779std::vector<int> Parcel::debugReadAllFileDescriptors() const {
780 std::vector<int> ret;
781
Frederick Mayle69a0c992022-05-26 20:38:39 +0000782 if (const auto* kernelFields = maybeKernelFields()) {
Andrei Homescua9cca9c2022-05-03 04:48:01 +0000783#ifdef BINDER_WITH_KERNEL_IPC
Frederick Mayle69a0c992022-05-26 20:38:39 +0000784 size_t initPosition = dataPosition();
785 for (size_t i = 0; i < kernelFields->mObjectsSize; i++) {
786 binder_size_t offset = kernelFields->mObjects[i];
787 const flat_binder_object* flat =
788 reinterpret_cast<const flat_binder_object*>(mData + offset);
789 if (flat->hdr.type != BINDER_TYPE_FD) continue;
790
791 setDataPosition(offset);
792
793 int fd = readFileDescriptor();
794 LOG_ALWAYS_FATAL_IF(fd == -1);
795 ret.push_back(fd);
796 }
797 setDataPosition(initPosition);
Andrei Homescua9cca9c2022-05-03 04:48:01 +0000798#else
799 LOG_ALWAYS_FATAL("Binder kernel driver disabled at build time");
800#endif
Frederick Mayle69a0c992022-05-26 20:38:39 +0000801 } else if (const auto* rpcFields = maybeRpcFields(); rpcFields && rpcFields->mFds) {
802 for (const auto& fd : *rpcFields->mFds) {
803 ret.push_back(toRawFd(fd));
804 }
Frederick Mayled3c595a2022-05-09 23:02:54 +0000805 }
806
Steven Moreland4b2f18d2022-03-24 00:32:25 +0000807 return ret;
808}
809
sandeepbandaruaa9d3a32024-02-22 17:16:53 +0000810status_t Parcel::hasBindersInRange(size_t offset, size_t len, bool* result) const {
811 if (len > INT32_MAX || offset > INT32_MAX) {
812 // Don't accept size_t values which may have come from an inadvertent conversion from a
813 // negative int.
814 return BAD_VALUE;
815 }
816 size_t limit;
817 if (__builtin_add_overflow(offset, len, &limit) || limit > mDataSize) {
818 return BAD_VALUE;
819 }
820 *result = false;
821 if (const auto* kernelFields = maybeKernelFields()) {
822#ifdef BINDER_WITH_KERNEL_IPC
823 for (size_t i = 0; i < kernelFields->mObjectsSize; i++) {
824 size_t pos = kernelFields->mObjects[i];
825 if (pos < offset) continue;
826 if (pos + sizeof(flat_binder_object) > offset + len) {
827 if (kernelFields->mObjectsSorted) {
828 break;
829 } else {
830 continue;
831 }
832 }
833 const flat_binder_object* flat =
834 reinterpret_cast<const flat_binder_object*>(mData + pos);
835 if (flat->hdr.type == BINDER_TYPE_BINDER || flat->hdr.type == BINDER_TYPE_HANDLE) {
836 *result = true;
837 break;
838 }
839 }
840#else
841 LOG_ALWAYS_FATAL("Binder kernel driver disabled at build time");
842 return INVALID_OPERATION;
843#endif // BINDER_WITH_KERNEL_IPC
844 } else if (const auto* rpcFields = maybeRpcFields()) {
845 return INVALID_OPERATION;
846 }
847 return NO_ERROR;
848}
849
Bernardo Rufinobbbd88d2021-10-15 14:54:30 +0100850status_t Parcel::hasFileDescriptorsInRange(size_t offset, size_t len, bool* result) const {
Bernardo Rufino22092af2021-10-07 14:09:24 +0100851 if (len > INT32_MAX || offset > INT32_MAX) {
852 // Don't accept size_t values which may have come from an inadvertent conversion from a
853 // negative int.
854 return BAD_VALUE;
855 }
Bernardo Rufinobbbd88d2021-10-15 14:54:30 +0100856 size_t limit;
857 if (__builtin_add_overflow(offset, len, &limit) || limit > mDataSize) {
Bernardo Rufino22092af2021-10-07 14:09:24 +0100858 return BAD_VALUE;
859 }
Bernardo Rufinobbbd88d2021-10-15 14:54:30 +0100860 *result = false;
Frederick Mayle69a0c992022-05-26 20:38:39 +0000861 if (const auto* kernelFields = maybeKernelFields()) {
Andrei Homescua9cca9c2022-05-03 04:48:01 +0000862#ifdef BINDER_WITH_KERNEL_IPC
Frederick Mayle69a0c992022-05-26 20:38:39 +0000863 for (size_t i = 0; i < kernelFields->mObjectsSize; i++) {
864 size_t pos = kernelFields->mObjects[i];
865 if (pos < offset) continue;
866 if (pos + sizeof(flat_binder_object) > offset + len) {
867 if (kernelFields->mObjectsSorted) {
868 break;
869 } else {
870 continue;
871 }
872 }
873 const flat_binder_object* flat =
874 reinterpret_cast<const flat_binder_object*>(mData + pos);
875 if (flat->hdr.type == BINDER_TYPE_FD) {
876 *result = true;
Frederick Mayled3c595a2022-05-09 23:02:54 +0000877 break;
Frederick Mayled3c595a2022-05-09 23:02:54 +0000878 }
Bernardo Rufino22092af2021-10-07 14:09:24 +0100879 }
Andrei Homescua9cca9c2022-05-03 04:48:01 +0000880#else
881 LOG_ALWAYS_FATAL("Binder kernel driver disabled at build time");
882 return INVALID_OPERATION;
883#endif // BINDER_WITH_KERNEL_IPC
Frederick Mayle69a0c992022-05-26 20:38:39 +0000884 } else if (const auto* rpcFields = maybeRpcFields()) {
885 for (uint32_t pos : rpcFields->mObjectPositions) {
886 if (offset <= pos && pos < limit) {
887 const auto* type = reinterpret_cast<const RpcFields::ObjectType*>(mData + pos);
888 if (*type == RpcFields::TYPE_NATIVE_FILE_DESCRIPTOR) {
889 *result = true;
890 break;
891 }
892 }
Bernardo Rufino22092af2021-10-07 14:09:24 +0100893 }
894 }
Bernardo Rufinobbbd88d2021-10-15 14:54:30 +0100895 return NO_ERROR;
Bernardo Rufino22092af2021-10-07 14:09:24 +0100896}
897
Steven Morelandf183fdd2020-10-27 00:12:12 +0000898void Parcel::markSensitive() const
899{
900 mDeallocZero = true;
901}
902
Steven Moreland5553ac42020-11-11 02:14:45 +0000903void Parcel::markForBinder(const sp<IBinder>& binder) {
Steven Moreland1fda67b2021-04-02 18:35:50 +0000904 LOG_ALWAYS_FATAL_IF(mData != nullptr, "format must be set before data is written");
905
Steven Moreland5553ac42020-11-11 02:14:45 +0000906 if (binder && binder->remoteBinder() && binder->remoteBinder()->isRpcBinder()) {
Steven Moreland99157622021-09-13 16:27:34 -0700907 markForRpc(binder->remoteBinder()->getPrivateAccessor().rpcSession());
Steven Moreland5553ac42020-11-11 02:14:45 +0000908 }
909}
910
Steven Morelandc9939062021-05-05 17:57:41 +0000911void Parcel::markForRpc(const sp<RpcSession>& session) {
Steven Moreland1fda67b2021-04-02 18:35:50 +0000912 LOG_ALWAYS_FATAL_IF(mData != nullptr && mOwner == nullptr,
913 "format must be set before data is written OR on IPC data");
914
Frederick Mayled3c595a2022-05-09 23:02:54 +0000915 mVariantFields.emplace<RpcFields>(session);
Steven Moreland5553ac42020-11-11 02:14:45 +0000916}
917
918bool Parcel::isForRpc() const {
Frederick Mayled3c595a2022-05-09 23:02:54 +0000919 return std::holds_alternative<RpcFields>(mVariantFields);
Steven Moreland5553ac42020-11-11 02:14:45 +0000920}
921
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000922void Parcel::updateWorkSourceRequestHeaderPosition() const {
Frederick Mayled3c595a2022-05-09 23:02:54 +0000923 auto* kernelFields = maybeKernelFields();
924 if (kernelFields == nullptr) {
925 return;
926 }
927
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000928 // Only update the request headers once. We only want to point
929 // to the first headers read/written.
Frederick Mayled3c595a2022-05-09 23:02:54 +0000930 if (!kernelFields->mRequestHeaderPresent) {
931 kernelFields->mWorkSourceRequestHeaderPosition = dataPosition();
932 kernelFields->mRequestHeaderPresent = true;
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000933 }
934}
935
Andrei Homescua9cca9c2022-05-03 04:48:01 +0000936#ifdef BINDER_WITH_KERNEL_IPC
Steven Moreland94e63e12023-12-05 20:29:50 +0000937
938#if defined(__ANDROID__)
939
Steven Morelandb6c7e222021-02-18 19:20:14 +0000940#if defined(__ANDROID_VNDK__)
Steven Morelandd70160f2019-07-23 10:20:38 -0700941constexpr int32_t kHeader = B_PACK_CHARS('V', 'N', 'D', 'R');
Yifan Hong70786532021-10-20 21:41:18 -0700942#elif defined(__ANDROID_RECOVERY__)
943constexpr int32_t kHeader = B_PACK_CHARS('R', 'E', 'C', 'O');
Steven Morelandd70160f2019-07-23 10:20:38 -0700944#else
945constexpr int32_t kHeader = B_PACK_CHARS('S', 'Y', 'S', 'T');
946#endif
Steven Moreland94e63e12023-12-05 20:29:50 +0000947
948#else // ANDROID not defined
949
950// If kernel binder is used in new environments, we need to make sure it's separated
951// out and has a separate header.
952constexpr int32_t kHeader = B_PACK_CHARS('U', 'N', 'K', 'N');
953#endif
954
Andrei Homescua9cca9c2022-05-03 04:48:01 +0000955#endif // BINDER_WITH_KERNEL_IPC
Steven Morelandd70160f2019-07-23 10:20:38 -0700956
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700957// Write RPC headers. (previously just the interface token)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700958status_t Parcel::writeInterfaceToken(const String16& interface)
959{
Tomasz Wasilczyk0bfea2d2023-08-11 00:06:51 +0000960 return writeInterfaceToken(interface.c_str(), interface.size());
Steven Morelanddbc76c72020-10-01 18:02:48 +0000961}
962
963status_t Parcel::writeInterfaceToken(const char16_t* str, size_t len) {
Frederick Mayled3c595a2022-05-09 23:02:54 +0000964 if (auto* kernelFields = maybeKernelFields()) {
Andrei Homescua9cca9c2022-05-03 04:48:01 +0000965#ifdef BINDER_WITH_KERNEL_IPC
Steven Moreland3af936a2021-03-26 03:05:38 +0000966 const IPCThreadState* threadState = IPCThreadState::self();
967 writeInt32(threadState->getStrictModePolicy() | STRICT_MODE_PENALTY_GATHER);
968 updateWorkSourceRequestHeaderPosition();
969 writeInt32(threadState->shouldPropagateWorkSource() ? threadState->getCallingWorkSourceUid()
970 : IPCThreadState::kUnsetWorkSource);
971 writeInt32(kHeader);
Andrei Homescua9cca9c2022-05-03 04:48:01 +0000972#else // BINDER_WITH_KERNEL_IPC
973 LOG_ALWAYS_FATAL("Binder kernel driver disabled at build time");
974 return INVALID_OPERATION;
975#endif // BINDER_WITH_KERNEL_IPC
Steven Moreland3af936a2021-03-26 03:05:38 +0000976 }
Steven Morelanddbc76c72020-10-01 18:02:48 +0000977
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700978 // currently the interface identification token is just its name as a string
Steven Morelanddbc76c72020-10-01 18:02:48 +0000979 return writeString16(str, len);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700980}
981
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000982bool Parcel::replaceCallingWorkSourceUid(uid_t uid)
983{
Frederick Mayled3c595a2022-05-09 23:02:54 +0000984 auto* kernelFields = maybeKernelFields();
985 if (kernelFields == nullptr) {
986 return false;
987 }
988 if (!kernelFields->mRequestHeaderPresent) {
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000989 return false;
990 }
991
992 const size_t initialPosition = dataPosition();
Frederick Mayled3c595a2022-05-09 23:02:54 +0000993 setDataPosition(kernelFields->mWorkSourceRequestHeaderPosition);
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000994 status_t err = writeInt32(uid);
995 setDataPosition(initialPosition);
996 return err == NO_ERROR;
997}
998
Steven Morelandf1b1e492019-05-06 15:05:13 -0700999uid_t Parcel::readCallingWorkSourceUid() const
Olivier Gaillarddc848a02019-01-30 17:10:44 +00001000{
Frederick Mayled3c595a2022-05-09 23:02:54 +00001001 auto* kernelFields = maybeKernelFields();
1002 if (kernelFields == nullptr) {
1003 return false;
1004 }
1005 if (!kernelFields->mRequestHeaderPresent) {
Olivier Gaillarddc848a02019-01-30 17:10:44 +00001006 return IPCThreadState::kUnsetWorkSource;
1007 }
1008
1009 const size_t initialPosition = dataPosition();
Frederick Mayled3c595a2022-05-09 23:02:54 +00001010 setDataPosition(kernelFields->mWorkSourceRequestHeaderPosition);
Olivier Gaillarddc848a02019-01-30 17:10:44 +00001011 uid_t uid = readInt32();
1012 setDataPosition(initialPosition);
1013 return uid;
1014}
1015
Mathias Agopian83c04462009-05-22 19:00:22 -07001016bool Parcel::checkInterface(IBinder* binder) const
1017{
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -07001018 return enforceInterface(binder->getInterfaceDescriptor());
Mathias Agopian83c04462009-05-22 19:00:22 -07001019}
1020
Brad Fitzpatricka877cd82010-07-07 16:06:39 -07001021bool Parcel::enforceInterface(const String16& interface,
Brad Fitzpatrick70081a12010-07-27 09:49:11 -07001022 IPCThreadState* threadState) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001023{
Tomasz Wasilczyk0bfea2d2023-08-11 00:06:51 +00001024 return enforceInterface(interface.c_str(), interface.size(), threadState);
Daniel Colascione0bb330d2019-10-29 16:44:19 -07001025}
1026
1027bool Parcel::enforceInterface(const char16_t* interface,
1028 size_t len,
1029 IPCThreadState* threadState) const
1030{
Frederick Mayled3c595a2022-05-09 23:02:54 +00001031 if (auto* kernelFields = maybeKernelFields()) {
Andrei Homescua9cca9c2022-05-03 04:48:01 +00001032#ifdef BINDER_WITH_KERNEL_IPC
Steven Moreland3af936a2021-03-26 03:05:38 +00001033 // StrictModePolicy.
1034 int32_t strictPolicy = readInt32();
1035 if (threadState == nullptr) {
1036 threadState = IPCThreadState::self();
1037 }
1038 if ((threadState->getLastTransactionBinderFlags() & IBinder::FLAG_ONEWAY) != 0) {
1039 // For one-way calls, the callee is running entirely
1040 // disconnected from the caller, so disable StrictMode entirely.
1041 // Not only does disk/network usage not impact the caller, but
1042 // there's no way to communicate back violations anyway.
1043 threadState->setStrictModePolicy(0);
1044 } else {
1045 threadState->setStrictModePolicy(strictPolicy);
1046 }
1047 // WorkSource.
1048 updateWorkSourceRequestHeaderPosition();
1049 int32_t workSource = readInt32();
1050 threadState->setCallingWorkSourceUidWithoutPropagation(workSource);
1051 // vendor header
1052 int32_t header = readInt32();
Steven Moreland3a667492023-06-02 21:41:39 +00001053
1054 // fuzzers skip this check, because it is for protecting the underlying ABI, but
1055 // we don't want it to reduce our coverage
1056 if (header != kHeader && !mServiceFuzzing) {
Steven Moreland3af936a2021-03-26 03:05:38 +00001057 ALOGE("Expecting header 0x%x but found 0x%x. Mixing copies of libbinder?", kHeader,
1058 header);
1059 return false;
1060 }
Andrei Homescua9cca9c2022-05-03 04:48:01 +00001061#else // BINDER_WITH_KERNEL_IPC
1062 LOG_ALWAYS_FATAL("Binder kernel driver disabled at build time");
1063 (void)threadState;
1064 return false;
1065#endif // BINDER_WITH_KERNEL_IPC
Brad Fitzpatricka877cd82010-07-07 16:06:39 -07001066 }
Steven Moreland3af936a2021-03-26 03:05:38 +00001067
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +01001068 // Interface descriptor.
Daniel Colascione0bb330d2019-10-29 16:44:19 -07001069 size_t parcel_interface_len;
1070 const char16_t* parcel_interface = readString16Inplace(&parcel_interface_len);
1071 if (len == parcel_interface_len &&
1072 (!len || !memcmp(parcel_interface, interface, len * sizeof (char16_t)))) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001073 return true;
1074 } else {
Steven Morelande99d8822023-06-02 21:56:39 +00001075 if (mServiceFuzzing) {
1076 // ignore. Theoretically, this could cause a few false positives, because
1077 // people could assume things about getInterfaceDescriptor if they pass
1078 // this point, but it would be extremely fragile. It's more important that
1079 // we fuzz with the above things read from the Parcel.
1080 return true;
1081 } else {
Steven Moreland3a667492023-06-02 21:41:39 +00001082 ALOGW("**** enforceInterface() expected '%s' but read '%s'",
Tomasz Wasilczyk0bfea2d2023-08-11 00:06:51 +00001083 String8(interface, len).c_str(),
1084 String8(parcel_interface, parcel_interface_len).c_str());
Steven Morelande99d8822023-06-02 21:56:39 +00001085 return false;
Steven Moreland3a667492023-06-02 21:41:39 +00001086 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001087 }
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -07001088}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001089
Pawan Wagh104654a2022-11-15 21:18:42 +00001090void Parcel::setEnforceNoDataAvail(bool enforceNoDataAvail) {
1091 mEnforceNoDataAvail = enforceNoDataAvail;
1092}
1093
Steven Moreland3a667492023-06-02 21:41:39 +00001094void Parcel::setServiceFuzzing() {
1095 mServiceFuzzing = true;
1096}
1097
Steven Moreland418914a2023-06-28 21:47:14 +00001098bool Parcel::isServiceFuzzing() const {
1099 return mServiceFuzzing;
1100}
1101
Jooyung Hand23f9502021-12-23 14:39:57 +09001102binder::Status Parcel::enforceNoDataAvail() const {
Pawan Wagh104654a2022-11-15 21:18:42 +00001103 if (!mEnforceNoDataAvail) {
1104 return binder::Status::ok();
1105 }
1106
Jooyung Hand23f9502021-12-23 14:39:57 +09001107 const auto n = dataAvail();
1108 if (n == 0) {
1109 return binder::Status::ok();
1110 }
1111 return binder::Status::
1112 fromExceptionCode(binder::Status::Exception::EX_BAD_PARCELABLE,
1113 String8::format("Parcel data not fully consumed, unread size: %zu",
1114 n));
1115}
1116
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001117size_t Parcel::objectsCount() const
1118{
Frederick Mayled3c595a2022-05-09 23:02:54 +00001119 if (const auto* kernelFields = maybeKernelFields()) {
1120 return kernelFields->mObjectsSize;
1121 }
1122 return 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001123}
1124
1125status_t Parcel::errorCheck() const
1126{
1127 return mError;
1128}
1129
1130void Parcel::setError(status_t err)
1131{
1132 mError = err;
1133}
1134
1135status_t Parcel::finishWrite(size_t len)
1136{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001137 if (len > INT32_MAX) {
1138 // don't accept size_t values which may have come from an
1139 // inadvertent conversion from a negative int.
1140 return BAD_VALUE;
1141 }
1142
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001143 //printf("Finish write of %d\n", len);
1144 mDataPos += len;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001145 ALOGV("finishWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001146 if (mDataPos > mDataSize) {
1147 mDataSize = mDataPos;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001148 ALOGV("finishWrite Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001149 }
1150 //printf("New pos=%d, size=%d\n", mDataPos, mDataSize);
1151 return NO_ERROR;
1152}
1153
1154status_t Parcel::writeUnpadded(const void* data, size_t len)
1155{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001156 if (len > INT32_MAX) {
1157 // don't accept size_t values which may have come from an
1158 // inadvertent conversion from a negative int.
1159 return BAD_VALUE;
1160 }
1161
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001162 size_t end = mDataPos + len;
1163 if (end < mDataPos) {
1164 // integer overflow
1165 return BAD_VALUE;
1166 }
1167
1168 if (end <= mDataCapacity) {
1169restart_write:
1170 memcpy(mData+mDataPos, data, len);
1171 return finishWrite(len);
1172 }
1173
1174 status_t err = growData(len);
1175 if (err == NO_ERROR) goto restart_write;
1176 return err;
1177}
1178
1179status_t Parcel::write(const void* data, size_t len)
1180{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001181 if (len > INT32_MAX) {
1182 // don't accept size_t values which may have come from an
1183 // inadvertent conversion from a negative int.
1184 return BAD_VALUE;
1185 }
1186
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001187 void* const d = writeInplace(len);
1188 if (d) {
1189 memcpy(d, data, len);
1190 return NO_ERROR;
1191 }
1192 return mError;
1193}
1194
1195void* Parcel::writeInplace(size_t len)
1196{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001197 if (len > INT32_MAX) {
1198 // don't accept size_t values which may have come from an
1199 // inadvertent conversion from a negative int.
Yi Kong91635562018-06-07 14:38:36 -07001200 return nullptr;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001201 }
1202
1203 const size_t padded = pad_size(len);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001204
Khalid Ramadanb3d817b2021-11-18 07:02:50 +00001205 // check for integer overflow
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001206 if (mDataPos+padded < mDataPos) {
Yi Kong91635562018-06-07 14:38:36 -07001207 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001208 }
1209
1210 if ((mDataPos+padded) <= mDataCapacity) {
1211restart_write:
1212 //printf("Writing %ld bytes, padded to %ld\n", len, padded);
1213 uint8_t* const data = mData+mDataPos;
1214
1215 // Need to pad at end?
1216 if (padded != len) {
1217#if BYTE_ORDER == BIG_ENDIAN
1218 static const uint32_t mask[4] = {
1219 0x00000000, 0xffffff00, 0xffff0000, 0xff000000
1220 };
1221#endif
1222#if BYTE_ORDER == LITTLE_ENDIAN
1223 static const uint32_t mask[4] = {
1224 0x00000000, 0x00ffffff, 0x0000ffff, 0x000000ff
1225 };
1226#endif
1227 //printf("Applying pad mask: %p to %p\n", (void*)mask[padded-len],
1228 // *reinterpret_cast<void**>(data+padded-4));
1229 *reinterpret_cast<uint32_t*>(data+padded-4) &= mask[padded-len];
1230 }
1231
1232 finishWrite(padded);
1233 return data;
1234 }
1235
1236 status_t err = growData(padded);
1237 if (err == NO_ERROR) goto restart_write;
Yi Kong91635562018-06-07 14:38:36 -07001238 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001239}
1240
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001241status_t Parcel::writeUtf8AsUtf16(const std::string& str) {
1242 const uint8_t* strData = (uint8_t*)str.data();
1243 const size_t strLen= str.length();
1244 const ssize_t utf16Len = utf8_to_utf16_length(strData, strLen);
Sergio Girof4607432016-07-21 14:46:35 +01001245 if (utf16Len < 0 || utf16Len > std::numeric_limits<int32_t>::max()) {
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001246 return BAD_VALUE;
1247 }
1248
1249 status_t err = writeInt32(utf16Len);
1250 if (err) {
1251 return err;
1252 }
1253
1254 // Allocate enough bytes to hold our converted string and its terminating NULL.
1255 void* dst = writeInplace((utf16Len + 1) * sizeof(char16_t));
1256 if (!dst) {
1257 return NO_MEMORY;
1258 }
1259
Sergio Girof4607432016-07-21 14:46:35 +01001260 utf8_to_utf16(strData, strLen, (char16_t*)dst, (size_t) utf16Len + 1);
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001261
1262 return NO_ERROR;
1263}
1264
Jooyung Han9fcc4ef2020-01-23 12:45:10 +09001265
Andy Hung49198cf2020-11-18 11:02:39 -08001266status_t Parcel::writeUtf8AsUtf16(const std::optional<std::string>& str) { return writeData(str); }
1267status_t Parcel::writeUtf8AsUtf16(const std::unique_ptr<std::string>& str) { return writeData(str); }
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001268
Andy Hung49198cf2020-11-18 11:02:39 -08001269status_t Parcel::writeString16(const std::optional<String16>& str) { return writeData(str); }
1270status_t Parcel::writeString16(const std::unique_ptr<String16>& str) { return writeData(str); }
Casey Dahlin451ff582015-10-19 18:12:18 -07001271
Andy Hung49198cf2020-11-18 11:02:39 -08001272status_t Parcel::writeByteVector(const std::vector<int8_t>& val) { return writeData(val); }
1273status_t Parcel::writeByteVector(const std::optional<std::vector<int8_t>>& val) { return writeData(val); }
1274status_t Parcel::writeByteVector(const std::unique_ptr<std::vector<int8_t>>& val) { return writeData(val); }
1275status_t Parcel::writeByteVector(const std::vector<uint8_t>& val) { return writeData(val); }
1276status_t Parcel::writeByteVector(const std::optional<std::vector<uint8_t>>& val) { return writeData(val); }
1277status_t Parcel::writeByteVector(const std::unique_ptr<std::vector<uint8_t>>& val){ return writeData(val); }
1278status_t Parcel::writeInt32Vector(const std::vector<int32_t>& val) { return writeData(val); }
1279status_t Parcel::writeInt32Vector(const std::optional<std::vector<int32_t>>& val) { return writeData(val); }
1280status_t Parcel::writeInt32Vector(const std::unique_ptr<std::vector<int32_t>>& val) { return writeData(val); }
1281status_t Parcel::writeInt64Vector(const std::vector<int64_t>& val) { return writeData(val); }
1282status_t Parcel::writeInt64Vector(const std::optional<std::vector<int64_t>>& val) { return writeData(val); }
1283status_t Parcel::writeInt64Vector(const std::unique_ptr<std::vector<int64_t>>& val) { return writeData(val); }
1284status_t Parcel::writeUint64Vector(const std::vector<uint64_t>& val) { return writeData(val); }
1285status_t Parcel::writeUint64Vector(const std::optional<std::vector<uint64_t>>& val) { return writeData(val); }
1286status_t Parcel::writeUint64Vector(const std::unique_ptr<std::vector<uint64_t>>& val) { return writeData(val); }
1287status_t Parcel::writeFloatVector(const std::vector<float>& val) { return writeData(val); }
1288status_t Parcel::writeFloatVector(const std::optional<std::vector<float>>& val) { return writeData(val); }
1289status_t Parcel::writeFloatVector(const std::unique_ptr<std::vector<float>>& val) { return writeData(val); }
1290status_t Parcel::writeDoubleVector(const std::vector<double>& val) { return writeData(val); }
1291status_t Parcel::writeDoubleVector(const std::optional<std::vector<double>>& val) { return writeData(val); }
1292status_t Parcel::writeDoubleVector(const std::unique_ptr<std::vector<double>>& val) { return writeData(val); }
1293status_t Parcel::writeBoolVector(const std::vector<bool>& val) { return writeData(val); }
1294status_t Parcel::writeBoolVector(const std::optional<std::vector<bool>>& val) { return writeData(val); }
1295status_t Parcel::writeBoolVector(const std::unique_ptr<std::vector<bool>>& val) { return writeData(val); }
1296status_t Parcel::writeCharVector(const std::vector<char16_t>& val) { return writeData(val); }
1297status_t Parcel::writeCharVector(const std::optional<std::vector<char16_t>>& val) { return writeData(val); }
1298status_t Parcel::writeCharVector(const std::unique_ptr<std::vector<char16_t>>& val) { return writeData(val); }
Casey Dahlin451ff582015-10-19 18:12:18 -07001299
Andy Hung49198cf2020-11-18 11:02:39 -08001300status_t Parcel::writeString16Vector(const std::vector<String16>& val) { return writeData(val); }
Casey Dahlinb9872622015-11-25 15:09:45 -08001301status_t Parcel::writeString16Vector(
Andy Hung49198cf2020-11-18 11:02:39 -08001302 const std::optional<std::vector<std::optional<String16>>>& val) { return writeData(val); }
Jooyung Han9fcc4ef2020-01-23 12:45:10 +09001303status_t Parcel::writeString16Vector(
Andy Hung49198cf2020-11-18 11:02:39 -08001304 const std::unique_ptr<std::vector<std::unique_ptr<String16>>>& val) { return writeData(val); }
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001305status_t Parcel::writeUtf8VectorAsUtf16Vector(
Andy Hung49198cf2020-11-18 11:02:39 -08001306 const std::optional<std::vector<std::optional<std::string>>>& val) { return writeData(val); }
Jooyung Han9fcc4ef2020-01-23 12:45:10 +09001307status_t Parcel::writeUtf8VectorAsUtf16Vector(
Andy Hung49198cf2020-11-18 11:02:39 -08001308 const std::unique_ptr<std::vector<std::unique_ptr<std::string>>>& val) { return writeData(val); }
1309status_t Parcel::writeUtf8VectorAsUtf16Vector(const std::vector<std::string>& val) { return writeData(val); }
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001310
Tomasz Wasilczyk639490b2023-11-01 13:49:41 -07001311status_t Parcel::writeUniqueFileDescriptorVector(const std::vector<unique_fd>& val) {
1312 return writeData(val);
1313}
1314status_t Parcel::writeUniqueFileDescriptorVector(const std::optional<std::vector<unique_fd>>& val) {
1315 return writeData(val);
1316}
Andy Hung49198cf2020-11-18 11:02:39 -08001317
1318status_t Parcel::writeStrongBinderVector(const std::vector<sp<IBinder>>& val) { return writeData(val); }
1319status_t Parcel::writeStrongBinderVector(const std::optional<std::vector<sp<IBinder>>>& val) { return writeData(val); }
1320status_t Parcel::writeStrongBinderVector(const std::unique_ptr<std::vector<sp<IBinder>>>& val) { return writeData(val); }
1321
1322status_t Parcel::writeParcelable(const Parcelable& parcelable) { return writeData(parcelable); }
1323
1324status_t Parcel::readUtf8FromUtf16(std::optional<std::string>* str) const { return readData(str); }
1325status_t Parcel::readUtf8FromUtf16(std::unique_ptr<std::string>* str) const { return readData(str); }
1326
1327status_t Parcel::readString16(std::optional<String16>* pArg) const { return readData(pArg); }
1328status_t Parcel::readString16(std::unique_ptr<String16>* pArg) const { return readData(pArg); }
1329
1330status_t Parcel::readByteVector(std::vector<int8_t>* val) const { return readData(val); }
1331status_t Parcel::readByteVector(std::vector<uint8_t>* val) const { return readData(val); }
1332status_t Parcel::readByteVector(std::optional<std::vector<int8_t>>* val) const { return readData(val); }
1333status_t Parcel::readByteVector(std::unique_ptr<std::vector<int8_t>>* val) const { return readData(val); }
1334status_t Parcel::readByteVector(std::optional<std::vector<uint8_t>>* val) const { return readData(val); }
1335status_t Parcel::readByteVector(std::unique_ptr<std::vector<uint8_t>>* val) const { return readData(val); }
1336status_t Parcel::readInt32Vector(std::optional<std::vector<int32_t>>* val) const { return readData(val); }
1337status_t Parcel::readInt32Vector(std::unique_ptr<std::vector<int32_t>>* val) const { return readData(val); }
1338status_t Parcel::readInt32Vector(std::vector<int32_t>* val) const { return readData(val); }
1339status_t Parcel::readInt64Vector(std::optional<std::vector<int64_t>>* val) const { return readData(val); }
1340status_t Parcel::readInt64Vector(std::unique_ptr<std::vector<int64_t>>* val) const { return readData(val); }
1341status_t Parcel::readInt64Vector(std::vector<int64_t>* val) const { return readData(val); }
1342status_t Parcel::readUint64Vector(std::optional<std::vector<uint64_t>>* val) const { return readData(val); }
1343status_t Parcel::readUint64Vector(std::unique_ptr<std::vector<uint64_t>>* val) const { return readData(val); }
1344status_t Parcel::readUint64Vector(std::vector<uint64_t>* val) const { return readData(val); }
1345status_t Parcel::readFloatVector(std::optional<std::vector<float>>* val) const { return readData(val); }
1346status_t Parcel::readFloatVector(std::unique_ptr<std::vector<float>>* val) const { return readData(val); }
1347status_t Parcel::readFloatVector(std::vector<float>* val) const { return readData(val); }
1348status_t Parcel::readDoubleVector(std::optional<std::vector<double>>* val) const { return readData(val); }
1349status_t Parcel::readDoubleVector(std::unique_ptr<std::vector<double>>* val) const { return readData(val); }
1350status_t Parcel::readDoubleVector(std::vector<double>* val) const { return readData(val); }
1351status_t Parcel::readBoolVector(std::optional<std::vector<bool>>* val) const { return readData(val); }
1352status_t Parcel::readBoolVector(std::unique_ptr<std::vector<bool>>* val) const { return readData(val); }
1353status_t Parcel::readBoolVector(std::vector<bool>* val) const { return readData(val); }
1354status_t Parcel::readCharVector(std::optional<std::vector<char16_t>>* val) const { return readData(val); }
1355status_t Parcel::readCharVector(std::unique_ptr<std::vector<char16_t>>* val) const { return readData(val); }
1356status_t Parcel::readCharVector(std::vector<char16_t>* val) const { return readData(val); }
1357
1358status_t Parcel::readString16Vector(
1359 std::optional<std::vector<std::optional<String16>>>* val) const { return readData(val); }
1360status_t Parcel::readString16Vector(
1361 std::unique_ptr<std::vector<std::unique_ptr<String16>>>* val) const { return readData(val); }
1362status_t Parcel::readString16Vector(std::vector<String16>* val) const { return readData(val); }
1363status_t Parcel::readUtf8VectorFromUtf16Vector(
1364 std::optional<std::vector<std::optional<std::string>>>* val) const { return readData(val); }
1365status_t Parcel::readUtf8VectorFromUtf16Vector(
1366 std::unique_ptr<std::vector<std::unique_ptr<std::string>>>* val) const { return readData(val); }
1367status_t Parcel::readUtf8VectorFromUtf16Vector(std::vector<std::string>* val) const { return readData(val); }
1368
Tomasz Wasilczyk639490b2023-11-01 13:49:41 -07001369status_t Parcel::readUniqueFileDescriptorVector(std::optional<std::vector<unique_fd>>* val) const {
1370 return readData(val);
1371}
Tomasz Wasilczyk639490b2023-11-01 13:49:41 -07001372status_t Parcel::readUniqueFileDescriptorVector(std::vector<unique_fd>* val) const {
1373 return readData(val);
1374}
Andy Hung49198cf2020-11-18 11:02:39 -08001375
1376status_t Parcel::readStrongBinderVector(std::optional<std::vector<sp<IBinder>>>* val) const { return readData(val); }
1377status_t Parcel::readStrongBinderVector(std::unique_ptr<std::vector<sp<IBinder>>>* val) const { return readData(val); }
1378status_t Parcel::readStrongBinderVector(std::vector<sp<IBinder>>* val) const { return readData(val); }
1379
1380status_t Parcel::readParcelable(Parcelable* parcelable) const { return readData(parcelable); }
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001381
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001382status_t Parcel::writeInt32(int32_t val)
1383{
Andreas Huber84a6d042009-08-17 13:33:27 -07001384 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001385}
Dan Stoza41a0f2f2014-12-01 10:01:10 -08001386
1387status_t Parcel::writeUint32(uint32_t val)
1388{
1389 return writeAligned(val);
1390}
1391
Marco Nelissen5c0106e2013-10-16 10:57:51 -07001392status_t Parcel::writeInt32Array(size_t len, const int32_t *val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001393 if (len > INT32_MAX) {
1394 // don't accept size_t values which may have come from an
1395 // inadvertent conversion from a negative int.
1396 return BAD_VALUE;
1397 }
1398
Marco Nelissen5c0106e2013-10-16 10:57:51 -07001399 if (!val) {
Chad Brubakere59cb432015-06-30 14:03:55 -07001400 return writeInt32(-1);
Marco Nelissen5c0106e2013-10-16 10:57:51 -07001401 }
Chad Brubakere59cb432015-06-30 14:03:55 -07001402 status_t ret = writeInt32(static_cast<uint32_t>(len));
Marco Nelissen5c0106e2013-10-16 10:57:51 -07001403 if (ret == NO_ERROR) {
1404 ret = write(val, len * sizeof(*val));
1405 }
1406 return ret;
1407}
Marco Nelissenf0190bf2014-03-13 14:17:40 -07001408status_t Parcel::writeByteArray(size_t len, const uint8_t *val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001409 if (len > INT32_MAX) {
1410 // don't accept size_t values which may have come from an
1411 // inadvertent conversion from a negative int.
1412 return BAD_VALUE;
1413 }
1414
Marco Nelissenf0190bf2014-03-13 14:17:40 -07001415 if (!val) {
Chad Brubakere59cb432015-06-30 14:03:55 -07001416 return writeInt32(-1);
Marco Nelissenf0190bf2014-03-13 14:17:40 -07001417 }
Chad Brubakere59cb432015-06-30 14:03:55 -07001418 status_t ret = writeInt32(static_cast<uint32_t>(len));
Marco Nelissenf0190bf2014-03-13 14:17:40 -07001419 if (ret == NO_ERROR) {
1420 ret = write(val, len * sizeof(*val));
1421 }
1422 return ret;
1423}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001424
Casey Dahlind6848f52015-10-15 15:44:59 -07001425status_t Parcel::writeBool(bool val)
1426{
1427 return writeInt32(int32_t(val));
1428}
1429
1430status_t Parcel::writeChar(char16_t val)
1431{
1432 return writeInt32(int32_t(val));
1433}
1434
1435status_t Parcel::writeByte(int8_t val)
1436{
1437 return writeInt32(int32_t(val));
1438}
1439
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001440status_t Parcel::writeInt64(int64_t val)
1441{
Andreas Huber84a6d042009-08-17 13:33:27 -07001442 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001443}
1444
Ronghua Wu2d13afd2015-03-16 11:11:07 -07001445status_t Parcel::writeUint64(uint64_t val)
1446{
1447 return writeAligned(val);
1448}
1449
Serban Constantinescuf683e012013-11-05 16:53:55 +00001450status_t Parcel::writePointer(uintptr_t val)
1451{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001452 return writeAligned<binder_uintptr_t>(val);
Serban Constantinescuf683e012013-11-05 16:53:55 +00001453}
1454
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001455status_t Parcel::writeFloat(float val)
1456{
Andreas Huber84a6d042009-08-17 13:33:27 -07001457 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001458}
1459
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001460#if defined(__mips__) && defined(__mips_hard_float)
1461
1462status_t Parcel::writeDouble(double val)
1463{
1464 union {
1465 double d;
1466 unsigned long long ll;
1467 } u;
1468 u.d = val;
1469 return writeAligned(u.ll);
1470}
1471
1472#else
1473
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001474status_t Parcel::writeDouble(double val)
1475{
Andreas Huber84a6d042009-08-17 13:33:27 -07001476 return writeAligned(val);
1477}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001478
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001479#endif
1480
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001481status_t Parcel::writeCString(const char* str)
1482{
1483 return write(str, strlen(str)+1);
1484}
1485
1486status_t Parcel::writeString8(const String8& str)
1487{
Tomasz Wasilczyk0bfea2d2023-08-11 00:06:51 +00001488 return writeString8(str.c_str(), str.size());
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06001489}
1490
1491status_t Parcel::writeString8(const char* str, size_t len)
1492{
1493 if (str == nullptr) return writeInt32(-1);
1494
Jeff Sharkey18220902020-11-05 08:36:20 -07001495 // NOTE: Keep this logic in sync with android_os_Parcel.cpp
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06001496 status_t err = writeInt32(len);
1497 if (err == NO_ERROR) {
1498 uint8_t* data = (uint8_t*)writeInplace(len+sizeof(char));
1499 if (data) {
1500 memcpy(data, str, len);
1501 *reinterpret_cast<char*>(data+len) = 0;
1502 return NO_ERROR;
1503 }
1504 err = mError;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001505 }
1506 return err;
1507}
1508
1509status_t Parcel::writeString16(const String16& str)
1510{
Tomasz Wasilczyk0bfea2d2023-08-11 00:06:51 +00001511 return writeString16(str.c_str(), str.size());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001512}
1513
1514status_t Parcel::writeString16(const char16_t* str, size_t len)
1515{
Yi Kong91635562018-06-07 14:38:36 -07001516 if (str == nullptr) return writeInt32(-1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001517
Jeff Sharkey18220902020-11-05 08:36:20 -07001518 // NOTE: Keep this logic in sync with android_os_Parcel.cpp
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001519 status_t err = writeInt32(len);
1520 if (err == NO_ERROR) {
1521 len *= sizeof(char16_t);
1522 uint8_t* data = (uint8_t*)writeInplace(len+sizeof(char16_t));
1523 if (data) {
1524 memcpy(data, str, len);
1525 *reinterpret_cast<char16_t*>(data+len) = 0;
1526 return NO_ERROR;
1527 }
1528 err = mError;
1529 }
1530 return err;
1531}
1532
1533status_t Parcel::writeStrongBinder(const sp<IBinder>& val)
1534{
Steven Morelanda86a3562019-08-01 23:28:34 +00001535 return flattenBinder(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001536}
1537
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001538
Casey Dahlinb9872622015-11-25 15:09:45 -08001539status_t Parcel::writeRawNullableParcelable(const Parcelable* parcelable) {
1540 if (!parcelable) {
1541 return writeInt32(0);
1542 }
1543
1544 return writeParcelable(*parcelable);
1545}
1546
Tomasz Wasilczyk232d0b32023-10-09 17:37:16 +00001547#ifndef BINDER_DISABLE_NATIVE_HANDLE
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001548status_t Parcel::writeNativeHandle(const native_handle* handle)
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001549{
Mathias Agopian1d0a95b2009-07-31 16:12:13 -07001550 if (!handle || handle->version != sizeof(native_handle))
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001551 return BAD_TYPE;
1552
1553 status_t err;
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001554 err = writeInt32(handle->numFds);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001555 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001556
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001557 err = writeInt32(handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001558 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001559
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001560 for (int i=0 ; err==NO_ERROR && i<handle->numFds ; i++)
1561 err = writeDupFileDescriptor(handle->data[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001562
1563 if (err != NO_ERROR) {
Steve Block9d453682011-12-20 16:23:08 +00001564 ALOGD("write native handle, write dup fd failed");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001565 return err;
1566 }
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001567 err = write(handle->data + handle->numFds, sizeof(int)*handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001568 return err;
1569}
Tomasz Wasilczyk232d0b32023-10-09 17:37:16 +00001570#endif
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001571
Frederick Mayle69a0c992022-05-26 20:38:39 +00001572status_t Parcel::writeFileDescriptor(int fd, bool takeOwnership) {
1573 if (auto* rpcFields = maybeRpcFields()) {
Tomasz Wasilczyk639490b2023-11-01 13:49:41 -07001574 std::variant<unique_fd, borrowed_fd> fdVariant;
Frederick Mayle69a0c992022-05-26 20:38:39 +00001575 if (takeOwnership) {
Tomasz Wasilczyk639490b2023-11-01 13:49:41 -07001576 fdVariant = unique_fd(fd);
Frederick Mayle69a0c992022-05-26 20:38:39 +00001577 } else {
Tomasz Wasilczyk639490b2023-11-01 13:49:41 -07001578 fdVariant = borrowed_fd(fd);
Frederick Mayle69a0c992022-05-26 20:38:39 +00001579 }
1580 if (!mAllowFds) {
Devin Mooref4c9b482024-07-01 20:22:23 +00001581 ALOGE("FDs are not allowed in this parcel. Both the service and the client must set "
1582 "the FileDescriptorTransportMode and agree on the support.");
Frederick Mayle69a0c992022-05-26 20:38:39 +00001583 return FDS_NOT_ALLOWED;
1584 }
1585 switch (rpcFields->mSession->getFileDescriptorTransportMode()) {
1586 case RpcSession::FileDescriptorTransportMode::NONE: {
Devin Mooref4c9b482024-07-01 20:22:23 +00001587 ALOGE("FDs are not allowed in this RpcSession. Both the service and the client "
1588 "must set "
1589 "the FileDescriptorTransportMode and agree on the support.");
Frederick Mayle69a0c992022-05-26 20:38:39 +00001590 return FDS_NOT_ALLOWED;
1591 }
Andrei Homescu1c18a802022-08-17 04:59:01 +00001592 case RpcSession::FileDescriptorTransportMode::UNIX:
1593 case RpcSession::FileDescriptorTransportMode::TRUSTY: {
Frederick Mayle69a0c992022-05-26 20:38:39 +00001594 if (rpcFields->mFds == nullptr) {
1595 rpcFields->mFds = std::make_unique<decltype(rpcFields->mFds)::element_type>();
1596 }
1597 size_t dataPos = mDataPos;
1598 if (dataPos > UINT32_MAX) {
1599 return NO_MEMORY;
1600 }
1601 if (status_t err = writeInt32(RpcFields::TYPE_NATIVE_FILE_DESCRIPTOR); err != OK) {
1602 return err;
1603 }
1604 if (status_t err = writeInt32(rpcFields->mFds->size()); err != OK) {
1605 return err;
1606 }
1607 rpcFields->mObjectPositions.push_back(dataPos);
1608 rpcFields->mFds->push_back(std::move(fdVariant));
1609 return OK;
1610 }
1611 }
Steven Moreland5553ac42020-11-11 02:14:45 +00001612 }
1613
Andrei Homescua9cca9c2022-05-03 04:48:01 +00001614#ifdef BINDER_WITH_KERNEL_IPC
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001615 flat_binder_object obj;
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07001616 obj.hdr.type = BINDER_TYPE_FD;
T.J. Mercierca0c2cb2022-12-19 21:56:35 +00001617 obj.flags = 0;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -08001618 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001619 obj.handle = fd;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001620 obj.cookie = takeOwnership ? 1 : 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001621 return writeObject(obj, true);
Andrei Homescua9cca9c2022-05-03 04:48:01 +00001622#else // BINDER_WITH_KERNEL_IPC
1623 LOG_ALWAYS_FATAL("Binder kernel driver disabled at build time");
1624 (void)fd;
1625 (void)takeOwnership;
1626 return INVALID_OPERATION;
1627#endif // BINDER_WITH_KERNEL_IPC
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001628}
1629
1630status_t Parcel::writeDupFileDescriptor(int fd)
1631{
Andrei Homescu24ad36e2022-08-04 01:33:33 +00001632 int dupFd;
Tomasz Wasilczyk0d9dec22023-10-06 20:28:49 +00001633 if (status_t err = binder::os::dupFileDescriptor(fd, &dupFd); err != OK) {
Andrei Homescu24ad36e2022-08-04 01:33:33 +00001634 return err;
Jeff Brownd341c712011-11-04 20:19:33 -07001635 }
1636 status_t err = writeFileDescriptor(dupFd, true /*takeOwnership*/);
Casey Dahlin06673e32015-11-23 13:24:23 -08001637 if (err != OK) {
Jeff Brownd341c712011-11-04 20:19:33 -07001638 close(dupFd);
1639 }
1640 return err;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001641}
1642
Dianne Hackborn1941a402016-08-29 12:30:43 -07001643status_t Parcel::writeParcelFileDescriptor(int fd, bool takeOwnership)
1644{
1645 writeInt32(0);
1646 return writeFileDescriptor(fd, takeOwnership);
1647}
1648
Ryo Hashimotobf551892018-05-31 16:58:35 +09001649status_t Parcel::writeDupParcelFileDescriptor(int fd)
1650{
Andrei Homescu24ad36e2022-08-04 01:33:33 +00001651 int dupFd;
Tomasz Wasilczyk0d9dec22023-10-06 20:28:49 +00001652 if (status_t err = binder::os::dupFileDescriptor(fd, &dupFd); err != OK) {
Andrei Homescu24ad36e2022-08-04 01:33:33 +00001653 return err;
Ryo Hashimotobf551892018-05-31 16:58:35 +09001654 }
1655 status_t err = writeParcelFileDescriptor(dupFd, true /*takeOwnership*/);
1656 if (err != OK) {
1657 close(dupFd);
1658 }
1659 return err;
1660}
1661
Tomasz Wasilczyk639490b2023-11-01 13:49:41 -07001662status_t Parcel::writeUniqueFileDescriptor(const unique_fd& fd) {
Casey Dahlin06673e32015-11-23 13:24:23 -08001663 return writeDupFileDescriptor(fd.get());
1664}
1665
Jeff Brown13b16042014-11-11 16:44:25 -08001666status_t Parcel::writeBlob(size_t len, bool mutableCopy, WritableBlob* outBlob)
Jeff Brown5707dbf2011-09-23 21:17:56 -07001667{
Tomasz Wasilczykbca8f942023-10-09 17:42:37 +00001668#ifdef BINDER_DISABLE_BLOB
1669 (void)len;
1670 (void)mutableCopy;
1671 (void)outBlob;
1672 return INVALID_OPERATION;
1673#else
Nick Kralevichb6b14232015-04-02 09:36:02 -07001674 if (len > INT32_MAX) {
1675 // don't accept size_t values which may have come from an
1676 // inadvertent conversion from a negative int.
1677 return BAD_VALUE;
1678 }
1679
Jeff Brown13b16042014-11-11 16:44:25 -08001680 status_t status;
1681 if (!mAllowFds || len <= BLOB_INPLACE_LIMIT) {
Steve Block6807e592011-10-20 11:56:00 +01001682 ALOGV("writeBlob: write in place");
Jeff Brown13b16042014-11-11 16:44:25 -08001683 status = writeInt32(BLOB_INPLACE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001684 if (status) return status;
1685
1686 void* ptr = writeInplace(len);
1687 if (!ptr) return NO_MEMORY;
1688
Jeff Brown13b16042014-11-11 16:44:25 -08001689 outBlob->init(-1, ptr, len, false);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001690 return NO_ERROR;
1691 }
1692
Steve Block6807e592011-10-20 11:56:00 +01001693 ALOGV("writeBlob: write to ashmem");
Jeff Brown5707dbf2011-09-23 21:17:56 -07001694 int fd = ashmem_create_region("Parcel Blob", len);
1695 if (fd < 0) return NO_MEMORY;
1696
1697 int result = ashmem_set_prot_region(fd, PROT_READ | PROT_WRITE);
1698 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -07001699 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001700 } else {
Yi Kong91635562018-06-07 14:38:36 -07001701 void* ptr = ::mmap(nullptr, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001702 if (ptr == MAP_FAILED) {
1703 status = -errno;
1704 } else {
Jeff Brown13b16042014-11-11 16:44:25 -08001705 if (!mutableCopy) {
1706 result = ashmem_set_prot_region(fd, PROT_READ);
1707 }
Jeff Brown5707dbf2011-09-23 21:17:56 -07001708 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -07001709 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001710 } else {
Jeff Brown13b16042014-11-11 16:44:25 -08001711 status = writeInt32(mutableCopy ? BLOB_ASHMEM_MUTABLE : BLOB_ASHMEM_IMMUTABLE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001712 if (!status) {
Jeff Brown93ff1f92011-11-04 19:01:44 -07001713 status = writeFileDescriptor(fd, true /*takeOwnership*/);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001714 if (!status) {
Jeff Brown13b16042014-11-11 16:44:25 -08001715 outBlob->init(fd, ptr, len, mutableCopy);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001716 return NO_ERROR;
1717 }
1718 }
1719 }
1720 }
Mike McTernan733610f2024-08-27 11:48:30 +01001721 if (::munmap(ptr, len) == -1) {
1722 ALOGW("munmap() failed: %s", strerror(errno));
1723 }
Jeff Brown5707dbf2011-09-23 21:17:56 -07001724 }
1725 ::close(fd);
1726 return status;
Tomasz Wasilczykbca8f942023-10-09 17:42:37 +00001727#endif
Jeff Brown5707dbf2011-09-23 21:17:56 -07001728}
1729
Jeff Brown13b16042014-11-11 16:44:25 -08001730status_t Parcel::writeDupImmutableBlobFileDescriptor(int fd)
1731{
1732 // Must match up with what's done in writeBlob.
1733 if (!mAllowFds) return FDS_NOT_ALLOWED;
1734 status_t status = writeInt32(BLOB_ASHMEM_IMMUTABLE);
1735 if (status) return status;
1736 return writeDupFileDescriptor(fd);
1737}
1738
Mathias Agopiane1424282013-07-29 21:24:40 -07001739status_t Parcel::write(const FlattenableHelperInterface& val)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001740{
1741 status_t err;
1742
1743 // size if needed
Mathias Agopiane1424282013-07-29 21:24:40 -07001744 const size_t len = val.getFlattenedSize();
1745 const size_t fd_count = val.getFdCount();
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001746
Andrei Homescu1519b982022-06-09 02:04:44 +00001747 if ((len > INT32_MAX) || (fd_count > kMaxFds)) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001748 // don't accept size_t values which may have come from an
1749 // inadvertent conversion from a negative int.
1750 return BAD_VALUE;
1751 }
1752
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001753 err = this->writeInt32(len);
1754 if (err) return err;
1755
1756 err = this->writeInt32(fd_count);
1757 if (err) return err;
1758
1759 // payload
Martijn Coenenf8542382018-04-04 11:46:56 +02001760 void* const buf = this->writeInplace(len);
Yi Kong91635562018-06-07 14:38:36 -07001761 if (buf == nullptr)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001762 return BAD_VALUE;
1763
Yi Kong91635562018-06-07 14:38:36 -07001764 int* fds = nullptr;
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001765 if (fd_count) {
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001766 fds = new (std::nothrow) int[fd_count];
1767 if (fds == nullptr) {
1768 ALOGE("write: failed to allocate requested %zu fds", fd_count);
1769 return BAD_VALUE;
1770 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001771 }
1772
1773 err = val.flatten(buf, len, fds, fd_count);
1774 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
1775 err = this->writeDupFileDescriptor( fds[i] );
1776 }
1777
1778 if (fd_count) {
1779 delete [] fds;
1780 }
1781
1782 return err;
1783}
1784
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001785status_t Parcel::writeObject(const flat_binder_object& val, bool nullMetaData)
1786{
Frederick Mayled3c595a2022-05-09 23:02:54 +00001787 auto* kernelFields = maybeKernelFields();
1788 LOG_ALWAYS_FATAL_IF(kernelFields == nullptr, "Can't write flat_binder_object to RPC Parcel");
1789
Andrei Homescua9cca9c2022-05-03 04:48:01 +00001790#ifdef BINDER_WITH_KERNEL_IPC
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001791 const bool enoughData = (mDataPos+sizeof(val)) <= mDataCapacity;
Frederick Mayled3c595a2022-05-09 23:02:54 +00001792 const bool enoughObjects = kernelFields->mObjectsSize < kernelFields->mObjectsCapacity;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001793 if (enoughData && enoughObjects) {
1794restart_write:
1795 *reinterpret_cast<flat_binder_object*>(mData+mDataPos) = val;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001796
Christopher Tate98e67d32015-06-03 18:44:15 -07001797 // remember if it's a file descriptor
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07001798 if (val.hdr.type == BINDER_TYPE_FD) {
Christopher Tate98e67d32015-06-03 18:44:15 -07001799 if (!mAllowFds) {
1800 // fail before modifying our object index
1801 return FDS_NOT_ALLOWED;
1802 }
Frederick Mayled3c595a2022-05-09 23:02:54 +00001803 kernelFields->mHasFds = kernelFields->mFdsKnown = true;
Christopher Tate98e67d32015-06-03 18:44:15 -07001804 }
1805
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001806 // Need to write meta-data?
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001807 if (nullMetaData || val.binder != 0) {
Frederick Mayled3c595a2022-05-09 23:02:54 +00001808 kernelFields->mObjects[kernelFields->mObjectsSize] = mDataPos;
Steven Morelandc673f1f2021-10-07 18:23:35 -07001809 acquire_object(ProcessState::self(), val, this);
Frederick Mayled3c595a2022-05-09 23:02:54 +00001810 kernelFields->mObjectsSize++;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001811 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001812
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001813 return finishWrite(sizeof(flat_binder_object));
1814 }
1815
1816 if (!enoughData) {
1817 const status_t err = growData(sizeof(val));
1818 if (err != NO_ERROR) return err;
1819 }
1820 if (!enoughObjects) {
Frederick Mayled3c595a2022-05-09 23:02:54 +00001821 if (kernelFields->mObjectsSize > SIZE_MAX - 2) return NO_MEMORY; // overflow
1822 if ((kernelFields->mObjectsSize + 2) > SIZE_MAX / 3) return NO_MEMORY; // overflow
1823 size_t newSize = ((kernelFields->mObjectsSize + 2) * 3) / 2;
Martijn Coenen93fe5182020-01-22 10:46:25 +01001824 if (newSize > SIZE_MAX / sizeof(binder_size_t)) return NO_MEMORY; // overflow
Frederick Mayled3c595a2022-05-09 23:02:54 +00001825 binder_size_t* objects =
1826 (binder_size_t*)realloc(kernelFields->mObjects, newSize * sizeof(binder_size_t));
Yi Kong91635562018-06-07 14:38:36 -07001827 if (objects == nullptr) return NO_MEMORY;
Frederick Mayled3c595a2022-05-09 23:02:54 +00001828 kernelFields->mObjects = objects;
1829 kernelFields->mObjectsCapacity = newSize;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001830 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001831
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001832 goto restart_write;
Andrei Homescua9cca9c2022-05-03 04:48:01 +00001833#else // BINDER_WITH_KERNEL_IPC
1834 LOG_ALWAYS_FATAL("Binder kernel driver disabled at build time");
1835 (void)val;
1836 (void)nullMetaData;
1837 return INVALID_OPERATION;
1838#endif // BINDER_WITH_KERNEL_IPC
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001839}
1840
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001841status_t Parcel::writeNoException()
1842{
Christopher Wiley09eb7492015-11-09 15:06:15 -08001843 binder::Status status;
1844 return status.writeToParcel(this);
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001845}
1846
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001847status_t Parcel::validateReadData(size_t upperBound) const
1848{
Frederick Mayled3c595a2022-05-09 23:02:54 +00001849 const auto* kernelFields = maybeKernelFields();
1850 if (kernelFields == nullptr) {
1851 // Can't validate RPC Parcel reads because the location of binder
1852 // objects is unknown.
1853 return OK;
1854 }
1855
Andrei Homescua9cca9c2022-05-03 04:48:01 +00001856#ifdef BINDER_WITH_KERNEL_IPC
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001857 // Don't allow non-object reads on object data
Frederick Mayled3c595a2022-05-09 23:02:54 +00001858 if (kernelFields->mObjectsSorted || kernelFields->mObjectsSize <= 1) {
1859 data_sorted:
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001860 // Expect to check only against the next object
Frederick Mayled3c595a2022-05-09 23:02:54 +00001861 if (kernelFields->mNextObjectHint < kernelFields->mObjectsSize &&
1862 upperBound > kernelFields->mObjects[kernelFields->mNextObjectHint]) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001863 // For some reason the current read position is greater than the next object
1864 // hint. Iterate until we find the right object
Frederick Mayled3c595a2022-05-09 23:02:54 +00001865 size_t nextObject = kernelFields->mNextObjectHint;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001866 do {
Frederick Mayled3c595a2022-05-09 23:02:54 +00001867 if (mDataPos < kernelFields->mObjects[nextObject] + sizeof(flat_binder_object)) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001868 // Requested info overlaps with an object
Steven Moreland3a667492023-06-02 21:41:39 +00001869 if (!mServiceFuzzing) {
1870 ALOGE("Attempt to read from protected data in Parcel %p", this);
1871 }
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001872 return PERMISSION_DENIED;
1873 }
1874 nextObject++;
Frederick Mayled3c595a2022-05-09 23:02:54 +00001875 } while (nextObject < kernelFields->mObjectsSize &&
1876 upperBound > kernelFields->mObjects[nextObject]);
1877 kernelFields->mNextObjectHint = nextObject;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001878 }
1879 return NO_ERROR;
1880 }
1881 // Quickly determine if mObjects is sorted.
Frederick Mayled3c595a2022-05-09 23:02:54 +00001882 binder_size_t* currObj = kernelFields->mObjects + kernelFields->mObjectsSize - 1;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001883 binder_size_t* prevObj = currObj;
Frederick Mayled3c595a2022-05-09 23:02:54 +00001884 while (currObj > kernelFields->mObjects) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001885 prevObj--;
1886 if(*prevObj > *currObj) {
1887 goto data_unsorted;
1888 }
1889 currObj--;
1890 }
Frederick Mayled3c595a2022-05-09 23:02:54 +00001891 kernelFields->mObjectsSorted = true;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001892 goto data_sorted;
1893
1894data_unsorted:
1895 // Insertion Sort mObjects
1896 // Great for mostly sorted lists. If randomly sorted or reverse ordered mObjects become common,
1897 // switch to std::sort(mObjects, mObjects + mObjectsSize);
Frederick Mayled3c595a2022-05-09 23:02:54 +00001898 for (binder_size_t* iter0 = kernelFields->mObjects + 1;
1899 iter0 < kernelFields->mObjects + kernelFields->mObjectsSize; iter0++) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001900 binder_size_t temp = *iter0;
1901 binder_size_t* iter1 = iter0 - 1;
Frederick Mayled3c595a2022-05-09 23:02:54 +00001902 while (iter1 >= kernelFields->mObjects && *iter1 > temp) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001903 *(iter1 + 1) = *iter1;
1904 iter1--;
1905 }
1906 *(iter1 + 1) = temp;
1907 }
Frederick Mayled3c595a2022-05-09 23:02:54 +00001908 kernelFields->mNextObjectHint = 0;
1909 kernelFields->mObjectsSorted = true;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001910 goto data_sorted;
Andrei Homescua9cca9c2022-05-03 04:48:01 +00001911#else // BINDER_WITH_KERNEL_IPC
1912 (void)upperBound;
1913 return NO_ERROR;
1914#endif // BINDER_WITH_KERNEL_IPC
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001915}
1916
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001917status_t Parcel::read(void* outData, size_t len) const
1918{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001919 if (len > INT32_MAX) {
1920 // don't accept size_t values which may have come from an
1921 // inadvertent conversion from a negative int.
1922 return BAD_VALUE;
1923 }
1924
1925 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize
1926 && len <= pad_size(len)) {
Frederick Mayled3c595a2022-05-09 23:02:54 +00001927 const auto* kernelFields = maybeKernelFields();
1928 if (kernelFields != nullptr && kernelFields->mObjectsSize > 0) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001929 status_t err = validateReadData(mDataPos + pad_size(len));
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001930 if(err != NO_ERROR) {
1931 // Still increment the data position by the expected length
1932 mDataPos += pad_size(len);
1933 ALOGV("read Setting data pos of %p to %zu", this, mDataPos);
1934 return err;
1935 }
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001936 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001937 memcpy(outData, mData+mDataPos, len);
Nick Kralevichb6b14232015-04-02 09:36:02 -07001938 mDataPos += pad_size(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001939 ALOGV("read Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001940 return NO_ERROR;
1941 }
1942 return NOT_ENOUGH_DATA;
1943}
1944
1945const void* Parcel::readInplace(size_t len) const
1946{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001947 if (len > INT32_MAX) {
1948 // don't accept size_t values which may have come from an
1949 // inadvertent conversion from a negative int.
Yi Kong91635562018-06-07 14:38:36 -07001950 return nullptr;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001951 }
1952
1953 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize
1954 && len <= pad_size(len)) {
Frederick Mayled3c595a2022-05-09 23:02:54 +00001955 const auto* kernelFields = maybeKernelFields();
1956 if (kernelFields != nullptr && kernelFields->mObjectsSize > 0) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001957 status_t err = validateReadData(mDataPos + pad_size(len));
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001958 if(err != NO_ERROR) {
1959 // Still increment the data position by the expected length
1960 mDataPos += pad_size(len);
1961 ALOGV("readInplace Setting data pos of %p to %zu", this, mDataPos);
Yi Kong91635562018-06-07 14:38:36 -07001962 return nullptr;
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001963 }
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001964 }
1965
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001966 const void* data = mData+mDataPos;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001967 mDataPos += pad_size(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001968 ALOGV("readInplace Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001969 return data;
1970 }
Yi Kong91635562018-06-07 14:38:36 -07001971 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001972}
1973
Steven Morelandd4f73fb2021-05-14 19:50:52 +00001974status_t Parcel::readOutVectorSizeWithCheck(size_t elmSize, int32_t* size) const {
1975 if (status_t status = readInt32(size); status != OK) return status;
1976 if (*size < 0) return OK; // may be null, client to handle
1977
1978 LOG_ALWAYS_FATAL_IF(elmSize > INT32_MAX, "Cannot have element as big as %zu", elmSize);
1979
1980 // approximation, can't know max element size (e.g. if it makes heap
1981 // allocations)
1982 static_assert(sizeof(int) == sizeof(int32_t), "Android is LP64");
1983 int32_t allocationSize;
1984 if (__builtin_smul_overflow(elmSize, *size, &allocationSize)) return NO_MEMORY;
1985
1986 // High limit of 1MB since something this big could never be returned. Could
1987 // probably scope this down, but might impact very specific usecases.
1988 constexpr int32_t kMaxAllocationSize = 1 * 1000 * 1000;
1989
1990 if (allocationSize >= kMaxAllocationSize) {
1991 return NO_MEMORY;
1992 }
1993
1994 return OK;
1995}
1996
Andreas Huber84a6d042009-08-17 13:33:27 -07001997template<class T>
1998status_t Parcel::readAligned(T *pArg) const {
Elliott Hughes42a9b942020-08-17 15:53:31 -07001999 static_assert(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));
Andrei Homescue33238e2022-03-29 22:50:00 +00002000 static_assert(std::is_trivially_copyable_v<T>);
Andreas Huber84a6d042009-08-17 13:33:27 -07002001
2002 if ((mDataPos+sizeof(T)) <= mDataSize) {
Frederick Mayled3c595a2022-05-09 23:02:54 +00002003 const auto* kernelFields = maybeKernelFields();
2004 if (kernelFields != nullptr && kernelFields->mObjectsSize > 0) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002005 status_t err = validateReadData(mDataPos + sizeof(T));
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07002006 if(err != NO_ERROR) {
2007 // Still increment the data position by the expected length
2008 mDataPos += sizeof(T);
2009 return err;
2010 }
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002011 }
2012
Andrei Homescue33238e2022-03-29 22:50:00 +00002013 memcpy(pArg, mData + mDataPos, sizeof(T));
Andreas Huber84a6d042009-08-17 13:33:27 -07002014 mDataPos += sizeof(T);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002015 return NO_ERROR;
2016 } else {
2017 return NOT_ENOUGH_DATA;
2018 }
2019}
2020
Andreas Huber84a6d042009-08-17 13:33:27 -07002021template<class T>
2022T Parcel::readAligned() const {
2023 T result;
2024 if (readAligned(&result) != NO_ERROR) {
2025 result = 0;
2026 }
2027
2028 return result;
2029}
2030
2031template<class T>
2032status_t Parcel::writeAligned(T val) {
Elliott Hughes42a9b942020-08-17 15:53:31 -07002033 static_assert(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));
Andrei Homescue33238e2022-03-29 22:50:00 +00002034 static_assert(std::is_trivially_copyable_v<T>);
Andreas Huber84a6d042009-08-17 13:33:27 -07002035
2036 if ((mDataPos+sizeof(val)) <= mDataCapacity) {
2037restart_write:
Andrei Homescue33238e2022-03-29 22:50:00 +00002038 memcpy(mData + mDataPos, &val, sizeof(val));
Andreas Huber84a6d042009-08-17 13:33:27 -07002039 return finishWrite(sizeof(val));
2040 }
2041
2042 status_t err = growData(sizeof(val));
2043 if (err == NO_ERROR) goto restart_write;
2044 return err;
2045}
2046
2047status_t Parcel::readInt32(int32_t *pArg) const
2048{
2049 return readAligned(pArg);
2050}
2051
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002052int32_t Parcel::readInt32() const
2053{
Andreas Huber84a6d042009-08-17 13:33:27 -07002054 return readAligned<int32_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002055}
2056
Dan Stoza41a0f2f2014-12-01 10:01:10 -08002057status_t Parcel::readUint32(uint32_t *pArg) const
2058{
2059 return readAligned(pArg);
2060}
2061
2062uint32_t Parcel::readUint32() const
2063{
2064 return readAligned<uint32_t>();
2065}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002066
2067status_t Parcel::readInt64(int64_t *pArg) const
2068{
Andreas Huber84a6d042009-08-17 13:33:27 -07002069 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002070}
2071
2072
2073int64_t Parcel::readInt64() const
2074{
Andreas Huber84a6d042009-08-17 13:33:27 -07002075 return readAligned<int64_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002076}
2077
Ronghua Wu2d13afd2015-03-16 11:11:07 -07002078status_t Parcel::readUint64(uint64_t *pArg) const
2079{
2080 return readAligned(pArg);
2081}
2082
2083uint64_t Parcel::readUint64() const
2084{
2085 return readAligned<uint64_t>();
2086}
2087
Serban Constantinescuf683e012013-11-05 16:53:55 +00002088status_t Parcel::readPointer(uintptr_t *pArg) const
2089{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002090 status_t ret;
2091 binder_uintptr_t ptr;
2092 ret = readAligned(&ptr);
2093 if (!ret)
2094 *pArg = ptr;
2095 return ret;
Serban Constantinescuf683e012013-11-05 16:53:55 +00002096}
2097
2098uintptr_t Parcel::readPointer() const
2099{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002100 return readAligned<binder_uintptr_t>();
Serban Constantinescuf683e012013-11-05 16:53:55 +00002101}
2102
2103
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002104status_t Parcel::readFloat(float *pArg) const
2105{
Andreas Huber84a6d042009-08-17 13:33:27 -07002106 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002107}
2108
2109
2110float Parcel::readFloat() const
2111{
Andreas Huber84a6d042009-08-17 13:33:27 -07002112 return readAligned<float>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002113}
2114
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08002115#if defined(__mips__) && defined(__mips_hard_float)
2116
2117status_t Parcel::readDouble(double *pArg) const
2118{
2119 union {
2120 double d;
2121 unsigned long long ll;
2122 } u;
Narayan Kamath2c68d382014-06-04 15:04:29 +01002123 u.d = 0;
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08002124 status_t status;
2125 status = readAligned(&u.ll);
2126 *pArg = u.d;
2127 return status;
2128}
2129
2130double Parcel::readDouble() const
2131{
2132 union {
2133 double d;
2134 unsigned long long ll;
2135 } u;
2136 u.ll = readAligned<unsigned long long>();
2137 return u.d;
2138}
2139
2140#else
2141
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002142status_t Parcel::readDouble(double *pArg) const
2143{
Andreas Huber84a6d042009-08-17 13:33:27 -07002144 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002145}
2146
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002147double Parcel::readDouble() const
2148{
Andreas Huber84a6d042009-08-17 13:33:27 -07002149 return readAligned<double>();
2150}
2151
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08002152#endif
2153
Casey Dahlind6848f52015-10-15 15:44:59 -07002154status_t Parcel::readBool(bool *pArg) const
2155{
Manoj Gupta6eb62052017-07-12 10:29:15 -07002156 int32_t tmp = 0;
Casey Dahlind6848f52015-10-15 15:44:59 -07002157 status_t ret = readInt32(&tmp);
2158 *pArg = (tmp != 0);
2159 return ret;
2160}
2161
2162bool Parcel::readBool() const
2163{
2164 return readInt32() != 0;
2165}
2166
2167status_t Parcel::readChar(char16_t *pArg) const
2168{
Manoj Gupta6eb62052017-07-12 10:29:15 -07002169 int32_t tmp = 0;
Casey Dahlind6848f52015-10-15 15:44:59 -07002170 status_t ret = readInt32(&tmp);
2171 *pArg = char16_t(tmp);
2172 return ret;
2173}
2174
2175char16_t Parcel::readChar() const
2176{
2177 return char16_t(readInt32());
2178}
2179
2180status_t Parcel::readByte(int8_t *pArg) const
2181{
Manoj Gupta6eb62052017-07-12 10:29:15 -07002182 int32_t tmp = 0;
Casey Dahlind6848f52015-10-15 15:44:59 -07002183 status_t ret = readInt32(&tmp);
2184 *pArg = int8_t(tmp);
2185 return ret;
2186}
2187
2188int8_t Parcel::readByte() const
2189{
2190 return int8_t(readInt32());
2191}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002192
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08002193status_t Parcel::readUtf8FromUtf16(std::string* str) const {
2194 size_t utf16Size = 0;
2195 const char16_t* src = readString16Inplace(&utf16Size);
2196 if (!src) {
2197 return UNEXPECTED_NULL;
2198 }
2199
2200 // Save ourselves the trouble, we're done.
2201 if (utf16Size == 0u) {
2202 str->clear();
2203 return NO_ERROR;
2204 }
2205
Sergio Giro9b39ebe2016-06-28 18:19:33 +01002206 // Allow for closing '\0'
2207 ssize_t utf8Size = utf16_to_utf8_length(src, utf16Size) + 1;
2208 if (utf8Size < 1) {
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08002209 return BAD_VALUE;
2210 }
2211 // Note that while it is probably safe to assume string::resize keeps a
Sergio Giro9b39ebe2016-06-28 18:19:33 +01002212 // spare byte around for the trailing null, we still pass the size including the trailing null
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08002213 str->resize(utf8Size);
Sergio Giro9b39ebe2016-06-28 18:19:33 +01002214 utf16_to_utf8(src, utf16Size, &((*str)[0]), utf8Size);
2215 str->resize(utf8Size - 1);
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08002216 return NO_ERROR;
2217}
2218
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002219const char* Parcel::readCString() const
2220{
Steven Morelandd0d4b582019-05-17 13:14:06 -07002221 if (mDataPos < mDataSize) {
2222 const size_t avail = mDataSize-mDataPos;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002223 const char* str = reinterpret_cast<const char*>(mData+mDataPos);
2224 // is the string's trailing NUL within the parcel's valid bounds?
2225 const char* eos = reinterpret_cast<const char*>(memchr(str, 0, avail));
2226 if (eos) {
2227 const size_t len = eos - str;
Nick Kralevichb6b14232015-04-02 09:36:02 -07002228 mDataPos += pad_size(len+1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002229 ALOGV("readCString Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002230 return str;
2231 }
2232 }
Yi Kong91635562018-06-07 14:38:36 -07002233 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002234}
2235
2236String8 Parcel::readString8() const
2237{
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06002238 size_t len;
2239 const char* str = readString8Inplace(&len);
2240 if (str) return String8(str, len);
Steven Moreland3a667492023-06-02 21:41:39 +00002241
2242 if (!mServiceFuzzing) {
2243 ALOGE("Reading a NULL string not supported here.");
2244 }
2245
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06002246 return String8();
Roshan Pius87b64d22016-07-18 12:51:02 -07002247}
2248
2249status_t Parcel::readString8(String8* pArg) const
2250{
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06002251 size_t len;
2252 const char* str = readString8Inplace(&len);
2253 if (str) {
2254 pArg->setTo(str, len);
2255 return 0;
2256 } else {
Roshan Pius87b64d22016-07-18 12:51:02 -07002257 *pArg = String8();
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06002258 return UNEXPECTED_NULL;
Roshan Pius87b64d22016-07-18 12:51:02 -07002259 }
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06002260}
2261
2262const char* Parcel::readString8Inplace(size_t* outLen) const
2263{
2264 int32_t size = readInt32();
2265 // watch for potential int overflow from size+1
2266 if (size >= 0 && size < INT32_MAX) {
2267 *outLen = size;
2268 const char* str = (const char*)readInplace(size+1);
Steven Moreland61d0f842020-12-04 21:13:03 +00002269 if (str != nullptr) {
2270 if (str[size] == '\0') {
2271 return str;
2272 }
2273 android_errorWriteLog(0x534e4554, "172655291");
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06002274 }
Roshan Pius87b64d22016-07-18 12:51:02 -07002275 }
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06002276 *outLen = 0;
2277 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002278}
2279
2280String16 Parcel::readString16() const
2281{
2282 size_t len;
2283 const char16_t* str = readString16Inplace(&len);
2284 if (str) return String16(str, len);
Steven Moreland3a667492023-06-02 21:41:39 +00002285
2286 if (!mServiceFuzzing) {
2287 ALOGE("Reading a NULL string not supported here.");
2288 }
2289
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002290 return String16();
2291}
2292
Casey Dahlinb9872622015-11-25 15:09:45 -08002293
Casey Dahlin451ff582015-10-19 18:12:18 -07002294status_t Parcel::readString16(String16* pArg) const
2295{
2296 size_t len;
2297 const char16_t* str = readString16Inplace(&len);
2298 if (str) {
Casey Dahlin1515ea12015-10-20 16:26:23 -07002299 pArg->setTo(str, len);
Casey Dahlin451ff582015-10-19 18:12:18 -07002300 return 0;
2301 } else {
2302 *pArg = String16();
Christopher Wiley4db672d2015-11-10 09:44:30 -08002303 return UNEXPECTED_NULL;
Casey Dahlin451ff582015-10-19 18:12:18 -07002304 }
2305}
2306
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002307const char16_t* Parcel::readString16Inplace(size_t* outLen) const
2308{
2309 int32_t size = readInt32();
2310 // watch for potential int overflow from size+1
2311 if (size >= 0 && size < INT32_MAX) {
2312 *outLen = size;
2313 const char16_t* str = (const char16_t*)readInplace((size+1)*sizeof(char16_t));
Steven Moreland61d0f842020-12-04 21:13:03 +00002314 if (str != nullptr) {
2315 if (str[size] == u'\0') {
2316 return str;
2317 }
2318 android_errorWriteLog(0x534e4554, "172655291");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002319 }
2320 }
2321 *outLen = 0;
Yi Kong91635562018-06-07 14:38:36 -07002322 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002323}
2324
Casey Dahlinf0c13772015-10-27 18:33:56 -07002325status_t Parcel::readStrongBinder(sp<IBinder>* val) const
2326{
Christopher Wiley35d77ca2016-03-08 10:49:51 -08002327 status_t status = readNullableStrongBinder(val);
2328 if (status == OK && !val->get()) {
Steven Moreland3a667492023-06-02 21:41:39 +00002329 if (!mServiceFuzzing) {
2330 ALOGW("Expecting binder but got null!");
2331 }
Christopher Wiley35d77ca2016-03-08 10:49:51 -08002332 status = UNEXPECTED_NULL;
2333 }
2334 return status;
2335}
2336
2337status_t Parcel::readNullableStrongBinder(sp<IBinder>* val) const
2338{
Steven Morelanda86a3562019-08-01 23:28:34 +00002339 return unflattenBinder(val);
Casey Dahlinf0c13772015-10-27 18:33:56 -07002340}
2341
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002342sp<IBinder> Parcel::readStrongBinder() const
2343{
2344 sp<IBinder> val;
Christopher Wiley35d77ca2016-03-08 10:49:51 -08002345 // Note that a lot of code in Android reads binders by hand with this
2346 // method, and that code has historically been ok with getting nullptr
2347 // back (while ignoring error codes).
2348 readNullableStrongBinder(&val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002349 return val;
2350}
2351
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07002352int32_t Parcel::readExceptionCode() const
2353{
Christopher Wiley09eb7492015-11-09 15:06:15 -08002354 binder::Status status;
2355 status.readFromParcel(*this);
2356 return status.exceptionCode();
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07002357}
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002358
Tomasz Wasilczyk232d0b32023-10-09 17:37:16 +00002359#ifndef BINDER_DISABLE_NATIVE_HANDLE
Mathias Agopiana47f02a2009-05-21 16:29:38 -07002360native_handle* Parcel::readNativeHandle() const
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002361{
2362 int numFds, numInts;
2363 status_t err;
2364 err = readInt32(&numFds);
Yi Kong91635562018-06-07 14:38:36 -07002365 if (err != NO_ERROR) return nullptr;
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002366 err = readInt32(&numInts);
Yi Kong91635562018-06-07 14:38:36 -07002367 if (err != NO_ERROR) return nullptr;
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002368
Mathias Agopiana47f02a2009-05-21 16:29:38 -07002369 native_handle* h = native_handle_create(numFds, numInts);
Adam Lesinskieaac99a2015-05-12 17:35:48 -07002370 if (!h) {
Yi Kong91635562018-06-07 14:38:36 -07002371 return nullptr;
Adam Lesinskieaac99a2015-05-12 17:35:48 -07002372 }
2373
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002374 for (int i=0 ; err==NO_ERROR && i<numFds ; i++) {
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002375 h->data[i] = fcntl(readFileDescriptor(), F_DUPFD_CLOEXEC, 0);
Marco Nelissen1de79662016-04-26 08:44:09 -07002376 if (h->data[i] < 0) {
2377 for (int j = 0; j < i; j++) {
2378 close(h->data[j]);
2379 }
2380 native_handle_delete(h);
Yi Kong91635562018-06-07 14:38:36 -07002381 return nullptr;
Marco Nelissen1de79662016-04-26 08:44:09 -07002382 }
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002383 }
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002384 err = read(h->data + numFds, sizeof(int)*numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002385 if (err != NO_ERROR) {
Mathias Agopiana47f02a2009-05-21 16:29:38 -07002386 native_handle_close(h);
2387 native_handle_delete(h);
Yi Kong91635562018-06-07 14:38:36 -07002388 h = nullptr;
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002389 }
2390 return h;
2391}
Tomasz Wasilczyk232d0b32023-10-09 17:37:16 +00002392#endif
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002393
Frederick Mayle69a0c992022-05-26 20:38:39 +00002394int Parcel::readFileDescriptor() const {
2395 if (const auto* rpcFields = maybeRpcFields()) {
2396 if (!std::binary_search(rpcFields->mObjectPositions.begin(),
2397 rpcFields->mObjectPositions.end(), mDataPos)) {
Steven Moreland3a667492023-06-02 21:41:39 +00002398 if (!mServiceFuzzing) {
2399 ALOGW("Attempt to read file descriptor from Parcel %p at offset %zu that is not in "
2400 "the object list",
2401 this, mDataPos);
2402 }
Frederick Mayle69a0c992022-05-26 20:38:39 +00002403 return BAD_TYPE;
2404 }
2405
2406 int32_t objectType = readInt32();
2407 if (objectType != RpcFields::TYPE_NATIVE_FILE_DESCRIPTOR) {
2408 return BAD_TYPE;
2409 }
2410
2411 int32_t fdIndex = readInt32();
2412 if (rpcFields->mFds == nullptr || fdIndex < 0 ||
2413 static_cast<size_t>(fdIndex) >= rpcFields->mFds->size()) {
2414 ALOGE("RPC Parcel contains invalid file descriptor index. index=%d fd_count=%zu",
2415 fdIndex, rpcFields->mFds ? rpcFields->mFds->size() : 0);
2416 return BAD_VALUE;
2417 }
2418 return toRawFd(rpcFields->mFds->at(fdIndex));
2419 }
2420
Andrei Homescua9cca9c2022-05-03 04:48:01 +00002421#ifdef BINDER_WITH_KERNEL_IPC
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002422 const flat_binder_object* flat = readObject(true);
Casey Dahlin06673e32015-11-23 13:24:23 -08002423
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002424 if (flat && flat->hdr.type == BINDER_TYPE_FD) {
Casey Dahlin06673e32015-11-23 13:24:23 -08002425 return flat->handle;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002426 }
Casey Dahlin06673e32015-11-23 13:24:23 -08002427
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002428 return BAD_TYPE;
Andrei Homescua9cca9c2022-05-03 04:48:01 +00002429#else // BINDER_WITH_KERNEL_IPC
2430 LOG_ALWAYS_FATAL("Binder kernel driver disabled at build time");
2431 return INVALID_OPERATION;
2432#endif // BINDER_WITH_KERNEL_IPC
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002433}
2434
Frederick Mayle69a0c992022-05-26 20:38:39 +00002435int Parcel::readParcelFileDescriptor() const {
Dianne Hackborn1941a402016-08-29 12:30:43 -07002436 int32_t hasComm = readInt32();
2437 int fd = readFileDescriptor();
2438 if (hasComm != 0) {
Steven Morelandb73806a2018-11-12 19:35:47 -08002439 // detach (owned by the binder driver)
2440 int comm = readFileDescriptor();
2441
2442 // warning: this must be kept in sync with:
2443 // frameworks/base/core/java/android/os/ParcelFileDescriptor.java
2444 enum ParcelFileDescriptorStatus {
2445 DETACHED = 2,
2446 };
2447
2448#if BYTE_ORDER == BIG_ENDIAN
2449 const int32_t message = ParcelFileDescriptorStatus::DETACHED;
2450#endif
2451#if BYTE_ORDER == LITTLE_ENDIAN
2452 const int32_t message = __builtin_bswap32(ParcelFileDescriptorStatus::DETACHED);
2453#endif
2454
2455 ssize_t written = TEMP_FAILURE_RETRY(
2456 ::write(comm, &message, sizeof(message)));
2457
Krzysztof Kosińskia8406892021-02-02 17:59:43 -08002458 if (written != sizeof(message)) {
Steven Morelandb73806a2018-11-12 19:35:47 -08002459 ALOGW("Failed to detach ParcelFileDescriptor written: %zd err: %s",
2460 written, strerror(errno));
2461 return BAD_TYPE;
2462 }
Dianne Hackborn1941a402016-08-29 12:30:43 -07002463 }
2464 return fd;
2465}
2466
Tomasz Wasilczyk639490b2023-11-01 13:49:41 -07002467status_t Parcel::readUniqueFileDescriptor(unique_fd* val) const {
Casey Dahlin06673e32015-11-23 13:24:23 -08002468 int got = readFileDescriptor();
2469
2470 if (got == BAD_TYPE) {
2471 return BAD_TYPE;
2472 }
2473
Andrei Homescu24ad36e2022-08-04 01:33:33 +00002474 int dupFd;
Tomasz Wasilczyk0d9dec22023-10-06 20:28:49 +00002475 if (status_t err = binder::os::dupFileDescriptor(got, &dupFd); err != OK) {
Andrei Homescu24ad36e2022-08-04 01:33:33 +00002476 return BAD_VALUE;
2477 }
2478
2479 val->reset(dupFd);
Casey Dahlin06673e32015-11-23 13:24:23 -08002480
2481 if (val->get() < 0) {
2482 return BAD_VALUE;
2483 }
2484
2485 return OK;
2486}
2487
Tomasz Wasilczyk639490b2023-11-01 13:49:41 -07002488status_t Parcel::readUniqueParcelFileDescriptor(unique_fd* val) const {
Ryo Hashimotobf551892018-05-31 16:58:35 +09002489 int got = readParcelFileDescriptor();
2490
2491 if (got == BAD_TYPE) {
2492 return BAD_TYPE;
2493 }
2494
Andrei Homescu24ad36e2022-08-04 01:33:33 +00002495 int dupFd;
Tomasz Wasilczyk0d9dec22023-10-06 20:28:49 +00002496 if (status_t err = binder::os::dupFileDescriptor(got, &dupFd); err != OK) {
Andrei Homescu24ad36e2022-08-04 01:33:33 +00002497 return BAD_VALUE;
2498 }
2499
2500 val->reset(dupFd);
Ryo Hashimotobf551892018-05-31 16:58:35 +09002501
2502 if (val->get() < 0) {
2503 return BAD_VALUE;
2504 }
2505
2506 return OK;
2507}
Casey Dahlin06673e32015-11-23 13:24:23 -08002508
Jeff Brown5707dbf2011-09-23 21:17:56 -07002509status_t Parcel::readBlob(size_t len, ReadableBlob* outBlob) const
2510{
Tomasz Wasilczykbca8f942023-10-09 17:42:37 +00002511#ifdef BINDER_DISABLE_BLOB
2512 (void)len;
2513 (void)outBlob;
2514 return INVALID_OPERATION;
2515#else
Jeff Brown13b16042014-11-11 16:44:25 -08002516 int32_t blobType;
2517 status_t status = readInt32(&blobType);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002518 if (status) return status;
2519
Jeff Brown13b16042014-11-11 16:44:25 -08002520 if (blobType == BLOB_INPLACE) {
Steve Block6807e592011-10-20 11:56:00 +01002521 ALOGV("readBlob: read in place");
Jeff Brown5707dbf2011-09-23 21:17:56 -07002522 const void* ptr = readInplace(len);
2523 if (!ptr) return BAD_VALUE;
2524
Jeff Brown13b16042014-11-11 16:44:25 -08002525 outBlob->init(-1, const_cast<void*>(ptr), len, false);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002526 return NO_ERROR;
2527 }
2528
Steve Block6807e592011-10-20 11:56:00 +01002529 ALOGV("readBlob: read from ashmem");
Jeff Brown13b16042014-11-11 16:44:25 -08002530 bool isMutable = (blobType == BLOB_ASHMEM_MUTABLE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002531 int fd = readFileDescriptor();
2532 if (fd == int(BAD_TYPE)) return BAD_VALUE;
2533
Jorim Jaggi150b4ef2018-07-13 11:18:30 +00002534 if (!ashmem_valid(fd)) {
2535 ALOGE("invalid fd");
2536 return BAD_VALUE;
2537 }
Marco Nelissen7a96ec42018-06-06 07:37:46 -07002538 int size = ashmem_get_size_region(fd);
2539 if (size < 0 || size_t(size) < len) {
Jorim Jaggi150b4ef2018-07-13 11:18:30 +00002540 ALOGE("request size %zu does not match fd size %d", len, size);
Marco Nelissen7a96ec42018-06-06 07:37:46 -07002541 return BAD_VALUE;
2542 }
Yi Kong91635562018-06-07 14:38:36 -07002543 void* ptr = ::mmap(nullptr, len, isMutable ? PROT_READ | PROT_WRITE : PROT_READ,
Jeff Brown13b16042014-11-11 16:44:25 -08002544 MAP_SHARED, fd, 0);
Narayan Kamath9ea09752014-10-08 17:35:45 +01002545 if (ptr == MAP_FAILED) return NO_MEMORY;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002546
Jeff Brown13b16042014-11-11 16:44:25 -08002547 outBlob->init(fd, ptr, len, isMutable);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002548 return NO_ERROR;
Tomasz Wasilczykbca8f942023-10-09 17:42:37 +00002549#endif
Jeff Brown5707dbf2011-09-23 21:17:56 -07002550}
2551
Mathias Agopiane1424282013-07-29 21:24:40 -07002552status_t Parcel::read(FlattenableHelperInterface& val) const
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002553{
2554 // size
2555 const size_t len = this->readInt32();
2556 const size_t fd_count = this->readInt32();
2557
Andrei Homescu1519b982022-06-09 02:04:44 +00002558 if ((len > INT32_MAX) || (fd_count > kMaxFds)) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07002559 // don't accept size_t values which may have come from an
2560 // inadvertent conversion from a negative int.
2561 return BAD_VALUE;
2562 }
2563
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002564 // payload
Nick Kralevichb6b14232015-04-02 09:36:02 -07002565 void const* const buf = this->readInplace(pad_size(len));
Yi Kong91635562018-06-07 14:38:36 -07002566 if (buf == nullptr)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002567 return BAD_VALUE;
2568
Yi Kong91635562018-06-07 14:38:36 -07002569 int* fds = nullptr;
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002570 if (fd_count) {
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002571 fds = new (std::nothrow) int[fd_count];
2572 if (fds == nullptr) {
2573 ALOGE("read: failed to allocate requested %zu fds", fd_count);
2574 return BAD_VALUE;
2575 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002576 }
2577
2578 status_t err = NO_ERROR;
2579 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
Fabien Sanglardd84ff312016-10-21 10:58:26 -07002580 int fd = this->readFileDescriptor();
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002581 if (fd < 0 || ((fds[i] = fcntl(fd, F_DUPFD_CLOEXEC, 0)) < 0)) {
Jun Jiangabf8a2c2014-04-29 14:22:10 +08002582 err = BAD_VALUE;
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002583 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 -07002584 i, fds[i], fd_count, strerror(fd < 0 ? -fd : errno));
2585 // Close all the file descriptors that were dup-ed.
2586 for (size_t j=0; j<i ;j++) {
2587 close(fds[j]);
2588 }
Jun Jiangabf8a2c2014-04-29 14:22:10 +08002589 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002590 }
2591
2592 if (err == NO_ERROR) {
2593 err = val.unflatten(buf, len, fds, fd_count);
2594 }
2595
2596 if (fd_count) {
2597 delete [] fds;
2598 }
2599
2600 return err;
2601}
Andrei Homescua9cca9c2022-05-03 04:48:01 +00002602
2603#ifdef BINDER_WITH_KERNEL_IPC
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002604const flat_binder_object* Parcel::readObject(bool nullMetaData) const
2605{
Frederick Mayled3c595a2022-05-09 23:02:54 +00002606 const auto* kernelFields = maybeKernelFields();
2607 if (kernelFields == nullptr) {
2608 return nullptr;
2609 }
2610
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002611 const size_t DPOS = mDataPos;
2612 if ((DPOS+sizeof(flat_binder_object)) <= mDataSize) {
2613 const flat_binder_object* obj
2614 = reinterpret_cast<const flat_binder_object*>(mData+DPOS);
2615 mDataPos = DPOS + sizeof(flat_binder_object);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002616 if (!nullMetaData && (obj->cookie == 0 && obj->binder == 0)) {
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002617 // When transferring a NULL object, we don't write it into
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002618 // the object list, so we don't want to check for it when
2619 // reading.
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002620 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002621 return obj;
2622 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002623
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002624 // Ensure that this object is valid...
Frederick Mayled3c595a2022-05-09 23:02:54 +00002625 binder_size_t* const OBJS = kernelFields->mObjects;
2626 const size_t N = kernelFields->mObjectsSize;
2627 size_t opos = kernelFields->mNextObjectHint;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002628
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002629 if (N > 0) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002630 ALOGV("Parcel %p looking for obj at %zu, hint=%zu",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002631 this, DPOS, opos);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002632
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002633 // Start at the current hint position, looking for an object at
2634 // the current data position.
2635 if (opos < N) {
2636 while (opos < (N-1) && OBJS[opos] < DPOS) {
2637 opos++;
2638 }
2639 } else {
2640 opos = N-1;
2641 }
2642 if (OBJS[opos] == DPOS) {
2643 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002644 ALOGV("Parcel %p found obj %zu at index %zu with forward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002645 this, DPOS, opos);
Frederick Mayled3c595a2022-05-09 23:02:54 +00002646 kernelFields->mNextObjectHint = opos + 1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002647 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002648 return obj;
2649 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002650
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002651 // Look backwards for it...
2652 while (opos > 0 && OBJS[opos] > DPOS) {
2653 opos--;
2654 }
2655 if (OBJS[opos] == DPOS) {
2656 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002657 ALOGV("Parcel %p found obj %zu at index %zu with backward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002658 this, DPOS, opos);
Frederick Mayled3c595a2022-05-09 23:02:54 +00002659 kernelFields->mNextObjectHint = opos + 1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002660 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002661 return obj;
2662 }
2663 }
Steven Moreland3a667492023-06-02 21:41:39 +00002664 if (!mServiceFuzzing) {
2665 ALOGW("Attempt to read object from Parcel %p at offset %zu that is not in the object "
2666 "list",
2667 this, DPOS);
2668 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002669 }
Yi Kong91635562018-06-07 14:38:36 -07002670 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002671}
Andrei Homescua9cca9c2022-05-03 04:48:01 +00002672#endif // BINDER_WITH_KERNEL_IPC
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002673
Frederick Mayled3c595a2022-05-09 23:02:54 +00002674void Parcel::closeFileDescriptors() {
Frederick Mayle69a0c992022-05-26 20:38:39 +00002675 if (auto* kernelFields = maybeKernelFields()) {
Andrei Homescua9cca9c2022-05-03 04:48:01 +00002676#ifdef BINDER_WITH_KERNEL_IPC
Frederick Mayle69a0c992022-05-26 20:38:39 +00002677 size_t i = kernelFields->mObjectsSize;
2678 if (i > 0) {
2679 // ALOGI("Closing file descriptors for %zu objects...", i);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002680 }
Frederick Mayle69a0c992022-05-26 20:38:39 +00002681 while (i > 0) {
2682 i--;
2683 const flat_binder_object* flat =
2684 reinterpret_cast<flat_binder_object*>(mData + kernelFields->mObjects[i]);
2685 if (flat->hdr.type == BINDER_TYPE_FD) {
2686 // ALOGI("Closing fd: %ld", flat->handle);
Steven Moreland02f736f2023-06-22 23:03:57 +00002687 // FDs from the kernel are always owned
2688 FdTagClose(flat->handle, this);
Frederick Mayle69a0c992022-05-26 20:38:39 +00002689 }
2690 }
Andrei Homescua9cca9c2022-05-03 04:48:01 +00002691#else // BINDER_WITH_KERNEL_IPC
2692 LOG_ALWAYS_FATAL("Binder kernel driver disabled at build time");
2693#endif // BINDER_WITH_KERNEL_IPC
Frederick Mayle69a0c992022-05-26 20:38:39 +00002694 } else if (auto* rpcFields = maybeRpcFields()) {
2695 rpcFields->mFds.reset();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002696 }
2697}
2698
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002699uintptr_t Parcel::ipcData() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002700{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002701 return reinterpret_cast<uintptr_t>(mData);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002702}
2703
2704size_t Parcel::ipcDataSize() const
2705{
2706 return (mDataSize > mDataPos ? mDataSize : mDataPos);
2707}
2708
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002709uintptr_t Parcel::ipcObjects() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002710{
Frederick Mayled3c595a2022-05-09 23:02:54 +00002711 if (const auto* kernelFields = maybeKernelFields()) {
2712 return reinterpret_cast<uintptr_t>(kernelFields->mObjects);
2713 }
2714 return 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002715}
2716
2717size_t Parcel::ipcObjectsCount() const
2718{
Frederick Mayled3c595a2022-05-09 23:02:54 +00002719 if (const auto* kernelFields = maybeKernelFields()) {
2720 return kernelFields->mObjectsSize;
2721 }
2722 return 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002723}
2724
Frederick Mayled3c595a2022-05-09 23:02:54 +00002725void Parcel::ipcSetDataReference(const uint8_t* data, size_t dataSize, const binder_size_t* objects,
2726 size_t objectsCount, release_func relFunc) {
Steven Moreland438cce82021-04-02 18:04:08 +00002727 // this code uses 'mOwner == nullptr' to understand whether it owns memory
2728 LOG_ALWAYS_FATAL_IF(relFunc == nullptr, "must provide cleanup function");
2729
Steven Morelandceed9bb2020-12-17 01:01:06 +00002730 freeData();
2731
Frederick Mayled3c595a2022-05-09 23:02:54 +00002732 auto* kernelFields = maybeKernelFields();
2733 LOG_ALWAYS_FATAL_IF(kernelFields == nullptr); // guaranteed by freeData.
2734
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002735 mData = const_cast<uint8_t*>(data);
2736 mDataSize = mDataCapacity = dataSize;
Frederick Mayled3c595a2022-05-09 23:02:54 +00002737 kernelFields->mObjects = const_cast<binder_size_t*>(objects);
2738 kernelFields->mObjectsSize = kernelFields->mObjectsCapacity = objectsCount;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002739 mOwner = relFunc;
Steven Morelandceed9bb2020-12-17 01:01:06 +00002740
Andrei Homescua9cca9c2022-05-03 04:48:01 +00002741#ifdef BINDER_WITH_KERNEL_IPC
Steven Morelandceed9bb2020-12-17 01:01:06 +00002742 binder_size_t minOffset = 0;
Frederick Mayled3c595a2022-05-09 23:02:54 +00002743 for (size_t i = 0; i < kernelFields->mObjectsSize; i++) {
2744 binder_size_t offset = kernelFields->mObjects[i];
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002745 if (offset < minOffset) {
Dan Albert3bdc5b82014-11-20 11:50:23 -08002746 ALOGE("%s: bad object offset %" PRIu64 " < %" PRIu64 "\n",
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002747 __func__, (uint64_t)offset, (uint64_t)minOffset);
Frederick Mayled3c595a2022-05-09 23:02:54 +00002748 kernelFields->mObjectsSize = 0;
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002749 break;
2750 }
Martijn Coenen82c75312019-07-24 15:18:30 +02002751 const flat_binder_object* flat
2752 = reinterpret_cast<const flat_binder_object*>(mData + offset);
2753 uint32_t type = flat->hdr.type;
2754 if (!(type == BINDER_TYPE_BINDER || type == BINDER_TYPE_HANDLE ||
2755 type == BINDER_TYPE_FD)) {
2756 // We should never receive other types (eg BINDER_TYPE_FDA) as long as we don't support
2757 // them in libbinder. If we do receive them, it probably means a kernel bug; try to
Steven Morelandf2e0a952021-11-01 18:17:23 -07002758 // recover gracefully by clearing out the objects.
Martijn Coenen82c75312019-07-24 15:18:30 +02002759 android_errorWriteLog(0x534e4554, "135930648");
Steven Morelandf2e0a952021-11-01 18:17:23 -07002760 android_errorWriteLog(0x534e4554, "203847542");
Martijn Coenen82c75312019-07-24 15:18:30 +02002761 ALOGE("%s: unsupported type object (%" PRIu32 ") at offset %" PRIu64 "\n",
2762 __func__, type, (uint64_t)offset);
Steven Morelandf2e0a952021-11-01 18:17:23 -07002763
2764 // WARNING: callers of ipcSetDataReference need to make sure they
2765 // don't rely on mObjectsSize in their release_func.
Frederick Mayled3c595a2022-05-09 23:02:54 +00002766 kernelFields->mObjectsSize = 0;
Martijn Coenen82c75312019-07-24 15:18:30 +02002767 break;
2768 }
Steven Moreland02f736f2023-06-22 23:03:57 +00002769 if (type == BINDER_TYPE_FD) {
2770 // FDs from the kernel are always owned
Tomasz Wasilczyke97f3a82024-04-30 10:37:32 -07002771 FdTag(flat->handle, nullptr, this);
Steven Moreland02f736f2023-06-22 23:03:57 +00002772 }
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002773 minOffset = offset + sizeof(flat_binder_object);
2774 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002775 scanForFds();
Andrei Homescua9cca9c2022-05-03 04:48:01 +00002776#else // BINDER_WITH_KERNEL_IPC
2777 LOG_ALWAYS_FATAL_IF(objectsCount != 0,
2778 "Non-zero objects count passed to Parcel with kernel driver disabled");
2779#endif // BINDER_WITH_KERNEL_IPC
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002780}
2781
Frederick Mayleffe9ac22022-06-30 02:07:36 +00002782status_t Parcel::rpcSetDataReference(
2783 const sp<RpcSession>& session, const uint8_t* data, size_t dataSize,
2784 const uint32_t* objectTable, size_t objectTableSize,
Tomasz Wasilczyk639490b2023-11-01 13:49:41 -07002785 std::vector<std::variant<unique_fd, borrowed_fd>>&& ancillaryFds, release_func relFunc) {
Frederick Mayled3c595a2022-05-09 23:02:54 +00002786 // this code uses 'mOwner == nullptr' to understand whether it owns memory
2787 LOG_ALWAYS_FATAL_IF(relFunc == nullptr, "must provide cleanup function");
2788
2789 LOG_ALWAYS_FATAL_IF(session == nullptr);
2790
Frederick Mayle694e9732022-07-15 00:24:58 +00002791 if (objectTableSize != ancillaryFds.size()) {
2792 ALOGE("objectTableSize=%zu ancillaryFds.size=%zu", objectTableSize, ancillaryFds.size());
2793 relFunc(data, dataSize, nullptr, 0);
2794 return BAD_VALUE;
2795 }
2796 for (size_t i = 0; i < objectTableSize; i++) {
2797 uint32_t minObjectEnd;
2798 if (__builtin_add_overflow(objectTable[i], sizeof(RpcFields::ObjectType), &minObjectEnd) ||
2799 minObjectEnd >= dataSize) {
2800 ALOGE("received out of range object position: %" PRIu32 " (parcel size is %zu)",
2801 objectTable[i], dataSize);
2802 relFunc(data, dataSize, nullptr, 0);
2803 return BAD_VALUE;
2804 }
2805 }
2806
Frederick Mayled3c595a2022-05-09 23:02:54 +00002807 freeData();
2808 markForRpc(session);
2809
Frederick Mayle69a0c992022-05-26 20:38:39 +00002810 auto* rpcFields = maybeRpcFields();
2811 LOG_ALWAYS_FATAL_IF(rpcFields == nullptr); // guaranteed by markForRpc.
2812
Frederick Mayled3c595a2022-05-09 23:02:54 +00002813 mData = const_cast<uint8_t*>(data);
2814 mDataSize = mDataCapacity = dataSize;
2815 mOwner = relFunc;
Frederick Mayle69a0c992022-05-26 20:38:39 +00002816
Frederick Mayle69a0c992022-05-26 20:38:39 +00002817 rpcFields->mObjectPositions.reserve(objectTableSize);
2818 for (size_t i = 0; i < objectTableSize; i++) {
2819 rpcFields->mObjectPositions.push_back(objectTable[i]);
2820 }
2821 if (!ancillaryFds.empty()) {
2822 rpcFields->mFds = std::make_unique<decltype(rpcFields->mFds)::element_type>();
Frederick Mayleffe9ac22022-06-30 02:07:36 +00002823 *rpcFields->mFds = std::move(ancillaryFds);
Frederick Mayle69a0c992022-05-26 20:38:39 +00002824 }
2825
2826 return OK;
Frederick Mayled3c595a2022-05-09 23:02:54 +00002827}
2828
Pawan Wagh7063b522022-09-28 18:52:26 +00002829void Parcel::print(std::ostream& to, uint32_t /*flags*/) const {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002830 to << "Parcel(";
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002831
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002832 if (errorCheck() != NO_ERROR) {
2833 const status_t err = errorCheck();
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002834 to << "Error: " << (void*)(intptr_t)err << " \"" << strerror(-err) << "\"";
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002835 } else if (dataSize() > 0) {
2836 const uint8_t* DATA = data();
Pawan Wagh7063b522022-09-28 18:52:26 +00002837 to << "\t" << HexDump(DATA, dataSize());
Andrei Homescua9cca9c2022-05-03 04:48:01 +00002838#ifdef BINDER_WITH_KERNEL_IPC
Frederick Mayled3c595a2022-05-09 23:02:54 +00002839 if (const auto* kernelFields = maybeKernelFields()) {
2840 const binder_size_t* OBJS = kernelFields->mObjects;
2841 const size_t N = objectsCount();
2842 for (size_t i = 0; i < N; i++) {
2843 const flat_binder_object* flat =
2844 reinterpret_cast<const flat_binder_object*>(DATA + OBJS[i]);
Pawan Wagh7063b522022-09-28 18:52:26 +00002845 to << "Object #" << i << " @ " << (void*)OBJS[i] << ": "
Frederick Mayled3c595a2022-05-09 23:02:54 +00002846 << TypeCode(flat->hdr.type & 0x7f7f7f00) << " = " << flat->binder;
2847 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002848 }
Andrei Homescua9cca9c2022-05-03 04:48:01 +00002849#endif // BINDER_WITH_KERNEL_IPC
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002850 } else {
2851 to << "NULL";
2852 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002853
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002854 to << ")";
2855}
2856
2857void Parcel::releaseObjects()
2858{
Frederick Mayled3c595a2022-05-09 23:02:54 +00002859 auto* kernelFields = maybeKernelFields();
2860 if (kernelFields == nullptr) {
2861 return;
2862 }
2863
Andrei Homescua9cca9c2022-05-03 04:48:01 +00002864#ifdef BINDER_WITH_KERNEL_IPC
Frederick Mayled3c595a2022-05-09 23:02:54 +00002865 size_t i = kernelFields->mObjectsSize;
Martijn Coenen69390d42018-10-22 15:18:10 +02002866 if (i == 0) {
2867 return;
2868 }
2869 sp<ProcessState> proc(ProcessState::self());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002870 uint8_t* const data = mData;
Frederick Mayled3c595a2022-05-09 23:02:54 +00002871 binder_size_t* const objects = kernelFields->mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002872 while (i > 0) {
2873 i--;
Frederick Mayled3c595a2022-05-09 23:02:54 +00002874 const flat_binder_object* flat = reinterpret_cast<flat_binder_object*>(data + objects[i]);
Steven Morelandc673f1f2021-10-07 18:23:35 -07002875 release_object(proc, *flat, this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002876 }
Andrei Homescua9cca9c2022-05-03 04:48:01 +00002877#endif // BINDER_WITH_KERNEL_IPC
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002878}
2879
2880void Parcel::acquireObjects()
2881{
Frederick Mayled3c595a2022-05-09 23:02:54 +00002882 auto* kernelFields = maybeKernelFields();
2883 if (kernelFields == nullptr) {
2884 return;
2885 }
2886
Andrei Homescua9cca9c2022-05-03 04:48:01 +00002887#ifdef BINDER_WITH_KERNEL_IPC
Frederick Mayled3c595a2022-05-09 23:02:54 +00002888 size_t i = kernelFields->mObjectsSize;
Martijn Coenen69390d42018-10-22 15:18:10 +02002889 if (i == 0) {
2890 return;
2891 }
2892 const sp<ProcessState> proc(ProcessState::self());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002893 uint8_t* const data = mData;
Frederick Mayled3c595a2022-05-09 23:02:54 +00002894 binder_size_t* const objects = kernelFields->mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002895 while (i > 0) {
2896 i--;
Frederick Mayled3c595a2022-05-09 23:02:54 +00002897 const flat_binder_object* flat = reinterpret_cast<flat_binder_object*>(data + objects[i]);
Steven Morelandc673f1f2021-10-07 18:23:35 -07002898 acquire_object(proc, *flat, this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002899 }
Andrei Homescua9cca9c2022-05-03 04:48:01 +00002900#endif // BINDER_WITH_KERNEL_IPC
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002901}
2902
2903void Parcel::freeData()
2904{
2905 freeDataNoInit();
2906 initState();
2907}
2908
2909void Parcel::freeDataNoInit()
2910{
2911 if (mOwner) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002912 LOG_ALLOC("Parcel %p: freeing other owner data", this);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002913 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
Frederick Mayled3c595a2022-05-09 23:02:54 +00002914 auto* kernelFields = maybeKernelFields();
Frederick Mayle53b6ffe2022-07-15 20:14:01 +00002915 // Close FDs before freeing, otherwise they will leak for kernel binder.
2916 closeFileDescriptors();
2917 mOwner(mData, mDataSize, kernelFields ? kernelFields->mObjects : nullptr,
Frederick Mayled3c595a2022-05-09 23:02:54 +00002918 kernelFields ? kernelFields->mObjectsSize : 0);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002919 } else {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002920 LOG_ALLOC("Parcel %p: freeing allocated data", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002921 releaseObjects();
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002922 if (mData) {
2923 LOG_ALLOC("Parcel %p: freeing with %zu capacity", this, mDataCapacity);
Jeff Sharkey8994c182020-09-11 12:07:10 -06002924 gParcelGlobalAllocSize -= mDataCapacity;
2925 gParcelGlobalAllocCount--;
Steven Morelandf183fdd2020-10-27 00:12:12 +00002926 if (mDeallocZero) {
2927 zeroMemory(mData, mDataSize);
2928 }
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002929 free(mData);
2930 }
Frederick Mayled3c595a2022-05-09 23:02:54 +00002931 auto* kernelFields = maybeKernelFields();
2932 if (kernelFields && kernelFields->mObjects) free(kernelFields->mObjects);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002933 }
2934}
2935
2936status_t Parcel::growData(size_t len)
2937{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002938 if (len > INT32_MAX) {
2939 // don't accept size_t values which may have come from an
2940 // inadvertent conversion from a negative int.
2941 return BAD_VALUE;
2942 }
2943
Martijn Coenen93fe5182020-01-22 10:46:25 +01002944 if (len > SIZE_MAX - mDataSize) return NO_MEMORY; // overflow
2945 if (mDataSize + len > SIZE_MAX / 3) return NO_MEMORY; // overflow
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002946 size_t newSize = ((mDataSize+len)*3)/2;
2947 return (newSize <= mDataSize)
2948 ? (status_t) NO_MEMORY
Steven Moreland042ae822020-05-27 17:45:17 +00002949 : continueWrite(std::max(newSize, (size_t) 128));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002950}
2951
Steven Morelandf183fdd2020-10-27 00:12:12 +00002952static uint8_t* reallocZeroFree(uint8_t* data, size_t oldCapacity, size_t newCapacity, bool zero) {
2953 if (!zero) {
2954 return (uint8_t*)realloc(data, newCapacity);
2955 }
2956 uint8_t* newData = (uint8_t*)malloc(newCapacity);
2957 if (!newData) {
2958 return nullptr;
2959 }
2960
2961 memcpy(newData, data, std::min(oldCapacity, newCapacity));
2962 zeroMemory(data, oldCapacity);
2963 free(data);
2964 return newData;
2965}
2966
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002967status_t Parcel::restartWrite(size_t desired)
2968{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002969 if (desired > INT32_MAX) {
2970 // don't accept size_t values which may have come from an
2971 // inadvertent conversion from a negative int.
2972 return BAD_VALUE;
2973 }
2974
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002975 if (mOwner) {
2976 freeData();
2977 return continueWrite(desired);
2978 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002979
Steven Morelandc6b0dfa2024-03-05 09:10:02 +00002980 releaseObjects();
2981
Steven Morelandf183fdd2020-10-27 00:12:12 +00002982 uint8_t* data = reallocZeroFree(mData, mDataCapacity, desired, mDeallocZero);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002983 if (!data && desired > mDataCapacity) {
Steven Moreland47d7f1e2024-03-08 23:43:21 +00002984 LOG_ALWAYS_FATAL("out of memory");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002985 mError = NO_MEMORY;
2986 return NO_MEMORY;
2987 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002988
Devin Moore4a0a55e2020-06-04 13:23:10 -07002989 if (data || desired == 0) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002990 LOG_ALLOC("Parcel %p: restart from %zu to %zu capacity", this, mDataCapacity, desired);
Jeff Sharkey8994c182020-09-11 12:07:10 -06002991 if (mDataCapacity > desired) {
2992 gParcelGlobalAllocSize -= (mDataCapacity - desired);
2993 } else {
2994 gParcelGlobalAllocSize += (desired - mDataCapacity);
2995 }
2996
Colin Cross83ec65e2015-12-08 17:15:50 -08002997 if (!mData) {
2998 gParcelGlobalAllocCount++;
2999 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003000 mData = data;
3001 mDataCapacity = desired;
3002 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07003003
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003004 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07003005 ALOGV("restartWrite Setting data size of %p to %zu", this, mDataSize);
3006 ALOGV("restartWrite Setting data pos of %p to %zu", this, mDataPos);
3007
Frederick Mayled3c595a2022-05-09 23:02:54 +00003008 if (auto* kernelFields = maybeKernelFields()) {
3009 free(kernelFields->mObjects);
3010 kernelFields->mObjects = nullptr;
3011 kernelFields->mObjectsSize = kernelFields->mObjectsCapacity = 0;
3012 kernelFields->mNextObjectHint = 0;
3013 kernelFields->mObjectsSorted = false;
3014 kernelFields->mHasFds = false;
3015 kernelFields->mFdsKnown = true;
Frederick Mayle69a0c992022-05-26 20:38:39 +00003016 } else if (auto* rpcFields = maybeRpcFields()) {
3017 rpcFields->mObjectPositions.clear();
3018 rpcFields->mFds.reset();
Frederick Mayled3c595a2022-05-09 23:02:54 +00003019 }
Dianne Hackborn8938ed22011-09-28 23:19:47 -04003020 mAllowFds = true;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07003021
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003022 return NO_ERROR;
3023}
3024
3025status_t Parcel::continueWrite(size_t desired)
3026{
Nick Kralevichb6b14232015-04-02 09:36:02 -07003027 if (desired > INT32_MAX) {
3028 // don't accept size_t values which may have come from an
3029 // inadvertent conversion from a negative int.
3030 return BAD_VALUE;
3031 }
3032
Frederick Mayled3c595a2022-05-09 23:02:54 +00003033 auto* kernelFields = maybeKernelFields();
Frederick Mayle69a0c992022-05-26 20:38:39 +00003034 auto* rpcFields = maybeRpcFields();
Frederick Mayled3c595a2022-05-09 23:02:54 +00003035
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003036 // If shrinking, first adjust for any objects that appear
3037 // after the new data size.
Frederick Mayle69a0c992022-05-26 20:38:39 +00003038 size_t objectsSize =
3039 kernelFields ? kernelFields->mObjectsSize : rpcFields->mObjectPositions.size();
3040 if (desired < mDataSize) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003041 if (desired == 0) {
3042 objectsSize = 0;
3043 } else {
Frederick Mayle69a0c992022-05-26 20:38:39 +00003044 if (kernelFields) {
3045 while (objectsSize > 0) {
3046 if (kernelFields->mObjects[objectsSize - 1] < desired) break;
3047 objectsSize--;
3048 }
3049 } else {
3050 while (objectsSize > 0) {
3051 if (rpcFields->mObjectPositions[objectsSize - 1] < desired) break;
3052 objectsSize--;
3053 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003054 }
3055 }
3056 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07003057
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003058 if (mOwner) {
3059 // If the size is going to zero, just release the owner's data.
3060 if (desired == 0) {
3061 freeData();
3062 return NO_ERROR;
3063 }
3064
3065 // If there is a different owner, we need to take
3066 // posession.
3067 uint8_t* data = (uint8_t*)malloc(desired);
3068 if (!data) {
3069 mError = NO_MEMORY;
3070 return NO_MEMORY;
3071 }
Yi Kong91635562018-06-07 14:38:36 -07003072 binder_size_t* objects = nullptr;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07003073
Frederick Mayle69a0c992022-05-26 20:38:39 +00003074 if (kernelFields && objectsSize) {
Nick Kraleviche9881a32015-04-28 16:21:30 -07003075 objects = (binder_size_t*)calloc(objectsSize, sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003076 if (!objects) {
Hyejin Kim3f727c02013-03-09 11:28:54 +09003077 free(data);
3078
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003079 mError = NO_MEMORY;
3080 return NO_MEMORY;
3081 }
3082
3083 // Little hack to only acquire references on objects
3084 // we will be keeping.
Frederick Mayled3c595a2022-05-09 23:02:54 +00003085 size_t oldObjectsSize = kernelFields->mObjectsSize;
3086 kernelFields->mObjectsSize = objectsSize;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003087 acquireObjects();
Frederick Mayled3c595a2022-05-09 23:02:54 +00003088 kernelFields->mObjectsSize = oldObjectsSize;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003089 }
Frederick Mayle69a0c992022-05-26 20:38:39 +00003090 if (rpcFields) {
3091 if (status_t status = truncateRpcObjects(objectsSize); status != OK) {
3092 free(data);
3093 return status;
3094 }
3095 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07003096
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003097 if (mData) {
3098 memcpy(data, mData, mDataSize < desired ? mDataSize : desired);
3099 }
Frederick Mayled3c595a2022-05-09 23:02:54 +00003100 if (objects && kernelFields && kernelFields->mObjects) {
3101 memcpy(objects, kernelFields->mObjects, objectsSize * sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003102 }
Frederick Mayle53b6ffe2022-07-15 20:14:01 +00003103 // ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
3104 if (kernelFields) {
3105 // TODO(b/239222407): This seems wrong. We should only free FDs when
3106 // they are in a truncated section of the parcel.
3107 closeFileDescriptors();
3108 }
3109 mOwner(mData, mDataSize, kernelFields ? kernelFields->mObjects : nullptr,
Frederick Mayled3c595a2022-05-09 23:02:54 +00003110 kernelFields ? kernelFields->mObjectsSize : 0);
Yi Kong91635562018-06-07 14:38:36 -07003111 mOwner = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003112
Dianne Hackborn7e790af2014-11-11 12:22:53 -08003113 LOG_ALLOC("Parcel %p: taking ownership of %zu capacity", this, desired);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08003114 gParcelGlobalAllocSize += desired;
3115 gParcelGlobalAllocCount++;
Dianne Hackborn7e790af2014-11-11 12:22:53 -08003116
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003117 mData = data;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003118 mDataSize = (mDataSize < desired) ? mDataSize : desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07003119 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003120 mDataCapacity = desired;
Frederick Mayled3c595a2022-05-09 23:02:54 +00003121 if (kernelFields) {
3122 kernelFields->mObjects = objects;
3123 kernelFields->mObjectsSize = kernelFields->mObjectsCapacity = objectsSize;
3124 kernelFields->mNextObjectHint = 0;
3125 kernelFields->mObjectsSorted = false;
3126 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003127
3128 } else if (mData) {
Frederick Mayled3c595a2022-05-09 23:02:54 +00003129 if (kernelFields && objectsSize < kernelFields->mObjectsSize) {
Andrei Homescua9cca9c2022-05-03 04:48:01 +00003130#ifdef BINDER_WITH_KERNEL_IPC
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003131 // Need to release refs on any objects we are dropping.
3132 const sp<ProcessState> proc(ProcessState::self());
Frederick Mayled3c595a2022-05-09 23:02:54 +00003133 for (size_t i = objectsSize; i < kernelFields->mObjectsSize; i++) {
3134 const flat_binder_object* flat =
3135 reinterpret_cast<flat_binder_object*>(mData + kernelFields->mObjects[i]);
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07003136 if (flat->hdr.type == BINDER_TYPE_FD) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003137 // will need to rescan because we may have lopped off the only FDs
Frederick Mayled3c595a2022-05-09 23:02:54 +00003138 kernelFields->mFdsKnown = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003139 }
Steven Morelandc673f1f2021-10-07 18:23:35 -07003140 release_object(proc, *flat, this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003141 }
Michael Wachenschwanz6af27a82019-06-03 17:24:51 -07003142
3143 if (objectsSize == 0) {
Frederick Mayled3c595a2022-05-09 23:02:54 +00003144 free(kernelFields->mObjects);
3145 kernelFields->mObjects = nullptr;
3146 kernelFields->mObjectsCapacity = 0;
Michael Wachenschwanz6af27a82019-06-03 17:24:51 -07003147 } else {
3148 binder_size_t* objects =
Frederick Mayled3c595a2022-05-09 23:02:54 +00003149 (binder_size_t*)realloc(kernelFields->mObjects,
3150 objectsSize * sizeof(binder_size_t));
Michael Wachenschwanz6af27a82019-06-03 17:24:51 -07003151 if (objects) {
Frederick Mayled3c595a2022-05-09 23:02:54 +00003152 kernelFields->mObjects = objects;
3153 kernelFields->mObjectsCapacity = objectsSize;
Michael Wachenschwanz6af27a82019-06-03 17:24:51 -07003154 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003155 }
Frederick Mayled3c595a2022-05-09 23:02:54 +00003156 kernelFields->mObjectsSize = objectsSize;
3157 kernelFields->mNextObjectHint = 0;
3158 kernelFields->mObjectsSorted = false;
Andrei Homescua9cca9c2022-05-03 04:48:01 +00003159#else // BINDER_WITH_KERNEL_IPC
3160 LOG_ALWAYS_FATAL("Non-zero numObjects for RPC Parcel");
3161#endif // BINDER_WITH_KERNEL_IPC
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003162 }
Frederick Mayle69a0c992022-05-26 20:38:39 +00003163 if (rpcFields) {
3164 if (status_t status = truncateRpcObjects(objectsSize); status != OK) {
3165 return status;
3166 }
3167 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003168
3169 // We own the data, so we can just do a realloc().
3170 if (desired > mDataCapacity) {
Steven Morelandf183fdd2020-10-27 00:12:12 +00003171 uint8_t* data = reallocZeroFree(mData, mDataCapacity, desired, mDeallocZero);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003172 if (data) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08003173 LOG_ALLOC("Parcel %p: continue from %zu to %zu capacity", this, mDataCapacity,
3174 desired);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08003175 gParcelGlobalAllocSize += desired;
3176 gParcelGlobalAllocSize -= mDataCapacity;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003177 mData = data;
3178 mDataCapacity = desired;
Ganesh Mahendranade89892017-09-28 16:56:03 +08003179 } else {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003180 mError = NO_MEMORY;
3181 return NO_MEMORY;
3182 }
3183 } else {
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07003184 if (mDataSize > desired) {
3185 mDataSize = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07003186 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07003187 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003188 if (mDataPos > desired) {
3189 mDataPos = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07003190 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003191 }
3192 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07003193
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003194 } else {
3195 // This is the first data. Easy!
3196 uint8_t* data = (uint8_t*)malloc(desired);
3197 if (!data) {
3198 mError = NO_MEMORY;
3199 return NO_MEMORY;
3200 }
Hyejin Kim3f727c02013-03-09 11:28:54 +09003201
Frederick Mayled3c595a2022-05-09 23:02:54 +00003202 if (!(mDataCapacity == 0 &&
3203 (kernelFields == nullptr ||
3204 (kernelFields->mObjects == nullptr && kernelFields->mObjectsCapacity == 0)))) {
3205 ALOGE("continueWrite: %zu/%p/%zu/%zu", mDataCapacity,
3206 kernelFields ? kernelFields->mObjects : nullptr,
3207 kernelFields ? kernelFields->mObjectsCapacity : 0, desired);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003208 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07003209
Dianne Hackborn7e790af2014-11-11 12:22:53 -08003210 LOG_ALLOC("Parcel %p: allocating with %zu capacity", this, desired);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08003211 gParcelGlobalAllocSize += desired;
3212 gParcelGlobalAllocCount++;
Dianne Hackborn7e790af2014-11-11 12:22:53 -08003213
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003214 mData = data;
3215 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07003216 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
3217 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003218 mDataCapacity = desired;
3219 }
3220
3221 return NO_ERROR;
3222}
3223
Frederick Mayle69a0c992022-05-26 20:38:39 +00003224status_t Parcel::truncateRpcObjects(size_t newObjectsSize) {
3225 auto* rpcFields = maybeRpcFields();
3226 if (newObjectsSize == 0) {
3227 rpcFields->mObjectPositions.clear();
3228 if (rpcFields->mFds) {
3229 rpcFields->mFds->clear();
3230 }
3231 return OK;
3232 }
3233 while (rpcFields->mObjectPositions.size() > newObjectsSize) {
3234 uint32_t pos = rpcFields->mObjectPositions.back();
3235 rpcFields->mObjectPositions.pop_back();
3236 const auto type = *reinterpret_cast<const RpcFields::ObjectType*>(mData + pos);
3237 if (type == RpcFields::TYPE_NATIVE_FILE_DESCRIPTOR) {
3238 const auto fdIndex =
3239 *reinterpret_cast<const int32_t*>(mData + pos + sizeof(RpcFields::ObjectType));
3240 if (rpcFields->mFds == nullptr || fdIndex < 0 ||
3241 static_cast<size_t>(fdIndex) >= rpcFields->mFds->size()) {
3242 ALOGE("RPC Parcel contains invalid file descriptor index. index=%d fd_count=%zu",
3243 fdIndex, rpcFields->mFds ? rpcFields->mFds->size() : 0);
3244 return BAD_VALUE;
3245 }
3246 // In practice, this always removes the last element.
3247 rpcFields->mFds->erase(rpcFields->mFds->begin() + fdIndex);
3248 }
3249 }
3250 return OK;
3251}
3252
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003253void Parcel::initState()
3254{
Dianne Hackborn7e790af2014-11-11 12:22:53 -08003255 LOG_ALLOC("Parcel %p: initState", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003256 mError = NO_ERROR;
Yi Kong91635562018-06-07 14:38:36 -07003257 mData = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003258 mDataSize = 0;
3259 mDataCapacity = 0;
3260 mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07003261 ALOGV("initState Setting data size of %p to %zu", this, mDataSize);
3262 ALOGV("initState Setting data pos of %p to %zu", this, mDataPos);
Frederick Mayled3c595a2022-05-09 23:02:54 +00003263 mVariantFields.emplace<KernelFields>();
Steven Moreland6e5a7752019-08-05 20:30:14 -07003264 mAllowFds = true;
Steven Morelandf183fdd2020-10-27 00:12:12 +00003265 mDeallocZero = false;
Yi Kong91635562018-06-07 14:38:36 -07003266 mOwner = nullptr;
Pawan Wagh104654a2022-11-15 21:18:42 +00003267 mEnforceNoDataAvail = true;
Steven Moreland3a667492023-06-02 21:41:39 +00003268 mServiceFuzzing = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003269}
3270
Bernardo Rufinobbbd88d2021-10-15 14:54:30 +01003271void Parcel::scanForFds() const {
Frederick Mayled3c595a2022-05-09 23:02:54 +00003272 auto* kernelFields = maybeKernelFields();
3273 if (kernelFields == nullptr) {
3274 return;
3275 }
3276 status_t status = hasFileDescriptorsInRange(0, dataSize(), &kernelFields->mHasFds);
Bernardo Rufinobbbd88d2021-10-15 14:54:30 +01003277 ALOGE_IF(status != NO_ERROR, "Error %d calling hasFileDescriptorsInRange()", status);
Frederick Mayled3c595a2022-05-09 23:02:54 +00003278 kernelFields->mFdsKnown = true;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003279}
3280
Andrei Homescua9cca9c2022-05-03 04:48:01 +00003281#ifdef BINDER_WITH_KERNEL_IPC
Adrian Rooscbf37262015-10-22 16:12:53 -07003282size_t Parcel::getOpenAshmemSize() const
3283{
Frederick Mayled3c595a2022-05-09 23:02:54 +00003284 auto* kernelFields = maybeKernelFields();
3285 if (kernelFields == nullptr) {
3286 return 0;
3287 }
3288
Steven Morelandc673f1f2021-10-07 18:23:35 -07003289 size_t openAshmemSize = 0;
Tomasz Wasilczykbca8f942023-10-09 17:42:37 +00003290#ifndef BINDER_DISABLE_BLOB
Frederick Mayled3c595a2022-05-09 23:02:54 +00003291 for (size_t i = 0; i < kernelFields->mObjectsSize; i++) {
Steven Morelandc673f1f2021-10-07 18:23:35 -07003292 const flat_binder_object* flat =
Frederick Mayled3c595a2022-05-09 23:02:54 +00003293 reinterpret_cast<const flat_binder_object*>(mData + kernelFields->mObjects[i]);
Steven Morelandc673f1f2021-10-07 18:23:35 -07003294
3295 // cookie is compared against zero for historical reasons
3296 // > obj.cookie = takeOwnership ? 1 : 0;
3297 if (flat->hdr.type == BINDER_TYPE_FD && flat->cookie != 0 && ashmem_valid(flat->handle)) {
3298 int size = ashmem_get_size_region(flat->handle);
3299 if (__builtin_add_overflow(openAshmemSize, size, &openAshmemSize)) {
3300 ALOGE("Overflow when computing ashmem size.");
3301 return SIZE_MAX;
3302 }
3303 }
3304 }
Tomasz Wasilczykbca8f942023-10-09 17:42:37 +00003305#endif
Steven Morelandc673f1f2021-10-07 18:23:35 -07003306 return openAshmemSize;
Dan Sandleraa5c2342015-04-10 10:08:45 -04003307}
Andrei Homescua9cca9c2022-05-03 04:48:01 +00003308#endif // BINDER_WITH_KERNEL_IPC
Dan Sandleraa5c2342015-04-10 10:08:45 -04003309
Jeff Brown5707dbf2011-09-23 21:17:56 -07003310// --- Parcel::Blob ---
3311
3312Parcel::Blob::Blob() :
Yi Kong91635562018-06-07 14:38:36 -07003313 mFd(-1), mData(nullptr), mSize(0), mMutable(false) {
Jeff Brown5707dbf2011-09-23 21:17:56 -07003314}
3315
3316Parcel::Blob::~Blob() {
3317 release();
3318}
3319
3320void Parcel::Blob::release() {
Jeff Brown13b16042014-11-11 16:44:25 -08003321 if (mFd != -1 && mData) {
Mike McTernan733610f2024-08-27 11:48:30 +01003322 if (::munmap(mData, mSize) == -1) {
3323 ALOGW("munmap() failed: %s", strerror(errno));
3324 }
Jeff Brown5707dbf2011-09-23 21:17:56 -07003325 }
3326 clear();
3327}
3328
Jeff Brown13b16042014-11-11 16:44:25 -08003329void Parcel::Blob::init(int fd, void* data, size_t size, bool isMutable) {
3330 mFd = fd;
Jeff Brown5707dbf2011-09-23 21:17:56 -07003331 mData = data;
3332 mSize = size;
Jeff Brown13b16042014-11-11 16:44:25 -08003333 mMutable = isMutable;
Jeff Brown5707dbf2011-09-23 21:17:56 -07003334}
3335
3336void Parcel::Blob::clear() {
Jeff Brown13b16042014-11-11 16:44:25 -08003337 mFd = -1;
Yi Kong91635562018-06-07 14:38:36 -07003338 mData = nullptr;
Jeff Brown5707dbf2011-09-23 21:17:56 -07003339 mSize = 0;
Jeff Brown13b16042014-11-11 16:44:25 -08003340 mMutable = false;
Jeff Brown5707dbf2011-09-23 21:17:56 -07003341}
3342
Steven Moreland61ff8492019-09-26 16:05:45 -07003343} // namespace android