fortify-headers

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

commit 00b376bb073b39f202db6f8080010a2812dde08b
parent aa19847d12f12587937a23e08114e413e6d29ee1
Author: sin <sin@2f30.org>
Date:   Thu,  5 Feb 2015 14:07:14 +0000

Add getcwd() checks

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

diff --git a/include/unistd.h b/include/unistd.h @@ -23,6 +23,21 @@ __fortify_confstr(int name, char *buf, size_t len) 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(__pread_error, "pread: buffer overflow detected"); static inline __attribute__ ((always_inline)) ssize_t @@ -55,6 +70,8 @@ __fortify_read(int fd, void *buf, size_t n) #undef confstr #define confstr(name, buf, len) __fortify_confstr(name, buf, len) +#undef getcwd +#define getcwd(buf, len) __fortify_getcwd(buf, len) #undef pread #define pread(fd, buf, n, offset) __fortify_pread(fd, buf, n, offset) #undef read