Skip to content

Commit c0ffd90

Browse files
committed
C++: Add more random sources.
1 parent 4a19a99 commit c0ffd90

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed

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

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,58 @@ import semmle.code.cpp.security.TaintTracking
1919
import TaintedWithPath
2020
import Bounded
2121

22+
/**
23+
* A function that outputs random data such as `std::rand`.
24+
*/
25+
abstract class RandomFunction extends Function {
26+
/**
27+
* Gets the `FunctionOutput` that describes how this function returns the random data.
28+
*/
29+
FunctionOutput getFunctionOutput() { result.isReturnValue() }
30+
}
31+
32+
/**
33+
* The standard function `std::rand`.
34+
*/
35+
private class StdRand extends RandomFunction {
36+
StdRand() {
37+
this.hasGlobalOrStdOrBslName("rand") and
38+
this.getNumberOfParameters() = 0
39+
}
40+
}
41+
42+
/**
43+
* The Unix function `rand_r`.
44+
*/
45+
private class RandR extends RandomFunction {
46+
RandR() {
47+
this.hasGlobalName("rand_r") and
48+
this.getNumberOfParameters() = 1
49+
}
50+
}
51+
52+
/**
53+
* The Unix function `random`.
54+
*/
55+
private class Random extends RandomFunction {
56+
Random() {
57+
this.hasGlobalName("random") and
58+
this.getNumberOfParameters() = 1
59+
}
60+
}
61+
62+
/**
63+
* The Windows `rand_s` function.
64+
*/
65+
private class RandS extends RandomFunction {
66+
RandS() {
67+
this.hasGlobalName("rand_s") and
68+
this.getNumberOfParameters() = 1
69+
}
70+
71+
override FunctionOutput getFunctionOutput() { result.isParameterDeref(0) }
72+
}
73+
2274
predicate isUnboundedRandCall(FunctionCall fc) {
2375
exists(Function func | func = fc.getTarget() |
2476
func.hasGlobalOrStdOrBslName("rand") and

0 commit comments

Comments
 (0)