• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <iostream>
24 
25 #include <android-base/hex.h>
26 #include <android-base/logging.h>
27 #include <android/binder_auto_utils.h>
28 #include <android/binder_libbinder.h>
29 #include <fuzzbinder/random_parcel.h>
30 #include <fuzzer/FuzzedDataProvider.h>
31 
32 #include <cstdlib>
33 #include <ctime>
34 #include <sys/resource.h>
35 #include <sys/time.h>
36 
37 using android::fillRandomParcel;
38 using android::RandomParcelOptions;
39 using android::sp;
40 using android::base::HexString;
41 
fillRandomParcel(::android::hardware::Parcel * p,FuzzedDataProvider && provider,RandomParcelOptions * options)42 void fillRandomParcel(::android::hardware::Parcel* p, FuzzedDataProvider&& provider,
43                       RandomParcelOptions* options) {
44     // TODO: functionality to create random parcels for libhwbinder parcels
45     (void)options;
46 
47     std::vector<uint8_t> input = provider.ConsumeRemainingBytes<uint8_t>();
48     p->setData(input.data(), input.size());
49 }
fillRandomParcel(NdkParcelAdapter * p,FuzzedDataProvider && provider,RandomParcelOptions * options)50 static void fillRandomParcel(NdkParcelAdapter* p, FuzzedDataProvider&& provider,
51                              RandomParcelOptions* options) {
52     // fill underlying parcel using functions to fill random libbinder parcel
53     fillRandomParcel(p->parcel(), std::move(provider), options);
54 }
55 
56 template <typename P, typename B>
doTransactFuzz(const char * backend,const sp<B> & binder,FuzzedDataProvider && provider)57 void doTransactFuzz(const char* backend, const sp<B>& binder, FuzzedDataProvider&& provider) {
58     uint32_t code = provider.ConsumeIntegral<uint32_t>();
59     uint32_t flag = provider.ConsumeIntegral<uint32_t>();
60 
61     FUZZ_LOG() << "backend: " << backend;
62 
63     RandomParcelOptions options;
64 
65     P reply;
66     P data;
67     fillRandomParcel(&data, std::move(provider), &options);
68     (void)binder->transact(code, data, &reply, flag);
69 }
70 
71 template <typename P>
doReadFuzz(const char * backend,const std::vector<ParcelRead<P>> & reads,FuzzedDataProvider && provider)72 void doReadFuzz(const char* backend, const std::vector<ParcelRead<P>>& reads,
73                 FuzzedDataProvider&& provider) {
74     // Allow some majority of the bytes to be dedicated to telling us what to
75     // do. The fixed value added here represents that we want to test doing a
76     // lot of 'instructions' even on really short parcels.
77     size_t maxInstructions = 20 + (provider.remaining_bytes() * 2 / 3);
78     // but don't always use that many instructions. We want to allow the fuzzer
79     // to explore large parcels with few instructions if it wants to.
80     std::vector<uint8_t> instructions = provider.ConsumeBytes<uint8_t>(
81             provider.ConsumeIntegralInRange<size_t>(0, maxInstructions));
82 
83     RandomParcelOptions options;
84 
85     P p;
86     fillRandomParcel(&p, std::move(provider), &options);
87 
88     // since we are only using a byte to index
89     CHECK(reads.size() <= 255) << reads.size();
90 
91     FUZZ_LOG() << "backend: " << backend;
92     FUZZ_LOG() << "input: " << HexString(p.data(), p.dataSize());
93     FUZZ_LOG() << "instructions: " << HexString(instructions.data(), instructions.size());
94 
95     FuzzedDataProvider instructionsProvider(instructions.data(), instructions.size());
96     while (instructionsProvider.remaining_bytes() > 0) {
97         uint8_t idx = instructionsProvider.ConsumeIntegralInRange<uint8_t>(0, reads.size() - 1);
98 
99         FUZZ_LOG() << "Instruction " << idx << " avail: " << p.dataAvail()
100                    << " pos: " << p.dataPosition() << " cap: " << p.dataCapacity();
101 
102         reads[idx](p, instructionsProvider);
103     }
104 }
105 
106 // Append two random parcels.
107 template <typename P>
doAppendFuzz(const char * backend,FuzzedDataProvider && provider)108 void doAppendFuzz(const char* backend, FuzzedDataProvider&& provider) {
109     int32_t start = provider.ConsumeIntegral<int32_t>();
110     int32_t len = provider.ConsumeIntegral<int32_t>();
111 
112     std::vector<uint8_t> bytes = provider.ConsumeBytes<uint8_t>(
113             provider.ConsumeIntegralInRange<size_t>(0, provider.remaining_bytes()));
114 
115     // same options so that FDs and binders could be shared in both Parcels
116     RandomParcelOptions options;
117 
118     P p0, p1;
119     fillRandomParcel(&p0, FuzzedDataProvider(bytes.data(), bytes.size()), &options);
120     fillRandomParcel(&p1, std::move(provider), &options);
121 
122     FUZZ_LOG() << "backend: " << backend;
123     FUZZ_LOG() << "start: " << start << " len: " << len;
124 
125     p0.appendFrom(&p1, start, len);
126 }
127 
NothingClass_onCreate(void * args)128 void* NothingClass_onCreate(void* args) {
129     return args;
130 }
NothingClass_onDestroy(void *)131 void NothingClass_onDestroy(void* /*userData*/) {}
NothingClass_onTransact(AIBinder *,transaction_code_t,const AParcel *,AParcel *)132 binder_status_t NothingClass_onTransact(AIBinder*, transaction_code_t, const AParcel*, AParcel*) {
133     return STATUS_UNKNOWN_ERROR;
134 }
135 static AIBinder_Class* kNothingClass =
136         AIBinder_Class_define("nothing", NothingClass_onCreate, NothingClass_onDestroy,
137                               NothingClass_onTransact);
138 
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)139 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
140     if (size <= 1) return 0;  // no use
141 
142     // avoid timeouts, see b/142617274, b/142473153
143     if (size > 50000) return 0;
144 
145     FuzzedDataProvider provider = FuzzedDataProvider(data, size);
146 
147     const std::function<void(FuzzedDataProvider &&)> fuzzBackend[] = {
148             [](FuzzedDataProvider&& provider) {
149                 doTransactFuzz<
150                         ::android::hardware::Parcel>("hwbinder",
151                                                      sp<::android::hardware::BHwBinder>::make(),
152                                                      std::move(provider));
153             },
154             [](FuzzedDataProvider&& provider) {
155                 doTransactFuzz<::android::Parcel>("binder", sp<::android::BBinder>::make(),
156                                                   std::move(provider));
157             },
158             [](FuzzedDataProvider&& provider) {
159                 // fuzz from the libbinder layer since it's a superset of the
160                 // interface you get at the libbinder_ndk layer
161                 auto ndkBinder = ndk::SpAIBinder(AIBinder_new(kNothingClass, nullptr));
162                 auto binder = AIBinder_toPlatformBinder(ndkBinder.get());
163                 doTransactFuzz<::android::Parcel>("binder_ndk", binder, std::move(provider));
164             },
165             [](FuzzedDataProvider&& provider) {
166                 doReadFuzz<::android::hardware::Parcel>("hwbinder", HWBINDER_PARCEL_READ_FUNCTIONS,
167                                                         std::move(provider));
168             },
169             [](FuzzedDataProvider&& provider) {
170                 doReadFuzz<::android::Parcel>("binder", BINDER_PARCEL_READ_FUNCTIONS,
171                                               std::move(provider));
172             },
173             [](FuzzedDataProvider&& provider) {
174                 doReadFuzz<NdkParcelAdapter>("binder_ndk", BINDER_NDK_PARCEL_READ_FUNCTIONS,
175                                              std::move(provider));
176             },
177             [](FuzzedDataProvider&& provider) {
178                 doAppendFuzz<::android::Parcel>("binder", std::move(provider));
179             },
180             [](FuzzedDataProvider&& provider) {
181                 doAppendFuzz<NdkParcelAdapter>("binder_ndk", std::move(provider));
182             },
183     };
184 
185     provider.PickValueInArray(fuzzBackend)(std::move(provider));
186 
187     return 0;
188 }
189