[PATCH 04/12] mtd: rawnand: brcmnand: Fix potential out-of-bounds access in oob write

William Zhang
2023-06-06T23:15:01Z

[-- Attachment #1: Type: text/plain, Size: 1341 bytes --]

When the oob buffer length is not in multiple of words, the oob write
function does out-of-bounds read on the oob source buffer at the last
iteration. Fix that by always checking length limit on the oob buffer
read and fill with 0xff when reaching the end of the buffer to the oob
registers.

Fixes: 27c5b17cd1b1 ("mtd: nand: add NAND driver "library" for Broadcom STB NAND controller")
Signed-off-by: William Zhang <william.zhang@broadcom.com>
Reviewed-by: Florian Fainelli <florian.fainelli@broadcom.com>
---

drivers/mtd/nand/raw/brcmnand/brcmnand.c | 8 ++++----
1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/drivers/mtd/nand/raw/brcmnand/brcmnand.c b/drivers/mtd/nand/raw/brcmnand/brcmnand.c
index 20832857c4aa..d920e88c7f5b 100644
--- a/drivers/mtd/nand/raw/brcmnand/brcmnand.c
+++ b/drivers/mtd/nand/raw/brcmnand/brcmnand.c
@@ -1486,10 +1486,10 @@ static int write_oob_to_regs(struct brcmnand_controller *ctrl, int i,

for (j = 0; j < tbytes; j += 4)
oob_reg_write(ctrl, j,
- (oob[j + 0] << 24) |
- (oob[j + 1] << 16) |
- (oob[j + 2] << 8) |
- (oob[j + 3] << 0));
+ (((j < tbytes) ? oob[j] : 0xff) << 24) |
+ (((j + 1 < tbytes) ? oob[j + 1] : 0xff) << 16) |
+ (((j + 2 < tbytes) ? oob[j + 2] : 0xff) << 8) |
+ ((j + 3 < tbytes) ? oob[j + 3] : 0xff));
return tbytes;
}

--
2.37.3