Skip to content

Commit

Permalink
Fix host env import
Browse files Browse the repository at this point in the history
Signed-off-by: Christoph M. Wintersteiger <[email protected]>
  • Loading branch information
wintersteiger committed Aug 7, 2020
1 parent c1898ca commit 809808a
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 19 deletions.
33 changes: 15 additions & 18 deletions src/enclave/enclave_oe.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,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,
Expand All @@ -67,16 +68,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;
}
}
}
Expand Down Expand Up @@ -335,14 +332,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++)
Expand All @@ -353,8 +348,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 */
Expand All @@ -372,8 +368,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);
}

Expand Down
1 change: 1 addition & 0 deletions src/include/shared/shared_memory.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Up @@ -1814,6 +1814,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);
Expand Down
2 changes: 1 addition & 1 deletion tests/basic/eeid-config/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
11 changes: 11 additions & 0 deletions tests/basic/eeid-config/hello-eeid.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

0 comments on commit 809808a

Please sign in to comment.