@@ -10,6 +10,7 @@ package usm
1010import (
1111 "fmt"
1212 "io"
13+ "reflect"
1314 "regexp"
1415 "time"
1516 "unsafe"
@@ -81,6 +82,46 @@ const (
8182 fdBySSLBioMap = "fd_by_ssl_bio"
8283)
8384
85+ // pidKeyedTLSMaps is the single source of truth for all TLS eBPF maps that use pid_tgid as keys.
86+ //
87+ // IMPORTANT: Map names must be unique within their first 15 characters due to kernel truncation
88+ // (BPF_OBJ_NAME_LEN - 1). The leak detection system searches maps by truncated names, so names
89+ // like "hash_map_name_10" and "hash_map_name_11" would collide as both truncate to "hash_map_name_1".
90+ //
91+ // When adding a new PID-keyed map:
92+ // 1. Add a new field to this struct with the map name as the value
93+ // 2. Ensure the name is unique within the first 15 characters
94+ // 3. Add the corresponding map cleaner field to sslProgram struct
95+ // 4. Initialize it in initAllMapCleaners() with uint64 key type
96+ // The GetPIDKeyedTLSMapNames() function will automatically include it via reflection.
97+ var pidKeyedTLSMaps = struct {
98+ SSLReadArgs string
99+ SSLReadExArgs string
100+ SSLWriteArgs string
101+ SSLWriteExArgs string
102+ BioNewSocketArgs string
103+ SSLCtxByPIDTGID string
104+ }{
105+ SSLReadArgs : "ssl_read_args" ,
106+ SSLReadExArgs : "ssl_read_ex_args" ,
107+ SSLWriteArgs : "ssl_write_args" ,
108+ SSLWriteExArgs : "ssl_write_ex_args" ,
109+ BioNewSocketArgs : "bio_new_socket_args" ,
110+ SSLCtxByPIDTGID : "ssl_ctx_by_pid_tgid" ,
111+ }
112+
113+ // GetPIDKeyedTLSMapNames returns the names of all TLS eBPF maps that use pid_tgid as keys.
114+ // It uses reflection to extract all field values from pidKeyedTLSMaps struct.
115+ // This ensures the list is automatically kept in sync with the struct definition.
116+ func GetPIDKeyedTLSMapNames () []string {
117+ v := reflect .ValueOf (pidKeyedTLSMaps )
118+ names := make ([]string , v .NumField ())
119+ for i := 0 ; i < v .NumField (); i ++ {
120+ names [i ] = v .Field (i ).String ()
121+ }
122+ return names
123+ }
124+
84125var openSSLProbes = []manager.ProbesSelector {
85126 & manager.BestEffort {
86127 Selectors : []manager.ProbesSelector {
@@ -254,25 +295,25 @@ var sharedLibrariesMaps = []*manager.Map{
254295 Name : sslCtxByTupleMap ,
255296 },
256297 {
257- Name : sslReadArgsMap ,
298+ Name : pidKeyedTLSMaps . SSLReadArgs ,
258299 },
259300 {
260- Name : sslReadExArgsMap ,
301+ Name : pidKeyedTLSMaps . SSLReadExArgs ,
261302 },
262303 {
263- Name : sslWriteArgsMap ,
304+ Name : pidKeyedTLSMaps . SSLWriteArgs ,
264305 },
265306 {
266- Name : sslWriteExArgsMap ,
307+ Name : pidKeyedTLSMaps . SSLWriteExArgs ,
267308 },
268309 {
269- Name : bioNewSocketArgsMap ,
310+ Name : pidKeyedTLSMaps . BioNewSocketArgs ,
270311 },
271312 {
272313 Name : fdBySSLBioMap ,
273314 },
274315 {
275- Name : sslCtxByPIDTGIDMap ,
316+ Name : pidKeyedTLSMaps . SSLCtxByPIDTGID ,
276317 },
277318}
278319
@@ -538,7 +579,7 @@ func sharedLibrariesConfigureOptions(options *manager.Options, cfg *config.Confi
538579 MaxEntries : cfg .MaxTrackedConnections ,
539580 EditorFlag : manager .EditMaxEntries ,
540581 }
541- options .MapSpecEditors [sslCtxByPIDTGIDMap ] = manager.MapSpecEditor {
582+ options .MapSpecEditors [pidKeyedTLSMaps . SSLCtxByPIDTGID ] = manager.MapSpecEditor {
542583 MaxEntries : cfg .MaxTrackedConnections ,
543584 EditorFlag : manager .EditMaxEntries ,
544585 }
@@ -570,32 +611,32 @@ func initMapCleaner[K, V interface{}](mgr *manager.Manager, mapName, attacherNam
570611func (o * sslProgram ) initAllMapCleaners () error {
571612 var err error
572613
573- o .sslReadArgsMapCleaner , err = initMapCleaner [uint64 , http.SslReadArgs ](o .ebpfManager , sslReadArgsMap , UsmTLSAttacherName )
614+ o .sslReadArgsMapCleaner , err = initMapCleaner [uint64 , http.SslReadArgs ](o .ebpfManager , pidKeyedTLSMaps . SSLReadArgs , UsmTLSAttacherName )
574615 if err != nil {
575616 return err
576617 }
577618
578- o .sslReadExArgsMapCleaner , err = initMapCleaner [uint64 , http.SslReadExArgs ](o .ebpfManager , sslReadExArgsMap , UsmTLSAttacherName )
619+ o .sslReadExArgsMapCleaner , err = initMapCleaner [uint64 , http.SslReadExArgs ](o .ebpfManager , pidKeyedTLSMaps . SSLReadExArgs , UsmTLSAttacherName )
579620 if err != nil {
580621 return err
581622 }
582623
583- o .sslWriteArgsMapCleaner , err = initMapCleaner [uint64 , http.SslWriteArgs ](o .ebpfManager , sslWriteArgsMap , UsmTLSAttacherName )
624+ o .sslWriteArgsMapCleaner , err = initMapCleaner [uint64 , http.SslWriteArgs ](o .ebpfManager , pidKeyedTLSMaps . SSLWriteArgs , UsmTLSAttacherName )
584625 if err != nil {
585626 return err
586627 }
587628
588- o .sslWriteExArgsMapCleaner , err = initMapCleaner [uint64 , http.SslWriteExArgs ](o .ebpfManager , sslWriteExArgsMap , UsmTLSAttacherName )
629+ o .sslWriteExArgsMapCleaner , err = initMapCleaner [uint64 , http.SslWriteExArgs ](o .ebpfManager , pidKeyedTLSMaps . SSLWriteExArgs , UsmTLSAttacherName )
589630 if err != nil {
590631 return err
591632 }
592633
593- o .bioNewSocketArgsMapCleaner , err = initMapCleaner [uint64 , uint32 ](o .ebpfManager , bioNewSocketArgsMap , UsmTLSAttacherName )
634+ o .bioNewSocketArgsMapCleaner , err = initMapCleaner [uint64 , uint32 ](o .ebpfManager , pidKeyedTLSMaps . BioNewSocketArgs , UsmTLSAttacherName )
594635 if err != nil {
595636 return err
596637 }
597638
598- o .sslCtxByPIDTGIDMapCleaner , err = initMapCleaner [uint64 , uint64 ](o .ebpfManager , sslCtxByPIDTGIDMap , UsmTLSAttacherName )
639+ o .sslCtxByPIDTGIDMapCleaner , err = initMapCleaner [uint64 , uint64 ](o .ebpfManager , pidKeyedTLSMaps . SSLCtxByPIDTGID , UsmTLSAttacherName )
599640 if err != nil {
600641 return err
601642 }
@@ -651,7 +692,7 @@ func (o *sslProgram) DumpMaps(w io.Writer, mapName string, currentMap *ebpf.Map)
651692 spew .Fdump (w , key , value )
652693 }
653694
654- case sslReadArgsMap : // maps/ssl_read_args (BPF_MAP_TYPE_HASH), key C.__u64, value C.ssl_read_args_t
695+ case pidKeyedTLSMaps . SSLReadArgs : // maps/ssl_read_args (BPF_MAP_TYPE_HASH), key C.__u64, value C.ssl_read_args_t
655696 io .WriteString (w , "Map: '" + mapName + "', key: 'C.__u64', value: 'C.ssl_read_args_t'\n " )
656697 iter := currentMap .Iterate ()
657698 var key uint64
@@ -660,7 +701,7 @@ func (o *sslProgram) DumpMaps(w io.Writer, mapName string, currentMap *ebpf.Map)
660701 spew .Fdump (w , key , value )
661702 }
662703
663- case sslReadExArgsMap : // maps/ssl_read_ex_args (BPF_MAP_TYPE_HASH), key C.__u64, value C.ssl_read_ex_args_t
704+ case pidKeyedTLSMaps . SSLReadExArgs : // maps/ssl_read_ex_args (BPF_MAP_TYPE_HASH), key C.__u64, value C.ssl_read_ex_args_t
664705 io .WriteString (w , "Map: '" + mapName + "', key: 'C.__u64', value: 'C.ssl_read_ex_args_t'\n " )
665706 iter := currentMap .Iterate ()
666707 var key uint64
@@ -669,7 +710,7 @@ func (o *sslProgram) DumpMaps(w io.Writer, mapName string, currentMap *ebpf.Map)
669710 spew .Fdump (w , key , value )
670711 }
671712
672- case sslWriteArgsMap : // maps/ssl_write_args (BPF_MAP_TYPE_HASH), key C.__u64, value C.ssl_write_args_t
713+ case pidKeyedTLSMaps . SSLWriteArgs : // maps/ssl_write_args (BPF_MAP_TYPE_HASH), key C.__u64, value C.ssl_write_args_t
673714 io .WriteString (w , "Map: '" + mapName + "', key: 'C.__u64', value: 'C.ssl_write_args_t'\n " )
674715 iter := currentMap .Iterate ()
675716 var key uint64
@@ -678,7 +719,7 @@ func (o *sslProgram) DumpMaps(w io.Writer, mapName string, currentMap *ebpf.Map)
678719 spew .Fdump (w , key , value )
679720 }
680721
681- case sslWriteExArgsMap : // maps/ssl_write_ex_args_t (BPF_MAP_TYPE_HASH), key C.__u64, value C.ssl_write_args_t
722+ case pidKeyedTLSMaps . SSLWriteExArgs : // maps/ssl_write_ex_args_t (BPF_MAP_TYPE_HASH), key C.__u64, value C.ssl_write_args_t
682723 io .WriteString (w , "Map: '" + mapName + "', key: 'C.__u64', value: 'C.ssl_write_ex_args_t'\n " )
683724 iter := currentMap .Iterate ()
684725 var key uint64
@@ -687,7 +728,7 @@ func (o *sslProgram) DumpMaps(w io.Writer, mapName string, currentMap *ebpf.Map)
687728 spew .Fdump (w , key , value )
688729 }
689730
690- case bioNewSocketArgsMap : // maps/bio_new_socket_args (BPF_MAP_TYPE_HASH), key C.__u64, value C.__u32
731+ case pidKeyedTLSMaps . BioNewSocketArgs : // maps/bio_new_socket_args (BPF_MAP_TYPE_HASH), key C.__u64, value C.__u32
691732 io .WriteString (w , "Map: '" + mapName + "', key: 'C.__u64', value: 'C.__u32'\n " )
692733 iter := currentMap .Iterate ()
693734 var key uint64
@@ -705,7 +746,7 @@ func (o *sslProgram) DumpMaps(w io.Writer, mapName string, currentMap *ebpf.Map)
705746 spew .Fdump (w , key , value )
706747 }
707748
708- case sslCtxByPIDTGIDMap : // maps/ssl_ctx_by_pid_tgid (BPF_MAP_TYPE_HASH), key C.__u64, value uintptr // C.void *
749+ case pidKeyedTLSMaps . SSLCtxByPIDTGID : // maps/ssl_ctx_by_pid_tgid (BPF_MAP_TYPE_HASH), key C.__u64, value uintptr // C.void *
709750 io .WriteString (w , "Map: '" + mapName + "', key: 'C.__u64', value: 'uintptr // C.void *'\n " )
710751 iter := currentMap .Iterate ()
711752 var key uint64
@@ -797,7 +838,7 @@ func (o *sslProgram) cleanupDeadPids(alivePIDs map[uint32]struct{}) {
797838 })
798839
799840 if err := o .deleteDeadPidsInSSLCtxMap (alivePIDs ); err != nil {
800- log .Debugf ("SSL map %q cleanup error: %v" , sslCtxByPIDTGIDMap , err )
841+ log .Debugf ("SSL map %q cleanup error: %v" , pidKeyedTLSMaps . SSLCtxByPIDTGID , err )
801842 }
802843}
803844
0 commit comments