@@ -103,13 +103,10 @@ func getPidFromEbpf(proto string, srcPort uint, srcIP net.IP, dstIP net.IP, dstP
103103 strconv .FormatUint (uint64 (dstPort ), 10 ))
104104 if cacheItem , isInCache := ebpfCache .isInCache (k ); isInCache {
105105 deleteEbpfEntry (proto , key )
106- if ev , found := procmon .EventsCache .IsInStoreByPID (cacheItem .Pid ); found {
107- proc = & ev .Proc
108- log .Debug ("[ebpf conn] in cache: %s, %d -> %s" , k , proc .ID , proc .Path )
106+ if p := isPIDinEventsCache (cacheItem .Pid , cacheItem .UID ); p != nil {
107+ proc = p
109108 return
110109 }
111- log .Trace ("[ebpf conn] in cache, with no proc %s, %d" , k , cacheItem .Pid )
112- return
113110 }
114111
115112 err := ebpfMaps [proto ].bpfMap .Lookup (& key , & value )
@@ -172,20 +169,28 @@ func getPidFromEbpf(proto string, srcPort uint, srcIP net.IP, dstIP net.IP, dstP
172169 return
173170}
174171
172+ // Check if the PID of the connection is in the cache.
173+ func isPIDinEventsCache (pid , uid int ) (proc * procmon.Process ) {
174+ if ev , found := procmon .EventsCache .IsInStoreByPID (pid ); found {
175+ // In some cases, a process may have dropped its privileges, from 0 to 123 for example.
176+ // In these cases use socket's UID. This is the UID that we've always used,
177+ ev .Proc .UID = uid
178+ proc = & ev .Proc
179+ log .Debug ("[ebpf conn] not in cache, but in execEvents, pid: %d, uid: %d -> %s -> %s" , proc .ID , proc .UID , proc .Path , proc .Args )
180+ return proc
181+ }
182+
183+ return nil
184+ }
185+
175186// findConnProcess finds the process' details of a connection.
176187// By default we only receive the PID of the process, so we need to get
177188// the rest of the details.
178189// TODO: get the details from kernel, with mm_struct (exe_file, fd_path, etc).
179190func findConnProcess (value * networkEventT , connKey string ) (proc * procmon.Process ) {
180191
181- // Use socket's UID. A process may have dropped privileges.
182- // This is the UID that we've always used.
183-
184- if ev , found := procmon .EventsCache .IsInStoreByPID (int (value .Pid )); found {
185- ev .Proc .UID = int (value .UID )
186- proc = & ev .Proc
187- log .Debug ("[ebpf conn] not in cache, but in execEvents: %s, %d -> %s -> %s" , connKey , proc .ID , proc .Path , proc .Args )
188- return
192+ if p := isPIDinEventsCache (int (value .Pid ), int (value .UID )); p != nil {
193+ return p
189194 }
190195
191196 // We'll end here if the events module has not been loaded, or if the process is not in cache.
0 commit comments