public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
* [PATCH 0/3] _FORTIFY_SOURCE=3 improvements
@ 2021-10-12 16:16 Siddhesh Poyarekar
  2021-10-12 16:16 ` [PATCH 1/3] Don't add access size hints to fortifiable functions Siddhesh Poyarekar
                   ` (4 more replies)
  0 siblings, 5 replies; 17+ messages in thread
From: Siddhesh Poyarekar @ 2021-10-12 16:16 UTC (permalink / raw)
  To: libc-alpha

This patchset changes the layout of fortified functions to make them
_FORTIFY_SOURCE=3 friendly and at the same time, continue working the
same for _FORTIFY_SOURCE=2 and lower.  At a high level it makes sure
that no branches are emitted at compile time and only one of either the
regular or _chk version of the function is called.  The conditions are
also reworked to make them more readable and foldable even in cases
where the compiler doesn't know the exact values of the operation length
and size, but can make decisions based on ranges of their values.

The changeset also adds some _FORTIFY_SOURCE=3 testing coverage on
compilers that are able to set that fortification level.

Siddhesh Poyarekar (3):
  Don't add access size hints to fortifiable functions
  Make sure that the fortified function conditionals are constant
  debug: Add tests for _FORTIFY_SOURCE=3

 debug/Makefile                 |  13 +-
 debug/tst-chk1.c               | 102 ++++++++-------
 debug/tst-chk7.c               |   2 +
 debug/tst-chk8.cc              |   2 +
 io/bits/poll2.h                |  31 ++---
 io/sys/poll.h                  |   6 +-
 libio/bits/stdio2.h            | 110 +++++++----------
 libio/stdio.h                  |   4 +-
 misc/sys/cdefs.h               |  71 +++++++++++
 posix/bits/unistd.h            | 174 ++++++--------------------
 posix/unistd.h                 |  28 +++--
 socket/bits/socket2.h          |  34 ++---
 stdlib/bits/stdlib.h           |  57 +++------
 stdlib/stdlib.h                |   5 +-
 string/bits/string_fortified.h |   5 +-
 string/string.h                |   2 +-
 wcsmbs/bits/wchar2.h           | 219 +++++++++------------------------
 17 files changed, 351 insertions(+), 514 deletions(-)
 create mode 100644 debug/tst-chk7.c
 create mode 100644 debug/tst-chk8.cc

-- 
2.31.1


^ permalink raw reply	[flat|nested] 17+ messages in thread

* [PATCH 1/3] Don't add access size hints to fortifiable functions
  2021-10-12 16:16 [PATCH 0/3] _FORTIFY_SOURCE=3 improvements Siddhesh Poyarekar
@ 2021-10-12 16:16 ` Siddhesh Poyarekar
  2021-10-19 17:54   ` Adhemerval Zanella
  2021-10-12 16:16 ` [PATCH 2/3] Make sure that the fortified function conditionals are constant Siddhesh Poyarekar
                   ` (3 subsequent siblings)
  4 siblings, 1 reply; 17+ messages in thread
From: Siddhesh Poyarekar @ 2021-10-12 16:16 UTC (permalink / raw)
  To: libc-alpha

In the context of a function definition, the size hints imply that the
size of an object pointed to by one parameter is another parameter.
This doesn't make sense for the fortified versions of the functions
since that's the bit it's trying to validate.

This is harmless with __builtin_object_size since it has fairly simple
semantics when it comes to objects passed as function parameters.
With __builtin_dynamic_object_size we could (as my patchset for gcc[1]
already does) use the access attribute to determine the object size in
the general case but it misleads the fortified functions.

Disable the access attribute for fortified function inline functions
when building at _FORTIFY_SOURCE=3 to make this work better.  The
access attributes remain for the _chk variants since they can be used
by the compiler to warn when the caller is passing invalid arguments.

Signed-off-by: Siddhesh Poyarekar <siddhesh@sourceware.org>
---
 io/bits/poll2.h                |  4 ++--
 io/sys/poll.h                  |  6 +++---
 libio/bits/stdio2.h            |  4 ++--
 libio/stdio.h                  |  4 ++--
 misc/sys/cdefs.h               | 10 ++++++++++
 posix/unistd.h                 | 28 ++++++++++++++++------------
 stdlib/stdlib.h                |  5 +++--
 string/bits/string_fortified.h |  5 +++--
 string/string.h                |  2 +-
 9 files changed, 42 insertions(+), 26 deletions(-)

diff --git a/io/bits/poll2.h b/io/bits/poll2.h
index a623678c09..be74d020f2 100644
--- a/io/bits/poll2.h
+++ b/io/bits/poll2.h
@@ -33,7 +33,7 @@ extern int __REDIRECT (__poll_chk_warn, (struct pollfd *__fds, nfds_t __nfds,
 		       __poll_chk)
   __warnattr ("poll called with fds buffer too small file nfds entries");
 
-__fortify_function __attr_access ((__write_only__, 1, 2)) int
+__fortify_function __fortified_attr_access (__write_only__, 1, 2) int
 poll (struct pollfd *__fds, nfds_t __nfds, int __timeout)
 {
   if (__glibc_objsize (__fds) != (__SIZE_TYPE__) -1)
@@ -64,7 +64,7 @@ extern int __REDIRECT (__ppoll_chk_warn, (struct pollfd *__fds, nfds_t __nfds,
 		       __ppoll_chk)
   __warnattr ("ppoll called with fds buffer too small file nfds entries");
 
-__fortify_function __attr_access ((__write_only__, 1, 2)) int
+__fortify_function __fortified_attr_access (__write_only__, 1, 2) int
 ppoll (struct pollfd *__fds, nfds_t __nfds, const struct timespec *__timeout,
        const __sigset_t *__ss)
 {
diff --git a/io/sys/poll.h b/io/sys/poll.h
index e640efb2bc..751c7f5f72 100644
--- a/io/sys/poll.h
+++ b/io/sys/poll.h
@@ -52,7 +52,7 @@ __BEGIN_DECLS
    This function is a cancellation point and therefore not marked with
    __THROW.  */
 extern int poll (struct pollfd *__fds, nfds_t __nfds, int __timeout)
-    __attr_access ((__write_only__, 1, 2));
+    __fortified_attr_access (__write_only__, 1, 2);
 
 #ifdef __USE_GNU
 /* Like poll, but before waiting the threads signal mask is replaced
@@ -64,7 +64,7 @@ extern int poll (struct pollfd *__fds, nfds_t __nfds, int __timeout)
 extern int ppoll (struct pollfd *__fds, nfds_t __nfds,
 		  const struct timespec *__timeout,
 		  const __sigset_t *__ss)
-    __attr_access ((__write_only__, 1, 2));
+    __fortified_attr_access (__write_only__, 1, 2);
 
 # ifdef __USE_TIME_BITS64
 #  ifdef __REDIRECT
@@ -72,7 +72,7 @@ extern int __REDIRECT (ppoll, (struct pollfd *__fds, nfds_t __nfds,
                                const struct timespec *__timeout,
                                const __sigset_t *__ss),
                        __ppoll64)
-    __attr_access ((__write_only__, 1, 2));
+    __fortified_attr_access (__write_only__, 1, 2);
 #  else
 #  define ppoll __ppoll64
 #  endif
diff --git a/libio/bits/stdio2.h b/libio/bits/stdio2.h
index 3f0cab1254..4f016a5638 100644
--- a/libio/bits/stdio2.h
+++ b/libio/bits/stdio2.h
@@ -258,7 +258,7 @@ extern char *__REDIRECT (__fgets_chk_warn,
      __wur __warnattr ("fgets called with bigger size than length "
 		       "of destination buffer");
 
-__fortify_function __wur __attr_access ((__write_only__, 1, 2)) char *
+__fortify_function __wur __fortified_attr_access (__write_only__, 1, 2) char *
 fgets (char *__restrict __s, int __n, FILE *__restrict __stream)
 {
   if (__glibc_objsize (__s) != (size_t) -1)
@@ -320,7 +320,7 @@ extern char *__REDIRECT (__fgets_unlocked_chk_warn,
      __wur __warnattr ("fgets_unlocked called with bigger size than length "
 		       "of destination buffer");
 
-__fortify_function __wur __attr_access ((__write_only__, 1, 2)) char *
+__fortify_function __wur __fortified_attr_access (__write_only__, 1, 2) char *
 fgets_unlocked (char *__restrict __s, int __n, FILE *__restrict __stream)
 {
   if (__glibc_objsize (__s) != (size_t) -1)
diff --git a/libio/stdio.h b/libio/stdio.h
index 0a5c76b552..f208f5ef79 100644
--- a/libio/stdio.h
+++ b/libio/stdio.h
@@ -590,7 +590,7 @@ extern int putw (int __w, FILE *__stream);
    This function is a possible cancellation point and therefore not
    marked with __THROW.  */
 extern char *fgets (char *__restrict __s, int __n, FILE *__restrict __stream)
-     __wur __attr_access ((__write_only__, 1, 2));
+     __wur __fortified_attr_access (__write_only__, 1, 2);
 
 #if __GLIBC_USE (DEPRECATED_GETS)
 /* Get a newline-terminated string from stdin, removing the newline.
@@ -614,7 +614,7 @@ extern char *gets (char *__s) __wur __attribute_deprecated__;
    therefore not marked with __THROW.  */
 extern char *fgets_unlocked (char *__restrict __s, int __n,
 			     FILE *__restrict __stream) __wur
-    __attr_access ((__write_only__, 1, 2));
+    __fortified_attr_access (__write_only__, 1, 2);
 #endif
 
 
diff --git a/misc/sys/cdefs.h b/misc/sys/cdefs.h
index c8e10aae17..d08c2adfd0 100644
--- a/misc/sys/cdefs.h
+++ b/misc/sys/cdefs.h
@@ -606,12 +606,22 @@ _Static_assert (0, "IEEE 128-bits long double requires redirection on this platf
    size-index is not provided:
      access (access-mode, <ref-index> [, <size-index>])  */
 #  define __attr_access(x) __attribute__ ((__access__ x))
+/* For _FORTIFY_SOURCE == 3 we use __builtin_dynamic_object_size, which may
+   use the access attribute to get object sizes from function definition
+   arguments, so we can't use them on functions we fortify.  Drop the object
+   size hints for such functions.  */
+#  if __USE_FORTIFY_LEVEL == 3
+#    define __fortified_attr_access(a, o, s) __attribute__ ((__access__ (a, o)))
+#  else
+#    define __fortified_attr_access(a, o, s) __attr_access ((a, o, s))
+#  endif
 #  if __GNUC_PREREQ (11, 0)
 #    define __attr_access_none(argno) __attribute__ ((__access__ (__none__, argno)))
 #  else
 #    define __attr_access_none(argno)
 #  endif
 #else
+#  define __fortified_attr_access(a, o, s)
 #  define __attr_access(x)
 #  define __attr_access_none(argno)
 #endif
diff --git a/posix/unistd.h b/posix/unistd.h
index 8224c5fbc9..7a61ff5e86 100644
--- a/posix/unistd.h
+++ b/posix/unistd.h
@@ -369,7 +369,7 @@ extern void closefrom (int __lowfd) __THROW;
    This function is a cancellation point and therefore not marked with
    __THROW.  */
 extern ssize_t read (int __fd, void *__buf, size_t __nbytes) __wur
-    __attr_access ((__write_only__, 2, 3));
+    __fortified_attr_access (__write_only__, 2, 3);
 
 /* Write N bytes of BUF to FD.  Return the number written, or -1.
 
@@ -388,7 +388,7 @@ extern ssize_t write (int __fd, const void *__buf, size_t __n) __wur
    __THROW.  */
 extern ssize_t pread (int __fd, void *__buf, size_t __nbytes,
 		      __off_t __offset) __wur
-    __attr_access ((__write_only__, 2, 3));
+    __fortified_attr_access (__write_only__, 2, 3);
 
 /* Write N bytes of BUF to FD at the given position OFFSET without
    changing the file pointer.  Return the number written, or -1.
@@ -404,7 +404,7 @@ extern ssize_t pwrite (int __fd, const void *__buf, size_t __n,
 extern ssize_t __REDIRECT (pread, (int __fd, void *__buf, size_t __nbytes,
 				   __off64_t __offset),
 			   pread64) __wur
-    __attr_access ((__write_only__, 2, 3));
+    __fortified_attr_access (__write_only__, 2, 3);
 extern ssize_t __REDIRECT (pwrite, (int __fd, const void *__buf,
 				    size_t __nbytes, __off64_t __offset),
 			   pwrite64) __wur
@@ -421,7 +421,7 @@ extern ssize_t __REDIRECT (pwrite, (int __fd, const void *__buf,
    or 0 for EOF.  */
 extern ssize_t pread64 (int __fd, void *__buf, size_t __nbytes,
 			__off64_t __offset) __wur
-    __attr_access ((__write_only__, 2, 3));
+    __fortified_attr_access (__write_only__, 2, 3);
 /* Write N bytes of BUF to FD at the given position OFFSET without
    changing the file pointer.  Return the number written, or -1.  */
 extern ssize_t pwrite64 (int __fd, const void *__buf, size_t __n,
@@ -642,7 +642,7 @@ extern long int sysconf (int __name) __THROW;
 #ifdef	__USE_POSIX2
 /* Get the value of the string-valued system variable NAME.  */
 extern size_t confstr (int __name, char *__buf, size_t __len) __THROW
-    __attr_access ((__write_only__, 2, 3));
+    __fortified_attr_access (__write_only__, 2, 3);
 #endif
 
 
@@ -709,7 +709,7 @@ extern __gid_t getegid (void) __THROW;
    the calling process is in.  Otherwise, fill in the group IDs
    of its supplementary groups in LIST and return the number written.  */
 extern int getgroups (int __size, __gid_t __list[]) __THROW __wur
-    __attr_access ((__write_only__, 2, 1));
+    __fortified_attr_access (__write_only__, 2, 1);
 #ifdef	__USE_GNU
 /* Return nonzero iff the calling process is in group GID.  */
 extern int group_member (__gid_t __gid) __THROW;
@@ -801,7 +801,8 @@ extern char *ttyname (int __fd) __THROW;
 /* Store at most BUFLEN characters of the pathname of the terminal FD is
    open on in BUF.  Return 0 on success, otherwise an error number.  */
 extern int ttyname_r (int __fd, char *__buf, size_t __buflen)
-     __THROW __nonnull ((2)) __wur __attr_access ((__write_only__, 2, 3));
+     __THROW __nonnull ((2)) __wur
+     __fortified_attr_access (__write_only__, 2, 3);
 
 /* Return 1 if FD is a valid descriptor associated
    with a terminal, zero if not.  */
@@ -836,7 +837,8 @@ extern int symlink (const char *__from, const char *__to)
    Returns the number of characters read, or -1 for errors.  */
 extern ssize_t readlink (const char *__restrict __path,
 			 char *__restrict __buf, size_t __len)
-     __THROW __nonnull ((1, 2)) __wur __attr_access ((__write_only__, 2, 3));
+     __THROW __nonnull ((1, 2)) __wur
+     __fortified_attr_access (__write_only__, 2, 3);
 
 #endif /* Use POSIX.1-2001.  */
 
@@ -848,7 +850,8 @@ extern int symlinkat (const char *__from, int __tofd,
 /* Like readlink but a relative PATH is interpreted relative to FD.  */
 extern ssize_t readlinkat (int __fd, const char *__restrict __path,
 			   char *__restrict __buf, size_t __len)
-     __THROW __nonnull ((2, 3)) __wur __attr_access ((__write_only__, 3, 4));
+     __THROW __nonnull ((2, 3)) __wur
+     __fortified_attr_access (__write_only__, 3, 4);
 #endif
 
 /* Remove the link NAME.  */
@@ -884,7 +887,7 @@ extern char *getlogin (void);
    This function is a possible cancellation point and therefore not
    marked with __THROW.  */
 extern int getlogin_r (char *__name, size_t __name_len) __nonnull ((1))
-    __attr_access ((__write_only__, 1, 2));
+    __fortified_attr_access (__write_only__, 1, 2);
 #endif
 
 #ifdef	__USE_MISC
@@ -906,7 +909,7 @@ extern int setlogin (const char *__name) __THROW __nonnull ((1));
    The result is null-terminated if LEN is large enough for the full
    name and the terminator.  */
 extern int gethostname (char *__name, size_t __len) __THROW __nonnull ((1))
-    __attr_access ((__write_only__, 1, 2));
+    __fortified_attr_access (__write_only__, 1, 2);
 #endif
 
 
@@ -925,7 +928,8 @@ extern int sethostid (long int __id) __THROW __wur;
    Called just like `gethostname' and `sethostname'.
    The NIS domain name is usually the empty string when not using NIS.  */
 extern int getdomainname (char *__name, size_t __len)
-     __THROW __nonnull ((1)) __wur __attr_access ((__write_only__, 1, 2));
+     __THROW __nonnull ((1)) __wur
+     __fortified_attr_access (__write_only__, 1, 2);
 extern int setdomainname (const char *__name, size_t __len)
      __THROW __nonnull ((1)) __wur __attr_access ((__read_only__, 1, 2));
 
diff --git a/stdlib/stdlib.h b/stdlib/stdlib.h
index 0481c12355..74c00eee73 100644
--- a/stdlib/stdlib.h
+++ b/stdlib/stdlib.h
@@ -943,7 +943,8 @@ extern size_t mbstowcs (wchar_t *__restrict  __pwcs,
 extern size_t wcstombs (char *__restrict __s,
 			const wchar_t *__restrict __pwcs, size_t __n)
      __THROW
-  __attr_access ((__write_only__, 1, 3)) __attr_access ((__read_only__, 2));
+  __fortified_attr_access (__write_only__, 1, 3)
+  __attr_access ((__read_only__, 2));
 
 #ifdef __USE_MISC
 /* Determine whether the string value of RESPONSE matches the affirmation
@@ -997,7 +998,7 @@ extern char *ptsname (int __fd) __THROW __wur;
    terminal associated with the master FD is open on in BUF.
    Return 0 on success, otherwise an error number.  */
 extern int ptsname_r (int __fd, char *__buf, size_t __buflen)
-     __THROW __nonnull ((2)) __attr_access ((__write_only__, 2, 3));
+     __THROW __nonnull ((2)) __fortified_attr_access (__write_only__, 2, 3);
 
 /* Open a master pseudo terminal and return its file descriptor.  */
 extern int getpt (void);
diff --git a/string/bits/string_fortified.h b/string/bits/string_fortified.h
index 67ae2c6b50..5731274848 100644
--- a/string/bits/string_fortified.h
+++ b/string/bits/string_fortified.h
@@ -64,7 +64,7 @@ __NTH (memset (void *__dest, int __ch, size_t __len))
 # include <bits/strings_fortified.h>
 
 void __explicit_bzero_chk (void *__dest, size_t __len, size_t __destlen)
-  __THROW __nonnull ((1)) __attr_access ((__write_only__, 1, 2));
+  __THROW __nonnull ((1)) __fortified_attr_access (__write_only__, 1, 2);
 
 __fortify_function void
 __NTH (explicit_bzero (void *__dest, size_t __len))
@@ -106,7 +106,8 @@ __NTH (stpncpy (char *__dest, const char *__src, size_t __n))
 #else
 extern char *__stpncpy_chk (char *__dest, const char *__src, size_t __n,
 			    size_t __destlen) __THROW
-  __attr_access ((__write_only__, 1, 3)) __attr_access ((__read_only__, 2));
+  __fortified_attr_access ((__write_only__, 1, 3))
+  __attr_access ((__read_only__, 2));
 extern char *__REDIRECT_NTH (__stpncpy_alias, (char *__dest, const char *__src,
 					       size_t __n), stpncpy);
 
diff --git a/string/string.h b/string/string.h
index 04e1b7067d..8dcafb4ac4 100644
--- a/string/string.h
+++ b/string/string.h
@@ -448,7 +448,7 @@ extern char *strerror_l (int __errnum, locale_t __l) __THROW;
 /* Set N bytes of S to 0.  The compiler will not delete a call to this
    function, even if S is dead after the call.  */
 extern void explicit_bzero (void *__s, size_t __n) __THROW __nonnull ((1))
-    __attr_access ((__write_only__, 1, 2));
+    __fortified_attr_access (__write_only__, 1, 2);
 
 /* Return the next DELIM-delimited token from *STRINGP,
    terminating it with a '\0', and update *STRINGP to point past it.  */
-- 
2.31.1


^ permalink raw reply	[flat|nested] 17+ messages in thread

* [PATCH 2/3] Make sure that the fortified function conditionals are constant
  2021-10-12 16:16 [PATCH 0/3] _FORTIFY_SOURCE=3 improvements Siddhesh Poyarekar
  2021-10-12 16:16 ` [PATCH 1/3] Don't add access size hints to fortifiable functions Siddhesh Poyarekar
@ 2021-10-12 16:16 ` Siddhesh Poyarekar
  2021-10-19 19:34   ` Adhemerval Zanella
  2021-10-12 16:16 ` [PATCH 3/3] debug: Add tests for _FORTIFY_SOURCE=3 Siddhesh Poyarekar
                   ` (2 subsequent siblings)
  4 siblings, 1 reply; 17+ messages in thread
From: Siddhesh Poyarekar @ 2021-10-12 16:16 UTC (permalink / raw)
  To: libc-alpha

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 <siddhesh@sourceware.org>
---
 io/bits/poll2.h       |  27 ++----
 libio/bits/stdio2.h   | 106 +++++++++-----------
 misc/sys/cdefs.h      |  61 ++++++++++++
 posix/bits/unistd.h   | 174 ++++++++-------------------------
 socket/bits/socket2.h |  34 +++----
 stdlib/bits/stdlib.h  |  57 ++++-------
 wcsmbs/bits/wchar2.h  | 219 ++++++++++++------------------------------
 7 files changed, 240 insertions(+), 438 deletions(-)

diff --git a/io/bits/poll2.h b/io/bits/poll2.h
index be74d020f2..91cdcaf66a 100644
--- a/io/bits/poll2.h
+++ b/io/bits/poll2.h
@@ -36,16 +36,9 @@ extern int __REDIRECT (__poll_chk_warn, (struct pollfd *__fds, nfds_t __nfds,
 __fortify_function __fortified_attr_access (__write_only__, 1, 2) int
 poll (struct pollfd *__fds, nfds_t __nfds, int __timeout)
 {
-  if (__glibc_objsize (__fds) != (__SIZE_TYPE__) -1)
-    {
-      if (! __builtin_constant_p (__nfds))
-	return __poll_chk (__fds, __nfds, __timeout, __glibc_objsize (__fds));
-      else if (__glibc_objsize (__fds) / sizeof (*__fds) < __nfds)
-	return __poll_chk_warn (__fds, __nfds, __timeout,
-				__glibc_objsize (__fds));
-    }
-
-  return __poll_alias (__fds, __nfds, __timeout);
+  return __glibc_fortify (poll, __nfds, sizeof (*__fds),
+			  __glibc_objsize (__fds),
+			  __fds, __nfds, __timeout);
 }
 
 
@@ -68,17 +61,9 @@ __fortify_function __fortified_attr_access (__write_only__, 1, 2) int
 ppoll (struct pollfd *__fds, nfds_t __nfds, const struct timespec *__timeout,
        const __sigset_t *__ss)
 {
-  if (__glibc_objsize (__fds) != (__SIZE_TYPE__) -1)
-    {
-      if (! __builtin_constant_p (__nfds))
-	return __ppoll_chk (__fds, __nfds, __timeout, __ss,
-			    __glibc_objsize (__fds));
-      else if (__glibc_objsize (__fds) / sizeof (*__fds) < __nfds)
-	return __ppoll_chk_warn (__fds, __nfds, __timeout, __ss,
-				 __glibc_objsize (__fds));
-    }
-
-  return __ppoll_alias (__fds, __nfds, __timeout, __ss);
+  return __glibc_fortify (ppoll, __nfds, sizeof (*__fds),
+			  __glibc_objsize (__fds),
+			  __fds, __nfds, __timeout, __ss);
 }
 #endif
 
diff --git a/libio/bits/stdio2.h b/libio/bits/stdio2.h
index 4f016a5638..c111d13320 100644
--- a/libio/bits/stdio2.h
+++ b/libio/bits/stdio2.h
@@ -261,15 +261,12 @@ extern char *__REDIRECT (__fgets_chk_warn,
 __fortify_function __wur __fortified_attr_access (__write_only__, 1, 2) char *
 fgets (char *__restrict __s, int __n, FILE *__restrict __stream)
 {
-  if (__glibc_objsize (__s) != (size_t) -1)
-    {
-      if (!__builtin_constant_p (__n) || __n <= 0)
-	return __fgets_chk (__s, __glibc_objsize (__s), __n, __stream);
-
-      if ((size_t) __n > __glibc_objsize (__s))
-	return __fgets_chk_warn (__s, __glibc_objsize (__s), __n, __stream);
-    }
-  return __fgets_alias (__s, __n, __stream);
+  size_t sz = __glibc_objsize (__s);
+  if (__glibc_safe_or_unknown_len_signed (__n, sizeof (char), sz))
+    return __fgets_alias (__s, __n, __stream);
+  if (__glibc_unsafe_len_signed (__n, sizeof (char), sz))
+    return __fgets_chk_warn (__s, sz, __n, __stream);
+  return __fgets_chk (__s, sz, __n, __stream);
 }
 
 extern size_t __fread_chk (void *__restrict __ptr, size_t __ptrlen,
@@ -291,19 +288,12 @@ __fortify_function __wur size_t
 fread (void *__restrict __ptr, size_t __size, size_t __n,
        FILE *__restrict __stream)
 {
-  if (__glibc_objsize0 (__ptr) != (size_t) -1)
-    {
-      if (!__builtin_constant_p (__size)
-	  || !__builtin_constant_p (__n)
-	  || (__size | __n) >= (((size_t) 1) << (8 * sizeof (size_t) / 2)))
-	return __fread_chk (__ptr, __glibc_objsize0 (__ptr), __size, __n,
-			    __stream);
-
-      if (__size * __n > __glibc_objsize0 (__ptr))
-	return __fread_chk_warn (__ptr, __glibc_objsize0 (__ptr), __size, __n,
-				 __stream);
-    }
-  return __fread_alias (__ptr, __size, __n, __stream);
+  size_t sz = __glibc_objsize0 (__ptr);
+  if (__glibc_safe_or_unknown_len (__n, __size, sz))
+    return __fread_alias (__ptr, __size, __n, __stream);
+  if (__glibc_unsafe_len (__n, __size, sz))
+    return __fread_chk_warn (__ptr, sz, __size, __n, __stream);
+  return __fread_chk (__ptr, sz, __size, __n, __stream);
 }
 
 #ifdef __USE_GNU
@@ -323,17 +313,12 @@ extern char *__REDIRECT (__fgets_unlocked_chk_warn,
 __fortify_function __wur __fortified_attr_access (__write_only__, 1, 2) char *
 fgets_unlocked (char *__restrict __s, int __n, FILE *__restrict __stream)
 {
-  if (__glibc_objsize (__s) != (size_t) -1)
-    {
-      if (!__builtin_constant_p (__n) || __n <= 0)
-	return __fgets_unlocked_chk (__s, __glibc_objsize (__s), __n,
-				     __stream);
-
-      if ((size_t) __n > __glibc_objsize (__s))
-	return __fgets_unlocked_chk_warn (__s, __glibc_objsize (__s), __n,
-					  __stream);
-    }
-  return __fgets_unlocked_alias (__s, __n, __stream);
+  size_t sz = __glibc_objsize (__s);
+  if (__glibc_safe_or_unknown_len_signed (__n, sizeof (char), sz))
+    return __fgets_unlocked_alias (__s, __n, __stream);
+  if (__glibc_unsafe_len_signed (__n, sizeof (char), sz))
+    return __fgets_unlocked_chk_warn (__s, sz, __n, __stream);
+  return __fgets_unlocked_chk (__s, sz, __n, __stream);
 }
 #endif
 
@@ -358,41 +343,36 @@ __fortify_function __wur size_t
 fread_unlocked (void *__restrict __ptr, size_t __size, size_t __n,
 		FILE *__restrict __stream)
 {
-  if (__glibc_objsize0 (__ptr) != (size_t) -1)
+  size_t sz = __glibc_objsize0 (__ptr);
+  if (__glibc_safe_or_unknown_len (__n, __size, sz))
     {
-      if (!__builtin_constant_p (__size)
-	  || !__builtin_constant_p (__n)
-	  || (__size | __n) >= (((size_t) 1) << (8 * sizeof (size_t) / 2)))
-	return __fread_unlocked_chk (__ptr, __glibc_objsize0 (__ptr), __size,
-				     __n, __stream);
-
-      if (__size * __n > __glibc_objsize0 (__ptr))
-	return __fread_unlocked_chk_warn (__ptr, __glibc_objsize0 (__ptr),
-					  __size, __n, __stream);
-    }
-
 # ifdef __USE_EXTERN_INLINES
-  if (__builtin_constant_p (__size)
-      && __builtin_constant_p (__n)
-      && (__size | __n) < (((size_t) 1) << (8 * sizeof (size_t) / 2))
-      && __size * __n <= 8)
-    {
-      size_t __cnt = __size * __n;
-      char *__cptr = (char *) __ptr;
-      if (__cnt == 0)
-	return 0;
-
-      for (; __cnt > 0; --__cnt)
+      if (__builtin_constant_p (__size)
+	  && __builtin_constant_p (__n)
+	  && (__size | __n) < (((size_t) 1) << (8 * sizeof (size_t) / 2))
+	  && __size * __n <= 8)
 	{
-	  int __c = getc_unlocked (__stream);
-	  if (__c == EOF)
-	    break;
-	  *__cptr++ = __c;
+	  size_t __cnt = __size * __n;
+	  char *__cptr = (char *) __ptr;
+	  if (__cnt == 0)
+	    return 0;
+
+	  for (; __cnt > 0; --__cnt)
+	    {
+	      int __c = getc_unlocked (__stream);
+	      if (__c == EOF)
+		break;
+	      *__cptr++ = __c;
+	    }
+	  return (__cptr - (char *) __ptr) / __size;
 	}
-      return (__cptr - (char *) __ptr) / __size;
-    }
 # endif
-  return __fread_unlocked_alias (__ptr, __size, __n, __stream);
+      return __fread_unlocked_alias (__ptr, __size, __n, __stream);
+    }
+  if (__glibc_unsafe_len (__n, __size, sz))
+    return __fread_unlocked_chk_warn (__ptr, sz, __size, __n, __stream);
+  return __fread_unlocked_chk (__ptr, sz, __size, __n, __stream);
+
 }
 #endif
 
diff --git a/misc/sys/cdefs.h b/misc/sys/cdefs.h
index d08c2adfd0..22062ffaa8 100644
--- a/misc/sys/cdefs.h
+++ b/misc/sys/cdefs.h
@@ -151,6 +151,67 @@
 # define __glibc_objsize(__o) __bos (__o)
 #endif
 
+/* Compile time conditions to choose between the regular, _chk and _chk_warn
+   variants.  These conditions should get evaluated to constant and optimized
+   away.  */
+
+#define __glibc_safe_len_cond(__l, __s, __osz) ((__l) <= (__osz) / (__s))
+
+/* Length is known to be safe at compile time if the __L * __S <= __OBJSZ
+   condition can be folded to a constant and if it is true.  The -1 check is
+   redundant because since it implies that __glibc_safe_len_cond is true.  */
+#define __glibc_safe_or_unknown_len(__l, __s, __osz) \
+  (__builtin_constant_p (__glibc_safe_len_cond (__l, __s, __osz))	      \
+   && __glibc_safe_len_cond (__l, __s, __osz))
+
+/* Same as above, but add a sign check.  */
+
+#define __glibc_safe_or_unknown_len_signed(__l, __s, __osz) \
+  (__builtin_constant_p (__l) && (__l) > 0				      \
+   && __glibc_safe_or_unknown_len ((__SIZE_TYPE__) (__l), (__s), (__osz)))
+
+/* Conversely, we know at compile time that the length is safe if the
+   __L * __S <= __OBJSZ condition can be folded to a constant and if it is
+   false.  */
+#define __glibc_unsafe_len(__l, __s, __osz) \
+  (__builtin_constant_p (__glibc_safe_len_cond (__l, __s, __osz))	      \
+   && !__glibc_safe_len_cond (__l, __s, __osz))
+
+/* Same as above, but add a sign check.  */
+
+#define __glibc_unsafe_len_signed(__l, __s, __osz) \
+  (__builtin_constant_p (__l) && (__l) > 0				      \
+   && __glibc_unsafe_len ((__SIZE_TYPE__) (__l), (__s), (__osz)))
+
+/* Fortify function f.  __f_alias, __f_chk and __f_chk_warn must be
+   declared.  */
+
+#define __glibc_fortify(f, __l, __s, __osz, ...) \
+  (__glibc_safe_or_unknown_len (__l, __s, __osz)			      \
+   ? __ ## f ## _alias (__VA_ARGS__)					      \
+   : (__glibc_unsafe_len (__l, __s, __osz)				      \
+      ? __ ## f ## _chk_warn (__VA_ARGS__, __osz)			      \
+      : __ ## f ## _chk (__VA_ARGS__, __osz)))			      \
+
+/* Fortify function f, with signed length __l.  */
+
+#define __glibc_fortify_signed(f, __l, __s, __osz, ...) \
+  (__glibc_safe_or_unknown_len_signed (__l, __s, __osz)			      \
+   ? __ ## f ## _alias (__VA_ARGS__)					      \
+   : (__glibc_unsafe_len_signed (__l, __s, __osz)			      \
+      ? __ ## f ## _chk_warn (__VA_ARGS__, __osz)			      \
+      : __ ## f ## _chk (__VA_ARGS__, __osz)))			      \
+
+/* Fortify function f, where object size argument passed to f is the number of
+   elements and not total size.  */
+
+#define __glibc_fortify_n(f, __l, __s, __osz, ...) \
+  (__glibc_safe_or_unknown_len (__l, __s, __osz)			      \
+   ? __ ## f ## _alias (__VA_ARGS__)					      \
+   : (__glibc_unsafe_len (__l, __s, __osz)				      \
+      ? __ ## f ## _chk_warn (__VA_ARGS__, (__osz) / (__s))		      \
+      : __ ## f ## _chk (__VA_ARGS__, (__osz) / (__s))))		      \
+
 #if __GNUC_PREREQ (4,3)
 # define __warnattr(msg) __attribute__((__warning__ (msg)))
 # define __errordecl(name, msg) \
diff --git a/posix/bits/unistd.h b/posix/bits/unistd.h
index 622adeb2b2..dd8d71d83c 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_signed (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
diff --git a/socket/bits/socket2.h b/socket/bits/socket2.h
index 9c8ac69624..b28cde55f3 100644
--- a/socket/bits/socket2.h
+++ b/socket/bits/socket2.h
@@ -33,17 +33,12 @@ extern ssize_t __REDIRECT (__recv_chk_warn,
 __fortify_function ssize_t
 recv (int __fd, void *__buf, size_t __n, int __flags)
 {
-  if (__glibc_objsize0 (__buf) != (size_t) -1)
-    {
-      if (!__builtin_constant_p (__n))
-	return __recv_chk (__fd, __buf, __n, __glibc_objsize0 (__buf),
-			   __flags);
-
-      if (__n > __glibc_objsize0 (__buf))
-	return __recv_chk_warn (__fd, __buf, __n, __glibc_objsize0 (__buf),
-				__flags);
-    }
-  return __recv_alias (__fd, __buf, __n, __flags);
+  size_t sz = __glibc_objsize0 (__buf);
+  if (__glibc_safe_or_unknown_len (__n, sizeof (char), sz))
+    return __recv_alias (__fd, __buf, __n, __flags);
+  if (__glibc_unsafe_len (__n, sizeof (char), sz))
+    return __recv_chk_warn (__fd, __buf, __n, sz, __flags);
+  return __recv_chk (__fd, __buf, __n, sz, __flags);
 }
 
 extern ssize_t __recvfrom_chk (int __fd, void *__restrict __buf, size_t __n,
@@ -66,14 +61,11 @@ __fortify_function ssize_t
 recvfrom (int __fd, void *__restrict __buf, size_t __n, int __flags,
 	  __SOCKADDR_ARG __addr, socklen_t *__restrict __addr_len)
 {
-  if (__glibc_objsize0 (__buf) != (size_t) -1)
-    {
-      if (!__builtin_constant_p (__n))
-	return __recvfrom_chk (__fd, __buf, __n, __glibc_objsize0 (__buf),
-			       __flags, __addr, __addr_len);
-      if (__n > __glibc_objsize0 (__buf))
-	return __recvfrom_chk_warn (__fd, __buf, __n, __glibc_objsize0 (__buf),
-				    __flags, __addr, __addr_len);
-    }
-  return __recvfrom_alias (__fd, __buf, __n, __flags, __addr, __addr_len);
+  size_t sz = __glibc_objsize0 (__buf);
+  if (__glibc_safe_or_unknown_len (__n, sizeof (char), sz))
+    return __recvfrom_alias (__fd, __buf, __n, __flags, __addr, __addr_len);
+  if (__glibc_unsafe_len (__n, sizeof (char), sz))
+    return __recvfrom_chk_warn (__fd, __buf, __n, sz, __flags, __addr,
+				__addr_len);
+  return __recvfrom_chk (__fd, __buf, __n, sz, __flags, __addr, __addr_len);
 }
diff --git a/stdlib/bits/stdlib.h b/stdlib/bits/stdlib.h
index eae31b38f0..067115eeca 100644
--- a/stdlib/bits/stdlib.h
+++ b/stdlib/bits/stdlib.h
@@ -36,17 +36,16 @@ extern char *__REDIRECT_NTH (__realpath_chk_warn,
 __fortify_function __wur char *
 __NTH (realpath (const char *__restrict __name, char *__restrict __resolved))
 {
-  if (__glibc_objsize (__resolved) != (size_t) -1)
-    {
+  size_t sz = __glibc_objsize (__resolved);
+
+  if (sz == (size_t) -1)
+    return __realpath_alias (__name, __resolved);
+
 #if defined _LIBC_LIMITS_H_ && defined PATH_MAX
-      if (__glibc_objsize (__resolved) < PATH_MAX)
-	return __realpath_chk_warn (__name, __resolved,
-				    __glibc_objsize (__resolved));
+  if (__glibc_unsafe_len (sz, sizeof (char), PATH_MAX))
+    return __realpath_chk_warn (__name, __resolved, sz);
 #endif
-      return __realpath_chk (__name, __resolved, __glibc_objsize (__resolved));
-    }
-
-  return __realpath_alias (__name, __resolved);
+  return __realpath_chk (__name, __resolved, sz);
 }
 
 
@@ -65,16 +64,9 @@ extern int __REDIRECT_NTH (__ptsname_r_chk_warn,
 __fortify_function int
 __NTH (ptsname_r (int __fd, char *__buf, size_t __buflen))
 {
-  if (__glibc_objsize (__buf) != (size_t) -1)
-    {
-      if (!__builtin_constant_p (__buflen))
-	return __ptsname_r_chk (__fd, __buf, __buflen,
-				__glibc_objsize (__buf));
-      if (__buflen > __glibc_objsize (__buf))
-	return __ptsname_r_chk_warn (__fd, __buf, __buflen,
-				     __glibc_objsize (__buf));
-    }
-  return __ptsname_r_alias (__fd, __buf, __buflen);
+  return __glibc_fortify (ptsname_r, __buflen, sizeof (char),
+			  __glibc_objsize (__buf),
+			  __fd, __buf, __buflen);
 }
 
 
@@ -120,18 +112,9 @@ __fortify_function size_t
 __NTH (mbstowcs (wchar_t *__restrict __dst, const char *__restrict __src,
 		 size_t __len))
 {
-  if (__glibc_objsize (__dst) != (size_t) -1)
-    {
-      if (!__builtin_constant_p (__len))
-	return __mbstowcs_chk (__dst, __src, __len,
-			       __glibc_objsize (__dst) / sizeof (wchar_t));
-
-      if (__len > __glibc_objsize (__dst) / sizeof (wchar_t))
-	return __mbstowcs_chk_warn (__dst, __src, __len,
-				    (__glibc_objsize (__dst)
-				     / sizeof (wchar_t)));
-    }
-  return __mbstowcs_alias (__dst, __src, __len);
+  return __glibc_fortify_n (mbstowcs, __len, sizeof (wchar_t),
+			    __glibc_objsize (__dst),
+			    __dst, __src, __len);
 }
 
 
@@ -154,13 +137,7 @@ __fortify_function size_t
 __NTH (wcstombs (char *__restrict __dst, const wchar_t *__restrict __src,
 		 size_t __len))
 {
-  if (__glibc_objsize (__dst) != (size_t) -1)
-    {
-      if (!__builtin_constant_p (__len))
-	return __wcstombs_chk (__dst, __src, __len, __glibc_objsize (__dst));
-      if (__len > __glibc_objsize (__dst))
-	return __wcstombs_chk_warn (__dst, __src, __len,
-				    __glibc_objsize (__dst));
-    }
-  return __wcstombs_alias (__dst, __src, __len);
+  return __glibc_fortify (wcstombs, __len, sizeof (char),
+			  __glibc_objsize (__dst),
+			  __dst, __src, __len);
 }
diff --git a/wcsmbs/bits/wchar2.h b/wcsmbs/bits/wchar2.h
index ea2518dc72..2f42fdcc64 100644
--- a/wcsmbs/bits/wchar2.h
+++ b/wcsmbs/bits/wchar2.h
@@ -39,17 +39,9 @@ __fortify_function wchar_t *
 __NTH (wmemcpy (wchar_t *__restrict __s1, const wchar_t *__restrict __s2,
 		size_t __n))
 {
-  if (__glibc_objsize0 (__s1) != (size_t) -1)
-    {
-      if (!__builtin_constant_p (__n))
-	return __wmemcpy_chk (__s1, __s2, __n,
-			      __glibc_objsize0 (__s1) / sizeof (wchar_t));
-
-      if (__n > __glibc_objsize0 (__s1) / sizeof (wchar_t))
-	return __wmemcpy_chk_warn (__s1, __s2, __n,
-				   __glibc_objsize0 (__s1) / sizeof (wchar_t));
-    }
-  return __wmemcpy_alias (__s1, __s2, __n);
+  return __glibc_fortify_n (wmemcpy, __n, sizeof (wchar_t),
+			    __glibc_objsize0 (__s1),
+			    __s1, __s2, __n);
 }
 
 
@@ -67,18 +59,9 @@ extern wchar_t *__REDIRECT_NTH (__wmemmove_chk_warn,
 __fortify_function wchar_t *
 __NTH (wmemmove (wchar_t *__s1, const wchar_t *__s2, size_t __n))
 {
-  if (__glibc_objsize0 (__s1) != (size_t) -1)
-    {
-      if (!__builtin_constant_p (__n))
-	return __wmemmove_chk (__s1, __s2, __n,
-			       __glibc_objsize0 (__s1) / sizeof (wchar_t));
-
-      if (__n > __glibc_objsize0 (__s1) / sizeof (wchar_t))
-	return __wmemmove_chk_warn (__s1, __s2, __n,
-				    (__glibc_objsize0 (__s1)
-				     / sizeof (wchar_t)));
-    }
-  return __wmemmove_alias (__s1, __s2, __n);
+  return __glibc_fortify_n (wmemmove, __n, sizeof (wchar_t),
+			    __glibc_objsize0 (__s1),
+			    __s1, __s2, __n);
 }
 
 
@@ -101,18 +84,9 @@ __fortify_function wchar_t *
 __NTH (wmempcpy (wchar_t *__restrict __s1, const wchar_t *__restrict __s2,
 		 size_t __n))
 {
-  if (__glibc_objsize0 (__s1) != (size_t) -1)
-    {
-      if (!__builtin_constant_p (__n))
-	return __wmempcpy_chk (__s1, __s2, __n,
-			       __glibc_objsize0 (__s1) / sizeof (wchar_t));
-
-      if (__n > __glibc_objsize0 (__s1) / sizeof (wchar_t))
-	return __wmempcpy_chk_warn (__s1, __s2, __n,
-				    (__glibc_objsize0 (__s1)
-				     / sizeof (wchar_t)));
-    }
-  return __wmempcpy_alias (__s1, __s2, __n);
+  return __glibc_fortify_n (wmempcpy, __n, sizeof (wchar_t),
+			    __glibc_objsize0 (__s1),
+			    __s1, __s2, __n);
 }
 #endif
 
@@ -130,17 +104,9 @@ extern wchar_t *__REDIRECT_NTH (__wmemset_chk_warn,
 __fortify_function wchar_t *
 __NTH (wmemset (wchar_t *__s, wchar_t __c, size_t __n))
 {
-  if (__glibc_objsize0 (__s) != (size_t) -1)
-    {
-      if (!__builtin_constant_p (__n))
-	return __wmemset_chk (__s, __c, __n,
-			      __glibc_objsize0 (__s) / sizeof (wchar_t));
-
-      if (__n > __glibc_objsize0 (__s) / sizeof (wchar_t))
-	return __wmemset_chk_warn (__s, __c, __n,
-				   __glibc_objsize0 (__s) / sizeof (wchar_t));
-    }
-  return __wmemset_alias (__s, __c, __n);
+  return __glibc_fortify_n (wmemset, __n, sizeof (wchar_t),
+			    __glibc_objsize0 (__s),
+			    __s, __c, __n);
 }
 
 
@@ -154,9 +120,9 @@ extern wchar_t *__REDIRECT_NTH (__wcscpy_alias,
 __fortify_function wchar_t *
 __NTH (wcscpy (wchar_t *__restrict __dest, const wchar_t *__restrict __src))
 {
-  if (__glibc_objsize (__dest) != (size_t) -1)
-    return __wcscpy_chk (__dest, __src,
-			 __glibc_objsize (__dest) / sizeof (wchar_t));
+  size_t sz = __glibc_objsize (__dest);
+  if (sz != (size_t) -1)
+    return __wcscpy_chk (__dest, __src, sz / sizeof (wchar_t));
   return __wcscpy_alias (__dest, __src);
 }
 
@@ -171,9 +137,9 @@ extern wchar_t *__REDIRECT_NTH (__wcpcpy_alias,
 __fortify_function wchar_t *
 __NTH (wcpcpy (wchar_t *__restrict __dest, const wchar_t *__restrict __src))
 {
-  if (__glibc_objsize (__dest) != (size_t) -1)
-    return __wcpcpy_chk (__dest, __src,
-			 __glibc_objsize (__dest) / sizeof (wchar_t));
+  size_t sz = __glibc_objsize (__dest);
+  if (sz != (size_t) -1)
+    return __wcpcpy_chk (__dest, __src, sz / sizeof (wchar_t));
   return __wcpcpy_alias (__dest, __src);
 }
 
@@ -196,17 +162,9 @@ __fortify_function wchar_t *
 __NTH (wcsncpy (wchar_t *__restrict __dest, const wchar_t *__restrict __src,
 		size_t __n))
 {
-  if (__glibc_objsize (__dest) != (size_t) -1)
-    {
-      if (!__builtin_constant_p (__n))
-	return __wcsncpy_chk (__dest, __src, __n,
-			      __glibc_objsize (__dest) / sizeof (wchar_t));
-      if (__n > __glibc_objsize (__dest) / sizeof (wchar_t))
-	return __wcsncpy_chk_warn (__dest, __src, __n,
-				   (__glibc_objsize (__dest)
-				    / sizeof (wchar_t)));
-    }
-  return __wcsncpy_alias (__dest, __src, __n);
+  return __glibc_fortify_n (wcsncpy, __n, sizeof (wchar_t),
+			    __glibc_objsize (__dest),
+			    __dest, __src, __n);
 }
 
 
@@ -228,17 +186,9 @@ __fortify_function wchar_t *
 __NTH (wcpncpy (wchar_t *__restrict __dest, const wchar_t *__restrict __src,
 		size_t __n))
 {
-  if (__glibc_objsize (__dest) != (size_t) -1)
-    {
-      if (!__builtin_constant_p (__n))
-	return __wcpncpy_chk (__dest, __src, __n,
-			      __glibc_objsize (__dest) / sizeof (wchar_t));
-      if (__n > __glibc_objsize (__dest) / sizeof (wchar_t))
-	return __wcpncpy_chk_warn (__dest, __src, __n,
-				   (__glibc_objsize (__dest)
-				    / sizeof (wchar_t)));
-    }
-  return __wcpncpy_alias (__dest, __src, __n);
+  return __glibc_fortify_n (wcpncpy, __n, sizeof (wchar_t),
+			    __glibc_objsize (__dest),
+			    __dest, __src, __n);
 }
 
 
@@ -252,9 +202,9 @@ extern wchar_t *__REDIRECT_NTH (__wcscat_alias,
 __fortify_function wchar_t *
 __NTH (wcscat (wchar_t *__restrict __dest, const wchar_t *__restrict __src))
 {
-  if (__glibc_objsize (__dest) != (size_t) -1)
-    return __wcscat_chk (__dest, __src,
-			 __glibc_objsize (__dest) / sizeof (wchar_t));
+  size_t sz = __glibc_objsize (__dest);
+  if (sz != (size_t) -1)
+    return __wcscat_chk (__dest, __src, sz / sizeof (wchar_t));
   return __wcscat_alias (__dest, __src);
 }
 
@@ -271,9 +221,9 @@ __fortify_function wchar_t *
 __NTH (wcsncat (wchar_t *__restrict __dest, const wchar_t *__restrict __src,
 		size_t __n))
 {
-  if (__glibc_objsize (__dest) != (size_t) -1)
-    return __wcsncat_chk (__dest, __src, __n,
-			  __glibc_objsize (__dest) / sizeof (wchar_t));
+  size_t sz = __glibc_objsize (__dest);
+  if (sz != (size_t) -1)
+    return __wcsncat_chk (__dest, __src, __n, sz / sizeof (wchar_t));
   return __wcsncat_alias (__dest, __src, __n);
 }
 
@@ -293,10 +243,10 @@ __fortify_function int
 __NTH (swprintf (wchar_t *__restrict __s, size_t __n,
 		 const wchar_t *__restrict __fmt, ...))
 {
-  if (__glibc_objsize (__s) != (size_t) -1 || __USE_FORTIFY_LEVEL > 1)
+  size_t sz = __glibc_objsize (__s);
+  if (sz != (size_t) -1 || __USE_FORTIFY_LEVEL > 1)
     return __swprintf_chk (__s, __n, __USE_FORTIFY_LEVEL - 1,
-			   __glibc_objsize (__s) / sizeof (wchar_t),
-			   __fmt, __va_arg_pack ());
+			   sz / sizeof (wchar_t), __fmt, __va_arg_pack ());
   return __swprintf_alias (__s, __n, __fmt, __va_arg_pack ());
 }
 #elif !defined __cplusplus
@@ -323,10 +273,10 @@ __fortify_function int
 __NTH (vswprintf (wchar_t *__restrict __s, size_t __n,
 		  const wchar_t *__restrict __fmt, __gnuc_va_list __ap))
 {
-  if (__glibc_objsize (__s) != (size_t) -1 || __USE_FORTIFY_LEVEL > 1)
+  size_t sz = __glibc_objsize (__s);
+  if (sz != (size_t) -1 || __USE_FORTIFY_LEVEL > 1)
     return __vswprintf_chk (__s, __n,  __USE_FORTIFY_LEVEL - 1,
-			    __glibc_objsize (__s) / sizeof (wchar_t), __fmt,
-			    __ap);
+			    sz / sizeof (wchar_t), __fmt, __ap);
   return __vswprintf_alias (__s, __n, __fmt, __ap);
 }
 
@@ -392,18 +342,12 @@ extern wchar_t *__REDIRECT (__fgetws_chk_warn,
 __fortify_function __wur wchar_t *
 fgetws (wchar_t *__restrict __s, int __n, __FILE *__restrict __stream)
 {
-  if (__glibc_objsize (__s) != (size_t) -1)
-    {
-      if (!__builtin_constant_p (__n) || __n <= 0)
-	return __fgetws_chk (__s, __glibc_objsize (__s) / sizeof (wchar_t),
-			     __n, __stream);
-
-      if ((size_t) __n > __glibc_objsize (__s) / sizeof (wchar_t))
-	return __fgetws_chk_warn (__s,
-				  __glibc_objsize (__s) / sizeof (wchar_t),
-				  __n, __stream);
-    }
-  return __fgetws_alias (__s, __n, __stream);
+  size_t sz = __glibc_objsize (__s);
+  if (__glibc_safe_or_unknown_len_signed (__n, sizeof (wchar_t), sz))
+    return __fgetws_alias (__s, __n, __stream);
+  if (__glibc_unsafe_len_signed (__n, sizeof (wchar_t), sz))
+    return __fgetws_chk_warn (__s, sz / sizeof (wchar_t), __n, __stream);
+  return __fgetws_chk (__s, sz / sizeof (wchar_t), __n, __stream);
 }
 
 #ifdef __USE_GNU
@@ -424,20 +368,13 @@ extern wchar_t *__REDIRECT (__fgetws_unlocked_chk_warn,
 __fortify_function __wur wchar_t *
 fgetws_unlocked (wchar_t *__restrict __s, int __n, __FILE *__restrict __stream)
 {
-  if (__glibc_objsize (__s) != (size_t) -1)
-    {
-      if (!__builtin_constant_p (__n) || __n <= 0)
-	return __fgetws_unlocked_chk (__s,
-				      __glibc_objsize (__s) / sizeof (wchar_t),
-				      __n, __stream);
-
-      if ((size_t) __n > __glibc_objsize (__s) / sizeof (wchar_t))
-	return __fgetws_unlocked_chk_warn (__s,
-					   (__glibc_objsize (__s)
-					    / sizeof (wchar_t)),
-					   __n, __stream);
-    }
-  return __fgetws_unlocked_alias (__s, __n, __stream);
+  size_t sz = __glibc_objsize (__s);
+  if (__glibc_safe_or_unknown_len_signed (__n, sizeof (wchar_t), sz))
+    return __fgetws_unlocked_alias (__s, __n, __stream);
+  if (__glibc_unsafe_len_signed (__n, sizeof (wchar_t), sz))
+    return __fgetws_unlocked_chk_warn (__s, sz / sizeof (wchar_t), __n,
+				       __stream);
+  return __fgetws_unlocked_chk (__s, sz / sizeof (wchar_t), __n, __stream);
 }
 #endif
 
@@ -488,18 +425,9 @@ __fortify_function size_t
 __NTH (mbsrtowcs (wchar_t *__restrict __dst, const char **__restrict __src,
 		  size_t __len, mbstate_t *__restrict __ps))
 {
-  if (__glibc_objsize (__dst) != (size_t) -1)
-    {
-      if (!__builtin_constant_p (__len))
-	return __mbsrtowcs_chk (__dst, __src, __len, __ps,
-				__glibc_objsize (__dst) / sizeof (wchar_t));
-
-      if (__len > __glibc_objsize (__dst) / sizeof (wchar_t))
-	return __mbsrtowcs_chk_warn (__dst, __src, __len, __ps,
-				     (__glibc_objsize (__dst)
-				      / sizeof (wchar_t)));
-    }
-  return __mbsrtowcs_alias (__dst, __src, __len, __ps);
+  return __glibc_fortify_n (mbsrtowcs, __len, sizeof (wchar_t),
+			    __glibc_objsize (__dst),
+			    __dst, __src, __len, __ps);
 }
 
 
@@ -523,17 +451,9 @@ __fortify_function size_t
 __NTH (wcsrtombs (char *__restrict __dst, const wchar_t **__restrict __src,
 		  size_t __len, mbstate_t *__restrict __ps))
 {
-  if (__glibc_objsize (__dst) != (size_t) -1)
-    {
-      if (!__builtin_constant_p (__len))
-	return __wcsrtombs_chk (__dst, __src, __len, __ps,
-				__glibc_objsize (__dst));
-
-      if (__len > __glibc_objsize (__dst))
-	return __wcsrtombs_chk_warn (__dst, __src, __len, __ps,
-				     __glibc_objsize (__dst));
-    }
-  return __wcsrtombs_alias (__dst, __src, __len, __ps);
+  return __glibc_fortify (wcsrtombs, __len, sizeof (char),
+			  __glibc_objsize (__dst),
+			  __dst, __src, __len, __ps);
 }
 
 
@@ -559,18 +479,9 @@ __fortify_function size_t
 __NTH (mbsnrtowcs (wchar_t *__restrict __dst, const char **__restrict __src,
 		   size_t __nmc, size_t __len, mbstate_t *__restrict __ps))
 {
-  if (__glibc_objsize (__dst) != (size_t) -1)
-    {
-      if (!__builtin_constant_p (__len))
-	return __mbsnrtowcs_chk (__dst, __src, __nmc, __len, __ps,
-				 __glibc_objsize (__dst) / sizeof (wchar_t));
-
-      if (__len > __glibc_objsize (__dst) / sizeof (wchar_t))
-	return __mbsnrtowcs_chk_warn (__dst, __src, __nmc, __len, __ps,
-				      (__glibc_objsize (__dst)
-				       / sizeof (wchar_t)));
-    }
-  return __mbsnrtowcs_alias (__dst, __src, __nmc, __len, __ps);
+  return __glibc_fortify_n (mbsnrtowcs, __len, sizeof (wchar_t),
+			    __glibc_objsize (__dst),
+			    __dst, __src, __nmc, __len, __ps);
 }
 
 
@@ -596,16 +507,8 @@ __fortify_function size_t
 __NTH (wcsnrtombs (char *__restrict __dst, const wchar_t **__restrict __src,
 		   size_t __nwc, size_t __len, mbstate_t *__restrict __ps))
 {
-  if (__glibc_objsize (__dst) != (size_t) -1)
-    {
-      if (!__builtin_constant_p (__len))
-	return __wcsnrtombs_chk (__dst, __src, __nwc, __len, __ps,
-				 __glibc_objsize (__dst));
-
-      if (__len > __glibc_objsize (__dst))
-	return __wcsnrtombs_chk_warn (__dst, __src, __nwc, __len, __ps,
-				      __glibc_objsize (__dst));
-    }
-  return __wcsnrtombs_alias (__dst, __src, __nwc, __len, __ps);
+  return __glibc_fortify (wcsnrtombs, __len, sizeof (char),
+			  __glibc_objsize (__dst),
+			  __dst, __src, __nwc, __len, __ps);
 }
 #endif
-- 
2.31.1


^ permalink raw reply	[flat|nested] 17+ messages in thread

* [PATCH 3/3] debug: Add tests for _FORTIFY_SOURCE=3
  2021-10-12 16:16 [PATCH 0/3] _FORTIFY_SOURCE=3 improvements Siddhesh Poyarekar
  2021-10-12 16:16 ` [PATCH 1/3] Don't add access size hints to fortifiable functions Siddhesh Poyarekar
  2021-10-12 16:16 ` [PATCH 2/3] Make sure that the fortified function conditionals are constant Siddhesh Poyarekar
@ 2021-10-12 16:16 ` Siddhesh Poyarekar
  2021-10-18 13:14 ` [PING][PATCH 0/3] _FORTIFY_SOURCE=3 improvements Siddhesh Poyarekar
  2021-10-20  5:24 ` [PATCH v2 0/2] " Siddhesh Poyarekar
  4 siblings, 0 replies; 17+ messages in thread
From: Siddhesh Poyarekar @ 2021-10-12 16:16 UTC (permalink / raw)
  To: libc-alpha

Add some testing coverage for _FORTIFY_SOURCE=3.

Signed-off-by: Siddhesh Poyarekar <siddhesh@sourceware.org>
---
 debug/Makefile    |  13 ++++--
 debug/tst-chk1.c  | 102 +++++++++++++++++++++++++---------------------
 debug/tst-chk7.c  |   2 +
 debug/tst-chk8.cc |   2 +
 4 files changed, 69 insertions(+), 50 deletions(-)
 create mode 100644 debug/tst-chk7.c
 create mode 100644 debug/tst-chk8.cc

diff --git a/debug/Makefile b/debug/Makefile
index 6893111cbf..357f888246 100644
--- a/debug/Makefile
+++ b/debug/Makefile
@@ -120,6 +120,8 @@ CFLAGS-tst-chk3.c += -Wno-format -Wno-deprecated-declarations -Wno-error
 CFLAGS-tst-chk4.cc += -Wno-format -Wno-deprecated-declarations -Wno-error
 CFLAGS-tst-chk5.cc += -Wno-format -Wno-deprecated-declarations -Wno-error
 CFLAGS-tst-chk6.cc += -Wno-format -Wno-deprecated-declarations -Wno-error
+CFLAGS-tst-chk7.c += -Wno-format -Wno-deprecated-declarations -Wno-error
+CFLAGS-tst-chk8.cc += -Wno-format -Wno-deprecated-declarations -Wno-error
 CFLAGS-tst-lfschk1.c += -Wno-format -Wno-deprecated-declarations -Wno-error
 CFLAGS-tst-lfschk2.c += -Wno-format -Wno-deprecated-declarations -Wno-error
 CFLAGS-tst-lfschk3.c += -Wno-format -Wno-deprecated-declarations -Wno-error
@@ -129,6 +131,7 @@ CFLAGS-tst-lfschk6.cc += -Wno-format -Wno-deprecated-declarations -Wno-error
 LDLIBS-tst-chk4 = -lstdc++
 LDLIBS-tst-chk5 = -lstdc++
 LDLIBS-tst-chk6 = -lstdc++
+LDLIBS-tst-chk8 = -lstdc++
 LDLIBS-tst-lfschk4 = -lstdc++
 LDLIBS-tst-lfschk5 = -lstdc++
 LDLIBS-tst-lfschk6 = -lstdc++
@@ -150,16 +153,16 @@ CFLAGS-tst-ssp-1.c += -fstack-protector-all
 
 tests = backtrace-tst tst-longjmp_chk tst-chk1 tst-chk2 tst-chk3 \
 	tst-lfschk1 tst-lfschk2 tst-lfschk3 test-strcpy_chk test-stpcpy_chk \
-	tst-chk4 tst-chk5 tst-chk6 tst-lfschk4 tst-lfschk5 tst-lfschk6 \
-	tst-longjmp_chk2 tst-backtrace2 tst-backtrace3 tst-backtrace4 \
-	tst-backtrace5 tst-backtrace6
+	tst-chk4 tst-chk5 tst-chk6 tst-chk7 tst-chk8 tst-lfschk4 tst-lfschk5 \
+	tst-lfschk6 tst-longjmp_chk2 tst-backtrace2 tst-backtrace3 \
+	tst-backtrace4 tst-backtrace5 tst-backtrace6
 
 ifeq ($(have-ssp),yes)
 tests += tst-ssp-1
 endif
 
 ifeq (,$(CXX))
-tests-unsupported = tst-chk4 tst-chk5 tst-chk6 \
+tests-unsupported = tst-chk4 tst-chk5 tst-chk6 tst-chk8 \
 		    tst-lfschk4 tst-lfschk5 tst-lfschk6
 endif
 
@@ -193,6 +196,8 @@ $(objpfx)tst-chk3.out: $(gen-locales)
 $(objpfx)tst-chk4.out: $(gen-locales)
 $(objpfx)tst-chk5.out: $(gen-locales)
 $(objpfx)tst-chk6.out: $(gen-locales)
+$(objpfx)tst-chk7.out: $(gen-locales)
+$(objpfx)tst-chk8.out: $(gen-locales)
 $(objpfx)tst-lfschk1.out: $(gen-locales)
 $(objpfx)tst-lfschk2.out: $(gen-locales)
 $(objpfx)tst-lfschk3.out: $(gen-locales)
diff --git a/debug/tst-chk1.c b/debug/tst-chk1.c
index 6712770201..d11e536f87 100644
--- a/debug/tst-chk1.c
+++ b/debug/tst-chk1.c
@@ -82,8 +82,14 @@ handler (int sig)
     _exit (127);
 }
 
+#if __USE_FORTIFY_LEVEL == 3
+volatile size_t buf_size = 10;
+#else
 char buf[10];
 wchar_t wbuf[10];
+#define buf_size sizeof (buf)
+#endif
+
 volatile size_t l0;
 volatile char *p;
 volatile wchar_t *wp;
@@ -122,6 +128,10 @@ int num2 = 987654;
 static int
 do_test (void)
 {
+#if __USE_FORTIFY_LEVEL == 3
+  char *buf = (char *) malloc (buf_size);
+  wchar_t *wbuf = (wchar_t *) malloc (buf_size * sizeof (wchar_t));
+#endif
   set_fortify_handler (handler);
 
   struct A { char buf1[9]; char buf2[1]; } a;
@@ -946,93 +956,93 @@ do_test (void)
 
   rewind (stdin);
 
-  if (fgets (buf, sizeof (buf), stdin) != buf
+  if (fgets (buf, buf_size, stdin) != buf
       || memcmp (buf, "abcdefgh\n", 10))
     FAIL ();
-  if (fgets (buf, sizeof (buf), stdin) != buf || memcmp (buf, "ABCDEFGHI", 10))
+  if (fgets (buf, buf_size, stdin) != buf || memcmp (buf, "ABCDEFGHI", 10))
     FAIL ();
 
   rewind (stdin);
 
-  if (fgets (buf, l0 + sizeof (buf), stdin) != buf
+  if (fgets (buf, l0 + buf_size, stdin) != buf
       || memcmp (buf, "abcdefgh\n", 10))
     FAIL ();
 
 #if __USE_FORTIFY_LEVEL >= 1
   CHK_FAIL_START
-  if (fgets (buf, sizeof (buf) + 1, stdin) != buf)
+  if (fgets (buf, buf_size + 1, stdin) != buf)
     FAIL ();
   CHK_FAIL_END
 
   CHK_FAIL_START
-  if (fgets (buf, l0 + sizeof (buf) + 1, stdin) != buf)
+  if (fgets (buf, l0 + buf_size + 1, stdin) != buf)
     FAIL ();
   CHK_FAIL_END
 #endif
 
   rewind (stdin);
 
-  if (fgets_unlocked (buf, sizeof (buf), stdin) != buf
+  if (fgets_unlocked (buf, buf_size, stdin) != buf
       || memcmp (buf, "abcdefgh\n", 10))
     FAIL ();
-  if (fgets_unlocked (buf, sizeof (buf), stdin) != buf
+  if (fgets_unlocked (buf, buf_size, stdin) != buf
       || memcmp (buf, "ABCDEFGHI", 10))
     FAIL ();
 
   rewind (stdin);
 
-  if (fgets_unlocked (buf, l0 + sizeof (buf), stdin) != buf
+  if (fgets_unlocked (buf, l0 + buf_size, stdin) != buf
       || memcmp (buf, "abcdefgh\n", 10))
     FAIL ();
 
 #if __USE_FORTIFY_LEVEL >= 1
   CHK_FAIL_START
-  if (fgets_unlocked (buf, sizeof (buf) + 1, stdin) != buf)
+  if (fgets_unlocked (buf, buf_size + 1, stdin) != buf)
     FAIL ();
   CHK_FAIL_END
 
   CHK_FAIL_START
-  if (fgets_unlocked (buf, l0 + sizeof (buf) + 1, stdin) != buf)
+  if (fgets_unlocked (buf, l0 + buf_size + 1, stdin) != buf)
     FAIL ();
   CHK_FAIL_END
 #endif
 
   rewind (stdin);
 
-  if (fread (buf, 1, sizeof (buf), stdin) != sizeof (buf)
+  if (fread (buf, 1, buf_size, stdin) != buf_size
       || memcmp (buf, "abcdefgh\nA", 10))
     FAIL ();
-  if (fread (buf, sizeof (buf), 1, stdin) != 1
+  if (fread (buf, buf_size, 1, stdin) != 1
       || memcmp (buf, "BCDEFGHI\na", 10))
     FAIL ();
 
   rewind (stdin);
 
-  if (fread (buf, l0 + 1, sizeof (buf), stdin) != sizeof (buf)
+  if (fread (buf, l0 + 1, buf_size, stdin) != buf_size
       || memcmp (buf, "abcdefgh\nA", 10))
     FAIL ();
-  if (fread (buf, sizeof (buf), l0 + 1, stdin) != 1
+  if (fread (buf, buf_size, l0 + 1, stdin) != 1
       || memcmp (buf, "BCDEFGHI\na", 10))
     FAIL ();
 
 #if __USE_FORTIFY_LEVEL >= 1
   CHK_FAIL_START
-  if (fread (buf, 1, sizeof (buf) + 1, stdin) != sizeof (buf) + 1)
+  if (fread (buf, 1, buf_size + 1, stdin) != buf_size + 1)
     FAIL ();
   CHK_FAIL_END
 
   CHK_FAIL_START
-  if (fread (buf, sizeof (buf) + 1, l0 + 1, stdin) != 1)
+  if (fread (buf, buf_size + 1, l0 + 1, stdin) != 1)
     FAIL ();
   CHK_FAIL_END
 #endif
 
   rewind (stdin);
 
-  if (fread_unlocked (buf, 1, sizeof (buf), stdin) != sizeof (buf)
+  if (fread_unlocked (buf, 1, buf_size, stdin) != buf_size
       || memcmp (buf, "abcdefgh\nA", 10))
     FAIL ();
-  if (fread_unlocked (buf, sizeof (buf), 1, stdin) != 1
+  if (fread_unlocked (buf, buf_size, 1, stdin) != 1
       || memcmp (buf, "BCDEFGHI\na", 10))
     FAIL ();
 
@@ -1047,100 +1057,100 @@ do_test (void)
 
   rewind (stdin);
 
-  if (fread_unlocked (buf, l0 + 1, sizeof (buf), stdin) != sizeof (buf)
+  if (fread_unlocked (buf, l0 + 1, buf_size, stdin) != buf_size
       || memcmp (buf, "abcdefgh\nA", 10))
     FAIL ();
-  if (fread_unlocked (buf, sizeof (buf), l0 + 1, stdin) != 1
+  if (fread_unlocked (buf, buf_size, l0 + 1, stdin) != 1
       || memcmp (buf, "BCDEFGHI\na", 10))
     FAIL ();
 
 #if __USE_FORTIFY_LEVEL >= 1
   CHK_FAIL_START
-  if (fread_unlocked (buf, 1, sizeof (buf) + 1, stdin) != sizeof (buf) + 1)
+  if (fread_unlocked (buf, 1, buf_size + 1, stdin) != buf_size + 1)
     FAIL ();
   CHK_FAIL_END
 
   CHK_FAIL_START
-  if (fread_unlocked (buf, sizeof (buf) + 1, l0 + 1, stdin) != 1)
+  if (fread_unlocked (buf, buf_size + 1, l0 + 1, stdin) != 1)
     FAIL ();
   CHK_FAIL_END
 #endif
 
   lseek (fileno (stdin), 0, SEEK_SET);
 
-  if (read (fileno (stdin), buf, sizeof (buf) - 1) != sizeof (buf) - 1
+  if (read (fileno (stdin), buf, buf_size - 1) != buf_size - 1
       || memcmp (buf, "abcdefgh\n", 9))
     FAIL ();
-  if (read (fileno (stdin), buf, sizeof (buf) - 1) != sizeof (buf) - 1
+  if (read (fileno (stdin), buf, buf_size - 1) != buf_size - 1
       || memcmp (buf, "ABCDEFGHI", 9))
     FAIL ();
 
   lseek (fileno (stdin), 0, SEEK_SET);
 
-  if (read (fileno (stdin), buf, l0 + sizeof (buf) - 1) != sizeof (buf) - 1
+  if (read (fileno (stdin), buf, l0 + buf_size - 1) != buf_size - 1
       || memcmp (buf, "abcdefgh\n", 9))
     FAIL ();
 
 #if __USE_FORTIFY_LEVEL >= 1
   CHK_FAIL_START
-  if (read (fileno (stdin), buf, sizeof (buf) + 1) != sizeof (buf) + 1)
+  if (read (fileno (stdin), buf, buf_size + 1) != buf_size + 1)
     FAIL ();
   CHK_FAIL_END
 
   CHK_FAIL_START
-  if (read (fileno (stdin), buf, l0 + sizeof (buf) + 1) != sizeof (buf) + 1)
+  if (read (fileno (stdin), buf, l0 + buf_size + 1) != buf_size + 1)
     FAIL ();
   CHK_FAIL_END
 #endif
 
-  if (pread (fileno (stdin), buf, sizeof (buf) - 1, sizeof (buf) - 2)
-      != sizeof (buf) - 1
+  if (pread (fileno (stdin), buf, buf_size - 1, buf_size - 2)
+      != buf_size - 1
       || memcmp (buf, "\nABCDEFGH", 9))
     FAIL ();
-  if (pread (fileno (stdin), buf, sizeof (buf) - 1, 0) != sizeof (buf) - 1
+  if (pread (fileno (stdin), buf, buf_size - 1, 0) != buf_size - 1
       || memcmp (buf, "abcdefgh\n", 9))
     FAIL ();
-  if (pread (fileno (stdin), buf, l0 + sizeof (buf) - 1, sizeof (buf) - 3)
-      != sizeof (buf) - 1
+  if (pread (fileno (stdin), buf, l0 + buf_size - 1, buf_size - 3)
+      != buf_size - 1
       || memcmp (buf, "h\nABCDEFG", 9))
     FAIL ();
 
 #if __USE_FORTIFY_LEVEL >= 1
   CHK_FAIL_START
-  if (pread (fileno (stdin), buf, sizeof (buf) + 1, 2 * sizeof (buf))
-      != sizeof (buf) + 1)
+  if (pread (fileno (stdin), buf, buf_size + 1, 2 * buf_size)
+      != buf_size + 1)
     FAIL ();
   CHK_FAIL_END
 
   CHK_FAIL_START
-  if (pread (fileno (stdin), buf, l0 + sizeof (buf) + 1, 2 * sizeof (buf))
-      != sizeof (buf) + 1)
+  if (pread (fileno (stdin), buf, l0 + buf_size + 1, 2 * buf_size)
+      != buf_size + 1)
     FAIL ();
   CHK_FAIL_END
 #endif
 
-  if (pread64 (fileno (stdin), buf, sizeof (buf) - 1, sizeof (buf) - 2)
-      != sizeof (buf) - 1
+  if (pread64 (fileno (stdin), buf, buf_size - 1, buf_size - 2)
+      != buf_size - 1
       || memcmp (buf, "\nABCDEFGH", 9))
     FAIL ();
-  if (pread64 (fileno (stdin), buf, sizeof (buf) - 1, 0) != sizeof (buf) - 1
+  if (pread64 (fileno (stdin), buf, buf_size - 1, 0) != buf_size - 1
       || memcmp (buf, "abcdefgh\n", 9))
     FAIL ();
-  if (pread64 (fileno (stdin), buf, l0 + sizeof (buf) - 1, sizeof (buf) - 3)
-      != sizeof (buf) - 1
+  if (pread64 (fileno (stdin), buf, l0 + buf_size - 1, buf_size - 3)
+      != buf_size - 1
       || memcmp (buf, "h\nABCDEFG", 9))
     FAIL ();
 
 #if __USE_FORTIFY_LEVEL >= 1
   CHK_FAIL_START
-  if (pread64 (fileno (stdin), buf, sizeof (buf) + 1, 2 * sizeof (buf))
-      != sizeof (buf) + 1)
+  if (pread64 (fileno (stdin), buf, buf_size + 1, 2 * buf_size)
+      != buf_size + 1)
     FAIL ();
   CHK_FAIL_END
 
   CHK_FAIL_START
-  if (pread64 (fileno (stdin), buf, l0 + sizeof (buf) + 1, 2 * sizeof (buf))
-      != sizeof (buf) + 1)
+  if (pread64 (fileno (stdin), buf, l0 + buf_size + 1, 2 * buf_size)
+      != buf_size + 1)
     FAIL ();
   CHK_FAIL_END
 #endif
@@ -1178,7 +1188,7 @@ do_test (void)
   CHK_FAIL2_END
 
   CHK_FAIL2_START
-  snprintf (buf, sizeof (buf), "%3$d\n", 1, 2, 3, 4);
+  snprintf (buf, buf_size, "%3$d\n", 1, 2, 3, 4);
   CHK_FAIL2_END
 
   int sp[2];
diff --git a/debug/tst-chk7.c b/debug/tst-chk7.c
new file mode 100644
index 0000000000..2a7b323812
--- /dev/null
+++ b/debug/tst-chk7.c
@@ -0,0 +1,2 @@
+#define _FORTIFY_SOURCE 3
+#include "tst-chk1.c"
diff --git a/debug/tst-chk8.cc b/debug/tst-chk8.cc
new file mode 100644
index 0000000000..2a7b323812
--- /dev/null
+++ b/debug/tst-chk8.cc
@@ -0,0 +1,2 @@
+#define _FORTIFY_SOURCE 3
+#include "tst-chk1.c"
-- 
2.31.1


^ permalink raw reply	[flat|nested] 17+ messages in thread

* [PING][PATCH 0/3] _FORTIFY_SOURCE=3 improvements
  2021-10-12 16:16 [PATCH 0/3] _FORTIFY_SOURCE=3 improvements Siddhesh Poyarekar
                   ` (2 preceding siblings ...)
  2021-10-12 16:16 ` [PATCH 3/3] debug: Add tests for _FORTIFY_SOURCE=3 Siddhesh Poyarekar
@ 2021-10-18 13:14 ` Siddhesh Poyarekar
  2021-10-20  5:24 ` [PATCH v2 0/2] " Siddhesh Poyarekar
  4 siblings, 0 replies; 17+ messages in thread
From: Siddhesh Poyarekar @ 2021-10-18 13:14 UTC (permalink / raw)
  To: libc-alpha

Ping!

On 10/12/21 21:46, Siddhesh Poyarekar via Libc-alpha wrote:
> This patchset changes the layout of fortified functions to make them
> _FORTIFY_SOURCE=3 friendly and at the same time, continue working the
> same for _FORTIFY_SOURCE=2 and lower.  At a high level it makes sure
> that no branches are emitted at compile time and only one of either the
> regular or _chk version of the function is called.  The conditions are
> also reworked to make them more readable and foldable even in cases
> where the compiler doesn't know the exact values of the operation length
> and size, but can make decisions based on ranges of their values.
> 
> The changeset also adds some _FORTIFY_SOURCE=3 testing coverage on
> compilers that are able to set that fortification level.
> 
> Siddhesh Poyarekar (3):
>    Don't add access size hints to fortifiable functions
>    Make sure that the fortified function conditionals are constant
>    debug: Add tests for _FORTIFY_SOURCE=3
> 
>   debug/Makefile                 |  13 +-
>   debug/tst-chk1.c               | 102 ++++++++-------
>   debug/tst-chk7.c               |   2 +
>   debug/tst-chk8.cc              |   2 +
>   io/bits/poll2.h                |  31 ++---
>   io/sys/poll.h                  |   6 +-
>   libio/bits/stdio2.h            | 110 +++++++----------
>   libio/stdio.h                  |   4 +-
>   misc/sys/cdefs.h               |  71 +++++++++++
>   posix/bits/unistd.h            | 174 ++++++--------------------
>   posix/unistd.h                 |  28 +++--
>   socket/bits/socket2.h          |  34 ++---
>   stdlib/bits/stdlib.h           |  57 +++------
>   stdlib/stdlib.h                |   5 +-
>   string/bits/string_fortified.h |   5 +-
>   string/string.h                |   2 +-
>   wcsmbs/bits/wchar2.h           | 219 +++++++++------------------------
>   17 files changed, 351 insertions(+), 514 deletions(-)
>   create mode 100644 debug/tst-chk7.c
>   create mode 100644 debug/tst-chk8.cc
> 


^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: [PATCH 1/3] Don't add access size hints to fortifiable functions
  2021-10-12 16:16 ` [PATCH 1/3] Don't add access size hints to fortifiable functions Siddhesh Poyarekar
@ 2021-10-19 17:54   ` Adhemerval Zanella
  2021-10-19 18:19     ` Siddhesh Poyarekar
  0 siblings, 1 reply; 17+ messages in thread
From: Adhemerval Zanella @ 2021-10-19 17:54 UTC (permalink / raw)
  To: Siddhesh Poyarekar, libc-alpha



On 12/10/2021 13:16, Siddhesh Poyarekar via Libc-alpha wrote:
> In the context of a function definition, the size hints imply that the
> size of an object pointed to by one parameter is another parameter.
> This doesn't make sense for the fortified versions of the functions
> since that's the bit it's trying to validate.
> 
> This is harmless with __builtin_object_size since it has fairly simple
> semantics when it comes to objects passed as function parameters.
> With __builtin_dynamic_object_size we could (as my patchset for gcc[1]
> already does) use the access attribute to determine the object size in
> the general case but it misleads the fortified functions.
> 
> Disable the access attribute for fortified function inline functions
> when building at _FORTIFY_SOURCE=3 to make this work better.  The

By 'work better' do you mean better code generation or better diagnostics
from gcc?

> access attributes remain for the _chk variants since they can be used
> by the compiler to warn when the caller is passing invalid arguments.
> 
> Signed-off-by: Siddhesh Poyarekar <siddhesh@sourceware.org>

The change looks ok, but what about other function that use
__attr_access ((__write_only__, ...). Should they be adapated as well?

Reviewed-by: Adhemerval Zanella  <adhemerval.zanella@linaro.org>

> ---
>  io/bits/poll2.h                |  4 ++--
>  io/sys/poll.h                  |  6 +++---
>  libio/bits/stdio2.h            |  4 ++--
>  libio/stdio.h                  |  4 ++--
>  misc/sys/cdefs.h               | 10 ++++++++++
>  posix/unistd.h                 | 28 ++++++++++++++++------------
>  stdlib/stdlib.h                |  5 +++--
>  string/bits/string_fortified.h |  5 +++--
>  string/string.h                |  2 +-
>  9 files changed, 42 insertions(+), 26 deletions(-)
> 
> diff --git a/io/bits/poll2.h b/io/bits/poll2.h
> index a623678c09..be74d020f2 100644
> --- a/io/bits/poll2.h
> +++ b/io/bits/poll2.h
> @@ -33,7 +33,7 @@ extern int __REDIRECT (__poll_chk_warn, (struct pollfd *__fds, nfds_t __nfds,
>  		       __poll_chk)
>    __warnattr ("poll called with fds buffer too small file nfds entries");
>  
> -__fortify_function __attr_access ((__write_only__, 1, 2)) int
> +__fortify_function __fortified_attr_access (__write_only__, 1, 2) int
>  poll (struct pollfd *__fds, nfds_t __nfds, int __timeout)
>  {
>    if (__glibc_objsize (__fds) != (__SIZE_TYPE__) -1)
> @@ -64,7 +64,7 @@ extern int __REDIRECT (__ppoll_chk_warn, (struct pollfd *__fds, nfds_t __nfds,
>  		       __ppoll_chk)
>    __warnattr ("ppoll called with fds buffer too small file nfds entries");
>  
> -__fortify_function __attr_access ((__write_only__, 1, 2)) int
> +__fortify_function __fortified_attr_access (__write_only__, 1, 2) int
>  ppoll (struct pollfd *__fds, nfds_t __nfds, const struct timespec *__timeout,
>         const __sigset_t *__ss)
>  {

Ok.

> diff --git a/io/sys/poll.h b/io/sys/poll.h
> index e640efb2bc..751c7f5f72 100644
> --- a/io/sys/poll.h
> +++ b/io/sys/poll.h
> @@ -52,7 +52,7 @@ __BEGIN_DECLS
>     This function is a cancellation point and therefore not marked with
>     __THROW.  */
>  extern int poll (struct pollfd *__fds, nfds_t __nfds, int __timeout)
> -    __attr_access ((__write_only__, 1, 2));
> +    __fortified_attr_access (__write_only__, 1, 2);
>  
>  #ifdef __USE_GNU
>  /* Like poll, but before waiting the threads signal mask is replaced
> @@ -64,7 +64,7 @@ extern int poll (struct pollfd *__fds, nfds_t __nfds, int __timeout)
>  extern int ppoll (struct pollfd *__fds, nfds_t __nfds,
>  		  const struct timespec *__timeout,
>  		  const __sigset_t *__ss)
> -    __attr_access ((__write_only__, 1, 2));
> +    __fortified_attr_access (__write_only__, 1, 2);
>  
>  # ifdef __USE_TIME_BITS64
>  #  ifdef __REDIRECT
> @@ -72,7 +72,7 @@ extern int __REDIRECT (ppoll, (struct pollfd *__fds, nfds_t __nfds,
>                                 const struct timespec *__timeout,
>                                 const __sigset_t *__ss),
>                         __ppoll64)
> -    __attr_access ((__write_only__, 1, 2));
> +    __fortified_attr_access (__write_only__, 1, 2);
>  #  else
>  #  define ppoll __ppoll64
>  #  endif

Ok.

> diff --git a/libio/bits/stdio2.h b/libio/bits/stdio2.h
> index 3f0cab1254..4f016a5638 100644
> --- a/libio/bits/stdio2.h
> +++ b/libio/bits/stdio2.h
> @@ -258,7 +258,7 @@ extern char *__REDIRECT (__fgets_chk_warn,
>       __wur __warnattr ("fgets called with bigger size than length "
>  		       "of destination buffer");
>  
> -__fortify_function __wur __attr_access ((__write_only__, 1, 2)) char *
> +__fortify_function __wur __fortified_attr_access (__write_only__, 1, 2) char *
>  fgets (char *__restrict __s, int __n, FILE *__restrict __stream)
>  {
>    if (__glibc_objsize (__s) != (size_t) -1)
> @@ -320,7 +320,7 @@ extern char *__REDIRECT (__fgets_unlocked_chk_warn,
>       __wur __warnattr ("fgets_unlocked called with bigger size than length "
>  		       "of destination buffer");
>  
> -__fortify_function __wur __attr_access ((__write_only__, 1, 2)) char *
> +__fortify_function __wur __fortified_attr_access (__write_only__, 1, 2) char *
>  fgets_unlocked (char *__restrict __s, int __n, FILE *__restrict __stream)
>  {
>    if (__glibc_objsize (__s) != (size_t) -1)

Ok.

Don't we need to change __sprintf_chk, __vsprintf_chk, __snprintf_chk as well?

> diff --git a/libio/stdio.h b/libio/stdio.h
> index 0a5c76b552..f208f5ef79 100644
> --- a/libio/stdio.h
> +++ b/libio/stdio.h
> @@ -590,7 +590,7 @@ extern int putw (int __w, FILE *__stream);
>     This function is a possible cancellation point and therefore not
>     marked with __THROW.  */
>  extern char *fgets (char *__restrict __s, int __n, FILE *__restrict __stream)
> -     __wur __attr_access ((__write_only__, 1, 2));
> +     __wur __fortified_attr_access (__write_only__, 1, 2);
>  
>  #if __GLIBC_USE (DEPRECATED_GETS)
>  /* Get a newline-terminated string from stdin, removing the newline.
> @@ -614,7 +614,7 @@ extern char *gets (char *__s) __wur __attribute_deprecated__;
>     therefore not marked with __THROW.  */
>  extern char *fgets_unlocked (char *__restrict __s, int __n,
>  			     FILE *__restrict __stream) __wur
> -    __attr_access ((__write_only__, 1, 2));
> +    __fortified_attr_access (__write_only__, 1, 2);
>  #endif
>  
>  

Ok.

> diff --git a/misc/sys/cdefs.h b/misc/sys/cdefs.h
> index c8e10aae17..d08c2adfd0 100644
> --- a/misc/sys/cdefs.h
> +++ b/misc/sys/cdefs.h
> @@ -606,12 +606,22 @@ _Static_assert (0, "IEEE 128-bits long double requires redirection on this platf
>     size-index is not provided:
>       access (access-mode, <ref-index> [, <size-index>])  */
>  #  define __attr_access(x) __attribute__ ((__access__ x))
> +/* For _FORTIFY_SOURCE == 3 we use __builtin_dynamic_object_size, which may
> +   use the access attribute to get object sizes from function definition
> +   arguments, so we can't use them on functions we fortify.  Drop the object
> +   size hints for such functions.  */
> +#  if __USE_FORTIFY_LEVEL == 3
> +#    define __fortified_attr_access(a, o, s) __attribute__ ((__access__ (a, o)))
> +#  else
> +#    define __fortified_attr_access(a, o, s) __attr_access ((a, o, s))
> +#  endif
>  #  if __GNUC_PREREQ (11, 0)
>  #    define __attr_access_none(argno) __attribute__ ((__access__ (__none__, argno)))
>  #  else
>  #    define __attr_access_none(argno)
>  #  endif
>  #else
> +#  define __fortified_attr_access(a, o, s)
>  #  define __attr_access(x)
>  #  define __attr_access_none(argno)
>  #endif

Ok, it removes the 'size-index'.

> diff --git a/posix/unistd.h b/posix/unistd.h
> index 8224c5fbc9..7a61ff5e86 100644
> --- a/posix/unistd.h
> +++ b/posix/unistd.h
> @@ -369,7 +369,7 @@ extern void closefrom (int __lowfd) __THROW;
>     This function is a cancellation point and therefore not marked with
>     __THROW.  */
>  extern ssize_t read (int __fd, void *__buf, size_t __nbytes) __wur
> -    __attr_access ((__write_only__, 2, 3));
> +    __fortified_attr_access (__write_only__, 2, 3);
>  
>  /* Write N bytes of BUF to FD.  Return the number written, or -1.
>  
> @@ -388,7 +388,7 @@ extern ssize_t write (int __fd, const void *__buf, size_t __n) __wur
>     __THROW.  */
>  extern ssize_t pread (int __fd, void *__buf, size_t __nbytes,
>  		      __off_t __offset) __wur
> -    __attr_access ((__write_only__, 2, 3));
> +    __fortified_attr_access (__write_only__, 2, 3);
>  
>  /* Write N bytes of BUF to FD at the given position OFFSET without
>     changing the file pointer.  Return the number written, or -1.
> @@ -404,7 +404,7 @@ extern ssize_t pwrite (int __fd, const void *__buf, size_t __n,
>  extern ssize_t __REDIRECT (pread, (int __fd, void *__buf, size_t __nbytes,
>  				   __off64_t __offset),
>  			   pread64) __wur
> -    __attr_access ((__write_only__, 2, 3));
> +    __fortified_attr_access (__write_only__, 2, 3);
>  extern ssize_t __REDIRECT (pwrite, (int __fd, const void *__buf,
>  				    size_t __nbytes, __off64_t __offset),
>  			   pwrite64) __wur
> @@ -421,7 +421,7 @@ extern ssize_t __REDIRECT (pwrite, (int __fd, const void *__buf,
>     or 0 for EOF.  */
>  extern ssize_t pread64 (int __fd, void *__buf, size_t __nbytes,
>  			__off64_t __offset) __wur
> -    __attr_access ((__write_only__, 2, 3));
> +    __fortified_attr_access (__write_only__, 2, 3);
>  /* Write N bytes of BUF to FD at the given position OFFSET without
>     changing the file pointer.  Return the number written, or -1.  */
>  extern ssize_t pwrite64 (int __fd, const void *__buf, size_t __n,
> @@ -642,7 +642,7 @@ extern long int sysconf (int __name) __THROW;
>  #ifdef	__USE_POSIX2
>  /* Get the value of the string-valued system variable NAME.  */
>  extern size_t confstr (int __name, char *__buf, size_t __len) __THROW
> -    __attr_access ((__write_only__, 2, 3));
> +    __fortified_attr_access (__write_only__, 2, 3);
>  #endif
>  
>  
> @@ -709,7 +709,7 @@ extern __gid_t getegid (void) __THROW;
>     the calling process is in.  Otherwise, fill in the group IDs
>     of its supplementary groups in LIST and return the number written.  */
>  extern int getgroups (int __size, __gid_t __list[]) __THROW __wur
> -    __attr_access ((__write_only__, 2, 1));
> +    __fortified_attr_access (__write_only__, 2, 1);
>  #ifdef	__USE_GNU
>  /* Return nonzero iff the calling process is in group GID.  */
>  extern int group_member (__gid_t __gid) __THROW;
> @@ -801,7 +801,8 @@ extern char *ttyname (int __fd) __THROW;
>  /* Store at most BUFLEN characters of the pathname of the terminal FD is
>     open on in BUF.  Return 0 on success, otherwise an error number.  */
>  extern int ttyname_r (int __fd, char *__buf, size_t __buflen)
> -     __THROW __nonnull ((2)) __wur __attr_access ((__write_only__, 2, 3));
> +     __THROW __nonnull ((2)) __wur
> +     __fortified_attr_access (__write_only__, 2, 3);
>  
>  /* Return 1 if FD is a valid descriptor associated
>     with a terminal, zero if not.  */
> @@ -836,7 +837,8 @@ extern int symlink (const char *__from, const char *__to)
>     Returns the number of characters read, or -1 for errors.  */
>  extern ssize_t readlink (const char *__restrict __path,
>  			 char *__restrict __buf, size_t __len)
> -     __THROW __nonnull ((1, 2)) __wur __attr_access ((__write_only__, 2, 3));
> +     __THROW __nonnull ((1, 2)) __wur
> +     __fortified_attr_access (__write_only__, 2, 3);
>  
>  #endif /* Use POSIX.1-2001.  */
>  
> @@ -848,7 +850,8 @@ extern int symlinkat (const char *__from, int __tofd,
>  /* Like readlink but a relative PATH is interpreted relative to FD.  */
>  extern ssize_t readlinkat (int __fd, const char *__restrict __path,
>  			   char *__restrict __buf, size_t __len)
> -     __THROW __nonnull ((2, 3)) __wur __attr_access ((__write_only__, 3, 4));
> +     __THROW __nonnull ((2, 3)) __wur
> +     __fortified_attr_access (__write_only__, 3, 4);
>  #endif
>  
>  /* Remove the link NAME.  */
> @@ -884,7 +887,7 @@ extern char *getlogin (void);
>     This function is a possible cancellation point and therefore not
>     marked with __THROW.  */
>  extern int getlogin_r (char *__name, size_t __name_len) __nonnull ((1))
> -    __attr_access ((__write_only__, 1, 2));
> +    __fortified_attr_access (__write_only__, 1, 2);
>  #endif
>  
>  #ifdef	__USE_MISC
> @@ -906,7 +909,7 @@ extern int setlogin (const char *__name) __THROW __nonnull ((1));
>     The result is null-terminated if LEN is large enough for the full
>     name and the terminator.  */
>  extern int gethostname (char *__name, size_t __len) __THROW __nonnull ((1))
> -    __attr_access ((__write_only__, 1, 2));
> +    __fortified_attr_access (__write_only__, 1, 2);
>  #endif
>  
>  
> @@ -925,7 +928,8 @@ extern int sethostid (long int __id) __THROW __wur;
>     Called just like `gethostname' and `sethostname'.
>     The NIS domain name is usually the empty string when not using NIS.  */
>  extern int getdomainname (char *__name, size_t __len)
> -     __THROW __nonnull ((1)) __wur __attr_access ((__write_only__, 1, 2));
> +     __THROW __nonnull ((1)) __wur
> +     __fortified_attr_access (__write_only__, 1, 2);
>  extern int setdomainname (const char *__name, size_t __len)
>       __THROW __nonnull ((1)) __wur __attr_access ((__read_only__, 1, 2));
>  
> diff --git a/stdlib/stdlib.h b/stdlib/stdlib.h
> index 0481c12355..74c00eee73 100644
> --- a/stdlib/stdlib.h
> +++ b/stdlib/stdlib.h
> @@ -943,7 +943,8 @@ extern size_t mbstowcs (wchar_t *__restrict  __pwcs,
>  extern size_t wcstombs (char *__restrict __s,
>  			const wchar_t *__restrict __pwcs, size_t __n)
>       __THROW
> -  __attr_access ((__write_only__, 1, 3)) __attr_access ((__read_only__, 2));
> +  __fortified_attr_access (__write_only__, 1, 3)
> +  __attr_access ((__read_only__, 2));
>  
>  #ifdef __USE_MISC
>  /* Determine whether the string value of RESPONSE matches the affirmation
> @@ -997,7 +998,7 @@ extern char *ptsname (int __fd) __THROW __wur;
>     terminal associated with the master FD is open on in BUF.
>     Return 0 on success, otherwise an error number.  */
>  extern int ptsname_r (int __fd, char *__buf, size_t __buflen)
> -     __THROW __nonnull ((2)) __attr_access ((__write_only__, 2, 3));
> +     __THROW __nonnull ((2)) __fortified_attr_access (__write_only__, 2, 3);
>  
>  /* Open a master pseudo terminal and return its file descriptor.  */
>  extern int getpt (void);
> diff --git a/string/bits/string_fortified.h b/string/bits/string_fortified.h
> index 67ae2c6b50..5731274848 100644
> --- a/string/bits/string_fortified.h
> +++ b/string/bits/string_fortified.h
> @@ -64,7 +64,7 @@ __NTH (memset (void *__dest, int __ch, size_t __len))
>  # include <bits/strings_fortified.h>
>  
>  void __explicit_bzero_chk (void *__dest, size_t __len, size_t __destlen)
> -  __THROW __nonnull ((1)) __attr_access ((__write_only__, 1, 2));
> +  __THROW __nonnull ((1)) __fortified_attr_access (__write_only__, 1, 2);
>  
>  __fortify_function void
>  __NTH (explicit_bzero (void *__dest, size_t __len))
> @@ -106,7 +106,8 @@ __NTH (stpncpy (char *__dest, const char *__src, size_t __n))
>  #else
>  extern char *__stpncpy_chk (char *__dest, const char *__src, size_t __n,
>  			    size_t __destlen) __THROW
> -  __attr_access ((__write_only__, 1, 3)) __attr_access ((__read_only__, 2));
> +  __fortified_attr_access ((__write_only__, 1, 3))
> +  __attr_access ((__read_only__, 2));
>  extern char *__REDIRECT_NTH (__stpncpy_alias, (char *__dest, const char *__src,
>  					       size_t __n), stpncpy);
>  

Ok.

> diff --git a/string/string.h b/string/string.h
> index 04e1b7067d..8dcafb4ac4 100644
> --- a/string/string.h
> +++ b/string/string.h
> @@ -448,7 +448,7 @@ extern char *strerror_l (int __errnum, locale_t __l) __THROW;
>  /* Set N bytes of S to 0.  The compiler will not delete a call to this
>     function, even if S is dead after the call.  */
>  extern void explicit_bzero (void *__s, size_t __n) __THROW __nonnull ((1))
> -    __attr_access ((__write_only__, 1, 2));
> +    __fortified_attr_access (__write_only__, 1, 2);
>  
>  /* Return the next DELIM-delimited token from *STRINGP,
>     terminating it with a '\0', and update *STRINGP to point past it.  */
> 

Ok.

^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: [PATCH 1/3] Don't add access size hints to fortifiable functions
  2021-10-19 17:54   ` Adhemerval Zanella
@ 2021-10-19 18:19     ` Siddhesh Poyarekar
  2021-10-19 18:24       ` Adhemerval Zanella
  0 siblings, 1 reply; 17+ messages in thread
From: Siddhesh Poyarekar @ 2021-10-19 18:19 UTC (permalink / raw)
  To: Adhemerval Zanella, libc-alpha

On 10/19/21 23:24, Adhemerval Zanella wrote:
> 
> 
> On 12/10/2021 13:16, Siddhesh Poyarekar via Libc-alpha wrote:
>> In the context of a function definition, the size hints imply that the
>> size of an object pointed to by one parameter is another parameter.
>> This doesn't make sense for the fortified versions of the functions
>> since that's the bit it's trying to validate.
>>
>> This is harmless with __builtin_object_size since it has fairly simple
>> semantics when it comes to objects passed as function parameters.
>> With __builtin_dynamic_object_size we could (as my patchset for gcc[1]
>> already does) use the access attribute to determine the object size in
>> the general case but it misleads the fortified functions.
>>
>> Disable the access attribute for fortified function inline functions
>> when building at _FORTIFY_SOURCE=3 to make this work better.  The
> 
> By 'work better' do you mean better code generation or better diagnostics
> from gcc?

Basically the problem occurs when access attributes are present on 
regular functions that have inline fortified definitions to generate 
_chk variants; the attributes get inherited by these definitions, 
causing problems when analyzing them.  For example with poll(fds, nfds, 
timeout), nfds is hinted using the __attr_access as being the size of fds.

Now, when analyzing the inline function definition in bits/poll2.h, the 
compiler sees that nfds is the size of fds and tries to use that 
information in the function body.  In _FORTIFY_SOURCE=3 case, where the 
object size could be a non-constant expression, this information results 
in the conclusion that nfds is the size of fds, which defeats the 
purpose of the implementation because we're trying to check here if nfds 
does indeed represent the size of fds.  Hence for this case, it is best 
to not have the access attribute.

With the attributes gone, the expression evaluation should get delayed 
until the function is actually inlined into its destinations.

>> access attributes remain for the _chk variants since they can be used
>> by the compiler to warn when the caller is passing invalid arguments.
>>
>> Signed-off-by: Siddhesh Poyarekar <siddhesh@sourceware.org>
> 
> The change looks ok, but what about other function that use
> __attr_access ((__write_only__, ...). Should they be adapated as well?

Only functions that have a fortified implementation need to be changed, 
which is basically all of the functions below.

> Reviewed-by: Adhemerval Zanella  <adhemerval.zanella@linaro.org>
> 

Thanks,
Siddhesh

^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: [PATCH 1/3] Don't add access size hints to fortifiable functions
  2021-10-19 18:19     ` Siddhesh Poyarekar
@ 2021-10-19 18:24       ` Adhemerval Zanella
  2021-10-19 18:37         ` Siddhesh Poyarekar
  0 siblings, 1 reply; 17+ messages in thread
From: Adhemerval Zanella @ 2021-10-19 18:24 UTC (permalink / raw)
  To: Siddhesh Poyarekar, libc-alpha



On 19/10/2021 15:19, Siddhesh Poyarekar wrote:
> On 10/19/21 23:24, Adhemerval Zanella wrote:
>>
>>
>> On 12/10/2021 13:16, Siddhesh Poyarekar via Libc-alpha wrote:
>>> In the context of a function definition, the size hints imply that the
>>> size of an object pointed to by one parameter is another parameter.
>>> This doesn't make sense for the fortified versions of the functions
>>> since that's the bit it's trying to validate.
>>>
>>> This is harmless with __builtin_object_size since it has fairly simple
>>> semantics when it comes to objects passed as function parameters.
>>> With __builtin_dynamic_object_size we could (as my patchset for gcc[1]
>>> already does) use the access attribute to determine the object size in
>>> the general case but it misleads the fortified functions.
>>>
>>> Disable the access attribute for fortified function inline functions
>>> when building at _FORTIFY_SOURCE=3 to make this work better.  The
>>
>> By 'work better' do you mean better code generation or better diagnostics
>> from gcc?
> 
> Basically the problem occurs when access attributes are present on regular functions that have inline fortified definitions to generate _chk variants; the attributes get inherited by these definitions, causing problems when analyzing them.  For example with poll(fds, nfds, timeout), nfds is hinted using the __attr_access as being the size of fds.
> 
> Now, when analyzing the inline function definition in bits/poll2.h, the compiler sees that nfds is the size of fds and tries to use that information in the function body.  In _FORTIFY_SOURCE=3 case, where the object size could be a non-constant expression, this information results in the conclusion that nfds is the size of fds, which defeats the purpose of the implementation because we're trying to check here if nfds does indeed represent the size of fds.  Hence for this case, it is best to not have the access attribute.
> 
> With the attributes gone, the expression evaluation should get delayed until the function is actually inlined into its destinations.

Thanks for the explanation, could you add this on the commit message
to make clear the change is to improve compiler diagnosis? 

> 
>>> access attributes remain for the _chk variants since they can be used
>>> by the compiler to warn when the caller is passing invalid arguments.
>>>
>>> Signed-off-by: Siddhesh Poyarekar <siddhesh@sourceware.org>
>>
>> The change looks ok, but what about other function that use
>> __attr_access ((__write_only__, ...). Should they be adapated as well?
> 
> Only functions that have a fortified implementation need to be changed, which is basically all of the functions below.
> 
>> Reviewed-by: Adhemerval Zanella  <adhemerval.zanella@linaro.org>
>>
> 
> Thanks,
> Siddhesh

^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: [PATCH 1/3] Don't add access size hints to fortifiable functions
  2021-10-19 18:24       ` Adhemerval Zanella
@ 2021-10-19 18:37         ` Siddhesh Poyarekar
  0 siblings, 0 replies; 17+ messages in thread
From: Siddhesh Poyarekar @ 2021-10-19 18:37 UTC (permalink / raw)
  To: Adhemerval Zanella, libc-alpha

On 10/19/21 23:54, Adhemerval Zanella wrote:
> Thanks for the explanation, could you add this on the commit message
> to make clear the change is to improve compiler diagnosis?

Sure thing.

Thanks,
Siddhesh

^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: [PATCH 2/3] Make sure that the fortified function conditionals are constant
  2021-10-12 16:16 ` [PATCH 2/3] Make sure that the fortified function conditionals are constant Siddhesh Poyarekar
@ 2021-10-19 19:34   ` Adhemerval Zanella
  2021-10-20  3:16     ` Siddhesh Poyarekar
  0 siblings, 1 reply; 17+ messages in thread
From: Adhemerval Zanella @ 2021-10-19 19:34 UTC (permalink / raw)
  To: Siddhesh Poyarekar, libc-alpha



On 12/10/2021 13:16, Siddhesh Poyarekar via Libc-alpha wrote:
> 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 <siddhesh@sourceware.org>

Path look ok in general, some suggestion regarding the macros below.


> ---
>  io/bits/poll2.h       |  27 ++----
>  libio/bits/stdio2.h   | 106 +++++++++-----------
>  misc/sys/cdefs.h      |  61 ++++++++++++
>  posix/bits/unistd.h   | 174 ++++++++-------------------------
>  socket/bits/socket2.h |  34 +++----
>  stdlib/bits/stdlib.h  |  57 ++++-------
>  wcsmbs/bits/wchar2.h  | 219 ++++++++++++------------------------------
>  7 files changed, 240 insertions(+), 438 deletions(-)
> 
> diff --git a/io/bits/poll2.h b/io/bits/poll2.h
> index be74d020f2..91cdcaf66a 100644
> --- a/io/bits/poll2.h
> +++ b/io/bits/poll2.h
> @@ -36,16 +36,9 @@ extern int __REDIRECT (__poll_chk_warn, (struct pollfd *__fds, nfds_t __nfds,
>  __fortify_function __fortified_attr_access (__write_only__, 1, 2) int
>  poll (struct pollfd *__fds, nfds_t __nfds, int __timeout)
>  {
> -  if (__glibc_objsize (__fds) != (__SIZE_TYPE__) -1)
> -    {
> -      if (! __builtin_constant_p (__nfds))
> -	return __poll_chk (__fds, __nfds, __timeout, __glibc_objsize (__fds));
> -      else if (__glibc_objsize (__fds) / sizeof (*__fds) < __nfds)
> -	return __poll_chk_warn (__fds, __nfds, __timeout,
> -				__glibc_objsize (__fds));
> -    }
> -
> -  return __poll_alias (__fds, __nfds, __timeout);
> +  return __glibc_fortify (poll, __nfds, sizeof (*__fds),
> +			  __glibc_objsize (__fds),
> +			  __fds, __nfds, __timeout);
>  }
>  
>  
> @@ -68,17 +61,9 @@ __fortify_function __fortified_attr_access (__write_only__, 1, 2) int
>  ppoll (struct pollfd *__fds, nfds_t __nfds, const struct timespec *__timeout,
>         const __sigset_t *__ss)
>  {
> -  if (__glibc_objsize (__fds) != (__SIZE_TYPE__) -1)
> -    {
> -      if (! __builtin_constant_p (__nfds))
> -	return __ppoll_chk (__fds, __nfds, __timeout, __ss,
> -			    __glibc_objsize (__fds));
> -      else if (__glibc_objsize (__fds) / sizeof (*__fds) < __nfds)
> -	return __ppoll_chk_warn (__fds, __nfds, __timeout, __ss,
> -				 __glibc_objsize (__fds));
> -    }
> -
> -  return __ppoll_alias (__fds, __nfds, __timeout, __ss);
> +  return __glibc_fortify (ppoll, __nfds, sizeof (*__fds),
> +			  __glibc_objsize (__fds),
> +			  __fds, __nfds, __timeout, __ss);
>  }
>  #endif
>  

Ok.

> diff --git a/libio/bits/stdio2.h b/libio/bits/stdio2.h
> index 4f016a5638..c111d13320 100644
> --- a/libio/bits/stdio2.h
> +++ b/libio/bits/stdio2.h
> @@ -261,15 +261,12 @@ extern char *__REDIRECT (__fgets_chk_warn,
>  __fortify_function __wur __fortified_attr_access (__write_only__, 1, 2) char *
>  fgets (char *__restrict __s, int __n, FILE *__restrict __stream)
>  {
> -  if (__glibc_objsize (__s) != (size_t) -1)
> -    {
> -      if (!__builtin_constant_p (__n) || __n <= 0)
> -	return __fgets_chk (__s, __glibc_objsize (__s), __n, __stream);
> -
> -      if ((size_t) __n > __glibc_objsize (__s))
> -	return __fgets_chk_warn (__s, __glibc_objsize (__s), __n, __stream);
> -    }
> -  return __fgets_alias (__s, __n, __stream);
> +  size_t sz = __glibc_objsize (__s);
> +  if (__glibc_safe_or_unknown_len_signed (__n, sizeof (char), sz))
> +    return __fgets_alias (__s, __n, __stream);
> +  if (__glibc_unsafe_len_signed (__n, sizeof (char), sz))
> +    return __fgets_chk_warn (__s, sz, __n, __stream);
> +  return __fgets_chk (__s, sz, __n, __stream);
>  }
>  

I am not sure if we still need to use reserved names (with double undescores)
on static inline functsion.  The changes loos ok.

>  extern size_t __fread_chk (void *__restrict __ptr, size_t __ptrlen,
> @@ -291,19 +288,12 @@ __fortify_function __wur size_t
>  fread (void *__restrict __ptr, size_t __size, size_t __n,
>         FILE *__restrict __stream)
>  {
> -  if (__glibc_objsize0 (__ptr) != (size_t) -1)
> -    {
> -      if (!__builtin_constant_p (__size)
> -	  || !__builtin_constant_p (__n)
> -	  || (__size | __n) >= (((size_t) 1) << (8 * sizeof (size_t) / 2)))
> -	return __fread_chk (__ptr, __glibc_objsize0 (__ptr), __size, __n,
> -			    __stream);
> -
> -      if (__size * __n > __glibc_objsize0 (__ptr))
> -	return __fread_chk_warn (__ptr, __glibc_objsize0 (__ptr), __size, __n,
> -				 __stream);
> -    }
> -  return __fread_alias (__ptr, __size, __n, __stream);
> +  size_t sz = __glibc_objsize0 (__ptr);
> +  if (__glibc_safe_or_unknown_len (__n, __size, sz))
> +    return __fread_alias (__ptr, __size, __n, __stream);
> +  if (__glibc_unsafe_len (__n, __size, sz))
> +    return __fread_chk_warn (__ptr, sz, __size, __n, __stream);
> +  return __fread_chk (__ptr, sz, __size, __n, __stream);
>  }
>  

Ok.

>  #ifdef __USE_GNU
> @@ -323,17 +313,12 @@ extern char *__REDIRECT (__fgets_unlocked_chk_warn,
>  __fortify_function __wur __fortified_attr_access (__write_only__, 1, 2) char *
>  fgets_unlocked (char *__restrict __s, int __n, FILE *__restrict __stream)
>  {
> -  if (__glibc_objsize (__s) != (size_t) -1)
> -    {
> -      if (!__builtin_constant_p (__n) || __n <= 0)
> -	return __fgets_unlocked_chk (__s, __glibc_objsize (__s), __n,
> -				     __stream);
> -
> -      if ((size_t) __n > __glibc_objsize (__s))
> -	return __fgets_unlocked_chk_warn (__s, __glibc_objsize (__s), __n,
> -					  __stream);
> -    }
> -  return __fgets_unlocked_alias (__s, __n, __stream);
> +  size_t sz = __glibc_objsize (__s);
> +  if (__glibc_safe_or_unknown_len_signed (__n, sizeof (char), sz))
> +    return __fgets_unlocked_alias (__s, __n, __stream);
> +  if (__glibc_unsafe_len_signed (__n, sizeof (char), sz))
> +    return __fgets_unlocked_chk_warn (__s, sz, __n, __stream);
> +  return __fgets_unlocked_chk (__s, sz, __n, __stream);
>  }
>  #endif
>  

Ok.

> @@ -358,41 +343,36 @@ __fortify_function __wur size_t
>  fread_unlocked (void *__restrict __ptr, size_t __size, size_t __n,
>  		FILE *__restrict __stream)
>  {
> -  if (__glibc_objsize0 (__ptr) != (size_t) -1)
> +  size_t sz = __glibc_objsize0 (__ptr);
> +  if (__glibc_safe_or_unknown_len (__n, __size, sz))
>      {
> -      if (!__builtin_constant_p (__size)
> -	  || !__builtin_constant_p (__n)
> -	  || (__size | __n) >= (((size_t) 1) << (8 * sizeof (size_t) / 2)))
> -	return __fread_unlocked_chk (__ptr, __glibc_objsize0 (__ptr), __size,
> -				     __n, __stream);
> -
> -      if (__size * __n > __glibc_objsize0 (__ptr))
> -	return __fread_unlocked_chk_warn (__ptr, __glibc_objsize0 (__ptr),
> -					  __size, __n, __stream);
> -    }
> -
>  # ifdef __USE_EXTERN_INLINES
> -  if (__builtin_constant_p (__size)
> -      && __builtin_constant_p (__n)
> -      && (__size | __n) < (((size_t) 1) << (8 * sizeof (size_t) / 2))
> -      && __size * __n <= 8)
> -    {
> -      size_t __cnt = __size * __n;
> -      char *__cptr = (char *) __ptr;
> -      if (__cnt == 0)
> -	return 0;
> -
> -      for (; __cnt > 0; --__cnt)
> +      if (__builtin_constant_p (__size)
> +	  && __builtin_constant_p (__n)
> +	  && (__size | __n) < (((size_t) 1) << (8 * sizeof (size_t) / 2))
> +	  && __size * __n <= 8)
>  	{
> -	  int __c = getc_unlocked (__stream);
> -	  if (__c == EOF)
> -	    break;
> -	  *__cptr++ = __c;
> +	  size_t __cnt = __size * __n;
> +	  char *__cptr = (char *) __ptr;
> +	  if (__cnt == 0)
> +	    return 0;
> +
> +	  for (; __cnt > 0; --__cnt)
> +	    {
> +	      int __c = getc_unlocked (__stream);
> +	      if (__c == EOF)
> +		break;
> +	      *__cptr++ = __c;
> +	    }
> +	  return (__cptr - (char *) __ptr) / __size;
>  	}
> -      return (__cptr - (char *) __ptr) / __size;
> -    }
>  # endif
> -  return __fread_unlocked_alias (__ptr, __size, __n, __stream);
> +      return __fread_unlocked_alias (__ptr, __size, __n, __stream);
> +    }
> +  if (__glibc_unsafe_len (__n, __size, sz))
> +    return __fread_unlocked_chk_warn (__ptr, sz, __size, __n, __stream);
> +  return __fread_unlocked_chk (__ptr, sz, __size, __n, __stream);
> +
>  }
>  #endif
>  

ok.

> diff --git a/misc/sys/cdefs.h b/misc/sys/cdefs.h
> index d08c2adfd0..22062ffaa8 100644
> --- a/misc/sys/cdefs.h
> +++ b/misc/sys/cdefs.h
> @@ -151,6 +151,67 @@
>  # define __glibc_objsize(__o) __bos (__o)
>  #endif
>  
> +/* Compile time conditions to choose between the regular, _chk and _chk_warn
> +   variants.  These conditions should get evaluated to constant and optimized
> +   away.  */
> +
> +#define __glibc_safe_len_cond(__l, __s, __osz) ((__l) <= (__osz) / (__s))
> +
> +/* Length is known to be safe at compile time if the __L * __S <= __OBJSZ
> +   condition can be folded to a constant and if it is true.  The -1 check is
> +   redundant because since it implies that __glibc_safe_len_cond is true.  */
> +#define __glibc_safe_or_unknown_len(__l, __s, __osz) \
> +  (__builtin_constant_p (__glibc_safe_len_cond (__l, __s, __osz))	      \
> +   && __glibc_safe_len_cond (__l, __s, __osz))
> +
> +/* Same as above, but add a sign check.  */
> +
> +#define __glibc_safe_or_unknown_len_signed(__l, __s, __osz) \
> +  (__builtin_constant_p (__l) && (__l) > 0				      \
> +   && __glibc_safe_or_unknown_len ((__SIZE_TYPE__) (__l), (__s), (__osz)))

Maybe to abstract the signed of the size within the macro itself:

  #define __glibc_type_signed(__t) (! ((__t) 0 < (__t) -1))

  #define __glibc_safe_or_unknown_len_ex(__l, __s, __osz) \
    (__builtin_constant_p (__glibc_safe_len_cond (__l, __s, __osz))             \
     && __glibc_safe_len_cond (__l, __s, __osz))

  #define __glibc_safe_or_unknown_len(__l, __s, __osz)                          \
    (__glibc_type_signed (__typeof__ (__s))                                     \
     ? __builtin_constant_p (__l) && (__l) > 0                                  \
       && __glibc_safe_or_unknown_len_ex ((__SIZE_TYPE__) (__l), (__s), (__osz))\
     : __glibc_safe_or_unknown_len_ex ((__l), (__s), (__osz)))

And then remove __glibc_safe_or_unknown_len_signed and use 
__glibc_safe_or_unknown_len instead? I think it slight less error prone so the
sign is checked automatically.

> +
> +/* Conversely, we know at compile time that the length is safe if the
> +   __L * __S <= __OBJSZ condition can be folded to a constant and if it is
> +   false.  */
> +#define __glibc_unsafe_len(__l, __s, __osz) \
> +  (__builtin_constant_p (__glibc_safe_len_cond (__l, __s, __osz))	      \
> +   && !__glibc_safe_len_cond (__l, __s, __osz))
> +
> +/* Same as above, but add a sign check.  */
> +
> +#define __glibc_unsafe_len_signed(__l, __s, __osz) \
> +  (__builtin_constant_p (__l) && (__l) > 0				      \
> +   && __glibc_unsafe_len ((__SIZE_TYPE__) (__l), (__s), (__osz)))
> +
> +/* Fortify function f.  __f_alias, __f_chk and __f_chk_warn must be
> +   declared.  */
> +

Same as before, maybe:

  #define __glibc_unsafe_len_ex(__l, __s, __osz) \
    (__builtin_constant_p (__glibc_safe_len_cond (__l, __s, __osz))             \
     && !__glibc_safe_len_cond (__l, __s, __osz))

  #define __glibc_unsafe_len(__l, __s, __osz) \
    (__glibc_type_signed (__typeof__ (__l))                                     \
     ? __builtin_constant_p (__l) && (__l) > 0                                  \
       && __glibc_unsafe_len_ex ((__SIZE_TYPE__) (__l), (__s), (__osz))         \
     : __glibc_unsafe_len_ex ((__l), (__s), (__osz)))


> +#define __glibc_fortify(f, __l, __s, __osz, ...) \
> +  (__glibc_safe_or_unknown_len (__l, __s, __osz)			      \
> +   ? __ ## f ## _alias (__VA_ARGS__)					      \
> +   : (__glibc_unsafe_len (__l, __s, __osz)				      \
> +      ? __ ## f ## _chk_warn (__VA_ARGS__, __osz)			      \
> +      : __ ## f ## _chk (__VA_ARGS__, __osz)))			      \
> +
> +/* Fortify function f, with signed length __l.  */
> +
> +#define __glibc_fortify_signed(f, __l, __s, __osz, ...) \
> +  (__glibc_safe_or_unknown_len_signed (__l, __s, __osz)			      \
> +   ? __ ## f ## _alias (__VA_ARGS__)					      \
> +   : (__glibc_unsafe_len_signed (__l, __s, __osz)			      \
> +      ? __ ## f ## _chk_warn (__VA_ARGS__, __osz)			      \
> +      : __ ## f ## _chk (__VA_ARGS__, __osz)))			      \

With the type agnostic macros this macro is not required.

> +
> +/* Fortify function f, where object size argument passed to f is the number of
> +   elements and not total size.  */
> +
> +#define __glibc_fortify_n(f, __l, __s, __osz, ...) \
> +  (__glibc_safe_or_unknown_len (__l, __s, __osz)			      \
> +   ? __ ## f ## _alias (__VA_ARGS__)					      \
> +   : (__glibc_unsafe_len (__l, __s, __osz)				      \
> +      ? __ ## f ## _chk_warn (__VA_ARGS__, (__osz) / (__s))		      \
> +      : __ ## f ## _chk (__VA_ARGS__, (__osz) / (__s))))		      \
> +
>  #if __GNUC_PREREQ (4,3)
>  # define __warnattr(msg) __attribute__((__warning__ (msg)))
>  # define __errordecl(name, msg) \
> diff --git a/posix/bits/unistd.h b/posix/bits/unistd.h
> index 622adeb2b2..dd8d71d83c 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_signed (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

Ok.

> diff --git a/socket/bits/socket2.h b/socket/bits/socket2.h
> index 9c8ac69624..b28cde55f3 100644
> --- a/socket/bits/socket2.h
> +++ b/socket/bits/socket2.h
> @@ -33,17 +33,12 @@ extern ssize_t __REDIRECT (__recv_chk_warn,
>  __fortify_function ssize_t
>  recv (int __fd, void *__buf, size_t __n, int __flags)
>  {
> -  if (__glibc_objsize0 (__buf) != (size_t) -1)
> -    {
> -      if (!__builtin_constant_p (__n))
> -	return __recv_chk (__fd, __buf, __n, __glibc_objsize0 (__buf),
> -			   __flags);
> -
> -      if (__n > __glibc_objsize0 (__buf))
> -	return __recv_chk_warn (__fd, __buf, __n, __glibc_objsize0 (__buf),
> -				__flags);
> -    }
> -  return __recv_alias (__fd, __buf, __n, __flags);
> +  size_t sz = __glibc_objsize0 (__buf);
> +  if (__glibc_safe_or_unknown_len (__n, sizeof (char), sz))
> +    return __recv_alias (__fd, __buf, __n, __flags);
> +  if (__glibc_unsafe_len (__n, sizeof (char), sz))
> +    return __recv_chk_warn (__fd, __buf, __n, sz, __flags);
> +  return __recv_chk (__fd, __buf, __n, sz, __flags);
>  }
>  
>  extern ssize_t __recvfrom_chk (int __fd, void *__restrict __buf, size_t __n,
> @@ -66,14 +61,11 @@ __fortify_function ssize_t
>  recvfrom (int __fd, void *__restrict __buf, size_t __n, int __flags,
>  	  __SOCKADDR_ARG __addr, socklen_t *__restrict __addr_len)
>  {
> -  if (__glibc_objsize0 (__buf) != (size_t) -1)
> -    {
> -      if (!__builtin_constant_p (__n))
> -	return __recvfrom_chk (__fd, __buf, __n, __glibc_objsize0 (__buf),
> -			       __flags, __addr, __addr_len);
> -      if (__n > __glibc_objsize0 (__buf))
> -	return __recvfrom_chk_warn (__fd, __buf, __n, __glibc_objsize0 (__buf),
> -				    __flags, __addr, __addr_len);
> -    }
> -  return __recvfrom_alias (__fd, __buf, __n, __flags, __addr, __addr_len);
> +  size_t sz = __glibc_objsize0 (__buf);
> +  if (__glibc_safe_or_unknown_len (__n, sizeof (char), sz))
> +    return __recvfrom_alias (__fd, __buf, __n, __flags, __addr, __addr_len);
> +  if (__glibc_unsafe_len (__n, sizeof (char), sz))
> +    return __recvfrom_chk_warn (__fd, __buf, __n, sz, __flags, __addr,
> +				__addr_len);
> +  return __recvfrom_chk (__fd, __buf, __n, sz, __flags, __addr, __addr_len);
>  }

Ok.

> diff --git a/stdlib/bits/stdlib.h b/stdlib/bits/stdlib.h
> index eae31b38f0..067115eeca 100644
> --- a/stdlib/bits/stdlib.h
> +++ b/stdlib/bits/stdlib.h
> @@ -36,17 +36,16 @@ extern char *__REDIRECT_NTH (__realpath_chk_warn,
>  __fortify_function __wur char *
>  __NTH (realpath (const char *__restrict __name, char *__restrict __resolved))
>  {
> -  if (__glibc_objsize (__resolved) != (size_t) -1)
> -    {
> +  size_t sz = __glibc_objsize (__resolved);
> +
> +  if (sz == (size_t) -1)
> +    return __realpath_alias (__name, __resolved);
> +
>  #if defined _LIBC_LIMITS_H_ && defined PATH_MAX
> -      if (__glibc_objsize (__resolved) < PATH_MAX)
> -	return __realpath_chk_warn (__name, __resolved,
> -				    __glibc_objsize (__resolved));
> +  if (__glibc_unsafe_len (sz, sizeof (char), PATH_MAX))
> +    return __realpath_chk_warn (__name, __resolved, sz);
>  #endif
> -      return __realpath_chk (__name, __resolved, __glibc_objsize (__resolved));
> -    }
> -
> -  return __realpath_alias (__name, __resolved);
> +  return __realpath_chk (__name, __resolved, sz);
>  }
>  
>  
> @@ -65,16 +64,9 @@ extern int __REDIRECT_NTH (__ptsname_r_chk_warn,
>  __fortify_function int
>  __NTH (ptsname_r (int __fd, char *__buf, size_t __buflen))
>  {
> -  if (__glibc_objsize (__buf) != (size_t) -1)
> -    {
> -      if (!__builtin_constant_p (__buflen))
> -	return __ptsname_r_chk (__fd, __buf, __buflen,
> -				__glibc_objsize (__buf));
> -      if (__buflen > __glibc_objsize (__buf))
> -	return __ptsname_r_chk_warn (__fd, __buf, __buflen,
> -				     __glibc_objsize (__buf));
> -    }
> -  return __ptsname_r_alias (__fd, __buf, __buflen);
> +  return __glibc_fortify (ptsname_r, __buflen, sizeof (char),
> +			  __glibc_objsize (__buf),
> +			  __fd, __buf, __buflen);
>  }
>  
>  
> @@ -120,18 +112,9 @@ __fortify_function size_t
>  __NTH (mbstowcs (wchar_t *__restrict __dst, const char *__restrict __src,
>  		 size_t __len))
>  {
> -  if (__glibc_objsize (__dst) != (size_t) -1)
> -    {
> -      if (!__builtin_constant_p (__len))
> -	return __mbstowcs_chk (__dst, __src, __len,
> -			       __glibc_objsize (__dst) / sizeof (wchar_t));
> -
> -      if (__len > __glibc_objsize (__dst) / sizeof (wchar_t))
> -	return __mbstowcs_chk_warn (__dst, __src, __len,
> -				    (__glibc_objsize (__dst)
> -				     / sizeof (wchar_t)));
> -    }
> -  return __mbstowcs_alias (__dst, __src, __len);
> +  return __glibc_fortify_n (mbstowcs, __len, sizeof (wchar_t),
> +			    __glibc_objsize (__dst),
> +			    __dst, __src, __len);
>  }
>  
>  
> @@ -154,13 +137,7 @@ __fortify_function size_t
>  __NTH (wcstombs (char *__restrict __dst, const wchar_t *__restrict __src,
>  		 size_t __len))
>  {
> -  if (__glibc_objsize (__dst) != (size_t) -1)
> -    {
> -      if (!__builtin_constant_p (__len))
> -	return __wcstombs_chk (__dst, __src, __len, __glibc_objsize (__dst));
> -      if (__len > __glibc_objsize (__dst))
> -	return __wcstombs_chk_warn (__dst, __src, __len,
> -				    __glibc_objsize (__dst));
> -    }
> -  return __wcstombs_alias (__dst, __src, __len);
> +  return __glibc_fortify (wcstombs, __len, sizeof (char),
> +			  __glibc_objsize (__dst),
> +			  __dst, __src, __len);
>  }
> diff --git a/wcsmbs/bits/wchar2.h b/wcsmbs/bits/wchar2.h
> index ea2518dc72..2f42fdcc64 100644
> --- a/wcsmbs/bits/wchar2.h
> +++ b/wcsmbs/bits/wchar2.h
> @@ -39,17 +39,9 @@ __fortify_function wchar_t *
>  __NTH (wmemcpy (wchar_t *__restrict __s1, const wchar_t *__restrict __s2,
>  		size_t __n))
>  {
> -  if (__glibc_objsize0 (__s1) != (size_t) -1)
> -    {
> -      if (!__builtin_constant_p (__n))
> -	return __wmemcpy_chk (__s1, __s2, __n,
> -			      __glibc_objsize0 (__s1) / sizeof (wchar_t));
> -
> -      if (__n > __glibc_objsize0 (__s1) / sizeof (wchar_t))
> -	return __wmemcpy_chk_warn (__s1, __s2, __n,
> -				   __glibc_objsize0 (__s1) / sizeof (wchar_t));
> -    }
> -  return __wmemcpy_alias (__s1, __s2, __n);
> +  return __glibc_fortify_n (wmemcpy, __n, sizeof (wchar_t),
> +			    __glibc_objsize0 (__s1),
> +			    __s1, __s2, __n);
>  }
>  
>  
> @@ -67,18 +59,9 @@ extern wchar_t *__REDIRECT_NTH (__wmemmove_chk_warn,
>  __fortify_function wchar_t *
>  __NTH (wmemmove (wchar_t *__s1, const wchar_t *__s2, size_t __n))
>  {
> -  if (__glibc_objsize0 (__s1) != (size_t) -1)
> -    {
> -      if (!__builtin_constant_p (__n))
> -	return __wmemmove_chk (__s1, __s2, __n,
> -			       __glibc_objsize0 (__s1) / sizeof (wchar_t));
> -
> -      if (__n > __glibc_objsize0 (__s1) / sizeof (wchar_t))
> -	return __wmemmove_chk_warn (__s1, __s2, __n,
> -				    (__glibc_objsize0 (__s1)
> -				     / sizeof (wchar_t)));
> -    }
> -  return __wmemmove_alias (__s1, __s2, __n);
> +  return __glibc_fortify_n (wmemmove, __n, sizeof (wchar_t),
> +			    __glibc_objsize0 (__s1),
> +			    __s1, __s2, __n);
>  }
>  
>  
> @@ -101,18 +84,9 @@ __fortify_function wchar_t *
>  __NTH (wmempcpy (wchar_t *__restrict __s1, const wchar_t *__restrict __s2,
>  		 size_t __n))
>  {
> -  if (__glibc_objsize0 (__s1) != (size_t) -1)
> -    {
> -      if (!__builtin_constant_p (__n))
> -	return __wmempcpy_chk (__s1, __s2, __n,
> -			       __glibc_objsize0 (__s1) / sizeof (wchar_t));
> -
> -      if (__n > __glibc_objsize0 (__s1) / sizeof (wchar_t))
> -	return __wmempcpy_chk_warn (__s1, __s2, __n,
> -				    (__glibc_objsize0 (__s1)
> -				     / sizeof (wchar_t)));
> -    }
> -  return __wmempcpy_alias (__s1, __s2, __n);
> +  return __glibc_fortify_n (wmempcpy, __n, sizeof (wchar_t),
> +			    __glibc_objsize0 (__s1),
> +			    __s1, __s2, __n);
>  }
>  #endif
>  
> @@ -130,17 +104,9 @@ extern wchar_t *__REDIRECT_NTH (__wmemset_chk_warn,
>  __fortify_function wchar_t *
>  __NTH (wmemset (wchar_t *__s, wchar_t __c, size_t __n))
>  {
> -  if (__glibc_objsize0 (__s) != (size_t) -1)
> -    {
> -      if (!__builtin_constant_p (__n))
> -	return __wmemset_chk (__s, __c, __n,
> -			      __glibc_objsize0 (__s) / sizeof (wchar_t));
> -
> -      if (__n > __glibc_objsize0 (__s) / sizeof (wchar_t))
> -	return __wmemset_chk_warn (__s, __c, __n,
> -				   __glibc_objsize0 (__s) / sizeof (wchar_t));
> -    }
> -  return __wmemset_alias (__s, __c, __n);
> +  return __glibc_fortify_n (wmemset, __n, sizeof (wchar_t),
> +			    __glibc_objsize0 (__s),
> +			    __s, __c, __n);
>  }
>  
>  
> @@ -154,9 +120,9 @@ extern wchar_t *__REDIRECT_NTH (__wcscpy_alias,
>  __fortify_function wchar_t *
>  __NTH (wcscpy (wchar_t *__restrict __dest, const wchar_t *__restrict __src))
>  {
> -  if (__glibc_objsize (__dest) != (size_t) -1)
> -    return __wcscpy_chk (__dest, __src,
> -			 __glibc_objsize (__dest) / sizeof (wchar_t));
> +  size_t sz = __glibc_objsize (__dest);
> +  if (sz != (size_t) -1)
> +    return __wcscpy_chk (__dest, __src, sz / sizeof (wchar_t));
>    return __wcscpy_alias (__dest, __src);
>  }
>  
> @@ -171,9 +137,9 @@ extern wchar_t *__REDIRECT_NTH (__wcpcpy_alias,
>  __fortify_function wchar_t *
>  __NTH (wcpcpy (wchar_t *__restrict __dest, const wchar_t *__restrict __src))
>  {
> -  if (__glibc_objsize (__dest) != (size_t) -1)
> -    return __wcpcpy_chk (__dest, __src,
> -			 __glibc_objsize (__dest) / sizeof (wchar_t));
> +  size_t sz = __glibc_objsize (__dest);
> +  if (sz != (size_t) -1)
> +    return __wcpcpy_chk (__dest, __src, sz / sizeof (wchar_t));
>    return __wcpcpy_alias (__dest, __src);
>  }
>  
> @@ -196,17 +162,9 @@ __fortify_function wchar_t *
>  __NTH (wcsncpy (wchar_t *__restrict __dest, const wchar_t *__restrict __src,
>  		size_t __n))
>  {
> -  if (__glibc_objsize (__dest) != (size_t) -1)
> -    {
> -      if (!__builtin_constant_p (__n))
> -	return __wcsncpy_chk (__dest, __src, __n,
> -			      __glibc_objsize (__dest) / sizeof (wchar_t));
> -      if (__n > __glibc_objsize (__dest) / sizeof (wchar_t))
> -	return __wcsncpy_chk_warn (__dest, __src, __n,
> -				   (__glibc_objsize (__dest)
> -				    / sizeof (wchar_t)));
> -    }
> -  return __wcsncpy_alias (__dest, __src, __n);
> +  return __glibc_fortify_n (wcsncpy, __n, sizeof (wchar_t),
> +			    __glibc_objsize (__dest),
> +			    __dest, __src, __n);
>  }
>  
>  
> @@ -228,17 +186,9 @@ __fortify_function wchar_t *
>  __NTH (wcpncpy (wchar_t *__restrict __dest, const wchar_t *__restrict __src,
>  		size_t __n))
>  {
> -  if (__glibc_objsize (__dest) != (size_t) -1)
> -    {
> -      if (!__builtin_constant_p (__n))
> -	return __wcpncpy_chk (__dest, __src, __n,
> -			      __glibc_objsize (__dest) / sizeof (wchar_t));
> -      if (__n > __glibc_objsize (__dest) / sizeof (wchar_t))
> -	return __wcpncpy_chk_warn (__dest, __src, __n,
> -				   (__glibc_objsize (__dest)
> -				    / sizeof (wchar_t)));
> -    }
> -  return __wcpncpy_alias (__dest, __src, __n);
> +  return __glibc_fortify_n (wcpncpy, __n, sizeof (wchar_t),
> +			    __glibc_objsize (__dest),
> +			    __dest, __src, __n);
>  }
>  
>  
> @@ -252,9 +202,9 @@ extern wchar_t *__REDIRECT_NTH (__wcscat_alias,
>  __fortify_function wchar_t *
>  __NTH (wcscat (wchar_t *__restrict __dest, const wchar_t *__restrict __src))
>  {
> -  if (__glibc_objsize (__dest) != (size_t) -1)
> -    return __wcscat_chk (__dest, __src,
> -			 __glibc_objsize (__dest) / sizeof (wchar_t));
> +  size_t sz = __glibc_objsize (__dest);
> +  if (sz != (size_t) -1)
> +    return __wcscat_chk (__dest, __src, sz / sizeof (wchar_t));
>    return __wcscat_alias (__dest, __src);
>  }
>  
> @@ -271,9 +221,9 @@ __fortify_function wchar_t *
>  __NTH (wcsncat (wchar_t *__restrict __dest, const wchar_t *__restrict __src,
>  		size_t __n))
>  {
> -  if (__glibc_objsize (__dest) != (size_t) -1)
> -    return __wcsncat_chk (__dest, __src, __n,
> -			  __glibc_objsize (__dest) / sizeof (wchar_t));
> +  size_t sz = __glibc_objsize (__dest);
> +  if (sz != (size_t) -1)
> +    return __wcsncat_chk (__dest, __src, __n, sz / sizeof (wchar_t));
>    return __wcsncat_alias (__dest, __src, __n);
>  }
>  
> @@ -293,10 +243,10 @@ __fortify_function int
>  __NTH (swprintf (wchar_t *__restrict __s, size_t __n,
>  		 const wchar_t *__restrict __fmt, ...))
>  {
> -  if (__glibc_objsize (__s) != (size_t) -1 || __USE_FORTIFY_LEVEL > 1)
> +  size_t sz = __glibc_objsize (__s);
> +  if (sz != (size_t) -1 || __USE_FORTIFY_LEVEL > 1)
>      return __swprintf_chk (__s, __n, __USE_FORTIFY_LEVEL - 1,
> -			   __glibc_objsize (__s) / sizeof (wchar_t),
> -			   __fmt, __va_arg_pack ());
> +			   sz / sizeof (wchar_t), __fmt, __va_arg_pack ());
>    return __swprintf_alias (__s, __n, __fmt, __va_arg_pack ());
>  }
>  #elif !defined __cplusplus
> @@ -323,10 +273,10 @@ __fortify_function int
>  __NTH (vswprintf (wchar_t *__restrict __s, size_t __n,
>  		  const wchar_t *__restrict __fmt, __gnuc_va_list __ap))
>  {
> -  if (__glibc_objsize (__s) != (size_t) -1 || __USE_FORTIFY_LEVEL > 1)
> +  size_t sz = __glibc_objsize (__s);
> +  if (sz != (size_t) -1 || __USE_FORTIFY_LEVEL > 1)
>      return __vswprintf_chk (__s, __n,  __USE_FORTIFY_LEVEL - 1,
> -			    __glibc_objsize (__s) / sizeof (wchar_t), __fmt,
> -			    __ap);
> +			    sz / sizeof (wchar_t), __fmt, __ap);
>    return __vswprintf_alias (__s, __n, __fmt, __ap);
>  }
>  
> @@ -392,18 +342,12 @@ extern wchar_t *__REDIRECT (__fgetws_chk_warn,
>  __fortify_function __wur wchar_t *
>  fgetws (wchar_t *__restrict __s, int __n, __FILE *__restrict __stream)
>  {
> -  if (__glibc_objsize (__s) != (size_t) -1)
> -    {
> -      if (!__builtin_constant_p (__n) || __n <= 0)
> -	return __fgetws_chk (__s, __glibc_objsize (__s) / sizeof (wchar_t),
> -			     __n, __stream);
> -
> -      if ((size_t) __n > __glibc_objsize (__s) / sizeof (wchar_t))
> -	return __fgetws_chk_warn (__s,
> -				  __glibc_objsize (__s) / sizeof (wchar_t),
> -				  __n, __stream);
> -    }
> -  return __fgetws_alias (__s, __n, __stream);
> +  size_t sz = __glibc_objsize (__s);
> +  if (__glibc_safe_or_unknown_len_signed (__n, sizeof (wchar_t), sz))
> +    return __fgetws_alias (__s, __n, __stream);
> +  if (__glibc_unsafe_len_signed (__n, sizeof (wchar_t), sz))
> +    return __fgetws_chk_warn (__s, sz / sizeof (wchar_t), __n, __stream);
> +  return __fgetws_chk (__s, sz / sizeof (wchar_t), __n, __stream);
>  }
>  
>  #ifdef __USE_GNU
> @@ -424,20 +368,13 @@ extern wchar_t *__REDIRECT (__fgetws_unlocked_chk_warn,
>  __fortify_function __wur wchar_t *
>  fgetws_unlocked (wchar_t *__restrict __s, int __n, __FILE *__restrict __stream)
>  {
> -  if (__glibc_objsize (__s) != (size_t) -1)
> -    {
> -      if (!__builtin_constant_p (__n) || __n <= 0)
> -	return __fgetws_unlocked_chk (__s,
> -				      __glibc_objsize (__s) / sizeof (wchar_t),
> -				      __n, __stream);
> -
> -      if ((size_t) __n > __glibc_objsize (__s) / sizeof (wchar_t))
> -	return __fgetws_unlocked_chk_warn (__s,
> -					   (__glibc_objsize (__s)
> -					    / sizeof (wchar_t)),
> -					   __n, __stream);
> -    }
> -  return __fgetws_unlocked_alias (__s, __n, __stream);
> +  size_t sz = __glibc_objsize (__s);
> +  if (__glibc_safe_or_unknown_len_signed (__n, sizeof (wchar_t), sz))
> +    return __fgetws_unlocked_alias (__s, __n, __stream);
> +  if (__glibc_unsafe_len_signed (__n, sizeof (wchar_t), sz))
> +    return __fgetws_unlocked_chk_warn (__s, sz / sizeof (wchar_t), __n,
> +				       __stream);
> +  return __fgetws_unlocked_chk (__s, sz / sizeof (wchar_t), __n, __stream);
>  }
>  #endif
>  
> @@ -488,18 +425,9 @@ __fortify_function size_t
>  __NTH (mbsrtowcs (wchar_t *__restrict __dst, const char **__restrict __src,
>  		  size_t __len, mbstate_t *__restrict __ps))
>  {
> -  if (__glibc_objsize (__dst) != (size_t) -1)
> -    {
> -      if (!__builtin_constant_p (__len))
> -	return __mbsrtowcs_chk (__dst, __src, __len, __ps,
> -				__glibc_objsize (__dst) / sizeof (wchar_t));
> -
> -      if (__len > __glibc_objsize (__dst) / sizeof (wchar_t))
> -	return __mbsrtowcs_chk_warn (__dst, __src, __len, __ps,
> -				     (__glibc_objsize (__dst)
> -				      / sizeof (wchar_t)));
> -    }
> -  return __mbsrtowcs_alias (__dst, __src, __len, __ps);
> +  return __glibc_fortify_n (mbsrtowcs, __len, sizeof (wchar_t),
> +			    __glibc_objsize (__dst),
> +			    __dst, __src, __len, __ps);
>  }
>  
>  
> @@ -523,17 +451,9 @@ __fortify_function size_t
>  __NTH (wcsrtombs (char *__restrict __dst, const wchar_t **__restrict __src,
>  		  size_t __len, mbstate_t *__restrict __ps))
>  {
> -  if (__glibc_objsize (__dst) != (size_t) -1)
> -    {
> -      if (!__builtin_constant_p (__len))
> -	return __wcsrtombs_chk (__dst, __src, __len, __ps,
> -				__glibc_objsize (__dst));
> -
> -      if (__len > __glibc_objsize (__dst))
> -	return __wcsrtombs_chk_warn (__dst, __src, __len, __ps,
> -				     __glibc_objsize (__dst));
> -    }
> -  return __wcsrtombs_alias (__dst, __src, __len, __ps);
> +  return __glibc_fortify (wcsrtombs, __len, sizeof (char),
> +			  __glibc_objsize (__dst),
> +			  __dst, __src, __len, __ps);
>  }
>  
>  
> @@ -559,18 +479,9 @@ __fortify_function size_t
>  __NTH (mbsnrtowcs (wchar_t *__restrict __dst, const char **__restrict __src,
>  		   size_t __nmc, size_t __len, mbstate_t *__restrict __ps))
>  {
> -  if (__glibc_objsize (__dst) != (size_t) -1)
> -    {
> -      if (!__builtin_constant_p (__len))
> -	return __mbsnrtowcs_chk (__dst, __src, __nmc, __len, __ps,
> -				 __glibc_objsize (__dst) / sizeof (wchar_t));
> -
> -      if (__len > __glibc_objsize (__dst) / sizeof (wchar_t))
> -	return __mbsnrtowcs_chk_warn (__dst, __src, __nmc, __len, __ps,
> -				      (__glibc_objsize (__dst)
> -				       / sizeof (wchar_t)));
> -    }
> -  return __mbsnrtowcs_alias (__dst, __src, __nmc, __len, __ps);
> +  return __glibc_fortify_n (mbsnrtowcs, __len, sizeof (wchar_t),
> +			    __glibc_objsize (__dst),
> +			    __dst, __src, __nmc, __len, __ps);
>  }
>  
>  
> @@ -596,16 +507,8 @@ __fortify_function size_t
>  __NTH (wcsnrtombs (char *__restrict __dst, const wchar_t **__restrict __src,
>  		   size_t __nwc, size_t __len, mbstate_t *__restrict __ps))
>  {
> -  if (__glibc_objsize (__dst) != (size_t) -1)
> -    {
> -      if (!__builtin_constant_p (__len))
> -	return __wcsnrtombs_chk (__dst, __src, __nwc, __len, __ps,
> -				 __glibc_objsize (__dst));
> -
> -      if (__len > __glibc_objsize (__dst))
> -	return __wcsnrtombs_chk_warn (__dst, __src, __nwc, __len, __ps,
> -				      __glibc_objsize (__dst));
> -    }
> -  return __wcsnrtombs_alias (__dst, __src, __nwc, __len, __ps);
> +  return __glibc_fortify (wcsnrtombs, __len, sizeof (char),
> +			  __glibc_objsize (__dst),
> +			  __dst, __src, __nwc, __len, __ps);
>  }
>  #endif
> 

^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: [PATCH 2/3] Make sure that the fortified function conditionals are constant
  2021-10-19 19:34   ` Adhemerval Zanella
@ 2021-10-20  3:16     ` Siddhesh Poyarekar
  0 siblings, 0 replies; 17+ messages in thread
From: Siddhesh Poyarekar @ 2021-10-20  3:16 UTC (permalink / raw)
  To: Adhemerval Zanella, libc-alpha

On 10/20/21 01:04, Adhemerval Zanella via Libc-alpha wrote:
> I am not sure if we still need to use reserved names (with double undescores)
> on static inline functsion.  The changes loos ok.

User code defining "stream" to something weird?

> Maybe to abstract the signed of the size within the macro itself:
> 
>    #define __glibc_type_signed(__t) (! ((__t) 0 < (__t) -1))
> 
>    #define __glibc_safe_or_unknown_len_ex(__l, __s, __osz) \
>      (__builtin_constant_p (__glibc_safe_len_cond (__l, __s, __osz))             \
>       && __glibc_safe_len_cond (__l, __s, __osz))
> 
>    #define __glibc_safe_or_unknown_len(__l, __s, __osz)                          \
>      (__glibc_type_signed (__typeof__ (__s))                                     \
>       ? __builtin_constant_p (__l) && (__l) > 0                                  \
>         && __glibc_safe_or_unknown_len_ex ((__SIZE_TYPE__) (__l), (__s), (__osz))\
>       : __glibc_safe_or_unknown_len_ex ((__l), (__s), (__osz)))
> 
> And then remove __glibc_safe_or_unknown_len_signed and use
> __glibc_safe_or_unknown_len instead? I think it slight less error prone so the
> sign is checked automatically.

I'll make this change, commit 1/3 and post a v2 patchset.

Thanks,
Siddhesh

^ permalink raw reply	[flat|nested] 17+ messages in thread

* [PATCH v2 0/2] _FORTIFY_SOURCE=3 improvements
  2021-10-12 16:16 [PATCH 0/3] _FORTIFY_SOURCE=3 improvements Siddhesh Poyarekar
                   ` (3 preceding siblings ...)
  2021-10-18 13:14 ` [PING][PATCH 0/3] _FORTIFY_SOURCE=3 improvements Siddhesh Poyarekar
@ 2021-10-20  5:24 ` Siddhesh Poyarekar
  2021-10-20  5:24   ` [PATCH v2 1/2] Make sure that the fortified function conditionals are constant Siddhesh Poyarekar
                     ` (2 more replies)
  4 siblings, 3 replies; 17+ messages in thread
From: Siddhesh Poyarekar @ 2021-10-20  5:24 UTC (permalink / raw)
  To: libc-alpha

This patchset changes the layout of fortified functions to make them
_FORTIFY_SOURCE=3 friendly and at the same time, continue working the
same for _FORTIFY_SOURCE=2 and lower.  At a high level it makes sure
that no branches are emitted at compile time and only one of either the
regular or _chk version of the function is called.  The conditions are
also reworked to make them more readable and foldable even in cases
where the compiler doesn't know the exact values of the operation length
and size, but can make decisions based on ranges of their values.

The changeset also adds some _FORTIFY_SOURCE=3 testing coverage on
compilers that are able to set that fortification level.

Changes from v1:

- Committed access attribute related patch
- Updated macros to fold the sign check into a single macro

Siddhesh Poyarekar (2):
  Make sure that the fortified function conditionals are constant
  debug: Add tests for _FORTIFY_SOURCE=3

 debug/Makefile        |  13 ++-
 debug/tst-chk1.c      | 102 +++++++++++---------
 debug/tst-chk7.c      |   2 +
 debug/tst-chk8.cc     |   2 +
 io/bits/poll2.h       |  27 ++----
 libio/bits/stdio2.h   | 106 +++++++++-----------
 misc/sys/cdefs.h      |  47 +++++++++
 posix/bits/unistd.h   | 174 ++++++++-------------------------
 socket/bits/socket2.h |  34 +++----
 stdlib/bits/stdlib.h  |  57 ++++-------
 wcsmbs/bits/wchar2.h  | 219 ++++++++++++------------------------------
 11 files changed, 295 insertions(+), 488 deletions(-)
 create mode 100644 debug/tst-chk7.c
 create mode 100644 debug/tst-chk8.cc

-- 
2.31.1


^ permalink raw reply	[flat|nested] 17+ messages in thread

* [PATCH v2 1/2] Make sure that the fortified function conditionals are constant
  2021-10-20  5:24 ` [PATCH v2 0/2] " Siddhesh Poyarekar
@ 2021-10-20  5:24   ` Siddhesh Poyarekar
  2021-10-20 12:00     ` Adhemerval Zanella
  2021-10-20  5:24   ` [PATCH v2 2/2] debug: Add tests for _FORTIFY_SOURCE=3 Siddhesh Poyarekar
  2021-10-20 14:28   ` [PATCH v2 0/2] _FORTIFY_SOURCE=3 improvements Siddhesh Poyarekar
  2 siblings, 1 reply; 17+ messages in thread
From: Siddhesh Poyarekar @ 2021-10-20  5:24 UTC (permalink / raw)
  To: libc-alpha

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 <siddhesh@sourceware.org>
---
 io/bits/poll2.h       |  27 ++----
 libio/bits/stdio2.h   | 106 +++++++++-----------
 misc/sys/cdefs.h      |  47 +++++++++
 posix/bits/unistd.h   | 174 ++++++++-------------------------
 socket/bits/socket2.h |  34 +++----
 stdlib/bits/stdlib.h  |  57 ++++-------
 wcsmbs/bits/wchar2.h  | 219 ++++++++++++------------------------------
 7 files changed, 226 insertions(+), 438 deletions(-)

diff --git a/io/bits/poll2.h b/io/bits/poll2.h
index be74d020f2..91cdcaf66a 100644
--- a/io/bits/poll2.h
+++ b/io/bits/poll2.h
@@ -36,16 +36,9 @@ extern int __REDIRECT (__poll_chk_warn, (struct pollfd *__fds, nfds_t __nfds,
 __fortify_function __fortified_attr_access (__write_only__, 1, 2) int
 poll (struct pollfd *__fds, nfds_t __nfds, int __timeout)
 {
-  if (__glibc_objsize (__fds) != (__SIZE_TYPE__) -1)
-    {
-      if (! __builtin_constant_p (__nfds))
-	return __poll_chk (__fds, __nfds, __timeout, __glibc_objsize (__fds));
-      else if (__glibc_objsize (__fds) / sizeof (*__fds) < __nfds)
-	return __poll_chk_warn (__fds, __nfds, __timeout,
-				__glibc_objsize (__fds));
-    }
-
-  return __poll_alias (__fds, __nfds, __timeout);
+  return __glibc_fortify (poll, __nfds, sizeof (*__fds),
+			  __glibc_objsize (__fds),
+			  __fds, __nfds, __timeout);
 }
 
 
@@ -68,17 +61,9 @@ __fortify_function __fortified_attr_access (__write_only__, 1, 2) int
 ppoll (struct pollfd *__fds, nfds_t __nfds, const struct timespec *__timeout,
        const __sigset_t *__ss)
 {
-  if (__glibc_objsize (__fds) != (__SIZE_TYPE__) -1)
-    {
-      if (! __builtin_constant_p (__nfds))
-	return __ppoll_chk (__fds, __nfds, __timeout, __ss,
-			    __glibc_objsize (__fds));
-      else if (__glibc_objsize (__fds) / sizeof (*__fds) < __nfds)
-	return __ppoll_chk_warn (__fds, __nfds, __timeout, __ss,
-				 __glibc_objsize (__fds));
-    }
-
-  return __ppoll_alias (__fds, __nfds, __timeout, __ss);
+  return __glibc_fortify (ppoll, __nfds, sizeof (*__fds),
+			  __glibc_objsize (__fds),
+			  __fds, __nfds, __timeout, __ss);
 }
 #endif
 
diff --git a/libio/bits/stdio2.h b/libio/bits/stdio2.h
index 4f016a5638..40ff16b01b 100644
--- a/libio/bits/stdio2.h
+++ b/libio/bits/stdio2.h
@@ -261,15 +261,12 @@ extern char *__REDIRECT (__fgets_chk_warn,
 __fortify_function __wur __fortified_attr_access (__write_only__, 1, 2) char *
 fgets (char *__restrict __s, int __n, FILE *__restrict __stream)
 {
-  if (__glibc_objsize (__s) != (size_t) -1)
-    {
-      if (!__builtin_constant_p (__n) || __n <= 0)
-	return __fgets_chk (__s, __glibc_objsize (__s), __n, __stream);
-
-      if ((size_t) __n > __glibc_objsize (__s))
-	return __fgets_chk_warn (__s, __glibc_objsize (__s), __n, __stream);
-    }
-  return __fgets_alias (__s, __n, __stream);
+  size_t sz = __glibc_objsize (__s);
+  if (__glibc_safe_or_unknown_len (__n, sizeof (char), sz))
+    return __fgets_alias (__s, __n, __stream);
+  if (__glibc_unsafe_len (__n, sizeof (char), sz))
+    return __fgets_chk_warn (__s, sz, __n, __stream);
+  return __fgets_chk (__s, sz, __n, __stream);
 }
 
 extern size_t __fread_chk (void *__restrict __ptr, size_t __ptrlen,
@@ -291,19 +288,12 @@ __fortify_function __wur size_t
 fread (void *__restrict __ptr, size_t __size, size_t __n,
        FILE *__restrict __stream)
 {
-  if (__glibc_objsize0 (__ptr) != (size_t) -1)
-    {
-      if (!__builtin_constant_p (__size)
-	  || !__builtin_constant_p (__n)
-	  || (__size | __n) >= (((size_t) 1) << (8 * sizeof (size_t) / 2)))
-	return __fread_chk (__ptr, __glibc_objsize0 (__ptr), __size, __n,
-			    __stream);
-
-      if (__size * __n > __glibc_objsize0 (__ptr))
-	return __fread_chk_warn (__ptr, __glibc_objsize0 (__ptr), __size, __n,
-				 __stream);
-    }
-  return __fread_alias (__ptr, __size, __n, __stream);
+  size_t sz = __glibc_objsize0 (__ptr);
+  if (__glibc_safe_or_unknown_len (__n, __size, sz))
+    return __fread_alias (__ptr, __size, __n, __stream);
+  if (__glibc_unsafe_len (__n, __size, sz))
+    return __fread_chk_warn (__ptr, sz, __size, __n, __stream);
+  return __fread_chk (__ptr, sz, __size, __n, __stream);
 }
 
 #ifdef __USE_GNU
@@ -323,17 +313,12 @@ extern char *__REDIRECT (__fgets_unlocked_chk_warn,
 __fortify_function __wur __fortified_attr_access (__write_only__, 1, 2) char *
 fgets_unlocked (char *__restrict __s, int __n, FILE *__restrict __stream)
 {
-  if (__glibc_objsize (__s) != (size_t) -1)
-    {
-      if (!__builtin_constant_p (__n) || __n <= 0)
-	return __fgets_unlocked_chk (__s, __glibc_objsize (__s), __n,
-				     __stream);
-
-      if ((size_t) __n > __glibc_objsize (__s))
-	return __fgets_unlocked_chk_warn (__s, __glibc_objsize (__s), __n,
-					  __stream);
-    }
-  return __fgets_unlocked_alias (__s, __n, __stream);
+  size_t sz = __glibc_objsize (__s);
+  if (__glibc_safe_or_unknown_len (__n, sizeof (char), sz))
+    return __fgets_unlocked_alias (__s, __n, __stream);
+  if (__glibc_unsafe_len (__n, sizeof (char), sz))
+    return __fgets_unlocked_chk_warn (__s, sz, __n, __stream);
+  return __fgets_unlocked_chk (__s, sz, __n, __stream);
 }
 #endif
 
@@ -358,41 +343,36 @@ __fortify_function __wur size_t
 fread_unlocked (void *__restrict __ptr, size_t __size, size_t __n,
 		FILE *__restrict __stream)
 {
-  if (__glibc_objsize0 (__ptr) != (size_t) -1)
+  size_t sz = __glibc_objsize0 (__ptr);
+  if (__glibc_safe_or_unknown_len (__n, __size, sz))
     {
-      if (!__builtin_constant_p (__size)
-	  || !__builtin_constant_p (__n)
-	  || (__size | __n) >= (((size_t) 1) << (8 * sizeof (size_t) / 2)))
-	return __fread_unlocked_chk (__ptr, __glibc_objsize0 (__ptr), __size,
-				     __n, __stream);
-
-      if (__size * __n > __glibc_objsize0 (__ptr))
-	return __fread_unlocked_chk_warn (__ptr, __glibc_objsize0 (__ptr),
-					  __size, __n, __stream);
-    }
-
 # ifdef __USE_EXTERN_INLINES
-  if (__builtin_constant_p (__size)
-      && __builtin_constant_p (__n)
-      && (__size | __n) < (((size_t) 1) << (8 * sizeof (size_t) / 2))
-      && __size * __n <= 8)
-    {
-      size_t __cnt = __size * __n;
-      char *__cptr = (char *) __ptr;
-      if (__cnt == 0)
-	return 0;
-
-      for (; __cnt > 0; --__cnt)
+      if (__builtin_constant_p (__size)
+	  && __builtin_constant_p (__n)
+	  && (__size | __n) < (((size_t) 1) << (8 * sizeof (size_t) / 2))
+	  && __size * __n <= 8)
 	{
-	  int __c = getc_unlocked (__stream);
-	  if (__c == EOF)
-	    break;
-	  *__cptr++ = __c;
+	  size_t __cnt = __size * __n;
+	  char *__cptr = (char *) __ptr;
+	  if (__cnt == 0)
+	    return 0;
+
+	  for (; __cnt > 0; --__cnt)
+	    {
+	      int __c = getc_unlocked (__stream);
+	      if (__c == EOF)
+		break;
+	      *__cptr++ = __c;
+	    }
+	  return (__cptr - (char *) __ptr) / __size;
 	}
-      return (__cptr - (char *) __ptr) / __size;
-    }
 # endif
-  return __fread_unlocked_alias (__ptr, __size, __n, __stream);
+      return __fread_unlocked_alias (__ptr, __size, __n, __stream);
+    }
+  if (__glibc_unsafe_len (__n, __size, sz))
+    return __fread_unlocked_chk_warn (__ptr, sz, __size, __n, __stream);
+  return __fread_unlocked_chk (__ptr, sz, __size, __n, __stream);
+
 }
 #endif
 
diff --git a/misc/sys/cdefs.h b/misc/sys/cdefs.h
index d08c2adfd0..d3dc7cfae1 100644
--- a/misc/sys/cdefs.h
+++ b/misc/sys/cdefs.h
@@ -151,6 +151,53 @@
 # define __glibc_objsize(__o) __bos (__o)
 #endif
 
+/* Compile time conditions to choose between the regular, _chk and _chk_warn
+   variants.  These conditions should get evaluated to constant and optimized
+   away.  */
+
+#define __glibc_safe_len_cond(__l, __s, __osz) ((__l) <= (__osz) / (__s))
+#define __glibc_unsigned_or_positive(__l) \
+  ((__typeof (__l)) 0 < (__typeof (__l)) -1				      \
+   || (__builtin_constant_p (__l) && (__l) > 0))
+
+/* Length is known to be safe at compile time if the __L * __S <= __OBJSZ
+   condition can be folded to a constant and if it is true.  The -1 check is
+   redundant because since it implies that __glibc_safe_len_cond is true.  */
+#define __glibc_safe_or_unknown_len(__l, __s, __osz) \
+  (__glibc_unsigned_or_positive (__l)					      \
+   && __builtin_constant_p (__glibc_safe_len_cond ((__SIZE_TYPE__) (__l),     \
+						   __s, __osz))		      \
+   && __glibc_safe_len_cond ((__SIZE_TYPE__) (__l), __s, __osz))
+
+/* Conversely, we know at compile time that the length is safe if the
+   __L * __S <= __OBJSZ condition can be folded to a constant and if it is
+   false.  */
+#define __glibc_unsafe_len(__l, __s, __osz) \
+  (__glibc_unsigned_or_positive (__l)					      \
+   && __builtin_constant_p (__glibc_safe_len_cond ((__SIZE_TYPE__) (__l),     \
+						   __s, __osz))		      \
+   && !__glibc_safe_len_cond ((__SIZE_TYPE__) (__l), __s, __osz))
+
+/* Fortify function f.  __f_alias, __f_chk and __f_chk_warn must be
+   declared.  */
+
+#define __glibc_fortify(f, __l, __s, __osz, ...) \
+  (__glibc_safe_or_unknown_len (__l, __s, __osz)			      \
+   ? __ ## f ## _alias (__VA_ARGS__)					      \
+   : (__glibc_unsafe_len (__l, __s, __osz)				      \
+      ? __ ## f ## _chk_warn (__VA_ARGS__, __osz)			      \
+      : __ ## f ## _chk (__VA_ARGS__, __osz)))			      \
+
+/* Fortify function f, where object size argument passed to f is the number of
+   elements and not total size.  */
+
+#define __glibc_fortify_n(f, __l, __s, __osz, ...) \
+  (__glibc_safe_or_unknown_len (__l, __s, __osz)			      \
+   ? __ ## f ## _alias (__VA_ARGS__)					      \
+   : (__glibc_unsafe_len (__l, __s, __osz)				      \
+      ? __ ## f ## _chk_warn (__VA_ARGS__, (__osz) / (__s))		      \
+      : __ ## f ## _chk (__VA_ARGS__, (__osz) / (__s))))		      \
+
 #if __GNUC_PREREQ (4,3)
 # define __warnattr(msg) __attribute__((__warning__ (msg)))
 # define __errordecl(name, msg) \
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
diff --git a/socket/bits/socket2.h b/socket/bits/socket2.h
index 9c8ac69624..b28cde55f3 100644
--- a/socket/bits/socket2.h
+++ b/socket/bits/socket2.h
@@ -33,17 +33,12 @@ extern ssize_t __REDIRECT (__recv_chk_warn,
 __fortify_function ssize_t
 recv (int __fd, void *__buf, size_t __n, int __flags)
 {
-  if (__glibc_objsize0 (__buf) != (size_t) -1)
-    {
-      if (!__builtin_constant_p (__n))
-	return __recv_chk (__fd, __buf, __n, __glibc_objsize0 (__buf),
-			   __flags);
-
-      if (__n > __glibc_objsize0 (__buf))
-	return __recv_chk_warn (__fd, __buf, __n, __glibc_objsize0 (__buf),
-				__flags);
-    }
-  return __recv_alias (__fd, __buf, __n, __flags);
+  size_t sz = __glibc_objsize0 (__buf);
+  if (__glibc_safe_or_unknown_len (__n, sizeof (char), sz))
+    return __recv_alias (__fd, __buf, __n, __flags);
+  if (__glibc_unsafe_len (__n, sizeof (char), sz))
+    return __recv_chk_warn (__fd, __buf, __n, sz, __flags);
+  return __recv_chk (__fd, __buf, __n, sz, __flags);
 }
 
 extern ssize_t __recvfrom_chk (int __fd, void *__restrict __buf, size_t __n,
@@ -66,14 +61,11 @@ __fortify_function ssize_t
 recvfrom (int __fd, void *__restrict __buf, size_t __n, int __flags,
 	  __SOCKADDR_ARG __addr, socklen_t *__restrict __addr_len)
 {
-  if (__glibc_objsize0 (__buf) != (size_t) -1)
-    {
-      if (!__builtin_constant_p (__n))
-	return __recvfrom_chk (__fd, __buf, __n, __glibc_objsize0 (__buf),
-			       __flags, __addr, __addr_len);
-      if (__n > __glibc_objsize0 (__buf))
-	return __recvfrom_chk_warn (__fd, __buf, __n, __glibc_objsize0 (__buf),
-				    __flags, __addr, __addr_len);
-    }
-  return __recvfrom_alias (__fd, __buf, __n, __flags, __addr, __addr_len);
+  size_t sz = __glibc_objsize0 (__buf);
+  if (__glibc_safe_or_unknown_len (__n, sizeof (char), sz))
+    return __recvfrom_alias (__fd, __buf, __n, __flags, __addr, __addr_len);
+  if (__glibc_unsafe_len (__n, sizeof (char), sz))
+    return __recvfrom_chk_warn (__fd, __buf, __n, sz, __flags, __addr,
+				__addr_len);
+  return __recvfrom_chk (__fd, __buf, __n, sz, __flags, __addr, __addr_len);
 }
diff --git a/stdlib/bits/stdlib.h b/stdlib/bits/stdlib.h
index eae31b38f0..067115eeca 100644
--- a/stdlib/bits/stdlib.h
+++ b/stdlib/bits/stdlib.h
@@ -36,17 +36,16 @@ extern char *__REDIRECT_NTH (__realpath_chk_warn,
 __fortify_function __wur char *
 __NTH (realpath (const char *__restrict __name, char *__restrict __resolved))
 {
-  if (__glibc_objsize (__resolved) != (size_t) -1)
-    {
+  size_t sz = __glibc_objsize (__resolved);
+
+  if (sz == (size_t) -1)
+    return __realpath_alias (__name, __resolved);
+
 #if defined _LIBC_LIMITS_H_ && defined PATH_MAX
-      if (__glibc_objsize (__resolved) < PATH_MAX)
-	return __realpath_chk_warn (__name, __resolved,
-				    __glibc_objsize (__resolved));
+  if (__glibc_unsafe_len (sz, sizeof (char), PATH_MAX))
+    return __realpath_chk_warn (__name, __resolved, sz);
 #endif
-      return __realpath_chk (__name, __resolved, __glibc_objsize (__resolved));
-    }
-
-  return __realpath_alias (__name, __resolved);
+  return __realpath_chk (__name, __resolved, sz);
 }
 
 
@@ -65,16 +64,9 @@ extern int __REDIRECT_NTH (__ptsname_r_chk_warn,
 __fortify_function int
 __NTH (ptsname_r (int __fd, char *__buf, size_t __buflen))
 {
-  if (__glibc_objsize (__buf) != (size_t) -1)
-    {
-      if (!__builtin_constant_p (__buflen))
-	return __ptsname_r_chk (__fd, __buf, __buflen,
-				__glibc_objsize (__buf));
-      if (__buflen > __glibc_objsize (__buf))
-	return __ptsname_r_chk_warn (__fd, __buf, __buflen,
-				     __glibc_objsize (__buf));
-    }
-  return __ptsname_r_alias (__fd, __buf, __buflen);
+  return __glibc_fortify (ptsname_r, __buflen, sizeof (char),
+			  __glibc_objsize (__buf),
+			  __fd, __buf, __buflen);
 }
 
 
@@ -120,18 +112,9 @@ __fortify_function size_t
 __NTH (mbstowcs (wchar_t *__restrict __dst, const char *__restrict __src,
 		 size_t __len))
 {
-  if (__glibc_objsize (__dst) != (size_t) -1)
-    {
-      if (!__builtin_constant_p (__len))
-	return __mbstowcs_chk (__dst, __src, __len,
-			       __glibc_objsize (__dst) / sizeof (wchar_t));
-
-      if (__len > __glibc_objsize (__dst) / sizeof (wchar_t))
-	return __mbstowcs_chk_warn (__dst, __src, __len,
-				    (__glibc_objsize (__dst)
-				     / sizeof (wchar_t)));
-    }
-  return __mbstowcs_alias (__dst, __src, __len);
+  return __glibc_fortify_n (mbstowcs, __len, sizeof (wchar_t),
+			    __glibc_objsize (__dst),
+			    __dst, __src, __len);
 }
 
 
@@ -154,13 +137,7 @@ __fortify_function size_t
 __NTH (wcstombs (char *__restrict __dst, const wchar_t *__restrict __src,
 		 size_t __len))
 {
-  if (__glibc_objsize (__dst) != (size_t) -1)
-    {
-      if (!__builtin_constant_p (__len))
-	return __wcstombs_chk (__dst, __src, __len, __glibc_objsize (__dst));
-      if (__len > __glibc_objsize (__dst))
-	return __wcstombs_chk_warn (__dst, __src, __len,
-				    __glibc_objsize (__dst));
-    }
-  return __wcstombs_alias (__dst, __src, __len);
+  return __glibc_fortify (wcstombs, __len, sizeof (char),
+			  __glibc_objsize (__dst),
+			  __dst, __src, __len);
 }
diff --git a/wcsmbs/bits/wchar2.h b/wcsmbs/bits/wchar2.h
index ea2518dc72..26012ef936 100644
--- a/wcsmbs/bits/wchar2.h
+++ b/wcsmbs/bits/wchar2.h
@@ -39,17 +39,9 @@ __fortify_function wchar_t *
 __NTH (wmemcpy (wchar_t *__restrict __s1, const wchar_t *__restrict __s2,
 		size_t __n))
 {
-  if (__glibc_objsize0 (__s1) != (size_t) -1)
-    {
-      if (!__builtin_constant_p (__n))
-	return __wmemcpy_chk (__s1, __s2, __n,
-			      __glibc_objsize0 (__s1) / sizeof (wchar_t));
-
-      if (__n > __glibc_objsize0 (__s1) / sizeof (wchar_t))
-	return __wmemcpy_chk_warn (__s1, __s2, __n,
-				   __glibc_objsize0 (__s1) / sizeof (wchar_t));
-    }
-  return __wmemcpy_alias (__s1, __s2, __n);
+  return __glibc_fortify_n (wmemcpy, __n, sizeof (wchar_t),
+			    __glibc_objsize0 (__s1),
+			    __s1, __s2, __n);
 }
 
 
@@ -67,18 +59,9 @@ extern wchar_t *__REDIRECT_NTH (__wmemmove_chk_warn,
 __fortify_function wchar_t *
 __NTH (wmemmove (wchar_t *__s1, const wchar_t *__s2, size_t __n))
 {
-  if (__glibc_objsize0 (__s1) != (size_t) -1)
-    {
-      if (!__builtin_constant_p (__n))
-	return __wmemmove_chk (__s1, __s2, __n,
-			       __glibc_objsize0 (__s1) / sizeof (wchar_t));
-
-      if (__n > __glibc_objsize0 (__s1) / sizeof (wchar_t))
-	return __wmemmove_chk_warn (__s1, __s2, __n,
-				    (__glibc_objsize0 (__s1)
-				     / sizeof (wchar_t)));
-    }
-  return __wmemmove_alias (__s1, __s2, __n);
+  return __glibc_fortify_n (wmemmove, __n, sizeof (wchar_t),
+			    __glibc_objsize0 (__s1),
+			    __s1, __s2, __n);
 }
 
 
@@ -101,18 +84,9 @@ __fortify_function wchar_t *
 __NTH (wmempcpy (wchar_t *__restrict __s1, const wchar_t *__restrict __s2,
 		 size_t __n))
 {
-  if (__glibc_objsize0 (__s1) != (size_t) -1)
-    {
-      if (!__builtin_constant_p (__n))
-	return __wmempcpy_chk (__s1, __s2, __n,
-			       __glibc_objsize0 (__s1) / sizeof (wchar_t));
-
-      if (__n > __glibc_objsize0 (__s1) / sizeof (wchar_t))
-	return __wmempcpy_chk_warn (__s1, __s2, __n,
-				    (__glibc_objsize0 (__s1)
-				     / sizeof (wchar_t)));
-    }
-  return __wmempcpy_alias (__s1, __s2, __n);
+  return __glibc_fortify_n (wmempcpy, __n, sizeof (wchar_t),
+			    __glibc_objsize0 (__s1),
+			    __s1, __s2, __n);
 }
 #endif
 
@@ -130,17 +104,9 @@ extern wchar_t *__REDIRECT_NTH (__wmemset_chk_warn,
 __fortify_function wchar_t *
 __NTH (wmemset (wchar_t *__s, wchar_t __c, size_t __n))
 {
-  if (__glibc_objsize0 (__s) != (size_t) -1)
-    {
-      if (!__builtin_constant_p (__n))
-	return __wmemset_chk (__s, __c, __n,
-			      __glibc_objsize0 (__s) / sizeof (wchar_t));
-
-      if (__n > __glibc_objsize0 (__s) / sizeof (wchar_t))
-	return __wmemset_chk_warn (__s, __c, __n,
-				   __glibc_objsize0 (__s) / sizeof (wchar_t));
-    }
-  return __wmemset_alias (__s, __c, __n);
+  return __glibc_fortify_n (wmemset, __n, sizeof (wchar_t),
+			    __glibc_objsize0 (__s),
+			    __s, __c, __n);
 }
 
 
@@ -154,9 +120,9 @@ extern wchar_t *__REDIRECT_NTH (__wcscpy_alias,
 __fortify_function wchar_t *
 __NTH (wcscpy (wchar_t *__restrict __dest, const wchar_t *__restrict __src))
 {
-  if (__glibc_objsize (__dest) != (size_t) -1)
-    return __wcscpy_chk (__dest, __src,
-			 __glibc_objsize (__dest) / sizeof (wchar_t));
+  size_t sz = __glibc_objsize (__dest);
+  if (sz != (size_t) -1)
+    return __wcscpy_chk (__dest, __src, sz / sizeof (wchar_t));
   return __wcscpy_alias (__dest, __src);
 }
 
@@ -171,9 +137,9 @@ extern wchar_t *__REDIRECT_NTH (__wcpcpy_alias,
 __fortify_function wchar_t *
 __NTH (wcpcpy (wchar_t *__restrict __dest, const wchar_t *__restrict __src))
 {
-  if (__glibc_objsize (__dest) != (size_t) -1)
-    return __wcpcpy_chk (__dest, __src,
-			 __glibc_objsize (__dest) / sizeof (wchar_t));
+  size_t sz = __glibc_objsize (__dest);
+  if (sz != (size_t) -1)
+    return __wcpcpy_chk (__dest, __src, sz / sizeof (wchar_t));
   return __wcpcpy_alias (__dest, __src);
 }
 
@@ -196,17 +162,9 @@ __fortify_function wchar_t *
 __NTH (wcsncpy (wchar_t *__restrict __dest, const wchar_t *__restrict __src,
 		size_t __n))
 {
-  if (__glibc_objsize (__dest) != (size_t) -1)
-    {
-      if (!__builtin_constant_p (__n))
-	return __wcsncpy_chk (__dest, __src, __n,
-			      __glibc_objsize (__dest) / sizeof (wchar_t));
-      if (__n > __glibc_objsize (__dest) / sizeof (wchar_t))
-	return __wcsncpy_chk_warn (__dest, __src, __n,
-				   (__glibc_objsize (__dest)
-				    / sizeof (wchar_t)));
-    }
-  return __wcsncpy_alias (__dest, __src, __n);
+  return __glibc_fortify_n (wcsncpy, __n, sizeof (wchar_t),
+			    __glibc_objsize (__dest),
+			    __dest, __src, __n);
 }
 
 
@@ -228,17 +186,9 @@ __fortify_function wchar_t *
 __NTH (wcpncpy (wchar_t *__restrict __dest, const wchar_t *__restrict __src,
 		size_t __n))
 {
-  if (__glibc_objsize (__dest) != (size_t) -1)
-    {
-      if (!__builtin_constant_p (__n))
-	return __wcpncpy_chk (__dest, __src, __n,
-			      __glibc_objsize (__dest) / sizeof (wchar_t));
-      if (__n > __glibc_objsize (__dest) / sizeof (wchar_t))
-	return __wcpncpy_chk_warn (__dest, __src, __n,
-				   (__glibc_objsize (__dest)
-				    / sizeof (wchar_t)));
-    }
-  return __wcpncpy_alias (__dest, __src, __n);
+  return __glibc_fortify_n (wcpncpy, __n, sizeof (wchar_t),
+			    __glibc_objsize (__dest),
+			    __dest, __src, __n);
 }
 
 
@@ -252,9 +202,9 @@ extern wchar_t *__REDIRECT_NTH (__wcscat_alias,
 __fortify_function wchar_t *
 __NTH (wcscat (wchar_t *__restrict __dest, const wchar_t *__restrict __src))
 {
-  if (__glibc_objsize (__dest) != (size_t) -1)
-    return __wcscat_chk (__dest, __src,
-			 __glibc_objsize (__dest) / sizeof (wchar_t));
+  size_t sz = __glibc_objsize (__dest);
+  if (sz != (size_t) -1)
+    return __wcscat_chk (__dest, __src, sz / sizeof (wchar_t));
   return __wcscat_alias (__dest, __src);
 }
 
@@ -271,9 +221,9 @@ __fortify_function wchar_t *
 __NTH (wcsncat (wchar_t *__restrict __dest, const wchar_t *__restrict __src,
 		size_t __n))
 {
-  if (__glibc_objsize (__dest) != (size_t) -1)
-    return __wcsncat_chk (__dest, __src, __n,
-			  __glibc_objsize (__dest) / sizeof (wchar_t));
+  size_t sz = __glibc_objsize (__dest);
+  if (sz != (size_t) -1)
+    return __wcsncat_chk (__dest, __src, __n, sz / sizeof (wchar_t));
   return __wcsncat_alias (__dest, __src, __n);
 }
 
@@ -293,10 +243,10 @@ __fortify_function int
 __NTH (swprintf (wchar_t *__restrict __s, size_t __n,
 		 const wchar_t *__restrict __fmt, ...))
 {
-  if (__glibc_objsize (__s) != (size_t) -1 || __USE_FORTIFY_LEVEL > 1)
+  size_t sz = __glibc_objsize (__s);
+  if (sz != (size_t) -1 || __USE_FORTIFY_LEVEL > 1)
     return __swprintf_chk (__s, __n, __USE_FORTIFY_LEVEL - 1,
-			   __glibc_objsize (__s) / sizeof (wchar_t),
-			   __fmt, __va_arg_pack ());
+			   sz / sizeof (wchar_t), __fmt, __va_arg_pack ());
   return __swprintf_alias (__s, __n, __fmt, __va_arg_pack ());
 }
 #elif !defined __cplusplus
@@ -323,10 +273,10 @@ __fortify_function int
 __NTH (vswprintf (wchar_t *__restrict __s, size_t __n,
 		  const wchar_t *__restrict __fmt, __gnuc_va_list __ap))
 {
-  if (__glibc_objsize (__s) != (size_t) -1 || __USE_FORTIFY_LEVEL > 1)
+  size_t sz = __glibc_objsize (__s);
+  if (sz != (size_t) -1 || __USE_FORTIFY_LEVEL > 1)
     return __vswprintf_chk (__s, __n,  __USE_FORTIFY_LEVEL - 1,
-			    __glibc_objsize (__s) / sizeof (wchar_t), __fmt,
-			    __ap);
+			    sz / sizeof (wchar_t), __fmt, __ap);
   return __vswprintf_alias (__s, __n, __fmt, __ap);
 }
 
@@ -392,18 +342,12 @@ extern wchar_t *__REDIRECT (__fgetws_chk_warn,
 __fortify_function __wur wchar_t *
 fgetws (wchar_t *__restrict __s, int __n, __FILE *__restrict __stream)
 {
-  if (__glibc_objsize (__s) != (size_t) -1)
-    {
-      if (!__builtin_constant_p (__n) || __n <= 0)
-	return __fgetws_chk (__s, __glibc_objsize (__s) / sizeof (wchar_t),
-			     __n, __stream);
-
-      if ((size_t) __n > __glibc_objsize (__s) / sizeof (wchar_t))
-	return __fgetws_chk_warn (__s,
-				  __glibc_objsize (__s) / sizeof (wchar_t),
-				  __n, __stream);
-    }
-  return __fgetws_alias (__s, __n, __stream);
+  size_t sz = __glibc_objsize (__s);
+  if (__glibc_safe_or_unknown_len (__n, sizeof (wchar_t), sz))
+    return __fgetws_alias (__s, __n, __stream);
+  if (__glibc_unsafe_len (__n, sizeof (wchar_t), sz))
+    return __fgetws_chk_warn (__s, sz / sizeof (wchar_t), __n, __stream);
+  return __fgetws_chk (__s, sz / sizeof (wchar_t), __n, __stream);
 }
 
 #ifdef __USE_GNU
@@ -424,20 +368,13 @@ extern wchar_t *__REDIRECT (__fgetws_unlocked_chk_warn,
 __fortify_function __wur wchar_t *
 fgetws_unlocked (wchar_t *__restrict __s, int __n, __FILE *__restrict __stream)
 {
-  if (__glibc_objsize (__s) != (size_t) -1)
-    {
-      if (!__builtin_constant_p (__n) || __n <= 0)
-	return __fgetws_unlocked_chk (__s,
-				      __glibc_objsize (__s) / sizeof (wchar_t),
-				      __n, __stream);
-
-      if ((size_t) __n > __glibc_objsize (__s) / sizeof (wchar_t))
-	return __fgetws_unlocked_chk_warn (__s,
-					   (__glibc_objsize (__s)
-					    / sizeof (wchar_t)),
-					   __n, __stream);
-    }
-  return __fgetws_unlocked_alias (__s, __n, __stream);
+  size_t sz = __glibc_objsize (__s);
+  if (__glibc_safe_or_unknown_len (__n, sizeof (wchar_t), sz))
+    return __fgetws_unlocked_alias (__s, __n, __stream);
+  if (__glibc_unsafe_len (__n, sizeof (wchar_t), sz))
+    return __fgetws_unlocked_chk_warn (__s, sz / sizeof (wchar_t), __n,
+				       __stream);
+  return __fgetws_unlocked_chk (__s, sz / sizeof (wchar_t), __n, __stream);
 }
 #endif
 
@@ -488,18 +425,9 @@ __fortify_function size_t
 __NTH (mbsrtowcs (wchar_t *__restrict __dst, const char **__restrict __src,
 		  size_t __len, mbstate_t *__restrict __ps))
 {
-  if (__glibc_objsize (__dst) != (size_t) -1)
-    {
-      if (!__builtin_constant_p (__len))
-	return __mbsrtowcs_chk (__dst, __src, __len, __ps,
-				__glibc_objsize (__dst) / sizeof (wchar_t));
-
-      if (__len > __glibc_objsize (__dst) / sizeof (wchar_t))
-	return __mbsrtowcs_chk_warn (__dst, __src, __len, __ps,
-				     (__glibc_objsize (__dst)
-				      / sizeof (wchar_t)));
-    }
-  return __mbsrtowcs_alias (__dst, __src, __len, __ps);
+  return __glibc_fortify_n (mbsrtowcs, __len, sizeof (wchar_t),
+			    __glibc_objsize (__dst),
+			    __dst, __src, __len, __ps);
 }
 
 
@@ -523,17 +451,9 @@ __fortify_function size_t
 __NTH (wcsrtombs (char *__restrict __dst, const wchar_t **__restrict __src,
 		  size_t __len, mbstate_t *__restrict __ps))
 {
-  if (__glibc_objsize (__dst) != (size_t) -1)
-    {
-      if (!__builtin_constant_p (__len))
-	return __wcsrtombs_chk (__dst, __src, __len, __ps,
-				__glibc_objsize (__dst));
-
-      if (__len > __glibc_objsize (__dst))
-	return __wcsrtombs_chk_warn (__dst, __src, __len, __ps,
-				     __glibc_objsize (__dst));
-    }
-  return __wcsrtombs_alias (__dst, __src, __len, __ps);
+  return __glibc_fortify (wcsrtombs, __len, sizeof (char),
+			  __glibc_objsize (__dst),
+			  __dst, __src, __len, __ps);
 }
 
 
@@ -559,18 +479,9 @@ __fortify_function size_t
 __NTH (mbsnrtowcs (wchar_t *__restrict __dst, const char **__restrict __src,
 		   size_t __nmc, size_t __len, mbstate_t *__restrict __ps))
 {
-  if (__glibc_objsize (__dst) != (size_t) -1)
-    {
-      if (!__builtin_constant_p (__len))
-	return __mbsnrtowcs_chk (__dst, __src, __nmc, __len, __ps,
-				 __glibc_objsize (__dst) / sizeof (wchar_t));
-
-      if (__len > __glibc_objsize (__dst) / sizeof (wchar_t))
-	return __mbsnrtowcs_chk_warn (__dst, __src, __nmc, __len, __ps,
-				      (__glibc_objsize (__dst)
-				       / sizeof (wchar_t)));
-    }
-  return __mbsnrtowcs_alias (__dst, __src, __nmc, __len, __ps);
+  return __glibc_fortify_n (mbsnrtowcs, __len, sizeof (wchar_t),
+			    __glibc_objsize (__dst),
+			    __dst, __src, __nmc, __len, __ps);
 }
 
 
@@ -596,16 +507,8 @@ __fortify_function size_t
 __NTH (wcsnrtombs (char *__restrict __dst, const wchar_t **__restrict __src,
 		   size_t __nwc, size_t __len, mbstate_t *__restrict __ps))
 {
-  if (__glibc_objsize (__dst) != (size_t) -1)
-    {
-      if (!__builtin_constant_p (__len))
-	return __wcsnrtombs_chk (__dst, __src, __nwc, __len, __ps,
-				 __glibc_objsize (__dst));
-
-      if (__len > __glibc_objsize (__dst))
-	return __wcsnrtombs_chk_warn (__dst, __src, __nwc, __len, __ps,
-				      __glibc_objsize (__dst));
-    }
-  return __wcsnrtombs_alias (__dst, __src, __nwc, __len, __ps);
+  return __glibc_fortify (wcsnrtombs, __len, sizeof (char),
+			  __glibc_objsize (__dst),
+			  __dst, __src, __nwc, __len, __ps);
 }
 #endif
-- 
2.31.1


^ permalink raw reply	[flat|nested] 17+ messages in thread

* [PATCH v2 2/2] debug: Add tests for _FORTIFY_SOURCE=3
  2021-10-20  5:24 ` [PATCH v2 0/2] " Siddhesh Poyarekar
  2021-10-20  5:24   ` [PATCH v2 1/2] Make sure that the fortified function conditionals are constant Siddhesh Poyarekar
@ 2021-10-20  5:24   ` Siddhesh Poyarekar
  2021-10-20 12:06     ` Adhemerval Zanella
  2021-10-20 14:28   ` [PATCH v2 0/2] _FORTIFY_SOURCE=3 improvements Siddhesh Poyarekar
  2 siblings, 1 reply; 17+ messages in thread
From: Siddhesh Poyarekar @ 2021-10-20  5:24 UTC (permalink / raw)
  To: libc-alpha

Add some testing coverage for _FORTIFY_SOURCE=3.

Signed-off-by: Siddhesh Poyarekar <siddhesh@sourceware.org>
---
 debug/Makefile    |  13 ++++--
 debug/tst-chk1.c  | 102 +++++++++++++++++++++++++---------------------
 debug/tst-chk7.c  |   2 +
 debug/tst-chk8.cc |   2 +
 4 files changed, 69 insertions(+), 50 deletions(-)
 create mode 100644 debug/tst-chk7.c
 create mode 100644 debug/tst-chk8.cc

diff --git a/debug/Makefile b/debug/Makefile
index 6893111cbf..357f888246 100644
--- a/debug/Makefile
+++ b/debug/Makefile
@@ -120,6 +120,8 @@ CFLAGS-tst-chk3.c += -Wno-format -Wno-deprecated-declarations -Wno-error
 CFLAGS-tst-chk4.cc += -Wno-format -Wno-deprecated-declarations -Wno-error
 CFLAGS-tst-chk5.cc += -Wno-format -Wno-deprecated-declarations -Wno-error
 CFLAGS-tst-chk6.cc += -Wno-format -Wno-deprecated-declarations -Wno-error
+CFLAGS-tst-chk7.c += -Wno-format -Wno-deprecated-declarations -Wno-error
+CFLAGS-tst-chk8.cc += -Wno-format -Wno-deprecated-declarations -Wno-error
 CFLAGS-tst-lfschk1.c += -Wno-format -Wno-deprecated-declarations -Wno-error
 CFLAGS-tst-lfschk2.c += -Wno-format -Wno-deprecated-declarations -Wno-error
 CFLAGS-tst-lfschk3.c += -Wno-format -Wno-deprecated-declarations -Wno-error
@@ -129,6 +131,7 @@ CFLAGS-tst-lfschk6.cc += -Wno-format -Wno-deprecated-declarations -Wno-error
 LDLIBS-tst-chk4 = -lstdc++
 LDLIBS-tst-chk5 = -lstdc++
 LDLIBS-tst-chk6 = -lstdc++
+LDLIBS-tst-chk8 = -lstdc++
 LDLIBS-tst-lfschk4 = -lstdc++
 LDLIBS-tst-lfschk5 = -lstdc++
 LDLIBS-tst-lfschk6 = -lstdc++
@@ -150,16 +153,16 @@ CFLAGS-tst-ssp-1.c += -fstack-protector-all
 
 tests = backtrace-tst tst-longjmp_chk tst-chk1 tst-chk2 tst-chk3 \
 	tst-lfschk1 tst-lfschk2 tst-lfschk3 test-strcpy_chk test-stpcpy_chk \
-	tst-chk4 tst-chk5 tst-chk6 tst-lfschk4 tst-lfschk5 tst-lfschk6 \
-	tst-longjmp_chk2 tst-backtrace2 tst-backtrace3 tst-backtrace4 \
-	tst-backtrace5 tst-backtrace6
+	tst-chk4 tst-chk5 tst-chk6 tst-chk7 tst-chk8 tst-lfschk4 tst-lfschk5 \
+	tst-lfschk6 tst-longjmp_chk2 tst-backtrace2 tst-backtrace3 \
+	tst-backtrace4 tst-backtrace5 tst-backtrace6
 
 ifeq ($(have-ssp),yes)
 tests += tst-ssp-1
 endif
 
 ifeq (,$(CXX))
-tests-unsupported = tst-chk4 tst-chk5 tst-chk6 \
+tests-unsupported = tst-chk4 tst-chk5 tst-chk6 tst-chk8 \
 		    tst-lfschk4 tst-lfschk5 tst-lfschk6
 endif
 
@@ -193,6 +196,8 @@ $(objpfx)tst-chk3.out: $(gen-locales)
 $(objpfx)tst-chk4.out: $(gen-locales)
 $(objpfx)tst-chk5.out: $(gen-locales)
 $(objpfx)tst-chk6.out: $(gen-locales)
+$(objpfx)tst-chk7.out: $(gen-locales)
+$(objpfx)tst-chk8.out: $(gen-locales)
 $(objpfx)tst-lfschk1.out: $(gen-locales)
 $(objpfx)tst-lfschk2.out: $(gen-locales)
 $(objpfx)tst-lfschk3.out: $(gen-locales)
diff --git a/debug/tst-chk1.c b/debug/tst-chk1.c
index 6712770201..d11e536f87 100644
--- a/debug/tst-chk1.c
+++ b/debug/tst-chk1.c
@@ -82,8 +82,14 @@ handler (int sig)
     _exit (127);
 }
 
+#if __USE_FORTIFY_LEVEL == 3
+volatile size_t buf_size = 10;
+#else
 char buf[10];
 wchar_t wbuf[10];
+#define buf_size sizeof (buf)
+#endif
+
 volatile size_t l0;
 volatile char *p;
 volatile wchar_t *wp;
@@ -122,6 +128,10 @@ int num2 = 987654;
 static int
 do_test (void)
 {
+#if __USE_FORTIFY_LEVEL == 3
+  char *buf = (char *) malloc (buf_size);
+  wchar_t *wbuf = (wchar_t *) malloc (buf_size * sizeof (wchar_t));
+#endif
   set_fortify_handler (handler);
 
   struct A { char buf1[9]; char buf2[1]; } a;
@@ -946,93 +956,93 @@ do_test (void)
 
   rewind (stdin);
 
-  if (fgets (buf, sizeof (buf), stdin) != buf
+  if (fgets (buf, buf_size, stdin) != buf
       || memcmp (buf, "abcdefgh\n", 10))
     FAIL ();
-  if (fgets (buf, sizeof (buf), stdin) != buf || memcmp (buf, "ABCDEFGHI", 10))
+  if (fgets (buf, buf_size, stdin) != buf || memcmp (buf, "ABCDEFGHI", 10))
     FAIL ();
 
   rewind (stdin);
 
-  if (fgets (buf, l0 + sizeof (buf), stdin) != buf
+  if (fgets (buf, l0 + buf_size, stdin) != buf
       || memcmp (buf, "abcdefgh\n", 10))
     FAIL ();
 
 #if __USE_FORTIFY_LEVEL >= 1
   CHK_FAIL_START
-  if (fgets (buf, sizeof (buf) + 1, stdin) != buf)
+  if (fgets (buf, buf_size + 1, stdin) != buf)
     FAIL ();
   CHK_FAIL_END
 
   CHK_FAIL_START
-  if (fgets (buf, l0 + sizeof (buf) + 1, stdin) != buf)
+  if (fgets (buf, l0 + buf_size + 1, stdin) != buf)
     FAIL ();
   CHK_FAIL_END
 #endif
 
   rewind (stdin);
 
-  if (fgets_unlocked (buf, sizeof (buf), stdin) != buf
+  if (fgets_unlocked (buf, buf_size, stdin) != buf
       || memcmp (buf, "abcdefgh\n", 10))
     FAIL ();
-  if (fgets_unlocked (buf, sizeof (buf), stdin) != buf
+  if (fgets_unlocked (buf, buf_size, stdin) != buf
       || memcmp (buf, "ABCDEFGHI", 10))
     FAIL ();
 
   rewind (stdin);
 
-  if (fgets_unlocked (buf, l0 + sizeof (buf), stdin) != buf
+  if (fgets_unlocked (buf, l0 + buf_size, stdin) != buf
       || memcmp (buf, "abcdefgh\n", 10))
     FAIL ();
 
 #if __USE_FORTIFY_LEVEL >= 1
   CHK_FAIL_START
-  if (fgets_unlocked (buf, sizeof (buf) + 1, stdin) != buf)
+  if (fgets_unlocked (buf, buf_size + 1, stdin) != buf)
     FAIL ();
   CHK_FAIL_END
 
   CHK_FAIL_START
-  if (fgets_unlocked (buf, l0 + sizeof (buf) + 1, stdin) != buf)
+  if (fgets_unlocked (buf, l0 + buf_size + 1, stdin) != buf)
     FAIL ();
   CHK_FAIL_END
 #endif
 
   rewind (stdin);
 
-  if (fread (buf, 1, sizeof (buf), stdin) != sizeof (buf)
+  if (fread (buf, 1, buf_size, stdin) != buf_size
       || memcmp (buf, "abcdefgh\nA", 10))
     FAIL ();
-  if (fread (buf, sizeof (buf), 1, stdin) != 1
+  if (fread (buf, buf_size, 1, stdin) != 1
       || memcmp (buf, "BCDEFGHI\na", 10))
     FAIL ();
 
   rewind (stdin);
 
-  if (fread (buf, l0 + 1, sizeof (buf), stdin) != sizeof (buf)
+  if (fread (buf, l0 + 1, buf_size, stdin) != buf_size
       || memcmp (buf, "abcdefgh\nA", 10))
     FAIL ();
-  if (fread (buf, sizeof (buf), l0 + 1, stdin) != 1
+  if (fread (buf, buf_size, l0 + 1, stdin) != 1
       || memcmp (buf, "BCDEFGHI\na", 10))
     FAIL ();
 
 #if __USE_FORTIFY_LEVEL >= 1
   CHK_FAIL_START
-  if (fread (buf, 1, sizeof (buf) + 1, stdin) != sizeof (buf) + 1)
+  if (fread (buf, 1, buf_size + 1, stdin) != buf_size + 1)
     FAIL ();
   CHK_FAIL_END
 
   CHK_FAIL_START
-  if (fread (buf, sizeof (buf) + 1, l0 + 1, stdin) != 1)
+  if (fread (buf, buf_size + 1, l0 + 1, stdin) != 1)
     FAIL ();
   CHK_FAIL_END
 #endif
 
   rewind (stdin);
 
-  if (fread_unlocked (buf, 1, sizeof (buf), stdin) != sizeof (buf)
+  if (fread_unlocked (buf, 1, buf_size, stdin) != buf_size
       || memcmp (buf, "abcdefgh\nA", 10))
     FAIL ();
-  if (fread_unlocked (buf, sizeof (buf), 1, stdin) != 1
+  if (fread_unlocked (buf, buf_size, 1, stdin) != 1
       || memcmp (buf, "BCDEFGHI\na", 10))
     FAIL ();
 
@@ -1047,100 +1057,100 @@ do_test (void)
 
   rewind (stdin);
 
-  if (fread_unlocked (buf, l0 + 1, sizeof (buf), stdin) != sizeof (buf)
+  if (fread_unlocked (buf, l0 + 1, buf_size, stdin) != buf_size
       || memcmp (buf, "abcdefgh\nA", 10))
     FAIL ();
-  if (fread_unlocked (buf, sizeof (buf), l0 + 1, stdin) != 1
+  if (fread_unlocked (buf, buf_size, l0 + 1, stdin) != 1
       || memcmp (buf, "BCDEFGHI\na", 10))
     FAIL ();
 
 #if __USE_FORTIFY_LEVEL >= 1
   CHK_FAIL_START
-  if (fread_unlocked (buf, 1, sizeof (buf) + 1, stdin) != sizeof (buf) + 1)
+  if (fread_unlocked (buf, 1, buf_size + 1, stdin) != buf_size + 1)
     FAIL ();
   CHK_FAIL_END
 
   CHK_FAIL_START
-  if (fread_unlocked (buf, sizeof (buf) + 1, l0 + 1, stdin) != 1)
+  if (fread_unlocked (buf, buf_size + 1, l0 + 1, stdin) != 1)
     FAIL ();
   CHK_FAIL_END
 #endif
 
   lseek (fileno (stdin), 0, SEEK_SET);
 
-  if (read (fileno (stdin), buf, sizeof (buf) - 1) != sizeof (buf) - 1
+  if (read (fileno (stdin), buf, buf_size - 1) != buf_size - 1
       || memcmp (buf, "abcdefgh\n", 9))
     FAIL ();
-  if (read (fileno (stdin), buf, sizeof (buf) - 1) != sizeof (buf) - 1
+  if (read (fileno (stdin), buf, buf_size - 1) != buf_size - 1
       || memcmp (buf, "ABCDEFGHI", 9))
     FAIL ();
 
   lseek (fileno (stdin), 0, SEEK_SET);
 
-  if (read (fileno (stdin), buf, l0 + sizeof (buf) - 1) != sizeof (buf) - 1
+  if (read (fileno (stdin), buf, l0 + buf_size - 1) != buf_size - 1
       || memcmp (buf, "abcdefgh\n", 9))
     FAIL ();
 
 #if __USE_FORTIFY_LEVEL >= 1
   CHK_FAIL_START
-  if (read (fileno (stdin), buf, sizeof (buf) + 1) != sizeof (buf) + 1)
+  if (read (fileno (stdin), buf, buf_size + 1) != buf_size + 1)
     FAIL ();
   CHK_FAIL_END
 
   CHK_FAIL_START
-  if (read (fileno (stdin), buf, l0 + sizeof (buf) + 1) != sizeof (buf) + 1)
+  if (read (fileno (stdin), buf, l0 + buf_size + 1) != buf_size + 1)
     FAIL ();
   CHK_FAIL_END
 #endif
 
-  if (pread (fileno (stdin), buf, sizeof (buf) - 1, sizeof (buf) - 2)
-      != sizeof (buf) - 1
+  if (pread (fileno (stdin), buf, buf_size - 1, buf_size - 2)
+      != buf_size - 1
       || memcmp (buf, "\nABCDEFGH", 9))
     FAIL ();
-  if (pread (fileno (stdin), buf, sizeof (buf) - 1, 0) != sizeof (buf) - 1
+  if (pread (fileno (stdin), buf, buf_size - 1, 0) != buf_size - 1
       || memcmp (buf, "abcdefgh\n", 9))
     FAIL ();
-  if (pread (fileno (stdin), buf, l0 + sizeof (buf) - 1, sizeof (buf) - 3)
-      != sizeof (buf) - 1
+  if (pread (fileno (stdin), buf, l0 + buf_size - 1, buf_size - 3)
+      != buf_size - 1
       || memcmp (buf, "h\nABCDEFG", 9))
     FAIL ();
 
 #if __USE_FORTIFY_LEVEL >= 1
   CHK_FAIL_START
-  if (pread (fileno (stdin), buf, sizeof (buf) + 1, 2 * sizeof (buf))
-      != sizeof (buf) + 1)
+  if (pread (fileno (stdin), buf, buf_size + 1, 2 * buf_size)
+      != buf_size + 1)
     FAIL ();
   CHK_FAIL_END
 
   CHK_FAIL_START
-  if (pread (fileno (stdin), buf, l0 + sizeof (buf) + 1, 2 * sizeof (buf))
-      != sizeof (buf) + 1)
+  if (pread (fileno (stdin), buf, l0 + buf_size + 1, 2 * buf_size)
+      != buf_size + 1)
     FAIL ();
   CHK_FAIL_END
 #endif
 
-  if (pread64 (fileno (stdin), buf, sizeof (buf) - 1, sizeof (buf) - 2)
-      != sizeof (buf) - 1
+  if (pread64 (fileno (stdin), buf, buf_size - 1, buf_size - 2)
+      != buf_size - 1
       || memcmp (buf, "\nABCDEFGH", 9))
     FAIL ();
-  if (pread64 (fileno (stdin), buf, sizeof (buf) - 1, 0) != sizeof (buf) - 1
+  if (pread64 (fileno (stdin), buf, buf_size - 1, 0) != buf_size - 1
       || memcmp (buf, "abcdefgh\n", 9))
     FAIL ();
-  if (pread64 (fileno (stdin), buf, l0 + sizeof (buf) - 1, sizeof (buf) - 3)
-      != sizeof (buf) - 1
+  if (pread64 (fileno (stdin), buf, l0 + buf_size - 1, buf_size - 3)
+      != buf_size - 1
       || memcmp (buf, "h\nABCDEFG", 9))
     FAIL ();
 
 #if __USE_FORTIFY_LEVEL >= 1
   CHK_FAIL_START
-  if (pread64 (fileno (stdin), buf, sizeof (buf) + 1, 2 * sizeof (buf))
-      != sizeof (buf) + 1)
+  if (pread64 (fileno (stdin), buf, buf_size + 1, 2 * buf_size)
+      != buf_size + 1)
     FAIL ();
   CHK_FAIL_END
 
   CHK_FAIL_START
-  if (pread64 (fileno (stdin), buf, l0 + sizeof (buf) + 1, 2 * sizeof (buf))
-      != sizeof (buf) + 1)
+  if (pread64 (fileno (stdin), buf, l0 + buf_size + 1, 2 * buf_size)
+      != buf_size + 1)
     FAIL ();
   CHK_FAIL_END
 #endif
@@ -1178,7 +1188,7 @@ do_test (void)
   CHK_FAIL2_END
 
   CHK_FAIL2_START
-  snprintf (buf, sizeof (buf), "%3$d\n", 1, 2, 3, 4);
+  snprintf (buf, buf_size, "%3$d\n", 1, 2, 3, 4);
   CHK_FAIL2_END
 
   int sp[2];
diff --git a/debug/tst-chk7.c b/debug/tst-chk7.c
new file mode 100644
index 0000000000..2a7b323812
--- /dev/null
+++ b/debug/tst-chk7.c
@@ -0,0 +1,2 @@
+#define _FORTIFY_SOURCE 3
+#include "tst-chk1.c"
diff --git a/debug/tst-chk8.cc b/debug/tst-chk8.cc
new file mode 100644
index 0000000000..2a7b323812
--- /dev/null
+++ b/debug/tst-chk8.cc
@@ -0,0 +1,2 @@
+#define _FORTIFY_SOURCE 3
+#include "tst-chk1.c"
-- 
2.31.1


^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: [PATCH v2 1/2] Make sure that the fortified function conditionals are constant
  2021-10-20  5:24   ` [PATCH v2 1/2] Make sure that the fortified function conditionals are constant Siddhesh Poyarekar
@ 2021-10-20 12:00     ` Adhemerval Zanella
  0 siblings, 0 replies; 17+ messages in thread
From: Adhemerval Zanella @ 2021-10-20 12:00 UTC (permalink / raw)
  To: Siddhesh Poyarekar, libc-alpha



On 20/10/2021 02:24, Siddhesh Poyarekar wrote:
> 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 <siddhesh@sourceware.org>

LGTM, thanks.

Reviewed-by: Adhemerval Zanella  <adhemerval.zanella@linaro.org>

> ---
>  io/bits/poll2.h       |  27 ++----
>  libio/bits/stdio2.h   | 106 +++++++++-----------
>  misc/sys/cdefs.h      |  47 +++++++++
>  posix/bits/unistd.h   | 174 ++++++++-------------------------
>  socket/bits/socket2.h |  34 +++----
>  stdlib/bits/stdlib.h  |  57 ++++-------
>  wcsmbs/bits/wchar2.h  | 219 ++++++++++++------------------------------
>  7 files changed, 226 insertions(+), 438 deletions(-)
> 
> diff --git a/io/bits/poll2.h b/io/bits/poll2.h
> index be74d020f2..91cdcaf66a 100644
> --- a/io/bits/poll2.h
> +++ b/io/bits/poll2.h
> @@ -36,16 +36,9 @@ extern int __REDIRECT (__poll_chk_warn, (struct pollfd *__fds, nfds_t __nfds,
>  __fortify_function __fortified_attr_access (__write_only__, 1, 2) int
>  poll (struct pollfd *__fds, nfds_t __nfds, int __timeout)
>  {
> -  if (__glibc_objsize (__fds) != (__SIZE_TYPE__) -1)
> -    {
> -      if (! __builtin_constant_p (__nfds))
> -	return __poll_chk (__fds, __nfds, __timeout, __glibc_objsize (__fds));
> -      else if (__glibc_objsize (__fds) / sizeof (*__fds) < __nfds)
> -	return __poll_chk_warn (__fds, __nfds, __timeout,
> -				__glibc_objsize (__fds));
> -    }
> -
> -  return __poll_alias (__fds, __nfds, __timeout);
> +  return __glibc_fortify (poll, __nfds, sizeof (*__fds),
> +			  __glibc_objsize (__fds),
> +			  __fds, __nfds, __timeout);
>  }
>  
>  
> @@ -68,17 +61,9 @@ __fortify_function __fortified_attr_access (__write_only__, 1, 2) int
>  ppoll (struct pollfd *__fds, nfds_t __nfds, const struct timespec *__timeout,
>         const __sigset_t *__ss)
>  {
> -  if (__glibc_objsize (__fds) != (__SIZE_TYPE__) -1)
> -    {
> -      if (! __builtin_constant_p (__nfds))
> -	return __ppoll_chk (__fds, __nfds, __timeout, __ss,
> -			    __glibc_objsize (__fds));
> -      else if (__glibc_objsize (__fds) / sizeof (*__fds) < __nfds)
> -	return __ppoll_chk_warn (__fds, __nfds, __timeout, __ss,
> -				 __glibc_objsize (__fds));
> -    }
> -
> -  return __ppoll_alias (__fds, __nfds, __timeout, __ss);
> +  return __glibc_fortify (ppoll, __nfds, sizeof (*__fds),
> +			  __glibc_objsize (__fds),
> +			  __fds, __nfds, __timeout, __ss);
>  }
>  #endif
>  

Ok.

> diff --git a/libio/bits/stdio2.h b/libio/bits/stdio2.h
> index 4f016a5638..40ff16b01b 100644
> --- a/libio/bits/stdio2.h
> +++ b/libio/bits/stdio2.h
> @@ -261,15 +261,12 @@ extern char *__REDIRECT (__fgets_chk_warn,
>  __fortify_function __wur __fortified_attr_access (__write_only__, 1, 2) char *
>  fgets (char *__restrict __s, int __n, FILE *__restrict __stream)
>  {
> -  if (__glibc_objsize (__s) != (size_t) -1)
> -    {
> -      if (!__builtin_constant_p (__n) || __n <= 0)
> -	return __fgets_chk (__s, __glibc_objsize (__s), __n, __stream);
> -
> -      if ((size_t) __n > __glibc_objsize (__s))
> -	return __fgets_chk_warn (__s, __glibc_objsize (__s), __n, __stream);
> -    }
> -  return __fgets_alias (__s, __n, __stream);
> +  size_t sz = __glibc_objsize (__s);
> +  if (__glibc_safe_or_unknown_len (__n, sizeof (char), sz))
> +    return __fgets_alias (__s, __n, __stream);
> +  if (__glibc_unsafe_len (__n, sizeof (char), sz))
> +    return __fgets_chk_warn (__s, sz, __n, __stream);
> +  return __fgets_chk (__s, sz, __n, __stream);
>  }
>  
>  extern size_t __fread_chk (void *__restrict __ptr, size_t __ptrlen,
> @@ -291,19 +288,12 @@ __fortify_function __wur size_t
>  fread (void *__restrict __ptr, size_t __size, size_t __n,
>         FILE *__restrict __stream)
>  {
> -  if (__glibc_objsize0 (__ptr) != (size_t) -1)
> -    {
> -      if (!__builtin_constant_p (__size)
> -	  || !__builtin_constant_p (__n)
> -	  || (__size | __n) >= (((size_t) 1) << (8 * sizeof (size_t) / 2)))
> -	return __fread_chk (__ptr, __glibc_objsize0 (__ptr), __size, __n,
> -			    __stream);
> -
> -      if (__size * __n > __glibc_objsize0 (__ptr))
> -	return __fread_chk_warn (__ptr, __glibc_objsize0 (__ptr), __size, __n,
> -				 __stream);
> -    }
> -  return __fread_alias (__ptr, __size, __n, __stream);
> +  size_t sz = __glibc_objsize0 (__ptr);
> +  if (__glibc_safe_or_unknown_len (__n, __size, sz))
> +    return __fread_alias (__ptr, __size, __n, __stream);
> +  if (__glibc_unsafe_len (__n, __size, sz))
> +    return __fread_chk_warn (__ptr, sz, __size, __n, __stream);
> +  return __fread_chk (__ptr, sz, __size, __n, __stream);
>  }
>  
>  #ifdef __USE_GNU
> @@ -323,17 +313,12 @@ extern char *__REDIRECT (__fgets_unlocked_chk_warn,
>  __fortify_function __wur __fortified_attr_access (__write_only__, 1, 2) char *
>  fgets_unlocked (char *__restrict __s, int __n, FILE *__restrict __stream)
>  {
> -  if (__glibc_objsize (__s) != (size_t) -1)
> -    {
> -      if (!__builtin_constant_p (__n) || __n <= 0)
> -	return __fgets_unlocked_chk (__s, __glibc_objsize (__s), __n,
> -				     __stream);
> -
> -      if ((size_t) __n > __glibc_objsize (__s))
> -	return __fgets_unlocked_chk_warn (__s, __glibc_objsize (__s), __n,
> -					  __stream);
> -    }
> -  return __fgets_unlocked_alias (__s, __n, __stream);
> +  size_t sz = __glibc_objsize (__s);
> +  if (__glibc_safe_or_unknown_len (__n, sizeof (char), sz))
> +    return __fgets_unlocked_alias (__s, __n, __stream);
> +  if (__glibc_unsafe_len (__n, sizeof (char), sz))
> +    return __fgets_unlocked_chk_warn (__s, sz, __n, __stream);
> +  return __fgets_unlocked_chk (__s, sz, __n, __stream);
>  }
>  #endif
>  
> @@ -358,41 +343,36 @@ __fortify_function __wur size_t
>  fread_unlocked (void *__restrict __ptr, size_t __size, size_t __n,
>  		FILE *__restrict __stream)
>  {
> -  if (__glibc_objsize0 (__ptr) != (size_t) -1)
> +  size_t sz = __glibc_objsize0 (__ptr);
> +  if (__glibc_safe_or_unknown_len (__n, __size, sz))
>      {
> -      if (!__builtin_constant_p (__size)
> -	  || !__builtin_constant_p (__n)
> -	  || (__size | __n) >= (((size_t) 1) << (8 * sizeof (size_t) / 2)))
> -	return __fread_unlocked_chk (__ptr, __glibc_objsize0 (__ptr), __size,
> -				     __n, __stream);
> -
> -      if (__size * __n > __glibc_objsize0 (__ptr))
> -	return __fread_unlocked_chk_warn (__ptr, __glibc_objsize0 (__ptr),
> -					  __size, __n, __stream);
> -    }
> -
>  # ifdef __USE_EXTERN_INLINES
> -  if (__builtin_constant_p (__size)
> -      && __builtin_constant_p (__n)
> -      && (__size | __n) < (((size_t) 1) << (8 * sizeof (size_t) / 2))
> -      && __size * __n <= 8)
> -    {
> -      size_t __cnt = __size * __n;
> -      char *__cptr = (char *) __ptr;
> -      if (__cnt == 0)
> -	return 0;
> -
> -      for (; __cnt > 0; --__cnt)
> +      if (__builtin_constant_p (__size)
> +	  && __builtin_constant_p (__n)
> +	  && (__size | __n) < (((size_t) 1) << (8 * sizeof (size_t) / 2))
> +	  && __size * __n <= 8)
>  	{
> -	  int __c = getc_unlocked (__stream);
> -	  if (__c == EOF)
> -	    break;
> -	  *__cptr++ = __c;
> +	  size_t __cnt = __size * __n;
> +	  char *__cptr = (char *) __ptr;
> +	  if (__cnt == 0)
> +	    return 0;
> +
> +	  for (; __cnt > 0; --__cnt)
> +	    {
> +	      int __c = getc_unlocked (__stream);
> +	      if (__c == EOF)
> +		break;
> +	      *__cptr++ = __c;
> +	    }
> +	  return (__cptr - (char *) __ptr) / __size;
>  	}
> -      return (__cptr - (char *) __ptr) / __size;
> -    }
>  # endif
> -  return __fread_unlocked_alias (__ptr, __size, __n, __stream);
> +      return __fread_unlocked_alias (__ptr, __size, __n, __stream);
> +    }
> +  if (__glibc_unsafe_len (__n, __size, sz))
> +    return __fread_unlocked_chk_warn (__ptr, sz, __size, __n, __stream);
> +  return __fread_unlocked_chk (__ptr, sz, __size, __n, __stream);
> +
>  }
>  #endif
>  

Ok.

> diff --git a/misc/sys/cdefs.h b/misc/sys/cdefs.h
> index d08c2adfd0..d3dc7cfae1 100644
> --- a/misc/sys/cdefs.h
> +++ b/misc/sys/cdefs.h
> @@ -151,6 +151,53 @@
>  # define __glibc_objsize(__o) __bos (__o)
>  #endif
>  
> +/* Compile time conditions to choose between the regular, _chk and _chk_warn
> +   variants.  These conditions should get evaluated to constant and optimized
> +   away.  */
> +
> +#define __glibc_safe_len_cond(__l, __s, __osz) ((__l) <= (__osz) / (__s))
> +#define __glibc_unsigned_or_positive(__l) \
> +  ((__typeof (__l)) 0 < (__typeof (__l)) -1				      \
> +   || (__builtin_constant_p (__l) && (__l) > 0))
> +
> +/* Length is known to be safe at compile time if the __L * __S <= __OBJSZ
> +   condition can be folded to a constant and if it is true.  The -1 check is
> +   redundant because since it implies that __glibc_safe_len_cond is true.  */
> +#define __glibc_safe_or_unknown_len(__l, __s, __osz) \
> +  (__glibc_unsigned_or_positive (__l)					      \
> +   && __builtin_constant_p (__glibc_safe_len_cond ((__SIZE_TYPE__) (__l),     \
> +						   __s, __osz))		      \
> +   && __glibc_safe_len_cond ((__SIZE_TYPE__) (__l), __s, __osz))
> +
> +/* Conversely, we know at compile time that the length is safe if the
> +   __L * __S <= __OBJSZ condition can be folded to a constant and if it is
> +   false.  */
> +#define __glibc_unsafe_len(__l, __s, __osz) \
> +  (__glibc_unsigned_or_positive (__l)					      \
> +   && __builtin_constant_p (__glibc_safe_len_cond ((__SIZE_TYPE__) (__l),     \
> +						   __s, __osz))		      \
> +   && !__glibc_safe_len_cond ((__SIZE_TYPE__) (__l), __s, __osz))
> +
> +/* Fortify function f.  __f_alias, __f_chk and __f_chk_warn must be
> +   declared.  */
> +
> +#define __glibc_fortify(f, __l, __s, __osz, ...) \
> +  (__glibc_safe_or_unknown_len (__l, __s, __osz)			      \
> +   ? __ ## f ## _alias (__VA_ARGS__)					      \
> +   : (__glibc_unsafe_len (__l, __s, __osz)				      \
> +      ? __ ## f ## _chk_warn (__VA_ARGS__, __osz)			      \
> +      : __ ## f ## _chk (__VA_ARGS__, __osz)))			      \
> +
> +/* Fortify function f, where object size argument passed to f is the number of
> +   elements and not total size.  */
> +
> +#define __glibc_fortify_n(f, __l, __s, __osz, ...) \
> +  (__glibc_safe_or_unknown_len (__l, __s, __osz)			      \
> +   ? __ ## f ## _alias (__VA_ARGS__)					      \
> +   : (__glibc_unsafe_len (__l, __s, __osz)				      \
> +      ? __ ## f ## _chk_warn (__VA_ARGS__, (__osz) / (__s))		      \
> +      : __ ## f ## _chk (__VA_ARGS__, (__osz) / (__s))))		      \
> +
>  #if __GNUC_PREREQ (4,3)
>  # define __warnattr(msg) __attribute__((__warning__ (msg)))
>  # define __errordecl(name, msg) \

Ok.

> 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

Ok.

> diff --git a/socket/bits/socket2.h b/socket/bits/socket2.h
> index 9c8ac69624..b28cde55f3 100644
> --- a/socket/bits/socket2.h
> +++ b/socket/bits/socket2.h
> @@ -33,17 +33,12 @@ extern ssize_t __REDIRECT (__recv_chk_warn,
>  __fortify_function ssize_t
>  recv (int __fd, void *__buf, size_t __n, int __flags)
>  {
> -  if (__glibc_objsize0 (__buf) != (size_t) -1)
> -    {
> -      if (!__builtin_constant_p (__n))
> -	return __recv_chk (__fd, __buf, __n, __glibc_objsize0 (__buf),
> -			   __flags);
> -
> -      if (__n > __glibc_objsize0 (__buf))
> -	return __recv_chk_warn (__fd, __buf, __n, __glibc_objsize0 (__buf),
> -				__flags);
> -    }
> -  return __recv_alias (__fd, __buf, __n, __flags);
> +  size_t sz = __glibc_objsize0 (__buf);
> +  if (__glibc_safe_or_unknown_len (__n, sizeof (char), sz))
> +    return __recv_alias (__fd, __buf, __n, __flags);
> +  if (__glibc_unsafe_len (__n, sizeof (char), sz))
> +    return __recv_chk_warn (__fd, __buf, __n, sz, __flags);
> +  return __recv_chk (__fd, __buf, __n, sz, __flags);
>  }
>  
>  extern ssize_t __recvfrom_chk (int __fd, void *__restrict __buf, size_t __n,
> @@ -66,14 +61,11 @@ __fortify_function ssize_t
>  recvfrom (int __fd, void *__restrict __buf, size_t __n, int __flags,
>  	  __SOCKADDR_ARG __addr, socklen_t *__restrict __addr_len)
>  {
> -  if (__glibc_objsize0 (__buf) != (size_t) -1)
> -    {
> -      if (!__builtin_constant_p (__n))
> -	return __recvfrom_chk (__fd, __buf, __n, __glibc_objsize0 (__buf),
> -			       __flags, __addr, __addr_len);
> -      if (__n > __glibc_objsize0 (__buf))
> -	return __recvfrom_chk_warn (__fd, __buf, __n, __glibc_objsize0 (__buf),
> -				    __flags, __addr, __addr_len);
> -    }
> -  return __recvfrom_alias (__fd, __buf, __n, __flags, __addr, __addr_len);
> +  size_t sz = __glibc_objsize0 (__buf);
> +  if (__glibc_safe_or_unknown_len (__n, sizeof (char), sz))
> +    return __recvfrom_alias (__fd, __buf, __n, __flags, __addr, __addr_len);
> +  if (__glibc_unsafe_len (__n, sizeof (char), sz))
> +    return __recvfrom_chk_warn (__fd, __buf, __n, sz, __flags, __addr,
> +				__addr_len);
> +  return __recvfrom_chk (__fd, __buf, __n, sz, __flags, __addr, __addr_len);
>  }

Ok.

> diff --git a/stdlib/bits/stdlib.h b/stdlib/bits/stdlib.h
> index eae31b38f0..067115eeca 100644
> --- a/stdlib/bits/stdlib.h
> +++ b/stdlib/bits/stdlib.h
> @@ -36,17 +36,16 @@ extern char *__REDIRECT_NTH (__realpath_chk_warn,
>  __fortify_function __wur char *
>  __NTH (realpath (const char *__restrict __name, char *__restrict __resolved))
>  {
> -  if (__glibc_objsize (__resolved) != (size_t) -1)
> -    {
> +  size_t sz = __glibc_objsize (__resolved);
> +
> +  if (sz == (size_t) -1)
> +    return __realpath_alias (__name, __resolved);
> +
>  #if defined _LIBC_LIMITS_H_ && defined PATH_MAX
> -      if (__glibc_objsize (__resolved) < PATH_MAX)
> -	return __realpath_chk_warn (__name, __resolved,
> -				    __glibc_objsize (__resolved));
> +  if (__glibc_unsafe_len (sz, sizeof (char), PATH_MAX))
> +    return __realpath_chk_warn (__name, __resolved, sz);
>  #endif
> -      return __realpath_chk (__name, __resolved, __glibc_objsize (__resolved));
> -    }
> -
> -  return __realpath_alias (__name, __resolved);
> +  return __realpath_chk (__name, __resolved, sz);
>  }
>  
>  
> @@ -65,16 +64,9 @@ extern int __REDIRECT_NTH (__ptsname_r_chk_warn,
>  __fortify_function int
>  __NTH (ptsname_r (int __fd, char *__buf, size_t __buflen))
>  {
> -  if (__glibc_objsize (__buf) != (size_t) -1)
> -    {
> -      if (!__builtin_constant_p (__buflen))
> -	return __ptsname_r_chk (__fd, __buf, __buflen,
> -				__glibc_objsize (__buf));
> -      if (__buflen > __glibc_objsize (__buf))
> -	return __ptsname_r_chk_warn (__fd, __buf, __buflen,
> -				     __glibc_objsize (__buf));
> -    }
> -  return __ptsname_r_alias (__fd, __buf, __buflen);
> +  return __glibc_fortify (ptsname_r, __buflen, sizeof (char),
> +			  __glibc_objsize (__buf),
> +			  __fd, __buf, __buflen);
>  }
>  
>  
> @@ -120,18 +112,9 @@ __fortify_function size_t
>  __NTH (mbstowcs (wchar_t *__restrict __dst, const char *__restrict __src,
>  		 size_t __len))
>  {
> -  if (__glibc_objsize (__dst) != (size_t) -1)
> -    {
> -      if (!__builtin_constant_p (__len))
> -	return __mbstowcs_chk (__dst, __src, __len,
> -			       __glibc_objsize (__dst) / sizeof (wchar_t));
> -
> -      if (__len > __glibc_objsize (__dst) / sizeof (wchar_t))
> -	return __mbstowcs_chk_warn (__dst, __src, __len,
> -				    (__glibc_objsize (__dst)
> -				     / sizeof (wchar_t)));
> -    }
> -  return __mbstowcs_alias (__dst, __src, __len);
> +  return __glibc_fortify_n (mbstowcs, __len, sizeof (wchar_t),
> +			    __glibc_objsize (__dst),
> +			    __dst, __src, __len);
>  }
>  
>  
> @@ -154,13 +137,7 @@ __fortify_function size_t
>  __NTH (wcstombs (char *__restrict __dst, const wchar_t *__restrict __src,
>  		 size_t __len))
>  {
> -  if (__glibc_objsize (__dst) != (size_t) -1)
> -    {
> -      if (!__builtin_constant_p (__len))
> -	return __wcstombs_chk (__dst, __src, __len, __glibc_objsize (__dst));
> -      if (__len > __glibc_objsize (__dst))
> -	return __wcstombs_chk_warn (__dst, __src, __len,
> -				    __glibc_objsize (__dst));
> -    }
> -  return __wcstombs_alias (__dst, __src, __len);
> +  return __glibc_fortify (wcstombs, __len, sizeof (char),
> +			  __glibc_objsize (__dst),
> +			  __dst, __src, __len);
>  }

Ok.

> diff --git a/wcsmbs/bits/wchar2.h b/wcsmbs/bits/wchar2.h
> index ea2518dc72..26012ef936 100644
> --- a/wcsmbs/bits/wchar2.h
> +++ b/wcsmbs/bits/wchar2.h
> @@ -39,17 +39,9 @@ __fortify_function wchar_t *
>  __NTH (wmemcpy (wchar_t *__restrict __s1, const wchar_t *__restrict __s2,
>  		size_t __n))
>  {
> -  if (__glibc_objsize0 (__s1) != (size_t) -1)
> -    {
> -      if (!__builtin_constant_p (__n))
> -	return __wmemcpy_chk (__s1, __s2, __n,
> -			      __glibc_objsize0 (__s1) / sizeof (wchar_t));
> -
> -      if (__n > __glibc_objsize0 (__s1) / sizeof (wchar_t))
> -	return __wmemcpy_chk_warn (__s1, __s2, __n,
> -				   __glibc_objsize0 (__s1) / sizeof (wchar_t));
> -    }
> -  return __wmemcpy_alias (__s1, __s2, __n);
> +  return __glibc_fortify_n (wmemcpy, __n, sizeof (wchar_t),
> +			    __glibc_objsize0 (__s1),
> +			    __s1, __s2, __n);
>  }
>  
>  
> @@ -67,18 +59,9 @@ extern wchar_t *__REDIRECT_NTH (__wmemmove_chk_warn,
>  __fortify_function wchar_t *
>  __NTH (wmemmove (wchar_t *__s1, const wchar_t *__s2, size_t __n))
>  {
> -  if (__glibc_objsize0 (__s1) != (size_t) -1)
> -    {
> -      if (!__builtin_constant_p (__n))
> -	return __wmemmove_chk (__s1, __s2, __n,
> -			       __glibc_objsize0 (__s1) / sizeof (wchar_t));
> -
> -      if (__n > __glibc_objsize0 (__s1) / sizeof (wchar_t))
> -	return __wmemmove_chk_warn (__s1, __s2, __n,
> -				    (__glibc_objsize0 (__s1)
> -				     / sizeof (wchar_t)));
> -    }
> -  return __wmemmove_alias (__s1, __s2, __n);
> +  return __glibc_fortify_n (wmemmove, __n, sizeof (wchar_t),
> +			    __glibc_objsize0 (__s1),
> +			    __s1, __s2, __n);
>  }
>  
>  
> @@ -101,18 +84,9 @@ __fortify_function wchar_t *
>  __NTH (wmempcpy (wchar_t *__restrict __s1, const wchar_t *__restrict __s2,
>  		 size_t __n))
>  {
> -  if (__glibc_objsize0 (__s1) != (size_t) -1)
> -    {
> -      if (!__builtin_constant_p (__n))
> -	return __wmempcpy_chk (__s1, __s2, __n,
> -			       __glibc_objsize0 (__s1) / sizeof (wchar_t));
> -
> -      if (__n > __glibc_objsize0 (__s1) / sizeof (wchar_t))
> -	return __wmempcpy_chk_warn (__s1, __s2, __n,
> -				    (__glibc_objsize0 (__s1)
> -				     / sizeof (wchar_t)));
> -    }
> -  return __wmempcpy_alias (__s1, __s2, __n);
> +  return __glibc_fortify_n (wmempcpy, __n, sizeof (wchar_t),
> +			    __glibc_objsize0 (__s1),
> +			    __s1, __s2, __n);
>  }
>  #endif
>  

Ok.

> @@ -130,17 +104,9 @@ extern wchar_t *__REDIRECT_NTH (__wmemset_chk_warn,
>  __fortify_function wchar_t *
>  __NTH (wmemset (wchar_t *__s, wchar_t __c, size_t __n))
>  {
> -  if (__glibc_objsize0 (__s) != (size_t) -1)
> -    {
> -      if (!__builtin_constant_p (__n))
> -	return __wmemset_chk (__s, __c, __n,
> -			      __glibc_objsize0 (__s) / sizeof (wchar_t));
> -
> -      if (__n > __glibc_objsize0 (__s) / sizeof (wchar_t))
> -	return __wmemset_chk_warn (__s, __c, __n,
> -				   __glibc_objsize0 (__s) / sizeof (wchar_t));
> -    }
> -  return __wmemset_alias (__s, __c, __n);
> +  return __glibc_fortify_n (wmemset, __n, sizeof (wchar_t),
> +			    __glibc_objsize0 (__s),
> +			    __s, __c, __n);
>  }
>  
>  
> @@ -154,9 +120,9 @@ extern wchar_t *__REDIRECT_NTH (__wcscpy_alias,
>  __fortify_function wchar_t *
>  __NTH (wcscpy (wchar_t *__restrict __dest, const wchar_t *__restrict __src))
>  {
> -  if (__glibc_objsize (__dest) != (size_t) -1)
> -    return __wcscpy_chk (__dest, __src,
> -			 __glibc_objsize (__dest) / sizeof (wchar_t));
> +  size_t sz = __glibc_objsize (__dest);
> +  if (sz != (size_t) -1)
> +    return __wcscpy_chk (__dest, __src, sz / sizeof (wchar_t));
>    return __wcscpy_alias (__dest, __src);
>  }
>  
> @@ -171,9 +137,9 @@ extern wchar_t *__REDIRECT_NTH (__wcpcpy_alias,
>  __fortify_function wchar_t *
>  __NTH (wcpcpy (wchar_t *__restrict __dest, const wchar_t *__restrict __src))
>  {
> -  if (__glibc_objsize (__dest) != (size_t) -1)
> -    return __wcpcpy_chk (__dest, __src,
> -			 __glibc_objsize (__dest) / sizeof (wchar_t));
> +  size_t sz = __glibc_objsize (__dest);
> +  if (sz != (size_t) -1)
> +    return __wcpcpy_chk (__dest, __src, sz / sizeof (wchar_t));
>    return __wcpcpy_alias (__dest, __src);
>  }
>  
> @@ -196,17 +162,9 @@ __fortify_function wchar_t *
>  __NTH (wcsncpy (wchar_t *__restrict __dest, const wchar_t *__restrict __src,
>  		size_t __n))
>  {
> -  if (__glibc_objsize (__dest) != (size_t) -1)
> -    {
> -      if (!__builtin_constant_p (__n))
> -	return __wcsncpy_chk (__dest, __src, __n,
> -			      __glibc_objsize (__dest) / sizeof (wchar_t));
> -      if (__n > __glibc_objsize (__dest) / sizeof (wchar_t))
> -	return __wcsncpy_chk_warn (__dest, __src, __n,
> -				   (__glibc_objsize (__dest)
> -				    / sizeof (wchar_t)));
> -    }
> -  return __wcsncpy_alias (__dest, __src, __n);
> +  return __glibc_fortify_n (wcsncpy, __n, sizeof (wchar_t),
> +			    __glibc_objsize (__dest),
> +			    __dest, __src, __n);
>  }
>  
>  
> @@ -228,17 +186,9 @@ __fortify_function wchar_t *
>  __NTH (wcpncpy (wchar_t *__restrict __dest, const wchar_t *__restrict __src,
>  		size_t __n))
>  {
> -  if (__glibc_objsize (__dest) != (size_t) -1)
> -    {
> -      if (!__builtin_constant_p (__n))
> -	return __wcpncpy_chk (__dest, __src, __n,
> -			      __glibc_objsize (__dest) / sizeof (wchar_t));
> -      if (__n > __glibc_objsize (__dest) / sizeof (wchar_t))
> -	return __wcpncpy_chk_warn (__dest, __src, __n,
> -				   (__glibc_objsize (__dest)
> -				    / sizeof (wchar_t)));
> -    }
> -  return __wcpncpy_alias (__dest, __src, __n);
> +  return __glibc_fortify_n (wcpncpy, __n, sizeof (wchar_t),
> +			    __glibc_objsize (__dest),
> +			    __dest, __src, __n);
>  }
>  
>  
> @@ -252,9 +202,9 @@ extern wchar_t *__REDIRECT_NTH (__wcscat_alias,
>  __fortify_function wchar_t *
>  __NTH (wcscat (wchar_t *__restrict __dest, const wchar_t *__restrict __src))
>  {
> -  if (__glibc_objsize (__dest) != (size_t) -1)
> -    return __wcscat_chk (__dest, __src,
> -			 __glibc_objsize (__dest) / sizeof (wchar_t));
> +  size_t sz = __glibc_objsize (__dest);
> +  if (sz != (size_t) -1)
> +    return __wcscat_chk (__dest, __src, sz / sizeof (wchar_t));
>    return __wcscat_alias (__dest, __src);
>  }
>  
> @@ -271,9 +221,9 @@ __fortify_function wchar_t *
>  __NTH (wcsncat (wchar_t *__restrict __dest, const wchar_t *__restrict __src,
>  		size_t __n))
>  {
> -  if (__glibc_objsize (__dest) != (size_t) -1)
> -    return __wcsncat_chk (__dest, __src, __n,
> -			  __glibc_objsize (__dest) / sizeof (wchar_t));
> +  size_t sz = __glibc_objsize (__dest);
> +  if (sz != (size_t) -1)
> +    return __wcsncat_chk (__dest, __src, __n, sz / sizeof (wchar_t));
>    return __wcsncat_alias (__dest, __src, __n);
>  }
>  
> @@ -293,10 +243,10 @@ __fortify_function int
>  __NTH (swprintf (wchar_t *__restrict __s, size_t __n,
>  		 const wchar_t *__restrict __fmt, ...))
>  {
> -  if (__glibc_objsize (__s) != (size_t) -1 || __USE_FORTIFY_LEVEL > 1)
> +  size_t sz = __glibc_objsize (__s);
> +  if (sz != (size_t) -1 || __USE_FORTIFY_LEVEL > 1)
>      return __swprintf_chk (__s, __n, __USE_FORTIFY_LEVEL - 1,
> -			   __glibc_objsize (__s) / sizeof (wchar_t),
> -			   __fmt, __va_arg_pack ());
> +			   sz / sizeof (wchar_t), __fmt, __va_arg_pack ());
>    return __swprintf_alias (__s, __n, __fmt, __va_arg_pack ());
>  }
>  #elif !defined __cplusplus
> @@ -323,10 +273,10 @@ __fortify_function int
>  __NTH (vswprintf (wchar_t *__restrict __s, size_t __n,
>  		  const wchar_t *__restrict __fmt, __gnuc_va_list __ap))
>  {
> -  if (__glibc_objsize (__s) != (size_t) -1 || __USE_FORTIFY_LEVEL > 1)
> +  size_t sz = __glibc_objsize (__s);
> +  if (sz != (size_t) -1 || __USE_FORTIFY_LEVEL > 1)
>      return __vswprintf_chk (__s, __n,  __USE_FORTIFY_LEVEL - 1,
> -			    __glibc_objsize (__s) / sizeof (wchar_t), __fmt,
> -			    __ap);
> +			    sz / sizeof (wchar_t), __fmt, __ap);
>    return __vswprintf_alias (__s, __n, __fmt, __ap);
>  }
>  
> @@ -392,18 +342,12 @@ extern wchar_t *__REDIRECT (__fgetws_chk_warn,
>  __fortify_function __wur wchar_t *
>  fgetws (wchar_t *__restrict __s, int __n, __FILE *__restrict __stream)
>  {
> -  if (__glibc_objsize (__s) != (size_t) -1)
> -    {
> -      if (!__builtin_constant_p (__n) || __n <= 0)
> -	return __fgetws_chk (__s, __glibc_objsize (__s) / sizeof (wchar_t),
> -			     __n, __stream);
> -
> -      if ((size_t) __n > __glibc_objsize (__s) / sizeof (wchar_t))
> -	return __fgetws_chk_warn (__s,
> -				  __glibc_objsize (__s) / sizeof (wchar_t),
> -				  __n, __stream);
> -    }
> -  return __fgetws_alias (__s, __n, __stream);
> +  size_t sz = __glibc_objsize (__s);
> +  if (__glibc_safe_or_unknown_len (__n, sizeof (wchar_t), sz))
> +    return __fgetws_alias (__s, __n, __stream);
> +  if (__glibc_unsafe_len (__n, sizeof (wchar_t), sz))
> +    return __fgetws_chk_warn (__s, sz / sizeof (wchar_t), __n, __stream);
> +  return __fgetws_chk (__s, sz / sizeof (wchar_t), __n, __stream);
>  }
>  
>  #ifdef __USE_GNU

OK.

> @@ -424,20 +368,13 @@ extern wchar_t *__REDIRECT (__fgetws_unlocked_chk_warn,
>  __fortify_function __wur wchar_t *
>  fgetws_unlocked (wchar_t *__restrict __s, int __n, __FILE *__restrict __stream)
>  {
> -  if (__glibc_objsize (__s) != (size_t) -1)
> -    {
> -      if (!__builtin_constant_p (__n) || __n <= 0)
> -	return __fgetws_unlocked_chk (__s,
> -				      __glibc_objsize (__s) / sizeof (wchar_t),
> -				      __n, __stream);
> -
> -      if ((size_t) __n > __glibc_objsize (__s) / sizeof (wchar_t))
> -	return __fgetws_unlocked_chk_warn (__s,
> -					   (__glibc_objsize (__s)
> -					    / sizeof (wchar_t)),
> -					   __n, __stream);
> -    }
> -  return __fgetws_unlocked_alias (__s, __n, __stream);
> +  size_t sz = __glibc_objsize (__s);
> +  if (__glibc_safe_or_unknown_len (__n, sizeof (wchar_t), sz))
> +    return __fgetws_unlocked_alias (__s, __n, __stream);
> +  if (__glibc_unsafe_len (__n, sizeof (wchar_t), sz))
> +    return __fgetws_unlocked_chk_warn (__s, sz / sizeof (wchar_t), __n,
> +				       __stream);
> +  return __fgetws_unlocked_chk (__s, sz / sizeof (wchar_t), __n, __stream);
>  }
>  #endif
>  
> @@ -488,18 +425,9 @@ __fortify_function size_t
>  __NTH (mbsrtowcs (wchar_t *__restrict __dst, const char **__restrict __src,
>  		  size_t __len, mbstate_t *__restrict __ps))
>  {
> -  if (__glibc_objsize (__dst) != (size_t) -1)
> -    {
> -      if (!__builtin_constant_p (__len))
> -	return __mbsrtowcs_chk (__dst, __src, __len, __ps,
> -				__glibc_objsize (__dst) / sizeof (wchar_t));
> -
> -      if (__len > __glibc_objsize (__dst) / sizeof (wchar_t))
> -	return __mbsrtowcs_chk_warn (__dst, __src, __len, __ps,
> -				     (__glibc_objsize (__dst)
> -				      / sizeof (wchar_t)));
> -    }
> -  return __mbsrtowcs_alias (__dst, __src, __len, __ps);
> +  return __glibc_fortify_n (mbsrtowcs, __len, sizeof (wchar_t),
> +			    __glibc_objsize (__dst),
> +			    __dst, __src, __len, __ps);
>  }
>  
>  
> @@ -523,17 +451,9 @@ __fortify_function size_t
>  __NTH (wcsrtombs (char *__restrict __dst, const wchar_t **__restrict __src,
>  		  size_t __len, mbstate_t *__restrict __ps))
>  {
> -  if (__glibc_objsize (__dst) != (size_t) -1)
> -    {
> -      if (!__builtin_constant_p (__len))
> -	return __wcsrtombs_chk (__dst, __src, __len, __ps,
> -				__glibc_objsize (__dst));
> -
> -      if (__len > __glibc_objsize (__dst))
> -	return __wcsrtombs_chk_warn (__dst, __src, __len, __ps,
> -				     __glibc_objsize (__dst));
> -    }
> -  return __wcsrtombs_alias (__dst, __src, __len, __ps);
> +  return __glibc_fortify (wcsrtombs, __len, sizeof (char),
> +			  __glibc_objsize (__dst),
> +			  __dst, __src, __len, __ps);
>  }
>  
>  
> @@ -559,18 +479,9 @@ __fortify_function size_t
>  __NTH (mbsnrtowcs (wchar_t *__restrict __dst, const char **__restrict __src,
>  		   size_t __nmc, size_t __len, mbstate_t *__restrict __ps))
>  {
> -  if (__glibc_objsize (__dst) != (size_t) -1)
> -    {
> -      if (!__builtin_constant_p (__len))
> -	return __mbsnrtowcs_chk (__dst, __src, __nmc, __len, __ps,
> -				 __glibc_objsize (__dst) / sizeof (wchar_t));
> -
> -      if (__len > __glibc_objsize (__dst) / sizeof (wchar_t))
> -	return __mbsnrtowcs_chk_warn (__dst, __src, __nmc, __len, __ps,
> -				      (__glibc_objsize (__dst)
> -				       / sizeof (wchar_t)));
> -    }
> -  return __mbsnrtowcs_alias (__dst, __src, __nmc, __len, __ps);
> +  return __glibc_fortify_n (mbsnrtowcs, __len, sizeof (wchar_t),
> +			    __glibc_objsize (__dst),
> +			    __dst, __src, __nmc, __len, __ps);
>  }
>  
>  
> @@ -596,16 +507,8 @@ __fortify_function size_t
>  __NTH (wcsnrtombs (char *__restrict __dst, const wchar_t **__restrict __src,
>  		   size_t __nwc, size_t __len, mbstate_t *__restrict __ps))
>  {
> -  if (__glibc_objsize (__dst) != (size_t) -1)
> -    {
> -      if (!__builtin_constant_p (__len))
> -	return __wcsnrtombs_chk (__dst, __src, __nwc, __len, __ps,
> -				 __glibc_objsize (__dst));
> -
> -      if (__len > __glibc_objsize (__dst))
> -	return __wcsnrtombs_chk_warn (__dst, __src, __nwc, __len, __ps,
> -				      __glibc_objsize (__dst));
> -    }
> -  return __wcsnrtombs_alias (__dst, __src, __nwc, __len, __ps);
> +  return __glibc_fortify (wcsnrtombs, __len, sizeof (char),
> +			  __glibc_objsize (__dst),
> +			  __dst, __src, __nwc, __len, __ps);
>  }
>  #endif
> 

Ok.

^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: [PATCH v2 2/2] debug: Add tests for _FORTIFY_SOURCE=3
  2021-10-20  5:24   ` [PATCH v2 2/2] debug: Add tests for _FORTIFY_SOURCE=3 Siddhesh Poyarekar
@ 2021-10-20 12:06     ` Adhemerval Zanella
  0 siblings, 0 replies; 17+ messages in thread
From: Adhemerval Zanella @ 2021-10-20 12:06 UTC (permalink / raw)
  To: Siddhesh Poyarekar, libc-alpha



On 20/10/2021 02:24, Siddhesh Poyarekar wrote:
> Add some testing coverage for _FORTIFY_SOURCE=3.
> 
> Signed-off-by: Siddhesh Poyarekar <siddhesh@sourceware.org>

LGTM, thanks.

Reviewed-by: Adhemerval Zanella  <adhemerval.zanella@linaro.org>

> ---
>  debug/Makefile    |  13 ++++--
>  debug/tst-chk1.c  | 102 +++++++++++++++++++++++++---------------------
>  debug/tst-chk7.c  |   2 +
>  debug/tst-chk8.cc |   2 +
>  4 files changed, 69 insertions(+), 50 deletions(-)
>  create mode 100644 debug/tst-chk7.c
>  create mode 100644 debug/tst-chk8.cc
> 
> diff --git a/debug/Makefile b/debug/Makefile
> index 6893111cbf..357f888246 100644
> --- a/debug/Makefile
> +++ b/debug/Makefile
> @@ -120,6 +120,8 @@ CFLAGS-tst-chk3.c += -Wno-format -Wno-deprecated-declarations -Wno-error
>  CFLAGS-tst-chk4.cc += -Wno-format -Wno-deprecated-declarations -Wno-error
>  CFLAGS-tst-chk5.cc += -Wno-format -Wno-deprecated-declarations -Wno-error
>  CFLAGS-tst-chk6.cc += -Wno-format -Wno-deprecated-declarations -Wno-error
> +CFLAGS-tst-chk7.c += -Wno-format -Wno-deprecated-declarations -Wno-error
> +CFLAGS-tst-chk8.cc += -Wno-format -Wno-deprecated-declarations -Wno-error
>  CFLAGS-tst-lfschk1.c += -Wno-format -Wno-deprecated-declarations -Wno-error
>  CFLAGS-tst-lfschk2.c += -Wno-format -Wno-deprecated-declarations -Wno-error
>  CFLAGS-tst-lfschk3.c += -Wno-format -Wno-deprecated-declarations -Wno-error
> @@ -129,6 +131,7 @@ CFLAGS-tst-lfschk6.cc += -Wno-format -Wno-deprecated-declarations -Wno-error
>  LDLIBS-tst-chk4 = -lstdc++
>  LDLIBS-tst-chk5 = -lstdc++
>  LDLIBS-tst-chk6 = -lstdc++
> +LDLIBS-tst-chk8 = -lstdc++
>  LDLIBS-tst-lfschk4 = -lstdc++
>  LDLIBS-tst-lfschk5 = -lstdc++
>  LDLIBS-tst-lfschk6 = -lstdc++
> @@ -150,16 +153,16 @@ CFLAGS-tst-ssp-1.c += -fstack-protector-all
>  
>  tests = backtrace-tst tst-longjmp_chk tst-chk1 tst-chk2 tst-chk3 \
>  	tst-lfschk1 tst-lfschk2 tst-lfschk3 test-strcpy_chk test-stpcpy_chk \
> -	tst-chk4 tst-chk5 tst-chk6 tst-lfschk4 tst-lfschk5 tst-lfschk6 \
> -	tst-longjmp_chk2 tst-backtrace2 tst-backtrace3 tst-backtrace4 \
> -	tst-backtrace5 tst-backtrace6
> +	tst-chk4 tst-chk5 tst-chk6 tst-chk7 tst-chk8 tst-lfschk4 tst-lfschk5 \
> +	tst-lfschk6 tst-longjmp_chk2 tst-backtrace2 tst-backtrace3 \
> +	tst-backtrace4 tst-backtrace5 tst-backtrace6
>  
>  ifeq ($(have-ssp),yes)
>  tests += tst-ssp-1
>  endif
>  
>  ifeq (,$(CXX))
> -tests-unsupported = tst-chk4 tst-chk5 tst-chk6 \
> +tests-unsupported = tst-chk4 tst-chk5 tst-chk6 tst-chk8 \
>  		    tst-lfschk4 tst-lfschk5 tst-lfschk6
>  endif
>  
> @@ -193,6 +196,8 @@ $(objpfx)tst-chk3.out: $(gen-locales)
>  $(objpfx)tst-chk4.out: $(gen-locales)
>  $(objpfx)tst-chk5.out: $(gen-locales)
>  $(objpfx)tst-chk6.out: $(gen-locales)
> +$(objpfx)tst-chk7.out: $(gen-locales)
> +$(objpfx)tst-chk8.out: $(gen-locales)
>  $(objpfx)tst-lfschk1.out: $(gen-locales)
>  $(objpfx)tst-lfschk2.out: $(gen-locales)
>  $(objpfx)tst-lfschk3.out: $(gen-locales)
> diff --git a/debug/tst-chk1.c b/debug/tst-chk1.c
> index 6712770201..d11e536f87 100644
> --- a/debug/tst-chk1.c
> +++ b/debug/tst-chk1.c
> @@ -82,8 +82,14 @@ handler (int sig)
>      _exit (127);
>  }
>  
> +#if __USE_FORTIFY_LEVEL == 3
> +volatile size_t buf_size = 10;
> +#else
>  char buf[10];
>  wchar_t wbuf[10];
> +#define buf_size sizeof (buf)
> +#endif
> +
>  volatile size_t l0;
>  volatile char *p;
>  volatile wchar_t *wp;
> @@ -122,6 +128,10 @@ int num2 = 987654;
>  static int
>  do_test (void)
>  {
> +#if __USE_FORTIFY_LEVEL == 3
> +  char *buf = (char *) malloc (buf_size);
> +  wchar_t *wbuf = (wchar_t *) malloc (buf_size * sizeof (wchar_t));
> +#endif
>    set_fortify_handler (handler);
>  
>    struct A { char buf1[9]; char buf2[1]; } a;
> @@ -946,93 +956,93 @@ do_test (void)
>  
>    rewind (stdin);
>  
> -  if (fgets (buf, sizeof (buf), stdin) != buf
> +  if (fgets (buf, buf_size, stdin) != buf
>        || memcmp (buf, "abcdefgh\n", 10))
>      FAIL ();
> -  if (fgets (buf, sizeof (buf), stdin) != buf || memcmp (buf, "ABCDEFGHI", 10))
> +  if (fgets (buf, buf_size, stdin) != buf || memcmp (buf, "ABCDEFGHI", 10))
>      FAIL ();
>  
>    rewind (stdin);
>  
> -  if (fgets (buf, l0 + sizeof (buf), stdin) != buf
> +  if (fgets (buf, l0 + buf_size, stdin) != buf
>        || memcmp (buf, "abcdefgh\n", 10))
>      FAIL ();
>  
>  #if __USE_FORTIFY_LEVEL >= 1
>    CHK_FAIL_START
> -  if (fgets (buf, sizeof (buf) + 1, stdin) != buf)
> +  if (fgets (buf, buf_size + 1, stdin) != buf)
>      FAIL ();
>    CHK_FAIL_END
>  
>    CHK_FAIL_START
> -  if (fgets (buf, l0 + sizeof (buf) + 1, stdin) != buf)
> +  if (fgets (buf, l0 + buf_size + 1, stdin) != buf)
>      FAIL ();
>    CHK_FAIL_END
>  #endif
>  
>    rewind (stdin);
>  
> -  if (fgets_unlocked (buf, sizeof (buf), stdin) != buf
> +  if (fgets_unlocked (buf, buf_size, stdin) != buf
>        || memcmp (buf, "abcdefgh\n", 10))
>      FAIL ();
> -  if (fgets_unlocked (buf, sizeof (buf), stdin) != buf
> +  if (fgets_unlocked (buf, buf_size, stdin) != buf
>        || memcmp (buf, "ABCDEFGHI", 10))
>      FAIL ();
>  
>    rewind (stdin);
>  
> -  if (fgets_unlocked (buf, l0 + sizeof (buf), stdin) != buf
> +  if (fgets_unlocked (buf, l0 + buf_size, stdin) != buf
>        || memcmp (buf, "abcdefgh\n", 10))
>      FAIL ();
>  
>  #if __USE_FORTIFY_LEVEL >= 1
>    CHK_FAIL_START
> -  if (fgets_unlocked (buf, sizeof (buf) + 1, stdin) != buf)
> +  if (fgets_unlocked (buf, buf_size + 1, stdin) != buf)
>      FAIL ();
>    CHK_FAIL_END
>  
>    CHK_FAIL_START
> -  if (fgets_unlocked (buf, l0 + sizeof (buf) + 1, stdin) != buf)
> +  if (fgets_unlocked (buf, l0 + buf_size + 1, stdin) != buf)
>      FAIL ();
>    CHK_FAIL_END
>  #endif
>  
>    rewind (stdin);
>  
> -  if (fread (buf, 1, sizeof (buf), stdin) != sizeof (buf)
> +  if (fread (buf, 1, buf_size, stdin) != buf_size
>        || memcmp (buf, "abcdefgh\nA", 10))
>      FAIL ();
> -  if (fread (buf, sizeof (buf), 1, stdin) != 1
> +  if (fread (buf, buf_size, 1, stdin) != 1
>        || memcmp (buf, "BCDEFGHI\na", 10))
>      FAIL ();
>  
>    rewind (stdin);
>  
> -  if (fread (buf, l0 + 1, sizeof (buf), stdin) != sizeof (buf)
> +  if (fread (buf, l0 + 1, buf_size, stdin) != buf_size
>        || memcmp (buf, "abcdefgh\nA", 10))
>      FAIL ();
> -  if (fread (buf, sizeof (buf), l0 + 1, stdin) != 1
> +  if (fread (buf, buf_size, l0 + 1, stdin) != 1
>        || memcmp (buf, "BCDEFGHI\na", 10))
>      FAIL ();
>  
>  #if __USE_FORTIFY_LEVEL >= 1
>    CHK_FAIL_START
> -  if (fread (buf, 1, sizeof (buf) + 1, stdin) != sizeof (buf) + 1)
> +  if (fread (buf, 1, buf_size + 1, stdin) != buf_size + 1)
>      FAIL ();
>    CHK_FAIL_END
>  
>    CHK_FAIL_START
> -  if (fread (buf, sizeof (buf) + 1, l0 + 1, stdin) != 1)
> +  if (fread (buf, buf_size + 1, l0 + 1, stdin) != 1)
>      FAIL ();
>    CHK_FAIL_END
>  #endif
>  
>    rewind (stdin);
>  
> -  if (fread_unlocked (buf, 1, sizeof (buf), stdin) != sizeof (buf)
> +  if (fread_unlocked (buf, 1, buf_size, stdin) != buf_size
>        || memcmp (buf, "abcdefgh\nA", 10))
>      FAIL ();
> -  if (fread_unlocked (buf, sizeof (buf), 1, stdin) != 1
> +  if (fread_unlocked (buf, buf_size, 1, stdin) != 1
>        || memcmp (buf, "BCDEFGHI\na", 10))
>      FAIL ();
>  
> @@ -1047,100 +1057,100 @@ do_test (void)
>  
>    rewind (stdin);
>  
> -  if (fread_unlocked (buf, l0 + 1, sizeof (buf), stdin) != sizeof (buf)
> +  if (fread_unlocked (buf, l0 + 1, buf_size, stdin) != buf_size
>        || memcmp (buf, "abcdefgh\nA", 10))
>      FAIL ();
> -  if (fread_unlocked (buf, sizeof (buf), l0 + 1, stdin) != 1
> +  if (fread_unlocked (buf, buf_size, l0 + 1, stdin) != 1
>        || memcmp (buf, "BCDEFGHI\na", 10))
>      FAIL ();
>  
>  #if __USE_FORTIFY_LEVEL >= 1
>    CHK_FAIL_START
> -  if (fread_unlocked (buf, 1, sizeof (buf) + 1, stdin) != sizeof (buf) + 1)
> +  if (fread_unlocked (buf, 1, buf_size + 1, stdin) != buf_size + 1)
>      FAIL ();
>    CHK_FAIL_END
>  
>    CHK_FAIL_START
> -  if (fread_unlocked (buf, sizeof (buf) + 1, l0 + 1, stdin) != 1)
> +  if (fread_unlocked (buf, buf_size + 1, l0 + 1, stdin) != 1)
>      FAIL ();
>    CHK_FAIL_END
>  #endif
>  
>    lseek (fileno (stdin), 0, SEEK_SET);
>  
> -  if (read (fileno (stdin), buf, sizeof (buf) - 1) != sizeof (buf) - 1
> +  if (read (fileno (stdin), buf, buf_size - 1) != buf_size - 1
>        || memcmp (buf, "abcdefgh\n", 9))
>      FAIL ();
> -  if (read (fileno (stdin), buf, sizeof (buf) - 1) != sizeof (buf) - 1
> +  if (read (fileno (stdin), buf, buf_size - 1) != buf_size - 1
>        || memcmp (buf, "ABCDEFGHI", 9))
>      FAIL ();
>  
>    lseek (fileno (stdin), 0, SEEK_SET);
>  
> -  if (read (fileno (stdin), buf, l0 + sizeof (buf) - 1) != sizeof (buf) - 1
> +  if (read (fileno (stdin), buf, l0 + buf_size - 1) != buf_size - 1
>        || memcmp (buf, "abcdefgh\n", 9))
>      FAIL ();
>  
>  #if __USE_FORTIFY_LEVEL >= 1
>    CHK_FAIL_START
> -  if (read (fileno (stdin), buf, sizeof (buf) + 1) != sizeof (buf) + 1)
> +  if (read (fileno (stdin), buf, buf_size + 1) != buf_size + 1)
>      FAIL ();
>    CHK_FAIL_END
>  
>    CHK_FAIL_START
> -  if (read (fileno (stdin), buf, l0 + sizeof (buf) + 1) != sizeof (buf) + 1)
> +  if (read (fileno (stdin), buf, l0 + buf_size + 1) != buf_size + 1)
>      FAIL ();
>    CHK_FAIL_END
>  #endif
>  
> -  if (pread (fileno (stdin), buf, sizeof (buf) - 1, sizeof (buf) - 2)
> -      != sizeof (buf) - 1
> +  if (pread (fileno (stdin), buf, buf_size - 1, buf_size - 2)
> +      != buf_size - 1
>        || memcmp (buf, "\nABCDEFGH", 9))
>      FAIL ();
> -  if (pread (fileno (stdin), buf, sizeof (buf) - 1, 0) != sizeof (buf) - 1
> +  if (pread (fileno (stdin), buf, buf_size - 1, 0) != buf_size - 1
>        || memcmp (buf, "abcdefgh\n", 9))
>      FAIL ();
> -  if (pread (fileno (stdin), buf, l0 + sizeof (buf) - 1, sizeof (buf) - 3)
> -      != sizeof (buf) - 1
> +  if (pread (fileno (stdin), buf, l0 + buf_size - 1, buf_size - 3)
> +      != buf_size - 1
>        || memcmp (buf, "h\nABCDEFG", 9))
>      FAIL ();
>  
>  #if __USE_FORTIFY_LEVEL >= 1
>    CHK_FAIL_START
> -  if (pread (fileno (stdin), buf, sizeof (buf) + 1, 2 * sizeof (buf))
> -      != sizeof (buf) + 1)
> +  if (pread (fileno (stdin), buf, buf_size + 1, 2 * buf_size)
> +      != buf_size + 1)
>      FAIL ();
>    CHK_FAIL_END
>  
>    CHK_FAIL_START
> -  if (pread (fileno (stdin), buf, l0 + sizeof (buf) + 1, 2 * sizeof (buf))
> -      != sizeof (buf) + 1)
> +  if (pread (fileno (stdin), buf, l0 + buf_size + 1, 2 * buf_size)
> +      != buf_size + 1)
>      FAIL ();
>    CHK_FAIL_END
>  #endif
>  
> -  if (pread64 (fileno (stdin), buf, sizeof (buf) - 1, sizeof (buf) - 2)
> -      != sizeof (buf) - 1
> +  if (pread64 (fileno (stdin), buf, buf_size - 1, buf_size - 2)
> +      != buf_size - 1
>        || memcmp (buf, "\nABCDEFGH", 9))
>      FAIL ();
> -  if (pread64 (fileno (stdin), buf, sizeof (buf) - 1, 0) != sizeof (buf) - 1
> +  if (pread64 (fileno (stdin), buf, buf_size - 1, 0) != buf_size - 1
>        || memcmp (buf, "abcdefgh\n", 9))
>      FAIL ();
> -  if (pread64 (fileno (stdin), buf, l0 + sizeof (buf) - 1, sizeof (buf) - 3)
> -      != sizeof (buf) - 1
> +  if (pread64 (fileno (stdin), buf, l0 + buf_size - 1, buf_size - 3)
> +      != buf_size - 1
>        || memcmp (buf, "h\nABCDEFG", 9))
>      FAIL ();
>  
>  #if __USE_FORTIFY_LEVEL >= 1
>    CHK_FAIL_START
> -  if (pread64 (fileno (stdin), buf, sizeof (buf) + 1, 2 * sizeof (buf))
> -      != sizeof (buf) + 1)
> +  if (pread64 (fileno (stdin), buf, buf_size + 1, 2 * buf_size)
> +      != buf_size + 1)
>      FAIL ();
>    CHK_FAIL_END
>  
>    CHK_FAIL_START
> -  if (pread64 (fileno (stdin), buf, l0 + sizeof (buf) + 1, 2 * sizeof (buf))
> -      != sizeof (buf) + 1)
> +  if (pread64 (fileno (stdin), buf, l0 + buf_size + 1, 2 * buf_size)
> +      != buf_size + 1)
>      FAIL ();
>    CHK_FAIL_END
>  #endif
> @@ -1178,7 +1188,7 @@ do_test (void)
>    CHK_FAIL2_END
>  
>    CHK_FAIL2_START
> -  snprintf (buf, sizeof (buf), "%3$d\n", 1, 2, 3, 4);
> +  snprintf (buf, buf_size, "%3$d\n", 1, 2, 3, 4);
>    CHK_FAIL2_END
>  
>    int sp[2];

Ok, it basically just parametrize 'sizeof (buf)'.

> diff --git a/debug/tst-chk7.c b/debug/tst-chk7.c
> new file mode 100644
> index 0000000000..2a7b323812
> --- /dev/null
> +++ b/debug/tst-chk7.c
> @@ -0,0 +1,2 @@
> +#define _FORTIFY_SOURCE 3
> +#include "tst-chk1.c"
> diff --git a/debug/tst-chk8.cc b/debug/tst-chk8.cc
> new file mode 100644
> index 0000000000..2a7b323812
> --- /dev/null
> +++ b/debug/tst-chk8.cc
> @@ -0,0 +1,2 @@
> +#define _FORTIFY_SOURCE 3
> +#include "tst-chk1.c"
> 

Ok.

^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: [PATCH v2 0/2] _FORTIFY_SOURCE=3 improvements
  2021-10-20  5:24 ` [PATCH v2 0/2] " Siddhesh Poyarekar
  2021-10-20  5:24   ` [PATCH v2 1/2] Make sure that the fortified function conditionals are constant Siddhesh Poyarekar
  2021-10-20  5:24   ` [PATCH v2 2/2] debug: Add tests for _FORTIFY_SOURCE=3 Siddhesh Poyarekar
@ 2021-10-20 14:28   ` Siddhesh Poyarekar
  2 siblings, 0 replies; 17+ messages in thread
From: Siddhesh Poyarekar @ 2021-10-20 14:28 UTC (permalink / raw)
  To: libc-alpha

On 10/20/21 10:54, Siddhesh Poyarekar via Libc-alpha wrote:
> This patchset changes the layout of fortified functions to make them
> _FORTIFY_SOURCE=3 friendly and at the same time, continue working the
> same for _FORTIFY_SOURCE=2 and lower.  At a high level it makes sure
> that no branches are emitted at compile time and only one of either the
> regular or _chk version of the function is called.  The conditions are
> also reworked to make them more readable and foldable even in cases
> where the compiler doesn't know the exact values of the operation length
> and size, but can make decisions based on ranges of their values.
> 
> The changeset also adds some _FORTIFY_SOURCE=3 testing coverage on
> compilers that are able to set that fortification level.
> 
> Changes from v1:
> 
> - Committed access attribute related patch
> - Updated macros to fold the sign check into a single macro

I'll be backporting this series to 2.34 unless there are any objections.

Thanks,
Siddhesh

^ permalink raw reply	[flat|nested] 17+ messages in thread

end of thread, other threads:[~2021-10-20 14:28 UTC | newest]

Thread overview: 17+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-10-12 16:16 [PATCH 0/3] _FORTIFY_SOURCE=3 improvements Siddhesh Poyarekar
2021-10-12 16:16 ` [PATCH 1/3] Don't add access size hints to fortifiable functions Siddhesh Poyarekar
2021-10-19 17:54   ` Adhemerval Zanella
2021-10-19 18:19     ` Siddhesh Poyarekar
2021-10-19 18:24       ` Adhemerval Zanella
2021-10-19 18:37         ` Siddhesh Poyarekar
2021-10-12 16:16 ` [PATCH 2/3] Make sure that the fortified function conditionals are constant Siddhesh Poyarekar
2021-10-19 19:34   ` Adhemerval Zanella
2021-10-20  3:16     ` Siddhesh Poyarekar
2021-10-12 16:16 ` [PATCH 3/3] debug: Add tests for _FORTIFY_SOURCE=3 Siddhesh Poyarekar
2021-10-18 13:14 ` [PING][PATCH 0/3] _FORTIFY_SOURCE=3 improvements Siddhesh Poyarekar
2021-10-20  5:24 ` [PATCH v2 0/2] " Siddhesh Poyarekar
2021-10-20  5:24   ` [PATCH v2 1/2] Make sure that the fortified function conditionals are constant Siddhesh Poyarekar
2021-10-20 12:00     ` Adhemerval Zanella
2021-10-20  5:24   ` [PATCH v2 2/2] debug: Add tests for _FORTIFY_SOURCE=3 Siddhesh Poyarekar
2021-10-20 12:06     ` Adhemerval Zanella
2021-10-20 14:28   ` [PATCH v2 0/2] _FORTIFY_SOURCE=3 improvements Siddhesh Poyarekar

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).