[PATCH] posix: Avoid a stack overflow when glob is given many trailing slashes [BZ #30635]

Adhemerval Zanella Netto adhemerval.zanella@linaro.org
Tue Oct 7 18:14:50 GMT 2025



On 07/10/25 03:15, Collin Funk wrote:
> * posix/glob.c (__glob): Strip trailing slashes before the recursive
> call, so it is not called for every trailing slash in the pattern.
> * posix/tst-glob-bz30635.c: Add a test case that would previously
> segmentation fault.
> * posix/Makefile (tests): Add the new test.

The fix looks ok to me, just a test suggestion below.

I am also CC gnulib maintainers, they might also want to fix it on their
side as well.

> ---
>  posix/Makefile           |  1 +
>  posix/glob.c             |  4 ++++
>  posix/tst-glob-bz30635.c | 41 ++++++++++++++++++++++++++++++++++++++++
>  3 files changed, 46 insertions(+)
>  create mode 100644 posix/tst-glob-bz30635.c
> 
> diff --git a/posix/Makefile b/posix/Makefile
> index a36e5decd3..f6421e5379 100644
> --- a/posix/Makefile
> +++ b/posix/Makefile
> @@ -288,6 +288,7 @@ tests := \
>    tst-fork \
>    tst-gai_strerror \
>    tst-getopt_long1 \
> +  tst-glob-bz30635 \
>    tst-glob-tilde \
>    tst-glob_symlinks \
>    tst-gnuglob \
> diff --git a/posix/glob.c b/posix/glob.c
> index a7c7dd1ebe..82ee873d80 100644
> --- a/posix/glob.c
> +++ b/posix/glob.c
> @@ -596,6 +596,10 @@ __glob (const char *pattern, int flags, int (*errfunc) (const char *, int),
>                    flags &= ~(GLOB_NOCHECK | GLOB_NOMAGIC);
>                  }
>              }
> +          /* Prevent stack overflows with many trailing '/' characters.  */
> +          for (char *p = &dirname[dirlen - 1];
> +               dirname < p && p[0] == '/' && p[-1] == '/'; --p)
> +            p[0] = '\0';
>            int val = __glob (dirname, flags | GLOB_MARK, errfunc, pglob);
>            if (val == 0)
>              pglob->gl_flags = ((pglob->gl_flags & ~GLOB_MARK)

Ok.

> diff --git a/posix/tst-glob-bz30635.c b/posix/tst-glob-bz30635.c
> new file mode 100644
> index 0000000000..22d7054ef0
> --- /dev/null
> +++ b/posix/tst-glob-bz30635.c
> @@ -0,0 +1,41 @@
> +/* Test that glob does not overflow the stack with many trailing slashes.
> +   Copyright (C) 2025 Free Software Foundation, Inc.
> +   This file is part of the GNU C Library.
> +
> +   The GNU C Library is free software; you can redistribute it and/or
> +   modify it under the terms of the GNU Lesser General Public
> +   License as published by the Free Software Foundation; either
> +   version 2.1 of the License, or (at your option) any later version.
> +
> +   The GNU C Library is distributed in the hope that it will be useful,
> +   but WITHOUT ANY WARRANTY; without even the implied warranty of
> +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
> +   Lesser General Public License for more details.
> +
> +   You should have received a copy of the GNU Lesser General Public
> +   License along with the GNU C Library; if not, see
> +   <https://www.gnu.org/licenses/>.  */
> +
> +#include <glob.h>
> +#include <string.h>
> +#include <support/test-driver.h>
> +#include <support/support.h>
> +#include <support/check.h>
> +
> +/* Allocation size.  */
> +#define SIZE 4096
> +
> +/* This test would cause a stack overflow on glibc 2.42 and earlier.  */

This will depend of the configured RLIMIT_STACK, and valgrind/massif indicated
that it peaks at ~13.5MB (x86_64) / ~13 (i686) of stack usage for this testcase:

$ ulimit -s $((14*1024))
$ posix/tst-glob-bz30635 --direct

So I think it would be better to explicit limit the stack usage:

diff --git a/posix/tst-glob-bz30635.c b/posix/tst-glob-bz30635.c
index 22d7054ef0c..59f66ada34e 100644
--- a/posix/tst-glob-bz30635.c
+++ b/posix/tst-glob-bz30635.c
@@ -21,14 +21,21 @@
 #include <support/test-driver.h>
 #include <support/support.h>
 #include <support/check.h>
+#include <sys/resource.h>

 /* Allocation size.  */
 #define SIZE 4096

 /* This test would cause a stack overflow on glibc 2.42 and earlier.  */
+#define MAX_STACK_SIZE (8 * 1024 * 1024)
+
 static int
 do_test (void)
 {
+  TEST_VERIFY_EXIT (
+   setrlimit (RLIMIT_STACK,
+             &(struct rlimit){ MAX_STACK_SIZE, MAX_STACK_SIZE }) == 0);
+
   glob_t g;
   char *pattern = xmalloc (SIZE);
   memset (pattern, '/', SIZE - 1);

> +static int
> +do_test (void)
> +{
> +  glob_t g;
> +  char *pattern = xmalloc (SIZE);
> +  memset (pattern, '/', SIZE - 1);
> +  pattern[SIZE - 1] = '\0';
> +  TEST_VERIFY (glob (pattern, 0, NULL, &g) == 0);
> +  globfree (&g);
> +  return 0;
> +}
> +
> +#include <support/test-driver.c>



More information about the Libc-alpha mailing list