Skip to content

Commit fa50b72

Browse files
authored
Merge pull request #11 from moby/apparmor_linting
apparmor: use "filepath" instead of "path", and fix linting (gosec)
2 parents 935f56c + 536f988 commit fa50b72

File tree

2 files changed

+11
-9
lines changed

2 files changed

+11
-9
lines changed

apparmor/apparmor.go

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
"io"
1212
"os"
1313
"os/exec"
14-
"path"
14+
"path/filepath"
1515
"strings"
1616
"text/template"
1717
)
@@ -53,7 +53,7 @@ func (p *profileData) generateDefault(out io.Writer) error {
5353

5454
// macroExists checks if the passed macro exists.
5555
func macroExists(m string) bool {
56-
_, err := os.Stat(path.Join(profileDirectory, m))
56+
_, err := os.Stat(filepath.Join(profileDirectory, m))
5757
return err == nil
5858
}
5959

@@ -63,7 +63,7 @@ func InstallDefault(name string) error {
6363
// Figure out the daemon profile.
6464
daemonProfile := "unconfined"
6565
if currentProfile, err := os.ReadFile("/proc/self/attr/current"); err == nil {
66-
// Normally profiles are suffixed by " (enforcing)" or similar. AppArmor
66+
// Normally profiles are suffixed by " (enforce)" or similar. AppArmor
6767
// profiles cannot contain spaces so this doesn't restrict daemon profile
6868
// names.
6969
if profile, _, _ := strings.Cut(string(currentProfile), " "); profile != "" {
@@ -72,14 +72,14 @@ func InstallDefault(name string) error {
7272
}
7373

7474
// Install to a temporary directory.
75-
tmpFile, err := os.CreateTemp("", name)
75+
tmpFile, err := os.CreateTemp("", "apparmor-profile-")
7676
if err != nil {
7777
return err
7878
}
7979

8080
defer func() {
8181
_ = tmpFile.Close()
82-
_ = os.Remove(tmpFile.Name())
82+
_ = os.Remove(tmpFile.Name()) // #nosec G703 -- ignore "G703: Path traversal via taint analysis (gosec)"
8383
}()
8484

8585
p := profileData{
@@ -110,6 +110,9 @@ func isLoaded(name string, fileName string) (bool, error) {
110110

111111
scanner := bufio.NewScanner(file)
112112
for scanner.Scan() {
113+
// Normally profiles are suffixed by " (enforce)" or similar. AppArmor
114+
// profiles cannot contain spaces so this doesn't restrict daemon profile
115+
// names.
113116
if prefix, _, ok := strings.Cut(scanner.Text(), " "); ok && prefix == name {
114117
return true, nil
115118
}
@@ -126,7 +129,7 @@ func isLoaded(name string, fileName string) (bool, error) {
126129
// replace the profile. The `-K` is necessary to make sure that apparmor_parser
127130
// doesn't try to write to a read-only filesystem.
128131
func loadProfile(profilePath string) error {
129-
c := exec.Command("apparmor_parser", "-Kr", profilePath)
132+
c := exec.Command("apparmor_parser", "-Kr", profilePath) // #nosec G204 G702 -- Ignore "Subprocess launched with variable (gosec)"
130133
c.Dir = ""
131134

132135
if output, err := c.CombinedOutput(); err != nil {

apparmor/apparmor_linux_test.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ package apparmor
66
import (
77
"errors"
88
"os"
9-
"path"
109
"path/filepath"
1110
"strings"
1211
"testing"
@@ -139,7 +138,7 @@ Discord (unconfined)
139138

140139
func TestIsLoaded(t *testing.T) {
141140
tmpDir := t.TempDir()
142-
profiles := path.Join(tmpDir, "apparmor_profiles")
141+
profiles := filepath.Join(tmpDir, "apparmor_profiles")
143142
if err := os.WriteFile(profiles, []byte(testAppArmorProfiles), 0o644); err != nil {
144143
t.Fatal(err)
145144
}
@@ -162,7 +161,7 @@ func TestIsLoaded(t *testing.T) {
162161
}
163162
})
164163
t.Run("error", func(t *testing.T) {
165-
_, err := isLoaded("anything", path.Join(tmpDir, "no_such_file"))
164+
_, err := isLoaded("anything", filepath.Join(tmpDir, "no_such_file"))
166165
if err == nil || !errors.Is(err, os.ErrNotExist) {
167166
t.Fatalf("expected error to be os.ErrNotExist, got %v", err)
168167
}

0 commit comments

Comments
 (0)