Skip to content

Commit

Permalink
Update internal malloc and others
Browse files Browse the repository at this point in the history
* Provide a `malloc` and friends compartment library to be used
  internally, overriding the previous interception implementation. We
  use the DDC capability to get the are of memory that is designated as
  heap space. This is done in a new library, `libcomputils.so`, to be
  loaded in a compartment. This means further libraries loaded will also
  use our own internal `malloc` implementation;
* Due to the above change, we also remove old intercept stuff, as that
  has been completely outdated by newer features (might come back for
  inter-compartment calls);
* Fix setting improper bounds for compartment DDC capabilities (i.e.,
  ensure the capability encompasses only the memory region designated
  for a compartment), and now set the offset to match the start of the
  compartment heap, to use for internal `malloc`;
* Further improve symbol relocation lookup, including support for "raw"
  relocations that refer only to addresses, not to a given symbol.
  • Loading branch information
0152la committed Apr 25, 2024
1 parent 9bc215b commit 7df6fee
Show file tree
Hide file tree
Showing 20 changed files with 219 additions and 530 deletions.
17 changes: 17 additions & 0 deletions include/comp_utils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#ifndef _COMP_UTILS_H
#define _COMP_UTILS_H

#include <err.h>
#include <stddef.h>
#include <string.h>

#include "cheriintrin.h"

void *malloc(size_t);
void
free(void *);
void *calloc(size_t, size_t);
void *
realloc(void *, size_t);

#endif // _COMP_UTILS_H
26 changes: 1 addition & 25 deletions include/compartment.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,30 +47,12 @@ comp_exec_out();
extern void
__clear_cache(void *, void *);

// Number of instructions to inject at intercepted function call point
// TODO ensure there is sufficient space for these, so we don't spill over
#define INTERCEPT_INSTR_COUNT 5

// Number of instructions required by the transition function
#define COMP_TRANS_FN_INSTR_CNT 4

extern void *__capability sealed_redirect_cap;
extern void *__capability comp_return_caps[2];

/* For a function to be intercepted, information required to insert the
* redirect code and perform redirection
*
* TODO recheck this is properly used, or re-design into a more light-weight
* approach with pre-given transition capabilities
*/
struct InterceptPatch
{
int *patch_addr;
int32_t instr[INTERCEPT_INSTR_COUNT];
uintptr_t comp_manager_cap_addr;
void *__capability manager_cap;
};

// Maximum size of an argument, in bytes
#define COMP_ARG_SIZE 8

Expand Down Expand Up @@ -210,20 +192,14 @@ struct Compartment

// Hardware info - maybe move
size_t page_size;

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

int
entry_point_cmp(const void *, const void *);
struct Compartment *
comp_init();
struct Compartment *
comp_from_elf(char *, char **, size_t, char **, void **, size_t, void *);
void
comp_add_intercept(struct Compartment *, uintptr_t, uintptr_t);
comp_from_elf(char *, char **, size_t, void *);
void
comp_map(struct Compartment *);
void
Expand Down
40 changes: 2 additions & 38 deletions include/intercept.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@

#include "cheriintrin.h"

#include "mem_mng.h"

// Forward declarations
struct Compartment;
extern struct Compartment *loaded_comp;
Expand All @@ -21,6 +19,8 @@ exec_comp(struct Compartment *, char *, char **);
struct Compartment *manager_get_compartment_by_id(size_t);

extern void *__capability manager_ddc;
extern void
comp_exec_out();

// Number of capabilities required to perform a transition
#define COMP_RETURN_CAPS_COUNT 2
Expand Down Expand Up @@ -55,43 +55,7 @@ intercept_wrapper();
void
setup_intercepts();

time_t
intercepted_time(time_t *);
FILE *
intercepted_fopen(const char *, const char *);
size_t
intercepted_fread(void *__restrict, size_t, size_t, FILE *__restrict);
size_t
intercepted_fwrite(void *__restrict, size_t, size_t, FILE *__restrict);
int
intercepted_fclose(FILE *);
int
intercepted_getc(FILE *);
int
intercepted_fputc(int, FILE *);
int
intercepted___srget(FILE *);

void *
my_realloc(void *, size_t);
void *my_malloc(size_t);
void
my_free(void *);
int
my_fprintf(FILE *, const char *, ...);

size_t
my_call_comp(size_t, char *, void *);
static const struct FuncIntercept to_intercept_funcs[] = {
/* Mem funcs */
{ "malloc", (void *) my_malloc, NULL },
{ "realloc", (void *) my_realloc, NULL },
{ "free", (void *) my_free, NULL },
};
//
// Functions to be intercepted and associated data
#define INTERCEPT_FUNC_COUNT \
sizeof(to_intercept_funcs) / sizeof(to_intercept_funcs[0])
extern struct FuncIntercept comp_intercept_funcs[INTERCEPT_FUNC_COUNT];

#endif // _INTERCEPT_H
2 changes: 0 additions & 2 deletions include/manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,4 @@ clean_compartment_config(struct CompEntryPointDef *, size_t);
* Memory allocation
******************************************************************************/

#include "mem_mng.h"

#endif // _MANAGER_H
35 changes: 0 additions & 35 deletions include/mem_mng.h

This file was deleted.

5 changes: 4 additions & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
# Compartment management library
add_library(chcomp STATIC
manager.c
mem_mng.c
compartment.c
intercept.c
transition.S
)
target_include_directories(chcomp PRIVATE ${INCLUDE_DIR} ${TOML_INCLUDE_DIR})
target_link_libraries(chcomp PRIVATE tomllib)

add_library(computils SHARED
comp_utils.c)
target_include_directories(computils PRIVATE ${INCLUDE_DIR})
46 changes: 46 additions & 0 deletions src/comp_utils.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#include "comp_utils.h"

static void *malloc_ptr;
static size_t heap_mem_left;

void *
malloc(size_t to_alloc)
{
if (!malloc_ptr)
{
void *__capability ddc = cheri_ddc_get();
malloc_ptr = (char *) cheri_address_get(ddc);
heap_mem_left = cheri_length_get(ddc) - cheri_offset_get(ddc);
}
if (to_alloc > heap_mem_left)
{
errx(1, "Insufficient heap space left.");
}
void *to_ret = malloc_ptr;
memset(to_ret, 0, to_alloc);
malloc_ptr = (char *) malloc_ptr + to_alloc;
heap_mem_left -= to_alloc;
return to_ret;
}

void
free(void *to_free)
{
// TODO temp usage for bump allocator implementation to satisfy compiler
to_free = to_free;
}

void *
calloc(size_t elem_count, size_t elem_size)
{
return malloc(elem_count * elem_size);
}

void *
realloc(void *to_realloc, size_t new_size)
{
// TODO temp usage for bump allocator implementation to satisfy compiler
to_realloc = to_realloc;

return malloc(new_size);
}
Loading

0 comments on commit 7df6fee

Please sign in to comment.