temporary workarounds for bugs

RQ1A.210205.004.2021.02.19.15
Daniel Micay 2019-01-01 14:45:27 -05:00
parent ddd14bc421
commit 1383b1629c
1 changed files with 43 additions and 8 deletions

View File

@ -76,6 +76,10 @@ static union {
#ifdef USE_PKEY #ifdef USE_PKEY
int metadata_pkey; int metadata_pkey;
#endif #endif
bool zero_on_free;
bool purge_slabs;
bool region_quarantine_protect;
bool slot_randomize;
}; };
char padding[PAGE_SIZE]; char padding[PAGE_SIZE];
} ro __attribute__((aligned(PAGE_SIZE))); } ro __attribute__((aligned(PAGE_SIZE)));
@ -347,7 +351,7 @@ static u64 get_mask(size_t slots) {
} }
static size_t get_free_slot(struct random_state *rng, size_t slots, struct slab_metadata *metadata) { static size_t get_free_slot(struct random_state *rng, size_t slots, struct slab_metadata *metadata) {
if (SLOT_RANDOMIZE) { if (ro.slot_randomize) {
// randomize start location for linear search (uniform random choice is too slow) // randomize start location for linear search (uniform random choice is too slow)
unsigned random_index = get_random_u16_uniform(rng, slots); unsigned random_index = get_random_u16_uniform(rng, slots);
unsigned first_bitmap = random_index / 64; unsigned first_bitmap = random_index / 64;
@ -429,7 +433,7 @@ static void *slot_pointer(size_t size, void *slab, size_t slot) {
} }
static void write_after_free_check(const char *p, size_t size) { static void write_after_free_check(const char *p, size_t size) {
if (!WRITE_AFTER_FREE_CHECK) { if (!WRITE_AFTER_FREE_CHECK || !ro.zero_on_free) {
return; return;
} }
@ -671,7 +675,7 @@ static inline void deallocate_small(void *p, const size_t *expected_size) {
} }
} }
if (ZERO_ON_FREE) { if (ro.zero_on_free) {
memset(p, 0, size - canary_size); memset(p, 0, size - canary_size);
} }
} }
@ -748,7 +752,7 @@ static inline void deallocate_small(void *p, const size_t *expected_size) {
metadata->prev = NULL; metadata->prev = NULL;
if (c->empty_slabs_total + slab_size > max_empty_slabs_total) { if (c->empty_slabs_total + slab_size > max_empty_slabs_total) {
if (!memory_map_fixed(slab, slab_size)) { if (ro.purge_slabs && !memory_map_fixed(slab, slab_size)) {
label_slab(slab, slab_size, class); label_slab(slab, slab_size, class);
stats_slab_deallocate(c, slab_size); stats_slab_deallocate(c, slab_size);
enqueue_free_slab(c, metadata); enqueue_free_slab(c, metadata);
@ -832,10 +836,14 @@ static void regions_quarantine_deallocate_pages(void *p, size_t size, size_t gua
return; return;
} }
if (ro.region_quarantine_protect) {
if (unlikely(memory_map_fixed(p, size))) { if (unlikely(memory_map_fixed(p, size))) {
deallocate_pages(p, size, guard_size); deallocate_pages(p, size, guard_size);
return; return;
} }
} else {
madvise(p, size, MADV_DONTNEED);
}
memory_set_name(p, size, "malloc large quarantine"); memory_set_name(p, size, "malloc large quarantine");
struct quarantine_info target = struct quarantine_info target =
@ -1048,6 +1056,27 @@ static inline void enforce_init(void) {
} }
} }
COLD static void handle_bugs(void) {
char path[256];
if (readlink("/proc/self/exe", path, sizeof(path)) == -1) {
return;
}
// Pixel 3, Pixel 3 XL, Pixel 3a and Pixel 3a XL camera provider
const char camera_provider[] = "/vendor/bin/hw/android.hardware.camera.provider@2.4-service_64";
if (strcmp(camera_provider, path) == 0) {
ro.zero_on_free = false;
ro.purge_slabs = false;
ro.region_quarantine_protect = false;
}
// DeviceDescriptor sorting wrongly relies on malloc addresses
const char audio_service[] = "/system/bin/audioserver";
if (strcmp(audio_service, path) == 0) {
ro.slot_randomize = false;
}
}
COLD static void init_slow_path(void) { COLD static void init_slow_path(void) {
static struct mutex lock = MUTEX_INITIALIZER; static struct mutex lock = MUTEX_INITIALIZER;
@ -1062,6 +1091,12 @@ COLD static void init_slow_path(void) {
ro.metadata_pkey = pkey_alloc(0, 0); ro.metadata_pkey = pkey_alloc(0, 0);
#endif #endif
ro.purge_slabs = true;
ro.zero_on_free = ZERO_ON_FREE;
ro.region_quarantine_protect = true;
ro.slot_randomize = SLOT_RANDOMIZE;
handle_bugs();
if (sysconf(_SC_PAGESIZE) != PAGE_SIZE) { if (sysconf(_SC_PAGESIZE) != PAGE_SIZE) {
fatal_error("runtime page size does not match compile-time page size which is not supported"); fatal_error("runtime page size does not match compile-time page size which is not supported");
} }
@ -1334,7 +1369,7 @@ EXPORT void *h_calloc(size_t nmemb, size_t size) {
total_size = adjust_size_for_canaries(total_size); total_size = adjust_size_for_canaries(total_size);
void *p = allocate(arena, total_size); void *p = allocate(arena, total_size);
thread_seal_metadata(); thread_seal_metadata();
if (!ZERO_ON_FREE && likely(p != NULL) && total_size && total_size <= MAX_SLAB_SIZE_CLASS) { if (!ro.zero_on_free && likely(p != NULL) && total_size && total_size <= MAX_SLAB_SIZE_CLASS) {
memset(p, 0, total_size - canary_size); memset(p, 0, total_size - canary_size);
} }
return p; return p;