|
| 1 | +/** |
| 2 | + * @name Incorrect conversion between integer types |
| 3 | + * @description Converting the result of strconv.Atoi, strconv.ParseInt and strconv.ParseUint |
| 4 | + * to integer types of smaller bit size can produce unexpected values. |
| 5 | + * @kind path-problem |
| 6 | + * @problem.severity warning |
| 7 | + * @id go/incorrect-integer-conversion |
| 8 | + * @tags security |
| 9 | + * external/cwe/cwe-190 |
| 10 | + * external/cwe/cwe-681 |
| 11 | + * @precision very-high |
| 12 | + */ |
| 13 | + |
| 14 | +import go |
| 15 | +import DataFlow::PathGraph |
| 16 | + |
| 17 | +/** |
| 18 | + * Gets the maximum value of an integer (signed if `isSigned` |
| 19 | + * is true, unsigned otherwise) with `bitSize` bits. |
| 20 | + */ |
| 21 | +float getMaxIntValue(int bitSize, boolean isSigned) { |
| 22 | + bitSize in [8, 16, 32, 64] and |
| 23 | + ( |
| 24 | + isSigned = true and result = 2.pow(bitSize - 1) - 1 |
| 25 | + or |
| 26 | + isSigned = false and result = 2.pow(bitSize) - 1 |
| 27 | + ) |
| 28 | +} |
| 29 | + |
| 30 | +/** |
| 31 | + * Holds if converting from an integer types with size `sourceBitSize` to |
| 32 | + * one with size `sinkBitSize` can produce unexpected values, where 0 means |
| 33 | + * architecture-dependent. |
| 34 | + */ |
| 35 | +private predicate isIncorrectIntegerConversion(int sourceBitSize, int sinkBitSize) { |
| 36 | + sourceBitSize in [0, 16, 32, 64] and |
| 37 | + sinkBitSize in [0, 8, 16, 32] and |
| 38 | + not (sourceBitSize = 0 and sinkBitSize = 0) and |
| 39 | + exists(int source, int sink | |
| 40 | + (if sourceBitSize = 0 then source = 64 else source = sourceBitSize) and |
| 41 | + if sinkBitSize = 0 then sink = 32 else sink = sinkBitSize |
| 42 | + | |
| 43 | + source > sink |
| 44 | + ) |
| 45 | +} |
| 46 | + |
| 47 | +/** |
| 48 | + * A taint-tracking configuration for reasoning about when an integer |
| 49 | + * obtained from parsing a string flows to a type conversion to a smaller |
| 50 | + * integer types, which could cause unexpected values. |
| 51 | + */ |
| 52 | +class ConversionWithoutBoundsCheckConfig extends TaintTracking::Configuration { |
| 53 | + boolean sourceIsSigned; |
| 54 | + int sourceBitSize; |
| 55 | + int sinkBitSize; |
| 56 | + |
| 57 | + ConversionWithoutBoundsCheckConfig() { |
| 58 | + sourceIsSigned in [true, false] and |
| 59 | + isIncorrectIntegerConversion(sourceBitSize, sinkBitSize) and |
| 60 | + this = |
| 61 | + sourceBitSize.toString() + sourceIsSigned.toString() + sinkBitSize.toString() + |
| 62 | + "ConversionWithoutBoundsCheckConfig" |
| 63 | + } |
| 64 | + |
| 65 | + override predicate isSource(DataFlow::Node source) { |
| 66 | + exists(ParserCall pc, int bitSize | source = pc.getResult(0) | |
| 67 | + sourceIsSigned = pc.getTargetIsSigned() and |
| 68 | + (if pc.getTargetBitSize() = 0 then bitSize = 0 else bitSize = pc.getTargetBitSize()) and |
| 69 | + // `bitSize` could be any value between 0 and 64, but we can round |
| 70 | + // it up to the nearest size of an integer type without changing |
| 71 | + // behaviour. |
| 72 | + sourceBitSize = min(int b | b in [0, 8, 16, 32, 64] and b >= bitSize) |
| 73 | + ) |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Holds if sink is a typecast to an integer type with size `bitSize` (where |
| 78 | + * 0 represents architecture-dependent) and the expression being typecast is |
| 79 | + * not also in a right-shift expression. |
| 80 | + */ |
| 81 | + predicate isSink(DataFlow::TypeCastNode sink, int bitSize) { |
| 82 | + exists(IntegerType integerType | sink.getType().getUnderlyingType() = integerType | |
| 83 | + bitSize = integerType.getSize() |
| 84 | + or |
| 85 | + ( |
| 86 | + integerType instanceof IntType or |
| 87 | + integerType instanceof UintType |
| 88 | + ) and |
| 89 | + bitSize = 0 |
| 90 | + ) and |
| 91 | + not exists(ShrExpr shrExpr | |
| 92 | + shrExpr.getLeftOperand().getGlobalValueNumber() = |
| 93 | + sink.getOperand().asExpr().getGlobalValueNumber() |
| 94 | + ) |
| 95 | + } |
| 96 | + |
| 97 | + override predicate isSink(DataFlow::Node sink) { isSink(sink, sinkBitSize) } |
| 98 | + |
| 99 | + override predicate isSanitizerGuard(DataFlow::BarrierGuard guard) { |
| 100 | + exists(int bitSize | if sinkBitSize != 0 then bitSize = sinkBitSize else bitSize = 32 | |
| 101 | + guard.(UpperBoundCheckGuard).getBound() <= getMaxIntValue(bitSize, sourceIsSigned) |
| 102 | + ) |
| 103 | + } |
| 104 | + |
| 105 | + override predicate isSanitizerOut(DataFlow::Node node) { |
| 106 | + exists(int bitSize | isIncorrectIntegerConversion(sourceBitSize, bitSize) | |
| 107 | + isSink(node, bitSize) |
| 108 | + ) |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +/** An upper bound check that compares a variable to a constant value. */ |
| 113 | +class UpperBoundCheckGuard extends DataFlow::BarrierGuard, DataFlow::RelationalComparisonNode { |
| 114 | + UpperBoundCheckGuard() { count(expr.getAnOperand().getIntValue()) = 1 } |
| 115 | + |
| 116 | + /** |
| 117 | + * Gets the constant value which this upper bound check ensures the |
| 118 | + * other value is less than or equal to. |
| 119 | + */ |
| 120 | + float getBound() { |
| 121 | + exists(int strictnessOffset | |
| 122 | + if expr.isStrict() then strictnessOffset = 1 else strictnessOffset = 0 |
| 123 | + | |
| 124 | + result = expr.getAnOperand().getIntValue() - strictnessOffset |
| 125 | + ) |
| 126 | + } |
| 127 | + |
| 128 | + override predicate checks(Expr e, boolean branch) { |
| 129 | + this.leq(branch, DataFlow::exprNode(e), _, _) and |
| 130 | + not exists(e.getIntValue()) |
| 131 | + } |
| 132 | +} |
| 133 | + |
| 134 | +from |
| 135 | + DataFlow::PathNode source, DataFlow::PathNode sink, ConversionWithoutBoundsCheckConfig cfg, |
| 136 | + ParserCall pc |
| 137 | +where cfg.hasFlowPath(source, sink) and pc.getResult(0) = source.getNode() |
| 138 | +select source.getNode(), source, sink, |
| 139 | + "Incorrect conversion of " + pc.getBitSizeString() + " from " + pc.getParserName() + |
| 140 | + " to a lower bit size type " + |
| 141 | + sink.getNode().(DataFlow::TypeCastNode).getType().getUnderlyingType().getName() + |
| 142 | + " without an upper bound check." |
0 commit comments