@@ -44,7 +44,7 @@ const unsigned long long kernelAddrSpace = 0x0;
44
44
#endif
45
45
46
46
char * bcc_procutils_which (const char * binpath ) {
47
- char buffer [4096 ];
47
+ char buffer [PATH_MAX ];
48
48
const char * PATH ;
49
49
50
50
if (strchr (binpath , '/' ))
@@ -495,8 +495,13 @@ static bool which_so_in_process(const char* libname, int pid, char* libpath) {
495
495
496
496
if (strstr (mapname , ".so" ) && (strstr (mapname , search1 ) ||
497
497
strstr (mapname , search2 ))) {
498
+ const size_t mapnamelen = strlen (mapname );
499
+ if (mapnamelen >= PATH_MAX ) {
500
+ fprintf (stderr , "Found mapped library path is too long\n" );
501
+ break ;
502
+ }
498
503
found = true;
499
- memcpy (libpath , mapname , strlen ( mapname ) + 1 );
504
+ memcpy (libpath , mapname , mapnamelen + 1 );
500
505
break ;
501
506
}
502
507
} while (ret != EOF );
@@ -505,34 +510,58 @@ static bool which_so_in_process(const char* libname, int pid, char* libpath) {
505
510
return found ;
506
511
}
507
512
508
- char * bcc_procutils_which_so (const char * libname , int pid ) {
513
+ static bool which_so_in_ldconfig_cache (const char * libname , char * libpath ) {
509
514
const size_t soname_len = strlen (libname ) + strlen ("lib.so" );
510
515
char soname [soname_len + 1 ];
511
- char libpath [4096 ];
512
516
int i ;
513
517
514
- if (strchr (libname , '/' ))
515
- return strdup (libname );
516
-
517
- if (pid && which_so_in_process (libname , pid , libpath ))
518
- return strdup (libpath );
519
-
520
518
if (lib_cache_count < 0 )
521
- return NULL ;
519
+ return false ;
522
520
523
521
if (!lib_cache_count && load_ld_cache (LD_SO_CACHE ) < 0 ) {
524
522
lib_cache_count = -1 ;
525
- return NULL ;
523
+ return false ;
526
524
}
527
525
528
526
snprintf (soname , soname_len + 1 , "lib%s.so" , libname );
529
527
530
528
for (i = 0 ; i < lib_cache_count ; ++ i ) {
531
529
if (!strncmp (lib_cache [i ].libname , soname , soname_len ) &&
532
530
match_so_flags (lib_cache [i ].flags )) {
533
- return strdup (lib_cache [i ].path );
531
+
532
+ const char * path = lib_cache [i ].path ;
533
+ const size_t pathlen = strlen (path );
534
+ if (pathlen >= PATH_MAX ) {
535
+ fprintf (stderr , "Found library path is too long\n" );
536
+ return false;
537
+ }
538
+ memcpy (libpath , path , pathlen + 1 );
539
+ return true;
534
540
}
535
541
}
542
+
543
+ return false;
544
+ }
545
+
546
+ char * bcc_procutils_which_so (const char * libname , int pid ) {
547
+ char libpath [PATH_MAX ];
548
+
549
+ if (strchr (libname , '/' ))
550
+ return strdup (libname );
551
+
552
+ if (pid && which_so_in_process (libname , pid , libpath ))
553
+ return strdup (libpath );
554
+
555
+ if (which_so_in_ldconfig_cache (libname , libpath ))
556
+ return strdup (libpath );
557
+
558
+ return NULL ;
559
+ }
560
+
561
+ char * bcc_procutils_which_so_in_process (const char * libname , int pid ) {
562
+ char libpath [PATH_MAX ];
563
+ if (pid && which_so_in_process (libname , pid , libpath ))
564
+ return strdup (libpath );
536
565
return NULL ;
537
566
}
538
567
@@ -558,7 +587,6 @@ const char *bcc_procutils_language(int pid) {
558
587
return languages [i ];
559
588
}
560
589
561
-
562
590
snprintf (procfilename , sizeof (procfilename ), "/proc/%ld/maps" , (long )pid );
563
591
procfile = fopen (procfilename , "r" );
564
592
if (!procfile )
0 commit comments