blob: 04fe0e7b501f17efe113dd8a4098359d157dfeab [file] [log] [blame]
Steven Moreland46e0da72019-09-05 15:52:02 -07001/*
2 * Copyright (C) 2019 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#define FUZZ_LOG_TAG "binder"
17
18#include "binder.h"
19#include "util.h"
20
Steven Moreland7c7c6482019-09-30 12:32:02 -070021#include <android/os/IServiceManager.h>
22
Steven Moreland46e0da72019-09-05 15:52:02 -070023using ::android::status_t;
24
Steven Moreland7c7c6482019-09-30 12:32:02 -070025class ExampleParcelable : public android::Parcelable {
26public:
27 status_t writeToParcel(android::Parcel* /*parcel*/) const override {
28 FUZZ_LOG() << "should not reach";
29 abort();
30 }
31 status_t readFromParcel(const android::Parcel* parcel) override {
32 mExampleExtraField++;
33 return parcel->readInt64(&(this->mExampleUsedData));
34 }
35private:
36 int64_t mExampleExtraField = 0;
37 int64_t mExampleUsedData = 0;
38};
39
Steven Moreland6d813932019-09-30 15:31:35 -070040struct ExampleFlattenable : public android::Flattenable<ExampleFlattenable> {
41public:
42 size_t getFlattenedSize() const { return sizeof(mValue); }
43 size_t getFdCount() const { return 0; }
44 status_t flatten(void*& /*buffer*/, size_t& /*size*/, int*& /*fds*/, size_t& /*count*/) const {
45 FUZZ_LOG() << "should not reach";
46 abort();
47 }
48 status_t unflatten(void const*& buffer, size_t& size, int const*& /*fds*/, size_t& /*count*/) {
49 if (size < sizeof(mValue)) {
50 return android::NO_MEMORY;
51 }
52 android::FlattenableUtils::read(buffer, size, mValue);
53 return android::OK;
54 }
55private:
56 int32_t mValue = 0xFEEDBEEF;
57};
58
59struct ExampleLightFlattenable : public android::LightFlattenablePod<ExampleLightFlattenable> {
60 int32_t mValue = 0;
61};
62
Steven Moreland46e0da72019-09-05 15:52:02 -070063#define PARCEL_READ_WITH_STATUS(T, FUN) \
64 [] (const ::android::Parcel& p, uint8_t /*data*/) {\
65 FUZZ_LOG() << "about to read " #T " using " #FUN " with status";\
66 T t{};\
67 status_t status = p.FUN(&t);\
68 FUZZ_LOG() << #T " status: " << status /* << " value: " << t*/;\
69 }
70
71#define PARCEL_READ_NO_STATUS(T, FUN) \
72 [] (const ::android::Parcel& p, uint8_t /*data*/) {\
73 FUZZ_LOG() << "about to read " #T " using " #FUN " with no status";\
74 T t = p.FUN();\
75 (void) t;\
76 FUZZ_LOG() << #T " done " /* << " value: " << t*/;\
77 }
78
79#define PARCEL_READ_OPT_STATUS(T, FUN) \
80 PARCEL_READ_WITH_STATUS(T, FUN), \
81 PARCEL_READ_NO_STATUS(T, FUN)
82
83std::vector<ParcelRead<::android::Parcel>> BINDER_PARCEL_READ_FUNCTIONS {
84 PARCEL_READ_NO_STATUS(size_t, dataSize),
85 PARCEL_READ_NO_STATUS(size_t, dataAvail),
86 PARCEL_READ_NO_STATUS(size_t, dataPosition),
87 PARCEL_READ_NO_STATUS(size_t, dataCapacity),
88 [] (const ::android::Parcel& p, uint8_t pos) {
89 FUZZ_LOG() << "about to setDataPosition: " << pos;
90 p.setDataPosition(pos);
91 FUZZ_LOG() << "setDataPosition done";
92 },
93 PARCEL_READ_NO_STATUS(size_t, allowFds),
94 PARCEL_READ_NO_STATUS(size_t, hasFileDescriptors),
95 [] (const ::android::Parcel& p, uint8_t len) {
Steven Moreland46e0da72019-09-05 15:52:02 -070096 std::string interface(len, 'a');
97 FUZZ_LOG() << "about to enforceInterface: " << interface;
98 bool b = p.enforceInterface(::android::String16(interface.c_str()));
99 FUZZ_LOG() << "enforced interface: " << b;
Steven Moreland46e0da72019-09-05 15:52:02 -0700100 },
101 [] (const ::android::Parcel& p, uint8_t /*len*/) {
Steven Moreland46e0da72019-09-05 15:52:02 -0700102 FUZZ_LOG() << "about to checkInterface";
Steven Moreland24bc0d12019-10-11 12:29:20 -0700103 android::sp<android::IBinder> aBinder = new android::BBinder();
104 bool b = p.checkInterface(aBinder.get());
Steven Moreland46e0da72019-09-05 15:52:02 -0700105 FUZZ_LOG() << "checked interface: " << b;
Steven Moreland46e0da72019-09-05 15:52:02 -0700106 },
107 PARCEL_READ_NO_STATUS(size_t, objectsCount),
108 PARCEL_READ_NO_STATUS(status_t, errorCheck),
109 [] (const ::android::Parcel& p, uint8_t len) {
110 FUZZ_LOG() << "about to read void*";
111 std::vector<uint8_t> data(len);
112 status_t status = p.read(data.data(), len);
113 FUZZ_LOG() << "read status: " << status;
114 },
115 [] (const ::android::Parcel& p, uint8_t len) {
116 FUZZ_LOG() << "about to readInplace";
117 const void* r = p.readInplace(len);
Steven Moreland6065c052019-09-30 18:22:44 -0700118 FUZZ_LOG() << "readInplace done. pointer: " << r << " bytes: " << hexString(r, len);
Steven Moreland46e0da72019-09-05 15:52:02 -0700119 },
120 PARCEL_READ_OPT_STATUS(int32_t, readInt32),
121 PARCEL_READ_OPT_STATUS(uint32_t, readUint32),
122 PARCEL_READ_OPT_STATUS(int64_t, readInt64),
123 PARCEL_READ_OPT_STATUS(uint64_t, readUint64),
124 PARCEL_READ_OPT_STATUS(float, readFloat),
125 PARCEL_READ_OPT_STATUS(double, readDouble),
126 PARCEL_READ_OPT_STATUS(intptr_t, readIntPtr),
127 PARCEL_READ_OPT_STATUS(bool, readBool),
128 PARCEL_READ_OPT_STATUS(char16_t, readChar),
129 PARCEL_READ_OPT_STATUS(int8_t, readByte),
130
131 PARCEL_READ_WITH_STATUS(std::string, readUtf8FromUtf16),
132 PARCEL_READ_WITH_STATUS(std::unique_ptr<std::string>, readUtf8FromUtf16),
133 [] (const ::android::Parcel& p, uint8_t /*data*/) {
134 FUZZ_LOG() << "about to read c-str";
135 const char* str = p.readCString();
136 FUZZ_LOG() << "read c-str: " << (str ? str : "<empty string>");
137 },
138 PARCEL_READ_OPT_STATUS(android::String8, readString8),
139 PARCEL_READ_OPT_STATUS(android::String16, readString16),
140 PARCEL_READ_WITH_STATUS(std::unique_ptr<android::String16>, readString16),
Steven Moreland7c7c6482019-09-30 12:32:02 -0700141 [] (const ::android::Parcel& p, uint8_t /*data*/) {
142 FUZZ_LOG() << "about to readString16Inplace";
143 size_t outLen = 0;
144 const char16_t* str = p.readString16Inplace(&outLen);
Steven Moreland6065c052019-09-30 18:22:44 -0700145 FUZZ_LOG() << "readString16Inplace: " << hexString(str, sizeof(char16_t) * outLen)
146 << " size: " << outLen;
Steven Moreland7c7c6482019-09-30 12:32:02 -0700147 },
Steven Moreland46e0da72019-09-05 15:52:02 -0700148 PARCEL_READ_WITH_STATUS(android::sp<android::IBinder>, readStrongBinder),
149 PARCEL_READ_WITH_STATUS(android::sp<android::IBinder>, readNullableStrongBinder),
150
Steven Moreland7c7c6482019-09-30 12:32:02 -0700151 // only reading one parcelable type for now
152 // TODO(b/131868573): can force read of arbitrarily sized vector
153 // PARCEL_READ_WITH_STATUS(std::unique_ptr<std::vector<std::unique_ptr<ExampleParcelable>>>, readParcelableVector),
154 // PARCEL_READ_WITH_STATUS(std::vector<ExampleParcelable>, readParcelableVector),
155 PARCEL_READ_WITH_STATUS(ExampleParcelable, readParcelable),
156 PARCEL_READ_WITH_STATUS(std::unique_ptr<ExampleParcelable>, readParcelable),
157
158 // only reading one binder type for now
159 PARCEL_READ_WITH_STATUS(android::sp<android::os::IServiceManager>, readStrongBinder),
160 PARCEL_READ_WITH_STATUS(android::sp<android::os::IServiceManager>, readNullableStrongBinder),
Steven Moreland46e0da72019-09-05 15:52:02 -0700161
162 // TODO(b/131868573): can force read of arbitrarily sized vector
163 // PARCEL_READ_WITH_STATUS(::std::unique_ptr<std::vector<android::sp<android::IBinder>>>, readStrongBinderVector),
164 // PARCEL_READ_WITH_STATUS(std::vector<android::sp<android::IBinder>>, readStrongBinderVector),
165
166 // TODO(b/131868573): can force read of arbitrarily sized vector
167 // PARCEL_READ_WITH_STATUS(std::unique_ptr<std::vector<int8_t>>, readByteVector),
168 // PARCEL_READ_WITH_STATUS(std::vector<int8_t>, readByteVector),
169 // PARCEL_READ_WITH_STATUS(std::unique_ptr<std::vector<uint8_t>>, readByteVector),
170 // PARCEL_READ_WITH_STATUS(std::vector<uint8_t>, readByteVector),
171 // PARCEL_READ_WITH_STATUS(std::unique_ptr<std::vector<int32_t>>, readInt32Vector),
172 // PARCEL_READ_WITH_STATUS(std::vector<int32_t>, readInt32Vector),
173 // PARCEL_READ_WITH_STATUS(std::unique_ptr<std::vector<int64_t>>, readInt64Vector),
174 // PARCEL_READ_WITH_STATUS(std::vector<int64_t>, readInt64Vector),
175 // PARCEL_READ_WITH_STATUS(std::unique_ptr<std::vector<uint64_t>>, readUint64Vector),
176 // PARCEL_READ_WITH_STATUS(std::vector<uint64_t>, readUint64Vector),
177 // PARCEL_READ_WITH_STATUS(std::unique_ptr<std::vector<float>>, readFloatVector),
178 // PARCEL_READ_WITH_STATUS(std::vector<float>, readFloatVector),
179 // PARCEL_READ_WITH_STATUS(std::unique_ptr<std::vector<double>>, readDoubleVector),
180 // PARCEL_READ_WITH_STATUS(std::vector<double>, readDoubleVector),
181 // PARCEL_READ_WITH_STATUS(std::unique_ptr<std::vector<bool>>, readBoolVector),
182 // PARCEL_READ_WITH_STATUS(std::vector<bool>, readBoolVector),
183 // PARCEL_READ_WITH_STATUS(std::unique_ptr<std::vector<char16_t>>, readCharVector),
184 // PARCEL_READ_WITH_STATUS(std::vector<char16_t>, readCharVector),
185 // PARCEL_READ_WITH_STATUS(std::unique_ptr<std::vector<std::unique_ptr<android::String16>>>, readString16Vector),
186 // PARCEL_READ_WITH_STATUS(std::vector<android::String16>, readString16Vector),
187 // PARCEL_READ_WITH_STATUS(std::unique_ptr<std::vector<std::unique_ptr<std::string>>>, readUtf8VectorFromUtf16Vector),
188 // PARCEL_READ_WITH_STATUS(std::vector<std::string>, readUtf8VectorFromUtf16Vector),
189
Steven Moreland6d813932019-09-30 15:31:35 -0700190 [] (const android::Parcel& p, uint8_t /*len*/) {
191 FUZZ_LOG() << "about to read flattenable";
192 ExampleFlattenable f;
193 status_t status = p.read(f);
194 FUZZ_LOG() << "read flattenable: " << status;
195 },
196 [] (const android::Parcel& p, uint8_t /*len*/) {
197 FUZZ_LOG() << "about to read lite flattenable";
198 ExampleLightFlattenable f;
199 status_t status = p.read(f);
200 FUZZ_LOG() << "read lite flattenable: " << status;
201 },
Steven Moreland7c7c6482019-09-30 12:32:02 -0700202
203 // TODO(b/131868573): can force read of arbitrarily sized vector
Steven Moreland46e0da72019-09-05 15:52:02 -0700204 // TODO: resizeOutVector
205
206 PARCEL_READ_NO_STATUS(int32_t, readExceptionCode),
Steven Moreland7c7c6482019-09-30 12:32:02 -0700207 [] (const android::Parcel& p, uint8_t /*len*/) {
208 FUZZ_LOG() << "about to readNativeHandle";
209 native_handle_t* t = p.readNativeHandle();
210 FUZZ_LOG() << "readNativeHandle: " << t;
211 if (t != nullptr) {
212 FUZZ_LOG() << "about to free readNativeHandle";
213 native_handle_close(t);
214 native_handle_delete(t);
215 FUZZ_LOG() << "readNativeHandle freed";
216 }
217 },
Steven Moreland46e0da72019-09-05 15:52:02 -0700218 PARCEL_READ_NO_STATUS(int, readFileDescriptor),
219 PARCEL_READ_NO_STATUS(int, readParcelFileDescriptor),
220 PARCEL_READ_WITH_STATUS(android::base::unique_fd, readUniqueFileDescriptor),
221
222 // TODO(b/131868573): can force read of arbitrarily sized vector
223 // PARCEL_READ_WITH_STATUS(std::unique_ptr<std::vector<android::base::unique_fd>>, readUniqueFileDescriptorVector),
224 // PARCEL_READ_WITH_STATUS(std::vector<android::base::unique_fd>, readUniqueFileDescriptorVector),
225
226 [] (const android::Parcel& p, uint8_t len) {
227 FUZZ_LOG() << "about to readBlob";
228 ::android::Parcel::ReadableBlob blob;
229 status_t status = p.readBlob(len, &blob);
230 FUZZ_LOG() << "readBlob status: " << status;
231 },
Steven Moreland7c7c6482019-09-30 12:32:02 -0700232 [] (const android::Parcel& p, uint8_t options) {
233 FUZZ_LOG() << "about to readObject";
234 bool nullMetaData = options & 0x1;
235 const void* obj = static_cast<const void*>(p.readObject(nullMetaData));
236 FUZZ_LOG() << "readObject: " << obj;
237 },
Steven Moreland46e0da72019-09-05 15:52:02 -0700238 PARCEL_READ_NO_STATUS(uid_t, readCallingWorkSourceUid),
239 PARCEL_READ_NO_STATUS(size_t, getBlobAshmemSize),
240 PARCEL_READ_NO_STATUS(size_t, getOpenAshmemSize),
241};