00db10
commit c2c6d39fab901c97c18fa3a3a3658d9dc3f7df61
00db10
Author: Paul Pluzhnikov <ppluzhnikov@google.com>
00db10
Date:   Mon Mar 2 13:34:22 2015 -0800
00db10
00db10
    Fix BZ 18036 buffer overflow (read past end of buffer) in internal_fnmatch
00db10
00db10
--- glibc-2.17-c758a686/posix/fnmatch_loop.c
00db10
+++ glibc-2.17-c758a686/posix/fnmatch_loop.c
00db10
@@ -1036,7 +1036,12 @@ END (const CHAR *pattern)
00db10
       }
00db10
     else if ((*p == L('?') || *p == L('*') || *p == L('+') || *p == L('@')
00db10
 	      || *p == L('!')) && p[1] == L('('))
00db10
-      p = END (p + 1);
00db10
+      {
00db10
+	p = END (p + 1);
00db10
+	if (*p == L('\0'))
00db10
+	  /* This is an invalid pattern.  */
00db10
+	  return pattern;
00db10
+      }
00db10
     else if (*p == L(')'))
00db10
       break;
00db10
 
00db10
diff --git glibc-2.17-c758a686/posix/tst-fnmatch3.c glibc-2.17-c758a686/posix/tst-fnmatch3.c
00db10
index 75bc00a..fdf9934 100644
00db10
--- glibc-2.17-c758a686/posix/tst-fnmatch3.c
00db10
+++ glibc-2.17-c758a686/posix/tst-fnmatch3.c
00db10
@@ -17,6 +17,26 @@
00db10
    <http://www.gnu.org/licenses/>.  */
00db10
 
00db10
 #include <fnmatch.h>
00db10
+#include <sys/mman.h>
00db10
+#include <string.h>
00db10
+#include <unistd.h>
00db10
+
00db10
+int
00db10
+do_bz18036 (void)
00db10
+{
00db10
+  const char p[] = "**(!()";
00db10
+  const int pagesize = getpagesize ();
00db10
+
00db10
+  char *pattern = mmap (0, 2 * pagesize, PROT_READ|PROT_WRITE,
00db10
+                        MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
00db10
+  if (pattern == MAP_FAILED) return 1;
00db10
+
00db10
+  mprotect (pattern + pagesize, pagesize, PROT_NONE);
00db10
+  memset (pattern, ' ', pagesize);
00db10
+  strcpy (pattern, p);
00db10
+
00db10
+  return fnmatch (pattern, p, FNM_EXTMATCH);
00db10
+}
00db10
 
00db10
 int
00db10
 do_test (void)
00db10
@@ -25,7 +45,7 @@ do_test (void)
00db10
     return 1;
00db10
   if (fnmatch ("[a[.\0.]]", "a", 0) != FNM_NOMATCH)
00db10
     return 1;
00db10
-  return 0;
00db10
+  return do_bz18036 ();
00db10
 }
00db10
 
00db10
 #define TEST_FUNCTION do_test ()