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

Skip to content

Commit 0343bd6

Browse files
committed
CPP: Inline BufferAccess into NtohlArrayNoBound.qll (we'd prefer other queries to be written with the models library or Security.BufferAccess).
1 parent f50dd84 commit 0343bd6

2 files changed

Lines changed: 173 additions & 175 deletions

File tree

cpp/ql/src/Likely Bugs/Memory Management/BufferAccess.qll

Lines changed: 0 additions & 174 deletions
This file was deleted.

cpp/ql/src/Likely Bugs/Memory Management/NtohlArrayNoBound.qll

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

7179
class NetworkFunctionCall extends FunctionCall {
8180
NetworkFunctionCall() {

0 commit comments

Comments
 (0)