Skip to content

Commit

Permalink
[CWS] skip useless work in the ptracer if no handler is defined for t…
Browse files Browse the repository at this point in the history
…his syscall (#27737)
  • Loading branch information
paulcacheux authored Jul 19, 2024
1 parent dfaca62 commit 82230bb
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions pkg/security/ptracer/cws.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"os/exec"
"os/signal"
"path/filepath"
"slices"
"sync"
"syscall"
"time"
Expand Down Expand Up @@ -444,6 +445,11 @@ func ptrace(tracer *Tracer, probeAddr string, syscallHandlers map[int]syscallHan
}

cb := func(cbType CallbackType, nr int, pid int, ppid int, regs syscall.PtraceRegs, waitStatus *syscall.WaitStatus) {
handler, found := syscallHandlers[nr]
if !found && !slices.Contains([]int{ExecveNr, ExecveatNr, IoctlNr, CloneNr, Clone3Nr, ForkNr, VforkNr, ExitNr}, nr) {
return
}

process := pc.Get(pid)
if process == nil {
process = NewProcess(pid)
Expand Down Expand Up @@ -474,8 +480,7 @@ func ptrace(tracer *Tracer, probeAddr string, syscallHandlers map[int]syscallHan
process.Nr[nr] = syscallMsg
}

handler, found := syscallHandlers[nr]
if found && handler.Func != nil {
if handler.Func != nil {
err := handler.Func(tracer, process, syscallMsg, regs, opts.StatsDisabled)
if err != nil {
return
Expand Down Expand Up @@ -539,8 +544,7 @@ func ptrace(tracer *Tracer, probeAddr string, syscallHandlers map[int]syscallHan
}
case CallbackPostType:
syscallMsg, msgExists := process.Nr[nr]
handler, handlerFound := syscallHandlers[nr]
if handlerFound && msgExists && (handler.ShouldSend != nil || handler.RetFunc != nil) {
if msgExists {
if handler.RetFunc != nil {
err := handler.RetFunc(tracer, process, syscallMsg, regs, opts.StatsDisabled)
if err != nil {
Expand Down

0 comments on commit 82230bb

Please sign in to comment.