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