Skip to contentSkip to conversation

New: [REGRESSION] 6.1.191+: NULL deref in ext4_mb_load_buddy_gfp() after a3a7d360252c ("ext4: fix e4b bitmap inconsistency reports")

Bug 222294 [email protected] 1 participant 5 messages Nov 19 – Nov 20

lore
Share & tools
lore
#0 lore raw

https://bugzilla.kernel.org/show_bug.cgi?id=222294

            Bug ID: 222294
           Summary: [REGRESSION] 6.1.191+: NULL deref in
                    ext4_mb_load_buddy_gfp() after a3a7d360252c ("ext4:
                    fix e4b bitmap inconsistency reports")
           Product: File System
           Version: 2.5
          Hardware: Intel
                OS: Linux
            Status: NEW
          Severity: normal
          Priority: P3
         Component: ext4
          Assignee: [email protected]
          Reporter: [email protected]
        Regression: No

Created attachment 311518
--> https://bugzilla.kernel.org/attachment.cgi?id=311518&action=edit
patch

Regression in the 6.1.y stable series, introduced in 6.1.191 and still present in 6.1.192. 6.1.190 is fine.

REPRODUCER (100% on 6.1.191/6.1.192, 0% on 6.1.190):

sync; echo 1 > /proc/sys/vm/drop_caches

The machine oopses immediately (kernel.panic_on_oops=1, so it reboots). Without manual action it also hit during a nightly cron job (rsync doing unlink()/rename() on an ext4 volume) at 04:45, after the page cache had been churned by earlier I/O.

SYSTEM
Dell PowerEdge R820, BIOS 2.7.0, x86_64, 40 CPUs, CONFIG_PREEMPT, ext4 on megaraid_sas. Self-built kernel from kernel.org tarballs. Tainted (O): out-of-tree vboxdrv/vboxnetflt/vboxnetadp and xt_IPMARK, none of which appear in the traces.
<.config attached / available on request>

FIRST OOPS (rsync, unlink path)

  Oops: 0000 [#1] PREEMPT SMP
  CPU: 15 PID: 73107 Comm: rsync Tainted: G O 6.1.192 #1
  RIP: 0010:ext4_mb_load_buddy_gfp+0xbc/0x390 [ext4]
  Code: ... 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 ...
  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 RIP via the allocation path (ext4_mb_regular_allocator <- ext4_mb_new_blocks <- ext4_ext_map_blocks <- ext4_writepages <- ext4_rename), for a different group (0x3fa vs 0x3e4). Everything after that was fallout: BSD process accounting writes from do_exit() into the already-broken ext4 led to further oopses, a dead mutex waiter on a freed stack, and finally soft lockups on the acct mutex's wait_lock until the box was reset.

CAUSE
Diffing 6.1.190 -> 6.1.191, the regression comes from
a3a7d360252c ("ext4: fix e4b bitmap inconsistency reports") [upstream bdc56a9c46b2]
preceded in the same release by the preparatory commits
e394adafb089, e6b6cbe9f2fb, 1c0f0e2b9b69
which are not themselves the cause.

The patch converts ext4_mb_load_buddy_gfp() and ext4_mb_get_buddy_page_lock() to __filemap_get_folio() and checks the result with IS_ERR() only. In 6.1.y, __filemap_get_folio() never returns ERR_PTR() (mm/filemap.c, no ERR_PTR anywhere in the function). It returns NULL when:
- the lookup misses and FGP_CREAT is not set (falls through no_page to the final "return folio;" with folio == NULL),
- FGP_NOWAIT and folio_trylock() fails,
- filemap_alloc_folio() fails,
- filemap_add_folio() fails (folio = NULL).

So in the fast path

    folio = __filemap_get_folio(mapping, pnum, FGP_ACCESSED, 0);
    if (IS_ERR(folio) || !folio_test_uptodate(folio) || ...)

a cache miss reaches folio_test_uptodate(NULL) and dereferences address 0. This matches the oops: "cmp $0xfffff000,%rax; ja" is IS_ERR(), followed by "mov (%rax),%rax; test $4,%al" (folio->flags, PG_uptodate) with RAX = 0, CR2 = 0. The FGP_CREAT slow path has the same flaw (PTR_ERR(NULL) == 0, so ret = 0 and the NULL folio is used), as does ext4_mb_get_buddy_page_lock() (folio->mapping on NULL).

Upstream (>= v6.3) returns ERR_PTR from __filemap_get_folio(), so only stable branches that received this backport without adapting it are affected. 5.15.y and 5.10.y may be affected too if they got the same backport; I have not checked.

WORKAROUND / FIX
Wrapper that maps NULL to ERR_PTR(-ENOMEM), used for all six __filemap_get_folio() calls in fs/ext4/mballoc.c:

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);
    }

With this applied on 6.1.192, 4x "echo 1" and 7x "echo 3" to drop_caches no longer crash (previously a single "echo 1" did).
<Result of cron.daily rsync after drop_caches with the patch: ...> Patch attached; also sent to [email protected] and [email protected]: <lore.kernel.org link>

Reverting a3a7d360252c is the other option.

Signature · 2 lines
You are receiving this mail because:
You are watching the assignee of the bug.
#1 lore raw

[Bug 222294] [REGRESSION] 6.1.191+: NULL deref in ext4_mb_load_buddy_gfp() after a3a7d360252c ("ext4: fix e4b bitmap inconsistency reports")

https://bugzilla.kernel.org/show_bug.cgi?id=222294

--- Comment #1 from Dawid Partyka ([email protected]) --- Created attachment 311519
--> https://bugzilla.kernel.org/attachment.cgi?id=311519&action=edit
.config

Signature · 2 lines
You are receiving this mail because:
You are watching the assignee of the bug.
#2 lore raw

[Bug 222294] [REGRESSION] 6.1.191+: NULL deref in ext4_mb_load_buddy_gfp() after a3a7d360252c ("ext4: fix e4b bitmap inconsistency reports")

In reply to [email protected]

https://bugzilla.kernel.org/show_bug.cgi?id=222294

Dawid Partyka ([email protected]) changed:

What |Removed |Added
----------------------------------------------------------------------------

         Regression|No                          |Yes
 Bisected commit-id|                            |a3a7d360252c0d84ae8bda067fb
                   |                            |296c9e82928d3
     Kernel Version|                            |6.1.192
Signature · 2 lines
You are receiving this mail because:
You are watching the assignee of the bug.
#3 lore raw

[Bug 222294] [REGRESSION] 6.1.191+: NULL deref in ext4_mb_load_buddy_gfp() after a3a7d360252c ("ext4: fix e4b bitmap inconsistency reports")

In reply to [email protected]

https://bugzilla.kernel.org/show_bug.cgi?id=222294

--- Comment #2 from Dawid Partyka ([email protected]) --- Created attachment 311520
--> https://bugzilla.kernel.org/attachment.cgi?id=311520&action=edit
log

Signature · 2 lines
You are receiving this mail because:
You are watching the assignee of the bug.
#4 lore raw

[Bug 222294] [REGRESSION] 6.1.191+: NULL deref in ext4_mb_load_buddy_gfp() after a3a7d360252c ("ext4: fix e4b bitmap inconsistency reports")

In reply to [email protected]

https://bugzilla.kernel.org/show_bug.cgi?id=222294

The Linux kernel's regression tracker (Thorsten Leemhuis) ([email protected]) changed:

What |Removed |Added
----------------------------------------------------------------------------
CC| |[email protected]

--- Comment #3 from The Linux kernel's regression tracker (Thorsten Leemhuis) ([email protected]) --- From a quick look I guess it's the same problem that is being discussed here,
with a fix already queued:
https://lore.kernel.org/linux-ext4/[email protected]/

Signature · 2 lines
You are receiving this mail because:
You are watching the assignee of the bug.