|
| 1 | +/** |
| 2 | + * @name Incorrect conversion between integer types |
| 3 | + * @description Converting the result of `strconv.Atoi`, `strconv.ParseInt`, |
| 4 | + * and `strconv.ParseUint` to integer types of smaller bit size |
| 5 | + * can produce unexpected values. |
| 6 | + * @kind path-problem |
| 7 | + * @problem.severity warning |
| 8 | + * @id go/incorrect-integer-conversion |
| 9 | + * @tags security |
| 10 | + * external/cwe/cwe-190 |
| 11 | + * external/cwe/cwe-681 |
| 12 | + * @precision very-high |
| 13 | + */ |
| 14 | + |
| 15 | +import go |
| 16 | +import DataFlow::PathGraph |
| 17 | + |
| 18 | +/** |
| 19 | + * Gets the maximum value of an integer (signed if `isSigned` |
| 20 | + * is true, unsigned otherwise) with `bitSize` bits. |
| 21 | + */ |
| 22 | +float getMaxIntValue(int bitSize, boolean isSigned) { |
| 23 | + bitSize in [8, 16, 32] and |
| 24 | + ( |
| 25 | + isSigned = true and result = 2.pow(bitSize - 1) - 1 |
| 26 | + or |
| 27 | + isSigned = false and result = 2.pow(bitSize) - 1 |
| 28 | + ) |
| 29 | +} |
| 30 | + |
| 31 | +/** |
| 32 | + * Get the size of `int` or `uint` in `file`, or 0 if it is |
| 33 | + * architecture-specific. |
| 34 | + */ |
| 35 | +int getIntTypeBitSize(File file) { |
| 36 | + file.constrainsIntBitSize(result) |
| 37 | + or |
| 38 | + not file.constrainsIntBitSize(_) and |
| 39 | + result = 0 |
| 40 | +} |
| 41 | + |
| 42 | +/** |
| 43 | + * Holds if converting from an integer types with size `sourceBitSize` to |
| 44 | + * one with size `sinkBitSize` can produce unexpected values, where 0 means |
| 45 | + * architecture-dependent. |
| 46 | + * |
| 47 | + * Architecture-dependent bit sizes can be 32 or 64. To catch flows that |
| 48 | + * only manifest on 64-bit architectures we consider an |
| 49 | + * architecture-dependent source bit size to be 64. To catch flows that |
| 50 | + * only happen on 32-bit architectures we consider an |
| 51 | + * architecture-dependent sink bit size to be 32. We exclude the case where |
| 52 | + * both source and sink have architecture-dependent bit sizes. |
| 53 | + */ |
| 54 | +private predicate isIncorrectIntegerConversion(int sourceBitSize, int sinkBitSize) { |
| 55 | + sourceBitSize in [16, 32, 64] and |
| 56 | + sinkBitSize in [8, 16, 32] and |
| 57 | + sourceBitSize > sinkBitSize |
| 58 | + or |
| 59 | + // Treat `sourceBitSize = 0` like `sourceBitSize = 64`, and exclude `sinkBitSize = 0` |
| 60 | + sourceBitSize = 0 and |
| 61 | + sinkBitSize in [8, 16, 32] |
| 62 | + or |
| 63 | + // Treat `sinkBitSize = 0` like `sinkBitSize = 32`, and exclude `sourceBitSize = 0` |
| 64 | + sourceBitSize = 64 and |
| 65 | + sinkBitSize = 0 |
| 66 | +} |
| 67 | + |
| 68 | +/** |
| 69 | + * A taint-tracking configuration for reasoning about when an integer |
| 70 | + * obtained from parsing a string flows to a type conversion to a smaller |
| 71 | + * integer types, which could cause unexpected values. |
| 72 | + */ |
| 73 | +class ConversionWithoutBoundsCheckConfig extends TaintTracking::Configuration { |
| 74 | + boolean sourceIsSigned; |
| 75 | + int sourceBitSize; |
| 76 | + int sinkBitSize; |
| 77 | + |
| 78 | + ConversionWithoutBoundsCheckConfig() { |
| 79 | + sourceIsSigned in [true, false] and |
| 80 | + isIncorrectIntegerConversion(sourceBitSize, sinkBitSize) and |
| 81 | + this = "ConversionWithoutBoundsCheckConfig" + sourceBitSize + sourceIsSigned + sinkBitSize |
| 82 | + } |
| 83 | + |
| 84 | + /** Gets the bit size of the source. */ |
| 85 | + int getSourceBitSize() { result = sourceBitSize } |
| 86 | + |
| 87 | + override predicate isSource(DataFlow::Node source) { |
| 88 | + exists( |
| 89 | + DataFlow::CallNode c, IntegerParser::Range ip, int apparentBitSize, int effectiveBitSize |
| 90 | + | |
| 91 | + c.getTarget() = ip and source = c.getResult(0) |
| 92 | + | |
| 93 | + ( |
| 94 | + if ip.getResultType(0) instanceof SignedIntegerType |
| 95 | + then sourceIsSigned = true |
| 96 | + else sourceIsSigned = false |
| 97 | + ) and |
| 98 | + ( |
| 99 | + apparentBitSize = ip.getTargetBitSize() |
| 100 | + or |
| 101 | + // If we are reading a variable, check if it is |
| 102 | + // `strconv.IntSize`, and use 0 if it is. |
| 103 | + exists(DataFlow::Node rawBitSize | rawBitSize = ip.getTargetBitSizeInput().getNode(c) | |
| 104 | + if rawBitSize = any(StrConv::IntSize intSize).getARead() |
| 105 | + then apparentBitSize = 0 |
| 106 | + else apparentBitSize = rawBitSize.getIntValue() |
| 107 | + ) |
| 108 | + ) and |
| 109 | + ( |
| 110 | + if apparentBitSize = 0 |
| 111 | + then effectiveBitSize = getIntTypeBitSize(source.getFile()) |
| 112 | + else effectiveBitSize = apparentBitSize |
| 113 | + ) and |
| 114 | + // `effectiveBitSize` could be any value between 0 and 64, but we |
| 115 | + // can round it up to the nearest size of an integer type without |
| 116 | + // changing behaviour. |
| 117 | + sourceBitSize = min(int b | b in [0, 8, 16, 32, 64] and b >= effectiveBitSize) |
| 118 | + ) |
| 119 | + } |
| 120 | + |
| 121 | + /** |
| 122 | + * Holds if `sink` is a typecast to an integer type with size `bitSize` (where |
| 123 | + * 0 represents architecture-dependent) and the expression being typecast is |
| 124 | + * not also in a right-shift expression. We allow this case because it is |
| 125 | + * a common pattern to serialise `byte(v)`, `byte(v >> 8)`, and so on. |
| 126 | + */ |
| 127 | + predicate isSink(DataFlow::TypeCastNode sink, int bitSize) { |
| 128 | + exists(IntegerType integerType | sink.getType().getUnderlyingType() = integerType | |
| 129 | + bitSize = integerType.getSize() |
| 130 | + or |
| 131 | + not exists(integerType.getSize()) and |
| 132 | + bitSize = getIntTypeBitSize(sink.getFile()) |
| 133 | + ) and |
| 134 | + not exists(ShrExpr shrExpr | |
| 135 | + shrExpr.getLeftOperand().getGlobalValueNumber() = |
| 136 | + sink.getOperand().asExpr().getGlobalValueNumber() or |
| 137 | + shrExpr.getLeftOperand().(AndExpr).getAnOperand().getGlobalValueNumber() = |
| 138 | + sink.getOperand().asExpr().getGlobalValueNumber() |
| 139 | + ) |
| 140 | + } |
| 141 | + |
| 142 | + override predicate isSink(DataFlow::Node sink) { isSink(sink, sinkBitSize) } |
| 143 | + |
| 144 | + override predicate isSanitizerGuard(DataFlow::BarrierGuard guard) { |
| 145 | + // To catch flows that only happen on 32-bit architectures we |
| 146 | + // consider an architecture-dependent sink bit size to be 32. |
| 147 | + exists(int bitSize | if sinkBitSize != 0 then bitSize = sinkBitSize else bitSize = 32 | |
| 148 | + guard.(UpperBoundCheckGuard).getBound() <= getMaxIntValue(bitSize, sourceIsSigned) |
| 149 | + ) |
| 150 | + } |
| 151 | + |
| 152 | + override predicate isSanitizerOut(DataFlow::Node node) { |
| 153 | + exists(int bitSize | isIncorrectIntegerConversion(sourceBitSize, bitSize) | |
| 154 | + isSink(node, bitSize) |
| 155 | + ) |
| 156 | + } |
| 157 | +} |
| 158 | + |
| 159 | +/** An upper bound check that compares a variable to a constant value. */ |
| 160 | +class UpperBoundCheckGuard extends DataFlow::BarrierGuard, DataFlow::RelationalComparisonNode { |
| 161 | + UpperBoundCheckGuard() { count(expr.getAnOperand().getIntValue()) = 1 } |
| 162 | + |
| 163 | + /** |
| 164 | + * Gets the constant value which this upper bound check ensures the |
| 165 | + * other value is less than or equal to. |
| 166 | + */ |
| 167 | + float getBound() { |
| 168 | + exists(int strictnessOffset | |
| 169 | + if expr.isStrict() then strictnessOffset = 1 else strictnessOffset = 0 |
| 170 | + | |
| 171 | + result = expr.getAnOperand().getExactValue().toFloat() - strictnessOffset |
| 172 | + ) |
| 173 | + } |
| 174 | + |
| 175 | + override predicate checks(Expr e, boolean branch) { |
| 176 | + this.leq(branch, DataFlow::exprNode(e), _, _) and |
| 177 | + not exists(e.getIntValue()) |
| 178 | + } |
| 179 | +} |
| 180 | + |
| 181 | +/** Gets a string describing the size of the integer parsed. */ |
| 182 | +string describeBitSize(int bitSize, int intTypeBitSize) { |
| 183 | + intTypeBitSize in [0, 32, 64] and |
| 184 | + if bitSize != 0 |
| 185 | + then bitSize in [8, 16, 32, 64] and result = "a " + bitSize + "-bit integer" |
| 186 | + else |
| 187 | + if intTypeBitSize = 0 |
| 188 | + then result = "an integer with architecture-dependent bit size" |
| 189 | + else |
| 190 | + result = |
| 191 | + "a number with architecture-dependent bit-width, which is constrained to be " + |
| 192 | + intTypeBitSize + "-bit by build constraints," |
| 193 | +} |
| 194 | + |
| 195 | +from |
| 196 | + DataFlow::PathNode source, DataFlow::PathNode sink, ConversionWithoutBoundsCheckConfig cfg, |
| 197 | + DataFlow::CallNode call |
| 198 | +where cfg.hasFlowPath(source, sink) and call.getResult(0) = source.getNode() |
| 199 | +select sink.getNode(), source, sink, |
| 200 | + "Incorrect conversion of " + |
| 201 | + describeBitSize(cfg.getSourceBitSize(), getIntTypeBitSize(source.getNode().getFile())) + |
| 202 | + " from $@ to a lower bit size type " + sink.getNode().getType().getUnderlyingType().getName() + |
| 203 | + " without an upper bound check.", source, call.getTarget().getQualifiedName() |
0 commit comments