1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
|
package ebpf
import (
"sync"
"time"
"github.com/evilsocket/opensnitch/daemon/procmon"
)
// NewExecEvent constructs a new execEvent from the arguments.
func NewExecEvent(pid, ppid, uid uint32, path string, comm [16]byte) *execEvent {
ev := &execEvent{
Type: EV_TYPE_EXEC,
PID: pid,
PPID: ppid,
UID: uid,
Comm: comm,
}
length := MaxPathLen
if len(path) < MaxPathLen {
length = len(path)
}
copy(ev.Filename[:], path[:length])
return ev
}
type execEventItem struct {
Proc procmon.Process
Event execEvent
LastSeen int64
}
type eventsStore struct {
execEvents map[uint32]*execEventItem
sync.RWMutex
}
// NewEventsStore creates a new store of events.
func NewEventsStore() *eventsStore {
return &eventsStore{
execEvents: make(map[uint32]*execEventItem),
}
}
func (e *eventsStore) add(key uint32, event execEvent, proc procmon.Process) {
e.Lock()
defer e.Unlock()
e.execEvents[key] = &execEventItem{
Proc: proc,
Event: event,
}
}
func (e *eventsStore) isInStore(key uint32) (item *execEventItem, found bool) {
e.RLock()
defer e.RUnlock()
item, found = e.execEvents[key]
return
}
func (e *eventsStore) delete(key uint32) {
e.Lock()
defer e.Unlock()
delete(e.execEvents, key)
}
func (e *eventsStore) DeleteOldItems() {
e.Lock()
defer e.Unlock()
for k, item := range e.execEvents {
if item.Proc.IsAlive() == false {
delete(e.execEvents, k)
}
}
}
//-----------------------------------------------------------------------------
type ebpfCacheItem struct {
Key []byte
Proc procmon.Process
LastSeen int64
}
type ebpfCacheType struct {
Items map[interface{}]*ebpfCacheItem
sync.RWMutex
}
var (
maxTTL = 40 // Seconds
maxCacheItems = 5000
ebpfCache *ebpfCacheType
ebpfCacheTicker *time.Ticker
)
// NewEbpfCacheItem creates a new cache item.
func NewEbpfCacheItem(key []byte, proc procmon.Process) *ebpfCacheItem {
return &ebpfCacheItem{
Key: key,
Proc: proc,
LastSeen: time.Now().UnixNano(),
}
}
func (i *ebpfCacheItem) isValid() bool {
lastSeen := time.Now().Sub(
time.Unix(0, i.LastSeen),
)
return int(lastSeen.Seconds()) < maxTTL
}
// NewEbpfCache creates a new cache store.
func NewEbpfCache() *ebpfCacheType {
ebpfCacheTicker = time.NewTicker(1 * time.Minute)
return &ebpfCacheType{
Items: make(map[interface{}]*ebpfCacheItem, 0),
}
}
func (e *ebpfCacheType) addNewItem(key interface{}, itemKey []byte, proc procmon.Process) {
e.Lock()
e.Items[key] = NewEbpfCacheItem(itemKey, proc)
e.Unlock()
}
func (e *ebpfCacheType) isInCache(key interface{}) (item *ebpfCacheItem, found bool) {
leng := e.Len()
e.Lock()
item, found = e.Items[key]
if found {
if item.isValid() {
e.update(key, item)
} else {
found = false
delete(e.Items, key)
}
}
e.Unlock()
if leng > maxCacheItems {
e.DeleteOldItems()
}
return
}
func (e *ebpfCacheType) update(key interface{}, item *ebpfCacheItem) {
item.LastSeen = time.Now().UnixNano()
e.Items[key] = item
}
func (e *ebpfCacheType) Len() int {
e.RLock()
defer e.RUnlock()
return len(e.Items)
}
func (e *ebpfCacheType) DeleteOldItems() {
length := e.Len()
e.Lock()
defer e.Unlock()
for k, item := range e.Items {
if length > maxCacheItems || (item != nil && !item.isValid()) {
delete(e.Items, k)
}
}
}
func (e *ebpfCacheType) delete(key interface{}) {
e.Lock()
defer e.Unlock()
if key, found := e.Items[key]; found {
delete(e.Items, key)
}
}
func (e *ebpfCacheType) clear() {
if e == nil {
return
}
e.Lock()
defer e.Unlock()
for k := range e.Items {
delete(e.Items, k)
}
if ebpfCacheTicker != nil {
ebpfCacheTicker.Stop()
}
}
|