|
| 1 | +/** |
| 2 | + * Provides taint-tracking configurations for detecting "path injection" vulnerabilities. |
| 3 | + * |
| 4 | + * Note, for performance reasons: only import this file if |
| 5 | + * `PathInjection::Configuration` is needed, otherwise |
| 6 | + * `PathInjectionCustomizations` should be imported instead. |
| 7 | + */ |
| 8 | + |
| 9 | +private import python |
| 10 | +private import semmle.python.Concepts |
| 11 | +import semmle.python.dataflow.new.DataFlow |
| 12 | +import semmle.python.dataflow.new.TaintTracking |
| 13 | + |
| 14 | +/** |
| 15 | + * Provides a taint-tracking configuration for detecting "path injection" vulnerabilities. |
| 16 | + */ |
| 17 | +module PathInjection { |
| 18 | + import PathInjectionQuery // ignore-query-import |
| 19 | +} |
| 20 | + |
| 21 | + |
| 22 | +// --------------------------------------------------------------------------- |
| 23 | +// Old, deprecated code |
| 24 | +// --------------------------------------------------------------------------- |
| 25 | +private import semmle.python.dataflow.new.DataFlow2 |
| 26 | +private import semmle.python.dataflow.new.TaintTracking2 |
| 27 | +private import ChainedConfigs12 |
| 28 | +import PathInjectionCustomizations::PathInjection |
| 29 | + |
| 30 | +// --------------------------------------------------------------------------- |
| 31 | +// Case 1. The path is never normalized. |
| 32 | +// --------------------------------------------------------------------------- |
| 33 | +/** |
| 34 | + * DEPRECATED: Use `PathInjection::Configuration` instead |
| 35 | + * |
| 36 | + * Configuration to find paths from sources to sinks that contain no normalization. |
| 37 | + */ |
| 38 | +deprecated class PathNotNormalizedConfiguration extends TaintTracking::Configuration { |
| 39 | + PathNotNormalizedConfiguration() { this = "PathNotNormalizedConfiguration" } |
| 40 | + |
| 41 | + override predicate isSource(DataFlow::Node source) { source instanceof Source } |
| 42 | + |
| 43 | + override predicate isSink(DataFlow::Node sink) { sink instanceof Sink } |
| 44 | + |
| 45 | + override predicate isSanitizer(DataFlow::Node node) { |
| 46 | + node instanceof Sanitizer |
| 47 | + or |
| 48 | + node instanceof Path::PathNormalization |
| 49 | + } |
| 50 | + |
| 51 | + override predicate isSanitizerGuard(DataFlow::BarrierGuard guard) { |
| 52 | + guard instanceof SanitizerGuard |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +/** |
| 57 | + * DEPRECATED: Use `PathInjection::Configuration` instead |
| 58 | + * |
| 59 | + * Holds if there is a path injection from source to sink, where the (python) path is |
| 60 | + * not normalized. |
| 61 | + */ |
| 62 | +deprecated predicate pathNotNormalized(CustomPathNode source, CustomPathNode sink) { |
| 63 | + any(PathNotNormalizedConfiguration config).hasFlowPath(source.asNode1(), sink.asNode1()) |
| 64 | +} |
| 65 | + |
| 66 | +// --------------------------------------------------------------------------- |
| 67 | +// Case 2. The path is normalized at least once, but never checked afterwards. |
| 68 | +// --------------------------------------------------------------------------- |
| 69 | +/** |
| 70 | + * DEPRECATED: Use `PathInjection::Configuration` instead |
| 71 | + * |
| 72 | + * Configuration to find paths from sources to normalizations that contain no prior normalizations. |
| 73 | + */ |
| 74 | +deprecated class FirstNormalizationConfiguration extends TaintTracking::Configuration { |
| 75 | + FirstNormalizationConfiguration() { this = "FirstNormalizationConfiguration" } |
| 76 | + |
| 77 | + override predicate isSource(DataFlow::Node source) { source instanceof Source } |
| 78 | + |
| 79 | + override predicate isSink(DataFlow::Node sink) { sink instanceof Path::PathNormalization } |
| 80 | + |
| 81 | + override predicate isSanitizer(DataFlow::Node node) { node instanceof Sanitizer } |
| 82 | + |
| 83 | + override predicate isSanitizerOut(DataFlow::Node node) { node instanceof Path::PathNormalization } |
| 84 | + |
| 85 | + override predicate isSanitizerGuard(DataFlow::BarrierGuard guard) { |
| 86 | + guard instanceof SanitizerGuard |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +/** |
| 91 | + * DEPRECATED: Use `PathInjection::Configuration` instead |
| 92 | + * |
| 93 | + * Configuration to find paths from normalizations to sinks that do not go through a check. |
| 94 | + */ |
| 95 | +deprecated class NormalizedPathNotCheckedConfiguration extends TaintTracking2::Configuration { |
| 96 | + NormalizedPathNotCheckedConfiguration() { this = "NormalizedPathNotCheckedConfiguration" } |
| 97 | + |
| 98 | + override predicate isSource(DataFlow::Node source) { source instanceof Path::PathNormalization } |
| 99 | + |
| 100 | + override predicate isSink(DataFlow::Node sink) { sink instanceof Sink } |
| 101 | + |
| 102 | + override predicate isSanitizer(DataFlow::Node node) { node instanceof Sanitizer } |
| 103 | + |
| 104 | + override predicate isSanitizerGuard(DataFlow::BarrierGuard guard) { |
| 105 | + guard instanceof Path::SafeAccessCheck |
| 106 | + or |
| 107 | + guard instanceof SanitizerGuard |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +/** |
| 112 | + * DEPRECATED: Use `PathInjection::Configuration` instead |
| 113 | + * |
| 114 | + * Holds if there is a path injection from source to sink, where the (python) path is |
| 115 | + * normalized at least once, but never checked afterwards. |
| 116 | + */ |
| 117 | +deprecated predicate pathNotCheckedAfterNormalization(CustomPathNode source, CustomPathNode sink) { |
| 118 | + exists( |
| 119 | + FirstNormalizationConfiguration config, DataFlow::PathNode mid1, DataFlow2::PathNode mid2, |
| 120 | + NormalizedPathNotCheckedConfiguration config2 |
| 121 | + | |
| 122 | + config.hasFlowPath(source.asNode1(), mid1) and |
| 123 | + config2.hasFlowPath(mid2, sink.asNode2()) and |
| 124 | + mid1.getNode().asCfgNode() = mid2.getNode().asCfgNode() |
| 125 | + ) |
| 126 | +} |
| 127 | + |
| 128 | +// --------------------------------------------------------------------------- |
| 129 | +// Query: Either case 1 or case 2. |
| 130 | +// --------------------------------------------------------------------------- |
| 131 | +/** |
| 132 | + * DEPRECATED: Use `PathInjection::Configuration` instead |
| 133 | + * |
| 134 | + * Holds if there is a path injection from source to sink |
| 135 | + */ |
| 136 | +deprecated predicate pathInjection(CustomPathNode source, CustomPathNode sink) { |
| 137 | + pathNotNormalized(source, sink) |
| 138 | + or |
| 139 | + pathNotCheckedAfterNormalization(source, sink) |
| 140 | +} |
0 commit comments