Skip to content

Commit

Permalink
c18n: Allow GDB to access compartment data
Browse files Browse the repository at this point in the history
Extend struct r_debug to contain a pointer to RTLD's array of
compartments and count of elements.  Export the size of struct compart
so a debugger can walk the array.

Co-authored-by: John Baldwin <[email protected]>
  • Loading branch information
dpgao and bsdjhb committed Apr 18, 2024
1 parent af5403b commit 0a35808
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 2 deletions.
2 changes: 2 additions & 0 deletions libexec/rtld-elf/Symbol-c18n.map
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
FBSDprivate_1.0 {
r_debug_comparts_state;
_compart_name_off;
_rtld_thread_start_init;
_rtld_thread_start;
_rtld_thr_exit;
Expand Down
27 changes: 25 additions & 2 deletions libexec/rtld-elf/rtld_c18n.c
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,25 @@ struct compart {
bool restrict_imports;
};

extern int _compart_size;

Check failure on line 155 in libexec/rtld-elf/rtld_c18n.c

View workflow job for this annotation

GitHub Actions / Style Checker

externs should be avoided in .c files
int _compart_size = sizeof(struct compart);

extern struct r_debug r_debug;

Check failure on line 158 in libexec/rtld-elf/rtld_c18n.c

View workflow job for this annotation

GitHub Actions / Style Checker

externs should be avoided in .c files

void

Check failure on line 160 in libexec/rtld-elf/rtld_c18n.c

View workflow job for this annotation

GitHub Actions / Style Checker

externs should be avoided in .c files
r_debug_comparts_state(struct r_debug *, struct compart *);
void
r_debug_comparts_state(struct r_debug *rd __unused, struct compart *m __unused)
{
/*
* See r_debug_state().
*/
__compiler_membar();
}

#define GDB_COMPARTS_STATE(s,m) \

Check failure on line 171 in libexec/rtld-elf/rtld_c18n.c

View workflow job for this annotation

GitHub Actions / Style Checker

space required after that ',' (ctx:VxV)
r_debug.r_comparts_state = s; r_debug_comparts_state(&r_debug, m);

/*
* A pseudo-compartment that encompasses all compartments.
*/
Expand Down Expand Up @@ -183,7 +202,7 @@ comparts_data_expand(compart_id_t capacity)
data = realloc(comparts.data, sizeof(*data) * capacity);
if (data == NULL)
rtld_fatal("realloc failed");
comparts.data = data;
comparts.data = r_debug.r_comparts = data;
comparts.capacity = capacity;
}

Expand All @@ -198,10 +217,13 @@ comparts_data_add(const char *name)
if (comparts.size == comparts.capacity)
comparts_data_expand(comparts.capacity * 2);

GDB_COMPARTS_STATE(RCT_ADD, NULL);
com = &comparts.data[comparts.size++];
*com = (struct compart) {
.name = name
};
r_debug.r_comparts_size = comparts.size;
GDB_COMPARTS_STATE(RCT_CONSISTENT, com);

return (com);
}
Expand Down Expand Up @@ -1496,7 +1518,8 @@ c18n_init(Obj_Entry *obj_rtld)
*/
data = xmalloc(sizeof(*data) * comparts.capacity);
memcpy(data, comparts.data, sizeof(*comparts.data) * comparts.capacity);
comparts.data = data;
comparts.data = r_debug.r_comparts = data;
r_debug.r_comparts_size = comparts.size;

/*
* Load the default policy
Expand Down
8 changes: 8 additions & 0 deletions sys/sys/link_elf.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,14 @@ struct r_debug {
RT_DELETE /* removing a shared library */
} r_state;
void *r_ldbase; /* Base address of rtld */
#if defined(IN_RTLD) && defined(__CHERI_PURE_CAPABILITY__) && defined(RTLD_SANDBOX)

Check warning on line 77 in sys/sys/link_elf.h

View workflow job for this annotation

GitHub Actions / Style Checker

line over 80 characters
enum {
RCT_CONSISTENT, /* vector is stable */
RCT_ADD, /* adding a compartment */
} r_comparts_state;
int r_comparts_size;
void *r_comparts; /* struct compart [] */
#endif
};

#define R_DEBUG_VERSION 1
Expand Down

0 comments on commit 0a35808

Please sign in to comment.