077c9d
commit 0b79004569e5ce1669136b8c41564c3809730f15
077c9d
Author: Florian Weimer <fweimer@redhat.com>
077c9d
Date:   Tue Aug 28 12:57:46 2018 +0200
077c9d
077c9d
    regex: Add test tst-regcomp-truncated [BZ #23578]
077c9d
    
077c9d
    (cherry picked from commit 761404b74d9853ce1608195e24f25b78a910591a)
077c9d
077c9d
diff --git a/posix/Makefile b/posix/Makefile
077c9d
index 00c62841a282f15a..83162123f9c927a0 100644
077c9d
--- a/posix/Makefile
077c9d
+++ b/posix/Makefile
077c9d
@@ -96,7 +96,7 @@ tests		:= test-errno tstgetopt testfnm runtests runptests \
077c9d
 		   tst-posix_fadvise tst-posix_fadvise64 \
077c9d
 		   tst-sysconf-empty-chroot tst-glob_symlinks tst-fexecve \
077c9d
 		   tst-glob-tilde test-ssize-max tst-spawn4 bug-regex37 \
077c9d
-		   bug-regex38
077c9d
+		   bug-regex38 tst-regcomp-truncated
077c9d
 tests-internal	:= bug-regex5 bug-regex20 bug-regex33 \
077c9d
 		   tst-rfc3484 tst-rfc3484-2 tst-rfc3484-3 \
077c9d
 		   tst-glob_lstat_compat tst-spawn4-compat
077c9d
@@ -194,6 +194,7 @@ $(objpfx)tst-regex2.out: $(gen-locales)
077c9d
 $(objpfx)tst-regexloc.out: $(gen-locales)
077c9d
 $(objpfx)tst-rxspencer.out: $(gen-locales)
077c9d
 $(objpfx)tst-rxspencer-no-utf8.out: $(gen-locales)
077c9d
+$(objpfx)tst-regcomp-truncated.out: $(gen-locales)
077c9d
 endif
077c9d
 
077c9d
 # If we will use the generic uname implementation, we must figure out what
077c9d
diff --git a/posix/tst-regcomp-truncated.c b/posix/tst-regcomp-truncated.c
077c9d
new file mode 100644
077c9d
index 0000000000000000..a4a1581bbc2b39eb
077c9d
--- /dev/null
077c9d
+++ b/posix/tst-regcomp-truncated.c
077c9d
@@ -0,0 +1,191 @@
077c9d
+/* Test compilation of truncated regular expressions.
077c9d
+   Copyright (C) 2018 Free Software Foundation, Inc.
077c9d
+   This file is part of the GNU C Library.
077c9d
+
077c9d
+   The GNU C Library is free software; you can redistribute it and/or
077c9d
+   modify it under the terms of the GNU Lesser General Public
077c9d
+   License as published by the Free Software Foundation; either
077c9d
+   version 2.1 of the License, or (at your option) any later version.
077c9d
+
077c9d
+   The GNU C Library is distributed in the hope that it will be useful,
077c9d
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
077c9d
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
077c9d
+   Lesser General Public License for more details.
077c9d
+
077c9d
+   You should have received a copy of the GNU Lesser General Public
077c9d
+   License along with the GNU C Library; if not, see
077c9d
+   <http://www.gnu.org/licenses/>.  */
077c9d
+
077c9d
+/* This test constructs various patterns in an attempt to trigger
077c9d
+   over-reading the regular expression compiler, such as bug
077c9d
+   23578.  */
077c9d
+
077c9d
+#include <array_length.h>
077c9d
+#include <errno.h>
077c9d
+#include <locale.h>
077c9d
+#include <regex.h>
077c9d
+#include <stdio.h>
077c9d
+#include <stdlib.h>
077c9d
+#include <string.h>
077c9d
+#include <support/check.h>
077c9d
+#include <support/next_to_fault.h>
077c9d
+#include <support/support.h>
077c9d
+#include <support/test-driver.h>
077c9d
+#include <wchar.h>
077c9d
+
077c9d
+/* Locales to test.  */
077c9d
+static const char locales[][17] =
077c9d
+  {
077c9d
+    "C",
077c9d
+    "en_US.UTF-8",
077c9d
+    "de_DE.ISO-8859-1",
077c9d
+  };
077c9d
+
077c9d
+/* Syntax options.  Will be combined with other flags.  */
077c9d
+static const reg_syntax_t syntaxes[] =
077c9d
+  {
077c9d
+    RE_SYNTAX_EMACS,
077c9d
+    RE_SYNTAX_AWK,
077c9d
+    RE_SYNTAX_GNU_AWK,
077c9d
+    RE_SYNTAX_POSIX_AWK,
077c9d
+    RE_SYNTAX_GREP,
077c9d
+    RE_SYNTAX_EGREP,
077c9d
+    RE_SYNTAX_POSIX_EGREP,
077c9d
+    RE_SYNTAX_POSIX_BASIC,
077c9d
+    RE_SYNTAX_POSIX_EXTENDED,
077c9d
+    RE_SYNTAX_POSIX_MINIMAL_EXTENDED,
077c9d
+  };
077c9d
+
077c9d
+/* Trailing characters placed after the initial character.  */
077c9d
+static const char trailing_strings[][4] =
077c9d
+  {
077c9d
+    "",
077c9d
+    "[",
077c9d
+    "\\",
077c9d
+    "[\\",
077c9d
+    "(",
077c9d
+    "(\\",
077c9d
+    "\\(",
077c9d
+  };
077c9d
+
077c9d
+static int
077c9d
+do_test (void)
077c9d
+{
077c9d
+  /* Staging buffer for the constructed regular expression.  */
077c9d
+  char buffer[16];
077c9d
+
077c9d
+  /* Allocation used to detect over-reading by the regular expression
077c9d
+     compiler.  */
077c9d
+  struct support_next_to_fault ntf
077c9d
+    = support_next_to_fault_allocate (sizeof (buffer));
077c9d
+
077c9d
+  /* Arbitrary Unicode codepoint at which we stop generating
077c9d
+     characters.  We do not probe the whole range because that would
077c9d
+     take too long due to combinatorical exploision as the result of
077c9d
+     combination with other flags.  */
077c9d
+  static const wchar_t last_character = 0xfff;
077c9d
+
077c9d
+  for (size_t locale_idx = 0; locale_idx < array_length (locales);
077c9d
+       ++ locale_idx)
077c9d
+    {
077c9d
+      if (setlocale (LC_ALL, locales[locale_idx]) == NULL)
077c9d
+        {
077c9d
+          support_record_failure ();
077c9d
+          printf ("error: setlocale (\"%s\"): %m", locales[locale_idx]);
077c9d
+          continue;
077c9d
+        }
077c9d
+      if (test_verbose > 0)
077c9d
+        printf ("info: testing locale \"%s\"\n", locales[locale_idx]);
077c9d
+
077c9d
+      for (wchar_t wc = 0; wc <= last_character; ++wc)
077c9d
+        {
077c9d
+          char *after_wc;
077c9d
+          if (wc == 0)
077c9d
+            {
077c9d
+              /* wcrtomb treats L'\0' in a special way.  */
077c9d
+              *buffer = '\0';
077c9d
+              after_wc = &buffer[1];
077c9d
+            }
077c9d
+          else
077c9d
+            {
077c9d
+              mbstate_t ps = { };
077c9d
+              size_t ret = wcrtomb (buffer, wc, &ps);
077c9d
+              if (ret == (size_t) -1)
077c9d
+                {
077c9d
+                  /* EILSEQ means that the target character set
077c9d
+                     cannot encode the character.  */
077c9d
+                  if (errno != EILSEQ)
077c9d
+                    {
077c9d
+                      support_record_failure ();
077c9d
+                      printf ("error: wcrtomb (0x%x) failed: %m\n",
077c9d
+                              (unsigned) wc);
077c9d
+                    }
077c9d
+                  continue;
077c9d
+                }
077c9d
+              TEST_VERIFY_EXIT (ret != 0);
077c9d
+              after_wc = &buffer[ret];
077c9d
+            }
077c9d
+
077c9d
+          for (size_t trailing_idx = 0;
077c9d
+               trailing_idx < array_length (trailing_strings);
077c9d
+               ++trailing_idx)
077c9d
+            {
077c9d
+              char *after_trailing
077c9d
+                = stpcpy (after_wc, trailing_strings[trailing_idx]);
077c9d
+
077c9d
+              for (int do_nul = 0; do_nul < 2; ++do_nul)
077c9d
+                {
077c9d
+                  char *after_nul;
077c9d
+                  if (do_nul)
077c9d
+                    {
077c9d
+                      *after_trailing = '\0';
077c9d
+                      after_nul = &after_trailing[1];
077c9d
+                    }
077c9d
+                  else
077c9d
+                    after_nul = after_trailing;
077c9d
+
077c9d
+                  size_t length = after_nul - buffer;
077c9d
+
077c9d
+                  /* Make sure that the faulting region starts
077c9d
+                     after the used portion of the buffer.  */
077c9d
+                  char *ntf_start = ntf.buffer + sizeof (buffer) - length;
077c9d
+                  memcpy (ntf_start, buffer, length);
077c9d
+
077c9d
+                  for (const reg_syntax_t *psyntax = syntaxes;
077c9d
+                       psyntax < array_end (syntaxes); ++psyntax)
077c9d
+                    for (int do_icase = 0; do_icase < 2; ++do_icase)
077c9d
+                      {
077c9d
+                        re_syntax_options = *psyntax;
077c9d
+                        if (do_icase)
077c9d
+                          re_syntax_options |= RE_ICASE;
077c9d
+
077c9d
+                        regex_t reg;
077c9d
+                        memset (&reg, 0, sizeof (reg));
077c9d
+                        const char *msg = re_compile_pattern
077c9d
+                          (ntf_start, length, ®);
077c9d
+                        if (msg != NULL)
077c9d
+                          {
077c9d
+                            if (test_verbose > 0)
077c9d
+                              {
077c9d
+                                char *quoted = support_quote_blob
077c9d
+                                  (buffer, length);
077c9d
+                                printf ("info: compilation failed for pattern"
077c9d
+                                        " \"%s\", syntax 0x%lx: %s\n",
077c9d
+                                        quoted, re_syntax_options, msg);
077c9d
+                                free (quoted);
077c9d
+                              }
077c9d
+                          }
077c9d
+                        else
077c9d
+                          regfree (®);
077c9d
+                      }
077c9d
+                }
077c9d
+            }
077c9d
+        }
077c9d
+    }
077c9d
+
077c9d
+  support_next_to_fault_free (&ntf;;
077c9d
+
077c9d
+  return 0;
077c9d
+}
077c9d
+
077c9d
+#include <support/test-driver.c>