diff --git a/include/compartment.h b/include/compartment.h index 7414398..97c934b 100644 --- a/include/compartment.h +++ b/include/compartment.h @@ -62,7 +62,7 @@ extern void *__capability comp_return_caps[2]; * TODO recheck this is properly used, or re-design into a more light-weight * approach with pre-given transition capabilities */ -struct intercept_patch +struct InterceptPatch { int *patch_addr; int32_t instr[INTERCEPT_INSTR_COUNT]; @@ -83,7 +83,7 @@ struct intercept_patch /* Struct representing a valid entry point to a compartment */ -struct entry_point +struct CompEntryPoint { const char *fn_name; void *fn_addr; @@ -153,7 +153,7 @@ struct Compartment size_t size; // size of compartment in memory void *base; // address where to load compartment size_t entry_point_count; - struct entry_point **comp_fns; + struct CompEntryPoint **comp_eps; void *mem_top; bool mapped; bool mapped_full; @@ -170,7 +170,7 @@ struct Compartment void *scratch_mem_stack_top; size_t scratch_mem_stack_size; void *stack_pointer; - struct mem_alloc *alloc_head; + struct MemAlloc *alloc_head; void *manager_caps; size_t max_manager_caps_count; @@ -190,7 +190,7 @@ struct Compartment // Misc short curr_intercept_count; - struct intercept_patch *intercept_patches; + struct InterceptPatch *intercept_patches; }; int diff --git a/include/intercept.h b/include/intercept.h index 9599421..7d00366 100644 --- a/include/intercept.h +++ b/include/intercept.h @@ -35,7 +35,7 @@ extern void *__capability sealed_redirect_cap; /* Data required to perform the transition for an intercepted function */ -struct func_intercept +struct FuncIntercept { char *func_name; void *redirect_func; @@ -82,7 +82,7 @@ my_fprintf(FILE *, const char *, ...); size_t my_call_comp(size_t, char *, void *, size_t); -static const struct func_intercept to_intercept_funcs[] = { +static const struct FuncIntercept to_intercept_funcs[] = { /* vDSO funcs */ { "time", (void *) intercepted_time }, /* Mem funcs */ @@ -94,6 +94,6 @@ static const struct func_intercept to_intercept_funcs[] = { // Functions to be intercepted and associated data #define INTERCEPT_FUNC_COUNT \ sizeof(to_intercept_funcs) / sizeof(to_intercept_funcs[0]) -extern struct func_intercept comp_intercept_funcs[INTERCEPT_FUNC_COUNT]; +extern struct FuncIntercept comp_intercept_funcs[INTERCEPT_FUNC_COUNT]; #endif // _INTERCEPT_H diff --git a/include/manager.h b/include/manager.h index bb11dc5..1cc4cbc 100644 --- a/include/manager.h +++ b/include/manager.h @@ -41,7 +41,7 @@ extern const char *comp_config_suffix; * information that we expect to appear in the compartment, as given by its * compartment configuration file */ -struct ConfigEntryPoint +struct CompEntryPointDef { const char *name; size_t arg_count; @@ -51,7 +51,7 @@ struct ConfigEntryPoint struct CompWithEntries { struct Compartment *comp; - struct ConfigEntryPoint *cep; + struct CompEntryPointDef *cep; }; void * @@ -61,24 +61,6 @@ register_new_comp(char *, bool); int64_t exec_comp(struct Compartment *, char *, char **); -struct Compartment * -manager_find_compartment_by_addr(void *); -struct Compartment * -manager_find_compartment_by_ddc(void *__capability); -struct Compartment *manager_get_compartment_by_id(size_t); - -// TODO stack setup when we transition into the compartment; unsure if needed, -// but keep for now, just in case -#define ENV_FIELDS_CNT 1 -extern const char *comp_env_fields[ENV_FIELDS_CNT]; -extern char **environ; -const char * -get_env_str(const char *); -int -manager___vdso_clock_gettime(clockid_t, struct timespec *); - -// END TODO - union arg_holder { int i; @@ -95,7 +77,7 @@ clean_all_comps(); void clean_comp(struct Compartment *); void -clean_compartment_config(struct ConfigEntryPoint *, size_t); +clean_compartment_config(struct CompEntryPointDef *, size_t); /******************************************************************************* * Memory allocation diff --git a/include/mem_mng.h b/include/mem_mng.h index b6aeaf2..fb5d8b0 100644 --- a/include/mem_mng.h +++ b/include/mem_mng.h @@ -11,13 +11,13 @@ #include "compartment.h" // TODO consider single linked list -struct mem_alloc +struct MemAlloc { uintptr_t ptr; size_t size; - struct mem_alloc *prev_alloc; - struct mem_alloc *next_alloc; + struct MemAlloc *prev_alloc; + struct MemAlloc *next_alloc; }; extern size_t comp_mem_alloc; @@ -26,10 +26,10 @@ extern size_t comp_mem_max; void * manager_register_mem_alloc(struct Compartment *, size_t); void -manager_insert_new_alloc(struct Compartment *, struct mem_alloc *); +manager_insert_new_alloc(struct Compartment *, struct MemAlloc *); size_t manager_free_mem_alloc(struct Compartment *, void *); -struct mem_alloc * +struct MemAlloc * get_alloc_struct_from_ptr(struct Compartment *, uintptr_t); #endif // MEM_MNG_H diff --git a/src/compartment.c b/src/compartment.c index ecc5886..05351e9 100644 --- a/src/compartment.c +++ b/src/compartment.c @@ -38,13 +38,13 @@ comp_init() * Main compartment functions ******************************************************************************/ -/* Comparison function for `struct entry_point` +/* Comparison function for `struct CompEntryPoint` */ int entry_point_cmp(const void *val1, const void *val2) { - struct entry_point *ep1 = *(struct entry_point **) val1; - struct entry_point *ep2 = *(struct entry_point **) val2; + struct CompEntryPoint *ep1 = *(struct CompEntryPoint **) val1; + struct CompEntryPoint *ep2 = *(struct CompEntryPoint **) val2; return strcmp(ep1->fn_name, ep2->fn_name); } @@ -67,7 +67,8 @@ comp_from_elf(char *filename, char **entry_points, size_t entry_point_count, assert(entry_points); assert(entry_point_count > 0); - new_comp->comp_fns = malloc(entry_point_count * sizeof(struct entry_point)); + new_comp->comp_eps + = malloc(entry_point_count * sizeof(struct CompEntryPoint)); // Read elf headers Elf64_Ehdr comp_ehdr; @@ -331,8 +332,8 @@ comp_from_elf(char *filename, char **entry_points, size_t entry_point_count, comp_symtb, comp_strtb, comp_symtb_shdr.sh_size); for (size_t i = 0; i < entry_point_count; ++i) { - struct entry_point *new_entry_point - = malloc(sizeof(struct entry_point)); + struct CompEntryPoint *new_entry_point + = malloc(sizeof(struct CompEntryPoint)); new_entry_point->fn_name = entry_points[i]; switch (new_comp->elf_type) { @@ -350,7 +351,7 @@ comp_from_elf(char *filename, char **entry_points, size_t entry_point_count, default: errx(1, "Invalid ELF type"); } - new_comp->comp_fns[new_comp->entry_point_count] = new_entry_point; + new_comp->comp_eps[new_comp->entry_point_count] = new_entry_point; new_comp->entry_point_count += 1; } free(ep_syms); @@ -517,7 +518,7 @@ comp_add_intercept(struct Compartment *new_comp, uintptr_t intercept_target, new_instrs[new_instr_idx++] = arm_b_instr; assert(new_instr_idx == INTERCEPT_INSTR_COUNT); - struct intercept_patch new_patch; + struct InterceptPatch new_patch; new_patch.patch_addr = (void *) intercept_target; memcpy(new_patch.instr, new_instrs, sizeof(new_instrs)); __clear_cache(new_patch.instr, new_patch.instr + sizeof(new_instrs)); @@ -525,7 +526,7 @@ comp_add_intercept(struct Compartment *new_comp, uintptr_t intercept_target, new_patch.manager_cap = sealed_redirect_cap; new_comp->curr_intercept_count += 1; new_comp->intercept_patches = realloc(new_comp->intercept_patches, - new_comp->curr_intercept_count * sizeof(struct intercept_patch)); + new_comp->curr_intercept_count * sizeof(struct InterceptPatch)); new_comp->intercept_patches[new_comp->curr_intercept_count - 1] = new_patch; } @@ -614,7 +615,7 @@ comp_map(struct Compartment *to_map) // Inject intercept instructions within identified intercepted functions for (size_t i = 0; i < to_map->curr_intercept_count; ++i) { - struct intercept_patch to_patch = to_map->intercept_patches[i]; + struct InterceptPatch to_patch = to_map->intercept_patches[i]; // TODO change to memcpy? for (size_t j = 0; j < INTERCEPT_INSTR_COUNT; ++j) { @@ -671,9 +672,9 @@ comp_exec( void *fn = NULL; for (size_t i = 0; i < to_exec->entry_point_count; ++i) { - if (!strcmp(fn_name, to_exec->comp_fns[i]->fn_name)) + if (!strcmp(fn_name, to_exec->comp_eps[i]->fn_name)) { - fn = (void *) to_exec->comp_fns[i]->fn_addr; + fn = (void *) to_exec->comp_eps[i]->fn_addr; break; } } @@ -726,8 +727,8 @@ comp_clean(struct Compartment *to_clean) for (size_t i = 0; i < to_clean->entry_point_count; ++i) { - free((char *) to_clean->comp_fns[i]->fn_name); - free(to_clean->comp_fns[i]); + free((char *) to_clean->comp_eps[i]->fn_name); + free(to_clean->comp_eps[i]); } for (size_t i = 0; i < to_clean->rela_maps_count; ++i) diff --git a/src/intercept.c b/src/intercept.c index 72f24ef..4969a4e 100644 --- a/src/intercept.c +++ b/src/intercept.c @@ -1,6 +1,6 @@ #include "intercept.h" -struct func_intercept comp_intercept_funcs[INTERCEPT_FUNC_COUNT]; +struct FuncIntercept comp_intercept_funcs[INTERCEPT_FUNC_COUNT]; void *__capability comp_return_caps[COMP_RETURN_CAPS_COUNT]; void *__capability sealed_redirect_cap; @@ -131,7 +131,7 @@ my_realloc(void *ptr, size_t to_alloc) } void *new_ptr = manager_register_mem_alloc(comp, to_alloc); - struct mem_alloc *old_alloc + struct MemAlloc *old_alloc = get_alloc_struct_from_ptr(comp, (uintptr_t) ptr); memcpy( new_ptr, ptr, to_alloc < old_alloc->size ? to_alloc : old_alloc->size); diff --git a/src/manager.c b/src/manager.c index 64a6721..01d4f60 100644 --- a/src/manager.c +++ b/src/manager.c @@ -11,39 +11,22 @@ const uintptr_t comp_start_addr = 0x1000000UL; const unsigned short comp_page_interval_count = 2; void *min_next_comp_addr = NULL; -const char *comp_env_fields[] = { - "PATH", -}; void *__capability manager_ddc = 0; const char *comp_config_suffix = ".comp"; -static struct ConfigEntryPoint * +static struct CompEntryPointDef * parse_compartment_config(char *, size_t *, bool); -static struct ConfigEntryPoint * +static struct CompEntryPointDef * make_default_entry_point(); -static struct ConfigEntryPoint -get_entry_point(char *, struct ConfigEntryPoint *, size_t); +static struct CompEntryPointDef +get_entry_point(char *, struct CompEntryPointDef *, size_t); static void * -prepare_compartment_args(char **args, struct ConfigEntryPoint); +prepare_compartment_args(char **args, struct CompEntryPointDef); static struct CompWithEntries * get_comp_with_entries(struct Compartment *); -const char * -get_env_str(const char *env_name) -{ - size_t env_name_len = strlen(env_name); - for (char **env = environ; env != NULL; ++env) - { - const char *str = *env; - if (strncmp(str, env_name, env_name_len) == 0 - && str[env_name_len] == '=') - return str; - } - return NULL; -} - /******************************************************************************* * Utility functions ******************************************************************************/ @@ -76,7 +59,7 @@ struct Compartment * register_new_comp(char *filename, bool allow_default_entry) { size_t new_comp_ep_count; - struct ConfigEntryPoint *new_cep = parse_compartment_config( + struct CompEntryPointDef *new_cep = parse_compartment_config( filename, &new_comp_ep_count, allow_default_entry); char **ep_names = calloc(new_comp_ep_count, sizeof(char *)); @@ -136,7 +119,7 @@ int64_t exec_comp(struct Compartment *to_exec, char *entry_fn, char **entry_fn_args) { struct CompWithEntries *comp_to_run = get_comp_with_entries(to_exec); - struct ConfigEntryPoint comp_entry = get_entry_point( + struct CompEntryPointDef comp_entry = get_entry_point( entry_fn, comp_to_run->cep, to_exec->entry_point_count); void *comp_args = prepare_compartment_args(entry_fn_args, comp_entry); @@ -251,7 +234,7 @@ prep_config_filename(char *filename) return config_filename; } -static struct ConfigEntryPoint * +static struct CompEntryPointDef * parse_compartment_config( char *comp_filename, size_t *entry_point_count, bool allow_default) { @@ -276,8 +259,8 @@ parse_compartment_config( toml_parse_error("TOML table parse error", toml_errbuf); } *entry_point_count = toml_table_ntab(tab); - struct ConfigEntryPoint *entry_points - = malloc(*entry_point_count * sizeof(struct ConfigEntryPoint)); + struct CompEntryPointDef *entry_points + = malloc(*entry_point_count * sizeof(struct CompEntryPointDef)); for (size_t i = 0; i < *entry_point_count; ++i) { const char *fname = toml_key_in(tab, i); @@ -304,7 +287,8 @@ parse_compartment_config( } void -clean_compartment_config(struct ConfigEntryPoint *cep, size_t entry_point_count) +clean_compartment_config( + struct CompEntryPointDef *cep, size_t entry_point_count) { for (size_t i = 0; i < entry_point_count; ++i) { @@ -318,11 +302,11 @@ clean_compartment_config(struct ConfigEntryPoint *cep, size_t entry_point_count) free(cep); } -static struct ConfigEntryPoint +static struct CompEntryPointDef get_entry_point( - char *entry_point_fn, struct ConfigEntryPoint *ceps, size_t cep_count) + char *entry_point_fn, struct CompEntryPointDef *ceps, size_t cep_count) { - struct ConfigEntryPoint curr_ep; + struct CompEntryPointDef curr_ep; while (cep_count != 0) { curr_ep = ceps[cep_count - 1]; @@ -336,7 +320,7 @@ get_entry_point( } static void * -prepare_compartment_args(char **args, struct ConfigEntryPoint cep) +prepare_compartment_args(char **args, struct CompEntryPointDef cep) { void *parsed_args = calloc(COMP_ARG_SIZE, cep.arg_count); size_t allocated_args = 0; @@ -379,10 +363,10 @@ prepare_compartment_args(char **args, struct ConfigEntryPoint cep) return parsed_args; } -static struct ConfigEntryPoint * +static struct CompEntryPointDef * make_default_entry_point() { - struct ConfigEntryPoint *cep = malloc(sizeof(struct ConfigEntryPoint)); + struct CompEntryPointDef *cep = malloc(sizeof(struct CompEntryPointDef)); cep->name = malloc(strlen("main") + 1); strcpy((char *) cep->name, "main"); cep->arg_count = 0; diff --git a/src/mem_mng.c b/src/mem_mng.c index a029093..061210d 100644 --- a/src/mem_mng.c +++ b/src/mem_mng.c @@ -14,7 +14,7 @@ manager_register_mem_alloc(struct Compartment *comp, size_t mem_size) { // TODO better algorithm to find blocks of memory available for mapping void *new_mem = (char *) comp->scratch_mem_base + comp->scratch_mem_alloc; - struct mem_alloc *new_alloc = malloc(sizeof(struct mem_alloc)); + struct MemAlloc *new_alloc = malloc(sizeof(struct MemAlloc)); new_alloc->ptr = (uintptr_t) new_mem; new_alloc->size = mem_size; manager_insert_new_alloc(comp, new_alloc); @@ -23,7 +23,7 @@ manager_register_mem_alloc(struct Compartment *comp, size_t mem_size) } void -manager_insert_new_alloc(struct Compartment *comp, struct mem_alloc *to_insert) +manager_insert_new_alloc(struct Compartment *comp, struct MemAlloc *to_insert) { if (comp->alloc_head == NULL) { @@ -42,7 +42,7 @@ manager_insert_new_alloc(struct Compartment *comp, struct mem_alloc *to_insert) return; } - struct mem_alloc *curr_alloc = comp->alloc_head; + struct MemAlloc *curr_alloc = comp->alloc_head; while (curr_alloc->next_alloc != NULL && curr_alloc->ptr < to_insert->ptr) { curr_alloc = curr_alloc->next_alloc; @@ -65,7 +65,7 @@ manager_insert_new_alloc(struct Compartment *comp, struct mem_alloc *to_insert) size_t manager_free_mem_alloc(struct Compartment *comp, void *ptr) { - struct mem_alloc *curr_alloc = comp->alloc_head; + struct MemAlloc *curr_alloc = comp->alloc_head; while (curr_alloc != NULL && curr_alloc->ptr != (uintptr_t) ptr) { curr_alloc = curr_alloc->next_alloc; @@ -99,10 +99,10 @@ manager_free_mem_alloc(struct Compartment *comp, void *ptr) * \param ptr Address to search for * \return A record indicating the requested memory allocation */ -struct mem_alloc * +struct MemAlloc * get_alloc_struct_from_ptr(struct Compartment *comp, uintptr_t ptr) { - struct mem_alloc *curr_alloc = comp->alloc_head; + struct MemAlloc *curr_alloc = comp->alloc_head; while (curr_alloc->next_alloc != NULL) { if (curr_alloc->ptr == ptr) diff --git a/tests/manager_caller.c b/tests/manager_caller.c index 8a631c0..6c7994a 100644 --- a/tests/manager_caller.c +++ b/tests/manager_caller.c @@ -10,11 +10,6 @@ main(int argc, char **argv) assert(argc >= 2 && "Expect at least one argument: binary file for compartment"); char *file = argv[1]; - const char *prefix = "./"; - if (!strncmp(file, prefix, strlen(prefix))) - { - file += strlen(prefix); - } struct Compartment *hw_comp = register_new_comp(file, true); comp_map(hw_comp);