Fix heap corruption in bcachefs format

In bch2_format(): recompute mi after each sb modification:
its location in memory may have changed due to reallocation.

This fixes an issue where labels were not assigned (because
they were written to an undefined part of the heap instead of
the superblock).

Signed-off-by: Wessel Dankers <wsl@fruit.je>
This commit is contained in:
Wessel Dankers 2022-01-12 17:26:14 +01:00
parent 2c0d0c4b7b
commit 7b15324de1
No known key found for this signature in database
GPG Key ID: 5FA3BB6F097F72B4

View File

@ -241,7 +241,7 @@ struct bch_sb *bch2_format(struct bch_opt_strs fs_opt_strs,
/* Disk labels*/
for (i = devs; i < devs + nr_devs; i++) {
struct bch_member *m = mi->members + (i - devs);
struct bch_member *m;
int idx;
if (!i->label)
@ -251,6 +251,13 @@ struct bch_sb *bch2_format(struct bch_opt_strs fs_opt_strs,
if (idx < 0)
die("error creating disk path: %s", strerror(-idx));
/*
* Recompute mi and m after each sb modification: its location
* in memory may have changed due to reallocation.
*/
mi = bch2_sb_get_members(sb.sb);
m = mi->members + (i - devs);
SET_BCH_MEMBER_GROUP(m, idx + 1);
}