CVE-2025-40274: KVM: guest_memfd: Remove bindings on memslot deletion when gmem is dying
In the Linux kernel, the following vulnerability has been resolved:
KVM: guestmemfd: Remove bindings on memslot deletion when gmem is dying
When unbinding a memslot from a guestmemfd instance, remove the bindings even if the guestmemfd file is dying, i.e. even if its file refcount has gone to zero. If the memslot is freed before the file is fully released, nullifying the memslot side of the binding in kvmgmemrelease() will write to freed memory, as detected by syzbot+KASAN:
================================================================== BUG: KASAN: slab-use-after-free in kvmgmemrelease+0x176/0x440 virt/kvm/guestmemfd.c:353 Write of size 8 at addr ffff88807befa508 by task syz.0.17/6022
CPU: 0 UID: 0 PID: 6022 Comm: syz.0.17 Not tainted syzkaller #0 PREEMPT(full) Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS Google 10/02/2025 Call Trace: <TASK> dumpstacklvl+0x189/0x250 lib/dumpstack.c:120 printaddressdescription mm/kasan/report.c:378 [inline] printreport+0xca/0x240 mm/kasan/report.c:482 kasanreport+0x118/0x150 mm/kasan/report.c:595 kvmgmemrelease+0x176/0x440 virt/kvm/guestmemfd.c:353 fput+0x44c/0xa70 fs/filetable.c:468 taskworkrun+0x1d4/0x260 kernel/taskwork.c:227 resumeusermodework include/linux/resumeusermode.h:50 [inline] exittousermodeloop+0xe9/0x130 kernel/entry/common.c:43 exittousermodeprepare include/linux/irq-entry-common.h:225 [inline] syscallexittousermodework include/linux/entry-common.h:175 [inline] syscallexittousermode include/linux/entry-common.h:210 [inline] dosyscall64+0x2bd/0xfa0 arch/x86/entry/syscall64.c:100 entrySYSCALL64afterhwframe+0x77/0x7f RIP: 0033:0x7fbeeff8efc9 </TASK>
Allocated by task 6023: kasansavestack mm/kasan/common.c:56 [inline] kasansavetrack+0x3e/0x80 mm/kasan/common.c:77 poisonkmallocredzone mm/kasan/common.c:397 [inline] kasankmalloc+0x93/0xb0 mm/kasan/common.c:414 kasankmalloc include/linux/kasan.h:262 [inline] kmalloccachenoprof+0x3e2/0x700 mm/slub.c:5758 kmallocnoprof include/linux/slab.h:957 [inline] kzallocnoprof include/linux/slab.h:1094 [inline] kvmsetmemoryregion+0x747/0xb90 virt/kvm/kvmmain.c:2104 kvmvmioctlsetmemoryregion+0x6f/0xd0 virt/kvm/kvmmain.c:2154 kvmvmioctl+0x957/0xc60 virt/kvm/kvmmain.c:5201 vfsioctl fs/ioctl.c:51 [inline] dosysioctl fs/ioctl.c:597 [inline] sesysioctl+0xfc/0x170 fs/ioctl.c:583 dosyscallx64 arch/x86/entry/syscall64.c:63 [inline] dosyscall64+0xfa/0xfa0 arch/x86/entry/syscall64.c:94 entrySYSCALL64afterhwframe+0x77/0x7f
Freed by task 6023: kasansavestack mm/kasan/common.c:56 [inline] kasansavetrack+0x3e/0x80 mm/kasan/common.c:77 kasansavefreeinfo+0x46/0x50 mm/kasan/generic.c:584 poisonslabobject mm/kasan/common.c:252 [inline] kasanslabfree+0x5c/0x80 mm/kasan/common.c:284 kasanslabfree include/linux/kasan.h:234 [inline] slabfreehook mm/slub.c:2533 [inline] slabfree mm/slub.c:6622 [inline] kfree+0x19a/0x6d0 mm/slub.c:6829 kvmsetmemoryregion+0x9c4/0xb90 virt/kvm/kvmmain.c:2130 kvmvmioctlsetmemoryregion+0x6f/0xd0 virt/kvm/kvmmain.c:2154 kvmvmioctl+0x957/0xc60 virt/kvm/kvmmain.c:5201 vfsioctl fs/ioctl.c:51 [inline] dosysioctl fs/ioctl.c:597 [inline] sesysioctl+0xfc/0x170 fs/ioctl.c:583 dosyscallx64 arch/x86/entry/syscall64.c:63 [inline] dosyscall64+0xfa/0xfa0 arch/x86/entry/syscall64.c:94 entrySYSCALL64afterhwframe+0x77/0x7f
Deliberately don't acquire filemap invalid lock when the file is dying as the lifecycle of fmapping is outside the purview of KVM. Dereferencing the mapping is probably fine, but there's no need to invalidate anything as memslot deletion is responsible for zapping SPTEs, and the only code that can access the dying file is kvmgmemrelease(), whose core code is mutual ---truncated---
Affected Software
Remediation
Recommended actions to resolve this vulnerability, in priority order.
- Upgrade
Upgrade
ubuntu/linux-aws-6.17to a version that resolves this vulnerability.Fixed in 6.17.0-1007.7~24.04.1 - Upgrade
Upgrade
ubuntu/linux-gcp-6.17to a version that resolves this vulnerability.Fixed in 6.17.0-1008.8~24.04.1 - Upgrade
Upgrade
ubuntu/linux-nvidia-6.17to a version that resolves this vulnerability.Fixed in 6.17.0-1008.8 - Upgrade
Upgrade
ubuntu/linux-nvidia-tegrato a version that resolves this vulnerability.Fixed in 6.8.0-1035.38 - Upgrade
Upgrade
ubuntu/linux-oem-6.17to a version that resolves this vulnerability.Fixed in 6.17.0-1011.11 - Upgrade
Upgrade
ubuntu/linux-oracle-6.17to a version that resolves this vulnerability.Fixed in 6.17.0-1007.7~24.04.1 - Upgrade
Upgrade
ubuntu/linux-realtime-6.17to a version that resolves this vulnerability.Fixed in 6.17.0-1006.7~24.04.1 - Upgrade
Upgrade
ubuntu/linuxto a version that resolves this vulnerability.Fixed in 6.17.0-14.14 - Upgrade
Upgrade
ubuntu/linux-awsto a version that resolves this vulnerability.Fixed in 6.17.0-1007.7 - Upgrade
Upgrade
ubuntu/linux-azureto a version that resolves this vulnerability.Fixed in 6.17.0-1008.8 - Upgrade
Upgrade
ubuntu/linux-gcpto a version that resolves this vulnerability.Fixed in 6.17.0-1007.7 - Upgrade
Upgrade
ubuntu/linux-oracleto a version that resolves this vulnerability.Fixed in 6.17.0-1007.7 - Upgrade
Upgrade
ubuntu/linux-raspito a version that resolves this vulnerability.Fixed in 6.17.0-1008.8 - Upgrade
Upgrade
ubuntu/linux-realtimeto a version that resolves this vulnerability.Fixed in 6.17.0-1006.7 - Upgrade
Upgrade
ubuntu/linux-riscvto a version that resolves this vulnerability.Fixed in 6.17.0-14.14.1 - Compensating control
In KVM guest_memfd, remove bindings when a memslot is deleted even if the guest_memfd file is dying or its file reference count has reached zero; avoid acquiring the filemap invalidation lock for a dying file, since memslot deletion handles SPTE zapping and the file mapping lifecycle is outside KVM.
Event History
Frequently Asked Questions
What is the severity of CVE-2025-40274?
CVE-2025-40274 has a severity rating that indicates it could lead to exploitation under specific circumstances in the Linux kernel.
How do I fix CVE-2025-40274?
To resolve CVE-2025-40274, update your Linux kernel to a version where this vulnerability has been addressed.
What systems are affected by CVE-2025-40274?
CVE-2025-40274 affects various versions of the Linux kernel that utilize guest_memfd instances.
Can CVE-2025-40274 be exploited remotely?
CVE-2025-40274 primarily impacts local interactions within a virtualized environment rather than facilitating remote exploits.
What are the implications of CVE-2025-40274 for virtualized environments?
CVE-2025-40274 may result in improper memory management in KVM-based virtualized environments, potentially leading to instability.