From d3b91bb73e21912d7bf7d2b25b4cef44f6062fbb Mon Sep 17 00:00:00 2001 From: ftheirs Date: Wed, 10 Jan 2024 12:26:32 -0300 Subject: [PATCH] improvements --- app/script_s2.ld | 170 ----------------------------------------- app/src/apdu_handler.c | 10 +-- app/src/parser_impl.h | 4 +- app/src/tx_display.c | 19 +++-- app/src/tx_parser.c | 17 +++-- 5 files changed, 26 insertions(+), 194 deletions(-) delete mode 100644 app/script_s2.ld diff --git a/app/script_s2.ld b/app/script_s2.ld deleted file mode 100644 index db3150b3..00000000 --- a/app/script_s2.ld +++ /dev/null @@ -1,170 +0,0 @@ -/******************************************************************************* -* Ledger Blue - Secure firmware -* (c) 2016, 2017, 2018, 2019 Ledger -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -********************************************************************************/ - -/** - * Global chip memory layout and constants - * - */ - -MEMORY -{ - DISCARD (rwx) : ORIGIN = 0xd0000000, LENGTH = 1M - - FLASH (rx) : ORIGIN = 0xc0de0000, LENGTH = 400K - DATA (r) : ORIGIN = 0xc0de0000, LENGTH = 400K - SRAM (rwx) : ORIGIN = 0xda7a0000, LENGTH = 30K -} - -PAGE_SIZE = 512; -STACK_SIZE = 8192; -END_STACK = ORIGIN(SRAM) + LENGTH(SRAM); - -ENTRY(main); - -SECTIONS -{ - /****************************************************************/ - /* This section locates the code in FLASH */ - /****************************************************************/ - - /** put text in Flash memory, VMA will be equal to LMA */ - .text : - { - /* provide start code symbol, shall be zero */ - _text = .; - _nvram_start = .; - - /* ensure main is always @ 0xC0D00000 */ - *(.boot*) - - /* place the other code and rodata defined BUT nvram variables that are displaced in a r/w area */ - *(.text*) - *(.rodata) - *(.rodata.[^N]*) /*.data.rel.ro* not here to detect invalid PIC usage */ - *(.rodata.N[^_]*) - - . = ALIGN(4); - - /* all code placed */ - _etext = .; - - . = ALIGN(PAGE_SIZE); - - _nvram_data = .; - - /* NVM data (ex-filesystem) */ - *(.bss.N_* .rodata.N_*) - - . = ALIGN(PAGE_SIZE); - _envram_data = .; - - _install_parameters = .; - _nvram_end = .; - } > FLASH = 0x00 - - .data (NOLOAD): - { - . = ALIGN(4); - - /** - * Place RAM initialized variables - */ - _data = .; - - *(vtable) - *(.data*) - - _edata = .; - - } > DISCARD /*> SRAM AT>FLASH = 0x00 */ - - .bss : - { - /** - * Place RAM uninitialized variables - */ - _bss = .; - *(.bss*) - _ebss = .; - - - /** - * Reserve stack size - */ - . = ALIGN(4); - app_stack_canary = .; - PROVIDE(app_stack_canary = .); - . += 4; - _stack_validation = .; - . = _stack_validation + STACK_SIZE; - _stack = ABSOLUTE(END_STACK) - STACK_SIZE; - PROVIDE( _stack = ABSOLUTE(END_STACK) - STACK_SIZE); - _estack = ABSOLUTE(END_STACK); - PROVIDE( _estack = ABSOLUTE(END_STACK) ); - - } > SRAM = 0x00 - - /****************************************************************/ - /* DEBUG */ - /****************************************************************/ - - /* remove the debugging information from the standard libraries */ - DEBUG (NOLOAD) : - { - libc.a ( * ) - libm.a ( * ) - libgcc.a ( * ) - *(.ARM.exidx* .gnu.linkonce.armexidx.*) - } > DISCARD - - /* Stabs debugging sections. */ - .stab 0 : { *(.stab) } - .stabstr 0 : { *(.stabstr) } - .stab.excl 0 : { *(.stab.excl) } - .stab.exclstr 0 : { *(.stab.exclstr) } - .stab.index 0 : { *(.stab.index) } - .stab.indexstr 0 : { *(.stab.indexstr) } - .comment 0 : { *(.comment) } - /* DWARF debug sections. - Symbols in the DWARF debugging sections are relative to the beginning - of the section so we begin them at 0. */ - /* DWARF 1 */ - .debug 0 : { *(.debug) } - .line 0 : { *(.line) } - /* GNU DWARF 1 extensions */ - .debug_srcinfo 0 : { *(.debug_srcinfo) } - .debug_sfnames 0 : { *(.debug_sfnames) } - /* DWARF 1.1 and DWARF 2 */ - .debug_aranges 0 : { *(.debug_aranges) } - .debug_pubnames 0 : { *(.debug_pubnames) } - /* DWARF 2 */ - .debug_info 0 : { *(.debug_info .gnu.linkonce.wi.*) } - .debug_abbrev 0 : { *(.debug_abbrev) } - .debug_line 0 : { *(.debug_line) } - .debug_frame 0 : { *(.debug_frame) } - .debug_str 0 : { *(.debug_str) } - .debug_loc 0 : { *(.debug_loc) } - .debug_macinfo 0 : { *(.debug_macinfo) } - /* SGI/MIPS DWARF 2 extensions */ - .debug_weaknames 0 : { *(.debug_weaknames) } - .debug_funcnames 0 : { *(.debug_funcnames) } - .debug_typenames 0 : { *(.debug_typenames) } - .debug_varnames 0 : { *(.debug_varnames) } -} - -PROVIDE(_nvram = ABSOLUTE(_nvram_start)); -PROVIDE(_envram = ABSOLUTE(_nvram_end)); \ No newline at end of file diff --git a/app/src/apdu_handler.c b/app/src/apdu_handler.c index a7ed6dc5..18ca7d10 100644 --- a/app/src/apdu_handler.c +++ b/app/src/apdu_handler.c @@ -203,18 +203,10 @@ __Z_INLINE void handleSign(volatile uint32_t *flags, volatile uint32_t *tx, uint THROW(APDU_CODE_DATA_INVALID); } - // Put address in output buffer, we will use it to confirm source address - zxerr_t zxerr = app_fill_address(); - if (zxerr != zxerr_ok) { - *tx = 0; - THROW(APDU_CODE_DATA_INVALID); - } - parser_tx_obj.tx_json.own_addr = (const char *) (G_io_apdu_buffer + VIEW_ADDRESS_OFFSET_SECP256K1); const char *error_msg = tx_parse(sign_type); - if (error_msg != NULL) { - int error_msg_length = strlen(error_msg); + const int error_msg_length = strnlen(error_msg, sizeof(G_io_apdu_buffer)); MEMCPY(G_io_apdu_buffer, error_msg, error_msg_length); *tx += (error_msg_length); THROW(APDU_CODE_DATA_INVALID); diff --git a/app/src/parser_impl.h b/app/src/parser_impl.h index 53d91cef..1af7a28f 100644 --- a/app/src/parser_impl.h +++ b/app/src/parser_impl.h @@ -33,8 +33,8 @@ typedef struct { } parser_context_t; typedef struct { - char str1[50]; - char str2[50]; + const char *str1; + const char *str2; } key_subst_t; typedef struct { diff --git a/app/src/tx_display.c b/app/src/tx_display.c index 208d436d..9df16143 100644 --- a/app/src/tx_display.c +++ b/app/src/tx_display.c @@ -323,7 +323,7 @@ __Z_INLINE parser_error_t is_default_chainid(bool *is_default) { if (is_default == NULL) { return parser_unexpected_value; } - + CHECK_PARSER_ERR(tx_indexRootFields()) *is_default = display_cache.is_default_chain; @@ -355,7 +355,7 @@ __Z_INLINE parser_error_t get_subitem_count(root_item_e root_item, uint8_t *num_ int32_t tmp_num_items = display_cache.root_item_number_subitems[root_item]; bool is_expert_or_default = false; - + switch (root_item) { case root_item_chain_id: case root_item_sequence: @@ -550,9 +550,18 @@ parser_error_t tx_display_make_friendly() { // post process keys for (size_t i = 0; i < array_length(key_substitutions); i++) { - if (!strncmp(parser_tx_obj.tx_json.query.out_key, key_substitutions[i].str1, strlen(key_substitutions[i].str1))) { - strncpy_s(parser_tx_obj.tx_json.query.out_key, key_substitutions[i].str2, parser_tx_obj.tx_json.query.out_key_len); - break; + const char* str1 = (const char*) PIC(key_substitutions[i].str1); + const char* str2 = (const char*) PIC(key_substitutions[i].str2); + const uint16_t str1Len = strlen(str1); + const uint16_t str2Len = strlen(str2); + + + const uint16_t outKeyLen = strnlen(parser_tx_obj.tx_json.query.out_key, parser_tx_obj.tx_json.query.out_key_len); + if ((outKeyLen == str1Len && strncmp(parser_tx_obj.tx_json.query.out_key, str1, str1Len) == 0) + && parser_tx_obj.tx_json.query.out_key_len >= str2Len) { + MEMZERO(parser_tx_obj.tx_json.query.out_key, parser_tx_obj.tx_json.query.out_key_len); + MEMCPY(parser_tx_obj.tx_json.query.out_key, str2, str2Len); + break; } } diff --git a/app/src/tx_parser.c b/app/src/tx_parser.c index f8ded002..43296511 100644 --- a/app/src/tx_parser.c +++ b/app/src/tx_parser.c @@ -84,28 +84,29 @@ parser_error_t tx_getToken(uint16_t token_index, const char *inValue = parser_tx_obj.tx_json.tx + token_start; uint16_t inLen = token_end - token_start; + // empty strings are considered the first page *pageCount = 1; if (inLen > 0) { for (uint32_t i = 0; i < array_length(value_substitutions); i++) { - const char *substStr = value_substitutions[i].str1; - const size_t substStrLen = strlen(substStr); - if (inLen == substStrLen && !MEMCMP(inValue, substStr, substStrLen)) { - inValue = value_substitutions[i].str2; - inLen = strlen(value_substitutions[i].str2); + const char* str1 = (const char*) PIC(value_substitutions[i].str1); + const char* str2 = (const char*) PIC(value_substitutions[i].str2); + const uint16_t str1Len = strlen(str1); + const uint16_t str2Len = strlen(str2); + + if (inLen == str1Len && strncmp(inValue, str1, str1Len) == 0) { + inValue = str2; + inLen = str2Len; //Extra Depth level for Multisend type extraDepthLevel = false; if (strstr(inValue, "Multi") != NULL) { extraDepthLevel = true; } - break; } } - pageStringExt(out_val, out_val_len, inValue, inLen, pageIdx, pageCount); - } if (pageIdx >= *pageCount) {