|
| 1 | +/** |
| 2 | + * @name Fizz Overflow |
| 3 | + * @description Narrowing conversions on untrusted data could enable |
| 4 | + * an attacker to trigger an integer overflow. |
| 5 | + * @kind path-problem |
| 6 | + * @problem.severity warning |
| 7 | + */ |
| 8 | + |
| 9 | +import cpp |
| 10 | +import semmle.code.cpp.ir.dataflow.TaintTracking |
| 11 | +import semmle.code.cpp.ir.IR |
| 12 | +import DataFlow::PathGraph |
| 13 | + |
| 14 | +/** |
| 15 | + * The endianness conversion function `Endian::big()`. |
| 16 | + * It is Folly's replacement for `ntohs` and `ntohl`. |
| 17 | + */ |
| 18 | +class EndianConvert extends Function { |
| 19 | + EndianConvert() { |
| 20 | + this.getName() = "big" and |
| 21 | + this.getDeclaringType().getName().matches("Endian") |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +class Cfg extends TaintTracking::Configuration { |
| 26 | + Cfg() { this = "FizzOverflowIR" } |
| 27 | + |
| 28 | + /** Holds if `source` is a call to `Endian::big()`. */ |
| 29 | + override predicate isSource(DataFlow::Node source) { |
| 30 | + source |
| 31 | + .asInstruction() |
| 32 | + .(CallInstruction) |
| 33 | + .getCallTarget() |
| 34 | + .(FunctionInstruction) |
| 35 | + .getFunctionSymbol() instanceof EndianConvert |
| 36 | + } |
| 37 | + |
| 38 | + /** Holds if `sink` is a narrowing conversion. */ |
| 39 | + override predicate isSink(DataFlow::Node sink) { |
| 40 | + sink.asInstruction().getResultSize() < sink |
| 41 | + .asInstruction() |
| 42 | + .(ConvertInstruction) |
| 43 | + .getUnary() |
| 44 | + .getResultSize() |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +from |
| 49 | + Cfg cfg, DataFlow::PathNode source, DataFlow::PathNode sink, ConvertInstruction conv, |
| 50 | + Type inputType, Type outputType |
| 51 | +where |
| 52 | + cfg.hasFlowPath(source, sink) and |
| 53 | + conv = sink.getNode().asInstruction() and |
| 54 | + inputType = conv.getUnary().getResultType() and |
| 55 | + outputType = conv.getResultType() |
| 56 | +select sink, source, sink, |
| 57 | + "Conversion of untrusted data from " + inputType + " to " + outputType + "." |
0 commit comments