|
| 1 | +/** |
| 2 | + * @name Uncontrolled data in SQL query to Postgres |
| 3 | + * @description Including user-supplied data in a SQL query to Postgres |
| 4 | + * without neutralizing special elements can make code |
| 5 | + * vulnerable to SQL Injection. |
| 6 | + * @kind path-problem |
| 7 | + * @problem.severity error |
| 8 | + * @precision high |
| 9 | + * @id cpp/sql-injection-via-pqxx |
| 10 | + * @tags security |
| 11 | + * external/cwe/cwe-089 |
| 12 | + */ |
| 13 | + |
| 14 | +import cpp |
| 15 | +import semmle.code.cpp.security.Security |
| 16 | +import semmle.code.cpp.dataflow.TaintTracking |
| 17 | +import DataFlow::PathGraph |
| 18 | + |
| 19 | +predicate pqxxTransationClassNames(string className, string namespace) { |
| 20 | + namespace = "pqxx" and |
| 21 | + className in [ |
| 22 | + "dbtransaction", "nontransaction", "basic_robusttransaction", "robusttransaction", |
| 23 | + "subtransaction", "transaction", "basic_transaction", "transaction_base", "work" |
| 24 | + ] |
| 25 | +} |
| 26 | + |
| 27 | +predicate pqxxConnectionClassNames(string className, string namespace) { |
| 28 | + namespace = "pqxx" and |
| 29 | + className in ["connection_base", "basic_connection", "connection"] |
| 30 | +} |
| 31 | + |
| 32 | +predicate pqxxTransactionSqlArgument(string function, int arg) { |
| 33 | + function = "exec" and arg = 0 |
| 34 | + or |
| 35 | + function = "exec0" and arg = 0 |
| 36 | + or |
| 37 | + function = "exec1" and arg = 0 |
| 38 | + or |
| 39 | + function = "exec_n" and arg = 1 |
| 40 | + or |
| 41 | + function = "exec_params" and arg = 0 |
| 42 | + or |
| 43 | + function = "exec_params0" and arg = 0 |
| 44 | + or |
| 45 | + function = "exec_params1" and arg = 0 |
| 46 | + or |
| 47 | + function = "exec_params_n" and arg = 1 |
| 48 | + or |
| 49 | + function = "query_value" and arg = 0 |
| 50 | + or |
| 51 | + function = "stream" and arg = 0 |
| 52 | +} |
| 53 | + |
| 54 | +predicate pqxxConnectionSqlArgument(string function, int arg) { function = "prepare" and arg = 1 } |
| 55 | + |
| 56 | +Expr getPqxxSqlArgument() { |
| 57 | + exists(FunctionCall fc, Expr e, int argIndex, UserType t | |
| 58 | + // examples: 'work' for 'work.exec(...)'; '->' for 'tx->exec()'. |
| 59 | + e = fc.getQualifier() and |
| 60 | + // to find ConnectionHandle/TransationHandle and similar classes which override '->' operator behavior |
| 61 | + // and return pointer to a connection/transation object |
| 62 | + e.getType().refersTo(t) and |
| 63 | + // transaction exec and connection prepare variations |
| 64 | + ( |
| 65 | + pqxxTransationClassNames(t.getName(), t.getNamespace().getName()) and |
| 66 | + pqxxTransactionSqlArgument(fc.getTarget().getName(), argIndex) |
| 67 | + or |
| 68 | + pqxxConnectionClassNames(t.getName(), t.getNamespace().getName()) and |
| 69 | + pqxxConnectionSqlArgument(fc.getTarget().getName(), argIndex) |
| 70 | + ) and |
| 71 | + result = fc.getArgument(argIndex) |
| 72 | + ) |
| 73 | +} |
| 74 | + |
| 75 | +predicate pqxxEscapeArgument(string function, int arg) { |
| 76 | + arg = 0 and |
| 77 | + function in ["esc", "esc_raw", "quote", "quote_raw", "quote_name", "quote_table", "esc_like"] |
| 78 | +} |
| 79 | + |
| 80 | +predicate isEscapedPqxxArgument(Expr argExpr) { |
| 81 | + exists(FunctionCall fc, Expr e, int argIndex, UserType t | |
| 82 | + // examples: 'work' for 'work.exec(...)'; '->' for 'tx->exec()'. |
| 83 | + e = fc.getQualifier() and |
| 84 | + // to find ConnectionHandle/TransationHandle and similar classes which override '->' operator behavior |
| 85 | + // and return pointer to a connection/transation object |
| 86 | + e.getType().refersTo(t) and |
| 87 | + // transaction and connection escape functions |
| 88 | + ( |
| 89 | + pqxxTransationClassNames(t.getName(), t.getNamespace().getName()) or |
| 90 | + pqxxConnectionClassNames(t.getName(), t.getNamespace().getName()) |
| 91 | + ) and |
| 92 | + pqxxEscapeArgument(fc.getTarget().getName(), argIndex) and |
| 93 | + // is escaped arg == argExpr |
| 94 | + argExpr = fc.getArgument(argIndex) |
| 95 | + ) |
| 96 | +} |
| 97 | + |
| 98 | +class Configuration extends TaintTracking::Configuration { |
| 99 | + Configuration() { this = "SqlPqxxTainted" } |
| 100 | + |
| 101 | + override predicate isSource(DataFlow::Node source) { isUserInput(source.asExpr(), _) } |
| 102 | + |
| 103 | + override predicate isSink(DataFlow::Node sink) { sink.asExpr() = getPqxxSqlArgument() } |
| 104 | + |
| 105 | + override predicate isSanitizer(DataFlow::Node node) { isEscapedPqxxArgument(node.asExpr()) } |
| 106 | +} |
| 107 | + |
| 108 | +from DataFlow::PathNode source, DataFlow::PathNode sink, Configuration config, string taintCause |
| 109 | +where |
| 110 | + config.hasFlowPath(source, sink) and |
| 111 | + isUserInput(source.getNode().asExpr(), taintCause) |
| 112 | +select sink, source, sink, "This argument to a SQL query function is derived from $@", source, |
| 113 | + "user input (" + taintCause + ")" |
0 commit comments