-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add log injection analysis * Improve callgraph construction * Add `(callgraph.Path).String` method * Use `→` for `(callgraph.Edge).String` * Add `(*callgraph.Graph).String` method * Do NOT assume so much about anonymous functions
- Loading branch information
Showing
7 changed files
with
283 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package main | ||
|
||
import ( | ||
"github.com/picatz/taint/log/injection" | ||
"golang.org/x/tools/go/analysis/singlechecker" | ||
) | ||
|
||
func main() { | ||
singlechecker.Main(injection.Analyzer) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
package injection | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/picatz/taint" | ||
"github.com/picatz/taint/callgraph" | ||
|
||
"golang.org/x/tools/go/analysis" | ||
"golang.org/x/tools/go/analysis/passes/buildssa" | ||
) | ||
|
||
var userControlledValues = taint.NewSources( | ||
"*net/http.Request", | ||
) | ||
|
||
var injectableLogFunctions = taint.NewSinks( | ||
// Note: at this time, they *must* be a function or method. | ||
"log.Fatal", | ||
"log.Fatalf", | ||
"log.Fatalln", | ||
"log.Panic", | ||
"log.Panicf", | ||
"log.Panicln", | ||
"log.Print", | ||
"log.Printf", | ||
"log.Println", | ||
"log.Output", | ||
"log.SetOutput", | ||
"log.SetPrefix", | ||
"log.Writer", | ||
"(*log.Logger).Fatal", | ||
"(*log.Logger).Fatalf", | ||
"(*log.Logger).Fatalln", | ||
"(*log.Logger).Panic", | ||
"(*log.Logger).Panicf", | ||
"(*log.Logger).Panicln", | ||
"(*log.Logger).Print", | ||
"(*log.Logger).Printf", | ||
"(*log.Logger).Println", | ||
"(*log.Logger).Output", | ||
"(*log.Logger).SetOutput", | ||
"(*log.Logger).SetPrefix", | ||
"(*log.Logger).Writer", | ||
// TODO: consider adding the following logger packages, | ||
// and the ability to configure this list generically. | ||
// | ||
// https://pkg.go.dev/golang.org/x/exp/slog | ||
// https://pkg.go.dev/github.com/golang/glog | ||
// https://pkg.go.dev/github.com/hashicorp/go-hclog | ||
// https://pkg.go.dev/github.com/sirupsen/logrus | ||
// https://pkg.go.dev/go.uber.org/zap | ||
// ... | ||
) | ||
|
||
// Analyzer finds potential log injection issues to demonstrate | ||
// the github.com/picatz/taint package. | ||
var Analyzer = &analysis.Analyzer{ | ||
Name: "logi", | ||
Doc: "finds potential log injection issues", | ||
Run: run, | ||
Requires: []*analysis.Analyzer{buildssa.Analyzer}, | ||
} | ||
|
||
// imports returns true if the package imports any of the given packages. | ||
func imports(pass *analysis.Pass, pkgs ...string) bool { | ||
var imported bool | ||
for _, imp := range pass.Pkg.Imports() { | ||
for _, pkg := range pkgs { | ||
if strings.HasSuffix(imp.Path(), pkg) { | ||
imported = true | ||
break | ||
} | ||
} | ||
if imported { | ||
break | ||
} | ||
} | ||
return imported | ||
} | ||
|
||
func run(pass *analysis.Pass) (interface{}, error) { | ||
// Require the log package is imported in the | ||
// program being analyzed before running the analysis. | ||
// | ||
// This prevents wasting time analyzing programs that don't log. | ||
if !imports(pass, "log") { | ||
return nil, nil | ||
} | ||
|
||
// Get the built SSA IR. | ||
buildSSA := pass.ResultOf[buildssa.Analyzer].(*buildssa.SSA) | ||
|
||
// Identify the main function from the package's SSA IR. | ||
mainFn := buildSSA.Pkg.Func("main") | ||
if mainFn == nil { | ||
return nil, nil | ||
} | ||
|
||
// Construct a callgraph, using the main function as the root, | ||
// constructed of all other functions. This returns a callgraph | ||
// we can use to identify directed paths to logging functions. | ||
cg, err := callgraph.New(mainFn, buildSSA.SrcFuncs...) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to create new callgraph: %w", err) | ||
} | ||
|
||
// Run taint check for user controlled values (sources) ending | ||
// up in injectable log functions (sinks). | ||
results := taint.Check(cg, userControlledValues, injectableLogFunctions) | ||
|
||
// For each result, check if a prepared statement is providing | ||
// a mitigation for the user controlled value. | ||
// | ||
// TODO: ensure this makes sense for all the GORM usage? | ||
for _, result := range results { | ||
pass.Reportf(result.SinkValue.Pos(), "potential log injection") | ||
} | ||
|
||
return nil, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package injection | ||
|
||
import ( | ||
"testing" | ||
|
||
"golang.org/x/tools/go/analysis/analysistest" | ||
) | ||
|
||
var testdata = analysistest.TestData() | ||
|
||
func TestA(t *testing.T) { | ||
analysistest.Run(t, testdata, Analyzer, "a") | ||
} | ||
|
||
func TestB(t *testing.T) { | ||
analysistest.Run(t, testdata, Analyzer, "b") | ||
} | ||
|
||
func TestC(t *testing.T) { | ||
analysistest.Run(t, testdata, Analyzer, "c") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package main | ||
|
||
import ( | ||
"log" | ||
"net/http" | ||
) | ||
|
||
func main() { | ||
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { | ||
log.Println(r.URL.Query().Get("input")) // want "potential log injection" | ||
}) | ||
|
||
http.ListenAndServe(":8080", nil) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package main | ||
|
||
import ( | ||
"log" | ||
"net/http" | ||
) | ||
|
||
func l(input string) { | ||
l := log.New(nil, "", 0) | ||
l.Println(input) // want "potential log injection" | ||
} | ||
|
||
func buisness(input string) { | ||
l(input) | ||
} | ||
|
||
func main() { | ||
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { | ||
input := r.URL.Query().Get("input") | ||
|
||
buisness(input) | ||
}) | ||
|
||
http.ListenAndServe(":8080", nil) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package main | ||
|
||
import ( | ||
"log" | ||
"net/http" | ||
) | ||
|
||
func l(input string) { | ||
log.Println(input) // want "potential log injection" | ||
} | ||
|
||
func buisness(input string) { | ||
l(input) | ||
} | ||
|
||
func main() { | ||
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { | ||
input := r.URL.Query().Get("input") | ||
|
||
f := func() { | ||
buisness(input) | ||
} | ||
|
||
f() | ||
}) | ||
|
||
http.ListenAndServe(":8080", nil) | ||
} |