Skip to content
Open
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
25 changes: 22 additions & 3 deletions internal/pkg/daemon/enricher/jsonenricher.go
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,8 @@ func (e *JsonEnricher) Run(ctx context.Context, runErr chan<- error) {
}

// Capture proc/pid/(cmdLine/environ) early; these files are ephemeral on some OS (e.g., Ubuntu).
logBucket.Mu.Lock()

if logBucket.ProcessInfo == nil {
uid, gid, err := auditsource.GetUidGid(line)
if err != nil {
Expand All @@ -324,12 +326,18 @@ func (e *JsonEnricher) Run(ctx context.Context, runErr chan<- error) {
auditLine.Executable, uid, gid)
}

logBucket.Mu.Unlock()
Comment on lines 316 to +329
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same concern as the fetchContainerInfo block below: the lock wraps both the nil check and fetchProcessInfo, which reads /proc files. Consider fetching outside the lock and only locking for the pointer assignment:

Suggested change
if logBucket.ProcessInfo == nil {
uid, gid, err := auditsource.GetUidGid(line)
if err != nil {
e.logger.V(config.VerboseLevel).Info(
"unable to get uid and gid", "line", line)
}
pi := e.fetchProcessInfo(auditLine.ProcessID,
auditLine.Executable, uid, gid)
logBucket.Mu.Lock()
logBucket.ProcessInfo = pi
logBucket.Mu.Unlock()
}

This eliminates the lock-unlock-lock-unlock pattern in Run(), keeping locks to short pointer assignments only.


e.processEbpf(logBucket, auditLine)

logBucket.Mu.Lock()

if logBucket.ContainerInfo == nil {
logBucket.ContainerInfo = e.fetchContainerInfo(ctx, auditLine.ProcessID, nodeName)
}

logBucket.Mu.Unlock()
Comment on lines +333 to +339
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fetchContainerInfo calls into the Kubernetes API. Holding the write lock during a network call blocks dispatchSeccompLine from dispatching any logs for this bucket until the API responds.

Consider narrowing the lock to just the pointer assignment:

Suggested change
logBucket.Mu.Lock()
if logBucket.ContainerInfo == nil {
logBucket.ContainerInfo = e.fetchContainerInfo(ctx, auditLine.ProcessID, nodeName)
}
logBucket.Mu.Unlock()
if logBucket.ContainerInfo == nil {
ci := e.fetchContainerInfo(ctx, auditLine.ProcessID, nodeName)
logBucket.Mu.Lock()
logBucket.ContainerInfo = ci
logBucket.Mu.Unlock()
}


logBucket.SyscallIds.LoadOrStore(auditLine.SystemCallID, struct{}{})

if !e.logLinesCache.Has(auditLine.ProcessID) {
Expand All @@ -341,6 +349,9 @@ func (e *JsonEnricher) Run(ctx context.Context, runErr chan<- error) {
}

func (e *JsonEnricher) processEbpf(logBucket *types.LogBucket, auditLine *types.AuditLine) {
logBucket.Mu.Lock()
defer logBucket.Mu.Unlock()

if e.bpfProcessCache != nil && logBucket.ProcessInfo != nil && logBucket.ProcessInfo.CmdLine == "" {
cmdLine, errCmdLine := e.bpfProcessCache.GetCmdLine(auditLine.ProcessID)
if errCmdLine == nil {
Expand Down Expand Up @@ -435,6 +446,10 @@ func (e *JsonEnricher) dispatchSeccompLine(
return true
})

// Acquire read lock to safely access ProcessInfo and ContainerInfo
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment restates what the code does. logBucket.Mu.RLock() is self-explanatory.

Suggested change
// Acquire read lock to safely access ProcessInfo and ContainerInfo

logBucket.Mu.RLock()
defer logBucket.Mu.RUnlock()

var resource map[string]string

if logBucket.ProcessInfo == nil {
Expand All @@ -448,10 +463,14 @@ func (e *JsonEnricher) dispatchSeccompLine(
}

if logBucket.ContainerInfo != nil {
Comment thread
BhargaviGudi marked this conversation as resolved.
podName := logBucket.ContainerInfo.PodName
namespace := logBucket.ContainerInfo.Namespace
containerName := logBucket.ContainerInfo.ContainerName

resource = map[string]string{
"pod": logBucket.ContainerInfo.PodName,
"namespace": logBucket.ContainerInfo.Namespace,
"container": logBucket.ContainerInfo.ContainerName,
"pod": podName,
"namespace": namespace,
"container": containerName,
}
Comment on lines +466 to 474
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These local variables add no safety since the RLock is already held. They make it look like the copies are the fix, when the lock is. The original direct access is clearer:

Suggested change
podName := logBucket.ContainerInfo.PodName
namespace := logBucket.ContainerInfo.Namespace
containerName := logBucket.ContainerInfo.ContainerName
resource = map[string]string{
"pod": logBucket.ContainerInfo.PodName,
"namespace": logBucket.ContainerInfo.Namespace,
"container": logBucket.ContainerInfo.ContainerName,
"pod": podName,
"namespace": namespace,
"container": containerName,
}
resource = map[string]string{
"pod": logBucket.ContainerInfo.PodName,
"namespace": logBucket.ContainerInfo.Namespace,
"container": logBucket.ContainerInfo.ContainerName,
}

}

Expand Down
1 change: 1 addition & 0 deletions internal/pkg/daemon/enricher/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ type ProcessInfo struct {
ExecRequestId *string
}
type LogBucket struct {
Mu sync.RWMutex
SyscallIds sync.Map
ContainerInfo *ContainerInfo
ProcessInfo *ProcessInfo
Expand Down
Loading