board: xilinx: Fix heap buffer overflow in board_name_decode()
PATCH 1/1 board: xilinx: Fix heap buffer overflow in board_name_decode()
board_name_decode() builds the board name by repeatedly appending to a fixed MAX_NAME_LENGTH (50-byte) buffer with strcat(), and only checks for overflow once, after the entire loop over all detected nvmemX EEPROM devices has completed. If enough devices are present and their name/revision fields are long enough, strcat() can write past the end of the buffer before the overflow check is reached, resulting in heap memory corruption.
Fix this by using strlcat() instead, which bounds every write to the destination buffer size and prevents overrunning the allocation. Use its return value to detect truncation during board name construction instead of relying on a single length check after all EEPROM devices have been processed.
--- board/xilinx/common/board.c | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-)
board/xilinx/common/board.c +11 -13
@@ -569,8 +569,9 @@ int __maybe_unused board_fit_config_name_match(const char *name)char *__maybe_unused __weak board_name_decode (void ) { -char *board_local_name;struct xilinx_board_description *desc; +char *board_local_name; +size_t len =0 ;int i, id; board_local_name =calloc (1 ,MAX_NAME_LENGTH );
@@ -590,39 +591,36 @@ char * __maybe_unused __weak board_name_decode(void)/* The first string should be soc name */ if (!id) -strcat (board_local_name,CONFIG_SYS_BOARD ); + len =strlcat (board_local_name,CONFIG_SYS_BOARD ,MAX_NAME_LENGTH );/* * For two purpose here: * soc_name- eg: zynqmp- * and between base board and CC eg: ..revA-sck... */ -strcat (board_local_name,"-" ); + len =strlcat (board_local_name,"-" ,MAX_NAME_LENGTH );if (desc->name[0 ]) {/* For DT composition name needs to be lowercase */ for (i =0 ; i <sizeof (desc->name); i++) desc->name[i] =tolower (desc->name[i]); -strcat (board_local_name, desc->name); + len =strlcat (board_local_name, desc->name,MAX_NAME_LENGTH ); }if (desc->revision[0 ]) { -strcat (board_local_name,"-rev" ); + len =strlcat (board_local_name,"-rev" ,MAX_NAME_LENGTH );/* And revision needs to be uppercase */ for (i =0 ; i <sizeof (desc->revision); i++) desc->revision[i] =toupper (desc->revision[i]); -strcat (board_local_name, desc->revision); + len =strlcat (board_local_name, desc->revision,MAX_NAME_LENGTH ); } - } -/* -* Longer strings will end up with buffer overflow and potential -* attacks that's why check it -*/ -if (strlen (board_local_name) >=MAX_NAME_LENGTH ) -panic ("Board name can't be determined\n" ); +/* Reject truncated board names. */ +if (len >=MAX_NAME_LENGTH ) +panic ("Board name can't be determined\n" ); + }if (strlen (board_local_name))return board_local_name;
Signature · 1 line
2.43.0
Quoted 74 lines · Pranav Tilak
board_name_decode()builds the board name by repeatedly appending to a fixedMAX_NAME_LENGTH(50-byte) buffer withstrcat(), and only checks for overflow once, after the entire loop over all detected nvmemX EEPROM devices has completed. If enough devices are present and their name/revision fields are long enough,strcat()can write past the end of the buffer before the overflow check is reached, resulting in heap memory corruption.Fix this by using
strlcat()instead, which bounds every write to the destination buffer size and prevents overrunning the allocation. Use its return value to detect truncation during board name construction instead of relying on a single length check after all EEPROM devices have been processed.Fixes 52ff1626cf4c ("xilinx: common: Enabling generic function for DT reselection")Signed-off-by Pranav Tilak <[email protected]>--- board/xilinx/common/board.c | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-)board/xilinx/common/board.c +7
-10@@ -569,8 +569,9 @@ int __maybe_unused board_fit_config_name_match(const char *name)char *__maybe_unused __weak board_name_decode (void ) { -char *board_local_name;struct xilinx_board_description *desc; +char *board_local_name; +size_t len =0 ;int i, id; board_local_name =calloc (1 ,MAX_NAME_LENGTH );@@ -590,39 +591,36 @@ char * __maybe_unused __weak board_name_decode(void)/* The first string should be soc name */ if (!id) -strcat (board_local_name,CONFIG_SYS_BOARD ); + len =strlcat (board_local_name,CONFIG_SYS_BOARD ,MAX_NAME_LENGTH );/* * For two purpose here: * soc_name- eg: zynqmp- * and between base board and CC eg: ..revA-sck... */ -strcat (board_local_name,"-" ); + len =strlcat (board_local_name,"-" ,MAX_NAME_LENGTH );if (desc->name[0 ]) {/* For DT composition name needs to be lowercase */ for (i =0 ; i <sizeof (desc->name); i++) desc->name[i] =tolower (desc->name[i]); -strcat (board_local_name, desc->name); + len =strlcat (board_local_name, desc->name,MAX_NAME_LENGTH ); }if (desc->revision[0 ]) { -strcat (board_local_name,"-rev" ); + len =strlcat (board_local_name,"-rev" ,MAX_NAME_LENGTH );/* And revision needs to be uppercase */ for (i =0 ; i <sizeof (desc->revision); i++) desc->revision[i] =toupper (desc->revision[i]); -strcat (board_local_name, desc->revision); + len =strlcat (board_local_name, desc->revision,MAX_NAME_LENGTH ); } - } -/* -* Longer strings will end up with buffer overflow and potential -* attacks that's why check it
board/xilinx/common/board.c char * __maybe_unused __weak board_name_decode(void) · line 626
Quoted 10 lines
board/xilinx/common/board.c +4
-3-*/ -if (strlen (board_local_name) >=MAX_NAME_LENGTH ) -panic ("Board name can't be determined\n" ); +/* Reject truncated board names. */ +if (len >=MAX_NAME_LENGTH ) +panic ("Board name can't be determined\n" ); + }if (strlen (board_local_name))return board_local_name;
Applied.
M