Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9b5437c

Browse files
authored
Merge pull request #2318 from rdmarsh2/rdmarsh/docs/cpp/taint-tracking-sanitizer-example
C++/Docs: add example based on NtohlArrayNoBound
2 parents 9471134 + 15f50e6 commit 9b5437c

1 file changed

Lines changed: 43 additions & 0 deletions

File tree

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

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,49 @@ 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 defines ``isSanitizer`` to prevent taint from propagating through them. It also uses ``isAdditionalTaintStep`` to add flow from loop bounds to loop indexes.
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 TaintTracking::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 isAdditionalTaintStep(DataFlow::Node pred, DataFlow::Node succ) {
267+
exists(Loop loop, LoopCounter lc |
268+
loop = lc.getALoop() and
269+
loop.getControllingExpr().(RelationalOperation).getGreaterOperand() = pred.asExpr() |
270+
succ.asExpr() = lc.getVariableAccessInLoop(loop)
271+
)
272+
}
273+
274+
override predicate isSanitizer(DataFlow::Node node) {
275+
exists(GuardCondition gc, Variable v |
276+
gc.getAChild*() = v.getAnAccess() and
277+
node.asExpr() = v.getAnAccess() and
278+
gc.controls(node.asExpr().getBasicBlock(), _)
279+
)
280+
}
281+
}
282+
283+
from DataFlow::Node ntohl, DataFlow::Node offset, NetworkToBufferSizeConfiguration conf
284+
where conf.hasFlow(ntohl, offset)
285+
select offset, "This array offset may be influenced by $@.", ntohl,
286+
"converted data from the network"
287+
288+
289+
247290
Exercises
248291
~~~~~~~~~
249292

0 commit comments

Comments
 (0)