22
33package apparmor
44
5- // #cgo LDFLAGS: -lapparmor
6- // #include <sys/apparmor.h>
7- // #include <stdlib.h>
8- import "C"
95import (
106 "fmt"
117 "io/ioutil"
128 "os"
13- "unsafe"
149)
1510
1611// IsEnabled returns true if apparmor is enabled for the host.
@@ -24,16 +19,36 @@ func IsEnabled() bool {
2419 return false
2520}
2621
22+ func setprocattr (attr , value string ) error {
23+ // Under AppArmor you can only change your own attr, so use /proc/self/
24+ // instead of /proc/<tid>/ like libapparmor does
25+ path := fmt .Sprintf ("/proc/self/attr/%s" , attr )
26+
27+ f , err := os .OpenFile (path , os .O_WRONLY , 0 )
28+ if err != nil {
29+ return err
30+ }
31+ defer f .Close ()
32+
33+ _ , err = fmt .Fprintf (f , "%s" , value )
34+ return err
35+ }
36+
37+ // changeOnExec reimplements aa_change_onexec from libapparmor in Go
38+ func changeOnExec (name string ) error {
39+ value := "exec " + name
40+ if err := setprocattr ("exec" , value ); err != nil {
41+ return fmt .Errorf ("apparmor failed to apply profile: %s" , err )
42+ }
43+ return nil
44+ }
45+
2746// ApplyProfile will apply the profile with the specified name to the process after
2847// the next exec.
2948func ApplyProfile (name string ) error {
3049 if name == "" {
3150 return nil
3251 }
33- cName := C .CString (name )
34- defer C .free (unsafe .Pointer (cName ))
35- if _ , err := C .aa_change_onexec (cName ); err != nil {
36- return fmt .Errorf ("apparmor failed to apply profile: %s" , err )
37- }
38- return nil
52+
53+ return changeOnExec (name )
3954}
0 commit comments