guard against unknown r->data_type in bch2_sb_dev_has_data()
PATCH 1/1 guard against unknown r->data_type in bch2_sb_dev_has_data()
Shift is perfomed only when the data type makes sense.
Fix a missing guard on r->data_type being known.
Reported-by [email protected]
Signed-off-by Camila Alvarez <[email protected]>
--- fs/bcachefs/replicas.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
fs/bcachefs/replicas.c +2 -2
@@ -1006,14 +1006,14 @@ unsigned bch2_sb_dev_has_data(struct bch_sb *sb, unsigned dev)for_each_replicas_entry (replicas, r)for (i =0 ; i < r->nr_devs; i++) -if (r->devs[i] == dev) +if (r->devs[i] == dev && r->data_type <BCH_DATA_NR ) data_has |=1 << r->data_type; }else if (replicas_v0) {struct bch_replicas_entry_v0 *r;for_each_replicas_entry_v0 (replicas_v0, r)for (i =0 ; i < r->nr_devs; i++) -if (r->devs[i] == dev) +if (r->devs[i] == dev && r->data_type <BCH_DATA_NR ) data_has |=1 << r->data_type; }
Signature · 1 line
2.34.1
Quoted 6 lines · Camila Alvarez
Shift is perfomed only when the data type makes sense.
Fix a missing guard on
r->data_typebeing known.Reported-by [email protected]Signed-off-by Camila Alvarez <[email protected]>
Sorry I just now saw this, already applied a different fix - BCH_DATA_NR isn't the right guard here, because we need to be able to handle unknown data types for backwards compatibilicy.
Here's my version:
commit 85e8b546542c9a1f9050a7f53d92b8bd39c8e9d8
Author: Kent Overstreet <[email protected]> Date: Sun May 19 17:28:44 2024 -0400
bcachefs: Fix shift overflows in replicas.c
We can't disallow unknown data_types in verify() - we have to preserve them unchanged for backwards compat; that means we have to add a few more guards.
Reported-by [email protected]
Signed-off-by Kent Overstreet <[email protected]>
fs/bcachefs/replicas.c +21 -8
@@ -524,13 +524,16 @@ int bch2_replicas_gc_start(struct bch_fs *c, unsigned typemask) c->replicas_gc.nr =0 ; c->replicas_gc.entry_size =0 ; -for_each_cpu_replicas_entry (&c->replicas, e) -if (!((1 << e->data_type) & typemask)) { +for_each_cpu_replicas_entry (&c->replicas, e) { +/* Preserve unknown data types */ +if (e->data_type >=BCH_DATA_NR || + !((1 << e->data_type) & typemask)) { c->replicas_gc.nr++; c->replicas_gc.entry_size =max_t (unsigned, c->replicas_gc.entry_size,replicas_entry_bytes (e)); } + } c->replicas_gc.entries = kcalloc(c->replicas_gc.nr, c->replicas_gc.entry_size,
@@ -542,7 +545,8 @@ int bch2_replicas_gc_start(struct bch_fs *c, unsigned typemask) }for_each_cpu_replicas_entry (&c->replicas, e) -if (!((1 << e->data_type) & typemask)) +if (e->data_type >=BCH_DATA_NR || + !((1 << e->data_type) & typemask))memcpy (cpu_replicas_entry (&c->replicas_gc, i++), e, c->replicas_gc.entry_size);
@@ -998,7 +1002,7 @@ unsigned bch2_sb_dev_has_data(struct bch_sb *sb, unsigned dev) {struct bch_sb_field_replicas *replicas;struct bch_sb_field_replicas_v0 *replicas_v0; -unsigned i, data_has =0 ; +unsigned data_has =0 ; replicas =bch2_sb_field_get (sb, replicas); replicas_v0 =bch2_sb_field_get (sb, replicas_v0);
@@ -1006,17 +1010,26 @@ unsigned bch2_sb_dev_has_data(struct bch_sb *sb, unsigned dev)if (replicas) {struct bch_replicas_entry_v1 *r; -for_each_replicas_entry (replicas, r) -for (i =0 ; i < r->nr_devs; i++) +for_each_replicas_entry (replicas, r) { +if (r->data_type >=sizeof (data_has) *8 ) +continue ; + +for (unsigned i =0 ; i < r->nr_devs; i++)if (r->devs[i] == dev) data_has |=1 << r->data_type; + } + }else if (replicas_v0) {struct bch_replicas_entry_v0 *r; -for_each_replicas_entry_v0 (replicas_v0, r) -for (i =0 ; i < r->nr_devs; i++) +for_each_replicas_entry_v0 (replicas_v0, r) { +if (r->data_type >=sizeof (data_has) *8 ) +continue ; + +for (unsigned i =0 ; i < r->nr_devs; i++)if (r->devs[i] == dev) data_has |=1 << r->data_type; + } }