Bug 24607 - Segmentation Fault in Glob with invalid path
Summary: Segmentation Fault in Glob with invalid path
Status: RESOLVED INVALID
Alias: None
Product: glibc
Classification: Unclassified
Component: glob (show other bugs)
Version: 2.29
: P2 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2019-05-23 04:31 UTC by Nicholas Starke
Modified: 2019-08-01 05:10 UTC (History)
3 users (show)

See Also:
Host:
Target:
Build:
Last reconfirmed: 2019-05-23 00:00:00
fweimer: security-


Attachments
Screenshot of Proof of Concept (289.25 KB, image/png)
2019-05-23 04:31 UTC, Nicholas Starke
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Nicholas Starke 2019-05-23 04:31:09 UTC
Created attachment 11798 [details]
Screenshot of Proof of Concept

I noticed a segmentation fault as a result of fuzzing the PHP glob functions.  I have provided the string below.  No flags to glob were specified. This was tested against the PHP glob function as well as the glob.h glob function in c.  This was tested against 2.27 and 2.29. Please let me know if you have any additional questions.  This may be a security issue.

String input:

//*/.*//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////./.**
Comment 1 Florian Weimer 2019-05-23 07:09:21 UTC
We need more details.  The input string alone, with the GLOB_ONLYDIR | GLOB_MARK | GLOB_NOSORT flags argument (as shown in the screenshot—the 8198 argument) does not reproduce this.  Thanks.
Comment 2 Jonathan Cottrill 2019-05-23 11:59:06 UTC
I'm able to reproduce this in Ubuntu 19.04, glibc 2.29, inside a Docker container, using these steps:

docker run --rm -it --privileged ubuntu:19.04

Inside the container:

apt update && apt install -y gcc gdb
cat >glob.c <<<'
#include <glob.h>
#include <stddef.h>

int main() {
    glob_t g;
    glob("//*/.*//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////./.**",
      8198, NULL, &g);

    return 0;
}
'
gcc -o glob glob.c
gdb -ex run glob

Output is similar to screenshot Nicholas Starke posted:

Program received signal SIGSEGV, Segmentation fault.
0x00007ffff7ec0984 in __glob (pattern=pattern@entry=0x7fffff7ff300 "//*/.*", '/' <repeats 194 times>..., flags=flags@entry=8198, errfunc=errfunc@entry=0x0, pglob=pglob@entry=0x7fffffffcfd0) at ../posix/glob.c:289
Comment 3 Andreas Schwab 2019-05-23 12:23:43 UTC
That is most likely a stack overflow.
Comment 4 Adhemerval Zanella 2019-05-23 12:46:36 UTC
The tests passed with 'ulimit -s unlimited'.  The pattern leads to recursive calls to __glob at glob.c:584.
Comment 5 Florian Weimer 2019-05-23 12:58:10 UTC
Right.  I suspect it's also related to file system contents whether it's triggered or not.
Comment 6 Nicholas Starke 2019-05-23 14:49:37 UTC
Thank you to Jonathan Cottrill for the in-depth reproduction steps. This is nearly exactly how I triaged the initial crash.

I apologize for not attaching the full proof of concept test harness for reproducing this. That's my mistake.  

What sort of information do you need at this point?  How can I help in your assessment?
Comment 7 Adhemerval Zanella 2019-05-23 18:15:23 UTC
(In reply to Nicholas Starke from comment #6)
> Thank you to Jonathan Cottrill for the in-depth reproduction steps. This is
> nearly exactly how I triaged the initial crash.
> 
> I apologize for not attaching the full proof of concept test harness for
> reproducing this. That's my mistake.  
> 
> What sort of information do you need at this point?  How can I help in your
> assessment?

My understanding is it works as intended.  The pattern triggers a recursive call on glob implementation, where when glob sees 'pattern/' it expands 'pattern' by calling.

The valgring massif tool shows a peak usage of 9,606,960 bytes for this testcase and usually, distro sets the default limit of stack to around 8MB.

To lower the glob stack usage it would require a complete refactor to save/restore the state in a heap-allocated variable and remove the recursion.
Comment 8 Nicholas Starke 2019-05-23 18:21:31 UTC
I apologize if this was a total waste of time.
Comment 9 Adhemerval Zanella 2019-05-23 18:34:17 UTC
(In reply to Nicholas Starke from comment #8)
> I apologize if this was a total waste of time.

No worries, if it is ok to you I will close this bug report as not a bug.
Comment 10 Nicholas Starke 2019-05-23 18:36:29 UTC
Thank you, I am fine with closing this as not a bug.
Comment 11 Adhemerval Zanella 2019-05-23 18:38:09 UTC
As per previous comments.