fortify-headers

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

commit eecef18261cc278fbc13ecbfb4e5bc10762cc794
parent 9a77136c5914f6be50df195dac0f99424252a297
Author: sin <sin@2f30.org>
Date:   Tue, 24 Feb 2015 18:12:27 +0000

Remove compile time checks

These can produce false positives.  Given that we support fortify
source level 1 we shouldn't break valid code.

Diffstat:
Minclude/stdio.h | 15---------------
Minclude/string.h | 40----------------------------------------
Minclude/strings.h | 12------------
Minclude/sys/socket.h | 12------------
Minclude/unistd.h | 24------------------------
5 files changed, 0 insertions(+), 103 deletions(-)

diff --git a/include/stdio.h b/include/stdio.h @@ -5,24 +5,17 @@ #if defined(_FORTIFY_SOURCE) && _FORTIFY_SOURCE > 0 && defined(__OPTIMIZE__) && __OPTIMIZE__ > 0 -#define __errordecl(name, msg) extern void name(void) __attribute__ ((__error__(msg))) - -__errordecl(__fgets_error, "fgets: buffer overflow detected"); static inline __attribute__ ((always_inline)) char * __fortify_fgets(char *s, int n, FILE *fp) { size_t bos = __builtin_object_size(s, 0); - if (__builtin_constant_p(n) && (size_t)n > bos) - __fgets_error(); - if ((size_t)n > bos) __builtin_trap(); return fgets(s, n, fp); } -__errordecl(__vsnprintf_error, "vsnprintf: buffer overflow detected"); static inline __attribute__ ((always_inline)) __attribute__ ((__format__ (printf, 3, 0))) @@ -32,9 +25,6 @@ __fortify_vsnprintf(char *s, size_t n, const char *fmt, __builtin_va_list ap) { size_t bos = __builtin_object_size(s, 0); - if (__builtin_constant_p(n) && n > bos) - __vsnprintf_error(); - if (n > bos) __builtin_trap(); return vsnprintf(s, n, fmt, ap); @@ -45,20 +35,15 @@ __fortify_vsnprintf(char *s, size_t n, const char *fmt, __builtin_va_list ap) #undef vsnprintf #define vsnprintf(s, n, fmt, ap) __fortify_vsnprintf(s, n, fmt, ap) -__errordecl(__snprintf_error, "snprintf: buffer overflow detected"); #undef snprintf #define snprintf(s, n, fmt, ...) ({ \ size_t _n = (n); \ size_t bos = __builtin_object_size(s, 0); \ - if (__builtin_constant_p(_n) && _n > bos) \ - __snprintf_error(); \ if (_n > bos) \ __builtin_trap(); \ snprintf(s, _n, fmt, ## __VA_ARGS__); \ }) -#undef __errordecl - #endif #endif diff --git a/include/string.h b/include/string.h @@ -5,9 +5,6 @@ #if defined(_FORTIFY_SOURCE) && _FORTIFY_SOURCE > 0 && defined(__OPTIMIZE__) && __OPTIMIZE__ > 0 -#define __errordecl(name, msg) extern void name(void) __attribute__ ((__error__(msg))) - -__errordecl(__memcpy_error, "memcpy: buffer overflow detected"); static inline __attribute__ ((always_inline)) void * __fortify_memcpy(void *dest, const void *src, size_t n) @@ -16,9 +13,6 @@ __fortify_memcpy(void *dest, const void *src, size_t n) char *d = dest; const char *s = src; - if (__builtin_constant_p(n) && n > bos) - __memcpy_error(); - /* trap if pointers are overlapping but not if dest == src */ if ((d < s && d + n > s) || (s < d && s + n > d)) @@ -28,31 +22,23 @@ __fortify_memcpy(void *dest, const void *src, size_t n) return memcpy(dest, src, n); } -__errordecl(__memmove_error, "memmove: buffer overflow detected"); static inline __attribute__ ((always_inline)) void * __fortify_memmove(void *dest, const void *src, size_t n) { size_t bos = __builtin_object_size(dest, 0); - if (__builtin_constant_p(n) && n > bos) - __memmove_error(); - if (n > bos) __builtin_trap(); return memmove(dest, src, n); } -__errordecl(__memset_error, "memset: buffer overflow detected"); static inline __attribute__ ((always_inline)) void * __fortify_memset(void *dest, int c, size_t n) { size_t bos = __builtin_object_size(dest, 0); - if (__builtin_constant_p(n) && n > bos) - __memset_error(); - if (n > bos) __builtin_trap(); return memset(dest, c, n); @@ -69,16 +55,12 @@ __fortify_stpcpy(char *dest, const char *src) return stpcpy(dest, src); } -__errordecl(__stpncpy_error, "stpncpy: buffer overflow detected"); static inline __attribute__ ((always_inline)) char * __fortify_stpncpy(char *dest, const char *src, size_t n) { size_t bos = __builtin_object_size(dest, 0); - if (__builtin_constant_p(n) && n > bos) - __stpncpy_error(); - if (n > bos) __builtin_trap(); return stpncpy(dest, src, n); @@ -106,7 +88,6 @@ __fortify_strcpy(char *dest, const char *src) return strcpy(dest, src); } -__errordecl(__strncat_error, "strncat: buffer overflow detected"); static inline __attribute__ ((always_inline)) char * __fortify_strncat(char *dest, const char *src, size_t n) @@ -114,9 +95,6 @@ __fortify_strncat(char *dest, const char *src, size_t n) size_t bos = __builtin_object_size(dest, 0); size_t slen, dlen; - if (__builtin_constant_p(n) && n > bos) - __strncat_error(); - if (n > bos) { slen = strlen(src); dlen = strlen(dest); @@ -128,32 +106,24 @@ __fortify_strncat(char *dest, const char *src, size_t n) return strncat(dest, src, n); } -__errordecl(__strncpy_error, "strncpy: buffer overflow detected"); static inline __attribute__ ((always_inline)) char * __fortify_strncpy(char *dest, const char *src, size_t n) { size_t bos = __builtin_object_size(dest, 0); - if (__builtin_constant_p(n) && n > bos) - __strncpy_error(); - if (n > bos) __builtin_trap(); return strncpy(dest, src, n); } #ifdef _GNU_SOURCE -__errordecl(__mempcpy_error, "mempcpy: buffer overflow detected"); static inline __attribute__ ((always_inline)) void * __fortify_mempcpy(void *dest, const void *src, size_t n) { size_t bos = __builtin_object_size(dest, 0); - if (__builtin_constant_p(n) && n > bos) - __mempcpy_error(); - if (n > bos) __builtin_trap(); return mempcpy(dest, src, n); @@ -161,31 +131,23 @@ __fortify_mempcpy(void *dest, const void *src, size_t n) #endif #if defined(_GNU_SOURCE) || defined(_BSD_SOURCE) -__errordecl(__strlcat_error, "strlcat: buffer overflow detected"); static inline __attribute__ ((always_inline)) size_t __fortify_strlcat(char *dest, const char *src, size_t n) { size_t bos = __builtin_object_size(dest, 0); - if (__builtin_constant_p(n) && n > bos) - __strlcat_error(); - if (n > bos) __builtin_trap(); return strlcat(dest, src, n); } -__errordecl(__strlcpy_error, "strlcpy: buffer overflow detected"); static inline __attribute__ ((always_inline)) size_t __fortify_strlcpy(char *dest, const char *src, size_t n) { size_t bos = __builtin_object_size(dest, 0); - if (__builtin_constant_p(n) && n > bos) - __strlcpy_error(); - if (n > bos) __builtin_trap(); return strlcpy(dest, src, n); @@ -223,8 +185,6 @@ __fortify_strlcpy(char *dest, const char *src, size_t n) #define strlcpy(dest, src, n) __fortify_strlcpy(dest, src, n) #endif -#undef __errordecl - #endif #endif diff --git a/include/strings.h b/include/strings.h @@ -5,36 +5,26 @@ #if defined(_FORTIFY_SOURCE) && _FORTIFY_SOURCE > 0 && defined(__OPTIMIZE__) && __OPTIMIZE__ > 0 -#define __errordecl(name, msg) extern void name(void) __attribute__ ((__error__(msg))) - #if defined(_GNU_SOURCE) || defined(_BSD_SOURCE) || defined(_POSIX_SOURCE) \ || (defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE+0 < 200809L) \ || (defined(_XOPEN_SOURCE) && _XOPEN_SOURCE+0 < 700) -__errordecl(__bcopy_error, "bcopy: buffer overflow detected"); static inline __attribute__ ((always_inline)) void __fortify_bcopy(const void *src, void *dest, size_t n) { size_t bos = __builtin_object_size(dest, 0); - if (__builtin_constant_p(n) && n > bos) - __bcopy_error(); - if (n > bos) __builtin_trap(); return bcopy(src, dest, n); } -__errordecl(__bzero_error, "bzero: buffer overflow detected"); static inline __attribute__ ((always_inline)) void __fortify_bzero(void *src, size_t n) { size_t bos = __builtin_object_size(src, 0); - if (__builtin_constant_p(n) && n > bos) - __bzero_error(); - if (n > bos) __builtin_trap(); return bzero(src, n); @@ -46,8 +36,6 @@ __fortify_bzero(void *src, size_t n) #define bzero(src, n) __fortify_bzero(src, n) #endif -#undef __errordecl - #endif #endif diff --git a/include/sys/socket.h b/include/sys/socket.h @@ -5,33 +5,23 @@ #if defined(_FORTIFY_SOURCE) && _FORTIFY_SOURCE > 0 && defined(__OPTIMIZE__) && __OPTIMIZE__ > 0 -#define __errordecl(name, msg) extern void name(void) __attribute__ ((__error__(msg))) - -__errordecl(__recv_error, "recv: buffer overflow detected"); static inline __attribute__ ((always_inline)) ssize_t __fortify_recv(int sockfd, void *buf, size_t n, int flags) { size_t bos = __builtin_object_size(buf, 0); - if (__builtin_constant_p(n) && n > bos) - __recv_error(); - if (n > bos) __builtin_trap(); return recv(sockfd, buf, n, flags); } -__errordecl(__recvfrom_error, "recvfrom: buffer overflow detected"); static inline __attribute__ ((always_inline)) ssize_t __fortify_recvfrom(int sockfd, void *buf, size_t n, int flags, struct sockaddr *sa, socklen_t *salen) { size_t bos = __builtin_object_size(buf, 0); - if (__builtin_constant_p(n) && n > bos) - __recvfrom_error(); - if (n > bos) __builtin_trap(); 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 * #undef recvfrom #define recvfrom(sockfd, buf, n, flags, sa, salen) __fortify_recvfrom(sockfd, buf, n, flags, sa, salen) -#undef __errordecl - #endif #endif diff --git a/include/unistd.h b/include/unistd.h @@ -5,78 +5,56 @@ #if defined(_FORTIFY_SOURCE) && _FORTIFY_SOURCE > 0 && defined(__OPTIMIZE__) && __OPTIMIZE__ > 0 -#define __errordecl(name, msg) extern void name(void) __attribute__ ((__error__(msg))) - -__errordecl(__confstr_error, "confstr: buffer overflow detected"); static inline __attribute__ ((always_inline)) size_t __fortify_confstr(int name, char *buf, size_t len) { size_t bos = __builtin_object_size(buf, 0); - if (__builtin_constant_p(len) && len > bos) - __confstr_error(); - if (len > bos) __builtin_trap(); return confstr(name, buf, len); } -__errordecl(__getcwd_error, "getcwd: buffer overflow detected"); static inline __attribute__ ((always_inline)) char * __fortify_getcwd(char *buf, size_t len) { size_t bos = __builtin_object_size(buf, 0); - if (__builtin_constant_p(len) && len > bos) - __getcwd_error(); - if (len > bos) __builtin_trap(); return getcwd(buf, len); } -__errordecl(__gethostname_error, "gethostname: buffer overflow detected"); static inline __attribute__ ((always_inline)) int __fortify_gethostname(char *name, size_t len) { size_t bos = __builtin_object_size(name, 0); - if (__builtin_constant_p(len) && len > bos) - __gethostname_error(); - if (len > bos) __builtin_trap(); return gethostname(name, len); } -__errordecl(__pread_error, "pread: buffer overflow detected"); static inline __attribute__ ((always_inline)) ssize_t __fortify_pread(int fd, void *buf, size_t n, off_t offset) { size_t bos = __builtin_object_size(buf, 0); - if (__builtin_constant_p(n) && n > bos) - __pread_error(); - if (n > bos) __builtin_trap(); return pread(fd, buf, n, offset); } -__errordecl(__read_error, "read: buffer overflow detected"); static inline __attribute__ ((always_inline)) ssize_t __fortify_read(int fd, void *buf, size_t n) { size_t bos = __builtin_object_size(buf, 0); - if (__builtin_constant_p(n) && n > bos) - __read_error(); - if (n > bos) __builtin_trap(); return read(fd, buf, n); @@ -93,8 +71,6 @@ __fortify_read(int fd, void *buf, size_t n) #undef read #define read(fd, buf, n) __fortify_read(fd, buf, n) -#undef __errordecl - #endif #endif