arrfab / rpms / glibc

Forked from rpms/glibc 5 years ago
Clone

Blame SOURCES/glibc-rh1579742.patch

147e83
commit 5460617d1567657621107d895ee2dd83bc1f88f2
147e83
Author: Paul Pluzhnikov <ppluzhnikov@google.com>
147e83
Date:   Tue May 8 18:12:41 2018 -0700
147e83
147e83
    Fix BZ 22786: integer addition overflow may cause stack buffer overflow
147e83
    when realpath() input length is close to SSIZE_MAX.
147e83
    
147e83
    2018-05-09  Paul Pluzhnikov  <ppluzhnikov@google.com>
147e83
    
147e83
            [BZ #22786]
147e83
            * stdlib/canonicalize.c (__realpath): Fix overflow in path length
147e83
            computation.
147e83
            * stdlib/Makefile (test-bz22786): New test.
147e83
            * stdlib/test-bz22786.c: New test.
147e83
147e83
diff --git a/stdlib/Makefile b/stdlib/Makefile
147e83
index b5553eafc2a4bbd5..7265b44a5082b991 100644
147e83
--- a/stdlib/Makefile
147e83
+++ b/stdlib/Makefile
147e83
@@ -72,7 +72,7 @@ tests		:= tst-strtol tst-strtod testmb testrand testsort testdiv   \
147e83
 		   tst-makecontext3 bug-getcontext bug-fmtmsg1		    \
147e83
 		   tst-secure-getenv tst-strtod-overflow tst-strtod-round   \
147e83
 		   tst-tininess tst-strtod-underflow tst-strfmon_l	    \
147e83
-		   tst-makecontext-align
147e83
+		   tst-makecontext-align test-bz22786
147e83
 tests-static	:= tst-secure-getenv
147e83
 
147e83
 include ../Makeconfig
147e83
diff --git a/stdlib/canonicalize.c b/stdlib/canonicalize.c
147e83
index aeff804c108de277..b43f6223b98a578a 100644
147e83
--- a/stdlib/canonicalize.c
147e83
+++ b/stdlib/canonicalize.c
147e83
@@ -181,7 +181,7 @@ __realpath (const char *name, char *resolved)
147e83
 		extra_buf = __alloca (path_max);
147e83
 
147e83
 	      len = strlen (end);
147e83
-	      if ((long int) (n + len) >= path_max)
147e83
+	      if (path_max - n <= len)
147e83
 		{
147e83
 		  __set_errno (ENAMETOOLONG);
147e83
 		  goto error;
147e83
diff --git a/stdlib/test-bz22786.c b/stdlib/test-bz22786.c
147e83
new file mode 100644
147e83
index 0000000000000000..e7837f98c19fc4bf
147e83
--- /dev/null
147e83
+++ b/stdlib/test-bz22786.c
147e83
@@ -0,0 +1,90 @@
147e83
+/* Bug 22786: test for buffer overflow in realpath.
147e83
+   Copyright (C) 2018 Free Software Foundation, Inc.
147e83
+   This file is part of the GNU C Library.
147e83
+
147e83
+   The GNU C Library is free software; you can redistribute it and/or
147e83
+   modify it under the terms of the GNU Lesser General Public
147e83
+   License as published by the Free Software Foundation; either
147e83
+   version 2.1 of the License, or (at your option) any later version.
147e83
+
147e83
+   The GNU C Library is distributed in the hope that it will be useful,
147e83
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
147e83
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
147e83
+   Lesser General Public License for more details.
147e83
+
147e83
+   You should have received a copy of the GNU Lesser General Public
147e83
+   License along with the GNU C Library; if not, see
147e83
+   <http://www.gnu.org/licenses/>.  */
147e83
+
147e83
+/* This file must be run from within a directory called "stdlib".  */
147e83
+
147e83
+#include <errno.h>
147e83
+#include <limits.h>
147e83
+#include <stdio.h>
147e83
+#include <stdlib.h>
147e83
+#include <string.h>
147e83
+#include <unistd.h>
147e83
+#include <sys/stat.h>
147e83
+#include <sys/types.h>
147e83
+#include <support/test-driver.h>
147e83
+#include <libc-diag.h>
147e83
+
147e83
+static int
147e83
+do_test (void)
147e83
+{
147e83
+  const char dir[] = "bz22786";
147e83
+  const char lnk[] = "bz22786/symlink";
147e83
+
147e83
+  rmdir (dir);
147e83
+  if (mkdir (dir, 0755) != 0 && errno != EEXIST)
147e83
+    {
147e83
+      printf ("mkdir %s: %m\n", dir);
147e83
+      return EXIT_FAILURE;
147e83
+    }
147e83
+  if (symlink (".", lnk) != 0 && errno != EEXIST)
147e83
+    {
147e83
+      printf ("symlink (%s, %s): %m\n", dir, lnk);
147e83
+      return EXIT_FAILURE;
147e83
+    }
147e83
+
147e83
+  const size_t path_len = (size_t) INT_MAX + 1;
147e83
+
147e83
+  DIAG_PUSH_NEEDS_COMMENT;
147e83
+#if __GNUC_PREREQ (7, 0)
147e83
+  /* GCC 7 warns about too-large allocations; here we need such
147e83
+     allocation to succeed for the test to work.  */
147e83
+  DIAG_IGNORE_NEEDS_COMMENT (7, "-Walloc-size-larger-than=");
147e83
+#endif
147e83
+  char *path = malloc (path_len);
147e83
+  DIAG_POP_NEEDS_COMMENT;
147e83
+
147e83
+  if (path == NULL)
147e83
+    {
147e83
+      printf ("malloc (%zu): %m\n", path_len);
147e83
+      return EXIT_UNSUPPORTED;
147e83
+    }
147e83
+
147e83
+  /* Construct very long path = "bz22786/symlink/aaaa....."  */
147e83
+  char *p = mempcpy (path, lnk, sizeof (lnk) - 1);
147e83
+  *(p++) = '/';
147e83
+  memset (p, 'a', path_len - (path - p) - 2);
147e83
+  p[path_len - (path - p) - 1] = '\0';
147e83
+
147e83
+  /* This call crashes before the fix for bz22786 on 32-bit platforms.  */
147e83
+  p = realpath (path, NULL);
147e83
+
147e83
+  if (p != NULL || errno != ENAMETOOLONG)
147e83
+    {
147e83
+      printf ("realpath: %s (%m)", p);
147e83
+      return EXIT_FAILURE;
147e83
+    }
147e83
+
147e83
+  /* Cleanup.  */
147e83
+  unlink (lnk);
147e83
+  rmdir (dir);
147e83
+
147e83
+  return 0;
147e83
+}
147e83
+
147e83
+#define TEST_FUNCTION do_test
147e83
+#include <support/test-driver.c>