Skip to contentSkip to conversation

KVM: sev: Fix int overflow in send|recieve_update_data ioctls

PATCH Superseded Peter Gonda 2 participants 5 messages Jan 9, 2023 – Jan 10, 2023

Subsystems (4)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 KVM: sev: Fix int overflow in send|recieve_update_data ioctls

KVM_SEV_SEND_UPDATE_DATA and KVM_SEV_RECEIVE_UPDATE_DATA have an integer overflow issue. Params.guest_len and offset are both 32bite wide, with a large params.guest_len the check to confirm a page boundary is not crossed can falsely pass:

    /* Check if we are crossing the page boundary *
    offset = params.guest_uaddr & (PAGE_SIZE - 1);
    if ((params.guest_len + offset > PAGE_SIZE))

Add an additional check to this conditional to confirm that params.guest_len itself is not greater than PAGE_SIZE.

The current code is can only overflow with a params.guest_len of greater than 0xfffff000. And the FW spec says these commands fail with lengths greater than 16KB. So this issue should not be a security concern

Fixes 15fb7de1a7f5 ("KVM: SVM: Add KVM_SEV_RECEIVE_UPDATE_DATA command")
Fixes d3d1af85e2c7 ("KVM: SVM: Add KVM_SEND_UPDATE_DATA command")
Cc David Rientjes <[email protected]>
Cc Paolo Bonzini <[email protected]>
Cc Sean Christopherson <[email protected]>
---
 arch/x86/kvm/svm/sev.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
arch/x86/kvm/svm/sev.c +2 -2
index 273cba809328..9451de72f917 100644
@@ -1294,7 +1294,7 @@ static int sev_send_update_data(struct kvm *kvm, struct kvm_sev_cmd *argp)
 
 	/* Check if we are crossing the page boundary */
 	offset = params.guest_uaddr & (PAGE_SIZE - 1);
-	if ((params.guest_len + offset > PAGE_SIZE))
+	if (params.guest_len > PAGE_SIZE || (params.guest_len + offset > PAGE_SIZE))
 		return -EINVAL;
 
 	/* Pin guest memory */
@@ -1474,7 +1474,7 @@ static int sev_receive_update_data(struct kvm *kvm, struct kvm_sev_cmd *argp)
 
 	/* Check if we are crossing the page boundary */
 	offset = params.guest_uaddr & (PAGE_SIZE - 1);
-	if ((params.guest_len + offset > PAGE_SIZE))
+	if (params.guest_len > PAGE_SIZE || (params.guest_len + offset > PAGE_SIZE))
 		return -EINVAL;
 
 	hdr = psp_copy_user_blob(params.hdr_uaddr, params.hdr_len);
Signature · 1 line
2.39.0.314.g84b9a713c41-goog
#1 lore raw
Quoted 36 lines · Peter Gonda

KVM_SEV_SEND_UPDATE_DATA and KVM_SEV_RECEIVE_UPDATE_DATA have an integer overflow issue. Params.guest_len and offset are both 32bite wide, with a large params.guest_len the check to confirm a page boundary is not crossed can falsely pass:

     /* Check if we are crossing the page boundary *
     offset = params.guest_uaddr & (PAGE_SIZE - 1);
     if ((params.guest_len + offset > PAGE_SIZE))

Add an additional check to this conditional to confirm that params.guest_len itself is not greater than PAGE_SIZE.

The current code is can only overflow with a params.guest_len of greater than 0xfffff000. And the FW spec says these commands fail with lengths greater than 16KB. So this issue should not be a security concern

Fixes 15fb7de1a7f5 ("KVM: SVM: Add KVM_SEV_RECEIVE_UPDATE_DATA command")
Fixes d3d1af85e2c7 ("KVM: SVM: Add KVM_SEND_UPDATE_DATA command")
Cc David Rientjes <[email protected]>
Cc Paolo Bonzini <[email protected]>
Cc Sean Christopherson <[email protected]>
---
  arch/x86/kvm/svm/sev.c | 4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)
arch/x86/kvm/svm/sev.c +0 -0
index 273cba809328..9451de72f917 100644

arch/x86/kvm/svm/sev.c static int sev_send_update_data(struct kvm *kvm, struct kvm_sev_cmd *argp) · line 1297

Quoted 5 lines
arch/x86/kvm/svm/sev.c +1 -1
@@ -1294,7 +1294,7 @@ static int sev_send_update_data(struct kvm *kvm, struct kvm_sev_cmd *argp)
 
  	/* Check if we are crossing the page boundary */
  	offset = params.guest_uaddr & (PAGE_SIZE - 1);
-	if ((params.guest_len + offset > PAGE_SIZE))
+	if (params.guest_len > PAGE_SIZE || (params.guest_len + offset > PAGE_SIZE))

I see the original if statement had double parentheses, which looks strange. Should this if (and the one below) be:

	if (params.guest_len > PAGE_SIZE || (params.guest_len + offset) > PAGE_SIZE)

Thanks,
Tom

Quoted 12 lines
arch/x86/kvm/svm/sev.c +1 -1
  		return -EINVAL;
 
  	/* Pin guest memory */
@@ -1474,7 +1474,7 @@ static int sev_receive_update_data(struct kvm *kvm, struct kvm_sev_cmd *argp)
 
  	/* Check if we are crossing the page boundary */
  	offset = params.guest_uaddr & (PAGE_SIZE - 1);
-	if ((params.guest_len + offset > PAGE_SIZE))
+	if (params.guest_len > PAGE_SIZE || (params.guest_len + offset > PAGE_SIZE))
  		return -EINVAL;
 
  	hdr = psp_copy_user_blob(params.hdr_uaddr, params.hdr_len);
#2 lore raw
Quoted 15 lines

arch/x86/kvm/svm/sev.c static int sev_send_update_data(struct kvm *kvm, struct kvm_sev_cmd *argp) · line 1297

Quoted 11 lines
arch/x86/kvm/svm/sev.c +1 -1
index 273cba809328..9451de72f917 100644
@@ -1294,7 +1294,7 @@ static int sev_send_update_data(struct kvm *kvm, struct kvm_sev_cmd *argp)
 
      /* Check if we are crossing the page boundary */
      offset = params.guest_uaddr & (PAGE_SIZE - 1);
-     if ((params.guest_len + offset > PAGE_SIZE))
+     if (params.guest_len > PAGE_SIZE || (params.guest_len + offset > PAGE_SIZE))

I see the original if statement had double parentheses, which looks strange. Should this if (and the one below) be:

Quoted 1 line
        if (params.guest_len > PAGE_SIZE || (params.guest_len + offset) > PAGE_SIZE)

Isn't the order of operations here: '+' and then '>'. So is the patch correct and matches the old conditional? I am fine adding additional () for clarity though.

#3 lore raw
Quoted 15 lines · Peter Gonda
Quoted 15 lines

arch/x86/kvm/svm/sev.c static int sev_send_update_data(struct kvm *kvm, struct kvm_sev_cmd *argp) · line 1297

Quoted 11 lines
arch/x86/kvm/svm/sev.c +1 -1
index 273cba809328..9451de72f917 100644
@@ -1294,7 +1294,7 @@ static int sev_send_update_data(struct kvm *kvm, struct kvm_sev_cmd *argp)
 
       /* Check if we are crossing the page boundary */
       offset = params.guest_uaddr & (PAGE_SIZE - 1);
-     if ((params.guest_len + offset > PAGE_SIZE))
+     if (params.guest_len > PAGE_SIZE || (params.guest_len + offset > PAGE_SIZE))

I see the original if statement had double parentheses, which looks strange. Should this if (and the one below) be:

Quoted 4 lines
Quoted 1 line
         if (params.guest_len > PAGE_SIZE || (params.guest_len + offset) > PAGE_SIZE)

Isn't the order of operations here: '+' and then '>'. So is the patch correct and matches the old conditional? I am fine adding additional

But what was the purpose of them in the old conditional? They weren't necessary.

But, yes, that order of operations is correct and those are both before '||'. So the extra parentheses around the second condition check are still strange then, right?

Given that, then:

	if (params.guest_len > PAGE_SIZE || params.guest_len + offset > PAGE_SIZE)
Quoted 1 line

() for clarity though.

I do like the look and clarity of the parentheses around the addition.

Thanks,
Tom

#4 lore raw
Quoted 29 lines · Tom Lendacky
Quoted 15 lines · Peter Gonda
Quoted 15 lines

arch/x86/kvm/svm/sev.c static int sev_send_update_data(struct kvm *kvm, struct kvm_sev_cmd *argp) · line 1297

Quoted 11 lines
arch/x86/kvm/svm/sev.c +1 -1
index 273cba809328..9451de72f917 100644
@@ -1294,7 +1294,7 @@ static int sev_send_update_data(struct kvm *kvm, struct kvm_sev_cmd *argp)
 
       /* Check if we are crossing the page boundary */
       offset = params.guest_uaddr & (PAGE_SIZE - 1);
-     if ((params.guest_len + offset > PAGE_SIZE))
+     if (params.guest_len > PAGE_SIZE || (params.guest_len + offset > PAGE_SIZE))

I see the original if statement had double parentheses, which looks strange. Should this if (and the one below) be:

Quoted 4 lines
Quoted 1 line
         if (params.guest_len > PAGE_SIZE || (params.guest_len + offset) > PAGE_SIZE)

Isn't the order of operations here: '+' and then '>'. So is the patch correct and matches the old conditional? I am fine adding additional

But what was the purpose of them in the old conditional? They weren't necessary.

But, yes, that order of operations is correct and those are both before '||'. So the extra parentheses around the second condition check are still strange then, right?

Quoted 7 lines

Given that, then:

        if (params.guest_len > PAGE_SIZE || params.guest_len + offset > PAGE_SIZE)
Quoted 1 line

() for clarity though.

I do like the look and clarity of the parentheses around the addition.

Sounds good to me. I'll update the V2 in a couple days to wait for any other comments.

Quoted 3 lines

Thanks,
Tom