Skip to contentSkip to conversation

x86/sgx: Remove unused definitions and reuse launch token size

PATCH Superseded Thorsten Blum 3 participants 10 messages Jul 11 – Jul 14

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover x86/sgx: Remove unused definitions and reuse launch token size

Remove SGX definitions that have never had in-tree users and reuse SGX_LAUNCH_TOKEN_SIZE when validating the launch token in __sgx_virt_einit().

Thorsten Blum (3):
x86/sgx: Remove unused TCS definitions
x86/sgx: Remove unused size and count definitions x86/sgx: Use SGX_LAUNCH_TOKEN_SIZE in __sgx_virt_einit()

 arch/x86/include/asm/sgx.h     | 59 ----------------------------------
 arch/x86/kernel/cpu/sgx/virt.c |  3 +-
 2 files changed, 1 insertion(+), 61 deletions(-)

base-commit: 2d3090a8aeb596a26935db0955d46c9a5db5c6ce

#1 lore raw

PATCH 1/3 x86/sgx: Remove unused TCS definitions

The TCS structure was added with commit 70d3b8ddcd20 ("x86/sgx: Add SGX architectural data structures"), but has never had an in-tree user. Remove the unused TCS structure and its corresponding definitions.

---
 arch/x86/include/asm/sgx.h | 50 --------------------------------------
 1 file changed, 50 deletions(-)
arch/x86/include/asm/sgx.h +0 -50
index 04958459a7ca..fe106c55a593 100644
@@ -193,56 +193,6 @@ struct sgx_secs {
 	u8  reserved4[3834];
 } __packed;
 
-/**
- * enum sgx_tcs_flags - execution flags for TCS
- * @SGX_TCS_DBGOPTIN:	If enabled allows single-stepping and breakpoints
- *			inside an enclave. It is cleared by EADD but can
- *			be set later with EDBGWR.
- */
-enum sgx_tcs_flags {
-	SGX_TCS_DBGOPTIN	= 0x01,
-};
-
-#define SGX_TCS_RESERVED_MASK	GENMASK_ULL(63, 1)
-#define SGX_TCS_RESERVED_SIZE	4024
-
-/**
- * struct sgx_tcs - Thread Control Structure (TCS)
- * @state:		used to mark an entered TCS
- * @flags:		execution flags (cleared by EADD)
- * @ssa_offset:		SSA stack offset relative to the enclave base
- * @ssa_index:		the current SSA frame index (cleard by EADD)
- * @nr_ssa_frames:	the number of frame in the SSA stack
- * @entry_offset:	entry point offset relative to the enclave base
- * @exit_addr:		address outside the enclave to exit on an exception or
- *			interrupt
- * @fs_offset:		offset relative to the enclave base to become FS
- *			segment inside the enclave
- * @gs_offset:		offset relative to the enclave base to become GS
- *			segment inside the enclave
- * @fs_limit:		size to become a new FS-limit (only 32-bit enclaves)
- * @gs_limit:		size to become a new GS-limit (only 32-bit enclaves)
- *
- * Thread Control Structure (TCS) is an enclave page visible in its address
- * space that defines an entry point inside the enclave. A thread enters inside
- * an enclave by supplying address of TCS to ENCLU(EENTER). A TCS can be entered
- * by only one thread at a time.
- */
-struct sgx_tcs {
-	u64 state;
-	u64 flags;
-	u64 ssa_offset;
-	u32 ssa_index;
-	u32 nr_ssa_frames;
-	u64 entry_offset;
-	u64 exit_addr;
-	u64 fs_offset;
-	u64 gs_offset;
-	u32 fs_limit;
-	u32 gs_limit;
-	u8  reserved[SGX_TCS_RESERVED_SIZE];
-} __packed;
-
 /**
  * struct sgx_pageinfo - an enclave page descriptor
  * @addr:	address of the enclave page
maintainer Reviewed-by on v1 1/3
#2 lore raw

Re: [PATCH 1/3] x86/sgx: Remove unused TCS definitions

Quoted 60 lines · Thorsten Blum

The TCS structure was added with commit 70d3b8ddcd20 ("x86/sgx: Add SGX architectural data structures"), but has never had an in-tree user. Remove the unused TCS structure and its corresponding definitions.

---
 arch/x86/include/asm/sgx.h | 50 --------------------------------------
 1 file changed, 50 deletions(-)
arch/x86/include/asm/sgx.h +0 -43
index 04958459a7ca..fe106c55a593 100644
@@ -193,56 +193,6 @@ struct sgx_secs {
 	u8  reserved4[3834];
 } __packed;
 
-/**
- * enum sgx_tcs_flags - execution flags for TCS
- * @SGX_TCS_DBGOPTIN:	If enabled allows single-stepping and breakpoints
- *			inside an enclave. It is cleared by EADD but can
- *			be set later with EDBGWR.
- */
-enum sgx_tcs_flags {
-	SGX_TCS_DBGOPTIN	= 0x01,
-};
-
-#define SGX_TCS_RESERVED_MASK	GENMASK_ULL(63, 1)
-#define SGX_TCS_RESERVED_SIZE	4024
-
-/**
- * struct sgx_tcs - Thread Control Structure (TCS)
- * @state:		used to mark an entered TCS
- * @flags:		execution flags (cleared by EADD)
- * @ssa_offset:		SSA stack offset relative to the enclave base
- * @ssa_index:		the current SSA frame index (cleard by EADD)
- * @nr_ssa_frames:	the number of frame in the SSA stack
- * @entry_offset:	entry point offset relative to the enclave base
- * @exit_addr:		address outside the enclave to exit on an exception or
- *			interrupt
- * @fs_offset:		offset relative to the enclave base to become FS
- *			segment inside the enclave
- * @gs_offset:		offset relative to the enclave base to become GS
- *			segment inside the enclave
- * @fs_limit:		size to become a new FS-limit (only 32-bit enclaves)
- * @gs_limit:		size to become a new GS-limit (only 32-bit enclaves)
- *
- * Thread Control Structure (TCS) is an enclave page visible in its address
- * space that defines an entry point inside the enclave. A thread enters inside
- * an enclave by supplying address of TCS to ENCLU(EENTER). A TCS can be entered
- * by only one thread at a time.
- */
-struct sgx_tcs {
-	u64 state;
-	u64 flags;
-	u64 ssa_offset;
-	u32 ssa_index;
-	u32 nr_ssa_frames;
-	u64 entry_offset;
-	u64 exit_addr;

arch/x86/include/asm/sgx.h struct sgx_secs { · line 198

Quoted 10 lines
arch/x86/include/asm/sgx.h +0 -7
-	u64 fs_offset;
-	u64 gs_offset;
-	u32 fs_limit;
-	u32 gs_limit;
-	u8  reserved[SGX_TCS_RESERVED_SIZE];
-} __packed;
-
 /**
  * struct sgx_pageinfo - an enclave page descriptor
  * @addr:	address of the enclave page

BR, Jarkko

#4 lore raw

PATCH 2/3 x86/sgx: Remove unused size and count definitions

In reply to Thorsten Blum

The definitions were added with commit 70d3b8ddcd20 ("x86/sgx: Add SGX architectural data structures"), but have never been used. The SIGSTRUCT reserved field sizes are also redundant because the structure uses literal sizes instead. Remove them.

---
 arch/x86/include/asm/sgx.h | 9 ---------
 1 file changed, 9 deletions(-)
arch/x86/include/asm/sgx.h +0 -9
index fe106c55a593..af5cd6ce9010 100644
@@ -108,9 +108,6 @@ enum sgx_miscselect {
 
 #define SGX_MISC_RESERVED_MASK	GENMASK_ULL(63, 1)
 
-#define SGX_SSA_GPRS_SIZE		184
-#define SGX_SSA_MISC_EXINFO_SIZE	16
-
 /**
  * enum sgx_attribute - the attributes field in &struct sgx_secs
  * @SGX_ATTR_INIT:		Enclave can be entered (is initialized).
@@ -227,7 +224,6 @@ enum sgx_page_type {
 	SGX_PAGE_TYPE_TRIM,
 };
 
-#define SGX_NR_PAGE_TYPES	5
 #define SGX_PAGE_TYPE_MASK	GENMASK(7, 0)
 
 /**
@@ -288,11 +284,6 @@ struct sgx_pcmd {
 	u8  mac[16];
 } __packed __aligned(128);
 
-#define SGX_SIGSTRUCT_RESERVED1_SIZE 84
-#define SGX_SIGSTRUCT_RESERVED2_SIZE 20
-#define SGX_SIGSTRUCT_RESERVED3_SIZE 32
-#define SGX_SIGSTRUCT_RESERVED4_SIZE 12
-
 /**
  * struct sgx_sigstruct_header -  defines author of the enclave
  * @header1:		constant byte string
maintainer Reviewed-by on v1 2/3
#5 lore raw

Re: [PATCH 2/3] x86/sgx: Remove unused size and count definitions

Quoted 34 lines · Thorsten Blum

The definitions were added with commit 70d3b8ddcd20 ("x86/sgx: Add SGX architectural data structures"), but have never been used. The SIGSTRUCT reserved field sizes are also redundant because the structure uses literal sizes instead. Remove them.

---
 arch/x86/include/asm/sgx.h | 9 ---------
 1 file changed, 9 deletions(-)
arch/x86/include/asm/sgx.h +0 -4
index fe106c55a593..af5cd6ce9010 100644
@@ -108,9 +108,6 @@ enum sgx_miscselect {
 
 #define SGX_MISC_RESERVED_MASK	GENMASK_ULL(63, 1)
 
-#define SGX_SSA_GPRS_SIZE		184
-#define SGX_SSA_MISC_EXINFO_SIZE	16
-
 /**
  * enum sgx_attribute - the attributes field in &struct sgx_secs
  * @SGX_ATTR_INIT:		Enclave can be entered (is initialized).
@@ -227,7 +224,6 @@ enum sgx_page_type {
 	SGX_PAGE_TYPE_TRIM,
 };
 
-#define SGX_NR_PAGE_TYPES	5
 #define SGX_PAGE_TYPE_MASK	GENMASK(7, 0)
 
 /**
@@ -288,11 +284,6 @@ struct sgx_pcmd {
 	u8  mac[16];

arch/x86/include/asm/sgx.h struct sgx_pcmd { · line 289

Quoted 10 lines
arch/x86/include/asm/sgx.h +0 -5
 } __packed __aligned(128);
 
-#define SGX_SIGSTRUCT_RESERVED1_SIZE 84
-#define SGX_SIGSTRUCT_RESERVED2_SIZE 20
-#define SGX_SIGSTRUCT_RESERVED3_SIZE 32
-#define SGX_SIGSTRUCT_RESERVED4_SIZE 12
-
 /**
  * struct sgx_sigstruct_header -  defines author of the enclave
  * @header1:		constant byte string

BR, Jarkko

Acked-by on v1 2/3
#6 lore raw

Re: [PATCH 2/3] x86/sgx: Remove unused size and count definitions

In reply to Thorsten Blum
Quoted 6 lines · Thorsten Blum

The definitions were added with commit 70d3b8ddcd20 ("x86/sgx: Add SGX architectural data structures"), but have never been used. The SIGSTRUCT reserved field sizes are also redundant because the structure uses literal sizes instead. Remove them.

#7 lore raw

PATCH 3/3 x86/sgx: Use SGX_LAUNCH_TOKEN_SIZE in __sgx_virt_einit()

In reply to Thorsten Blum

Both sgx_ioc_enclave_init() and __sgx_virt_einit() use the same 304-byte launch token. Use SGX_LAUNCH_TOKEN_SIZE in __sgx_virt_einit() instead of maintaining a second local definition, ensuring the token initialization and validation paths use the same size.

---
 arch/x86/kernel/cpu/sgx/virt.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
arch/x86/kernel/cpu/sgx/virt.c +1 -2
index db6806c40483..fa9173a9384a 100644
@@ -394,9 +394,8 @@ static int __sgx_virt_einit(void __user *sigstruct, void __user *token,
 	 * All other checks deferred to ENCLS itself.  Also see comment
 	 * for @secs in sgx_virt_ecreate().
 	 */
-#define SGX_EINITTOKEN_SIZE	304
 	if (WARN_ON_ONCE(!access_ok(sigstruct, sizeof(struct sgx_sigstruct)) ||
-			 !access_ok(token, SGX_EINITTOKEN_SIZE) ||
+			 !access_ok(token, SGX_LAUNCH_TOKEN_SIZE) ||
 			 !access_ok(secs, PAGE_SIZE)))
 		return -EINVAL;
 
maintainer Reviewed-by on v1 3/3
#8 lore raw

Re: [PATCH 3/3] x86/sgx: Use SGX_LAUNCH_TOKEN_SIZE in __sgx_virt_einit()

Quoted 15 lines · Thorsten Blum

Both sgx_ioc_enclave_init() and __sgx_virt_einit() use the same 304-byte launch token. Use SGX_LAUNCH_TOKEN_SIZE in __sgx_virt_einit() instead of maintaining a second local definition, ensuring the token initialization and validation paths use the same size.

---
 arch/x86/kernel/cpu/sgx/virt.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
arch/x86/kernel/cpu/sgx/virt.c +0 -0
index db6806c40483..fa9173a9384a 100644

arch/x86/kernel/cpu/sgx/virt.c static int __sgx_virt_einit(void __user *sigstruct, void __user *token, · line 401

Quoted 10 lines
arch/x86/kernel/cpu/sgx/virt.c +1 -2
@@ -394,9 +394,8 @@ static int __sgx_virt_einit(void __user *sigstruct, void __user *token,
 	 * All other checks deferred to ENCLS itself.  Also see comment
 	 * for @secs in sgx_virt_ecreate().
 	 */
-#define SGX_EINITTOKEN_SIZE	304
 	if (WARN_ON_ONCE(!access_ok(sigstruct, sizeof(struct sgx_sigstruct)) ||
-			 !access_ok(token, SGX_EINITTOKEN_SIZE) ||
+			 !access_ok(token, SGX_LAUNCH_TOKEN_SIZE) ||
 			 !access_ok(secs, PAGE_SIZE)))
 		return -EINVAL;
 

true:

❯ git grep LAUNCH_TOKEN_SIZE arch/x86/include/asm/sgx.h:#define SGX_LAUNCH_TOKEN_SIZE 304
arch/x86/kernel/cpu/sgx/ioctl.c: memset(token, 0, SGX_LAUNCH_TOKEN_SIZE);

BR, Jarkko

Reviewed-by on v1 3/3
#9 lore raw

Re: [PATCH 3/3] x86/sgx: Use SGX_LAUNCH_TOKEN_SIZE in __sgx_virt_einit()

In reply to Thorsten Blum
Quoted 4 lines · Thorsten Blum

Both sgx_ioc_enclave_init() and __sgx_virt_einit() use the same 304-byte launch token. Use SGX_LAUNCH_TOKEN_SIZE in __sgx_virt_einit() instead of maintaining a second local definition,

[...]

Quoted 2 lines

ensuring the token initialization
and validation paths use the same size.

Nit:

This part isn't that accurate to me. sgx_ioc_enclave_init() is for running host SGX enclave and __sgx_virt_einit() is for executing the EINIT trapped from KVM guests. I.e., IMHO they are not "token initialization" and "token validation" which sounds like two sides of the same coin. And the access_ok() doesn't validate the content of the EINITTOKEN anyway.

IHMO we can just remove this part.

Quoted 11 lines
---
 arch/x86/kernel/cpu/sgx/virt.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
arch/x86/kernel/cpu/sgx/virt.c +0 -0
index db6806c40483..fa9173a9384a 100644

arch/x86/kernel/cpu/sgx/virt.c static int __sgx_virt_einit(void __user *sigstruct, void __user *token, · line 401

Quoted 10 lines
arch/x86/kernel/cpu/sgx/virt.c +1 -2
@@ -394,9 +394,8 @@ static int __sgx_virt_einit(void __user *sigstruct, void __user *token,
 	 * All other checks deferred to ENCLS itself.  Also see comment
 	 * for @secs in sgx_virt_ecreate().
 	 */
-#define SGX_EINITTOKEN_SIZE	304
 	if (WARN_ON_ONCE(!access_ok(sigstruct, sizeof(struct sgx_sigstruct)) ||
-			 !access_ok(token, SGX_EINITTOKEN_SIZE) ||
+			 !access_ok(token, SGX_LAUNCH_TOKEN_SIZE) ||
 			 !access_ok(secs, PAGE_SIZE)))
 		return -EINVAL;
 

Not sure whether the name SGX_EINITTOKEN_SIZE slightly matches better, but I am fine with both.

With the nit mentioned in changelog fixed: