crypto: ccp: Use the stack for small SEV command buffers

For commands with small input/output buffers, use the local stack to
"allocate" the structures used to communicate with the PSP.   Now that
__sev_do_cmd_locked() gracefully handles vmalloc'd buffers, there's no
reason to avoid using the stack, e.g. CONFIG_VMAP_STACK=y will just work.

Signed-off-by: Sean Christopherson <seanjc@google.com>
Message-Id: <20210406224952.4177376-6-seanjc@google.com>
Reviewed-by: Brijesh Singh <brijesh.singh@amd.com>
Acked-by: Tom Lendacky <thomas.lendacky@amd.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
This commit is contained in:
Sean Christopherson 2021-04-06 15:49:49 -07:00 committed by Paolo Bonzini
parent 8347b99473
commit e4a9af799e
1 changed files with 44 additions and 73 deletions

View File

@ -397,7 +397,7 @@ static int sev_ioctl_do_pek_csr(struct sev_issue_cmd *argp, bool writable)
{ {
struct sev_device *sev = psp_master->sev_data; struct sev_device *sev = psp_master->sev_data;
struct sev_user_data_pek_csr input; struct sev_user_data_pek_csr input;
struct sev_data_pek_csr *data; struct sev_data_pek_csr data;
void __user *input_address; void __user *input_address;
void *blob = NULL; void *blob = NULL;
int ret; int ret;
@ -408,9 +408,7 @@ static int sev_ioctl_do_pek_csr(struct sev_issue_cmd *argp, bool writable)
if (copy_from_user(&input, (void __user *)argp->data, sizeof(input))) if (copy_from_user(&input, (void __user *)argp->data, sizeof(input)))
return -EFAULT; return -EFAULT;
data = kzalloc(sizeof(*data), GFP_KERNEL); memset(&data, 0, sizeof(data));
if (!data)
return -ENOMEM;
/* userspace wants to query CSR length */ /* userspace wants to query CSR length */
if (!input.address || !input.length) if (!input.address || !input.length)
@ -418,19 +416,15 @@ static int sev_ioctl_do_pek_csr(struct sev_issue_cmd *argp, bool writable)
/* allocate a physically contiguous buffer to store the CSR blob */ /* allocate a physically contiguous buffer to store the CSR blob */
input_address = (void __user *)input.address; input_address = (void __user *)input.address;
if (input.length > SEV_FW_BLOB_MAX_SIZE) { if (input.length > SEV_FW_BLOB_MAX_SIZE)
ret = -EFAULT; return -EFAULT;
goto e_free;
}
blob = kmalloc(input.length, GFP_KERNEL); blob = kmalloc(input.length, GFP_KERNEL);
if (!blob) { if (!blob)
ret = -ENOMEM; return -ENOMEM;
goto e_free;
}
data->address = __psp_pa(blob); data.address = __psp_pa(blob);
data->len = input.length; data.len = input.length;
cmd: cmd:
if (sev->state == SEV_STATE_UNINIT) { if (sev->state == SEV_STATE_UNINIT) {
@ -439,10 +433,10 @@ cmd:
goto e_free_blob; goto e_free_blob;
} }
ret = __sev_do_cmd_locked(SEV_CMD_PEK_CSR, data, &argp->error); ret = __sev_do_cmd_locked(SEV_CMD_PEK_CSR, &data, &argp->error);
/* If we query the CSR length, FW responded with expected data. */ /* If we query the CSR length, FW responded with expected data. */
input.length = data->len; input.length = data.len;
if (copy_to_user((void __user *)argp->data, &input, sizeof(input))) { if (copy_to_user((void __user *)argp->data, &input, sizeof(input))) {
ret = -EFAULT; ret = -EFAULT;
@ -456,8 +450,6 @@ cmd:
e_free_blob: e_free_blob:
kfree(blob); kfree(blob);
e_free:
kfree(data);
return ret; return ret;
} }
@ -589,7 +581,7 @@ static int sev_ioctl_do_pek_import(struct sev_issue_cmd *argp, bool writable)
{ {
struct sev_device *sev = psp_master->sev_data; struct sev_device *sev = psp_master->sev_data;
struct sev_user_data_pek_cert_import input; struct sev_user_data_pek_cert_import input;
struct sev_data_pek_cert_import *data; struct sev_data_pek_cert_import data;
void *pek_blob, *oca_blob; void *pek_blob, *oca_blob;
int ret; int ret;
@ -599,19 +591,14 @@ static int sev_ioctl_do_pek_import(struct sev_issue_cmd *argp, bool writable)
if (copy_from_user(&input, (void __user *)argp->data, sizeof(input))) if (copy_from_user(&input, (void __user *)argp->data, sizeof(input)))
return -EFAULT; return -EFAULT;
data = kzalloc(sizeof(*data), GFP_KERNEL);
if (!data)
return -ENOMEM;
/* copy PEK certificate blobs from userspace */ /* copy PEK certificate blobs from userspace */
pek_blob = psp_copy_user_blob(input.pek_cert_address, input.pek_cert_len); pek_blob = psp_copy_user_blob(input.pek_cert_address, input.pek_cert_len);
if (IS_ERR(pek_blob)) { if (IS_ERR(pek_blob))
ret = PTR_ERR(pek_blob); return PTR_ERR(pek_blob);
goto e_free;
}
data->pek_cert_address = __psp_pa(pek_blob); data.reserved = 0;
data->pek_cert_len = input.pek_cert_len; data.pek_cert_address = __psp_pa(pek_blob);
data.pek_cert_len = input.pek_cert_len;
/* copy PEK certificate blobs from userspace */ /* copy PEK certificate blobs from userspace */
oca_blob = psp_copy_user_blob(input.oca_cert_address, input.oca_cert_len); oca_blob = psp_copy_user_blob(input.oca_cert_address, input.oca_cert_len);
@ -620,8 +607,8 @@ static int sev_ioctl_do_pek_import(struct sev_issue_cmd *argp, bool writable)
goto e_free_pek; goto e_free_pek;
} }
data->oca_cert_address = __psp_pa(oca_blob); data.oca_cert_address = __psp_pa(oca_blob);
data->oca_cert_len = input.oca_cert_len; data.oca_cert_len = input.oca_cert_len;
/* If platform is not in INIT state then transition it to INIT */ /* If platform is not in INIT state then transition it to INIT */
if (sev->state != SEV_STATE_INIT) { if (sev->state != SEV_STATE_INIT) {
@ -630,21 +617,19 @@ static int sev_ioctl_do_pek_import(struct sev_issue_cmd *argp, bool writable)
goto e_free_oca; goto e_free_oca;
} }
ret = __sev_do_cmd_locked(SEV_CMD_PEK_CERT_IMPORT, data, &argp->error); ret = __sev_do_cmd_locked(SEV_CMD_PEK_CERT_IMPORT, &data, &argp->error);
e_free_oca: e_free_oca:
kfree(oca_blob); kfree(oca_blob);
e_free_pek: e_free_pek:
kfree(pek_blob); kfree(pek_blob);
e_free:
kfree(data);
return ret; return ret;
} }
static int sev_ioctl_do_get_id2(struct sev_issue_cmd *argp) static int sev_ioctl_do_get_id2(struct sev_issue_cmd *argp)
{ {
struct sev_user_data_get_id2 input; struct sev_user_data_get_id2 input;
struct sev_data_get_id *data; struct sev_data_get_id data;
void __user *input_address; void __user *input_address;
void *id_blob = NULL; void *id_blob = NULL;
int ret; int ret;
@ -658,28 +643,25 @@ static int sev_ioctl_do_get_id2(struct sev_issue_cmd *argp)
input_address = (void __user *)input.address; input_address = (void __user *)input.address;
data = kzalloc(sizeof(*data), GFP_KERNEL);
if (!data)
return -ENOMEM;
if (input.address && input.length) { if (input.address && input.length) {
id_blob = kmalloc(input.length, GFP_KERNEL); id_blob = kmalloc(input.length, GFP_KERNEL);
if (!id_blob) { if (!id_blob)
kfree(data);
return -ENOMEM; return -ENOMEM;
}
data->address = __psp_pa(id_blob); data.address = __psp_pa(id_blob);
data->len = input.length; data.len = input.length;
} else {
data.address = 0;
data.len = 0;
} }
ret = __sev_do_cmd_locked(SEV_CMD_GET_ID, data, &argp->error); ret = __sev_do_cmd_locked(SEV_CMD_GET_ID, &data, &argp->error);
/* /*
* Firmware will return the length of the ID value (either the minimum * Firmware will return the length of the ID value (either the minimum
* required length or the actual length written), return it to the user. * required length or the actual length written), return it to the user.
*/ */
input.length = data->len; input.length = data.len;
if (copy_to_user((void __user *)argp->data, &input, sizeof(input))) { if (copy_to_user((void __user *)argp->data, &input, sizeof(input))) {
ret = -EFAULT; ret = -EFAULT;
@ -687,7 +669,7 @@ static int sev_ioctl_do_get_id2(struct sev_issue_cmd *argp)
} }
if (id_blob) { if (id_blob) {
if (copy_to_user(input_address, id_blob, data->len)) { if (copy_to_user(input_address, id_blob, data.len)) {
ret = -EFAULT; ret = -EFAULT;
goto e_free; goto e_free;
} }
@ -695,7 +677,6 @@ static int sev_ioctl_do_get_id2(struct sev_issue_cmd *argp)
e_free: e_free:
kfree(id_blob); kfree(id_blob);
kfree(data);
return ret; return ret;
} }
@ -745,7 +726,7 @@ static int sev_ioctl_do_pdh_export(struct sev_issue_cmd *argp, bool writable)
struct sev_device *sev = psp_master->sev_data; struct sev_device *sev = psp_master->sev_data;
struct sev_user_data_pdh_cert_export input; struct sev_user_data_pdh_cert_export input;
void *pdh_blob = NULL, *cert_blob = NULL; void *pdh_blob = NULL, *cert_blob = NULL;
struct sev_data_pdh_cert_export *data; struct sev_data_pdh_cert_export data;
void __user *input_cert_chain_address; void __user *input_cert_chain_address;
void __user *input_pdh_cert_address; void __user *input_pdh_cert_address;
int ret; int ret;
@ -763,9 +744,7 @@ static int sev_ioctl_do_pdh_export(struct sev_issue_cmd *argp, bool writable)
if (copy_from_user(&input, (void __user *)argp->data, sizeof(input))) if (copy_from_user(&input, (void __user *)argp->data, sizeof(input)))
return -EFAULT; return -EFAULT;
data = kzalloc(sizeof(*data), GFP_KERNEL); memset(&data, 0, sizeof(data));
if (!data)
return -ENOMEM;
/* Userspace wants to query the certificate length. */ /* Userspace wants to query the certificate length. */
if (!input.pdh_cert_address || if (!input.pdh_cert_address ||
@ -777,25 +756,19 @@ static int sev_ioctl_do_pdh_export(struct sev_issue_cmd *argp, bool writable)
input_cert_chain_address = (void __user *)input.cert_chain_address; input_cert_chain_address = (void __user *)input.cert_chain_address;
/* Allocate a physically contiguous buffer to store the PDH blob. */ /* Allocate a physically contiguous buffer to store the PDH blob. */
if (input.pdh_cert_len > SEV_FW_BLOB_MAX_SIZE) { if (input.pdh_cert_len > SEV_FW_BLOB_MAX_SIZE)
ret = -EFAULT; return -EFAULT;
goto e_free;
}
/* Allocate a physically contiguous buffer to store the cert chain blob. */ /* Allocate a physically contiguous buffer to store the cert chain blob. */
if (input.cert_chain_len > SEV_FW_BLOB_MAX_SIZE) { if (input.cert_chain_len > SEV_FW_BLOB_MAX_SIZE)
ret = -EFAULT; return -EFAULT;
goto e_free;
}
pdh_blob = kmalloc(input.pdh_cert_len, GFP_KERNEL); pdh_blob = kmalloc(input.pdh_cert_len, GFP_KERNEL);
if (!pdh_blob) { if (!pdh_blob)
ret = -ENOMEM; return -ENOMEM;
goto e_free;
}
data->pdh_cert_address = __psp_pa(pdh_blob); data.pdh_cert_address = __psp_pa(pdh_blob);
data->pdh_cert_len = input.pdh_cert_len; data.pdh_cert_len = input.pdh_cert_len;
cert_blob = kmalloc(input.cert_chain_len, GFP_KERNEL); cert_blob = kmalloc(input.cert_chain_len, GFP_KERNEL);
if (!cert_blob) { if (!cert_blob) {
@ -803,15 +776,15 @@ static int sev_ioctl_do_pdh_export(struct sev_issue_cmd *argp, bool writable)
goto e_free_pdh; goto e_free_pdh;
} }
data->cert_chain_address = __psp_pa(cert_blob); data.cert_chain_address = __psp_pa(cert_blob);
data->cert_chain_len = input.cert_chain_len; data.cert_chain_len = input.cert_chain_len;
cmd: cmd:
ret = __sev_do_cmd_locked(SEV_CMD_PDH_CERT_EXPORT, data, &argp->error); ret = __sev_do_cmd_locked(SEV_CMD_PDH_CERT_EXPORT, &data, &argp->error);
/* If we query the length, FW responded with expected data. */ /* If we query the length, FW responded with expected data. */
input.cert_chain_len = data->cert_chain_len; input.cert_chain_len = data.cert_chain_len;
input.pdh_cert_len = data->pdh_cert_len; input.pdh_cert_len = data.pdh_cert_len;
if (copy_to_user((void __user *)argp->data, &input, sizeof(input))) { if (copy_to_user((void __user *)argp->data, &input, sizeof(input))) {
ret = -EFAULT; ret = -EFAULT;
@ -836,8 +809,6 @@ e_free_cert:
kfree(cert_blob); kfree(cert_blob);
e_free_pdh: e_free_pdh:
kfree(pdh_blob); kfree(pdh_blob);
e_free:
kfree(data);
return ret; return ret;
} }