When journal v2 entry nr overflow, it will cause the value of ja->nr to
be incorrect, this will result in the allocated memory to ja->buckets
being too small, leading to out of bounds access.

Reported-by: [email protected]
Closes: https://syzkaller.appspot.com/bug?extid=47ecc948aadfb2ab3efc
Signed-off-by: Lizhi Xu <[email protected]>
---
 fs/bcachefs/journal.c | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)

diff --git a/fs/bcachefs/journal.c b/fs/bcachefs/journal.c
index 13669dd0e375..d4fb5a23b3f6 100644
--- a/fs/bcachefs/journal.c
+++ b/fs/bcachefs/journal.c
@@ -1307,8 +1307,18 @@ int bch2_dev_journal_init(struct bch_dev *ca, struct 
bch_sb *sb)
        if (journal_buckets_v2) {
                unsigned nr = 
bch2_sb_field_journal_v2_nr_entries(journal_buckets_v2);
 
-               for (unsigned i = 0; i < nr; i++)
+               for (unsigned i = 0; i < nr; i++) {
                        ja->nr += le64_to_cpu(journal_buckets_v2->d[i].nr);
+                       if (le64_to_cpu(journal_buckets_v2->d[i].nr) > 
UINT_MAX) {
+                               struct bch_fs *c = ca->fs;
+                               struct printbuf buf = PRINTBUF;
+                               prt_printf(&buf, "journal v2 entry d[%u].nr %lu 
overflow!\n", i,
+                                       
le64_to_cpu(journal_buckets_v2->d[i].nr));
+                               bch_info(c, "%s", buf.buf);
+                               printbuf_exit(&buf);
+                               return -BCH_ERR_ENOMEM_dev_journal_init;
+                       }
+               }
        } else if (journal_buckets) {
                ja->nr = bch2_nr_journal_buckets(journal_buckets);
        }
-- 
2.43.0


Reply via email to