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

Skip to content

Commit 8c75e73

Browse files
committed
CPP: Widen TaintedAllocationSize.ql.
1 parent dab1bba commit 8c75e73

4 files changed

Lines changed: 29 additions & 17 deletions

File tree

cpp/ql/src/Security/CWE/CWE-190/TaintedAllocationSize.ql

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,22 @@
1414
import cpp
1515
import semmle.code.cpp.security.TaintTracking
1616

17-
from Expr source, Expr tainted, BinaryArithmeticOperation oper,
18-
SizeofOperator sizeof, string taintCause
19-
where tainted(source, tainted)
20-
and oper.getAnOperand() = tainted
21-
and oper.getOperator() = "*"
22-
and oper.getAnOperand() = sizeof
23-
and oper != tainted
24-
and sizeof.getValue().toInt() > 1
25-
and isUserInput(source, taintCause)
17+
predicate taintedAllocSize(Expr e, Expr source, string taintCause) {
18+
(
19+
isAllocationExpr(e) or
20+
any(MulExpr me | me.getAChild() instanceof SizeofOperator) = e
21+
) and exists(Expr tainted |
22+
tainted = e.getAChild() and
23+
tainted.getType().getUnspecifiedType() instanceof IntegralType and
24+
isUserInput(source, taintCause) and
25+
tainted(source, tainted)
26+
)
27+
}
28+
29+
from Expr e, Expr source, string taintCause
30+
where
31+
taintedAllocSize(e, source, taintCause)
2632
select
27-
oper, "This allocation size is derived from $@ and might overflow",
33+
e, "This allocation size is derived from $@ and might overflow",
2834
source, "user input (" + taintCause + ")"
35+

cpp/ql/src/semmle/code/cpp/security/TaintTracking.qll

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -245,9 +245,10 @@ predicate insideFunctionValueMoveTo(Element src, Element dest)
245245
and format.getConversionChar(arg - formattingSend.getTarget().getNumberOfParameters()) = argFormat
246246
and (argFormat = "s" or argFormat = "S" or argFormat = "@"))
247247
// Expressions computed from tainted data are also tainted
248-
or (exists (FunctionCall call | dest = call and isPureFunction(call.getTarget().getName()) |
249-
call.getAnArgument() = src
250-
and forall(Expr arg | arg = call.getAnArgument() | arg = src or predictable(arg))))
248+
or exists(FunctionCall call | dest = call and isPureFunction(call.getTarget().getName()) |
249+
call.getAnArgument() = src and
250+
forall(Expr arg | arg = call.getAnArgument() | arg = src or predictable(arg))
251+
)
251252
or exists(Element a, Element b |
252253
moveToDependingOnSide(a, b) and
253254
if insideValueSource(a) then
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,6 @@
1+
| test.cpp:42:31:42:36 | call to malloc | This allocation size is derived from $@ and might overflow | test.cpp:39:21:39:24 | argv | user input (argv) |
12
| test.cpp:43:38:43:63 | ... * ... | This allocation size is derived from $@ and might overflow | test.cpp:39:21:39:24 | argv | user input (argv) |
3+
| test.cpp:48:25:48:30 | call to malloc | This allocation size is derived from $@ and might overflow | test.cpp:39:21:39:24 | argv | user input (argv) |
4+
| test.cpp:49:17:49:30 | new[] | This allocation size is derived from $@ and might overflow | test.cpp:39:21:39:24 | argv | user input (argv) |
25
| test.cpp:52:35:52:60 | ... * ... | This allocation size is derived from $@ and might overflow | test.cpp:39:21:39:24 | argv | user input (argv) |
6+
| test.cpp:55:11:55:24 | new[] | This allocation size is derived from $@ and might overflow | test.cpp:39:21:39:24 | argv | user input (argv) |

cpp/ql/test/query-tests/Security/CWE/CWE-190/semmle/TaintedAllocationSize/test.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,20 +39,20 @@ int main(int argc, char **argv) {
3939
int tainted = atoi(argv[1]);
4040

4141
MyStruct *arr1 = (MyStruct *)malloc(sizeof(MyStruct)); // GOOD
42-
MyStruct *arr2 = (MyStruct *)malloc(tainted); // BAD [NOT DETECTED]
42+
MyStruct *arr2 = (MyStruct *)malloc(tainted); // BAD
4343
MyStruct *arr3 = (MyStruct *)malloc(tainted * sizeof(MyStruct)); // BAD
4444
MyStruct *arr4 = (MyStruct *)malloc(getTainted() * sizeof(MyStruct)); // BAD [NOT DETECTED]
4545
MyStruct *arr5 = (MyStruct *)malloc(sizeof(MyStruct) + tainted); // BAD [NOT DETECTED]
4646

4747
int size = tainted * 8;
48-
char *chars1 = (char *)malloc(size); // BAD [NOT DETECTED]
49-
char *chars2 = new char[size]; // BAD [NOT DETECTED]
48+
char *chars1 = (char *)malloc(size); // BAD
49+
char *chars2 = new char[size]; // BA
5050
char *chars3 = new char[8]; // GOOD
5151

5252
arr1 = (MyStruct *)realloc(arr1, sizeof(MyStruct) * tainted); // BAD
5353

5454
size = 8;
55-
chars3 = new char[size]; // GOOD
55+
chars3 = new char[size]; // GOOD [FALSE POSITIVE]
5656

5757
return 0;
5858
}

0 commit comments

Comments
 (0)