-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Quantum: Initial support for C# #19905
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
Draft
fegge
wants to merge
36
commits into
github:main
Choose a base branch
from
trailofbits:tob/quantum-csharp
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
36 commits
Select commit
Hold shift + click to select a range
9decd51
Initial quantum support for C#
fegge 66eee73
Fixed issues in C# Language.qll
fegge 69e6308
Added C# query to generate CBOM graph
fegge 07a3032
quantum-c#: initial support for ECDSA and RSA signatures
fcasal 36213c5
Merge branch 'tob/quantum-csharp' of github.com:trailofbits/codeql in…
fcasal d690a34
quantum-c#: make type precise
fcasal 9970e58
quantum-c#: add HashAlgorithms
fcasal 1762959
quantum-c#: Add HashAlgorithmInstance
fcasal 01b98fc
quantum-c#: Add generic dataflow module
fcasal 3913f44
quanntum-c#: refactor class names
fcasal b1bbd97
Added initial support for C# block ciphers
fegge 791c260
quantum-c#: Add HashAlgorithmInstance
fcasal 2642e32
Merge branch 'tob/quantum-csharp' of github.com:trailofbits/codeql in…
fcasal 6d0024d
quantum-c#: refactoring
fcasal c7caf97
Extended CryptoStreamKeyOperationInstance
fegge a3e93ce
quantum-c#: add hash operation instance
fcasal e0430a7
Merge branch 'tob/quantum-csharp' of github.com:trailofbits/codeql in…
fcasal 9238b12
Added support for the system CSPRNG
fegge 46c037c
Added unit tests for C# block cipher modes
fegge a7c5f7a
refactoring
fcasal 50c98f6
Merge branch 'tob/quantum-csharp' of github.com:trailofbits/codeql in…
fcasal 3e70e8e
Updated Stream related data flow
fegge e488a74
Merge branch 'tob/quantum-csharp' of github.com:trailofbits/codeql in…
fcasal f3c436a
quantum-c#: Use stream flows for the HashOperationInstance
fcasal 45ae4d1
Added support for AesGcm and AesCcm
fegge e643cc4
Updated test suite with AesGcm and AesCcm tests
fegge 5fc267a
Added support for ChaCha20Poly1305
fegge e344505
quantum-c#: refactor AVCs for hashes and signatures.
fcasal 601d5d1
Merge branch 'tob/quantum-csharp' of github.com:trailofbits/codeql in…
fcasal 298d226
Added support for bare symmetric algorithms
fegge 2d4af33
quantum-c#: add tests for signatures
fcasal 7c72a65
Add RSAPKCS1Signature formatters
fcasal 7d7b8b2
Merge branch 'tob/quantum-csharp' of github.com:trailofbits/codeql in…
fcasal 39581e2
quantum-c#: add support for RSAPKC1Signature formatters
fcasal 6763f18
quantum-c#: Add hash tests
fcasal b417436
quantum-c#: Add support for HMACs
fcasal File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,183 @@ | ||
private import csharp as Language | ||
private import semmle.code.csharp.dataflow.DataFlow | ||
private import codeql.quantum.experimental.Model | ||
|
||
private class UnknownLocation extends Language::Location { | ||
UnknownLocation() { this.getFile().getAbsolutePath() = "" } | ||
} | ||
|
||
/** | ||
* A dummy location which is used when something doesn't have a location in | ||
* the source code but needs to have a `Location` associated with it. There | ||
* may be several distinct kinds of unknown locations. For example: one for | ||
* expressions, one for statements and one for other program elements. | ||
*/ | ||
private class UnknownDefaultLocation extends UnknownLocation { | ||
UnknownDefaultLocation() { locations_default(this, _, 0, 0, 0, 0) } | ||
} | ||
|
||
module CryptoInput implements InputSig<Language::Location> { | ||
class DataFlowNode = Language::DataFlow::Node; | ||
|
||
class LocatableElement = Language::Element; | ||
|
||
class UnknownLocation = UnknownDefaultLocation; | ||
|
||
string locationToFileBaseNameAndLineNumberString(Language::Location location) { | ||
result = location.getFile().getBaseName() + ":" + location.getStartLine() | ||
} | ||
|
||
LocatableElement dfn_to_element(Language::DataFlow::Node node) { | ||
result = node.asExpr() or | ||
result = node.asParameter() | ||
} | ||
|
||
predicate artifactOutputFlowsToGenericInput( | ||
Language::DataFlow::Node artifactOutput, Language::DataFlow::Node otherInput | ||
) { | ||
ArtifactFlow::flow(artifactOutput, otherInput) | ||
} | ||
} | ||
|
||
// Instantiate the `CryptographyBase` module | ||
module Crypto = CryptographyBase<Language::Location, CryptoInput>; | ||
|
||
/** | ||
* An additional flow step in generic data-flow configurations. | ||
* Where a step is an edge between nodes `n1` and `n2`, | ||
* `this` = `n1` and `getOutput()` = `n2`. | ||
* | ||
* FOR INTERNAL MODELING USE ONLY. | ||
*/ | ||
abstract class AdditionalFlowInputStep extends DataFlow::Node { | ||
abstract DataFlow::Node getOutput(); | ||
|
||
final DataFlow::Node getInput() { result = this } | ||
} | ||
|
||
/** | ||
* Generic data source to node input configuration | ||
*/ | ||
module GenericDataSourceFlowConfig implements DataFlow::ConfigSig { | ||
predicate isSource(DataFlow::Node source) { | ||
source = any(Crypto::GenericSourceInstance i).getOutputNode() | ||
} | ||
|
||
predicate isSink(DataFlow::Node sink) { | ||
sink = any(Crypto::FlowAwareElement other).getInputNode() | ||
} | ||
|
||
predicate isBarrierOut(DataFlow::Node node) { | ||
node = any(Crypto::FlowAwareElement element).getInputNode() | ||
} | ||
|
||
predicate isBarrierIn(DataFlow::Node node) { | ||
node = any(Crypto::FlowAwareElement element).getOutputNode() | ||
} | ||
|
||
predicate isAdditionalFlowStep(DataFlow::Node node1, DataFlow::Node node2) { | ||
node1.(AdditionalFlowInputStep).getOutput() = node2 | ||
} | ||
} | ||
|
||
module ArtifactFlowConfig implements DataFlow::ConfigSig { | ||
predicate isSource(DataFlow::Node source) { | ||
source = any(Crypto::ArtifactInstance artifact).getOutputNode() | ||
} | ||
|
||
predicate isSink(DataFlow::Node sink) { | ||
sink = any(Crypto::FlowAwareElement other).getInputNode() | ||
} | ||
|
||
predicate isBarrierOut(DataFlow::Node node) { | ||
node = any(Crypto::FlowAwareElement element).getInputNode() | ||
} | ||
|
||
predicate isBarrierIn(DataFlow::Node node) { | ||
node = any(Crypto::FlowAwareElement element).getOutputNode() | ||
} | ||
|
||
predicate isAdditionalFlowStep(DataFlow::Node node1, DataFlow::Node node2) { | ||
node1.(AdditionalFlowInputStep).getOutput() = node2 | ||
} | ||
} | ||
|
||
module GenericDataSourceFlow = TaintTracking::Global<GenericDataSourceFlowConfig>; | ||
|
||
module ArtifactFlow = DataFlow::Global<ArtifactFlowConfig>; | ||
|
||
/** | ||
* A method access that returns random data or writes random data to an argument. | ||
*/ | ||
abstract class RandomnessSource extends MethodCall { | ||
/** | ||
* Gets the expression representing the output of this source. | ||
*/ | ||
abstract Expr getOutput(); | ||
|
||
/** | ||
* Gets the type of the source of randomness used by this call. | ||
*/ | ||
Type getGenerator() { result = this.getQualifier().getType() } | ||
} | ||
|
||
/** | ||
* A call to `System.Security.Cryptography.RandomNumberGenerator.GetBytes`. | ||
*/ | ||
class SecureRandomnessSource extends RandomnessSource { | ||
SecureRandomnessSource() { | ||
this.getTarget() | ||
.hasFullyQualifiedName("System.Security.Cryptography", "RandomNumberGenerator", "GetBytes") | ||
} | ||
|
||
override Expr getOutput() { result = this.getArgument(0) } | ||
} | ||
|
||
/** | ||
* A call to `System.Random.NextBytes`. | ||
*/ | ||
class InsecureRandomnessSource extends RandomnessSource { | ||
InsecureRandomnessSource() { | ||
this.getTarget().hasFullyQualifiedName("System", "Random", "NextBytes") | ||
} | ||
|
||
override Expr getOutput() { result = this.getArgument(0) } | ||
} | ||
|
||
/** | ||
* An instance of random number generation, modelled as the expression | ||
* tied to an output node (i.e., the RNG output) | ||
*/ | ||
abstract class RandomnessInstance extends Crypto::RandomNumberGenerationInstance { | ||
override DataFlow::Node getOutputNode() { result.asExpr() = this } | ||
} | ||
|
||
/** | ||
* An output instance from the system cryptographically secure RNG. | ||
*/ | ||
class SecureRandomnessInstance extends RandomnessInstance { | ||
RandomnessSource source; | ||
|
||
SecureRandomnessInstance() { | ||
source instanceof SecureRandomnessSource and | ||
this = source.getOutput() | ||
} | ||
|
||
override string getGeneratorName() { result = source.getGenerator().getName() } | ||
} | ||
|
||
/** | ||
* An output instance from an insecure RNG. | ||
*/ | ||
class InsecureRandomnessInstance extends RandomnessInstance { | ||
RandomnessSource source; | ||
|
||
InsecureRandomnessInstance() { | ||
not source instanceof SecureRandomnessSource and | ||
this = source.getOutput() | ||
} | ||
|
||
override string getGeneratorName() { result = source.getGenerator().getName() } | ||
} | ||
|
||
import dotnet |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import dotnet.AlgorithmValueConsumers | ||
import dotnet.AlgorithmInstances | ||
import dotnet.OperationInstances |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Check warning
Code scanning / CodeQL
Misspelling Warning