|
| 1 | +/** |
| 2 | + * Provides classes modeling security-relevant aspects of the PyYAML package |
| 3 | + * https://pyyaml.org/wiki/PyYAMLDocumentation (obtained via `import yaml`). |
| 4 | + */ |
| 5 | + |
| 6 | +private import python |
| 7 | +private import experimental.dataflow.DataFlow |
| 8 | +private import experimental.dataflow.RemoteFlowSources |
| 9 | +private import experimental.semmle.python.Concepts |
| 10 | + |
| 11 | +private module Yaml { |
| 12 | + /** Gets a reference to the `yaml` module. */ |
| 13 | + private DataFlow::Node yaml(DataFlow::TypeTracker t) { |
| 14 | + t.start() and |
| 15 | + result = DataFlow::importNode("yaml") |
| 16 | + or |
| 17 | + exists(DataFlow::TypeTracker t2 | result = yaml(t2).track(t2, t)) |
| 18 | + } |
| 19 | + |
| 20 | + /** Gets a reference to the `yaml` module. */ |
| 21 | + DataFlow::Node yaml() { result = yaml(DataFlow::TypeTracker::end()) } |
| 22 | + |
| 23 | + /** Provides models for the `yaml` module. */ |
| 24 | + module yaml { |
| 25 | + /** |
| 26 | + * Gets a reference to the attribute `attr_name` of the `yaml` module. |
| 27 | + * WARNING: Only holds for a few predefined attributes. |
| 28 | + * |
| 29 | + * For example, using `attr_name = "load"` will get all uses of `yaml.load`. |
| 30 | + */ |
| 31 | + private DataFlow::Node yaml_attr(DataFlow::TypeTracker t, string attr_name) { |
| 32 | + attr_name in ["load", "SafeLoader", "BaseLoader"] and |
| 33 | + ( |
| 34 | + t.start() and |
| 35 | + result = DataFlow::importNode("yaml." + attr_name) |
| 36 | + or |
| 37 | + t.startInAttr(attr_name) and |
| 38 | + result = yaml() |
| 39 | + ) |
| 40 | + or |
| 41 | + // Due to bad performance when using normal setup with `yaml_attr(t2, attr_name).track(t2, t)` |
| 42 | + // we have inlined that code and forced a join |
| 43 | + exists(DataFlow::TypeTracker t2 | |
| 44 | + exists(DataFlow::StepSummary summary | |
| 45 | + yaml_attr_first_join(t2, attr_name, result, summary) and |
| 46 | + t = t2.append(summary) |
| 47 | + ) |
| 48 | + ) |
| 49 | + } |
| 50 | + |
| 51 | + pragma[nomagic] |
| 52 | + private predicate yaml_attr_first_join( |
| 53 | + DataFlow::TypeTracker t2, string attr_name, DataFlow::Node res, DataFlow::StepSummary summary |
| 54 | + ) { |
| 55 | + DataFlow::StepSummary::step(yaml_attr(t2, attr_name), res, summary) |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * Gets a reference to the attribute `attr_name` of the `yaml` module. |
| 60 | + * WARNING: Only holds for a few predefined attributes. |
| 61 | + * |
| 62 | + * For example, using `attr_name = "load"` will get all uses of `yaml.load`. |
| 63 | + */ |
| 64 | + DataFlow::Node yaml_attr(string attr_name) { |
| 65 | + result = yaml_attr(DataFlow::TypeTracker::end(), attr_name) |
| 66 | + } |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +/** |
| 71 | + * A call to `yaml.load` |
| 72 | + * See https://pyyaml.org/wiki/PyYAMLDocumentation (you will have to scroll down). |
| 73 | + */ |
| 74 | +private class YamlLoadCall extends Decoding::Range, DataFlow::CfgNode { |
| 75 | + override CallNode node; |
| 76 | + |
| 77 | + YamlLoadCall() { node.getFunction() = Yaml::yaml::yaml_attr("load").asCfgNode() } |
| 78 | + |
| 79 | + /** |
| 80 | + * This function was thought safe from the 5.1 release in 2017, when the default loader was changed to `FullLoader`. |
| 81 | + * In 2020 new exploits were found, meaning it's not safe. The Current plan is to change the default to `SafeLoader` in release 6.0 |
| 82 | + * (as explained in https://github.com/yaml/pyyaml/issues/420#issuecomment-696752389). |
| 83 | + * Until 6.0 is released, we will mark `yaml.load` as possibly leading to arbitrary code execution. |
| 84 | + * See https://github.com/yaml/pyyaml/wiki/PyYAML-yaml.load(input)-Deprecation for more details. |
| 85 | + */ |
| 86 | + override predicate mayExecuteInput() { |
| 87 | + // If the `Loader` is not set to either `SafeLoader` or `BaseLoader` or not set at all, |
| 88 | + // then the default loader will be used, which is not safe. |
| 89 | + not node.getArgByName("Loader") = |
| 90 | + Yaml::yaml::yaml_attr(["SafeLoader", "BaseLoader"]).asCfgNode() |
| 91 | + } |
| 92 | + |
| 93 | + override DataFlow::Node getAnInput() { result.asCfgNode() = node.getArg(0) } |
| 94 | + |
| 95 | + override DataFlow::Node getOutput() { result = this } |
| 96 | + |
| 97 | + override string getFormat() { result = "YAML" } |
| 98 | +} |
0 commit comments