Skip to content

Commit 8cf2036

Browse files
author
Kent Overstreet
committed
bcachefs: add safety checks in bch2_btree_node_fill()
Signed-off-by: Kent Overstreet <[email protected]>
1 parent d789e9a commit 8cf2036

File tree

1 file changed

+24
-1
lines changed

1 file changed

+24
-1
lines changed

fs/bcachefs/btree_cache.c

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -711,7 +711,30 @@ static noinline struct btree *bch2_btree_node_fill(struct btree_trans *trans,
711711
struct btree *b;
712712
u32 seq;
713713

714-
BUG_ON(level + 1 >= BTREE_MAX_DEPTH);
714+
if (unlikely(level >= BTREE_MAX_DEPTH)) {
715+
int ret = bch2_fs_topology_error(c, "attempting to get btree node at level %u, >= max depth %u",
716+
level, BTREE_MAX_DEPTH);
717+
return ERR_PTR(ret);
718+
}
719+
720+
if (unlikely(!bkey_is_btree_ptr(&k->k))) {
721+
struct printbuf buf = PRINTBUF;
722+
bch2_bkey_val_to_text(&buf, c, bkey_i_to_s_c(k));
723+
724+
int ret = bch2_fs_topology_error(c, "attempting to get btree node with non-btree key %s", buf.buf);
725+
printbuf_exit(&buf);
726+
return ERR_PTR(ret);
727+
}
728+
729+
if (unlikely(k->k.u64s > BKEY_BTREE_PTR_U64s_MAX)) {
730+
struct printbuf buf = PRINTBUF;
731+
bch2_bkey_val_to_text(&buf, c, bkey_i_to_s_c(k));
732+
733+
int ret = bch2_fs_topology_error(c, "attempting to get btree node with too big key %s", buf.buf);
734+
printbuf_exit(&buf);
735+
return ERR_PTR(ret);
736+
}
737+
715738
/*
716739
* Parent node must be locked, else we could read in a btree node that's
717740
* been freed:

0 commit comments

Comments
 (0)