I am new to CodeQL and have started learning about dataflow queries for C/C++ programs. Following is a excerpt of a C program that I want to analyse:
int main(int argc, char * argv[])
{
unsigned short size, x, y;
int r1, r2;
x = atoi(argv[1]);// one dim of the data
y = atoi(argv[2]);//other dim of the data
size = x*y; //total size of the data
r1 = MyVuln(size*sizeof(char));
r2 = MyVuln(x*sizeof(char));
...
// some code
...
return 0
}
In the above example, I want to capture if MyVuln
function is called with size
as argument. The size
is defined as a result of AssignExpr
such that its Rvalue
is a result of multiplication. Following is the COdeQL queries that I wrote:
/*
@kind path-problem
*/
import cpp
import semmle.code.cpp.dataflow.new.DataFlow
//import DataFlow::PathGraph
from Function myvuln, FunctionCall fc, AssignExpr ab
where
myvuln.hasGlobalName("MyVuln")
and fc.getTarget() = myvuln
and ab.getLValue().getType().getUnspecifiedType() instanceof IntegralType
and ab.getRValue() instanceof MulExpr
and exists (DataFlow::Node src, DataFlow::Node sink|
src.asExpr() = ab.getLValue()
and sink.asExpr() = fc.getArgument(0)
and DataFlow::localFlow(src, sink)
)
select fc, "MyVuln with Arithmetic arg at " + fc.getLocation().toString()
The query returns no result (I am using CodeQl with VS Code). I also checked if a smaller partial query can detect expression corresponding to size
definition and it is working. I also checked if the query finds calls to MyVuln and it is working. Only when I start writing dataflow path query, I am getting no result. This type of query seems pretty straight forward, but I am not getting any clue where I have gone wrong or what is that I am missing in this query. A help is highly appreciates.
thanks