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