|
| 1 | +/** |
| 2 | + * @name Type confusion |
| 3 | + * @description Casting a value to an incompatible type can lead to undefined behavior. |
| 4 | + * @kind path-problem |
| 5 | + * @problem.severity warning |
| 6 | + * @security-severity 9.3 |
| 7 | + * @precision medium |
| 8 | + * @id cpp/type-confusion |
| 9 | + * @tags security |
| 10 | + * external/cwe/cwe-843 |
| 11 | + */ |
| 12 | + |
| 13 | +import cpp |
| 14 | +import semmle.code.cpp.dataflow.new.DataFlow |
| 15 | +import BadFlow::PathGraph |
| 16 | + |
| 17 | +/** |
| 18 | + * Holds if `f` is a field located at byte offset `offset` in `c`. |
| 19 | + * |
| 20 | + * Note that predicate is recursive, so that given the following: |
| 21 | + * ```cpp |
| 22 | + * struct S1 { |
| 23 | + * int a; |
| 24 | + * void* b; |
| 25 | + * }; |
| 26 | + * |
| 27 | + * struct S2 { |
| 28 | + * S1 s1; |
| 29 | + * char c; |
| 30 | + * }; |
| 31 | + * ``` |
| 32 | + * both `hasAFieldWithOffset(S2, s1, 0)` and `hasAFieldWithOffset(S2, a, 0)` |
| 33 | + * holds. |
| 34 | + */ |
| 35 | +predicate hasAFieldWithOffset(Class c, Field f, int offset) { |
| 36 | + // Base case: `f` is a field in `c`. |
| 37 | + f = c.getAField() and |
| 38 | + offset = f.getByteOffset() and |
| 39 | + not f.getUnspecifiedType().(Class).hasDefinition() |
| 40 | + or |
| 41 | + // Otherwise, we find the struct that is a field of `c` which then has |
| 42 | + // the field `f` as a member. |
| 43 | + exists(Field g | |
| 44 | + g = c.getAField() and |
| 45 | + // Find the field with the largest offset that's less than or equal to |
| 46 | + // offset. That's the struct we need to search recursively. |
| 47 | + g = |
| 48 | + max(Field cand, int candOffset | |
| 49 | + cand = c.getAField() and |
| 50 | + candOffset = cand.getByteOffset() and |
| 51 | + offset >= candOffset |
| 52 | + | |
| 53 | + cand order by candOffset |
| 54 | + ) and |
| 55 | + hasAFieldWithOffset(g.getUnspecifiedType(), f, offset - g.getByteOffset()) |
| 56 | + ) |
| 57 | +} |
| 58 | + |
| 59 | +/** Holds if `f` is the last field of its declaring class. */ |
| 60 | +predicate lastField(Field f) { |
| 61 | + exists(Class c | c = f.getDeclaringType() | |
| 62 | + f = |
| 63 | + max(Field cand, int byteOffset | |
| 64 | + cand.getDeclaringType() = c and byteOffset = f.getByteOffset() |
| 65 | + | |
| 66 | + cand order by byteOffset |
| 67 | + ) |
| 68 | + ) |
| 69 | +} |
| 70 | + |
| 71 | +/** |
| 72 | + * Holds if there exists a field in `c2` at offset `offset` that's compatible |
| 73 | + * with `f1`. |
| 74 | + */ |
| 75 | +bindingset[f1, offset, c2] |
| 76 | +pragma[inline_late] |
| 77 | +predicate hasCompatibleFieldAtOffset(Field f1, int offset, Class c2) { |
| 78 | + exists(Field f2 | hasAFieldWithOffset(c2, f2, offset) | |
| 79 | + // Let's not deal with bit-fields for now. |
| 80 | + f2 instanceof BitField |
| 81 | + or |
| 82 | + f1.getUnspecifiedType().getSize() = f2.getUnspecifiedType().getSize() |
| 83 | + or |
| 84 | + lastField(f1) and |
| 85 | + f1.getUnspecifiedType().getSize() <= f2.getUnspecifiedType().getSize() |
| 86 | + ) |
| 87 | +} |
| 88 | + |
| 89 | +/** |
| 90 | + * Holds if `c1` is a prefix of `c2`. |
| 91 | + */ |
| 92 | +bindingset[c1, c2] |
| 93 | +pragma[inline_late] |
| 94 | +predicate prefix(Class c1, Class c2) { |
| 95 | + not c1.isPolymorphic() and |
| 96 | + not c2.isPolymorphic() and |
| 97 | + if c1 instanceof Union |
| 98 | + then |
| 99 | + // If it's a union we just verify that one of it's variants is compatible with the other class |
| 100 | + exists(Field f1, int offset | |
| 101 | + // Let's not deal with bit-fields for now. |
| 102 | + not f1 instanceof BitField and |
| 103 | + hasAFieldWithOffset(c1, f1, offset) |
| 104 | + | |
| 105 | + hasCompatibleFieldAtOffset(f1, offset, c2) |
| 106 | + ) |
| 107 | + else |
| 108 | + forall(Field f1, int offset | |
| 109 | + // Let's not deal with bit-fields for now. |
| 110 | + not f1 instanceof BitField and |
| 111 | + hasAFieldWithOffset(c1, f1, offset) |
| 112 | + | |
| 113 | + hasCompatibleFieldAtOffset(f1, offset, c2) |
| 114 | + ) |
| 115 | +} |
| 116 | + |
| 117 | +/** |
| 118 | + * An unsafe cast is any explicit cast that is not |
| 119 | + * a `dynamic_cast`. |
| 120 | + */ |
| 121 | +class UnsafeCast extends Cast { |
| 122 | + private Class toType; |
| 123 | + |
| 124 | + UnsafeCast() { |
| 125 | + ( |
| 126 | + this instanceof CStyleCast |
| 127 | + or |
| 128 | + this instanceof StaticCast |
| 129 | + or |
| 130 | + this instanceof ReinterpretCast |
| 131 | + ) and |
| 132 | + toType = this.getExplicitlyConverted().getUnspecifiedType().stripType() and |
| 133 | + not this.isImplicit() and |
| 134 | + exists(TypeDeclarationEntry tde | |
| 135 | + tde = toType.getDefinition() and |
| 136 | + not tde.isFromUninstantiatedTemplate(_) |
| 137 | + ) |
| 138 | + } |
| 139 | + |
| 140 | + Class getConvertedType() { result = toType } |
| 141 | + |
| 142 | + bindingset[this, t] |
| 143 | + pragma[inline_late] |
| 144 | + predicate compatibleWith(Type t) { |
| 145 | + t.stripType() = this.getConvertedType() |
| 146 | + or |
| 147 | + prefix(this.getConvertedType(), t.stripType()) |
| 148 | + or |
| 149 | + t.stripType().(Class).getABaseClass+() = this.getConvertedType() |
| 150 | + or |
| 151 | + t.stripType() = this.getConvertedType().getABaseClass+() |
| 152 | + } |
| 153 | +} |
| 154 | + |
| 155 | +/** |
| 156 | + * Holds if `source` is an allocation that allocates a value of type `state`. |
| 157 | + */ |
| 158 | +predicate isSourceImpl(DataFlow::Node source, Class state) { |
| 159 | + state = source.asExpr().(AllocationExpr).getAllocatedElementType().stripType() and |
| 160 | + exists(TypeDeclarationEntry tde | |
| 161 | + tde = state.getDefinition() and |
| 162 | + not tde.isFromUninstantiatedTemplate(_) |
| 163 | + ) |
| 164 | +} |
| 165 | + |
| 166 | +module RelevantStateConfig implements DataFlow::ConfigSig { |
| 167 | + predicate isSource(DataFlow::Node source) { isSourceImpl(source, _) } |
| 168 | + |
| 169 | + predicate isBarrier(DataFlow::Node node) { |
| 170 | + // We disable flow through global variables to reduce FPs from infeasible paths |
| 171 | + node instanceof DataFlow::VariableNode |
| 172 | + or |
| 173 | + exists(Class c | c = node.getType().stripType() | |
| 174 | + not c.hasDefinition() |
| 175 | + or |
| 176 | + exists(TypeDeclarationEntry tde | |
| 177 | + tde = c.getDefinition() and |
| 178 | + tde.isFromUninstantiatedTemplate(_) |
| 179 | + ) |
| 180 | + ) |
| 181 | + } |
| 182 | + |
| 183 | + predicate isSink(DataFlow::Node sink) { |
| 184 | + exists(UnsafeCast cast | sink.asExpr() = cast.getUnconverted()) |
| 185 | + } |
| 186 | +} |
| 187 | + |
| 188 | +module RelevantStateFlow = DataFlow::Global<RelevantStateConfig>; |
| 189 | + |
| 190 | +predicate relevantState(DataFlow::Node sink, Class state) { |
| 191 | + exists(DataFlow::Node source | |
| 192 | + RelevantStateFlow::flow(source, sink) and |
| 193 | + isSourceImpl(source, state) |
| 194 | + ) |
| 195 | +} |
| 196 | + |
| 197 | +predicate isSinkImpl(DataFlow::Node sink, Class state, Type convertedType, boolean compatible) { |
| 198 | + exists(UnsafeCast cast | |
| 199 | + relevantState(sink, state) and |
| 200 | + sink.asExpr() = cast.getUnconverted() and |
| 201 | + convertedType = cast.getConvertedType() |
| 202 | + | |
| 203 | + if cast.compatibleWith(state) then compatible = true else compatible = false |
| 204 | + ) |
| 205 | +} |
| 206 | + |
| 207 | +module BadConfig implements DataFlow::StateConfigSig { |
| 208 | + class FlowState extends Class { |
| 209 | + FlowState() { isSourceImpl(_, this) } |
| 210 | + } |
| 211 | + |
| 212 | + predicate isSource(DataFlow::Node source, FlowState state) { isSourceImpl(source, state) } |
| 213 | + |
| 214 | + predicate isBarrier(DataFlow::Node node) { RelevantStateConfig::isBarrier(node) } |
| 215 | + |
| 216 | + predicate isSink(DataFlow::Node sink, FlowState state) { isSinkImpl(sink, state, _, false) } |
| 217 | + |
| 218 | + predicate isBarrierOut(DataFlow::Node sink, FlowState state) { isSink(sink, state) } |
| 219 | +} |
| 220 | + |
| 221 | +module BadFlow = DataFlow::GlobalWithState<BadConfig>; |
| 222 | + |
| 223 | +module GoodConfig implements DataFlow::StateConfigSig { |
| 224 | + class FlowState = BadConfig::FlowState; |
| 225 | + |
| 226 | + predicate isSource(DataFlow::Node source, FlowState state) { BadConfig::isSource(source, state) } |
| 227 | + |
| 228 | + predicate isBarrier(DataFlow::Node node) { BadConfig::isBarrier(node) } |
| 229 | + |
| 230 | + predicate isSink(DataFlow::Node sink, FlowState state) { |
| 231 | + isSinkImpl(sink, state, _, true) and |
| 232 | + BadFlow::flowTo(sink) |
| 233 | + } |
| 234 | +} |
| 235 | + |
| 236 | +module GoodFlow = DataFlow::GlobalWithState<GoodConfig>; |
| 237 | + |
| 238 | +from |
| 239 | + BadFlow::PathNode source, BadFlow::PathNode sink, Type sourceType, Type sinkType, |
| 240 | + DataFlow::Node sinkNode |
| 241 | +where |
| 242 | + BadFlow::flowPath(source, sink) and |
| 243 | + sinkNode = sink.getNode() and |
| 244 | + // If there is any flow that would result in a valid cast then we don't |
| 245 | + // report an alert here. This reduces the number of FPs from infeasible paths |
| 246 | + // significantly. |
| 247 | + not GoodFlow::flowTo(sinkNode) and |
| 248 | + isSourceImpl(source.getNode(), sourceType) and |
| 249 | + isSinkImpl(sinkNode, _, sinkType, false) |
| 250 | +select sinkNode, source, sink, "Conversion from $@ to $@ is invalid.", sourceType, |
| 251 | + sourceType.toString(), sinkType, sinkType.toString() |
0 commit comments