Linux-Sgx Archive on lore.kernel.org
 help / color / Atom feed
From: Jarkko Sakkinen <jarkko.sakkinen@linux.intel.com>
To: linux-sgx@vger.kernel.org
Cc: Jarkko Sakkinen <jarkko.sakkinen@linux.intel.com>,
	Sean Christopherson <sean.j.christopherson@intel.com>
Subject: [PATCH 5/5] selftests/sgx: Load encl.elf directly in the test program
Date: Mon, 23 Mar 2020 05:46:34 +0200
Message-ID: <20200323034634.4157-5-jarkko.sakkinen@linux.intel.com> (raw)
In-Reply-To: <20200323034634.4157-1-jarkko.sakkinen@linux.intel.com>

To make test program more realistic and robust, load the test enclave
directly from encl.elf.

Cc: Sean Christopherson <sean.j.christopherson@intel.com>
Signed-off-by: Jarkko Sakkinen <jarkko.sakkinen@linux.intel.com>
---
 tools/testing/selftests/sgx/Makefile  | 11 +++---
 tools/testing/selftests/sgx/defines.h |  1 +
 tools/testing/selftests/sgx/main.c    | 48 ++++++++++++++++++++-------
 3 files changed, 41 insertions(+), 19 deletions(-)

diff --git a/tools/testing/selftests/sgx/Makefile b/tools/testing/selftests/sgx/Makefile
index d9c3b3a1983b..48a2cda6c34d 100644
--- a/tools/testing/selftests/sgx/Makefile
+++ b/tools/testing/selftests/sgx/Makefile
@@ -16,7 +16,7 @@ HOST_CFLAGS := -Wall -Werror -g $(INCLUDES) -fPIC -z noexecstack
 ENCL_CFLAGS := -Wall -Werror -static -nostdlib -nostartfiles -fPIC \
 	       -fno-stack-protector -mrdrnd $(INCLUDES)
 
-TEST_CUSTOM_PROGS := $(OUTPUT)/test_sgx $(OUTPUT)/encl.bin
+TEST_CUSTOM_PROGS := $(OUTPUT)/test_sgx $(OUTPUT)/encl.elf
 
 ifeq ($(CAN_BUILD_X86_64), 1)
 all: $(TEST_CUSTOM_PROGS)
@@ -34,16 +34,13 @@ $(OUTPUT)/sign.o: sign.c
 $(OUTPUT)/call.o: call.S
 	$(CC) $(HOST_CFLAGS) -c $< -o $@
 
-$(OUTPUT)/encl.bin: $(OUTPUT)/encl.elf
-	$(OBJCOPY) -O binary $< $@
-
 $(OUTPUT)/encl.elf: encl.lds encl.c encl_bootstrap.S
 	$(CC) $(ENCL_CFLAGS) -T $^ -o $@
 
 EXTRA_CLEAN := \
-	$(OUTPUT)/encl.bin \
 	$(OUTPUT)/encl.elf \
-	$(OUTPUT)/sgx_call.o \
+	$(OUTPUT)/call.o \
+	$(OUTPUT)/main.o \
+	$(OUTPUT)/sign.o \
 	$(OUTPUT)/test_sgx \
 	$(OUTPUT)/test_sgx.o \
-
diff --git a/tools/testing/selftests/sgx/defines.h b/tools/testing/selftests/sgx/defines.h
index 8f4d17cf8cee..1802cace7527 100644
--- a/tools/testing/selftests/sgx/defines.h
+++ b/tools/testing/selftests/sgx/defines.h
@@ -9,6 +9,7 @@
 #include <stdint.h>
 
 #define PAGE_SIZE 4096
+#define PAGE_MASK (~(PAGE_SIZE - 1))
 
 #define __aligned(x) __attribute__((__aligned__(x)))
 #define __packed __attribute__((packed))
diff --git a/tools/testing/selftests/sgx/main.c b/tools/testing/selftests/sgx/main.c
index a78e64159313..a0a37d85714b 100644
--- a/tools/testing/selftests/sgx/main.c
+++ b/tools/testing/selftests/sgx/main.c
@@ -223,11 +223,6 @@ bool get_file_size(const char *path, off_t *bin_size)
 		return false;
 	}
 
-	if (!sb.st_size || sb.st_size & 0xfff) {
-		fprintf(stderr, "Invalid blob size %lu\n", sb.st_size);
-		return false;
-	}
-
 	*bin_size = sb.st_size;
 	return true;
 }
@@ -291,12 +286,17 @@ int main(int argc, char *argv[], char *envp[])
 	struct sgx_enclave_exception exception;
 	struct sgx_sigstruct sigstruct;
 	struct sgx_enclave_init ioc;
+	Elf64_Phdr *phdr, *phdr_tbl;
+	unsigned long start_offset;
 	struct vdso_symtab symtab;
+	unsigned long encl_size;
 	Elf64_Sym *eenter_sym;
 	uint64_t result = 0;
 	struct context ctx;
+	Elf64_Ehdr *ehdr;
 	void *addr;
 	int ret;
+	int i;
 
 	context_init(&ctx);
 
@@ -306,25 +306,49 @@ int main(int argc, char *argv[], char *envp[])
 		goto err;
 	}
 
-	if (!encl_data_map("encl.bin", &ctx.bin, &ctx.bin_size))
+	if (!encl_data_map("encl.elf", &ctx.bin, &ctx.bin_size))
 		goto err;
 
-	if (!encl_create(ctx.encl_fd, ctx.bin_size, &ctx.secs))
+	ehdr = ctx.bin;
+	phdr_tbl = ctx.bin + ehdr->e_phoff;
+	start_offset = 0;
+	encl_size = 0;
+
+	for (i = 0; i < ehdr->e_phnum; i++) {
+		unsigned long offset, size;
+
+		phdr = &phdr_tbl[i];
+		if (phdr->p_type != PT_LOAD)
+			continue;
+
+		offset = phdr->p_offset & PAGE_MASK;
+		if (!start_offset)
+			start_offset = offset;
+
+		size = (offset - start_offset + phdr->p_filesz +
+			PAGE_SIZE - 1) & PAGE_MASK;
+		if (size > encl_size)
+			encl_size = size;
+	}
+
+	if (!encl_create(ctx.encl_fd, encl_size, &ctx.secs))
 		goto err;
 
 	/* TCS */
-	if (!encl_build_segment(ctx.encl_fd, &ctx.secs, ctx.bin, 0, PAGE_SIZE,
-				SGX_SECINFO_TCS, PROT_READ | PROT_WRITE))
+	if (!encl_build_segment(ctx.encl_fd, &ctx.secs, ctx.bin + start_offset,
+				0, PAGE_SIZE, SGX_SECINFO_TCS,
+				PROT_READ | PROT_WRITE))
 		goto err;
 
-	if (!encl_build_segment(ctx.encl_fd, &ctx.secs, ctx.bin, PAGE_SIZE,
-				ctx.bin_size - PAGE_SIZE,
+	if (!encl_build_segment(ctx.encl_fd, &ctx.secs, ctx.bin + start_offset,
+				PAGE_SIZE, encl_size - PAGE_SIZE,
 				SGX_SECINFO_REG | SGX_SECINFO_R |
 				SGX_SECINFO_W | SGX_SECINFO_X,
 				PROT_READ | PROT_WRITE | PROT_EXEC))
 		goto err;
 
-	if (!encl_create_sigstruct(ctx.bin, ctx.bin_size, &sigstruct))
+	if (!encl_create_sigstruct(ctx.bin + start_offset, encl_size,
+				   &sigstruct))
 		goto err;
 
 	ioc.sigstruct = (uint64_t)&sigstruct;
-- 
2.25.1


  parent reply index

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-03-23  3:46 [PATCH 1/5] selftests/sgx: Add PHDRS to encl.lds Jarkko Sakkinen
2020-03-23  3:46 ` [PATCH 2/5] selftests/sgx: Manage encl_fd in the main function Jarkko Sakkinen
2020-03-23  3:46 ` [PATCH 3/5] selftests/sgx: Move EINIT out of encl_build() Jarkko Sakkinen
2020-03-23  3:46 ` [PATCH 4/5] selftest/sgx: Replace encl_build() with encl_build_segment() Jarkko Sakkinen
2020-03-23  3:46 ` Jarkko Sakkinen [this message]
2020-03-23  3:52 ` [PATCH 1/5] selftests/sgx: Add PHDRS to encl.lds Jarkko Sakkinen

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20200323034634.4157-5-jarkko.sakkinen@linux.intel.com \
    --to=jarkko.sakkinen@linux.intel.com \
    --cc=linux-sgx@vger.kernel.org \
    --cc=sean.j.christopherson@intel.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

Linux-Sgx Archive on lore.kernel.org

Archives are clonable:
	git clone --mirror https://lore.kernel.org/linux-sgx/0 linux-sgx/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 linux-sgx linux-sgx/ https://lore.kernel.org/linux-sgx \
		linux-sgx@vger.kernel.org
	public-inbox-index linux-sgx

Example config snippet for mirrors

Newsgroup available over NNTP:
	nntp://nntp.lore.kernel.org/org.kernel.vger.linux-sgx


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git