Skip to content

Commit 1e020e1

Browse files
Zhihao Chengrichardweinberger
authored andcommitted
ubi: Fix failure attaching when vid_hdr offset equals to (sub)page size
Following process will make ubi attaching failed since commit 1b42b1a ("ubi: ensure that VID header offset ... size"): ID="0xec,0xa1,0x00,0x15" # 128M 128KB 2KB modprobe nandsim id_bytes=$ID flash_eraseall /dev/mtd0 modprobe ubi mtd="0,2048" # set vid_hdr offset as 2048 (one page) (dmesg): ubi0 error: ubi_attach_mtd_dev [ubi]: VID header offset 2048 too large. UBI error: cannot attach mtd0 UBI error: cannot initialize UBI, error -22 Rework original solution, the key point is making sure 'vid_hdr_shift + UBI_VID_HDR_SIZE < ubi->vid_hdr_alsize', so we should check vid_hdr_shift rather not vid_hdr_offset. Then, ubi still support (sub)page aligined VID header offset. Fixes: 1b42b1a ("ubi: ensure that VID header offset ... size") Signed-off-by: Zhihao Cheng <[email protected]> Tested-by: Nicolas Schichan <[email protected]> Tested-by: Miquel Raynal <[email protected]> # v5.10, v4.19 Signed-off-by: Richard Weinberger <[email protected]>
1 parent f773f0a commit 1e020e1

1 file changed

Lines changed: 15 additions & 6 deletions

File tree

drivers/mtd/ubi/build.c

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -666,12 +666,6 @@ static int io_init(struct ubi_device *ubi, int max_beb_per1024)
666666
ubi->ec_hdr_alsize = ALIGN(UBI_EC_HDR_SIZE, ubi->hdrs_min_io_size);
667667
ubi->vid_hdr_alsize = ALIGN(UBI_VID_HDR_SIZE, ubi->hdrs_min_io_size);
668668

669-
if (ubi->vid_hdr_offset && ((ubi->vid_hdr_offset + UBI_VID_HDR_SIZE) >
670-
ubi->vid_hdr_alsize)) {
671-
ubi_err(ubi, "VID header offset %d too large.", ubi->vid_hdr_offset);
672-
return -EINVAL;
673-
}
674-
675669
dbg_gen("min_io_size %d", ubi->min_io_size);
676670
dbg_gen("max_write_size %d", ubi->max_write_size);
677671
dbg_gen("hdrs_min_io_size %d", ubi->hdrs_min_io_size);
@@ -689,6 +683,21 @@ static int io_init(struct ubi_device *ubi, int max_beb_per1024)
689683
ubi->vid_hdr_aloffset;
690684
}
691685

686+
/*
687+
* Memory allocation for VID header is ubi->vid_hdr_alsize
688+
* which is described in comments in io.c.
689+
* Make sure VID header shift + UBI_VID_HDR_SIZE not exceeds
690+
* ubi->vid_hdr_alsize, so that all vid header operations
691+
* won't access memory out of bounds.
692+
*/
693+
if ((ubi->vid_hdr_shift + UBI_VID_HDR_SIZE) > ubi->vid_hdr_alsize) {
694+
ubi_err(ubi, "Invalid VID header offset %d, VID header shift(%d)"
695+
" + VID header size(%zu) > VID header aligned size(%d).",
696+
ubi->vid_hdr_offset, ubi->vid_hdr_shift,
697+
UBI_VID_HDR_SIZE, ubi->vid_hdr_alsize);
698+
return -EINVAL;
699+
}
700+
692701
/* Similar for the data offset */
693702
ubi->leb_start = ubi->vid_hdr_offset + UBI_VID_HDR_SIZE;
694703
ubi->leb_start = ALIGN(ubi->leb_start, ubi->min_io_size);

0 commit comments

Comments
 (0)