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