Skip to content

Commit

Permalink
KVM: SVM: CSV: Add KVM_CSV3_RECEIVE_ENCRYPT_DATA command
Browse files Browse the repository at this point in the history
hygon inclusion
category: feature
CVE: NA

---------------------------

The command is used for copying the incoming buffer into the
CSV3 guest's private memory.

Signed-off-by: Xin Jiang <jiangxin@hygon.cn>
Signed-off-by: hanliyang <hanliyang@hygon.cn>
  • Loading branch information
Xin Jiang authored and Avenger-285714 committed Sep 3, 2024
1 parent 2eec03f commit 943b908
Show file tree
Hide file tree
Showing 2 changed files with 132 additions and 0 deletions.
122 changes: 122 additions & 0 deletions arch/x86/kvm/svm/csv.c
Original file line number Diff line number Diff line change
Expand Up @@ -1546,6 +1546,125 @@ static int csv3_send_encrypt_context(struct kvm *kvm, struct kvm_sev_cmd *argp)
return ret;
}

static int csv3_receive_encrypt_data(struct kvm *kvm, struct kvm_sev_cmd *argp)
{
struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
struct kvm_csv_info *csv = &to_kvm_svm_csv(kvm)->csv_info;
struct csv3_data_receive_encrypt_data data;
struct kvm_csv3_receive_encrypt_data params;
void *hdr;
void *trans_data;
struct trans_paddr_block *trans_block;
struct guest_paddr_block *guest_block;
unsigned long pfn;
int i;
u32 offset;
int ret = 0;

if (!csv3_guest(kvm))
return -ENOTTY;

if (unlikely(list_empty(&csv->smr_list))) {
/* Allocate all the guest memory from CMA */
ret = csv3_set_guest_private_memory(kvm);
if (ret)
goto exit;
}

if (copy_from_user(&params, (void __user *)(uintptr_t)argp->data,
sizeof(params)))
return -EFAULT;

if (!params.hdr_uaddr || !params.hdr_len ||
!params.guest_addr_data || !params.guest_addr_len ||
!params.trans_uaddr || !params.trans_len)
return -EINVAL;

if (params.guest_addr_len > sizeof(*guest_block))
return -EINVAL;

if (params.trans_len > ARRAY_SIZE(trans_block->trans_paddr) * PAGE_SIZE)
return -EINVAL;

/* allocate memory for header and transport buffer */
hdr = kzalloc(params.hdr_len, GFP_KERNEL_ACCOUNT);
if (!hdr) {
ret = -ENOMEM;
goto exit;
}

if (copy_from_user(hdr,
(void __user *)(uintptr_t)params.hdr_uaddr,
params.hdr_len)) {
ret = -EFAULT;
goto e_free_hdr;
}

guest_block = kzalloc(sizeof(*guest_block), GFP_KERNEL_ACCOUNT);
if (!guest_block) {
ret = -ENOMEM;
goto e_free_hdr;
}

if (copy_from_user(guest_block,
(void __user *)(uintptr_t)params.guest_addr_data,
params.guest_addr_len)) {
ret = -EFAULT;
goto e_free_guest_block;
}

trans_block = kzalloc(sizeof(*trans_block), GFP_KERNEL_ACCOUNT);
if (!trans_block) {
ret = -ENOMEM;
goto e_free_guest_block;
}
trans_data = vzalloc(params.trans_len);
if (!trans_data) {
ret = -ENOMEM;
goto e_free_trans_block;
}

if (copy_from_user(trans_data,
(void __user *)(uintptr_t)params.trans_uaddr,
params.trans_len)) {
ret = -EFAULT;
goto e_free_trans_data;
}

for (offset = 0, i = 0; offset < params.trans_len; offset += PAGE_SIZE) {
pfn = vmalloc_to_pfn(offset + trans_data);
trans_block->trans_paddr[i] = __sme_set(pfn_to_hpa(pfn));
i++;
}

memset(&data, 0, sizeof(data));
data.hdr_address = __psp_pa(hdr);
data.hdr_len = params.hdr_len;
data.trans_block = __psp_pa(trans_block);
data.trans_len = params.trans_len;
data.guest_block = __psp_pa(guest_block);
data.guest_len = params.guest_addr_len;
data.handle = sev->handle;

clflush_cache_range(hdr, params.hdr_len);
clflush_cache_range(trans_data, params.trans_len);
clflush_cache_range(trans_block, PAGE_SIZE);
clflush_cache_range(guest_block, PAGE_SIZE);
ret = hygon_kvm_hooks.sev_issue_cmd(kvm, CSV3_CMD_RECEIVE_ENCRYPT_DATA,
&data, &argp->error);

e_free_trans_data:
vfree(trans_data);
e_free_trans_block:
kfree(trans_block);
e_free_guest_block:
kfree(guest_block);
e_free_hdr:
kfree(hdr);
exit:
return ret;
}

static void csv3_mark_page_dirty(struct kvm_vcpu *vcpu, gva_t gpa,
unsigned long npages)
{
Expand Down Expand Up @@ -2024,6 +2143,9 @@ static int csv_mem_enc_ioctl(struct kvm *kvm, void __user *argp)
case KVM_CSV3_SEND_ENCRYPT_CONTEXT:
r = csv3_send_encrypt_context(kvm, &sev_cmd);
break;
case KVM_CSV3_RECEIVE_ENCRYPT_DATA:
r = csv3_receive_encrypt_data(kvm, &sev_cmd);
break;
default:
/*
* If the command is compatible between CSV and SEV, the
Expand Down
10 changes: 10 additions & 0 deletions include/uapi/linux/kvm.h
Original file line number Diff line number Diff line change
Expand Up @@ -2319,6 +2319,7 @@ enum csv3_cmd_id {
KVM_CSV3_LAUNCH_ENCRYPT_VMCB,
KVM_CSV3_SEND_ENCRYPT_DATA,
KVM_CSV3_SEND_ENCRYPT_CONTEXT,
KVM_CSV3_RECEIVE_ENCRYPT_DATA,

KVM_CSV3_NR_MAX,
};
Expand Down Expand Up @@ -2349,4 +2350,13 @@ struct kvm_csv3_send_encrypt_context {
__u32 trans_len;
};

struct kvm_csv3_receive_encrypt_data {
__u64 hdr_uaddr;
__u32 hdr_len;
__u64 guest_addr_data;
__u32 guest_addr_len;
__u64 trans_uaddr;
__u32 trans_len;
};

#endif /* __LINUX_KVM_H */

0 comments on commit 943b908

Please sign in to comment.