tdx_guest_test.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Test TDX guest features
  4. *
  5. * Copyright (C) 2022 Intel Corporation.
  6. *
  7. * Author: Kuppuswamy Sathyanarayanan <sathyanarayanan.kuppuswamy@linux.intel.com>
  8. */
  9. #include <sys/ioctl.h>
  10. #include <errno.h>
  11. #include <fcntl.h>
  12. #include <linux/tdx-guest.h>
  13. #include "kselftest_harness.h"
  14. #define TDX_GUEST_DEVNAME "/dev/tdx_guest"
  15. #define HEX_DUMP_SIZE 8
  16. #define DEBUG 0
  17. /**
  18. * struct tdreport_type - Type header of TDREPORT_STRUCT.
  19. * @type: Type of the TDREPORT (0 - SGX, 81 - TDX, rest are reserved)
  20. * @sub_type: Subtype of the TDREPORT (Default value is 0).
  21. * @version: TDREPORT version (Default value is 0).
  22. * @reserved: Added for future extension.
  23. *
  24. * More details can be found in TDX v1.0 module specification, sec
  25. * titled "REPORTTYPE".
  26. */
  27. struct tdreport_type {
  28. __u8 type;
  29. __u8 sub_type;
  30. __u8 version;
  31. __u8 reserved;
  32. };
  33. /**
  34. * struct reportmac - TDX guest report data, MAC and TEE hashes.
  35. * @type: TDREPORT type header.
  36. * @reserved1: Reserved for future extension.
  37. * @cpu_svn: CPU security version.
  38. * @tee_tcb_info_hash: SHA384 hash of TEE TCB INFO.
  39. * @tee_td_info_hash: SHA384 hash of TDINFO_STRUCT.
  40. * @reportdata: User defined unique data passed in TDG.MR.REPORT request.
  41. * @reserved2: Reserved for future extension.
  42. * @mac: CPU MAC ID.
  43. *
  44. * It is MAC-protected and contains hashes of the remainder of the
  45. * report structure along with user provided report data. More details can
  46. * be found in TDX v1.0 Module specification, sec titled "REPORTMACSTRUCT"
  47. */
  48. struct reportmac {
  49. struct tdreport_type type;
  50. __u8 reserved1[12];
  51. __u8 cpu_svn[16];
  52. __u8 tee_tcb_info_hash[48];
  53. __u8 tee_td_info_hash[48];
  54. __u8 reportdata[64];
  55. __u8 reserved2[32];
  56. __u8 mac[32];
  57. };
  58. /**
  59. * struct td_info - TDX guest measurements and configuration.
  60. * @attr: TDX Guest attributes (like debug, spet_disable, etc).
  61. * @xfam: Extended features allowed mask.
  62. * @mrtd: Build time measurement register.
  63. * @mrconfigid: Software-defined ID for non-owner-defined configuration
  64. * of the guest - e.g., run-time or OS configuration.
  65. * @mrowner: Software-defined ID for the guest owner.
  66. * @mrownerconfig: Software-defined ID for owner-defined configuration of
  67. * the guest - e.g., specific to the workload.
  68. * @rtmr: Run time measurement registers.
  69. * @reserved: Added for future extension.
  70. *
  71. * It contains the measurements and initial configuration of the TDX guest
  72. * that was locked at initialization and a set of measurement registers
  73. * that are run-time extendable. More details can be found in TDX v1.0
  74. * Module specification, sec titled "TDINFO_STRUCT".
  75. */
  76. struct td_info {
  77. __u8 attr[8];
  78. __u64 xfam;
  79. __u64 mrtd[6];
  80. __u64 mrconfigid[6];
  81. __u64 mrowner[6];
  82. __u64 mrownerconfig[6];
  83. __u64 rtmr[24];
  84. __u64 reserved[14];
  85. };
  86. /*
  87. * struct tdreport - Output of TDCALL[TDG.MR.REPORT].
  88. * @reportmac: Mac protected header of size 256 bytes.
  89. * @tee_tcb_info: Additional attestable elements in the TCB are not
  90. * reflected in the reportmac.
  91. * @reserved: Added for future extension.
  92. * @tdinfo: Measurements and configuration data of size 512 bytes.
  93. *
  94. * More details can be found in TDX v1.0 Module specification, sec
  95. * titled "TDREPORT_STRUCT".
  96. */
  97. struct tdreport {
  98. struct reportmac reportmac;
  99. __u8 tee_tcb_info[239];
  100. __u8 reserved[17];
  101. struct td_info tdinfo;
  102. };
  103. static void print_array_hex(const char *title, const char *prefix_str,
  104. const void *buf, int len)
  105. {
  106. int i, j, line_len, rowsize = HEX_DUMP_SIZE;
  107. const __u8 *ptr = buf;
  108. printf("\t\t%s", title);
  109. for (j = 0; j < len; j += rowsize) {
  110. line_len = rowsize < (len - j) ? rowsize : (len - j);
  111. printf("%s%.8x:", prefix_str, j);
  112. for (i = 0; i < line_len; i++)
  113. printf(" %.2x", ptr[j + i]);
  114. printf("\n");
  115. }
  116. printf("\n");
  117. }
  118. TEST(verify_report)
  119. {
  120. struct tdx_report_req req;
  121. struct tdreport *tdreport;
  122. int devfd, i;
  123. devfd = open(TDX_GUEST_DEVNAME, O_RDWR | O_SYNC);
  124. ASSERT_LT(0, devfd);
  125. /* Generate sample report data */
  126. for (i = 0; i < TDX_REPORTDATA_LEN; i++)
  127. req.reportdata[i] = i;
  128. /* Get TDREPORT */
  129. ASSERT_EQ(0, ioctl(devfd, TDX_CMD_GET_REPORT0, &req));
  130. if (DEBUG) {
  131. print_array_hex("\n\t\tTDX report data\n", "",
  132. req.reportdata, sizeof(req.reportdata));
  133. print_array_hex("\n\t\tTDX tdreport data\n", "",
  134. req.tdreport, sizeof(req.tdreport));
  135. }
  136. /* Make sure TDREPORT data includes the REPORTDATA passed */
  137. tdreport = (struct tdreport *)req.tdreport;
  138. ASSERT_EQ(0, memcmp(&tdreport->reportmac.reportdata[0],
  139. req.reportdata, sizeof(req.reportdata)));
  140. ASSERT_EQ(0, close(devfd));
  141. }
  142. TEST_HARNESS_MAIN