08c3a6
commit c82bdf033f93a710044e25f721340c26e89a3769
08c3a6
Author: Siddhesh Poyarekar <siddhesh@sourceware.org>
08c3a6
Date:   Tue Oct 12 12:29:13 2021 +0530
08c3a6
08c3a6
    Don't add access size hints to fortifiable functions
08c3a6
    
08c3a6
    In the context of a function definition, the size hints imply that the
08c3a6
    size of an object pointed to by one parameter is another parameter.
08c3a6
    This doesn't make sense for the fortified versions of the functions
08c3a6
    since that's the bit it's trying to validate.
08c3a6
    
08c3a6
    This is harmless with __builtin_object_size since it has fairly simple
08c3a6
    semantics when it comes to objects passed as function parameters.
08c3a6
    With __builtin_dynamic_object_size we could (as my patchset for gcc[1]
08c3a6
    already does) use the access attribute to determine the object size in
08c3a6
    the general case but it misleads the fortified functions.
08c3a6
    
08c3a6
    Basically the problem occurs when access attributes are present on
08c3a6
    regular functions that have inline fortified definitions to generate
08c3a6
    _chk variants; the attributes get inherited by these definitions,
08c3a6
    causing problems when analyzing them.  For example with poll(fds, nfds,
08c3a6
    timeout), nfds is hinted using the __attr_access as being the size of
08c3a6
    fds.
08c3a6
    
08c3a6
    Now, when analyzing the inline function definition in bits/poll2.h, the
08c3a6
    compiler sees that nfds is the size of fds and tries to use that
08c3a6
    information in the function body.  In _FORTIFY_SOURCE=3 case, where the
08c3a6
    object size could be a non-constant expression, this information results
08c3a6
    in the conclusion that nfds is the size of fds, which defeats the
08c3a6
    purpose of the implementation because we're trying to check here if nfds
08c3a6
    does indeed represent the size of fds.  Hence for this case, it is best
08c3a6
    to not have the access attribute.
08c3a6
    
08c3a6
    With the attributes gone, the expression evaluation should get delayed
08c3a6
    until the function is actually inlined into its destinations.
08c3a6
    
08c3a6
    Disable the access attribute for fortified function inline functions
08c3a6
    when building at _FORTIFY_SOURCE=3 to make this work better.  The
08c3a6
    access attributes remain for the _chk variants since they can be used
08c3a6
    by the compiler to warn when the caller is passing invalid arguments.
08c3a6
    
08c3a6
    [1] https://gcc.gnu.org/pipermail/gcc-patches/2021-October/581125.html
08c3a6
    
08c3a6
    Signed-off-by: Siddhesh Poyarekar <siddhesh@sourceware.org>
08c3a6
    (cherry picked from commit e938c02748402c50f60ba0eb983273e7b52937d1)
08c3a6
08c3a6
diff --git a/io/bits/poll2.h b/io/bits/poll2.h
08c3a6
index a623678c09f9f04f..be74d020f2e0e434 100644
08c3a6
--- a/io/bits/poll2.h
08c3a6
+++ b/io/bits/poll2.h
08c3a6
@@ -33,7 +33,7 @@ extern int __REDIRECT (__poll_chk_warn, (struct pollfd *__fds, nfds_t __nfds,
08c3a6
 		       __poll_chk)
08c3a6
   __warnattr ("poll called with fds buffer too small file nfds entries");
08c3a6
 
08c3a6
-__fortify_function __attr_access ((__write_only__, 1, 2)) int
08c3a6
+__fortify_function __fortified_attr_access (__write_only__, 1, 2) int
08c3a6
 poll (struct pollfd *__fds, nfds_t __nfds, int __timeout)
08c3a6
 {
08c3a6
   if (__glibc_objsize (__fds) != (__SIZE_TYPE__) -1)
08c3a6
@@ -64,7 +64,7 @@ extern int __REDIRECT (__ppoll_chk_warn, (struct pollfd *__fds, nfds_t __nfds,
08c3a6
 		       __ppoll_chk)
08c3a6
   __warnattr ("ppoll called with fds buffer too small file nfds entries");
08c3a6
 
08c3a6
-__fortify_function __attr_access ((__write_only__, 1, 2)) int
08c3a6
+__fortify_function __fortified_attr_access (__write_only__, 1, 2) int
08c3a6
 ppoll (struct pollfd *__fds, nfds_t __nfds, const struct timespec *__timeout,
08c3a6
        const __sigset_t *__ss)
08c3a6
 {
08c3a6
diff --git a/io/sys/poll.h b/io/sys/poll.h
08c3a6
index e640efb2bce7ea67..751c7f5f72db8be2 100644
08c3a6
--- a/io/sys/poll.h
08c3a6
+++ b/io/sys/poll.h
08c3a6
@@ -52,7 +52,7 @@ __BEGIN_DECLS
08c3a6
    This function is a cancellation point and therefore not marked with
08c3a6
    __THROW.  */
08c3a6
 extern int poll (struct pollfd *__fds, nfds_t __nfds, int __timeout)
08c3a6
-    __attr_access ((__write_only__, 1, 2));
08c3a6
+    __fortified_attr_access (__write_only__, 1, 2);
08c3a6
 
08c3a6
 #ifdef __USE_GNU
08c3a6
 /* Like poll, but before waiting the threads signal mask is replaced
08c3a6
@@ -64,7 +64,7 @@ extern int poll (struct pollfd *__fds, nfds_t __nfds, int __timeout)
08c3a6
 extern int ppoll (struct pollfd *__fds, nfds_t __nfds,
08c3a6
 		  const struct timespec *__timeout,
08c3a6
 		  const __sigset_t *__ss)
08c3a6
-    __attr_access ((__write_only__, 1, 2));
08c3a6
+    __fortified_attr_access (__write_only__, 1, 2);
08c3a6
 
08c3a6
 # ifdef __USE_TIME_BITS64
08c3a6
 #  ifdef __REDIRECT
08c3a6
@@ -72,7 +72,7 @@ extern int __REDIRECT (ppoll, (struct pollfd *__fds, nfds_t __nfds,
08c3a6
                                const struct timespec *__timeout,
08c3a6
                                const __sigset_t *__ss),
08c3a6
                        __ppoll64)
08c3a6
-    __attr_access ((__write_only__, 1, 2));
08c3a6
+    __fortified_attr_access (__write_only__, 1, 2);
08c3a6
 #  else
08c3a6
 #  define ppoll __ppoll64
08c3a6
 #  endif
08c3a6
diff --git a/libio/bits/stdio2.h b/libio/bits/stdio2.h
08c3a6
index 3f0cab1254b02c43..4f016a563857a137 100644
08c3a6
--- a/libio/bits/stdio2.h
08c3a6
+++ b/libio/bits/stdio2.h
08c3a6
@@ -258,7 +258,7 @@ extern char *__REDIRECT (__fgets_chk_warn,
08c3a6
      __wur __warnattr ("fgets called with bigger size than length "
08c3a6
 		       "of destination buffer");
08c3a6
 
08c3a6
-__fortify_function __wur __attr_access ((__write_only__, 1, 2)) char *
08c3a6
+__fortify_function __wur __fortified_attr_access (__write_only__, 1, 2) char *
08c3a6
 fgets (char *__restrict __s, int __n, FILE *__restrict __stream)
08c3a6
 {
08c3a6
   if (__glibc_objsize (__s) != (size_t) -1)
08c3a6
@@ -320,7 +320,7 @@ extern char *__REDIRECT (__fgets_unlocked_chk_warn,
08c3a6
      __wur __warnattr ("fgets_unlocked called with bigger size than length "
08c3a6
 		       "of destination buffer");
08c3a6
 
08c3a6
-__fortify_function __wur __attr_access ((__write_only__, 1, 2)) char *
08c3a6
+__fortify_function __wur __fortified_attr_access (__write_only__, 1, 2) char *
08c3a6
 fgets_unlocked (char *__restrict __s, int __n, FILE *__restrict __stream)
08c3a6
 {
08c3a6
   if (__glibc_objsize (__s) != (size_t) -1)
08c3a6
diff --git a/libio/stdio.h b/libio/stdio.h
08c3a6
index 497da016ffa2e230..abefe640e52d18d5 100644
08c3a6
--- a/libio/stdio.h
08c3a6
+++ b/libio/stdio.h
08c3a6
@@ -584,7 +584,7 @@ extern int putw (int __w, FILE *__stream);
08c3a6
    This function is a possible cancellation point and therefore not
08c3a6
    marked with __THROW.  */
08c3a6
 extern char *fgets (char *__restrict __s, int __n, FILE *__restrict __stream)
08c3a6
-     __wur __attr_access ((__write_only__, 1, 2));
08c3a6
+     __wur __fortified_attr_access (__write_only__, 1, 2);
08c3a6
 
08c3a6
 #if __GLIBC_USE (DEPRECATED_GETS)
08c3a6
 /* Get a newline-terminated string from stdin, removing the newline.
08c3a6
@@ -608,7 +608,7 @@ extern char *gets (char *__s) __wur __attribute_deprecated__;
08c3a6
    therefore not marked with __THROW.  */
08c3a6
 extern char *fgets_unlocked (char *__restrict __s, int __n,
08c3a6
 			     FILE *__restrict __stream) __wur
08c3a6
-    __attr_access ((__write_only__, 1, 2));
08c3a6
+    __fortified_attr_access (__write_only__, 1, 2);
08c3a6
 #endif
08c3a6
 
08c3a6
 
08c3a6
diff --git a/misc/sys/cdefs.h b/misc/sys/cdefs.h
08c3a6
index e490fc1aebeadc3d..cd836441a9807d6a 100644
08c3a6
--- a/misc/sys/cdefs.h
08c3a6
+++ b/misc/sys/cdefs.h
08c3a6
@@ -603,12 +603,22 @@ _Static_assert (0, "IEEE 128-bits long double requires redirection on this platf
08c3a6
    size-index is not provided:
08c3a6
      access (access-mode, <ref-index> [, <size-index>])  */
08c3a6
 #  define __attr_access(x) __attribute__ ((__access__ x))
08c3a6
+/* For _FORTIFY_SOURCE == 3 we use __builtin_dynamic_object_size, which may
08c3a6
+   use the access attribute to get object sizes from function definition
08c3a6
+   arguments, so we can't use them on functions we fortify.  Drop the object
08c3a6
+   size hints for such functions.  */
08c3a6
+#  if __USE_FORTIFY_LEVEL == 3
08c3a6
+#    define __fortified_attr_access(a, o, s) __attribute__ ((__access__ (a, o)))
08c3a6
+#  else
08c3a6
+#    define __fortified_attr_access(a, o, s) __attr_access ((a, o, s))
08c3a6
+#  endif
08c3a6
 #  if __GNUC_PREREQ (11, 0)
08c3a6
 #    define __attr_access_none(argno) __attribute__ ((__access__ (__none__, argno)))
08c3a6
 #  else
08c3a6
 #    define __attr_access_none(argno)
08c3a6
 #  endif
08c3a6
 #else
08c3a6
+#  define __fortified_attr_access(a, o, s)
08c3a6
 #  define __attr_access(x)
08c3a6
 #  define __attr_access_none(argno)
08c3a6
 #endif
08c3a6
diff --git a/posix/unistd.h b/posix/unistd.h
08c3a6
index 8224c5fbc956306f..7a61ff5e868c3456 100644
08c3a6
--- a/posix/unistd.h
08c3a6
+++ b/posix/unistd.h
08c3a6
@@ -369,7 +369,7 @@ extern void closefrom (int __lowfd) __THROW;
08c3a6
    This function is a cancellation point and therefore not marked with
08c3a6
    __THROW.  */
08c3a6
 extern ssize_t read (int __fd, void *__buf, size_t __nbytes) __wur
08c3a6
-    __attr_access ((__write_only__, 2, 3));
08c3a6
+    __fortified_attr_access (__write_only__, 2, 3);
08c3a6
 
08c3a6
 /* Write N bytes of BUF to FD.  Return the number written, or -1.
08c3a6
 
08c3a6
@@ -388,7 +388,7 @@ extern ssize_t write (int __fd, const void *__buf, size_t __n) __wur
08c3a6
    __THROW.  */
08c3a6
 extern ssize_t pread (int __fd, void *__buf, size_t __nbytes,
08c3a6
 		      __off_t __offset) __wur
08c3a6
-    __attr_access ((__write_only__, 2, 3));
08c3a6
+    __fortified_attr_access (__write_only__, 2, 3);
08c3a6
 
08c3a6
 /* Write N bytes of BUF to FD at the given position OFFSET without
08c3a6
    changing the file pointer.  Return the number written, or -1.
08c3a6
@@ -404,7 +404,7 @@ extern ssize_t pwrite (int __fd, const void *__buf, size_t __n,
08c3a6
 extern ssize_t __REDIRECT (pread, (int __fd, void *__buf, size_t __nbytes,
08c3a6
 				   __off64_t __offset),
08c3a6
 			   pread64) __wur
08c3a6
-    __attr_access ((__write_only__, 2, 3));
08c3a6
+    __fortified_attr_access (__write_only__, 2, 3);
08c3a6
 extern ssize_t __REDIRECT (pwrite, (int __fd, const void *__buf,
08c3a6
 				    size_t __nbytes, __off64_t __offset),
08c3a6
 			   pwrite64) __wur
08c3a6
@@ -421,7 +421,7 @@ extern ssize_t __REDIRECT (pwrite, (int __fd, const void *__buf,
08c3a6
    or 0 for EOF.  */
08c3a6
 extern ssize_t pread64 (int __fd, void *__buf, size_t __nbytes,
08c3a6
 			__off64_t __offset) __wur
08c3a6
-    __attr_access ((__write_only__, 2, 3));
08c3a6
+    __fortified_attr_access (__write_only__, 2, 3);
08c3a6
 /* Write N bytes of BUF to FD at the given position OFFSET without
08c3a6
    changing the file pointer.  Return the number written, or -1.  */
08c3a6
 extern ssize_t pwrite64 (int __fd, const void *__buf, size_t __n,
08c3a6
@@ -642,7 +642,7 @@ extern long int sysconf (int __name) __THROW;
08c3a6
 #ifdef	__USE_POSIX2
08c3a6
 /* Get the value of the string-valued system variable NAME.  */
08c3a6
 extern size_t confstr (int __name, char *__buf, size_t __len) __THROW
08c3a6
-    __attr_access ((__write_only__, 2, 3));
08c3a6
+    __fortified_attr_access (__write_only__, 2, 3);
08c3a6
 #endif
08c3a6
 
08c3a6
 
08c3a6
@@ -709,7 +709,7 @@ extern __gid_t getegid (void) __THROW;
08c3a6
    the calling process is in.  Otherwise, fill in the group IDs
08c3a6
    of its supplementary groups in LIST and return the number written.  */
08c3a6
 extern int getgroups (int __size, __gid_t __list[]) __THROW __wur
08c3a6
-    __attr_access ((__write_only__, 2, 1));
08c3a6
+    __fortified_attr_access (__write_only__, 2, 1);
08c3a6
 #ifdef	__USE_GNU
08c3a6
 /* Return nonzero iff the calling process is in group GID.  */
08c3a6
 extern int group_member (__gid_t __gid) __THROW;
08c3a6
@@ -801,7 +801,8 @@ extern char *ttyname (int __fd) __THROW;
08c3a6
 /* Store at most BUFLEN characters of the pathname of the terminal FD is
08c3a6
    open on in BUF.  Return 0 on success, otherwise an error number.  */
08c3a6
 extern int ttyname_r (int __fd, char *__buf, size_t __buflen)
08c3a6
-     __THROW __nonnull ((2)) __wur __attr_access ((__write_only__, 2, 3));
08c3a6
+     __THROW __nonnull ((2)) __wur
08c3a6
+     __fortified_attr_access (__write_only__, 2, 3);
08c3a6
 
08c3a6
 /* Return 1 if FD is a valid descriptor associated
08c3a6
    with a terminal, zero if not.  */
08c3a6
@@ -836,7 +837,8 @@ extern int symlink (const char *__from, const char *__to)
08c3a6
    Returns the number of characters read, or -1 for errors.  */
08c3a6
 extern ssize_t readlink (const char *__restrict __path,
08c3a6
 			 char *__restrict __buf, size_t __len)
08c3a6
-     __THROW __nonnull ((1, 2)) __wur __attr_access ((__write_only__, 2, 3));
08c3a6
+     __THROW __nonnull ((1, 2)) __wur
08c3a6
+     __fortified_attr_access (__write_only__, 2, 3);
08c3a6
 
08c3a6
 #endif /* Use POSIX.1-2001.  */
08c3a6
 
08c3a6
@@ -848,7 +850,8 @@ extern int symlinkat (const char *__from, int __tofd,
08c3a6
 /* Like readlink but a relative PATH is interpreted relative to FD.  */
08c3a6
 extern ssize_t readlinkat (int __fd, const char *__restrict __path,
08c3a6
 			   char *__restrict __buf, size_t __len)
08c3a6
-     __THROW __nonnull ((2, 3)) __wur __attr_access ((__write_only__, 3, 4));
08c3a6
+     __THROW __nonnull ((2, 3)) __wur
08c3a6
+     __fortified_attr_access (__write_only__, 3, 4);
08c3a6
 #endif
08c3a6
 
08c3a6
 /* Remove the link NAME.  */
08c3a6
@@ -884,7 +887,7 @@ extern char *getlogin (void);
08c3a6
    This function is a possible cancellation point and therefore not
08c3a6
    marked with __THROW.  */
08c3a6
 extern int getlogin_r (char *__name, size_t __name_len) __nonnull ((1))
08c3a6
-    __attr_access ((__write_only__, 1, 2));
08c3a6
+    __fortified_attr_access (__write_only__, 1, 2);
08c3a6
 #endif
08c3a6
 
08c3a6
 #ifdef	__USE_MISC
08c3a6
@@ -906,7 +909,7 @@ extern int setlogin (const char *__name) __THROW __nonnull ((1));
08c3a6
    The result is null-terminated if LEN is large enough for the full
08c3a6
    name and the terminator.  */
08c3a6
 extern int gethostname (char *__name, size_t __len) __THROW __nonnull ((1))
08c3a6
-    __attr_access ((__write_only__, 1, 2));
08c3a6
+    __fortified_attr_access (__write_only__, 1, 2);
08c3a6
 #endif
08c3a6
 
08c3a6
 
08c3a6
@@ -925,7 +928,8 @@ extern int sethostid (long int __id) __THROW __wur;
08c3a6
    Called just like `gethostname' and `sethostname'.
08c3a6
    The NIS domain name is usually the empty string when not using NIS.  */
08c3a6
 extern int getdomainname (char *__name, size_t __len)
08c3a6
-     __THROW __nonnull ((1)) __wur __attr_access ((__write_only__, 1, 2));
08c3a6
+     __THROW __nonnull ((1)) __wur
08c3a6
+     __fortified_attr_access (__write_only__, 1, 2);
08c3a6
 extern int setdomainname (const char *__name, size_t __len)
08c3a6
      __THROW __nonnull ((1)) __wur __attr_access ((__read_only__, 1, 2));
08c3a6
 
08c3a6
diff --git a/stdlib/stdlib.h b/stdlib/stdlib.h
08c3a6
index 0481c1235514f6e7..74c00eee73e4009d 100644
08c3a6
--- a/stdlib/stdlib.h
08c3a6
+++ b/stdlib/stdlib.h
08c3a6
@@ -943,7 +943,8 @@ extern size_t mbstowcs (wchar_t *__restrict  __pwcs,
08c3a6
 extern size_t wcstombs (char *__restrict __s,
08c3a6
 			const wchar_t *__restrict __pwcs, size_t __n)
08c3a6
      __THROW
08c3a6
-  __attr_access ((__write_only__, 1, 3)) __attr_access ((__read_only__, 2));
08c3a6
+  __fortified_attr_access (__write_only__, 1, 3)
08c3a6
+  __attr_access ((__read_only__, 2));
08c3a6
 
08c3a6
 #ifdef __USE_MISC
08c3a6
 /* Determine whether the string value of RESPONSE matches the affirmation
08c3a6
@@ -997,7 +998,7 @@ extern char *ptsname (int __fd) __THROW __wur;
08c3a6
    terminal associated with the master FD is open on in BUF.
08c3a6
    Return 0 on success, otherwise an error number.  */
08c3a6
 extern int ptsname_r (int __fd, char *__buf, size_t __buflen)
08c3a6
-     __THROW __nonnull ((2)) __attr_access ((__write_only__, 2, 3));
08c3a6
+     __THROW __nonnull ((2)) __fortified_attr_access (__write_only__, 2, 3);
08c3a6
 
08c3a6
 /* Open a master pseudo terminal and return its file descriptor.  */
08c3a6
 extern int getpt (void);
08c3a6
diff --git a/string/bits/string_fortified.h b/string/bits/string_fortified.h
08c3a6
index 67ae2c6b50435368..5731274848260ad2 100644
08c3a6
--- a/string/bits/string_fortified.h
08c3a6
+++ b/string/bits/string_fortified.h
08c3a6
@@ -64,7 +64,7 @@ __NTH (memset (void *__dest, int __ch, size_t __len))
08c3a6
 # include <bits/strings_fortified.h>
08c3a6
 
08c3a6
 void __explicit_bzero_chk (void *__dest, size_t __len, size_t __destlen)
08c3a6
-  __THROW __nonnull ((1)) __attr_access ((__write_only__, 1, 2));
08c3a6
+  __THROW __nonnull ((1)) __fortified_attr_access (__write_only__, 1, 2);
08c3a6
 
08c3a6
 __fortify_function void
08c3a6
 __NTH (explicit_bzero (void *__dest, size_t __len))
08c3a6
@@ -106,7 +106,8 @@ __NTH (stpncpy (char *__dest, const char *__src, size_t __n))
08c3a6
 #else
08c3a6
 extern char *__stpncpy_chk (char *__dest, const char *__src, size_t __n,
08c3a6
 			    size_t __destlen) __THROW
08c3a6
-  __attr_access ((__write_only__, 1, 3)) __attr_access ((__read_only__, 2));
08c3a6
+  __fortified_attr_access ((__write_only__, 1, 3))
08c3a6
+  __attr_access ((__read_only__, 2));
08c3a6
 extern char *__REDIRECT_NTH (__stpncpy_alias, (char *__dest, const char *__src,
08c3a6
 					       size_t __n), stpncpy);
08c3a6
 
08c3a6
diff --git a/string/string.h b/string/string.h
08c3a6
index 04e1b7067dc31d3c..8dcafb4ac4952853 100644
08c3a6
--- a/string/string.h
08c3a6
+++ b/string/string.h
08c3a6
@@ -448,7 +448,7 @@ extern char *strerror_l (int __errnum, locale_t __l) __THROW;
08c3a6
 /* Set N bytes of S to 0.  The compiler will not delete a call to this
08c3a6
    function, even if S is dead after the call.  */
08c3a6
 extern void explicit_bzero (void *__s, size_t __n) __THROW __nonnull ((1))
08c3a6
-    __attr_access ((__write_only__, 1, 2));
08c3a6
+    __fortified_attr_access (__write_only__, 1, 2);
08c3a6
 
08c3a6
 /* Return the next DELIM-delimited token from *STRINGP,
08c3a6
    terminating it with a '\0', and update *STRINGP to point past it.  */