From 5e0ea3e3fca14587b4494f604c887761dc4f5a17 Mon Sep 17 00:00:00 2001 From: jvoisin Date: Thu, 3 Aug 2023 19:41:27 +0200 Subject: Add hardening for memchr and memrchr --- include/string.h | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/include/string.h b/include/string.h index 36f1df2..42a3aaf 100644 --- a/include/string.h +++ b/include/string.h @@ -30,6 +30,7 @@ extern "C" { #endif #undef memcpy +#undef memchr #undef memmove #undef memset #undef strcat @@ -80,6 +81,29 @@ _FORTIFY_FN(memset) void *memset(void * _FORTIFY_POS0 __d, int __c, size_t __n) return __builtin_memset(__d, __c, __n); } +__access(read_only, 1, 3) +_FORTIFY_FN(memchr) void *memchr(const void * _FORTIFY_POS0 __d, int __c, size_t __n) +{ + size_t __b = __bos(__d, 0); + + if (__n > __b) + __builtin_trap(); + return __builtin_memchr(__d, __c, __n); +} + +#if defined(_GNU_SOURCE) +#undef memrchr +__access(read_only, 1, 3) +_FORTIFY_FN(memrchr) void *memrchr(const void * _FORTIFY_POS0 __d, int __c, size_t __n) +{ + size_t __b = __bos(__d, 0); + + if (__n > __b) + __builtin_trap(); + return __builtin_memrchr(__d, __c, __n); +} +#endif + #if defined(_POSIX_SOURCE) || defined(_POSIX_C_SOURCE) \ || defined(_XOPEN_SOURCE) || defined(_GNU_SOURCE) \ || defined(_BSD_SOURCE) -- cgit v1.3