-
Notifications
You must be signed in to change notification settings - Fork 2k
Crypto: Adding bad decrypt then mac order query. #20696
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -7,6 +7,7 @@ | |||||||||||||||||||||||||
| * that flows to the input artifact of a mac operation. | ||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||
| predicate isDecryptToMacFlow(ArtifactFlow::PathNode src, ArtifactFlow::PathNode sink) { | ||||||||||||||||||||||||||
| // Simply flow from decrypt output to a mac input | ||||||||||||||||||||||||||
| ArtifactFlow::flowPath(src, sink) and | ||||||||||||||||||||||||||
| exists(Crypto::CipherOperationNode cipherOp | | ||||||||||||||||||||||||||
| cipherOp.getKeyOperationSubtype() = Crypto::TDecryptMode() and | ||||||||||||||||||||||||||
|
|
@@ -17,30 +18,163 @@ | |||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||
| * Experimental interface for graph generation, supply the | ||||||||||||||||||||||||||
| * node to determine if a issue exists, and if so | ||||||||||||||||||||||||||
| * the graph can add a property on the node. | ||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||
| predicate isDecryptToMacNode(Crypto::ArtifactNode node) { | ||||||||||||||||||||||||||
| exists(ArtifactFlow::PathNode src | | ||||||||||||||||||||||||||
| isDecryptToMacFlow(src, _) and | ||||||||||||||||||||||||||
| node.asElement() = src.getNode().asExpr() | ||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| predicate isDecryptThenMacFlow(DecryptThenMacFlow::PathNode src, DecryptThenMacFlow::PathNode sink) { | ||||||||||||||||||||||||||
| DecryptThenMacFlow::flowPath(src, sink) | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||
| * Holds when the src node is used as plaintext input to both | ||||||||||||||||||||||||||
| * an encryption operation and a mac operation, via the | ||||||||||||||||||||||||||
| * argument represented by InterimArg. | ||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||
| predicate isPlaintextInEncryptionAndMac( | ||||||||||||||||||||||||||
| PlaintextUseAsMacAndCipherInputFlow::PathNode src, | ||||||||||||||||||||||||||
| PlaintextUseAsMacAndCipherInputFlow::PathNode sink, InterimArg arg | ||||||||||||||||||||||||||
| PlaintextUseAsMacAndCipherInputFlow::PathNode sink, EncryptOrMacCallArg arg | ||||||||||||||||||||||||||
| ) { | ||||||||||||||||||||||||||
| PlaintextUseAsMacAndCipherInputFlow::flowPath(src, sink) and | ||||||||||||||||||||||||||
| arg = sink.getState().asSome() | ||||||||||||||||||||||||||
| arg = sink.getState().asSome() and | ||||||||||||||||||||||||||
| // the above pathing adds flow steps that may not have consideration for the calling context | ||||||||||||||||||||||||||
| // TODO: this is something we want to look into to improving, but for now | ||||||||||||||||||||||||||
| // we can filter bad flows with one additional flow check, that the source goes to both | ||||||||||||||||||||||||||
| // src and sink through a generic flow | ||||||||||||||||||||||||||
| // note that the flow path above ensures src gets to the interim arg, so we just need to | ||||||||||||||||||||||||||
| // verify the source to sink. | ||||||||||||||||||||||||||
| // TODO: having to copy the generic data flow config into a use-use variant | ||||||||||||||||||||||||||
| // should we fix this at the language level to allow use use more intuitively? | ||||||||||||||||||||||||||
| // Seems to be a common issue. | ||||||||||||||||||||||||||
| GenericDataSourceFlowUseUseFlow::flow(src.getNode(), sink.getNode()) | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| module ArgToSinkConfig implements DataFlow::ConfigSig { | ||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||
| * A copy of GenericDataSourceFlow but with use-use flows enabled by removing the barrier out | ||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||
| module GenericDataSourceFlowUseUseConfig 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 isBarrierIn(DataFlow::Node node) { | ||||||||||||||||||||||||||
| node = any(Crypto::FlowAwareElement element).getOutputNode() | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| predicate isAdditionalFlowStep(DataFlow::Node node1, DataFlow::Node node2) { | ||||||||||||||||||||||||||
| node1.(AdditionalFlowInputStep).getOutput() = node2 | ||||||||||||||||||||||||||
| or | ||||||||||||||||||||||||||
| exists(MethodCall m | | ||||||||||||||||||||||||||
| m.getMethod().hasQualifiedName("java.lang", "String", "getBytes") and | ||||||||||||||||||||||||||
| node1.asExpr() = m.getQualifier() and | ||||||||||||||||||||||||||
| node2.asExpr() = m | ||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| module GenericDataSourceFlowUseUseFlow = TaintTracking::Global<GenericDataSourceFlowUseUseConfig>; | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| module WrapperArgFlowConfig implements DataFlow::ConfigSig { | ||||||||||||||||||||||||||
| predicate isSource(DataFlow::Node source) { | ||||||||||||||||||||||||||
| // Start from a parameter and not a call to avoid flow going out of | ||||||||||||||||||||||||||
| // the call. We want to flow down a call, so start from a parameter | ||||||||||||||||||||||||||
| // and barrier flows through returns | ||||||||||||||||||||||||||
| exists(Method m | m.getParameter(_) = source.asParameter()) | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| predicate isSink(DataFlow::Node sink) { | ||||||||||||||||||||||||||
| exists(Crypto::CipherOperationNode cipherOp | | ||||||||||||||||||||||||||
| cipherOp.getAnInputArtifact().asElement() = sink.asExpr() | ||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||
| or | ||||||||||||||||||||||||||
| exists(Crypto::MacOperationNode macOp | macOp.getAnInputArtifact().asElement() = sink.asExpr()) | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| predicate isBarrierIn(DataFlow::Node node) { | ||||||||||||||||||||||||||
| node = any(Crypto::FlowAwareElement element).getOutputNode() | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| predicate isBarrierOut(DataFlow::Node node) { | ||||||||||||||||||||||||||
| // stop all flow out of a call return | ||||||||||||||||||||||||||
| // TODO: this might be too strict and remove taint flow, need to reassess | ||||||||||||||||||||||||||
| exists(Call c | c = node.asExpr()) or | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| node = any(Crypto::FlowAwareElement element).getInputNode() | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| predicate isAdditionalFlowStep(DataFlow::Node node1, DataFlow::Node node2) { | ||||||||||||||||||||||||||
| node1.(AdditionalFlowInputStep).getOutput() = node2 | ||||||||||||||||||||||||||
| or | ||||||||||||||||||||||||||
| exists(MethodCall m | | ||||||||||||||||||||||||||
| m.getMethod().hasQualifiedName("java.lang", "String", "getBytes") and | ||||||||||||||||||||||||||
| node1.asExpr() = m.getQualifier() and | ||||||||||||||||||||||||||
| node2.asExpr() = m | ||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| module WrapperArgFlow = TaintTracking::Global<WrapperArgFlowConfig>; | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| predicate encryptWrapperArg(DataFlow::Node n, DataFlow::Node sink) { | ||||||||||||||||||||||||||
| exists(Crypto::CipherOperationNode cipherOp | | ||||||||||||||||||||||||||
| cipherOp.getKeyOperationSubtype() = Crypto::TEncryptMode() and | ||||||||||||||||||||||||||
| cipherOp.getAnInputArtifact().asElement() = sink.asExpr() | ||||||||||||||||||||||||||
| ) and | ||||||||||||||||||||||||||
| ( | ||||||||||||||||||||||||||
| exists(Parameter p, DataFlow::Node src | | ||||||||||||||||||||||||||
| p = src.asParameter() and | ||||||||||||||||||||||||||
| WrapperArgFlow::flow(src, sink) and | ||||||||||||||||||||||||||
| n.asExpr() = p.getAnArgument() | ||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||
| or | ||||||||||||||||||||||||||
| n = sink // the call the target operation is considered a wrapper arg to itself | ||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| predicate decryptWrapperArg(DataFlow::Node n, DataFlow::Node sink) { | ||||||||||||||||||||||||||
| exists(Crypto::CipherOperationNode cipherOp | | ||||||||||||||||||||||||||
| cipherOp.getKeyOperationSubtype() = Crypto::TDecryptMode() and | ||||||||||||||||||||||||||
| cipherOp.getAnInputArtifact().asElement() = sink.asExpr() | ||||||||||||||||||||||||||
| ) and | ||||||||||||||||||||||||||
| ( | ||||||||||||||||||||||||||
| exists(Parameter p, DataFlow::Node src | | ||||||||||||||||||||||||||
| p = src.asParameter() and | ||||||||||||||||||||||||||
| WrapperArgFlow::flow(src, sink) and | ||||||||||||||||||||||||||
| n.asExpr() = p.getAnArgument() | ||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||
| or | ||||||||||||||||||||||||||
| n = sink // the call the target operation is considered a wrapper arg to itself | ||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| predicate macWrapperArg(DataFlow::Node n, DataFlow::Node sink) { | ||||||||||||||||||||||||||
| exists(Crypto::MacOperationNode macOp | macOp.getAnInputArtifact().asElement() = sink.asExpr()) and | ||||||||||||||||||||||||||
| ( | ||||||||||||||||||||||||||
| exists(Parameter p, DataFlow::Node src | | ||||||||||||||||||||||||||
| p = src.asParameter() and | ||||||||||||||||||||||||||
| WrapperArgFlow::flow(src, sink) and | ||||||||||||||||||||||||||
| n.asExpr() = p.getAnArgument() | ||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||
| or | ||||||||||||||||||||||||||
| n = sink // the call the target operation is considered a wrapper arg to itself | ||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| module ArgToEncryptOrMacConfig implements DataFlow::ConfigSig { | ||||||||||||||||||||||||||
| predicate isSource(DataFlow::Node source) { exists(Call c | c.getAnArgument() = source.asExpr()) } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| predicate isSink(DataFlow::Node sink) { targetSinks(sink) } | ||||||||||||||||||||||||||
| predicate isSink(DataFlow::Node sink) { encryptOrMacSink(sink) } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| // Don't go in to a known out node, this will prevent the plaintext | ||||||||||||||||||||||||||
| // from tracing out of cipher operations for example, we just want to trace | ||||||||||||||||||||||||||
|
|
@@ -62,12 +196,12 @@ | |||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| module ArgToSinkFlow = TaintTracking::Global<ArgToSinkConfig>; | ||||||||||||||||||||||||||
| module ArgToEncryptOrMacFlow = TaintTracking::Global<ArgToEncryptOrMacConfig>; | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||
| * Target sinks for this query are either encryption operations or mac operation message inputs | ||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||
| predicate targetSinks(DataFlow::Node n) { | ||||||||||||||||||||||||||
| predicate encryptOrMacSink(DataFlow::Node n) { | ||||||||||||||||||||||||||
| exists(Crypto::CipherOperationNode cipherOp | | ||||||||||||||||||||||||||
| cipherOp.getKeyOperationSubtype() = Crypto::TEncryptMode() and | ||||||||||||||||||||||||||
| cipherOp.getAnInputArtifact().asElement() = n.asExpr() | ||||||||||||||||||||||||||
|
|
@@ -77,44 +211,48 @@ | |||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||
| * An argument of a target sink or a parent call whose parameter flows to a target sink | ||||||||||||||||||||||||||
| * Target sinks for decryption operations | ||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||
| class InterimArg extends DataFlow::Node { | ||||||||||||||||||||||||||
| DataFlow::Node targetSink; | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| InterimArg() { | ||||||||||||||||||||||||||
| targetSinks(targetSink) and | ||||||||||||||||||||||||||
| ( | ||||||||||||||||||||||||||
| this = targetSink | ||||||||||||||||||||||||||
| or | ||||||||||||||||||||||||||
| ArgToSinkFlow::flow(this, targetSink) and | ||||||||||||||||||||||||||
| this.getEnclosingCallable().calls+(targetSink.getEnclosingCallable()) | ||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| DataFlow::Node getTargetSink() { result = targetSink } | ||||||||||||||||||||||||||
| predicate decryptSink(DataFlow::Node n) { | ||||||||||||||||||||||||||
| exists(Crypto::CipherOperationNode cipherOp | | ||||||||||||||||||||||||||
| cipherOp.getKeyOperationSubtype() = Crypto::TDecryptMode() and | ||||||||||||||||||||||||||
| cipherOp.getAnInputArtifact().asElement() = n.asExpr() | ||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||
| * A wrapper class to represent a target argument dataflow node. | ||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||
| class TargetArg extends DataFlow::Node { | ||||||||||||||||||||||||||
| TargetArg() { targetSinks(this) } | ||||||||||||||||||||||||||
| // /** | ||||||||||||||||||||||||||
| // * An argument of a target sink or a parent call whose parameter flows to a target sink | ||||||||||||||||||||||||||
| // */ | ||||||||||||||||||||||||||
| // class EncryptOrMacPartialFlowArg extends DataFlow::Node { | ||||||||||||||||||||||||||
| // DataFlow::Node targetSink; | ||||||||||||||||||||||||||
| // EncryptOrMacPartialFlowArg() { | ||||||||||||||||||||||||||
| // encryptWrapperArg(this, targetSink) | ||||||||||||||||||||||||||
| // or | ||||||||||||||||||||||||||
| // macWrapperArg(this, targetSink) | ||||||||||||||||||||||||||
| // } | ||||||||||||||||||||||||||
| // DataFlow::Node getTargetSink() { result = targetSink } | ||||||||||||||||||||||||||
| // } | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
| // /** | |
| // * An argument of a target sink or a parent call whose parameter flows to a target sink | |
| // */ | |
| // class EncryptOrMacPartialFlowArg extends DataFlow::Node { | |
| // DataFlow::Node targetSink; | |
| // EncryptOrMacPartialFlowArg() { | |
| // encryptWrapperArg(this, targetSink) | |
| // or | |
| // macWrapperArg(this, targetSink) | |
| // } | |
| // DataFlow::Node getTargetSink() { result = targetSink } | |
| // } |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,19 @@ | ||||||
| /** | ||||||
| * @name Bad MAC order: decrypt then mac | ||||||
| * @description Decryption on cipher text, then MAC on ciopher text, is incorrect order | ||||||
|
||||||
| * @description Decryption on cipher text, then MAC on ciopher text, is incorrect order | |
| * @description Decryption on cipher text, then MAC on cipher text, is incorrect order |
Uh oh!
There was an error while loading. Please reload this page.