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"
19 #include "binder_ndk.h"
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>
doFuzz(const std::vector<ParcelRead<P>> & reads,const std::vector<uint8_t> & input,const std::vector<uint8_t> & instructions)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 
37     // since we are only using a byte to index
38     CHECK(reads.size() <= 255) << reads.size();
39 
40     for (size_t i = 0; i < instructions.size() - 1; i += 2) {
41         uint8_t a = instructions[i];
42         uint8_t readIdx = a % reads.size();
43 
44         uint8_t b = instructions[i + 1];
45 
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();
51 
52         reads[readIdx](p, b);
53     }
54 }
55 
fuzz(uint8_t options,const std::vector<uint8_t> & input,const std::vector<uint8_t> & instructions)56 void fuzz(uint8_t options, const std::vector<uint8_t>& input, const std::vector<uint8_t>& instructions) {
57     uint8_t parcelType = options & 0x3;
58 
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     }
76 }
77 
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)78 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
79     if (size <= 1) return 0;  // no use
80 
81     // avoid timeouts, see b/142617274, b/142473153
82     if (size > 50000) return 0;
83 
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 }
111