Skip to content

Commit

Permalink
user: fix concurrent map read and map write #467
Browse files Browse the repository at this point in the history
Signed-off-by: CFC4N <cfc4n.cs@gmail.com>
  • Loading branch information
cfc4n committed Jan 26, 2024
1 parent da0b1a6 commit ab13932
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion user/module/probe_openssl.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)

Expand Down Expand Up @@ -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)
Expand All @@ -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)
}
Expand All @@ -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
Expand Down

0 comments on commit ab13932

Please sign in to comment.