Giter VIP home page Giter VIP logo

go-geiger's Introduction

go-geiger

build

go-geiger logo

Find and count unsafe usages in Go packages and their dependencies.

Output example

go-geiger -v github.com/jlauinger/go-geiger

go-geiger output example

What is the benefit?

A Go package can avoid the restrictions Go normally sets on pointer use by using unsafe.Pointer. This can be valuable to improve efficiency or even necessary e.g. to interact with C code or syscalls.

However, developers must use extreme caution with unsafe.Pointer because mistakes can happen quickly and they can introduce serious vulnerabilities such as use-after-free, buffer reuses or buffer overflows.

Since usages of unsafe.Pointer can be introduced through dependencies (and dependencies of dependencies), it is necessary to audit not only the project code but also its dependencies, or at least know who one needs to trust.

go-geiger helps developers to quickly identify which packages in the import tree of a Go package use unsafe.Pointer, so that developers can focus auditing efforts onto those, or decide to switch libraries for one that does not use unsafe.Pointer.

Install

To install go-geiger, use the following command:

go get github.com/jlauinger/go-geiger

This will install go-geiger to $GOPATH/bin, so make sure that it is included in your $PATH environment variable.

Usage

Run go-geiger on a package like this:

$ go-geiger example/cmd

Or supply multiple packages, separated by spaces:

$ go-geiger example/cmd example/util strings

To check the package in the current directory you can call go-geiger without parameters:

$ go-geiger

Supplying the --help flag prints the usage information for go-geiger:

$ go-geiger --help

There are the following flags available:

      --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

Unsafe Match Types

By default, go-geiger will count only unsafe.Pointer usages. 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.

Unsafe Context Types

Using the --verbose argument, you can instruct go-geiger to show individual counts for different usage contexts of unsafe. go-geiger distinguishes between the following:

Variable

var x unsafe.Pointer

Parameter

func foo(x unsafe.Pointer) {}

Assignment

x := unsafe.Pointer(&y)

Call

x := unsafe.Pointer(&y)
foo(x)

Other, which includes everything that doesn't fall under the first four.

Use the --filter-context argument to filter counting to a specific context type. You can use variable, parameter, assignment, call, other, or all.

Dependency management

If your project uses Go modules and a go.mod file, go-geiger will fetch all dependencies automatically before it analyzes them. It behaves exactly like go build would.

If you use a different form of dependency management, e.g. manual go get, go mod vendor or anything else, you need to run your dependency management before running go-geiger in order to have all dependencies up to date before analysis.

Related work

go-geiger is inspired by Cargo Geiger, a similar tool to find unsafe code blocks in Rust programs and their dependencies.

jlauinger/go-unsafepointer-poc contains proof of concepts for exploiting vulnerabilities caused by misuse of unsafe.Pointer. I also wrote a blog post series on the specific problems and vulnerabilities.

go-safer is a Go linter tool that can help to identify two common and dangerous usage patterns of unsafe.Pointer.

Development

To get the source code and compile the binary, run this:

$ git clone https://github.com/jlauinger/go-geiger
$ cd go-geiger
$ go build

Run the tests with go test.

License

Licensed under the MIT License (the "License"). You may not use this project except in compliance with the License. You may obtain a copy of the License here.

Copyright 2020 Johannes Lauinger

This tool has been developed as part of my Master's thesis at the Software Technology Group at TU Darmstadt.

go-geiger's People

Contributors

jlauinger avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

go-geiger's Issues

go-geiger does not detect unsafe usages with absolute paths in WSL2

When I use absolute paths for a go project, go-geiger won't detect it. The package path is also different, the first one has an underscore prepended to the path.

antoniozhu@WS0075-21:/mnt/c/Users/antonio.zhu/Documents/repos/praktikum/unsafe-toolkit$ go-geiger ./dummy_project/
  WITH DEPENDENCIES   LOCAL PACKAGE   PACKAGE PATH                                                                      
--------------------+---------------+-----------------------------------------------------------------------------------
          1                 1         _/mnt/c/Users/antonio.zhu/Documents/repos/praktikum/unsafe-toolkit/dummy_project  

Package _/mnt/c/Users/antonio.zhu/Documents/repos/praktikum/unsafe-toolkit/dummy_project including imports effectively makes up 1 packages
  1 of those contain unsafe usages

Couting occurances of unsafe.Pointer

Packages in green have no unsafe usages
Packages in red contain unsafe usages
Packages in white import packages with unsafe usages
antoniozhu@WS0075-21:/mnt/c/Users/antonio.zhu/Documents/repos/praktikum/unsafe-toolkit$ go-geiger $PWD/dummy_project/
  WITH DEPENDENCIES   LOCAL PACKAGE   PACKAGE PATH                                                                     
--------------------+---------------+----------------------------------------------------------------------------------
          0                 0         /mnt/c/Users/antonio.zhu/Documents/repos/praktikum/unsafe-toolkit/dummy_project  

Package /mnt/c/Users/antonio.zhu/Documents/repos/praktikum/unsafe-toolkit/dummy_project including imports effectively makes up 1 packages
  1 of those do not contain any unsafe usages

Couting occurances of unsafe.Pointer

Packages in green have no unsafe usages
Packages in red contain unsafe usages
Packages in white import packages with unsafe usages

I'm using the following software:
go version go1.13.8 linux/amd64
Ubuntu 20.04 on WSL2

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.