fortify-headers

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

commit 9a77136c5914f6be50df195dac0f99424252a297
parent c1734bf20671b1c45d6964e34b85d954e2620b88
Author: sin <sin@2f30.org>
Date:   Mon, 23 Feb 2015 10:45:20 +0000

Add gethostname() checks

Diffstat:
Minclude/unistd.h | 17+++++++++++++++++
1 file changed, 17 insertions(+), 0 deletions(-)

diff --git a/include/unistd.h b/include/unistd.h @@ -37,6 +37,21 @@ __fortify_getcwd(char *buf, size_t len) 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 @@ -71,6 +86,8 @@ __fortify_read(int fd, void *buf, size_t n) #define confstr(name, buf, len) __fortify_confstr(name, buf, len) #undef getcwd #define getcwd(buf, len) __fortify_getcwd(buf, len) +#undef gethostname +#define gethostname(name, len) __fortify_gethostname(name, len) #undef pread #define pread(fd, buf, n, offset) __fortify_pread(fd, buf, n, offset) #undef read