Skip to content

Commit d08c3b7

Browse files
vagnihotri-devArtem Bityutskiy
authored andcommitted
UBI: fix overflow bug
I was experiencing overflows in multiplications for volume->used_bytes in vmt.c & vtbl.c, while creating & resizing large volumes. vol->used_bytes is long long however its 2 operands vol->used_ebs & vol->usable_leb_size are int. So their multiplication for larger values causes integer overflows. Typecasting them solves the problem. My machine & flash details: 64Bit dual-core AMD opteron, 1 GB RAM, linux 2.6.18.3. mtd size = 6GB, volume size= 5GB, peb_size = 4MB. heres patch which does the fix. Signed-off-by: Vinit Agnihotri <[email protected]> Signed-off-by: Artem Bityutskiy <[email protected]>
1 parent 2f3cdb5 commit d08c3b7

File tree

2 files changed

+11
-6
lines changed

2 files changed

+11
-6
lines changed

drivers/mtd/ubi/vmt.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,8 @@ int ubi_create_volume(struct ubi_device *ubi, struct ubi_mkvol_req *req)
280280
if (vol->vol_type == UBI_DYNAMIC_VOLUME) {
281281
vol->used_ebs = vol->reserved_pebs;
282282
vol->last_eb_bytes = vol->usable_leb_size;
283-
vol->used_bytes = vol->used_ebs * vol->usable_leb_size;
283+
vol->used_bytes =
284+
(long long)vol->used_ebs * vol->usable_leb_size;
284285
} else {
285286
bytes = vol->used_bytes;
286287
vol->last_eb_bytes = do_div(bytes, vol->usable_leb_size);
@@ -538,7 +539,8 @@ int ubi_resize_volume(struct ubi_volume_desc *desc, int reserved_pebs)
538539
if (vol->vol_type == UBI_DYNAMIC_VOLUME) {
539540
vol->used_ebs = reserved_pebs;
540541
vol->last_eb_bytes = vol->usable_leb_size;
541-
vol->used_bytes = vol->used_ebs * vol->usable_leb_size;
542+
vol->used_bytes =
543+
(long long)vol->used_ebs * vol->usable_leb_size;
542544
}
543545

544546
paranoid_check_volumes(ubi);
@@ -739,7 +741,7 @@ static void paranoid_check_volume(struct ubi_device *ubi, int vol_id)
739741
goto fail;
740742
}
741743

742-
n = vol->used_ebs * vol->usable_leb_size;
744+
n = (long long)vol->used_ebs * vol->usable_leb_size;
743745
if (vol->vol_type == UBI_DYNAMIC_VOLUME) {
744746
if (vol->corrupted != 0) {
745747
ubi_err("corrupted dynamic volume");

drivers/mtd/ubi/vtbl.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -531,7 +531,8 @@ static int init_volumes(struct ubi_device *ubi, const struct ubi_scan_info *si,
531531
if (vol->vol_type == UBI_DYNAMIC_VOLUME) {
532532
vol->used_ebs = vol->reserved_pebs;
533533
vol->last_eb_bytes = vol->usable_leb_size;
534-
vol->used_bytes = vol->used_ebs * vol->usable_leb_size;
534+
vol->used_bytes =
535+
(long long)vol->used_ebs * vol->usable_leb_size;
535536
continue;
536537
}
537538

@@ -561,7 +562,8 @@ static int init_volumes(struct ubi_device *ubi, const struct ubi_scan_info *si,
561562
}
562563

563564
vol->used_ebs = sv->used_ebs;
564-
vol->used_bytes = (vol->used_ebs - 1) * vol->usable_leb_size;
565+
vol->used_bytes =
566+
(long long)(vol->used_ebs - 1) * vol->usable_leb_size;
565567
vol->used_bytes += sv->last_data_size;
566568
vol->last_eb_bytes = sv->last_data_size;
567569
}
@@ -578,7 +580,8 @@ static int init_volumes(struct ubi_device *ubi, const struct ubi_scan_info *si,
578580
vol->usable_leb_size = ubi->leb_size;
579581
vol->used_ebs = vol->reserved_pebs;
580582
vol->last_eb_bytes = vol->reserved_pebs;
581-
vol->used_bytes = vol->used_ebs * (ubi->leb_size - vol->data_pad);
583+
vol->used_bytes =
584+
(long long)vol->used_ebs * (ubi->leb_size - vol->data_pad);
582585
vol->vol_id = UBI_LAYOUT_VOL_ID;
583586

584587
ubi_assert(!ubi->volumes[i]);

0 commit comments

Comments
 (0)