* [PATCH 0/2] KVM: Fix a guest_memfd memslot UAF
@ 2025-12-02 2:03 Sean Christopherson
2025-12-02 2:03 ` [PATCH 1/2] KVM: Disallow toggling KVM_MEM_GUEST_MEMFD on an existing memslot Sean Christopherson
` (3 more replies)
0 siblings, 4 replies; 5+ messages in thread
From: Sean Christopherson @ 2025-12-02 2:03 UTC (permalink / raw)
To: Paolo Bonzini; +Cc: kvm, linux-kernel, Alexander Potapenko, Sean Christopherson
Fix a UAF due to leaving a dangling guest_memfd memslot binding by
disallowing clearing KVM_MEM_GUEST_MEMFD on a memslot. The intent was
that guest_memfd memslots would be immutable (could only be deleted),
but somewhat ironically we missed the case where KVM_MEM_GUEST_MEMFD
itself is the only flag that's toggled.
This is an ABI change, but I can't imagine anyone was relying on
disappearing a guest_memfd memslot.
Patch 2 hardens against the UAF, and prepares for allowing FLAGS_ONLY
changes on guest_memfd memslots. Sooner or later, we're going to allow
dirty logging on guest_memfd, so I think it makes sense to guard against
that so that whoever adds dirty logging support doesn't forget to unbind
on a FLAGS_ONLY change.
I'll respond with the syzkaller reproducer (it's comically simple).
Sean Christopherson (2):
KVM: Disallow toggling KVM_MEM_GUEST_MEMFD on an existing memslot
KVM: Harden and prepare for modifying existing guest_memfd memslots
virt/kvm/kvm_main.c | 17 ++++++++++++++++-
1 file changed, 16 insertions(+), 1 deletion(-)
base-commit: 115d5de2eef32ac5cd488404b44b38789362dbe6
--
2.52.0.107.ga0afd4fd5b-goog
^ permalink raw reply [flat|nested] 5+ messages in thread
* [PATCH 1/2] KVM: Disallow toggling KVM_MEM_GUEST_MEMFD on an existing memslot
2025-12-02 2:03 [PATCH 0/2] KVM: Fix a guest_memfd memslot UAF Sean Christopherson
@ 2025-12-02 2:03 ` Sean Christopherson
2025-12-02 2:03 ` [PATCH 2/2] KVM: Harden and prepare for modifying existing guest_memfd memslots Sean Christopherson
` (2 subsequent siblings)
3 siblings, 0 replies; 5+ messages in thread
From: Sean Christopherson @ 2025-12-02 2:03 UTC (permalink / raw)
To: Paolo Bonzini; +Cc: kvm, linux-kernel, Alexander Potapenko, Sean Christopherson
Reject attempts to disable KVM_MEM_GUEST_MEMFD on a memslot that was
initially created with a guest_memfd binding, as KVM doesn't support
toggling KVM_MEM_GUEST_MEMFD on existing memslots. KVM prevents enabling
KVM_MEM_GUEST_MEMFD, but doesn't prevent clearing the flag.
Failure to reject the new memslot results in a use-after-free due to KVM
not unbinding from the guest_memfd instance. Unbinding on a FLAGS_ONLY
change is easy enough, and can/will be done as a hardening measure (in
anticipation of KVM supporting dirty logging on guest_memfd at some point),
but fixing the use-after-free would only address the immediate symptom.
==================================================================
BUG: KASAN: slab-use-after-free in kvm_gmem_release+0x362/0x400 [kvm]
Write of size 8 at addr ffff8881111ae908 by task repro/745
CPU: 7 UID: 1000 PID: 745 Comm: repro Not tainted 6.18.0-rc6-115d5de2eef3-next-kasan #3 NONE
Hardware name: QEMU Standard PC (Q35 + ICH9, 2009), BIOS 0.0.0 02/06/2015
Call Trace:
<TASK>
dump_stack_lvl+0x51/0x60
print_report+0xcb/0x5c0
kasan_report+0xb4/0xe0
kvm_gmem_release+0x362/0x400 [kvm]
__fput+0x2fa/0x9d0
task_work_run+0x12c/0x200
do_exit+0x6ae/0x2100
do_group_exit+0xa8/0x230
__x64_sys_exit_group+0x3a/0x50
x64_sys_call+0x737/0x740
do_syscall_64+0x5b/0x900
entry_SYSCALL_64_after_hwframe+0x4b/0x53
RIP: 0033:0x7f581f2eac31
</TASK>
Allocated by task 745 on cpu 6 at 9.746971s:
kasan_save_stack+0x20/0x40
kasan_save_track+0x13/0x50
__kasan_kmalloc+0x77/0x90
kvm_set_memory_region.part.0+0x652/0x1110 [kvm]
kvm_vm_ioctl+0x14b0/0x3290 [kvm]
__x64_sys_ioctl+0x129/0x1a0
do_syscall_64+0x5b/0x900
entry_SYSCALL_64_after_hwframe+0x4b/0x53
Freed by task 745 on cpu 6 at 9.747467s:
kasan_save_stack+0x20/0x40
kasan_save_track+0x13/0x50
__kasan_save_free_info+0x37/0x50
__kasan_slab_free+0x3b/0x60
kfree+0xf5/0x440
kvm_set_memslot+0x3c2/0x1160 [kvm]
kvm_set_memory_region.part.0+0x86a/0x1110 [kvm]
kvm_vm_ioctl+0x14b0/0x3290 [kvm]
__x64_sys_ioctl+0x129/0x1a0
do_syscall_64+0x5b/0x900
entry_SYSCALL_64_after_hwframe+0x4b/0x53
Reported-by: Alexander Potapenko <glider@google.com>
Fixes: a7800aa80ea4 ("KVM: Add KVM_CREATE_GUEST_MEMFD ioctl() for guest-specific backing memory")
Cc: stable@vger.kernel.org
Signed-off-by: Sean Christopherson <seanjc@google.com>
---
virt/kvm/kvm_main.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c
index 9eca084bdcbe..8891df136416 100644
--- a/virt/kvm/kvm_main.c
+++ b/virt/kvm/kvm_main.c
@@ -2085,7 +2085,7 @@ static int kvm_set_memory_region(struct kvm *kvm,
return -EINVAL;
if ((mem->userspace_addr != old->userspace_addr) ||
(npages != old->npages) ||
- ((mem->flags ^ old->flags) & KVM_MEM_READONLY))
+ ((mem->flags ^ old->flags) & (KVM_MEM_READONLY | KVM_MEM_GUEST_MEMFD)))
return -EINVAL;
if (base_gfn != old->base_gfn)
--
2.52.0.107.ga0afd4fd5b-goog
^ permalink raw reply related [flat|nested] 5+ messages in thread
* [PATCH 2/2] KVM: Harden and prepare for modifying existing guest_memfd memslots
2025-12-02 2:03 [PATCH 0/2] KVM: Fix a guest_memfd memslot UAF Sean Christopherson
2025-12-02 2:03 ` [PATCH 1/2] KVM: Disallow toggling KVM_MEM_GUEST_MEMFD on an existing memslot Sean Christopherson
@ 2025-12-02 2:03 ` Sean Christopherson
2025-12-02 15:27 ` [PATCH 0/2] KVM: Fix a guest_memfd memslot UAF Sean Christopherson
2025-12-05 16:59 ` Sean Christopherson
3 siblings, 0 replies; 5+ messages in thread
From: Sean Christopherson @ 2025-12-02 2:03 UTC (permalink / raw)
To: Paolo Bonzini; +Cc: kvm, linux-kernel, Alexander Potapenko, Sean Christopherson
Unbind guest_memfd memslots if KVM commits a MOVE or FLAGS_ONLY memslot
change to harden against use-after-free, and to prepare for eventually
supporting dirty logging on guest_memfd memslots, at which point
FLAGS_ONLY changes will be expected/supported.
Add two separate WARNs, once to yell if a guest_memfd memslot is moved
(which KVM is never expected to allow/support), and again if the unbind()
is triggered, to help detect uAPI goofs prior to deliberately allowing
FLAGS_ONLY changes.
Signed-off-by: Sean Christopherson <seanjc@google.com>
---
virt/kvm/kvm_main.c | 15 +++++++++++++++
1 file changed, 15 insertions(+)
diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c
index 8891df136416..f822d3e389b0 100644
--- a/virt/kvm/kvm_main.c
+++ b/virt/kvm/kvm_main.c
@@ -1748,6 +1748,12 @@ static void kvm_commit_memory_region(struct kvm *kvm,
kvm_free_memslot(kvm, old);
break;
case KVM_MR_MOVE:
+ /*
+ * Moving a guest_memfd memslot isn't supported, and will never
+ * be supported.
+ */
+ WARN_ON_ONCE(old->flags & KVM_MEM_GUEST_MEMFD);
+ fallthrough;
case KVM_MR_FLAGS_ONLY:
/*
* Free the dirty bitmap as needed; the below check encompasses
@@ -1756,6 +1762,15 @@ static void kvm_commit_memory_region(struct kvm *kvm,
if (old->dirty_bitmap && !new->dirty_bitmap)
kvm_destroy_dirty_bitmap(old);
+ /*
+ * Unbind the guest_memfd instance as needed; the @new slot has
+ * already created its own binding. TODO: Drop the WARN when
+ * dirty logging guest_memfd memslots is supported. Until then,
+ * flags-only changes on guest_memfd slots should be impossible.
+ */
+ if (WARN_ON_ONCE(old->flags & KVM_MEM_GUEST_MEMFD))
+ kvm_gmem_unbind(old);
+
/*
* The final quirk. Free the detached, old slot, but only its
* memory, not any metadata. Metadata, including arch specific
--
2.52.0.107.ga0afd4fd5b-goog
^ permalink raw reply related [flat|nested] 5+ messages in thread
* Re: [PATCH 0/2] KVM: Fix a guest_memfd memslot UAF
2025-12-02 2:03 [PATCH 0/2] KVM: Fix a guest_memfd memslot UAF Sean Christopherson
2025-12-02 2:03 ` [PATCH 1/2] KVM: Disallow toggling KVM_MEM_GUEST_MEMFD on an existing memslot Sean Christopherson
2025-12-02 2:03 ` [PATCH 2/2] KVM: Harden and prepare for modifying existing guest_memfd memslots Sean Christopherson
@ 2025-12-02 15:27 ` Sean Christopherson
2025-12-05 16:59 ` Sean Christopherson
3 siblings, 0 replies; 5+ messages in thread
From: Sean Christopherson @ 2025-12-02 15:27 UTC (permalink / raw)
To: Paolo Bonzini, kvm, linux-kernel, Alexander Potapenko
[-- Attachment #1: Type: text/plain, Size: 904 bytes --]
On Mon, Dec 01, 2025, Sean Christopherson wrote:
> Fix a UAF due to leaving a dangling guest_memfd memslot binding by
> disallowing clearing KVM_MEM_GUEST_MEMFD on a memslot. The intent was
> that guest_memfd memslots would be immutable (could only be deleted),
> but somewhat ironically we missed the case where KVM_MEM_GUEST_MEMFD
> itself is the only flag that's toggled.
>
> This is an ABI change, but I can't imagine anyone was relying on
> disappearing a guest_memfd memslot.
>
> Patch 2 hardens against the UAF, and prepares for allowing FLAGS_ONLY
> changes on guest_memfd memslots. Sooner or later, we're going to allow
> dirty logging on guest_memfd, so I think it makes sense to guard against
> that so that whoever adds dirty logging support doesn't forget to unbind
> on a FLAGS_ONLY change.
>
> I'll respond with the syzkaller reproducer (it's comically simple).
And almost forgot...
[-- Attachment #2: reproducer.c --]
[-- Type: text/x-csrc, Size: 5755 bytes --]
// autogenerated by syzkaller (https://github.com/google/syzkaller)
#define _GNU_SOURCE
#include <endian.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/syscall.h>
#include <sys/types.h>
#include <unistd.h>
uint64_t r[3] = {0xffffffffffffffff, 0xffffffffffffffff, 0xffffffffffffffff};
int main(void)
{
syscall(__NR_mmap, /*addr=*/0x1ffffffff000ul, /*len=*/0x1000ul, /*prot=*/0ul,
/*flags=MAP_FIXED|MAP_ANONYMOUS|MAP_PRIVATE*/ 0x32ul,
/*fd=*/(intptr_t)-1, /*offset=*/0ul);
syscall(__NR_mmap, /*addr=*/0x200000000000ul, /*len=*/0x1000000ul,
/*prot=PROT_WRITE|PROT_READ|PROT_EXEC*/ 7ul,
/*flags=MAP_FIXED|MAP_ANONYMOUS|MAP_PRIVATE*/ 0x32ul,
/*fd=*/(intptr_t)-1, /*offset=*/0ul);
syscall(__NR_mmap, /*addr=*/0x200001000000ul, /*len=*/0x1000ul, /*prot=*/0ul,
/*flags=MAP_FIXED|MAP_ANONYMOUS|MAP_PRIVATE*/ 0x32ul,
/*fd=*/(intptr_t)-1, /*offset=*/0ul);
const char* reason;
(void)reason;
intptr_t res = 0;
if (write(1, "executing program\n", sizeof("executing program\n") - 1)) {
}
// openat$kvm arguments: [
// fd: const = 0xffffffffffffff9c (8 bytes)
// file: ptr[in, buffer] {
// buffer: {2f 64 65 76 2f 6b 76 6d 00} (length 0x9)
// }
// flags: open_flags = 0x0 (4 bytes)
// mode: const = 0x0 (2 bytes)
// ]
// returns fd_kvm
memcpy((void*)0x200000000000, "/dev/kvm\000", 9);
res = syscall(__NR_openat, /*fd=*/0xffffffffffffff9cul,
/*file=*/0x200000000000ul, /*flags=*/0, /*mode=*/0);
if (res != -1)
r[0] = res;
// ioctl$KVM_CREATE_VM arguments: [
// fd: fd_kvm (resource)
// cmd: const = 0xae01 (4 bytes)
// type: intptr = 0x0 (8 bytes)
// ]
// returns fd_kvmvm
res = syscall(__NR_ioctl, /*fd=*/r[0], /*cmd=*/0xae01, /*type=*/0ul);
if (res != -1)
r[1] = res;
// ioctl$KVM_CREATE_GUEST_MEMFD arguments: [
// fd: fd_kvmvm (resource)
// cmd: const = 0xc040aed4 (4 bytes)
// arg: ptr[in, kvm_create_guest_memfd] {
// kvm_create_guest_memfd {
// size: int64 = 0x200001fe0000 (8 bytes)
// flags: int64 = 0x0 (8 bytes)
// reserved: buffer: {00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
// 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
// 00 00 00 00 00 00 00 00 00} (length 0x30)
// }
// }
// ]
// returns fd_kvm_guest_memfd
*(uint64_t*)0x2000000001c0 = 0x200001fe0000;
*(uint64_t*)0x2000000001c8 = 0;
memset((void*)0x2000000001d0, 0, 48);
res = syscall(__NR_ioctl, /*fd=*/r[1], /*cmd=*/0xc040aed4,
/*arg=*/0x2000000001c0ul);
if (res != -1)
r[2] = res;
// ioctl$KVM_SET_USER_MEMORY_REGION2 arguments: [
// fd: fd_kvmvm (resource)
// cmd: const = 0x40a0ae49 (4 bytes)
// arg: ptr[in, kvm_userspace_memory_region2] {
// kvm_userspace_memory_region2 {
// slot: kvm_mem_slots = 0x4 (4 bytes)
// flags: kvm_mem_region_flags = 0x4 (4 bytes)
// paddr: kvm_guest_addrs = 0x80a0000 (8 bytes)
// size: len = 0x2000 (8 bytes)
// addr: VMA[0x2000]
// guest_memfd_offset: int64 = 0x4000 (8 bytes)
// guest_memfd: fd_kvm_guest_memfd (resource)
// pad1: const = 0x0 (4 bytes)
// pad2: buffer: {00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
// 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
// 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
// 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
// 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
// 00 00} (length 0x70)
// }
// }
// ]
*(uint32_t*)0x200000000180 = 4;
*(uint32_t*)0x200000000184 = 4;
*(uint64_t*)0x200000000188 = 0x80a0000;
*(uint64_t*)0x200000000190 = 0x2000;
*(uint64_t*)0x200000000198 = 0x200000ffc000;
*(uint64_t*)0x2000000001a0 = 0x4000;
*(uint32_t*)0x2000000001a8 = r[2];
*(uint32_t*)0x2000000001ac = 0;
memset((void*)0x2000000001b0, 0, 112);
syscall(__NR_ioctl, /*fd=*/r[1], /*cmd=*/0x40a0ae49,
/*arg=*/0x200000000180ul);
// ioctl$KVM_SET_USER_MEMORY_REGION2 arguments: [
// fd: fd_kvmvm (resource)
// cmd: const = 0x40a0ae49 (4 bytes)
// arg: ptr[in, kvm_userspace_memory_region2] {
// kvm_userspace_memory_region2 {
// slot: kvm_mem_slots = 0x4 (4 bytes)
// flags: kvm_mem_region_flags = 0x1 (4 bytes)
// paddr: kvm_guest_addrs = 0xffff1000 (8 bytes)
// size: len = 0x2000 (8 bytes)
// addr: VMA[0x2000]
// guest_memfd_offset: int64 = 0x8 (8 bytes)
// guest_memfd: fd_kvm_guest_memfd (resource)
// pad1: const = 0x0 (4 bytes)
// pad2: buffer: {00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
// 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
// 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
// 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
// 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
// 00 00} (length 0x70)
// }
// }
// ]
*(uint32_t*)0x200000000240 = 4;
*(uint32_t*)0x200000000244 = 1;
*(uint64_t*)0x200000000248 = 0xffff1000;
*(uint64_t*)0x200000000250 = 0x2000;
*(uint64_t*)0x200000000258 = 0x200000ffc000;
*(uint64_t*)0x200000000260 = 8;
*(uint32_t*)0x200000000268 = r[2];
*(uint32_t*)0x20000000026c = 0;
memset((void*)0x200000000270, 0, 112);
syscall(__NR_ioctl, /*fd=*/r[1], /*cmd=*/0x40a0ae49,
/*arg=*/0x200000000240ul);
return 0;
}
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH 0/2] KVM: Fix a guest_memfd memslot UAF
2025-12-02 2:03 [PATCH 0/2] KVM: Fix a guest_memfd memslot UAF Sean Christopherson
` (2 preceding siblings ...)
2025-12-02 15:27 ` [PATCH 0/2] KVM: Fix a guest_memfd memslot UAF Sean Christopherson
@ 2025-12-05 16:59 ` Sean Christopherson
3 siblings, 0 replies; 5+ messages in thread
From: Sean Christopherson @ 2025-12-05 16:59 UTC (permalink / raw)
To: Sean Christopherson, Paolo Bonzini; +Cc: kvm, linux-kernel, Alexander Potapenko
On Mon, 01 Dec 2025 18:03:32 -0800, Sean Christopherson wrote:
> Fix a UAF due to leaving a dangling guest_memfd memslot binding by
> disallowing clearing KVM_MEM_GUEST_MEMFD on a memslot. The intent was
> that guest_memfd memslots would be immutable (could only be deleted),
> but somewhat ironically we missed the case where KVM_MEM_GUEST_MEMFD
> itself is the only flag that's toggled.
>
> This is an ABI change, but I can't imagine anyone was relying on
> disappearing a guest_memfd memslot.
>
> [...]
Applied to kvm-x86 fixes (I'm feeling lucky with the ABI change).
[1/2] KVM: Disallow toggling KVM_MEM_GUEST_MEMFD on an existing memslot
https://github.com/kvm-x86/linux/commit/9935df5333aa
[2/2] KVM: Harden and prepare for modifying existing guest_memfd memslots
https://github.com/kvm-x86/linux/commit/af62fe2494da
--
https://github.com/kvm-x86/linux/tree/next
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2025-12-05 17:00 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-12-02 2:03 [PATCH 0/2] KVM: Fix a guest_memfd memslot UAF Sean Christopherson
2025-12-02 2:03 ` [PATCH 1/2] KVM: Disallow toggling KVM_MEM_GUEST_MEMFD on an existing memslot Sean Christopherson
2025-12-02 2:03 ` [PATCH 2/2] KVM: Harden and prepare for modifying existing guest_memfd memslots Sean Christopherson
2025-12-02 15:27 ` [PATCH 0/2] KVM: Fix a guest_memfd memslot UAF Sean Christopherson
2025-12-05 16:59 ` Sean Christopherson
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox