From e478eb327493db91754a6fbfdea4ef270835c9e8 Mon Sep 17 00:00:00 2001 From: Andrei Lascu Date: Thu, 31 Oct 2024 11:35:09 +0000 Subject: [PATCH] fixup! Add some useful script and various changes Clang format --- include/compartment.h | 8 +-- include/manager.h | 2 + src/compartment.c | 94 ++++++++++++++++++--------------- src/manager.c | 4 +- tests/manager_caller_multiple.c | 7 +-- tests/test_map_multi.c | 1 - 6 files changed, 62 insertions(+), 54 deletions(-) diff --git a/include/compartment.h b/include/compartment.h index a5539f4..b32f809 100644 --- a/include/compartment.h +++ b/include/compartment.h @@ -167,9 +167,8 @@ struct CompConfig size_t stack_size; struct CompEntryPointDef *entry_points; size_t entry_point_count; - void *base_address; - char** env_ptr; + char **env_ptr; size_t env_ptr_sz; unsigned short env_ptr_count; }; @@ -187,9 +186,6 @@ struct Compartment void *__capability ddc; // ELF data size_t size; // size of compartment in memory - void *base; // address where to load compartment - void *mem_top; - bool mapped; // Environ char **environ_ptr; @@ -221,7 +217,7 @@ entry_point_cmp(const void *, const void *); struct Compartment * comp_from_elf(char *, struct CompConfig *); // char **, size_t, void *); void -comp_map(struct Compartment *); +comp_map(struct Compartment *, void *); void comp_unmap(struct Compartment *); void diff --git a/include/manager.h b/include/manager.h index d9762e2..413623e 100644 --- a/include/manager.h +++ b/include/manager.h @@ -37,6 +37,8 @@ struct Compartment * register_new_comp(char *, bool); int64_t exec_comp(struct Compartment *, char *, char **); +void +map_comp(struct Compartment *, void *); union arg_holder { diff --git a/src/compartment.c b/src/compartment.c index b40794d..6c24255 100644 --- a/src/compartment.c +++ b/src/compartment.c @@ -16,19 +16,23 @@ lib_init(); static struct LibDependency * parse_lib_file(char *, struct Compartment *); static void -parse_lib_segs(Elf64_Ehdr *, void*, struct LibDependency *, struct Compartment *); +parse_lib_segs( + Elf64_Ehdr *, void *, struct LibDependency *, struct Compartment *); static void -parse_lib_symtb(Elf64_Shdr *, Elf64_Ehdr *, void*, struct LibDependency *); +parse_lib_symtb(Elf64_Shdr *, Elf64_Ehdr *, void *, struct LibDependency *); static void -parse_lib_rela(Elf64_Shdr *, Elf64_Ehdr *, void*, struct LibDependency *); +parse_lib_rela(Elf64_Shdr *, Elf64_Ehdr *, void *, struct LibDependency *); static void -parse_lib_dynamic_deps(Elf64_Shdr *, Elf64_Ehdr *, void*, struct LibDependency *); +parse_lib_dynamic_deps( + Elf64_Shdr *, Elf64_Ehdr *, void *, struct LibDependency *); static void map_comp_entry_points(struct Compartment *); static void resolve_rela_syms(struct Compartment *); static void find_tls_lookup_func(struct Compartment *); +static void +prepare_compartment_mapping(struct Compartment *); static bool check_lib_dep_sym(lib_symbol *, const unsigned short); @@ -42,9 +46,9 @@ eval_sym_tls_offset(struct Compartment *, const comp_symbol *); static ssize_t do_pread(int, void *, size_t, off_t); static void -get_lib_data(void*, void*, size_t, off_t); -static void* -seek_lib_data(void*, off_t); +get_lib_data(void *, void *, size_t, off_t); +static void * +seek_lib_data(void *, off_t); static char * find_in_dir(const char *, char *); static void @@ -79,8 +83,6 @@ comp_init() new_comp->ddc = NULL; new_comp->size = 0; - new_comp->base = NULL; - new_comp->mem_top = NULL; new_comp->mapped = false; new_comp->scratch_mem_base = NULL; @@ -138,8 +140,6 @@ comp_from_elf(char *filename, struct CompConfig *cc) { struct Compartment *new_comp = comp_init(); new_comp->cc = cc; - new_comp->base = cc->base_address; // TODO reuse `cc` base - new_comp->mem_top = cc->base_address; unsigned short libs_to_parse_count = 1; unsigned short libs_parsed_count = 0; @@ -204,6 +204,8 @@ comp_from_elf(char *filename, struct CompConfig *cc) assert(new_comp->environ_sz + new_comp->total_tls_size == new_comp->scratch_mem_extra); + prepare_compartment_mapping(new_comp); + return new_comp; } @@ -220,9 +222,10 @@ comp_map(struct Compartment *to_map) struct SegmentMap lib_dep_seg; int lib_dep_fd; - void* map_result = mmap(to_map->base, (intptr_t) ((char*) to_map->mem_top - (char*) to_map->base), - PROT_READ | PROT_WRITE | PROT_EXEC, // TODO fix - MAP_PRIVATE | MAP_FIXED | MAP_ANONYMOUS, -1, 0); + void *map_result = mmap(to_map->base, + (intptr_t) ((char *) to_map->mem_top - (char *) to_map->base), + PROT_READ | PROT_WRITE | PROT_EXEC, // TODO fix + MAP_PRIVATE | MAP_FIXED | MAP_ANONYMOUS, -1, 0); if (map_result == MAP_FAILED) { err(1, "Error mapping compartment %zu data", to_map->id); @@ -236,9 +239,10 @@ comp_map(struct Compartment *to_map) { lib_dep_seg = lib_dep->lib_segs[j]; do_pread(lib_dep_fd, - (char*) lib_dep->lib_mem_base - + (uintptr_t) lib_dep_seg.mem_bot, - lib_dep_seg.file_sz, lib_dep_seg.offset); + (char *) lib_dep->lib_mem_base + + (uintptr_t) lib_dep_seg.mem_bot, + lib_dep_seg.file_sz, lib_dep_seg.offset); + // TODO mprotect } close(lib_dep_fd); } @@ -304,18 +308,19 @@ comp_map(struct Compartment *to_map) } void -comp_unmap(struct Compartment* to_unmap) +comp_unmap(struct Compartment *to_unmap) { int res; - res = munmap(to_unmap->base, (intptr_t) ((char*) to_unmap->mem_top - (char*) to_unmap->base)); + res = munmap(to_unmap->base, + (intptr_t) ((char *) to_unmap->mem_top - (char *) to_unmap->base)); if (res == -1) { err(1, "Error unmapping compartment %zu data", to_unmap->id); } - res - = munmap((void *) to_unmap->scratch_mem_base, to_unmap->scratch_mem_size); + res = munmap( + (void *) to_unmap->scratch_mem_base, to_unmap->scratch_mem_size); if (res == -1) { err(1, "Error unmapping compartment %zu scratch memory", to_unmap->id); @@ -487,8 +492,8 @@ parse_lib_file(char *lib_name, struct Compartment *new_comp) { err(1, "Error accessing data for file %s", lib_path); } - void* lib_data = mmap(NULL, lib_fd_stat.st_size, PROT_READ, MAP_PRIVATE, - lib_fd, 0); + void *lib_data + = mmap(NULL, lib_fd_stat.st_size, PROT_READ, MAP_PRIVATE, lib_fd, 0); close(lib_fd); // Read ELF headers @@ -522,7 +527,7 @@ parse_lib_file(char *lib_name, struct Compartment *new_comp) Elf64_Shdr shstrtab_hdr; get_lib_data(&shstrtab_hdr, lib_data, sizeof(Elf64_Shdr), lib_ehdr.e_shoff + lib_ehdr.e_shstrndx * sizeof(Elf64_Shdr)); - char* shstrtab = (char*) seek_lib_data(lib_data, shstrtab_hdr.sh_offset); + char *shstrtab = (char *) seek_lib_data(lib_data, shstrtab_hdr.sh_offset); // XXX The string table is read in `strtab` as a sequence of // variable-length strings. Then, symbol names are obtained by indexing at @@ -596,8 +601,8 @@ parse_lib_file(char *lib_name, struct Compartment *new_comp) } static void -parse_lib_segs(Elf64_Ehdr *lib_ehdr, void* lib_data, struct LibDependency *lib_dep, - struct Compartment *new_comp) +parse_lib_segs(Elf64_Ehdr *lib_ehdr, void *lib_data, + struct LibDependency *lib_dep, struct Compartment *new_comp) { // Get segment data Elf64_Phdr lib_phdr; @@ -655,7 +660,7 @@ parse_lib_segs(Elf64_Ehdr *lib_ehdr, void* lib_data, struct LibDependency *lib_d } static void -parse_lib_symtb(Elf64_Shdr *symtb_shdr, Elf64_Ehdr *lib_ehdr, void* lib_data, +parse_lib_symtb(Elf64_Shdr *symtb_shdr, Elf64_Ehdr *lib_ehdr, void *lib_data, struct LibDependency *lib_dep) { // Get symbol table @@ -664,8 +669,9 @@ parse_lib_symtb(Elf64_Shdr *symtb_shdr, Elf64_Ehdr *lib_ehdr, void* lib_data, get_lib_data(&link_shdr, lib_data, sizeof(Elf64_Shdr), lib_ehdr->e_shoff + symtb_shdr->sh_link * sizeof(Elf64_Shdr)); - Elf64_Sym* sym_tb = (Elf64_Sym*) seek_lib_data(lib_data, symtb_shdr->sh_offset); - char* str_tb = (char*) seek_lib_data(lib_data, link_shdr.sh_offset); + Elf64_Sym *sym_tb + = (Elf64_Sym *) seek_lib_data(lib_data, symtb_shdr->sh_offset); + char *str_tb = (char *) seek_lib_data(lib_data, link_shdr.sh_offset); size_t lib_syms_count = symtb_shdr->sh_size / sizeof(Elf64_Sym); size_t actual_syms = 0; @@ -699,23 +705,25 @@ parse_lib_symtb(Elf64_Shdr *symtb_shdr, Elf64_Ehdr *lib_ehdr, void* lib_data, } static void -parse_lib_rela(Elf64_Shdr *rela_shdr, Elf64_Ehdr *lib_ehdr, void* lib_data, +parse_lib_rela(Elf64_Shdr *rela_shdr, Elf64_Ehdr *lib_ehdr, void *lib_data, struct LibDependency *lib_dep) { // Traverse `.rela.plt`, so we can see which function addresses we need // to eagerly load - Elf64_Rela* rela_sec = (Elf64_Rela*) seek_lib_data(lib_data, rela_shdr->sh_offset); + Elf64_Rela *rela_sec + = (Elf64_Rela *) seek_lib_data(lib_data, rela_shdr->sh_offset); size_t rela_count = rela_shdr->sh_size / sizeof(Elf64_Rela); Elf64_Shdr dyn_sym_hdr; get_lib_data(&dyn_sym_hdr, lib_data, sizeof(Elf64_Shdr), lib_ehdr->e_shoff + rela_shdr->sh_link * sizeof(Elf64_Shdr)); - Elf64_Sym *dyn_sym_tbl = (Elf64_Sym*) seek_lib_data(lib_data, dyn_sym_hdr.sh_offset); + Elf64_Sym *dyn_sym_tbl + = (Elf64_Sym *) seek_lib_data(lib_data, dyn_sym_hdr.sh_offset); Elf64_Shdr dyn_str_hdr; get_lib_data(&dyn_str_hdr, lib_data, sizeof(Elf64_Shdr), lib_ehdr->e_shoff + dyn_sym_hdr.sh_link * sizeof(Elf64_Shdr)); - char *dyn_str_tbl = (char*) seek_lib_data(lib_data, dyn_str_hdr.sh_offset); + char *dyn_str_tbl = (char *) seek_lib_data(lib_data, dyn_str_hdr.sh_offset); // XXX Since TLSDESC entries might resolve to two relocation slots, we // ensure we have enough space by doubling the expected relocation counts @@ -873,14 +881,15 @@ parse_lib_rela(Elf64_Shdr *rela_shdr, Elf64_Ehdr *lib_ehdr, void* lib_data, static void parse_lib_dynamic_deps(Elf64_Shdr *dynamic_shdr, Elf64_Ehdr *lib_ehdr, - void* lib_data, struct LibDependency *lib_dep) + void *lib_data, struct LibDependency *lib_dep) { // Find additional library dependencies - Elf64_Dyn *dyn_entries = (Elf64_Dyn*) seek_lib_data(lib_data, dynamic_shdr->sh_offset); + Elf64_Dyn *dyn_entries + = (Elf64_Dyn *) seek_lib_data(lib_data, dynamic_shdr->sh_offset); Elf64_Shdr dynstr_shdr; get_lib_data(&dynstr_shdr, lib_data, sizeof(Elf64_Shdr), lib_ehdr->e_shoff + dynamic_shdr->sh_link * sizeof(Elf64_Shdr)); - char *dynstr_tbl = (char*) seek_lib_data(lib_data, dynstr_shdr.sh_offset); + char *dynstr_tbl = (char *) seek_lib_data(lib_data, dynstr_shdr.sh_offset); for (size_t i = 0; i < dynamic_shdr->sh_size / sizeof(Elf64_Dyn); ++i) { @@ -1089,15 +1098,15 @@ do_pread(int fd, void *buf, size_t count, off_t offset) } static void -get_lib_data(void* buf, void* lib_file_addr, size_t data_sz, off_t offset) +get_lib_data(void *buf, void *lib_file_addr, size_t data_sz, off_t offset) { - memcpy(buf, (char*) lib_file_addr + offset, data_sz); + memcpy(buf, (char *) lib_file_addr + offset, data_sz); } -static void* -seek_lib_data(void* lib_data, off_t offset) +static void * +seek_lib_data(void *lib_data, off_t offset) { - return (void*) ((char*) lib_data + offset); + return (void *) ((char *) lib_data + offset); } static void * @@ -1235,7 +1244,8 @@ setup_environ(struct Compartment *new_comp) { assert(new_comp->cc->env_ptr != NULL); // TODO consider optional check new_comp->environ_sz - = align_up(new_comp->cc->env_ptr_sz, new_comp->page_size) + new_comp->page_size; + = align_up(new_comp->cc->env_ptr_sz, new_comp->page_size) + + new_comp->page_size; new_comp->environ_ptr = get_extra_scratch_region_base(new_comp); adjust_comp_scratch_mem(new_comp, new_comp->environ_sz); } diff --git a/src/manager.c b/src/manager.c index 6f35fa2..7b14f64 100644 --- a/src/manager.c +++ b/src/manager.c @@ -376,14 +376,14 @@ prepare_compartment_environ() // We only save offsets for the pointers, since they'll be relocated // relative to the compartment base address proc_env_ptr[proc_env_count] = (char *) (vals_offset + proc_env_ptr_sz); - strcpy((char *) proc_env_ptr + vals_offset + proc_env_ptr_sz, *curr_env); + strcpy( + (char *) proc_env_ptr + vals_offset + proc_env_ptr_sz, *curr_env); proc_env_count += 1; proc_env_ptr_sz += strlen(*curr_env) + 1; } proc_env_ptr_sz += vals_offset; proc_env_ptr = realloc(proc_env_ptr, proc_env_ptr_sz); - } static void * diff --git a/tests/manager_caller_multiple.c b/tests/manager_caller_multiple.c index 131762e..c41583d 100644 --- a/tests/manager_caller_multiple.c +++ b/tests/manager_caller_multiple.c @@ -3,10 +3,11 @@ int main(int argc, char **argv) { - const char* count_env_name = "EXECUTE_COUNT"; - const char* count_env_val = getenv(count_env_name); + const char *count_env_name = "EXECUTE_COUNT"; + const char *count_env_val = getenv(count_env_name); const unsigned int comps_count_default = 100; - unsigned int comps_count = count_env_val ? atoi(count_env_val) : comps_count_default; + unsigned int comps_count + = count_env_val ? atoi(count_env_val) : comps_count_default; // Initial setup manager_ddc = cheri_ddc_get(); diff --git a/tests/test_map_multi.c b/tests/test_map_multi.c index ebf9f51..75f4971 100644 --- a/tests/test_map_multi.c +++ b/tests/test_map_multi.c @@ -17,4 +17,3 @@ main() comp_clean(hw_comp); return 0; } -