|
| 1 | +/** |
| 2 | + * Provides classes modeling security-relevant aspects of the `log` package. |
| 3 | + */ |
| 4 | + |
| 5 | +import go |
| 6 | + |
| 7 | +/** Provides models of commonly used functions in the `log` package. */ |
| 8 | +module Log { |
| 9 | + private class LogCall extends LoggerCall::Range, DataFlow::CallNode { |
| 10 | + LogCall() { |
| 11 | + exists(string fn | |
| 12 | + fn.matches("Fatal%") |
| 13 | + or |
| 14 | + fn.matches("Panic%") |
| 15 | + or |
| 16 | + fn.matches("Print%") |
| 17 | + | |
| 18 | + this.getTarget().hasQualifiedName("log", fn) |
| 19 | + or |
| 20 | + this.getTarget().(Method).hasQualifiedName("log", "Logger", fn) |
| 21 | + ) |
| 22 | + } |
| 23 | + |
| 24 | + override DataFlow::Node getAMessageComponent() { result = this.getAnArgument() } |
| 25 | + } |
| 26 | + |
| 27 | + /** A fatal log function, which calls `os.Exit`. */ |
| 28 | + private class FatalLogFunction extends Function { |
| 29 | + FatalLogFunction() { exists(string fn | fn.matches("Fatal%") | hasQualifiedName("log", fn)) } |
| 30 | + |
| 31 | + override predicate mayReturnNormally() { none() } |
| 32 | + } |
| 33 | + |
| 34 | + private class FunctionModels extends TaintTracking::FunctionModel { |
| 35 | + FunctionInput inp; |
| 36 | + FunctionOutput outp; |
| 37 | + |
| 38 | + FunctionModels() { |
| 39 | + // signature: func New(out io.Writer, prefix string, flag int) *Logger |
| 40 | + hasQualifiedName("log", "New") and |
| 41 | + (inp.isResult() and outp.isParameter(0)) |
| 42 | + } |
| 43 | + |
| 44 | + override predicate hasTaintFlow(FunctionInput input, FunctionOutput output) { |
| 45 | + input = inp and output = outp |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + private class MethodModels extends TaintTracking::FunctionModel, Method { |
| 50 | + FunctionInput inp; |
| 51 | + FunctionOutput outp; |
| 52 | + |
| 53 | + MethodModels() { |
| 54 | + // signature: func (*Logger).Fatal(v ...interface{}) |
| 55 | + this.hasQualifiedName("log", "Logger", "Fatal") and |
| 56 | + (inp.isParameter(_) and outp.isReceiver()) |
| 57 | + or |
| 58 | + // signature: func (*Logger).Fatalf(format string, v ...interface{}) |
| 59 | + this.hasQualifiedName("log", "Logger", "Fatalf") and |
| 60 | + (inp.isParameter(_) and outp.isReceiver()) |
| 61 | + or |
| 62 | + // signature: func (*Logger).Fatalln(v ...interface{}) |
| 63 | + this.hasQualifiedName("log", "Logger", "Fatalln") and |
| 64 | + (inp.isParameter(_) and outp.isReceiver()) |
| 65 | + or |
| 66 | + // signature: func (*Logger).Panic(v ...interface{}) |
| 67 | + this.hasQualifiedName("log", "Logger", "Panic") and |
| 68 | + (inp.isParameter(_) and outp.isReceiver()) |
| 69 | + or |
| 70 | + // signature: func (*Logger).Panicf(format string, v ...interface{}) |
| 71 | + this.hasQualifiedName("log", "Logger", "Panicf") and |
| 72 | + (inp.isParameter(_) and outp.isReceiver()) |
| 73 | + or |
| 74 | + // signature: func (*Logger).Panicln(v ...interface{}) |
| 75 | + this.hasQualifiedName("log", "Logger", "Panicln") and |
| 76 | + (inp.isParameter(_) and outp.isReceiver()) |
| 77 | + or |
| 78 | + // signature: func (*Logger).Print(v ...interface{}) |
| 79 | + this.hasQualifiedName("log", "Logger", "Print") and |
| 80 | + (inp.isParameter(_) and outp.isReceiver()) |
| 81 | + or |
| 82 | + // signature: func (*Logger).Printf(format string, v ...interface{}) |
| 83 | + this.hasQualifiedName("log", "Logger", "Printf") and |
| 84 | + (inp.isParameter(_) and outp.isReceiver()) |
| 85 | + or |
| 86 | + // signature: func (*Logger).Println(v ...interface{}) |
| 87 | + this.hasQualifiedName("log", "Logger", "Println") and |
| 88 | + (inp.isParameter(_) and outp.isReceiver()) |
| 89 | + or |
| 90 | + // signature: func (*Logger).SetOutput(w io.Writer) |
| 91 | + this.hasQualifiedName("log", "Logger", "SetOutput") and |
| 92 | + (inp.isReceiver() and outp.isParameter(0)) |
| 93 | + or |
| 94 | + // signature: func (*Logger).SetPrefix(prefix string) |
| 95 | + this.hasQualifiedName("log", "Logger", "SetPrefix") and |
| 96 | + (inp.isParameter(0) and outp.isReceiver()) |
| 97 | + or |
| 98 | + // signature: func (*Logger).Writer() io.Writer |
| 99 | + this.hasQualifiedName("log", "Logger", "Writer") and |
| 100 | + (inp.isResult() and outp.isReceiver()) |
| 101 | + } |
| 102 | + |
| 103 | + override predicate hasTaintFlow(FunctionInput input, FunctionOutput output) { |
| 104 | + input = inp and output = outp |
| 105 | + } |
| 106 | + } |
| 107 | +} |
0 commit comments