mirror of
https://github.com/GrapheneOS/hardened_malloc.git
synced 2025-11-04 17:56:33 +01:00
override memcpy and perform sanity checks
Signed-off-by: Tavi <tavi@divested.dev>
This commit is contained in:
parent
4fe9018b6f
commit
39b1b13bce
2 changed files with 23 additions and 0 deletions
21
h_malloc.c
21
h_malloc.c
|
|
@ -1874,6 +1874,27 @@ EXPORT size_t h_malloc_object_size_fast(const void *p) {
|
|||
return SIZE_MAX;
|
||||
}
|
||||
|
||||
EXPORT void *h_memcpy(void *dst, const void *src, size_t len) {
|
||||
if (len > malloc_object_size(src)) {
|
||||
fatal_error("memcpy: length greater than source");
|
||||
}
|
||||
if (len > malloc_object_size(dst)) {
|
||||
fatal_error("memcpy: destination too small");
|
||||
}
|
||||
|
||||
if (dst == src) {
|
||||
return dst;
|
||||
}
|
||||
|
||||
char *d = (char *)dst;
|
||||
const char *s = (const char *)src;
|
||||
for (size_t i = 0; i < len; ++i) {
|
||||
d[i] = s[i];
|
||||
}
|
||||
|
||||
return dst;
|
||||
}
|
||||
|
||||
EXPORT int h_mallopt(UNUSED int param, UNUSED int value) {
|
||||
#ifdef __ANDROID__
|
||||
if (param == M_PURGE) {
|
||||
|
|
|
|||
|
|
@ -15,6 +15,7 @@ extern "C" {
|
|||
#define h_realloc realloc
|
||||
#define h_aligned_alloc aligned_alloc
|
||||
#define h_free free
|
||||
#define h_memcpy memcpy
|
||||
|
||||
#define h_posix_memalign posix_memalign
|
||||
|
||||
|
|
@ -54,6 +55,7 @@ __attribute__((alloc_size(2))) void *h_realloc(void *ptr, size_t size);
|
|||
__attribute__((malloc)) __attribute__((alloc_size(2))) __attribute__((alloc_align(1)))
|
||||
void *h_aligned_alloc(size_t alignment, size_t size);
|
||||
void h_free(void *ptr);
|
||||
void *h_memcpy(void *dstpp, const void *srcpp, size_t len);
|
||||
|
||||
// POSIX
|
||||
int h_posix_memalign(void **memptr, size_t alignment, size_t size);
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue