Skip to content

Commit 34fdeb4

Browse files
authored
Merge pull request github#13203 from jketema/deref-size
C++: Use range analysis-based `hasSize` predicate in `cpp/invalid-pointer-deref`
2 parents 1b06bf1 + d1efffe commit 34fdeb4

File tree

1 file changed

+11
-32
lines changed

1 file changed

+11
-32
lines changed

cpp/ql/src/experimental/Security/CWE/CWE-193/InvalidPointerDeref.ql

Lines changed: 11 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -48,44 +48,23 @@ bindingset[b]
4848
pragma[inline_late]
4949
predicate bounded2(Instruction i, Instruction b, int delta) { boundedImpl(i, b, delta) }
5050

51-
/**
52-
* Holds if the combination of `n` and `state` represents an appropriate
53-
* source for the expression `e` suitable for use-use flow.
54-
*/
55-
private predicate hasSizeImpl(Expr e, DataFlow::Node n, int state) {
56-
// The simple case: If the size is a variable access with no qualifier we can just use the
57-
// dataflow node for that expression and no state.
58-
exists(VariableAccess va |
59-
va = e and
60-
not va instanceof FieldAccess and
61-
n.asConvertedExpr() = va.getFullyConverted() and
62-
state = 0
63-
)
64-
or
65-
// If the size is a choice between two expressions we allow both to be nodes representing the size.
66-
exists(ConditionalExpr cond | cond = e | hasSizeImpl([cond.getThen(), cond.getElse()], n, state))
67-
or
68-
// If the size is an expression plus a constant, we pick the dataflow node of the expression and
69-
// remember the constant in the state.
70-
exists(Expr const, Expr nonconst |
71-
e.(AddExpr).hasOperands(const, nonconst) and
72-
state = const.getValue().toInt() and
73-
hasSizeImpl(nonconst, n, _)
74-
)
75-
or
76-
exists(Expr const, Expr nonconst |
77-
e.(SubExpr).hasOperands(const, nonconst) and
78-
state = -const.getValue().toInt() and
79-
hasSizeImpl(nonconst, n, _)
80-
)
81-
}
51+
VariableAccess getAVariableAccess(Expr e) { e.getAChild*() = result }
8252

8353
/**
8454
* Holds if `(n, state)` pair represents the source of flow for the size
8555
* expression associated with `alloc`.
8656
*/
8757
predicate hasSize(HeuristicAllocationExpr alloc, DataFlow::Node n, int state) {
88-
hasSizeImpl(alloc.getSizeExpr(), n, state)
58+
exists(VariableAccess va, Expr size, int delta |
59+
size = alloc.getSizeExpr() and
60+
// Get the unique variable in a size expression like `x` in `malloc(x + 1)`.
61+
va = unique( | | getAVariableAccess(size)) and
62+
// Compute `delta` as the constant difference between `x` and `x + 1`.
63+
bounded1(any(Instruction instr | instr.getUnconvertedResultExpression() = size),
64+
any(LoadInstruction load | load.getUnconvertedResultExpression() = va), delta) and
65+
n.asConvertedExpr() = va.getFullyConverted() and
66+
state = delta
67+
)
8968
}
9069

9170
/**

0 commit comments

Comments
 (0)