1 /*
2  * Copyright (C) 2021 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 
17 /* Following files are taken as reference to come up with this PoC */
18 /* 1. 'system/nfc/src/fuzzers/fuzz_utils.cc'                       */
19 /* 2. 'system/nfc/src/fuzzers/rw/main.cc'                          */
20 
21 #include <vector>
22 #include "../includes/common.h"
23 #include "fuzz.h"
24 #include "fuzz_cmn.h"
25 extern void Type4_Fuzz(uint8_t SubType, const std::vector<bytes_t> &Packets);
26 
27 FILE *file = nullptr;
28 char *vulnPtr = nullptr;
29 bool testInProgress = false;
30 
31 struct sigaction new_action, old_action;
sigsegv_handler(int signum,siginfo_t * info,void * context)32 void sigsegv_handler(int signum, siginfo_t *info, void *context) {
33     if (testInProgress && info->si_signo == SIGSEGV) {
34         size_t pageSize = getpagesize();
35         if (pageSize) {
36             char *vulnPtrGuardPage = (char *)((size_t)vulnPtr & PAGE_MASK) + pageSize;
37             char *faultPage = (char *)((size_t)info->si_addr & PAGE_MASK);
38             if (faultPage == vulnPtrGuardPage) {
39                 (*old_action.sa_sigaction)(signum, info, context);
40                 return;
41             }
42         }
43     }
44     _exit(EXIT_FAILURE);
45 }
46 
exit_Handler(void)47 void exit_Handler(void) {
48     if (file) {
49         fclose(file);
50     }
51 }
52 
UnpackPackets(const uint8_t * Data,size_t Size)53 static std::vector<bytes_t> UnpackPackets(const uint8_t *Data, size_t Size) {
54     std::vector<bytes_t> result;
55     while (Size > 0) {
56         auto s = *Data++;
57         Size--;
58 
59         if (s > Size) {
60             s = Size;
61         }
62 
63         if (s > 0) {
64             result.push_back(bytes_t(Data, Data + s));
65         }
66 
67         Size -= s;
68         Data += s;
69     }
70 
71     return result;
72 }
73 
main(int argc,char ** argv)74 int main(int argc, char **argv) {
75     sigemptyset(&new_action.sa_mask);
76     new_action.sa_flags = SA_SIGINFO;
77     new_action.sa_sigaction = sigsegv_handler;
78     sigaction(SIGSEGV, &new_action, &old_action);
79     FAIL_CHECK(argc > 1);
80 
81     file = fopen(argv[1], "r");
82     FAIL_CHECK(file);
83 
84     fseek(file, 0, SEEK_END);
85     size_t size = ftell(file);
86     fseek(file, 0, SEEK_SET);
87 
88     std::vector<uint8_t> bufVector(size);
89     FAIL_CHECK(bufVector.data());
90     FAIL_CHECK(fread(bufVector.data(), 1, size, file) == size);
91 
92     const uint8_t *data = (const uint8_t *)bufVector.data();
93     auto Packets = UnpackPackets(data, size);
94     FAIL_CHECK(Packets.size() > 1);
95 
96     auto &ctrl = Packets[0];
97     FAIL_CHECK(ctrl.size() > 1);
98 
99     Type4_Fuzz(ctrl[1], Packets);
100 
101     return EXIT_SUCCESS;
102 }
103