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

Collin Funk collin.funk1@gmail.com
Tue Oct 7 21:21:59 GMT 2025


Adhemerval Zanella Netto <adhemerval.zanella@linaro.org> writes:

> 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.

I'm a committer there, so I was going to patch it there after glibc.

For Gnulib it probably needs ISSLASH (*p) to handle backslashes in file
names on Windows. But IIRC the files are a bit out of sync already, so I
didn't spend too much time messing with '#ifdef _LIBC'. Those cleanups
can be done later.

> 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);

Good idea. Let me send a v2 with those changes. For Gnulib the test
without setrlimit will probably work fine.

Collin


More information about the Libc-alpha mailing list