| 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 |  | 
|  | 23 | #include <android-base/logging.h> | 
|  | 24 |  | 
|  | 25 | #include <cstdlib> | 
|  | 26 | #include <ctime> | 
|  | 27 |  | 
|  | 28 | template <typename P> | 
|  | 29 | void doFuzz( | 
|  | 30 | const std::vector<ParcelRead<P>>& reads, | 
|  | 31 | const std::vector<uint8_t>& input, | 
|  | 32 | const std::vector<uint8_t>& instructions) { | 
|  | 33 |  | 
|  | 34 | P p; | 
|  | 35 | p.setData(input.data(), input.size()); | 
|  | 36 |  | 
| Steven Moreland | 7eac78a | 2019-10-11 18:46:24 -0700 | [diff] [blame] | 37 | // since we are only using a byte to index | 
|  | 38 | CHECK(reads.size() <= 255) << reads.size(); | 
|  | 39 |  | 
| Steven Moreland | 46e0da7 | 2019-09-05 15:52:02 -0700 | [diff] [blame] | 40 | for (size_t i = 0; i < instructions.size() - 1; i += 2) { | 
|  | 41 | uint8_t a = instructions[i]; | 
| Steven Moreland | dc449dc | 2019-10-10 10:06:58 -0700 | [diff] [blame] | 42 | uint8_t readIdx = a % reads.size(); | 
|  | 43 |  | 
| Steven Moreland | 46e0da7 | 2019-09-05 15:52:02 -0700 | [diff] [blame] | 44 | uint8_t b = instructions[i + 1]; | 
|  | 45 |  | 
| Steven Moreland | dc449dc | 2019-10-10 10:06:58 -0700 | [diff] [blame] | 46 | FUZZ_LOG() << "Instruction: " << (i / 2) + 1 << "/" << instructions.size() / 2 | 
|  | 47 | << " cmd: " << static_cast<size_t>(a) << " (" << static_cast<size_t>(readIdx) | 
|  | 48 | << ") arg: " << static_cast<size_t>(b) << " size: " << p.dataSize() | 
|  | 49 | << " avail: " << p.dataAvail() << " pos: " << p.dataPosition() | 
|  | 50 | << " cap: " << p.dataCapacity(); | 
| Steven Moreland | 46e0da7 | 2019-09-05 15:52:02 -0700 | [diff] [blame] | 51 |  | 
| Steven Moreland | dc449dc | 2019-10-10 10:06:58 -0700 | [diff] [blame] | 52 | reads[readIdx](p, b); | 
| Steven Moreland | 46e0da7 | 2019-09-05 15:52:02 -0700 | [diff] [blame] | 53 | } | 
|  | 54 | } | 
|  | 55 |  | 
|  | 56 | void fuzz(uint8_t options, const std::vector<uint8_t>& input, const std::vector<uint8_t>& instructions) { | 
| Steven Moreland | ea9ed86 | 2019-10-10 12:43:29 -0700 | [diff] [blame] | 57 | uint8_t parcelType = options & 0x3; | 
| Steven Moreland | 46e0da7 | 2019-09-05 15:52:02 -0700 | [diff] [blame] | 58 |  | 
| Steven Moreland | ea9ed86 | 2019-10-10 12:43:29 -0700 | [diff] [blame] | 59 | switch (parcelType) { | 
|  | 60 | case 0x0: | 
|  | 61 | doFuzz<::android::hardware::Parcel>(HWBINDER_PARCEL_READ_FUNCTIONS, input, | 
|  | 62 | instructions); | 
|  | 63 | break; | 
|  | 64 | case 0x1: | 
|  | 65 | doFuzz<::android::Parcel>(BINDER_PARCEL_READ_FUNCTIONS, input, instructions); | 
|  | 66 | break; | 
|  | 67 | case 0x2: | 
|  | 68 | doFuzz<NdkParcelAdapter>(BINDER_NDK_PARCEL_READ_FUNCTIONS, input, instructions); | 
|  | 69 | break; | 
|  | 70 | case 0x3: | 
|  | 71 | /*reserved for future use*/ | 
|  | 72 | break; | 
|  | 73 | default: | 
|  | 74 | LOG_ALWAYS_FATAL("unknown parcel type %d", static_cast<int>(parcelType)); | 
|  | 75 | } | 
| Steven Moreland | 46e0da7 | 2019-09-05 15:52:02 -0700 | [diff] [blame] | 76 | } | 
|  | 77 |  | 
|  | 78 | extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { | 
|  | 79 | if (size <= 1) return 0;  // no use | 
| Steven Moreland | 9894741 | 2019-10-15 10:36:05 -0700 | [diff] [blame] | 80 |  | 
|  | 81 | // avoid timeouts, see b/142617274, b/142473153 | 
|  | 82 | if (size > 50000) return 0; | 
|  | 83 |  | 
| Steven Moreland | 46e0da7 | 2019-09-05 15:52:02 -0700 | [diff] [blame] | 84 | uint8_t options = *data; | 
|  | 85 | data++; | 
|  | 86 | size--; | 
|  | 87 |  | 
|  | 88 | // TODO: generate 'objects' data | 
|  | 89 |  | 
|  | 90 | // data to fill out parcel | 
|  | 91 | size_t inputLen = size / 2; | 
|  | 92 | std::vector<uint8_t> input(data, data + inputLen); | 
|  | 93 | data += inputLen; | 
|  | 94 | size -= inputLen; | 
|  | 95 |  | 
|  | 96 | // data to use to determine what to do | 
|  | 97 | size_t instructionLen = size; | 
|  | 98 | std::vector<uint8_t> instructions(data, data + instructionLen); | 
|  | 99 | data += instructionLen; | 
|  | 100 | size -= instructionLen; | 
|  | 101 |  | 
|  | 102 | CHECK(size == 0) << "size: " << size; | 
|  | 103 |  | 
|  | 104 | FUZZ_LOG() << "options: " << (int)options << " inputLen: " << inputLen << " instructionLen: " << instructionLen; | 
|  | 105 | FUZZ_LOG() << "input: " << hexString(input); | 
|  | 106 | FUZZ_LOG() << "instructions: " << hexString(instructions); | 
|  | 107 |  | 
|  | 108 | fuzz(options, input, instructions); | 
|  | 109 | return 0; | 
|  | 110 | } |