From ab13932f74d7adc5c30a8871a19f45645605f55b Mon Sep 17 00:00:00 2001 From: CFC4N Date: Fri, 26 Jan 2024 13:33:06 +0800 Subject: [PATCH] user: fix concurrent map read and map write #467 Signed-off-by: CFC4N --- user/module/probe_openssl.go | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/user/module/probe_openssl.go b/user/module/probe_openssl.go index 5a921f2cd..2bf0bc968 100644 --- a/user/module/probe_openssl.go +++ b/user/module/probe_openssl.go @@ -63,7 +63,8 @@ type MOpenSSLProbe struct { eventMaps []*ebpf.Map // pid[fd:Addr] - pidConns map[uint32]map[uint32]string + pidConns map[uint32]map[uint32]string + pidLocker sync.Locker keyloggerFilename string keylogger *os.File @@ -85,6 +86,7 @@ func (m *MOpenSSLProbe) Init(ctx context.Context, logger *log.Logger, conf confi m.eventMaps = make([]*ebpf.Map, 0, 2) m.eventFuncMaps = make(map[*ebpf.Map]event.IEventStruct) m.pidConns = make(map[uint32]map[uint32]string) + m.pidLocker = new(sync.Mutex) m.masterKeys = make(map[string]bool) m.sslVersionBpfMap = make(map[string]string) @@ -289,6 +291,8 @@ func (m *MOpenSSLProbe) AddConn(pid, fd uint32, addr string) { // save var connMap map[uint32]string var f bool + m.pidLocker.Lock() + defer m.pidLocker.Unlock() connMap, f = m.pidConns[pid] if !f { connMap = make(map[uint32]string) @@ -307,6 +311,8 @@ func (m *MOpenSSLProbe) DelConn(pid, fd uint32) { if pid == 0 { return } + m.pidLocker.Lock() + defer m.pidLocker.Unlock() if fd == 0 { delete(m.pidConns, pid) } @@ -328,6 +334,8 @@ func (m *MOpenSSLProbe) GetConn(pid, fd uint32) string { var connMap map[uint32]string var f bool //m.logger.Printf("%s\tGetConn pid:%d, fd:%d, mapinfo:%v\n", m.Name(), pid, fd, m.pidConns) + m.pidLocker.Lock() + defer m.pidLocker.Unlock() connMap, f = m.pidConns[pid] if !f { return ConnNotFound