fortify-headers

standalone fortify-source implementation
git clone git://git.2f30.org/fortify-headers.git
Log | Files | Refs | README | LICENSE

commit 6bb34c15b4d7989c5785577fbee72fdffa198d1f
parent b258495ebacc4a05c08fdf0ed1aa71ceb9e629e0
Author: sin <sin@2f30.org>
Date:   Wed Jan 28 15:36:44 +0000

Merge __foo_chk() into __fortify_foo()

Diffstat:
include/string.h | 153++++++++++++++++++-------------------------------------------------------------
1 file changed, 34 insertions(+), 119 deletions(-)
diff --git a/include/string.h b/include/string.h @@ -6,175 +6,112 @@ #if defined(_FORTIFY_SOURCE) && _FORTIFY_SOURCE > 0 && defined(__OPTIMIZE__) && __OPTIMIZE__ > 0 static inline __attribute__ ((always_inline)) -void *__memcpy_chk(void *__restrict dest, const void *__restrict src, size_t ssize, - size_t dsize) -{ - if (ssize > dsize) - __builtin_trap(); - return memcpy(dest, src, ssize); -} - -static inline __attribute__ ((always_inline)) void *__fortify_memcpy(void *__restrict dest, const void *__restrict src, size_t n) { size_t bos = __builtin_object_size(dest, 0); if (bos == (size_t)-1) return memcpy(dest, src, n); - if (__builtin_constant_p(n)) - if (n <= bos) - return memcpy(dest, src, n); - return __memcpy_chk(dest, src, n, bos); + if (__builtin_constant_p(n) && n > bos) + __builtin_trap(); + return memcpy(dest, src, n); } #undef memcpy #define memcpy(dest, src, n) __fortify_memcpy(dest, src, n) static inline __attribute__ ((always_inline)) -void *__memmove_chk(void *__restrict dest, const void *__restrict src, size_t ssize, - size_t dsize) -{ - if (ssize > dsize) - __builtin_trap(); - return memmove(dest, src, ssize); -} - -static inline __attribute__ ((always_inline)) void *__fortify_memmove(void *__restrict dest, const void *__restrict src, size_t n) { size_t bos = __builtin_object_size(dest, 0); if (bos == (size_t)-1) return memmove(dest, src, n); - if (__builtin_constant_p(n)) - if (n <= bos) - return memmove(dest, src, n); - return __memmove_chk(dest, src, n, bos); + if (__builtin_constant_p(n) && n > bos) + __builtin_trap(); + return memmove(dest, src, n); } #undef memmove #define memmove(dest, src, n) __fortify_memmove(dest, src, n) static inline __attribute__ ((always_inline)) -void *__memset_chk(void *dest, int c, size_t n, size_t dsize) -{ - if (n > dsize) - __builtin_trap(); - return memset(dest, c, n); -} - -static inline __attribute__ ((always_inline)) void *__fortify_memset(void *dest, int c, size_t n) { size_t bos = __builtin_object_size(dest, 0); if (bos == (size_t)-1) return memset(dest, c, n); - if (__builtin_constant_p(n)) - if (n <= bos) - return memset(dest, c, n); - return __memset_chk(dest, c, n, bos); + if (__builtin_constant_p(n) && n > bos) + __builtin_trap(); + return memset(dest, c, n); } #undef memset #define memset(dest, src, n) __fortify_memset(dest, src, n) static inline __attribute__ ((always_inline)) -char *__strcat_chk(char *__restrict dest, const char *__restrict src, size_t n) -{ - size_t slen = strlen(src); - size_t dlen = strlen(dest); - - if (slen + dlen + 1 > n) - __builtin_trap(); - return strcat(dest, src); -} - -static inline __attribute__ ((always_inline)) char *__fortify_strcat(char *__restrict dest, const char *__restrict src) { size_t bos = __builtin_object_size(dest, 0); if (bos == (size_t)-1) return strcat(dest, src); - return __strcat_chk(dest, src, bos); + if (strlen(src) + strlen(dest) + 1 > bos) + __builtin_trap(); + return strcat(dest, src); } #undef strcat #define strcat(dest, src) __fortify_strcat(dest, src) static inline __attribute__ ((always_inline)) -char *__strcpy_chk(char *__restrict dest, const char *__restrict src, size_t n) -{ - size_t slen = strlen(src); - - if (slen + 1 > n) - __builtin_trap(); - return strcpy(dest, src); -} - -static inline __attribute__ ((always_inline)) char *__fortify_strcpy(char *__restrict dest, const char *__restrict src) { size_t bos = __builtin_object_size(dest, 0); if (bos == (size_t)-1) return strcpy(dest, src); - return __strcpy_chk(dest, src, bos); + if (strlen(src) + 1 > bos) + __builtin_trap(); + return strcpy(dest, src); } #undef strcpy #define strcpy(dest, src) __fortify_strcpy(dest, src) static inline __attribute__ ((always_inline)) -char *__strncat_chk(char *__restrict dest, const char *__restrict src, size_t n, - size_t dsize) -{ - size_t slen = strlen(src); - size_t dlen = strlen(dest); - - if (slen > n) slen = n; - if (slen + dlen + 1 > dsize) - __builtin_trap(); - return strncat(dest, src, n); -} - -static inline __attribute__ ((always_inline)) char *__fortify_strncat(char *__restrict dest, const char *__restrict src, size_t n) { size_t bos = __builtin_object_size(dest, 0); + size_t slen, dlen; if (bos == (size_t)-1) return strncat(dest, src, n); - if (__builtin_constant_p(n)) - if (n <= bos) - return strncat(dest, src, n); - return __strncat_chk(dest, src, n, bos); + if (__builtin_constant_p(n) && n > bos) { + slen = strlen(src); + dlen = strlen(dest); + if (slen > n) + slen = n; + if (slen + dlen + 1 > bos) + __builtin_trap(); + } + return strncat(dest, src, n); } #undef strncat #define strncat(dest, src, n) __fortify_strcat(dest, src, n) static inline __attribute__ ((always_inline)) -char *__strncpy_chk(char *__restrict dest, const char *__restrict src, size_t n, - size_t dsize) -{ - if (n > dsize) - __builtin_trap(); - return strncpy(dest, src, n); -} - -static inline __attribute__ ((always_inline)) char *__fortify_strncpy(char *__restrict dest, const char *__restrict src, size_t n) { size_t bos = __builtin_object_size(dest, 0); if (bos == (size_t)-1) return strncpy(dest, src, n); - if (__builtin_constant_p(n)) - if (n <= bos) - return strncpy(dest, src, n); - return __strncpy_chk(dest, src, n, bos); + if (__builtin_constant_p(n) && n > bos) + __builtin_trap(); + return strncpy(dest, src, n); } #undef strncpy @@ -182,52 +119,30 @@ char *__fortify_strncpy(char *__restrict dest, const char *__restrict src, size_ #if defined(_GNU_SOURCE) || defined(_BSD_SOURCE) static inline __attribute__ ((always_inline)) -size_t __strlcat_chk(char *__restrict dest, const char *__restrict src, size_t n, - size_t dsize) -{ - if (n > dsize) - __builtin_trap(); - return strlcat(dest, src, n); -} - -static inline __attribute__ ((always_inline)) size_t __fortify_strlcat(char *__restrict dest, const char *__restrict src, size_t n) { size_t bos = __builtin_object_size(dest, 0); if (bos == (size_t)-1) return strlcat(dest, src, n); - if (__builtin_constant_p(n)) { - if (n <= bos) - return strlcat(dest, src, n); - } - return __strlcat_chk(dest, src, n, bos); + if (__builtin_constant_p(n) && n > bos) + __builtin_trap(); + return strlcat(dest, src, n); } #undef strlcat #define strlcat(dest, src, n) __fortify_strlcat(dest, src, n) static inline __attribute__ ((always_inline)) -size_t __strlcpy_chk(char *__restrict dest, const char *__restrict src, size_t n, - size_t dsize) -{ - if (n > dsize) - __builtin_trap(); - return strlcpy(dest, src, n); -} - -static inline __attribute__ ((always_inline)) size_t __fortify_strlcpy(char *__restrict dest, const char *__restrict src, size_t n) { size_t bos = __builtin_object_size(dest, 0); if (bos == (size_t)-1) return strlcpy(dest, src, n); - if (__builtin_constant_p(n)) { - if (n <= bos) - return strlcpy(dest, src, n); - } - return __strlcpy_chk(dest, src, n, bos); + if (__builtin_constant_p(n) && n > bos) + __builtin_trap(); + return strlcpy(dest, src, n); } #undef strlcpy