Skip to content

Commit

Permalink
Merge pull request #56 from aegis2501/master
Browse files Browse the repository at this point in the history
Gramine single-stepping example
  • Loading branch information
jovanbulck authored Mar 3, 2023
2 parents f4ea5b9 + ebddce2 commit ea0a73f
Show file tree
Hide file tree
Showing 11 changed files with 426 additions and 129 deletions.
7 changes: 6 additions & 1 deletion libsgxstep/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ ASM = $(shell ls *.S)
OBJECTS = $(SOURCES:.c=.o) $(ASM:.S=.o)
OUTPUT = libsgx-step.a

ifeq ($(GRAMINE), 1)
CFLAGS += -DSGX_SSAFRAMESIZE=16384
SOURCES += ./../sdk/gramine/aep.c
endif

all: $(OUTPUT)

$(OUTPUT): $(OBJECTS)
Expand All @@ -31,7 +36,7 @@ $(OUTPUT): $(OBJECTS)

%.o : %.c
echo "$(INDENT)[CC] " $<
$(CC) $(CPPFLAGS) $(CFLAGS) $(INCLUDE) -c $<
$(CC) $(CPPFLAGS) $(CFLAGS) $(INCLUDE) -c $< -o $@

%.o : %.S
echo "$(INDENT)[AS] " $<
Expand Down
2 changes: 1 addition & 1 deletion libsgxstep/apic.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ static inline int apic_write(uint32_t reg, uint32_t v)
if (!apic_base) apic_init();

addr = (volatile uint32_t *)(apic_base + reg);
asm volatile ("movl %1, %0\n\t"
__asm__ volatile ("movl %1, %0\n\t"
:"=m"(*addr):"r"(v):);

return 0;
Expand Down
6 changes: 3 additions & 3 deletions libsgxstep/enclave.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,10 @@ int edbgrdwr(void *adrs, void* res, int len, int write);

/* HACK: to avoid having to retrieve the SSA framesize from the untrusted
runtime (driver), we assume a standard/hard-coded SSA framesize of 1 page */
#define SGX_SSAFRAMESIZE 4096
//TODO determine this at runtime..
// SSA framesize for Gramine seems to be as follows
// #define SGX_SSAFRAMESIZE 16384
#ifndef SGX_SSAFRAMESIZE
#define SGX_SSAFRAMESIZE 4096
#endif

struct gprsgx_region {
uint64_t rax;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,27 @@
From 01802a7641e0090e3a4926239ca004d7de2e6a5c Mon Sep 17 00:00:00 2001
From: Jo Van Bulck <jo.vanbulck@cs.kuleuven.be>
Date: Wed, 12 Oct 2022 19:14:24 +0200
Subject: [PATCH 1/2] SGX-Step Gramine patches to reconfigure AEP/TCS.

---
pal/src/host/linux-sgx/host_entry.S | 52 +++++++++++++++++++++++--
pal/src/host/linux-sgx/host_exception.c | 13 +++++++
pal/src/host/linux-sgx/meson.build | 11 ++++++
3 files changed, 73 insertions(+), 3 deletions(-)

diff --git a/meson.build b/meson.build
index 5cdb1c5c..3ecc93d3 100644
--- a/meson.build
+++ b/meson.build
@@ -282,6 +282,10 @@ if sgx
sgx_dcap_quoteverify_dep = cc.find_library('sgx_dcap_quoteverify')
endif

+ # XXX taken from: https://stackoverflow.com/questions/34663124/link-static-library-to-shared-library-or-to-a-binary
+ libsgxstepdir = '<ENTER ABSOLUTE PATH OF LIBSGXSTEP HERE>'
+ libsgxstep_dep = cc.find_library('libsgx-step', dirs : libsgxstepdir)
+
vtune_dep = []
dl_dep = []

diff --git a/pal/src/host/linux-sgx/host_entry.S b/pal/src/host/linux-sgx/host_entry.S
index bb38ced4..013323d7 100644
index bb38ced4..f53ea26c 100644
--- a/pal/src/host/linux-sgx/host_entry.S
+++ b/pal/src/host/linux-sgx/host_entry.S
@@ -2,6 +2,18 @@
@@ -1,7 +1,18 @@
#include "sgx_arch.h"

#include "asm-offsets.h"
-
+/************************************************************************/
+/* XXX AEP hook for SGX-Step support */
+ .data
Expand All @@ -32,7 +37,7 @@ index bb38ced4..013323d7 100644
.extern tcs_base
.extern g_in_aex_profiling

@@ -38,9 +50,17 @@ sgx_ecall:
@@ -38,9 +49,17 @@ sgx_ecall:
movq %gs:PAL_HOST_TCB_TCS, %rbx

# RCX has to be the AEP (Asynchronous Exit Pointer)
Expand All @@ -53,7 +58,7 @@ index bb38ced4..013323d7 100644
ENCLU

# currently only ECALL_THREAD_RESET returns
@@ -178,3 +198,29 @@ sgx_raise:
@@ -178,3 +197,29 @@ sgx_raise:
# RSI - external event
jmp .Ldo_ecall
.cfi_endproc
Expand Down Expand Up @@ -84,10 +89,10 @@ index bb38ced4..013323d7 100644
+
+/************************************************************************/
diff --git a/pal/src/host/linux-sgx/host_exception.c b/pal/src/host/linux-sgx/host_exception.c
index f0aae5bd..7bdb092d 100644
index f0aae5bd..670ec638 100644
--- a/pal/src/host/linux-sgx/host_exception.c
+++ b/pal/src/host/linux-sgx/host_exception.c
@@ -85,9 +85,22 @@ static enum pal_event signal_to_pal_event(int sig) {
@@ -85,8 +85,21 @@ static enum pal_event signal_to_pal_event(int sig) {
}
}

Expand All @@ -96,7 +101,7 @@ index f0aae5bd..7bdb092d 100644
+
static bool interrupted_in_enclave(struct ucontext* uc) {
unsigned long rip = ucontext_get_ip(uc);

+
+ /************************************************************************/
+ if (g_aep_pointer)
+ {
Expand All @@ -106,34 +111,48 @@ index f0aae5bd..7bdb092d 100644
+ return (rip == (unsigned long) g_aep_pointer);
+ }
+ /************************************************************************/
+
/* in case of AEX, RIP can point to any instruction in the AEP/ERESUME trampoline code, i.e.,
* RIP can point to anywhere in [async_exit_pointer, async_exit_pointer_end) interval */
return rip >= (unsigned long)async_exit_pointer && rip < (unsigned long)async_exit_pointer_end;
diff --git a/pal/src/host/linux-sgx/meson.build b/pal/src/host/linux-sgx/meson.build
index 3e77bdcc..a1776fb8 100644
index 3e77bdcc..29f16a32 100644
--- a/pal/src/host/linux-sgx/meson.build
+++ b/pal/src/host/linux-sgx/meson.build
@@ -152,10 +152,19 @@ libpal_sgx_host = executable('loader',
@@ -4,12 +4,15 @@ gsgx_h = configure_file(
configuration: conf_sgx,
)

+sgxstepdir = '<ENTER ABSOLUTE PATH OF SGX-STEP PARENT FOLDER HERE>'
+
sgx_inc = [
includes_pal_common,
include_directories(
'.',
'../../../include/arch/@0@/linux'.format(host_machine.cpu_family()),
'../../../include/host/linux-common',
+ join_paths(sgxstepdir, 'sgx-step/')
),
]

@@ -152,6 +155,12 @@ libpal_sgx_host = executable('loader',
# host part of PAL uses stack protector with standard parameters (not the ones defined for
# PAL/LibOS)
'-fstack-protector-strong',
+
+ # XXX add libsgxstep include path
+ '-I../../../..',
+
+ # XXX FORTIFY_SOURCE conflicts with regular libc header includes in libsgxstep
+ '-D_FORTIFY_SOURCE=0',
+ # XXX libsgxstep needs Intel SDK headers (only for error reporting; can be hacked out if needed)
+ '-I/opt/intel/sgxsdk/include/'
+
],
link_args: [
'-Wl,-zrelro',
'-Wl,-znow',
+
@@ -164,6 +173,8 @@ libpal_sgx_host = executable('loader',
protobuf_dep,
vtune_dep,
dl_dep,
+ # XXX link in libsgxstep library
+ '-L../../../../libsgxstep',
+ '-lsgx-step',
+ libsgxstep_dep
],
pie: true,

--
2.34.1

install: true,

This file was deleted.

80 changes: 80 additions & 0 deletions sdk/gramine/0002-helloworld-Nemesis.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
diff --git a/CI-Examples/helloworld/helloworld.manifest.template b/CI-Examples/helloworld/helloworld.manifest.template
index 64c908e1..205860da 100644
--- a/CI-Examples/helloworld/helloworld.manifest.template
+++ b/CI-Examples/helloworld/helloworld.manifest.template
@@ -6,6 +6,9 @@ loader.log_level = "{{ log_level }}"

loader.env.LD_LIBRARY_PATH = "/lib"

+loader.insecure__use_cmdline_argv = true
+loader.insecure__disable_aslr = true
+
fs.mounts = [
{ path = "/lib", uri = "file:{{ gramine.runtimedir() }}" },
{ path = "/helloworld", uri = "file:helloworld" },
@@ -13,6 +16,8 @@ fs.mounts = [

sgx.debug = true
sgx.nonpie_binary = true
+sgx.preheat_enclave = true
+sgx.thread_num = 4

sgx.trusted_files = [
"file:{{ gramine.libos }}",
diff --git a/pal/src/host/linux-sgx/host_ecalls.c b/pal/src/host/linux-sgx/host_ecalls.c
index 9387266b..e83acc63 100644
--- a/pal/src/host/linux-sgx/host_ecalls.c
+++ b/pal/src/host/linux-sgx/host_ecalls.c
@@ -5,6 +5,14 @@
#include "host_internal.h"
#include "pal_ecall_types.h"
#include "pal_rpc_queue.h"
+#include "sdk/gramine/aep.h"
+
+#define SGX_STEP_ENABLE 1
+
+#define THREAD_END 4
+
+static int thread_ctr = 0;
+static spinlock_t g_step_lock = INIT_SPINLOCK_UNLOCKED;

int ecall_enclave_start(char* libpal_uri, char* args, size_t args_size, char* env,
size_t env_size, int parent_stream_fd, sgx_target_info_t* qe_targetinfo,
@@ -32,13 +40,37 @@ int ecall_enclave_start(char* libpal_uri, char* args, size_t args_size, char* en
ms.ms_topo_info = topo_info;
ms.ms_dns_host_conf = dns_conf;
ms.rpc_queue = g_rpc_queue;
+
+#if SGX_STEP_ENABLE
+ /* NOTE: set sgx.preheat_enclave = true in manifest to prefault pages (occurs in pal_main.c) */
+ /* Configure and check attacker untrusted runtime environment. */
+ attacker_config_runtime();
+#endif
+
return sgx_ecall(ECALL_ENCLAVE_START, &ms);
}

int ecall_thread_start(void) {
+
+#if SGX_STEP_ENABLE
+ thread_ctr++;
+ if(thread_ctr == 4) {
+ spinlock_lock(&g_step_lock);
+ configure_mapping();
+ spinlock_unlock(&g_step_lock);
+ }
+#endif
+
return sgx_ecall(ECALL_THREAD_START, NULL);
}

int ecall_thread_reset(void) {
+
+#if SGX_STEP_ENABLE
+ if(thread_ctr == THREAD_END){
+ restore_timer();
+ }
+#endif
+
return sgx_ecall(ECALL_THREAD_RESET, NULL);
}
Loading

0 comments on commit ea0a73f

Please sign in to comment.