Skip to content

Commit 483ab98

Browse files
test: fuzz 1.16.0 new features
1 parent 9a13852 commit 483ab98

3 files changed

Lines changed: 85 additions & 2 deletions

File tree

tests/fuzzing/CMakeLists.txt

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,12 @@ if (NOT DEFINED BOLOS_SDK)
2424
set(BOLOS_SDK /opt/${TARGET_DEVICE}-secure-sdk)
2525
endif()
2626

27+
# some flags to mimic the embedded build (such as packed enums)
28+
set(CUSTOM_C_FLAGS -fdata-sections -ffunction-sections -funsigned-char -fshort-enums)
29+
2730
# compatible with ClusterFuzzLite
2831
if (NOT DEFINED ENV{LIB_FUZZING_ENGINE})
29-
set(COMPILATION_FLAGS -g -O0 -Wall -Wextra -fprofile-instr-generate -fcoverage-mapping)
32+
set(COMPILATION_FLAGS ${CUSTOM_C_FLAGS} -g -O0 -Wall -Wextra -fprofile-instr-generate -fcoverage-mapping)
3033
if (SANITIZER MATCHES "address")
3134
set(COMPILATION_FLAGS ${COMPILATION_FLAGS} -fsanitize=fuzzer,address,undefined)
3235
elseif (SANITIZER MATCHES "memory")
@@ -35,7 +38,7 @@ if (NOT DEFINED ENV{LIB_FUZZING_ENGINE})
3538
message(FATAL_ERROR "Unkown sanitizer type. It must be set to `address` or `memory`.")
3639
endif()
3740
else()
38-
set(COMPILATION_FLAGS "$ENV{LIB_FUZZING_ENGINE} $ENV{CFLAGS}")
41+
set(COMPILATION_FLAGS "$ENV{LIB_FUZZING_ENGINE} $ENV{CFLAGS} ${CUSTOM_C_FLAGS}")
3942
separate_arguments(COMPILATION_FLAGS)
4043
endif()
4144

@@ -155,6 +158,7 @@ set(DEFINES
155158
HAVE_ENUM_VALUE
156159
HAVE_NFT_SUPPORT
157160
HAVE_DYNAMIC_NETWORKS
161+
HAVE_WEB3_CHECKS
158162
explicit_bzero=bzero # Fix for https://github.com/google/sanitizers/issues/1507
159163
)
160164
@@ -185,6 +189,7 @@ include_directories(
185189
${CMAKE_SOURCE_DIR}/../../src_features/getChallenge/
186190
${CMAKE_SOURCE_DIR}/../../src_features/signMessageEIP712/
187191
${CMAKE_SOURCE_DIR}/../../src_features/provide_proxy_info/
192+
${CMAKE_SOURCE_DIR}/../../src_features/provide_tx_simulation/
188193
${BOLOS_SDK}/include
189194
${BOLOS_SDK}/target/${TARGET_DEVICE}/include
190195
${BOLOS_SDK}/lib_cxng/include
@@ -203,6 +208,7 @@ FILE(GLOB_RECURSE SOURCES
203208
${CMAKE_SOURCE_DIR}/../../src_features/provide_network_info/*.c
204209
${CMAKE_SOURCE_DIR}/../../src_features/provideNFTInformation/*.c
205210
${CMAKE_SOURCE_DIR}/../../src_features/provide_proxy_info/*.c
211+
${CMAKE_SOURCE_DIR}/../../src_features/provide_tx_simulation/*.c
206212
${CMAKE_SOURCE_DIR}/../../src/mem.c
207213
${CMAKE_SOURCE_DIR}/../../src/mem_utils.c
208214
${CMAKE_SOURCE_DIR}/../../src/network.c

tests/fuzzing/src/fuzzer.c

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55

66
#include "cmd_network_info.h"
77

8+
#include "cmd_get_tx_simulation.h"
9+
10+
#include "cmd_proxy_info.h"
11+
812
#include "cmd_field.h"
913
#include "cmd_tx_info.h"
1014
#include "cmd_enum_value.h"
@@ -15,6 +19,7 @@
1519

1620
#include "shared_context.h"
1721
#include "tlv.h"
22+
#include "mem.h"
1823
#include "apdu_constants.h"
1924

2025
// Fuzzing harness interface
@@ -34,6 +39,7 @@ const chain_config_t *chainConfig = &config;
3439
uint8_t appState;
3540
tmpCtx_t tmpCtx;
3641
strings_t strings;
42+
const internalStorage_t N_storage_real = {.w3c_enable = true, .w3c_opt_in = true};
3743

3844
int fuzzGenericParserFieldCmd(const uint8_t *data, size_t size) {
3945
s_field field = {0};
@@ -105,6 +111,55 @@ int fuzzNFTInfo(const uint8_t *data, size_t size) {
105111
return handleProvideNFTInformation(data, size, &tx) != APDU_RESPONSE_OK;
106112
}
107113

114+
int fuzzProxyInfo(const uint8_t *data, size_t size) {
115+
if (size < 1) return 0;
116+
return handle_proxy_info(data[0], 0, size - 1, data + 1);
117+
}
118+
119+
int fuzzTxSimulation(const uint8_t *data, size_t size) {
120+
unsigned int flags;
121+
if (size < 2) return 0;
122+
123+
if (handleTxSimulation(data[0], data[1], data + 2, size - 2, &flags) != APDU_RESPONSE_OK)
124+
return 1;
125+
126+
getTxSimuRiskStr();
127+
getTxSimuCategoryStr();
128+
return 0;
129+
}
130+
131+
int fuzzCalldata(const uint8_t *data, size_t size) {
132+
calldata_cleanup();
133+
while (size > 0) {
134+
switch (data[0]) {
135+
case 'I':
136+
data++;
137+
size--;
138+
calldata_init(500);
139+
break;
140+
case 'W':
141+
size--;
142+
data++;
143+
if (size < 1 || size < data[0] + 1) return 0;
144+
calldata_append(data + 1, data[0]);
145+
size -= (1 + data[0]);
146+
data += 1 + data[0];
147+
break;
148+
case 'R':
149+
size--;
150+
data++;
151+
if (size < 1) return 0;
152+
calldata_get_chunk(data[0]);
153+
size--;
154+
data++;
155+
break;
156+
default:
157+
return 0;
158+
}
159+
}
160+
return 0;
161+
}
162+
108163
// Array of fuzzing harness functions
109164
harness harnesses[] = {
110165
fuzzGenericParserFieldCmd,
@@ -113,6 +168,9 @@ harness harnesses[] = {
113168
fuzzDynamicNetworks,
114169
fuzzTrustedNames,
115170
fuzzNFTInfo,
171+
fuzzProxyInfo,
172+
fuzzTxSimulation,
173+
fuzzCalldata,
116174
};
117175

118176
/* Main fuzzing handler called by libfuzzer */
@@ -126,6 +184,9 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
126184
explicit_bzero(&G_io_apdu_buffer, 260);
127185
explicit_bzero(&sha3, sizeof(sha3));
128186

187+
calldata_cleanup();
188+
mem_reset();
189+
129190
uint8_t target;
130191

131192
txContext.content = &txContent;

tests/fuzzing/src/mock.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include "cx_sha256.h"
66
#include "cx_sha3.h"
77
#include "buffer.h"
8+
#include "lcx_ecfp.h"
89

910
/** MemorySanitizer does not wrap explicit_bzero https://github.com/google/sanitizers/issues/1507
1011
* which results in false positives when running MemorySanitizer.
@@ -181,3 +182,18 @@ int io_send_response_buffers(const buffer_t *rdatalist, size_t count, uint16_t s
181182
UNUSED(sw);
182183
return 0;
183184
}
185+
186+
uint16_t io_seproxyhal_send_status(uint16_t sw, uint32_t tx, bool reset, bool idle) {
187+
return 0;
188+
}
189+
190+
uint32_t os_pki_get_info(uint8_t *key_usage,
191+
uint8_t *trusted_name,
192+
size_t *trusted_name_len,
193+
cx_ecfp_384_public_key_t *public_key) {
194+
memcpy(trusted_name, "trusted name", sizeof("trusted name"));
195+
return 0;
196+
}
197+
198+
void ui_tx_simulation_opt_in(bool response_expected) {
199+
}

0 commit comments

Comments
 (0)