DivestOS/Patches/Common/android_external_hardened_malloc/0001-Broken_Cameras-1.patch

103 lines
3.5 KiB
Diff
Raw Normal View History

From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Daniel Micay <danielmicay@gmail.com>
Date: Tue, 1 Jan 2019 14:45:27 -0500
Subject: [PATCH] workarounds for Pixel 3 SoC era camera driver bugs
---
h_malloc.c | 33 ++++++++++++++++++++++++++++-----
1 file changed, 28 insertions(+), 5 deletions(-)
diff --git a/h_malloc.c b/h_malloc.c
index 15be0a2..3fa9ed7 100644
--- a/h_malloc.c
+++ b/h_malloc.c
@@ -80,6 +80,9 @@ static union {
#ifdef MEMTAG
bool is_memtag_disabled;
#endif
+ bool zero_on_free;
+ bool purge_slabs;
+ bool region_quarantine_protect;
};
char padding[PAGE_SIZE];
} ro __attribute__((aligned(PAGE_SIZE)));
@@ -465,7 +468,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) {
- if (!WRITE_AFTER_FREE_CHECK) {
+ if (!WRITE_AFTER_FREE_CHECK || !ro.zero_on_free) {
return;
}
@@ -812,7 +815,7 @@ static inline void deallocate_small(void *p, const size_t *expected_size) {
}
#endif
- if (ZERO_ON_FREE && !skip_zero) {
+ if (ro.zero_on_free && !skip_zero) {
memset(p, 0, size - canary_size);
}
}
@@ -890,7 +893,7 @@ static inline void deallocate_small(void *p, const size_t *expected_size) {
if (c->empty_slabs_total + slab_size > max_empty_slabs_total) {
int saved_errno = errno;
- if (!memory_map_fixed(slab, slab_size)) {
+ if (ro.purge_slabs && !memory_map_fixed(slab, slab_size)) {
label_slab(slab, slab_size, class);
stats_slab_deallocate(c, slab_size);
enqueue_free_slab(c, metadata);
@@ -976,7 +979,7 @@ static void regions_quarantine_deallocate_pages(void *p, size_t size, size_t gua
return;
}
- if (unlikely(memory_map_fixed(p, size))) {
+ if (!ro.region_quarantine_protect || unlikely(memory_map_fixed(p, size))) {
memory_purge(p, size);
} else {
memory_set_name(p, size, "malloc large quarantine");
@@ -1192,6 +1195,21 @@ 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;
+ }
+}
+
static struct mutex init_lock = MUTEX_INITIALIZER;
COLD static void init_slow_path(void) {
@@ -1207,6 +1225,11 @@ COLD static void init_slow_path(void) {
ro.metadata_pkey = pkey_alloc(0, 0);
#endif
+ ro.purge_slabs = true;
+ ro.zero_on_free = ZERO_ON_FREE;
+ ro.region_quarantine_protect = true;
+ handle_bugs();
+
if (unlikely(sysconf(_SC_PAGESIZE) != PAGE_SIZE)) {
fatal_error("runtime page size does not match compile-time page size which is not supported");
}
@@ -1491,7 +1514,7 @@ EXPORT void *h_calloc(size_t nmemb, size_t size) {
}
total_size = adjust_size_for_canary(total_size);
void *p = alloc(total_size);
- 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);
}
#ifdef HAS_ARM_MTE