Skip to content

Commit a89c02f

Browse files
kevinbackhouseneheb
authored andcommitted
Add support for indirect null-pointer dereferences.
1 parent 79ffedc commit a89c02f

File tree

1 file changed

+38
-11
lines changed

1 file changed

+38
-11
lines changed

.github/codeql-queries/exiv2-cpp-queries/null_metadata_in_print.ql

Lines changed: 38 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,42 @@ class PrintFunction extends Function {
2424
}
2525
}
2626

27-
from PrintFunction f, Parameter p, Call call, Expr qualifier
28-
where
29-
p = f.getParameter(2) and
30-
qualifier = p.getAnAccess() and
31-
call.getQualifier() = qualifier and
32-
// Don't complain if the access is protected by a null check.
33-
not exists(GuardCondition nonNullCheck, BasicBlock block, boolean branch |
34-
validCheckExpr(nonNullCheck, p) and
35-
nonNullCheck.controls(block, branch) and
36-
block.contains(call)
27+
predicate metadataDeref(Expr metadata) {
28+
exists(Call call | call.getQualifier() = metadata)
29+
or
30+
exists(FunctionCall call, int argIndex, Function f |
31+
call.getArgument(argIndex) = metadata and
32+
f = call.getTarget() and
33+
metadataDeref(f.getParameter(argIndex).getAnAccess())
3734
)
38-
select qualifier, "Print functions need to check that the metadata isn't null."
35+
}
36+
37+
predicate unsafePointerParam(Function f, int paramIndex, Expr use) {
38+
exists(Parameter p |
39+
p = f.getParameter(paramIndex) and
40+
use = p.getAnAccess() and
41+
unsafePointerExpr(use) and
42+
not exists(GuardCondition nonNullCheck, BasicBlock block, boolean branch |
43+
validCheckExpr(nonNullCheck, p) and
44+
nonNullCheck.controls(block, branch) and
45+
block.contains(use)
46+
)
47+
)
48+
}
49+
50+
predicate unsafePointerExpr(Expr e) {
51+
exists(Call call |
52+
call.getQualifier() = e and
53+
e.getType().getUnspecifiedType() instanceof PointerType
54+
)
55+
or
56+
exists(FunctionCall call, int argIndex, Function f |
57+
call.getArgument(argIndex) = e and
58+
f = call.getTarget() and
59+
unsafePointerParam(f, argIndex, _)
60+
)
61+
}
62+
63+
from PrintFunction printfcn, Parameter p, Expr metadata
64+
where unsafePointerParam(printfcn, 2, metadata)
65+
select metadata, "Print functions need to check that the metadata isn't null."

0 commit comments

Comments
 (0)