Skip to content

Commit 2cb2075

Browse files
fix: fuzzing issues
1 parent dc227cc commit 2cb2075

9 files changed

Lines changed: 54 additions & 13 deletions

File tree

src/features/generic_tx_parser/gtp_field.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,10 @@ static bool handle_param_constraint(const s_tlv_data *data, s_field_ctx *context
124124
PRINTF("Error: CONSTRAINT present but VISIBLE is not MUST_BE or IF_NOT_IN!\n");
125125
return false;
126126
}
127+
if (data->length == 0 || data->value == NULL) {
128+
PRINTF("Error: Empty constraint value!\n");
129+
return false;
130+
}
127131
// Allocate new constraint node
128132
s_field_constraint *node = NULL;
129133
if (mem_buffer_allocate((void **) &node, sizeof(s_field_constraint)) == false) {

src/plugins/erc1155/erc1155_ui.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,11 @@ static void set_batch_transfer_ui(ethQueryContractUI_t *msg, erc1155_context_t *
134134
void handle_query_contract_ui_1155(ethQueryContractUI_t *msg) {
135135
erc1155_context_t *context = (erc1155_context_t *) msg->pluginContext;
136136

137+
if (msg->item1 == NULL) {
138+
msg->result = ETH_PLUGIN_RESULT_ERROR;
139+
return;
140+
}
141+
137142
msg->result = ETH_PLUGIN_RESULT_OK;
138143
switch (context->selectorIndex) {
139144
case SET_APPROVAL_FOR_ALL:

src/plugins/eth2/eth2_plugin.c

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -103,10 +103,7 @@ void eth2_plugin_call(eth_plugin_msg_t message, void *parameters) {
103103

104104
case 4 + (32 * 5): // deposit pubkey 1
105105
{
106-
// Copy the first 32 bytes.
107-
memcpy(context->deposit_address,
108-
msg->parameter,
109-
sizeof(context->deposit_address));
106+
memcpy(context->deposit_address, msg->parameter, PARAMETER_LENGTH);
110107
msg->result = ETH_PLUGIN_RESULT_OK;
111108
break;
112109
}
@@ -141,7 +138,7 @@ void eth2_plugin_call(eth_plugin_msg_t message, void *parameters) {
141138

142139
case 4 + (32 * 8): // withdrawal credentials
143140
{
144-
uint8_t tmp[48];
141+
uint8_t tmp[48] = {0};
145142
uint32_t withdrawalKeyPath[4];
146143
withdrawalKeyPath[0] = WITHDRAWAL_KEY_PATH_1;
147144
withdrawalKeyPath[1] = WITHDRAWAL_KEY_PATH_2;

tests/fuzzing/harness/fuzz_eip7702.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
#include <setjmp.h>
12
#include "fuzz_utils.h"
3+
#include "mocks.h"
24

35
int fuzzEIP7702(const uint8_t *data, size_t size) {
46
size_t offset = 0;
@@ -17,11 +19,10 @@ int fuzzEIP7702(const uint8_t *data, size_t size) {
1719
return 0;
1820
}
1921

20-
/* Main fuzzing handler called by libfuzzer */
2122
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
2223
init_fuzzing_environment();
24+
if (sigsetjmp(fuzz_exit_jump_ctx.jmp_buf, 1)) return 0;
2325

24-
// Run the harness
2526
fuzzEIP7702(data, size);
2627

2728
return 0;

tests/fuzzing/harness/fuzz_plugin_swap_calldata.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,12 @@
77
* - G_swap_crosschain_hash must point to a valid hash for comparison
88
*/
99

10+
#include <setjmp.h>
1011
#include "fuzz_utils.h"
12+
#include "mocks.h"
1113
#include "shared_context.h"
1214
#include "swap_lib_calls.h"
1315

14-
// Forward declaration of the plugin call function
1516
void swap_with_calldata_plugin_call(eth_plugin_msg_t message, void *parameters);
1617

1718
// Buffer sizes
@@ -110,6 +111,7 @@ static int fuzz_swap_calldata_plugin(const uint8_t *data, size_t size) {
110111

111112
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
112113
init_fuzzing_environment();
114+
if (sigsetjmp(fuzz_exit_jump_ctx.jmp_buf, 1)) return 0;
113115
fuzz_swap_calldata_plugin(data, size);
114116
return 0;
115117
}

tests/fuzzing/harness/fuzz_proxy.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
1+
#include <setjmp.h>
12
#include "fuzz_utils.h"
3+
#include "mocks.h"
24

35
int fuzzProxyInfo(const uint8_t *data, size_t size) {
46
if (size < 1) return 0;
57
return handle_proxy_info(data[0], 0, size - 1, data + 1);
68
}
79

8-
/* Main fuzzing handler called by libfuzzer */
910
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
1011
init_fuzzing_environment();
12+
if (sigsetjmp(fuzz_exit_jump_ctx.jmp_buf, 1)) return 0;
1113

12-
// Run the harness
1314
fuzzProxyInfo(data, size);
1415

1516
return 0;

tests/fuzzing/harness/fuzz_safe.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
#include <setjmp.h>
12
#include "fuzz_utils.h"
3+
#include "mocks.h"
24

35
#include "safe_descriptor.h"
46

@@ -21,13 +23,12 @@ int fuzzSignerCmd(const uint8_t *data, size_t size) {
2123
return 0;
2224
}
2325

24-
/* Main fuzzing handler called by libfuzzer */
2526
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
2627
uint8_t target;
2728
init_fuzzing_environment();
2829
SAFE_DESC = NULL;
30+
if (sigsetjmp(fuzz_exit_jump_ctx.jmp_buf, 1)) return 0;
2931

30-
// Determine which harness function to call based on the first byte of data
3132
if (size < 1) return 0;
3233
target = data[0];
3334
data++;

tests/fuzzing/mock/mock.c

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,40 @@
11
#include <string.h>
22
#include <stdlib.h>
3+
#include <setjmp.h>
34

45
#include "cx_errors.h"
56
#include "cx_sha256.h"
67
#include "cx_sha3.h"
78
#include "buffer.h"
89
#include "lcx_ecfp.h"
910
#include "mem_alloc.h"
11+
#include "exceptions.h"
12+
#include "os_task.h"
1013

1114
#include "bip32_utils.h"
1215

16+
try_context_t fuzz_exit_jump_ctx = {0};
17+
try_context_t *G_exception_context = &fuzz_exit_jump_ctx;
18+
19+
try_context_t *try_context_get(void) {
20+
return G_exception_context;
21+
}
22+
23+
try_context_t *try_context_set(try_context_t *context) {
24+
try_context_t *previous = G_exception_context;
25+
G_exception_context = context;
26+
return previous;
27+
}
28+
29+
void __attribute__((noreturn))
30+
os_sched_exit(bolos_task_status_t exit_code __attribute__((unused))) {
31+
longjmp(fuzz_exit_jump_ctx.jmp_buf, 1);
32+
}
33+
34+
void __attribute__((noreturn)) os_lib_end(void) {
35+
longjmp(fuzz_exit_jump_ctx.jmp_buf, 1);
36+
}
37+
1338
/** MemorySanitizer does not wrap explicit_bzero https://github.com/google/sanitizers/issues/1507
1439
* which results in false positives when running MemorySanitizer.
1540
*/
@@ -55,7 +80,6 @@ void mem_free(mem_ctx_t ctx, void *ptr) {
5580
free(ptr);
5681
}
5782

58-
// APPs expect a specific length
5983
cx_err_t cx_ecdomain_parameters_length(cx_curve_t cv, size_t *length) {
6084
(void) cv;
6185
*length = (size_t) 32;

tests/fuzzing/mock/mocks.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#pragma once
2+
3+
#include <setjmp.h>
4+
#include "exceptions.h"
5+
6+
extern try_context_t fuzz_exit_jump_ctx;

0 commit comments

Comments
 (0)