general protection fault in vma_interval_tree_remove

Status: upstream: reported C repro on 2022/05/02 12:06
Subsystems: mm
Labels: prio:high
[Documentation on labels]
Reported-by: syzbot+ee1fdd8dcc770a3a169a@syzkaller.appspotmail.com
Fix commit: mm/mmap: fix advanced maple tree API for mmap_region() mm/mmap: qvoid dereferencing next on null in BUG_ON()
Patched on: [ci-upstream-linux-next-kasan-gce-root], missing on: [ci-qemu-gce-upstream-auto ci-qemu-native-arm64-kvm ci-qemu-upstream ci-qemu-upstream-386 ci-qemu2-arm32 ci-qemu2-arm64 ci-qemu2-arm64-compat ci-qemu2-arm64-mte ci-qemu2-riscv64 ci-snapshot-upstream-root ci-upstream-bpf-kasan-gce ci-upstream-bpf-next-kasan-gce ci-upstream-gce-arm64 ci-upstream-gce-leak ci-upstream-kasan-badwrites-root ci-upstream-kasan-gce ci-upstream-kasan-gce-386 ci-upstream-kasan-gce-root ci-upstream-kasan-gce-selinux-root ci-upstream-kasan-gce-smack-root ci-upstream-kmsan-gce-386-root ci-upstream-kmsan-gce-root ci-upstream-net-kasan-gce ci-upstream-net-this-kasan-gce ci-upstream-rust-kasan-gce ci2-upstream-fs ci2-upstream-kcsan-gce ci2-upstream-usb]
First crash: 1661d, last: 900d
Cause bisection: introduced by (bisect log) :
commit b7d0f898f5ce328ad809417f2e728b58153d52d1
Author: Liam R. Howlett <Liam.Howlett@Oracle.com>
Date: Fri May 13 04:15:44 2022 +0000

  mm: remove the vma linked list

Crash: BUG: Bad rss-counter state (log)
Repro: C syz .config
  
✨ AI-generated root cause summary:

In mmap_region() (mm/mmap.c), when call_mmap() modifies vma->vm_flags, the kernel attempts to merge the VMA with its predecessor by calling vma_merge(). The return value of vma_merge() is stored in the local variable 'next', clobbering the pointer to the successor VMA with NULL when the merge fails. If a subsequent step fails (such as mas_preallocate() under fault injection or memory pressure), error cleanup jumps to unmap_and_free_vma and calls unmap_region() with next == NULL, causing free_pgtables() to use USER_PGTABLES_CEILING and mistakenly unlink and free page tables for all subsequent active VMAs in the address space. Later, when exit_mmap() runs, free_pgtables() is invoked again on the address space, attempting to remove the already-unlinked VMAs from their file mapping interval tree and crashing on a NULL pointer dereference in ____rb_erase_color().

▶ ▼ ✨ Full AI root cause analysis
Based on kernel commit 1e1b28b936ae:

Root Cause Analysis

Involved Functions, Structs, and Files


Technical Mechanism and Triggering Flow

  1. Successor VMA Pointer Initialization in mmap_region(): In mmap_region() (mm/mmap.c), after determining the range to map and unmapping any conflicting VMAs, the kernel locates the neighboring VMAs:

    next = mas_next(&mas, ULONG_MAX);
    prev = mas_prev(&mas, 0);
    

    Here, next holds a pointer to the existing successor vm_area_struct located at or above end, if one exists in mm->mm_mt.

  2. Clobbering next on vma_merge() Failure: When mapping a file, call_mmap(file, vma) is invoked. Device drivers or subsystems (such as io_uring_mmap() calling remap_pfn_range()) can alter vma->vm_flags (e.g., adding VM_SPECIAL flags like VM_IO | VM_PFNMAP | VM_DONTEXPAND | VM_DONTDUMP). Because flags changed, mmap_region() checks whether merging with prev is now possible:

    if (unlikely(vm_flags != vma->vm_flags && prev)) {
        next = vma_merge(mm, prev, vma->vm_start, vma->vm_end, vma->vm_flags,
                         NULL, vma->vm_file, vma->vm_pgoff, NULL, NULL_VM_UFFD_CTX, NULL);
        if (next) {
            ...
            goto unmap_writable;
        }
    }
    

    If merging fails (e.g. because vma->vm_flags & VM_SPECIAL prohibits merging in vma_merge()), vma_merge() returns NULL. Storing this result directly into next clobbers the previously found successor VMA pointer, setting next = NULL.

  3. Erroneous Range Ceiling during Error Recovery: Later in mmap_region(), if an error occursβ€”such as mas_preallocate(&mas, vma, GFP_KERNEL) failing with -ENOMEM (triggered by fault injection)β€”execution jumps to unmap_and_free_vma:

    unmap_and_free_vma:
        fput(vma->vm_file);
        vma->vm_file = NULL;
    
        /* Undo any partial mapping done by a device driver. */
        unmap_region(mm, mas.tree, vma, prev, next, vma->vm_start, vma->vm_end);
    

    unmap_region() receives next == NULL. It then invokes free_pgtables():

    free_pgtables(&tlb, mt, vma, prev ? prev->vm_end : FIRST_USER_ADDRESS,
                  next ? next->vm_start : USER_PGTABLES_CEILING);
    

    Because next is NULL, the ceiling argument passed to free_pgtables() defaults to USER_PGTABLES_CEILING.

  4. Premature Unlinking of Active VMAs: In free_pgtables() (mm/memory.c), the maple tree iterator mas starts at vma->vm_end and iterates using mas_find(&mas, ceiling - 1). Because ceiling is USER_PGTABLES_CEILING rather than the successor VMA's start address:

    • The loop finds all active VMAs residing above vma->vm_end in mm->mm_mt.
    • For each of those VMAs, free_pgtables() invokes unlink_anon_vmas(vma) and unlink_file_vma(vma), and frees their page table ranges (free_pgd_range()).
    • unlink_file_vma() calls vma_interval_tree_remove(vma, &mapping->i_mmap), removing these active VMAs from their backing file's interval tree (i_mmap).
    • However, because these VMAs were not actually being unmapped, they remain present in mm->mm_mt.
  5. Double Unlink and Tree Invariant Violation on Process Exit: When the process subsequently exits, exit_mmap() (mm/mmap.c) runs to tear down the address space:

    free_pgtables(&tlb, &mm->mm_mt, vma, FIRST_USER_ADDRESS, USER_PGTABLES_CEILING);
    

    free_pgtables() traverses mm->mm_mt and encounters those active VMAs a second time. It invokes unlink_file_vma(vma) again, which calls vma_interval_tree_remove(vma, &mapping->i_mmap) on nodes that were already removed from the red-black tree.

    In include/linux/rbtree_augmented.h and lib/rbtree.c, rb_erase_augmented() attempts to delete and rebalance the node. Because the node was already detached, the red-black tree structural invariants are broken. Rebalancing enters ____rb_erase_color() (lib/rbtree.c:354), which expects a black node undergoing deletion to have a non-NULL sibling. Instead, sibling = parent->rb_left evaluates to NULL. Checking rb_is_red(sibling) dereferences sibling->__rb_parent_color at address 0x0, causing a General Protection Fault under KASAN shadow memory checking (0xdffffc0000000000) and crashing the kernel.

▶ ▼ ✨ AI Jobs (10)
ID Workflow Result Correct Bug Created Started Finished Revision
dae74cb1 assessment-security DenialOfService: βœ… Exploitable: βœ… FilesystemTrigger: ❌ FuzzingArtifact: ❌ MemoryCorruption: βœ… NetworkNeedsSetup: ❌ NetworkTrigger: ❌ PeripheralTrigger: ❌ RemoteTrigger: ❌ Unprivileged: βœ… UserNamespace: βœ… VMGuestTrigger: ❌ VMHostTrigger: ❌ ❓ general protection fault in vma_interval_tree_remove 2026/11/05 14:35 2026/11/05 14:35 2026/11/05 15:25 5cecaf1f
49439c95 assessment-security πŸ’₯ general protection fault in vma_interval_tree_remove 2026/10/05 17:04 2026/10/05 17:04 2026/10/05 17:16 1995e6c7
f145f028 assessment-security πŸ’₯ general protection fault in vma_interval_tree_remove 2026/09/04 12:03 2026/09/04 12:03 2026/09/04 12:12 801f0966
ce008b77 assessment-security πŸ’₯ general protection fault in vma_interval_tree_remove 2026/08/05 00:07 2026/08/05 00:07 2026/08/05 00:07 d4abbeac
fb1226d6 assessment-security πŸ’₯ general protection fault in vma_interval_tree_remove 2026/07/05 09:22 2026/07/05 09:22 2026/07/05 09:36 93f57954
fa4b3dc3 assessment-security πŸ’₯ general protection fault in vma_interval_tree_remove 2026/06/18 09:06 2026/06/18 09:06 2026/06/18 09:20 b62b3ded
1b62c75e assessment-security πŸ’₯ general protection fault in vma_interval_tree_remove 2026/06/10 04:44 2026/06/10 04:44 2026/06/10 04:59 c36c07f6
3c06c374 assessment-security πŸ’₯ general protection fault in vma_interval_tree_remove 2026/06/04 08:10 2026/06/04 08:10 2026/06/04 08:21 62fe1528
15707494 assessment-security πŸ’₯ general protection fault in vma_interval_tree_remove 2026/06/02 06:18 2026/06/02 06:18 2026/06/02 06:41 1095583b
8907304c assessment-security πŸ’₯ general protection fault in vma_interval_tree_remove 2026/05/22 22:36 2026/05/22 22:36 2026/05/22 22:47 df8ac09c
▶ ▼ Discussions (1)
Title Replies (including bot) Last reply
[syzbot] general protection fault in vma_interval_tree_remove 4 (7) 2022/05/19 05:18
▶ ▼ Similar bugs (2)
Kernel Title Rank πŸ›ˆ Repro Cause bisect Fix bisect Count Last Reported Patched Status
android-5-15 general protection fault in vma_interval_tree_remove origin:downstream 19 syz, C error 2396 5d03h 1494d 0/2 upstream: reported C repro on 2022/10/12 19:12
linux-4.19 general protection fault in vma_interval_tree_remove 2 1 1836d 1836d 0/1 auto-closed as invalid on 2022/03/05 11:44

Sample crash report:
general protection fault, probably for non-canonical address 0xdffffc0000000000: 0000 [#1] PREEMPT SMP KASAN
KASAN: null-ptr-deref in range [0x0000000000000000-0x0000000000000007]
CPU: 1 PID: 3612 Comm: syz-executor255 Not tainted 5.18.0-rc6-next-20220513-syzkaller #0
Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS Google 01/01/2011
RIP: 0010:____rb_erase_color lib/rbtree.c:354 [inline]
RIP: 0010:__rb_erase_color+0x159/0xdb0 lib/rbtree.c:413
Code: 89 ed 48 89 c5 e9 f5 fe ff ff 4c 8d 45 10 4c 89 c0 48 c1 e8 03 80 3c 18 00 0f 85 3a 08 00 00 4c 8b 65 10 4c 89 e0 48 c1 e8 03 <80> 3c 18 00 0f 85 6a 08 00 00 49 8b 04 24 49 8d 7c 24 08 48 89 f9
RSP: 0018:ffffc90002e877a8 EFLAGS: 00010246
RAX: 0000000000000000 RBX: dffffc0000000000 RCX: 0000000000000000
RDX: ffffed100e8d3aed RSI: ffff88807469d768 RDI: ffff8880202132b0
RBP: ffff8880202132b0 R08: ffff8880202132c0 R09: ffff88807469d75f
R10: ffffffff81b02518 R11: 0000000000000001 R12: 0000000000000000
R13: 0000000000000000 R14: ffff88807469d768 R15: ffffffff81b01300
FS:  0000000000000000(0000) GS:ffff8880b9d00000(0000) knlGS:0000000000000000
CS:  0010 DS: 0000 ES: 0000 CR0: 0000000080050033
CR2: 00007ffe92002ff8 CR3: 00000000764a0000 CR4: 00000000003506e0
DR0: 0000000000000000 DR1: 0000000000000000 DR2: 0000000000000000
DR3: 0000000000000000 DR6: 00000000fffe0ff0 DR7: 0000000000000400
Call Trace:
 <TASK>
 rb_erase_augmented include/linux/rbtree_augmented.h:305 [inline]
 rb_erase_augmented_cached include/linux/rbtree_augmented.h:314 [inline]
 vma_interval_tree_remove+0x694/0xed0 mm/interval_tree.c:23
 __remove_shared_vm_struct mm/mmap.c:160 [inline]
 unlink_file_vma+0xbd/0x110 mm/mmap.c:175
 free_pgtables+0x255/0x420 mm/memory.c:440
 exit_mmap+0x1ff/0x740 mm/mmap.c:3219
 __mmput+0x128/0x4c0 kernel/fork.c:1180
 mmput+0x5c/0x70 kernel/fork.c:1201
 exit_mm kernel/exit.c:510 [inline]
 do_exit+0xa18/0x2a00 kernel/exit.c:782
 do_group_exit+0xd2/0x2f0 kernel/exit.c:925
 get_signal+0x2542/0x2600 kernel/signal.c:2857
 arch_do_signal_or_restart+0x82/0x20f0 arch/x86/kernel/signal.c:869
 exit_to_user_mode_loop kernel/entry/common.c:166 [inline]
 exit_to_user_mode_prepare+0x15f/0x250 kernel/entry/common.c:201
 irqentry_exit_to_user_mode+0x5/0x30 kernel/entry/common.c:307
 exc_page_fault+0xc6/0x180 arch/x86/mm/fault.c:1543
 asm_exc_page_fault+0x27/0x30 arch/x86/include/asm/idtentry.h:570
RIP: 0033:0x7f1771dc98cf
Code: Unable to access opcode bytes at RIP 0x7f1771dc98a5.
RSP: 002b:00007ffe920035a0 EFLAGS: 00010206
RAX: 0000000000000001 RBX: 00007f1771e78138 RCX: 0000000000000001
RDX: 0000000000000001 RSI: 00007f1771e78138 RDI: 000000000000000b
RBP: 000000000000000b R08: 0000000000000005 R09: 0000000000000000
R10: 0000000000008011 R11: 0000000000000206 R12: 0000000000000000
R13: 0000000000000001 R14: 00000000000c3ec0 R15: 0000000000000001
 </TASK>
Modules linked in:
---[ end trace 0000000000000000 ]---
RIP: 0010:____rb_erase_color lib/rbtree.c:354 [inline]
RIP: 0010:__rb_erase_color+0x159/0xdb0 lib/rbtree.c:413
Code: 89 ed 48 89 c5 e9 f5 fe ff ff 4c 8d 45 10 4c 89 c0 48 c1 e8 03 80 3c 18 00 0f 85 3a 08 00 00 4c 8b 65 10 4c 89 e0 48 c1 e8 03 <80> 3c 18 00 0f 85 6a 08 00 00 49 8b 04 24 49 8d 7c 24 08 48 89 f9
RSP: 0018:ffffc90002e877a8 EFLAGS: 00010246
RAX: 0000000000000000 RBX: dffffc0000000000 RCX: 0000000000000000
RDX: ffffed100e8d3aed RSI: ffff88807469d768 RDI: ffff8880202132b0
RBP: ffff8880202132b0 R08: ffff8880202132c0 R09: ffff88807469d75f
R10: ffffffff81b02518 R11: 0000000000000001 R12: 0000000000000000
R13: 0000000000000000 R14: ffff88807469d768 R15: ffffffff81b01300
FS:  0000000000000000(0000) GS:ffff8880b9c00000(0000) knlGS:0000000000000000
CS:  0010 DS: 0000 ES: 0000 CR0: 0000000080050033
CR2: 00000000000c3ec8 CR3: 0000000023516000 CR4: 00000000003506f0
DR0: 0000000000000000 DR1: 0000000000000000 DR2: 0000000000000000
DR3: 0000000000000000 DR6: 00000000fffe0ff0 DR7: 0000000000000400
----------------
Code disassembly (best guess):
   0:	89 ed                	mov    %ebp,%ebp
   2:	48 89 c5             	mov    %rax,%rbp
   5:	e9 f5 fe ff ff       	jmpq   0xfffffeff
   a:	4c 8d 45 10          	lea    0x10(%rbp),%r8
   e:	4c 89 c0             	mov    %r8,%rax
  11:	48 c1 e8 03          	shr    $0x3,%rax
  15:	80 3c 18 00          	cmpb   $0x0,(%rax,%rbx,1)
  19:	0f 85 3a 08 00 00    	jne    0x859
  1f:	4c 8b 65 10          	mov    0x10(%rbp),%r12
  23:	4c 89 e0             	mov    %r12,%rax
  26:	48 c1 e8 03          	shr    $0x3,%rax
* 2a:	80 3c 18 00          	cmpb   $0x0,(%rax,%rbx,1) <-- trapping instruction
  2e:	0f 85 6a 08 00 00    	jne    0x89e
  34:	49 8b 04 24          	mov    (%r12),%rax
  38:	49 8d 7c 24 08       	lea    0x8(%r12),%rdi
  3d:	48 89 f9             	mov    %rdi,%rcx

Crashes (14):
Time Kernel Commit Syzkaller Config Log Report Syz repro C repro VM info Assets (help?) Manager Title
2022/05/14 20:18 linux-next 1e1b28b936ae 744a39e2 .config strace log report syz C ci-upstream-linux-next-kasan-gce-root general protection fault in vma_interval_tree_remove
2024/05/29 15:31 upstream e0cce98fe279 34889ee3 .config console log report syz / log C [disk image (non-bootable)] [vmlinux] [kernel image] ci-qemu2-arm64-mte BUG: unable to handle kernel NULL pointer dereference in vma_interval_tree_remove
2022/05/20 03:38 linux-next 3f7bdc402fb0 cb1ac2e7 .config console log report info ci-upstream-linux-next-kasan-gce-root general protection fault in vma_interval_tree_remove
2022/05/18 10:56 linux-next 3f7bdc402fb0 744a39e2 .config console log report info ci-upstream-linux-next-kasan-gce-root general protection fault in vma_interval_tree_remove
2022/05/18 03:17 linux-next 3f7bdc402fb0 744a39e2 .config console log report info ci-upstream-linux-next-kasan-gce-root general protection fault in vma_interval_tree_remove
2022/05/17 09:02 linux-next 3f7bdc402fb0 744a39e2 .config console log report info ci-upstream-linux-next-kasan-gce-root general protection fault in vma_interval_tree_remove
2022/05/17 01:46 linux-next 3f7bdc402fb0 744a39e2 .config console log report info ci-upstream-linux-next-kasan-gce-root general protection fault in vma_interval_tree_remove
2022/05/16 16:23 linux-next 3f7bdc402fb0 744a39e2 .config console log report info ci-upstream-linux-next-kasan-gce-root general protection fault in vma_interval_tree_remove
2022/05/15 21:44 linux-next 1e1b28b936ae 744a39e2 .config console log report info ci-upstream-linux-next-kasan-gce-root general protection fault in vma_interval_tree_remove
2022/05/15 18:36 linux-next 1e1b28b936ae 744a39e2 .config console log report info ci-upstream-linux-next-kasan-gce-root general protection fault in vma_interval_tree_remove
2022/05/15 06:40 linux-next 1e1b28b936ae 744a39e2 .config console log report info ci-upstream-linux-next-kasan-gce-root general protection fault in vma_interval_tree_remove
2022/05/15 06:25 linux-next 1e1b28b936ae 744a39e2 .config console log report info ci-upstream-linux-next-kasan-gce-root general protection fault in vma_interval_tree_remove
2022/05/14 20:05 linux-next 1e1b28b936ae 744a39e2 .config console log report info ci-upstream-linux-next-kasan-gce-root general protection fault in vma_interval_tree_remove
2022/04/29 01:50 linux-next bdc61aad77fa e9076525 .config console log report info ci-upstream-linux-next-kasan-gce-root general protection fault in vma_interval_tree_remove
* Struck through repros no longer work on HEAD.