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