Skip to contentSkip to conversation

xilinx: fru: harden FRU parsing against malformed data

PATCH Applied Michal Simek 1 participant 5 messages Oct 20 – Nov 3 Arm/Zynq architecture

lore
Share & tools
lore

Michal Simek

author maintainer
#0 lore raw

cover xilinx: fru: harden FRU parsing against malformed data

Hi,

this series fixes out-of-bounds reads in FRU parsing when handling malformed or corrupted FRU data.

Thanks,
Michal

Pranav Tilak (3):
xilinx: fru: Add bounds checks when parsing FRU data
xilinx: fru: Fix wrong argv index in do_fru_capture() addr check
xilinx: fru: Use sizeof() for board header size

 board/xilinx/common/board.c   |   7 ++-
 board/xilinx/common/fru.c     |  14 +++--
 board/xilinx/common/fru.h     |   5 +-
 board/xilinx/common/fru_ops.c | 103 +++++++++++++++++++++++++++-------
 4 files changed, 102 insertions(+), 27 deletions(-)

---
base-commit: c859517f0606595da489b5762844292e28c948fa

Signature · 1 line
2.43.0
author maintainer
#1 lore raw

PATCH 1/3 xilinx: fru: Add bounds checks when parsing FRU data

From: Pranav Tilak <[email protected]>

fru_capture() reads hdr->off_board and hdr->off_multirec and uses them to locate FRU areas without checking whether the resulting offsets are within the captured buffer. The board and multirecord parsers then trust lengths read from the FRU contents, which can cause reads beyond the end of the buffer when processing a malformed or corrupted FRU image.

Pass the buffer size through the parsing path and validate that FRU headers, areas and fields fit within the supplied buffer, and that the board area meets the FRU spec's minimum size, before accessing them. board.c passes the size of the probed EEPROM; the fru capture command now requires an explicit <size> argument.

Fixes f1b97b5f18a8 ("xilinx: cmd: Add support for FRU commands")

---

 board/xilinx/common/board.c   |  7 ++-
 board/xilinx/common/fru.c     | 12 +++--
 board/xilinx/common/fru.h     |  5 +-
 board/xilinx/common/fru_ops.c | 98 ++++++++++++++++++++++++++++-------
 4 files changed, 98 insertions(+), 24 deletions(-)
board/xilinx/common/board.c +6 -1
index ea438472696d..790494f9b388 100644
@@ -234,7 +234,12 @@ static int xilinx_read_eeprom_fru(struct udevice *dev, char *name,
 		goto end;
 	}
 
-	fru_capture((unsigned long)fru_content);
+	ret = fru_capture((unsigned long)fru_content, eeprom_size);
+	if (ret) {
+		printf("%s: FRU capture failed\n", __func__);
+		goto end;
+	}
+
 	if (gd->flags & GD_FLG_RELOC || (_DEBUG && IS_ENABLED(CONFIG_DTB_RESELECT))) {
 		printf("Xilinx I2C FRU format at %s:\n", name);
 		ret = fru_display(0);
board/xilinx/common/fru.c +9 -3
index 8cf307e33f20..de165c548592 100644
@@ -14,6 +14,7 @@ static int do_fru_capture(struct cmd_tbl *cmdtp, int flag, int argc,
 {
 	unsigned long addr;
 	char *endp;
+	u32 size;
 
 	if (argc < cmdtp->maxargs)
 		return CMD_RET_USAGE;
@@ -22,7 +23,11 @@ static int do_fru_capture(struct cmd_tbl *cmdtp, int flag, int argc,
 	if (*argv[1] == 0 || *endp != 0)
 		return -1;
 
-	return fru_capture(addr);
+	size = hextoul(argv[3], &endp);
+	if (*argv[3] == 0 || *endp != 0)
+		return -1;
+
+	return fru_capture(addr, size);
 }
 
 static int do_fru_display(struct cmd_tbl *cmdtp, int flag, int argc,
@@ -46,7 +51,7 @@ static int do_fru_generate(struct cmd_tbl *cmdtp, int flag, int argc,
 }
 
 static struct cmd_tbl cmd_fru_sub[] = {
-	U_BOOT_CMD_MKENT(capture, 3, 0, do_fru_capture, "", ""),
+	U_BOOT_CMD_MKENT(capture, 4, 0, do_fru_capture, "", ""),
 	U_BOOT_CMD_MKENT(display, 2, 0, do_fru_display, "", ""),
 	U_BOOT_CMD_MKENT(board_gen, 8, 0, do_fru_generate, "", ""),
 };
@@ -72,7 +77,8 @@ static int do_fru(struct cmd_tbl *cmdtp, int flag, int argc,
 
 /***************************************************/
 U_BOOT_LONGHELP(fru,
-	"capture <addr> - Parse and capture FRU table present at address.\n"
+	"capture <addr> <size> - Parse and capture FRU table present at\n"
+	"              <addr>. <size> is the buffer length in bytes.\n"
 	"fru display - Displays content of FRU table that was captured using\n"
 	"              fru capture command\n"
 	"fru board_gen <addr> <manufacturer> <board name> <serial number>\n"
board/xilinx/common/fru.h +4 -1
index 2b3fa05a6136..f61c3b31df72 100644
@@ -98,8 +98,11 @@ struct fru_table {
 #define FRU_TYPELEN_TYPE_BINARY		0
 #define FRU_TYPELEN_TYPE_ASCII8		3
 
+/* FRU spec minimum board area size */
+#define FRU_BOARD_AREA_MIN_LEN		16
+
 int fru_display(int verbose);
-int fru_capture(unsigned long addr);
+int fru_capture(unsigned long addr, u32 size);
 int fru_generate(unsigned long addr, char *manufacturer, char *board_name,
 		 char *serial_no, char *part_no, char *revision);
 u8 fru_checksum(u8 *addr, u8 len);
board/xilinx/common/fru_ops.c +79 -19

183 lines not on this page · raw message

Signature · 1 line
2.43.0
author maintainer
#2 lore raw

PATCH 2/3 xilinx: fru: Fix wrong argv index in do_fru_capture() addr check

In reply to Michal Simek

From: Pranav Tilak <[email protected]>

do_fru_capture() parses the address from argv[2] but validates emptiness against argv[1], which is always the literal "capture" subcommand token and can never be empty. An empty address string passed as argv[2] is never rejected.

Check argv[2] instead, matching what was actually parsed.

Fixes f1b97b5f18a8 ("xilinx: cmd: Add support for FRU commands")

---

 board/xilinx/common/fru.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
board/xilinx/common/fru.c +1 -1
index de165c548592..7566d961af1e 100644
@@ -20,7 +20,7 @@ static int do_fru_capture(struct cmd_tbl *cmdtp, int flag, int argc,
 		return CMD_RET_USAGE;
 
 	addr = hextoul(argv[2], &endp);
-	if (*argv[1] == 0 || *endp != 0)
+	if (*argv[2] == 0 || *endp != 0)
 		return -1;
 
 	size = hextoul(argv[3], &endp);
Signature · 1 line
2.43.0
author maintainer
#3 lore raw

PATCH 3/3 xilinx: fru: Use sizeof() for board header size

In reply to Michal Simek

From: Pranav Tilak <[email protected]>

fru_parse_board() copies the board area header and then skips past it using the bare literal 6, the header's size.

Use sizeof(struct fru_board_info_header) at both sites instead, so the two copies of this constant cannot drift apart if the struct ever changes.

---

 board/xilinx/common/fru_ops.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
board/xilinx/common/fru_ops.c +3 -2
index fb88c21cdf6f..0fc95b927f8e 100644
@@ -178,7 +178,8 @@ static int fru_parse_board(unsigned long addr, u32 size)
 		return -EINVAL;
 	}
 
-	memcpy(&fru_data.brd.ver, (void *)addr, 6);
+	memcpy(&fru_data.brd.ver, (void *)addr,
+	       sizeof(struct fru_board_info_header));
 
 	area_len = fru_data.brd.len * FRU_COMMON_HDR_LEN_MULTIPLIER;
 	if (area_len > size) {
@@ -198,7 +199,7 @@ static int fru_parse_board(unsigned long addr, u32 size)
 	eof = (u8 *)(area_len + addr - 2);
 	end = (u8 *)addr + size;
 
-	addr += 6;
+	addr += sizeof(struct fru_board_info_header);
 	data = (u8 *)&fru_data.brd.manufacturer_type_len;
 
 	/* Record max structure limit not to write data over allocated space */
Signature · 1 line
2.43.0
author maintainer Applied
#4 lore raw
In reply to Michal Simek
Quoted 14 lines · Michal Simek

Hi,

this series fixes out-of-bounds reads in FRU parsing when handling malformed or corrupted FRU data.

Thanks,
Michal

Pranav Tilak (3):
xilinx: fru: Add bounds checks when parsing FRU data
xilinx: fru: Fix wrong argv index in do_fru_capture() addr check
xilinx: fru: Use sizeof() for board header size

Quoted 9 lines
  board/xilinx/common/board.c   |   7 ++-
  board/xilinx/common/fru.c     |  14 +++--
  board/xilinx/common/fru.h     |   5 +-
  board/xilinx/common/fru_ops.c | 103 +++++++++++++++++++++++++++-------
  4 files changed, 102 insertions(+), 27 deletions(-)

---
base-commit: c859517f0606595da489b5762844292e28c948fa

Applied.

M