
在GO軟件包及其依賴項中查找併計算unsafe用法。
go-geiger -v github.com/jlauinger/go-geiger

GO軟件包可以避免通過使用unsafe.Pointer來限制Pointer使用的限制。這對於提高效率甚至是必要的,即與C代碼或SYSCALL相互作用可能是有價值的。
但是,開發人員必須在unsafe.Pointer下使用極端的謹慎,因為錯誤可能會很快發生,並且可以引入嚴重的漏洞,例如無用後,緩衝液重複使用或緩衝區溢出。
由於可以通過依賴項(和依賴關係的依賴項)引入unsafe.Pointer的用法,因此不僅有必要審核項目代碼,而且還必須審核其依賴項,或者至少知道誰需要信任。
go-geiger可幫助開發人員快速確定GO包裝的導入樹中的哪些軟件包使用unsafe.Pointer ,以便開發人員可以將審計工作集中在這些方案上,或決定為不使用unsafe.Pointer庫切換庫。
要安裝go-geiger ,請使用以下命令:
go get github.com/jlauinger/go-geiger
這將安裝go-geiger到$GOPATH/bin ,因此請確保它包含在您的$PATH環境變量中。
在這樣的包裝上運行Go-Geiger:
$ go-geiger example/cmd
或提供多個包裹,由空間隔開:
$ go-geiger example/cmd example/util strings
要檢查當前目錄中的軟件包,您可以在沒有參數的情況下調用go-geiger :
$ go-geiger
提供--help Flag打印了go-geiger的使用信息:
$ go-geiger --help
有以下標誌可用:
--filter-context string Count only lines of requested context type (all,variable,parameter,assignment,call,other). Default all (default "all")
--filter-match string Count only lines of requested match type (all,pointer,sizeof,offsetof,alignof,sliceheader,stringheader,uintptr). Default pointer (default "pointer")
-h, --help help for geiger
-q, --hide-stats Hide statistics table, print only code. --show-code needs to be set manually
--include-std Show / include Golang stdlib packages
-l, --link Print link to pkg.go.dev instead of package name
-d, --max-depth int Maximum transitive import depth (default 10)
--show-code Print the code lines with unsafe usage
--show-only-once Do not repeat packages, show them only once and abbreviate further imports (default true)
-v, --verbose Show usage counts by different usage types
默認情況下, go-geiger僅計數unsafe.Pointer用法。 By setting the --filter-match argument to one of sizeof , offsetof , alignof , sliceheader , stringheader , uintptr , or all , you can also use go-geiger to find usages of unsafe.Sizeof , unsafe.Offsetof , unsafe.Alignof , reflect.SliceHeader , reflect.StringHeader , uintptr , or all of them at the same time.
使用--verbose參數,您可以指示go-geiger顯示不安全使用情況上下文的個人計數。 go-geiger區分以下幾點:
多變的
var x unsafe. Pointer範圍
func foo ( x unsafe. Pointer ) {}任務
x := unsafe . Pointer ( & y )稱呼
x := unsafe . Pointer ( & y )
foo ( x )其他,其中包括所有不屬於前四個的東西。
使用--filter-context參數將計數過濾為特定上下文類型。您可以使用variable , parameter , assignment , call , other或all 。
如果您的項目使用go模塊和go.mod文件,則go-geiger將在分析它們之前自動獲取所有依賴項。它的行為與go build一樣。
如果您使用不同形式的依賴項管理,例如手動go get , go mod vendor或其他任何東西,則需要在運行go-geiger之前運行依賴項管理,以便在分析之前對所有依賴關係保持最新。
go-geiger的靈感來自Cargo Geiger,這是一種類似的工具,可以在Rust程序及其依賴項中找到不安全的代碼塊。
jlauinger/go-unsafepointer-poc包含用於利用濫用unsafe.Pointer引起的漏洞的概念證明。我還寫了一個有關特定問題和漏洞的博客文章系列。
Go-Safer是一個GO Linter工具,可以幫助識別兩個常見和危險的使用模式unsafe.Pointer 。
要獲取源代碼並編譯二進制代碼,請運行以下操作:
$ git clone https://github.com/jlauinger/go-geiger
$ cd go-geiger
$ go build
使用go test進行測試。
根據MIT許可(“許可”)許可。除了符合許可外,您不得使用此項目。您可以在此處獲得許可證的副本。
版權所有2020 Johannes Lauinger
該工具是在我在TU Darmstadt的軟件技術集團上的碩士論文中開發的。