Deep copy host file

This commit is contained in:
ClawSeven 2022-04-22 16:46:41 +08:00 committed by volcano
parent ff48b7d807
commit 036eb08193
7 changed files with 63 additions and 29 deletions

@ -32,6 +32,15 @@ enclave {
uint32_t qe_identity_size; uint32_t qe_identity_size;
}; };
struct host_file_buffer {
[size=resolv_conf_buf_size]char *resolv_conf_buf;
uint32_t resolv_conf_buf_size;
[size=hosts_buf_size]char *hosts_buf;
uint32_t hosts_buf_size;
[size=hostname_buf_size]char *hostname_buf;
uint32_t hostname_buf_size;
};
trusted { trusted {
/* /*
* Initialize the LibOS according to the specified attributes. * Initialize the LibOS according to the specified attributes.
@ -43,7 +52,10 @@ enclave {
* EINVAL - The value of an argument are invalid. * EINVAL - The value of an argument are invalid.
*/ */
public int occlum_ecall_init([in, string] const char* log_level, [in, string] const char* instance_dir, [in] const struct host_file_buffer* file_buffer); public int occlum_ecall_init(
[in, string] const char* log_level,
[in, string] const char* instance_dir,
[in] const struct host_file_buffer* file_buffer);
/* /*
* Create a new LibOS process to do the task specified by the given * Create a new LibOS process to do the task specified by the given

@ -29,13 +29,6 @@ typedef struct itimerspec{
struct _timespec it_value; struct _timespec it_value;
} itimerspec_t; } itimerspec_t;
// The host_file_buffer struct includes /etc/resolv.conf, /etc/hosts and /etc/hostname buffer
struct host_file_buffer {
const char* resolv_conf_buf;
const char* hosts_buf;
const char* hostname_buf;
};
#define FD_SETSIZE 1024 #define FD_SETSIZE 1024
typedef struct { typedef struct {
unsigned long fds_bits[FD_SETSIZE / 8 / sizeof(long)]; unsigned long fds_bits[FD_SETSIZE / 8 / sizeof(long)];

@ -7,8 +7,11 @@ use std::str;
#[repr(C)] #[repr(C)]
pub struct host_file_buffer { pub struct host_file_buffer {
pub resolv_conf_buf: *const c_char, pub resolv_conf_buf: *const c_char,
resolv_conf_buf_size: u32,
pub hosts_buf: *const c_char, pub hosts_buf: *const c_char,
hosts_buf_size: u32,
pub hostname_buf: *const c_char, pub hostname_buf: *const c_char,
hostname_buf_size: u32,
} }
pub enum HostFile { pub enum HostFile {

@ -93,10 +93,13 @@ struct occlum_pal_create_process_args {
int *pid; int *pid;
}; };
struct host_file_buffer { struct host_file_buffer_t {
const char *resolv_conf_buf; char *resolv_conf_buf;
const char *hosts_buf; unsigned int resolv_conf_buf_size;
const char *hostname_buf; char *hosts_buf;
unsigned int hosts_buf_size;
char *hostname_buf;
unsigned int hostname_buf_size;
}; };
/* /*
@ -164,7 +167,7 @@ int occlum_pal_kill(int pid, int sig);
*/ */
int occlum_pal_destroy(void); int occlum_pal_destroy(void);
void free_host_file_buffer(struct host_file_buffer file_buffer); void free_host_file_buffer_t(struct host_file_buffer_t file_buffer);
#ifdef __cplusplus #ifdef __cplusplus
} }

@ -107,18 +107,33 @@ int occlum_pal_init(const struct occlum_pal_attr *attr) {
eid = pal_get_enclave_id(); eid = pal_get_enclave_id();
int ecall_ret = 0; int ecall_ret = 0;
struct host_file_buffer file_buffer = {
.hostname_buf = pal_load_file_to_string("/etc/hostname"), load_file_t hostname_ptr = {0, NULL};
.hosts_buf = pal_load_file_to_string("/etc/hosts"), load_file_t hosts_ptr = {0, NULL};
.resolv_conf_buf = pal_load_file_to_string("/etc/resolv.conf"), 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);
struct host_file_buffer_t file_buffer = {
.hostname_buf = hostname_ptr.buffer,
.hostname_buf_size = hostname_ptr.size,
.hosts_buf = hosts_ptr.buffer,
.hosts_buf_size = hosts_ptr.size,
.resolv_conf_buf = resolv_conf_ptr.buffer,
.resolv_conf_buf_size = resolv_conf_ptr.size,
}; };
const struct host_file_buffer *file_buffer_ptr = &file_buffer; 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, sgx_status_t ecall_status = occlum_ecall_init(eid, &ecall_ret, attr->log_level,
resolved_path, file_buffer_ptr); resolved_path, file_buffer_ptr);
free_host_file_buffer(file_buffer); free_host_file_buffer_t(file_buffer);
hostname_ptr.buffer = NULL;
hosts_ptr.buffer = NULL;
resolv_conf_ptr.buffer = NULL;
if (ecall_status != SGX_SUCCESS) { if (ecall_status != SGX_SUCCESS) {
const char *sgx_err = pal_get_sgx_error_msg(ecall_status); const char *sgx_err = pal_get_sgx_error_msg(ecall_status);
@ -280,7 +295,7 @@ int occlum_pal_destroy(void) {
return ret; return ret;
} }
void free_host_file_buffer(struct host_file_buffer file_buffer) { void free_host_file_buffer_t(struct host_file_buffer_t file_buffer) {
free((void *)file_buffer.hostname_buf); free((void *)file_buffer.hostname_buf);
file_buffer.hostname_buf = NULL; file_buffer.hostname_buf = NULL;

@ -1,24 +1,27 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include "pal_log.h" #include "pal_log.h"
#include "pal_load_file.h"
char *pal_load_file_to_string(const char *filename) { void pal_load_file(const char *filename, load_file_t *load_file) {
FILE *fp = fopen(filename, "rb"); FILE *fp = fopen(filename, "rb");
if (fp == NULL) { if (fp == NULL) {
PAL_WARN("Warning: Failed to open file: %s", filename); PAL_WARN("Warning: Failed to open file: %s", filename);
return NULL; return;
} }
fseek(fp, 0, SEEK_END); fseek(fp, 0, SEEK_END);
long fsize = ftell(fp); long fsize = ftell(fp);
fseek(fp, 0, SEEK_SET); fseek(fp, 0, SEEK_SET);
char *file_buffer = malloc(fsize + 1); load_file->buffer = malloc(fsize + 1);
if (file_buffer == NULL) { if (load_file->buffer == NULL) {
PAL_WARN("Warning: Failed to malloc buffer for file: %s", filename); PAL_WARN("Warning: Failed to malloc buffer for file: %s", filename);
return NULL; return;
} }
fread(file_buffer, 1, fsize, fp); fread(load_file->buffer, 1, fsize, fp);
file_buffer[fsize] = '\0'; load_file->buffer[fsize] = '\0';
load_file->size = fsize + 1;
fclose(fp); fclose(fp);
return file_buffer;
} }

@ -1,6 +1,11 @@
#ifndef __PAL_LOAD_FILE_H__ #ifndef __PAL_LOAD_FILE_H__
#define __PAL_LOAD_FILE_H__ #define __PAL_LOAD_FILE_H__
char *pal_load_file_to_string(const char *filename); typedef struct {
unsigned int size;
char *buffer;
} load_file_t;
void pal_load_file(const char *filename, load_file_t *load_file);
#endif /* __PAL_LOAD_FILE_H__ */ #endif /* __PAL_LOAD_FILE_H__ */