Skip to content

Commit abec99b

Browse files
author
Gulshan Singh
committed
C++: Add getSizeExpr and getSizeMult predicates to BufferAccess
1 parent 117a983 commit abec99b

File tree

1 file changed

+60
-42
lines changed

1 file changed

+60
-42
lines changed

cpp/ql/lib/semmle/code/cpp/security/BufferAccess.qll

Lines changed: 60 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,23 @@ abstract class BufferAccess extends Expr {
2929
*/
3030
abstract Expr getBuffer(string bufferDesc, int accessType);
3131

32-
abstract int getSize();
32+
/**
33+
* Gets the expression that represents the size of the buffer access. The
34+
* actual size is typically the value of this expression multiplied by the
35+
* result of `getSizeMult()`, in bytes.
36+
*/
37+
Expr getSizeExpr() { none() }
38+
39+
/**
40+
* Gets a constant multiplier for the buffer access size given by
41+
* `getSizeExpr`, in bytes.
42+
*/
43+
int getSizeMult() { none() }
44+
45+
/**
46+
* Gets the buffer access size in bytes.
47+
*/
48+
int getSize() { result = this.getSizeExpr().getValue().toInt() * this.getSizeMult() }
3349
}
3450

3551
/**
@@ -63,10 +79,10 @@ class MemcpyBA extends BufferAccess {
6379
accessType = 1
6480
}
6581

66-
override int getSize() {
67-
result =
68-
this.(FunctionCall).getArgument(2).getValue().toInt() *
69-
getPointedSize(this.(FunctionCall).getTarget().getParameter(0).getType())
82+
override Expr getSizeExpr() { result = this.(FunctionCall).getArgument(2) }
83+
84+
override int getSizeMult() {
85+
result = getPointedSize(this.(FunctionCall).getTarget().getParameter(0).getType())
7086
}
7187
}
7288

@@ -89,10 +105,10 @@ class BCopyBA extends BufferAccess {
89105
accessType = 1
90106
}
91107

92-
override int getSize() {
93-
result =
94-
this.(FunctionCall).getArgument(2).getValue().toInt() *
95-
getPointedSize(this.(FunctionCall).getTarget().getParameter(0).getType())
108+
override Expr getSizeExpr() { result = this.(FunctionCall).getArgument(2) }
109+
110+
override int getSizeMult() {
111+
result = getPointedSize(this.(FunctionCall).getTarget().getParameter(0).getType())
96112
}
97113
}
98114

@@ -115,10 +131,10 @@ class StrncpyBA extends BufferAccess {
115131
accessType = 2
116132
}
117133

118-
override int getSize() {
119-
result =
120-
this.(FunctionCall).getArgument(2).getValue().toInt() *
121-
getPointedSize(this.(FunctionCall).getTarget().getParameter(0).getType())
134+
override Expr getSizeExpr() { result = this.(FunctionCall).getArgument(2) }
135+
136+
override int getSizeMult() {
137+
result = getPointedSize(this.(FunctionCall).getTarget().getParameter(0).getType())
122138
}
123139
}
124140

@@ -141,10 +157,10 @@ class MemccpyBA extends BufferAccess {
141157
accessType = 2
142158
}
143159

144-
override int getSize() {
145-
result =
146-
this.(FunctionCall).getArgument(3).getValue().toInt() *
147-
getPointedSize(this.(FunctionCall).getTarget().getParameter(0).getType())
160+
override Expr getSizeExpr() { result = this.(FunctionCall).getArgument(3) }
161+
162+
override int getSizeMult() {
163+
result = getPointedSize(this.(FunctionCall).getTarget().getParameter(0).getType())
148164
}
149165
}
150166

@@ -172,10 +188,10 @@ class MemcmpBA extends BufferAccess {
172188
accessType = 2
173189
}
174190

175-
override int getSize() {
176-
result =
177-
this.(FunctionCall).getArgument(2).getValue().toInt() *
178-
getPointedSize(this.(FunctionCall).getTarget().getParameter(0).getType())
191+
override Expr getSizeExpr() { result = this.(FunctionCall).getArgument(2) }
192+
193+
override int getSizeMult() {
194+
result = getPointedSize(this.(FunctionCall).getTarget().getParameter(0).getType())
179195
}
180196
}
181197

@@ -199,10 +215,10 @@ class SwabBA extends BufferAccess {
199215
accessType = 1
200216
}
201217

202-
override int getSize() {
203-
result =
204-
this.(FunctionCall).getArgument(2).getValue().toInt() *
205-
getPointedSize(this.(FunctionCall).getTarget().getParameter(0).getType())
218+
override Expr getSizeExpr() { result = this.(FunctionCall).getArgument(2) }
219+
220+
override int getSizeMult() {
221+
result = getPointedSize(this.(FunctionCall).getTarget().getParameter(0).getType())
206222
}
207223
}
208224

@@ -222,10 +238,10 @@ class MemsetBA extends BufferAccess {
222238
accessType = 1
223239
}
224240

225-
override int getSize() {
226-
result =
227-
this.(FunctionCall).getArgument(2).getValue().toInt() *
228-
getPointedSize(this.(FunctionCall).getTarget().getParameter(0).getType())
241+
override Expr getSizeExpr() { result = this.(FunctionCall).getArgument(2) }
242+
243+
override int getSizeMult() {
244+
result = getPointedSize(this.(FunctionCall).getTarget().getParameter(0).getType())
229245
}
230246
}
231247

@@ -244,7 +260,9 @@ class ZeroMemoryBA extends BufferAccess {
244260
accessType = 1
245261
}
246262

247-
override int getSize() { result = this.(FunctionCall).getArgument(1).getValue().toInt() }
263+
override Expr getSizeExpr() { result = this.(FunctionCall).getArgument(1) }
264+
265+
override int getSizeMult() { result = 1 }
248266
}
249267

250268
/**
@@ -263,10 +281,10 @@ class MemchrBA extends BufferAccess {
263281
accessType = 2
264282
}
265283

266-
override int getSize() {
267-
result =
268-
this.(FunctionCall).getArgument(2).getValue().toInt() *
269-
getPointedSize(this.(FunctionCall).getTarget().getParameter(0).getType())
284+
override Expr getSizeExpr() { result = this.(FunctionCall).getArgument(2) }
285+
286+
override int getSizeMult() {
287+
result = getPointedSize(this.(FunctionCall).getTarget().getParameter(0).getType())
270288
}
271289
}
272290

@@ -285,11 +303,9 @@ class FreadBA extends BufferAccess {
285303
accessType = 2
286304
}
287305

288-
override int getSize() {
289-
result =
290-
this.(FunctionCall).getArgument(1).getValue().toInt() *
291-
this.(FunctionCall).getArgument(2).getValue().toInt()
292-
}
306+
override Expr getSizeExpr() { result = this.(FunctionCall).getArgument(1) }
307+
308+
override int getSizeMult() { result = this.(FunctionCall).getArgument(2).getValue().toInt() }
293309
}
294310

295311
/**
@@ -318,11 +334,13 @@ class ArrayExprBA extends BufferAccess {
318334
accessType = 3
319335
}
320336

337+
override Expr getSizeExpr() { result = this.(ArrayExpr).getArrayOffset() }
338+
321339
override int getSize() {
322340
// byte size of the buffer that would be required to support this
323341
// access
324-
result =
325-
(1 + this.(ArrayExpr).getArrayOffset().getValue().toInt()) *
326-
this.(ArrayExpr).getType().getSize()
342+
result = (1 + this.getSizeExpr().getValue().toInt()) * this.getSizeMult()
327343
}
344+
345+
override int getSizeMult() { result = this.(ArrayExpr).getType().getSize() }
328346
}

0 commit comments

Comments
 (0)