Skip to content

Commit 70a34d5

Browse files
perform size checks on memcpy/memmove/memset
Signed-off-by: Tavi <tavi@divested.dev> Co-authored-by: =?UTF-8?q?Christian=20G=C3=B6ttsche?= <cgzones@googlemail.com>
1 parent 4fe9018 commit 70a34d5

29 files changed

+728
-10
lines changed

Android.bp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ common_cflags = [
2828
"-DN_ARENA=1",
2929
"-DCONFIG_STATS=true",
3030
"-DCONFIG_SELF_INIT=false",
31+
"-DCONFIG_BLOCK_OPS_CHECK_SIZE=false",
3132
]
3233

3334
cc_defaults {

CREDITS

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,30 @@ h_malloc.c open-addressed hash table (regions_grow, regions_insert, regions_find
2323
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
2424
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
2525

26+
memcpy.c, memmove.c, memset.c and their wide variants:
27+
Copyright © 2005-2020 Rich Felker, et al.
28+
29+
Permission is hereby granted, free of charge, to any person obtaining
30+
a copy of this software and associated documentation files (the
31+
"Software"), to deal in the Software without restriction, including
32+
without limitation the rights to use, copy, modify, merge, publish,
33+
distribute, sublicense, and/or sell copies of the Software, and to
34+
permit persons to whom the Software is furnished to do so, subject to
35+
the following conditions:
36+
37+
The above copyright notice and this permission notice shall be
38+
included in all copies or substantial portions of the Software.
39+
40+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
41+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
42+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
43+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
44+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
45+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
46+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
47+
48+
Contributor list: https://git.musl-libc.org/cgit/musl/tree/COPYRIGHT
49+
2650
libdivide:
2751

2852
Copyright (C) 2010 - 2019 ridiculous_fish, <libdivide@ridiculousfish.com>

Makefile

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ CFLAGS := $(CFLAGS) -std=c17 $(SHARED_FLAGS) -Wmissing-prototypes -Wstrict-proto
3939
CXXFLAGS := $(CXXFLAGS) -std=c++17 -fsized-deallocation $(SHARED_FLAGS)
4040
LDFLAGS := $(LDFLAGS) -Wl,-O1,--as-needed,-z,defs,-z,relro,-z,now,-z,nodlopen,-z,text
4141

42-
SOURCES := chacha.c h_malloc.c memory.c pages.c random.c util.c
42+
SOURCES := chacha.c h_malloc.c memory.c pages.c random.c util.c memcpy.c memmove.c memset.c wmemcpy.c wmemmove.c wmemset.c
4343
OBJECTS := $(SOURCES:.c=.o)
4444

4545
ifeq ($(CONFIG_CXX_ALLOCATOR),true)
@@ -89,6 +89,10 @@ ifeq (,$(filter $(CONFIG_SELF_INIT),true false))
8989
$(error CONFIG_SELF_INIT must be true or false)
9090
endif
9191

92+
ifeq (,$(filter $(CONFIG_BLOCK_OPS_CHECK_SIZE),true false))
93+
$(error CONFIG_BLOCK_OPS_CHECK_SIZE must be true or false)
94+
endif
95+
9296
CPPFLAGS += \
9397
-DCONFIG_SEAL_METADATA=$(CONFIG_SEAL_METADATA) \
9498
-DZERO_ON_FREE=$(CONFIG_ZERO_ON_FREE) \
@@ -108,7 +112,8 @@ CPPFLAGS += \
108112
-DCONFIG_CLASS_REGION_SIZE=$(CONFIG_CLASS_REGION_SIZE) \
109113
-DN_ARENA=$(CONFIG_N_ARENA) \
110114
-DCONFIG_STATS=$(CONFIG_STATS) \
111-
-DCONFIG_SELF_INIT=$(CONFIG_SELF_INIT)
115+
-DCONFIG_SELF_INIT=$(CONFIG_SELF_INIT) \
116+
-DCONFIG_BLOCK_OPS_CHECK_SIZE=$(CONFIG_BLOCK_OPS_CHECK_SIZE)
112117

113118
$(OUT)/libhardened_malloc$(SUFFIX).so: $(OBJECTS) | $(OUT)
114119
$(CC) $(CFLAGS) $(LDFLAGS) -shared $^ $(LDLIBS) -o $@
@@ -118,7 +123,7 @@ $(OUT):
118123

119124
$(OUT)/chacha.o: chacha.c chacha.h util.h $(CONFIG_FILE) | $(OUT)
120125
$(COMPILE.c) $(OUTPUT_OPTION) $<
121-
$(OUT)/h_malloc.o: h_malloc.c include/h_malloc.h mutex.h memory.h pages.h random.h util.h $(CONFIG_FILE) | $(OUT)
126+
$(OUT)/h_malloc.o: h_malloc.c include/h_malloc.h mutex.h memory.h musl.h pages.h random.h util.h $(CONFIG_FILE) | $(OUT)
122127
$(COMPILE.c) $(OUTPUT_OPTION) $<
123128
$(OUT)/memory.o: memory.c memory.h util.h $(CONFIG_FILE) | $(OUT)
124129
$(COMPILE.c) $(OUTPUT_OPTION) $<
@@ -131,6 +136,19 @@ $(OUT)/random.o: random.c random.h chacha.h util.h $(CONFIG_FILE) | $(OUT)
131136
$(OUT)/util.o: util.c util.h $(CONFIG_FILE) | $(OUT)
132137
$(COMPILE.c) $(OUTPUT_OPTION) $<
133138

139+
$(OUT)/memcpy.o: memcpy.c musl.h $(CONFIG_FILE) | $(OUT)
140+
$(COMPILE.c) -Wno-cast-align $(OUTPUT_OPTION) $<
141+
$(OUT)/memmove.o: memmove.c musl.h $(CONFIG_FILE) | $(OUT)
142+
$(COMPILE.c) -Wno-cast-align $(OUTPUT_OPTION) $<
143+
$(OUT)/memset.o: memset.c musl.h $(CONFIG_FILE) | $(OUT)
144+
$(COMPILE.c) -Wno-cast-align $(OUTPUT_OPTION) $<
145+
$(OUT)/wmemcpy.o: wmemcpy.c musl.h $(CONFIG_FILE) | $(OUT)
146+
$(COMPILE.c) $(OUTPUT_OPTION) $<
147+
$(OUT)/wmemmove.o: wmemmove.c musl.h $(CONFIG_FILE) | $(OUT)
148+
$(COMPILE.c) $(OUTPUT_OPTION) $<
149+
$(OUT)/wmemset.o: wmemset.c musl.h $(CONFIG_FILE) | $(OUT)
150+
$(COMPILE.c) $(OUTPUT_OPTION) $<
151+
134152
check: tidy
135153

136154
tidy:

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,9 @@ The following boolean configuration options are available:
276276
hardware, which may become drastically lower in the future. Whether or not
277277
this feature is enabled, the metadata is all contained within an isolated
278278
memory region with high entropy random guard regions around it.
279+
* `CONFIG_BLOCK_OPS_CHECK_SIZE`: `true` or `false` (default) to ensure length
280+
parameter of the memcpy/memmove/memset block operations and their wide
281+
variants are within approximate bounds to minimize buffer overflows.
279282

280283
The following integer configuration options are available:
281284

config/default.mk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,4 @@ CONFIG_CLASS_REGION_SIZE := 34359738368 # 32GiB
2121
CONFIG_N_ARENA := 4
2222
CONFIG_STATS := false
2323
CONFIG_SELF_INIT := true
24+
CONFIG_BLOCK_OPS_CHECK_SIZE := true

config/light.mk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,4 @@ CONFIG_CLASS_REGION_SIZE := 34359738368 # 32GiB
2121
CONFIG_N_ARENA := 4
2222
CONFIG_STATS := false
2323
CONFIG_SELF_INIT := true
24+
CONFIG_BLOCK_OPS_CHECK_SIZE := false

h_malloc.c

Lines changed: 87 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "h_malloc.h"
1616
#include "memory.h"
1717
#include "memtag.h"
18+
#include "musl.h"
1819
#include "mutex.h"
1920
#include "pages.h"
2021
#include "random.h"
@@ -528,7 +529,7 @@ static void set_canary(UNUSED const struct slab_metadata *metadata, UNUSED void
528529
}
529530
#endif
530531

531-
memcpy((char *)p + size - canary_size, &metadata->canary_value, canary_size);
532+
h_memcpy_internal((char *)p + size - canary_size, &metadata->canary_value, canary_size);
532533
#endif
533534
}
534535

@@ -541,7 +542,7 @@ static void check_canary(UNUSED const struct slab_metadata *metadata, UNUSED con
541542
#endif
542543

543544
u64 canary_value;
544-
memcpy(&canary_value, (const char *)p + size - canary_size, canary_size);
545+
h_memcpy_internal(&canary_value, (const char *)p + size - canary_size, canary_size);
545546

546547
#ifdef HAS_ARM_MTE
547548
if (unlikely(canary_value == 0)) {
@@ -831,7 +832,7 @@ static inline void deallocate_small(void *p, const size_t *expected_size) {
831832
#endif
832833

833834
if (ZERO_ON_FREE && !skip_zero) {
834-
memset(p, 0, size - canary_size);
835+
h_memset_internal(p, 0, size - canary_size);
835836
}
836837
}
837838

@@ -1502,7 +1503,7 @@ EXPORT void *h_calloc(size_t nmemb, size_t size) {
15021503
total_size = adjust_size_for_canary(total_size);
15031504
void *p = alloc(total_size);
15041505
if (!ZERO_ON_FREE && likely(p != NULL) && total_size && total_size <= max_slab_size_class) {
1505-
memset(p, 0, total_size - canary_size);
1506+
h_memset_internal(p, 0, total_size - canary_size);
15061507
}
15071508
#ifdef HAS_ARM_MTE
15081509
// use an assert instead of adding a conditional to memset() above (freed memory is always
@@ -1624,7 +1625,7 @@ EXPORT void *h_realloc(void *old, size_t size) {
16241625
mutex_unlock(&ra->lock);
16251626

16261627
if (memory_remap_fixed(old, old_size, new, size)) {
1627-
memcpy(new, old, copy_size);
1628+
h_memcpy_internal(new, old, copy_size);
16281629
deallocate_pages(old, old_size, old_guard_size);
16291630
} else {
16301631
memory_unmap((char *)old - old_guard_size, old_guard_size);
@@ -1646,7 +1647,7 @@ EXPORT void *h_realloc(void *old, size_t size) {
16461647
if (copy_size > 0 && copy_size <= max_slab_size_class) {
16471648
copy_size -= canary_size;
16481649
}
1649-
memcpy(new, old_orig, copy_size);
1650+
h_memcpy_internal(new, old_orig, copy_size);
16501651
if (old_size <= max_slab_size_class) {
16511652
deallocate_small(old, NULL);
16521653
} else {
@@ -1874,6 +1875,86 @@ EXPORT size_t h_malloc_object_size_fast(const void *p) {
18741875
return SIZE_MAX;
18751876
}
18761877

1878+
#if CONFIG_BLOCK_OPS_CHECK_SIZE && !defined(HAS_ARM_MTE)
1879+
EXPORT void *memcpy(void *restrict dst, const void *restrict src, size_t len) {
1880+
if(dst == src || len == 0) {
1881+
return dst;
1882+
}
1883+
if (unlikely(dst < src + len && dst + len > src)) {
1884+
fatal_error("memcpy overlap");
1885+
}
1886+
if (unlikely(len > malloc_object_size(src))) {
1887+
fatal_error("memcpy read overflow");
1888+
}
1889+
if (unlikely(len > malloc_object_size(dst))) {
1890+
fatal_error("memcpy buffer overflow");
1891+
}
1892+
return musl_memcpy(dst, src, len);
1893+
}
1894+
1895+
EXPORT void *memmove(void *dst, const void *src, size_t len) {
1896+
if(dst == src || len == 0) {
1897+
return dst;
1898+
}
1899+
if (unlikely(len > malloc_object_size(src))) {
1900+
fatal_error("memmove read overflow");
1901+
}
1902+
if (unlikely(len > malloc_object_size(dst))) {
1903+
fatal_error("memmove buffer overflow");
1904+
}
1905+
return musl_memmove(dst, src, len);
1906+
}
1907+
1908+
EXPORT void *memset(void *dst, int value, size_t len) {
1909+
if(len == 0) {
1910+
return dst;
1911+
}
1912+
if (unlikely(len > malloc_object_size(dst))) {
1913+
fatal_error("memset buffer overflow");
1914+
}
1915+
return musl_memset(dst, value, len);
1916+
}
1917+
1918+
EXPORT wchar_t *wmemcpy(wchar_t *restrict dst, const wchar_t *restrict src, size_t len) {
1919+
if(dst == src || len == 0) {
1920+
return dst;
1921+
}
1922+
if (dst < src + len && dst + len > src) {
1923+
fatal_error("wmemcpy overlap");
1924+
}
1925+
if (len > malloc_object_size(src)) {
1926+
fatal_error("wmemcpy read overflow");
1927+
}
1928+
if (len > malloc_object_size(dst)) {
1929+
fatal_error("wmemcpy buffer overflow");
1930+
}
1931+
return musl_wmemcpy(dst, src, len);
1932+
}
1933+
1934+
EXPORT wchar_t *wmemmove(wchar_t *dst, const wchar_t *src, size_t len) {
1935+
if(dst == src || len == 0) {
1936+
return dst;
1937+
}
1938+
if (len > malloc_object_size(src)) {
1939+
fatal_error("wmemmove read overflow");
1940+
}
1941+
if (len > malloc_object_size(dst)) {
1942+
fatal_error("wmemmove buffer overflow");
1943+
}
1944+
return musl_wmemmove(dst, src, len);
1945+
}
1946+
1947+
EXPORT wchar_t *wmemset(wchar_t *dst, wchar_t value, size_t len) {
1948+
if(len == 0) {
1949+
return dst;
1950+
}
1951+
if (len > malloc_object_size(dst)) {
1952+
fatal_error("wmemset buffer overflow");
1953+
}
1954+
return musl_wmemset(dst, value, len);
1955+
}
1956+
#endif
1957+
18771958
EXPORT int h_mallopt(UNUSED int param, UNUSED int value) {
18781959
#ifdef __ANDROID__
18791960
if (param == M_PURGE) {

include/h_malloc.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,22 @@ __attribute__((malloc)) __attribute__((alloc_size(2))) __attribute__((alloc_alig
5555
void *h_aligned_alloc(size_t alignment, size_t size);
5656
void h_free(void *ptr);
5757

58+
#if CONFIG_BLOCK_OPS_CHECK_SIZE && !defined(HAS_ARM_MTE)
59+
void *memcpy(void *dst, const void *src, size_t len);
60+
void *memmove(void *dst, const void *src, size_t len);
61+
void *memset(void *dst, int value, size_t len);
62+
wchar_t *wmemcpy(wchar_t *dst, const wchar_t *src, size_t len);
63+
wchar_t *wmemmove(wchar_t *dst, const wchar_t *src, size_t len);
64+
wchar_t *wmemset(wchar_t *dst, wchar_t value, size_t len);
65+
#define h_memcpy_internal musl_memcpy
66+
#define h_memmove_internal musl_memmove
67+
#define h_memset_internal musl_memset
68+
#else
69+
#define h_memcpy_internal memcpy
70+
#define h_memmove_internal memmove
71+
#define h_memset_internal memset
72+
#endif
73+
5874
// POSIX
5975
int h_posix_memalign(void **memptr, size_t alignment, size_t size);
6076

0 commit comments

Comments
 (0)