olga / rpms / glibc

Forked from rpms/glibc 5 years ago
Clone

Blame SOURCES/glibc-rh1579742.patch

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