Skip to content

Commit b8596c9

Browse files
committed
Make cgroup-based policy filter mapping optional
By adding a command line argument (and the appropriate configmap option). Signed-off-by: Anastasios Papagiannis <[email protected]>
1 parent e747a09 commit b8596c9

File tree

14 files changed

+87
-34
lines changed

14 files changed

+87
-34
lines changed

cmd/tetra/debug/dump.go

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -200,18 +200,20 @@ func PolicyfilterState(fname string) {
200200
fmt.Printf("%d: %s\n", polId, strings.Join(ids, ","))
201201
}
202202

203-
fmt.Println("--- CgroupID to PolicyIDs mapping ---")
203+
if data.Cgroup != nil {
204+
fmt.Println("--- CgroupID to PolicyIDs mapping ---")
204205

205-
if len(data.Cgroup) == 0 {
206-
fmt.Printf("(empty)\n")
207-
}
206+
if len(data.Cgroup) == 0 {
207+
fmt.Printf("(empty)\n")
208+
}
208209

209-
for cgIDs, polIds := range data.Cgroup {
210-
ids := make([]string, 0, len(polIds))
211-
for id := range polIds {
212-
ids = append(ids, strconv.FormatUint(uint64(id), 10))
210+
for cgIDs, polIds := range data.Cgroup {
211+
ids := make([]string, 0, len(polIds))
212+
for id := range polIds {
213+
ids = append(ids, strconv.FormatUint(uint64(id), 10))
214+
}
215+
fmt.Printf("%d: %s\n", cgIDs, strings.Join(ids, ","))
213216
}
214-
fmt.Printf("%d: %s\n", cgIDs, strings.Join(ids, ","))
215217
}
216218
}
217219

docs/content/en/docs/reference/helm-chart.md

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/data/tetragon_flags.yaml

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

install/kubernetes/tetragon/README.md

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

install/kubernetes/tetragon/templates/tetragon_configmap.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ data:
5959
{{- if .Values.tetragon.enablePolicyFilter }}
6060
enable-policy-filter: "true"
6161
{{- end }}
62+
{{- if .Values.tetragon.enablePolicyFilterCgroupMap }}
63+
enable-policy-filter-cgroup-map: "true"
64+
{{- end }}
6265
{{- if .Values.tetragon.enablePolicyFilterDebug }}
6366
enable-policy-filter-debug: "true"
6467
{{- end }}

install/kubernetes/tetragon/values.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,8 @@ tetragon:
193193
port: 6060
194194
# -- Enable policy filter. This is required for K8s namespace and pod-label filtering.
195195
enablePolicyFilter: True
196+
# -- Enable policy filter cgroup map.
197+
enablePolicyFilterCgroupMap: false
196198
# -- Enable policy filter debug messages.
197199
enablePolicyFilterDebug: false
198200
# -- Enable latency monitoring in message handling

pkg/option/config.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,9 @@ type config struct {
7777

7878
ReleasePinned bool
7979

80-
EnablePolicyFilter bool
81-
EnablePolicyFilterDebug bool
80+
EnablePolicyFilter bool
81+
EnablePolicyFilterCgroupMap bool
82+
EnablePolicyFilterDebug bool
8283

8384
EnablePidSetFilter bool
8485

pkg/option/flags.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,9 @@ const (
8181

8282
KeyReleasePinnedBPF = "release-pinned-bpf"
8383

84-
KeyEnablePolicyFilter = "enable-policy-filter"
85-
KeyEnablePolicyFilterDebug = "enable-policy-filter-debug"
84+
KeyEnablePolicyFilter = "enable-policy-filter"
85+
KeyEnablePolicyFilterCgroupMap = "enable-policy-filter-cgroup-map"
86+
KeyEnablePolicyFilterDebug = "enable-policy-filter-debug"
8687

8788
KeyEnablePidSetFilter = "enable-pid-set-filter"
8889

@@ -202,6 +203,7 @@ func ReadAndSetFlags() error {
202203

203204
Config.ReleasePinned = viper.GetBool(KeyReleasePinnedBPF)
204205
Config.EnablePolicyFilter = viper.GetBool(KeyEnablePolicyFilter)
206+
Config.EnablePolicyFilterCgroupMap = viper.GetBool(KeyEnablePolicyFilterCgroupMap)
205207
Config.EnablePolicyFilterDebug = viper.GetBool(KeyEnablePolicyFilterDebug)
206208
Config.EnableMsgHandlingLatency = viper.GetBool(KeyEnableMsgHandlingLatency)
207209

@@ -378,6 +380,7 @@ func AddFlags(flags *pflag.FlagSet) {
378380
// Provide option to enable policy filtering. Because the code is new,
379381
// this is set to false by default.
380382
flags.Bool(KeyEnablePolicyFilter, false, "Enable policy filter code")
383+
flags.Bool(KeyEnablePolicyFilterCgroupMap, false, "Enable cgroup mappings for policy filter maps")
381384
flags.Bool(KeyEnablePolicyFilterDebug, false, "Enable policy filter debug messages")
382385

383386
// Provide option to enable the pidSet export filters.

pkg/policyfilter/k8s_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -739,7 +739,7 @@ func TestK8s(t *testing.T) {
739739

740740
// testState implements cgFinder
741741
ts := newTestState(client)
742-
st, err := newState(log, ts)
742+
st, err := newState(log, ts, true)
743743
if err != nil {
744744
t.Skipf("failed to initialize policy filter state: %s", err)
745745
}

pkg/policyfilter/map.go

Lines changed: 49 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ func openMap(spec *ebpf.CollectionSpec, mapName string, innerMaxEntries uint32)
6060
}
6161

6262
// newMap returns a new policy filter map.
63-
func newPfMap() (PfMap, error) {
63+
func newPfMap(enableCgroupMap bool) (PfMap, error) {
6464
// use the generic kprobe program, to find the policy filter map spec
6565
objName, _ := kernels.GenericKprobeObjs()
6666
objPath := path.Join(option.Config.HubbleLib, objName)
@@ -73,14 +73,23 @@ func newPfMap() (PfMap, error) {
7373
if ret.policyMap, err = openMap(spec, MapName, polMapSize); err != nil {
7474
return PfMap{}, fmt.Errorf("opening map %s failed: %w", MapName, err)
7575
}
76-
if ret.cgroupMap, err = openMap(spec, CgroupMapName, polMaxPolicies); err != nil {
77-
releaseMap(ret.policyMap)
78-
return PfMap{}, fmt.Errorf("opening cgroup map %s failed: %w", MapName, err)
76+
77+
if enableCgroupMap {
78+
if ret.cgroupMap, err = openMap(spec, CgroupMapName, polMaxPolicies); err != nil {
79+
releaseMap(ret.policyMap)
80+
return PfMap{}, fmt.Errorf("opening cgroup map %s failed: %w", MapName, err)
81+
}
7982
}
83+
8084
return ret, nil
8185
}
8286

8387
func releaseMap(m *ebpf.Map) error {
88+
// this may happen in the case where the cgroup map is not enabled
89+
if m == nil {
90+
return nil
91+
}
92+
8493
if err := m.Close(); err != nil {
8594
return err
8695
}
@@ -111,6 +120,11 @@ func (m polMap) addPolicyIDs(polID PolicyID, cgIDs []CgroupID) error {
111120
}
112121

113122
func addPolicyIDMapping(m *ebpf.Map, polID PolicyID, cgID CgroupID) error {
123+
// cgroup map does not exist, so nothing to do here
124+
if m == nil {
125+
return nil
126+
}
127+
114128
var id uint32
115129
err := m.Lookup(cgID, &id)
116130
if err == nil { // inner map exists
@@ -218,6 +232,11 @@ func getMapSize(m *ebpf.Map) (uint32, error) {
218232
}
219233

220234
func (m PfMap) deletePolicyIDInCgroupMap(polID PolicyID) error {
235+
// cgroup map does not exist, so nothing to do here
236+
if m.cgroupMap == nil {
237+
return nil
238+
}
239+
221240
var key CgroupID
222241
var id uint32
223242

@@ -315,9 +334,12 @@ func (m PfMap) readAll() (PfMapDump, error) {
315334
return PfMapDump{}, fmt.Errorf("error reading direct map: %w", err)
316335
}
317336

318-
r, err := readAll[CgroupID, PolicyID](m.cgroupMap)
319-
if err != nil {
320-
return PfMapDump{}, fmt.Errorf("error reading cgroup map: %w", err)
337+
var r map[CgroupID]map[PolicyID]struct{}
338+
if m.cgroupMap != nil {
339+
r, err = readAll[CgroupID, PolicyID](m.cgroupMap)
340+
if err != nil {
341+
return PfMapDump{}, fmt.Errorf("error reading cgroup map: %w", err)
342+
}
321343
}
322344

323345
return PfMapDump{Policy: d, Cgroup: r}, nil
@@ -362,6 +384,11 @@ func (m polMap) addCgroupIDs(cgIDs []CgroupID) error {
362384
// delCgroupIDs delete cgroups ids from the policy map
363385
// todo: use batch operations when supported
364386
func (m polMap) delCgroupIDs(polID PolicyID, cgIDs []CgroupID) error {
387+
// cgroup map does not exist, so nothing to do here
388+
if m.cgroupMap == nil {
389+
return nil
390+
}
391+
365392
rmRevCgIDs := []CgroupID{}
366393
for i, cgID := range cgIDs {
367394
if err := m.Inner.Delete(&cgID); err != nil {
@@ -425,21 +452,29 @@ func OpenMap(fname string) (PfMap, error) {
425452

426453
dir := filepath.Dir(fname)
427454
cgroupMapPath := filepath.Join(dir, CgroupMapName)
428-
r, err := ebpf.LoadPinnedMap(cgroupMapPath, &ebpf.LoadPinOptions{
429-
ReadOnly: true,
430-
})
431455

432-
if err != nil {
433-
d.Close()
434-
return PfMap{}, err
456+
// check if the cgroup map exists
457+
// the cgroup map may not exist in the case where
458+
// enable-policy-filter-cgroup-map is false
459+
var r *ebpf.Map
460+
if _, err := os.Stat(cgroupMapPath); err == nil {
461+
r, err = ebpf.LoadPinnedMap(cgroupMapPath, &ebpf.LoadPinOptions{
462+
ReadOnly: true,
463+
})
464+
if err != nil {
465+
d.Close()
466+
return PfMap{}, err
467+
}
435468
}
436469

437470
return PfMap{policyMap: d, cgroupMap: r}, err
438471
}
439472

440473
func (m PfMap) Close() {
441474
m.policyMap.Close()
442-
m.cgroupMap.Close()
475+
if m.cgroupMap != nil {
476+
m.cgroupMap.Close()
477+
}
443478
}
444479

445480
func (m PfMap) Dump() (PfMapDump, error) {

0 commit comments

Comments
 (0)