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 #include <nfc_int.h>
18 #include <rw_int.h>
19 #include <dlfcn.h>
20 #include <unistd.h>
21 
22 #include "../includes/common.h"
23 #include "../includes/memutils.h"
24 
25 #define T3T_MSG_FELICALITE_MC_OFFSET 0x01
26 
27 char enable_selective_overload = ENABLE_NONE;
28 char *vulnPtr = nullptr;
29 
30 bool testInProgress = false;
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 
47 extern tRW_CB rw_cb;
48 extern tNFC_CB nfc_cb;
49 void rw_init(void);
50 tNFC_STATUS rw_t3t_select(uint8_t peer_nfcid2[NCI_RF_F_UID_LEN],
51                           uint8_t mrti_check, uint8_t mrti_update);
52 
53 static void (*real_GKI_freebuf)(void* ptr) = nullptr;
54 void *kAllocatedPointers[2];
55 int allocatedPointersIndex = -1;
56 bool kIsInitialized = false;
57 
init(void)58 void init(void) {
59     real_GKI_freebuf = (void (*)(void*))dlsym(RTLD_NEXT, "_Z11GKI_freebufPv");
60     if (!real_GKI_freebuf) {
61         return;
62     }
63 
64     kIsInitialized = true;
65 }
66 
GKI_freebuf(void * ptr)67 void GKI_freebuf(void* ptr) {
68     if (!kIsInitialized) {
69         init();
70     }
71     if (ptr == kAllocatedPointers[0] || ptr == kAllocatedPointers[1]) {
72         return;
73     } else {
74         real_GKI_freebuf(ptr);
75     }
76 }
77 
allocate_memory(size_t size)78 void *allocate_memory(size_t size) {
79     void *ptr = memalign(16, size);
80     memset(ptr, 0x0, size);
81     kAllocatedPointers[++allocatedPointersIndex] = ptr;
82     return ptr;
83 }
84 
85 // borrowed from rw_i93.cc
86 enum {
87     RW_T3T_CMD_DETECT_NDEF,
88     RW_T3T_CMD_CHECK_NDEF,
89     RW_T3T_CMD_UPDATE_NDEF,
90     RW_T3T_CMD_CHECK,
91     RW_T3T_CMD_UPDATE,
92     RW_T3T_CMD_SEND_RAW_FRAME,
93     RW_T3T_CMD_GET_SYSTEM_CODES,
94     RW_T3T_CMD_FORMAT,
95     RW_T3T_CMD_SET_READ_ONLY_SOFT,
96     RW_T3T_CMD_SET_READ_ONLY_HARD,
97 
98     RW_T3T_CMD_MAX
99 };
100 
101 // borrowed from rw_i93.cc
102 enum {
103     RW_T3T_STATE_NOT_ACTIVATED,
104     RW_T3T_STATE_IDLE,
105     RW_T3T_STATE_COMMAND_PENDING
106 };
107 
108 // borrowed from rw_i93.cc
109 enum {
110     /* Sub states for formatting Felica-Lite */
111     RW_T3T_FMT_SST_POLL_FELICA_LITE, /* Waiting for POLL Felica-Lite response (for
112      formatting) */
113     RW_T3T_FMT_SST_CHECK_MC_BLK, /* Waiting for Felica-Lite MC (MemoryControl)
114      block-read to complete */
115     RW_T3T_FMT_SST_UPDATE_MC_BLK, /* Waiting for Felica-Lite MC (MemoryControl)
116      block-write to complete */
117     RW_T3T_FMT_SST_UPDATE_NDEF_ATTRIB, /* Waiting for NDEF attribute block-write
118      to complete */
119 
120     /* Sub states for setting Felica-Lite read only */
121     RW_T3T_SRO_SST_POLL_FELICA_LITE, /* Waiting for POLL Felica-Lite response (for
122      setting read only) */
123     RW_T3T_SRO_SST_UPDATE_NDEF_ATTRIB, /* Waiting for NDEF attribute block-write
124      to complete */
125     RW_T3T_SRO_SST_CHECK_MC_BLK, /* Waiting for Felica-Lite MC (MemoryControl)
126      block-read to complete */
127     RW_T3T_SRO_SST_UPDATE_MC_BLK /* Waiting for Felica-Lite MC (MemoryControl)
128      block-write to complete */
129 };
130 
cback(tRW_EVENT event,tRW_DATA * p_data)131 void cback(tRW_EVENT event __attribute__((unused)),
132            tRW_DATA* p_data __attribute__((unused))) {
133 }
134 
main()135 int main() {
136     sigemptyset(&new_action.sa_mask);
137     new_action.sa_flags = SA_SIGINFO;
138     new_action.sa_sigaction = sigsegv_handler;
139     sigaction(SIGSEGV, &new_action, &old_action);
140     tRW_T3T_CB* p_t3t = &rw_cb.tcb.t3t;
141 
142     GKI_init();
143     rw_init();
144 
145     uint8_t peer_nfcid2[NCI_RF_F_UID_LEN];
146     uint8_t mrti_check = 1, mrti_update = 1;
147     enable_selective_overload = ENABLE_MEMALIGN_CHECK;
148     if (rw_t3t_select(peer_nfcid2, mrti_check, mrti_update) != NFC_STATUS_OK) {
149         return EXIT_FAILURE;
150     }
151 
152     tNFC_CONN *p_data = (tNFC_CONN *) allocate_memory(sizeof(tNFC_CONN));
153     if (!p_data) {
154         return EXIT_FAILURE;
155     }
156     p_data->data.p_data = (NFC_HDR *) allocate_memory(sizeof(NFC_HDR) * 4);
157     enable_selective_overload = ENABLE_FREE_CHECK | ENABLE_REALLOC_CHECK;
158     if (!(p_data->data.p_data)) {
159         free(p_data);
160         return EXIT_FAILURE;
161     }
162     vulnPtr = (char *)p_data->data.p_data;
163     p_data->status = NFC_STATUS_OK;
164 
165     p_t3t->cur_cmd = RW_T3T_CMD_SET_READ_ONLY_HARD;
166     p_t3t->rw_state = RW_T3T_STATE_COMMAND_PENDING;
167     p_t3t->rw_substate = RW_T3T_SRO_SST_CHECK_MC_BLK;
168 
169     NFC_HDR* p_msg = (p_data->data).p_data;
170     p_msg->len = T3T_MSG_RSP_COMMON_HDR_LEN;
171 
172     uint8_t* p_t3t_rsp = (uint8_t*) (p_msg + 1) + (p_msg->offset + 1);
173     p_t3t_rsp[T3T_MSG_RSP_OFFSET_RSPCODE] = T3T_MSG_OPC_CHECK_RSP;
174     p_t3t_rsp[T3T_MSG_RSP_OFFSET_STATUS1] = T3T_MSG_RSP_STATUS_OK;
175 
176     uint8_t* p_mc = &p_t3t_rsp[T3T_MSG_RSP_OFFSET_CHECK_DATA];
177     p_mc[T3T_MSG_FELICALITE_MC_OFFSET_SYS_OP] = T3T_MSG_FELICALITE_MC_OFFSET;
178 
179     tNFC_CONN_CB* p_cb = &nfc_cb.conn_cb[NFC_RF_CONN_ID];
180     tNFC_CONN_EVT event = NFC_DATA_CEVT;
181     TIMER_LIST_ENT pFirst = { };
182     nfc_cb.quick_timer_queue.p_first = &pFirst;
183 
184     rw_cb.p_cback = &cback;
185     testInProgress = true;
186     p_cb->p_cback(0, event, p_data);
187     testInProgress = false;
188 
189     free(p_data->data.p_data);
190     free(p_data);
191 
192     return EXIT_SUCCESS;
193 }
194