Giter VIP home page Giter VIP logo

go-apparmor's Introduction

go-apparmor

Go Reference

AppArmor 3.x kernel interface bindings written in pure Go.

Examples

This package provides three levels of abstraction for transitioning between hats:

Fully Managed using apparmor.WithHat()

The following example runs a function in a confined hat environment.

All necessary setup and teardown of Go the goroutine runtime is handled by the package.

import (
    "github.com/eternal-flame-AD/go-apparmor/apparmor"
	"github.com/eternal-flame-AD/go-apparmor/apparmor/magic"
)

func ExampleWithHat() {
	// setup a place to store the magic key for transitioning back to the original hat
	keyRing, err := magic.NewKeyring(nil)
	if err != nil {
		log.Fatalf("failed to initialize kernel keyring: %v", err)
	}
	if generatedMagic, err := magic.Generate(nil); err != nil {
		log.Fatalf("failed to generate magic key: %v", err)
	} else if err := keyRing.Set(generatedMagic); err != nil {
		log.Fatalf("failed to store magic key: %v", err)
	}
	getMagic := func() uint64 {
		ret, err := keyRing.Get()
		if err != nil {
			log.Panicf("failed to get magic key: %v", err)
		}
		return ret
	}

	apparmor.WithHat("confined_hat", getMagic, func() {
		// This code is running in the confined_hat hat.

		// to spawn a goroutine:
		go apparmor.WithHat("confined_hat", getMagic, func() {
			// This code is ensured to be running in the confined_hat hat as well.
		})

	})
}

Managed Setup using apparmor.SetHat()

The following example transitions a goroutine into a confined hat environment. The caller is responsible for cleaning up by transitioning back to the original hat or killing the thread by returning without calling runtime.UnlockOSThread().

func ExampleSetHat() {
	// setup a place to store the magic key for transitioning back to the original hat
	keyRing, err := magic.NewKeyring(nil)
	if err != nil {
		log.Fatalf("failed to initialize kernel keyring: %v", err)
	}
	if generatedMagic, err := magic.Generate(nil); err != nil {
		log.Fatalf("failed to generate magic key: %v", err)
	} else if err := keyRing.Set(generatedMagic); err != nil {
		log.Fatalf("failed to store magic key: %v", err)
	}
	getMagic := func() uint64 {
		ret, err := keyRing.Get()
		if err != nil {
			log.Panicf("failed to get magic key: %v", err)
		}
		return ret
	}
	go func() {
		apparmor.SetHat("confined_hat", getMagic())

		// This code is running in the confined_hat hat.
		// it is NOT safe to spawn goroutines without additional setup here.

		// if you are able to transition the thread back to the original state,
		// call runtime.UnlockOSThread() as many times as you called apparmor.SetHat().
		// otherwise, the underlying OS thread will not be reused and will be killed by the scheduler.
	}()
}

Unmanaged Using libapparmor style functions

Functions that begin with AA are functional replicas of their respective libapparmor C API. That means the caller is responsible for setting up the goroutine runtime so that the OS thread that gets modified is scheduled to the desired goroutine(s), this usually boils down to:

  • Locking the thread to the calling goroutine with runtime.LockOSThread()
  • Make sure that additional goroutines that are spawned are also locked to a transitioned thread.
  • Determining whether the thread could be reused by calling runtime.UnlockOSThread() after transitioning the thread back to its original state.
func getConfinement() {
    label, mode, err := apparmor.AAGetCon()
    if err != nil {
        log.Fatalf("failed to get confinement: %v, do we have introspect permissions?", err)
    }
    log.Printf("confinement: %s, mode: %s", label, mode)
    // example: confinement: /usr/bin/program, mode: enforce
}

func transitionToSubprofile() {
    go func() {
        runtime.LockOSThread()
        // IMPORTANT: do not unlock the thread unless you can transition the OS thread
        // back to its original profile. Otherwise, other goroutines may be confined
        // to the subprofile as well.

        apparmor.AAChangeProfile("sub-profile")

        exec.Command("cat", "/proc/self/attr/apparmor/current").Run()
        // example: .../sub-profile
    }()
   
}

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.