From 9f8c543dc81f0c4239acae6713f5414eb7dc681d Mon Sep 17 00:00:00 2001 From: sin Date: Fri, 13 Mar 2015 11:00:46 +0000 Subject: Rework fortify implementation to use extern inline Overriding functions with macros is legal in C but a lot of software is not prepared for it. Use the extern inline method to achieve the same result. --- include/string.h | 151 ++++++++++++++++++++++++++----------------------------- 1 file changed, 72 insertions(+), 79 deletions(-) (limited to 'include/string.h') diff --git a/include/string.h b/include/string.h index 65df3a1..02e931e 100644 --- a/include/string.h +++ b/include/string.h @@ -6,10 +6,20 @@ #if defined(_FORTIFY_SOURCE) && _FORTIFY_SOURCE > 0 && defined(__OPTIMIZE__) && __OPTIMIZE__ > 0 #ifndef __cplusplus +#undef memcpy +#undef memmove +#undef memset +#undef stpcpy +#undef stpncpy +#undef strcat +#undef strcpy +#undef strncat +#undef strncpy -static inline __attribute__ ((always_inline)) -void * -__fortify_memcpy(void *dest, const void *src, size_t n) +extern void *__memcpy_orig(void *, const void *, size_t) + __asm__(__USER_LABEL_PREFIX__ "memcpy"); +extern __inline __attribute__((__always_inline__,__gnu_inline__)) +void *memcpy(void *dest, const void *src, size_t n) { size_t bos = __builtin_object_size(dest, 0); char *d = dest; @@ -22,78 +32,85 @@ __fortify_memcpy(void *dest, const void *src, size_t n) __builtin_trap(); if (n > bos) __builtin_trap(); - return memcpy(dest, src, n); + return __memcpy_orig(dest, src, n); } -static inline __attribute__ ((always_inline)) -void * -__fortify_memmove(void *dest, const void *src, size_t n) +extern void *__memmove_orig(void *, const void *, size_t) + __asm__(__USER_LABEL_PREFIX__ "memmove"); +extern __inline __attribute__((__always_inline__,__gnu_inline__)) +void *memmove(void *dest, const void *src, size_t n) { size_t bos = __builtin_object_size(dest, 0); if (n > bos) __builtin_trap(); - return memmove(dest, src, n); + return __memmove_orig(dest, src, n); } -static inline __attribute__ ((always_inline)) -void * -__fortify_memset(void *dest, int c, size_t n) +extern void *__memset_orig(void *, int, size_t) + __asm__(__USER_LABEL_PREFIX__ "memset"); +extern __inline __attribute__((__always_inline__,__gnu_inline__)) +void *memset(void *dest, int c, size_t n) { size_t bos = __builtin_object_size(dest, 0); if (n > bos) __builtin_trap(); - return memset(dest, c, n); + return __memset_orig(dest, c, n); } -static inline __attribute__ ((always_inline)) -char * -__fortify_stpcpy(char *dest, const char *src) +extern char *__stpcpy_orig(char *, const char *) + __asm__(__USER_LABEL_PREFIX__ "stpcpy"); +extern __inline __attribute__((__always_inline__,__gnu_inline__)) +char *stpcpy(char *dest, const char *src) { size_t bos = __builtin_object_size(dest, 0); if (strlen(src) + 1 > bos) __builtin_trap(); - return stpcpy(dest, src); + return __stpcpy_orig(dest, src); } -static inline __attribute__ ((always_inline)) -char * -__fortify_stpncpy(char *dest, const char *src, size_t n) +extern char *__stpncpy_orig(char *, const char *, size_t) + __asm__(__USER_LABEL_PREFIX__ "stpncpy"); +extern __inline __attribute__((__always_inline__,__gnu_inline__)) +char *stpncpy(char *dest, const char *src, size_t n) { size_t bos = __builtin_object_size(dest, 0); if (n > bos) __builtin_trap(); - return stpncpy(dest, src, n); + return __stpncpy_orig(dest, src, n); } -static inline __attribute__ ((always_inline)) -char * -__fortify_strcat(char *dest, const char *src) +extern char *__strcat_orig(char *, const char *) + __asm__(__USER_LABEL_PREFIX__ "strcat"); +extern __inline __attribute__((__always_inline__,__gnu_inline__)) +char *strcat(char *dest, const char *src) { size_t bos = __builtin_object_size(dest, 0); if (strlen(src) + strlen(dest) + 1 > bos) __builtin_trap(); - return strcat(dest, src); + return __strcat_orig(dest, src); } -static inline __attribute__ ((always_inline)) -char * -__fortify_strcpy(char *dest, const char *src) +extern char *__strcpy_orig(char *, const char *) + __asm__(__USER_LABEL_PREFIX__ "strcpy"); +extern __inline __attribute__((__always_inline__,__gnu_inline__)) +char *strcpy(char *dest, const char *src) { size_t bos = __builtin_object_size(dest, 0); if (strlen(src) + 1 > bos) __builtin_trap(); - return strcpy(dest, src); + return __strcpy_orig(dest, src); } -static inline __attribute__ ((always_inline)) -char * -__fortify_strncat(char *dest, const char *src, size_t n) +extern char *__strncat_orig(char *, const char *, size_t) + __asm__(__USER_LABEL_PREFIX__ "strncat"); +extern __inline __attribute__((__always_inline__,__gnu_inline__)) +char *strncat(char *dest, const char *src, size_t n) { size_t bos = __builtin_object_size(dest, 0); size_t slen, dlen; @@ -106,88 +123,64 @@ __fortify_strncat(char *dest, const char *src, size_t n) if (slen + dlen + 1 > bos) __builtin_trap(); } - return strncat(dest, src, n); + return __strncat_orig(dest, src, n); } -static inline __attribute__ ((always_inline)) -char * -__fortify_strncpy(char *dest, const char *src, size_t n) +extern char *__strncpy_orig(char *, const char *, size_t) + __asm__(__USER_LABEL_PREFIX__ "strncpy"); +extern __inline __attribute__((__always_inline__,__gnu_inline__)) +char *strncpy(char *dest, const char *src, size_t n) { size_t bos = __builtin_object_size(dest, 0); if (n > bos) __builtin_trap(); - return strncpy(dest, src, n); + return __strncpy_orig(dest, src, n); } #ifdef _GNU_SOURCE -static inline __attribute__ ((always_inline)) -void * -__fortify_mempcpy(void *dest, const void *src, size_t n) +#undef mempcpy +extern void *__mempcpy_orig(void *, const void *, size_t n) + __asm__(__USER_LABEL_PREFIX__ "mempcpy"); +extern __inline __attribute__((__always_inline__,__gnu_inline__)) +void *mempcpy(void *dest, const void *src, size_t n) { size_t bos = __builtin_object_size(dest, 0); if (n > bos) __builtin_trap(); - return mempcpy(dest, src, n); + return __mempcpy_orig(dest, src, n); } #endif #if defined(_GNU_SOURCE) || defined(_BSD_SOURCE) -static inline __attribute__ ((always_inline)) -size_t -__fortify_strlcat(char *dest, const char *src, size_t n) +#undef strlcat +#undef strlcpy +extern size_t __strlcat_orig(char *, const char *, size_t) + __asm__(__USER_LABEL_PREFIX__ "strlcat"); +extern __inline __attribute__((__always_inline__,__gnu_inline__)) +size_t strlcat(char *dest, const char *src, size_t n) { size_t bos = __builtin_object_size(dest, 0); if (n > bos) __builtin_trap(); - return strlcat(dest, src, n); + return __strlcat_orig(dest, src, n); } -static inline __attribute__ ((always_inline)) -size_t -__fortify_strlcpy(char *dest, const char *src, size_t n) +extern size_t __strlcpy_orig(char *, const char *, size_t) + __asm__(__USER_LABEL_PREFIX__ "strlcpy"); +extern __inline __attribute__((__always_inline__,__gnu_inline__)) +size_t strlcpy(char *dest, const char *src, size_t n) { size_t bos = __builtin_object_size(dest, 0); if (n > bos) __builtin_trap(); - return strlcpy(dest, src, n); + return __strlcpy_orig(dest, src, n); } #endif -#undef memcpy -#define memcpy(dest, src, n) __fortify_memcpy(dest, src, n) -#undef memmove -#define memmove(dest, src, n) __fortify_memmove(dest, src, n) -#undef memset -#define memset(dest, src, n) __fortify_memset(dest, src, n) -#undef stpcpy -#define stpcpy(dest, src) __fortify_stpcpy(dest, src) -#undef stpncpy -#define stpncpy(dest, src, n) __fortify_stpncpy(dest, src, n) -#undef strcat -#define strcat(dest, src) __fortify_strcat(dest, src) -#undef strcpy -#define strcpy(dest, src) __fortify_strcpy(dest, src) -#undef strncat -#define strncat(dest, src, n) __fortify_strncat(dest, src, n) -#undef strncpy -#define strncpy(dest, src, n) __fortify_strncpy(dest, src, n) - -#ifdef _GNU_SOURCE -#undef mempcpy -#define mempcpy(dest, src, n) __fortify_mempcpy(dest, src, n) -#endif - -#if defined(_GNU_SOURCE) || defined(_BSD_SOURCE) -#undef strlcat -#define strlcat(dest, src, n) __fortify_strlcat(dest, src, n) -#undef strlcpy -#define strlcpy(dest, src, n) __fortify_strlcpy(dest, src, n) -#endif - #endif #endif -- cgit v1.3