1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * AMD Secure Encrypted Virtualization (SEV) guest driver interface 4 * 5 * Copyright (C) 2021 Advanced Micro Devices, Inc. 6 * 7 * Author: Brijesh Singh <brijesh.singh@amd.com> 8 */ 9 10 #include <linux/module.h> 11 #include <linux/kernel.h> 12 #include <linux/types.h> 13 #include <linux/mutex.h> 14 #include <linux/io.h> 15 #include <linux/platform_device.h> 16 #include <linux/miscdevice.h> 17 #include <linux/set_memory.h> 18 #include <linux/fs.h> 19 #include <linux/tsm.h> 20 #include <crypto/aead.h> 21 #include <linux/scatterlist.h> 22 #include <linux/psp-sev.h> 23 #include <linux/sockptr.h> 24 #include <linux/cleanup.h> 25 #include <linux/uuid.h> 26 #include <uapi/linux/sev-guest.h> 27 #include <uapi/linux/psp-sev.h> 28 29 #include <asm/svm.h> 30 #include <asm/sev.h> 31 32 #define DEVICE_NAME "sev-guest" 33 #define AAD_LEN 48 34 #define MSG_HDR_VER 1 35 36 #define SNP_REQ_MAX_RETRY_DURATION (60*HZ) 37 #define SNP_REQ_RETRY_DELAY (2*HZ) 38 39 struct snp_guest_crypto { 40 struct crypto_aead *tfm; 41 u8 *iv, *authtag; 42 int iv_len, a_len; 43 }; 44 45 struct snp_guest_dev { 46 struct device *dev; 47 struct miscdevice misc; 48 49 void *certs_data; 50 struct snp_guest_crypto *crypto; 51 /* request and response are in unencrypted memory */ 52 struct snp_guest_msg *request, *response; 53 54 /* 55 * Avoid information leakage by double-buffering shared messages 56 * in fields that are in regular encrypted memory. 57 */ 58 struct snp_guest_msg secret_request, secret_response; 59 60 struct snp_secrets_page *secrets; 61 struct snp_req_data input; 62 union { 63 struct snp_report_req report; 64 struct snp_derived_key_req derived_key; 65 struct snp_ext_report_req ext_report; 66 } req; 67 u32 *os_area_msg_seqno; 68 u8 *vmpck; 69 }; 70 71 static u32 vmpck_id; 72 module_param(vmpck_id, uint, 0444); 73 MODULE_PARM_DESC(vmpck_id, "The VMPCK ID to use when communicating with the PSP."); 74 75 /* Mutex to serialize the shared buffer access and command handling. */ 76 static DEFINE_MUTEX(snp_cmd_mutex); 77 78 static bool is_vmpck_empty(struct snp_guest_dev *snp_dev) 79 { 80 char zero_key[VMPCK_KEY_LEN] = {0}; 81 82 if (snp_dev->vmpck) 83 return !memcmp(snp_dev->vmpck, zero_key, VMPCK_KEY_LEN); 84 85 return true; 86 } 87 88 /* 89 * If an error is received from the host or AMD Secure Processor (ASP) there 90 * are two options. Either retry the exact same encrypted request or discontinue 91 * using the VMPCK. 92 * 93 * This is because in the current encryption scheme GHCB v2 uses AES-GCM to 94 * encrypt the requests. The IV for this scheme is the sequence number. GCM 95 * cannot tolerate IV reuse. 96 * 97 * The ASP FW v1.51 only increments the sequence numbers on a successful 98 * guest<->ASP back and forth and only accepts messages at its exact sequence 99 * number. 100 * 101 * So if the sequence number were to be reused the encryption scheme is 102 * vulnerable. If the sequence number were incremented for a fresh IV the ASP 103 * will reject the request. 104 */ 105 static void snp_disable_vmpck(struct snp_guest_dev *snp_dev) 106 { 107 dev_alert(snp_dev->dev, "Disabling vmpck_id %d to prevent IV reuse.\n", 108 vmpck_id); 109 memzero_explicit(snp_dev->vmpck, VMPCK_KEY_LEN); 110 snp_dev->vmpck = NULL; 111 } 112 113 static inline u64 __snp_get_msg_seqno(struct snp_guest_dev *snp_dev) 114 { 115 u64 count; 116 117 lockdep_assert_held(&snp_cmd_mutex); 118 119 /* Read the current message sequence counter from secrets pages */ 120 count = *snp_dev->os_area_msg_seqno; 121 122 return count + 1; 123 } 124 125 /* Return a non-zero on success */ 126 static u64 snp_get_msg_seqno(struct snp_guest_dev *snp_dev) 127 { 128 u64 count = __snp_get_msg_seqno(snp_dev); 129 130 /* 131 * The message sequence counter for the SNP guest request is a 64-bit 132 * value but the version 2 of GHCB specification defines a 32-bit storage 133 * for it. If the counter exceeds the 32-bit value then return zero. 134 * The caller should check the return value, but if the caller happens to 135 * not check the value and use it, then the firmware treats zero as an 136 * invalid number and will fail the message request. 137 */ 138 if (count >= UINT_MAX) { 139 dev_err(snp_dev->dev, "request message sequence counter overflow\n"); 140 return 0; 141 } 142 143 return count; 144 } 145 146 static void snp_inc_msg_seqno(struct snp_guest_dev *snp_dev) 147 { 148 /* 149 * The counter is also incremented by the PSP, so increment it by 2 150 * and save in secrets page. 151 */ 152 *snp_dev->os_area_msg_seqno += 2; 153 } 154 155 static inline struct snp_guest_dev *to_snp_dev(struct file *file) 156 { 157 struct miscdevice *dev = file->private_data; 158 159 return container_of(dev, struct snp_guest_dev, misc); 160 } 161 162 static struct snp_guest_crypto *init_crypto(struct snp_guest_dev *snp_dev, u8 *key, size_t keylen) 163 { 164 struct snp_guest_crypto *crypto; 165 166 crypto = kzalloc(sizeof(*crypto), GFP_KERNEL_ACCOUNT); 167 if (!crypto) 168 return NULL; 169 170 crypto->tfm = crypto_alloc_aead("gcm(aes)", 0, 0); 171 if (IS_ERR(crypto->tfm)) 172 goto e_free; 173 174 if (crypto_aead_setkey(crypto->tfm, key, keylen)) 175 goto e_free_crypto; 176 177 crypto->iv_len = crypto_aead_ivsize(crypto->tfm); 178 crypto->iv = kmalloc(crypto->iv_len, GFP_KERNEL_ACCOUNT); 179 if (!crypto->iv) 180 goto e_free_crypto; 181 182 if (crypto_aead_authsize(crypto->tfm) > MAX_AUTHTAG_LEN) { 183 if (crypto_aead_setauthsize(crypto->tfm, MAX_AUTHTAG_LEN)) { 184 dev_err(snp_dev->dev, "failed to set authsize to %d\n", MAX_AUTHTAG_LEN); 185 goto e_free_iv; 186 } 187 } 188 189 crypto->a_len = crypto_aead_authsize(crypto->tfm); 190 crypto->authtag = kmalloc(crypto->a_len, GFP_KERNEL_ACCOUNT); 191 if (!crypto->authtag) 192 goto e_free_iv; 193 194 return crypto; 195 196 e_free_iv: 197 kfree(crypto->iv); 198 e_free_crypto: 199 crypto_free_aead(crypto->tfm); 200 e_free: 201 kfree(crypto); 202 203 return NULL; 204 } 205 206 static void deinit_crypto(struct snp_guest_crypto *crypto) 207 { 208 crypto_free_aead(crypto->tfm); 209 kfree(crypto->iv); 210 kfree(crypto->authtag); 211 kfree(crypto); 212 } 213 214 static int enc_dec_message(struct snp_guest_crypto *crypto, struct snp_guest_msg *msg, 215 u8 *src_buf, u8 *dst_buf, size_t len, bool enc) 216 { 217 struct snp_guest_msg_hdr *hdr = &msg->hdr; 218 struct scatterlist src[3], dst[3]; 219 DECLARE_CRYPTO_WAIT(wait); 220 struct aead_request *req; 221 int ret; 222 223 req = aead_request_alloc(crypto->tfm, GFP_KERNEL); 224 if (!req) 225 return -ENOMEM; 226 227 /* 228 * AEAD memory operations: 229 * +------ AAD -------+------- DATA -----+---- AUTHTAG----+ 230 * | msg header | plaintext | hdr->authtag | 231 * | bytes 30h - 5Fh | or | | 232 * | | cipher | | 233 * +------------------+------------------+----------------+ 234 */ 235 sg_init_table(src, 3); 236 sg_set_buf(&src[0], &hdr->algo, AAD_LEN); 237 sg_set_buf(&src[1], src_buf, hdr->msg_sz); 238 sg_set_buf(&src[2], hdr->authtag, crypto->a_len); 239 240 sg_init_table(dst, 3); 241 sg_set_buf(&dst[0], &hdr->algo, AAD_LEN); 242 sg_set_buf(&dst[1], dst_buf, hdr->msg_sz); 243 sg_set_buf(&dst[2], hdr->authtag, crypto->a_len); 244 245 aead_request_set_ad(req, AAD_LEN); 246 aead_request_set_tfm(req, crypto->tfm); 247 aead_request_set_callback(req, 0, crypto_req_done, &wait); 248 249 aead_request_set_crypt(req, src, dst, len, crypto->iv); 250 ret = crypto_wait_req(enc ? crypto_aead_encrypt(req) : crypto_aead_decrypt(req), &wait); 251 252 aead_request_free(req); 253 return ret; 254 } 255 256 static int __enc_payload(struct snp_guest_dev *snp_dev, struct snp_guest_msg *msg, 257 void *plaintext, size_t len) 258 { 259 struct snp_guest_crypto *crypto = snp_dev->crypto; 260 struct snp_guest_msg_hdr *hdr = &msg->hdr; 261 262 memset(crypto->iv, 0, crypto->iv_len); 263 memcpy(crypto->iv, &hdr->msg_seqno, sizeof(hdr->msg_seqno)); 264 265 return enc_dec_message(crypto, msg, plaintext, msg->payload, len, true); 266 } 267 268 static int dec_payload(struct snp_guest_dev *snp_dev, struct snp_guest_msg *msg, 269 void *plaintext, size_t len) 270 { 271 struct snp_guest_crypto *crypto = snp_dev->crypto; 272 struct snp_guest_msg_hdr *hdr = &msg->hdr; 273 274 /* Build IV with response buffer sequence number */ 275 memset(crypto->iv, 0, crypto->iv_len); 276 memcpy(crypto->iv, &hdr->msg_seqno, sizeof(hdr->msg_seqno)); 277 278 return enc_dec_message(crypto, msg, msg->payload, plaintext, len, false); 279 } 280 281 static int verify_and_dec_payload(struct snp_guest_dev *snp_dev, void *payload, u32 sz) 282 { 283 struct snp_guest_crypto *crypto = snp_dev->crypto; 284 struct snp_guest_msg *resp = &snp_dev->secret_response; 285 struct snp_guest_msg *req = &snp_dev->secret_request; 286 struct snp_guest_msg_hdr *req_hdr = &req->hdr; 287 struct snp_guest_msg_hdr *resp_hdr = &resp->hdr; 288 289 dev_dbg(snp_dev->dev, "response [seqno %lld type %d version %d sz %d]\n", 290 resp_hdr->msg_seqno, resp_hdr->msg_type, resp_hdr->msg_version, resp_hdr->msg_sz); 291 292 /* Copy response from shared memory to encrypted memory. */ 293 memcpy(resp, snp_dev->response, sizeof(*resp)); 294 295 /* Verify that the sequence counter is incremented by 1 */ 296 if (unlikely(resp_hdr->msg_seqno != (req_hdr->msg_seqno + 1))) 297 return -EBADMSG; 298 299 /* Verify response message type and version number. */ 300 if (resp_hdr->msg_type != (req_hdr->msg_type + 1) || 301 resp_hdr->msg_version != req_hdr->msg_version) 302 return -EBADMSG; 303 304 /* 305 * If the message size is greater than our buffer length then return 306 * an error. 307 */ 308 if (unlikely((resp_hdr->msg_sz + crypto->a_len) > sz)) 309 return -EBADMSG; 310 311 /* Decrypt the payload */ 312 return dec_payload(snp_dev, resp, payload, resp_hdr->msg_sz + crypto->a_len); 313 } 314 315 static int enc_payload(struct snp_guest_dev *snp_dev, u64 seqno, int version, u8 type, 316 void *payload, size_t sz) 317 { 318 struct snp_guest_msg *req = &snp_dev->secret_request; 319 struct snp_guest_msg_hdr *hdr = &req->hdr; 320 321 memset(req, 0, sizeof(*req)); 322 323 hdr->algo = SNP_AEAD_AES_256_GCM; 324 hdr->hdr_version = MSG_HDR_VER; 325 hdr->hdr_sz = sizeof(*hdr); 326 hdr->msg_type = type; 327 hdr->msg_version = version; 328 hdr->msg_seqno = seqno; 329 hdr->msg_vmpck = vmpck_id; 330 hdr->msg_sz = sz; 331 332 /* Verify the sequence number is non-zero */ 333 if (!hdr->msg_seqno) 334 return -ENOSR; 335 336 dev_dbg(snp_dev->dev, "request [seqno %lld type %d version %d sz %d]\n", 337 hdr->msg_seqno, hdr->msg_type, hdr->msg_version, hdr->msg_sz); 338 339 return __enc_payload(snp_dev, req, payload, sz); 340 } 341 342 static int __handle_guest_request(struct snp_guest_dev *snp_dev, u64 exit_code, 343 struct snp_guest_request_ioctl *rio) 344 { 345 unsigned long req_start = jiffies; 346 unsigned int override_npages = 0; 347 u64 override_err = 0; 348 int rc; 349 350 retry_request: 351 /* 352 * Call firmware to process the request. In this function the encrypted 353 * message enters shared memory with the host. So after this call the 354 * sequence number must be incremented or the VMPCK must be deleted to 355 * prevent reuse of the IV. 356 */ 357 rc = snp_issue_guest_request(exit_code, &snp_dev->input, rio); 358 switch (rc) { 359 case -ENOSPC: 360 /* 361 * If the extended guest request fails due to having too 362 * small of a certificate data buffer, retry the same 363 * guest request without the extended data request in 364 * order to increment the sequence number and thus avoid 365 * IV reuse. 366 */ 367 override_npages = snp_dev->input.data_npages; 368 exit_code = SVM_VMGEXIT_GUEST_REQUEST; 369 370 /* 371 * Override the error to inform callers the given extended 372 * request buffer size was too small and give the caller the 373 * required buffer size. 374 */ 375 override_err = SNP_GUEST_VMM_ERR(SNP_GUEST_VMM_ERR_INVALID_LEN); 376 377 /* 378 * If this call to the firmware succeeds, the sequence number can 379 * be incremented allowing for continued use of the VMPCK. If 380 * there is an error reflected in the return value, this value 381 * is checked further down and the result will be the deletion 382 * of the VMPCK and the error code being propagated back to the 383 * user as an ioctl() return code. 384 */ 385 goto retry_request; 386 387 /* 388 * The host may return SNP_GUEST_VMM_ERR_BUSY if the request has been 389 * throttled. Retry in the driver to avoid returning and reusing the 390 * message sequence number on a different message. 391 */ 392 case -EAGAIN: 393 if (jiffies - req_start > SNP_REQ_MAX_RETRY_DURATION) { 394 rc = -ETIMEDOUT; 395 break; 396 } 397 schedule_timeout_killable(SNP_REQ_RETRY_DELAY); 398 goto retry_request; 399 } 400 401 /* 402 * Increment the message sequence number. There is no harm in doing 403 * this now because decryption uses the value stored in the response 404 * structure and any failure will wipe the VMPCK, preventing further 405 * use anyway. 406 */ 407 snp_inc_msg_seqno(snp_dev); 408 409 if (override_err) { 410 rio->exitinfo2 = override_err; 411 412 /* 413 * If an extended guest request was issued and the supplied certificate 414 * buffer was not large enough, a standard guest request was issued to 415 * prevent IV reuse. If the standard request was successful, return -EIO 416 * back to the caller as would have originally been returned. 417 */ 418 if (!rc && override_err == SNP_GUEST_VMM_ERR(SNP_GUEST_VMM_ERR_INVALID_LEN)) 419 rc = -EIO; 420 } 421 422 if (override_npages) 423 snp_dev->input.data_npages = override_npages; 424 425 return rc; 426 } 427 428 static int handle_guest_request(struct snp_guest_dev *snp_dev, u64 exit_code, 429 struct snp_guest_request_ioctl *rio, u8 type, 430 void *req_buf, size_t req_sz, void *resp_buf, 431 u32 resp_sz) 432 { 433 u64 seqno; 434 int rc; 435 436 /* Get message sequence and verify that its a non-zero */ 437 seqno = snp_get_msg_seqno(snp_dev); 438 if (!seqno) 439 return -EIO; 440 441 /* Clear shared memory's response for the host to populate. */ 442 memset(snp_dev->response, 0, sizeof(struct snp_guest_msg)); 443 444 /* Encrypt the userspace provided payload in snp_dev->secret_request. */ 445 rc = enc_payload(snp_dev, seqno, rio->msg_version, type, req_buf, req_sz); 446 if (rc) 447 return rc; 448 449 /* 450 * Write the fully encrypted request to the shared unencrypted 451 * request page. 452 */ 453 memcpy(snp_dev->request, &snp_dev->secret_request, 454 sizeof(snp_dev->secret_request)); 455 456 rc = __handle_guest_request(snp_dev, exit_code, rio); 457 if (rc) { 458 if (rc == -EIO && 459 rio->exitinfo2 == SNP_GUEST_VMM_ERR(SNP_GUEST_VMM_ERR_INVALID_LEN)) 460 return rc; 461 462 dev_alert(snp_dev->dev, 463 "Detected error from ASP request. rc: %d, exitinfo2: 0x%llx\n", 464 rc, rio->exitinfo2); 465 466 snp_disable_vmpck(snp_dev); 467 return rc; 468 } 469 470 rc = verify_and_dec_payload(snp_dev, resp_buf, resp_sz); 471 if (rc) { 472 dev_alert(snp_dev->dev, "Detected unexpected decode failure from ASP. rc: %d\n", rc); 473 snp_disable_vmpck(snp_dev); 474 return rc; 475 } 476 477 return 0; 478 } 479 480 struct snp_req_resp { 481 sockptr_t req_data; 482 sockptr_t resp_data; 483 }; 484 485 static int get_report(struct snp_guest_dev *snp_dev, struct snp_guest_request_ioctl *arg) 486 { 487 struct snp_guest_crypto *crypto = snp_dev->crypto; 488 struct snp_report_req *req = &snp_dev->req.report; 489 struct snp_report_resp *resp; 490 int rc, resp_len; 491 492 lockdep_assert_held(&snp_cmd_mutex); 493 494 if (!arg->req_data || !arg->resp_data) 495 return -EINVAL; 496 497 if (copy_from_user(req, (void __user *)arg->req_data, sizeof(*req))) 498 return -EFAULT; 499 500 /* 501 * The intermediate response buffer is used while decrypting the 502 * response payload. Make sure that it has enough space to cover the 503 * authtag. 504 */ 505 resp_len = sizeof(resp->data) + crypto->a_len; 506 resp = kzalloc(resp_len, GFP_KERNEL_ACCOUNT); 507 if (!resp) 508 return -ENOMEM; 509 510 rc = handle_guest_request(snp_dev, SVM_VMGEXIT_GUEST_REQUEST, arg, 511 SNP_MSG_REPORT_REQ, req, sizeof(*req), resp->data, 512 resp_len); 513 if (rc) 514 goto e_free; 515 516 if (copy_to_user((void __user *)arg->resp_data, resp, sizeof(*resp))) 517 rc = -EFAULT; 518 519 e_free: 520 kfree(resp); 521 return rc; 522 } 523 524 static int get_derived_key(struct snp_guest_dev *snp_dev, struct snp_guest_request_ioctl *arg) 525 { 526 struct snp_derived_key_req *req = &snp_dev->req.derived_key; 527 struct snp_guest_crypto *crypto = snp_dev->crypto; 528 struct snp_derived_key_resp resp = {0}; 529 int rc, resp_len; 530 /* Response data is 64 bytes and max authsize for GCM is 16 bytes. */ 531 u8 buf[64 + 16]; 532 533 lockdep_assert_held(&snp_cmd_mutex); 534 535 if (!arg->req_data || !arg->resp_data) 536 return -EINVAL; 537 538 /* 539 * The intermediate response buffer is used while decrypting the 540 * response payload. Make sure that it has enough space to cover the 541 * authtag. 542 */ 543 resp_len = sizeof(resp.data) + crypto->a_len; 544 if (sizeof(buf) < resp_len) 545 return -ENOMEM; 546 547 if (copy_from_user(req, (void __user *)arg->req_data, sizeof(*req))) 548 return -EFAULT; 549 550 rc = handle_guest_request(snp_dev, SVM_VMGEXIT_GUEST_REQUEST, arg, 551 SNP_MSG_KEY_REQ, req, sizeof(*req), buf, resp_len); 552 if (rc) 553 return rc; 554 555 memcpy(resp.data, buf, sizeof(resp.data)); 556 if (copy_to_user((void __user *)arg->resp_data, &resp, sizeof(resp))) 557 rc = -EFAULT; 558 559 /* The response buffer contains the sensitive data, explicitly clear it. */ 560 memzero_explicit(buf, sizeof(buf)); 561 memzero_explicit(&resp, sizeof(resp)); 562 return rc; 563 } 564 565 static int get_ext_report(struct snp_guest_dev *snp_dev, struct snp_guest_request_ioctl *arg, 566 struct snp_req_resp *io) 567 568 { 569 struct snp_ext_report_req *req = &snp_dev->req.ext_report; 570 struct snp_guest_crypto *crypto = snp_dev->crypto; 571 struct snp_report_resp *resp; 572 int ret, npages = 0, resp_len; 573 sockptr_t certs_address; 574 575 lockdep_assert_held(&snp_cmd_mutex); 576 577 if (sockptr_is_null(io->req_data) || sockptr_is_null(io->resp_data)) 578 return -EINVAL; 579 580 if (copy_from_sockptr(req, io->req_data, sizeof(*req))) 581 return -EFAULT; 582 583 /* caller does not want certificate data */ 584 if (!req->certs_len || !req->certs_address) 585 goto cmd; 586 587 if (req->certs_len > SEV_FW_BLOB_MAX_SIZE || 588 !IS_ALIGNED(req->certs_len, PAGE_SIZE)) 589 return -EINVAL; 590 591 if (sockptr_is_kernel(io->resp_data)) { 592 certs_address = KERNEL_SOCKPTR((void *)req->certs_address); 593 } else { 594 certs_address = USER_SOCKPTR((void __user *)req->certs_address); 595 if (!access_ok(certs_address.user, req->certs_len)) 596 return -EFAULT; 597 } 598 599 /* 600 * Initialize the intermediate buffer with all zeros. This buffer 601 * is used in the guest request message to get the certs blob from 602 * the host. If host does not supply any certs in it, then copy 603 * zeros to indicate that certificate data was not provided. 604 */ 605 memset(snp_dev->certs_data, 0, req->certs_len); 606 npages = req->certs_len >> PAGE_SHIFT; 607 cmd: 608 /* 609 * The intermediate response buffer is used while decrypting the 610 * response payload. Make sure that it has enough space to cover the 611 * authtag. 612 */ 613 resp_len = sizeof(resp->data) + crypto->a_len; 614 resp = kzalloc(resp_len, GFP_KERNEL_ACCOUNT); 615 if (!resp) 616 return -ENOMEM; 617 618 snp_dev->input.data_npages = npages; 619 ret = handle_guest_request(snp_dev, SVM_VMGEXIT_EXT_GUEST_REQUEST, arg, 620 SNP_MSG_REPORT_REQ, &req->data, 621 sizeof(req->data), resp->data, resp_len); 622 623 /* If certs length is invalid then copy the returned length */ 624 if (arg->vmm_error == SNP_GUEST_VMM_ERR_INVALID_LEN) { 625 req->certs_len = snp_dev->input.data_npages << PAGE_SHIFT; 626 627 if (copy_to_sockptr(io->req_data, req, sizeof(*req))) 628 ret = -EFAULT; 629 } 630 631 if (ret) 632 goto e_free; 633 634 if (npages && copy_to_sockptr(certs_address, snp_dev->certs_data, req->certs_len)) { 635 ret = -EFAULT; 636 goto e_free; 637 } 638 639 if (copy_to_sockptr(io->resp_data, resp, sizeof(*resp))) 640 ret = -EFAULT; 641 642 e_free: 643 kfree(resp); 644 return ret; 645 } 646 647 static long snp_guest_ioctl(struct file *file, unsigned int ioctl, unsigned long arg) 648 { 649 struct snp_guest_dev *snp_dev = to_snp_dev(file); 650 void __user *argp = (void __user *)arg; 651 struct snp_guest_request_ioctl input; 652 struct snp_req_resp io; 653 int ret = -ENOTTY; 654 655 if (copy_from_user(&input, argp, sizeof(input))) 656 return -EFAULT; 657 658 input.exitinfo2 = 0xff; 659 660 /* Message version must be non-zero */ 661 if (!input.msg_version) 662 return -EINVAL; 663 664 mutex_lock(&snp_cmd_mutex); 665 666 /* Check if the VMPCK is not empty */ 667 if (is_vmpck_empty(snp_dev)) { 668 dev_err_ratelimited(snp_dev->dev, "VMPCK is disabled\n"); 669 mutex_unlock(&snp_cmd_mutex); 670 return -ENOTTY; 671 } 672 673 switch (ioctl) { 674 case SNP_GET_REPORT: 675 ret = get_report(snp_dev, &input); 676 break; 677 case SNP_GET_DERIVED_KEY: 678 ret = get_derived_key(snp_dev, &input); 679 break; 680 case SNP_GET_EXT_REPORT: 681 /* 682 * As get_ext_report() may be called from the ioctl() path and a 683 * kernel internal path (configfs-tsm), decorate the passed 684 * buffers as user pointers. 685 */ 686 io.req_data = USER_SOCKPTR((void __user *)input.req_data); 687 io.resp_data = USER_SOCKPTR((void __user *)input.resp_data); 688 ret = get_ext_report(snp_dev, &input, &io); 689 break; 690 default: 691 break; 692 } 693 694 mutex_unlock(&snp_cmd_mutex); 695 696 if (input.exitinfo2 && copy_to_user(argp, &input, sizeof(input))) 697 return -EFAULT; 698 699 return ret; 700 } 701 702 static void free_shared_pages(void *buf, size_t sz) 703 { 704 unsigned int npages = PAGE_ALIGN(sz) >> PAGE_SHIFT; 705 int ret; 706 707 if (!buf) 708 return; 709 710 ret = set_memory_encrypted((unsigned long)buf, npages); 711 if (ret) { 712 WARN_ONCE(ret, "failed to restore encryption mask (leak it)\n"); 713 return; 714 } 715 716 __free_pages(virt_to_page(buf), get_order(sz)); 717 } 718 719 static void *alloc_shared_pages(struct device *dev, size_t sz) 720 { 721 unsigned int npages = PAGE_ALIGN(sz) >> PAGE_SHIFT; 722 struct page *page; 723 int ret; 724 725 page = alloc_pages(GFP_KERNEL_ACCOUNT, get_order(sz)); 726 if (!page) 727 return NULL; 728 729 ret = set_memory_decrypted((unsigned long)page_address(page), npages); 730 if (ret) { 731 dev_err(dev, "failed to mark page shared, ret=%d\n", ret); 732 __free_pages(page, get_order(sz)); 733 return NULL; 734 } 735 736 return page_address(page); 737 } 738 739 static const struct file_operations snp_guest_fops = { 740 .owner = THIS_MODULE, 741 .unlocked_ioctl = snp_guest_ioctl, 742 }; 743 744 static u8 *get_vmpck(int id, struct snp_secrets_page *secrets, u32 **seqno) 745 { 746 u8 *key = NULL; 747 748 switch (id) { 749 case 0: 750 *seqno = &secrets->os_area.msg_seqno_0; 751 key = secrets->vmpck0; 752 break; 753 case 1: 754 *seqno = &secrets->os_area.msg_seqno_1; 755 key = secrets->vmpck1; 756 break; 757 case 2: 758 *seqno = &secrets->os_area.msg_seqno_2; 759 key = secrets->vmpck2; 760 break; 761 case 3: 762 *seqno = &secrets->os_area.msg_seqno_3; 763 key = secrets->vmpck3; 764 break; 765 default: 766 break; 767 } 768 769 return key; 770 } 771 772 struct snp_msg_report_resp_hdr { 773 u32 status; 774 u32 report_size; 775 u8 rsvd[24]; 776 }; 777 778 struct snp_msg_cert_entry { 779 guid_t guid; 780 u32 offset; 781 u32 length; 782 }; 783 784 static int sev_report_new(struct tsm_report *report, void *data) 785 { 786 struct snp_msg_cert_entry *cert_table; 787 struct tsm_desc *desc = &report->desc; 788 struct snp_guest_dev *snp_dev = data; 789 struct snp_msg_report_resp_hdr hdr; 790 const u32 report_size = SZ_4K; 791 const u32 ext_size = SEV_FW_BLOB_MAX_SIZE; 792 u32 certs_size, i, size = report_size + ext_size; 793 int ret; 794 795 if (desc->inblob_len != SNP_REPORT_USER_DATA_SIZE) 796 return -EINVAL; 797 798 void *buf __free(kvfree) = kvzalloc(size, GFP_KERNEL); 799 if (!buf) 800 return -ENOMEM; 801 802 guard(mutex)(&snp_cmd_mutex); 803 804 /* Check if the VMPCK is not empty */ 805 if (is_vmpck_empty(snp_dev)) { 806 dev_err_ratelimited(snp_dev->dev, "VMPCK is disabled\n"); 807 return -ENOTTY; 808 } 809 810 cert_table = buf + report_size; 811 struct snp_ext_report_req ext_req = { 812 .data = { .vmpl = desc->privlevel }, 813 .certs_address = (__u64)cert_table, 814 .certs_len = ext_size, 815 }; 816 memcpy(&ext_req.data.user_data, desc->inblob, desc->inblob_len); 817 818 struct snp_guest_request_ioctl input = { 819 .msg_version = 1, 820 .req_data = (__u64)&ext_req, 821 .resp_data = (__u64)buf, 822 .exitinfo2 = 0xff, 823 }; 824 struct snp_req_resp io = { 825 .req_data = KERNEL_SOCKPTR(&ext_req), 826 .resp_data = KERNEL_SOCKPTR(buf), 827 }; 828 829 ret = get_ext_report(snp_dev, &input, &io); 830 if (ret) 831 return ret; 832 833 memcpy(&hdr, buf, sizeof(hdr)); 834 if (hdr.status == SEV_RET_INVALID_PARAM) 835 return -EINVAL; 836 if (hdr.status == SEV_RET_INVALID_KEY) 837 return -EINVAL; 838 if (hdr.status) 839 return -ENXIO; 840 if ((hdr.report_size + sizeof(hdr)) > report_size) 841 return -ENOMEM; 842 843 void *rbuf __free(kvfree) = kvzalloc(hdr.report_size, GFP_KERNEL); 844 if (!rbuf) 845 return -ENOMEM; 846 847 memcpy(rbuf, buf + sizeof(hdr), hdr.report_size); 848 report->outblob = no_free_ptr(rbuf); 849 report->outblob_len = hdr.report_size; 850 851 certs_size = 0; 852 for (i = 0; i < ext_size / sizeof(struct snp_msg_cert_entry); i++) { 853 struct snp_msg_cert_entry *ent = &cert_table[i]; 854 855 if (guid_is_null(&ent->guid) && !ent->offset && !ent->length) 856 break; 857 certs_size = max(certs_size, ent->offset + ent->length); 858 } 859 860 /* Suspicious that the response populated entries without populating size */ 861 if (!certs_size && i) 862 dev_warn_ratelimited(snp_dev->dev, "certificate slots conveyed without size\n"); 863 864 /* No certs to report */ 865 if (!certs_size) 866 return 0; 867 868 /* Suspicious that the certificate blob size contract was violated 869 */ 870 if (certs_size > ext_size) { 871 dev_warn_ratelimited(snp_dev->dev, "certificate data truncated\n"); 872 certs_size = ext_size; 873 } 874 875 void *cbuf __free(kvfree) = kvzalloc(certs_size, GFP_KERNEL); 876 if (!cbuf) 877 return -ENOMEM; 878 879 memcpy(cbuf, cert_table, certs_size); 880 report->auxblob = no_free_ptr(cbuf); 881 report->auxblob_len = certs_size; 882 883 return 0; 884 } 885 886 static const struct tsm_ops sev_tsm_ops = { 887 .name = KBUILD_MODNAME, 888 .report_new = sev_report_new, 889 }; 890 891 static void unregister_sev_tsm(void *data) 892 { 893 tsm_unregister(&sev_tsm_ops); 894 } 895 896 static int __init sev_guest_probe(struct platform_device *pdev) 897 { 898 struct sev_guest_platform_data *data; 899 struct snp_secrets_page *secrets; 900 struct device *dev = &pdev->dev; 901 struct snp_guest_dev *snp_dev; 902 struct miscdevice *misc; 903 void __iomem *mapping; 904 int ret; 905 906 if (!cc_platform_has(CC_ATTR_GUEST_SEV_SNP)) 907 return -ENODEV; 908 909 if (!dev->platform_data) 910 return -ENODEV; 911 912 data = (struct sev_guest_platform_data *)dev->platform_data; 913 mapping = ioremap_encrypted(data->secrets_gpa, PAGE_SIZE); 914 if (!mapping) 915 return -ENODEV; 916 917 secrets = (__force void *)mapping; 918 919 ret = -ENOMEM; 920 snp_dev = devm_kzalloc(&pdev->dev, sizeof(struct snp_guest_dev), GFP_KERNEL); 921 if (!snp_dev) 922 goto e_unmap; 923 924 ret = -EINVAL; 925 snp_dev->vmpck = get_vmpck(vmpck_id, secrets, &snp_dev->os_area_msg_seqno); 926 if (!snp_dev->vmpck) { 927 dev_err(dev, "invalid vmpck id %d\n", vmpck_id); 928 goto e_unmap; 929 } 930 931 /* Verify that VMPCK is not zero. */ 932 if (is_vmpck_empty(snp_dev)) { 933 dev_err(dev, "vmpck id %d is null\n", vmpck_id); 934 goto e_unmap; 935 } 936 937 platform_set_drvdata(pdev, snp_dev); 938 snp_dev->dev = dev; 939 snp_dev->secrets = secrets; 940 941 /* Allocate the shared page used for the request and response message. */ 942 snp_dev->request = alloc_shared_pages(dev, sizeof(struct snp_guest_msg)); 943 if (!snp_dev->request) 944 goto e_unmap; 945 946 snp_dev->response = alloc_shared_pages(dev, sizeof(struct snp_guest_msg)); 947 if (!snp_dev->response) 948 goto e_free_request; 949 950 snp_dev->certs_data = alloc_shared_pages(dev, SEV_FW_BLOB_MAX_SIZE); 951 if (!snp_dev->certs_data) 952 goto e_free_response; 953 954 ret = -EIO; 955 snp_dev->crypto = init_crypto(snp_dev, snp_dev->vmpck, VMPCK_KEY_LEN); 956 if (!snp_dev->crypto) 957 goto e_free_cert_data; 958 959 misc = &snp_dev->misc; 960 misc->minor = MISC_DYNAMIC_MINOR; 961 misc->name = DEVICE_NAME; 962 misc->fops = &snp_guest_fops; 963 964 /* initial the input address for guest request */ 965 snp_dev->input.req_gpa = __pa(snp_dev->request); 966 snp_dev->input.resp_gpa = __pa(snp_dev->response); 967 snp_dev->input.data_gpa = __pa(snp_dev->certs_data); 968 969 ret = tsm_register(&sev_tsm_ops, snp_dev, &tsm_report_extra_type); 970 if (ret) 971 goto e_free_cert_data; 972 973 ret = devm_add_action_or_reset(&pdev->dev, unregister_sev_tsm, NULL); 974 if (ret) 975 goto e_free_cert_data; 976 977 ret = misc_register(misc); 978 if (ret) 979 goto e_free_cert_data; 980 981 dev_info(dev, "Initialized SEV guest driver (using vmpck_id %d)\n", vmpck_id); 982 return 0; 983 984 e_free_cert_data: 985 free_shared_pages(snp_dev->certs_data, SEV_FW_BLOB_MAX_SIZE); 986 e_free_response: 987 free_shared_pages(snp_dev->response, sizeof(struct snp_guest_msg)); 988 e_free_request: 989 free_shared_pages(snp_dev->request, sizeof(struct snp_guest_msg)); 990 e_unmap: 991 iounmap(mapping); 992 return ret; 993 } 994 995 static void __exit sev_guest_remove(struct platform_device *pdev) 996 { 997 struct snp_guest_dev *snp_dev = platform_get_drvdata(pdev); 998 999 free_shared_pages(snp_dev->certs_data, SEV_FW_BLOB_MAX_SIZE); 1000 free_shared_pages(snp_dev->response, sizeof(struct snp_guest_msg)); 1001 free_shared_pages(snp_dev->request, sizeof(struct snp_guest_msg)); 1002 deinit_crypto(snp_dev->crypto); 1003 misc_deregister(&snp_dev->misc); 1004 } 1005 1006 /* 1007 * This driver is meant to be a common SEV guest interface driver and to 1008 * support any SEV guest API. As such, even though it has been introduced 1009 * with the SEV-SNP support, it is named "sev-guest". 1010 */ 1011 static struct platform_driver sev_guest_driver = { 1012 .remove_new = __exit_p(sev_guest_remove), 1013 .driver = { 1014 .name = "sev-guest", 1015 }, 1016 }; 1017 1018 module_platform_driver_probe(sev_guest_driver, sev_guest_probe); 1019 1020 MODULE_AUTHOR("Brijesh Singh <brijesh.singh@amd.com>"); 1021 MODULE_LICENSE("GPL"); 1022 MODULE_VERSION("1.0.0"); 1023 MODULE_DESCRIPTION("AMD SEV Guest Driver"); 1024 MODULE_ALIAS("platform:sev-guest"); 1025