Skip to content

Commit

Permalink
Some renames and cleanup
Browse files Browse the repository at this point in the history
* Rename `ConfigEntryPoint` -> `CompEntryPointDef`
* `struct entry_point` > `struct CompEntryPoint`
* Rename other snake_case `struct`s to PascalCase
* `compartment->comp_fns` > `compartment->comp_eps`
* Rethink filename manipulation to get compartment config file name;
  should fix some potential leaks
  • Loading branch information
0152la committed Feb 8, 2024
1 parent e9ffc4b commit 3bac0aa
Show file tree
Hide file tree
Showing 9 changed files with 57 additions and 95 deletions.
10 changes: 5 additions & 5 deletions include/compartment.h
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -190,7 +190,7 @@ struct Compartment

// Misc
short curr_intercept_count;
struct intercept_patch *intercept_patches;
struct InterceptPatch *intercept_patches;
};

int
Expand Down
6 changes: 3 additions & 3 deletions include/intercept.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 */
Expand All @@ -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
24 changes: 3 additions & 21 deletions include/manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -51,7 +51,7 @@ struct ConfigEntryPoint
struct CompWithEntries
{
struct Compartment *comp;
struct ConfigEntryPoint *cep;
struct CompEntryPointDef *cep;
};

void *
Expand All @@ -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;
Expand All @@ -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
Expand Down
10 changes: 5 additions & 5 deletions include/mem_mng.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
29 changes: 15 additions & 14 deletions src/compartment.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand All @@ -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;
Expand Down Expand Up @@ -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)
{
Expand All @@ -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);
Expand Down Expand Up @@ -517,15 +518,15 @@ 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));
new_patch.comp_manager_cap_addr = comp_manager_cap_addr;
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;
}

Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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;
}
}
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions src/intercept.c
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -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);
Expand Down
52 changes: 18 additions & 34 deletions src/manager.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
******************************************************************************/
Expand Down Expand Up @@ -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 *));
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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)
{
Expand All @@ -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);
Expand All @@ -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)
{
Expand All @@ -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];
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
Loading

0 comments on commit 3bac0aa

Please sign in to comment.