Skip to content

Commit cb4cedc

Browse files
author
Andrew Stucki
authored
[Auditbeat] Fix up socket dataset runaway CPU usage (#19764)
* Fix up socket dataset * Add Changelog entry
1 parent b9cb9e4 commit cb4cedc

3 files changed

Lines changed: 36 additions & 8 deletions

File tree

CHANGELOG.next.asciidoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ https://github.com/elastic/beats/compare/v7.0.0-alpha2...master[Check the HEAD d
148148
- system/package: Fix parsing of Installed-Size field of DEB packages. {issue}16661[16661] {pull}17188[17188]
149149
- system module: Fix panic during initialisation when /proc/stat can't be read. {pull}17569[17569]
150150
- system/package: Fix an error that can occur while trying to persist package metadata. {issue}18536[18536] {pull}18887[18887]
151-
- system/socket: Fix dataset using 100% CPU and becoming unresponsive in some scenarios. {pull}19033[19033]
151+
- system/socket: Fix dataset using 100% CPU and becoming unresponsive in some scenarios. {pull}19033[19033] {pull}19764[19764]
152152
- system/socket: Fixed tracking of long-running connections. {pull}19033[19033]
153153
- system/package: Fix librpm loading on Fedora 31/32. {pull}NNNN[NNNN]
154154
- file_integrity: Create fsnotify watcher only when starting file_integrity module {pull}19505[19505]

x-pack/auditbeat/module/system/socket/state.go

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -523,7 +523,7 @@ func (s *state) ExpireOlder() {
523523
deadline = s.clock().Add(-s.socketTimeout)
524524
for item := s.socketLRU.peek(); item != nil && item.Timestamp().Before(deadline); {
525525
if sock, ok := item.(*socket); ok {
526-
s.onSockDestroyed(sock.sock, 0)
526+
s.onSockDestroyed(sock.sock, sock, 0)
527527
} else {
528528
s.socketLRU.get()
529529
}
@@ -704,13 +704,16 @@ func (s *state) OnSockDestroyed(ptr uintptr, pid uint32) error {
704704
s.Lock()
705705
defer s.Unlock()
706706

707-
return s.onSockDestroyed(ptr, pid)
707+
return s.onSockDestroyed(ptr, nil, pid)
708708
}
709709

710-
func (s *state) onSockDestroyed(ptr uintptr, pid uint32) error {
711-
sock, found := s.socks[ptr]
712-
if !found {
713-
return nil
710+
func (s *state) onSockDestroyed(ptr uintptr, sock *socket, pid uint32) error {
711+
var found bool
712+
if sock == nil {
713+
sock, found = s.socks[ptr]
714+
if !found {
715+
return nil
716+
}
714717
}
715718
// Enrich with pid
716719
if sock.pid == 0 && pid != 0 {

x-pack/auditbeat/module/system/socket/state_test.go

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,6 @@ func TestTCPConnWithProcessSocketTimeouts(t *testing.T) {
152152
lPort, rPort := be16(localPort), be16(remotePort)
153153
lAddr, rAddr := ipv4(localIP), ipv4(remoteIP)
154154
evs := []event{
155-
156155
callExecve(meta(1234, 1234, 1), []string{"/usr/bin/curl", "https://example.net/", "-o", "/tmp/site.html"}),
157156
&commitCreds{Meta: meta(1234, 1234, 2), UID: 501, GID: 20, EUID: 501, EGID: 20},
158157
&execveRet{Meta: meta(1234, 1234, 2), Retval: 1234},
@@ -302,6 +301,32 @@ func TestTCPConnWithProcessSocketTimeouts(t *testing.T) {
302301
}
303302
}
304303

304+
func TestSocketExpirationWithOverwrittenSockets(t *testing.T) {
305+
const (
306+
sock uintptr = 0xff1234
307+
flowTimeout = time.Hour
308+
socketTimeout = time.Minute * 3
309+
closeTimeout = time.Minute
310+
)
311+
st := makeState(nil, (*logWrapper)(t), flowTimeout, socketTimeout, closeTimeout, time.Second)
312+
now := time.Now()
313+
st.clock = func() time.Time {
314+
return now
315+
}
316+
if err := feedEvents([]event{
317+
&inetCreate{Meta: meta(1234, 1236, 5), Proto: 0},
318+
&sockInitData{Meta: meta(1234, 1236, 5), Sock: sock},
319+
&inetCreate{Meta: meta(1234, 1237, 5), Proto: 0},
320+
&sockInitData{Meta: meta(1234, 1237, 5), Sock: sock},
321+
}, st, t); err != nil {
322+
t.Fatal(err)
323+
}
324+
now = now.Add(closeTimeout + 1)
325+
st.ExpireOlder()
326+
now = now.Add(socketTimeout + 1)
327+
st.ExpireOlder()
328+
}
329+
305330
func TestUDPOutgoingSinglePacketWithProcess(t *testing.T) {
306331
const (
307332
localIP = "192.168.33.10"

0 commit comments

Comments
 (0)