linux-sgx.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH for_v22] selftests/x86/sgx: Ensure SECS base (ELRANGE) is naturally aligned
@ 2019-08-10  0:30 Sean Christopherson
  2019-08-10  0:39 ` Jethro Beekman
  2019-08-10 11:22 ` Jarkko Sakkinen
  0 siblings, 2 replies; 5+ messages in thread
From: Sean Christopherson @ 2019-08-10  0:30 UTC (permalink / raw)
  To: Jarkko Sakkinen; +Cc: linux-sgx

Manually align ELRANGE now that the kernel doesn't automatically do so
for SGX mappings.  To guarantee mmap() returns a region that can be
naturally aligned, temporarily map 2x the enclave size and do fancy
arithmetic to naturally align the base.

Signed-off-by: Sean Christopherson <sean.j.christopherson@intel.com>
---
 tools/testing/selftests/x86/sgx/main.c | 19 ++++++++++++++-----
 1 file changed, 14 insertions(+), 5 deletions(-)

diff --git a/tools/testing/selftests/x86/sgx/main.c b/tools/testing/selftests/x86/sgx/main.c
index effcdb3380ad..5bbc60cf7f89 100644
--- a/tools/testing/selftests/x86/sgx/main.c
+++ b/tools/testing/selftests/x86/sgx/main.c
@@ -130,7 +130,8 @@ static bool encl_create(int dev_fd, unsigned long bin_size,
 			struct sgx_secs *secs)
 {
 	struct sgx_enclave_create ioc;
-	void *base;
+	uint64_t base;
+	void *basep;
 	int rc;
 
 	memset(secs, 0, sizeof(*secs));
@@ -141,19 +142,27 @@ static bool encl_create(int dev_fd, unsigned long bin_size,
 	for (secs->size = 4096; secs->size < bin_size; )
 		secs->size <<= 1;
 
-	base = mmap(NULL, secs->size, PROT_NONE, MAP_SHARED, dev_fd, 0);
-	if (base == MAP_FAILED) {
+	basep = mmap(NULL, secs->size * 2, PROT_NONE, MAP_SHARED, dev_fd, 0);
+	if (basep == MAP_FAILED) {
 		perror("mmap");
 		return false;
 	}
+	base = (uint64_t)basep;
 
-	secs->base = (uint64_t)base;
+	secs->base = base + secs->size - (base % secs->size);
+
+	if (secs->base != base)
+		munmap(basep, secs->base - base);
+
+	if (secs->base - secs->size != base)
+		munmap((void *)(secs->base + secs->size),
+		       base + secs->size - secs->base);
 
 	ioc.src = (unsigned long)secs;
 	rc = ioctl(dev_fd, SGX_IOC_ENCLAVE_CREATE, &ioc);
 	if (rc) {
 		fprintf(stderr, "ECREATE failed rc=%d, err=%d.\n", rc, errno);
-		munmap(base, secs->size);
+		munmap((void *)secs->base, secs->size);
 		return false;
 	}
 
-- 
2.22.0


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH for_v22] selftests/x86/sgx: Ensure SECS base (ELRANGE) is naturally aligned
  2019-08-10  0:30 [PATCH for_v22] selftests/x86/sgx: Ensure SECS base (ELRANGE) is naturally aligned Sean Christopherson
@ 2019-08-10  0:39 ` Jethro Beekman
  2019-08-10 11:23   ` Jarkko Sakkinen
  2019-08-15 12:26   ` Jarkko Sakkinen
  2019-08-10 11:22 ` Jarkko Sakkinen
  1 sibling, 2 replies; 5+ messages in thread
From: Jethro Beekman @ 2019-08-10  0:39 UTC (permalink / raw)
  To: Sean Christopherson, Jarkko Sakkinen; +Cc: linux-sgx

[-- Attachment #1: Type: text/plain, Size: 301 bytes --]

On 2019-08-09 17:30, Sean Christopherson wrote:
> Manually align ELRANGE now that the kernel doesn't automatically do so
> for SGX mappings.

What was the rationale for moving this out of the kernel? Now every user 
of this API needs to implement it manually.

--
Jethro Beekman | Fortanix


[-- Attachment #2: S/MIME Cryptographic Signature --]
[-- Type: application/pkcs7-signature, Size: 3990 bytes --]

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH for_v22] selftests/x86/sgx: Ensure SECS base (ELRANGE) is naturally aligned
  2019-08-10  0:30 [PATCH for_v22] selftests/x86/sgx: Ensure SECS base (ELRANGE) is naturally aligned Sean Christopherson
  2019-08-10  0:39 ` Jethro Beekman
@ 2019-08-10 11:22 ` Jarkko Sakkinen
  1 sibling, 0 replies; 5+ messages in thread
From: Jarkko Sakkinen @ 2019-08-10 11:22 UTC (permalink / raw)
  To: Sean Christopherson; +Cc: linux-sgx

On Fri, Aug 09, 2019 at 05:30:51PM -0700, Sean Christopherson wrote:
> Manually align ELRANGE now that the kernel doesn't automatically do so
> for SGX mappings.  To guarantee mmap() returns a region that can be
> naturally aligned, temporarily map 2x the enclave size and do fancy
> arithmetic to naturally align the base.
> 
> Signed-off-by: Sean Christopherson <sean.j.christopherson@intel.com>

Thank you.

I fine-tuned the calculations a bit:

area = mmap(NULL, secs->size * 2, PROT_NONE, MAP_SHARED, dev_fd, 0);
if (area == MAP_FAILED) {
	perror("mmap");
	return false;
}

secs->base = ((uint64_t)area + secs->size - 1) & ~(secs->size - 1);

munmap(area, secs->base - (uint64_t)area);
munmap((void *)(secs->base + secs->size),
       (uint64_t)area + secs->size - secs->base);

/Jarkko

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH for_v22] selftests/x86/sgx: Ensure SECS base (ELRANGE) is naturally aligned
  2019-08-10  0:39 ` Jethro Beekman
@ 2019-08-10 11:23   ` Jarkko Sakkinen
  2019-08-15 12:26   ` Jarkko Sakkinen
  1 sibling, 0 replies; 5+ messages in thread
From: Jarkko Sakkinen @ 2019-08-10 11:23 UTC (permalink / raw)
  To: Jethro Beekman; +Cc: Sean Christopherson, linux-sgx

On Sat, Aug 10, 2019 at 12:39:54AM +0000, Jethro Beekman wrote:
> On 2019-08-09 17:30, Sean Christopherson wrote:
> > Manually align ELRANGE now that the kernel doesn't automatically do so
> > for SGX mappings.
> 
> What was the rationale for moving this out of the kernel? Now every user of
> this API needs to implement it manually.

VMAs are viewports to the enclave, not the enclave. You can even fully
create and initialize an enclave without creating VMAs. Thus, binding
must be a loose one.

/Jarkko

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH for_v22] selftests/x86/sgx: Ensure SECS base (ELRANGE) is naturally aligned
  2019-08-10  0:39 ` Jethro Beekman
  2019-08-10 11:23   ` Jarkko Sakkinen
@ 2019-08-15 12:26   ` Jarkko Sakkinen
  1 sibling, 0 replies; 5+ messages in thread
From: Jarkko Sakkinen @ 2019-08-15 12:26 UTC (permalink / raw)
  To: Jethro Beekman; +Cc: Sean Christopherson, linux-sgx

On Sat, Aug 10, 2019 at 12:39:54AM +0000, Jethro Beekman wrote:
> On 2019-08-09 17:30, Sean Christopherson wrote:
> > Manually align ELRANGE now that the kernel doesn't automatically do so
> > for SGX mappings.
> 
> What was the rationale for moving this out of the kernel? Now every user of
> this API needs to implement it manually.

The binding is now loose between VMAs and enclave e.g. you can
even create and initialize enclave without even a single mmap()
call.

/Jarkko

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2019-08-15 12:26 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-08-10  0:30 [PATCH for_v22] selftests/x86/sgx: Ensure SECS base (ELRANGE) is naturally aligned Sean Christopherson
2019-08-10  0:39 ` Jethro Beekman
2019-08-10 11:23   ` Jarkko Sakkinen
2019-08-15 12:26   ` Jarkko Sakkinen
2019-08-10 11:22 ` Jarkko Sakkinen

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).