]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
KVM: TDX: Add tdx_get_cmd() helper to get and validate sub-ioctl command
authorSean Christopherson <seanjc@google.com>
Thu, 30 Oct 2025 20:09:45 +0000 (13:09 -0700)
committerSean Christopherson <seanjc@google.com>
Wed, 5 Nov 2025 19:17:29 +0000 (11:17 -0800)
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 <rick.p.edgecombe@intel.com>
Reviewed-by: Kai Huang <kai.huang@intel.com>
Reviewed-by: Binbin Wu <binbin.wu@linux.intel.com>
Reviewed-by: Yan Zhao <yan.y.zhao@intel.com>
Tested-by: Yan Zhao <yan.y.zhao@intel.com>
Tested-by: Kai Huang <kai.huang@intel.com>
Link: https://patch.msgid.link/20251030200951.3402865-23-seanjc@google.com
Signed-off-by: Sean Christopherson <seanjc@google.com>
arch/x86/kvm/vmx/tdx.c

index 88c85fd82a0ee6f46aa5d3c204a1af1a7bfff4e1..f42cc955d49c926f154939f2040fe2ce72c94551 100644 (file)
@@ -2792,21 +2792,30 @@ static int tdx_td_finalize(struct kvm *kvm, struct kvm_tdx_cmd *cmd)
        return 0;
 }
 
-int tdx_vm_ioctl(struct kvm *kvm, void __user *argp)
+static int tdx_get_cmd(void __user *argp, struct kvm_tdx_cmd *cmd)
 {
-       struct kvm_tdx_cmd tdx_cmd;
-       int r;
-
-       if (copy_from_user(&tdx_cmd, argp, sizeof(struct kvm_tdx_cmd)))
+       if (copy_from_user(cmd, argp, sizeof(*cmd)))
                return -EFAULT;
 
        /*
-        * Userspace should never set hw_error. It is used to fill
-        * hardware-defined error by the kernel.
+        * Userspace should never set hw_error.  KVM writes hw_error to report
+        * hardware-defined error back to userspace.
         */
-       if (tdx_cmd.hw_error)
+       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;
+
+       r = tdx_get_cmd(argp, &tdx_cmd);
+       if (r)
+               return r;
+
        mutex_lock(&kvm->lock);
 
        switch (tdx_cmd.id) {
@@ -3181,11 +3190,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: