Skip to content

Commit eddcece

Browse files
committed
spoc: abort apparmor recording if BPF LSM is not enabled
1 parent 5a1e2a3 commit eddcece

File tree

3 files changed

+30
-5
lines changed

3 files changed

+30
-5
lines changed

internal/pkg/cli/recorder/recorder.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ package recorder
2222
import (
2323
"context"
2424
"encoding/binary"
25+
"errors"
2526
"fmt"
2627
"io"
2728
"log"
@@ -78,6 +79,13 @@ func (r *Recorder) Run() error {
7879
(r.options.typ == TypeRawSeccomp) ||
7980
(r.options.typ == TypeAll))
8081

82+
// https://github.com/kubernetes-sigs/security-profiles-operator/issues/2384
83+
// Explicitly check for BPF LSM support as the recorder fails silently
84+
// to support seccomp-only use cases.
85+
if recordAppArmor && !bpfrecorder.BPFLSMEnabled() {
86+
return errors.New("BPF LSM is not enabled for this kernel")
87+
}
88+
8189
r.bpfRecorder = bpfrecorder.New(
8290
r.options.commandOptions.Command(),
8391
logr.New(&cli.LogSink{}),

internal/pkg/daemon/bpfrecorder/bpfrecorder.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -476,6 +476,8 @@ func (b *BpfRecorder) Load(startEventProcessor bool) (err error) {
476476
// Only log an error here, if Apparmor cannot be loaded. This is because it is
477477
// enabled by default, and there are Linux distributions which either do not
478478
// support Apparmor or BPF LSM is not yet available.
479+
//
480+
// see also https://github.com/kubernetes-sigs/security-profiles-operator/issues/2384
479481
b.logger.Error(err, "Loading bpf program")
480482
}
481483
}

test/spoc/e2e_spoc_test.go

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -196,14 +196,29 @@ func recordAppArmorTest(t *testing.T) {
196196

197197
require.Contains(t, stdout.String(), "allowTcp", "did not find TCP permission in profile")
198198
})
199+
t.Run("unsupported", func(t *testing.T) {
200+
if bpfrecorder.BPFLSMEnabled() {
201+
t.Skip("BPF LSM enabled")
202+
}
203+
_, err := runSpoc(
204+
t,
205+
"record",
206+
"-t",
207+
"apparmor",
208+
"-o",
209+
"/dev/stdout",
210+
"./demobinary",
211+
)
212+
require.Error(t, err)
213+
})
199214
}
200215

201216
func recordSeccompTest(t *testing.T) {
202217
profile := recordSeccomp(t, "--net-tcp")
203218
require.Contains(t, profile.Syscalls[0].Names, "listen")
204219
}
205220

206-
func runSpoc(t *testing.T, args ...string) []byte {
221+
func runSpoc(t *testing.T, args ...string) ([]byte, error) {
207222
t.Helper()
208223
args = append([]string{spocPath}, args...)
209224
cmd := exec.Command(
@@ -212,17 +227,17 @@ func runSpoc(t *testing.T, args ...string) []byte {
212227
)
213228
cmd.Stderr = os.Stderr
214229
out, err := cmd.Output()
215-
require.NoError(t, err, "failed to run spoc")
216-
return out
230+
return out, err
217231
}
218232

219233
func record(t *testing.T, typ string, profile client.Object, args ...string) {
220234
t.Helper()
221235
args = append([]string{
222236
"record", "-t", typ, "-o", "/dev/stdout", "--no-base-syscalls", "./demobinary",
223237
}, args...)
224-
content := runSpoc(t, args...)
225-
err := yaml.Unmarshal(content, &profile)
238+
content, err := runSpoc(t, args...)
239+
require.NoError(t, err, "failed to run spoc")
240+
err = yaml.Unmarshal(content, &profile)
226241
require.NoError(t, err, "failed to parse yaml")
227242
}
228243

0 commit comments

Comments
 (0)