Skip to content

fuzzers: add a new fuzzer for patch parsing#5269

Merged
pks-t merged 1 commit into
libgit2:masterfrom
durin42:fuzzpatch
Oct 18, 2019
Merged

fuzzers: add a new fuzzer for patch parsing#5269
pks-t merged 1 commit into
libgit2:masterfrom
durin42:fuzzpatch

Conversation

@durin42

@durin42 durin42 commented Oct 15, 2019

Copy link
Copy Markdown

I was looking at this code anyway because the sr.ht people nerdsniped
me, and it gave me that "I should fuzz this" feeling. So have a fuzzer!

@pks-t pks-t left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Totally agreed, parsing arbitrary input is always likely to lead to errors. So this is a very welcome addition to libgit2, thanks a lot! Some minor comments which should be rather easy to fix, but other than that it looks good to me! :)

Comment thread fuzzers/corpora/patch_parse/patch_fuzzer-patch.diff
Comment thread fuzzers/patch_parse_fuzzer.c Outdated
Comment thread fuzzers/patch_parse_fuzzer.c
Comment thread fuzzers/patch_parse_fuzzer.c Outdated
I was looking at this code anyway because the sr.ht people nerdsniped
me, and it gave me that "I should fuzz this" feeling. So have a fuzzer!
@durin42

durin42 commented Oct 17, 2019

Copy link
Copy Markdown
Author

Also, in case you find it useful, I formatted the new c code with this clang-format config:

BasedOnStyle: LLVM
IndentWidth: 8
UseTab: ForIndentation
BreakBeforeBraces: Linux
AllowShortIfStatementsOnASingleLine: false
IndentCaseLabels: false
AllowShortBlocksOnASingleLine: false
AllowShortFunctionsOnASingleLine: false
IncludeCategories:
  - Regex:           '^<'
    Priority:        1
  - Regex:           '^"'
    Priority:        2

It seems to roughly match what you're already doing (it's cribbed from Mercurial's config, which appears to be similar, I suspect they both originated with the Linux kernel.)

@durin42 durin42 requested a review from pks-t October 17, 2019 19:05
if (size) {
git_patch *patch = NULL;
git_patch_options opts = GIT_PATCH_OPTIONS_INIT;
opts.prefix_len = (uint32_t)data[0];

@pks-t pks-t Oct 18, 2019

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I somehow didn't see this in my first round, but I'm wondering about the first byte of data here. So you're skipping the first byte and use it as prefix_len, right? But judging from the fuzzing corpora, the first byte is simply part of the diff, and thus it is very likely that the initial line is a non-valid diff header already when skipping the first byte. So shouldn't we just leave the prefix_len alone and just use data as input for git_patch_from_buffer directly?

E.g. just:

git_patch *patch = NULL;
git_patch_from_buffer(&patch, data, size, NULL);
git_patch_free(patch);

@durin42

durin42 commented Oct 18, 2019 via email

Copy link
Copy Markdown
Author

@pks-t

pks-t commented Oct 18, 2019

Copy link
Copy Markdown
Member

The included corpora files should both have a leading 0x01. GitHub doesn't
seem to show that...

Ah, fair. Not too sure about that though, to be honest. I somehow guess that the fuzzers will generate a lot less valid patches and thus impact speed, especially so because it's unlikely that the fuzzer will generate correct prefixes depending on this prefix byte. For simplicity's sake I'd say to not have that and just keep the default options. I assume there will be enough things to be fixed already.

@durin42

durin42 commented Oct 18, 2019 via email

Copy link
Copy Markdown
Author

@pks-t

pks-t commented Oct 18, 2019

Copy link
Copy Markdown
Member

From experience fuzzing Mercurial's parsers, the fuzzing system is absurdly
good at figuring out prefix bytes like this. But if you feel strongly I can
rip it out. What's your preference?

Okay, that sounds promising, I didn't expect them to be that "intelligent". Let's go for it then, no need to muse about this more than necessary. We can still change it at a later point if required. Thanks a lot!

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