| 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 "main" | 
|  | 17 |  | 
|  | 18 | #include "binder.h" | 
| Steven Moreland | 28f8142 | 2019-10-03 10:40:59 -0700 | [diff] [blame] | 19 | #include "binder_ndk.h" | 
| Steven Moreland | 46e0da7 | 2019-09-05 15:52:02 -0700 | [diff] [blame] | 20 | #include "hwbinder.h" | 
|  | 21 | #include "util.h" | 
|  | 22 |  | 
| Steven Moreland | 139514d | 2020-12-08 01:12:26 +0000 | [diff] [blame] | 23 | #include <iostream> | 
|  | 24 |  | 
| Steven Moreland | 46e0da7 | 2019-09-05 15:52:02 -0700 | [diff] [blame] | 25 | #include <android-base/logging.h> | 
| Steven Moreland | 362e4da | 2020-10-16 19:49:39 +0000 | [diff] [blame] | 26 | #include <fuzzbinder/random_parcel.h> | 
| Steven Moreland | 842b593 | 2020-10-08 00:23:35 +0000 | [diff] [blame] | 27 | #include <fuzzer/FuzzedDataProvider.h> | 
| Steven Moreland | 46e0da7 | 2019-09-05 15:52:02 -0700 | [diff] [blame] | 28 |  | 
|  | 29 | #include <cstdlib> | 
|  | 30 | #include <ctime> | 
| Steven Moreland | 139514d | 2020-12-08 01:12:26 +0000 | [diff] [blame] | 31 | #include <sys/resource.h> | 
|  | 32 | #include <sys/time.h> | 
| Steven Moreland | 46e0da7 | 2019-09-05 15:52:02 -0700 | [diff] [blame] | 33 |  | 
| Steven Moreland | 5e561af | 2020-10-08 02:16:03 +0000 | [diff] [blame] | 34 | using android::fillRandomParcel; | 
|  | 35 |  | 
|  | 36 | void fillRandomParcel(::android::hardware::Parcel* p, FuzzedDataProvider&& provider) { | 
| Steven Moreland | 362e4da | 2020-10-16 19:49:39 +0000 | [diff] [blame] | 37 | // TODO: functionality to create random parcels for libhwbinder parcels | 
| Steven Moreland | 5e561af | 2020-10-08 02:16:03 +0000 | [diff] [blame] | 38 | std::vector<uint8_t> input = provider.ConsumeRemainingBytes<uint8_t>(); | 
|  | 39 | p->setData(input.data(), input.size()); | 
|  | 40 | } | 
| Steven Moreland | 362e4da | 2020-10-16 19:49:39 +0000 | [diff] [blame] | 41 | static void fillRandomParcel(NdkParcelAdapter* p, FuzzedDataProvider&& provider) { | 
|  | 42 | // fill underlying parcel using functions to fill random libbinder parcel | 
|  | 43 | fillRandomParcel(p->parcel(), std::move(provider)); | 
|  | 44 | } | 
| Steven Moreland | 5e561af | 2020-10-08 02:16:03 +0000 | [diff] [blame] | 45 |  | 
| Steven Moreland | 46e0da7 | 2019-09-05 15:52:02 -0700 | [diff] [blame] | 46 | template <typename P> | 
| Steven Moreland | 842b593 | 2020-10-08 00:23:35 +0000 | [diff] [blame] | 47 | void doFuzz(const char* backend, const std::vector<ParcelRead<P>>& reads, | 
| Steven Moreland | 5e561af | 2020-10-08 02:16:03 +0000 | [diff] [blame] | 48 | FuzzedDataProvider&& provider) { | 
| Steven Moreland | 842b593 | 2020-10-08 00:23:35 +0000 | [diff] [blame] | 49 | // Allow some majority of the bytes to be dedicated to telling us what to | 
|  | 50 | // do. The fixed value added here represents that we want to test doing a | 
|  | 51 | // lot of 'instructions' even on really short parcels. | 
| Steven Moreland | 5e561af | 2020-10-08 02:16:03 +0000 | [diff] [blame] | 52 | size_t maxInstructions = 20 + (provider.remaining_bytes() * 2 / 3); | 
| Steven Moreland | 842b593 | 2020-10-08 00:23:35 +0000 | [diff] [blame] | 53 | // but don't always use that many instructions. We want to allow the fuzzer | 
|  | 54 | // to explore large parcels with few instructions if it wants to. | 
| Steven Moreland | 5e561af | 2020-10-08 02:16:03 +0000 | [diff] [blame] | 55 | std::vector<uint8_t> instructions = provider.ConsumeBytes<uint8_t>( | 
|  | 56 | provider.ConsumeIntegralInRange<size_t>(0, maxInstructions)); | 
| Steven Moreland | 46e0da7 | 2019-09-05 15:52:02 -0700 | [diff] [blame] | 57 |  | 
|  | 58 | P p; | 
| Steven Moreland | 5e561af | 2020-10-08 02:16:03 +0000 | [diff] [blame] | 59 | fillRandomParcel(&p, std::move(provider)); | 
| Steven Moreland | 46e0da7 | 2019-09-05 15:52:02 -0700 | [diff] [blame] | 60 |  | 
| Steven Moreland | 7eac78a | 2019-10-11 18:46:24 -0700 | [diff] [blame] | 61 | // since we are only using a byte to index | 
|  | 62 | CHECK(reads.size() <= 255) << reads.size(); | 
|  | 63 |  | 
| Steven Moreland | 842b593 | 2020-10-08 00:23:35 +0000 | [diff] [blame] | 64 | FUZZ_LOG() << "backend: " << backend; | 
| Steven Moreland | 5e561af | 2020-10-08 02:16:03 +0000 | [diff] [blame] | 65 | FUZZ_LOG() << "input: " << hexString(p.data(), p.dataSize()); | 
| Steven Moreland | 842b593 | 2020-10-08 00:23:35 +0000 | [diff] [blame] | 66 | FUZZ_LOG() << "instructions: " << hexString(instructions); | 
|  | 67 |  | 
|  | 68 | for (size_t i = 0; i + 1 < instructions.size(); i += 2) { | 
| Steven Moreland | 46e0da7 | 2019-09-05 15:52:02 -0700 | [diff] [blame] | 69 | uint8_t a = instructions[i]; | 
| Steven Moreland | dc449dc | 2019-10-10 10:06:58 -0700 | [diff] [blame] | 70 | uint8_t readIdx = a % reads.size(); | 
|  | 71 |  | 
| Steven Moreland | 46e0da7 | 2019-09-05 15:52:02 -0700 | [diff] [blame] | 72 | uint8_t b = instructions[i + 1]; | 
|  | 73 |  | 
| Steven Moreland | dc449dc | 2019-10-10 10:06:58 -0700 | [diff] [blame] | 74 | FUZZ_LOG() << "Instruction: " << (i / 2) + 1 << "/" << instructions.size() / 2 | 
|  | 75 | << " cmd: " << static_cast<size_t>(a) << " (" << static_cast<size_t>(readIdx) | 
|  | 76 | << ") arg: " << static_cast<size_t>(b) << " size: " << p.dataSize() | 
|  | 77 | << " avail: " << p.dataAvail() << " pos: " << p.dataPosition() | 
|  | 78 | << " cap: " << p.dataCapacity(); | 
| Steven Moreland | 46e0da7 | 2019-09-05 15:52:02 -0700 | [diff] [blame] | 79 |  | 
| Steven Moreland | dc449dc | 2019-10-10 10:06:58 -0700 | [diff] [blame] | 80 | reads[readIdx](p, b); | 
| Steven Moreland | 46e0da7 | 2019-09-05 15:52:02 -0700 | [diff] [blame] | 81 | } | 
|  | 82 | } | 
|  | 83 |  | 
| Steven Moreland | 139514d | 2020-12-08 01:12:26 +0000 | [diff] [blame] | 84 | size_t getHardMemoryLimit() { | 
|  | 85 | struct rlimit limit; | 
|  | 86 | CHECK(0 == getrlimit(RLIMIT_AS, &limit)) << errno; | 
|  | 87 | return limit.rlim_max; | 
|  | 88 | } | 
|  | 89 |  | 
|  | 90 | void setMemoryLimit(size_t cur, size_t max) { | 
|  | 91 | const struct rlimit kLimit = { | 
|  | 92 | .rlim_cur = cur, | 
|  | 93 | .rlim_max = max, | 
|  | 94 | }; | 
|  | 95 | CHECK(0 == setrlimit(RLIMIT_AS, &kLimit)) << errno; | 
|  | 96 | } | 
|  | 97 |  | 
| Steven Moreland | 46e0da7 | 2019-09-05 15:52:02 -0700 | [diff] [blame] | 98 | extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { | 
| Steven Moreland | 139514d | 2020-12-08 01:12:26 +0000 | [diff] [blame] | 99 | static constexpr size_t kMemLimit = 1 * 1024 * 1024; | 
|  | 100 | size_t hardLimit = getHardMemoryLimit(); | 
|  | 101 | setMemoryLimit(std::min(kMemLimit, hardLimit), hardLimit); | 
|  | 102 |  | 
| Steven Moreland | 46e0da7 | 2019-09-05 15:52:02 -0700 | [diff] [blame] | 103 | if (size <= 1) return 0;  // no use | 
| Steven Moreland | 9894741 | 2019-10-15 10:36:05 -0700 | [diff] [blame] | 104 |  | 
|  | 105 | // avoid timeouts, see b/142617274, b/142473153 | 
|  | 106 | if (size > 50000) return 0; | 
|  | 107 |  | 
| Steven Moreland | 842b593 | 2020-10-08 00:23:35 +0000 | [diff] [blame] | 108 | FuzzedDataProvider provider = FuzzedDataProvider(data, size); | 
| Steven Moreland | 46e0da7 | 2019-09-05 15:52:02 -0700 | [diff] [blame] | 109 |  | 
| Steven Moreland | 5e561af | 2020-10-08 02:16:03 +0000 | [diff] [blame] | 110 | const std::function<void(FuzzedDataProvider &&)> fuzzBackend[3] = { | 
|  | 111 | [](FuzzedDataProvider&& provider) { | 
| Steven Moreland | 842b593 | 2020-10-08 00:23:35 +0000 | [diff] [blame] | 112 | doFuzz<::android::hardware::Parcel>("hwbinder", HWBINDER_PARCEL_READ_FUNCTIONS, | 
| Steven Moreland | 5e561af | 2020-10-08 02:16:03 +0000 | [diff] [blame] | 113 | std::move(provider)); | 
| Steven Moreland | 842b593 | 2020-10-08 00:23:35 +0000 | [diff] [blame] | 114 | }, | 
| Steven Moreland | 5e561af | 2020-10-08 02:16:03 +0000 | [diff] [blame] | 115 | [](FuzzedDataProvider&& provider) { | 
|  | 116 | doFuzz<::android::Parcel>("binder", BINDER_PARCEL_READ_FUNCTIONS, | 
|  | 117 | std::move(provider)); | 
| Steven Moreland | 842b593 | 2020-10-08 00:23:35 +0000 | [diff] [blame] | 118 | }, | 
| Steven Moreland | 5e561af | 2020-10-08 02:16:03 +0000 | [diff] [blame] | 119 | [](FuzzedDataProvider&& provider) { | 
|  | 120 | doFuzz<NdkParcelAdapter>("binder_ndk", BINDER_NDK_PARCEL_READ_FUNCTIONS, | 
|  | 121 | std::move(provider)); | 
| Steven Moreland | 842b593 | 2020-10-08 00:23:35 +0000 | [diff] [blame] | 122 | }, | 
|  | 123 | }; | 
| Steven Moreland | 46e0da7 | 2019-09-05 15:52:02 -0700 | [diff] [blame] | 124 |  | 
| Steven Moreland | 5e561af | 2020-10-08 02:16:03 +0000 | [diff] [blame] | 125 | provider.PickValueInArray(fuzzBackend)(std::move(provider)); | 
| Steven Moreland | 46e0da7 | 2019-09-05 15:52:02 -0700 | [diff] [blame] | 126 |  | 
| Steven Moreland | 139514d | 2020-12-08 01:12:26 +0000 | [diff] [blame] | 127 | setMemoryLimit(hardLimit, hardLimit); | 
|  | 128 |  | 
| Steven Moreland | 46e0da7 | 2019-09-05 15:52:02 -0700 | [diff] [blame] | 129 | return 0; | 
|  | 130 | } |