mirror of
https://git.kernel.org/pub/scm/linux/kernel/git/herbert/cryptodev-2.6.git
synced 2026-05-02 18:17:50 -04:00
virt: sev-guest: Move SNP Guest Request data pages handling under snp_cmd_mutex
Compared to the SNP Guest Request, the "Extended" version adds data pages for receiving certificates. If not enough pages provided, the HV can report to the VM how much is needed so the VM can reallocate and repeat. Commitae596615d9("virt: sev-guest: Reduce the scope of SNP command mutex") moved handling of the allocated/desired pages number out of scope of said mutex and create a possibility for a race (multiple instances trying to trigger Extended request in a VM) as there is just one instance of snp_msg_desc per /dev/sev-guest and no locking other than snp_cmd_mutex. Fix the issue by moving the data blob/size and the GHCB input struct (snp_req_data) into snp_guest_req which is allocated on stack now and accessed by the GHCB caller under that mutex. Stop allocating SEV_FW_BLOB_MAX_SIZE in snp_msg_alloc() as only one of four callers needs it. Free the received blob in get_ext_report() right after it is copied to the userspace. Possible future users of snp_send_guest_request() are likely to have different ideas about the buffer size anyways. Fixes:ae596615d9("virt: sev-guest: Reduce the scope of SNP command mutex") Signed-off-by: Alexey Kardashevskiy <aik@amd.com> Signed-off-by: Borislav Petkov (AMD) <bp@alien8.de> Reviewed-by: Nikunj A Dadhania <nikunj@amd.com> Cc: stable@vger.kernel.org Link: https://lore.kernel.org/r/20250307013700.437505-3-aik@amd.com
This commit is contained in:
committed by
Borislav Petkov (AMD)
parent
ac7c06acaa
commit
3e385c0d6c
@@ -176,6 +176,7 @@ static int get_ext_report(struct snp_guest_dev *snp_dev, struct snp_guest_reques
|
||||
struct snp_guest_req req = {};
|
||||
int ret, npages = 0, resp_len;
|
||||
sockptr_t certs_address;
|
||||
struct page *page;
|
||||
|
||||
if (sockptr_is_null(io->req_data) || sockptr_is_null(io->resp_data))
|
||||
return -EINVAL;
|
||||
@@ -209,8 +210,20 @@ static int get_ext_report(struct snp_guest_dev *snp_dev, struct snp_guest_reques
|
||||
* the host. If host does not supply any certs in it, then copy
|
||||
* zeros to indicate that certificate data was not provided.
|
||||
*/
|
||||
memset(mdesc->certs_data, 0, report_req->certs_len);
|
||||
npages = report_req->certs_len >> PAGE_SHIFT;
|
||||
page = alloc_pages(GFP_KERNEL_ACCOUNT | __GFP_ZERO,
|
||||
get_order(report_req->certs_len));
|
||||
if (!page)
|
||||
return -ENOMEM;
|
||||
|
||||
req.certs_data = page_address(page);
|
||||
ret = set_memory_decrypted((unsigned long)req.certs_data, npages);
|
||||
if (ret) {
|
||||
pr_err("failed to mark page shared, ret=%d\n", ret);
|
||||
__free_pages(page, get_order(report_req->certs_len));
|
||||
return -EFAULT;
|
||||
}
|
||||
|
||||
cmd:
|
||||
/*
|
||||
* The intermediate response buffer is used while decrypting the
|
||||
@@ -219,10 +232,12 @@ cmd:
|
||||
*/
|
||||
resp_len = sizeof(report_resp->data) + mdesc->ctx->authsize;
|
||||
report_resp = kzalloc(resp_len, GFP_KERNEL_ACCOUNT);
|
||||
if (!report_resp)
|
||||
return -ENOMEM;
|
||||
if (!report_resp) {
|
||||
ret = -ENOMEM;
|
||||
goto e_free_data;
|
||||
}
|
||||
|
||||
mdesc->input.data_npages = npages;
|
||||
req.input.data_npages = npages;
|
||||
|
||||
req.msg_version = arg->msg_version;
|
||||
req.msg_type = SNP_MSG_REPORT_REQ;
|
||||
@@ -237,7 +252,7 @@ cmd:
|
||||
|
||||
/* If certs length is invalid then copy the returned length */
|
||||
if (arg->vmm_error == SNP_GUEST_VMM_ERR_INVALID_LEN) {
|
||||
report_req->certs_len = mdesc->input.data_npages << PAGE_SHIFT;
|
||||
report_req->certs_len = req.input.data_npages << PAGE_SHIFT;
|
||||
|
||||
if (copy_to_sockptr(io->req_data, report_req, sizeof(*report_req)))
|
||||
ret = -EFAULT;
|
||||
@@ -246,7 +261,7 @@ cmd:
|
||||
if (ret)
|
||||
goto e_free;
|
||||
|
||||
if (npages && copy_to_sockptr(certs_address, mdesc->certs_data, report_req->certs_len)) {
|
||||
if (npages && copy_to_sockptr(certs_address, req.certs_data, report_req->certs_len)) {
|
||||
ret = -EFAULT;
|
||||
goto e_free;
|
||||
}
|
||||
@@ -256,6 +271,13 @@ cmd:
|
||||
|
||||
e_free:
|
||||
kfree(report_resp);
|
||||
e_free_data:
|
||||
if (npages) {
|
||||
if (set_memory_encrypted((unsigned long)req.certs_data, npages))
|
||||
WARN_ONCE(ret, "failed to restore encryption mask (leak it)\n");
|
||||
else
|
||||
__free_pages(page, get_order(report_req->certs_len));
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user