Add a helper to copy a kvm_tdx_cmd structure from userspace and verify
that must-be-zero fields are indeed zero.

No functional change intended.

Reviewed-by: Rick Edgecombe <[email protected]>
Reviewed-by: Kai Huang <[email protected]>
Signed-off-by: Sean Christopherson <[email protected]>
---
 arch/x86/kvm/vmx/tdx.c | 35 +++++++++++++++++++++--------------
 1 file changed, 21 insertions(+), 14 deletions(-)

diff --git a/arch/x86/kvm/vmx/tdx.c b/arch/x86/kvm/vmx/tdx.c
index 97632fc6b520..390c934562c1 100644
--- a/arch/x86/kvm/vmx/tdx.c
+++ b/arch/x86/kvm/vmx/tdx.c
@@ -2782,20 +2782,29 @@ static int tdx_td_finalize(struct kvm *kvm, struct 
kvm_tdx_cmd *cmd)
        return 0;
 }
 
+static int tdx_get_cmd(void __user *argp, struct kvm_tdx_cmd *cmd)
+{
+       if (copy_from_user(cmd, argp, sizeof(*cmd)))
+               return -EFAULT;
+
+       /*
+        * Userspace should never set hw_error.  KVM writes hw_error to report
+        * hardware-defined error back to userspace.
+        */
+       if (cmd->hw_error)
+               return -EINVAL;
+
+       return 0;
+}
+
 int tdx_vm_ioctl(struct kvm *kvm, void __user *argp)
 {
        struct kvm_tdx_cmd tdx_cmd;
        int r;
 
-       if (copy_from_user(&tdx_cmd, argp, sizeof(struct kvm_tdx_cmd)))
-               return -EFAULT;
-
-       /*
-        * Userspace should never set hw_error. It is used to fill
-        * hardware-defined error by the kernel.
-        */
-       if (tdx_cmd.hw_error)
-               return -EINVAL;
+       r = tdx_get_cmd(argp, &tdx_cmd);
+       if (r)
+               return r;
 
        mutex_lock(&kvm->lock);
 
@@ -3171,11 +3180,9 @@ int tdx_vcpu_ioctl(struct kvm_vcpu *vcpu, void __user 
*argp)
        if (!is_hkid_assigned(kvm_tdx) || kvm_tdx->state == TD_STATE_RUNNABLE)
                return -EINVAL;
 
-       if (copy_from_user(&cmd, argp, sizeof(cmd)))
-               return -EFAULT;
-
-       if (cmd.hw_error)
-               return -EINVAL;
+       ret = tdx_get_cmd(argp, &cmd);
+       if (ret)
+               return ret;
 
        switch (cmd.id) {
        case KVM_TDX_INIT_VCPU:
-- 
2.51.1.930.gacf6e81ea2-goog


Reply via email to