Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

mm: open-code page_folio() in dump_page()

page_folio() calls page_fixed_fake_head() which will misidentify this page
as being a fake head and load off the end of 'precise'. We may have a
pointer to a fake head, but that's OK because it contains the right
information for dump_page().

gcc-15 is smart enough to catch this with -Warray-bounds:

In function 'page_fixed_fake_head',
inlined from '_compound_head' at ../include/linux/page-flags.h:251:24,
inlined from '__dump_page' at ../mm/debug.c:123:11:
../include/asm-generic/rwonce.h:44:26: warning: array subscript 9 is outside
+array bounds of 'struct page[1]' [-Warray-bounds=]

Link: https://lkml.kernel.org/r/20241125201721.2963278-2-willy@infradead.org
Fixes: fae7d834c43c ("mm: add __dump_folio()")
Signed-off-by: Matthew Wilcox (Oracle) <willy@infradead.org>
Reported-by: Kees Cook <kees@kernel.org>
Cc: <stable@vger.kernel.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>

authored by

Matthew Wilcox (Oracle) and committed by
Andrew Morton
6a7de1bf 4de22b2a

+5 -2
+5 -2
mm/debug.c
··· 124 124 { 125 125 struct folio *foliop, folio; 126 126 struct page precise; 127 + unsigned long head; 127 128 unsigned long pfn = page_to_pfn(page); 128 129 unsigned long idx, nr_pages = 1; 129 130 int loops = 5; 130 131 131 132 again: 132 133 memcpy(&precise, page, sizeof(*page)); 133 - foliop = page_folio(&precise); 134 - if (foliop == (struct folio *)&precise) { 134 + head = precise.compound_head; 135 + if ((head & 1) == 0) { 136 + foliop = (struct folio *)&precise; 135 137 idx = 0; 136 138 if (!folio_test_large(foliop)) 137 139 goto dump; 138 140 foliop = (struct folio *)page; 139 141 } else { 142 + foliop = (struct folio *)(head - 1); 140 143 idx = folio_page_idx(foliop, page); 141 144 } 142 145