Skip to content

Commit 03c2579

Browse files
Dan Carpentergregkh
authored andcommitted
ubi: Fix an error pointer dereference in error handling code
commit 5d3805a upstream. If "seen_pebs = init_seen(ubi);" fails then "seen_pebs" is an error pointer and we try to kfree() it which results in an Oops. This patch re-arranges the error handling so now it only frees things which have been allocated successfully. Fixes: daef3dd ("UBI: Fastmap: Add self check to detect absent PEBs") Signed-off-by: Dan Carpenter <[email protected]> Signed-off-by: Richard Weinberger <[email protected]> Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent a17bdec commit 03c2579

File tree

1 file changed

+12
-9
lines changed

1 file changed

+12
-9
lines changed

drivers/mtd/ubi/fastmap.c

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1127,7 +1127,7 @@ static int ubi_write_fastmap(struct ubi_device *ubi,
11271127
struct rb_node *tmp_rb;
11281128
int ret, i, j, free_peb_count, used_peb_count, vol_count;
11291129
int scrub_peb_count, erase_peb_count;
1130-
unsigned long *seen_pebs = NULL;
1130+
unsigned long *seen_pebs;
11311131

11321132
fm_raw = ubi->fm_buf;
11331133
memset(ubi->fm_buf, 0, ubi->fm_size);
@@ -1141,7 +1141,7 @@ static int ubi_write_fastmap(struct ubi_device *ubi,
11411141
dvbuf = new_fm_vbuf(ubi, UBI_FM_DATA_VOLUME_ID);
11421142
if (!dvbuf) {
11431143
ret = -ENOMEM;
1144-
goto out_kfree;
1144+
goto out_free_avbuf;
11451145
}
11461146

11471147
avhdr = ubi_get_vid_hdr(avbuf);
@@ -1150,7 +1150,7 @@ static int ubi_write_fastmap(struct ubi_device *ubi,
11501150
seen_pebs = init_seen(ubi);
11511151
if (IS_ERR(seen_pebs)) {
11521152
ret = PTR_ERR(seen_pebs);
1153-
goto out_kfree;
1153+
goto out_free_dvbuf;
11541154
}
11551155

11561156
spin_lock(&ubi->volumes_lock);
@@ -1318,7 +1318,7 @@ static int ubi_write_fastmap(struct ubi_device *ubi,
13181318
ret = ubi_io_write_vid_hdr(ubi, new_fm->e[0]->pnum, avbuf);
13191319
if (ret) {
13201320
ubi_err(ubi, "unable to write vid_hdr to fastmap SB!");
1321-
goto out_kfree;
1321+
goto out_free_seen;
13221322
}
13231323

13241324
for (i = 0; i < new_fm->used_blocks; i++) {
@@ -1340,7 +1340,7 @@ static int ubi_write_fastmap(struct ubi_device *ubi,
13401340
if (ret) {
13411341
ubi_err(ubi, "unable to write vid_hdr to PEB %i!",
13421342
new_fm->e[i]->pnum);
1343-
goto out_kfree;
1343+
goto out_free_seen;
13441344
}
13451345
}
13461346

@@ -1350,7 +1350,7 @@ static int ubi_write_fastmap(struct ubi_device *ubi,
13501350
if (ret) {
13511351
ubi_err(ubi, "unable to write fastmap to PEB %i!",
13521352
new_fm->e[i]->pnum);
1353-
goto out_kfree;
1353+
goto out_free_seen;
13541354
}
13551355
}
13561356

@@ -1360,10 +1360,13 @@ static int ubi_write_fastmap(struct ubi_device *ubi,
13601360
ret = self_check_seen(ubi, seen_pebs);
13611361
dbg_bld("fastmap written!");
13621362

1363-
out_kfree:
1364-
ubi_free_vid_buf(avbuf);
1365-
ubi_free_vid_buf(dvbuf);
1363+
out_free_seen:
13661364
free_seen(seen_pebs);
1365+
out_free_dvbuf:
1366+
ubi_free_vid_buf(dvbuf);
1367+
out_free_avbuf:
1368+
ubi_free_vid_buf(avbuf);
1369+
13671370
out:
13681371
return ret;
13691372
}

0 commit comments

Comments
 (0)