Find calls to ProcessBuilder and it's value equals to X #8759
Replies: 1 comment 1 reply
-
If you want to match the constructor of The class To match string literals in Java code, you can use If the arguments in Java code would be directly provided to the varargs The following CodeQL query would probably cover your use case, however it neither covers Java code directly providing the arguments to the varargs /**
* ...
*
* @kind path-problem
*/
import java
import semmle.code.java.dataflow.DataFlow
import DataFlow::PathGraph
// Define a custom data flow configuration
class BashCommandConfig extends DataFlow::Configuration {
BashCommandConfig() { this = "BashCommandConfig" }
override predicate isSource(DataFlow::Node source) {
// Source is the creation of an array
exists(ArrayCreationExpr commandArrayCreation, ArrayInit arrayInit |
commandArrayCreation = source.asExpr() and
arrayInit = commandArrayCreation.getInit() and
// Check the specified command arguments
arrayInit.getInit(0).(CompileTimeConstantExpr).getStringValue() = "bash" and
arrayInit.getInit(1).(CompileTimeConstantExpr).getStringValue() = "-c"
)
}
override predicate isSink(DataFlow::Node sink) {
// Sink is an argument to the ProcessBuilder constructor
exists(ClassInstanceExpr newProcessBuilderCall |
newProcessBuilderCall.getConstructedType().hasQualifiedName("java.lang", "ProcessBuilder") and
// Match the correct overload
newProcessBuilderCall.getConstructor().hasStringSignature("ProcessBuilder(String[])") and
newProcessBuilderCall.getArgument(0) = sink.asExpr()
)
}
}
from BashCommandConfig config, DataFlow::PathNode source, DataFlow::PathNode sink
where config.hasFlowPath(source, sink)
select sink.getNode(), source, sink, "Flow from bash command creation to execution" This uses a path query to visualize how the data flows from the array creation to the constructor call. Path queries can be especially useful when flow goes through multiple methods. With a regular query you could only report the start and end of the flow, which might make it difficult to comprehend the flow path. Note that CodeQL already has queries for untrusted user input used as process argument (see |
Beta Was this translation helpful? Give feedback.
-
I'm using this java source code to test codeql: Foo.txt
And I need to find all calls to ProcessBuilder whose argument is an array and the first two items equals to "bash" and "-c".
So I created this query:
I'm not sure how to match the string value? It looks like there's no such method in codeql.
Beta Was this translation helpful? Give feedback.
All reactions