Skip to content

Commit 9e84554

Browse files
committed
C++/Docs: add example based on NtohlArrayNoBound
1 parent 6c9f926 commit 9e84554

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

docs/language/learn-ql/cpp/dataflow.rst

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,41 @@ The following data flow configuration tracks data flow from environment variable
244244
select fopen, "This 'fopen' uses data from $@.",
245245
getenv, "call to 'getenv'"
246246
247+
The following taint tracking configuration tracks data from a call to ``ntohl`` to an array index operation. It uses the ``Guards`` library to recognize expressions that have been bounds checked and avoid propagating taint through them.
248+
249+
.. code-block:: ql
250+
251+
import cpp
252+
import semmle.code.cpp.controlflow.Guards
253+
import semmle.code.cpp.dataflow.TaintTracking
254+
255+
class NetworkToBufferSizeConfiguration extends DataFlow::Configuration {
256+
NetworkToBufferSizeConfiguration() { this = "NetworkToBufferSizeConfiguration" }
257+
258+
override predicate isSource(DataFlow::Node node) {
259+
node.asExpr().(FunctionCall).getTarget().hasGlobalName("ntohl")
260+
}
261+
262+
override predicate isSink(DataFlow::Node node) {
263+
exists(ArrayExpr ae | node.asExpr() = ae.getArrayOffset())
264+
}
265+
266+
override predicate isBarrier(DataFlow::Node node) {
267+
exists(GuardCondition gc, Variable v |
268+
gc.getAChild*() = v.getAnAccess() and
269+
node.asExpr() = v.getAnAccess() and
270+
gc.controls(node.asExpr().getBasicBlock(), _)
271+
)
272+
}
273+
}
274+
275+
from DataFlow::Node ntohl, DataFlow::Node offset, NetworkToBufferSizeConfiguration conf
276+
where conf.hasFlow(ntohl, offset)
277+
select offset, "This array offset may be influenced by $@.", ntohl,
278+
"converted data from the network"
279+
280+
281+
247282
Exercises
248283
~~~~~~~~~
249284

0 commit comments

Comments
 (0)