From a643f60c53876be0d57b4b7373770e6cb356fd13 Mon Sep 17 00:00:00 2001 From: Siddhesh Poyarekar Date: Wed, 20 Oct 2021 18:12:41 +0530 Subject: Make sure that the fortified function conditionals are constant In _FORTIFY_SOURCE=3, the size expression may be non-constant, resulting in branches in the inline functions remaining intact and causing a tiny overhead. Clang (and in future, gcc) make sure that the -1 case is always safe, i.e. any comparison of the generated expression with (size_t)-1 is always false so that bit is taken care of. The rest is avoidable since we want the _chk variant whenever we have a size expression and it's not -1. Rework the conditionals in a uniform way to clearly indicate two conditions at compile time: - Either the size is unknown (-1) or we know at compile time that the operation length is less than the object size. We can call the original function in this case. It could be that either the length, object size or both are non-constant, but the compiler, through range analysis, is able to fold the *comparison* to a constant. - The size and length are known and the compiler can see at compile time that operation length > object size. This is valid grounds for a warning at compile time, followed by emitting the _chk variant. For everything else, emit the _chk variant. This simplifies most of the fortified function implementations and at the same time, ensures that only one call from _chk or the regular function is emitted. Signed-off-by: Siddhesh Poyarekar Reviewed-by: Adhemerval Zanella --- posix/bits/unistd.h | 174 ++++++++++++---------------------------------------- 1 file changed, 39 insertions(+), 135 deletions(-) (limited to 'posix') diff --git a/posix/bits/unistd.h b/posix/bits/unistd.h index 622adeb2b2..697dcbbf7b 100644 --- a/posix/bits/unistd.h +++ b/posix/bits/unistd.h @@ -35,16 +35,9 @@ extern ssize_t __REDIRECT (__read_chk_warn, __fortify_function __wur ssize_t read (int __fd, void *__buf, size_t __nbytes) { - if (__glibc_objsize0 (__buf) != (size_t) -1) - { - if (!__builtin_constant_p (__nbytes)) - return __read_chk (__fd, __buf, __nbytes, __glibc_objsize0 (__buf)); - - if (__nbytes > __glibc_objsize0 (__buf)) - return __read_chk_warn (__fd, __buf, __nbytes, - __glibc_objsize0 (__buf)); - } - return __read_alias (__fd, __buf, __nbytes); + return __glibc_fortify (read, __nbytes, sizeof (char), + __glibc_objsize0 (__buf), + __fd, __buf, __nbytes); } #ifdef __USE_UNIX98 @@ -78,34 +71,17 @@ extern ssize_t __REDIRECT (__pread64_chk_warn, __fortify_function __wur ssize_t pread (int __fd, void *__buf, size_t __nbytes, __off_t __offset) { - if (__glibc_objsize0 (__buf) != (size_t) -1) - { - if (!__builtin_constant_p (__nbytes)) - return __pread_chk (__fd, __buf, __nbytes, __offset, - __glibc_objsize0 (__buf)); - - if ( __nbytes > __glibc_objsize0 (__buf)) - return __pread_chk_warn (__fd, __buf, __nbytes, __offset, - __glibc_objsize0 (__buf)); - } - return __pread_alias (__fd, __buf, __nbytes, __offset); + return __glibc_fortify (pread, __nbytes, sizeof (char), + __glibc_objsize0 (__buf), + __fd, __buf, __nbytes, __offset); } # else __fortify_function __wur ssize_t pread (int __fd, void *__buf, size_t __nbytes, __off64_t __offset) { - if (__glibc_objsize0 (__buf) != (size_t) -1) - { - if (!__builtin_constant_p (__nbytes)) - return __pread64_chk (__fd, __buf, __nbytes, __offset, - __glibc_objsize0 (__buf)); - - if ( __nbytes > __glibc_objsize0 (__buf)) - return __pread64_chk_warn (__fd, __buf, __nbytes, __offset, - __glibc_objsize0 (__buf)); - } - - return __pread64_alias (__fd, __buf, __nbytes, __offset); + return __glibc_fortify (pread64, __nbytes, sizeof (char), + __glibc_objsize0 (__buf), + __fd, __buf, __nbytes, __offset); } # endif @@ -113,18 +89,9 @@ pread (int __fd, void *__buf, size_t __nbytes, __off64_t __offset) __fortify_function __wur ssize_t pread64 (int __fd, void *__buf, size_t __nbytes, __off64_t __offset) { - if (__glibc_objsize0 (__buf) != (size_t) -1) - { - if (!__builtin_constant_p (__nbytes)) - return __pread64_chk (__fd, __buf, __nbytes, __offset, - __glibc_objsize0 (__buf)); - - if ( __nbytes > __glibc_objsize0 (__buf)) - return __pread64_chk_warn (__fd, __buf, __nbytes, __offset, - __glibc_objsize0 (__buf)); - } - - return __pread64_alias (__fd, __buf, __nbytes, __offset); + return __glibc_fortify (pread64, __nbytes, sizeof (char), + __glibc_objsize0 (__buf), + __fd, __buf, __nbytes, __offset); } # endif #endif @@ -149,16 +116,9 @@ __fortify_function __nonnull ((1, 2)) __wur ssize_t __NTH (readlink (const char *__restrict __path, char *__restrict __buf, size_t __len)) { - if (__glibc_objsize (__buf) != (size_t) -1) - { - if (!__builtin_constant_p (__len)) - return __readlink_chk (__path, __buf, __len, __glibc_objsize (__buf)); - - if ( __len > __glibc_objsize (__buf)) - return __readlink_chk_warn (__path, __buf, __len, - __glibc_objsize (__buf)); - } - return __readlink_alias (__path, __buf, __len); + return __glibc_fortify (readlink, __len, sizeof (char), + __glibc_objsize (__buf), + __path, __buf, __len); } #endif @@ -184,17 +144,9 @@ __fortify_function __nonnull ((2, 3)) __wur ssize_t __NTH (readlinkat (int __fd, const char *__restrict __path, char *__restrict __buf, size_t __len)) { - if (__glibc_objsize (__buf) != (size_t) -1) - { - if (!__builtin_constant_p (__len)) - return __readlinkat_chk (__fd, __path, __buf, __len, - __glibc_objsize (__buf)); - - if (__len > __glibc_objsize (__buf)) - return __readlinkat_chk_warn (__fd, __path, __buf, __len, - __glibc_objsize (__buf)); - } - return __readlinkat_alias (__fd, __path, __buf, __len); + return __glibc_fortify (readlinkat, __len, sizeof (char), + __glibc_objsize (__buf), + __fd, __path, __buf, __len); } #endif @@ -211,15 +163,9 @@ extern char *__REDIRECT_NTH (__getcwd_chk_warn, __fortify_function __wur char * __NTH (getcwd (char *__buf, size_t __size)) { - if (__glibc_objsize (__buf) != (size_t) -1) - { - if (!__builtin_constant_p (__size)) - return __getcwd_chk (__buf, __size, __glibc_objsize (__buf)); - - if (__size > __glibc_objsize (__buf)) - return __getcwd_chk_warn (__buf, __size, __glibc_objsize (__buf)); - } - return __getcwd_alias (__buf, __size); + return __glibc_fortify (getcwd, __size, sizeof (char), + __glibc_objsize (__buf), + __buf, __size); } #if defined __USE_MISC || defined __USE_XOPEN_EXTENDED @@ -253,16 +199,9 @@ extern size_t __REDIRECT_NTH (__confstr_chk_warn, __fortify_function size_t __NTH (confstr (int __name, char *__buf, size_t __len)) { - if (__glibc_objsize (__buf) != (size_t) -1) - { - if (!__builtin_constant_p (__len)) - return __confstr_chk (__name, __buf, __len, __glibc_objsize (__buf)); - - if (__glibc_objsize (__buf) < __len) - return __confstr_chk_warn (__name, __buf, __len, - __glibc_objsize (__buf)); - } - return __confstr_alias (__name, __buf, __len); + return __glibc_fortify (confstr, __len, sizeof (char), + __glibc_objsize (__buf), + __name, __buf, __len); } @@ -279,15 +218,9 @@ extern int __REDIRECT_NTH (__getgroups_chk_warn, __fortify_function int __NTH (getgroups (int __size, __gid_t __list[])) { - if (__glibc_objsize (__list) != (size_t) -1) - { - if (!__builtin_constant_p (__size) || __size < 0) - return __getgroups_chk (__size, __list, __glibc_objsize (__list)); - - if (__size * sizeof (__gid_t) > __glibc_objsize (__list)) - return __getgroups_chk_warn (__size, __list, __glibc_objsize (__list)); - } - return __getgroups_alias (__size, __list); + return __glibc_fortify (getgroups, __size, sizeof (__gid_t), + __glibc_objsize (__list), + __size, __list); } @@ -306,17 +239,9 @@ extern int __REDIRECT_NTH (__ttyname_r_chk_warn, __fortify_function int __NTH (ttyname_r (int __fd, char *__buf, size_t __buflen)) { - if (__glibc_objsize (__buf) != (size_t) -1) - { - if (!__builtin_constant_p (__buflen)) - return __ttyname_r_chk (__fd, __buf, __buflen, - __glibc_objsize (__buf)); - - if (__buflen > __glibc_objsize (__buf)) - return __ttyname_r_chk_warn (__fd, __buf, __buflen, - __glibc_objsize (__buf)); - } - return __ttyname_r_alias (__fd, __buf, __buflen); + return __glibc_fortify (ttyname_r, __buflen, sizeof (char), + __glibc_objsize (__buf), + __fd, __buf, __buflen); } @@ -334,16 +259,9 @@ extern int __REDIRECT (__getlogin_r_chk_warn, __fortify_function int getlogin_r (char *__buf, size_t __buflen) { - if (__glibc_objsize (__buf) != (size_t) -1) - { - if (!__builtin_constant_p (__buflen)) - return __getlogin_r_chk (__buf, __buflen, __glibc_objsize (__buf)); - - if (__buflen > __glibc_objsize (__buf)) - return __getlogin_r_chk_warn (__buf, __buflen, - __glibc_objsize (__buf)); - } - return __getlogin_r_alias (__buf, __buflen); + return __glibc_fortify (getlogin_r, __buflen, sizeof (char), + __glibc_objsize (__buf), + __buf, __buflen); } #endif @@ -363,16 +281,9 @@ extern int __REDIRECT_NTH (__gethostname_chk_warn, __fortify_function int __NTH (gethostname (char *__buf, size_t __buflen)) { - if (__glibc_objsize (__buf) != (size_t) -1) - { - if (!__builtin_constant_p (__buflen)) - return __gethostname_chk (__buf, __buflen, __glibc_objsize (__buf)); - - if (__buflen > __glibc_objsize (__buf)) - return __gethostname_chk_warn (__buf, __buflen, - __glibc_objsize (__buf)); - } - return __gethostname_alias (__buf, __buflen); + return __glibc_fortify (gethostname, __buflen, sizeof (char), + __glibc_objsize (__buf), + __buf, __buflen); } #endif @@ -394,15 +305,8 @@ extern int __REDIRECT_NTH (__getdomainname_chk_warn, __fortify_function int __NTH (getdomainname (char *__buf, size_t __buflen)) { - if (__glibc_objsize (__buf) != (size_t) -1) - { - if (!__builtin_constant_p (__buflen)) - return __getdomainname_chk (__buf, __buflen, __glibc_objsize (__buf)); - - if (__buflen > __glibc_objsize (__buf)) - return __getdomainname_chk_warn (__buf, __buflen, - __glibc_objsize (__buf)); - } - return __getdomainname_alias (__buf, __buflen); + return __glibc_fortify (getdomainname, __buflen, sizeof (char), + __glibc_objsize (__buf), + __buf, __buflen); } #endif -- cgit 1.4.1