@@ -16,8 +16,8 @@ import cpp
1616import semmle.code.cpp.security.Overflow
1717import semmle.code.cpp.security.Security
1818import semmle.code.cpp.security.TaintTracking
19- import semmle.code.cpp.rangeanalysis.SimpleRangeAnalysis
2019import TaintedWithPath
20+ import Bounded
2121
2222predicate isUnboundedRandCall ( FunctionCall fc ) {
2323 exists ( Function func | func = fc .getTarget ( ) |
@@ -27,74 +27,6 @@ predicate isUnboundedRandCall(FunctionCall fc) {
2727 )
2828}
2929
30- /**
31- * An operand `e` of a division expression (i.e., `e` is an operand of either a `DivExpr` or
32- * a `AssignDivExpr`) is bounded when `e` is the left-hand side of the division.
33- */
34- pragma [ inline]
35- predicate boundedDiv ( Expr e , Expr left ) { e = left }
36-
37- /**
38- * An operand `e` of a remainder expression `rem` (i.e., `rem` is either a `RemExpr` or
39- * an `AssignRemExpr`) with left-hand side `left` and right-ahnd side `right` is bounded
40- * when `e` is `left` and `right` is upper bounded by some number that is less than the maximum integer
41- * allowed by the result type of `rem`.
42- */
43- pragma [ inline]
44- predicate boundedRem ( Expr e , Expr rem , Expr left , Expr right ) {
45- e = left and
46- upperBound ( right .getFullyConverted ( ) ) < exprMaxVal ( rem .getFullyConverted ( ) )
47- }
48-
49- /**
50- * An operand `e` of a bitwise and expression `andExpr` (i.e., `andExpr` is either an `BitwiseAndExpr`
51- * or an `AssignAndExpr`) with operands `operand1` and `operand2` is the operand that is not `e` is upper
52- * bounded by some number that is less than the maximum integer allowed by the result type of `andExpr`.
53- */
54- pragma [ inline]
55- predicate boundedBitwiseAnd ( Expr e , Expr andExpr , Expr operand1 , Expr operand2 ) {
56- operand1 != operand2 and
57- e = operand1 and
58- upperBound ( operand2 .getFullyConverted ( ) ) < exprMaxVal ( andExpr .getFullyConverted ( ) )
59- }
60-
61- /**
62- * Holds if `fc` is a part of the left operand of a binary operation that greatly reduces the range
63- * of possible values.
64- */
65- predicate bounded ( Expr e ) {
66- // For `%` and `&` we require that `e` is bounded by a value that is strictly smaller than the
67- // maximum possible value of the result type of the operation.
68- // For example, the function call `rand()` is considered bounded in the following program:
69- // ```
70- // int i = rand() % (UINT8_MAX + 1);
71- // ```
72- // but not in:
73- // ```
74- // unsigned char uc = rand() % (UINT8_MAX + 1);
75- // ```
76- exists ( RemExpr rem | boundedRem ( e , rem , rem .getLeftOperand ( ) , rem .getRightOperand ( ) ) )
77- or
78- exists ( AssignRemExpr rem | boundedRem ( e , rem , rem .getLValue ( ) , rem .getRValue ( ) ) )
79- or
80- exists ( BitwiseAndExpr andExpr |
81- boundedBitwiseAnd ( e , andExpr , andExpr .getAnOperand ( ) , andExpr .getAnOperand ( ) )
82- )
83- or
84- exists ( AssignAndExpr andExpr |
85- boundedBitwiseAnd ( e , andExpr , andExpr .getAnOperand ( ) , andExpr .getAnOperand ( ) )
86- )
87- or
88- // Optimitically assume that a division always yields a much smaller value.
89- boundedDiv ( e , any ( DivExpr div ) .getLeftOperand ( ) )
90- or
91- boundedDiv ( e , any ( AssignDivExpr div ) .getLValue ( ) )
92- or
93- boundedDiv ( e , any ( RShiftExpr shift ) .getLeftOperand ( ) )
94- or
95- boundedDiv ( e , any ( AssignRShiftExpr div ) .getLValue ( ) )
96- }
97-
9830predicate isUnboundedRandCallOrParent ( Expr e ) {
9931 isUnboundedRandCall ( e )
10032 or
0 commit comments