Skip to content

Commit fc55dac

Browse files
Hou Taorichardweinberger
authored andcommitted
ubi: Free the normal volumes in error paths of ubi_attach_mtd_dev()
The allocated normal volumes saved in ubi->volumes are not freed in the error paths in ubi_attach_mtd_dev() and its callees (e.g. ubi_attach() and ubi_read_volume_table()). These normal volumes should be freed through kill_volumes() and vol_release(), but ubi_attach_mtd_dev() may fail before calling uif_init(), and there will be memory leaks. So adding a new helper ubi_free_all_volumes() to free the normal and the internal volumes. And in order to prevent double-free of volume, reset ubi->volumes[i] to NULL after freeing. Signed-off-by: Hou Tao <houtao1@huawei.com> Signed-off-by: Richard Weinberger <richard@nod.at>
1 parent 9d6c474 commit fc55dac

4 files changed

Lines changed: 30 additions & 14 deletions

File tree

‎drivers/mtd/ubi/attach.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1640,7 +1640,7 @@ int ubi_attach(struct ubi_device *ubi, int force_scan)
16401640
out_wl:
16411641
ubi_wl_close(ubi);
16421642
out_vtbl:
1643-
ubi_free_internal_volumes(ubi);
1643+
ubi_free_all_volumes(ubi);
16441644
vfree(ubi->vtbl);
16451645
out_ai:
16461646
destroy_ai(ai);

‎drivers/mtd/ubi/build.c‎

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -503,21 +503,42 @@ static void uif_close(struct ubi_device *ubi)
503503
}
504504

505505
/**
506-
* ubi_free_internal_volumes - free internal volumes.
506+
* ubi_free_volumes_from - free volumes from specific index.
507507
* @ubi: UBI device description object
508+
* @from: the start index used for volume free.
508509
*/
509-
void ubi_free_internal_volumes(struct ubi_device *ubi)
510+
static void ubi_free_volumes_from(struct ubi_device *ubi, int from)
510511
{
511512
int i;
512513

513-
for (i = ubi->vtbl_slots;
514-
i < ubi->vtbl_slots + UBI_INT_VOL_COUNT; i++) {
514+
for (i = from; i < ubi->vtbl_slots + UBI_INT_VOL_COUNT; i++) {
515+
if (!ubi->volumes[i])
516+
continue;
515517
ubi_eba_replace_table(ubi->volumes[i], NULL);
516518
ubi_fastmap_destroy_checkmap(ubi->volumes[i]);
517519
kfree(ubi->volumes[i]);
520+
ubi->volumes[i] = NULL;
518521
}
519522
}
520523

524+
/**
525+
* ubi_free_all_volumes - free all volumes.
526+
* @ubi: UBI device description object
527+
*/
528+
void ubi_free_all_volumes(struct ubi_device *ubi)
529+
{
530+
ubi_free_volumes_from(ubi, 0);
531+
}
532+
533+
/**
534+
* ubi_free_internal_volumes - free internal volumes.
535+
* @ubi: UBI device description object
536+
*/
537+
void ubi_free_internal_volumes(struct ubi_device *ubi)
538+
{
539+
ubi_free_volumes_from(ubi, ubi->vtbl_slots);
540+
}
541+
521542
static int get_bad_peb_limit(const struct ubi_device *ubi, int max_beb_per1024)
522543
{
523544
int limit, device_pebs;
@@ -1013,7 +1034,7 @@ int ubi_attach_mtd_dev(struct mtd_info *mtd, int ubi_num,
10131034
out_detach:
10141035
ubi_devices[ubi_num] = NULL;
10151036
ubi_wl_close(ubi);
1016-
ubi_free_internal_volumes(ubi);
1037+
ubi_free_all_volumes(ubi);
10171038
vfree(ubi->vtbl);
10181039
out_free:
10191040
vfree(ubi->peb_buf);

‎drivers/mtd/ubi/ubi.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -950,6 +950,7 @@ int ubi_volume_notify(struct ubi_device *ubi, struct ubi_volume *vol,
950950
int ubi_notify_all(struct ubi_device *ubi, int ntype,
951951
struct notifier_block *nb);
952952
int ubi_enumerate_volumes(struct notifier_block *nb);
953+
void ubi_free_all_volumes(struct ubi_device *ubi);
953954
void ubi_free_internal_volumes(struct ubi_device *ubi);
954955

955956
/* kapi.c */

‎drivers/mtd/ubi/vtbl.c‎

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -782,7 +782,7 @@ static int check_attaching_info(const struct ubi_device *ubi,
782782
*/
783783
int ubi_read_volume_table(struct ubi_device *ubi, struct ubi_attach_info *ai)
784784
{
785-
int i, err;
785+
int err;
786786
struct ubi_ainf_volume *av;
787787

788788
empty_vtbl_record.crc = cpu_to_be32(0xf116c36b);
@@ -851,13 +851,7 @@ int ubi_read_volume_table(struct ubi_device *ubi, struct ubi_attach_info *ai)
851851

852852
out_free:
853853
vfree(ubi->vtbl);
854-
for (i = 0; i < ubi->vtbl_slots + UBI_INT_VOL_COUNT; i++) {
855-
if (!ubi->volumes[i])
856-
continue;
857-
ubi_fastmap_destroy_checkmap(ubi->volumes[i]);
858-
kfree(ubi->volumes[i]);
859-
ubi->volumes[i] = NULL;
860-
}
854+
ubi_free_all_volumes(ubi);
861855
return err;
862856
}
863857

0 commit comments

Comments
 (0)