From 63a0befe23332dcda3e47ed5f05a7a8120faa415 Mon Sep 17 00:00:00 2001 From: Alexandre Paillier Date: Mon, 5 Feb 2024 10:21:54 +0100 Subject: [PATCH] Improve error-handling of chain ID when parsing APDUs --- .../cmd_provideTokenInfo.c | 12 ++++++++---- .../provideNFTInformation/cmd_provideNFTInfo.c | 4 +++- src_features/setPlugin/cmd_setPlugin.c | 4 +++- 3 files changed, 14 insertions(+), 6 deletions(-) diff --git a/src_features/provideErc20TokenInformation/cmd_provideTokenInfo.c b/src_features/provideErc20TokenInformation/cmd_provideTokenInfo.c index 5fd546bb44..fc8f69f9d1 100644 --- a/src_features/provideErc20TokenInformation/cmd_provideTokenInfo.c +++ b/src_features/provideErc20TokenInformation/cmd_provideTokenInfo.c @@ -3,6 +3,7 @@ #include "public_keys.h" #include "common_ui.h" #include "os_io_seproxyhal.h" +#include "network.h" #ifdef HAVE_CONTRACT_NAME_IN_DESCRIPTOR @@ -111,7 +112,7 @@ void handleProvideErc20TokenInformation(uint8_t p1, UNUSED(tx); uint32_t offset = 0; uint8_t tickerLength; - uint32_t chainId; + uint64_t chain_id; uint8_t hash[INT256_LENGTH]; cx_ecfp_public_key_t tokenKey; @@ -141,12 +142,15 @@ void handleProvideErc20TokenInformation(uint8_t p1, memmove(token->address, workBuffer + offset, 20); offset += 20; dataLength -= 20; + // TODO: Handle 64-bit long chain IDs token->decimals = U4BE(workBuffer, offset); offset += 4; dataLength -= 4; - chainId = U4BE(workBuffer, offset); - if ((chainConfig->chainId != ETHEREUM_MAINNET_CHAINID) && (chainConfig->chainId != chainId)) { - PRINTF("ChainId token mismatch: %d vs %d\n", chainConfig->chainId, chainId); + chain_id = U4BE(workBuffer, offset); + if ((chainConfig->chainId != chain_id) && + (!chain_is_ethereum_compatible(&chainConfig->chainId) || + !chain_is_ethereum_compatible(&chain_id))) { + PRINTF("ChainId token mismatch: %d vs %d\n", chainConfig->chainId, chain_id); THROW(0x6A80); } offset += 4; diff --git a/src_features/provideNFTInformation/cmd_provideNFTInfo.c b/src_features/provideNFTInformation/cmd_provideNFTInfo.c index cde32159d6..5040f8773f 100644 --- a/src_features/provideNFTInformation/cmd_provideNFTInfo.c +++ b/src_features/provideNFTInformation/cmd_provideNFTInfo.c @@ -130,7 +130,9 @@ void handleProvideNFTInformation(uint8_t p1, // this prints raw data, so to have a more meaningful print, display // the buffer before the endianness swap PRINTF("ChainID: %.*H\n", sizeof(chain_id), (workBuffer + offset)); - if (!chain_is_ethereum_compatible(&chain_id)) { + if ((chainConfig->chainId != chain_id) && + (!chain_is_ethereum_compatible(&chainConfig->chainId) || + !chain_is_ethereum_compatible(&chain_id))) { PRINTF("Unsupported chain ID!\n"); THROW(APDU_RESPONSE_INVALID_DATA); } diff --git a/src_features/setPlugin/cmd_setPlugin.c b/src_features/setPlugin/cmd_setPlugin.c index 8c4a67e187..feffbe3067 100644 --- a/src_features/setPlugin/cmd_setPlugin.c +++ b/src_features/setPlugin/cmd_setPlugin.c @@ -159,7 +159,9 @@ void handleSetPlugin(uint8_t p1, // this prints raw data, so to have a more meaningful print, display // the buffer before the endianness swap PRINTF("ChainID: %.*H\n", sizeof(chain_id), (workBuffer + offset)); - if (!chain_is_ethereum_compatible(&chain_id)) { + if ((chainConfig->chainId != chain_id) && + (!chain_is_ethereum_compatible(&chainConfig->chainId) || + !chain_is_ethereum_compatible(&chain_id))) { PRINTF("Unsupported chain ID!\n"); THROW(APDU_RESPONSE_INVALID_DATA); }