diff options
| author | sin | 2015-02-24 18:12:27 +0000 |
|---|---|---|
| committer | sin | 2015-02-24 18:14:33 +0000 |
| commit | eecef18261cc278fbc13ecbfb4e5bc10762cc794 (patch) | |
| tree | 483074e25fbbcbb198ac4d339b84ace4205987f6 /include | |
| parent | 9a77136c5914f6be50df195dac0f99424252a297 (diff) | |
Remove compile time checks
These can produce false positives. Given that we support fortify
source level 1 we shouldn't break valid code.
Diffstat (limited to 'include')
| -rw-r--r-- | include/stdio.h | 15 | ||||
| -rw-r--r-- | include/string.h | 40 | ||||
| -rw-r--r-- | include/strings.h | 12 | ||||
| -rw-r--r-- | include/sys/socket.h | 12 | ||||
| -rw-r--r-- | include/unistd.h | 24 |
5 files changed, 0 insertions, 103 deletions
diff --git a/include/stdio.h b/include/stdio.h index a637f83..aeff658 100644 --- a/include/stdio.h +++ b/include/stdio.h | |||
| @@ -5,24 +5,17 @@ | |||
| 5 | 5 | ||
| 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 | #define __errordecl(name, msg) extern void name(void) __attribute__ ((__error__(msg))) | ||
| 9 | |||
| 10 | __errordecl(__fgets_error, "fgets: buffer overflow detected"); | ||
| 11 | static inline __attribute__ ((always_inline)) | 8 | static inline __attribute__ ((always_inline)) |
| 12 | char * | 9 | char * |
| 13 | __fortify_fgets(char *s, int n, FILE *fp) | 10 | __fortify_fgets(char *s, int n, FILE *fp) |
| 14 | { | 11 | { |
| 15 | size_t bos = __builtin_object_size(s, 0); | 12 | size_t bos = __builtin_object_size(s, 0); |
| 16 | 13 | ||
| 17 | if (__builtin_constant_p(n) && (size_t)n > bos) | ||
| 18 | __fgets_error(); | ||
| 19 | |||
| 20 | if ((size_t)n > bos) | 14 | if ((size_t)n > bos) |
| 21 | __builtin_trap(); | 15 | __builtin_trap(); |
| 22 | return fgets(s, n, fp); | 16 | return fgets(s, n, fp); |
| 23 | } | 17 | } |
| 24 | 18 | ||
| 25 | __errordecl(__vsnprintf_error, "vsnprintf: buffer overflow detected"); | ||
| 26 | static inline | 19 | static inline |
| 27 | __attribute__ ((always_inline)) | 20 | __attribute__ ((always_inline)) |
| 28 | __attribute__ ((__format__ (printf, 3, 0))) | 21 | __attribute__ ((__format__ (printf, 3, 0))) |
| @@ -32,9 +25,6 @@ __fortify_vsnprintf(char *s, size_t n, const char *fmt, __builtin_va_list ap) | |||
| 32 | { | 25 | { |
| 33 | size_t bos = __builtin_object_size(s, 0); | 26 | size_t bos = __builtin_object_size(s, 0); |
| 34 | 27 | ||
| 35 | if (__builtin_constant_p(n) && n > bos) | ||
| 36 | __vsnprintf_error(); | ||
| 37 | |||
| 38 | if (n > bos) | 28 | if (n > bos) |
| 39 | __builtin_trap(); | 29 | __builtin_trap(); |
| 40 | return vsnprintf(s, n, fmt, ap); | 30 | return vsnprintf(s, n, fmt, ap); |
| @@ -45,20 +35,15 @@ __fortify_vsnprintf(char *s, size_t n, const char *fmt, __builtin_va_list ap) | |||
| 45 | #undef vsnprintf | 35 | #undef vsnprintf |
| 46 | #define vsnprintf(s, n, fmt, ap) __fortify_vsnprintf(s, n, fmt, ap) | 36 | #define vsnprintf(s, n, fmt, ap) __fortify_vsnprintf(s, n, fmt, ap) |
| 47 | 37 | ||
| 48 | __errordecl(__snprintf_error, "snprintf: buffer overflow detected"); | ||
| 49 | #undef snprintf | 38 | #undef snprintf |
| 50 | #define snprintf(s, n, fmt, ...) ({ \ | 39 | #define snprintf(s, n, fmt, ...) ({ \ |
| 51 | size_t _n = (n); \ | 40 | size_t _n = (n); \ |
| 52 | size_t bos = __builtin_object_size(s, 0); \ | 41 | size_t bos = __builtin_object_size(s, 0); \ |
| 53 | if (__builtin_constant_p(_n) && _n > bos) \ | ||
| 54 | __snprintf_error(); \ | ||
| 55 | if (_n > bos) \ | 42 | if (_n > bos) \ |
| 56 | __builtin_trap(); \ | 43 | __builtin_trap(); \ |
| 57 | snprintf(s, _n, fmt, ## __VA_ARGS__); \ | 44 | snprintf(s, _n, fmt, ## __VA_ARGS__); \ |
| 58 | }) | 45 | }) |
| 59 | 46 | ||
| 60 | #undef __errordecl | ||
| 61 | |||
| 62 | #endif | 47 | #endif |
| 63 | 48 | ||
| 64 | #endif | 49 | #endif |
diff --git a/include/string.h b/include/string.h index 9f69d31..9d40e77 100644 --- a/include/string.h +++ b/include/string.h | |||
| @@ -5,9 +5,6 @@ | |||
| 5 | 5 | ||
| 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 | #define __errordecl(name, msg) extern void name(void) __attribute__ ((__error__(msg))) | ||
| 9 | |||
| 10 | __errordecl(__memcpy_error, "memcpy: buffer overflow detected"); | ||
| 11 | static inline __attribute__ ((always_inline)) | 8 | static inline __attribute__ ((always_inline)) |
| 12 | void * | 9 | void * |
| 13 | __fortify_memcpy(void *dest, const void *src, size_t n) | 10 | __fortify_memcpy(void *dest, const void *src, size_t n) |
| @@ -16,9 +13,6 @@ __fortify_memcpy(void *dest, const void *src, size_t n) | |||
| 16 | char *d = dest; | 13 | char *d = dest; |
| 17 | const char *s = src; | 14 | const char *s = src; |
| 18 | 15 | ||
| 19 | if (__builtin_constant_p(n) && n > bos) | ||
| 20 | __memcpy_error(); | ||
| 21 | |||
| 22 | /* trap if pointers are overlapping but not if dest == src */ | 16 | /* trap if pointers are overlapping but not if dest == src */ |
| 23 | if ((d < s && d + n > s) || | 17 | if ((d < s && d + n > s) || |
| 24 | (s < d && s + n > d)) | 18 | (s < d && s + n > d)) |
| @@ -28,31 +22,23 @@ __fortify_memcpy(void *dest, const void *src, size_t n) | |||
| 28 | return memcpy(dest, src, n); | 22 | return memcpy(dest, src, n); |
| 29 | } | 23 | } |
| 30 | 24 | ||
| 31 | __errordecl(__memmove_error, "memmove: buffer overflow detected"); | ||
| 32 | static inline __attribute__ ((always_inline)) | 25 | static inline __attribute__ ((always_inline)) |
| 33 | void * | 26 | void * |
| 34 | __fortify_memmove(void *dest, const void *src, size_t n) | 27 | __fortify_memmove(void *dest, const void *src, size_t n) |
| 35 | { | 28 | { |
| 36 | size_t bos = __builtin_object_size(dest, 0); | 29 | size_t bos = __builtin_object_size(dest, 0); |
| 37 | 30 | ||
| 38 | if (__builtin_constant_p(n) && n > bos) | ||
| 39 | __memmove_error(); | ||
| 40 | |||
| 41 | if (n > bos) | 31 | if (n > bos) |
| 42 | __builtin_trap(); | 32 | __builtin_trap(); |
| 43 | return memmove(dest, src, n); | 33 | return memmove(dest, src, n); |
| 44 | } | 34 | } |
| 45 | 35 | ||
| 46 | __errordecl(__memset_error, "memset: buffer overflow detected"); | ||
| 47 | static inline __attribute__ ((always_inline)) | 36 | static inline __attribute__ ((always_inline)) |
| 48 | void * | 37 | void * |
| 49 | __fortify_memset(void *dest, int c, size_t n) | 38 | __fortify_memset(void *dest, int c, size_t n) |
| 50 | { | 39 | { |
| 51 | size_t bos = __builtin_object_size(dest, 0); | 40 | size_t bos = __builtin_object_size(dest, 0); |
| 52 | 41 | ||
| 53 | if (__builtin_constant_p(n) && n > bos) | ||
| 54 | __memset_error(); | ||
| 55 | |||
| 56 | if (n > bos) | 42 | if (n > bos) |
| 57 | __builtin_trap(); | 43 | __builtin_trap(); |
| 58 | return memset(dest, c, n); | 44 | return memset(dest, c, n); |
| @@ -69,16 +55,12 @@ __fortify_stpcpy(char *dest, const char *src) | |||
| 69 | return stpcpy(dest, src); | 55 | return stpcpy(dest, src); |
| 70 | } | 56 | } |
| 71 | 57 | ||
| 72 | __errordecl(__stpncpy_error, "stpncpy: buffer overflow detected"); | ||
| 73 | static inline __attribute__ ((always_inline)) | 58 | static inline __attribute__ ((always_inline)) |
| 74 | char * | 59 | char * |
| 75 | __fortify_stpncpy(char *dest, const char *src, size_t n) | 60 | __fortify_stpncpy(char *dest, const char *src, size_t n) |
| 76 | { | 61 | { |
| 77 | size_t bos = __builtin_object_size(dest, 0); | 62 | size_t bos = __builtin_object_size(dest, 0); |
| 78 | 63 | ||
| 79 | if (__builtin_constant_p(n) && n > bos) | ||
| 80 | __stpncpy_error(); | ||
| 81 | |||
| 82 | if (n > bos) | 64 | if (n > bos) |
| 83 | __builtin_trap(); | 65 | __builtin_trap(); |
| 84 | return stpncpy(dest, src, n); | 66 | return stpncpy(dest, src, n); |
| @@ -106,7 +88,6 @@ __fortify_strcpy(char *dest, const char *src) | |||
| 106 | return strcpy(dest, src); | 88 | return strcpy(dest, src); |
| 107 | } | 89 | } |
| 108 | 90 | ||
| 109 | __errordecl(__strncat_error, "strncat: buffer overflow detected"); | ||
| 110 | static inline __attribute__ ((always_inline)) | 91 | static inline __attribute__ ((always_inline)) |
| 111 | char * | 92 | char * |
| 112 | __fortify_strncat(char *dest, const char *src, size_t n) | 93 | __fortify_strncat(char *dest, const char *src, size_t n) |
| @@ -114,9 +95,6 @@ __fortify_strncat(char *dest, const char *src, size_t n) | |||
| 114 | size_t bos = __builtin_object_size(dest, 0); | 95 | size_t bos = __builtin_object_size(dest, 0); |
| 115 | size_t slen, dlen; | 96 | size_t slen, dlen; |
| 116 | 97 | ||
| 117 | if (__builtin_constant_p(n) && n > bos) | ||
| 118 | __strncat_error(); | ||
| 119 | |||
| 120 | if (n > bos) { | 98 | if (n > bos) { |
| 121 | slen = strlen(src); | 99 | slen = strlen(src); |
| 122 | dlen = strlen(dest); | 100 | dlen = strlen(dest); |
| @@ -128,32 +106,24 @@ __fortify_strncat(char *dest, const char *src, size_t n) | |||
| 128 | return strncat(dest, src, n); | 106 | return strncat(dest, src, n); |
| 129 | } | 107 | } |
| 130 | 108 | ||
| 131 | __errordecl(__strncpy_error, "strncpy: buffer overflow detected"); | ||
| 132 | static inline __attribute__ ((always_inline)) | 109 | static inline __attribute__ ((always_inline)) |
| 133 | char * | 110 | char * |
| 134 | __fortify_strncpy(char *dest, const char *src, size_t n) | 111 | __fortify_strncpy(char *dest, const char *src, size_t n) |
| 135 | { | 112 | { |
| 136 | size_t bos = __builtin_object_size(dest, 0); | 113 | size_t bos = __builtin_object_size(dest, 0); |
| 137 | 114 | ||
| 138 | if (__builtin_constant_p(n) && n > bos) | ||
| 139 | __strncpy_error(); | ||
| 140 | |||
| 141 | if (n > bos) | 115 | if (n > bos) |
| 142 | __builtin_trap(); | 116 | __builtin_trap(); |
| 143 | return strncpy(dest, src, n); | 117 | return strncpy(dest, src, n); |
| 144 | } | 118 | } |
| 145 | 119 | ||
| 146 | #ifdef _GNU_SOURCE | 120 | #ifdef _GNU_SOURCE |
| 147 | __errordecl(__mempcpy_error, "mempcpy: buffer overflow detected"); | ||
| 148 | static inline __attribute__ ((always_inline)) | 121 | static inline __attribute__ ((always_inline)) |
| 149 | void * | 122 | void * |
| 150 | __fortify_mempcpy(void *dest, const void *src, size_t n) | 123 | __fortify_mempcpy(void *dest, const void *src, size_t n) |
| 151 | { | 124 | { |
| 152 | size_t bos = __builtin_object_size(dest, 0); | 125 | size_t bos = __builtin_object_size(dest, 0); |
| 153 | 126 | ||
| 154 | if (__builtin_constant_p(n) && n > bos) | ||
| 155 | __mempcpy_error(); | ||
| 156 | |||
| 157 | if (n > bos) | 127 | if (n > bos) |
| 158 | __builtin_trap(); | 128 | __builtin_trap(); |
| 159 | return mempcpy(dest, src, n); | 129 | return mempcpy(dest, src, n); |
| @@ -161,31 +131,23 @@ __fortify_mempcpy(void *dest, const void *src, size_t n) | |||
| 161 | #endif | 131 | #endif |
| 162 | 132 | ||
| 163 | #if defined(_GNU_SOURCE) || defined(_BSD_SOURCE) | 133 | #if defined(_GNU_SOURCE) || defined(_BSD_SOURCE) |
| 164 | __errordecl(__strlcat_error, "strlcat: buffer overflow detected"); | ||
| 165 | static inline __attribute__ ((always_inline)) | 134 | static inline __attribute__ ((always_inline)) |
| 166 | size_t | 135 | size_t |
| 167 | __fortify_strlcat(char *dest, const char *src, size_t n) | 136 | __fortify_strlcat(char *dest, const char *src, size_t n) |
| 168 | { | 137 | { |
| 169 | size_t bos = __builtin_object_size(dest, 0); | 138 | size_t bos = __builtin_object_size(dest, 0); |
| 170 | 139 | ||
| 171 | if (__builtin_constant_p(n) && n > bos) | ||
| 172 | __strlcat_error(); | ||
| 173 | |||
| 174 | if (n > bos) | 140 | if (n > bos) |
| 175 | __builtin_trap(); | 141 | __builtin_trap(); |
| 176 | return strlcat(dest, src, n); | 142 | return strlcat(dest, src, n); |
| 177 | } | 143 | } |
| 178 | 144 | ||
| 179 | __errordecl(__strlcpy_error, "strlcpy: buffer overflow detected"); | ||
| 180 | static inline __attribute__ ((always_inline)) | 145 | static inline __attribute__ ((always_inline)) |
| 181 | size_t | 146 | size_t |
| 182 | __fortify_strlcpy(char *dest, const char *src, size_t n) | 147 | __fortify_strlcpy(char *dest, const char *src, size_t n) |
| 183 | { | 148 | { |
| 184 | size_t bos = __builtin_object_size(dest, 0); | 149 | size_t bos = __builtin_object_size(dest, 0); |
| 185 | 150 | ||
| 186 | if (__builtin_constant_p(n) && n > bos) | ||
| 187 | __strlcpy_error(); | ||
| 188 | |||
| 189 | if (n > bos) | 151 | if (n > bos) |
| 190 | __builtin_trap(); | 152 | __builtin_trap(); |
| 191 | return strlcpy(dest, src, n); | 153 | return strlcpy(dest, src, n); |
| @@ -223,8 +185,6 @@ __fortify_strlcpy(char *dest, const char *src, size_t n) | |||
| 223 | #define strlcpy(dest, src, n) __fortify_strlcpy(dest, src, n) | 185 | #define strlcpy(dest, src, n) __fortify_strlcpy(dest, src, n) |
| 224 | #endif | 186 | #endif |
| 225 | 187 | ||
| 226 | #undef __errordecl | ||
| 227 | |||
| 228 | #endif | 188 | #endif |
| 229 | 189 | ||
| 230 | #endif | 190 | #endif |
diff --git a/include/strings.h b/include/strings.h index e23c8eb..943b565 100644 --- a/include/strings.h +++ b/include/strings.h | |||
| @@ -5,36 +5,26 @@ | |||
| 5 | 5 | ||
| 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 | #define __errordecl(name, msg) extern void name(void) __attribute__ ((__error__(msg))) | ||
| 9 | |||
| 10 | #if defined(_GNU_SOURCE) || defined(_BSD_SOURCE) || defined(_POSIX_SOURCE) \ | 8 | #if defined(_GNU_SOURCE) || defined(_BSD_SOURCE) || defined(_POSIX_SOURCE) \ |
| 11 | || (defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE+0 < 200809L) \ | 9 | || (defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE+0 < 200809L) \ |
| 12 | || (defined(_XOPEN_SOURCE) && _XOPEN_SOURCE+0 < 700) | 10 | || (defined(_XOPEN_SOURCE) && _XOPEN_SOURCE+0 < 700) |
| 13 | __errordecl(__bcopy_error, "bcopy: buffer overflow detected"); | ||
| 14 | static inline __attribute__ ((always_inline)) | 11 | static inline __attribute__ ((always_inline)) |
| 15 | void | 12 | void |
| 16 | __fortify_bcopy(const void *src, void *dest, size_t n) | 13 | __fortify_bcopy(const void *src, void *dest, size_t n) |
| 17 | { | 14 | { |
| 18 | size_t bos = __builtin_object_size(dest, 0); | 15 | size_t bos = __builtin_object_size(dest, 0); |
| 19 | 16 | ||
| 20 | if (__builtin_constant_p(n) && n > bos) | ||
| 21 | __bcopy_error(); | ||
| 22 | |||
| 23 | if (n > bos) | 17 | if (n > bos) |
| 24 | __builtin_trap(); | 18 | __builtin_trap(); |
| 25 | return bcopy(src, dest, n); | 19 | return bcopy(src, dest, n); |
| 26 | } | 20 | } |
| 27 | 21 | ||
| 28 | __errordecl(__bzero_error, "bzero: buffer overflow detected"); | ||
| 29 | static inline __attribute__ ((always_inline)) | 22 | static inline __attribute__ ((always_inline)) |
| 30 | void | 23 | void |
| 31 | __fortify_bzero(void *src, size_t n) | 24 | __fortify_bzero(void *src, size_t n) |
| 32 | { | 25 | { |
| 33 | size_t bos = __builtin_object_size(src, 0); | 26 | size_t bos = __builtin_object_size(src, 0); |
| 34 | 27 | ||
| 35 | if (__builtin_constant_p(n) && n > bos) | ||
| 36 | __bzero_error(); | ||
| 37 | |||
| 38 | if (n > bos) | 28 | if (n > bos) |
| 39 | __builtin_trap(); | 29 | __builtin_trap(); |
| 40 | return bzero(src, n); | 30 | return bzero(src, n); |
| @@ -46,8 +36,6 @@ __fortify_bzero(void *src, size_t n) | |||
| 46 | #define bzero(src, n) __fortify_bzero(src, n) | 36 | #define bzero(src, n) __fortify_bzero(src, n) |
| 47 | #endif | 37 | #endif |
| 48 | 38 | ||
| 49 | #undef __errordecl | ||
| 50 | |||
| 51 | #endif | 39 | #endif |
| 52 | 40 | ||
| 53 | #endif | 41 | #endif |
diff --git a/include/sys/socket.h b/include/sys/socket.h index e8f8db6..85ba63b 100644 --- a/include/sys/socket.h +++ b/include/sys/socket.h | |||
| @@ -5,33 +5,23 @@ | |||
| 5 | 5 | ||
| 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 | #define __errordecl(name, msg) extern void name(void) __attribute__ ((__error__(msg))) | ||
| 9 | |||
| 10 | __errordecl(__recv_error, "recv: buffer overflow detected"); | ||
| 11 | static inline __attribute__ ((always_inline)) | 8 | static inline __attribute__ ((always_inline)) |
| 12 | ssize_t | 9 | ssize_t |
| 13 | __fortify_recv(int sockfd, void *buf, size_t n, int flags) | 10 | __fortify_recv(int sockfd, void *buf, size_t n, int flags) |
| 14 | { | 11 | { |
| 15 | size_t bos = __builtin_object_size(buf, 0); | 12 | size_t bos = __builtin_object_size(buf, 0); |
| 16 | 13 | ||
| 17 | if (__builtin_constant_p(n) && n > bos) | ||
| 18 | __recv_error(); | ||
| 19 | |||
| 20 | if (n > bos) | 14 | if (n > bos) |
| 21 | __builtin_trap(); | 15 | __builtin_trap(); |
| 22 | return recv(sockfd, buf, n, flags); | 16 | return recv(sockfd, buf, n, flags); |
| 23 | } | 17 | } |
| 24 | 18 | ||
| 25 | __errordecl(__recvfrom_error, "recvfrom: buffer overflow detected"); | ||
| 26 | static inline __attribute__ ((always_inline)) | 19 | static inline __attribute__ ((always_inline)) |
| 27 | ssize_t | 20 | ssize_t |
| 28 | __fortify_recvfrom(int sockfd, void *buf, size_t n, int flags, struct sockaddr *sa, socklen_t *salen) | 21 | __fortify_recvfrom(int sockfd, void *buf, size_t n, int flags, struct sockaddr *sa, socklen_t *salen) |
| 29 | { | 22 | { |
| 30 | size_t bos = __builtin_object_size(buf, 0); | 23 | size_t bos = __builtin_object_size(buf, 0); |
| 31 | 24 | ||
| 32 | if (__builtin_constant_p(n) && n > bos) | ||
| 33 | __recvfrom_error(); | ||
| 34 | |||
| 35 | if (n > bos) | 25 | if (n > bos) |
| 36 | __builtin_trap(); | 26 | __builtin_trap(); |
| 37 | return recvfrom(sockfd, buf, n, flags, sa, salen); | 27 | return recvfrom(sockfd, buf, n, flags, sa, salen); |
| @@ -42,8 +32,6 @@ __fortify_recvfrom(int sockfd, void *buf, size_t n, int flags, struct sockaddr * | |||
| 42 | #undef recvfrom | 32 | #undef recvfrom |
| 43 | #define recvfrom(sockfd, buf, n, flags, sa, salen) __fortify_recvfrom(sockfd, buf, n, flags, sa, salen) | 33 | #define recvfrom(sockfd, buf, n, flags, sa, salen) __fortify_recvfrom(sockfd, buf, n, flags, sa, salen) |
| 44 | 34 | ||
| 45 | #undef __errordecl | ||
| 46 | |||
| 47 | #endif | 35 | #endif |
| 48 | 36 | ||
| 49 | #endif | 37 | #endif |
diff --git a/include/unistd.h b/include/unistd.h index bffccf7..b13b507 100644 --- a/include/unistd.h +++ b/include/unistd.h | |||
| @@ -5,78 +5,56 @@ | |||
| 5 | 5 | ||
| 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 | #define __errordecl(name, msg) extern void name(void) __attribute__ ((__error__(msg))) | ||
| 9 | |||
| 10 | __errordecl(__confstr_error, "confstr: buffer overflow detected"); | ||
| 11 | static inline __attribute__ ((always_inline)) | 8 | static inline __attribute__ ((always_inline)) |
| 12 | size_t | 9 | size_t |
| 13 | __fortify_confstr(int name, char *buf, size_t len) | 10 | __fortify_confstr(int name, char *buf, size_t len) |
| 14 | { | 11 | { |
| 15 | size_t bos = __builtin_object_size(buf, 0); | 12 | size_t bos = __builtin_object_size(buf, 0); |
| 16 | 13 | ||
| 17 | if (__builtin_constant_p(len) && len > bos) | ||
| 18 | __confstr_error(); | ||
| 19 | |||
| 20 | if (len > bos) | 14 | if (len > bos) |
| 21 | __builtin_trap(); | 15 | __builtin_trap(); |
| 22 | return confstr(name, buf, len); | 16 | return confstr(name, buf, len); |
| 23 | } | 17 | } |
| 24 | 18 | ||
| 25 | __errordecl(__getcwd_error, "getcwd: buffer overflow detected"); | ||
| 26 | static inline __attribute__ ((always_inline)) | 19 | static inline __attribute__ ((always_inline)) |
| 27 | char * | 20 | char * |
| 28 | __fortify_getcwd(char *buf, size_t len) | 21 | __fortify_getcwd(char *buf, size_t len) |
| 29 | { | 22 | { |
| 30 | size_t bos = __builtin_object_size(buf, 0); | 23 | size_t bos = __builtin_object_size(buf, 0); |
| 31 | 24 | ||
| 32 | if (__builtin_constant_p(len) && len > bos) | ||
| 33 | __getcwd_error(); | ||
| 34 | |||
| 35 | if (len > bos) | 25 | if (len > bos) |
| 36 | __builtin_trap(); | 26 | __builtin_trap(); |
| 37 | return getcwd(buf, len); | 27 | return getcwd(buf, len); |
| 38 | } | 28 | } |
| 39 | 29 | ||
| 40 | __errordecl(__gethostname_error, "gethostname: buffer overflow detected"); | ||
| 41 | static inline __attribute__ ((always_inline)) | 30 | static inline __attribute__ ((always_inline)) |
| 42 | int | 31 | int |
| 43 | __fortify_gethostname(char *name, size_t len) | 32 | __fortify_gethostname(char *name, size_t len) |
| 44 | { | 33 | { |
| 45 | size_t bos = __builtin_object_size(name, 0); | 34 | size_t bos = __builtin_object_size(name, 0); |
| 46 | 35 | ||
| 47 | if (__builtin_constant_p(len) && len > bos) | ||
| 48 | __gethostname_error(); | ||
| 49 | |||
| 50 | if (len > bos) | 36 | if (len > bos) |
| 51 | __builtin_trap(); | 37 | __builtin_trap(); |
| 52 | return gethostname(name, len); | 38 | return gethostname(name, len); |
| 53 | } | 39 | } |
| 54 | 40 | ||
| 55 | __errordecl(__pread_error, "pread: buffer overflow detected"); | ||
| 56 | static inline __attribute__ ((always_inline)) | 41 | static inline __attribute__ ((always_inline)) |
| 57 | ssize_t | 42 | ssize_t |
| 58 | __fortify_pread(int fd, void *buf, size_t n, off_t offset) | 43 | __fortify_pread(int fd, void *buf, size_t n, off_t offset) |
| 59 | { | 44 | { |
| 60 | size_t bos = __builtin_object_size(buf, 0); | 45 | size_t bos = __builtin_object_size(buf, 0); |
| 61 | 46 | ||
| 62 | if (__builtin_constant_p(n) && n > bos) | ||
| 63 | __pread_error(); | ||
| 64 | |||
| 65 | if (n > bos) | 47 | if (n > bos) |
| 66 | __builtin_trap(); | 48 | __builtin_trap(); |
| 67 | return pread(fd, buf, n, offset); | 49 | return pread(fd, buf, n, offset); |
| 68 | } | 50 | } |
| 69 | 51 | ||
| 70 | __errordecl(__read_error, "read: buffer overflow detected"); | ||
| 71 | static inline __attribute__ ((always_inline)) | 52 | static inline __attribute__ ((always_inline)) |
| 72 | ssize_t | 53 | ssize_t |
| 73 | __fortify_read(int fd, void *buf, size_t n) | 54 | __fortify_read(int fd, void *buf, size_t n) |
| 74 | { | 55 | { |
| 75 | size_t bos = __builtin_object_size(buf, 0); | 56 | size_t bos = __builtin_object_size(buf, 0); |
| 76 | 57 | ||
| 77 | if (__builtin_constant_p(n) && n > bos) | ||
| 78 | __read_error(); | ||
| 79 | |||
| 80 | if (n > bos) | 58 | if (n > bos) |
| 81 | __builtin_trap(); | 59 | __builtin_trap(); |
| 82 | return read(fd, buf, n); | 60 | return read(fd, buf, n); |
| @@ -93,8 +71,6 @@ __fortify_read(int fd, void *buf, size_t n) | |||
| 93 | #undef read | 71 | #undef read |
| 94 | #define read(fd, buf, n) __fortify_read(fd, buf, n) | 72 | #define read(fd, buf, n) __fortify_read(fd, buf, n) |
| 95 | 73 | ||
| 96 | #undef __errordecl | ||
| 97 | |||
| 98 | #endif | 74 | #endif |
| 99 | 75 | ||
| 100 | #endif | 76 | #endif |
