c5d972
commit fcfc9086815bf0d277ad47a90ee3fda4c37acca8
c5d972
Author: Siddhesh Poyarekar <siddhesh@sourceware.org>
c5d972
Date:   Wed Jan 12 23:34:48 2022 +0530
c5d972
c5d972
    debug: Synchronize feature guards in fortified functions [BZ #28746]
c5d972
    
c5d972
    Some functions (e.g. stpcpy, pread64, etc.) had moved to POSIX in the
c5d972
    main headers as they got incorporated into the standard, but their
c5d972
    fortified variants remained under __USE_GNU.  As a result, these
c5d972
    functions did not get fortified when _GNU_SOURCE was not defined.
c5d972
    
c5d972
    Add test wrappers that check all functions tested in tst-chk0 at all
c5d972
    levels with _GNU_SOURCE undefined and then use the failures to (1)
c5d972
    exclude checks for _GNU_SOURCE functions in these tests and (2) Fix
c5d972
    feature macro guards in the fortified function headers so that they're
c5d972
    the same as the ones in the main headers.
c5d972
    
c5d972
    This fixes BZ #28746.
c5d972
    
c5d972
    Signed-off-by: Siddhesh Poyarekar <siddhesh@sourceware.org>
c5d972
    Reviewed-by: Adhemerval Zanella  <adhemerval.zanella@linaro.org>
c5d972
c5d972
# Conflicts:
c5d972
#	debug/tst-fortify.c
c5d972
c5d972
diff --git a/debug/Makefile b/debug/Makefile
c5d972
index c92fd23dda1a7279..b0f0b7beb6d5cef5 100644
c5d972
--- a/debug/Makefile
c5d972
+++ b/debug/Makefile
c5d972
@@ -132,6 +132,12 @@ define cflags-lfs
c5d972
 CFLAGS-tst-fortify-$(1)-lfs-$(2).$(1) += -D_FILE_OFFSET_BITS=64
c5d972
 endef
c5d972
 
c5d972
+define cflags-nongnu
c5d972
+CFLAGS-tst-fortify-$(1)-nongnu-$(2).$(1) += -D_LARGEFILE64_SOURCE=1
c5d972
+endef
c5d972
+
c5d972
+src-chk-nongnu = \#undef _GNU_SOURCE
c5d972
+
c5d972
 # We know these tests have problems with format strings, this is what
c5d972
 # we are testing.  Disable that warning.  They are also testing
c5d972
 # deprecated functions (notably gets) so disable that warning as well.
c5d972
@@ -145,13 +151,13 @@ CFLAGS-tst-fortify-$(1)-$(2)-$(3).$(1) += -D_FORTIFY_SOURCE=$(3) -Wno-format \
c5d972
 $(eval $(call cflags-$(2),$(1),$(3)))
c5d972
 $(objpfx)tst-fortify-$(1)-$(2)-$(3).$(1): tst-fortify.c Makefile
c5d972
 	( echo "/* Autogenerated from Makefile.  */"; \
c5d972
-	  echo ""; \
c5d972
+	  echo "$(src-chk-$(2))"; \
c5d972
 	  echo "#include \"tst-fortify.c\"" ) > $$@.tmp
c5d972
 	mv $$@.tmp $$@
c5d972
 endef
c5d972
 
c5d972
 chk-extensions = c cc
c5d972
-chk-types = default lfs
c5d972
+chk-types = default lfs nongnu
c5d972
 chk-levels = 1 2 3
c5d972
 
c5d972
 $(foreach e,$(chk-extensions), \
c5d972
diff --git a/debug/tst-fortify.c b/debug/tst-fortify.c
c5d972
index 5e76081255316a93..1668294e48b5c63c 100644
c5d972
--- a/debug/tst-fortify.c
c5d972
+++ b/debug/tst-fortify.c
c5d972
@@ -1,4 +1,5 @@
c5d972
-/* Copyright (C) 2004-2018 Free Software Foundation, Inc.
c5d972
+/* Copyright (C) 2004-2022 Free Software Foundation, Inc.
c5d972
+   Copyright The GNU Toolchain Authors.
c5d972
    This file is part of the GNU C Library.
c5d972
    Contributed by Jakub Jelinek <jakub@redhat.com>, 2004.
c5d972
 
c5d972
@@ -37,6 +38,17 @@
c5d972
 #include <sys/socket.h>
c5d972
 #include <sys/un.h>
c5d972
 
c5d972
+#ifndef _GNU_SOURCE
c5d972
+# define MEMPCPY memcpy
c5d972
+# define WMEMPCPY wmemcpy
c5d972
+# define MEMPCPY_RET(x) 0
c5d972
+# define WMEMPCPY_RET(x) 0
c5d972
+#else
c5d972
+# define MEMPCPY mempcpy
c5d972
+# define WMEMPCPY wmempcpy
c5d972
+# define MEMPCPY_RET(x) __builtin_strlen (x)
c5d972
+# define WMEMPCPY_RET(x) wcslen (x)
c5d972
+#endif
c5d972
 
c5d972
 #define obstack_chunk_alloc malloc
c5d972
 #define obstack_chunk_free free
c5d972
@@ -163,7 +175,7 @@ do_test (void)
c5d972
   if (memcmp (buf, "aabcdefghi", 10))
c5d972
     FAIL ();
c5d972
 
c5d972
-  if (mempcpy (buf + 5, "abcde", 5) != buf + 10
c5d972
+  if (MEMPCPY (buf + 5, "abcde", 5) != buf + 5 + MEMPCPY_RET ("abcde")
c5d972
       || memcmp (buf, "aabcdabcde", 10))
c5d972
     FAIL ();
c5d972
 
c5d972
@@ -208,7 +220,7 @@ do_test (void)
c5d972
   if (memcmp (buf, "aabcdefghi", 10))
c5d972
     FAIL ();
c5d972
 
c5d972
-  if (mempcpy (buf + 5, "abcde", l0 + 5) != buf + 10
c5d972
+  if (MEMPCPY (buf + 5, "abcde", l0 + 5) != buf + 5 + MEMPCPY_RET ("abcde")
c5d972
       || memcmp (buf, "aabcdabcde", 10))
c5d972
     FAIL ();
c5d972
 
c5d972
@@ -267,7 +279,8 @@ do_test (void)
c5d972
   if (memcmp (a.buf1, "aabcdefghi", 10))
c5d972
     FAIL ();
c5d972
 
c5d972
-  if (mempcpy (a.buf1 + 5, "abcde", l0 + 5) != a.buf1 + 10
c5d972
+  if (MEMPCPY (a.buf1 + 5, "abcde", l0 + 5)
c5d972
+      != a.buf1 + 5 + MEMPCPY_RET ("abcde")
c5d972
       || memcmp (a.buf1, "aabcdabcde", 10))
c5d972
     FAIL ();
c5d972
 
c5d972
@@ -348,6 +361,7 @@ do_test (void)
c5d972
   bcopy (buf + 1, buf + 2, l0 + 9);
c5d972
   CHK_FAIL_END
c5d972
 
c5d972
+#ifdef _GNU_SOURCE
c5d972
   CHK_FAIL_START
c5d972
   p = (char *) mempcpy (buf + 6, "abcde", 5);
c5d972
   CHK_FAIL_END
c5d972
@@ -355,6 +369,7 @@ do_test (void)
c5d972
   CHK_FAIL_START
c5d972
   p = (char *) mempcpy (buf + 6, "abcde", l0 + 5);
c5d972
   CHK_FAIL_END
c5d972
+#endif
c5d972
 
c5d972
   CHK_FAIL_START
c5d972
   memset (buf + 9, 'j', 2);
c5d972
@@ -465,6 +480,7 @@ do_test (void)
c5d972
   bcopy (a.buf1 + 1, a.buf1 + 2, l0 + 9);
c5d972
   CHK_FAIL_END
c5d972
 
c5d972
+#ifdef _GNU_SOURCE
c5d972
   CHK_FAIL_START
c5d972
   p = (char *) mempcpy (a.buf1 + 6, "abcde", 5);
c5d972
   CHK_FAIL_END
c5d972
@@ -472,6 +488,7 @@ do_test (void)
c5d972
   CHK_FAIL_START
c5d972
   p = (char *) mempcpy (a.buf1 + 6, "abcde", l0 + 5);
c5d972
   CHK_FAIL_END
c5d972
+#endif
c5d972
 
c5d972
   CHK_FAIL_START
c5d972
   memset (a.buf1 + 9, 'j', 2);
c5d972
@@ -551,7 +568,7 @@ do_test (void)
c5d972
   if (wmemcmp (wbuf, L"aabcdefghi", 10))
c5d972
     FAIL ();
c5d972
 
c5d972
-  if (wmempcpy (wbuf + 5, L"abcde", 5) != wbuf + 10
c5d972
+  if (WMEMPCPY (wbuf + 5, L"abcde", 5) != wbuf + 5 + WMEMPCPY_RET (L"abcde")
c5d972
       || wmemcmp (wbuf, L"aabcdabcde", 10))
c5d972
     FAIL ();
c5d972
 
c5d972
@@ -584,7 +601,8 @@ do_test (void)
c5d972
   if (wmemcmp (wbuf, L"aabcdefghi", 10))
c5d972
     FAIL ();
c5d972
 
c5d972
-  if (wmempcpy (wbuf + 5, L"abcde", l0 + 5) != wbuf + 10
c5d972
+  if (WMEMPCPY (wbuf + 5, L"abcde", l0 + 5)
c5d972
+      != wbuf + 5 + WMEMPCPY_RET (L"abcde")
c5d972
       || wmemcmp (wbuf, L"aabcdabcde", 10))
c5d972
     FAIL ();
c5d972
 
c5d972
@@ -626,7 +644,8 @@ do_test (void)
c5d972
   if (wmemcmp (wa.buf1, L"aabcdefghi", 10))
c5d972
     FAIL ();
c5d972
 
c5d972
-  if (wmempcpy (wa.buf1 + 5, L"abcde", l0 + 5) != wa.buf1 + 10
c5d972
+  if (WMEMPCPY (wa.buf1 + 5, L"abcde", l0 + 5)
c5d972
+      != wa.buf1 + 5 + WMEMPCPY_RET (L"abcde")
c5d972
       || wmemcmp (wa.buf1, L"aabcdabcde", 10))
c5d972
     FAIL ();
c5d972
 
c5d972
@@ -695,6 +714,7 @@ do_test (void)
c5d972
   wmemmove (wbuf + 2, wbuf + 1, l0 + 9);
c5d972
   CHK_FAIL_END
c5d972
 
c5d972
+#ifdef _GNU_SOURCE
c5d972
   CHK_FAIL_START
c5d972
   wp = wmempcpy (wbuf + 6, L"abcde", 5);
c5d972
   CHK_FAIL_END
c5d972
@@ -702,6 +722,7 @@ do_test (void)
c5d972
   CHK_FAIL_START
c5d972
   wp = wmempcpy (wbuf + 6, L"abcde", l0 + 5);
c5d972
   CHK_FAIL_END
c5d972
+#endif
c5d972
 
c5d972
   CHK_FAIL_START
c5d972
   wmemset (wbuf + 9, L'j', 2);
c5d972
@@ -769,6 +790,7 @@ do_test (void)
c5d972
   wmemmove (wa.buf1 + 2, wa.buf1 + 1, l0 + 9);
c5d972
   CHK_FAIL_END
c5d972
 
c5d972
+#ifdef _GNU_SOURCE
c5d972
   CHK_FAIL_START
c5d972
   wp = wmempcpy (wa.buf1 + 6, L"abcde", 5);
c5d972
   CHK_FAIL_END
c5d972
@@ -776,6 +798,7 @@ do_test (void)
c5d972
   CHK_FAIL_START
c5d972
   wp = wmempcpy (wa.buf1 + 6, L"abcde", l0 + 5);
c5d972
   CHK_FAIL_END
c5d972
+#endif
c5d972
 
c5d972
   CHK_FAIL_START
c5d972
   wmemset (wa.buf1 + 9, L'j', 2);
c5d972
@@ -907,6 +930,7 @@ do_test (void)
c5d972
   if (fprintf (fp, buf2 + 4, str5) != 7)
c5d972
     FAIL ();
c5d972
 
c5d972
+#ifdef _GNU_SOURCE
c5d972
   char *my_ptr = NULL;
c5d972
   strcpy (buf2 + 2, "%n%s%n");
c5d972
   /* When the format string is writable and contains %n,
c5d972
@@ -936,6 +960,7 @@ do_test (void)
c5d972
   if (obstack_printf (&obs, "%s%n%s%n", str4, &n1, str5, &n1) != 14)
c5d972
     FAIL ();
c5d972
   obstack_free (&obs, NULL);
c5d972
+#endif
c5d972
 
c5d972
   if (freopen (temp_filename, "r", stdin) == NULL)
c5d972
     {
c5d972
@@ -983,6 +1008,7 @@ do_test (void)
c5d972
 
c5d972
   rewind (stdin);
c5d972
 
c5d972
+#ifdef _GNU_SOURCE
c5d972
   if (fgets_unlocked (buf, buf_size, stdin) != buf
c5d972
       || memcmp (buf, "abcdefgh\n", 10))
c5d972
     FAIL ();
c5d972
@@ -1009,6 +1035,7 @@ do_test (void)
c5d972
 #endif
c5d972
 
c5d972
   rewind (stdin);
c5d972
+#endif
c5d972
 
c5d972
   if (fread (buf, 1, buf_size, stdin) != buf_size
c5d972
       || memcmp (buf, "abcdefgh\nA", 10))
c5d972
@@ -1579,7 +1606,10 @@ do_test (void)
c5d972
       ret = 1;
c5d972
     }
c5d972
 
c5d972
-  int fd = posix_openpt (O_RDWR);
c5d972
+  int fd;
c5d972
+
c5d972
+#ifdef _GNU_SOURCE
c5d972
+  fd = posix_openpt (O_RDWR);
c5d972
   if (fd != -1)
c5d972
     {
c5d972
       char enough[1000];
c5d972
@@ -1595,6 +1625,7 @@ do_test (void)
c5d972
 #endif
c5d972
       close (fd);
c5d972
     }
c5d972
+#endif
c5d972
 
c5d972
 #if PATH_MAX > 0
c5d972
   confstr (_CS_GNU_LIBC_VERSION, largebuf, sizeof (largebuf));
c5d972
@@ -1712,8 +1743,9 @@ do_test (void)
c5d972
   poll (fds, l0 + 2, 0);
c5d972
   CHK_FAIL_END
c5d972
 #endif
c5d972
+#ifdef _GNU_SOURCE
c5d972
   ppoll (fds, 1, NULL, NULL);
c5d972
-#if __USE_FORTIFY_LEVEL >= 1
c5d972
+# if __USE_FORTIFY_LEVEL >= 1
c5d972
   CHK_FAIL_START
c5d972
   ppoll (fds, 2, NULL, NULL);
c5d972
   CHK_FAIL_END
c5d972
@@ -1721,6 +1753,7 @@ do_test (void)
c5d972
   CHK_FAIL_START
c5d972
   ppoll (fds, l0 + 2, NULL, NULL);
c5d972
   CHK_FAIL_END
c5d972
+# endif
c5d972
 #endif
c5d972
 
c5d972
   return ret;
c5d972
diff --git a/posix/bits/unistd.h b/posix/bits/unistd.h
c5d972
index a456d1723547db70..ddfaed4dd7574cd2 100644
c5d972
--- a/posix/bits/unistd.h
c5d972
+++ b/posix/bits/unistd.h
c5d972
@@ -38,7 +38,7 @@ read (int __fd, void *__buf, size_t __nbytes)
c5d972
 			  __fd, __buf, __nbytes);
c5d972
 }
c5d972
 
c5d972
-#ifdef __USE_UNIX98
c5d972
+#if defined __USE_UNIX98 || defined __USE_XOPEN2K8
c5d972
 extern ssize_t __pread_chk (int __fd, void *__buf, size_t __nbytes,
c5d972
 			    __off_t __offset, size_t __bufsize) __wur;
c5d972
 extern ssize_t __pread64_chk (int __fd, void *__buf, size_t __nbytes,
c5d972
diff --git a/string/bits/string_fortified.h b/string/bits/string_fortified.h
c5d972
index 27ec273ec41cd81c..3f86629bf8fc51a2 100644
c5d972
--- a/string/bits/string_fortified.h
c5d972
+++ b/string/bits/string_fortified.h
c5d972
@@ -94,7 +94,7 @@ __NTH (strcpy (char *__restrict __dest, const char *__restrict __src))
c5d972
   return __builtin___strcpy_chk (__dest, __src, __glibc_objsize (__dest));
c5d972
 }
c5d972
 
c5d972
-#ifdef __USE_GNU
c5d972
+#ifdef __USE_XOPEN2K8
c5d972
 __fortify_function char *
c5d972
 __NTH (stpcpy (char *__restrict __dest, const char *__restrict __src))
c5d972
 {
c5d972
@@ -111,14 +111,15 @@ __NTH (strncpy (char *__restrict __dest, const char *__restrict __src,
c5d972
 				  __glibc_objsize (__dest));
c5d972
 }
c5d972
 
c5d972
-#if __GNUC_PREREQ (4, 7) || __glibc_clang_prereq (2, 6)
c5d972
+#ifdef __USE_XOPEN2K8
c5d972
+# if __GNUC_PREREQ (4, 7) || __glibc_clang_prereq (2, 6)
c5d972
 __fortify_function char *
c5d972
 __NTH (stpncpy (char *__dest, const char *__src, size_t __n))
c5d972
 {
c5d972
   return __builtin___stpncpy_chk (__dest, __src, __n,
c5d972
 				  __glibc_objsize (__dest));
c5d972
 }
c5d972
-#else
c5d972
+# else
c5d972
 extern char *__stpncpy_chk (char *__dest, const char *__src, size_t __n,
c5d972
 			    size_t __destlen) __THROW;
c5d972
 extern char *__REDIRECT_NTH (__stpncpy_alias, (char *__dest, const char *__src,
c5d972
@@ -132,6 +133,7 @@ __NTH (stpncpy (char *__dest, const char *__src, size_t __n))
c5d972
     return __stpncpy_chk (__dest, __src, __n, __bos (__dest));
c5d972
   return __stpncpy_alias (__dest, __src, __n);
c5d972
 }
c5d972
+# endif
c5d972
 #endif
c5d972
 
c5d972
 
c5d972
diff --git a/support/xsignal.h b/support/xsignal.h
c5d972
index 9ab8d1bfddf6c598..fae6108a522ae5fe 100644
c5d972
--- a/support/xsignal.h
c5d972
+++ b/support/xsignal.h
c5d972
@@ -28,7 +28,9 @@ __BEGIN_DECLS
c5d972
    terminate the process on error.  */
c5d972
 
c5d972
 void xraise (int sig);
c5d972
+#ifdef _GNU_SOURCE
c5d972
 sighandler_t xsignal (int sig, sighandler_t handler);
c5d972
+#endif
c5d972
 void xsigaction (int sig, const struct sigaction *newact,
c5d972
                  struct sigaction *oldact);
c5d972
 
c5d972
diff --git a/wcsmbs/bits/wchar2.h b/wcsmbs/bits/wchar2.h
c5d972
index f82bba481981e4fb..5c68979e96a504b4 100644
c5d972
--- a/wcsmbs/bits/wchar2.h
c5d972
+++ b/wcsmbs/bits/wchar2.h
c5d972
@@ -457,7 +457,7 @@ __NTH (wcsrtombs (char *__restrict __dst, const wchar_t **__restrict __src,
c5d972
 }
c5d972
 
c5d972
 
c5d972
-#ifdef __USE_GNU
c5d972
+#ifdef	__USE_XOPEN2K8
c5d972
 extern size_t __mbsnrtowcs_chk (wchar_t *__restrict __dst,
c5d972
 				const char **__restrict __src, size_t __nmc,
c5d972
 				size_t __len, mbstate_t *__restrict __ps,