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