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

fix: use thread safe wrapper for ksyms table #3786

Merged
merged 1 commit into from
Jan 4, 2024
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
3 changes: 2 additions & 1 deletion pkg/ebpf/ksymbols.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/aquasecurity/tracee/pkg/errfmt"
"github.com/aquasecurity/tracee/pkg/events"
"github.com/aquasecurity/tracee/pkg/utils/ksyms"
)

var maxKsymNameLen = 64 // Most match the constant in the bpf code
Expand Down Expand Up @@ -39,7 +40,7 @@ func SendKsymbolsToMap(bpfKsymsMap *libbpfgo.BPFMap, ksymbols map[string]*helper

func (t *Tracee) NewKernelSymbols() error {
// reading kallsyms needs CAP_SYSLOG
kernelSymbols, err := helpers.NewLazyKernelSymbolsMap()
kernelSymbols, err := ksyms.NewSafeKsymbolTable()
if err != nil {
return errfmt.WrapError(err)
}
Expand Down
49 changes: 49 additions & 0 deletions pkg/utils/ksyms/safe_ksym_table.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package ksyms

import (
"sync"

"github.com/aquasecurity/libbpfgo/helpers"
)

type safeKsymbolTable struct {
helpers.KernelSymbolTable
l *sync.Mutex
AlonZivony marked this conversation as resolved.
Show resolved Hide resolved
}

// NewSafeKsymbolTable returns a safe wrapper for implementations of the KernelSymbolTable interface.
// The wrapper is needed because provided implementations do not guarentee thread-safety.
func NewSafeKsymbolTable() (*safeKsymbolTable, error) {
table, err := helpers.NewLazyKernelSymbolsMap()
if err != nil {
return nil, err
}

return &safeKsymbolTable{
table, new(sync.Mutex),
}, nil
}

func (t *safeKsymbolTable) TextSegmentContains(addr uint64) (bool, error) {
t.l.Lock()
defer t.l.Unlock()
return t.KernelSymbolTable.TextSegmentContains(addr)
}

func (t *safeKsymbolTable) GetSymbolByName(owner string, name string) (*helpers.KernelSymbol, error) {
t.l.Lock()
defer t.l.Unlock()
return t.KernelSymbolTable.GetSymbolByName(owner, name)
}

func (t *safeKsymbolTable) GetSymbolByAddr(addr uint64) (*helpers.KernelSymbol, error) {
t.l.Lock()
defer t.l.Unlock()
return t.KernelSymbolTable.GetSymbolByAddr(addr)
}

func (t *safeKsymbolTable) Refresh() error {
t.l.Lock()
defer t.l.Unlock()
return t.KernelSymbolTable.Refresh()
}
Loading