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

Go: Decompression Bombs #13553

Merged
merged 67 commits into from
Mar 10, 2024
Merged

Go: Decompression Bombs #13553

merged 67 commits into from
Mar 10, 2024

Conversation

am0o0
Copy link
Contributor

@am0o0 am0o0 commented Jun 24, 2023

This is part of All for one, one for all query submission, I'm going to submit an issue in github/securitylab for this pull request too.
I tried my best to cover Command line Flow sources in Golang too. these can be used in other queries as Flow sources too.
An unsolved problem still exists about the isControlledRead predicate which is act as a sanitizer. I wrote the sanitizer with the same method in C++ usage and sanitizer predicate but in Java and Go I couldn't do it and it is really weird because I test it with another global taint config and I didn't get the proper flow to my desired sink. I put comments I hope it is enough I'M ready to explain more.

@am0o0 am0o0 requested a review from a team as a code owner June 24, 2023 15:22
@smowton
Copy link
Contributor

smowton commented Jun 24, 2023

Anyone not watching the repo in general, note this is part of a family of submissions:

#13553
#13554
#13555
#13556
#13557
#13558

@am0o0
Copy link
Contributor Author

am0o0 commented Jun 24, 2023

Hi @smowton sorry about delay of writing proper comments, I thought it is weekend and I had enough time for writing details until Monday.

@smowton
Copy link
Contributor

smowton commented Jun 24, 2023

No problem, just getting out in front of anyone happens to be reviewing it right away -- you're probably right that they'll all wait til Monday

@am0o0 am0o0 mentioned this pull request Jun 25, 2023
@am0o0
Copy link
Contributor Author

am0o0 commented Jun 25, 2023

Anyone not watching the repo in general, note this is part of a family of submissions:

#13553 #13554 #13555 #13556 #13557 #13558

#13560 is added too

@am0o0
Copy link
Contributor Author

am0o0 commented Jun 26, 2023

Hi, I've completed the work on this query and I don't have any further updates/commits here.

@ghsecuritylab ghsecuritylab marked this pull request as draft July 31, 2023 00:08
@Kwstubbs
Copy link
Contributor

Kwstubbs commented Sep 7, 2023

@amammad please remove Parameter as a source for the dataflow. Even after removing the Parameter source, I am still getting 10 alerts that are all the same path. Putting this for CodeQL go team cause I'm not quite sure why that is happening. Ex: https://gist.github.com/Kwstubbs/382d245e8e3ca9f424223aed75e67126

@am0o0
Copy link
Contributor Author

am0o0 commented Jan 14, 2024

@owen-mc I'm sorry for the late response. I caught a cold this whole week.
I tried my best to have a good rebase. It was a good experience :)

Comment on lines 43 to 50
class TheSink extends Sink {
TheSink() {
exists(Method f | f.hasQualifiedName("github.com/DataDog/zstd", "reader", "Read") |
this = f.getACall().getReceiver()
)
}
}

Copy link
Contributor

Choose a reason for hiding this comment

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

Why is this being removed? And a few methods called "WriteTo"?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

zstdDataDog.NewReader(file) returns io.ReadCloser so the Read call wasn't from the reader type of github.com/DataDog/zstd. This sink is included in another place now.

go/ql/src/experimental/frameworks/DecompressionBombs.qll Outdated Show resolved Hide resolved
go/ql/test/experimental/CWE-522-DecompressionBombs/test.go Outdated Show resolved Hide resolved
Copy link
Contributor

@owen-mc owen-mc left a comment

Choose a reason for hiding this comment

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

It would easier to read if the list of function calls in DecompressHandler in test.go were in the same order as the function declarations.

Comment on lines 578 to 586
TheSink() {
exists(Method m, DataFlow::CallNode cn |
m.hasQualifiedName("github.com/golang/snappy", "Reader", ["Read", "ReadByte"]) and
cn = m.getACall()
|
this = cn.getReceiver() and
not hasFlowToComparison(cn.getResult(0))
)
}
Copy link
Contributor

Choose a reason for hiding this comment

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

The docs for snappy say that these two methods just implement interface methods from the standard library, one of which you model. I think it would be better to just model those interface methods. Please check if the same is true for the other libraries. (Annoyingly it isn't very clear in Go if a method is implementing an interface method, but there are limited interface methods that make sense here so you can just compare with those function names and signatures.)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hi, I'm not sure if I understand this this correctly.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

do you mean something like this?

exists(Method m, DataFlow::CallNode cn |
        m.implements("io", "Reader", "Read") and
        cn = m.getACall()
      |
        result = cn.getReceiver() and
        not hasFlowToComparison(cn.getResult(0))
      )

Copy link
Contributor Author

Choose a reason for hiding this comment

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

please review the last commit to check if it is your suggestion.

@am0o0
Copy link
Contributor Author

am0o0 commented Feb 21, 2024

hmm, the tests are done locally without error.

Copy link
Contributor

@owen-mc owen-mc left a comment

Choose a reason for hiding this comment

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

The commit "use implements predicate for io interfaces" is what I meant. Could you perhaps put in comments to make it easier to see at a glance which methods are modeled for each package? Something like "// Decoder.WriteTo and Decoder.Read already modeled" for the first deletion in that commit.

am0o0 added 2 commits March 8, 2024 19:30
remove some sinks about `"decompressor"` which was added wrongly.
change `GeneralReadIoSink` type from module to class.
separate `KlauspostGzipAndPgzip` `KlauspostPgzip` and `KlauspostGzip`.
Comment on lines +520 to +522
/**
* Provides decompression bomb sinks for packages that use some standard IO interfaces/methods for reading decompressed data
*/

Check warning

Code scanning / CodeQL

Class QLDoc style. Warning

The QLDoc for a class should start with 'A', 'An', or 'The'.
Copy link
Contributor

@owen-mc owen-mc left a comment

Choose a reason for hiding this comment

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

Thanks for all your work on this.

@owen-mc
Copy link
Contributor

owen-mc commented Mar 10, 2024

The integration test failure is unrelated.

@owen-mc owen-mc merged commit 820c145 into github:main Mar 10, 2024
13 of 14 checks passed
@am0o0
Copy link
Contributor Author

am0o0 commented Mar 10, 2024

@owen-mc Thanks again for helping me with this PR, do you have any suggestions to improve future PRs for me? I tried my best to consider your previous suggestions in this PR as much as possible.

@owen-mc
Copy link
Contributor

owen-mc commented Mar 11, 2024

Hi @am0o0 . I'm sorry this PR took so many rounds of review. I'm sure you've learned a lot from it. CodeQL has a very steep learning curve. It also suffers from the fact that things are done a bit differently in the libraries for the different languages that we support. (We are trying to improve that, slowly.) The only way to get better is by practice, and to some extent by reading the existing CodeQL code.

There is also, of course, general software development advice:

  • try to make commits easy to review;
  • check that tests pass before you push (though I am very guilty of failing to do that);
  • write tests early and use them to guide your CodeQL writing (e.g. by quick-evaluating predicates to make sure they match the code you want.

owen-mc added a commit to owen-mc/codeql that referenced this pull request Apr 11, 2024
owen-mc added a commit to owen-mc/codeql that referenced this pull request Apr 19, 2024
owen-mc added a commit to owen-mc/codeql that referenced this pull request Jun 11, 2024
@am0o0 am0o0 deleted the amammad-go-bombs branch September 14, 2024 11:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants