apparmor.go 898 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. // +build apparmor,linux
  2. package apparmor
  3. // #cgo LDFLAGS: -lapparmor
  4. // #include <sys/apparmor.h>
  5. // #include <stdlib.h>
  6. import "C"
  7. import (
  8. "io/ioutil"
  9. "os"
  10. "unsafe"
  11. )
  12. // IsEnabled returns true if apparmor is enabled for the host.
  13. func IsEnabled() bool {
  14. if _, err := os.Stat("/sys/kernel/security/apparmor"); err == nil && os.Getenv("container") == "" {
  15. if _, err = os.Stat("/sbin/apparmor_parser"); err == nil {
  16. buf, err := ioutil.ReadFile("/sys/module/apparmor/parameters/enabled")
  17. return err == nil && len(buf) > 1 && buf[0] == 'Y'
  18. }
  19. }
  20. return false
  21. }
  22. // ApplyProfile will apply the profile with the specified name to the process after
  23. // the next exec.
  24. func ApplyProfile(name string) error {
  25. if name == "" {
  26. return nil
  27. }
  28. cName := C.CString(name)
  29. defer C.free(unsafe.Pointer(cName))
  30. if _, err := C.aa_change_onexec(cName); err != nil {
  31. return err
  32. }
  33. return nil
  34. }