@@ -1,146 +0,0 @@
1−From 5460617d1567657621107d895ee2dd83bc1f88f2 Mon Sep 17 00:00:00 2001
2−From: Paul Pluzhnikov <ppluzhnikov@google.com>
3−Date: Tue, 8 May 2018 18:12:41 -0700
4−Subject: [PATCH] Fix BZ 22786: integer addition overflow may cause stack
5− buffer overflow when realpath() input length is close to SSIZE_MAX.
6−
7−2018-05-09 Paul Pluzhnikov <ppluzhnikov@google.com>
8−
9− [BZ #22786]
10− * stdlib/canonicalize.c (__realpath): Fix overflow in path length
11− computation.
12− * stdlib/Makefile (test-bz22786): New test.
13− * stdlib/test-bz22786.c: New test.
14−---
15− ChangeLog | 8 +++++
16− stdlib/Makefile | 2 +-
17− stdlib/canonicalize.c | 2 +-
18− stdlib/test-bz22786.c | 90 +++++++++++++++++++++++++++++++++++++++++++++++++++
19− 4 files changed, 100 insertions(+), 2 deletions(-)
20− create mode 100644 stdlib/test-bz22786.c
21−
22−diff --git a/stdlib/Makefile b/stdlib/Makefile
23−index af1643c..1ddb1f9 100644
24−--- a/stdlib/Makefile
25−+++ b/stdlib/Makefile
26−@@ -84,7 +84,7 @@ tests := tst-strtol tst-strtod testmb testrand testsort testdiv \
27− tst-cxa_atexit tst-on_exit test-atexit-race \
28− test-at_quick_exit-race test-cxa_atexit-race \
29− test-on_exit-race test-dlclose-exit-race \
30−- tst-makecontext-align
31−+ tst-makecontext-align test-bz22786
32−
33− tests-internal := tst-strtod1i tst-strtod3 tst-strtod4 tst-strtod5i \
34− tst-tls-atexit tst-tls-atexit-nodelete
35−diff --git a/stdlib/canonicalize.c b/stdlib/canonicalize.c
36−index 4135f3f..390fb43 100644
37−--- a/stdlib/canonicalize.c
38−+++ b/stdlib/canonicalize.c
39−@@ -181,7 +181,7 @@ __realpath (const char *name, char *resolved)
40− extra_buf = __alloca (path_max);
41−
42− len = strlen (end);
43−- if ((long int) (n + len) >= path_max)
44−+ if (path_max - n <= len)
45− {
46− __set_errno (ENAMETOOLONG);
47− goto error;
48−diff --git a/stdlib/test-bz22786.c b/stdlib/test-bz22786.c
49−new file mode 100644
50−index 0000000..e7837f9
51−--- /dev/null
52−+++ b/stdlib/test-bz22786.c
53−@@ -0,0 +1,90 @@
54−+/* Bug 22786: test for buffer overflow in realpath.
55−+ Copyright (C) 2018 Free Software Foundation, Inc.
56−+ This file is part of the GNU C Library.
57−+
58−+ The GNU C Library is free software; you can redistribute it and/or
59−+ modify it under the terms of the GNU Lesser General Public
60−+ License as published by the Free Software Foundation; either
61−+ version 2.1 of the License, or (at your option) any later version.
62−+
63−+ The GNU C Library is distributed in the hope that it will be useful,
64−+ but WITHOUT ANY WARRANTY; without even the implied warranty of
65−+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
66−+ Lesser General Public License for more details.
67−+
68−+ You should have received a copy of the GNU Lesser General Public
69−+ License along with the GNU C Library; if not, see
70−+ <http://www.gnu.org/licenses/>. */
71−+
72−+/* This file must be run from within a directory called "stdlib". */
73−+
74−+#include <errno.h>
75−+#include <limits.h>
76−+#include <stdio.h>
77−+#include <stdlib.h>
78−+#include <string.h>
79−+#include <unistd.h>
80−+#include <sys/stat.h>
81−+#include <sys/types.h>
82−+#include <support/test-driver.h>
83−+#include <libc-diag.h>
84−+
85−+static int
86−+do_test (void)
87−+{
88−+ const char dir[] = "bz22786";
89−+ const char lnk[] = "bz22786/symlink";
90−+
91−+ rmdir (dir);
92−+ if (mkdir (dir, 0755) != 0 && errno != EEXIST)
93−+ {
94−+ printf ("mkdir %s: %m\n", dir);
95−+ return EXIT_FAILURE;
96−+ }
97−+ if (symlink (".", lnk) != 0 && errno != EEXIST)
98−+ {
99−+ printf ("symlink (%s, %s): %m\n", dir, lnk);
100−+ return EXIT_FAILURE;
101−+ }
102−+
103−+ const size_t path_len = (size_t) INT_MAX + 1;
104−+
105−+ DIAG_PUSH_NEEDS_COMMENT;
106−+#if __GNUC_PREREQ (7, 0)
107−+ /* GCC 7 warns about too-large allocations; here we need such
108−+ allocation to succeed for the test to work. */
109−+ DIAG_IGNORE_NEEDS_COMMENT (7, "-Walloc-size-larger-than=");
110−+#endif
111−+ char *path = malloc (path_len);
112−+ DIAG_POP_NEEDS_COMMENT;
113−+
114−+ if (path == NULL)
115−+ {
116−+ printf ("malloc (%zu): %m\n", path_len);
117−+ return EXIT_UNSUPPORTED;
118−+ }
119−+
120−+ /* Construct very long path = "bz22786/symlink/aaaa....." */
121−+ char *p = mempcpy (path, lnk, sizeof (lnk) - 1);
122−+ *(p++) = '/';
123−+ memset (p, 'a', path_len - (path - p) - 2);
124−+ p[path_len - (path - p) - 1] = '\0';
125−+
126−+ /* This call crashes before the fix for bz22786 on 32-bit platforms. */
127−+ p = realpath (path, NULL);
128−+
129−+ if (p != NULL || errno != ENAMETOOLONG)
130−+ {
131−+ printf ("realpath: %s (%m)", p);
132−+ return EXIT_FAILURE;
133−+ }
134−+
135−+ /* Cleanup. */
136−+ unlink (lnk);
137−+ rmdir (dir);
138−+
139−+ return 0;
140−+}
141−+
142−+#define TEST_FUNCTION do_test
143−+#include <support/test-driver.c>
144−--
145−2.9.3
146−