1 From ae42121b0cffa26a09c4402d42710324e762c36c Mon Sep 17 00:00:00 2001
2 From: Sasha Levin <sashal@kernel.org>
3 Date: Wed, 24 Jan 2024 07:37:02 +0100
4 Subject: ubi: Check for too small LEB size in VTBL code
6 From: Richard Weinberger <richard@nod.at>
8 [ Upstream commit 68a24aba7c593eafa8fd00f2f76407b9b32b47a9 ]
10 If the LEB size is smaller than a volume table record we cannot
12 In this case abort attaching.
14 Cc: Chenyuan Yang <cy54@illinois.edu>
15 Cc: stable@vger.kernel.org
16 Fixes: 801c135ce73d ("UBI: Unsorted Block Images")
17 Reported-by: Chenyuan Yang <cy54@illinois.edu>
18 Closes: https://lore.kernel.org/linux-mtd/1433EB7A-FC89-47D6-8F47-23BE41B263B3@illinois.edu/
19 Signed-off-by: Richard Weinberger <richard@nod.at>
20 Reviewed-by: Zhihao Cheng <chengzhihao1@huawei.com>
21 Signed-off-by: Sasha Levin <sashal@kernel.org>
23 drivers/mtd/ubi/vtbl.c | 6 ++++++
24 1 file changed, 6 insertions(+)
26 diff --git a/drivers/mtd/ubi/vtbl.c b/drivers/mtd/ubi/vtbl.c
27 index f700f0e4f2ec4..6e5489e233dd2 100644
28 --- a/drivers/mtd/ubi/vtbl.c
29 +++ b/drivers/mtd/ubi/vtbl.c
30 @@ -791,6 +791,12 @@ int ubi_read_volume_table(struct ubi_device *ubi, struct ubi_attach_info *ai)
31 * The number of supported volumes is limited by the eraseblock size
32 * and by the UBI_MAX_VOLUMES constant.
35 + if (ubi->leb_size < UBI_VTBL_RECORD_SIZE) {
36 + ubi_err(ubi, "LEB size too small for a volume record");
40 ubi->vtbl_slots = ubi->leb_size / UBI_VTBL_RECORD_SIZE;
41 if (ubi->vtbl_slots > UBI_MAX_VOLUMES)
42 ubi->vtbl_slots = UBI_MAX_VOLUMES;