25
"github.com/cilium/ebpf"
26
"github.com/cilium/ebpf/link"
27
"github.com/cilium/ebpf/perf"
29
gadgetcontext "github.com/inspektor-gadget/inspektor-gadget/pkg/gadget-context"
30
"github.com/inspektor-gadget/inspektor-gadget/pkg/gadgets"
31
"github.com/inspektor-gadget/inspektor-gadget/pkg/gadgets/audit/seccomp/types"
32
eventtypes "github.com/inspektor-gadget/inspektor-gadget/pkg/types"
39
enricher gadgets.DataEnricherByMntNs
40
eventCallback func(*types.Event)
42
objs auditseccompObjects
56
func NewTracer(config *Config, enricher gadgets.DataEnricherByMntNs,
57
eventCallback func(*types.Event),
62
eventCallback: eventCallback,
65
if err := t.install(); err != nil {
75
func (t *Tracer) install() error {
76
spec, err := loadAuditseccomp()
78
return fmt.Errorf("loading ebpf program: %w", err)
81
if err := gadgets.LoadeBPFSpec(t.config.MountnsMap, spec, nil, &t.objs); err != nil {
82
return fmt.Errorf("loading ebpf spec: %w", err)
85
t.reader, err = perf.NewReader(t.objs.Events, gadgets.PerfBufferPages*os.Getpagesize())
87
return fmt.Errorf("getting a perf reader: %w", err)
90
t.progLink, err = link.Kprobe("audit_seccomp", t.objs.IgAuditSecc, nil)
92
return fmt.Errorf("attaching kprobe: %w", err)
98
func (t *Tracer) run() {
100
record, err := t.reader.Read()
102
if errors.Is(err, perf.ErrClosed) {
107
msg := fmt.Sprintf("Error reading perf ring buffer: %s", err)
108
t.eventCallback(types.Base(eventtypes.Err(msg)))
112
if record.LostSamples > 0 {
113
msg := fmt.Sprintf("lost %d samples", record.LostSamples)
114
t.eventCallback(types.Base(eventtypes.Warn(msg)))
118
eventC := (*auditseccompEvent)(unsafe.Pointer(&record.RawSample[0]))
120
event := types.Event{
121
Event: eventtypes.Event{
122
Type: eventtypes.NORMAL,
123
Timestamp: gadgets.WallTimeFromBootTime(eventC.Timestamp),
125
Pid: uint32(eventC.Pid),
126
WithMountNsID: eventtypes.WithMountNsID{MountNsID: eventC.MntnsId},
127
Syscall: syscallToName(int(eventC.Syscall)),
128
Code: codeToName(uint(eventC.Code)),
129
Comm: gadgets.FromCString(eventC.Comm[:]),
132
if t.enricher != nil {
133
t.enricher.EnrichByMntNs(&event.CommonData, event.MountNsID)
136
t.eventCallback(&event)
142
func (t *Tracer) Close() {
143
t.progLink = gadgets.CloseLink(t.progLink)
152
func (t *Tracer) Run(gadgetCtx gadgets.GadgetContext) error {
154
if err := t.install(); err != nil {
155
return fmt.Errorf("installing tracer: %w", err)
159
gadgetcontext.WaitForTimeoutOrDone(gadgetCtx)
164
func (t *Tracer) SetMountNsMap(mountnsMap *ebpf.Map) {
165
t.config.MountnsMap = mountnsMap
168
func (t *Tracer) SetEventHandler(handler any) {
169
nh, ok := handler.(func(ev *types.Event))
171
panic("event handler invalid")
176
func (g *GadgetDesc) NewInstance() (gadgets.Gadget, error) {