From 26b892d116fabd0395de4dcddbeb2dfdbd4a7426 Mon Sep 17 00:00:00 2001 From: Suraj Sonawane Date: Mon, 25 Nov 2024 12:22:12 +0530 Subject: [PATCH] fix kernel BUG in const_folio_flags (2) syz test Signed-off-by: Suraj Sonawane --- include/linux/page-flags.h | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/include/linux/page-flags.h b/include/linux/page-flags.h index 908ee0aad..ab562ff45 100644 --- a/include/linux/page-flags.h +++ b/include/linux/page-flags.h @@ -306,6 +306,12 @@ static const unsigned long *const_folio_flags(const struct folio *folio, { const struct page *page = &folio->page; + /* Add a check for n to ensure it's within bounds. */ + if (n >= (1 << (PAGE_SHIFT - PAGE_SHIFT))) { + // pr_err("Invalid folio index: n=%u, folio_nr_pages=%u\n", n, folio_nr_pages(folio)); + return -EINVAL; + } + VM_BUG_ON_PGFLAGS(PageTail(page), page); VM_BUG_ON_PGFLAGS(n > 0 && !test_bit(PG_head, &page->flags), page); return &page[n].flags; -- 2.34.1