diff options
| author | jvoisin | 2023-08-03 19:41:27 +0200 |
|---|---|---|
| committer | jvoisin | 2023-08-03 19:54:20 +0200 |
| commit | 5e0ea3e3fca14587b4494f604c887761dc4f5a17 (patch) | |
| tree | ecbc87102498ae4b1ff8fa9d7afd0b4145a377a9 | |
| parent | 9aa4490263ead62dc545cf0e8b83e0ef77eb7a6e (diff) | |
Add hardening for memchr and memrchr
| -rw-r--r-- | include/string.h | 24 |
1 files changed, 24 insertions, 0 deletions
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" { | |||
| 30 | #endif | 30 | #endif |
| 31 | 31 | ||
| 32 | #undef memcpy | 32 | #undef memcpy |
| 33 | #undef memchr | ||
| 33 | #undef memmove | 34 | #undef memmove |
| 34 | #undef memset | 35 | #undef memset |
| 35 | #undef strcat | 36 | #undef strcat |
| @@ -80,6 +81,29 @@ _FORTIFY_FN(memset) void *memset(void * _FORTIFY_POS0 __d, int __c, size_t __n) | |||
| 80 | return __builtin_memset(__d, __c, __n); | 81 | return __builtin_memset(__d, __c, __n); |
| 81 | } | 82 | } |
| 82 | 83 | ||
| 84 | __access(read_only, 1, 3) | ||
| 85 | _FORTIFY_FN(memchr) void *memchr(const void * _FORTIFY_POS0 __d, int __c, size_t __n) | ||
| 86 | { | ||
| 87 | size_t __b = __bos(__d, 0); | ||
| 88 | |||
| 89 | if (__n > __b) | ||
| 90 | __builtin_trap(); | ||
| 91 | return __builtin_memchr(__d, __c, __n); | ||
| 92 | } | ||
| 93 | |||
| 94 | #if defined(_GNU_SOURCE) | ||
| 95 | #undef memrchr | ||
| 96 | __access(read_only, 1, 3) | ||
| 97 | _FORTIFY_FN(memrchr) void *memrchr(const void * _FORTIFY_POS0 __d, int __c, size_t __n) | ||
| 98 | { | ||
| 99 | size_t __b = __bos(__d, 0); | ||
| 100 | |||
| 101 | if (__n > __b) | ||
| 102 | __builtin_trap(); | ||
| 103 | return __builtin_memrchr(__d, __c, __n); | ||
| 104 | } | ||
| 105 | #endif | ||
| 106 | |||
| 83 | #if defined(_POSIX_SOURCE) || defined(_POSIX_C_SOURCE) \ | 107 | #if defined(_POSIX_SOURCE) || defined(_POSIX_C_SOURCE) \ |
| 84 | || defined(_XOPEN_SOURCE) || defined(_GNU_SOURCE) \ | 108 | || defined(_XOPEN_SOURCE) || defined(_GNU_SOURCE) \ |
| 85 | || defined(_BSD_SOURCE) | 109 | || defined(_BSD_SOURCE) |
