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

Add fuzzer for SIP parser #41

Merged
merged 9 commits into from
Sep 14, 2023
Merged

Add fuzzer for SIP parser #41

merged 9 commits into from
Sep 14, 2023

Conversation

oystedal
Copy link
Contributor

@oystedal oystedal commented Sep 9, 2023

Fuzzing input received from the network is a best practice for security and correctness. Go 1.18 added native support for fuzzing, see https://go.dev/doc/tutorial/fuzz and https://go.dev/security/fuzz.

The first commit introduces the fuzzer, which can be executed with go test -fuzz=Fuzz. For now, this starts off with a trivial SIP message. This can be expanded in the future by adding additional input strings as needed.

The subsequent commits are fixes to various issues found during execution of the fuzzer. Note that each of the files under parser/testdata/fuzz automatically become tests that are executed when running go test. This ensures that previously discovered crashes are not reintroduced with new changes.

I have executed the fuzzer for a little over two hours after fixing these issues without finding any additional issues.

@@ -27,11 +38,11 @@ func ParseUri(uriStr string, uri *sip.Uri) (err error) {
func uriStateSIP(uri *sip.Uri, s string) (uriFSM, string, error) {
switch s[0] {
case 'S', 's':
if s[3] == 'S' || s[3] == 's' {
Copy link
Owner

Choose a reason for hiding this comment

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

I think that maybe we should require sip/sips to be present, per comment above. This maybe would then simplify check

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated the code to check for sip: and sips:.

Copy link
Owner

Choose a reason for hiding this comment

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

ok this maybe now decreases performance.
We need also to check upper Case. SIP, SIPS, that is why old implementation was looking both.
I mostly wanted to remove default case, but keep this small check.
This is how is done in some other implements, compromise performance and full check.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We need also to check upper Case. SIP, SIPS, that is why old implementation was looking both.

strings.EqualFold does case-insensitive matching. I added test cases to verify that we match both sip: and SIP:.

I don't think a huge amount of performance is lost here.

@@ -0,0 +1,2 @@
go test fuzz v1
Copy link
Owner

Choose a reason for hiding this comment

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

I guess this was failing examples, but now they are fixed.
Do we really need to hold on this?
Files are just very autogenerated and I guess they can be created with fuzz test every time (if not fixed already)

Copy link
Contributor Author

@oystedal oystedal Sep 12, 2023

Choose a reason for hiding this comment

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

These files become test cases that help verify that the same kind of crash is not reintroduced into the parser later, so it's a free test case as such. If you undo one of my changes and run go test you'll see that the test run fails.

These files are based on crashing cases that the fuzzer has found, they will not be generated just from new runs of the parser after the crash has been fixed.

I agree that the fuzzer ends up generating very quirky examples. Part of the reason is that it works hard to make the test cases as minimal as possible.

@emiago
Copy link
Owner

emiago commented Sep 12, 2023

@oystedal forgot to mention. Very cool, thnx for contributing.
I will also run some benchs.

@emiago emiago merged commit 1acac37 into emiago:main Sep 14, 2023
@emiago
Copy link
Owner

emiago commented Sep 14, 2023

merged. @oystedal

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