|
| 1 | +/** |
| 2 | + * Provides classes for modeling string formatting libraries. |
| 3 | + */ |
| 4 | + |
| 5 | +private import codeql.ruby.AST as Ast |
| 6 | +private import codeql.ruby.DataFlow |
| 7 | +private import codeql.ruby.ApiGraphs |
| 8 | +private import codeql.ruby.frameworks.core.IO |
| 9 | + |
| 10 | +/** |
| 11 | + * A call to `printf` or `sprintf`. |
| 12 | + */ |
| 13 | +abstract class PrintfStyleCall extends DataFlow::CallNode { |
| 14 | + // We assume that most printf-like calls have the signature f(format_string, args...) |
| 15 | + /** |
| 16 | + * Gets the format string of this call. |
| 17 | + */ |
| 18 | + DataFlow::Node getFormatString() { result = this.getArgument(0) } |
| 19 | + |
| 20 | + /** |
| 21 | + * Gets then `n`th formatted argument of this call. |
| 22 | + */ |
| 23 | + DataFlow::Node getFormatArgument(int n) { n >= 0 and result = this.getArgument(n + 1) } |
| 24 | + |
| 25 | + /** Holds if this call returns the formatted string. */ |
| 26 | + predicate returnsFormatted() { any() } |
| 27 | +} |
| 28 | + |
| 29 | +/** |
| 30 | + * A call to `Kernel.printf`. |
| 31 | + */ |
| 32 | +class KernelPrintfCall extends PrintfStyleCall { |
| 33 | + KernelPrintfCall() { |
| 34 | + this = API::getTopLevelMember("Kernel").getAMethodCall("printf") |
| 35 | + or |
| 36 | + this.asExpr().getExpr() instanceof Ast::UnknownMethodCall and |
| 37 | + this.getMethodName() = "printf" |
| 38 | + } |
| 39 | + |
| 40 | + // Kernel#printf supports two signatures: |
| 41 | + // printf(io, string, ...) |
| 42 | + // printf(string, ...) |
| 43 | + override DataFlow::Node getFormatString() { |
| 44 | + // Because `printf` has two different signatures, we can't be sure which |
| 45 | + // argument is the format string, so we use a heuristic: |
| 46 | + // If the first argument has a string value, then we assume it is the format string. |
| 47 | + // Otherwise we treat both the first and second args as the format string. |
| 48 | + if this.getArgument(0).getExprNode().getConstantValue().isString(_) |
| 49 | + then result = this.getArgument(0) |
| 50 | + else result = this.getArgument([0, 1]) |
| 51 | + } |
| 52 | + |
| 53 | + override predicate returnsFormatted() { none() } |
| 54 | +} |
| 55 | + |
| 56 | +/** |
| 57 | + * A call to `Kernel.sprintf`. |
| 58 | + */ |
| 59 | +class KernelSprintfCall extends PrintfStyleCall { |
| 60 | + KernelSprintfCall() { |
| 61 | + this = API::getTopLevelMember("Kernel").getAMethodCall("sprintf") |
| 62 | + or |
| 63 | + this.asExpr().getExpr() instanceof Ast::UnknownMethodCall and |
| 64 | + this.getMethodName() = "sprintf" |
| 65 | + } |
| 66 | + |
| 67 | + override predicate returnsFormatted() { any() } |
| 68 | +} |
| 69 | + |
| 70 | +/** |
| 71 | + * A call to `IO#printf`. |
| 72 | + */ |
| 73 | +class IOPrintfCall extends PrintfStyleCall { |
| 74 | + IOPrintfCall() { |
| 75 | + this.getReceiver() instanceof IO::IOInstance and this.getMethodName() = "printf" |
| 76 | + } |
| 77 | + |
| 78 | + override predicate returnsFormatted() { none() } |
| 79 | +} |
| 80 | + |
| 81 | +/** |
| 82 | + * A call to `String#%`. |
| 83 | + */ |
| 84 | +class StringPercentCall extends PrintfStyleCall { |
| 85 | + StringPercentCall() { this.getMethodName() = "%" } |
| 86 | + |
| 87 | + override DataFlow::Node getFormatString() { result = this.getReceiver() } |
| 88 | + |
| 89 | + override DataFlow::Node getFormatArgument(int n) { |
| 90 | + exists(CfgNodes::ExprNodes::ArrayLiteralCfgNode arrLit | arrLit = this.getArgument(0).asExpr() | |
| 91 | + result.asExpr() = arrLit.getArgument(n) |
| 92 | + ) |
| 93 | + or |
| 94 | + exists(CfgNodes::ExprNodes::HashLiteralCfgNode hashLit | |
| 95 | + hashLit = this.getArgument(0).asExpr() |
| 96 | + | |
| 97 | + n = -2 and // -2 is indicates that the index does not make sense in this context |
| 98 | + result.asExpr() = hashLit.getAKeyValuePair().getValue() |
| 99 | + ) |
| 100 | + } |
| 101 | + |
| 102 | + override predicate returnsFormatted() { any() } |
| 103 | +} |
| 104 | + |
| 105 | +private import codeql.ruby.dataflow.FlowSteps |
| 106 | +private import codeql.ruby.CFG |
| 107 | + |
| 108 | +/** |
| 109 | + * A step for string interpolation of `pred` into `succ`. |
| 110 | + * E.g. |
| 111 | + * ```rb |
| 112 | + * succ = "foo #{pred} bar" |
| 113 | + * ``` |
| 114 | + */ |
| 115 | +private class StringLiteralFormatStep extends AdditionalTaintStep { |
| 116 | + override predicate step(DataFlow::Node pred, DataFlow::Node succ) { |
| 117 | + pred.asExpr() = succ.asExpr().(CfgNodes::ExprNodes::StringlikeLiteralCfgNode).getAComponent() |
| 118 | + } |
| 119 | +} |
| 120 | + |
| 121 | +/** |
| 122 | + * A taint propagating data flow edge arising from string formatting. |
| 123 | + */ |
| 124 | +private class StringFormattingTaintStep extends AdditionalTaintStep { |
| 125 | + override predicate step(DataFlow::Node pred, DataFlow::Node succ) { |
| 126 | + exists(PrintfStyleCall call | |
| 127 | + call.returnsFormatted() and |
| 128 | + succ = call |
| 129 | + | |
| 130 | + pred = call.getFormatString() |
| 131 | + or |
| 132 | + pred = call.getFormatArgument(_) |
| 133 | + ) |
| 134 | + } |
| 135 | +} |
0 commit comments