|
| 1 | +/** |
| 2 | + * @name Information exposure through a stack trace |
| 3 | + * @description Information from a stack trace propagates to an external user. |
| 4 | + * Stack traces can unintentionally reveal implementation details |
| 5 | + * that are useful to an attacker for developing a subsequent exploit. |
| 6 | + * @kind path-problem |
| 7 | + * @problem.severity error |
| 8 | + * @precision high |
| 9 | + * @id go/stack-trace-exposure |
| 10 | + * @tags security |
| 11 | + * external/cwe/cwe-209 |
| 12 | + * external/cwe/cwe-497 |
| 13 | + */ |
| 14 | + |
| 15 | +import go |
| 16 | +import semmle.go.security.InsecureFeatureFlag::InsecureFeatureFlag |
| 17 | +import DataFlow::PathGraph |
| 18 | + |
| 19 | +/** |
| 20 | + * A flag indicating the program is in debug or development mode, or that stack |
| 21 | + * dumps have been specifically enabled. |
| 22 | + */ |
| 23 | +class DebugModeFlag extends FlagKind { |
| 24 | + DebugModeFlag() { this = "debugMode" } |
| 25 | + |
| 26 | + bindingset[result] |
| 27 | + override string getAFlagName() { |
| 28 | + result.regexpMatch("(?i).*(trace|debug|devel|(enable|disable|print)stack).*") |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +/** |
| 33 | + * The function `runtime.Stack`, which emits a stack trace. |
| 34 | + */ |
| 35 | +class StackFunction extends Function { |
| 36 | + StackFunction() { this.hasQualifiedName("runtime", "Stack") } |
| 37 | +} |
| 38 | + |
| 39 | +/** |
| 40 | + * The function `runtime/debug.Stack`, which emits a stack trace. |
| 41 | + */ |
| 42 | +class DebugStackFunction extends Function { |
| 43 | + DebugStackFunction() { this.hasQualifiedName("runtime/debug", "Stack") } |
| 44 | +} |
| 45 | + |
| 46 | +/** |
| 47 | + * A taint-tracking configuration that looks for stack traces being written to |
| 48 | + * an HTTP response body without an intervening debug- or development-mode conditional. |
| 49 | + */ |
| 50 | +class StackTraceExposureConfig extends TaintTracking::Configuration { |
| 51 | + StackTraceExposureConfig() { this = "StackTraceExposureConfig" } |
| 52 | + |
| 53 | + override predicate isSource(DataFlow::Node node) { |
| 54 | + node.(DataFlow::PostUpdateNode).getPreUpdateNode() = |
| 55 | + any(StackFunction f).getACall().getArgument(0) or |
| 56 | + node = any(DebugStackFunction f).getACall().getResult() |
| 57 | + } |
| 58 | + |
| 59 | + override predicate isSink(DataFlow::Node node) { node instanceof HTTP::ResponseBody } |
| 60 | + |
| 61 | + override predicate isSanitizer(DataFlow::Node node) { |
| 62 | + // Sanitise everything controlled by an is-debug-mode check. |
| 63 | + // Imprecision: I don't try to guess which arm of a branch is intended |
| 64 | + // to mean debug mode, and which is production mode. |
| 65 | + exists(ControlFlow::ConditionGuardNode cgn | |
| 66 | + cgn.ensures(any(DebugModeFlag f).getAFlag().getANode(), _) |
| 67 | + | |
| 68 | + cgn.dominates(node.getBasicBlock()) |
| 69 | + ) |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +from StackTraceExposureConfig cfg, DataFlow::PathNode source, DataFlow::PathNode sink |
| 74 | +where cfg.hasFlowPath(source, sink) |
| 75 | +select source.getNode(), source, sink, "This stack trace is exposed to a remote user $@.", |
| 76 | + sink.getNode(), "here" |
0 commit comments