Skip to contentSkip to conversation

KVM: TDX: INIT_MEM_REGION fixes

PATCH Applied Sean Christopherson 4 participants 10 messages Aug 6 – Sep 22

Subsystems (4)
lore
Share & tools
lore

Sean Christopherson

author maintainer
#0 lore raw

v2 cover KVM: TDX: INIT_MEM_REGION fixes

I completely lost track of this, meant to post these patches weeks ago. Compile tested only.

v2:
- Fix an off-by-one bug.
- Use gpa_t instead of raw u64.
- Tweak the order of checks to bundle similar checks together, and to perform the super basic checks (alignment, size, etc.) first.

v1: https://lore.kernel.org/all/[email protected]

Haotian Jiang (1):
KVM: TDX: Reject INIT_MEM_REGION if number of bytes would overflow a

    u64

Sean Christopherson (1):
KVM: TDX: Fix a benign off-by-one bug on the end GPA for
INIT_MEM_REGION

 arch/x86/kvm/vmx/tdx.c | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)

base-commit: a806d364ef288a6443a1337820ea8410a7ccc6b3

Signature · 1 line
2.55.0.679.g6767b8d81c-goog
author maintainer
#1 lore raw

PATCH v2 1/2 KVM: TDX: Reject INIT_MEM_REGION if number of bytes would overflow a u64

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")
Cc Yan Zhao <[email protected]>
Cc Binbin Wu <[email protected]>
Cc Ackerley Tng <[email protected]>
[sean: use gpa_t, isolate check_shl_overflow() change, tweak changelog]
---
 arch/x86/kvm/vmx/tdx.c | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)
arch/x86/kvm/vmx/tdx.c +7 -3
index b272c20586a7..929115aeb9ec 100644
@@ -3219,6 +3219,7 @@ static int tdx_vcpu_init_mem_region(struct kvm_vcpu *vcpu, struct kvm_tdx_cmd *c
 	struct kvm_tdx *kvm_tdx = to_kvm_tdx(kvm);
 	struct kvm_tdx_init_mem_region region;
 	struct tdx_gmem_post_populate_arg arg;
+	gpa_t nr_bytes;
 	long gmem_ret;
 	int ret;
 
@@ -3236,10 +3237,13 @@ static int tdx_vcpu_init_mem_region(struct kvm_vcpu *vcpu, struct kvm_tdx_cmd *c
 		return -EFAULT;
 
 	if (!PAGE_ALIGNED(region.source_addr) || !region.source_addr ||
-	    !PAGE_ALIGNED(region.gpa) || !region.nr_pages ||
-	    region.gpa + (region.nr_pages << PAGE_SHIFT) <= region.gpa ||
+	    !PAGE_ALIGNED(region.gpa) || !region.nr_pages)
+		return -EINVAL;
+
+	if (check_shl_overflow(region.nr_pages, PAGE_SHIFT, &nr_bytes) ||
+	    region.gpa + nr_bytes <= region.gpa ||
 	    !vt_is_tdx_private_gpa(kvm, region.gpa) ||
-	    !vt_is_tdx_private_gpa(kvm, region.gpa + (region.nr_pages << PAGE_SHIFT) - 1))
+	    !vt_is_tdx_private_gpa(kvm, region.gpa + nr_bytes - 1))
 		return -EINVAL;
 
 	ret = 0;
Signature · 1 line
2.55.0.679.g6767b8d81c-goog
Reviewed-by Tested-by on v2 1/2
#2 lore raw

Re: [PATCH v2 1/2] KVM: TDX: Reject INIT_MEM_REGION if number of bytes would overflow a u64

Quoted 25 lines · Sean Christopherson

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.

Quoted 9 lines
Fixes c846b451d3c5 ("KVM: TDX: Add an ioctl to create initial guest memory")
Cc Yan Zhao <[email protected]>
Cc Binbin Wu <[email protected]>
Cc Ackerley Tng <[email protected]>
[sean: use gpa_t, isolate check_shl_overflow() change, tweak changelog]
Quoted 35 lines
 arch/x86/kvm/vmx/tdx.c | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)
arch/x86/kvm/vmx/tdx.c +7 -3
index b272c20586a7..929115aeb9ec 100644
@@ -3219,6 +3219,7 @@ static int tdx_vcpu_init_mem_region(struct kvm_vcpu *vcpu, struct kvm_tdx_cmd *c
 	struct kvm_tdx *kvm_tdx = to_kvm_tdx(kvm);
 	struct kvm_tdx_init_mem_region region;
 	struct tdx_gmem_post_populate_arg arg;
+	gpa_t nr_bytes;
 	long gmem_ret;
 	int ret;
 
@@ -3236,10 +3237,13 @@ static int tdx_vcpu_init_mem_region(struct kvm_vcpu *vcpu, struct kvm_tdx_cmd *c
 		return -EFAULT;
 
 	if (!PAGE_ALIGNED(region.source_addr) || !region.source_addr ||
-	    !PAGE_ALIGNED(region.gpa) || !region.nr_pages ||
-	    region.gpa + (region.nr_pages << PAGE_SHIFT) <= region.gpa ||
+	    !PAGE_ALIGNED(region.gpa) || !region.nr_pages)
+		return -EINVAL;
+
+	if (check_shl_overflow(region.nr_pages, PAGE_SHIFT, &nr_bytes) ||
+	    region.gpa + nr_bytes <= region.gpa ||
 	    !vt_is_tdx_private_gpa(kvm, region.gpa) ||
-	    !vt_is_tdx_private_gpa(kvm, region.gpa + (region.nr_pages << PAGE_SHIFT) - 1))
+	    !vt_is_tdx_private_gpa(kvm, region.gpa + nr_bytes - 1))
 		return -EINVAL;
 
 	ret = 0;
Signature · 1 line
2.55.0.679.g6767b8d81c-goog
Reviewed-by on v2 1/2
#3 lore raw

Re: [PATCH v2 1/2] KVM: TDX: Reject INIT_MEM_REGION if number of bytes would overflow a u64

In reply to Sean Christopherson
Quoted 25 lines · Sean Christopherson

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.

Quoted 9 lines
Fixes c846b451d3c5 ("KVM: TDX: Add an ioctl to create initial guest memory")
Cc Yan Zhao <[email protected]>
Cc Binbin Wu <[email protected]>
Cc Ackerley Tng <[email protected]>
[sean: use gpa_t, isolate check_shl_overflow() change, tweak changelog]
author maintainer
#4 lore raw

PATCH v2 2/2 KVM: TDX: Fix a benign off-by-one bug on the end GPA for INIT_MEM_REGION

In reply to Sean Christopherson

When verifying that the incoming GPA rage for INIT_MEM_REGION doesn't wrap, check the inclusive last GPA, not the exclusive last GPA. Super duper technically, it's ok if the very last GPA is -1ull. In practice, the flaw is benign as KVM x86 disallows memslots with GPAs that exceed MAXPHYADDR, i.e. INIT_MEM_REGION would fail with -EINVAL anyways due to the memslot check in kvm_gmem_populate().

Opportunistically use check_add_overflow() instead of manually checking for wrap, mostly so that the inclusive math doesn't need to be copy+pasted in the "is private" check.

Fixes c846b451d3c5 ("KVM: TDX: Add an ioctl to create initial guest memory")
---
 arch/x86/kvm/vmx/tdx.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
arch/x86/kvm/vmx/tdx.c +3 -3
index 929115aeb9ec..85699ea021fe 100644
@@ -3219,7 +3219,7 @@ static int tdx_vcpu_init_mem_region(struct kvm_vcpu *vcpu, struct kvm_tdx_cmd *c
 	struct kvm_tdx *kvm_tdx = to_kvm_tdx(kvm);
 	struct kvm_tdx_init_mem_region region;
 	struct tdx_gmem_post_populate_arg arg;
-	gpa_t nr_bytes;
+	gpa_t nr_bytes, end_gpa;
 	long gmem_ret;
 	int ret;
 
@@ -3241,9 +3241,9 @@ static int tdx_vcpu_init_mem_region(struct kvm_vcpu *vcpu, struct kvm_tdx_cmd *c
 		return -EINVAL;
 
 	if (check_shl_overflow(region.nr_pages, PAGE_SHIFT, &nr_bytes) ||
-	    region.gpa + nr_bytes <= region.gpa ||
+	    check_add_overflow(region.gpa, nr_bytes - 1, &end_gpa) ||
 	    !vt_is_tdx_private_gpa(kvm, region.gpa) ||
-	    !vt_is_tdx_private_gpa(kvm, region.gpa + nr_bytes - 1))
+	    !vt_is_tdx_private_gpa(kvm, end_gpa))
 		return -EINVAL;
 
 	ret = 0;
Signature · 1 line
2.55.0.679.g6767b8d81c-goog
Reviewed-by Tested-by on v2 2/2
#5 lore raw

Re: [PATCH v2 2/2] KVM: TDX: Fix a benign off-by-one bug on the end GPA for INIT_MEM_REGION

Quoted 31 lines · Sean Christopherson

When verifying that the incoming GPA rage for INIT_MEM_REGION doesn't wrap, check the inclusive last GPA, not the exclusive last GPA. Super duper technically, it's ok if the very last GPA is -1ull. In practice, the flaw is benign as KVM x86 disallows memslots with GPAs that exceed MAXPHYADDR, i.e. INIT_MEM_REGION would fail with -EINVAL anyways due to the memslot check in kvm_gmem_populate().

Opportunistically use check_add_overflow() instead of manually checking for wrap, mostly so that the inclusive math doesn't need to be copy+pasted in the "is private" check.

Fixes c846b451d3c5 ("KVM: TDX: Add an ioctl to create initial guest memory")
---
 arch/x86/kvm/vmx/tdx.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
arch/x86/kvm/vmx/tdx.c +1 -1
index 929115aeb9ec..85699ea021fe 100644
@@ -3219,7 +3219,7 @@ static int tdx_vcpu_init_mem_region(struct kvm_vcpu *vcpu, struct kvm_tdx_cmd *c
 	struct kvm_tdx *kvm_tdx = to_kvm_tdx(kvm);
 	struct kvm_tdx_init_mem_region region;
 	struct tdx_gmem_post_populate_arg arg;
-	gpa_t nr_bytes;
+	gpa_t nr_bytes, end_gpa;
 	long gmem_ret;
 	int ret;
 

arch/x86/kvm/vmx/tdx.c static int tdx_vcpu_init_mem_region(struct kvm_vcpu *vcpu, struct kvm_tdx_cmd *c · line 3244

Quoted 5 lines
arch/x86/kvm/vmx/tdx.c +1 -1
@@ -3241,9 +3241,9 @@ static int tdx_vcpu_init_mem_region(struct kvm_vcpu *vcpu, struct kvm_tdx_cmd *c
 		return -EINVAL;
 
 	if (check_shl_overflow(region.nr_pages, PAGE_SHIFT, &nr_bytes) ||
-	    region.gpa + nr_bytes <= region.gpa ||
+	    check_add_overflow(region.gpa, nr_bytes - 1, &end_gpa) ||

Nit: explain in patch log that check_add_overflow() does not catch the case where "nr_bytes == 0", unlike the check in "region.gpa + nr_bytes <= region.gpa"? However, this is already guarded by the preceding "!region.nr_pages" check.

Quoted 9 lines
arch/x86/kvm/vmx/tdx.c +1 -1
 	    !vt_is_tdx_private_gpa(kvm, region.gpa) ||
-	    !vt_is_tdx_private_gpa(kvm, region.gpa + nr_bytes - 1))
+	    !vt_is_tdx_private_gpa(kvm, end_gpa))
 		return -EINVAL;
 
 	ret = 0;
Signature · 1 line
2.55.0.679.g6767b8d81c-goog
Reviewed-by on v2 2/2
#6 lore raw

Re: [PATCH v2 2/2] KVM: TDX: Fix a benign off-by-one bug on the end GPA for INIT_MEM_REGION

In reply to Sean Christopherson
Quoted 7 lines · Sean Christopherson

When verifying that the incoming GPA rage for INIT_MEM_REGION doesn't wrap, check the inclusive last GPA, not the exclusive last GPA. Super duper technically, it's ok if the very last GPA is -1ull. In practice, the flaw is benign as KVM x86 disallows memslots with GPAs that exceed MAXPHYADDR, i.e. INIT_MEM_REGION would fail with -EINVAL anyways due to the memslot check in kvm_gmem_populate().

Quoted 6 lines

Opportunistically use check_add_overflow() instead of manually checking for wrap, mostly so that the inclusive math doesn't need to be copy+pasted in the "is private" check.

Fixes c846b451d3c5 ("KVM: TDX: Add an ioctl to create initial guest memory")
author maintainer Applied
#7 lore raw
In reply to Sean Christopherson
Quoted 10 lines · Sean Christopherson

I completely lost track of this, meant to post these patches weeks ago. Compile tested only.

v2:
- Fix an off-by-one bug.
- Use gpa_t instead of raw u64.
- Tweak the order of checks to bundle similar checks together, and to perform the super basic checks (alignment, size, etc.) first.

[...]

Applied to kvm-x86 vmx, thanks!

[1/2] KVM: TDX: Reject INIT_MEM_REGION if number of bytes would overflow a u64

      https://github.com/kvm-x86/linux/commit/7c9a2e3a4564
[2/2] KVM: TDX: Fix a benign off-by-one bug on the end GPA for INIT_MEM_REGION
      https://github.com/kvm-x86/linux/commit/872aae89167c

--
https://github.com/kvm-x86/linux/tree/next

Reviewed-by on the v2 series
#8 lore raw
In reply to Sean Christopherson
Quoted 19 lines · Sean Christopherson

I completely lost track of this, meant to post these patches weeks ago. Compile tested only.

v2:
- Fix an off-by-one bug.
- Use gpa_t instead of raw u64.
- Tweak the order of checks to bundle similar checks together, and to perform the super basic checks (alignment, size, etc.) first.

v1: https://lore.kernel.org/all/[email protected]

Haotian Jiang (1):
KVM: TDX: Reject INIT_MEM_REGION if number of bytes would overflow a

    u64

Sean Christopherson (1):
KVM: TDX: Fix a benign off-by-one bug on the end GPA for
INIT_MEM_REGION

Quoted 7 lines
 arch/x86/kvm/vmx/tdx.c | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)

base-commit: a806d364ef288a6443a1337820ea8410a7ccc6b3 --
2.55.0.679.g6767b8d81c-goog

Looks good to me, just a question on the use of check_*_overflow: I thought Sean didn't like burying an out-param in an if-statement? [1]

[1] https://lore.kernel.org/all/[email protected]/

author maintainer
#9 lore raw
Quoted 25 lines · Ackerley Tng
Quoted 23 lines · Sean Christopherson

I completely lost track of this, meant to post these patches weeks ago. Compile tested only.

v2:
- Fix an off-by-one bug.
- Use gpa_t instead of raw u64.
- Tweak the order of checks to bundle similar checks together, and to perform the super basic checks (alignment, size, etc.) first.

v1: https://lore.kernel.org/all/[email protected]

Haotian Jiang (1):
KVM: TDX: Reject INIT_MEM_REGION if number of bytes would overflow a

    u64

Sean Christopherson (1):
KVM: TDX: Fix a benign off-by-one bug on the end GPA for
INIT_MEM_REGION

 arch/x86/kvm/vmx/tdx.c | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)
Quoted 6 lines
Quoted 3 lines

base-commit: a806d364ef288a6443a1337820ea8410a7ccc6b3 --
2.55.0.679.g6767b8d81c-goog

Looks good to me, just a question on the use of check_*_overflow: I thought Sean didn't like burying an out-param in an if-statement? [1]

I don't, but in this case I found it to be the lesser evil. I think it helps that both the out-params are used only in the same if-statement. One could argue that's even *more* evil, but t doesn't "surprise" me as much, e.g. I'm not having to hunt for where some variable in the main loop is set.

Quoted 1 line

[1] https://lore.kernel.org/all/[email protected]