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