Skip to content

Commit be2a480

Browse files
authored
Merge pull request #843 from geoffw0/strtoul
CPP: Improve ArithmeticTainted.ql
2 parents fc5b9dd + b0805f8 commit be2a480

File tree

4 files changed

+35
-11
lines changed

4 files changed

+35
-11
lines changed

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

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,18 @@ import semmle.code.cpp.security.Overflow
1616
import semmle.code.cpp.security.Security
1717
import semmle.code.cpp.security.TaintTracking
1818

19-
predicate taintedVarAccess(Expr origin, VariableAccess va) {
20-
isUserInput(origin, _) and
21-
tainted(origin, va)
22-
}
23-
24-
from Expr origin, Operation op, VariableAccess va, string effect
25-
where taintedVarAccess(origin, va)
26-
and op.getAnOperand() = va
19+
from Expr origin, Operation op, Expr e, string effect
20+
where isUserInput(origin, _)
21+
and tainted(origin, e)
22+
and op.getAnOperand() = e
2723
and
2824
(
29-
(missingGuardAgainstUnderflow(op, va) and effect = "underflow") or
30-
(missingGuardAgainstOverflow(op, va) and effect = "overflow")
25+
(missingGuardAgainstUnderflow(op, e) and effect = "underflow") or
26+
(missingGuardAgainstOverflow(op, e) and effect = "overflow") or
27+
(not e instanceof VariableAccess and effect = "overflow")
28+
) and (
29+
op instanceof UnaryArithmeticOperation or
30+
op instanceof BinaryArithmeticOperation
3131
)
32-
select va, "$@ flows to here and is used in arithmetic, potentially causing an " + effect + ".",
32+
select e, "$@ flows to here and is used in arithmetic, potentially causing an " + effect + ".",
3333
origin, "User-provided value"

cpp/ql/test/query-tests/Security/CWE/CWE-190/semmle/tainted/ArithmeticTainted.expected

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
| test3.c:15:10:15:10 | x | $@ flows to here and is used in arithmetic, potentially causing an overflow. | test3.c:11:15:11:18 | argv | User-provided value |
22
| test3.c:15:14:15:14 | y | $@ flows to here and is used in arithmetic, potentially causing an overflow. | test3.c:11:15:11:18 | argv | User-provided value |
33
| test3.c:15:18:15:18 | z | $@ flows to here and is used in arithmetic, potentially causing an overflow. | test3.c:11:15:11:18 | argv | User-provided value |
4+
| test5.cpp:17:6:17:18 | call to getTaintedInt | $@ flows to here and is used in arithmetic, potentially causing an overflow. | test5.cpp:9:7:9:9 | buf | User-provided value |
5+
| test5.cpp:19:6:19:6 | y | $@ flows to here and is used in arithmetic, potentially causing an overflow. | test5.cpp:9:7:9:9 | buf | User-provided value |
6+
| test5.cpp:19:6:19:6 | y | $@ flows to here and is used in arithmetic, potentially causing an underflow. | test5.cpp:9:7:9:9 | buf | User-provided value |
47
| test.c:14:15:14:28 | maxConnections | $@ flows to here and is used in arithmetic, potentially causing an overflow. | test.c:11:29:11:32 | argv | User-provided value |
58
| test.c:14:15:14:28 | maxConnections | $@ flows to here and is used in arithmetic, potentially causing an underflow. | test.c:11:29:11:32 | argv | User-provided value |
69
| test.c:44:7:44:10 | len2 | $@ flows to here and is used in arithmetic, potentially causing an underflow. | test.c:41:17:41:20 | argv | User-provided value |
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
| test3.c:12:31:12:34 | * ... | $@ flows to here and is used in an expression which might overflow negatively. | test3.c:11:15:11:18 | argv | User-provided value |
22
| test3.c:13:16:13:19 | * ... | $@ flows to here and is used in an expression which might overflow negatively. | test3.c:11:15:11:18 | argv | User-provided value |
33
| test4.cpp:13:17:13:20 | access to array | $@ flows to here and is used in an expression which might overflow negatively. | test4.cpp:9:13:9:16 | argv | User-provided value |
4+
| test5.cpp:10:9:10:15 | call to strtoul | $@ flows to here and is used in an expression which might overflow. | test5.cpp:9:7:9:9 | buf | User-provided value |
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
2+
char *gets(char *s);
3+
unsigned long int strtoul( const char * nptr, char ** endptr, int base);
4+
5+
int getTaintedInt()
6+
{
7+
char buf[128];
8+
9+
gets(buf);
10+
return strtoul(buf, 0, 10);
11+
}
12+
13+
void useTaintedInt()
14+
{
15+
int x, y;
16+
17+
x = getTaintedInt() * 1024; // BAD: arithmetic on a tainted value
18+
y = getTaintedInt();
19+
y = y * 1024; // BAD: arithmetic on a tainted value
20+
}

0 commit comments

Comments
 (0)