summaryrefslogtreecommitdiff
path: root/src/malloc/malloc.c
diff options
context:
space:
mode:
authorRich Felker <dalias@aerifal.cx>2018-04-19 22:19:29 -0400
committerRich Felker <dalias@aerifal.cx>2018-04-19 22:22:11 -0400
commitb4b1e10364c8737a632be61582e05a8d3acf5690 (patch)
tree68c07893e646a5e94c47afb7f8b01317c143968b /src/malloc/malloc.c
parent72141795d4edd17f88da192447395a48444afa10 (diff)
downloadmusl-b4b1e10364c8737a632be61582e05a8d3acf5690.tar.gz
reintroduce hardening against partially-replaced allocator
commit 618b18c78e33acfe54a4434e91aa57b8e171df89 removed the previous detection and hardening since it was incorrect. commit 72141795d4edd17f88da192447395a48444afa10 already handled all that remained for hardening the static-linked case. in the dynamic-linked case, have the dynamic linker check whether malloc was replaced and make that information available. with these changes, the properties documented in commit c9f415d7ea2dace5bf77f6518b6afc36bb7a5732 are restored: if calloc is not provided, it will behave as malloc+memset, and any of the memalign-family functions not provided will fail with ENOMEM.
Diffstat (limited to 'src/malloc/malloc.c')
-rw-r--r--src/malloc/malloc.c13
1 files changed, 9 insertions, 4 deletions
diff --git a/src/malloc/malloc.c b/src/malloc/malloc.c
index 239ab9c6..d72883e1 100644
--- a/src/malloc/malloc.c
+++ b/src/malloc/malloc.c
@@ -20,6 +20,8 @@ static struct {
volatile int free_lock[2];
} mal;
+int __malloc_replaced;
+
/* Synchronization tools */
static inline void lock(volatile int *lk)
@@ -356,10 +358,13 @@ void *calloc(size_t m, size_t n)
}
n *= m;
void *p = malloc(n);
- if (!p || IS_MMAPPED(MEM_TO_CHUNK(p)))
- return p;
- if (n >= PAGE_SIZE)
- n = mal0_clear(p, PAGE_SIZE, n);
+ if (!p) return p;
+ if (!__malloc_replaced) {
+ if (IS_MMAPPED(MEM_TO_CHUNK(p)))
+ return p;
+ if (n >= PAGE_SIZE)
+ n = mal0_clear(p, PAGE_SIZE, n);
+ }
return memset(p, 0, n);
}