|
| 1 | +import cpp |
| 2 | +import semmle.code.cpp.dataflow.TaintTracking |
| 3 | +private import semmle.code.cpp.dataflow.RecursionPrevention |
| 4 | + |
| 5 | +/** |
| 6 | + * A buffer which includes an allocation size. |
| 7 | + */ |
| 8 | +abstract class BufferWithSize extends DataFlow::Node { |
| 9 | + abstract Expr getSizeExpr(); |
| 10 | + |
| 11 | + BufferAccess getAnAccess() { |
| 12 | + any(BufferWithSizeConfig bsc).hasFlow(this, DataFlow::exprNode(result.getPointer())) |
| 13 | + } |
| 14 | +} |
| 15 | + |
| 16 | +/** An allocation function. */ |
| 17 | +abstract class Alloc extends Function { } |
| 18 | + |
| 19 | +/** |
| 20 | + * Allocation functions identified by the QL for C/C++ standard library. |
| 21 | + */ |
| 22 | +class DefaultAlloc extends Alloc { |
| 23 | + DefaultAlloc() { allocationFunction(this) } |
| 24 | +} |
| 25 | + |
| 26 | +/** A buffer created through a call to an allocation function. */ |
| 27 | +class AllocBuffer extends BufferWithSize { |
| 28 | + FunctionCall call; |
| 29 | + |
| 30 | + AllocBuffer() { |
| 31 | + asExpr() = call and |
| 32 | + call.getTarget() instanceof Alloc |
| 33 | + } |
| 34 | + |
| 35 | + override Expr getSizeExpr() { result = call.getArgument(0) } |
| 36 | +} |
| 37 | + |
| 38 | +/** |
| 39 | + * Find accesses of buffers for which we have a size expression. |
| 40 | + */ |
| 41 | +private class BufferWithSizeConfig extends TaintTracking::Configuration { |
| 42 | + BufferWithSizeConfig() { this = "BufferWithSize" } |
| 43 | + |
| 44 | + override predicate isSource(DataFlow::Node n) { n = any(BufferWithSize b) } |
| 45 | + |
| 46 | + override predicate isSink(DataFlow::Node n) { n.asExpr() = any(BufferAccess ae).getPointer() } |
| 47 | + |
| 48 | + override predicate isSanitizer(DataFlow::Node s) { |
| 49 | + s = any(BufferWithSize b) and |
| 50 | + s.asExpr().getControlFlowScope() instanceof Alloc |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +/** |
| 55 | + * An access (read or write) to a buffer, provided as a pair of |
| 56 | + * a pointer to the buffer and the length of data to be read or written. |
| 57 | + * Extend this class to support different kinds of buffer access. |
| 58 | + */ |
| 59 | +abstract class BufferAccess extends Locatable { |
| 60 | + /** Gets the pointer to the buffer being accessed. */ |
| 61 | + abstract Expr getPointer(); |
| 62 | + |
| 63 | + /** Gets the length of the data being read or written by this buffer access. */ |
| 64 | + abstract Expr getAccessedLength(); |
| 65 | +} |
| 66 | + |
| 67 | +/** |
| 68 | + * A buffer access through an array expression. |
| 69 | + */ |
| 70 | +class ArrayBufferAccess extends BufferAccess, ArrayExpr { |
| 71 | + override Expr getPointer() { result = this.getArrayBase() } |
| 72 | + |
| 73 | + override Expr getAccessedLength() { result = this.getArrayOffset() } |
| 74 | +} |
| 75 | + |
| 76 | +/** |
| 77 | + * A buffer access through an overloaded array expression. |
| 78 | + */ |
| 79 | +class OverloadedArrayBufferAccess extends BufferAccess, OverloadedArrayExpr { |
| 80 | + override Expr getPointer() { result = this.getQualifier() } |
| 81 | + |
| 82 | + override Expr getAccessedLength() { result = this.getAnArgument() } |
| 83 | +} |
| 84 | + |
| 85 | +/** |
| 86 | + * A buffer access through pointer arithmetic. |
| 87 | + */ |
| 88 | +class PointerArithmeticAccess extends BufferAccess, Expr { |
| 89 | + PointerArithmeticOperation p; |
| 90 | + |
| 91 | + PointerArithmeticAccess() { |
| 92 | + this = p and |
| 93 | + p.getAnOperand().getType().getUnspecifiedType() instanceof IntegralType and |
| 94 | + not p.getParent() instanceof ComparisonOperation |
| 95 | + } |
| 96 | + |
| 97 | + override Expr getPointer() { |
| 98 | + result = p.getAnOperand() and |
| 99 | + result.getType().getUnspecifiedType() instanceof PointerType |
| 100 | + } |
| 101 | + |
| 102 | + override Expr getAccessedLength() { |
| 103 | + result = p.getAnOperand() and |
| 104 | + result.getType().getUnspecifiedType() instanceof IntegralType |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +/** |
| 109 | + * A pair of buffer accesses through a call to memcpy. |
| 110 | + */ |
| 111 | +class MemCpy extends BufferAccess, FunctionCall { |
| 112 | + MemCpy() { getTarget().hasName("memcpy") } |
| 113 | + |
| 114 | + override Expr getPointer() { |
| 115 | + result = getArgument(0) or |
| 116 | + result = getArgument(1) |
| 117 | + } |
| 118 | + |
| 119 | + override Expr getAccessedLength() { result = getArgument(2) } |
| 120 | +} |
| 121 | + |
| 122 | +class StrncpySizeExpr extends BufferAccess, FunctionCall { |
| 123 | + StrncpySizeExpr() { getTarget().hasName("strncpy") } |
| 124 | + |
| 125 | + override Expr getPointer() { |
| 126 | + result = getArgument(0) or |
| 127 | + result = getArgument(1) |
| 128 | + } |
| 129 | + |
| 130 | + override Expr getAccessedLength() { result = getArgument(2) } |
| 131 | +} |
| 132 | + |
| 133 | +class RecvSizeExpr extends BufferAccess, FunctionCall { |
| 134 | + RecvSizeExpr() { getTarget().hasName("recv") } |
| 135 | + |
| 136 | + override Expr getPointer() { result = getArgument(1) } |
| 137 | + |
| 138 | + override Expr getAccessedLength() { result = getArgument(2) } |
| 139 | +} |
| 140 | + |
| 141 | +class SendSizeExpr extends BufferAccess, FunctionCall { |
| 142 | + SendSizeExpr() { getTarget().hasName("send") } |
| 143 | + |
| 144 | + override Expr getPointer() { result = getArgument(1) } |
| 145 | + |
| 146 | + override Expr getAccessedLength() { result = getArgument(2) } |
| 147 | +} |
| 148 | + |
| 149 | +class SnprintfSizeExpr extends BufferAccess, FunctionCall { |
| 150 | + SnprintfSizeExpr() { getTarget().hasName("snprintf") } |
| 151 | + |
| 152 | + override Expr getPointer() { result = getArgument(0) } |
| 153 | + |
| 154 | + override Expr getAccessedLength() { result = getArgument(1) } |
| 155 | +} |
| 156 | + |
| 157 | +class MemcmpSizeExpr extends BufferAccess, FunctionCall { |
| 158 | + MemcmpSizeExpr() { getTarget().hasName("Memcmp") } |
| 159 | + |
| 160 | + override Expr getPointer() { |
| 161 | + result = getArgument(0) or |
| 162 | + result = getArgument(1) |
| 163 | + } |
| 164 | + |
| 165 | + override Expr getAccessedLength() { result = getArgument(2) } |
| 166 | +} |
| 167 | + |
| 168 | +class MallocSizeExpr extends BufferAccess, FunctionCall { |
| 169 | + MallocSizeExpr() { getTarget().hasName("malloc") } |
| 170 | + |
| 171 | + override Expr getPointer() { none() } |
| 172 | + |
| 173 | + override Expr getAccessedLength() { result = getArgument(1) } |
| 174 | +} |
0 commit comments