Skip to content

Commit

Permalink
compartment init: fix spurious UBSan error
Browse files Browse the repository at this point in the history
I couldn't find any way to write this in terms of void * or char * that made UBSan happy, so I think it's simply a matter of it not liking that we modify this memory at all

I've minimized the span we ignore to avoid hiding other potential UB
  • Loading branch information
fw-immunant committed Oct 3, 2023
1 parent e90436b commit 1400488
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion libia2/include/ia2_compartment_init.inc
Original file line number Diff line number Diff line change
Expand Up @@ -153,14 +153,20 @@ COMPARTMENT_IDENT(ia2_compartment_destructor)(void) {
static void (*const compartment_destructor_ptr)(void) =
COMPARTMENT_IDENT(__wrap_ia2_compartment_destructor);

/* UBSan doesn't like this access, so opt it out of sanitization. */
__attribute__((no_sanitize("undefined")))
static inline uint32_t ubsan_access_phdr_type(Elf64_Phdr *phdr, int i) {
return phdr[i].p_type;
}

void COMPARTMENT_IDENT(ia2_setup_destructors)(void) {
int res = 0;
Elf64_Ehdr *ehdr = &__ehdr_start;
Elf64_Phdr *phdr = (Elf64_Phdr *)((uint8_t *)ehdr + __ehdr_start.e_phoff);
assert(sizeof(Elf64_Phdr) == ehdr->e_phentsize);
Elf64_Phdr *dynamic_phdr = NULL;
for (int i = 0; i < ehdr->e_phnum; i++) {
if (phdr[i].p_type == PT_DYNAMIC) {
if (ubsan_access_phdr_type(phdr, i) == PT_DYNAMIC) {
dynamic_phdr = &phdr[i];
break;
}
Expand Down

0 comments on commit 1400488

Please sign in to comment.