diff options
| author | sin | 2015-01-28 15:36:44 +0000 |
|---|---|---|
| committer | sin | 2015-01-28 15:39:11 +0000 |
| commit | 6bb34c15b4d7989c5785577fbee72fdffa198d1f (patch) | |
| tree | 7ba68e381cf3d39f0555833832af0dbd0cb9d80e | |
| parent | b258495ebacc4a05c08fdf0ed1aa71ceb9e629e0 (diff) | |
Merge __foo_chk() into __fortify_foo()
| -rw-r--r-- | include/string.h | 153 |
1 files changed, 34 insertions, 119 deletions
diff --git a/include/string.h b/include/string.h index 944cf0b..7e6873e 100644 --- a/include/string.h +++ b/include/string.h | |||
| @@ -6,175 +6,112 @@ | |||
| 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 | static inline __attribute__ ((always_inline)) | 8 | static inline __attribute__ ((always_inline)) |
| 9 | void *__memcpy_chk(void *__restrict dest, const void *__restrict src, size_t ssize, | ||
| 10 | size_t dsize) | ||
| 11 | { | ||
| 12 | if (ssize > dsize) | ||
| 13 | __builtin_trap(); | ||
| 14 | return memcpy(dest, src, ssize); | ||
| 15 | } | ||
| 16 | |||
| 17 | static inline __attribute__ ((always_inline)) | ||
| 18 | void *__fortify_memcpy(void *__restrict dest, const void *__restrict src, size_t n) | 9 | void *__fortify_memcpy(void *__restrict dest, const void *__restrict src, size_t n) |
| 19 | { | 10 | { |
| 20 | size_t bos = __builtin_object_size(dest, 0); | 11 | size_t bos = __builtin_object_size(dest, 0); |
| 21 | 12 | ||
| 22 | if (bos == (size_t)-1) | 13 | if (bos == (size_t)-1) |
| 23 | return memcpy(dest, src, n); | 14 | return memcpy(dest, src, n); |
| 24 | if (__builtin_constant_p(n)) | 15 | if (__builtin_constant_p(n) && n > bos) |
| 25 | if (n <= bos) | 16 | __builtin_trap(); |
| 26 | return memcpy(dest, src, n); | 17 | return memcpy(dest, src, n); |
| 27 | return __memcpy_chk(dest, src, n, bos); | ||
| 28 | } | 18 | } |
| 29 | 19 | ||
| 30 | #undef memcpy | 20 | #undef memcpy |
| 31 | #define memcpy(dest, src, n) __fortify_memcpy(dest, src, n) | 21 | #define memcpy(dest, src, n) __fortify_memcpy(dest, src, n) |
| 32 | 22 | ||
| 33 | static inline __attribute__ ((always_inline)) | 23 | static inline __attribute__ ((always_inline)) |
| 34 | void *__memmove_chk(void *__restrict dest, const void *__restrict src, size_t ssize, | ||
| 35 | size_t dsize) | ||
| 36 | { | ||
| 37 | if (ssize > dsize) | ||
| 38 | __builtin_trap(); | ||
| 39 | return memmove(dest, src, ssize); | ||
| 40 | } | ||
| 41 | |||
| 42 | static inline __attribute__ ((always_inline)) | ||
| 43 | void *__fortify_memmove(void *__restrict dest, const void *__restrict src, size_t n) | 24 | void *__fortify_memmove(void *__restrict dest, const void *__restrict src, size_t n) |
| 44 | { | 25 | { |
| 45 | size_t bos = __builtin_object_size(dest, 0); | 26 | size_t bos = __builtin_object_size(dest, 0); |
| 46 | 27 | ||
| 47 | if (bos == (size_t)-1) | 28 | if (bos == (size_t)-1) |
| 48 | return memmove(dest, src, n); | 29 | return memmove(dest, src, n); |
| 49 | if (__builtin_constant_p(n)) | 30 | if (__builtin_constant_p(n) && n > bos) |
| 50 | if (n <= bos) | 31 | __builtin_trap(); |
| 51 | return memmove(dest, src, n); | 32 | return memmove(dest, src, n); |
| 52 | return __memmove_chk(dest, src, n, bos); | ||
| 53 | } | 33 | } |
| 54 | 34 | ||
| 55 | #undef memmove | 35 | #undef memmove |
| 56 | #define memmove(dest, src, n) __fortify_memmove(dest, src, n) | 36 | #define memmove(dest, src, n) __fortify_memmove(dest, src, n) |
| 57 | 37 | ||
| 58 | static inline __attribute__ ((always_inline)) | 38 | static inline __attribute__ ((always_inline)) |
| 59 | void *__memset_chk(void *dest, int c, size_t n, size_t dsize) | ||
| 60 | { | ||
| 61 | if (n > dsize) | ||
| 62 | __builtin_trap(); | ||
| 63 | return memset(dest, c, n); | ||
| 64 | } | ||
| 65 | |||
| 66 | static inline __attribute__ ((always_inline)) | ||
| 67 | void *__fortify_memset(void *dest, int c, size_t n) | 39 | void *__fortify_memset(void *dest, int c, size_t n) |
| 68 | { | 40 | { |
| 69 | size_t bos = __builtin_object_size(dest, 0); | 41 | size_t bos = __builtin_object_size(dest, 0); |
| 70 | 42 | ||
| 71 | if (bos == (size_t)-1) | 43 | if (bos == (size_t)-1) |
| 72 | return memset(dest, c, n); | 44 | return memset(dest, c, n); |
| 73 | if (__builtin_constant_p(n)) | 45 | if (__builtin_constant_p(n) && n > bos) |
| 74 | if (n <= bos) | 46 | __builtin_trap(); |
| 75 | return memset(dest, c, n); | 47 | return memset(dest, c, n); |
| 76 | return __memset_chk(dest, c, n, bos); | ||
| 77 | } | 48 | } |
| 78 | 49 | ||
| 79 | #undef memset | 50 | #undef memset |
| 80 | #define memset(dest, src, n) __fortify_memset(dest, src, n) | 51 | #define memset(dest, src, n) __fortify_memset(dest, src, n) |
| 81 | 52 | ||
| 82 | static inline __attribute__ ((always_inline)) | 53 | static inline __attribute__ ((always_inline)) |
| 83 | char *__strcat_chk(char *__restrict dest, const char *__restrict src, size_t n) | ||
| 84 | { | ||
| 85 | size_t slen = strlen(src); | ||
| 86 | size_t dlen = strlen(dest); | ||
| 87 | |||
| 88 | if (slen + dlen + 1 > n) | ||
| 89 | __builtin_trap(); | ||
| 90 | return strcat(dest, src); | ||
| 91 | } | ||
| 92 | |||
| 93 | static inline __attribute__ ((always_inline)) | ||
| 94 | char *__fortify_strcat(char *__restrict dest, const char *__restrict src) | 54 | char *__fortify_strcat(char *__restrict dest, const char *__restrict src) |
| 95 | { | 55 | { |
| 96 | size_t bos = __builtin_object_size(dest, 0); | 56 | size_t bos = __builtin_object_size(dest, 0); |
| 97 | 57 | ||
| 98 | if (bos == (size_t)-1) | 58 | if (bos == (size_t)-1) |
| 99 | return strcat(dest, src); | 59 | return strcat(dest, src); |
| 100 | return __strcat_chk(dest, src, bos); | 60 | if (strlen(src) + strlen(dest) + 1 > bos) |
| 61 | __builtin_trap(); | ||
| 62 | return strcat(dest, src); | ||
| 101 | } | 63 | } |
| 102 | 64 | ||
| 103 | #undef strcat | 65 | #undef strcat |
| 104 | #define strcat(dest, src) __fortify_strcat(dest, src) | 66 | #define strcat(dest, src) __fortify_strcat(dest, src) |
| 105 | 67 | ||
| 106 | static inline __attribute__ ((always_inline)) | 68 | static inline __attribute__ ((always_inline)) |
| 107 | char *__strcpy_chk(char *__restrict dest, const char *__restrict src, size_t n) | ||
| 108 | { | ||
| 109 | size_t slen = strlen(src); | ||
| 110 | |||
| 111 | if (slen + 1 > n) | ||
| 112 | __builtin_trap(); | ||
| 113 | return strcpy(dest, src); | ||
| 114 | } | ||
| 115 | |||
| 116 | static inline __attribute__ ((always_inline)) | ||
| 117 | char *__fortify_strcpy(char *__restrict dest, const char *__restrict src) | 69 | char *__fortify_strcpy(char *__restrict dest, const char *__restrict src) |
| 118 | { | 70 | { |
| 119 | size_t bos = __builtin_object_size(dest, 0); | 71 | size_t bos = __builtin_object_size(dest, 0); |
| 120 | 72 | ||
| 121 | if (bos == (size_t)-1) | 73 | if (bos == (size_t)-1) |
| 122 | return strcpy(dest, src); | 74 | return strcpy(dest, src); |
| 123 | return __strcpy_chk(dest, src, bos); | 75 | if (strlen(src) + 1 > bos) |
| 76 | __builtin_trap(); | ||
| 77 | return strcpy(dest, src); | ||
| 124 | } | 78 | } |
| 125 | 79 | ||
| 126 | #undef strcpy | 80 | #undef strcpy |
| 127 | #define strcpy(dest, src) __fortify_strcpy(dest, src) | 81 | #define strcpy(dest, src) __fortify_strcpy(dest, src) |
| 128 | 82 | ||
| 129 | static inline __attribute__ ((always_inline)) | 83 | static inline __attribute__ ((always_inline)) |
| 130 | char *__strncat_chk(char *__restrict dest, const char *__restrict src, size_t n, | ||
| 131 | size_t dsize) | ||
| 132 | { | ||
| 133 | size_t slen = strlen(src); | ||
| 134 | size_t dlen = strlen(dest); | ||
| 135 | |||
| 136 | if (slen > n) slen = n; | ||
| 137 | if (slen + dlen + 1 > dsize) | ||
| 138 | __builtin_trap(); | ||
| 139 | return strncat(dest, src, n); | ||
| 140 | } | ||
| 141 | |||
| 142 | static inline __attribute__ ((always_inline)) | ||
| 143 | char *__fortify_strncat(char *__restrict dest, const char *__restrict src, size_t n) | 84 | char *__fortify_strncat(char *__restrict dest, const char *__restrict src, size_t n) |
| 144 | { | 85 | { |
| 145 | size_t bos = __builtin_object_size(dest, 0); | 86 | size_t bos = __builtin_object_size(dest, 0); |
| 87 | size_t slen, dlen; | ||
| 146 | 88 | ||
| 147 | if (bos == (size_t)-1) | 89 | if (bos == (size_t)-1) |
| 148 | return strncat(dest, src, n); | 90 | return strncat(dest, src, n); |
| 149 | if (__builtin_constant_p(n)) | 91 | if (__builtin_constant_p(n) && n > bos) { |
| 150 | if (n <= bos) | 92 | slen = strlen(src); |
| 151 | return strncat(dest, src, n); | 93 | dlen = strlen(dest); |
| 152 | return __strncat_chk(dest, src, n, bos); | 94 | if (slen > n) |
| 95 | slen = n; | ||
| 96 | if (slen + dlen + 1 > bos) | ||
| 97 | __builtin_trap(); | ||
| 98 | } | ||
| 99 | return strncat(dest, src, n); | ||
| 153 | } | 100 | } |
| 154 | 101 | ||
| 155 | #undef strncat | 102 | #undef strncat |
| 156 | #define strncat(dest, src, n) __fortify_strcat(dest, src, n) | 103 | #define strncat(dest, src, n) __fortify_strcat(dest, src, n) |
| 157 | 104 | ||
| 158 | static inline __attribute__ ((always_inline)) | 105 | static inline __attribute__ ((always_inline)) |
| 159 | char *__strncpy_chk(char *__restrict dest, const char *__restrict src, size_t n, | ||
| 160 | size_t dsize) | ||
| 161 | { | ||
| 162 | if (n > dsize) | ||
| 163 | __builtin_trap(); | ||
| 164 | return strncpy(dest, src, n); | ||
| 165 | } | ||
| 166 | |||
| 167 | static inline __attribute__ ((always_inline)) | ||
| 168 | char *__fortify_strncpy(char *__restrict dest, const char *__restrict src, size_t n) | 106 | char *__fortify_strncpy(char *__restrict dest, const char *__restrict src, size_t n) |
| 169 | { | 107 | { |
| 170 | size_t bos = __builtin_object_size(dest, 0); | 108 | size_t bos = __builtin_object_size(dest, 0); |
| 171 | 109 | ||
| 172 | if (bos == (size_t)-1) | 110 | if (bos == (size_t)-1) |
| 173 | return strncpy(dest, src, n); | 111 | return strncpy(dest, src, n); |
| 174 | if (__builtin_constant_p(n)) | 112 | if (__builtin_constant_p(n) && n > bos) |
| 175 | if (n <= bos) | 113 | __builtin_trap(); |
| 176 | return strncpy(dest, src, n); | 114 | return strncpy(dest, src, n); |
| 177 | return __strncpy_chk(dest, src, n, bos); | ||
| 178 | } | 115 | } |
| 179 | 116 | ||
| 180 | #undef strncpy | 117 | #undef strncpy |
| @@ -182,52 +119,30 @@ char *__fortify_strncpy(char *__restrict dest, const char *__restrict src, size_ | |||
| 182 | 119 | ||
| 183 | #if defined(_GNU_SOURCE) || defined(_BSD_SOURCE) | 120 | #if defined(_GNU_SOURCE) || defined(_BSD_SOURCE) |
| 184 | static inline __attribute__ ((always_inline)) | 121 | static inline __attribute__ ((always_inline)) |
| 185 | size_t __strlcat_chk(char *__restrict dest, const char *__restrict src, size_t n, | ||
| 186 | size_t dsize) | ||
| 187 | { | ||
| 188 | if (n > dsize) | ||
| 189 | __builtin_trap(); | ||
| 190 | return strlcat(dest, src, n); | ||
| 191 | } | ||
| 192 | |||
| 193 | static inline __attribute__ ((always_inline)) | ||
| 194 | size_t __fortify_strlcat(char *__restrict dest, const char *__restrict src, size_t n) | 122 | size_t __fortify_strlcat(char *__restrict dest, const char *__restrict src, size_t n) |
| 195 | { | 123 | { |
| 196 | size_t bos = __builtin_object_size(dest, 0); | 124 | size_t bos = __builtin_object_size(dest, 0); |
| 197 | 125 | ||
| 198 | if (bos == (size_t)-1) | 126 | if (bos == (size_t)-1) |
| 199 | return strlcat(dest, src, n); | 127 | return strlcat(dest, src, n); |
| 200 | if (__builtin_constant_p(n)) { | 128 | if (__builtin_constant_p(n) && n > bos) |
| 201 | if (n <= bos) | 129 | __builtin_trap(); |
| 202 | return strlcat(dest, src, n); | 130 | return strlcat(dest, src, n); |
| 203 | } | ||
| 204 | return __strlcat_chk(dest, src, n, bos); | ||
| 205 | } | 131 | } |
| 206 | 132 | ||
| 207 | #undef strlcat | 133 | #undef strlcat |
| 208 | #define strlcat(dest, src, n) __fortify_strlcat(dest, src, n) | 134 | #define strlcat(dest, src, n) __fortify_strlcat(dest, src, n) |
| 209 | 135 | ||
| 210 | static inline __attribute__ ((always_inline)) | 136 | static inline __attribute__ ((always_inline)) |
| 211 | size_t __strlcpy_chk(char *__restrict dest, const char *__restrict src, size_t n, | ||
| 212 | size_t dsize) | ||
| 213 | { | ||
| 214 | if (n > dsize) | ||
| 215 | __builtin_trap(); | ||
| 216 | return strlcpy(dest, src, n); | ||
| 217 | } | ||
| 218 | |||
| 219 | static inline __attribute__ ((always_inline)) | ||
| 220 | size_t __fortify_strlcpy(char *__restrict dest, const char *__restrict src, size_t n) | 137 | size_t __fortify_strlcpy(char *__restrict dest, const char *__restrict src, size_t n) |
| 221 | { | 138 | { |
| 222 | size_t bos = __builtin_object_size(dest, 0); | 139 | size_t bos = __builtin_object_size(dest, 0); |
| 223 | 140 | ||
| 224 | if (bos == (size_t)-1) | 141 | if (bos == (size_t)-1) |
| 225 | return strlcpy(dest, src, n); | 142 | return strlcpy(dest, src, n); |
| 226 | if (__builtin_constant_p(n)) { | 143 | if (__builtin_constant_p(n) && n > bos) |
| 227 | if (n <= bos) | 144 | __builtin_trap(); |
| 228 | return strlcpy(dest, src, n); | 145 | return strlcpy(dest, src, n); |
| 229 | } | ||
| 230 | return __strlcpy_chk(dest, src, n, bos); | ||
| 231 | } | 146 | } |
| 232 | 147 | ||
| 233 | #undef strlcpy | 148 | #undef strlcpy |
