6.1.192: NULL deref in ext4_mb_load_buddy_gfp() after, dropping page cache (__filemap_get_folio() returns NULL, callers check IS_ERR())
Hi,
After updating from 6.1.190 to 6.1.192 I get a reproducible NULL pointer dereference in ext4_mb_load_buddy_gfp() whenever the buddy/bitmap pages of an ext4 group are not in the page cache.
Reproducer (100% on 6.1.192, 100% OK on 6.1.190):
sync; echo 1 > /proc/sys/vm/drop_caches
The machine oopses immediately (panic_on_oops=1, so it reboots). It also happened without any manual action: a nightly cron job (rsync doing unlink()/rename() on an ext4 volume) hit the same oops at 04:45. I have not yet tested 6.1.191.
Setup: Dell PowerEdge R820, x86_64, ext4 on megaraid_sas, CONFIG_PREEMPT. Kernel is tainted (O) by out-of-tree modules (vboxdrv, xt_IPMARK), but neither appears in the trace.
First oops (from the cron run, rsync doing unlink):
BUG: kernel NULL pointer dereference, address: 0000000000000000 Oops: 0000 [#1] PREEMPT SMP CPU: 15 PID: 73107 Comm: rsync Tainted: G O 6.1.192 #1 Hardware name: Dell Inc. PowerEdge R820/066N7P, BIOS 2.7.0 01/07/2020 RIP: 0010:ext4_mb_load_buddy_gfp+0xbc/0x390 [ext4] Code: 89 44 24 08 f7 3c 24 48 63 d8 89 54 24 0c ba 01 00 00 00 48 89 de
e8 b3 f3 70 e0 49 89 c7 48 3d 00 f0 ff ff 0f 87 3a 01 00 00 <48> 8b 00 a8 04 0f 84 24 01 00 00 49 8b 07 a8 01 0f 85 19 01 00 00
RAX: 0000000000000000 RBX: 00000000000003e4 ...
CR2: 0000000000000000
Call Trace: ext4_free_blocks+0x3a7/0xb30 [ext4] ext4_ext_remove_space+0x80f/0x1570 [ext4] ext4_ext_truncate+0xb4/0xc0 [ext4] ext4_truncate+0x310/0x480 [ext4] ext4_evict_inode+0x308/0x720 [ext4] evict+0x103/0x2b0 do_unlinkat+0x143/0x2f0 __x64_sys_unlink+0x3e/0x60 do_syscall_64+0x35/0x90
A second rsync hit the same function via the allocation path (ext4_mb_regular_allocator <- ext4_mb_new_blocks <- ext4_ext_map_blocks <- ext4_writepages <- ext4_rename), different group (0x3fa vs 0x3e4), same RIP. The subsequent oopses were fallout (BSD accounting write from do_exit() into the already-broken ext4).
Analysis:
The diff 6.1.190 -> 6.1.192 of fs/ext4/mballoc.c converts bd_bitmap_page / bd_buddy_page to folios, with code like:
folio = __filemap_get_folio(inode->i_mapping, pnum, FGP_ACCESSED, 0); if (IS_ERR(folio) || !folio_test_uptodate(folio) || folio_test_locked(folio)) {
The decoded oops matches this: "cmp $0xfffff000,%rax; ja" is IS_ERR(), followed by "mov (%rax),%rax; test $4,%al" which is folio->flags / folio_test_uptodate() with RAX = 0.
As far as I can tell, in 6.1.y __filemap_get_folio() still returns NULL on failure/miss; the conversion to ERR_PTR() came in a later release (I believe 6.3). If so, the backport relies on semantics that do not exist in 6.1:
- fast path (FGP_ACCESSED, no FGP_CREAT): NULL on cache miss -> IS_ERR(NULL) is false -> folio_test_uptodate(NULL) -> oops;
- slow path (FGP_CREAT) on allocation failure: NULL -> PTR_ERR(NULL) is 0, so ret = 0 and the function continues with a NULL folio;
- ext4_mb_get_buddy_page_lock(): IS_ERR(NULL) false -> folio->mapping dereferenced.
I verified the 6.1.192 source of __filemap_get_folio() returns NULL:
<yes/no - please check with: grep -n -A70 '^struct folio \*__filemap_get_folio' mm/filemap.c>
Possible fix (UNTESTED unless noted below): either handle NULL explicitly at all six __filemap_get_folio() call sites in mballoc.c, or add a small wrapper that maps NULL to ERR_PTR(-ENOMEM) so the rest of the backported code works as written:
static struct folio *ext4_mb_get_folio(struct address_space *mapping, pgoff_t index, int fgp, gfp_t gfp)
{
struct folio *folio = __filemap_get_folio(mapping, index, fgp, gfp);
return folio ? folio : ERR_PTR(-ENOMEM); }
(fgp is plain int in 6.1; fgf_t does not exist there.) For the fast-path lookup, -ENOMEM simply falls through to the FGP_CREAT slow path. <Tested with this wrapper: yes/no, result: ...>
Please let me know what other information would help (full .config, tests on 6.1.191, etc.).
Thanks,
Safe-Lock.NET Dawid Partyka
Signature · 7 lines
Safe-Lock.NET - Dawid Partyka ul. Zebrzydowicka 137 44-217 Rybnik NIP: PL6422786434, REGON: 276286525 tel +48 694 590 047 serwis: +48 784 947 278
Sasha Levin
maintainerQuoted 3 lines
After updating from 6.1.190 to 6.1.192 I get a reproducible NULL pointer dereference in
ext4_mb_load_buddy_gfp()whenever the buddy/bitmap pages of an ext4 group are not in the page cache.
The fix, "ext4: fix NULL folio dereference in mballoc", is queued and is in 6.1.193-rc2.
Signature · 2 lines
Thanks, Sasha