Re: [syzbot] [bcachefs?] UBSAN: shift-out-of-bounds in bch2_stripe_to_text

From: Zhao Mengmeng
Date: Tue Oct 08 2024 - 04:42:19 EST


#syz test: git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git 1ec6d097897adiff --git a/fs/bcachefs/ec.c b/fs/bcachefs/ec.c
index 141a4c63142f..0801db97394e 100644
--- a/fs/bcachefs/ec.c
+++ b/fs/bcachefs/ec.c
@@ -113,6 +113,12 @@ int bch2_stripe_validate(struct bch_fs *c, struct bkey_s_c k,
const struct bch_stripe *s = bkey_s_c_to_stripe(k).v;
int ret = 0;

+ if (s->csum_granularity_bits >= 32) {
+ bch_err(c, "stripe csum gran bits too big (%u)",
+ s->csum_granularity_bits);
+ return -BCH_ERR_stripe_csum_granularity_bits_too_big;
+ }
+
bkey_fsck_err_on(bkey_eq(k.k->p, POS_MIN) ||
bpos_gt(k.k->p, POS(0, U32_MAX)),
c, stripe_pos_bad,
diff --git a/fs/bcachefs/errcode.h b/fs/bcachefs/errcode.h
index 742dcdd3e5d7..14ba6bc7a029 100644
--- a/fs/bcachefs/errcode.h
+++ b/fs/bcachefs/errcode.h
@@ -258,7 +258,8 @@
x(BCH_ERR_nopromote, nopromote_no_writes) \
x(BCH_ERR_nopromote, nopromote_enomem) \
x(0, invalid_snapshot_node) \
- x(0, option_needs_open_fs)
+ x(0, option_needs_open_fs) \
+ x(EINVAL, stripe_csum_granularity_bits_too_big)

enum bch_errcode {
BCH_ERR_START = 2048,