Skip to content

Commit fc52992

Browse files
committed
fix conversions in some equality operations
1 parent 2d31be3 commit fc52992

File tree

1 file changed

+21
-29
lines changed

1 file changed

+21
-29
lines changed

cpp/src/security/UnsafeImplicitConversions/UnsafeImplicitConversions.ql

Lines changed: 21 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -40,18 +40,14 @@ private class LenApproxFunc extends SimpleRangeAnalysisExpr, FunctionCall {
4040
* to be passed to strlen to be exploitable.
4141
*/
4242

43-
/*
44-
* private class StrlenFunAssumption extends SimpleRangeAnalysisExpr, FunctionCall {
45-
* StrlenFunAssumption() { this.getTarget().hasName("strlen") }
46-
*
47-
* override float getLowerBounds() { result = 0 }
48-
*
49-
* override float getUpperBounds() { result = 536870911 }
50-
*
51-
* override predicate dependsOnChild(Expr child) { none() }
52-
* }
53-
*/
54-
43+
// private class StrlenFunAssumption extends SimpleRangeAnalysisExpr, FunctionCall {
44+
// StrlenFunAssumption() {
45+
// this.getTarget().hasName("strlen")
46+
// }
47+
// override float getLowerBounds() { result = 0 }
48+
// override float getUpperBounds() { result = 536870912 }
49+
// override predicate dependsOnChild(Expr child) { none() }
50+
// }
5551
/**
5652
* Determines if a function's address is taken in the codebase.
5753
* This indicates that the function may be called while
@@ -184,8 +180,8 @@ predicate safeLowerBound(Expr cast, IntegralType toType) {
184180
lowerB = lowerBound(cast) and
185181
lowerB >= typeLowerBound(toType)
186182
)
187-
// comment the exists formula below to speed up the query
188183
or
184+
// comment the exists formula below to speed up the query
189185
exists(Instruction instr, Bound b, int delta |
190186
not exists(float knownValue | knownValue = cast.getValue().toFloat()) and
191187
instr.getUnconvertedResultExpression() = cast and
@@ -204,8 +200,8 @@ predicate safeUpperBound(Expr cast, IntegralType toType) {
204200
upperB = upperBound(cast) and
205201
upperB <= typeUpperBound(toType)
206202
)
207-
// comment the exists formula below to speed up the query
208203
or
204+
// comment the exists formula below to speed up the query
209205
exists(Instruction instr, Bound b, int delta |
210206
not exists(float knownValue | knownValue = cast.getValue().toFloat()) and
211207
instr.getUnconvertedResultExpression() = cast and
@@ -224,7 +220,7 @@ predicate safeBounds(Expr cast, IntegralType toType) {
224220

225221
/**
226222
* Taint tracking from user-controlled inputs to implicit conversions
227-
* UNUSED: uncomment the code below (near "select") to use
223+
* UNUSED: uncomment the code near "select" statement at the bottom to use
228224
*/
229225
module UnsafeUserInputConversionConfig implements DataFlow::ConfigSig {
230226
predicate isSource(DataFlow::Node source) {
@@ -306,13 +302,12 @@ where
306302
// skip some conversions in some equality operations
307303
not (
308304
fromType.getSize() <= toType.getSize() and
309-
fromType.isSigned() and // should always hold
310-
exists(EqualityOperation eq, Expr castHandSide, Expr otherHandSide |
311-
castHandSide = eq.getAnOperand() and
312-
otherHandSide = eq.getAnOperand() and
313-
castHandSide != otherHandSide and
314-
castHandSide.getConversion*() = cast and
315-
otherHandSide.getValue().toFloat() < typeUpperBound(toType) + typeLowerBound(fromType)
305+
exists(EqualityOperation eq, Expr firstHandSide, Expr secondHandSide |
306+
firstHandSide = eq.getAnOperand() and
307+
secondHandSide = eq.getAnOperand() and
308+
firstHandSide != secondHandSide and
309+
firstHandSide.getConversion*() = cast and
310+
upperBound(secondHandSide) < (typeUpperBound(toType) + typeLowerBound(fromType))
316311
)
317312
) and
318313
// skip unused function
@@ -326,11 +321,8 @@ where
326321
addressIsTaken(cast.getEnclosingFunction())
327322
)
328323
// Uncomment to report conversions with untrusted inputs only
329-
/*
330-
* and exists(DataFlow::Node source, DataFlow::Node sink |
331-
* cast.getExpr() = sink.asExpr() and
332-
* UnsafeUserInputConversionFlow::flow(source, sink)
333-
* )
334-
*/
335-
324+
// and exists(DataFlow::Node source, DataFlow::Node sink |
325+
// cast.getExpr() = sink.asExpr() and
326+
// UnsafeUserInputConversionFlow::flow(source, sink)
327+
// )
336328
select cast, "Implicit cast from " + fromType + " to " + toType + " (" + problemType + ")"

0 commit comments

Comments
 (0)