Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add shared memory checks #743

Open
wants to merge 15 commits into
base: oe_port
Choose a base branch
from
Prev Previous commit
Next Next commit
Fix host env import
Signed-off-by: Christoph M. Wintersteiger <cwinter@microsoft.com>
Christoph M. Wintersteiger committed Sep 15, 2020
commit bbeb7dee6c46355d7a92d2fe15ff5887d01117a9
33 changes: 15 additions & 18 deletions src/enclave/enclave_oe.c
Original file line number Diff line number Diff line change
@@ -108,7 +108,8 @@ static void _prepare_elf_stack()
size_t num_imported_env = 0;
const char** imported_env = NULL;

if (sgxlkl_enclave_state.shared_memory.env && cfg->num_host_import_env > 0)
if (sgxlkl_enclave_state.shared_memory.env &&
sgxlkl_enclave_state.shared_memory.envc && cfg->num_host_import_env > 0)
{
imported_env = oe_calloc_or_die(
cfg->num_host_import_env,
@@ -118,16 +119,12 @@ static void _prepare_elf_stack()
for (size_t i = 0; i < cfg->num_host_import_env; i++)
{
const char* name = cfg->host_import_env[i];
for (char* const* p = sgxlkl_enclave_state.shared_memory.env;
p && *p != NULL;
p++)
size_t n = oe_strlen(name);
for (size_t i = 0; i < sgxlkl_enclave_state.shared_memory.envc; i++)
{
size_t n = oe_strlen(name);
if (_strncmp(name, *p, n) == 0 && (*p)[n] == '=')
{
const char* str = *p;
imported_env[num_imported_env++] = str;
}
const char* henv_i = sgxlkl_enclave_state.shared_memory.env[i];
if (_strncmp(name, henv_i, n) == 0 && henv_i[n] == '=')
imported_env[num_imported_env++] = henv_i;
}
}
}
@@ -388,14 +385,12 @@ static void _copy_shared_memory(const sgxlkl_shared_memory_t* host)

/* Copy the host's environment variables to enclave memory */
char* const* henv = host->env;
if (henv)
size_t henvc = host->envc;
if (henv && henvc)
{
size_t henvc = 0;
while (henv[henvc] != 0)
henvc++;
CHECK_OUTSIDE(henv, sizeof(char*) * henvc);
char** tmp = oe_calloc_or_die(
henvc + 1,
henvc,
sizeof(char*),
"Could not allocate memory for host import environment variable\n");
for (size_t i = 0; i < henvc; i++)
@@ -406,8 +401,9 @@ static void _copy_shared_memory(const sgxlkl_shared_memory_t* host)
tmp[i] = oe_malloc(n);
memcpy(tmp[i], env_i, n);
}
tmp[henvc] = NULL;
enc->env = tmp;
enc->envc = henvc;
CHECK_INSIDE(enc->env, sizeof(char*) * enc->envc);
}

/* Commit to the temporary copy */
@@ -425,8 +421,9 @@ static void _free_shared_memory()
oe_free(shm->virtio_blk_dev_mem);
oe_free(shm->virtio_blk_dev_names);

for (size_t i = 0; shm->env[i] != 0; i++)
oe_free(shm->env[i]);
if (shm->env && shm->envc)
for (size_t i = 0; i < shm->envc; i++)
oe_free(shm->env[i]);
oe_free((char**)shm->env);
}

1 change: 1 addition & 0 deletions src/include/shared/shared_memory.h
Original file line number Diff line number Diff line change
@@ -29,6 +29,7 @@ typedef struct sgxlkl_shared_memory

/* Host environment variables for optional import */
char* const* env;
size_t envc;
} sgxlkl_shared_memory_t;

#endif /* SGXLKL_SHARED_MEMORY_H */
3 changes: 3 additions & 0 deletions src/main-oe/sgxlkl_run_oe.c
Original file line number Diff line number Diff line change
@@ -1884,6 +1884,9 @@ int main(int argc, char* argv[], char* envp[])

bool have_enclave_config_file = enclave_config_path != NULL;
set_clock_res(have_enclave_config_file);
sgxlkl_host_state.shared_memory.envc = 0;
for (char** env = envp; *env != 0; env++)
sgxlkl_host_state.shared_memory.envc++;
sgxlkl_host_state.shared_memory.env = envp;
set_tls(have_enclave_config_file);
register_hds(root_hd);
2 changes: 1 addition & 1 deletion tests/basic/eeid-config/Makefile
Original file line number Diff line number Diff line change
@@ -6,7 +6,7 @@ IMAGE_SIZE=5M

EXECUTION_TIMEOUT=60

SGXLKL_ENV=SGXLKL_VERBOSE=1 SGXLKL_KERNEL_VERBOSE=1
SGXLKL_ENV=SGXLKL_VERBOSE=1 SGXLKL_KERNEL_VERBOSE=1 HOSTNAME=EEIDHOST
SGXLKL_HW_PARAMS=--hw-debug
SGXLKL_SW_PARAMS=--sw-debug

11 changes: 11 additions & 0 deletions tests/basic/eeid-config/hello-eeid.c
Original file line number Diff line number Diff line change
@@ -31,5 +31,16 @@ int main(int argc, char** argv)
exit(1);
}

// Application environment variable
const char* abc = getenv("ABC");
if (strcmp(abc, "DEF") != 0)
exit(1);

// Environment variable imported from host
const char* hostname = getenv("HOSTNAME");
printf("HOSTNAME=%s\n", hostname);
if (!hostname || strcmp(hostname, "EEIDHOST") != 0)
exit(1);

return 0;
}