Re: [PATCH v2 1/2] KVM: TDX: Reject INIT_MEM_REGION if number of bytes would overflow a u64
Binbin Wu <[email protected]>
| Newsgroups | org.kernel.vger.kvm,org.kernel.vger.linux-kernel |
|---|---|
| Message-ID | <[email protected]> |
On 8/7/2026 1:06 AM, Sean Christopherson wrote: > From: Haotian Jiang <[email protected]> > > The nr_pages field in struct kvm_tdx_init_mem_region is a u64 that comes > directly from userspace via copy_from_user(). The current validation > uses a manual overflow check: > > region.gpa + (region.nr_pages << PAGE_SHIFT) <= region.gpa > > When nr_pages >= 2^52, the shift (nr_pages << PAGE_SHIFT) wraps around > to a small value, bypassing the wrap check. While downstream protections > (gfn_to_memslot() returning NULL for GFNs outside any memslot, and > kvm_slot_has_gmem() checking for NULL) prevent any actual out-of-bounds > access, the overflow itself is a real bug that should be caught at the > validation layer. > > Replace the manual overflow check with check_shl_overflow() to correctly > detect the wrap-around. > > Note, the manual wrap-around check on the gpa+size technically has a benign > off-by-one bug, and can also use check_add_overflow(). Those flaws will be > addressed shortly. > > Opportunistically separate the initial sanity checks from the more involved > checks to try and make the code easier to read. > > Fixes: c846b451d3c5 ("KVM: TDX: Add an ioctl to create initial guest memory") > Reported-by: Sashiko Bot <[email protected]> > Closes: https://lore.kernel.org/all/[email protected] > Cc: Yan Zhao <[email protected]> > Cc: Binbin Wu <[email protected]> > Cc: Ackerley Tng <[email protected]> > Signed-off-by: Haotian Jiang <[email protected]> > [sean: use gpa_t, isolate check_shl_overflow() change, tweak changelog] > Signed-off-by: Sean Christopherson <[email protected]> Reviewed-by: Binbin Wu <[email protected]>