From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pl1-f201.google.com (mail-pl1-f201.google.com [209.85.214.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 852F83859F3 for ; Tue, 2 Jun 2026 17:09:24 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780420167; cv=none; b=sLtuQ0MEmlOZ+WRVYDNqE3kZYQy20E6rEP5IxzKAn2e1HXXJUfsmdLk7FuxDQeJVB3FPJbaXq7pVWObtAUlStHwtMT6/oArsdwjiONUXSHtbcPN+JHDVK4BP3T6RQC90koWzdKOieHHmqdAMpX8t952vgGekgQlOQJz0MKygZU0= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780420167; c=relaxed/simple; bh=zXVaPGZ4QHBBXBt4EQ+3Iyp8TBCruXvDKknQdm+GMz8=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=tJGvkYsKHQtxnW52IUoJtjTqnB8JR7BNbK7c9Fjh0BC/itQpVOnkYe4Wu6b2Yf6ChxQ9mDFmTVn/3cy4k4jgwrC6uHPpCIOGldrQlOqGnsLCMN60ebe4R4jocNmzyvVk0EpqMDWKZ4WH4JNVf+WmOYm9o8ek6Xl1x/cnqGxWfgE= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=TVs0tCQJ; arc=none smtp.client-ip=209.85.214.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="TVs0tCQJ" Received: by mail-pl1-f201.google.com with SMTP id d9443c01a7336-2bf30576aa3so27083125ad.3 for ; Tue, 02 Jun 2026 10:09:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1780420164; x=1781024964; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=G8m+N8ql2TMEshzUG0QqHbQGCheVGCGULcRY+JWxijg=; b=TVs0tCQJDUfRWcVUsMXLC6jT32/ZlA32NT9j1UKN7MOuvWgNMgiW1Ij2lpp/lT5+3V g8+WYIhC6eG5i2b6GBmNghi1UfNP8uUSFiyfVVOEhzGC9oi/xJF7Umr11DJhonS8V01N PLGYt+MAr9GinTSq/xhVbIULy53iRHgaRtAThv+aHHhb+fOuBkFLr2qiSNGDXy5D0H7j 4X5Hwb909GT4wiFhc0NXksKPQNKB4ThzXHxEjaTV/gTNmiPIwv96jdO1nUR1ryZYhszn 2XCe8bvFjuw9yd/fHEnJBc0qd9O9kgjGqsPzVogSne7Rlda3/b2sszZ4mEfVb8smzll7 /iLA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1780420164; x=1781024964; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=G8m+N8ql2TMEshzUG0QqHbQGCheVGCGULcRY+JWxijg=; b=Q6/MwiWy0rIW88VNGxPuRl7m6Dkk7KhLq/nPo98ekxrIiUkPlm8PMkOW6wSwIwxSTd rcXktnTeLsERIhLHd80hOoOY+UauhY5LbyA+lxfS7xkt6KGyILJs0xlkKdezsvuk8YfV 8TOPYg2FA0izPZEltsTo2C5TbZt76fjVgXoK2tZ/lX2VQFXPzCWYCIiDTtBwN/uH5TdV RRF8UYin9ljQU8LGe5GOauT05PjjPXVA90leLvtFsGnCAdKVM3F7OH9I4m7ZngeFJxTX m5rn8QfrmgYOcWquh8VzzpAbLJahp+ZksAKObM5BJBuhCQMpPnQXK3tQ6vhNz+rwc4Nu q4Fg== X-Forwarded-Encrypted: i=1; AFNElJ+rM4cQoa28SCZeiO0A5fhjlRM/tCP906qDHKFk/RLa0Uxq40PoyFW0IpPIDo3i8IjsyJK3Qqj4edmoj0s=@vger.kernel.org X-Gm-Message-State: AOJu0YylHUKkQfgnHe7Lm582A2JjriNadGubNODMw6rchXohYPfQIMYb UOxjeG3CweA/SOP7iRHedSpbd0JX0FTm43kiusTQ0Z/mErDI6u2itPArgVSDcVLzp6Xu9BA9Ec1 BUH5WEA== X-Received: from plry22.prod.google.com ([2002:a17:902:b496:b0:2bd:9e64:2df1]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:902:ce8d:b0:2c0:c625:4011 with SMTP id d9443c01a7336-2c0c62543ffmr134938685ad.4.1780420163529; Tue, 02 Jun 2026 10:09:23 -0700 (PDT) Reply-To: Sean Christopherson Date: Tue, 2 Jun 2026 10:09:19 -0700 In-Reply-To: <20260602170921.1304394-1-seanjc@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260602170921.1304394-1-seanjc@google.com> X-Mailer: git-send-email 2.54.0.1013.g208068f2d8-goog Message-ID: <20260602170921.1304394-2-seanjc@google.com> Subject: [PATCH v4 1/3] KVM: guest_memfd: Treat memslot binding offset+size as unsigned values From: Sean Christopherson To: Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Ackerley Tng , Michael Roth , Sean Christopherson Content-Type: text/plain; charset="UTF-8" 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: a7800aa80ea4 ("KVM: Add KVM_CREATE_GUEST_MEMFD ioctl() for guest-specific backing memory") Cc: stable@vger.kernel.org Cc: Ackerley Tng Reviewed-by: Michael Roth Reviewed-by: Ackerley Tng Signed-off-by: Sean Christopherson --- virt/kvm/guest_memfd.c | 8 ++++---- virt/kvm/kvm_mm.h | 7 +++++-- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/virt/kvm/guest_memfd.c b/virt/kvm/guest_memfd.c index bf9659a7b0f6..a1cb72e66288 100644 --- a/virt/kvm/guest_memfd.c +++ b/virt/kvm/guest_memfd.c @@ -640,15 +640,16 @@ int kvm_gmem_create(struct kvm *kvm, struct kvm_create_guest_memfd *args) } int kvm_gmem_bind(struct kvm *kvm, struct kvm_memory_slot *slot, - unsigned int fd, loff_t offset) + unsigned int fd, uoff_t offset) { - loff_t size = slot->npages << PAGE_SHIFT; + uoff_t size = slot->npages << PAGE_SHIFT; unsigned long start, end; struct gmem_file *f; struct inode *inode; struct file *file; int r = -EINVAL; + BUILD_BUG_ON(sizeof(gpa_t) != sizeof(offset)); BUILD_BUG_ON(sizeof(gfn_t) != sizeof(slot->gmem.pgoff)); file = fget(fd); @@ -664,8 +665,7 @@ int kvm_gmem_bind(struct kvm *kvm, struct kvm_memory_slot *slot, inode = file_inode(file); - if (offset < 0 || !PAGE_ALIGNED(offset) || - offset + size > i_size_read(inode)) + if (!PAGE_ALIGNED(offset) || offset + size > i_size_read(inode)) goto err; filemap_invalidate_lock(inode->i_mapping); diff --git a/virt/kvm/kvm_mm.h b/virt/kvm/kvm_mm.h index 9fcc5d5b7f8d..7510ca915dd1 100644 --- a/virt/kvm/kvm_mm.h +++ b/virt/kvm/kvm_mm.h @@ -3,6 +3,9 @@ #ifndef __KVM_MM_H__ #define __KVM_MM_H__ 1 +#include +#include + /* * Architectures can choose whether to use an rwlock or spinlock * for the mmu_lock. These macros, for use in common code @@ -72,7 +75,7 @@ int kvm_gmem_init(struct module *module); void kvm_gmem_exit(void); int kvm_gmem_create(struct kvm *kvm, struct kvm_create_guest_memfd *args); int kvm_gmem_bind(struct kvm *kvm, struct kvm_memory_slot *slot, - unsigned int fd, loff_t offset); + unsigned int fd, uoff_t offset); void kvm_gmem_unbind(struct kvm_memory_slot *slot); #else static inline int kvm_gmem_init(struct module *module) @@ -82,7 +85,7 @@ static inline int kvm_gmem_init(struct module *module) static inline void kvm_gmem_exit(void) {}; static inline int kvm_gmem_bind(struct kvm *kvm, struct kvm_memory_slot *slot, - unsigned int fd, loff_t offset) + unsigned int fd, uoff_t offset) { WARN_ON_ONCE(1); return -EIO; -- 2.54.0.929.g9b7fa37559-goog