Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent out-of-bound memory access. #124

Merged
merged 3 commits into from
Oct 17, 2018
Merged

Prevent out-of-bound memory access. #124

merged 3 commits into from
Oct 17, 2018

Conversation

Xadeck
Copy link

@Xadeck Xadeck commented Oct 17, 2018

Before the change, the scan_delimiters function could return max_delims+1.
The scan_delimiters function is called by extensions/strikethrough with:

18:    inline_parser, sizeof(buffer)-1, '~'

where buffer is:

12:    char buffer[101];

So scan_delimiters can return delims = 101. A few lines later, it is
used to 0-terminate the buffer with:

23:    buffer[delims] = 0;

so it effectively does buffer[101] = 0, which is an out-of-bound access.

A test was added. That's the test that I used inside Google to reproduce
the bug, thanks to internal infrastructure (sanitizers) to detect bad
memory access. Unfortunately, I couldnt' reproduce the bug with
cmark-gfm build suite. I tried make asan test but it gave nothing. I'm
still including the test for reference.

Before the change, the scan_delimiters function could return max_delims+1.
The scan_delimiters function is called by extensions/strikethrough with:

18:    inline_parser, sizeof(buffer)-1, '~'

where buffer is:

12:    char buffer[101];

So scan_delimiters can return delims = 101. A few lines later, it is
used to 0-terminate the buffer with:

23:    buffer[delims] = 0;

so it effectively does buffer[101] = 0, which is an out-of-bound access.

A test was added. That's the test that I used inside Google to reproduce
the bug, thanks to internal infrastructure (sanitizers) to detect bad
memory access. Unfortunately, I couldnt' reproduce the bug with
cmark-gfm build suite. I tried `make asan test` but it gave nothing. I'm
still including the test for reference.
@Xadeck
Copy link
Author

Xadeck commented Oct 17, 2018

The bug in this PR was found through fuzzing through the Google Autofuzz project. Autofuzz is an internal project, but a similar service is offered for free to open-source software. This is called OSS-Fuzz and described on the Google Opensource blog.

This is not really a bug, but it's possible to send an input markdown
consisting of lots of @ signs, and the recursion will cause memory
explosion. The limit depends on the running environment, but there is no
reason to accept arbitrarily long sequence of @, so let's just cut off
at 1000.
@Xadeck
Copy link
Author

Xadeck commented Oct 17, 2018

The PR is really one commit. It shows as 3 because I got confused when preparing another PR in the same branch, when I now know it should be in a separate branch.

@kivikakk
Copy link

Thank you! I'll squash the commits into one.

@kivikakk kivikakk merged commit 198669d into github:master Oct 17, 2018
talum pushed a commit that referenced this pull request Sep 14, 2021
* Prevent out-of-bound memory access.

Before the change, the scan_delimiters function could return max_delims+1.
The scan_delimiters function is called by extensions/strikethrough with:

18:    inline_parser, sizeof(buffer)-1, '~'

where buffer is:

12:    char buffer[101];

So scan_delimiters can return delims = 101. A few lines later, it is
used to 0-terminate the buffer with:

23:    buffer[delims] = 0;

so it effectively does buffer[101] = 0, which is an out-of-bound access.

A test was added. That's the test that I used inside Google to reproduce
the bug, thanks to internal infrastructure (sanitizers) to detect bad
memory access. Unfortunately, I couldnt' reproduce the bug with
cmark-gfm build suite. I tried `make asan test` but it gave nothing. I'm
still including the test for reference.

* Limit the recursion in autolink extension.

This is not really a bug, but it's possible to send an input markdown
consisting of lots of @ signs, and the recursion will cause memory
explosion. The limit depends on the running environment, but there is no
reason to accept arbitrarily long sequence of @, so let's just cut off
at 1000.

* Reverted change
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants