Skip to content

Commit 692aa7a

Browse files
author
Kent Overstreet
committed
bcachefs: Fix sb_field_downgrade validation
- bch2_sb_downgrade_validate() wasn't checking for a downgrade entry extending past the end of the superblock section - for_each_downgrade_entry() is used in to_text() and needs to work on malformed input; it also was missing a check for a field extending past the end of the section Reported-by: syzbot+e49ccab73449180bc9be@syzkaller.appspotmail.com Fixes: 84f1638 ("bcachefs: bch_sb_field_downgrade") Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
1 parent a5c3e26 commit 692aa7a

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

fs/bcachefs/sb-downgrade.c

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,15 +134,25 @@ downgrade_entry_next_c(const struct bch_sb_field_downgrade_entry *e)
134134
#define for_each_downgrade_entry(_d, _i) \
135135
for (const struct bch_sb_field_downgrade_entry *_i = (_d)->entries; \
136136
(void *) _i < vstruct_end(&(_d)->field) && \
137-
(void *) &_i->errors[0] < vstruct_end(&(_d)->field); \
137+
(void *) &_i->errors[0] <= vstruct_end(&(_d)->field) && \
138+
(void *) downgrade_entry_next_c(_i) <= vstruct_end(&(_d)->field); \
138139
_i = downgrade_entry_next_c(_i))
139140

140141
static int bch2_sb_downgrade_validate(struct bch_sb *sb, struct bch_sb_field *f,
141142
enum bch_validate_flags flags, struct printbuf *err)
142143
{
143144
struct bch_sb_field_downgrade *e = field_to_type(f, downgrade);
144145

145-
for_each_downgrade_entry(e, i) {
146+
for (const struct bch_sb_field_downgrade_entry *i = e->entries;
147+
(void *) i < vstruct_end(&e->field);
148+
i = downgrade_entry_next_c(i)) {
149+
if (flags & BCH_VALIDATE_write &&
150+
((void *) &i->errors[0] > vstruct_end(&e->field) ||
151+
(void *) downgrade_entry_next_c(i) > vstruct_end(&e->field))) {
152+
prt_printf(err, "downgrade entry overruns end of superblock section)");
153+
return -BCH_ERR_invalid_sb_downgrade;
154+
}
155+
146156
if (BCH_VERSION_MAJOR(le16_to_cpu(i->version)) !=
147157
BCH_VERSION_MAJOR(le16_to_cpu(sb->version))) {
148158
prt_printf(err, "downgrade entry with mismatched major version (%u != %u)",

0 commit comments

Comments
 (0)