Skip to contentSkip to conversation

x86/sgx: Improve permission handing

PATCH In review Jarkko Sakkinen 3 participants 29 messages Aug 19, 2019 – Aug 23, 2019

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

cover x86/sgx: Improve permission handing

Refine the naming, documentation and validation of the page permissions. silent behaviour to occur.

Cc Sean Christopherson <[email protected]>
Cc Shay Katz-zamir <[email protected]>
Cc Serge Ayoun <[email protected]>

Jarkko Sakkinen (5):
x86/sgx: Document permission handling better x86/sgx: Use memchr_inv() in sgx_validate_secinfo() x86/sgx: Make sgx_validate_secinfo() more readable x86/sgx: Validate TCS permssions in sgx_validate_secinfo() x86/sgx: Rename vm_prot_bits as max_vm_flags

 arch/x86/kernel/cpu/sgx/driver/ioctl.c | 36 +++++++++++++++++++-------
 arch/x86/kernel/cpu/sgx/encl.c         | 14 +++++-----
 arch/x86/kernel/cpu/sgx/encl.h         |  4 +--
 3 files changed, 35 insertions(+), 19 deletions(-)
Signature · 1 line
2.20.1
#1 lore raw

PATCH 1/5 x86/sgx: Document permission handling better

The way permissions are managed inside the driver is not trivial and intuitive. The non-obvious parts were not properly remarked in the source code. This patch refines them a bit.

Cc Sean Christopherson <[email protected]>
Cc Shay Katz-zamir <[email protected]>
Cc Serge Ayoun <[email protected]>
---
 arch/x86/kernel/cpu/sgx/driver/ioctl.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)
arch/x86/kernel/cpu/sgx/driver/ioctl.c +8 -1
index 9b784a061a47..64d3286f3324 100644
@@ -259,7 +259,10 @@ static struct sgx_encl_page *sgx_encl_page_alloc(struct sgx_encl *encl,
 		return ERR_PTR(-ENOMEM);
 	encl_page->desc = addr;
 	encl_page->encl = encl;
+
+	/* Calculate maximum of the VM flags for the page. */
 	encl_page->vm_prot_bits = calc_vm_prot_bits(prot, 0);
+
 	ret = radix_tree_insert(&encl->page_tree, PFN_DOWN(encl_page->desc),
 				encl_page);
 	if (ret) {
@@ -640,11 +643,15 @@ static long sgx_ioc_enclave_add_page(struct file *filep, void __user *arg)
 
 	data = kmap(data_page);
 
+	/* Set prot bits matching to the SECINFO permissions. */
 	prot = _calc_vm_trans(secinfo.flags, SGX_SECINFO_R, PROT_READ)  |
 	       _calc_vm_trans(secinfo.flags, SGX_SECINFO_W, PROT_WRITE) |
 	       _calc_vm_trans(secinfo.flags, SGX_SECINFO_X, PROT_EXEC);
 
-	/* TCS pages need to be RW in the PTEs, but can be 0 in the EPCM. */
+	/* TCS pages must always RW set for CPU access while the SECINFO
+	 * permissions are *always* zero - the CPU ignores the user provided
+	 * values and silently overwrites zero permissions.
+	 */
 	if ((secinfo.flags & SGX_SECINFO_PAGE_TYPE_MASK) == SGX_SECINFO_TCS)
 		prot |= PROT_READ | PROT_WRITE;
 
Signature · 1 line
2.20.1
#2 lore raw

Re: [PATCH 1/5] x86/sgx: Document permission handling better

Quoted 28 lines · Jarkko Sakkinen

The way permissions are managed inside the driver is not trivial and intuitive. The non-obvious parts were not properly remarked in the source code. This patch refines them a bit.

Cc Sean Christopherson <[email protected]>
Cc Shay Katz-zamir <[email protected]>
Cc Serge Ayoun <[email protected]>
---
 arch/x86/kernel/cpu/sgx/driver/ioctl.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)
arch/x86/kernel/cpu/sgx/driver/ioctl.c +3 -0
index 9b784a061a47..64d3286f3324 100644
@@ -259,7 +259,10 @@ static struct sgx_encl_page *sgx_encl_page_alloc(struct sgx_encl *encl,
 		return ERR_PTR(-ENOMEM);
 	encl_page->desc = addr;
 	encl_page->encl = encl;
+
+	/* Calculate maximum of the VM flags for the page. */
 	encl_page->vm_prot_bits = calc_vm_prot_bits(prot, 0);
+
 	ret = radix_tree_insert(&encl->page_tree, PFN_DOWN(encl_page->desc),
 				encl_page);
 	if (ret) {

arch/x86/kernel/cpu/sgx/driver/ioctl.c static long sgx_ioc_enclave_add_page(struct file *filep, void __user *arg) · line 651

Quoted 10 lines
arch/x86/kernel/cpu/sgx/driver/ioctl.c +2 -1
@@ -640,11 +643,15 @@ static long sgx_ioc_enclave_add_page(struct file *filep, void __user *arg)
 
 	data = kmap(data_page);
 
+	/* Set prot bits matching to the SECINFO permissions. */
 	prot = _calc_vm_trans(secinfo.flags, SGX_SECINFO_R, PROT_READ)  |
 	       _calc_vm_trans(secinfo.flags, SGX_SECINFO_W, PROT_WRITE) |
 	       _calc_vm_trans(secinfo.flags, SGX_SECINFO_X, PROT_EXEC);
 
-	/* TCS pages need to be RW in the PTEs, but can be 0 in the EPCM. */
+	/* TCS pages must always RW set for CPU access while the SECINFO

Should be

	/*
	 * TCS pages ...

arch/x86/kernel/cpu/sgx/driver/ioctl.c static long sgx_ioc_enclave_add_page(struct file *filep, void __user *arg) · line 653

Quoted 2 lines
arch/x86/kernel/cpu/sgx/driver/ioctl.c +2 -0
+	 * permissions are *always* zero - the CPU ignores the user provided
+	 * values and silently overwrites zero permissions.

Maybe 'overwrites with zero permissions'?

Quoted 7 lines
arch/x86/kernel/cpu/sgx/driver/ioctl.c +1 -0
+	 */
 	if ((secinfo.flags & SGX_SECINFO_PAGE_TYPE_MASK) == SGX_SECINFO_TCS)
 		prot |= PROT_READ | PROT_WRITE;
 
Signature · 1 line
2.20.1
#3 lore raw

Re: [PATCH 1/5] x86/sgx: Document permission handling better

Quoted 5 lines · Sean Christopherson

arch/x86/kernel/cpu/sgx/driver/ioctl.c static long sgx_ioc_enclave_add_page(struct file *filep, void __user *arg) · line 651

Quoted 2 lines
arch/x86/kernel/cpu/sgx/driver/ioctl.c +1 -1
-	/* TCS pages need to be RW in the PTEs, but can be 0 in the EPCM. */
+	/* TCS pages must always RW set for CPU access while the SECINFO

Should be

Quoted 7 lines
	/*
	 * TCS pages ...

arch/x86/kernel/cpu/sgx/driver/ioctl.c static long sgx_ioc_enclave_add_page(struct file *filep, void __user *arg) · line 653

Quoted 2 lines
arch/x86/kernel/cpu/sgx/driver/ioctl.c +2 -0
+	 * permissions are *always* zero - the CPU ignores the user provided
+	 * values and silently overwrites zero permissions.

Maybe 'overwrites with zero permissions'?

I squashed this with the proposed changes made. Thank you.

/Jarkko

#4 lore raw

PATCH 2/5 x86/sgx: Use memchr_inv() in sgx_validate_secinfo()

In reply to Jarkko Sakkinen

Instead of looping through the array of reserved bytes use memchr_inv() to check the bytes.

Cc Sean Christopherson <[email protected]>
Cc Shay Katz-zamir <[email protected]>
Cc Serge Ayoun <[email protected]>
---
 arch/x86/kernel/cpu/sgx/driver/ioctl.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
arch/x86/kernel/cpu/sgx/driver/ioctl.c +2 -4
index 64d3286f3324..d5f326411df0 100644
@@ -414,7 +414,6 @@ static int sgx_validate_secinfo(struct sgx_secinfo *secinfo)
 {
 	u64 page_type = secinfo->flags & SGX_SECINFO_PAGE_TYPE_MASK;
 	u64 perm = secinfo->flags & SGX_SECINFO_PERMISSION_MASK;
-	int i;
 
 	if ((secinfo->flags & SGX_SECINFO_RESERVED_MASK) ||
 	    ((perm & SGX_SECINFO_W) && !(perm & SGX_SECINFO_R)) ||
@@ -422,9 +421,8 @@ static int sgx_validate_secinfo(struct sgx_secinfo *secinfo)
 	     page_type != SGX_SECINFO_REG))
 		return -EINVAL;
 
-	for (i = 0; i < SGX_SECINFO_RESERVED_SIZE; i++)
-		if (secinfo->reserved[i])
-			return -EINVAL;
+	if (memchr_inv(secinfo->reserved, 0, SGX_SECINFO_RESERVED_SIZE))
+		return -EINVAL;
 
 	return 0;
 }
Signature · 1 line
2.20.1
#5 lore raw

Re: [PATCH 2/5] x86/sgx: Use memchr_inv() in sgx_validate_secinfo()

Quoted 24 lines · Jarkko Sakkinen

Instead of looping through the array of reserved bytes use memchr_inv() to check the bytes.

Cc Sean Christopherson <[email protected]>
Cc Shay Katz-zamir <[email protected]>
Cc Serge Ayoun <[email protected]>
---
 arch/x86/kernel/cpu/sgx/driver/ioctl.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
arch/x86/kernel/cpu/sgx/driver/ioctl.c +0 -1
index 64d3286f3324..d5f326411df0 100644
@@ -414,7 +414,6 @@ static int sgx_validate_secinfo(struct sgx_secinfo *secinfo)
 {
 	u64 page_type = secinfo->flags & SGX_SECINFO_PAGE_TYPE_MASK;
 	u64 perm = secinfo->flags & SGX_SECINFO_PERMISSION_MASK;
-	int i;
 
 	if ((secinfo->flags & SGX_SECINFO_RESERVED_MASK) ||
 	    ((perm & SGX_SECINFO_W) && !(perm & SGX_SECINFO_R)) ||

arch/x86/kernel/cpu/sgx/driver/ioctl.c static int sgx_validate_secinfo(struct sgx_secinfo *secinfo) · line 424

Quoted 7 lines
arch/x86/kernel/cpu/sgx/driver/ioctl.c +1 -3
@@ -422,9 +421,8 @@ static int sgx_validate_secinfo(struct sgx_secinfo *secinfo)
 	     page_type != SGX_SECINFO_REG))
 		return -EINVAL;
 
-	for (i = 0; i < SGX_SECINFO_RESERVED_SIZE; i++)
-		if (secinfo->reserved[i])
-			return -EINVAL;
+	if (memchr_inv(secinfo->reserved, 0, SGX_SECINFO_RESERVED_SIZE))

Doing 'sizeof(secinfo->reserved)' would be preferable, that way we're not dependent on SGX_SECINFO_RESERVED_SIZE being in bytes (I had to check).

Obviously not in this patch, but the same cleanup can be applied to sgx_validate_secs().

Quoted 7 lines
arch/x86/kernel/cpu/sgx/driver/ioctl.c +1 -0
+		return -EINVAL;
 
 	return 0;
 }
Signature · 1 line
2.20.1
#6 lore raw

Re: [PATCH 2/5] x86/sgx: Use memchr_inv() in sgx_validate_secinfo()

Quoted 7 lines · Sean Christopherson

arch/x86/kernel/cpu/sgx/driver/ioctl.c static int sgx_validate_secinfo(struct sgx_secinfo *secinfo) · line 424

Quoted 1 line
arch/x86/kernel/cpu/sgx/driver/ioctl.c +1 -0
+	if (memchr_inv(secinfo->reserved, 0, SGX_SECINFO_RESERVED_SIZE))

Doing 'sizeof(secinfo->reserved)' would be preferable, that way we're not dependent on SGX_SECINFO_RESERVED_SIZE being in bytes (I had to check).

Obviously not in this patch, but the same cleanup can be applied to sgx_validate_secs().

Thanks for the valid remark, I squashed with that change. I also edited prepending commit and removed the constant altogether.

/Jarkko

#7 lore raw

PATCH 3/5 x86/sgx: Make sgx_validate_secinfo() more readable

In reply to Jarkko Sakkinen

Split the huge conditional statement to three separate ones in order to make it easier to understand what is going on in the validation code.

Cc Sean Christopherson <[email protected]>
Cc Shay Katz-zamir <[email protected]>
Cc Serge Ayoun <[email protected]>
---
 arch/x86/kernel/cpu/sgx/driver/ioctl.c | 13 +++++++++----
 1 file changed, 9 insertions(+), 4 deletions(-)
arch/x86/kernel/cpu/sgx/driver/ioctl.c +9 -4
index d5f326411df0..99b1b9776c3a 100644
@@ -415,10 +415,15 @@ static int sgx_validate_secinfo(struct sgx_secinfo *secinfo)
 	u64 page_type = secinfo->flags & SGX_SECINFO_PAGE_TYPE_MASK;
 	u64 perm = secinfo->flags & SGX_SECINFO_PERMISSION_MASK;
 
-	if ((secinfo->flags & SGX_SECINFO_RESERVED_MASK) ||
-	    ((perm & SGX_SECINFO_W) && !(perm & SGX_SECINFO_R)) ||
-	    (page_type != SGX_SECINFO_TCS && page_type != SGX_SECINFO_TRIM &&
-	     page_type != SGX_SECINFO_REG))
+	if ((page_type != SGX_SECINFO_REG &&
+	     page_type != SGX_SECINFO_TCS &&
+	     page_type != SGX_SECINFO_TRIM))
+		return -EINVAL;
+
+	if ((perm & SGX_SECINFO_W) && !(perm & SGX_SECINFO_R))
+		return -EINVAL;
+
+	if (secinfo->flags & SGX_SECINFO_RESERVED_MASK)
 		return -EINVAL;
 
 	if (memchr_inv(secinfo->reserved, 0, SGX_SECINFO_RESERVED_SIZE))
Signature · 1 line
2.20.1
#8 lore raw

Re: [PATCH 3/5] x86/sgx: Make sgx_validate_secinfo() more readable

Quoted 17 lines · Jarkko Sakkinen

Split the huge conditional statement to three separate ones in order to make it easier to understand what is going on in the validation code.

Cc Sean Christopherson <[email protected]>
Cc Shay Katz-zamir <[email protected]>
Cc Serge Ayoun <[email protected]>
---
 arch/x86/kernel/cpu/sgx/driver/ioctl.c | 13 +++++++++----
 1 file changed, 9 insertions(+), 4 deletions(-)
arch/x86/kernel/cpu/sgx/driver/ioctl.c +0 -0
index d5f326411df0..99b1b9776c3a 100644

arch/x86/kernel/cpu/sgx/driver/ioctl.c static int sgx_validate_secinfo(struct sgx_secinfo *secinfo) · line 420

Quoted 10 lines
arch/x86/kernel/cpu/sgx/driver/ioctl.c +3 -4
@@ -415,10 +415,15 @@ static int sgx_validate_secinfo(struct sgx_secinfo *secinfo)
 	u64 page_type = secinfo->flags & SGX_SECINFO_PAGE_TYPE_MASK;
 	u64 perm = secinfo->flags & SGX_SECINFO_PERMISSION_MASK;
 
-	if ((secinfo->flags & SGX_SECINFO_RESERVED_MASK) ||
-	    ((perm & SGX_SECINFO_W) && !(perm & SGX_SECINFO_R)) ||
-	    (page_type != SGX_SECINFO_TCS && page_type != SGX_SECINFO_TRIM &&
-	     page_type != SGX_SECINFO_REG))
+	if ((page_type != SGX_SECINFO_REG &&
+	     page_type != SGX_SECINFO_TCS &&
+	     page_type != SGX_SECINFO_TRIM))

Shouldn't we disallow TRIM until SGX2 is supported?

Quoted 12 lines
arch/x86/kernel/cpu/sgx/driver/ioctl.c +6 -0
+		return -EINVAL;
+
+	if ((perm & SGX_SECINFO_W) && !(perm & SGX_SECINFO_R))
+		return -EINVAL;
+
+	if (secinfo->flags & SGX_SECINFO_RESERVED_MASK)
 		return -EINVAL;
 
 	if (memchr_inv(secinfo->reserved, 0, SGX_SECINFO_RESERVED_SIZE))
Signature · 1 line
2.20.1
#9 lore raw

Re: [PATCH 3/5] x86/sgx: Make sgx_validate_secinfo() more readable

Quoted 5 lines · Sean Christopherson

arch/x86/kernel/cpu/sgx/driver/ioctl.c static int sgx_validate_secinfo(struct sgx_secinfo *secinfo) · line 420

Quoted 3 lines
arch/x86/kernel/cpu/sgx/driver/ioctl.c +3 -0
+	if ((page_type != SGX_SECINFO_REG &&
+	     page_type != SGX_SECINFO_TCS &&
+	     page_type != SGX_SECINFO_TRIM))

Shouldn't we disallow TRIM until SGX2 is supported?

Yes.

I squashed the change with TRIM removed.

I also renamed the local variable page_type as pt. I think that is a good abbrevation for page type and saves some screen space.

/Jarkko

#10 lore raw

RE: [PATCH 3/5] x86/sgx: Make sgx_validate_secinfo() more readable

In reply to Jarkko Sakkinen
Quoted 33 lines

From: Jarkko Sakkinen <[email protected]>
Sent: Monday, August 19, 2019 18:26
To: [email protected]
Cc: Jarkko Sakkinen <[email protected]>; Sean Christopherson <[email protected]>; Katz-zamir, Shay <shay.katz- [email protected]>; Ayoun, Serge <[email protected]>
Subject: [PATCH 3/5] x86/sgx: Make sgx_validate_secinfo() more readable

Split the huge conditional statement to three separate ones in order to make it easier to understand what is going on in the validation code.

Cc Sean Christopherson <[email protected]>
Cc Shay Katz-zamir <[email protected]>
Cc Serge Ayoun <[email protected]>
---
 arch/x86/kernel/cpu/sgx/driver/ioctl.c | 13 +++++++++----
 1 file changed, 9 insertions(+), 4 deletions(-)
arch/x86/kernel/cpu/sgx/driver/ioctl.c +0 -3
index d5f326411df0..99b1b9776c3a 100644
@@ -415,10 +415,15 @@ static int sgx_validate_secinfo(struct sgx_secinfo
 *secinfo)
 	u64 page_type = secinfo->flags & SGX_SECINFO_PAGE_TYPE_MASK;
 	u64 perm = secinfo->flags & SGX_SECINFO_PERMISSION_MASK;
 
-	if ((secinfo->flags & SGX_SECINFO_RESERVED_MASK) ||
-	    ((perm & SGX_SECINFO_W) && !(perm & SGX_SECINFO_R)) ||
-	    (page_type != SGX_SECINFO_TCS && page_type !=

SGX_SECINFO_TRIM &&

arch/x86/kernel/cpu/sgx/driver/ioctl.c static int sgx_validate_secinfo(struct sgx_secinfo *secinfo) · line 421

Quoted 5 lines
arch/x86/kernel/cpu/sgx/driver/ioctl.c +4 -1
-	     page_type != SGX_SECINFO_REG))
+	if ((page_type != SGX_SECINFO_REG &&
+	     page_type != SGX_SECINFO_TCS &&
+	     page_type != SGX_SECINFO_TRIM))
+		return -EINVAL;

sgx_validate_secinfo() is called via eadd ioctl. Eadd will fail with TRIM page type, so you probably need to remove it from the if sgx2.0 does not change this behavior

Quoted 11 lines
arch/x86/kernel/cpu/sgx/driver/ioctl.c +5 -0
+
+	if ((perm & SGX_SECINFO_W) && !(perm & SGX_SECINFO_R))
+		return -EINVAL;
+
+	if (secinfo->flags & SGX_SECINFO_RESERVED_MASK)
 		return -EINVAL;
 
 	if (memchr_inv(secinfo->reserved, 0,

SGX_SECINFO_RESERVED_SIZE))
--
2.20.1

---------------------------------------------------------------------
Intel Israel (74) Limited

This e-mail and any attachments may contain confidential material for the sole use of the intended recipient(s). Any review or distribution by others is strictly prohibited. If you are not the intended recipient, please contact the sender and delete all copies.

#11 lore raw

Re: [PATCH 3/5] x86/sgx: Make sgx_validate_secinfo() more readable

Quoted 8 lines · Serge

arch/x86/kernel/cpu/sgx/driver/ioctl.c static int sgx_validate_secinfo(struct sgx_secinfo *secinfo) · line 421

Quoted 4 lines
arch/x86/kernel/cpu/sgx/driver/ioctl.c +4 -0
+	if ((page_type != SGX_SECINFO_REG &&
+	     page_type != SGX_SECINFO_TCS &&
+	     page_type != SGX_SECINFO_TRIM))
+		return -EINVAL;

sgx_validate_secinfo() is called via eadd ioctl. Eadd will fail with TRIM page type, so you probably need to remove it from the if sgx2.0 does not change this behavior

Thanks! Removed.

/Jarkko

#12 lore raw

PATCH 4/5 x86/sgx: Validate TCS permssions in sgx_validate_secinfo()

In reply to Jarkko Sakkinen

The validation of TCS permissions was missing from sgx_validate_secinfo(). This patch adds the validation.

---
 arch/x86/kernel/cpu/sgx/driver/ioctl.c | 6 ++++++
 1 file changed, 6 insertions(+)
arch/x86/kernel/cpu/sgx/driver/ioctl.c +6 -0
index 99b1b9776c3a..2415dcb20a6e 100644
@@ -423,6 +423,12 @@ static int sgx_validate_secinfo(struct sgx_secinfo *secinfo)
 	if ((perm & SGX_SECINFO_W) && !(perm & SGX_SECINFO_R))
 		return -EINVAL;
 
+	/* CPU will silently overwrite the permissions as zero, which means
+	 * that we need to validate it ourselves.
+	 */
+	if (page_type == SGX_SECINFO_TCS && perm)
+		return -EINVAL;
+
 	if (secinfo->flags & SGX_SECINFO_RESERVED_MASK)
 		return -EINVAL;
 
Signature · 1 line
2.20.1
#13 lore raw

Re: [PATCH 4/5] x86/sgx: Validate TCS permssions in sgx_validate_secinfo()

Quoted 28 lines · Jarkko Sakkinen

The validation of TCS permissions was missing from sgx_validate_secinfo(). This patch adds the validation.

---
 arch/x86/kernel/cpu/sgx/driver/ioctl.c | 6 ++++++
 1 file changed, 6 insertions(+)
arch/x86/kernel/cpu/sgx/driver/ioctl.c +6 -0
index 99b1b9776c3a..2415dcb20a6e 100644
@@ -423,6 +423,12 @@ static int sgx_validate_secinfo(struct sgx_secinfo *secinfo)
 	if ((perm & SGX_SECINFO_W) && !(perm & SGX_SECINFO_R))
 		return -EINVAL;
 
+	/* CPU will silently overwrite the permissions as zero, which means
+	 * that we need to validate it ourselves.
+	 */
+	if (page_type == SGX_SECINFO_TCS && perm)
+		return -EINVAL;
+
 	if (secinfo->flags & SGX_SECINFO_RESERVED_MASK)
 		return -EINVAL;
 
Signature · 1 line
2.20.1

OK, just found out that this patch did not end up to my test image and causes a regression.

I think this should be fixed in sgx_encl_may_map() by having the following special case for TCS (in addition to the change in this patch of course):

1. Check if we encounter a TCS page.
2. If yes, we evaluate RW for the VM flags.

/Jarkko

#14 lore raw

RE: [PATCH 4/5] x86/sgx: Validate TCS permssions in sgx_validate_secinfo()

Quoted 43 lines

From: [email protected] <[email protected]> On Behalf Of Jarkko Sakkinen
Sent: Wednesday, August 21, 2019 21:46
To: [email protected]
Cc: Christopherson, Sean J <[email protected]>
Subject: Re: [PATCH 4/5] x86/sgx: Validate TCS permssions in sgx_validate_secinfo()

arch/x86/kernel/cpu/sgx/driver/ioctl.c static int sgx_validate_secinfo(struct sgx_secinfo

Quoted 14 lines · Jarkko Sakkinen

The validation of TCS permissions was missing from sgx_validate_secinfo(). This patch adds the validation.

---
 arch/x86/kernel/cpu/sgx/driver/ioctl.c | 6 ++++++
 1 file changed, 6 insertions(+)
arch/x86/kernel/cpu/sgx/driver/ioctl.c +0 -0
index 99b1b9776c3a..2415dcb20a6e 100644
@@ -423,6 +423,12 @@ static int sgx_validate_secinfo(struct sgx_secinfo

*secinfo)

Quoted 13 lines
arch/x86/kernel/cpu/sgx/driver/ioctl.c +6 -1
 	if ((perm & SGX_SECINFO_W) && !(perm & SGX_SECINFO_R))
 		return -EINVAL;
 
+	/* CPU will silently overwrite the permissions as zero, which means
+	 * that we need to validate it ourselves.
+	 */
+	if (page_type == SGX_SECINFO_TCS && perm)
+		return -EINVAL;
+
 	if (secinfo->flags & SGX_SECINFO_RESERVED_MASK)
 		return -EINVAL;
 
--
Quoted 2 lines

2.20.1

OK, just found out that this patch did not end up to my test image and causes a regression.

Quoted 7 lines

I think this should be fixed in sgx_encl_may_map() by having the following special case for TCS (in addition to the change in this patch of course):

1. Check if we encounter a TCS page.
2. If yes, we evaluate RW for the VM flags.

Also replying to Sean.
Sean is right that never mind the value in secsinfo->flags, HW will reset RWX For TCS pages.
So basically you may not enforce and and could not check those but... The signature depends On those flags, so if you put a non-zero flag value, eadd will pass but if you compute the signature according to this non zero value then you will have
a delta between ur signature and HW's signature: einit will fail.
So this is tricky and more a usability issue.
I vote for checking the flag is zeroed.
---------------------------------------------------------------------
Intel Israel (74) Limited

This e-mail and any attachments may contain confidential material for the sole use of the intended recipient(s). Any review or distribution by others is strictly prohibited. If you are not the intended recipient, please contact the sender and delete all copies.

#15 lore raw

Re: [PATCH 4/5] x86/sgx: Validate TCS permssions in sgx_validate_secinfo()

Quoted 7 lines · Serge

Sean is right that never mind the value in secsinfo->flags, HW will reset RWX For TCS pages. So basically you may not enforce and and could not check those but... The signature depends On those flags, so if you put a non-zero flag value, eadd will pass but if you compute the signature according to this non zero value then you will have a delta between ur signature and HW's
signature: einit will fail. So this is tricky and more a usability issue. I vote for checking the flag is zeroed.

Ugh, didn't think about that behavior. That's obnoxious. Adding the check makes sense in that case.

#16 lore raw

Re: [PATCH 4/5] x86/sgx: Validate TCS permssions in sgx_validate_secinfo()

In reply to Ayoun, Serge
Quoted 9 lines · Serge

Also replying to Sean.
Sean is right that never mind the value in secsinfo->flags, HW will reset RWX For TCS pages.
So basically you may not enforce and and could not check those but... The signature depends On those flags, so if you put a non-zero flag value, eadd will pass but if you compute the signature according to this non zero value then you will have
a delta between ur signature and HW's signature: einit will fail.
So this is tricky and more a usability issue.
I vote for checking the flag is zeroed.

As I responded to Sean that as long as the ioctl does not adjust prot bits I'm cool with any sane solution. What do you think of requiring at minimum RW?

Doing that kind of adjusting is just doing fixup's for corrupted data from the user space.

/Jarkko

#17 lore raw

Re: [PATCH 4/5] x86/sgx: Validate TCS permssions in sgx_validate_secinfo()

Quoted 11 lines · Jarkko Sakkinen
Quoted 9 lines · Serge

Also replying to Sean.
Sean is right that never mind the value in secsinfo->flags, HW will reset RWX For TCS pages.
So basically you may not enforce and and could not check those but... The signature depends On those flags, so if you put a non-zero flag value, eadd will pass but if you compute the signature according to this non zero value then you will have
a delta between ur signature and HW's signature: einit will fail.
So this is tricky and more a usability issue.
I vote for checking the flag is zeroed.

Quoted 6 lines

As I responded to Sean that as long as the ioctl does not adjust prot bits I'm cool with any sane solution. What do you think of requiring at minimum RW?

Doing that kind of adjusting is just doing fixup's for corrupted data from the user space.

Kind of missed your comment about EINIT in rush! A valid point and good catch.

I still think my 2nd proposal would be more appropriate than this patch. Signatures will work and we don't need special cases anywhere.

/Jarkko

#18 lore raw

Re: [PATCH 4/5] x86/sgx: Validate TCS permssions in sgx_validate_secinfo()

In reply to Jarkko Sakkinen
Quoted 13 lines · Jarkko Sakkinen

The validation of TCS permissions was missing from sgx_validate_secinfo(). This patch adds the validation.

---
 arch/x86/kernel/cpu/sgx/driver/ioctl.c | 6 ++++++
 1 file changed, 6 insertions(+)
arch/x86/kernel/cpu/sgx/driver/ioctl.c +0 -0
index 99b1b9776c3a..2415dcb20a6e 100644

arch/x86/kernel/cpu/sgx/driver/ioctl.c static int sgx_validate_secinfo(struct sgx_secinfo *secinfo) · line 426

Quoted 4 lines
arch/x86/kernel/cpu/sgx/driver/ioctl.c +1 -0
@@ -423,6 +423,12 @@ static int sgx_validate_secinfo(struct sgx_secinfo *secinfo)
 	if ((perm & SGX_SECINFO_W) && !(perm & SGX_SECINFO_R))
 		return -EINVAL;
 
+	/* CPU will silently overwrite the permissions as zero, which means

Comment style again. I looked it up just to double check :)

Documentation/process/coding-style.rst says:

When commenting the kernel API functions, please use the kernel-doc format. See the files at :ref:Documentation/doc-guide/ <doc_guide> and ``scripts/kernel-doc`` for details.

The preferred style for long (multi-line) comments is:

.. code-block:: c

/*
* This is the preferred style for multi-line
* comments in the Linux kernel source code.
* Please use it consistently.
*
* Description: A column of asterisks on the left side,
* with beginning and ending almost-blank lines. */

For files in net/ and drivers/net/ the preferred style for long (multi-line) comments is a little different.

.. code-block:: c

/* The preferred comment style for files in net/ and drivers/net
* looks like this.
*
* It is nearly the same as the generally preferred comment style,
* but there is no initial almost-blank line.
*/

arch/x86/kernel/cpu/sgx/driver/ioctl.c static int sgx_validate_secinfo(struct sgx_secinfo *secinfo) · line 430

Quoted 4 lines
arch/x86/kernel/cpu/sgx/driver/ioctl.c +4 -0
+	 * that we need to validate it ourselves.
+	 */
+	if (page_type == SGX_SECINFO_TCS && perm)
+		return -EINVAL;

Why are we validating the TCS protection bits? Hardware ignores them, so why do we care? sgx_ioc_enclave_add_page() sets the internal protection bits so there's no danger of putting the wrong thing in the page tables.

Quoted 7 lines
arch/x86/kernel/cpu/sgx/driver/ioctl.c +1 -0
+
 	if (secinfo->flags & SGX_SECINFO_RESERVED_MASK)
 		return -EINVAL;
 
Signature · 1 line
2.20.1
#19 lore raw

Re: [PATCH 4/5] x86/sgx: Validate TCS permssions in sgx_validate_secinfo()

Quoted 3 lines · Sean Christopherson

Why are we validating the TCS protection bits? Hardware ignores them, so why do we care? sgx_ioc_enclave_add_page() sets the internal protection bits so there's no danger of putting the wrong thing in the page tables.

I think that in this commit I got it wrong but I think this is awkward:

	/*
	 * TCS pages must always RW set for CPU access while the SECINFO
	 * permissions are *always* zero - the CPU ignores the user provided
	 * values and silently overwrites with zero permissions.
	 */
	if ((secinfo.flags & SGX_SECINFO_PAGE_TYPE_MASK) == SGX_SECINFO_TCS)
		prot |= PROT_READ | PROT_WRITE;

In my opinion the right thing to do would be check that SECINFO has *at minimum* RW and return -EINVAL if not.

I don't like the SGX silently adjusting permissions like this.

/Jarkko

#20 lore raw

Re: [PATCH 4/5] x86/sgx: Validate TCS permssions in sgx_validate_secinfo()

Quoted 15 lines · Jarkko Sakkinen
Quoted 3 lines · Sean Christopherson

Why are we validating the TCS protection bits? Hardware ignores them, so why do we care? sgx_ioc_enclave_add_page() sets the internal protection bits so there's no danger of putting the wrong thing in the page tables.

I think that in this commit I got it wrong but I think this is awkward:

	/*
	 * TCS pages must always RW set for CPU access while the SECINFO
	 * permissions are *always* zero - the CPU ignores the user provided
	 * values and silently overwrites with zero permissions.
	 */
	if ((secinfo.flags & SGX_SECINFO_PAGE_TYPE_MASK) == SGX_SECINFO_TCS)
		prot |= PROT_READ | PROT_WRITE;
Quoted 2 lines

In my opinion the right thing to do would be check that SECINFO has *at minimum* RW and return -EINVAL if not.

Based on Serge's comment, hardware updates MRENCLAVE with SECINFO *after* it overwrites the flags for TCS pages. I.e. requiring RW for the TCS would result in every enclave failing EINIT due to an invalid measurement. It'd be fairly easy to verify this if we want to triple check that that is indeed hardware behavior.

#21 lore raw

Re: [PATCH 4/5] x86/sgx: Validate TCS permssions in sgx_validate_secinfo()

Quoted 16 lines · Sean Christopherson
Quoted 15 lines · Jarkko Sakkinen
Quoted 3 lines · Sean Christopherson

Why are we validating the TCS protection bits? Hardware ignores them, so why do we care? sgx_ioc_enclave_add_page() sets the internal protection bits so there's no danger of putting the wrong thing in the page tables.

I think that in this commit I got it wrong but I think this is awkward:

	/*
	 * TCS pages must always RW set for CPU access while the SECINFO
	 * permissions are *always* zero - the CPU ignores the user provided
	 * values and silently overwrites with zero permissions.
	 */
	if ((secinfo.flags & SGX_SECINFO_PAGE_TYPE_MASK) == SGX_SECINFO_TCS)
		prot |= PROT_READ | PROT_WRITE;
Quoted 8 lines
Quoted 2 lines

In my opinion the right thing to do would be check that SECINFO has *at minimum* RW and return -EINVAL if not.

Based on Serge's comment, hardware updates MRENCLAVE with SECINFO *after* it overwrites the flags for TCS pages. I.e. requiring RW for the TCS would result in every enclave failing EINIT due to an invalid measurement. It'd be fairly easy to verify this if we want to triple check that that is indeed hardware behavior.

This is from the signing tool that I wrote back in 2016 used in the selftest:

struct mreadd {
	uint64_t tag;
	uint64_t offset;
	uint64_t flags; /* SECINFO flags */
	uint8_t reserved[40];
} __attribute__((__packed__));

static bool mrenclave_eadd(EVP_MD_CTX *ctx, uint64_t offset, uint64_t flags)

{
	struct mreadd mreadd;

	memset(&mreadd, 0, sizeof(mreadd));
	mreadd.tag = MREADD;
	mreadd.offset = offset;
	mreadd.flags = flags;

	return mrenclave_update(ctx, &mreadd);
}

If MRENCLAVE was updated after the overwrite, this would not work.

The least confusing semantics would be to require RW, no more or less.

/Jarkko

#22 lore raw

Re: [PATCH 4/5] x86/sgx: Validate TCS permssions in sgx_validate_secinfo()

Quoted 48 lines · Jarkko Sakkinen
Quoted 16 lines · Sean Christopherson
Quoted 15 lines · Jarkko Sakkinen
Quoted 3 lines · Sean Christopherson

Why are we validating the TCS protection bits? Hardware ignores them, so why do we care? sgx_ioc_enclave_add_page() sets the internal protection bits so there's no danger of putting the wrong thing in the page tables.

I think that in this commit I got it wrong but I think this is awkward:

	/*
	 * TCS pages must always RW set for CPU access while the SECINFO
	 * permissions are *always* zero - the CPU ignores the user provided
	 * values and silently overwrites with zero permissions.
	 */
	if ((secinfo.flags & SGX_SECINFO_PAGE_TYPE_MASK) == SGX_SECINFO_TCS)
		prot |= PROT_READ | PROT_WRITE;
Quoted 8 lines
Quoted 2 lines

In my opinion the right thing to do would be check that SECINFO has *at minimum* RW and return -EINVAL if not.

Based on Serge's comment, hardware updates MRENCLAVE with SECINFO *after* it overwrites the flags for TCS pages. I.e. requiring RW for the TCS would result in every enclave failing EINIT due to an invalid measurement. It'd be fairly easy to verify this if we want to triple check that that is indeed hardware behavior.

This is from the signing tool that I wrote back in 2016 used in the selftest:

struct mreadd {
	uint64_t tag;
	uint64_t offset;
	uint64_t flags; /* SECINFO flags */
	uint8_t reserved[40];
} __attribute__((__packed__));

static bool mrenclave_eadd(EVP_MD_CTX *ctx, uint64_t offset, uint64_t flags)

{
	struct mreadd mreadd;

	memset(&mreadd, 0, sizeof(mreadd));
	mreadd.tag = MREADD;
	mreadd.offset = offset;
	mreadd.flags = flags;

	return mrenclave_update(ctx, &mreadd);
}
Quoted 3 lines

If MRENCLAVE was updated after the overwrite, this would not work.

The least confusing semantics would be to require RW, no more or less.

OK, it is how Serge said.

This can we verified from the SDM easily (SCRATCH_SECINFO gets zeros is extended after that).

And also from my signing tool :-)

for (offset = 0; offset < sb.st_size; offset += 0x1000) {
	if (!offset)
		flags = SGX_SECINFO_TCS;
	else
		flags = SGX_SECINFO_REG | SGX_SECINFO_R |
			SGX_SECINFO_W | SGX_SECINFO_X;

OK, so this looks like that my patch does exactly the right thing, right?

/Jarkko

#23 lore raw

Re: [PATCH 4/5] x86/sgx: Validate TCS permssions in sgx_validate_secinfo()

Quoted 67 lines · Jarkko Sakkinen
Quoted 48 lines · Jarkko Sakkinen
Quoted 16 lines · Sean Christopherson
Quoted 15 lines · Jarkko Sakkinen
Quoted 3 lines · Sean Christopherson

Why are we validating the TCS protection bits? Hardware ignores them, so why do we care? sgx_ioc_enclave_add_page() sets the internal protection bits so there's no danger of putting the wrong thing in the page tables.

I think that in this commit I got it wrong but I think this is awkward:

	/*
	 * TCS pages must always RW set for CPU access while the SECINFO
	 * permissions are *always* zero - the CPU ignores the user provided
	 * values and silently overwrites with zero permissions.
	 */
	if ((secinfo.flags & SGX_SECINFO_PAGE_TYPE_MASK) == SGX_SECINFO_TCS)
		prot |= PROT_READ | PROT_WRITE;
Quoted 8 lines
Quoted 2 lines

In my opinion the right thing to do would be check that SECINFO has *at minimum* RW and return -EINVAL if not.

Based on Serge's comment, hardware updates MRENCLAVE with SECINFO *after* it overwrites the flags for TCS pages. I.e. requiring RW for the TCS would result in every enclave failing EINIT due to an invalid measurement. It'd be fairly easy to verify this if we want to triple check that that is indeed hardware behavior.

This is from the signing tool that I wrote back in 2016 used in the selftest:

struct mreadd {
	uint64_t tag;
	uint64_t offset;
	uint64_t flags; /* SECINFO flags */
	uint8_t reserved[40];
} __attribute__((__packed__));

static bool mrenclave_eadd(EVP_MD_CTX *ctx, uint64_t offset, uint64_t flags)

{
	struct mreadd mreadd;

	memset(&mreadd, 0, sizeof(mreadd));
	mreadd.tag = MREADD;
	mreadd.offset = offset;
	mreadd.flags = flags;

	return mrenclave_update(ctx, &mreadd);
}
Quoted 3 lines

If MRENCLAVE was updated after the overwrite, this would not work.

The least confusing semantics would be to require RW, no more or less.

OK, it is how Serge said.

This can we verified from the SDM easily (SCRATCH_SECINFO gets zeros is extended after that).

And also from my signing tool :-)

for (offset = 0; offset < sb.st_size; offset += 0x1000) {
	if (!offset)
		flags = SGX_SECINFO_TCS;
	else
		flags = SGX_SECINFO_REG | SGX_SECINFO_R |
			SGX_SECINFO_W | SGX_SECINFO_X;
Quoted 2 lines

OK, so this looks like that my patch does exactly the right thing, right?

That's my understanding as well. Definitely worthy of a comment explaining all of the above.

#24 lore raw

Re: [PATCH 4/5] x86/sgx: Validate TCS permssions in sgx_validate_secinfo()

Quoted 12 lines · Sean Christopherson
Quoted 12 lines

This can we verified from the SDM easily (SCRATCH_SECINFO gets zeros is extended after that).

And also from my signing tool :-)

for (offset = 0; offset < sb.st_size; offset += 0x1000) {
	if (!offset)
		flags = SGX_SECINFO_TCS;
	else
		flags = SGX_SECINFO_REG | SGX_SECINFO_R |
			SGX_SECINFO_W | SGX_SECINFO_X;
Quoted 5 lines
Quoted 2 lines

OK, so this looks like that my patch does exactly the right thing, right?

That's my understanding as well. Definitely worthy of a comment explaining all of the above.

Now that I looked at my own code I even remember going through this same thought process three years ago when I wrote that :-) Oh well.

So should I apply my zero check patch?

/Jarkko

#25 lore raw

Re: [PATCH 4/5] x86/sgx: Validate TCS permssions in sgx_validate_secinfo()

In reply to Jarkko Sakkinen
Quoted 15 lines · Jarkko Sakkinen
Quoted 3 lines · Sean Christopherson

Why are we validating the TCS protection bits? Hardware ignores them, so why do we care? sgx_ioc_enclave_add_page() sets the internal protection bits so there's no danger of putting the wrong thing in the page tables.

I think that in this commit I got it wrong but I think this is awkward:

	/*
	 * TCS pages must always RW set for CPU access while the SECINFO
	 * permissions are *always* zero - the CPU ignores the user provided
	 * values and silently overwrites with zero permissions.
	 */
	if ((secinfo.flags & SGX_SECINFO_PAGE_TYPE_MASK) == SGX_SECINFO_TCS)
		prot |= PROT_READ | PROT_WRITE;
Quoted 4 lines

In my opinion the right thing to do would be check that SECINFO has *at minimum* RW and return -EINVAL if not.

I don't like the SGX silently adjusting permissions like this.

For me any sane solution goes where we don't have that kind of tweaking probably my "minimum RW" is more sane than my earlier proposal.

/Jarkko

#26 lore raw

PATCH 5/5 x86/sgx: Rename vm_prot_bits as max_vm_flags

In reply to Jarkko Sakkinen

vm_prot_bits is very bad and misleading name for the field in struct sgx_encl_page. What the field contains exactly is not @prot of mprotect() but the *maximum* VM flags for the VMA that contains the given enclave page.

Thus, the only viable name for the field is max_vm_flags. In functions that also pass VM flags the parameter name is renamed from vm_prot_bits to vm_flags.

To summarize, this commit makes two improvements to clarify the permission handling:

1. Changes the name to match better the contents.
2. Uses naming to differentiate the field inside the struct and the parameter passed to functions.

Cc Sean Christopherson <[email protected]>
Cc Shay Katz-zamir <[email protected]>
Cc Serge Ayoun <[email protected]>
---
 arch/x86/kernel/cpu/sgx/driver/ioctl.c |  2 +-
 arch/x86/kernel/cpu/sgx/encl.c         | 14 +++++++-------
 arch/x86/kernel/cpu/sgx/encl.h         |  4 ++--
 3 files changed, 10 insertions(+), 10 deletions(-)
arch/x86/kernel/cpu/sgx/driver/ioctl.c +1 -1
index 2415dcb20a6e..b61e06daad6e 100644
@@ -261,7 +261,7 @@ static struct sgx_encl_page *sgx_encl_page_alloc(struct sgx_encl *encl,
 	encl_page->encl = encl;
 
 	/* Calculate maximum of the VM flags for the page. */
-	encl_page->vm_prot_bits = calc_vm_prot_bits(prot, 0);
+	encl_page->max_vm_flags = calc_vm_prot_bits(prot, 0);
 
 	ret = radix_tree_insert(&encl->page_tree, PFN_DOWN(encl_page->desc),
 				encl_page);
arch/x86/kernel/cpu/sgx/encl.c +7 -7
index a20d498e9dcd..890eacb45a80 100644
@@ -292,25 +292,25 @@ static unsigned int sgx_vma_fault(struct vm_fault *vmf)
  * @encl:		an enclave
  * @start:		lower bound of the address range, inclusive
  * @end:		upper bound of the address range, exclusive
- * @vm_prot_bits:	requested protections of the address range
+ * @vm_flags:		requested VM flags for the address range
  *
  * Iterate through the enclave pages contained within [@start, @end) to verify
- * the permissions requested by @vm_prot_bits do not exceed that of any enclave
- * page to be mapped.  Page addresses that do not have an associated enclave
- * page are interpreted to zero permissions.
+ * the the VM flags do not exceed that of any enclave page to be mapped. Page
+ * addresses that do not have an associated enclave page are interpreted to zero
+ * permissions.
  *
  * Return:
  *   0 on success,
  *   -EACCES if VMA permissions exceed enclave page permissions
  */
 int sgx_encl_may_map(struct sgx_encl *encl, unsigned long start,
-		     unsigned long end, unsigned long vm_prot_bits)
+		     unsigned long end, unsigned long vm_flags)
 {
 	unsigned long idx, idx_start, idx_end;
 	struct sgx_encl_page *page;
 
 	/* PROT_NONE always succeeds. */
-	if (!vm_prot_bits)
+	if (!vm_flags)
 		return 0;
 
 	idx_start = PFN_DOWN(start);
@@ -321,7 +321,7 @@ int sgx_encl_may_map(struct sgx_encl *encl, unsigned long start,
 		page = radix_tree_lookup(&encl->page_tree, idx);
 		mutex_unlock(&encl->lock);
 
-		if (!page || (~page->vm_prot_bits & vm_prot_bits))
+		if (!page || (~page->max_vm_flags & vm_flags))
 			return -EACCES;
 	}
 
arch/x86/kernel/cpu/sgx/encl.h +2 -2
index d3a1687ed84c..0e28b784a8c5 100644
@@ -44,7 +44,7 @@ enum sgx_encl_page_desc {
 
 struct sgx_encl_page {
 	unsigned long desc;
-	unsigned long vm_prot_bits;
+	unsigned long max_vm_flags;
 	struct sgx_epc_page *epc_page;
 	struct sgx_va_page *va_page;
 	struct sgx_encl *encl;
@@ -134,6 +134,6 @@ void sgx_free_va_slot(struct sgx_va_page *va_page, unsigned int offset);
 bool sgx_va_page_full(struct sgx_va_page *va_page);
 
 int sgx_encl_may_map(struct sgx_encl *encl, unsigned long start,
-		     unsigned long end, unsigned long vm_prot_bits);
+		     unsigned long end, unsigned long vm_flags);
 
 #endif /* _X86_ENCL_H */
Signature · 1 line
2.20.1
#27 lore raw

Re: [PATCH 5/5] x86/sgx: Rename vm_prot_bits as max_vm_flags

Quoted 8 lines · Jarkko Sakkinen

vm_prot_bits is very bad and misleading name for the field in struct sgx_encl_page. What the field contains exactly is not @prot of mprotect() but the *maximum* VM flags for the VMA that contains the given enclave page.

Thus, the only viable name for the field is max_vm_flags. In functions that also pass VM flags the parameter name is renamed from vm_prot_bits to vm_flags.

Why not max_vm_prot_bits? 'vm_flags' implies the field contains all manner of VM_FLAGS. The 'vm_prot_bits' name was derived from calc_vm_prot_bits() to provide this differentiation, especially since there's also a calc_vm_flag_bits(),

Quoted 46 lines

To summarize, this commit makes two improvements to clarify the permission handling:

1. Changes the name to match better the contents.
2. Uses naming to differentiate the field inside the struct and the parameter passed to functions.

Cc Sean Christopherson <[email protected]>
Cc Shay Katz-zamir <[email protected]>
Cc Serge Ayoun <[email protected]>
---
 arch/x86/kernel/cpu/sgx/driver/ioctl.c |  2 +-
 arch/x86/kernel/cpu/sgx/encl.c         | 14 +++++++-------
 arch/x86/kernel/cpu/sgx/encl.h         |  4 ++--
 3 files changed, 10 insertions(+), 10 deletions(-)
arch/x86/kernel/cpu/sgx/driver/ioctl.c +1 -1
index 2415dcb20a6e..b61e06daad6e 100644
@@ -261,7 +261,7 @@ static struct sgx_encl_page *sgx_encl_page_alloc(struct sgx_encl *encl,
 	encl_page->encl = encl;
 
 	/* Calculate maximum of the VM flags for the page. */
-	encl_page->vm_prot_bits = calc_vm_prot_bits(prot, 0);
+	encl_page->max_vm_flags = calc_vm_prot_bits(prot, 0);
 
 	ret = radix_tree_insert(&encl->page_tree, PFN_DOWN(encl_page->desc),
 				encl_page);
arch/x86/kernel/cpu/sgx/encl.c +2 -4
index a20d498e9dcd..890eacb45a80 100644
@@ -292,25 +292,25 @@ static unsigned int sgx_vma_fault(struct vm_fault *vmf)
  * @encl:		an enclave
  * @start:		lower bound of the address range, inclusive
  * @end:		upper bound of the address range, exclusive
- * @vm_prot_bits:	requested protections of the address range
+ * @vm_flags:		requested VM flags for the address range
  *
  * Iterate through the enclave pages contained within [@start, @end) to verify
- * the permissions requested by @vm_prot_bits do not exceed that of any enclave
- * page to be mapped.  Page addresses that do not have an associated enclave
- * page are interpreted to zero permissions.
+ * the the VM flags do not exceed that of any enclave page to be mapped. Page

arch/x86/kernel/cpu/sgx/encl.c static unsigned int sgx_vma_fault(struct vm_fault *vmf) · line 307

Quoted 10 lines
arch/x86/kernel/cpu/sgx/encl.c +3 -1
+ * addresses that do not have an associated enclave page are interpreted to zero
+ * permissions.
  *
  * Return:
  *   0 on success,
  *   -EACCES if VMA permissions exceed enclave page permissions
  */
 int sgx_encl_may_map(struct sgx_encl *encl, unsigned long start,
-		     unsigned long end, unsigned long vm_prot_bits)
+		     unsigned long end, unsigned long vm_flags)

If 'vm_prot_bits' is distasteful, 'prot' would be preferrable as its used throughout the kernel for variable that hold only the prot bits.

Quoted 43 lines
arch/x86/kernel/cpu/sgx/encl.c +2 -2
 {
 	unsigned long idx, idx_start, idx_end;
 	struct sgx_encl_page *page;
 
 	/* PROT_NONE always succeeds. */
-	if (!vm_prot_bits)
+	if (!vm_flags)
 		return 0;
 
 	idx_start = PFN_DOWN(start);
@@ -321,7 +321,7 @@ int sgx_encl_may_map(struct sgx_encl *encl, unsigned long start,
 		page = radix_tree_lookup(&encl->page_tree, idx);
 		mutex_unlock(&encl->lock);
 
-		if (!page || (~page->vm_prot_bits & vm_prot_bits))
+		if (!page || (~page->max_vm_flags & vm_flags))
 			return -EACCES;
 	}
 
arch/x86/kernel/cpu/sgx/encl.h +2 -2
index d3a1687ed84c..0e28b784a8c5 100644
@@ -44,7 +44,7 @@ enum sgx_encl_page_desc {
 
 struct sgx_encl_page {
 	unsigned long desc;
-	unsigned long vm_prot_bits;
+	unsigned long max_vm_flags;
 	struct sgx_epc_page *epc_page;
 	struct sgx_va_page *va_page;
 	struct sgx_encl *encl;
@@ -134,6 +134,6 @@ void sgx_free_va_slot(struct sgx_va_page *va_page, unsigned int offset);
 bool sgx_va_page_full(struct sgx_va_page *va_page);
 
 int sgx_encl_may_map(struct sgx_encl *encl, unsigned long start,
-		     unsigned long end, unsigned long vm_prot_bits);
+		     unsigned long end, unsigned long vm_flags);
 
 #endif /* _X86_ENCL_H */
Signature · 1 line
2.20.1
#28 lore raw

Re: [PATCH 5/5] x86/sgx: Rename vm_prot_bits as max_vm_flags

Quoted 6 lines · Sean Christopherson
Quoted 5 lines · Jarkko Sakkinen

vm_prot_bits is very bad and misleading name for the field in struct sgx_encl_page. What the field contains exactly is not @prot of mprotect() but the *maximum* VM flags for the VMA that contains the given enclave page.

Quoted 8 lines
Quoted 3 lines

Thus, the only viable name for the field is max_vm_flags. In functions that also pass VM flags the parameter name is renamed from vm_prot_bits to vm_flags.

Why not max_vm_prot_bits? 'vm_flags' implies the field contains all manner of VM_FLAGS. The 'vm_prot_bits' name was derived from calc_vm_prot_bits() to provide this differentiation, especially since there's also a calc_vm_flag_bits(),

Squashed with vm_max_prot_bits.

/Jarkko