diff options
| author | sin | 2015-03-13 11:00:46 +0000 |
|---|---|---|
| committer | sin | 2015-03-13 11:00:46 +0000 |
| commit | 9f8c543dc81f0c4239acae6713f5414eb7dc681d (patch) | |
| tree | 0c8dad17e27c510cc3c98502841aa1a75dfa3d1e /include/string.h | |
| parent | b211796d68c4a6b56f999534627791f3576b6135 (diff) | |
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.
Diffstat (limited to 'include/string.h')
| -rw-r--r-- | include/string.h | 151 |
1 files changed, 72 insertions, 79 deletions
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 @@ | |||
| 6 | #if defined(_FORTIFY_SOURCE) && _FORTIFY_SOURCE > 0 && defined(__OPTIMIZE__) && __OPTIMIZE__ > 0 | 6 | #if defined(_FORTIFY_SOURCE) && _FORTIFY_SOURCE > 0 && defined(__OPTIMIZE__) && __OPTIMIZE__ > 0 |
| 7 | 7 | ||
| 8 | #ifndef __cplusplus | 8 | #ifndef __cplusplus |
| 9 | #undef memcpy | ||
| 10 | #undef memmove | ||
| 11 | #undef memset | ||
| 12 | #undef stpcpy | ||
| 13 | #undef stpncpy | ||
| 14 | #undef strcat | ||
| 15 | #undef strcpy | ||
| 16 | #undef strncat | ||
| 17 | #undef strncpy | ||
| 9 | 18 | ||
| 10 | static inline __attribute__ ((always_inline)) | 19 | extern void *__memcpy_orig(void *, const void *, size_t) |
| 11 | void * | 20 | __asm__(__USER_LABEL_PREFIX__ "memcpy"); |
| 12 | __fortify_memcpy(void *dest, const void *src, size_t n) | 21 | extern __inline __attribute__((__always_inline__,__gnu_inline__)) |
| 22 | void *memcpy(void *dest, const void *src, size_t n) | ||
| 13 | { | 23 | { |
| 14 | size_t bos = __builtin_object_size(dest, 0); | 24 | size_t bos = __builtin_object_size(dest, 0); |
| 15 | char *d = dest; | 25 | char *d = dest; |
| @@ -22,78 +32,85 @@ __fortify_memcpy(void *dest, const void *src, size_t n) | |||
| 22 | __builtin_trap(); | 32 | __builtin_trap(); |
| 23 | if (n > bos) | 33 | if (n > bos) |
| 24 | __builtin_trap(); | 34 | __builtin_trap(); |
| 25 | return memcpy(dest, src, n); | 35 | return __memcpy_orig(dest, src, n); |
| 26 | } | 36 | } |
| 27 | 37 | ||
| 28 | static inline __attribute__ ((always_inline)) | 38 | extern void *__memmove_orig(void *, const void *, size_t) |
| 29 | void * | 39 | __asm__(__USER_LABEL_PREFIX__ "memmove"); |
| 30 | __fortify_memmove(void *dest, const void *src, size_t n) | 40 | extern __inline __attribute__((__always_inline__,__gnu_inline__)) |
| 41 | void *memmove(void *dest, const void *src, size_t n) | ||
| 31 | { | 42 | { |
| 32 | size_t bos = __builtin_object_size(dest, 0); | 43 | size_t bos = __builtin_object_size(dest, 0); |
| 33 | 44 | ||
| 34 | if (n > bos) | 45 | if (n > bos) |
| 35 | __builtin_trap(); | 46 | __builtin_trap(); |
| 36 | return memmove(dest, src, n); | 47 | return __memmove_orig(dest, src, n); |
| 37 | } | 48 | } |
| 38 | 49 | ||
| 39 | static inline __attribute__ ((always_inline)) | 50 | extern void *__memset_orig(void *, int, size_t) |
| 40 | void * | 51 | __asm__(__USER_LABEL_PREFIX__ "memset"); |
| 41 | __fortify_memset(void *dest, int c, size_t n) | 52 | extern __inline __attribute__((__always_inline__,__gnu_inline__)) |
| 53 | void *memset(void *dest, int c, size_t n) | ||
| 42 | { | 54 | { |
| 43 | size_t bos = __builtin_object_size(dest, 0); | 55 | size_t bos = __builtin_object_size(dest, 0); |
| 44 | 56 | ||
| 45 | if (n > bos) | 57 | if (n > bos) |
| 46 | __builtin_trap(); | 58 | __builtin_trap(); |
| 47 | return memset(dest, c, n); | 59 | return __memset_orig(dest, c, n); |
| 48 | } | 60 | } |
| 49 | 61 | ||
| 50 | static inline __attribute__ ((always_inline)) | 62 | extern char *__stpcpy_orig(char *, const char *) |
| 51 | char * | 63 | __asm__(__USER_LABEL_PREFIX__ "stpcpy"); |
| 52 | __fortify_stpcpy(char *dest, const char *src) | 64 | extern __inline __attribute__((__always_inline__,__gnu_inline__)) |
| 65 | char *stpcpy(char *dest, const char *src) | ||
| 53 | { | 66 | { |
| 54 | size_t bos = __builtin_object_size(dest, 0); | 67 | size_t bos = __builtin_object_size(dest, 0); |
| 55 | 68 | ||
| 56 | if (strlen(src) + 1 > bos) | 69 | if (strlen(src) + 1 > bos) |
| 57 | __builtin_trap(); | 70 | __builtin_trap(); |
| 58 | return stpcpy(dest, src); | 71 | return __stpcpy_orig(dest, src); |
| 59 | } | 72 | } |
| 60 | 73 | ||
| 61 | static inline __attribute__ ((always_inline)) | 74 | extern char *__stpncpy_orig(char *, const char *, size_t) |
| 62 | char * | 75 | __asm__(__USER_LABEL_PREFIX__ "stpncpy"); |
| 63 | __fortify_stpncpy(char *dest, const char *src, size_t n) | 76 | extern __inline __attribute__((__always_inline__,__gnu_inline__)) |
| 77 | char *stpncpy(char *dest, const char *src, size_t n) | ||
| 64 | { | 78 | { |
| 65 | size_t bos = __builtin_object_size(dest, 0); | 79 | size_t bos = __builtin_object_size(dest, 0); |
| 66 | 80 | ||
| 67 | if (n > bos) | 81 | if (n > bos) |
| 68 | __builtin_trap(); | 82 | __builtin_trap(); |
| 69 | return stpncpy(dest, src, n); | 83 | return __stpncpy_orig(dest, src, n); |
| 70 | } | 84 | } |
| 71 | 85 | ||
| 72 | static inline __attribute__ ((always_inline)) | 86 | extern char *__strcat_orig(char *, const char *) |
| 73 | char * | 87 | __asm__(__USER_LABEL_PREFIX__ "strcat"); |
| 74 | __fortify_strcat(char *dest, const char *src) | 88 | extern __inline __attribute__((__always_inline__,__gnu_inline__)) |
| 89 | char *strcat(char *dest, const char *src) | ||
| 75 | { | 90 | { |
| 76 | size_t bos = __builtin_object_size(dest, 0); | 91 | size_t bos = __builtin_object_size(dest, 0); |
| 77 | 92 | ||
| 78 | if (strlen(src) + strlen(dest) + 1 > bos) | 93 | if (strlen(src) + strlen(dest) + 1 > bos) |
| 79 | __builtin_trap(); | 94 | __builtin_trap(); |
| 80 | return strcat(dest, src); | 95 | return __strcat_orig(dest, src); |
| 81 | } | 96 | } |
| 82 | 97 | ||
| 83 | static inline __attribute__ ((always_inline)) | 98 | extern char *__strcpy_orig(char *, const char *) |
| 84 | char * | 99 | __asm__(__USER_LABEL_PREFIX__ "strcpy"); |
| 85 | __fortify_strcpy(char *dest, const char *src) | 100 | extern __inline __attribute__((__always_inline__,__gnu_inline__)) |
| 101 | char *strcpy(char *dest, const char *src) | ||
| 86 | { | 102 | { |
| 87 | size_t bos = __builtin_object_size(dest, 0); | 103 | size_t bos = __builtin_object_size(dest, 0); |
| 88 | 104 | ||
| 89 | if (strlen(src) + 1 > bos) | 105 | if (strlen(src) + 1 > bos) |
| 90 | __builtin_trap(); | 106 | __builtin_trap(); |
| 91 | return strcpy(dest, src); | 107 | return __strcpy_orig(dest, src); |
| 92 | } | 108 | } |
| 93 | 109 | ||
| 94 | static inline __attribute__ ((always_inline)) | 110 | extern char *__strncat_orig(char *, const char *, size_t) |
| 95 | char * | 111 | __asm__(__USER_LABEL_PREFIX__ "strncat"); |
| 96 | __fortify_strncat(char *dest, const char *src, size_t n) | 112 | extern __inline __attribute__((__always_inline__,__gnu_inline__)) |
| 113 | char *strncat(char *dest, const char *src, size_t n) | ||
| 97 | { | 114 | { |
| 98 | size_t bos = __builtin_object_size(dest, 0); | 115 | size_t bos = __builtin_object_size(dest, 0); |
| 99 | size_t slen, dlen; | 116 | size_t slen, dlen; |
| @@ -106,88 +123,64 @@ __fortify_strncat(char *dest, const char *src, size_t n) | |||
| 106 | if (slen + dlen + 1 > bos) | 123 | if (slen + dlen + 1 > bos) |
| 107 | __builtin_trap(); | 124 | __builtin_trap(); |
| 108 | } | 125 | } |
| 109 | return strncat(dest, src, n); | 126 | return __strncat_orig(dest, src, n); |
| 110 | } | 127 | } |
| 111 | 128 | ||
| 112 | static inline __attribute__ ((always_inline)) | 129 | extern char *__strncpy_orig(char *, const char *, size_t) |
| 113 | char * | 130 | __asm__(__USER_LABEL_PREFIX__ "strncpy"); |
| 114 | __fortify_strncpy(char *dest, const char *src, size_t n) | 131 | extern __inline __attribute__((__always_inline__,__gnu_inline__)) |
| 132 | char *strncpy(char *dest, const char *src, size_t n) | ||
| 115 | { | 133 | { |
| 116 | size_t bos = __builtin_object_size(dest, 0); | 134 | size_t bos = __builtin_object_size(dest, 0); |
| 117 | 135 | ||
| 118 | if (n > bos) | 136 | if (n > bos) |
| 119 | __builtin_trap(); | 137 | __builtin_trap(); |
| 120 | return strncpy(dest, src, n); | 138 | return __strncpy_orig(dest, src, n); |
| 121 | } | 139 | } |
| 122 | 140 | ||
| 123 | #ifdef _GNU_SOURCE | 141 | #ifdef _GNU_SOURCE |
| 124 | static inline __attribute__ ((always_inline)) | 142 | #undef mempcpy |
| 125 | void * | 143 | extern void *__mempcpy_orig(void *, const void *, size_t n) |
| 126 | __fortify_mempcpy(void *dest, const void *src, size_t n) | 144 | __asm__(__USER_LABEL_PREFIX__ "mempcpy"); |
| 145 | extern __inline __attribute__((__always_inline__,__gnu_inline__)) | ||
| 146 | void *mempcpy(void *dest, const void *src, size_t n) | ||
| 127 | { | 147 | { |
| 128 | size_t bos = __builtin_object_size(dest, 0); | 148 | size_t bos = __builtin_object_size(dest, 0); |
| 129 | 149 | ||
| 130 | if (n > bos) | 150 | if (n > bos) |
| 131 | __builtin_trap(); | 151 | __builtin_trap(); |
| 132 | return mempcpy(dest, src, n); | 152 | return __mempcpy_orig(dest, src, n); |
| 133 | } | 153 | } |
| 134 | #endif | 154 | #endif |
| 135 | 155 | ||
| 136 | #if defined(_GNU_SOURCE) || defined(_BSD_SOURCE) | 156 | #if defined(_GNU_SOURCE) || defined(_BSD_SOURCE) |
| 137 | static inline __attribute__ ((always_inline)) | 157 | #undef strlcat |
| 138 | size_t | 158 | #undef strlcpy |
| 139 | __fortify_strlcat(char *dest, const char *src, size_t n) | 159 | extern size_t __strlcat_orig(char *, const char *, size_t) |
| 160 | __asm__(__USER_LABEL_PREFIX__ "strlcat"); | ||
| 161 | extern __inline __attribute__((__always_inline__,__gnu_inline__)) | ||
| 162 | size_t strlcat(char *dest, const char *src, size_t n) | ||
| 140 | { | 163 | { |
| 141 | size_t bos = __builtin_object_size(dest, 0); | 164 | size_t bos = __builtin_object_size(dest, 0); |
| 142 | 165 | ||
| 143 | if (n > bos) | 166 | if (n > bos) |
| 144 | __builtin_trap(); | 167 | __builtin_trap(); |
| 145 | return strlcat(dest, src, n); | 168 | return __strlcat_orig(dest, src, n); |
| 146 | } | 169 | } |
| 147 | 170 | ||
| 148 | static inline __attribute__ ((always_inline)) | 171 | extern size_t __strlcpy_orig(char *, const char *, size_t) |
| 149 | size_t | 172 | __asm__(__USER_LABEL_PREFIX__ "strlcpy"); |
| 150 | __fortify_strlcpy(char *dest, const char *src, size_t n) | 173 | extern __inline __attribute__((__always_inline__,__gnu_inline__)) |
| 174 | size_t strlcpy(char *dest, const char *src, size_t n) | ||
| 151 | { | 175 | { |
| 152 | size_t bos = __builtin_object_size(dest, 0); | 176 | size_t bos = __builtin_object_size(dest, 0); |
| 153 | 177 | ||
| 154 | if (n > bos) | 178 | if (n > bos) |
| 155 | __builtin_trap(); | 179 | __builtin_trap(); |
| 156 | return strlcpy(dest, src, n); | 180 | return __strlcpy_orig(dest, src, n); |
| 157 | } | 181 | } |
| 158 | #endif | 182 | #endif |
| 159 | 183 | ||
| 160 | #undef memcpy | ||
| 161 | #define memcpy(dest, src, n) __fortify_memcpy(dest, src, n) | ||
| 162 | #undef memmove | ||
| 163 | #define memmove(dest, src, n) __fortify_memmove(dest, src, n) | ||
| 164 | #undef memset | ||
| 165 | #define memset(dest, src, n) __fortify_memset(dest, src, n) | ||
| 166 | #undef stpcpy | ||
| 167 | #define stpcpy(dest, src) __fortify_stpcpy(dest, src) | ||
| 168 | #undef stpncpy | ||
| 169 | #define stpncpy(dest, src, n) __fortify_stpncpy(dest, src, n) | ||
| 170 | #undef strcat | ||
| 171 | #define strcat(dest, src) __fortify_strcat(dest, src) | ||
| 172 | #undef strcpy | ||
| 173 | #define strcpy(dest, src) __fortify_strcpy(dest, src) | ||
| 174 | #undef strncat | ||
| 175 | #define strncat(dest, src, n) __fortify_strncat(dest, src, n) | ||
| 176 | #undef strncpy | ||
| 177 | #define strncpy(dest, src, n) __fortify_strncpy(dest, src, n) | ||
| 178 | |||
| 179 | #ifdef _GNU_SOURCE | ||
| 180 | #undef mempcpy | ||
| 181 | #define mempcpy(dest, src, n) __fortify_mempcpy(dest, src, n) | ||
| 182 | #endif | ||
| 183 | |||
| 184 | #if defined(_GNU_SOURCE) || defined(_BSD_SOURCE) | ||
| 185 | #undef strlcat | ||
| 186 | #define strlcat(dest, src, n) __fortify_strlcat(dest, src, n) | ||
| 187 | #undef strlcpy | ||
| 188 | #define strlcpy(dest, src, n) __fortify_strlcpy(dest, src, n) | ||
| 189 | #endif | ||
| 190 | |||
| 191 | #endif | 184 | #endif |
| 192 | 185 | ||
| 193 | #endif | 186 | #endif |
