fortify-headers

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

commit 60dcebb6b812097eb6ca141086c2d8c1875d3347
parent edb2ded3af887cd0a206c0f00e20118d58a7775c
Author: sin <sin@2f30.org>
Date:   Thu, 16 Jul 2015 11:45:19 +0100

Only crash on overflow for realpath()

Diffstat:
Minclude/stdlib.h | 14+++++++++++---
1 file changed, 11 insertions(+), 3 deletions(-)

diff --git a/include/stdlib.h b/include/stdlib.h @@ -35,15 +35,23 @@ extern "C" { #undef realpath _FORTIFY_FN(realpath) char *realpath(const char *__p, char *__r) { - size_t __b; + size_t __b = __builtin_object_size(__r, 0); if (__r) { #ifndef PATH_MAX #error PATH_MAX unset. A fortified realpath will not work. #else - __b = __builtin_object_size(__r, 0); - if (PATH_MAX > __b) + char __buf[PATH_MAX], *__ret; + size_t __l; + + __ret = __orig_realpath(__p, __buf); + if (!__ret) + return NULL; + __l = __builtin_strlen(__ret) + 1; + if (__l > __b) __builtin_trap(); + __builtin_memcpy(__r, __ret, __l); + return __r; #endif } return __orig_realpath(__p, __r);