Skip to contentSkip to conversation

fpga: altera-cvp: Avoid out-of-bounds read in trailing byte write

PATCH Applied Daisuke Matsuda 2 participants 2 messages Jul 23 – Aug 3 FPGA manager framework

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 fpga: altera-cvp: Avoid out-of-bounds read in trailing byte write

From: Daisuke Matsuda <[email protected]>

The trailing byte path in altera_cvp_send_block() dereferences a u32 pointer even when only 1-3 bytes remain in the input buffer. If the buffer ends at a page or scatterlist boundary, this can read past the valid image data and fault.

Copy the remaining bytes into a zero-initialized u32 before writing the final word so only valid bytes are read from the input buffer.

Fixes 34d1dc17ce97 ("fpga manager: Add Altera CvP driver")

---
v2:
- Move the declaration of 'word' into the trailing-byte block as suggested by Yilun.

 drivers/fpga/altera-cvp.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)
drivers/fpga/altera-cvp.c +6 -4
index 44badfd11e1b..070c025ee215 100644
@@ -16,6 +16,7 @@
 #include <linux/module.h>
 #include <linux/pci.h>
 #include <linux/sizes.h>
+#include <linux/string.h>
 
 #define CVP_BAR		0	/* BAR used for data transfer in memory mode */
 #define CVP_DUMMY_WR	244	/* dummy writes to clear CvP state machine */
@@ -261,7 +262,7 @@ static int altera_cvp_v2_wait_for_credit(struct fpga_manager *mgr,
 static int altera_cvp_send_block(struct altera_cvp_conf *conf,
 				 const u32 *data, size_t len)
 {
-	u32 mask, words = len / sizeof(u32);
+	u32 words = len / sizeof(u32);
 	int i, remainder;
 
 	for (i = 0; i < words; i++)
@@ -270,9 +271,10 @@ static int altera_cvp_send_block(struct altera_cvp_conf *conf,
 	/* write up to 3 trailing bytes, if any */
 	remainder = len % sizeof(u32);
 	if (remainder) {
-		mask = BIT(remainder * 8) - 1;
-		if (mask)
-			conf->write_data(conf, *data & mask);
+		u32 word = 0;
+
+		memcpy(&word, data, remainder);
+		conf->write_data(conf, word);
 	}
 
 	return 0;
Signature · 1 line
2.47.3
Reviewed-by on v2 1/1 Applied
#1 lore raw
Quoted 7 lines · Daisuke Matsuda

From: Daisuke Matsuda <[email protected]>

The trailing byte path in altera_cvp_send_block() dereferences a u32 pointer even when only 1-3 bytes remain in the input buffer. If the buffer ends at a page or scatterlist boundary, this can read past the valid image data and fault.

Quoted 5 lines

Copy the remaining bytes into a zero-initialized u32 before writing the final word so only valid bytes are read from the input buffer.

Fixes 34d1dc17ce97 ("fpga manager: Add Altera CvP driver")

Applied to for-next