fortify-headers

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

commit 75b95fa25df74fcd0498bf59e3524f20f594755d
parent 8c2352b985b1bf69efb965ba15f5e300eb7e8e12
Author: jvoisin <julien.voisin@dustri.org>
Date:   Fri, 22 Sep 2023 18:27:54 +0200

Add more __diagnose_as_builtin annotations

Diffstat:
Minclude/poll.h | 6++++++
Minclude/stdio.h | 26++++++++++++++++++++++++++
Minclude/stdlib.h | 15+++++++++++++++
Minclude/string.h | 6++++++
Minclude/strings.h | 4++++
Minclude/unistd.h | 39+++++++++++++++++++++++++++++++++++++++
Minclude/wchar.h | 48++++++++++++++++++++++++++++++++++++++++++++++++
7 files changed, 144 insertions(+), 0 deletions(-)

diff --git a/include/poll.h b/include/poll.h @@ -31,6 +31,9 @@ extern "C" { #undef poll +#if __has_builtin(__builtin_poll) +__diagnose_as_builtin(__builtin_poll, 1, 2, 3) +#endif _FORTIFY_FN(poll) int poll(struct pollfd * _FORTIFY_POS0 __f, nfds_t __n, int __s) { size_t __b = __bos(__f, 0); @@ -42,6 +45,9 @@ _FORTIFY_FN(poll) int poll(struct pollfd * _FORTIFY_POS0 __f, nfds_t __n, int __ #if defined(_GNU_SOURCE) && !_REDIR_TIME64 #undef ppoll +#if __has_builtin(__builtin_ppoll) +__diagnose_as_builtin(__builtin_ppoll, 1, 2, 3, 4) +#endif _FORTIFY_FN(ppoll) int ppoll(struct pollfd * _FORTIFY_POS0 __f, nfds_t __n, const struct timespec *__s, const sigset_t *__m) { diff --git a/include/stdio.h b/include/stdio.h @@ -40,12 +40,18 @@ extern "C" { #undef vsprintf __access(read_only, 2) +#if __has_builtin(__builtin_fdopen) +__diagnose_as_builtin(__builtin_fdopen, 1, 2) +#endif _FORTIFY_FN(fdopen) FILE *fdopen(int __f, const char* _FORTIFY_POS0 __m) { return __orig_fdopen(__f, __m); } __access(write_only, 1, 2) +#if __has_builtin(__builtin_fgets) +__diagnose_as_builtin(__builtin_fgets, 1, 2, 3) +#endif _FORTIFY_FN(fgets) char *fgets(char * _FORTIFY_POS0 __s, int __n, FILE *__f) { size_t __b = __bos(__s, 0); @@ -56,6 +62,9 @@ _FORTIFY_FN(fgets) char *fgets(char * _FORTIFY_POS0 __s, int __n, FILE *__f) } __malloc(malloc (fclose, 1)) +#if __has_builtin(__builtin_fmemopen) +__diagnose_as_builtin(__builtin_fmemopen, 1, 2, 3) +#endif _FORTIFY_FN(fmemopen) FILE *fmemopen(void* _FORTIFY_POS0 __b, size_t __s, const char* _FORTIFY_POS0 __m) { return __orig_fmemopen(__b, __s, __m); @@ -64,12 +73,18 @@ _FORTIFY_FN(fmemopen) FILE *fmemopen(void* _FORTIFY_POS0 __b, size_t __s, const __access(read_only, 1) __access(read_only, 2) __malloc(malloc (fclose, 1)) +#if __has_builtin(__builtin_fopen) +__diagnose_as_builtin(__builtin_fopen, 1, 2) +#endif _FORTIFY_FN(fopen) FILE *fopen(const char* _FORTIFY_POS0 __p, const char* _FORTIFY_POS0 __m) { return __orig_fopen(__p, __m); } __access(write_only, 1) +#if __has_builtin(__builtin_fread) +__diagnose_as_builtin(__builtin_fread, 1, 2, 3, 4) +#endif _FORTIFY_FN(fread) size_t fread(void * _FORTIFY_POS0 __d, size_t __n, size_t __m, FILE *__f) { @@ -83,6 +98,9 @@ _FORTIFY_FN(fread) size_t fread(void * _FORTIFY_POS0 __d, size_t __n, } __access(read_only, 1) +#if __has_builtin(__builtin_fwrite) +__diagnose_as_builtin(__builtin_fwrite, 1, 2, 3, 4) +#endif _FORTIFY_FN(fwrite) size_t fwrite(const void * _FORTIFY_POS0 __d, size_t __n, size_t __m, FILE *__f) { @@ -98,6 +116,9 @@ _FORTIFY_FN(fwrite) size_t fwrite(const void * _FORTIFY_POS0 __d, size_t __n, __access(read_only, 1) __access(read_only, 2) __malloc(malloc (pclose, 1)) +#if __has_builtin(__builtin_popen) +__diagnose_as_builtin(__builtin_popen, 1, 2) +#endif _FORTIFY_FN(popen) FILE *popen(const char* _FORTIFY_POS0 __c, const char* _FORTIFY_POS0 __t) { return __orig_popen(__c, __t); @@ -106,6 +127,9 @@ _FORTIFY_FN(popen) FILE *popen(const char* _FORTIFY_POS0 __c, const char* _FORTI #ifndef __clang__ /* FIXME */ #undef tmpfile __malloc(malloc (fclose, 1)) +#if __has_builtin(__builtin_tmpfile) +__diagnose_as_builtin(__builtin_tmpfile) +#endif _FORTIFY_FN(tmpfile) FILE *tmpfile(void) { return __orig_tmpfile(); @@ -162,6 +186,8 @@ _FORTIFY_FN(vsprintf) int vsprintf(char * _FORTIFY_POS0 __s, const char *__f, * have the __chk variants, we'd need to implement a body with intermediate * variables within the macro, which means more non-portable mess * 3) not implementing these under clang, which is what we do for now + * + * TODO: add __diagnose_as_builtin */ #undef snprintf diff --git a/include/stdlib.h b/include/stdlib.h @@ -43,6 +43,9 @@ extern "C" { __malloc(malloc (free, 1)) __alloc_size(1) __warn_unused_result +#if __has_builtin(__builtin_malloc) +__diagnose_as_builtin(__builtin_malloc, 1) +#endif _FORTIFY_FN(malloc) void *malloc(size_t __s) { return __orig_malloc(__s); @@ -50,6 +53,9 @@ _FORTIFY_FN(malloc) void *malloc(size_t __s) __alloc_size(2) __warn_unused_result +#if __has_builtin(__builtin_realloc) +__diagnose_as_builtin(__builtin_realloc, 1, 2) +#endif _FORTIFY_FN(realloc) void *realloc(void *__p, size_t __s) { return __orig_realloc(__p, __s); @@ -57,6 +63,9 @@ _FORTIFY_FN(realloc) void *realloc(void *__p, size_t __s) __alloc_size(1, 2) __warn_unused_result +#if __has_builtin(__builtin_calloc) +__diagnose_as_builtin(__builtin_calloc, 1, 2) +#endif _FORTIFY_FN(calloc) void *calloc(size_t __n, size_t __s) { return __orig_calloc(__n, __s); @@ -66,6 +75,9 @@ _FORTIFY_FN(calloc) void *calloc(size_t __n, size_t __s) #undef reallocarray __alloc_size (2, 3) __warn_unused_result +#if __has_builtin(__builtin_reallocarray) +__diagnose_as_builtin(__builtin_reallocarray, 1, 2, 3) +#endif _FORTIFY_FN(reallocarray) void* reallocarray(void* __p, size_t __n, size_t __s) { return __orig_reallocarray(__p, __n, __s); @@ -76,6 +88,9 @@ _FORTIFY_FN(reallocarray) void* reallocarray(void* __p, size_t __n, size_t __s) #if (defined(_XOPEN_SOURCE) || defined(_GNU_SOURCE) || defined(_BSD_SOURCE)) && !defined(__clang__) #undef realpath __warning_if(__p == NULL, "'realpath' called with path set to `NULL`; did you invert the arguments?") +#if __has_builtin(__builtin_realpath) +__diagnose_as_builtin(__builtin_realpath, 1, 2) +#endif _FORTIFY_FN(realpath) char *realpath(const char *__p, char *__r) { #ifndef PATH_MAX diff --git a/include/string.h b/include/string.h @@ -257,6 +257,9 @@ _FORTIFY_FN(mempcpy) void *mempcpy(void * _FORTIFY_POS0 __d, #undef strlcpy __access (read_write, 1) __access (read_only, 2, 3) +#if __has_builtin(__builtin_strlcat) +__diagnose_as_builtin(__builtin_strlcat, 1, 2, 3) +#endif _FORTIFY_FN(strlcat) size_t strlcat(char * _FORTIFY_POS0 __d, const char *__s, size_t __n) { @@ -269,6 +272,9 @@ _FORTIFY_FN(strlcat) size_t strlcat(char * _FORTIFY_POS0 __d, __access (write_only, 1) __access (read_only, 2, 3) +#if __has_builtin(__builtin_strlcpy) +__diagnose_as_builtin(__builtin_strlcpy, 1, 2, 3) +#endif _FORTIFY_FN(strlcpy) size_t strlcpy(char * _FORTIFY_POS0 __d, const char *__s, size_t __n) { diff --git a/include/strings.h b/include/strings.h @@ -33,6 +33,10 @@ extern "C" { #undef bzero __access(write_only, 2, 3) __access(read_only, 1, 3) +#if __has_builtin(__builtin_bcopy) && !defined(__clang__) +// parameter 1 of function '__orig_bcopy' has type 'const void *', but parameter 1 of function '__builtin_bcopy' has type 'void *' +__diagnose_as_builtin(__builtin_bcopy, 1, 2, 3) +#endif _FORTIFY_FN(bcopy) void bcopy(const void * _FORTIFY_POS0 __s, void * _FORTIFY_POS0 __d, size_t __n) { diff --git a/include/unistd.h b/include/unistd.h @@ -42,6 +42,9 @@ extern "C" { #undef write __access(write_only, 2, 3) +#if __has_builtin(__builtin_confstr) +__diagnose_as_builtin(__builtin_confstr, 1, 2, 3) +#endif _FORTIFY_FN(confstr) size_t confstr(int __n, char * _FORTIFY_POS0 __s, size_t __l) { size_t __b = __bos(__s, 0); @@ -53,6 +56,9 @@ _FORTIFY_FN(confstr) size_t confstr(int __n, char * _FORTIFY_POS0 __s, size_t __ } __access(write_only, 1, 2) +#if __has_builtin(__builtin_getcwd) +__diagnose_as_builtin(__builtin_getcwd, 1, 2) +#endif _FORTIFY_FN(getcwd) char *getcwd(char * _FORTIFY_POS0 __s, size_t __l) { size_t __b = __bos(__s, 0); @@ -65,6 +71,9 @@ _FORTIFY_FN(getcwd) char *getcwd(char * _FORTIFY_POS0 __s, size_t __l) #if defined(_GNU_SOURCE) || defined(_BSD_SOURCE) #undef getdomainname __access(write_only, 1, 2) +#if __has_builtin(__builtin_getdomainname) +__diagnose_as_builtin(__builtin_getdomainname, 1, 2) +#endif _FORTIFY_FN(getdomainname) int getdomainname(char * _FORTIFY_POS0 __s, size_t __l) { size_t __b = __bos(__s, 0); @@ -75,6 +84,9 @@ _FORTIFY_FN(getdomainname) int getdomainname(char * _FORTIFY_POS0 __s, size_t __ } #endif +#if __has_builtin(__builtin_getgroups) +__diagnose_as_builtin(__builtin_getgroups, 1, 2) +#endif _FORTIFY_FN(getgroups) int getgroups(int __l, gid_t * _FORTIFY_POS0 __s) { size_t __b = __bos(__s, 0); @@ -85,6 +97,9 @@ _FORTIFY_FN(getgroups) int getgroups(int __l, gid_t * _FORTIFY_POS0 __s) } __access(write_only, 1, 2) +#if __has_builtin(__builtin_gethostname) +__diagnose_as_builtin(__builtin_gethostname, 1, 2) +#endif _FORTIFY_FN(gethostname) int gethostname(char * _FORTIFY_POS0 __s, size_t __l) { size_t __b = __bos(__s, 0); @@ -95,6 +110,9 @@ _FORTIFY_FN(gethostname) int gethostname(char * _FORTIFY_POS0 __s, size_t __l) } __access(write_only, 1, 2) +#if __has_builtin(__builtin_getlogin_r) +__diagnose_as_builtin(__builtin_getlogin_r, 1, 2) +#endif _FORTIFY_FN(getlogin_r) int getlogin_r(char * _FORTIFY_POS0 __s, size_t __l) { size_t __b = __bos(__s, 0); @@ -104,6 +122,9 @@ _FORTIFY_FN(getlogin_r) int getlogin_r(char * _FORTIFY_POS0 __s, size_t __l) return __orig_getlogin_r(__s, __l); } +#if __has_builtin(__builtin_pread) +__diagnose_as_builtin(__builtin_pread, 1, 2, 3, 4) +#endif _FORTIFY_FN(pread) ssize_t pread(int __f, void * _FORTIFY_POS0 __s, size_t __n, off_t __o) { @@ -115,6 +136,9 @@ _FORTIFY_FN(pread) ssize_t pread(int __f, void * _FORTIFY_POS0 __s, } __access(write_only, 2, 3) +#if __has_builtin(__builtin_read) +__diagnose_as_builtin(__builtin_read, 1, 2, 3) +#endif _FORTIFY_FN(read) ssize_t read(int __f, void * _FORTIFY_POS0 __s, size_t __n) { size_t __b = __bos(__s, 0); @@ -125,6 +149,9 @@ _FORTIFY_FN(read) ssize_t read(int __f, void * _FORTIFY_POS0 __s, size_t __n) } __access(write_only, 2, 3) +#if __has_builtin(__builtin_readlink) +__diagnose_as_builtin(__builtin_readlink, 1, 2, 3) +#endif _FORTIFY_FN(readlink) ssize_t readlink(const char *__p, char * _FORTIFY_POS0 __s, size_t __n) { @@ -136,6 +163,9 @@ _FORTIFY_FN(readlink) ssize_t readlink(const char *__p, } __access(write_only, 3, 4) +#if __has_builtin(__builtin_readlinkat) +__diagnose_as_builtin(__builtin_readlinkat, 1, 2, 3, 4) +#endif _FORTIFY_FN(readlinkat) ssize_t readlinkat(int __f, const char *__p, char * _FORTIFY_POS0 __s, size_t __n) { @@ -147,6 +177,9 @@ _FORTIFY_FN(readlinkat) ssize_t readlinkat(int __f, const char *__p, } __access(write_only, 2, 3) +#if __has_builtin(__builtin_ttyname_r) +__diagnose_as_builtin(__builtin_ttyname_r, 1, 2, 3) +#endif _FORTIFY_FN(ttyname_r) int ttyname_r(int __f, char * _FORTIFY_POS0 __s, size_t __n) { @@ -158,6 +191,9 @@ _FORTIFY_FN(ttyname_r) int ttyname_r(int __f, char * _FORTIFY_POS0 __s, } __access(read_only, 2, 3) +#if __has_builtin(__builtin_write) +__diagnose_as_builtin(__builtin_write, 1, 2, 3) +#endif _FORTIFY_FN(write) ssize_t write(int __f, const void * _FORTIFY_POS0 __s, size_t __n) { @@ -168,6 +204,9 @@ _FORTIFY_FN(write) ssize_t write(int __f, const void * _FORTIFY_POS0 __s, return __orig_write(__f, __s, __n); } +#if __has_builtin(__builtin_pwrite) +__diagnose_as_builtin(__builtin_pwrite, 1, 2, 3, 4) +#endif _FORTIFY_FN(pwrite) ssize_t pwrite(int __f, const void * _FORTIFY_POS0 __s, size_t __n, off_t __o) { diff --git a/include/wchar.h b/include/wchar.h @@ -56,6 +56,9 @@ extern "C" { #undef wmemmove #undef wmemset +#if __has_builtin(__builtin_fgetws) +__diagnose_as_builtin(__builtin_fgetws, 1, 2, 3) +#endif _FORTIFY_FN(fgetws) wchar_t *fgetws(wchar_t * _FORTIFY_POS0 __s, int __n, FILE *__f) { @@ -69,6 +72,9 @@ _FORTIFY_FN(fgetws) wchar_t *fgetws(wchar_t * _FORTIFY_POS0 __s, #if defined(_POSIX_SOURCE) || defined(_POSIX_C_SOURCE) \ || defined(_XOPEN_SOURCE) || defined(_GNU_SOURCE) || defined(_BSD_SOURCE) #undef mbsnrtowcs +#if __has_builtin(__builtin_mbsnrtowcs) +__diagnose_as_builtin(__builtin_mbsnrtowcs, 1, 2, 3, 4, 5) +#endif _FORTIFY_FN(mbsnrtowcs) size_t mbsnrtowcs(wchar_t * _FORTIFY_POS0 __d, const char **__s, size_t __n, size_t __wn, mbstate_t *__st) @@ -90,6 +96,9 @@ _FORTIFY_FN(mbsnrtowcs) size_t mbsnrtowcs(wchar_t * _FORTIFY_POS0 __d, } #endif +#if __has_builtin(__builtin_mbsrtowcs) +__diagnose_as_builtin(__builtin_mbsrtowcs, 1, 2, 3, 4) +#endif _FORTIFY_FN(mbsrtowcs) size_t mbsrtowcs(wchar_t * _FORTIFY_POS0 __d, const char **__s, size_t __wn, mbstate_t *__st) @@ -104,6 +113,9 @@ _FORTIFY_FN(mbsrtowcs) size_t mbsrtowcs(wchar_t * _FORTIFY_POS0 __d, return __r; } +#if __has_builtin(__builtin_mbstowcs) +__diagnose_as_builtin(__builtin_mbstowcs, 1, 2, 3) +#endif _FORTIFY_FN(mbstowcs) size_t mbstowcs(wchar_t * _FORTIFY_POS0 __ws, const char *__s, size_t __wn) { @@ -116,6 +128,9 @@ _FORTIFY_FN(mbstowcs) size_t mbstowcs(wchar_t * _FORTIFY_POS0 __ws, /* FIXME clang */ #ifndef __clang__ +#if __has_builtin(__builtin_wcrtomb) +__diagnose_as_builtin(__builtin_wcrtomb, 1, 2, 3) +#endif _FORTIFY_FN(wcrtomb) size_t wcrtomb(char * __s, wchar_t __w, mbstate_t *__st) { if (__s && MB_LEN_MAX > __bos(__s, 2)) { @@ -134,6 +149,9 @@ _FORTIFY_FN(wcrtomb) size_t wcrtomb(char * __s, wchar_t __w, mbstate_t *__st) } #endif +#if __has_builtin(__builtin_wcscat) +__diagnose_as_builtin(__builtin_wcscat, 1, 2) +#endif _FORTIFY_FN(wcscat) wchar_t *wcscat(wchar_t * _FORTIFY_POS0 __d, const wchar_t *__s) { @@ -144,6 +162,9 @@ _FORTIFY_FN(wcscat) wchar_t *wcscat(wchar_t * _FORTIFY_POS0 __d, return __orig_wcscat(__d, __s); } +#if __has_builtin(__builtin_wcscpy) +__diagnose_as_builtin(__builtin_wcscpy, 1, 2) +#endif _FORTIFY_FN(wcscpy) wchar_t *wcscpy(wchar_t * _FORTIFY_POS0 __d, const wchar_t *__s) { @@ -154,6 +175,9 @@ _FORTIFY_FN(wcscpy) wchar_t *wcscpy(wchar_t * _FORTIFY_POS0 __d, return __orig_wcscpy(__d, __s); } +#if __has_builtin(__builtin_wcsncat) +__diagnose_as_builtin(__builtin_wcsncat, 1, 2, 3) +#endif _FORTIFY_FN(wcsncat) wchar_t *wcsncat(wchar_t * _FORTIFY_POS0 __d, const wchar_t *__s, size_t __n) { @@ -171,6 +195,9 @@ _FORTIFY_FN(wcsncat) wchar_t *wcsncat(wchar_t * _FORTIFY_POS0 __d, return __orig_wcsncat(__d, __s, __n); } +#if __has_builtin(__builtin_wcsncpy) +__diagnose_as_builtin(__builtin_wcsncpy, 1, 2, 3) +#endif _FORTIFY_FN(wcsncpy) wchar_t *wcsncpy(wchar_t * _FORTIFY_POS0 __d, const wchar_t *__s, size_t __n) { @@ -184,6 +211,9 @@ _FORTIFY_FN(wcsncpy) wchar_t *wcsncpy(wchar_t * _FORTIFY_POS0 __d, #if defined(_POSIX_SOURCE) || defined(_POSIX_C_SOURCE) \ || defined(_XOPEN_SOURCE) || defined(_GNU_SOURCE) || defined(_BSD_SOURCE) #undef wcsnrtombs +#if __has_builtin(__builtin_wcsnrtombs) +__diagnose_as_builtin(__builtin_wcsnrtombs, 1, 2, 3, 4, 5) +#endif _FORTIFY_FN(wcsnrtombs) size_t wcsnrtombs(char * _FORTIFY_POS0 __d, const wchar_t **__s, size_t __wn, size_t __n, mbstate_t *__st) @@ -205,6 +235,9 @@ _FORTIFY_FN(wcsnrtombs) size_t wcsnrtombs(char * _FORTIFY_POS0 __d, } #endif +#if __has_builtin(__builtin_wcsrtombs) +__diagnose_as_builtin(__builtin_wcsrtombs, 1, 2, 3, 4) +#endif _FORTIFY_FN(wcsrtombs) size_t wcsrtombs(char * _FORTIFY_POS0 __d, const wchar_t **__s, size_t __n, mbstate_t *__st) @@ -219,6 +252,9 @@ _FORTIFY_FN(wcsrtombs) size_t wcsrtombs(char * _FORTIFY_POS0 __d, } __access(write_only, 1, 3) +#if __has_builtin(__builtin_wcstombs) +__diagnose_as_builtin(__builtin_wcstombs, 1, 2, 3) +#endif _FORTIFY_FN(wcstombs) size_t wcstombs(char * _FORTIFY_POS0 __s, const wchar_t *__ws, size_t __n) { @@ -229,6 +265,9 @@ _FORTIFY_FN(wcstombs) size_t wcstombs(char * _FORTIFY_POS0 __s, return __orig_wcstombs(__s, __ws, __n); } +#if __has_builtin(__builtin_wctomb) +__diagnose_as_builtin(__builtin_wctomb, 1, 2) +#endif _FORTIFY_FN(wctomb) int wctomb(char * _FORTIFY_POS0 __s, wchar_t __w) { size_t __b = __bos(__s, 0); @@ -238,6 +277,9 @@ _FORTIFY_FN(wctomb) int wctomb(char * _FORTIFY_POS0 __s, wchar_t __w) return __orig_wctomb(__s, __w); } +#if __has_builtin(__builtin_wmemcpy) +__diagnose_as_builtin(__builtin_wmemcpy, 1, 2, 3) +#endif _FORTIFY_FN(wmemcpy) wchar_t *wmemcpy(wchar_t * _FORTIFY_POS0 __d, const wchar_t *__s, size_t __n) { @@ -248,6 +290,9 @@ _FORTIFY_FN(wmemcpy) wchar_t *wmemcpy(wchar_t * _FORTIFY_POS0 __d, return __orig_wmemcpy(__d, __s, __n); } +#if __has_builtin(__builtin_wmemmove) +__diagnose_as_builtin(__builtin_wmemmove, 1, 2, 3) +#endif _FORTIFY_FN(wmemmove) wchar_t *wmemmove(wchar_t * _FORTIFY_POS0 __d, const wchar_t *__s, size_t __n) { @@ -258,6 +303,9 @@ _FORTIFY_FN(wmemmove) wchar_t *wmemmove(wchar_t * _FORTIFY_POS0 __d, return __orig_wmemmove(__d, __s, __n); } +#if __has_builtin(__builtin_wmemset) +__diagnose_as_builtin(__builtin_wmemset, 1, 2, 3) +#endif _FORTIFY_FN(wmemset) wchar_t *wmemset(wchar_t * _FORTIFY_POS0 __s, wchar_t __c, size_t __n) {