xref: /linux/tools/testing/selftests/tdx/tdx_guest_test.c (revision 9a87ffc99ec8eb8d35eed7c4f816d75f5cc9662e)
100e07cfbSKuppuswamy Sathyanarayanan // SPDX-License-Identifier: GPL-2.0
200e07cfbSKuppuswamy Sathyanarayanan /*
300e07cfbSKuppuswamy Sathyanarayanan  * Test TDX guest features
400e07cfbSKuppuswamy Sathyanarayanan  *
500e07cfbSKuppuswamy Sathyanarayanan  * Copyright (C) 2022 Intel Corporation.
600e07cfbSKuppuswamy Sathyanarayanan  *
700e07cfbSKuppuswamy Sathyanarayanan  * Author: Kuppuswamy Sathyanarayanan <sathyanarayanan.kuppuswamy@linux.intel.com>
800e07cfbSKuppuswamy Sathyanarayanan  */
900e07cfbSKuppuswamy Sathyanarayanan 
1000e07cfbSKuppuswamy Sathyanarayanan #include <sys/ioctl.h>
1100e07cfbSKuppuswamy Sathyanarayanan 
1200e07cfbSKuppuswamy Sathyanarayanan #include <errno.h>
1300e07cfbSKuppuswamy Sathyanarayanan #include <fcntl.h>
1400e07cfbSKuppuswamy Sathyanarayanan 
15*5adbe55cSMathieu Desnoyers #include <linux/tdx-guest.h>
1600e07cfbSKuppuswamy Sathyanarayanan #include "../kselftest_harness.h"
1700e07cfbSKuppuswamy Sathyanarayanan 
1800e07cfbSKuppuswamy Sathyanarayanan #define TDX_GUEST_DEVNAME "/dev/tdx_guest"
1900e07cfbSKuppuswamy Sathyanarayanan #define HEX_DUMP_SIZE 8
2000e07cfbSKuppuswamy Sathyanarayanan #define DEBUG 0
2100e07cfbSKuppuswamy Sathyanarayanan 
2200e07cfbSKuppuswamy Sathyanarayanan /**
2300e07cfbSKuppuswamy Sathyanarayanan  * struct tdreport_type - Type header of TDREPORT_STRUCT.
2400e07cfbSKuppuswamy Sathyanarayanan  * @type: Type of the TDREPORT (0 - SGX, 81 - TDX, rest are reserved)
2500e07cfbSKuppuswamy Sathyanarayanan  * @sub_type: Subtype of the TDREPORT (Default value is 0).
2600e07cfbSKuppuswamy Sathyanarayanan  * @version: TDREPORT version (Default value is 0).
2700e07cfbSKuppuswamy Sathyanarayanan  * @reserved: Added for future extension.
2800e07cfbSKuppuswamy Sathyanarayanan  *
2900e07cfbSKuppuswamy Sathyanarayanan  * More details can be found in TDX v1.0 module specification, sec
3000e07cfbSKuppuswamy Sathyanarayanan  * titled "REPORTTYPE".
3100e07cfbSKuppuswamy Sathyanarayanan  */
3200e07cfbSKuppuswamy Sathyanarayanan struct tdreport_type {
3300e07cfbSKuppuswamy Sathyanarayanan 	__u8 type;
3400e07cfbSKuppuswamy Sathyanarayanan 	__u8 sub_type;
3500e07cfbSKuppuswamy Sathyanarayanan 	__u8 version;
3600e07cfbSKuppuswamy Sathyanarayanan 	__u8 reserved;
3700e07cfbSKuppuswamy Sathyanarayanan };
3800e07cfbSKuppuswamy Sathyanarayanan 
3900e07cfbSKuppuswamy Sathyanarayanan /**
4000e07cfbSKuppuswamy Sathyanarayanan  * struct reportmac - TDX guest report data, MAC and TEE hashes.
4100e07cfbSKuppuswamy Sathyanarayanan  * @type: TDREPORT type header.
4200e07cfbSKuppuswamy Sathyanarayanan  * @reserved1: Reserved for future extension.
4300e07cfbSKuppuswamy Sathyanarayanan  * @cpu_svn: CPU security version.
4400e07cfbSKuppuswamy Sathyanarayanan  * @tee_tcb_info_hash: SHA384 hash of TEE TCB INFO.
4500e07cfbSKuppuswamy Sathyanarayanan  * @tee_td_info_hash: SHA384 hash of TDINFO_STRUCT.
4600e07cfbSKuppuswamy Sathyanarayanan  * @reportdata: User defined unique data passed in TDG.MR.REPORT request.
4700e07cfbSKuppuswamy Sathyanarayanan  * @reserved2: Reserved for future extension.
4800e07cfbSKuppuswamy Sathyanarayanan  * @mac: CPU MAC ID.
4900e07cfbSKuppuswamy Sathyanarayanan  *
5000e07cfbSKuppuswamy Sathyanarayanan  * It is MAC-protected and contains hashes of the remainder of the
5100e07cfbSKuppuswamy Sathyanarayanan  * report structure along with user provided report data. More details can
5200e07cfbSKuppuswamy Sathyanarayanan  * be found in TDX v1.0 Module specification, sec titled "REPORTMACSTRUCT"
5300e07cfbSKuppuswamy Sathyanarayanan  */
5400e07cfbSKuppuswamy Sathyanarayanan struct reportmac {
5500e07cfbSKuppuswamy Sathyanarayanan 	struct tdreport_type type;
5600e07cfbSKuppuswamy Sathyanarayanan 	__u8 reserved1[12];
5700e07cfbSKuppuswamy Sathyanarayanan 	__u8 cpu_svn[16];
5800e07cfbSKuppuswamy Sathyanarayanan 	__u8 tee_tcb_info_hash[48];
5900e07cfbSKuppuswamy Sathyanarayanan 	__u8 tee_td_info_hash[48];
6000e07cfbSKuppuswamy Sathyanarayanan 	__u8 reportdata[64];
6100e07cfbSKuppuswamy Sathyanarayanan 	__u8 reserved2[32];
6200e07cfbSKuppuswamy Sathyanarayanan 	__u8 mac[32];
6300e07cfbSKuppuswamy Sathyanarayanan };
6400e07cfbSKuppuswamy Sathyanarayanan 
6500e07cfbSKuppuswamy Sathyanarayanan /**
6600e07cfbSKuppuswamy Sathyanarayanan  * struct td_info - TDX guest measurements and configuration.
6700e07cfbSKuppuswamy Sathyanarayanan  * @attr: TDX Guest attributes (like debug, spet_disable, etc).
6800e07cfbSKuppuswamy Sathyanarayanan  * @xfam: Extended features allowed mask.
6900e07cfbSKuppuswamy Sathyanarayanan  * @mrtd: Build time measurement register.
7000e07cfbSKuppuswamy Sathyanarayanan  * @mrconfigid: Software-defined ID for non-owner-defined configuration
7100e07cfbSKuppuswamy Sathyanarayanan  *              of the guest - e.g., run-time or OS configuration.
7200e07cfbSKuppuswamy Sathyanarayanan  * @mrowner: Software-defined ID for the guest owner.
7300e07cfbSKuppuswamy Sathyanarayanan  * @mrownerconfig: Software-defined ID for owner-defined configuration of
7400e07cfbSKuppuswamy Sathyanarayanan  *                 the guest - e.g., specific to the workload.
7500e07cfbSKuppuswamy Sathyanarayanan  * @rtmr: Run time measurement registers.
7600e07cfbSKuppuswamy Sathyanarayanan  * @reserved: Added for future extension.
7700e07cfbSKuppuswamy Sathyanarayanan  *
7800e07cfbSKuppuswamy Sathyanarayanan  * It contains the measurements and initial configuration of the TDX guest
7900e07cfbSKuppuswamy Sathyanarayanan  * that was locked at initialization and a set of measurement registers
8000e07cfbSKuppuswamy Sathyanarayanan  * that are run-time extendable. More details can be found in TDX v1.0
8100e07cfbSKuppuswamy Sathyanarayanan  * Module specification, sec titled "TDINFO_STRUCT".
8200e07cfbSKuppuswamy Sathyanarayanan  */
8300e07cfbSKuppuswamy Sathyanarayanan struct td_info {
8400e07cfbSKuppuswamy Sathyanarayanan 	__u8 attr[8];
8500e07cfbSKuppuswamy Sathyanarayanan 	__u64 xfam;
8600e07cfbSKuppuswamy Sathyanarayanan 	__u64 mrtd[6];
8700e07cfbSKuppuswamy Sathyanarayanan 	__u64 mrconfigid[6];
8800e07cfbSKuppuswamy Sathyanarayanan 	__u64 mrowner[6];
8900e07cfbSKuppuswamy Sathyanarayanan 	__u64 mrownerconfig[6];
9000e07cfbSKuppuswamy Sathyanarayanan 	__u64 rtmr[24];
9100e07cfbSKuppuswamy Sathyanarayanan 	__u64 reserved[14];
9200e07cfbSKuppuswamy Sathyanarayanan };
9300e07cfbSKuppuswamy Sathyanarayanan 
9400e07cfbSKuppuswamy Sathyanarayanan /*
9500e07cfbSKuppuswamy Sathyanarayanan  * struct tdreport - Output of TDCALL[TDG.MR.REPORT].
9600e07cfbSKuppuswamy Sathyanarayanan  * @reportmac: Mac protected header of size 256 bytes.
9700e07cfbSKuppuswamy Sathyanarayanan  * @tee_tcb_info: Additional attestable elements in the TCB are not
9800e07cfbSKuppuswamy Sathyanarayanan  *                reflected in the reportmac.
9900e07cfbSKuppuswamy Sathyanarayanan  * @reserved: Added for future extension.
10000e07cfbSKuppuswamy Sathyanarayanan  * @tdinfo: Measurements and configuration data of size 512 bytes.
10100e07cfbSKuppuswamy Sathyanarayanan  *
10200e07cfbSKuppuswamy Sathyanarayanan  * More details can be found in TDX v1.0 Module specification, sec
10300e07cfbSKuppuswamy Sathyanarayanan  * titled "TDREPORT_STRUCT".
10400e07cfbSKuppuswamy Sathyanarayanan  */
10500e07cfbSKuppuswamy Sathyanarayanan struct tdreport {
10600e07cfbSKuppuswamy Sathyanarayanan 	struct reportmac reportmac;
10700e07cfbSKuppuswamy Sathyanarayanan 	__u8 tee_tcb_info[239];
10800e07cfbSKuppuswamy Sathyanarayanan 	__u8 reserved[17];
10900e07cfbSKuppuswamy Sathyanarayanan 	struct td_info tdinfo;
11000e07cfbSKuppuswamy Sathyanarayanan };
11100e07cfbSKuppuswamy Sathyanarayanan 
print_array_hex(const char * title,const char * prefix_str,const void * buf,int len)11200e07cfbSKuppuswamy Sathyanarayanan static void print_array_hex(const char *title, const char *prefix_str,
11300e07cfbSKuppuswamy Sathyanarayanan 			    const void *buf, int len)
11400e07cfbSKuppuswamy Sathyanarayanan {
11500e07cfbSKuppuswamy Sathyanarayanan 	int i, j, line_len, rowsize = HEX_DUMP_SIZE;
11600e07cfbSKuppuswamy Sathyanarayanan 	const __u8 *ptr = buf;
11700e07cfbSKuppuswamy Sathyanarayanan 
11800e07cfbSKuppuswamy Sathyanarayanan 	printf("\t\t%s", title);
11900e07cfbSKuppuswamy Sathyanarayanan 
12000e07cfbSKuppuswamy Sathyanarayanan 	for (j = 0; j < len; j += rowsize) {
12100e07cfbSKuppuswamy Sathyanarayanan 		line_len = rowsize < (len - j) ? rowsize : (len - j);
12200e07cfbSKuppuswamy Sathyanarayanan 		printf("%s%.8x:", prefix_str, j);
12300e07cfbSKuppuswamy Sathyanarayanan 		for (i = 0; i < line_len; i++)
12400e07cfbSKuppuswamy Sathyanarayanan 			printf(" %.2x", ptr[j + i]);
12500e07cfbSKuppuswamy Sathyanarayanan 		printf("\n");
12600e07cfbSKuppuswamy Sathyanarayanan 	}
12700e07cfbSKuppuswamy Sathyanarayanan 
12800e07cfbSKuppuswamy Sathyanarayanan 	printf("\n");
12900e07cfbSKuppuswamy Sathyanarayanan }
13000e07cfbSKuppuswamy Sathyanarayanan 
TEST(verify_report)13100e07cfbSKuppuswamy Sathyanarayanan TEST(verify_report)
13200e07cfbSKuppuswamy Sathyanarayanan {
13300e07cfbSKuppuswamy Sathyanarayanan 	struct tdx_report_req req;
13400e07cfbSKuppuswamy Sathyanarayanan 	struct tdreport *tdreport;
13500e07cfbSKuppuswamy Sathyanarayanan 	int devfd, i;
13600e07cfbSKuppuswamy Sathyanarayanan 
13700e07cfbSKuppuswamy Sathyanarayanan 	devfd = open(TDX_GUEST_DEVNAME, O_RDWR | O_SYNC);
13800e07cfbSKuppuswamy Sathyanarayanan 	ASSERT_LT(0, devfd);
13900e07cfbSKuppuswamy Sathyanarayanan 
14000e07cfbSKuppuswamy Sathyanarayanan 	/* Generate sample report data */
14100e07cfbSKuppuswamy Sathyanarayanan 	for (i = 0; i < TDX_REPORTDATA_LEN; i++)
14200e07cfbSKuppuswamy Sathyanarayanan 		req.reportdata[i] = i;
14300e07cfbSKuppuswamy Sathyanarayanan 
14400e07cfbSKuppuswamy Sathyanarayanan 	/* Get TDREPORT */
14500e07cfbSKuppuswamy Sathyanarayanan 	ASSERT_EQ(0, ioctl(devfd, TDX_CMD_GET_REPORT0, &req));
14600e07cfbSKuppuswamy Sathyanarayanan 
14700e07cfbSKuppuswamy Sathyanarayanan 	if (DEBUG) {
14800e07cfbSKuppuswamy Sathyanarayanan 		print_array_hex("\n\t\tTDX report data\n", "",
14900e07cfbSKuppuswamy Sathyanarayanan 				req.reportdata, sizeof(req.reportdata));
15000e07cfbSKuppuswamy Sathyanarayanan 
15100e07cfbSKuppuswamy Sathyanarayanan 		print_array_hex("\n\t\tTDX tdreport data\n", "",
15200e07cfbSKuppuswamy Sathyanarayanan 				req.tdreport, sizeof(req.tdreport));
15300e07cfbSKuppuswamy Sathyanarayanan 	}
15400e07cfbSKuppuswamy Sathyanarayanan 
15500e07cfbSKuppuswamy Sathyanarayanan 	/* Make sure TDREPORT data includes the REPORTDATA passed */
15600e07cfbSKuppuswamy Sathyanarayanan 	tdreport = (struct tdreport *)req.tdreport;
15700e07cfbSKuppuswamy Sathyanarayanan 	ASSERT_EQ(0, memcmp(&tdreport->reportmac.reportdata[0],
15800e07cfbSKuppuswamy Sathyanarayanan 			    req.reportdata, sizeof(req.reportdata)));
15900e07cfbSKuppuswamy Sathyanarayanan 
16000e07cfbSKuppuswamy Sathyanarayanan 	ASSERT_EQ(0, close(devfd));
16100e07cfbSKuppuswamy Sathyanarayanan }
16200e07cfbSKuppuswamy Sathyanarayanan 
16300e07cfbSKuppuswamy Sathyanarayanan TEST_HARNESS_MAIN
164