Skip to content

Commit

Permalink
libsgxstep: Fix bug in /proc/self/maps parsing for enclave discovery.
Browse files Browse the repository at this point in the history
Fixes #54
  • Loading branch information
jovanbulck committed Oct 1, 2022
1 parent c09a1b9 commit d53ab9b
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 6 deletions.
2 changes: 2 additions & 0 deletions libsgxstep/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
#ifndef SGX_STEP_CONFIG
#define SGX_STEP_CONFIG

#define DEBUG 0

#define PSTATE_PCT 100
#define SINGLE_STEP_ENABLE 1
#define USER_IDT_ENABLE 1
Expand Down
8 changes: 8 additions & 0 deletions libsgxstep/debug.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include "config.h"

#if !NO_SGX
#include <sgx_error.h>
Expand Down Expand Up @@ -55,6 +56,13 @@ extern sgx_status_t sgx_step_rv;
fflush(stdout); \
} while(0)

#if DEBUG
#define debug(msg, ...) info("DEBUG: " msg, ##__VA_ARGS__)
#else
#define debug(msg, ...)
#endif


#if LIBSGXSTEP_SILENT
#define libsgxstep_info(msg, ...)
#else
Expand Down
13 changes: 7 additions & 6 deletions libsgxstep/enclave.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ void register_enclave_info(void)

/* Parse /proc/self/maps to detect any enclaves mapped in the address space.
* Expected format: "start-end perms offset dev inode optional_pathname"
* For documentation of /proc/pid/maps, see `man 5 proc`.
*
* NOTES: - victim.tcs is set by the patched untrusted runtime on first
* enclave entry (e.g., as part of sgx_create_enclave)
Expand All @@ -67,25 +68,25 @@ void register_enclave_info(void)
* - only supports a single enclave that is expected to be
* contiguously mapped in the address space
*/
#if 0
info("cat /proc/self/maps");
#if DEBUG
debug("cat /proc/self/maps");
char command[256];
sprintf(command, "cat /proc/%d/maps", getpid());
system(command);
debug("------");
#endif
ASSERT((fd_self_maps = fopen("/proc/self/maps", "r")) >= 0);
while (fscanf(fd_self_maps, "%lx-%lx %*s %*x %*d:%*d %*[0-9 ]%m[^\n]",
while (fscanf(fd_self_maps, "%lx-%lx %*s %*x %*x:%*x %*[0-9 ]%m[^\n]",
&start, &end, &pathname) > 0)
{
//info("%p - %p %s", (void*) start, (void*) end, pathname);
debug("%p - %p %s", (void*) start, (void*) end, pathname);
is_isgx = (pathname != NULL) && strstr(pathname, "/dev/isgx") != NULL;
is_kern = (pathname != NULL) && strstr(pathname, "/dev/sgx_enclave") != NULL;
is_enclave = is_isgx || is_kern;

if (is_enclave && !prev_is_enclave && !victim.base)
{
//info("Found %s enclave at %p in /proc/self/maps", pathname, (void*) start);
//ASSERT( !victim.base && "multiple enclaves found in /proc/self/maps");
debug("Found %s enclave at %p in /proc/self/maps", pathname, (void*) start);
victim.base = (uint64_t) start;
victim.drv = is_isgx ? "/dev/isgx" : "/dev/sgx_enclave";
}
Expand Down

0 comments on commit d53ab9b

Please sign in to comment.