From 5e9aae9ef069912edea21f807be5512249971127 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Old=C5=99ich=20Jedli=C4=8Dka?= Date: Wed, 25 Sep 2019 21:45:42 +0200 Subject: [PATCH] kernel: Fix off-by-one error in FIT mtd partition search. MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit This fixes off-by-one error introduced in commit dc76900021b8 ("kernel: Correctly search for the FIT image in mtd partition.") Function `mtd_read` starts reading at `offset` and needs `hdr_len` number of bytes to be available. Suppose the easiest case when `offset` is `0` and `hdr_len` equals to `mtd->size` - the `for` loop will not be entered even when enough bytes are available to be read. Same happens for any non-zero `offset`, when `hdr_len` is just enough bytes to be read until `mtd->size` is reached. Imagine that for example `mtd->size=5`, `offset=4` and `hdr_len=1`. Then `offset+hdr_len=5` and the check has to be `offset+hdr_len <= mtd->size`, i.e. `5 <= 5`. The check for `offset + hdr_len` value needs to be inclusive, therefore use `<=`. Fixes: dc76900021b8 ("kernel: Correctly search for the FIT image in mtd partition.") Signed-off-by: Oldřich Jedlička [adjusted commit ref, fixes tag] Signed-off-by: Petr Štetiar (cherry picked from commit e0ce80d42ace6feba509da16795ab0eb81cf5bf4) --- target/linux/generic/files/drivers/mtd/mtdsplit/mtdsplit_fit.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/target/linux/generic/files/drivers/mtd/mtdsplit/mtdsplit_fit.c b/target/linux/generic/files/drivers/mtd/mtdsplit/mtdsplit_fit.c index d206fecd5b..67ee33d085 100644 --- a/target/linux/generic/files/drivers/mtd/mtdsplit/mtdsplit_fit.c +++ b/target/linux/generic/files/drivers/mtd/mtdsplit/mtdsplit_fit.c @@ -60,7 +60,7 @@ mtdsplit_fit_parse(struct mtd_info *mtd, hdr_len = sizeof(struct fdt_header); /* Parse the MTD device & search for the FIT image location */ - for(offset = 0; offset + hdr_len < mtd->size; offset += mtd->erasesize) { + for(offset = 0; offset + hdr_len <= mtd->size; offset += mtd->erasesize) { ret = mtd_read(mtd, offset, hdr_len, &retlen, (void*) &hdr); if (ret) { pr_err("read error in \"%s\" at offset 0x%llx\n", -- 2.25.1