All of lore.kernel.org
 help / color / mirror / Atom feed
From: Jo Van Bulck <jo.vanbulck@cs.kuleuven.be>
To: jarkko@kernel.org, linux-sgx@vger.kernel.org,
	linux-kernel@vger.kernel.org
Cc: dave.hansen@linux.intel.com, Jo Van Bulck <jo.vanbulck@cs.kuleuven.be>
Subject: [PATCH 4/4] selftests/sgx: Fix compiler optimizations in test enclave
Date: Fri, 21 Jul 2023 00:16:23 +0200	[thread overview]
Message-ID: <20230720221623.9530-5-jo.vanbulck@cs.kuleuven.be> (raw)
In-Reply-To: <20230720221623.9530-1-jo.vanbulck@cs.kuleuven.be>

Relocate encl_op_array entries at runtime relative to the enclave base to
ensure correct function pointer when compiling the test enclave with -Os.

Declare the secinfo struct as volatile to prevent compiler optimizations
from passing an unaligned pointer to ENCLU.

Signed-off-by: Jo Van Bulck <jo.vanbulck@cs.kuleuven.be>
---
 tools/testing/selftests/sgx/test_encl.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)

diff --git a/tools/testing/selftests/sgx/test_encl.c b/tools/testing/selftests/sgx/test_encl.c
index ea24cdf9e..b580c37f3 100644
--- a/tools/testing/selftests/sgx/test_encl.c
+++ b/tools/testing/selftests/sgx/test_encl.c
@@ -105,7 +105,8 @@ static inline void assert_inside_enclave(uint64_t arg, size_t len)
 static void do_encl_emodpe(void *_op)
 {
 	struct encl_op_emodpe op;
-	struct sgx_secinfo secinfo __aligned(sizeof(struct sgx_secinfo)) = {0};
+	/* declare secinfo volatile to preserve alignment */
+	volatile struct __aligned(sizeof(struct sgx_secinfo)) sgx_secinfo secinfo = {0};
 
 	copy_inside_enclave(&op, _op, sizeof(op));
 	assert_inside_enclave(op.epc_addr, PAGE_SIZE);
@@ -122,8 +123,9 @@ static void do_encl_emodpe(void *_op)
 static void do_encl_eaccept(void *_op)
 {
 	struct encl_op_eaccept op;
-	struct sgx_secinfo secinfo __aligned(sizeof(struct sgx_secinfo)) = {0};
 	int rax;
+	/* declare secinfo volatile to preserve alignment */
+	volatile struct __aligned(sizeof(struct sgx_secinfo)) sgx_secinfo secinfo = {0};
 
 	copy_inside_enclave(&op, _op, sizeof(op));
 	assert_inside_enclave(op.epc_addr, PAGE_SIZE);
@@ -222,7 +224,7 @@ static void do_encl_op_nop(void *_op)
 
 void encl_body(void *rdi,  void *rsi)
 {
-	const void (*encl_op_array[ENCL_OP_MAX])(void *) = {
+	static const void (*encl_op_array[ENCL_OP_MAX])(void *) = {
 		do_encl_op_put_to_buf,
 		do_encl_op_get_from_buf,
 		do_encl_op_put_to_addr,
@@ -237,5 +239,5 @@ void encl_body(void *rdi,  void *rsi)
 	copy_inside_enclave(&op, rdi, sizeof(op));
 
 	if (op.type < ENCL_OP_MAX)
-		(*encl_op_array[op.type])(rdi);
+		(*(get_enclave_base() + encl_op_array[op.type]))(rdi);
 }
-- 
2.34.1


  parent reply	other threads:[~2023-07-20 22:17 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-07-20 22:16 [PATCH v2 0/4] selftests/sgx: Harden test enclave Jo Van Bulck
2023-07-20 22:16 ` [PATCH 1/4] selftests/sgx: Harden test enclave ABI Jo Van Bulck
2023-07-20 22:16 ` [PATCH 2/4] selftests/sgx: Store base address and size in test enclave Jo Van Bulck
2023-07-20 22:16 ` [PATCH 3/4] selftests/sgx: Harden test enclave API Jo Van Bulck
2023-07-20 22:16 ` Jo Van Bulck [this message]
2023-07-21  0:24 ` [PATCH v2 0/4] selftests/sgx: Harden test enclave Dave Hansen
2023-07-24 10:33   ` Jo Van Bulck
  -- strict thread matches above, loose matches on Subject: below --
2023-07-19 14:24 [PATCH " Jo Van Bulck
2023-07-19 14:25 ` [PATCH 4/4] selftests/sgx: Fix compiler optimizations in " Jo Van Bulck

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=20230720221623.9530-5-jo.vanbulck@cs.kuleuven.be \
    --to=jo.vanbulck@cs.kuleuven.be \
    --cc=dave.hansen@linux.intel.com \
    --cc=jarkko@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-sgx@vger.kernel.org \
    /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
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.