|
| 1 | +/** |
| 2 | + * @name MMIO/DMA unsanitized memory copy |
| 3 | + * @description Memory copy sizes derived from memory-mapped I/O or DMA |
| 4 | + * descriptor fields without bounds validation may overflow |
| 5 | + * destination buffers. |
| 6 | + * @kind path-problem |
| 7 | + * @problem.severity error |
| 8 | + * @security-severity 8.6 |
| 9 | + * @precision medium |
| 10 | + * @id cpp/mmio-unsanitized-memcpy |
| 11 | + * @tags security |
| 12 | + * external/cwe/cwe-120 |
| 13 | + * external/cwe/cwe-787 |
| 14 | + */ |
| 15 | + |
| 16 | +import cpp |
| 17 | +import semmle.code.cpp.dataflow.new.TaintTracking |
| 18 | +import semmle.code.cpp.controlflow.IRGuards |
| 19 | +import MmioFlow::PathGraph |
| 20 | + |
| 21 | +/** Holds if `e` is an expression that reads MMIO/DMA hardware state. */ |
| 22 | +predicate isMmioExpr(Expr e) { |
| 23 | + exists(VariableAccess va | va = e and va.getTarget().isVolatile()) |
| 24 | + or |
| 25 | + exists(FieldAccess fa | fa = e and fa.getTarget().getType().isVolatile()) |
| 26 | + or |
| 27 | + exists(FunctionCall call | |
| 28 | + call = e and |
| 29 | + call.getTarget().hasName(["READ_REG", "GET_MMIO", "REG_READ", "DMA_READ"]) |
| 30 | + ) |
| 31 | + or |
| 32 | + exists(PointerDereferenceExpr deref | |
| 33 | + deref = e and |
| 34 | + deref.getOperand().getUnspecifiedType() instanceof PointerType and |
| 35 | + deref.getOperand().getUnspecifiedType().(PointerType).getBaseType().isVolatile() |
| 36 | + ) |
| 37 | +} |
| 38 | + |
| 39 | +predicate isMmioSource(DataFlow::Node source) { |
| 40 | + isMmioExpr(source.asExpr()) |
| 41 | + or |
| 42 | + exists(MacroInvocation mi | |
| 43 | + mi.getMacro().hasName(["READ_REG", "GET_MMIO", "REG_READ", "DMA_READ"]) and |
| 44 | + source.asExpr() = mi.getExpr() |
| 45 | + ) |
| 46 | +} |
| 47 | + |
| 48 | +predicate isMemcpySizeSink(DataFlow::Node sink, FunctionCall fc) { |
| 49 | + fc.getTarget().hasName(["memcpy", "memmove", "strncpy", "wmemcpy", "wmemmove"]) and |
| 50 | + sink.asExpr() = fc.getArgument(2) |
| 51 | +} |
| 52 | + |
| 53 | +/** Recognizes relational comparison bounds checks using public IRGuards API. */ |
| 54 | +predicate lessThanOrEqual(IRGuardCondition g, Expr e, boolean branch) { |
| 55 | + exists(Operand left | |
| 56 | + g.comparesLt(left, _, _, true, branch) or |
| 57 | + g.comparesEq(left, _, _, true, branch) |
| 58 | + | |
| 59 | + left.getDef().getConvertedResultExpression() = e |
| 60 | + ) |
| 61 | +} |
| 62 | + |
| 63 | +module MmioConfig implements DataFlow::ConfigSig { |
| 64 | + predicate isSource(DataFlow::Node source) { isMmioSource(source) } |
| 65 | + |
| 66 | + predicate isSink(DataFlow::Node sink) { isMemcpySizeSink(sink, _) } |
| 67 | + |
| 68 | + predicate isBarrier(DataFlow::Node node) { |
| 69 | + node = DataFlow::BarrierGuard<lessThanOrEqual/3>::getABarrierNode() or |
| 70 | + node = DataFlow::BarrierGuard<lessThanOrEqual/3>::getAnIndirectBarrierNode() |
| 71 | + } |
| 72 | + |
| 73 | + predicate observeDiffInformedIncrementalMode() { any() } |
| 74 | +} |
| 75 | + |
| 76 | +module MmioFlow = TaintTracking::Global<MmioConfig>; |
| 77 | + |
| 78 | +from FunctionCall memcpyCall, MmioFlow::PathNode source, MmioFlow::PathNode sink |
| 79 | +where |
| 80 | + MmioFlow::flowPath(source, sink) and |
| 81 | + isMemcpySizeSink(sink.getNode(), memcpyCall) |
| 82 | +select memcpyCall, source, sink, |
| 83 | + "Memory copy size argument is derived from $@ without sufficient bounds validation.", |
| 84 | + source.getNode(), "an MMIO/DMA hardware register read" |
0 commit comments