|
| 1 | +/** |
| 2 | + * @name Dangerous use of transformation after operation. |
| 3 | + * @description By using the transformation after the operation, you are doing a pointless and dangerous action. |
| 4 | + * @kind problem |
| 5 | + * @id cpp/dangerous-use-of-transformation-after-operation |
| 6 | + * @problem.severity warning |
| 7 | + * @precision medium |
| 8 | + * @tags correctness |
| 9 | + * security |
| 10 | + * external/cwe/cwe-190 |
| 11 | + */ |
| 12 | + |
| 13 | +import cpp |
| 14 | + |
| 15 | +/** Returns the number of the expression in the function call arguments. */ |
| 16 | +int argumentPosition(FunctionCall fc, Expr exp, int n) { |
| 17 | + n in [0 .. fc.getNumberOfArguments() - 1] and |
| 18 | + fc.getArgument(n) = exp and |
| 19 | + result = n |
| 20 | +} |
| 21 | + |
| 22 | +/** Holds if a nonsensical type conversion situation is found. */ |
| 23 | +predicate conversionDoneLate(MulExpr mexp) { |
| 24 | + exists(Expr e1, Expr e2 | |
| 25 | + mexp.hasOperands(e1, e2) and |
| 26 | + not e1.isConstant() and |
| 27 | + not e1.hasConversion() and |
| 28 | + not e1.hasConversion() and |
| 29 | + ( |
| 30 | + e2.isConstant() or |
| 31 | + not e2.hasConversion() |
| 32 | + ) and |
| 33 | + mexp.getConversion().hasExplicitConversion() and |
| 34 | + mexp.getConversion() instanceof ParenthesisExpr and |
| 35 | + mexp.getConversion().getConversion() instanceof CStyleCast and |
| 36 | + mexp.getConversion().getConversion().getType().getSize() > mexp.getType().getSize() and |
| 37 | + mexp.getConversion().getConversion().getType().getSize() > e2.getType().getSize() and |
| 38 | + mexp.getConversion().getConversion().getType().getSize() > e1.getType().getSize() and |
| 39 | + exists(Expr e0 | |
| 40 | + e0.(AssignExpr).getRValue() = mexp.getParent*() and |
| 41 | + e0.(AssignExpr).getLValue().getType().getSize() = |
| 42 | + mexp.getConversion().getConversion().getType().getSize() |
| 43 | + or |
| 44 | + mexp.getEnclosingElement().(ComparisonOperation).hasOperands(mexp, e0) and |
| 45 | + e0.getType().getSize() = mexp.getConversion().getConversion().getType().getSize() |
| 46 | + or |
| 47 | + e0.(FunctionCall) |
| 48 | + .getTarget() |
| 49 | + .getParameter(argumentPosition(e0.(FunctionCall), mexp, _)) |
| 50 | + .getType() |
| 51 | + .getSize() = mexp.getConversion().getConversion().getType().getSize() |
| 52 | + ) |
| 53 | + ) |
| 54 | +} |
| 55 | + |
| 56 | +/** Holds if the situation of a possible signed overflow used in pointer arithmetic is found. */ |
| 57 | +predicate signSmallerWithEqualSizes(MulExpr mexp) { |
| 58 | + exists(Expr e1, Expr e2 | |
| 59 | + mexp.hasOperands(e1, e2) and |
| 60 | + not e1.isConstant() and |
| 61 | + not e1.hasConversion() and |
| 62 | + not e1.hasConversion() and |
| 63 | + ( |
| 64 | + e2.isConstant() or |
| 65 | + not e2.hasConversion() |
| 66 | + ) and |
| 67 | + mexp.getConversion+().getUnderlyingType().getSize() = e1.getUnderlyingType().getSize() and |
| 68 | + ( |
| 69 | + e2.isConstant() or |
| 70 | + mexp.getConversion+().getUnderlyingType().getSize() = e2.getUnderlyingType().getSize() |
| 71 | + ) and |
| 72 | + mexp.getConversion+().getUnderlyingType().getSize() = e1.getUnderlyingType().getSize() and |
| 73 | + exists(AssignExpr ae | |
| 74 | + ae.getRValue() = mexp.getParent*() and |
| 75 | + ae.getRValue().getUnderlyingType().(IntegralType).isUnsigned() and |
| 76 | + ae.getLValue().getUnderlyingType().(IntegralType).isSigned() and |
| 77 | + ( |
| 78 | + not exists(DivExpr de | mexp.getParent*() = de) |
| 79 | + or |
| 80 | + exists(DivExpr de, Expr ec | |
| 81 | + e2.isConstant() and |
| 82 | + de.hasOperands(mexp.getParent*(), ec) and |
| 83 | + ec.isConstant() and |
| 84 | + e2.getValue().toInt() > ec.getValue().toInt() |
| 85 | + ) |
| 86 | + ) and |
| 87 | + exists(PointerAddExpr pa | |
| 88 | + ae.getASuccessor+() = pa and |
| 89 | + pa.getAnOperand().(VariableAccess).getTarget() = ae.getLValue().(VariableAccess).getTarget() |
| 90 | + ) |
| 91 | + ) |
| 92 | + ) |
| 93 | +} |
| 94 | + |
| 95 | +from MulExpr mexp, string msg |
| 96 | +where |
| 97 | + conversionDoneLate(mexp) and |
| 98 | + msg = "This transformation is applied after multiplication." |
| 99 | + or |
| 100 | + signSmallerWithEqualSizes(mexp) and |
| 101 | + msg = "Possible signed overflow followed by offset of the pointer out of bounds." |
| 102 | +select mexp, msg |
0 commit comments