Skip to content

Commit 67ebc3a

Browse files
committed
fortify: Make sure strlen() may still be used as a constant expression
In preparation for enabling Clang FORTIFY_SOURCE support, redefine strlen() as a macro that tests for being a constant expression so that strlen() can still be used in static initializers, which is lost when adding __pass_object_size and __overloadable. An example of this usage can be seen here: https://lore.kernel.org/all/[email protected]/ Notably, this constant expression feature of strlen() is not available for architectures that build with -ffreestanding. This means the kernel currently does not universally expect strlen() to be used this way, but since there _are_ some build configurations that depend on it, retain the characteristic for Clang FORTIFY_SOURCE builds too. Signed-off-by: Kees Cook <[email protected]> Reviewed-by: Nick Desaulniers <[email protected]> Link: https://lore.kernel.org/r/[email protected]
1 parent 92df138 commit 67ebc3a

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

include/linux/fortify-string.h

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
#ifndef _LINUX_FORTIFY_STRING_H_
33
#define _LINUX_FORTIFY_STRING_H_
44

5+
#include <linux/const.h>
6+
57
#define __FORTIFY_INLINE extern __always_inline __gnu_inline
68
#define __RENAME(x) __asm__(#x)
79

@@ -95,9 +97,16 @@ __FORTIFY_INLINE __kernel_size_t strnlen(const char * const p, __kernel_size_t m
9597
return ret;
9698
}
9799

98-
/* defined after fortified strnlen to reuse it. */
100+
/*
101+
* Defined after fortified strnlen to reuse it. However, it must still be
102+
* possible for strlen() to be used on compile-time strings for use in
103+
* static initializers (i.e. as a constant expression).
104+
*/
105+
#define strlen(p) \
106+
__builtin_choose_expr(__is_constexpr(__builtin_strlen(p)), \
107+
__builtin_strlen(p), __fortify_strlen(p))
99108
__FORTIFY_INLINE __diagnose_as(__builtin_strlen, 1)
100-
__kernel_size_t strlen(const char * const p)
109+
__kernel_size_t __fortify_strlen(const char * const p)
101110
{
102111
__kernel_size_t ret;
103112
size_t p_size = __builtin_object_size(p, 1);

0 commit comments

Comments
 (0)