Skip to content

Commit eba85fe

Browse files
committed
KVM: guest_memfd: Treat memslot binding offset+size as unsigned values
When binding a memslot to a guest_memfd file, treat the offset and size as unsigned values to fix a bug where the sum of the two can result in a false negative when checking for overflow against the size of the file. Passing unsigned values also avoids relying on somewhat obscure checks in other flows for safety, and tracks the offset and size as they are intended to be tracked, as unsigned values. On 64-bit kernels, the number of pages a memslot contains and thus the size (and offset) of its guest_memfd binding are unsigned 64-bit values. Taking the offset+size as an loff_t instead of a uoff_t inadvertently converts the unsigned value to a signed value if the offset and/or size is massive. Locally storing the offset and size as signed values is benign in and of itself (though even that is *extremely* difficult to discern), but operating on their sum is not. For the offset, KVM explicitly checks against a negative value, which might seem like a bug as KVM could incorrectly reject a legitimate binding, but that's not actually the case as KVM_CREATE_GUEST_MEMFD takes a signed value for its size, i.e. a would-be-negative offset is also greater than the maximum possible size of any guest_memfd file. Regarding the size, while KVM lacks an explicit check for a negative value, i.e. seemingly has a flawed overflow check, KVM restricts the number of pages in a single memslot to the largest positive signed 32-bit value: if (id < KVM_USER_MEM_SLOTS && (mem->memory_size >> PAGE_SHIFT) > KVM_MEM_MAX_NR_PAGES) return -EINVAL; and so that maximum "size" will ever be is 0x7fffffff000. The sum of the two is, however, problematic. While the size is restricted by KVM's memslot logic, the offset is not, i.e. the offset is completely unchecked until the "offset + size > i_size_read(inode)" check. If the offset is the (nearly) largest possible _positive_ value, then adding size to the offset can result in a signed, negative 64-bit value. When compared against the size of the file (guaranteed to be positive), the negative sum is always smaller, and KVM incorrectly allows the absurd offset. Opportunistically add missing includes in kvm_mm.h (instead of relying on its parents). Fixes: a7800aa ("KVM: Add KVM_CREATE_GUEST_MEMFD ioctl() for guest-specific backing memory") Cc: stable@vger.kernel.org Cc: Ackerley Tng <ackerleytng@google.com> Reviewed-by: Michael Roth <michael.roth@amd.com> Reviewed-by: Ackerley Tng <ackerleytng@google.com> Link: https://patch.msgid.link/20260602170921.1304394-2-seanjc@google.com Signed-off-by: Sean Christopherson <seanjc@google.com>
1 parent b0e476e commit eba85fe

2 files changed

Lines changed: 9 additions & 6 deletions

File tree

virt/kvm/guest_memfd.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -640,15 +640,16 @@ int kvm_gmem_create(struct kvm *kvm, struct kvm_create_guest_memfd *args)
640640
}
641641

642642
int kvm_gmem_bind(struct kvm *kvm, struct kvm_memory_slot *slot,
643-
unsigned int fd, loff_t offset)
643+
unsigned int fd, uoff_t offset)
644644
{
645-
loff_t size = slot->npages << PAGE_SHIFT;
645+
uoff_t size = slot->npages << PAGE_SHIFT;
646646
unsigned long start, end;
647647
struct gmem_file *f;
648648
struct inode *inode;
649649
struct file *file;
650650
int r = -EINVAL;
651651

652+
BUILD_BUG_ON(sizeof(gpa_t) != sizeof(offset));
652653
BUILD_BUG_ON(sizeof(gfn_t) != sizeof(slot->gmem.pgoff));
653654

654655
file = fget(fd);
@@ -664,8 +665,7 @@ int kvm_gmem_bind(struct kvm *kvm, struct kvm_memory_slot *slot,
664665

665666
inode = file_inode(file);
666667

667-
if (offset < 0 || !PAGE_ALIGNED(offset) ||
668-
offset + size > i_size_read(inode))
668+
if (!PAGE_ALIGNED(offset) || offset + size > i_size_read(inode))
669669
goto err;
670670

671671
filemap_invalidate_lock(inode->i_mapping);

virt/kvm/kvm_mm.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
#ifndef __KVM_MM_H__
44
#define __KVM_MM_H__ 1
55

6+
#include <linux/kvm.h>
7+
#include <linux/kvm_types.h>
8+
69
/*
710
* Architectures can choose whether to use an rwlock or spinlock
811
* for the mmu_lock. These macros, for use in common code
@@ -72,7 +75,7 @@ int kvm_gmem_init(struct module *module);
7275
void kvm_gmem_exit(void);
7376
int kvm_gmem_create(struct kvm *kvm, struct kvm_create_guest_memfd *args);
7477
int kvm_gmem_bind(struct kvm *kvm, struct kvm_memory_slot *slot,
75-
unsigned int fd, loff_t offset);
78+
unsigned int fd, uoff_t offset);
7679
void kvm_gmem_unbind(struct kvm_memory_slot *slot);
7780
#else
7881
static inline int kvm_gmem_init(struct module *module)
@@ -82,7 +85,7 @@ static inline int kvm_gmem_init(struct module *module)
8285
static inline void kvm_gmem_exit(void) {};
8386
static inline int kvm_gmem_bind(struct kvm *kvm,
8487
struct kvm_memory_slot *slot,
85-
unsigned int fd, loff_t offset)
88+
unsigned int fd, uoff_t offset)
8689
{
8790
WARN_ON_ONCE(1);
8891
return -EIO;

0 commit comments

Comments
 (0)