|
| 1 | +/** |
| 2 | + * @name Wrong usage of package unsafe |
| 3 | + * @description Casting between types with different memory sizes can produce reads to |
| 4 | + * memory locations that are after the target buffer, and/or unexpected values. |
| 5 | + * @kind path-problem |
| 6 | + * @problem.severity error |
| 7 | + * @id go/wrong-usage-of-unsafe |
| 8 | + * @tags security |
| 9 | + * external/cwe/cwe-119 |
| 10 | + * external/cwe/cwe-126 |
| 11 | + */ |
| 12 | + |
| 13 | +import go |
| 14 | +import DataFlow::PathGraph |
| 15 | + |
| 16 | +/* |
| 17 | + * Returns the type after all aliases, named types, and pointer |
| 18 | + * types have been replaced with the actual underlying type. |
| 19 | + */ |
| 20 | + |
| 21 | +Type getFinalType(Type typ) { result = getBaseType*(typ.getUnderlyingType()).getUnderlyingType() } |
| 22 | + |
| 23 | +/* |
| 24 | + * Returns the base type of a PointerType; |
| 25 | + * if the provided type is not a pointer, |
| 26 | + * then the original type is returned. |
| 27 | + */ |
| 28 | + |
| 29 | +Type getBaseType(Type typ) { |
| 30 | + result = getBaseType*(typ.(PointerType).getBaseType*()) |
| 31 | + or |
| 32 | + result = typ |
| 33 | +} |
| 34 | + |
| 35 | +/* A conversion to a `unsafe.Pointer` */ |
| 36 | +class ConversionToUnsafePointer extends DataFlow::TypeCastNode { |
| 37 | + ConversionToUnsafePointer() { getFinalType(getType()) instanceof UnsafePointerType } |
| 38 | +} |
| 39 | + |
| 40 | +/* Type casting from a `unsafe.Pointer`.*/ |
| 41 | +class UnsafeTypeCastingConf extends TaintTracking::Configuration { |
| 42 | + UnsafeTypeCastingConf() { this = "UnsafeTypeCastingConf" } |
| 43 | + |
| 44 | + predicate isSource(DataFlow::Node source, ConversionToUnsafePointer conv) { source = conv } |
| 45 | + |
| 46 | + predicate isSink(DataFlow::Node sink, DataFlow::TypeCastNode ca) { |
| 47 | + ca.getOperand().getType() instanceof UnsafePointerType and |
| 48 | + sink = ca |
| 49 | + } |
| 50 | + |
| 51 | + override predicate isSource(DataFlow::Node source) { isSource(source, _) } |
| 52 | + |
| 53 | + override predicate isSink(DataFlow::Node sink) { isSink(sink, _) } |
| 54 | +} |
| 55 | + |
| 56 | +/* |
| 57 | + * Type casting from a shorter array to a longer array |
| 58 | + * through the use of unsafe pointers. |
| 59 | + */ |
| 60 | + |
| 61 | +predicate castShortArrayToLongerArray( |
| 62 | + DataFlow::PathNode source, DataFlow::PathNode sink, string message |
| 63 | +) { |
| 64 | + exists( |
| 65 | + UnsafeTypeCastingConf cfg, DataFlow::TypeCastNode castBig, ConversionToUnsafePointer castLittle, |
| 66 | + ArrayType arrTo, ArrayType arrFrom, int arrFromSize |
| 67 | + | |
| 68 | + cfg.hasFlowPath(source, sink) and |
| 69 | + cfg.isSource(source.getNode(), castLittle) and |
| 70 | + cfg.isSink(sink.getNode(), castBig) and |
| 71 | + arrTo = getFinalType(castBig.getType()) and |
| 72 | + ( |
| 73 | + // Array (whole) to array: |
| 74 | + // The `unsafe.Pointer` expression is on the array |
| 75 | + // (e.g. unsafe.Pointer(&someArray)), |
| 76 | + // meaning that the pointer points to the start of the array: |
| 77 | + arrFrom = getFinalType(castLittle.getOperand().getType()) and |
| 78 | + arrFromSize = arrFrom.getLength() and |
| 79 | + message = "Dangerous array type casting to " + arrTo.pp() + " from " + arrFrom.pp() |
| 80 | + or |
| 81 | + // Piece of array (starting from an index), to array: |
| 82 | + // The `unsafe.Pointer` expression can also point to a specific |
| 83 | + // element of an array |
| 84 | + // (e.g. unsafe.Pointer(&someArray[2])), |
| 85 | + // which will be the starting point in memory for the newly cast |
| 86 | + // variable. |
| 87 | + exists(DataFlow::ElementReadNode indexExpr | |
| 88 | + indexExpr = castLittle.getOperand().(DataFlow::AddressOperationNode).getOperand() and |
| 89 | + // The `arrFrom` is the base of the index expression: |
| 90 | + arrFrom = indexExpr.getBase().getType() and |
| 91 | + // Calculate the size of the `arrFrom`: |
| 92 | + arrFromSize = arrFrom.getLength() - indexExpr.getIndex().getIntValue() and |
| 93 | + message = |
| 94 | + "Dangerous array type casting to " + arrTo.pp() + " from an index expression (" + |
| 95 | + arrFrom.pp() + ")[" + indexExpr.getIndex().getIntValue() + "]" + |
| 96 | + " (the destination type is " + (arrTo.getLength() - arrFromSize) + " elements longer)" |
| 97 | + ) |
| 98 | + ) and |
| 99 | + arrTo.getLength() > arrFromSize |
| 100 | + ) |
| 101 | +} |
| 102 | + |
| 103 | +/* |
| 104 | + * Type casting from a type to an array |
| 105 | + * through the use of unsafe pointers. |
| 106 | + */ |
| 107 | + |
| 108 | +predicate castTypeToArray(DataFlow::PathNode source, DataFlow::PathNode sink, string message) { |
| 109 | + exists( |
| 110 | + UnsafeTypeCastingConf cfg, DataFlow::TypeCastNode castBig, ConversionToUnsafePointer castLittle, |
| 111 | + ArrayType arrTo, Type typeFrom |
| 112 | + | |
| 113 | + cfg.hasFlowPath(source, sink) and |
| 114 | + cfg.isSource(source.getNode(), castLittle) and |
| 115 | + cfg.isSink(sink.getNode(), castBig) and |
| 116 | + arrTo = getFinalType(castBig.getType()) and |
| 117 | + not typeFrom.getUnderlyingType() instanceof ArrayType and |
| 118 | + not typeFrom instanceof PointerType and |
| 119 | + not castLittle |
| 120 | + .getOperand() |
| 121 | + .(DataFlow::AddressOperationNode) |
| 122 | + .getOperand() |
| 123 | + .(DataFlow::ElementReadNode) |
| 124 | + .getBase() |
| 125 | + .getType() instanceof ArrayType and |
| 126 | + typeFrom = getFinalType(castLittle.getOperand().getType()) and |
| 127 | + message = "Dangerous type up-casting to " + arrTo.pp() + " from " + typeFrom |
| 128 | + ) |
| 129 | +} |
| 130 | + |
| 131 | +/* |
| 132 | + * Type casting between numeric types that have different bit sizes |
| 133 | + * through the use of unsafe pointers. |
| 134 | + */ |
| 135 | + |
| 136 | +predicate castDifferentBitSizeNumbers( |
| 137 | + DataFlow::PathNode source, DataFlow::PathNode sink, string message |
| 138 | +) { |
| 139 | + exists( |
| 140 | + UnsafeTypeCastingConf cfg, DataFlow::TypeCastNode castBig, ConversionToUnsafePointer castLittle, |
| 141 | + NumericType numTo, NumericType numFrom |
| 142 | + | |
| 143 | + cfg.hasFlowPath(source, sink) and |
| 144 | + cfg.isSource(source.getNode(), castLittle) and |
| 145 | + cfg.isSink(sink.getNode(), castBig) and |
| 146 | + numTo = getFinalType(castBig.getType()) and |
| 147 | + numFrom = getFinalType(castLittle.getOperand().getType()) and |
| 148 | + // TODO: also consider cast from uint to int? |
| 149 | + getNumericTypeSize(numTo) != getNumericTypeSize(numFrom) and |
| 150 | + // Exclude casts to UintptrType (which is still a pointer): |
| 151 | + not numTo instanceof UintptrType and |
| 152 | + message = "Dangerous numeric type casting to " + numTo.getName() + " from " + numFrom.getName() |
| 153 | + ) |
| 154 | +} |
| 155 | + |
| 156 | +/* |
| 157 | + * Returns 0 if the NumericType is one of the numeric |
| 158 | + * types that have architecture-specific bit sizes. |
| 159 | + */ |
| 160 | + |
| 161 | +int getNumericTypeSize(NumericType typ) { |
| 162 | + // If the numeric types have arch-specific |
| 163 | + // bit sizes, then set the size to 0 to distinguish |
| 164 | + // it from others. |
| 165 | + not exists(typ.getSize()) and |
| 166 | + result = 0 |
| 167 | + or |
| 168 | + result = typ.getSize() |
| 169 | +} |
| 170 | + |
| 171 | +from DataFlow::PathNode source, DataFlow::PathNode sink, string message |
| 172 | +where |
| 173 | + castShortArrayToLongerArray(source, sink, message) or |
| 174 | + castTypeToArray(source, sink, message) or |
| 175 | + castDifferentBitSizeNumbers(source, sink, message) |
| 176 | +select sink.getNode(), source, sink, "$@.", source.getNode(), message |
0 commit comments