Skip to content

Commit f399876

Browse files
author
Jamie C. Driver
committed
messaging: remove fixed output buffer from 'reply_to_message_result()'
Remove 'MAX_STANDARD_OUTPUT_MSG_SIZE' buffer from stack. Have the caller pass a buffer appropriately sized for cbor serialisation into this call, consistent with the other calls for sending replies/messages. Place large buffers onto the heap.
1 parent edb60f0 commit f399876

22 files changed

+100
-47
lines changed

main/process.c

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -541,29 +541,33 @@ void cbor_result_uint64_cb(const void* ctx, CborEncoder* container)
541541
JADE_ASSERT(cberr == CborNoError);
542542
}
543543

544-
void jade_process_reply_to_message_result(const cbor_msg_t ctx, const void* cbctx, cbor_encoder_fn_t cb)
544+
void jade_process_reply_to_message_result(
545+
const cbor_msg_t ctx, uint8_t* output, size_t output_size, const void* cbctx, cbor_encoder_fn_t cb)
545546
{
546547
JADE_ASSERT(cb);
548+
JADE_ASSERT(output);
549+
JADE_ASSERT(output_size);
547550

548551
char id[MAXLEN_ID + 1];
549552
size_t written = 0;
550553
rpc_get_id(&ctx.value, id, sizeof(id), &written);
551554
JADE_ASSERT(written != 0);
552555

553-
uint8_t buf[MAX_STANDARD_OUTPUT_MSG_SIZE];
554-
jade_process_reply_to_message_result_with_id(id, buf, sizeof(buf), ctx.source, cbctx, cb);
556+
jade_process_reply_to_message_result_with_id(id, output, output_size, ctx.source, cbctx, cb);
555557
}
556558

557559
void jade_process_reply_to_message_ok(jade_process_t* process)
558560
{
561+
uint8_t buf[64];
559562
const bool ok = true;
560-
jade_process_reply_to_message_result(process->ctx, &ok, cbor_result_boolean_cb);
563+
jade_process_reply_to_message_result(process->ctx, buf, sizeof(buf), &ok, cbor_result_boolean_cb);
561564
}
562565

563566
void jade_process_reply_to_message_fail(jade_process_t* process)
564567
{
568+
uint8_t buf[64];
565569
const bool ok = false;
566-
jade_process_reply_to_message_result(process->ctx, &ok, cbor_result_boolean_cb);
570+
jade_process_reply_to_message_result(process->ctx, buf, sizeof(buf), &ok, cbor_result_boolean_cb);
567571
}
568572

569573
void jade_process_reject_message_with_id(const char* id, int code, const char* message, const uint8_t* data,

main/process.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,8 @@ void jade_process_push_out_message(const uint8_t* data, size_t length, jade_msg_
9595
// Send message replies
9696
void jade_process_reply_to_message_result_with_id(const char* id, uint8_t* output, size_t output_size,
9797
jade_msg_source_t source, const void* cbctx, cbor_encoder_fn_t cb);
98-
void jade_process_reply_to_message_result(const cbor_msg_t ctx, const void* cbctx, cbor_encoder_fn_t cb);
98+
void jade_process_reply_to_message_result(
99+
const cbor_msg_t ctx, uint8_t* output, size_t output_size, const void* cbctx, cbor_encoder_fn_t cb);
99100
void jade_process_reply_to_message_ok(jade_process_t* process);
100101
void jade_process_reply_to_message_fail(jade_process_t* process);
101102
void jade_process_reply_to_message_ex(jade_msg_source_t source, const uint8_t* reply_payload, size_t payload_len);

main/process/dashboard.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,10 @@ void set_request_change_pin(bool change_pin);
250250
static void process_get_version_info_request(jade_process_t* process)
251251
{
252252
ASSERT_CURRENT_MESSAGE(process, "get_version_info");
253-
jade_process_reply_to_message_result(process->ctx, &process->ctx.source, build_version_info_reply);
253+
254+
uint8_t buf[1024];
255+
jade_process_reply_to_message_result(
256+
process->ctx, buf, sizeof(buf), &process->ctx.source, build_version_info_reply);
254257
}
255258

256259
// If the user has successfully authenticated over a given connection interface,
@@ -515,7 +518,10 @@ static void dispatch_message(jade_process_t* process)
515518
if (debug_selfcheck(process)) {
516519
const TickType_t end_time = xTaskGetTickCount();
517520
const uint64_t elapsed_time_ms = (end_time - start_time) * portTICK_PERIOD_MS;
518-
jade_process_reply_to_message_result(process->ctx, &elapsed_time_ms, cbor_result_uint64_cb);
521+
522+
uint8_t buf[64];
523+
jade_process_reply_to_message_result(
524+
process->ctx, buf, sizeof(buf), &elapsed_time_ms, cbor_result_uint64_cb);
519525
} else {
520526
jade_process_reject_message(process, CBOR_RPC_INTERNAL_ERROR, "ERROR", NULL);
521527
}

main/process/debug_scan_qr.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,8 +185,9 @@ void debug_scan_qr_process(void* process_ptr)
185185
}
186186

187187
// Reply with the decoded data (empty if failed)
188+
uint8_t buf[512]; // sufficient for all existing test cases
188189
const bytes_info_t bytes_info = { .data = qr_data.data, .size = qr_data.len };
189-
jade_process_reply_to_message_result(process->ctx, &bytes_info, cbor_result_bytes_cb);
190+
jade_process_reply_to_message_result(process->ctx, buf, sizeof(buf), &bytes_info, cbor_result_bytes_cb);
190191
JADE_LOGI("Success");
191192

192193
cleanup:

main/process/get_bip85_entropy.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,8 @@ void get_bip85_bip39_entropy_process(void* process_ptr)
302302
}
303303

304304
// Reply with the encrypted bip85 entropy reply
305-
jade_process_reply_to_message_result(process->ctx, &bip85_data, reply_bip85_data);
305+
uint8_t buf[256];
306+
jade_process_reply_to_message_result(process->ctx, buf, sizeof(buf), &bip85_data, reply_bip85_data);
306307
JADE_LOGI("Success");
307308

308309
cleanup:
@@ -328,7 +329,8 @@ void get_bip85_rsa_entropy_process(void* process_ptr)
328329
}
329330

330331
// Reply with the encrypted bip85 entropy reply
331-
jade_process_reply_to_message_result(process->ctx, &bip85_data, reply_bip85_data);
332+
uint8_t buf[256];
333+
jade_process_reply_to_message_result(process->ctx, buf, sizeof(buf), &bip85_data, reply_bip85_data);
332334
JADE_LOGI("Success");
333335

334336
cleanup:

main/process/get_bip85_pubkey.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,15 @@ void get_bip85_pubkey_process(void* process_ptr)
2929

3030
display_processing_message_activity();
3131

32-
char pubkey_pem[1024];
32+
char pubkey_pem[896];
3333
if (!rsa_get_bip85_pubkey_pem(key_bits, index, pubkey_pem, sizeof(pubkey_pem))) {
3434
jade_process_reject_message(process, CBOR_RPC_INTERNAL_ERROR, "Failed to generate RSA key", NULL);
3535
goto cleanup;
3636
}
3737

3838
// Reply with the pubkey pem
39-
jade_process_reply_to_message_result(process->ctx, pubkey_pem, cbor_result_string_cb);
39+
uint8_t buf[1024];
40+
jade_process_reply_to_message_result(process->ctx, buf, sizeof(buf), pubkey_pem, cbor_result_string_cb);
4041
JADE_LOGI("Success");
4142

4243
cleanup:

main/process/get_commitments.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,9 @@ void get_commitments_process(void* process_ptr)
128128
}
129129

130130
commitments.content = COMMITMENTS_ABF | COMMITMENTS_VBF | COMMITMENTS_INCLUDES_COMMITMENTS;
131-
jade_process_reply_to_message_result(process->ctx, &commitments, reply_commitments);
131+
132+
uint8_t buf[320];
133+
jade_process_reply_to_message_result(process->ctx, buf, sizeof(buf), &commitments, reply_commitments);
132134

133135
JADE_LOGI("Success");
134136

main/process/get_otp_code.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,8 @@ void get_otp_code_process(void* process_ptr)
9494
}
9595
JADE_LOGD("User pressed accept");
9696

97-
jade_process_reply_to_message_result(process->ctx, token, cbor_result_string_cb);
97+
uint8_t buf[64];
98+
jade_process_reply_to_message_result(process->ctx, buf, sizeof(buf), token, cbor_result_string_cb);
9899

99100
JADE_LOGI("Success");
100101

main/process/get_receive_address.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,8 @@ void get_receive_address_process(void* process_ptr)
267267
}
268268

269269
// Reply with the address
270-
jade_process_reply_to_message_result(process->ctx, address, cbor_result_string_cb);
270+
uint8_t buf[256];
271+
jade_process_reply_to_message_result(process->ctx, buf, sizeof(buf), address, cbor_result_string_cb);
271272

272273
JADE_LOGI("Success");
273274

main/process/get_registered_descriptor.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,12 @@ void get_registered_descriptor_process(void* process_ptr)
9595
}
9696

9797
// Reply with this info
98+
const size_t buflen = 1024 + (128 * descriptor_data.num_values);
99+
uint8_t* const buf = JADE_MALLOC(buflen);
98100
const descriptor_details_t descriptor_details
99101
= { .descriptor_name = descriptor_name, .descriptor_data = &descriptor_data };
100-
jade_process_reply_to_message_result(process->ctx, &descriptor_details, reply_registered_descriptor);
102+
jade_process_reply_to_message_result(process->ctx, buf, buflen, &descriptor_details, reply_registered_descriptor);
103+
free(buf);
101104

102105
JADE_LOGI("Success");
103106

0 commit comments

Comments
 (0)