Skip to content

Commit 2f7596a

Browse files
committed
apparmor: do not save profile to /etc/apparmor.d
Writing the profile to /etc/apparmor.d, while also manually loading it into the kernel results in quite a bit of confusion. In addition, it means that people using apparmor but have /etc mounted read-only cannot use apparmor at all on a Docker host. Fix this by writing the profile to a temporary directory and deleting it after it's been inserted. Signed-off-by: Aleksa Sarai <[email protected]>
1 parent 6fafd07 commit 2f7596a

File tree

2 files changed

+12
-13
lines changed

2 files changed

+12
-13
lines changed

pkg/aaparser/aaparser.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@ func GetVersion() (int, error) {
2323
return parseVersion(output)
2424
}
2525

26-
// LoadProfile runs `apparmor_parser -r -W` on a specified apparmor profile to
27-
// replace and write it to disk.
26+
// LoadProfile runs `apparmor_parser -r` on a specified apparmor profile to
27+
// replace the profile.
2828
func LoadProfile(profilePath string) error {
29-
_, err := cmd(filepath.Dir(profilePath), "-r", "-W", filepath.Base(profilePath))
29+
_, err := cmd("-r", filepath.Dir(profilePath))
3030
if err != nil {
3131
return err
3232
}

profiles/apparmor/apparmor.go

+9-10
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package apparmor
55
import (
66
"bufio"
77
"io"
8+
"io/ioutil"
89
"os"
910
"path"
1011
"strings"
@@ -16,8 +17,6 @@ import (
1617
var (
1718
// profileDirectory is the file store for apparmor profiles and macros.
1819
profileDirectory = "/etc/apparmor.d"
19-
// defaultProfilePath is the default path for the apparmor profile to be saved.
20-
defaultProfilePath = path.Join(profileDirectory, "docker")
2120
)
2221

2322
// profileData holds information about the given profile for generation.
@@ -70,26 +69,26 @@ func macroExists(m string) bool {
7069
// InstallDefault generates a default profile and installs it in the
7170
// ProfileDirectory with `apparmor_parser`.
7271
func InstallDefault(name string) error {
73-
// Make sure the path where they want to save the profile exists
74-
if err := os.MkdirAll(profileDirectory, 0755); err != nil {
75-
return err
76-
}
77-
7872
p := profileData{
7973
Name: name,
8074
}
8175

82-
f, err := os.OpenFile(defaultProfilePath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
76+
// Install to a temporary directory.
77+
f, err := ioutil.TempFile("", name)
8378
if err != nil {
8479
return err
8580
}
81+
profilePath := f.Name()
82+
83+
defer f.Close()
84+
defer os.Remove(profilePath)
85+
8686
if err := p.generateDefault(f); err != nil {
8787
f.Close()
8888
return err
8989
}
90-
f.Close()
9190

92-
if err := aaparser.LoadProfile(defaultProfilePath); err != nil {
91+
if err := aaparser.LoadProfile(profilePath); err != nil {
9392
return err
9493
}
9594

0 commit comments

Comments
 (0)