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