fortify-headers

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

commit 9f8c543dc81f0c4239acae6713f5414eb7dc681d
parent b211796d68c4a6b56f999534627791f3576b6135
Author: sin <sin@2f30.org>
Date:   Fri, 13 Mar 2015 11:00:46 +0000

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:
Minclude/poll.h | 29++++++++++++-----------------
Minclude/stdio.h | 65+++++++++++++++++++++++++++++++++--------------------------------
Minclude/stdlib.h | 13++++++-------
Minclude/string.h | 151++++++++++++++++++++++++++++++++++++++-----------------------------------------
Minclude/strings.h | 25++++++++++++-------------
Minclude/sys/select.h | 10++++------
Minclude/sys/socket.h | 53++++++++++++++++++++++++++---------------------------
Minclude/unistd.h | 149++++++++++++++++++++++++++++++++++++++-----------------------------------------
Minclude/wchar.h | 201+++++++++++++++++++++++++++++++++++++++----------------------------------------
9 files changed, 335 insertions(+), 361 deletions(-)

diff --git a/include/poll.h b/include/poll.h @@ -6,40 +6,35 @@ #if defined(_FORTIFY_SOURCE) && _FORTIFY_SOURCE > 0 && defined(__OPTIMIZE__) && __OPTIMIZE__ > 0 #ifndef __cplusplus +#undef poll -static inline __attribute__ ((always_inline)) -int -__fortify_poll(struct pollfd *fds, nfds_t nfds, int timeout) +extern int __poll_orig(struct pollfd *, nfds_t, int) + __asm__(__USER_LABEL_PREFIX__ "poll"); +extern __inline __attribute__((__always_inline__,__gnu_inline__)) +int poll(struct pollfd *fds, nfds_t nfds, int timeout) { __typeof__(sizeof 0) bos = __builtin_object_size(fds, 0); if (nfds > bos / sizeof(struct pollfd)) __builtin_trap(); - return poll(fds, nfds, timeout); + return __poll_orig(fds, nfds, timeout); } #ifdef _GNU_SOURCE -static inline __attribute__ ((always_inline)) -int -__fortify_ppoll(struct pollfd *fds, nfds_t nfds, - const struct timespec *timeout, const sigset_t *mask) +#undef ppoll +extern int __ppoll_orig(struct pollfd *, nfds_t, const struct timespec *, const sigset_t *) + __asm__(__USER_LABEL_PREFIX__ "ppoll"); +extern __inline __attribute__((__always_inline__,__gnu_inline__)) +int ppoll(struct pollfd *fds, nfds_t nfds, const struct timespec *timeout, const sigset_t *mask) { __typeof__(sizeof 0) bos = __builtin_object_size(fds, 0); if (nfds > bos / sizeof(struct pollfd)) __builtin_trap(); - return ppoll(fds, nfds, timeout, mask); + return __ppoll_orig(fds, nfds, timeout, mask); } #endif -#undef poll -#define poll(fds, nfds, timeout) __fortify_poll(fds, nfds, timeout) - -#ifdef _GNU_SOURCE -#undef ppoll -#define ppoll(fds, nfds, timeout, mask) __fortify_ppoll(fds, nfds, timeout, mask) -#endif - #endif #endif diff --git a/include/stdio.h b/include/stdio.h @@ -6,21 +6,30 @@ #if defined(_FORTIFY_SOURCE) && _FORTIFY_SOURCE > 0 && defined(__OPTIMIZE__) && __OPTIMIZE__ > 0 #ifndef __cplusplus +#undef fgets +#undef fread +#undef fwrite +#undef vsnprintf +#undef vsnprintf +#undef snprintf +#undef sprintf -static inline __attribute__ ((always_inline)) -char * -__fortify_fgets(char *s, int n, FILE *fp) +extern char *__fgets_orig(char *, int, FILE *) + __asm__(__USER_LABEL_PREFIX__ "fgets"); +extern __inline __attribute__((__always_inline__,__gnu_inline__)) +char *fgets(char *s, int n, FILE *fp) { size_t bos = __builtin_object_size(s, 0); if ((size_t)n > bos) __builtin_trap(); - return fgets(s, n, fp); + return __fgets_orig(s, n, fp); } -static inline __attribute__ ((always_inline)) -size_t -__fortify_fread(void *dst, size_t n, size_t nmemb, FILE *fp) +extern size_t __fread_orig(void *, size_t, size_t, FILE *) + __asm__(__USER_LABEL_PREFIX__ "fread"); +extern __inline __attribute__((__always_inline__,__gnu_inline__)) +size_t fread(void *dst, size_t n, size_t nmemb, FILE *fp) { size_t bos = __builtin_object_size(dst, 0); @@ -28,12 +37,13 @@ __fortify_fread(void *dst, size_t n, size_t nmemb, FILE *fp) __builtin_trap(); if (n * nmemb > bos) __builtin_trap(); - return fread(dst, n, nmemb, fp); + return __fread_orig(dst, n, nmemb, fp); } -static inline __attribute__ ((always_inline)) -size_t -__fortify_fwrite(const void *dst, size_t n, size_t nmemb, FILE *fp) +extern size_t __fwrite_orig(const void *, size_t, size_t, FILE *) + __asm__(__USER_LABEL_PREFIX__ "fwrite"); +extern __inline __attribute__((__always_inline__,__gnu_inline__)) +size_t fwrite(const void *dst, size_t n, size_t nmemb, FILE *fp) { size_t bos = __builtin_object_size(dst, 0); @@ -41,48 +51,39 @@ __fortify_fwrite(const void *dst, size_t n, size_t nmemb, FILE *fp) __builtin_trap(); if (n * nmemb > bos) __builtin_trap(); - return fwrite(dst, n, nmemb, fp); + return __fwrite_orig(dst, n, nmemb, fp); } -static inline __attribute__ ((always_inline)) -int -__fortify_vsprintf(char *s, const char *fmt, __builtin_va_list ap) +extern int __vsprintf_orig(char *, const char *, __builtin_va_list) + __asm__(__USER_LABEL_PREFIX__ "vsprintf"); +extern __inline __attribute__((__always_inline__,__gnu_inline__)) +int vsprintf(char *s, const char *fmt, __builtin_va_list ap) { size_t bos = __builtin_object_size(s, 0); int r; if (bos != (size_t)-1) { - r = vsnprintf(s, bos, fmt, ap); + r = __vsnprintf_orig(s, bos, fmt, ap); if (r != -1 && (size_t)r >= bos) __builtin_trap(); } else { - r = vsprintf(s, fmt, ap); + r = __vsprintf_orig(s, fmt, ap); } return r; } -static inline __attribute__ ((always_inline)) -int -__fortify_vsnprintf(char *s, size_t n, const char *fmt, __builtin_va_list ap) +extern int __vsnprintf_orig(char *, size_t, const char *, __builtin_va_list) + __asm__(__USER_LABEL_PREFIX__ "vsnprintf"); +extern __inline __attribute__((__always_inline__,__gnu_inline__)) +int vsnprintf(char *s, size_t n, const char *fmt, __builtin_va_list ap) { size_t bos = __builtin_object_size(s, 0); if (n > bos) __builtin_trap(); - return vsnprintf(s, n, fmt, ap); + return __vsnprintf_orig(s, n, fmt, ap); } -#undef fgets -#define fgets(s, n, fp) __fortify_fgets(s, n, fp) -#undef fread -#define fread(dst, n, nmemb, fp) __fortify_fread(dst, n, nmemb, fp) -#undef fwrite -#define fwrite(dst, n, nmemb, fp) __fortify_fwrite(dst, n, nmemb, fp) -#undef vsprintf -#define vsprintf(s, fmt, ap) __fortify_vsprintf(s, fmt, ap) -#undef vsnprintf -#define vsnprintf(s, n, fmt, ap) __fortify_vsnprintf(s, n, fmt, ap) - #undef snprintf #define snprintf(s, n, fmt, ...) ({ \ size_t _n = n; \ diff --git a/include/stdlib.h b/include/stdlib.h @@ -12,9 +12,11 @@ #ifndef __cplusplus #if defined(_XOPEN_SOURCE) || defined(_GNU_SOURCE) || defined(_BSD_SOURCE) -static inline __attribute__ ((always_inline)) -char * -__fortify_realpath(const char *path, char *resolved) +#undef realpath +extern char *__realpath_orig(const char *, char *) + __asm__(__USER_LABEL_PREFIX__ "realpath"); +extern __inline __attribute__((__always_inline__,__gnu_inline__)) +char *realpath(const char *path, char *resolved) { size_t bos; @@ -27,11 +29,8 @@ __fortify_realpath(const char *path, char *resolved) __builtin_trap(); #endif } - return realpath(path, resolved); + return __realpath_orig(path, resolved); } - -#undef realpath -#define realpath(path, resolved) __fortify_realpath(path, resolved) #endif #endif diff --git 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 diff --git a/include/strings.h b/include/strings.h @@ -10,32 +10,31 @@ #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) -static inline __attribute__ ((always_inline)) -void -__fortify_bcopy(const void *src, void *dest, size_t n) +#undef bcopy +#undef bzero +extern void __bcopy_orig(const void *, void *, size_t) + __asm__(__USER_LABEL_PREFIX__ "bcopy"); +extern __inline __attribute__((__always_inline__,__gnu_inline__)) +void bcopy(const void *src, void *dest, size_t n) { size_t bos = __builtin_object_size(dest, 0); if (n > bos) __builtin_trap(); - return bcopy(src, dest, n); + return __bcopy_orig(src, dest, n); } -static inline __attribute__ ((always_inline)) -void -__fortify_bzero(void *src, size_t n) +extern void __bzero_orig(void *, size_t) + __asm__(__USER_LABEL_PREFIX__ "bzero"); +extern __inline __attribute__((__always_inline__,__gnu_inline__)) +void bzero(void *src, size_t n) { size_t bos = __builtin_object_size(src, 0); if (n > bos) __builtin_trap(); - return bzero(src, n); + return __bzero_orig(src, n); } - -#undef bcopy -#define bcopy(src, dest, n) __fortify_bcopy(src, dest, n) -#undef bzero -#define bzero(src, n) __fortify_bzero(src, n) #endif #endif diff --git a/include/sys/select.h b/include/sys/select.h @@ -7,9 +7,8 @@ #ifndef __cplusplus -static inline __attribute__ ((always_inline)) -int -__fortify_FD_CLR(int fd, fd_set *set) +static __inline __attribute__((__always_inline__,__gnu_inline__)) +int __fortify_FD_CLR(int fd, fd_set *set) { size_t bos = __builtin_object_size(set, 0); @@ -18,9 +17,8 @@ __fortify_FD_CLR(int fd, fd_set *set) return FD_CLR(fd, set); } -static inline __attribute__ ((always_inline)) -int -__fortify_FD_SET(int fd, fd_set *set) +static __inline __attribute__((__always_inline__,__gnu_inline__)) +int __fortify_FD_SET(int fd, fd_set *set) { size_t bos = __builtin_object_size(set, 0); diff --git a/include/sys/socket.h b/include/sys/socket.h @@ -6,62 +6,61 @@ #if defined(_FORTIFY_SOURCE) && _FORTIFY_SOURCE > 0 && defined(__OPTIMIZE__) && __OPTIMIZE__ > 0 #ifndef __cplusplus +#undef recv +#undef recvfrom +#undef send +#undef sendto -static inline __attribute__ ((always_inline)) -ssize_t -__fortify_recv(int sockfd, void *buf, size_t n, int flags) +extern ssize_t __recv_orig(int, void *, size_t, int) + __asm__(__USER_LABEL_PREFIX__ "recv"); +extern __inline __attribute__((__always_inline__,__gnu_inline__)) +ssize_t recv(int sockfd, void *buf, size_t n, int flags) { size_t bos = __builtin_object_size(buf, 0); if (n > bos) __builtin_trap(); - return recv(sockfd, buf, n, flags); + return __recv_orig(sockfd, buf, n, flags); } -static inline __attribute__ ((always_inline)) -ssize_t -__fortify_recvfrom(int sockfd, void *buf, size_t n, int flags, - struct sockaddr *sa, socklen_t *salen) +extern ssize_t __recvfrom_orig(int, void *, size_t, int, struct sockaddr *, socklen_t *) + __asm__(__USER_LABEL_PREFIX__ "recvfrom"); +extern __inline __attribute__((__always_inline__,__gnu_inline__)) +ssize_t 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 (n > bos) __builtin_trap(); - return recvfrom(sockfd, buf, n, flags, sa, salen); + return __recvfrom_orig(sockfd, buf, n, flags, sa, salen); } -static inline __attribute__ ((always_inline)) -ssize_t -__fortify_send(int sockfd, const void *buf, size_t n, int flags) +extern ssize_t __send_orig(int, const void *, size_t, int) + __asm__(__USER_LABEL_PREFIX__ "send"); +extern __inline __attribute__((__always_inline__,__gnu_inline__)) +ssize_t send(int sockfd, const void *buf, size_t n, int flags) { size_t bos = __builtin_object_size(buf, 0); if (n > bos) __builtin_trap(); - return send(sockfd, buf, n, flags); + return __send_orig(sockfd, buf, n, flags); } -static inline __attribute__ ((always_inline)) -ssize_t -__fortify_sendto(int sockfd, const void *buf, size_t n, int flags, - const struct sockaddr *sa, socklen_t salen) +extern ssize_t __sendto_orig(int, const void *, size_t, int, const struct sockaddr *, socklen_t) + __asm__(__USER_LABEL_PREFIX__ "sendto"); +extern __inline __attribute__((__always_inline__,__gnu_inline__)) +ssize_t sendto(int sockfd, const void *buf, size_t n, int flags, + const struct sockaddr *sa, socklen_t salen) { size_t bos = __builtin_object_size(buf, 0); if (n > bos) __builtin_trap(); - return sendto(sockfd, buf, n, flags, sa, salen); + return __sendto_orig(sockfd, buf, n, flags, sa, salen); } -#undef recv -#define recv(sockfd, buf, n, flags) __fortify_recv(sockfd, buf, n, flags) -#undef recvfrom -#define recvfrom(sockfd, buf, n, flags, sa, salen) __fortify_recvfrom(sockfd, buf, n, flags, sa, salen) -#undef send -#define send(sockfd, buf, n, flags) __fortify_send(sockfd, buf, n, flags) -#undef sendto -#define sendto(sockfd, buf, n, flags, sa, salen) __fortify_sendto(sockfd, buf, n, flags, sa, salen) - #endif #endif diff --git a/include/unistd.h b/include/unistd.h @@ -6,170 +6,165 @@ #if defined(_FORTIFY_SOURCE) && _FORTIFY_SOURCE > 0 && defined(__OPTIMIZE__) && __OPTIMIZE__ > 0 #ifndef __cplusplus +#undef confstr +#undef getcwd +#undef getgroups +#undef gethostname +#undef getlogin_r +#undef pread +#undef read +#undef readlink +#undef readlinkat +#undef ttyname_r +#undef write -static inline __attribute__ ((always_inline)) -size_t -__fortify_confstr(int name, char *buf, size_t len) +extern size_t __confstr_orig(int, char *, size_t) + __asm__(__USER_LABEL_PREFIX__ "confstr"); +extern __inline __attribute__((__always_inline__,__gnu_inline__)) +size_t confstr(int name, char *buf, size_t len) { size_t bos = __builtin_object_size(buf, 0); if (len > bos) __builtin_trap(); - return confstr(name, buf, len); + return __confstr_orig(name, buf, len); } -static inline __attribute__ ((always_inline)) -char * -__fortify_getcwd(char *buf, size_t len) +extern char *__getcwd_orig(char *, size_t) + __asm__(__USER_LABEL_PREFIX__ "getcwd"); +extern __inline __attribute__((__always_inline__,__gnu_inline__)) +char *getcwd(char *buf, size_t len) { size_t bos = __builtin_object_size(buf, 0); if (len > bos) __builtin_trap(); - return getcwd(buf, len); + return __getcwd_orig(buf, len); } #if defined(_GNU_SOURCE) || defined(_BSD_SOURCE) -static inline __attribute__ ((always_inline)) -int -__fortify_getdomainname(char *name, size_t len) +#undef getdomainname +extern int __getdomainname_orig(char *, size_t) + __asm__(__USER_LABEL_PREFIX__ "getdomainname"); +extern __inline __attribute__((__always_inline__,__gnu_inline__)) +int getdomainname(char *name, size_t len) { size_t bos = __builtin_object_size(name, 0); if (len > bos) __builtin_trap(); - return getdomainname(name, len); + return __getdomainname_orig(name, len); } #endif -static inline __attribute__ ((always_inline)) -int -__fortify_getgroups(int len, gid_t *set) +extern int __getgroups_orig(int, gid_t *) + __asm__(__USER_LABEL_PREFIX__ "getgroups"); +extern __inline __attribute__((__always_inline__,__gnu_inline__)) +int getgroups(int len, gid_t *set) { size_t bos = __builtin_object_size(set, 0); if (len > bos / sizeof(gid_t)) __builtin_trap(); - return getgroups(len, set); + return __getgroups_orig(len, set); } -static inline __attribute__ ((always_inline)) -int -__fortify_gethostname(char *name, size_t len) +extern int __gethostname_orig(char *, size_t) + __asm__(__USER_LABEL_PREFIX__ "gethostname"); +extern __inline __attribute__((__always_inline__,__gnu_inline__)) +int gethostname(char *name, size_t len) { size_t bos = __builtin_object_size(name, 0); if (len > bos) __builtin_trap(); - return gethostname(name, len); + return __gethostname_orig(name, len); } -static inline __attribute__ ((always_inline)) -int -__fortify_getlogin_r(char *name, size_t len) +extern int __getlogin_r_orig(char *, size_t) + __asm__(__USER_LABEL_PREFIX__ "getlogin_r"); +extern __inline __attribute__((__always_inline__,__gnu_inline__)) +int getlogin_r(char *name, size_t len) { size_t bos = __builtin_object_size(name, 0); if (len > bos) __builtin_trap(); - return getlogin_r(name, len); + return __getlogin_r_orig(name, len); } -static inline __attribute__ ((always_inline)) -ssize_t -__fortify_pread(int fd, void *buf, size_t n, off_t offset) +extern ssize_t __pread_orig(int, void *, size_t, off_t) + __asm__(__USER_LABEL_PREFIX__ "pread"); +extern __inline __attribute__((__always_inline__,__gnu_inline__)) +ssize_t pread(int fd, void *buf, size_t n, off_t offset) { size_t bos = __builtin_object_size(buf, 0); if (n > bos) __builtin_trap(); - return pread(fd, buf, n, offset); + return __pread_orig(fd, buf, n, offset); } -static inline __attribute__ ((always_inline)) -ssize_t -__fortify_read(int fd, void *buf, size_t n) +extern ssize_t __read_orig(int, void *, size_t) + __asm__(__USER_LABEL_PREFIX__ "read"); +extern __inline __attribute__((__always_inline__,__gnu_inline__)) +ssize_t read(int fd, void *buf, size_t n) { size_t bos = __builtin_object_size(buf, 0); if (n > bos) __builtin_trap(); - return read(fd, buf, n); + return __read_orig(fd, buf, n); } -static inline __attribute__ ((always_inline)) -ssize_t -__fortify_readlink(const char *path, char *buf, size_t n) +extern ssize_t __readlink_orig(const char *, char *, size_t) + __asm__(__USER_LABEL_PREFIX__ "readlink"); +extern __inline __attribute__((__always_inline__,__gnu_inline__)) +ssize_t readlink(const char *path, char *buf, size_t n) { size_t bos = __builtin_object_size(buf, 0); if (n > bos) __builtin_trap(); - return readlink(path, buf, n); + return __readlink_orig(path, buf, n); } -static inline __attribute__ ((always_inline)) -ssize_t -__fortify_readlinkat(int fd, const char *path, char *buf, size_t n) +extern ssize_t __readlinkat_orig(int, const char *, char *, size_t) + __asm__(__USER_LABEL_PREFIX__ "readlinkat"); +extern __inline __attribute__((__always_inline__,__gnu_inline__)) +ssize_t readlinkat(int fd, const char *path, char *buf, size_t n) { size_t bos = __builtin_object_size(buf, 0); if (n > bos) __builtin_trap(); - return readlinkat(fd, path, buf, n); + return __readlinkat_orig(fd, path, buf, n); } -static inline __attribute__ ((always_inline)) -int -__fortify_ttyname_r(int fd, char *name, size_t n) +extern int __ttyname_r_orig(int, char *, size_t) + __asm__(__USER_LABEL_PREFIX__ "ttyname_r"); +extern __inline __attribute__((__always_inline__,__gnu_inline__)) +int ttyname_r(int fd, char *name, size_t n) { size_t bos = __builtin_object_size(name, 0); if (n > bos) __builtin_trap(); - return ttyname_r(fd, name, n); + return __ttyname_r_orig(fd, name, n); } -static inline __attribute__ ((always_inline)) -ssize_t -__fortify_write(int fd, const void *buf, size_t n) +extern ssize_t __write_orig(int, const void *, size_t) + __asm__(__USER_LABEL_PREFIX__ "write"); +extern __inline __attribute__((__always_inline__,__gnu_inline__)) +ssize_t write(int fd, const void *buf, size_t n) { size_t bos = __builtin_object_size(buf, 0); if (n > bos) __builtin_trap(); - return write(fd, buf, n); + return __write_orig(fd, buf, n); } -#undef confstr -#define confstr(name, buf, len) __fortify_confstr(name, buf, len) -#undef getcwd -#define getcwd(buf, len) __fortify_getcwd(buf, len) - -#if defined(_GNU_SOURCE) || defined(_BSD_SOURCE) -#undef getdomainname -#define getdomainname(name, len) __fortify_getdomainname(name, len) -#endif - -#undef getgroups -#define getgroups(len, set) __fortify_getgroups(len, set) -#undef gethostname -#define gethostname(name, len) __fortify_gethostname(name, len) -#undef getlogin_r -#define getlogin_r(name, len) __fortify_getlogin_r(name, len) -#undef pread -#define pread(fd, buf, n, offset) __fortify_pread(fd, buf, n, offset) -#undef read -#define read(fd, buf, n) __fortify_read(fd, buf, n) -#undef readlink -#define readlink(path, buf, n) __fortify_readlink(path, buf, n) -#undef readlinkat -#define readlinkat(fd, path, buf, n) __fortify_readlinkat(fd, path, buf, n) -#undef ttyname_r -#define ttyname_r(fd, name, n) __fortify_ttyname_r(fd, name, n) -#undef write -#define write(fd, buf, n) __fortify_write(fd, buf, n) - #endif #endif diff --git a/include/wchar.h b/include/wchar.h @@ -7,101 +7,123 @@ #if defined(_FORTIFY_SOURCE) && _FORTIFY_SOURCE > 0 && defined(__OPTIMIZE__) && __OPTIMIZE__ > 0 #ifndef __cplusplus +#undef fgetws +#undef mbsnrtowcs +#undef mbsrtowcs +#undef mbstowcs +#undef wcrtomb +#undef wcscat +#undef wcscpy +#undef wcsncat +#undef wcsncpy +#undef wcsnrtombs +#undef wcsrtombs +#undef wcstombs +#undef wctomb +#undef wmemcpy +#undef wmemmove +#undef wmemset -static inline __attribute__ ((always_inline)) -wchar_t * -__fortify_fgetws(wchar_t *s, int n, FILE *fp) +extern wchar_t *__fgetws_orig(wchar_t *, int, FILE *) + __asm__(__USER_LABEL_PREFIX__ "fgetws"); +extern __inline __attribute__((__always_inline__,__gnu_inline__)) +wchar_t *fgetws(wchar_t *s, int n, FILE *fp) { size_t bos = __builtin_object_size(s, 0); if ((size_t)n > bos / sizeof(wchar_t)) __builtin_trap(); - return fgetws(s, n, fp); + return __fgetws_orig(s, n, fp); } -static inline __attribute__ ((always_inline)) -size_t -__fortify_mbsnrtowcs(wchar_t *d, const char **s, size_t n, - size_t wn, mbstate_t *st) +extern size_t __mbsnrtowcs(wchar_t *, const char **, size_t, size_t, mbstate_t *) + __asm__(__USER_LABEL_PREFIX__ "mbsnrtowcs"); +extern __inline __attribute__((__always_inline__,__gnu_inline__)) +size_t mbsnrtowcs(wchar_t *d, const char **s, size_t n, size_t wn, mbstate_t *st) { size_t bos = __builtin_object_size(d, 0); size_t r; if (wn > n / sizeof(wchar_t)) { bos /= sizeof(wchar_t); - r = mbsnrtowcs(d, s, n, wn > bos ? bos : wn, st); + r = __mbsnrtowcs_orig(d, s, n, wn > bos ? bos : wn, st); if (bos < wn && d && *s && r != (size_t)-1) __builtin_trap(); } else { - r = mbsnrtowcs(d, s, n > bos ? bos : n, wn, st); + r = __mbsnrtowcs_orig(d, s, n > bos ? bos : n, wn, st); if (bos < n && d && *s && r != (size_t)-1) __builtin_trap(); } return r; } -static inline __attribute__ ((always_inline)) -size_t -__fortify_mbsrtowcs(wchar_t *d, const char **s, - size_t wn, mbstate_t *st) +extern size_t __mbsrtowcs_orig(wchar_t *, const char **, size_t, mbstate_t *) + __asm__(__USER_LABEL_PREFIX__ "mbsrtowcs"); +extern __inline __attribute__((__always_inline__,__gnu_inline__)) +size_t mbsrtowcs(wchar_t *d, const char **s, size_t wn, mbstate_t *st) { size_t bos = __builtin_object_size(d, 0); size_t r; bos /= sizeof(wchar_t); - r = mbsrtowcs(d, s, wn > bos ? bos : wn, st); + r = __mbsrtowcs_orig(d, s, wn > bos ? bos : wn, st); if (bos < wn && d && *s && r != (size_t)-1) __builtin_trap(); return r; } -static inline __attribute__ ((always_inline)) -size_t -__fortify_mbstowcs(wchar_t *ws, const char *s, size_t wn) +extern size_t __mbstowcs_orig(wchar_t *, const char *, size_t) + __asm__(__USER_LABEL_PREFIX__ "mbstowcs"); +extern __inline __attribute__((__always_inline__,__gnu_inline__)) +size_t mbstowcs(wchar_t *ws, const char *s, size_t wn) { size_t bos = __builtin_object_size(ws, 0); if (ws && wn > bos / sizeof(wchar_t)) __builtin_trap(); - return mbstowcs(ws, s, wn); + return __mbstowcs_orig(ws, s, wn); } -static inline __attribute__ ((always_inline)) -size_t -__fortify_wcrtomb(char *s, wchar_t wc, mbstate_t *st) +extern size_t __wcrtomb_orig(char *, wchar_t, mbstate_t *) + __asm__(__USER_LABEL_PREFIX__ "wcrtomb"); +extern __inline __attribute__((__always_inline__,__gnu_inline__)) +size_t wcrtomb(char *s, wchar_t wc, mbstate_t *st) { size_t bos = __builtin_object_size(s, 0); if (s && MB_CUR_MAX > bos) __builtin_trap(); - return wcrtomb(s, wc, st); + return __wcrtomb_orig(s, wc, st); } -static inline __attribute__ ((always_inline)) -wchar_t * -__fortify_wcscat(wchar_t *d, const wchar_t *s) +extern wchar_t *__wcscat_orig(wchar_t *, const wchar_t *) + __asm__(__USER_LABEL_PREFIX__ "wcscat"); +extern __inline __attribute__((__always_inline__,__gnu_inline__)) +wchar_t *wcscat(wchar_t *d, const wchar_t *s) { size_t bos = __builtin_object_size(d, 0); if (wcslen(s) + wcslen(d) + 1 > bos / sizeof(wchar_t)) __builtin_trap(); - return wcscat(d, s); + return __wcscat_orig(d, s); } -static inline __attribute__ ((always_inline)) -wchar_t * -__fortify_wcscpy(wchar_t *d, const wchar_t *s) +extern wchar_t *__wcscpy_orig(wchar_t *, const wchar_t *) + __asm__(__USER_LABEL_PREFIX__ "wcscpy"); +extern __inline __attribute__((__always_inline__,__gnu_inline__)) +wchar_t *wcscpy(wchar_t *d, const wchar_t *s) { size_t bos = __builtin_object_size(d, 0); if (wcslen(s) + 1 > bos / sizeof(wchar_t)) __builtin_trap(); - return wcscpy(d, s); + return __wcscpy_orig(d, s); } -static inline __attribute__ ((always_inline)) -wchar_t * -__fortify_wcsncat(wchar_t *d, const wchar_t *s, size_t n) +extern wchar_t *__wcsncat_orig(wchar_t *, const wchar_t *, size_t) + __asm__(__USER_LABEL_PREFIX__ "wcsncat"); +extern __inline __attribute__((__always_inline__,__gnu_inline__)) +wchar_t *wcsncat(wchar_t *d, const wchar_t *s, size_t n) { size_t bos = __builtin_object_size(d, 0); size_t slen, dlen; @@ -114,143 +136,116 @@ __fortify_wcsncat(wchar_t *d, const wchar_t *s, size_t n) if (slen + dlen + 1 > bos / sizeof(wchar_t)) __builtin_trap(); } - return wcsncat(d, s, n); + return __wcsncat_orig(d, s, n); } -static inline __attribute__ ((always_inline)) -wchar_t * -__fortify_wcsncpy(wchar_t *d, const wchar_t *s, size_t n) +extern wchar_t *__wcsncpy_orig(wchar_t *, const wchar_t *, size_t) + __asm__(__USER_LABEL_PREFIX__ "wcsncpy"); +extern __inline __attribute__((__always_inline__,__gnu_inline__)) +wchar_t *wcsncpy(wchar_t *d, const wchar_t *s, size_t n) { size_t bos = __builtin_object_size(d, 0); if (n > bos / sizeof(wchar_t)) __builtin_trap(); - return wcsncpy(d, s, n); + return __wcsncpy_orig(d, s, n); } -static inline __attribute__ ((always_inline)) -size_t -__fortify_wcsnrtombs(char *d, const wchar_t **s, size_t wn, - size_t n, mbstate_t *st) +extern size_t __wcsnrtombs_orig(char *, const wchar_t **, size_t, size_t, mbstate_t *) + __asm__(__USER_LABEL_PREFIX__ "wcsnrtombs"); +extern __inline __attribute__((__always_inline__,__gnu_inline__)) +size_t wcsnrtombs(char *d, const wchar_t **s, size_t wn, size_t n, mbstate_t *st) { size_t bos = __builtin_object_size(d, 0); size_t r; if (wn > n / sizeof(wchar_t)) { bos /= sizeof(wchar_t); - r = wcsnrtombs(d, s, wn > bos ? bos : wn, n, st); + r = __wcsnrtombs_orig(d, s, wn > bos ? bos : wn, n, st); if (bos < wn && d && *s && r != (size_t)-1) __builtin_trap(); } else { - r = wcsnrtombs(d, s, wn, n > bos ? bos : n, st); + r = __wcsnrtombs_orig(d, s, wn, n > bos ? bos : n, st); if (bos < n && d && *s && r != (size_t)-1) __builtin_trap(); } return r; } -static inline __attribute__ ((always_inline)) -size_t -__fortify_wcsrtombs(char *d, const wchar_t **s, size_t n, - mbstate_t *st) +extern size_t __wcsrtombs_orig(char *, const wchar_t **, size_t, mbstate_t *) + __asm__(__USER_LABEL_PREFIX__ "wcsrtombs"); +extern __inline __attribute__((__always_inline__,__gnu_inline__)) +size_t wcsrtombs(char *d, const wchar_t **s, size_t n, mbstate_t *st) { size_t bos = __builtin_object_size(d, 0); size_t r; - r = wcsrtombs(d, s, n > bos ? bos : n, st); + r = __wcsrtombs_orig(d, s, n > bos ? bos : n, st); if (bos < n && d && *s && r != (size_t)-1) __builtin_trap(); return r; } -static inline __attribute__ ((always_inline)) -size_t -__fortify_wcstombs(char *s, const wchar_t *ws, size_t n) +extern size_t __wcstombs_orig(char *, const wchar_t *, size_t) + __asm__(__USER_LABEL_PREFIX__ "wcstombs"); +extern __inline __attribute__((__always_inline__,__gnu_inline__)) +size_t wcstombs(char *s, const wchar_t *ws, size_t n) { size_t bos = __builtin_object_size(s, 0); if (s && n > bos) __builtin_trap(); - return wcstombs(s, ws, n); + return __wcstombs_orig(s, ws, n); } -static inline __attribute__ ((always_inline)) -int -__fortify_wctomb(char *s, wchar_t wc) +extern int __wctomb_orig(char *, wchar_t) + __asm__(__USER_LABEL_PREFIX__ "wctomb"); +extern __inline __attribute__((__always_inline__,__gnu_inline__)) +int wctomb(char *s, wchar_t wc) { size_t bos = __builtin_object_size(s, 0); if (s && MB_CUR_MAX > bos) __builtin_trap(); - return wctomb(s, wc); + return __wctomb_orig(s, wc); } -static inline __attribute__ ((always_inline)) -wchar_t * -__fortify_wmemcpy(wchar_t *d, const wchar_t *s, size_t n) +extern wchar_t *__wmemcpy_orig(wchar_t *, const wchar_t *, size_t) + __asm__(__USER_LABEL_PREFIX__ "wmemcpy"); +extern __inline __attribute__((__always_inline__,__gnu_inline__)) +wchar_t *wmemcpy(wchar_t *d, const wchar_t *s, size_t n) { size_t bos = __builtin_object_size(d, 0); if (n > bos / sizeof(wchar_t)) __builtin_trap(); - return wmemcpy(d, s, n); + return __wmemcpy_orig(d, s, n); } -static inline __attribute__ ((always_inline)) -wchar_t * -__fortify_wmemmove(wchar_t *d, const wchar_t *s, size_t n) +extern wchar_t *__wmemmove_orig(wchar_t *, const wchar_t *, size_t) + __asm__(__USER_LABEL_PREFIX__ "wmemmove"); +extern __inline __attribute__((__always_inline__,__gnu_inline__)) +wchar_t *wmemmove(wchar_t *d, const wchar_t *s, size_t n) { size_t bos = __builtin_object_size(d, 0); if (n > bos / sizeof(wchar_t)) __builtin_trap(); - return wmemmove(d, s, n); + return __wmemmove_orig(d, s, n); } -static inline __attribute__ ((always_inline)) -wchar_t * -__fortify_wmemset(wchar_t *s, wchar_t c, size_t n) +extern wchar_t *__wmemset_orig(wchar_t *, wchar_t, size_t) + __asm__(__USER_LABEL_PREFIX__ "wmemset"); +extern __inline __attribute__((__always_inline__,__gnu_inline__)) +wchar_t *wmemset(wchar_t *s, wchar_t c, size_t n) { size_t bos = __builtin_object_size(s, 0); if (n > bos / sizeof(wchar_t)) __builtin_trap(); - return wmemset(s, c, n); + return __wmemset_orig(s, c, n); } -#undef fgetws -#define fgetws(s, n, fp) __fortify_fgetws(s, n, fp) -#undef mbsnrtowcs -#define mbsnrtowcs(d, s, n, wn, st) __fortify_mbsnrtowcs(d, s, n, wn, st) -#undef mbsrtowcs -#define mbsrtowcs(d, s, wn, st) __fortify_mbsrtowcs(d, s, wn, st) -#undef mbstowcs -#define mbstowcs(ws, s, wn) __fortify_mbstowcs(ws, s, wn) -#undef wcrtomb -#define wcrtomb(s, wc, st) __fortify_wcrtomb(s, wc, st) -#undef wcscat -#define wcscat(d, s) __fortify_wcscat(d, s) -#undef wcscpy -#define wcscpy(d, s) __fortify_wcscpy(d, s) -#undef wcsncat -#define wcsncat(d, s, n) __fortify_wcsncat(d, s, n) -#undef wcsncpy -#define wcsncpy(d, s, n) __fortify_wcsncpy(d, s, n) -#undef wcsnrtombs -#define wcsnrtombs(d, s, wn, n, st) __fortify_wcsnrtombs(d, s, wn, n, st) -#undef wcsrtombs -#define wcsrtombs(d, s, n, st) __fortify_wcsrtombs(d, s, n, st) -#undef wcstombs -#define wcstombs(s, ws, n) __fortify_wcstombs(s, ws, n) -#undef wctomb -#define wctomb(s, wc) __fortify_wctomb(s, wc) -#undef wmemcpy -#define wmemcpy(d, s, n) __fortify_wmemcpy(d, s, n) -#undef wmemmove -#define wmemmove(d, s, n) __fortify_wmemmove(d, s, n) -#undef wmemset -#define wmemset(s, c, n) __fortify_wmemset(s, c, n) - #endif #endif