Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions unified/ql/lib/codeql/unified/security/SensitiveData.qll
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@ private class SensitiveDataIdentifier extends SensitiveData {
override SensitiveDataClassification getClassification() { result = classification }
}

private Expr memberAccessParent(MemberAccessExpr e) { result = e.getParent() }

/**
* A member access that might contain sensitive data. This includes a member itself and
* components of a receiver chain, for example:
Expand All @@ -51,7 +49,7 @@ private class SensitiveDataMemberAccess extends SensitiveData {
SensitiveDataClassification classification;

SensitiveDataMemberAccess() {
exists(MemberAccessExpr e | memberAccessParent*(e) = this.asExpr() |
exists(MemberAccessExpr e | e = this.asExpr() |
HeuristicNames::nameIndicatesSensitiveData(e.getMemberName(), classification)
)
}
Expand Down
47 changes: 44 additions & 3 deletions unified/ql/src/queries/security/CWE-312/CleartextLogging.ql
Original file line number Diff line number Diff line change
Expand Up @@ -13,32 +13,73 @@
* external/cwe/cwe-532
*/

//
// FIXME: This is a deliberately dumb and noisy version of the query used to exercise data flow early on.
//
import unified
import codeql.unified.security.SensitiveData
import codeql.unified.internal.typeinference.TypeInference as TypeInference

private string getNameFromExpr(Expr e) {
result = e.(IdentifierExpr).getValue()
or
result = e.(MemberAccessExpr).getMemberName()
}

/**
* Holds if `label` looks like the name of a logging function.
*/
bindingset[label]
private predicate logLikeHeuristic(string label) {
label.regexpMatch("(l|.*L)og([A-Z0-9].*)?") // e.g. "logMessage", "debugLog"
}

predicate heuristicSink(DataFlow::Node node) {
exists(CallExpr call, FunctionDeclaration f, Expr e |
f = TypeInference::resolveCallTarget(call) and
(
logLikeHeuristic(f.getName())
or
exists(ClassLikeDeclaration type |
f = type.getAMemberIncludingExtensions() and
logLikeHeuristic(type.getName())
)
) and
e = call.getAnArgument().getValue() and
TypeInference::inferType(e).asClass().getName() = ["String", "NSString"] and
node.asExpr() = e
)
}

/**
* A taint-tracking configuration for cleartext logging vulnerabilities.
*/
module CleartextLoggingConfig implements DataFlow::ConfigSig {
predicate isSource(DataFlow::Node node) { node instanceof SensitiveData }

predicate isSink(DataFlow::Node node) {
Models::isSink(node, "log-injection")
Comment thread
geoffw0 marked this conversation as resolved.
Resolved
Comment thread
geoffw0 marked this conversation as resolved.
Resolved
or
heuristicSink(node)
or
// TODO: port StringFormat.qll and use it, as in `PrintfCleartextLoggingSink` in the original Swift query
// TODO: then we shouldn't need this:
exists(CallExpr call |
getNameFromExpr(call.getCallee()).regexpMatch("(?i)(ns)?(log|warn(ing)?|error|print).*") and
node.asExpr() = call.getAnArgument().getValue()
)
}

predicate isBarrier(DataFlow::Node node) {
none() // TODO: barriers for booleans, encrypted data, and MaD defined barriers as in original Swift
}

predicate isBarrierIn(DataFlow::Node node) { isSource(node) }

predicate isAdditionalFlowStep(DataFlow::Node nodeFrom, DataFlow::Node nodeTo) {
// if `a` is sensitive, then `a.b` is also sensitive.
nodeTo.asExpr().(MemberAccessExpr).getBase() = nodeFrom.asExpr()
or
// if `a` is sensitive, then `a()` is also sensitive.
nodeTo.asExpr().(CallExpr).getCallee() = nodeFrom.asExpr()

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Added the test and restored the old SensitiveDataCall class. Unless there's something cleverer we can do, it looks like we need both.

}
}

module CleartextLoggingFlow = TaintTracking::Global<CleartextLoggingConfig>;
Expand Down
Loading
Loading