Skip to contentSkip to conversation

fpga: zynqpl: fix bitstream header scan length checks

PATCH Applied Michal Simek 1 participant 4 messages Oct 20 – Nov 3

Subsystems (2)
lore
Share & tools
lore

Michal Simek

author maintainer
#0 lore raw

cover fpga: zynqpl: fix bitstream header scan length checks

Hi,

This series fixes bitstream header scan length checks in check_data(), reachable from fpga load and fpga loadfs.

Patch 1 stops the bitstream header scan once there's no longer enough room left for a full header. The old scan kept checking positions near the end of the buffer that could never hold a complete header, and a match there caused an unbounded header read to go past the buffer end.

Patch 2 fixes zynq_loadfs() passing blocksize into zynq_validate_bitstream() after fs_read(), even when actread was smaller. Pass actread so the scan length matches the bytes actually written into the buffer.

Thanks,
Michal

Suraj Kakade (2):
fpga: zynqpl: use actread for bitstream scan in zynq_loadfs
fpga: zynqpl: stop header scan once a full header can't fit

 drivers/fpga/zynqpl.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

---

Signature · 1 line
2.43.0
author maintainer
#1 lore raw

PATCH 1/2 fpga: zynqpl: use actread for bitstream scan in zynq_loadfs

From: Suraj Kakade <[email protected]>

zynq_loadfs() called zynq_validate_bitstream() with blocksize after fs_read(), even when actread was smaller. check_data() then scanned past the bytes actually written into the buffer.

Pass actread instead of blocksize so the header scan length matches what fs_read() wrote.

Fixes 1a897668ac33 ("fpga: Added support to load bit stream from SD/MMC")

---

 drivers/fpga/zynqpl.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/fpga/zynqpl.c +1 -1
index 5a37a33b0a70..e8046995d7ca 100644
@@ -445,7 +445,7 @@ static int zynq_loadfs(xilinx_desc *desc, const void *buf, size_t bsize,
 	if (fs_read(filename, (u32) buf, pos, blocksize, &actread) < 0)
 		return FPGA_FAIL;
 
-	if (zynq_validate_bitstream(desc, buf, bsize, blocksize, &swap,
+	if (zynq_validate_bitstream(desc, buf, bsize, actread, &swap,
 				    &partialbit))
 		return FPGA_FAIL;
 
Signature · 1 line
2.43.0
author maintainer
#2 lore raw

PATCH 2/2 fpga: zynqpl: stop header scan once a full header can't fit

In reply to Michal Simek

From: Suraj Kakade <[email protected]>

The bitstream header scan didn't check whether enough bytes were left in the buffer before testing for a match. Near the end of the buffer this let it examine positions that could never hold a complete header, and a match there caused the subsequent header read to go past the end of the buffer.

Stop the scan once there isn't enough room left for a full header. This only bounds the scan, a buffer just big enough to match the header but with no data behind it would still pass, no minimum bitstream size is enforced here.

Fixes d5dae85f23c7 ("fpga: zynq: Add support for loading bitstream")

---

 drivers/fpga/zynqpl.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/fpga/zynqpl.c +1 -1
index e8046995d7ca..8c4bdb5ccf66 100644
@@ -135,7 +135,7 @@ static void *check_data(u8 *buf, size_t bsize, u32 *swap)
 	u32 word, p = 0; /* possition */
 
 	/* Because buf doesn't need to be aligned let's read it by chars */
-	for (p = 0; p < bsize; p++) {
+	for (p = 0; p + sizeof(bin_format) <= bsize; p++) {
 		word = load_word(&buf[p], SWAP_NO);
 		debug("%s: word %x %x/%x\n", __func__, word, p, (u32)&buf[p]);
 
Signature · 1 line
2.43.0
author maintainer Applied
#3 lore raw
In reply to Michal Simek
Quoted 20 lines · Michal Simek

Hi,

This series fixes bitstream header scan length checks in check_data(), reachable from fpga load and fpga loadfs.

Patch 1 stops the bitstream header scan once there's no longer enough room left for a full header. The old scan kept checking positions near the end of the buffer that could never hold a complete header, and a match there caused an unbounded header read to go past the buffer end.

Patch 2 fixes zynq_loadfs() passing blocksize into zynq_validate_bitstream() after fs_read(), even when actread was smaller. Pass actread so the scan length matches the bytes actually written into the buffer.

Thanks,
Michal

Quoted 9 lines

Suraj Kakade (2):
fpga: zynqpl: use actread for bitstream scan in zynq_loadfs
fpga: zynqpl: stop header scan once a full header can't fit

  drivers/fpga/zynqpl.c | 4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)

---

Applied.
M