bcachefs: fix unsafety in bch2_stripe_to_text()

.to_text() functions need to work on key values that didn't pass .valid

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
This commit is contained in:
Kent Overstreet 2024-04-11 23:37:24 -04:00
parent dc32c118ec
commit 2aeed876d7
2 changed files with 25 additions and 19 deletions

View File

@ -131,29 +131,33 @@ fsck_err:
void bch2_stripe_to_text(struct printbuf *out, struct bch_fs *c, void bch2_stripe_to_text(struct printbuf *out, struct bch_fs *c,
struct bkey_s_c k) struct bkey_s_c k)
{ {
const struct bch_stripe *s = bkey_s_c_to_stripe(k).v; const struct bch_stripe *sp = bkey_s_c_to_stripe(k).v;
unsigned i, nr_data = s->nr_blocks - s->nr_redundant; struct bch_stripe s = {};
memcpy(&s, sp, min(sizeof(s), bkey_val_bytes(k.k)));
unsigned nr_data = s.nr_blocks - s.nr_redundant;
prt_printf(out, "algo %u sectors %u blocks %u:%u csum %u gran %u", prt_printf(out, "algo %u sectors %u blocks %u:%u csum %u gran %u",
s->algorithm, s.algorithm,
le16_to_cpu(s->sectors), le16_to_cpu(s.sectors),
nr_data, nr_data,
s->nr_redundant, s.nr_redundant,
s->csum_type, s.csum_type,
1U << s->csum_granularity_bits); 1U << s.csum_granularity_bits);
for (i = 0; i < s->nr_blocks; i++) { for (unsigned i = 0; i < s.nr_blocks; i++) {
const struct bch_extent_ptr *ptr = s->ptrs + i; const struct bch_extent_ptr *ptr = sp->ptrs + i;
struct bch_dev *ca = bch_dev_bkey_exists(c, ptr->dev);
u32 offset;
u64 b = sector_to_bucket_and_offset(ca, ptr->offset, &offset);
prt_printf(out, " %u:%llu:%u", ptr->dev, b, offset); if ((void *) ptr >= bkey_val_end(k))
if (i < nr_data) break;
prt_printf(out, "#%u", stripe_blockcount_get(s, i));
prt_printf(out, " gen %u", ptr->gen); bch2_extent_ptr_to_text(out, c, ptr);
if (ptr_stale(ca, ptr))
prt_printf(out, " stale"); if (s.csum_type < BCH_CSUM_NR &&
i < nr_data &&
stripe_blockcount_offset(&s, i) < bkey_val_bytes(k.k))
prt_printf(out, "#%u", stripe_blockcount_get(sp, i));
} }
} }

View File

@ -32,6 +32,8 @@ static inline unsigned stripe_csums_per_device(const struct bch_stripe *s)
static inline unsigned stripe_csum_offset(const struct bch_stripe *s, static inline unsigned stripe_csum_offset(const struct bch_stripe *s,
unsigned dev, unsigned csum_idx) unsigned dev, unsigned csum_idx)
{ {
EBUG_ON(s->csum_type >= BCH_CSUM_NR);
unsigned csum_bytes = bch_crc_bytes[s->csum_type]; unsigned csum_bytes = bch_crc_bytes[s->csum_type];
return sizeof(struct bch_stripe) + return sizeof(struct bch_stripe) +