|
| 1 | +import codeql.ruby.AST |
| 2 | +import codeql.ruby.frameworks.Files |
| 3 | +import codeql.ruby.ApiGraphs |
| 4 | +import codeql.ruby.DataFlow |
| 5 | +import codeql.ruby.dataflow.RemoteFlowSources |
| 6 | + |
| 7 | +module DecompressionBomb { |
| 8 | + /** |
| 9 | + * A abstract class responsible for extending new decompression sinks |
| 10 | + * |
| 11 | + * can be a path, stream of compressed data, |
| 12 | + * or a call to function that use pipe |
| 13 | + */ |
| 14 | + abstract class Sink extends DataFlow::Node { } |
| 15 | + |
| 16 | + /** |
| 17 | + * The additional taint steps that need for creating taint tracking or dataflow. |
| 18 | + */ |
| 19 | + abstract class AdditionalTaintStep extends string { |
| 20 | + AdditionalTaintStep() { this = "AdditionalTaintStep" } |
| 21 | + |
| 22 | + /** |
| 23 | + * Holds if there is a additional taint step between pred and succ. |
| 24 | + */ |
| 25 | + abstract predicate isAdditionalTaintStep(DataFlow::Node pred, DataFlow::Node succ); |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +module Zlib { |
| 30 | + /** |
| 31 | + * Gets a node of `Zlib::GzipReader` member |
| 32 | + * |
| 33 | + * Note that if you use the lower level Zip::InputStream interface, rubyzip does not check the entry sizes. |
| 34 | + */ |
| 35 | + private API::Node gzipReaderInstance() { |
| 36 | + result = API::getTopLevelMember("Zlib").getMember("GzipReader") |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * A return values of following methods |
| 41 | + * `Zlib::GzipReader.open` |
| 42 | + * `Zlib::GzipReader.zcat` |
| 43 | + * `Zlib::GzipReader.new` |
| 44 | + */ |
| 45 | + class DecompressionBombSink extends DecompressionBomb::Sink { |
| 46 | + DecompressionBombSink() { |
| 47 | + this = gzipReaderInstance().getMethod(["open", "new", "zcat"]).getReturn().asSource() |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * The additional taint steps that need for creating taint tracking or dataflow for `Zlib`. |
| 53 | + */ |
| 54 | + class AdditionalTaintStep extends DecompressionBomb::AdditionalTaintStep { |
| 55 | + AdditionalTaintStep() { this = "AdditionalTaintStep" } |
| 56 | + |
| 57 | + override predicate isAdditionalTaintStep(DataFlow::Node nodeFrom, DataFlow::Node nodeTo) { |
| 58 | + exists(API::Node openOrNewOrZcat | |
| 59 | + openOrNewOrZcat = gzipReaderInstance().getMethod(["open", "new", "zcat"]) |
| 60 | + | |
| 61 | + nodeFrom = openOrNewOrZcat.getParameter(0).asSink() and |
| 62 | + nodeTo = openOrNewOrZcat.getReturn().asSource() |
| 63 | + ) |
| 64 | + } |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +module ZipInputStream { |
| 69 | + /** |
| 70 | + * Gets a node of `Zip::InputStream` member |
| 71 | + * |
| 72 | + * Note that if you use the lower level Zip::InputStream interface, rubyZip does not check the entry sizes. |
| 73 | + */ |
| 74 | + private API::Node zipInputStream() { |
| 75 | + result = API::getTopLevelMember("Zip").getMember("InputStream") |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * The methods |
| 80 | + * `Zip::InputStream.read` |
| 81 | + * `Zip::InputStream.extract` |
| 82 | + * |
| 83 | + * as source of decompression bombs, they need an additional taint step for a dataflow or taint tracking query |
| 84 | + */ |
| 85 | + class DecompressionBombSink extends DecompressionBomb::Sink { |
| 86 | + DecompressionBombSink() { |
| 87 | + this = zipInputStream().getMethod(["open", "new"]).getReturn().asSource() |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * The additional taint steps that need for creating taint tracking or dataflow for `Zip`. |
| 93 | + */ |
| 94 | + class AdditionalTaintStep extends DecompressionBomb::AdditionalTaintStep { |
| 95 | + AdditionalTaintStep() { this = "AdditionalTaintStep" } |
| 96 | + |
| 97 | + override predicate isAdditionalTaintStep(DataFlow::Node nodeFrom, DataFlow::Node nodeTo) { |
| 98 | + exists(API::Node openOrNew | openOrNew = zipInputStream().getMethod(["open", "new"]) | |
| 99 | + nodeFrom = openOrNew.getParameter(0).asSink() and |
| 100 | + nodeTo = openOrNew.getReturn().asSource() |
| 101 | + ) |
| 102 | + } |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +module ZipFile { |
| 107 | + /** |
| 108 | + * Gets a node of `Zip::File` member |
| 109 | + */ |
| 110 | + API::Node zipFile() { result = API::getTopLevelMember("Zip").getMember("File") } |
| 111 | + |
| 112 | + /** |
| 113 | + * Gets nodes that have a `base` at the beginning |
| 114 | + */ |
| 115 | + API::Node rubyZipNode(API::Node base) { |
| 116 | + result = base |
| 117 | + or |
| 118 | + result = rubyZipNode(base).getMethod(_) |
| 119 | + or |
| 120 | + result = rubyZipNode(base).getReturn() |
| 121 | + or |
| 122 | + result = rubyZipNode(base).getParameter(_) |
| 123 | + or |
| 124 | + result = rubyZipNode(base).getAnElement() |
| 125 | + or |
| 126 | + result = rubyZipNode(base).getBlock() |
| 127 | + } |
| 128 | + |
| 129 | + /** |
| 130 | + * The return values of following methods |
| 131 | + * `ZipIO.read` |
| 132 | + * `ZipEntry.extract` |
| 133 | + * A sanitizer exists inside the nodes which have `entry.size > someOBJ` |
| 134 | + */ |
| 135 | + class DecompressionBombSink extends DecompressionBomb::Sink { |
| 136 | + DecompressionBombSink() { |
| 137 | + exists(API::Node rubyZip | rubyZip = rubyZipNode(zipFile()) | |
| 138 | + this = rubyZip.getMethod(["extract", "read"]).getReturn().asSource() and |
| 139 | + not exists( |
| 140 | + rubyZip.getMethod("size").getReturn().getMethod([">", " <", "<=", " >="]).getParameter(0) |
| 141 | + ) |
| 142 | + ) |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + /** |
| 147 | + * The additional taint steps that need for creating taint tracking or dataflow for `Zip::File`. |
| 148 | + */ |
| 149 | + class AdditionalTaintStep extends DecompressionBomb::AdditionalTaintStep { |
| 150 | + AdditionalTaintStep() { this = "AdditionalTaintStep" } |
| 151 | + |
| 152 | + override predicate isAdditionalTaintStep(DataFlow::Node nodeFrom, DataFlow::Node nodeTo) { |
| 153 | + exists(API::Node zipFile | zipFile = zipFile().getMethod("open") | |
| 154 | + nodeFrom = zipFile.getParameter(0).asSink() and |
| 155 | + nodeTo = rubyZipNode(zipFile).getMethod(["extract", "read"]).getReturn().asSource() |
| 156 | + ) |
| 157 | + } |
| 158 | + } |
| 159 | + |
| 160 | + predicate isAdditionalTaintStep(DataFlow::Node nodeFrom, DataFlow::Node nodeTo) { |
| 161 | + exists(API::Node zipFile | zipFile = zipFile().getMethod("open") | |
| 162 | + nodeFrom = zipFile.getParameter(0).asSink() and |
| 163 | + nodeTo = rubyZipNode(zipFile).getMethod(["extract", "read"]).getReturn().asSource() |
| 164 | + ) |
| 165 | + } |
| 166 | + // /** |
| 167 | + // * The additional taint steps that need for creating taint tracking or dataflow for `Zip::File`. |
| 168 | + // */ |
| 169 | + // class AdditionalTaintStep1 extends DecompressionBomb::AdditionalTaintStep { |
| 170 | + // AdditionalTaintStep1() { this = "AdditionalTaintStep" } |
| 171 | + // override predicate isAdditionalTaintStep(DataFlow::Node nodeFrom, DataFlow::Node nodeTo) { |
| 172 | + // exists(API::Node zipFile | zipFile = zipFile().getMethod("open") | |
| 173 | + // nodeFrom = zipFile.getParameter(0).asSink() and |
| 174 | + // nodeTo = zipFile.asSource() |
| 175 | + // ) |
| 176 | + // } |
| 177 | + // } |
| 178 | + // API::Node rubyZipNode2() { |
| 179 | + // result = zipFile().getMethod("open") |
| 180 | + // or |
| 181 | + // result = rubyZipNode2().getMethod(_) |
| 182 | + // or |
| 183 | + // result = rubyZipNode2().getReturn() |
| 184 | + // or |
| 185 | + // result = rubyZipNode2().getParameter(_) |
| 186 | + // or |
| 187 | + // result = rubyZipNode2().getAnElement() |
| 188 | + // or |
| 189 | + // result = rubyZipNode2().getBlock() |
| 190 | + // } |
| 191 | + // /** |
| 192 | + // * The additional taint steps that need for creating taint tracking or dataflow for `Zip::File`. |
| 193 | + // */ |
| 194 | + // class AdditionalTaintStep2 extends DecompressionBomb::AdditionalTaintStep { |
| 195 | + // AdditionalTaintStep2() { this = "AdditionalTaintStep" } |
| 196 | + // override predicate isAdditionalTaintStep(DataFlow::Node nodeFrom, DataFlow::Node nodeTo) { |
| 197 | + // exists(API::Node zipFileOpen | zipFileOpen = rubyZipNode2() | |
| 198 | + // nodeFrom = zipFileOpen.getReturn().asSource() and |
| 199 | + // nodeTo = zipFileOpen.getMethod(["extract", "read"]).getReturn().asSource() |
| 200 | + // ) |
| 201 | + // } |
| 202 | + // } |
| 203 | + // /** |
| 204 | + // * The additional taint steps that need for creating taint tracking or dataflow for `Zip::File`. |
| 205 | + // */ |
| 206 | + // class AdditionalTaintStep3 extends DecompressionBomb::AdditionalTaintStep { |
| 207 | + // AdditionalTaintStep3() { this = "AdditionalTaintStep" } |
| 208 | + // override predicate isAdditionalTaintStep(DataFlow::Node nodeFrom, DataFlow::Node nodeTo) { |
| 209 | + // exists(API::Node zipFileOpen | zipFileOpen = rubyZipNode2() | |
| 210 | + // nodeFrom = zipFileOpen.asCall() and |
| 211 | + // nodeTo = zipFileOpen.getMethod(["extract", "read"]).getReturn().asSource() |
| 212 | + // ) |
| 213 | + // } |
| 214 | + // } |
| 215 | +} |
0 commit comments