x86/sev: Change snp_guest_issue_request's fw_err
Subsystems (5)
A message that is not in the archive (sent off-list or not yet received): [email protected]
PATCH v10 1/4 crypto: ccp - Name -1 return value as SEV_RET_NO_FW_CALL
From: Peter Gonda <[email protected]>
The PSP can return a "firmware error" code of -1 in circumstances where the PSP is not actually called. To make this protocol unambiguous, the value is named SEV_RET_NO_FW_CALL.
--- Documentation/virt/coco/sev-guest.rst | 2 +- drivers/crypto/ccp/sev-dev.c | 22 ++++++++++++++-------- include/uapi/linux/psp-sev.h | 7 +++++++ 3 files changed, 22 insertions(+), 9 deletions(-)
Documentation/virt/coco/sev-guest.rst +1 -1
@@ -41,7 +41,7 @@ The guest ioctl should be issued on a file descriptor of the /dev/sev-guest devi The ioctl accepts struct snp_user_guest_request. The input and output structure is specified through the req_data and resp_data field respectively. If the ioctl fails to execute due to a firmware error, then fw_err code will be set otherwise the -fw_err will be set to 0x00000000000000ff. +fw_err will be set to 0x00000000ffffffff, i.e., the lower 32-bits are -1. The firmware checks that the message sequence counter is one greater than the guests message sequence counter. If guest driver fails to increment message
drivers/crypto/ccp/sev-dev.c +14 -8
@@ -440,12 +440,19 @@ static int __sev_init_ex_locked(int *error)return __sev_do_cmd_locked (SEV_CMD_INIT_EX , &data, error); } +static inline int __sev_do_init_locked (int *psp_ret) +{ +if (sev_init_ex_buffer) +return __sev_init_ex_locked (psp_ret); +else +return __sev_init_locked (psp_ret); +} +static int __sev_platform_init_locked (int *error) {struct psp_device *psp = psp_master;struct sev_device *sev; -int rc =0 , psp_ret =-1 ; -int (*init_function)(int *error); +int rc =0 , psp_ret =SEV_RET_NO_FW_CALL ;if (!psp || !psp->sev_data)return -ENODEV ;
@@ -456,15 +463,12 @@ static int __sev_platform_init_locked(int *error)return 0 ;if (sev_init_ex_buffer) { - init_function = __sev_init_ex_locked; rc =sev_read_init_ex_file ();if (rc)return rc; - }else { - init_function = __sev_init_locked; } - rc =init_function (&psp_ret); + rc =__sev_do_init_locked (&psp_ret);if (rc && psp_ret ==SEV_RET_SECURE_DATA_INVALID ) {/* * Initialization command returned an integrity check failure
@@ -473,9 +477,11 @@ static int __sev_platform_init_locked(int *error)* initialization function should succeed by replacing the state * with a reset state. */ -dev_err (sev->dev,"SEV: retrying INIT command because of SECURE_DATA_INVALID error. Retrying once to reset PSP SEV state." ); - rc =init_function (&psp_ret); +dev_err (sev->dev, +"SEV: retrying INIT command because of SECURE_DATA_INVALID error. Retrying once to reset PSP SEV state." ); + rc =__sev_do_init_locked (&psp_ret); } +if (error) *error = psp_ret;
include/uapi/linux/psp-sev.h +7 -0
@@ -36,6 +36,13 @@ enum {* SEV Firmware status code */ typedef enum { + /* + * This error code is not in the SEV spec but is added to convey that + * there was an error that prevented the SEV Firmware from being called. + * The SEV API error codes are 16 bits, so the -1 value will not overlap + * with possible values from the specification. + */ + SEV_RET_NO_FW_CALL = -1, SEV_RET_SUCCESS = 0, SEV_RET_INVALID_PLATFORM_STATE, SEV_RET_INVALID_GUEST_STATE,
Signature · 1 line
2.39.0.314.g84b9a713c41-goog
PATCH v10 2/4 x86/sev: Change snp_guest_issue_request's fw_err
In reply to a message not in the archiveThe GHCB specification declares that the firmware error value for a guest request will be stored in the lower 32 bits of EXIT_INFO_2. The upper 32 bits are for the VMM's own error code. The fw_err argument is thus a misnomer, and callers will need access to all 64 bits.
The type of unsigned long also causes problems, since sw_exit_info2 is u64 (unsigned long long) vs the argument's previous unsigned long*. The signature change requires the follow-up change to drivers/virt/coco/sev-guest to use the new expected type in order to compile.
The firmware might not even be called, so the call is bookended with the no firmware call error and clearing the error.
--- arch/x86/include/asm/sev.h | 4 ++-- arch/x86/kernel/sev.c | 10 ++++++---- 2 files changed, 8 insertions(+), 6 deletions(-)
arch/x86/include/asm/sev.h +2 -2
@@ -196,7 +196,7 @@ void snp_set_memory_private(unsigned long vaddr, unsigned int npages);void snp_set_wakeup_secondary_cpu (void );bool snp_init (struct boot_params *bp);void __init __noreturn snp_abort (void ); -int snp_issue_guest_request (u64 exit_code,struct snp_req_data *input,unsigned long *fw_err); +int snp_issue_guest_request (u64 exit_code,struct snp_req_data *input,u64 *exitinfo2);#else static inline void sev_es_ist_enter (struct pt_regs *regs) { }static inline void sev_es_ist_exit (void ) { }
@@ -217,7 +217,7 @@ static inline void snp_set_wakeup_secondary_cpu(void) { }static inline bool snp_init (struct boot_params *bp) {return false; }static inline void snp_abort (void ) { }static inline int snp_issue_guest_request (u64 exit_code,struct snp_req_data *input, -unsigned long *fw_err) +u64 *exitinfo2) {return -ENOTTY ; }
arch/x86/kernel/sev.c +6 -4
@@ -22,6 +22,7 @@#include <linux/efi.h> #include <linux/platform_device.h> #include <linux/io.h> +#include <linux/psp-sev.h> #include <asm/cpu_entry_area.h> #include <asm/stacktrace.h>
@@ -2175,7 +2176,7 @@ static int __init init_sev_config(char *str) }__setup ("sev=" , init_sev_config); -int snp_issue_guest_request (u64 exit_code,struct snp_req_data *input,unsigned long *fw_err) +int snp_issue_guest_request (u64 exit_code,struct snp_req_data *input,u64 *exitinfo2) {struct ghcb_state state;struct es_em_ctxt ctxt;
@@ -2186,9 +2187,11 @@ int snp_issue_guest_request(u64 exit_code, struct snp_req_data *input, unsignedif (!cc_platform_has (CC_ATTR_GUEST_SEV_SNP ))return -ENODEV ; -if (!fw_err) +if (!exitinfo2)return -EINVAL ; + *exitinfo2 =SEV_RET_NO_FW_CALL ; +/* * __sev_get_ghcb() needs to run with IRQs disabled because it is using * a per-CPU GHCB.
@@ -2212,14 +2215,13 @@ int snp_issue_guest_request(u64 exit_code, struct snp_req_data *input, unsignedif (ret) goto e_put; + *exitinfo2 = ghcb->save.sw_exit_info_2;if (ghcb->save.sw_exit_info_2) {/* Number of expected pages are returned in RBX */ if (exit_code ==SVM_VMGEXIT_EXT_GUEST_REQUEST && ghcb->save.sw_exit_info_2 ==SNP_GUEST_REQ_INVALID_LEN ) input->data_npages =ghcb_get_rbx (ghcb); - *fw_err = ghcb->save.sw_exit_info_2; - ret = -EIO ; }
Signature · 1 line
2.39.0.314.g84b9a713c41-goog
PATCH v10 3/4 virt: sev-guest: Remove err in handle_guest_request
In reply to a message not in the archiveThe err variable may not be set in the call to snp_issue_guest_request, yet it is unconditionally written back to fw_err if fw_err is non-null. This is undefined behavior, and currently returns uninitialized kernel stack memory to user space.
The fw_err argument is better to just pass through to snp_issue_guest_request, so that's done by passing along the ioctl argument. This removes the need for an argument to handle_guest_request.
--- drivers/virt/coco/sev-guest/sev-guest.c | 35 ++++++++++++------------- 1 file changed, 17 insertions(+), 18 deletions(-)
drivers/virt/coco/sev-guest/sev-guest.c +17 -18
@@ -318,11 +318,11 @@ static int enc_payload(struct snp_guest_dev *snp_dev, u64 seqno, int version, u8return __enc_payload (snp_dev, req, payload, sz); } -static int handle_guest_request (struct snp_guest_dev *snp_dev,u64 exit_code,int msg_ver, +static int handle_guest_request (struct snp_guest_dev *snp_dev,u64 exit_code, +struct snp_guest_request_ioctl *arg,u8 type,void *req_buf,size_t req_sz,void *resp_buf, -u32 resp_sz,__u64 *fw_err) +u32 resp_sz) { -unsigned long err;u64 seqno;int rc;
@@ -334,7 +334,8 @@ static int handle_guest_request(struct snp_guest_dev *snp_dev, u64 exit_code, inmemset (snp_dev->response,0 ,sizeof (struct snp_guest_msg ));/* Encrypt the userspace provided payload */ - rc =enc_payload (snp_dev, seqno, msg_ver, type, req_buf, req_sz); + rc =enc_payload (snp_dev, seqno, arg->msg_version, type, req_buf, + req_sz);if (rc)return rc;
@@ -344,7 +345,8 @@ static int handle_guest_request(struct snp_guest_dev *snp_dev, u64 exit_code, in* sequence number must be incremented or the VMPCK must be deleted to * prevent reuse of the IV. */ - rc =snp_issue_guest_request (exit_code, &snp_dev->input, &err); + rc =snp_issue_guest_request (exit_code, &snp_dev->input, + &arg->fw_err);/* * If the extended guest request fails due to having too small of a
@@ -366,24 +368,22 @@ static int handle_guest_request(struct snp_guest_dev *snp_dev, u64 exit_code, in* of the VMPCK and the error code being propagated back to the * user as an ioctl() return code. */ - rc =snp_issue_guest_request (exit_code, &snp_dev->input, &err); + rc =snp_issue_guest_request (exit_code, &snp_dev->input, + &arg->fw_err);/* * Override the error to inform callers the given extended * request buffer size was too small and give the caller the * required buffer size. */ - err =SNP_GUEST_REQ_INVALID_LEN ; + arg->fw_err =SNP_GUEST_REQ_INVALID_LEN ; snp_dev->input.data_npages = certs_npages; } -if (fw_err) - *fw_err = err; -if (rc) {dev_alert (snp_dev->dev,"Detected error from ASP request. rc: %d, fw_err: %llu\n" , - rc, *fw_err); + rc, arg->fw_err); goto disable_vmpck; }
@@ -430,9 +430,9 @@ static int get_report(struct snp_guest_dev *snp_dev, struct snp_guest_request_ioif (!resp)return -ENOMEM ; - rc =handle_guest_request (snp_dev,SVM_VMGEXIT_GUEST_REQUEST , arg->msg_version, + rc =handle_guest_request (snp_dev,SVM_VMGEXIT_GUEST_REQUEST , arg,SNP_MSG_REPORT_REQ , &req,sizeof (req), resp->data, - resp_len, &arg->fw_err); + resp_len);if (rc) goto e_free;
@@ -470,9 +470,8 @@ static int get_derived_key(struct snp_guest_dev *snp_dev, struct snp_guest_requeif (copy_from_user (&req, (void __user *)arg->req_data,sizeof (req)))return -EFAULT ; - rc =handle_guest_request (snp_dev,SVM_VMGEXIT_GUEST_REQUEST , arg->msg_version, -SNP_MSG_KEY_REQ , &req,sizeof (req), buf, resp_len, - &arg->fw_err); + rc =handle_guest_request (snp_dev,SVM_VMGEXIT_GUEST_REQUEST , arg, +SNP_MSG_KEY_REQ , &req,sizeof (req), buf, resp_len);if (rc)return rc;
@@ -532,9 +531,9 @@ static int get_ext_report(struct snp_guest_dev *snp_dev, struct snp_guest_requesreturn -ENOMEM ; snp_dev->input.data_npages = npages; - ret =handle_guest_request (snp_dev,SVM_VMGEXIT_EXT_GUEST_REQUEST , arg->msg_version, + ret =handle_guest_request (snp_dev,SVM_VMGEXIT_EXT_GUEST_REQUEST , arg,SNP_MSG_REPORT_REQ , &req.data, -sizeof (req.data), resp->data, resp_len, &arg->fw_err); +sizeof (req.data), resp->data, resp_len);/* If certs length is invalid then copy the returned length */ if (arg->fw_err ==SNP_GUEST_REQ_INVALID_LEN ) {
Signature · 1 line
2.39.0.314.g84b9a713c41-goog
PATCH v10 4/4 virt: sev-guest: interpret VMM errors from guest request
In reply to a message not in the archiveThe GHCB specification states that the upper 32 bits of exitinfo2 are for the VMM's error codes. The sev-guest ABI has already locked in that the fw_err status of the input will be 64 bits, and that BIT_ULL(32) means that the extended guest request's data buffer was too small. Add BIT_ULL(33) for "host refused the request for throttling reasons".
The driver interprets the upper 32 bits of exitinfo2 for the user anyway in case the request gets throttled. For safety, since the encryption algorithm in GHCBv2 is AES_GCM, control must remain in the kernel to complete the request with the current sequence number. Returning without finishing the request allows the the guest to make another request but with different message contents. This is IV reuse, and breaks cryptographic protections.
When throttled, the driver will reschedule itself and then try again after sleeping half its ratelimit time to avoid a big wait queue. The ioctl may block indefinitely, but that has always been the case when deferring these requests to the host.
--- Documentation/virt/coco/sev-guest.rst | 21 +++++++---- drivers/virt/coco/sev-guest/sev-guest.c | 49 ++++++++++++++++++++----- include/uapi/linux/sev-guest.h | 18 ++++++++- 3 files changed, 70 insertions(+), 18 deletions(-)
Documentation/virt/coco/sev-guest.rst +14 -7
@@ -37,11 +37,12 @@ along with a description: the return value. General error numbers (-ENOMEM, -EINVAL) are not detailed, but errors with specific meanings are. -The guest ioctl should be issued on a file descriptor of the /dev/sev-guest device. -The ioctl accepts struct snp_user_guest_request. The input and output structure is -specified through the req_data and resp_data field respectively. If the ioctl fails -to execute due to a firmware error, then fw_err code will be set otherwise the -fw_err will be set to 0x00000000ffffffff, i.e., the lower 32-bits are -1. +The guest ioctl should be issued on a file descriptor of the +/dev/sev-guest device. The ioctl accepts struct +snp_user_guest_request. The input and output structure is specified +through the req_data and resp_data field respectively. If the ioctl +fails to execute due to a firmware error, then the fw_error code will +be set, otherwise fw_error will be set to -1. The firmware checks that the message sequence counter is one greater than the guests message sequence counter. If guest driver fails to increment message
@@ -57,8 +58,14 @@ counter (e.g. counter overflow), then -EIO will be returned. __u64 req_data; __u64 resp_data; - /* firmware error code on failure (see psp-sev.h) */ - __u64 fw_err; + /* bits[63:32]: VMM error code, bits[31:0] firmware error code (see psp-sev.h) */ + union { + __u64 exitinfo2; + struct { + __u32 fw_error; + __u32 vmm_error; + }; + }; }; 2.1 SNP_GET_REPORT
drivers/virt/coco/sev-guest/sev-guest.c +40 -9
126 lines not on this page · raw message
include/uapi/linux/sev-guest.h +16 -2
@@ -52,8 +52,15 @@ struct snp_guest_request_ioctl {__u64 req_data;__u64 resp_data; -/* firmware error code on failure (see psp-sev.h) */ -__u64 fw_err; +/* bits[63:32]: VMM error code, bits[31:0] firmware error code (see psp-sev.h) */ +union { +__u64 exitinfo2; +__u64 fw_err;/* Name deprecated in favor of others */ +struct { +__u32 fw_error; +__u32 vmm_error; + }; + }; };struct snp_ext_report_req {
@@ -77,4 +84,11 @@ struct snp_ext_report_req {/* Get SNP extended report as defined in the GHCB specification version 2. */ #define SNP_GET_EXT_REPORT _IOWR(SNP_GUEST_REQ_IOC_TYPE, 0x2, struct snp_guest_request_ioctl) +/* Guest message request EXIT_INFO_2 constants */ +#define SNP_GUEST_FW_ERR_MASK GENMASK_ULL(31, 0) +#define SNP_GUEST_VMM_ERR_SHIFT 32 + +#define SNP_GUEST_VMM_ERR_INVALID_LEN 1 +#define SNP_GUEST_VMM_ERR_BUSY 2 +#endif /* __UAPI_LINUX_SEV_GUEST_H_ */
Signature · 1 line
2.39.0.314.g84b9a713c41-goog