Skip to content
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: 1 addition & 1 deletion pkg/rules/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ func (e *Engine) Compile() (*config.RulesCompileResult, error) {
for c, f := range filters {
var ss *sequenceState
if f.IsSequence() {
ss = newSequenceState(f, c)
ss = newSequenceState(f, c, e.psnap)
}
fltr := newCompiledFilter(f, c, ss)
if ss != nil {
Expand Down
10 changes: 9 additions & 1 deletion pkg/rules/sequence.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"github.com/rabbitstack/fibratus/pkg/kevent"
"github.com/rabbitstack/fibratus/pkg/kevent/kparams"
"github.com/rabbitstack/fibratus/pkg/kevent/ktypes"
"github.com/rabbitstack/fibratus/pkg/ps"
"github.com/rabbitstack/fibratus/pkg/util/atomic"
log "github.com/sirupsen/logrus"
"sort"
Expand Down Expand Up @@ -114,9 +115,11 @@ type sequenceState struct {
states map[fsm.State]bool
// smu guards the states map
smu sync.RWMutex

psnap ps.Snapshotter
}

func newSequenceState(f filter.Filter, c *config.FilterConfig) *sequenceState {
func newSequenceState(f filter.Filter, c *config.FilterConfig, psnap ps.Snapshotter) *sequenceState {
ss := &sequenceState{
filter: f,
seq: f.GetSequence(),
Expand All @@ -129,6 +132,7 @@ func newSequenceState(f filter.Filter, c *config.FilterConfig) *sequenceState {
spanDeadlines: make(map[fsm.State]*time.Timer),
initialState: sequenceInitialState,
inDeadline: atomic.MakeBool(false),
psnap: psnap,
}

ss.initFSM()
Expand Down Expand Up @@ -480,6 +484,10 @@ func (s *sequenceState) runSequence(e *kevent.Kevent) bool {
if !evt.ContainsMeta(kevent.RuleSequenceOOOKey) {
continue
}
// try to initialize process state before evaluating the event
if evt.PS == nil {
_, evt.PS = s.psnap.Find(evt.PID)
}
matches = s.filter.RunSequence(evt, seqID, s.partials, false)
// transition the state machine
if matches {
Expand Down
23 changes: 12 additions & 11 deletions pkg/rules/sequence_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"github.com/rabbitstack/fibratus/pkg/kevent"
"github.com/rabbitstack/fibratus/pkg/kevent/kparams"
"github.com/rabbitstack/fibratus/pkg/kevent/ktypes"
"github.com/rabbitstack/fibratus/pkg/ps"
pstypes "github.com/rabbitstack/fibratus/pkg/ps/types"
log "github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
Expand All @@ -50,7 +51,7 @@ func TestSequenceState(t *testing.T) {

require.NoError(t, f.Compile())

ss := newSequenceState(f, c)
ss := newSequenceState(f, c, new(ps.SnapshotterMock))

assert.Equal(t, 0, ss.currentState())
assert.True(t, ss.isInitialState())
Expand Down Expand Up @@ -190,7 +191,7 @@ func TestSimpleSequence(t *testing.T) {
`, &config.Config{Kstream: config.KstreamConfig{EnableFileIOKevents: true}, Filters: &config.Filters{}})
require.NoError(t, f.Compile())

ss := newSequenceState(f, c)
ss := newSequenceState(f, c, new(ps.SnapshotterMock))

var tests = []struct {
evts []*kevent.Kevent
Expand Down Expand Up @@ -276,7 +277,7 @@ func TestSimpleSequenceMultiplePartials(t *testing.T) {
`, &config.Config{Kstream: config.KstreamConfig{EnableFileIOKevents: true}, Filters: &config.Filters{}})
require.NoError(t, f.Compile())

ss := newSequenceState(f, c)
ss := newSequenceState(f, c, new(ps.SnapshotterMock))

// create random matches which don't satisfy the sequence link
for i, pid := range []uint32{2343, 1024, 11122, 3450, 12319} {
Expand Down Expand Up @@ -382,7 +383,7 @@ func TestSimpleSequenceDeadline(t *testing.T) {
`, &config.Config{Kstream: config.KstreamConfig{EnableFileIOKevents: true}, Filters: &config.Filters{}})
require.NoError(t, f.Compile())

ss := newSequenceState(f, c)
ss := newSequenceState(f, c, new(ps.SnapshotterMock))

e1 := &kevent.Kevent{
Type: ktypes.CreateProcess,
Expand Down Expand Up @@ -453,7 +454,7 @@ func TestComplexSequence(t *testing.T) {
`, &config.Config{Kstream: config.KstreamConfig{EnableFileIOKevents: true}, Filters: &config.Filters{}})
require.NoError(t, f.Compile())

ss := newSequenceState(f, c)
ss := newSequenceState(f, c, new(ps.SnapshotterMock))

e1 := &kevent.Kevent{
Seq: 1,
Expand Down Expand Up @@ -546,7 +547,7 @@ func TestSequenceOOO(t *testing.T) {
`, &config.Config{Kstream: config.KstreamConfig{EnableFileIOKevents: true}, Filters: &config.Filters{}})
require.NoError(t, f.Compile())

ss := newSequenceState(f, c)
ss := newSequenceState(f, c, new(ps.SnapshotterMock))

e1 := &kevent.Kevent{
Type: ktypes.CreateFile,
Expand Down Expand Up @@ -606,7 +607,7 @@ func TestSequenceGC(t *testing.T) {
`, &config.Config{Kstream: config.KstreamConfig{EnableFileIOKevents: true}, Filters: &config.Filters{}})
require.NoError(t, f.Compile())

ss := newSequenceState(f, c)
ss := newSequenceState(f, c, new(ps.SnapshotterMock))

e := &kevent.Kevent{
Type: ktypes.OpenProcess,
Expand Down Expand Up @@ -755,7 +756,7 @@ func TestSequenceExpire(t *testing.T) {
f := filter.New(tt.expr, &config.Config{Kstream: config.KstreamConfig{EnableFileIOKevents: true}, Filters: &config.Filters{}})
require.NoError(t, f.Compile())

ss := newSequenceState(f, tt.c)
ss := newSequenceState(f, tt.c, new(ps.SnapshotterMock))
for _, evt := range tt.evts {
if evt.IsTerminateProcess() {
ss.expire(evt)
Expand Down Expand Up @@ -787,7 +788,7 @@ func TestSequenceBoundFields(t *testing.T) {
`, &config.Config{Kstream: config.KstreamConfig{EnableFileIOKevents: true}, Filters: &config.Filters{}})
require.NoError(t, f.Compile())

ss := newSequenceState(f, c)
ss := newSequenceState(f, c, new(ps.SnapshotterMock))

e1 := &kevent.Kevent{
Type: ktypes.CreateProcess,
Expand Down Expand Up @@ -882,7 +883,7 @@ func TestSequenceBoundFieldsWithFunctions(t *testing.T) {
`, &config.Config{Kstream: config.KstreamConfig{EnableFileIOKevents: true, EnableRegistryKevents: true}, Filters: &config.Filters{}})
require.NoError(t, f.Compile())

ss := newSequenceState(f, c)
ss := newSequenceState(f, c, new(ps.SnapshotterMock))

e1 := &kevent.Kevent{
Type: ktypes.CreateFile,
Expand Down Expand Up @@ -942,7 +943,7 @@ func TestIsExpressionEvaluable(t *testing.T) {
`, &config.Config{Kstream: config.KstreamConfig{EnableFileIOKevents: true}, Filters: &config.Filters{}})
require.NoError(t, f.Compile())

ss := newSequenceState(f, c)
ss := newSequenceState(f, c, new(ps.SnapshotterMock))

e1 := &kevent.Kevent{
Type: ktypes.CreateProcess,
Expand Down