From 0182c097dd7152f316be3e7560b05ae4e9313f8a Mon Sep 17 00:00:00 2001 From: volcano Date: Sun, 24 Apr 2022 15:45:25 +0800 Subject: [PATCH] [hyper mode] Support ms buffer for deep copy hostfile --- src/pal/include/occlum_pal_api.h | 2 -- src/pal/src/pal_api.c | 26 ++++--------------- src/pal/src/pal_load_file.c | 43 +++++++++++++++++++++++++++++++- src/pal/src/pal_load_file.h | 8 +++++- 4 files changed, 54 insertions(+), 25 deletions(-) diff --git a/src/pal/include/occlum_pal_api.h b/src/pal/include/occlum_pal_api.h index 0b69a64f..967a0213 100644 --- a/src/pal/include/occlum_pal_api.h +++ b/src/pal/include/occlum_pal_api.h @@ -167,8 +167,6 @@ int occlum_pal_kill(int pid, int sig); */ int occlum_pal_destroy(void); -void free_host_file_buffer_t(struct host_file_buffer_t file_buffer); - #ifdef __cplusplus } #endif diff --git a/src/pal/src/pal_api.c b/src/pal/src/pal_api.c index 02eca647..b1c6dc2d 100644 --- a/src/pal/src/pal_api.c +++ b/src/pal/src/pal_api.c @@ -112,9 +112,9 @@ int occlum_pal_init(const struct occlum_pal_attr *attr) { load_file_t hosts_ptr = {0, NULL}; load_file_t resolv_conf_ptr = {0, NULL}; - pal_load_file("/etc/hostname", &hostname_ptr); - pal_load_file("/etc/hosts", &hosts_ptr); - pal_load_file("/etc/resolv.conf", &resolv_conf_ptr); + pal_load_file(eid, "/etc/hostname", &hostname_ptr); + pal_load_file(eid, "/etc/hosts", &hosts_ptr); + pal_load_file(eid, "/etc/resolv.conf", &resolv_conf_ptr); struct host_file_buffer_t file_buffer = { .hostname_buf = hostname_ptr.buffer, @@ -125,15 +125,10 @@ int occlum_pal_init(const struct occlum_pal_attr *attr) { .resolv_conf_buf_size = resolv_conf_ptr.size, }; - const struct host_file_buffer_t *file_buffer_ptr = &file_buffer; - sgx_status_t ecall_status = occlum_ecall_init(eid, &ecall_ret, attr->log_level, - resolved_path, file_buffer_ptr); + resolved_path, &file_buffer); - free_host_file_buffer_t(file_buffer); - hostname_ptr.buffer = NULL; - hosts_ptr.buffer = NULL; - resolv_conf_ptr.buffer = NULL; + free_host_file_buffer(eid, &file_buffer); if (ecall_status != SGX_SUCCESS) { const char *sgx_err = pal_get_sgx_error_msg(ecall_status); @@ -295,17 +290,6 @@ int occlum_pal_destroy(void) { return ret; } -void free_host_file_buffer_t(struct host_file_buffer_t file_buffer) { - free((void *)file_buffer.hostname_buf); - file_buffer.hostname_buf = NULL; - - free((void *)file_buffer.hosts_buf); - file_buffer.hosts_buf = NULL; - - free((void *)file_buffer.resolv_conf_buf); - file_buffer.resolv_conf_buf = NULL; -} - int pal_get_version(void) __attribute__((weak, alias ("occlum_pal_get_version"))); int pal_init(const struct occlum_pal_attr *attr)\ diff --git a/src/pal/src/pal_load_file.c b/src/pal/src/pal_load_file.c index d540f33b..ce160917 100644 --- a/src/pal/src/pal_load_file.c +++ b/src/pal/src/pal_load_file.c @@ -1,9 +1,14 @@ #include #include +#include "Enclave_u.h" #include "pal_log.h" #include "pal_load_file.h" -void pal_load_file(const char *filename, load_file_t *load_file) { +void pal_load_file(const sgx_enclave_id_t eid, const char *filename, + load_file_t *load_file) { +#ifndef SGX_MODE_HYPER + UNUSED(eid); +#endif FILE *fp = fopen(filename, "rb"); if (fp == NULL) { @@ -14,7 +19,11 @@ void pal_load_file(const char *filename, load_file_t *load_file) { long fsize = ftell(fp); fseek(fp, 0, SEEK_SET); +#ifndef SGX_MODE_HYPER load_file->buffer = malloc(fsize + 1); +#else + load_file->buffer = sgx_ecall_ms_buffer_alloc(eid, fsize + 1); +#endif if (load_file->buffer == NULL) { PAL_WARN("Warning: Failed to malloc buffer for file: %s", filename); return; @@ -25,3 +34,35 @@ void pal_load_file(const char *filename, load_file_t *load_file) { fclose(fp); } + +void free_host_file_buffer(const sgx_enclave_id_t eid, + struct host_file_buffer_t *file_buffer) { +#ifndef SGX_MODE_HYPER + UNUSED(eid); + if (file_buffer->hostname_buf) { + free((void *)file_buffer->hostname_buf); + } + if (file_buffer->hosts_buf) { + free((void *)file_buffer->hosts_buf); + } + if (file_buffer->resolv_conf_buf) { + free((void *)file_buffer->resolv_conf_buf); + } +#else + if (file_buffer->hostname_buf) { + sgx_ecall_ms_buffer_free(eid); + } + if (file_buffer->hosts_buf) { + sgx_ecall_ms_buffer_free(eid); + } + if (file_buffer->resolv_conf_buf) { + sgx_ecall_ms_buffer_free(eid); + } +#endif + file_buffer->hostname_buf = NULL; + file_buffer->hostname_buf_size = 0; + file_buffer->hosts_buf = NULL; + file_buffer->hosts_buf_size = 0; + file_buffer->resolv_conf_buf = NULL; + file_buffer->resolv_conf_buf_size = 0; +} diff --git a/src/pal/src/pal_load_file.h b/src/pal/src/pal_load_file.h index 05933dee..b1b0137b 100644 --- a/src/pal/src/pal_load_file.h +++ b/src/pal/src/pal_load_file.h @@ -1,11 +1,17 @@ #ifndef __PAL_LOAD_FILE_H__ #define __PAL_LOAD_FILE_H__ +#include + +#define UNUSED(val) (void)(val) typedef struct { unsigned int size; char *buffer; } load_file_t; -void pal_load_file(const char *filename, load_file_t *load_file); +void pal_load_file(const sgx_enclave_id_t eid, const char *filename, + load_file_t *load_file); +void free_host_file_buffer(const sgx_enclave_id_t eid, + struct host_file_buffer_t *file_buffer); #endif /* __PAL_LOAD_FILE_H__ */