|
|
a41fe4 |
commit c43c5796121bc5bcc0867f02e5536874aa8196c1
|
|
|
a41fe4 |
Author: Siddhesh Poyarekar <siddhesh@sourceware.org>
|
|
|
a41fe4 |
Date: Wed Dec 30 11:54:00 2020 +0530
|
|
|
a41fe4 |
|
|
|
a41fe4 |
Introduce _FORTIFY_SOURCE=3
|
|
|
a41fe4 |
|
|
|
a41fe4 |
Introduce a new _FORTIFY_SOURCE level of 3 to enable additional
|
|
|
a41fe4 |
fortifications that may have a noticeable performance impact, allowing
|
|
|
a41fe4 |
more fortification coverage at the cost of some performance.
|
|
|
a41fe4 |
|
|
|
a41fe4 |
With llvm 9.0 or later, this will replace the use of
|
|
|
a41fe4 |
__builtin_object_size with __builtin_dynamic_object_size.
|
|
|
a41fe4 |
|
|
|
a41fe4 |
__builtin_dynamic_object_size
|
|
|
a41fe4 |
-----------------------------
|
|
|
a41fe4 |
|
|
|
a41fe4 |
__builtin_dynamic_object_size is an LLVM builtin that is similar to
|
|
|
a41fe4 |
__builtin_object_size. In addition to what __builtin_object_size
|
|
|
a41fe4 |
does, i.e. replace the builtin call with a constant object size,
|
|
|
a41fe4 |
__builtin_dynamic_object_size will replace the call site with an
|
|
|
a41fe4 |
expression that evaluates to the object size, thus expanding its
|
|
|
a41fe4 |
applicability. In practice, __builtin_dynamic_object_size evaluates
|
|
|
a41fe4 |
these expressions through malloc/calloc calls that it can associate
|
|
|
a41fe4 |
with the object being evaluated.
|
|
|
a41fe4 |
|
|
|
a41fe4 |
A simple motivating example is below; -D_FORTIFY_SOURCE=2 would miss
|
|
|
a41fe4 |
this and emit memcpy, but -D_FORTIFY_SOURCE=3 with the help of
|
|
|
a41fe4 |
__builtin_dynamic_object_size is able to emit __memcpy_chk with the
|
|
|
a41fe4 |
allocation size expression passed into the function:
|
|
|
a41fe4 |
|
|
|
a41fe4 |
void *copy_obj (const void *src, size_t alloc, size_t copysize)
|
|
|
a41fe4 |
{
|
|
|
a41fe4 |
void *obj = malloc (alloc);
|
|
|
a41fe4 |
memcpy (obj, src, copysize);
|
|
|
a41fe4 |
return obj;
|
|
|
a41fe4 |
}
|
|
|
a41fe4 |
|
|
|
a41fe4 |
Limitations
|
|
|
a41fe4 |
-----------
|
|
|
a41fe4 |
|
|
|
a41fe4 |
If the object was allocated elsewhere that the compiler cannot see, or
|
|
|
a41fe4 |
if it was allocated in the function with a function that the compiler
|
|
|
a41fe4 |
does not recognize as an allocator then __builtin_dynamic_object_size
|
|
|
a41fe4 |
also returns -1.
|
|
|
a41fe4 |
|
|
|
a41fe4 |
Further, the expression used to compute object size may be non-trivial
|
|
|
a41fe4 |
and may potentially incur a noticeable performance impact. These
|
|
|
a41fe4 |
fortifications are hence enabled at a new _FORTIFY_SOURCE level to
|
|
|
a41fe4 |
allow developers to make a choice on the tradeoff according to their
|
|
|
a41fe4 |
environment.
|
|
|
a41fe4 |
|
|
|
a41fe4 |
diff --git a/include/features.h b/include/features.h
|
|
|
a41fe4 |
index ea7673ee115bcf0a..fe9fe16d034fad1b 100644
|
|
|
a41fe4 |
--- a/include/features.h
|
|
|
a41fe4 |
+++ b/include/features.h
|
|
|
a41fe4 |
@@ -381,6 +381,11 @@
|
|
|
a41fe4 |
# warning _FORTIFY_SOURCE requires compiling with optimization (-O)
|
|
|
a41fe4 |
# elif !__GNUC_PREREQ (4, 1)
|
|
|
a41fe4 |
# warning _FORTIFY_SOURCE requires GCC 4.1 or later
|
|
|
a41fe4 |
+# elif _FORTIFY_SOURCE > 2 && __glibc_clang_prereq (9, 0)
|
|
|
a41fe4 |
+# if _FORTIFY_SOURCE > 3
|
|
|
a41fe4 |
+# warning _FORTIFY_SOURCE > 3 is treated like 3 on this platform
|
|
|
a41fe4 |
+# endif
|
|
|
a41fe4 |
+# define __USE_FORTIFY_LEVEL 3
|
|
|
a41fe4 |
# elif _FORTIFY_SOURCE > 1
|
|
|
a41fe4 |
# if _FORTIFY_SOURCE > 2
|
|
|
a41fe4 |
# warning _FORTIFY_SOURCE > 2 is treated like 2 on this platform
|
|
|
a41fe4 |
diff --git a/manual/creature.texi b/manual/creature.texi
|
|
|
a41fe4 |
index 8876b2ab779c988f..64f361f27a7d6cdf 100644
|
|
|
a41fe4 |
--- a/manual/creature.texi
|
|
|
a41fe4 |
+++ b/manual/creature.texi
|
|
|
a41fe4 |
@@ -247,7 +247,8 @@ included.
|
|
|
a41fe4 |
@standards{GNU, (none)}
|
|
|
a41fe4 |
If this macro is defined to @math{1}, security hardening is added to
|
|
|
a41fe4 |
various library functions. If defined to @math{2}, even stricter
|
|
|
a41fe4 |
-checks are applied.
|
|
|
a41fe4 |
+checks are applied. If defined to @math{3}, @theglibc{} may also use
|
|
|
a41fe4 |
+checks that may have an additional performance overhead.
|
|
|
a41fe4 |
@end defvr
|
|
|
a41fe4 |
|
|
|
a41fe4 |
@defvr Macro _REENTRANT
|
|
|
a41fe4 |
diff --git a/misc/sys/cdefs.h b/misc/sys/cdefs.h
|
|
|
a41fe4 |
index 3f6fe3cc8563b493..1e39307b0ebcf38f 100644
|
|
|
a41fe4 |
--- a/misc/sys/cdefs.h
|
|
|
a41fe4 |
+++ b/misc/sys/cdefs.h
|
|
|
a41fe4 |
@@ -123,6 +123,15 @@
|
|
|
a41fe4 |
#define __bos(ptr) __builtin_object_size (ptr, __USE_FORTIFY_LEVEL > 1)
|
|
|
a41fe4 |
#define __bos0(ptr) __builtin_object_size (ptr, 0)
|
|
|
a41fe4 |
|
|
|
a41fe4 |
+/* Use __builtin_dynamic_object_size at _FORTIFY_SOURCE=3 when available. */
|
|
|
a41fe4 |
+#if __USE_FORTIFY_LEVEL == 3 && __glibc_clang_prereq (9, 0)
|
|
|
a41fe4 |
+# define __glibc_objsize0(__o) __builtin_dynamic_object_size (__o, 0)
|
|
|
a41fe4 |
+# define __glibc_objsize(__o) __builtin_dynamic_object_size (__o, 1)
|
|
|
a41fe4 |
+#else
|
|
|
a41fe4 |
+# define __glibc_objsize0(__o) __bos0 (__o)
|
|
|
a41fe4 |
+# define __glibc_objsize(__o) __bos (__o)
|
|
|
a41fe4 |
+#endif
|
|
|
a41fe4 |
+
|
|
|
a41fe4 |
#if __GNUC_PREREQ (4,3)
|
|
|
a41fe4 |
# define __warndecl(name, msg) \
|
|
|
a41fe4 |
extern void name (void) __attribute__((__warning__ (msg)))
|