Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tetra policyfilter command #1639

Merged
merged 2 commits into from
Oct 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions cmd/tetra/commands_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package main
import (
"github.com/cilium/tetragon/cmd/tetra/bugtool"
"github.com/cilium/tetragon/cmd/tetra/dump"
"github.com/cilium/tetragon/cmd/tetra/policyfilter"
"github.com/cilium/tetragon/cmd/tetra/tracingpolicy"
"github.com/spf13/cobra"
)
Expand All @@ -15,4 +16,5 @@ func addCommands(rootCmd *cobra.Command) {
rootCmd.AddCommand(bugtool.New())
rootCmd.AddCommand(tracingpolicy.New())
rootCmd.AddCommand(dump.New())
rootCmd.AddCommand(policyfilter.New())
}
5 changes: 3 additions & 2 deletions cmd/tetra/dump/dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func policyfilterCmd() *cobra.Command {
Short: "dump policyfilter state",
Args: cobra.ExactArgs(0),
Run: func(cmd *cobra.Command, _ []string) {
dumpPolicyfilterState(mapFname)
PolicyfilterState(mapFname)
},
}

Expand Down Expand Up @@ -101,12 +101,13 @@ func dumpExecveMap(fname string) {
}
}

func dumpPolicyfilterState(fname string) {
func PolicyfilterState(fname string) {
m, err := policyfilter.OpenMap(fname)
if err != nil {
logger.GetLogger().WithError(err).Fatal("Failed to open policyfilter map")
return
}
defer m.Close()

data, err := m.Dump()
if err != nil {
Expand Down
101 changes: 101 additions & 0 deletions cmd/tetra/policyfilter/policyfilter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
// SPDX-License-Identifier: Apache-2.0
// Copyright Authors of Tetragon

package policyfilter

import (
"path/filepath"
"strconv"

"github.com/cilium/tetragon/cmd/tetra/dump"
"github.com/cilium/tetragon/pkg/cgroups"
"github.com/cilium/tetragon/pkg/defaults"
"github.com/cilium/tetragon/pkg/logger"
"github.com/cilium/tetragon/pkg/policyfilter"
"github.com/spf13/cobra"
)

func New() *cobra.Command {
ret := &cobra.Command{
Use: "policyfilter",
Short: "manage policyfilter map (only for debugging)",
Hidden: true,
SilenceUsage: true,
}

ret.AddCommand(
dumpCmd(),
addCommand(),
)

return ret
}

func dumpCmd() *cobra.Command {
mapFname := filepath.Join(defaults.DefaultMapRoot, defaults.DefaultMapPrefix, policyfilter.MapName)
ret := &cobra.Command{
Use: "dump",
Short: "dump policyfilter state",
Args: cobra.ExactArgs(0),
Run: func(cmd *cobra.Command, _ []string) {
dump.PolicyfilterState(mapFname)
},
}

flags := ret.Flags()
flags.StringVar(&mapFname, "map-fname", mapFname, "policyfilter map filename")
return ret
}

func addCommand() *cobra.Command {
var argType string
mapFname := filepath.Join(defaults.DefaultMapRoot, defaults.DefaultMapPrefix, policyfilter.MapName)
ret := &cobra.Command{
Use: "add [policy id] [cgroup]",
Short: "add policyfilter entry",
Args: cobra.ExactArgs(2),
Run: func(cmd *cobra.Command, args []string) {
x, err := strconv.ParseUint(args[0], 10, 32)
if err != nil {
logger.GetLogger().WithError(err).Fatal("Failed to parse policy id")
}
polID := policyfilter.PolicyID(x)

var cgID uint64
switch argType {
case "file":
cgID, err = cgroups.GetCgroupIdFromPath(args[1])
case "id":
cgID, err = strconv.ParseUint(args[1], 10, 32)
default:
logger.GetLogger().WithField("type", argType).WithError(err).Fatal("Unknown type")
}

if err != nil {
logger.GetLogger().WithError(err).Fatal("Failed to parse cgroup")
}

addCgroup(mapFname, polID, policyfilter.CgroupID(cgID))
},
}

flags := ret.Flags()
flags.StringVar(&argType, "arg-type", "file", "cgroup type (id,file)")
flags.StringVar(&mapFname, "map-fname", mapFname, "policyfilter map filename")
return ret
}

func addCgroup(fname string, polID policyfilter.PolicyID, cgID policyfilter.CgroupID) {
m, err := policyfilter.OpenMap(fname)
if err != nil {
logger.GetLogger().WithError(err).Fatal("Failed to open policyfilter map")
return
}
defer m.Close()

err = m.AddCgroup(polID, cgID)
if err != nil {
logger.GetLogger().WithError(err).Fatal("Failed to add cgroup id")
}

}
21 changes: 21 additions & 0 deletions pkg/policyfilter/map.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,3 +222,24 @@ func OpenMap(fname string) (PfMap, error) {
func (m PfMap) Dump() (map[PolicyID]map[CgroupID]struct{}, error) {
return m.readAll()
}

func (m PfMap) AddCgroup(polID PolicyID, cgID CgroupID) error {
var innerID uint32

if err := m.Lookup(&polID, &innerID); err != nil {
return fmt.Errorf("failed to lookup policy id %d: %w", polID, err)
}

inMap, err := ebpf.NewMapFromID(ebpf.MapID(innerID))
if err != nil {
return fmt.Errorf("error opening inner map: %w", err)
}
defer inMap.Close()

val := uint8(0)
if err := inMap.Update(&cgID, &val, ebpf.UpdateAny); err != nil {
return fmt.Errorf("error updating inner map: %w", err)
}

return nil
}