forked from praetorian-inc/gokart
/
traversal.go
86 lines (69 loc) · 2.98 KB
/
traversal.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
// Copyright 2021 Praetorian Security, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package analyzers
import (
"github.com/praetorian-inc/gokart/util"
"golang.org/x/tools/go/analysis"
"golang.org/x/tools/go/analysis/passes/buildssa"
)
// PathTraversalAnalyzer constructs Sinks from a set of functions known to be vulnerable to path injection
// all variables are converted to SSA form and a call graph is constructed
// recursive taint analysis is then used to search from a given Sink up the callgraph for Sources of user-controllable data
var PathTraversalAnalyzer = &analysis.Analyzer{
Name: "path_traversal",
Doc: "reports when path traversal can occur",
Run: traversalRun,
Requires: []*analysis.Analyzer{buildssa.Analyzer},
}
// getVulnerableInjectionFunctions() returns a map of functions that may be vulnerable to path traversal when used with user controlled input
func getVulnInjectionFunctions() map[string][]string {
return map[string][]string{
"os": {"Create", "Open", "OpenFile"},
"io/ioutil": {"ReadFile", "WriteFile"},
}
}
// traversalRun runs the path traversal analyzer
func traversalRun(pass *analysis.Pass) (interface{}, error) {
results := []util.Finding{}
// Builds SSA model of Go code
ssaFuncs := pass.ResultOf[buildssa.Analyzer].(*buildssa.SSA).SrcFuncs
// Creates call graph of function calls
cg := make(util.CallGraph)
// Fills in call graph
for _, fn := range ssaFuncs {
cg.AnalyzeFunction(fn)
}
// Grabs vulnerable functions to scan for
pathFuncs := getVulnInjectionFunctions()
// Iterate over every specified vulnerable package
for pkg, funcs := range pathFuncs {
// Iterate over every specified vulnerable function per package
for _, fn := range funcs {
// Construct full name of function
curFunc := pkg + "." + fn
// Iterate over occurrences of vulnerable function in call graph
for _, vulnFunc := range cg[curFunc] {
// Check if argument of vulnerable function is tainted by possibly user-controlled input
taintAnalyzer := util.CreateTaintAnalyzer(pass, vulnFunc.Fn.Pos())
if taintAnalyzer.ContainsTaint(&vulnFunc.Instr.Call, &vulnFunc.Instr.Call.Args[0], cg) {
message := "Danger: possible path traversal injection detected"
targetFunc := util.GenerateTaintedCode(pass, vulnFunc.Fn, vulnFunc.Instr.Pos())
taintSource := taintAnalyzer.TaintSource
results = append(results, util.MakeFinding(message, targetFunc, taintSource, "CWE-22: Path Traversal"))
}
}
}
}
return results, nil
}