Skip to content

Commit 58caa78

Browse files
author
Kent Overstreet
committed
bcachefs: Fix UAFs of btree_insert_entry array
The btree paths array is now dynamically resizable - and as well the btree_insert_entries array, as it needs to be the same size. The merge path (and interior update path) allocates new btree paths, thus can trigger a resize; thus we need to not retain direct pointers after invoking merge; similarly when running btree node triggers. Signed-off-by: Kent Overstreet <[email protected]>
1 parent 2b3e79f commit 58caa78

File tree

1 file changed

+14
-13
lines changed

1 file changed

+14
-13
lines changed

fs/bcachefs/btree_trans_commit.c

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -499,9 +499,8 @@ static int run_one_trans_trigger(struct btree_trans *trans, struct btree_insert_
499499
}
500500

501501
static int run_btree_triggers(struct btree_trans *trans, enum btree_id btree_id,
502-
struct btree_insert_entry *btree_id_start)
502+
unsigned btree_id_start)
503503
{
504-
struct btree_insert_entry *i;
505504
bool trans_trigger_run;
506505
int ret, overwrite;
507506

@@ -514,13 +513,13 @@ static int run_btree_triggers(struct btree_trans *trans, enum btree_id btree_id,
514513
do {
515514
trans_trigger_run = false;
516515

517-
for (i = btree_id_start;
518-
i < trans->updates + trans->nr_updates && i->btree_id <= btree_id;
516+
for (unsigned i = btree_id_start;
517+
i < trans->nr_updates && trans->updates[i].btree_id <= btree_id;
519518
i++) {
520-
if (i->btree_id != btree_id)
519+
if (trans->updates[i].btree_id != btree_id)
521520
continue;
522521

523-
ret = run_one_trans_trigger(trans, i, overwrite);
522+
ret = run_one_trans_trigger(trans, trans->updates + i, overwrite);
524523
if (ret < 0)
525524
return ret;
526525
if (ret)
@@ -534,8 +533,7 @@ static int run_btree_triggers(struct btree_trans *trans, enum btree_id btree_id,
534533

535534
static int bch2_trans_commit_run_triggers(struct btree_trans *trans)
536535
{
537-
struct btree_insert_entry *btree_id_start = trans->updates;
538-
unsigned btree_id = 0;
536+
unsigned btree_id = 0, btree_id_start = 0;
539537
int ret = 0;
540538

541539
/*
@@ -549,20 +547,22 @@ static int bch2_trans_commit_run_triggers(struct btree_trans *trans)
549547
if (btree_id == BTREE_ID_alloc)
550548
continue;
551549

552-
while (btree_id_start < trans->updates + trans->nr_updates &&
553-
btree_id_start->btree_id < btree_id)
550+
while (btree_id_start < trans->nr_updates &&
551+
trans->updates[btree_id_start].btree_id < btree_id)
554552
btree_id_start++;
555553

556554
ret = run_btree_triggers(trans, btree_id, btree_id_start);
557555
if (ret)
558556
return ret;
559557
}
560558

561-
trans_for_each_update(trans, i) {
559+
for (unsigned idx = 0; idx < trans->nr_updates; idx++) {
560+
struct btree_insert_entry *i = trans->updates + idx;
561+
562562
if (i->btree_id > BTREE_ID_alloc)
563563
break;
564564
if (i->btree_id == BTREE_ID_alloc) {
565-
ret = run_btree_triggers(trans, BTREE_ID_alloc, i);
565+
ret = run_btree_triggers(trans, BTREE_ID_alloc, idx);
566566
if (ret)
567567
return ret;
568568
break;
@@ -826,7 +826,8 @@ static inline int do_bch2_trans_commit(struct btree_trans *trans, unsigned flags
826826
struct bch_fs *c = trans->c;
827827
int ret = 0, u64s_delta = 0;
828828

829-
trans_for_each_update(trans, i) {
829+
for (unsigned idx = 0; idx < trans->nr_updates; idx++) {
830+
struct btree_insert_entry *i = trans->updates + idx;
830831
if (i->cached)
831832
continue;
832833

0 commit comments

Comments
 (0)