|
| 1 | +/** |
| 2 | + * @name XML external entity expansion |
| 3 | + * @description Parsing user-controlled XML documents and allowing expansion of |
| 4 | + * external entity references may lead to disclosure of |
| 5 | + * confidential data or denial of service. |
| 6 | + * @kind path-problem |
| 7 | + * @id cpp/external-entity-expansion |
| 8 | + * @problem.severity warning |
| 9 | + * @security-severity 9.1 |
| 10 | + * @precision medium |
| 11 | + * @tags security |
| 12 | + * external/cwe/cwe-611 |
| 13 | + */ |
| 14 | + |
| 15 | +import cpp |
| 16 | +import semmle.code.cpp.ir.dataflow.DataFlow |
| 17 | +import DataFlow::PathGraph |
| 18 | +import semmle.code.cpp.ir.IR |
| 19 | + |
| 20 | +/** |
| 21 | + * A flow state representing a possible configuration of an XML object. |
| 22 | + */ |
| 23 | +abstract class XXEFlowState extends DataFlow::FlowState { |
| 24 | + bindingset[this] |
| 25 | + XXEFlowState() { any() } // required characteristic predicate |
| 26 | +} |
| 27 | + |
| 28 | +/** |
| 29 | + * An `Expr` that changes the configuration of an XML object, transforming the |
| 30 | + * `XXEFlowState` that flows through it. |
| 31 | + */ |
| 32 | +abstract class XXEFlowStateTranformer extends Expr { |
| 33 | + /** |
| 34 | + * Gets the flow state that `flowstate` is transformed into. |
| 35 | + * |
| 36 | + * Due to limitations of the implementation the transformation defined by this |
| 37 | + * predicate must be idempotent, that is, for any input `x` it must be that: |
| 38 | + * ``` |
| 39 | + * transform(tranform(x)) = tranform(x) |
| 40 | + * ``` |
| 41 | + */ |
| 42 | + abstract XXEFlowState transform(XXEFlowState flowstate); |
| 43 | +} |
| 44 | + |
| 45 | +/** |
| 46 | + * The `AbstractDOMParser` class. |
| 47 | + */ |
| 48 | +class AbstractDOMParserClass extends Class { |
| 49 | + AbstractDOMParserClass() { this.hasName("AbstractDOMParser") } |
| 50 | +} |
| 51 | + |
| 52 | +/** |
| 53 | + * The `XercesDOMParser` class. |
| 54 | + */ |
| 55 | +class XercesDOMParserClass extends Class { |
| 56 | + XercesDOMParserClass() { this.hasName("XercesDOMParser") } |
| 57 | +} |
| 58 | + |
| 59 | +/** |
| 60 | + * Gets a valid flow state for `XercesDOMParser` flow. |
| 61 | + * |
| 62 | + * These flow states take the form `XercesDOM-A-B`, where: |
| 63 | + * - A is 1 if `setDisableDefaultEntityResolution` is `true`, 0 otherwise. |
| 64 | + * - B is 1 if `setCreateEntityReferenceNodes` is `true`, 0 otherwise. |
| 65 | + */ |
| 66 | +predicate encodeXercesDOMFlowState( |
| 67 | + string flowstate, int disabledDefaultEntityResolution, int createEntityReferenceNodes |
| 68 | +) { |
| 69 | + flowstate = "XercesDOM-0-0" and |
| 70 | + disabledDefaultEntityResolution = 0 and |
| 71 | + createEntityReferenceNodes = 0 |
| 72 | + or |
| 73 | + flowstate = "XercesDOM-0-1" and |
| 74 | + disabledDefaultEntityResolution = 0 and |
| 75 | + createEntityReferenceNodes = 1 |
| 76 | + or |
| 77 | + flowstate = "XercesDOM-1-0" and |
| 78 | + disabledDefaultEntityResolution = 1 and |
| 79 | + createEntityReferenceNodes = 0 |
| 80 | + or |
| 81 | + flowstate = "XercesDOM-1-1" and |
| 82 | + disabledDefaultEntityResolution = 1 and |
| 83 | + createEntityReferenceNodes = 1 |
| 84 | +} |
| 85 | + |
| 86 | +/** |
| 87 | + * A flow state representing the configuration of a `XercesDOMParser` object. |
| 88 | + */ |
| 89 | +class XercesDOMParserFlowState extends XXEFlowState { |
| 90 | + XercesDOMParserFlowState() { encodeXercesDOMFlowState(this, _, _) } |
| 91 | +} |
| 92 | + |
| 93 | +/** |
| 94 | + * Flow state transformer for a call to |
| 95 | + * `AbstractDOMParser.setDisableDefaultEntityResolution`. Transforms the flow |
| 96 | + * state through the qualifier according to the setting in the parameter. |
| 97 | + */ |
| 98 | +class DisableDefaultEntityResolutionTranformer extends XXEFlowStateTranformer { |
| 99 | + Expr newValue; |
| 100 | + |
| 101 | + DisableDefaultEntityResolutionTranformer() { |
| 102 | + exists(Call call, Function f | |
| 103 | + call.getTarget() = f and |
| 104 | + f.getDeclaringType() instanceof AbstractDOMParserClass and |
| 105 | + f.hasName("setDisableDefaultEntityResolution") and |
| 106 | + this = call.getQualifier() and |
| 107 | + newValue = call.getArgument(0) |
| 108 | + ) |
| 109 | + } |
| 110 | + |
| 111 | + final override XXEFlowState transform(XXEFlowState flowstate) { |
| 112 | + exists(int createEntityReferenceNodes | |
| 113 | + encodeXercesDOMFlowState(flowstate, _, createEntityReferenceNodes) and |
| 114 | + ( |
| 115 | + newValue.getValue().toInt() = 1 and // true |
| 116 | + encodeXercesDOMFlowState(result, 1, createEntityReferenceNodes) |
| 117 | + or |
| 118 | + not newValue.getValue().toInt() = 1 and // false or unknown |
| 119 | + encodeXercesDOMFlowState(result, 0, createEntityReferenceNodes) |
| 120 | + ) |
| 121 | + ) |
| 122 | + } |
| 123 | +} |
| 124 | + |
| 125 | +/** |
| 126 | + * Flow state transformer for a call to |
| 127 | + * `AbstractDOMParser.setCreateEntityReferenceNodes`. Transforms the flow |
| 128 | + * state through the qualifier according to the setting in the parameter. |
| 129 | + */ |
| 130 | +class CreateEntityReferenceNodesTranformer extends XXEFlowStateTranformer { |
| 131 | + Expr newValue; |
| 132 | + |
| 133 | + CreateEntityReferenceNodesTranformer() { |
| 134 | + exists(Call call, Function f | |
| 135 | + call.getTarget() = f and |
| 136 | + f.getDeclaringType() instanceof AbstractDOMParserClass and |
| 137 | + f.hasName("setCreateEntityReferenceNodes") and |
| 138 | + this = call.getQualifier() and |
| 139 | + newValue = call.getArgument(0) |
| 140 | + ) |
| 141 | + } |
| 142 | + |
| 143 | + final override XXEFlowState transform(XXEFlowState flowstate) { |
| 144 | + exists(int disabledDefaultEntityResolution | |
| 145 | + encodeXercesDOMFlowState(flowstate, disabledDefaultEntityResolution, _) and |
| 146 | + ( |
| 147 | + newValue.getValue().toInt() = 1 and // true |
| 148 | + encodeXercesDOMFlowState(result, disabledDefaultEntityResolution, 1) |
| 149 | + or |
| 150 | + not newValue.getValue().toInt() = 1 and // false or unknown |
| 151 | + encodeXercesDOMFlowState(result, disabledDefaultEntityResolution, 0) |
| 152 | + ) |
| 153 | + ) |
| 154 | + } |
| 155 | +} |
| 156 | + |
| 157 | +/** |
| 158 | + * The `AbstractDOMParser.parse` method. |
| 159 | + */ |
| 160 | +class ParseFunction extends Function { |
| 161 | + ParseFunction() { this.getClassAndName("parse") instanceof AbstractDOMParserClass } |
| 162 | +} |
| 163 | + |
| 164 | +/** |
| 165 | + * Configuration for tracking XML objects and their states. |
| 166 | + */ |
| 167 | +class XXEConfiguration extends DataFlow::Configuration { |
| 168 | + XXEConfiguration() { this = "XXEConfiguration" } |
| 169 | + |
| 170 | + override predicate isSource(DataFlow::Node node, string flowstate) { |
| 171 | + // source is the write on `this` of a call to the `XercesDOMParser` |
| 172 | + // constructor. |
| 173 | + exists(CallInstruction call | |
| 174 | + call.getStaticCallTarget() = any(XercesDOMParserClass c).getAConstructor() and |
| 175 | + node.asInstruction().(WriteSideEffectInstruction).getDestinationAddress() = |
| 176 | + call.getThisArgument() and |
| 177 | + encodeXercesDOMFlowState(flowstate, 0, 1) // default configuration |
| 178 | + ) |
| 179 | + } |
| 180 | + |
| 181 | + override predicate isSink(DataFlow::Node node, string flowstate) { |
| 182 | + // sink is the read of the qualifier of a call to `parse`. |
| 183 | + exists(Call call | |
| 184 | + call.getTarget() instanceof ParseFunction and |
| 185 | + call.getQualifier() = node.asConvertedExpr() |
| 186 | + ) and |
| 187 | + flowstate instanceof XercesDOMParserFlowState and |
| 188 | + not encodeXercesDOMFlowState(flowstate, 1, 1) // safe configuration |
| 189 | + } |
| 190 | + |
| 191 | + override predicate isAdditionalFlowStep( |
| 192 | + DataFlow::Node node1, string state1, DataFlow::Node node2, string state2 |
| 193 | + ) { |
| 194 | + // create additional flow steps for `XXEFlowStateTranformer`s |
| 195 | + state2 = node2.asConvertedExpr().(XXEFlowStateTranformer).transform(state1) and |
| 196 | + DataFlow::simpleLocalFlowStep(node1, node2) |
| 197 | + } |
| 198 | + |
| 199 | + override predicate isBarrier(DataFlow::Node node, string flowstate) { |
| 200 | + // when the flowstate is transformed at a call node, block the original |
| 201 | + // flowstate value. |
| 202 | + node.asConvertedExpr().(XXEFlowStateTranformer).transform(flowstate) != flowstate |
| 203 | + } |
| 204 | +} |
| 205 | + |
| 206 | +from XXEConfiguration conf, DataFlow::PathNode source, DataFlow::PathNode sink |
| 207 | +where conf.hasFlowPath(source, sink) |
| 208 | +select sink, source, sink, |
| 209 | + "This $@ is not configured to prevent an XML external entity (XXE) attack.", source, "XML parser" |
0 commit comments