Skip to content

Commit 7f24b40

Browse files
Merge pull request #1675 from tklauser/apparmor-no-cgo
RFC: libcontainer: remove dependency on libapparmor
2 parents c6e4a1e + db093f6 commit 7f24b40

File tree

3 files changed

+28
-13
lines changed

3 files changed

+28
-13
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ env:
2323
before_install:
2424
- echo "deb http://archive.ubuntu.com/ubuntu trusty-backports main restricted universe multiverse" | sudo tee -a /etc/apt/sources.list
2525
- sudo apt-get -qq update
26-
- sudo apt-get install -y libapparmor-dev libseccomp-dev/trusty-backports
26+
- sudo apt-get install -y libseccomp-dev/trusty-backports
2727
- go get -u github.com/golang/lint/golint
2828
- go get -u github.com/vbatts/git-validation
2929
- env | grep TRAVIS_

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ make BUILDTAGS='seccomp apparmor'
5656
|-----------|------------------------------------|-------------|
5757
| seccomp | Syscall filtering | libseccomp |
5858
| selinux | selinux process and mount labeling | <none> |
59-
| apparmor | apparmor profile support | libapparmor |
59+
| apparmor | apparmor profile support | <none> |
6060
| ambient | ambient capability support | kernel 4.3 |
6161

6262

libcontainer/apparmor/apparmor.go

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,10 @@
22

33
package apparmor
44

5-
// #cgo LDFLAGS: -lapparmor
6-
// #include <sys/apparmor.h>
7-
// #include <stdlib.h>
8-
import "C"
95
import (
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.
2948
func 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

Comments
 (0)