fortify-headers

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

commit 158782b3bb791eae3c97947944c7023452bfbc96
parent 316a48653315b4bc36d8c50b542471b18441c9d5
Author: sin <sin@2f30.org>
Date:   Wed, 13 May 2015 12:04:15 +0100

Add fortify_fn() helper in fortify-headers.h

Diffstat:
Ainclude/fortify-headers.h | 7+++++++
Minclude/poll.h | 14++++++--------
Minclude/stdio.h | 48++++++++++++++++++------------------------------
Minclude/stdlib.h | 7+++----
Minclude/string.h | 73+++++++++++++++++++++++++------------------------------------------------
Minclude/strings.h | 13+++++--------
Minclude/sys/socket.h | 29+++++++++++------------------
Minclude/unistd.h | 73+++++++++++++++++++++++++------------------------------------------------
Minclude/wchar.h | 101++++++++++++++++++++++++++++---------------------------------------------------
9 files changed, 135 insertions(+), 230 deletions(-)

diff --git a/include/fortify-headers.h b/include/fortify-headers.h @@ -0,0 +1,7 @@ +#ifndef _FORTIFY_HEADERS_H +#define _FORTIFY_HEADERS_H + +#define fortify_fn(fn) __typeof__(fn) __orig_##fn __asm__(__USER_LABEL_PREFIX__ #fn); \ + extern __inline __attribute__((__always_inline__,__gnu_inline__,__artificial__)) + +#endif diff --git a/include/poll.h b/include/poll.h @@ -4,6 +4,7 @@ #include_next <poll.h> #if defined(_FORTIFY_SOURCE) && _FORTIFY_SOURCE > 0 && defined(__OPTIMIZE__) && __OPTIMIZE__ > 0 +#include "fortify-headers.h" #ifdef __cplusplus extern "C" { @@ -11,28 +12,25 @@ extern "C" { #undef poll -__typeof__(poll) __poll_orig __asm__(__USER_LABEL_PREFIX__ "poll"); -extern __inline __attribute__((__always_inline__,__gnu_inline__,__artificial__)) -int poll(struct pollfd *fds, nfds_t nfds, int timeout) +fortify_fn(poll) 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_orig(fds, nfds, timeout); + return __orig_poll(fds, nfds, timeout); } #ifdef _GNU_SOURCE #undef ppoll -__typeof__(ppoll) __ppoll_orig __asm__(__USER_LABEL_PREFIX__ "ppoll"); -extern __inline __attribute__((__always_inline__,__gnu_inline__,__artificial__)) -int ppoll(struct pollfd *fds, nfds_t nfds, const struct timespec *timeout, const sigset_t *mask) +fortify_fn(ppoll) 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_orig(fds, nfds, timeout, mask); + return __orig_ppoll(fds, nfds, timeout, mask); } #endif diff --git a/include/stdio.h b/include/stdio.h @@ -4,6 +4,7 @@ #include_next <stdio.h> #if defined(_FORTIFY_SOURCE) && _FORTIFY_SOURCE > 0 && defined(__OPTIMIZE__) && __OPTIMIZE__ > 0 +#include "fortify-headers.h" #ifdef __cplusplus extern "C" { @@ -17,20 +18,16 @@ extern "C" { #undef snprintf #undef sprintf -__typeof__(fgets) __fgets_orig __asm__(__USER_LABEL_PREFIX__ "fgets"); -extern __inline __attribute__((__always_inline__,__gnu_inline__,__artificial__)) -char *fgets(char *s, int n, FILE *fp) +fortify_fn(fgets) 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_orig(s, n, fp); + return __orig_fgets(s, n, fp); } -__typeof__(fread) __fread_orig __asm__(__USER_LABEL_PREFIX__ "fread"); -extern __inline __attribute__((__always_inline__,__gnu_inline__,__artificial__)) -size_t fread(void *dst, size_t n, size_t nmemb, FILE *fp) +fortify_fn(fread) size_t fread(void *dst, size_t n, size_t nmemb, FILE *fp) { size_t bos = __builtin_object_size(dst, 0); @@ -38,12 +35,10 @@ size_t fread(void *dst, size_t n, size_t nmemb, FILE *fp) __builtin_trap(); if (n * nmemb > bos) __builtin_trap(); - return __fread_orig(dst, n, nmemb, fp); + return __orig_fread(dst, n, nmemb, fp); } -__typeof__(fwrite) __fwrite_orig __asm__(__USER_LABEL_PREFIX__ "fwrite"); -extern __inline __attribute__((__always_inline__,__gnu_inline__,__artificial__)) -size_t fwrite(const void *dst, size_t n, size_t nmemb, FILE *fp) +fortify_fn(fwrite) size_t fwrite(const void *dst, size_t n, size_t nmemb, FILE *fp) { size_t bos = __builtin_object_size(dst, 0); @@ -51,61 +46,54 @@ size_t fwrite(const void *dst, size_t n, size_t nmemb, FILE *fp) __builtin_trap(); if (n * nmemb > bos) __builtin_trap(); - return __fwrite_orig(dst, n, nmemb, fp); + return __orig_fwrite(dst, n, nmemb, fp); } -__typeof__(vsnprintf) __vsnprintf_orig __asm__(__USER_LABEL_PREFIX__ "vsnprintf"); -extern __inline __attribute__((__always_inline__,__gnu_inline__,__artificial__)) -int vsnprintf(char *s, size_t n, const char *fmt, __builtin_va_list ap) +fortify_fn(vsnprintf) 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_orig(s, n, fmt, ap); + return __orig_vsnprintf(s, n, fmt, ap); } -__typeof__(vsprintf) __vsprintf_orig __asm__(__USER_LABEL_PREFIX__ "vsprintf"); -extern __inline __attribute__((__always_inline__,__gnu_inline__,__artificial__)) -int vsprintf(char *s, const char *fmt, __builtin_va_list ap) +fortify_fn(vsprintf) 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_orig(s, bos, fmt, ap); + r = __orig_vsnprintf(s, bos, fmt, ap); if (r != -1 && (size_t)r >= bos) __builtin_trap(); } else { - r = __vsprintf_orig(s, fmt, ap); + r = __orig_vsprintf(s, fmt, ap); } return r; } -__typeof__(snprintf) __snprintf_orig __asm__(__USER_LABEL_PREFIX__ "snprintf"); -extern __inline __attribute__((__always_inline__,__gnu_inline__,__artificial__)) -int snprintf(char *s, size_t n, const char *fmt, ...) +fortify_fn(snprintf) int snprintf(char *s, size_t n, const char *fmt, ...) { size_t bos = __builtin_object_size(s, 0); if (n > bos) __builtin_trap(); - return __snprintf_orig(s, n, fmt, __builtin_va_arg_pack()); + return __orig_snprintf(s, n, fmt, __builtin_va_arg_pack()); } -__typeof__(sprintf) __sprintf_orig __asm__(__USER_LABEL_PREFIX__ "sprintf"); -extern __inline __attribute__((__always_inline__,__gnu_inline__,__artificial__)) -int sprintf(char *s, const char *fmt, ...) +fortify_fn(sprintf) int sprintf(char *s, const char *fmt, ...) { size_t bos = __builtin_object_size(s, 0); int r; if (bos != (size_t)-1) { - r = __snprintf_orig(s, bos, fmt, __builtin_va_arg_pack()); + r = __orig_snprintf(s, bos, fmt, __builtin_va_arg_pack()); if (r != -1 && (size_t)r >= bos) __builtin_trap(); } else { - r = __sprintf_orig(s, fmt, __builtin_va_arg_pack()); + r = __orig_sprintf(s, fmt, __builtin_va_arg_pack()); } return r; } diff --git a/include/stdlib.h b/include/stdlib.h @@ -8,6 +8,7 @@ #endif #if defined(_FORTIFY_SOURCE) && _FORTIFY_SOURCE > 0 && defined(__OPTIMIZE__) && __OPTIMIZE__ > 0 +#include "fortify-headers.h" #ifdef __cplusplus extern "C" { @@ -15,9 +16,7 @@ extern "C" { #if defined(_XOPEN_SOURCE) || defined(_GNU_SOURCE) || defined(_BSD_SOURCE) #undef realpath -__typeof__(realpath) __realpath_orig __asm__(__USER_LABEL_PREFIX__ "realpath"); -extern __inline __attribute__((__always_inline__,__gnu_inline__,__artificial__)) -char *realpath(const char *path, char *resolved) +fortify_fn(realpath) char *realpath(const char *path, char *resolved) { size_t bos; @@ -30,7 +29,7 @@ char *realpath(const char *path, char *resolved) __builtin_trap(); #endif } - return __realpath_orig(path, resolved); + return __orig_realpath(path, resolved); } #endif diff --git a/include/string.h b/include/string.h @@ -4,6 +4,7 @@ #include_next <string.h> #if defined(_FORTIFY_SOURCE) && _FORTIFY_SOURCE > 0 && defined(__OPTIMIZE__) && __OPTIMIZE__ > 0 +#include "fortify-headers.h" #ifdef __cplusplus extern "C" { @@ -17,9 +18,7 @@ extern "C" { #undef strncat #undef strncpy -__typeof__(memcpy) __memcpy_orig __asm__(__USER_LABEL_PREFIX__ "memcpy"); -extern __inline __attribute__((__always_inline__,__gnu_inline__,__artificial__)) -void *memcpy(void *dst, const void *src, size_t n) +fortify_fn(memcpy) void *memcpy(void *dst, const void *src, size_t n) { size_t bos_dst = __builtin_object_size(dst, 0); size_t bos_src = __builtin_object_size(src, 0); @@ -33,85 +32,71 @@ void *memcpy(void *dst, const void *src, size_t n) __builtin_trap(); if (n > bos_dst || n > bos_src) __builtin_trap(); - return __memcpy_orig(dst, src, n); + return __orig_memcpy(dst, src, n); } -__typeof__(memmove) __memmove_orig __asm__(__USER_LABEL_PREFIX__ "memmove"); -extern __inline __attribute__((__always_inline__,__gnu_inline__,__artificial__)) -void *memmove(void *dst, const void *src, size_t n) +fortify_fn(memmove) void *memmove(void *dst, const void *src, size_t n) { size_t bos_dst = __builtin_object_size(dst, 0); size_t bos_src = __builtin_object_size(src, 0); if (n > bos_dst || n > bos_src) __builtin_trap(); - return __memmove_orig(dst, src, n); + return __orig_memmove(dst, src, n); } -__typeof__(memset) __memset_orig __asm__(__USER_LABEL_PREFIX__ "memset"); -extern __inline __attribute__((__always_inline__,__gnu_inline__,__artificial__)) -void *memset(void *dst, int c, size_t n) +fortify_fn(memset) void *memset(void *dst, int c, size_t n) { size_t bos = __builtin_object_size(dst, 0); if (n > bos) __builtin_trap(); - return __memset_orig(dst, c, n); + return __orig_memset(dst, c, n); } #if defined(_POSIX_SOURCE) || defined(_POSIX_C_SOURCE) \ || defined(_XOPEN_SOURCE) || defined(_GNU_SOURCE) \ || defined(_BSD_SOURCE) #undef stpcpy -__typeof__(stpcpy) __stpcpy_orig __asm__(__USER_LABEL_PREFIX__ "stpcpy"); -extern __inline __attribute__((__always_inline__,__gnu_inline__,__artificial__)) -char *stpcpy(char *dst, const char *src) +fortify_fn(stpcpy) char *stpcpy(char *dst, const char *src) { size_t bos = __builtin_object_size(dst, 0); if (strlen(src) + 1 > bos) __builtin_trap(); - return __stpcpy_orig(dst, src); + return __orig_stpcpy(dst, src); } #undef stpncpy -__typeof__(stpncpy) __stpncpy_orig __asm__(__USER_LABEL_PREFIX__ "stpncpy"); -extern __inline __attribute__((__always_inline__,__gnu_inline__,__artificial__)) -char *stpncpy(char *dst, const char *src, size_t n) +fortify_fn(stpncpy) char *stpncpy(char *dst, const char *src, size_t n) { size_t bos = __builtin_object_size(dst, 0); if (n > bos) __builtin_trap(); - return __stpncpy_orig(dst, src, n); + return __orig_stpncpy(dst, src, n); } #endif -__typeof__(strcat) __strcat_orig __asm__(__USER_LABEL_PREFIX__ "strcat"); -extern __inline __attribute__((__always_inline__,__gnu_inline__,__artificial__)) -char *strcat(char *dst, const char *src) +fortify_fn(strcat) char *strcat(char *dst, const char *src) { size_t bos = __builtin_object_size(dst, 0); if (strlen(src) + strlen(dst) + 1 > bos) __builtin_trap(); - return __strcat_orig(dst, src); + return __orig_strcat(dst, src); } -__typeof__(strcpy) __strcpy_orig __asm__(__USER_LABEL_PREFIX__ "strcpy"); -extern __inline __attribute__((__always_inline__,__gnu_inline__,__artificial__)) -char *strcpy(char *dst, const char *src) +fortify_fn(strcpy) char *strcpy(char *dst, const char *src) { size_t bos = __builtin_object_size(dst, 0); if (strlen(src) + 1 > bos) __builtin_trap(); - return __strcpy_orig(dst, src); + return __orig_strcpy(dst, src); } -__typeof__(strncat) __strncat_orig __asm__(__USER_LABEL_PREFIX__ "strncat"); -extern __inline __attribute__((__always_inline__,__gnu_inline__,__artificial__)) -char *strncat(char *dst, const char *src, size_t n) +fortify_fn(strncat) char *strncat(char *dst, const char *src, size_t n) { size_t bos = __builtin_object_size(dst, 0); size_t slen, dlen; @@ -124,58 +109,50 @@ char *strncat(char *dst, const char *src, size_t n) if (slen + dlen + 1 > bos) __builtin_trap(); } - return __strncat_orig(dst, src, n); + return __orig_strncat(dst, src, n); } -__typeof__(strncpy) __strncpy_orig __asm__(__USER_LABEL_PREFIX__ "strncpy"); -extern __inline __attribute__((__always_inline__,__gnu_inline__,__artificial__)) -char *strncpy(char *dst, const char *src, size_t n) +fortify_fn(strncpy) char *strncpy(char *dst, const char *src, size_t n) { size_t bos = __builtin_object_size(dst, 0); if (n > bos) __builtin_trap(); - return __strncpy_orig(dst, src, n); + return __orig_strncpy(dst, src, n); } #ifdef _GNU_SOURCE #undef mempcpy -__typeof__(mempcpy) __mempcpy_orig __asm__(__USER_LABEL_PREFIX__ "mempcpy"); -extern __inline __attribute__((__always_inline__,__gnu_inline__,__artificial__)) -void *mempcpy(void *dst, const void *src, size_t n) +fortify_fn(mempcpy) void *mempcpy(void *dst, const void *src, size_t n) { size_t bos_dst = __builtin_object_size(dst, 0); size_t bos_src = __builtin_object_size(src, 0); if (n > bos_dst || n > bos_src) __builtin_trap(); - return __mempcpy_orig(dst, src, n); + return __orig_mempcpy(dst, src, n); } #endif #if defined(_GNU_SOURCE) || defined(_BSD_SOURCE) #undef strlcat #undef strlcpy -__typeof__(strlcat) __strlcat_orig __asm__(__USER_LABEL_PREFIX__ "strlcat"); -extern __inline __attribute__((__always_inline__,__gnu_inline__,__artificial__)) -size_t strlcat(char *dst, const char *src, size_t n) +fortify_fn(strlcat) size_t strlcat(char *dst, const char *src, size_t n) { size_t bos = __builtin_object_size(dst, 0); if (n > bos) __builtin_trap(); - return __strlcat_orig(dst, src, n); + return __orig_strlcat(dst, src, n); } -__typeof__(strlcpy) __strlcpy_orig __asm__(__USER_LABEL_PREFIX__ "strlcpy"); -extern __inline __attribute__((__always_inline__,__gnu_inline__,__artificial__)) -size_t strlcpy(char *dst, const char *src, size_t n) +fortify_fn(strlcpy) size_t strlcpy(char *dst, const char *src, size_t n) { size_t bos = __builtin_object_size(dst, 0); if (n > bos) __builtin_trap(); - return __strlcpy_orig(dst, src, n); + return __orig_strlcpy(dst, src, n); } #endif diff --git a/include/strings.h b/include/strings.h @@ -4,6 +4,7 @@ #include_next <strings.h> #if defined(_FORTIFY_SOURCE) && _FORTIFY_SOURCE > 0 && defined(__OPTIMIZE__) && __OPTIMIZE__ > 0 +#include "fortify-headers.h" #ifdef __cplusplus extern "C" { @@ -14,27 +15,23 @@ extern "C" { || (defined(_XOPEN_SOURCE) && _XOPEN_SOURCE+0 < 700) #undef bcopy #undef bzero -__typeof__(bcopy) __bcopy_orig __asm__(__USER_LABEL_PREFIX__ "bcopy"); -extern __inline __attribute__((__always_inline__,__gnu_inline__,__artificial__)) -void bcopy(const void *src, void *dst, size_t n) +fortify_fn(bcopy) void bcopy(const void *src, void *dst, size_t n) { size_t bos_dst = __builtin_object_size(dst, 0); size_t bos_src = __builtin_object_size(src, 0); if (n > bos_dst || n > bos_src) __builtin_trap(); - return __bcopy_orig(src, dst, n); + return __orig_bcopy(src, dst, n); } -__typeof__(bzero) __bzero_orig __asm__(__USER_LABEL_PREFIX__ "bzero"); -extern __inline __attribute__((__always_inline__,__gnu_inline__,__artificial__)) -void bzero(void *src, size_t n) +fortify_fn(bzero) void bzero(void *src, size_t n) { size_t bos = __builtin_object_size(src, 0); if (n > bos) __builtin_trap(); - return __bzero_orig(src, n); + return __orig_bzero(src, n); } #endif diff --git a/include/sys/socket.h b/include/sys/socket.h @@ -4,6 +4,7 @@ #include_next <sys/socket.h> #if defined(_FORTIFY_SOURCE) && _FORTIFY_SOURCE > 0 && defined(__OPTIMIZE__) && __OPTIMIZE__ > 0 +#include "../fortify-headers.h" #ifdef __cplusplus extern "C" { @@ -14,50 +15,42 @@ extern "C" { #undef send #undef sendto -__typeof__(recv) __recv_orig __asm__(__USER_LABEL_PREFIX__ "recv"); -extern __inline __attribute__((__always_inline__,__gnu_inline__,__artificial__)) -ssize_t recv(int sockfd, void *buf, size_t n, int flags) +fortify_fn(recv) 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_orig(sockfd, buf, n, flags); + return __orig_recv(sockfd, buf, n, flags); } -__typeof__(recvfrom) __recvfrom_orig __asm__(__USER_LABEL_PREFIX__ "recvfrom"); -extern __inline __attribute__((__always_inline__,__gnu_inline__,__artificial__)) -ssize_t recvfrom(int sockfd, void *buf, size_t n, int flags, - struct sockaddr *sa, socklen_t *salen) +fortify_fn(recvfrom) 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_orig(sockfd, buf, n, flags, sa, salen); + return __orig_recvfrom(sockfd, buf, n, flags, sa, salen); } -__typeof__(send) __send_orig __asm__(__USER_LABEL_PREFIX__ "send"); -extern __inline __attribute__((__always_inline__,__gnu_inline__,__artificial__)) -ssize_t send(int sockfd, const void *buf, size_t n, int flags) +fortify_fn(send) 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_orig(sockfd, buf, n, flags); + return __orig_send(sockfd, buf, n, flags); } -__typeof__(sendto) __sendto_orig __asm__(__USER_LABEL_PREFIX__ "sendto"); -extern __inline __attribute__((__always_inline__,__gnu_inline__,__artificial__)) -ssize_t sendto(int sockfd, const void *buf, size_t n, int flags, - const struct sockaddr *sa, socklen_t salen) +fortify_fn(sendto) 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_orig(sockfd, buf, n, flags, sa, salen); + return __orig_sendto(sockfd, buf, n, flags, sa, salen); } #ifdef __cplusplus diff --git a/include/unistd.h b/include/unistd.h @@ -4,6 +4,7 @@ #include_next <unistd.h> #if defined(_FORTIFY_SOURCE) && _FORTIFY_SOURCE > 0 && defined(__OPTIMIZE__) && __OPTIMIZE__ > 0 +#include "fortify-headers.h" #ifdef __cplusplus extern "C" { @@ -21,139 +22,115 @@ extern "C" { #undef ttyname_r #undef write -__typeof__(confstr) __confstr_orig __asm__(__USER_LABEL_PREFIX__ "confstr"); -extern __inline __attribute__((__always_inline__,__gnu_inline__,__artificial__)) -size_t confstr(int name, char *buf, size_t len) +fortify_fn(confstr) 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_orig(name, buf, len); + return __orig_confstr(name, buf, len); } -__typeof__(getcwd) __getcwd_orig __asm__(__USER_LABEL_PREFIX__ "getcwd"); -extern __inline __attribute__((__always_inline__,__gnu_inline__,__artificial__)) -char *getcwd(char *buf, size_t len) +fortify_fn(getcwd) char *getcwd(char *buf, size_t len) { size_t bos = __builtin_object_size(buf, 0); if (len > bos) __builtin_trap(); - return __getcwd_orig(buf, len); + return __orig_getcwd(buf, len); } #if defined(_GNU_SOURCE) || defined(_BSD_SOURCE) #undef getdomainname -__typeof__(getdomainname) __getdomainname_orig __asm__(__USER_LABEL_PREFIX__ "getdomainname"); -extern __inline __attribute__((__always_inline__,__gnu_inline__,__artificial__)) -int getdomainname(char *name, size_t len) +fortify_fn(getdomainname) int getdomainname(char *name, size_t len) { size_t bos = __builtin_object_size(name, 0); if (len > bos) __builtin_trap(); - return __getdomainname_orig(name, len); + return __orig_getdomainname(name, len); } #endif -__typeof__(getgroups) __getgroups_orig __asm__(__USER_LABEL_PREFIX__ "getgroups"); -extern __inline __attribute__((__always_inline__,__gnu_inline__,__artificial__)) -int getgroups(int len, gid_t *set) +fortify_fn(getgroups) 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_orig(len, set); + return __orig_getgroups(len, set); } -__typeof__(gethostname) __gethostname_orig __asm__(__USER_LABEL_PREFIX__ "gethostname"); -extern __inline __attribute__((__always_inline__,__gnu_inline__,__artificial__)) -int gethostname(char *name, size_t len) +fortify_fn(gethostname) int gethostname(char *name, size_t len) { size_t bos = __builtin_object_size(name, 0); if (len > bos) __builtin_trap(); - return __gethostname_orig(name, len); + return __orig_gethostname(name, len); } -__typeof__(getlogin_r) __getlogin_r_orig __asm__(__USER_LABEL_PREFIX__ "getlogin_r"); -extern __inline __attribute__((__always_inline__,__gnu_inline__,__artificial__)) -int getlogin_r(char *name, size_t len) +fortify_fn(getlogin_r) int getlogin_r(char *name, size_t len) { size_t bos = __builtin_object_size(name, 0); if (len > bos) __builtin_trap(); - return __getlogin_r_orig(name, len); + return __orig_getlogin_r(name, len); } -__typeof__(pread) __pread_orig __asm__(__USER_LABEL_PREFIX__ "pread"); -extern __inline __attribute__((__always_inline__,__gnu_inline__,__artificial__)) -ssize_t pread(int fd, void *buf, size_t n, off_t offset) +fortify_fn(pread) 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_orig(fd, buf, n, offset); + return __orig_pread(fd, buf, n, offset); } -__typeof__(read) __read_orig __asm__(__USER_LABEL_PREFIX__ "read"); -extern __inline __attribute__((__always_inline__,__gnu_inline__,__artificial__)) -ssize_t read(int fd, void *buf, size_t n) +fortify_fn(read) 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_orig(fd, buf, n); + return __orig_read(fd, buf, n); } -__typeof__(readlink) __readlink_orig __asm__(__USER_LABEL_PREFIX__ "readlink"); -extern __inline __attribute__((__always_inline__,__gnu_inline__,__artificial__)) -ssize_t readlink(const char *path, char *buf, size_t n) +fortify_fn(readlink) 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_orig(path, buf, n); + return __orig_readlink(path, buf, n); } -__typeof__(readlinkat) __readlinkat_orig __asm__(__USER_LABEL_PREFIX__ "readlinkat"); -extern __inline __attribute__((__always_inline__,__gnu_inline__,__artificial__)) -ssize_t readlinkat(int fd, const char *path, char *buf, size_t n) +fortify_fn(readlinkat) 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_orig(fd, path, buf, n); + return __orig_readlinkat(fd, path, buf, n); } -__typeof__(ttyname_r) __ttyname_r_orig __asm__(__USER_LABEL_PREFIX__ "ttyname_r"); -extern __inline __attribute__((__always_inline__,__gnu_inline__,__artificial__)) -int ttyname_r(int fd, char *name, size_t n) +fortify_fn(ttyname_r) 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_orig(fd, name, n); + return __orig_ttyname_r(fd, name, n); } -__typeof__(write) __write_orig __asm__(__USER_LABEL_PREFIX__ "write"); -extern __inline __attribute__((__always_inline__,__gnu_inline__,__artificial__)) -ssize_t write(int fd, const void *buf, size_t n) +fortify_fn(write) 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_orig(fd, buf, n); + return __orig_write(fd, buf, n); } #ifdef __cplusplus diff --git a/include/wchar.h b/include/wchar.h @@ -5,6 +5,7 @@ #include_next <wchar.h> #if defined(_FORTIFY_SOURCE) && _FORTIFY_SOURCE > 0 && defined(__OPTIMIZE__) && __OPTIMIZE__ > 0 +#include "fortify-headers.h" #ifdef __cplusplus extern "C" { @@ -25,34 +26,30 @@ extern "C" { #undef wmemmove #undef wmemset -__typeof__(fgetws) __fgetws_orig __asm__(__USER_LABEL_PREFIX__ "fgetws"); -extern __inline __attribute__((__always_inline__,__gnu_inline__,__artificial__)) -wchar_t *fgetws(wchar_t *s, int n, FILE *fp) +fortify_fn(fgetws) 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_orig(s, n, fp); + return __orig_fgetws(s, n, fp); } #if defined(_POSIX_SOURCE) || defined(_POSIX_C_SOURCE) \ || defined(_XOPEN_SOURCE) || defined(_GNU_SOURCE) || defined(_BSD_SOURCE) #undef mbsnrtowcs -__typeof__(mbsnrtowcs) __mbsnrtowcs_orig __asm__(__USER_LABEL_PREFIX__ "mbsnrtowcs"); -extern __inline __attribute__((__always_inline__,__gnu_inline__,__artificial__)) -size_t mbsnrtowcs(wchar_t *d, const char **s, size_t n, size_t wn, mbstate_t *st) +fortify_fn(mbsnrtowcs) 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_orig(d, s, n, wn > bos ? bos : wn, st); + r = __orig_mbsnrtowcs(d, s, n, wn > bos ? bos : wn, st); if (bos < wn && d && *s && r != (size_t)-1) __builtin_trap(); } else { - r = __mbsnrtowcs_orig(d, s, n > bos ? bos : n, wn, st); + r = __orig_mbsnrtowcs(d, s, n > bos ? bos : n, wn, st); if (bos < n && d && *s && r != (size_t)-1) __builtin_trap(); } @@ -60,67 +57,55 @@ size_t mbsnrtowcs(wchar_t *d, const char **s, size_t n, size_t wn, mbstate_t *st } #endif -__typeof__(mbsrtowcs) __mbsrtowcs_orig __asm__(__USER_LABEL_PREFIX__ "mbsrtowcs"); -extern __inline __attribute__((__always_inline__,__gnu_inline__,__artificial__)) -size_t mbsrtowcs(wchar_t *d, const char **s, size_t wn, mbstate_t *st) +fortify_fn(mbsrtowcs) 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_orig(d, s, wn > bos ? bos : wn, st); + r = __orig_mbsrtowcs(d, s, wn > bos ? bos : wn, st); if (bos < wn && d && *s && r != (size_t)-1) __builtin_trap(); return r; } -__typeof__(mbstowcs) __mbstowcs_orig __asm__(__USER_LABEL_PREFIX__ "mbstowcs"); -extern __inline __attribute__((__always_inline__,__gnu_inline__,__artificial__)) -size_t mbstowcs(wchar_t *ws, const char *s, size_t wn) +fortify_fn(mbstowcs) 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_orig(ws, s, wn); + return __orig_mbstowcs(ws, s, wn); } -__typeof__(wcrtomb) __wcrtomb_orig __asm__(__USER_LABEL_PREFIX__ "wcrtomb"); -extern __inline __attribute__((__always_inline__,__gnu_inline__,__artificial__)) -size_t wcrtomb(char *s, wchar_t wc, mbstate_t *st) +fortify_fn(wcrtomb) 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_orig(s, wc, st); + return __orig_wcrtomb(s, wc, st); } -__typeof__(wcscat) __wcscat_orig __asm__(__USER_LABEL_PREFIX__ "wcscat"); -extern __inline __attribute__((__always_inline__,__gnu_inline__,__artificial__)) -wchar_t *wcscat(wchar_t *d, const wchar_t *s) +fortify_fn(wcscat) 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_orig(d, s); + return __orig_wcscat(d, s); } -__typeof__(wcscpy) __wcscpy_orig __asm__(__USER_LABEL_PREFIX__ "wcscpy"); -extern __inline __attribute__((__always_inline__,__gnu_inline__,__artificial__)) -wchar_t *wcscpy(wchar_t *d, const wchar_t *s) +fortify_fn(wcscpy) 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_orig(d, s); + return __orig_wcscpy(d, s); } -__typeof__(wcsncat) __wcsncat_orig __asm__(__USER_LABEL_PREFIX__ "wcsncat"); -extern __inline __attribute__((__always_inline__,__gnu_inline__,__artificial__)) -wchar_t *wcsncat(wchar_t *d, const wchar_t *s, size_t n) +fortify_fn(wcsncat) 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; @@ -133,37 +118,33 @@ wchar_t *wcsncat(wchar_t *d, const wchar_t *s, size_t n) if (slen + dlen + 1 > bos / sizeof(wchar_t)) __builtin_trap(); } - return __wcsncat_orig(d, s, n); + return __orig_wcsncat(d, s, n); } -__typeof__(wcsncpy) __wcsncpy_orig __asm__(__USER_LABEL_PREFIX__ "wcsncpy"); -extern __inline __attribute__((__always_inline__,__gnu_inline__,__artificial__)) -wchar_t *wcsncpy(wchar_t *d, const wchar_t *s, size_t n) +fortify_fn(wcsncpy) 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_orig(d, s, n); + return __orig_wcsncpy(d, s, n); } #if defined(_POSIX_SOURCE) || defined(_POSIX_C_SOURCE) \ || defined(_XOPEN_SOURCE) || defined(_GNU_SOURCE) || defined(_BSD_SOURCE) #undef wcsnrtombs -__typeof__(wcsnrtombs) __wcsnrtombs_orig __asm__(__USER_LABEL_PREFIX__ "wcsnrtombs"); -extern __inline __attribute__((__always_inline__,__gnu_inline__,__artificial__)) -size_t wcsnrtombs(char *d, const wchar_t **s, size_t wn, size_t n, mbstate_t *st) +fortify_fn(wcsnrtombs) 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_orig(d, s, wn > bos ? bos : wn, n, st); + r = __orig_wcsnrtombs(d, s, wn > bos ? bos : wn, n, st); if (bos < wn && d && *s && r != (size_t)-1) __builtin_trap(); } else { - r = __wcsnrtombs_orig(d, s, wn, n > bos ? bos : n, st); + r = __orig_wcsnrtombs(d, s, wn, n > bos ? bos : n, st); if (bos < n && d && *s && r != (size_t)-1) __builtin_trap(); } @@ -171,72 +152,60 @@ size_t wcsnrtombs(char *d, const wchar_t **s, size_t wn, size_t n, mbstate_t *st } #endif -__typeof__(wcsrtombs) __wcsrtombs_orig __asm__(__USER_LABEL_PREFIX__ "wcsrtombs"); -extern __inline __attribute__((__always_inline__,__gnu_inline__,__artificial__)) -size_t wcsrtombs(char *d, const wchar_t **s, size_t n, mbstate_t *st) +fortify_fn(wcsrtombs) 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_orig(d, s, n > bos ? bos : n, st); + r = __orig_wcsrtombs(d, s, n > bos ? bos : n, st); if (bos < n && d && *s && r != (size_t)-1) __builtin_trap(); return r; } -__typeof__(wcstombs) __wcstombs_orig __asm__(__USER_LABEL_PREFIX__ "wcstombs"); -extern __inline __attribute__((__always_inline__,__gnu_inline__,__artificial__)) -size_t wcstombs(char *s, const wchar_t *ws, size_t n) +fortify_fn(wcstombs) 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_orig(s, ws, n); + return __orig_wcstombs(s, ws, n); } -__typeof__(wctomb) __wctomb_orig __asm__(__USER_LABEL_PREFIX__ "wctomb"); -extern __inline __attribute__((__always_inline__,__gnu_inline__,__artificial__)) -int wctomb(char *s, wchar_t wc) +fortify_fn(wctomb) 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_orig(s, wc); + return __orig_wctomb(s, wc); } -__typeof__(wmemcpy) __wmemcpy_orig __asm__(__USER_LABEL_PREFIX__ "wmemcpy"); -extern __inline __attribute__((__always_inline__,__gnu_inline__,__artificial__)) -wchar_t *wmemcpy(wchar_t *d, const wchar_t *s, size_t n) +fortify_fn(wmemcpy) 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_orig(d, s, n); + return __orig_wmemcpy(d, s, n); } -__typeof__(wmemmove) __wmemmove_orig __asm__(__USER_LABEL_PREFIX__ "wmemmove"); -extern __inline __attribute__((__always_inline__,__gnu_inline__,__artificial__)) -wchar_t *wmemmove(wchar_t *d, const wchar_t *s, size_t n) +fortify_fn(wmemmove) 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_orig(d, s, n); + return __orig_wmemmove(d, s, n); } -__typeof__(wmemset) __wmemset_orig __asm__(__USER_LABEL_PREFIX__ "wmemset"); -extern __inline __attribute__((__always_inline__,__gnu_inline__,__artificial__)) -wchar_t *wmemset(wchar_t *s, wchar_t c, size_t n) +fortify_fn(wmemset) 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_orig(s, c, n); + return __orig_wmemset(s, c, n); } #ifdef __cplusplus