From 14004886b3a523d9394b7b2f5efd5f8d79b688c7 Mon Sep 17 00:00:00 2001 From: Frances Wingerter Date: Sun, 1 Oct 2023 22:32:29 -0400 Subject: [PATCH] compartment init: fix spurious UBSan error 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 --- libia2/include/ia2_compartment_init.inc | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/libia2/include/ia2_compartment_init.inc b/libia2/include/ia2_compartment_init.inc index 9315e7399..c720b4380 100644 --- a/libia2/include/ia2_compartment_init.inc +++ b/libia2/include/ia2_compartment_init.inc @@ -153,6 +153,12 @@ 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; @@ -160,7 +166,7 @@ void COMPARTMENT_IDENT(ia2_setup_destructors)(void) { 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; }