Skip to content

Commit 2aeed87

Browse files
author
Kent Overstreet
committed
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 <[email protected]>
1 parent dc32c11 commit 2aeed87

File tree

2 files changed

+27
-21
lines changed

2 files changed

+27
-21
lines changed

fs/bcachefs/ec.c

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -131,29 +131,33 @@ int bch2_stripe_invalid(struct bch_fs *c, struct bkey_s_c k,
131131
void bch2_stripe_to_text(struct printbuf *out, struct bch_fs *c,
132132
struct bkey_s_c k)
133133
{
134-
const struct bch_stripe *s = bkey_s_c_to_stripe(k).v;
135-
unsigned i, nr_data = s->nr_blocks - s->nr_redundant;
134+
const struct bch_stripe *sp = bkey_s_c_to_stripe(k).v;
135+
struct bch_stripe s = {};
136+
137+
memcpy(&s, sp, min(sizeof(s), bkey_val_bytes(k.k)));
138+
139+
unsigned nr_data = s.nr_blocks - s.nr_redundant;
136140

137141
prt_printf(out, "algo %u sectors %u blocks %u:%u csum %u gran %u",
138-
s->algorithm,
139-
le16_to_cpu(s->sectors),
140-
nr_data,
141-
s->nr_redundant,
142-
s->csum_type,
143-
1U << s->csum_granularity_bits);
144-
145-
for (i = 0; i < s->nr_blocks; i++) {
146-
const struct bch_extent_ptr *ptr = s->ptrs + i;
147-
struct bch_dev *ca = bch_dev_bkey_exists(c, ptr->dev);
148-
u32 offset;
149-
u64 b = sector_to_bucket_and_offset(ca, ptr->offset, &offset);
150-
151-
prt_printf(out, " %u:%llu:%u", ptr->dev, b, offset);
152-
if (i < nr_data)
153-
prt_printf(out, "#%u", stripe_blockcount_get(s, i));
154-
prt_printf(out, " gen %u", ptr->gen);
155-
if (ptr_stale(ca, ptr))
156-
prt_printf(out, " stale");
142+
s.algorithm,
143+
le16_to_cpu(s.sectors),
144+
nr_data,
145+
s.nr_redundant,
146+
s.csum_type,
147+
1U << s.csum_granularity_bits);
148+
149+
for (unsigned i = 0; i < s.nr_blocks; i++) {
150+
const struct bch_extent_ptr *ptr = sp->ptrs + i;
151+
152+
if ((void *) ptr >= bkey_val_end(k))
153+
break;
154+
155+
bch2_extent_ptr_to_text(out, c, ptr);
156+
157+
if (s.csum_type < BCH_CSUM_NR &&
158+
i < nr_data &&
159+
stripe_blockcount_offset(&s, i) < bkey_val_bytes(k.k))
160+
prt_printf(out, "#%u", stripe_blockcount_get(sp, i));
157161
}
158162
}
159163

fs/bcachefs/ec.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ static inline unsigned stripe_csums_per_device(const struct bch_stripe *s)
3232
static inline unsigned stripe_csum_offset(const struct bch_stripe *s,
3333
unsigned dev, unsigned csum_idx)
3434
{
35+
EBUG_ON(s->csum_type >= BCH_CSUM_NR);
36+
3537
unsigned csum_bytes = bch_crc_bytes[s->csum_type];
3638

3739
return sizeof(struct bch_stripe) +

0 commit comments

Comments
 (0)