From 8f89f98254f5bd3036cd72394210ce9aa2bc845f Mon Sep 17 00:00:00 2001 From: calesanz <8714917+calesanz@users.noreply.github.com> Date: Tue, 8 Feb 2022 20:59:17 +0100 Subject: [PATCH] Add generic way to retreive libc path --- daemon/dns/ebpfhook.go | 63 ++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 61 insertions(+), 2 deletions(-) diff --git a/daemon/dns/ebpfhook.go b/daemon/dns/ebpfhook.go index 8c6f9be06f..776488fbb1 100644 --- a/daemon/dns/ebpfhook.go +++ b/daemon/dns/ebpfhook.go @@ -15,7 +15,49 @@ import ( bpf "github.com/iovisor/gobpf/elf" ) -var libcFile = "/lib/libc.so.6" +/* +#cgo LDFLAGS: -ldl + +#define _GNU_SOURCE +#include +#include +#include +#include +#include + +char* find_libc() { + void *handle; + struct link_map * map; + + handle = dlopen(NULL, RTLD_NOW); + if (handle == NULL) { + fprintf(stderr, "EBPF-DNS dlopen() failed: %s\n", dlerror()); + return NULL; + } + + + if (dlinfo(handle, RTLD_DI_LINKMAP, &map) == -1) { + fprintf(stderr, "EBPF-DNS: dlinfo failed: %s\n", dlerror()); + return NULL; + } + + while(1){ + if(map == NULL){ + break; + } + + if(strstr(map->l_name, "libc.so")){ + fprintf(stderr,"found %s\n", map->l_name); + return map->l_name; + } + map = map->l_next; + } + return NULL; +} + + +*/ +import "C" type nameLookupEvent struct { AddrType uint32 @@ -23,6 +65,17 @@ type nameLookupEvent struct { Host [252]byte } +func findLibc() (string, error) { + ret := C.find_libc() + + if ret == nil { + return "", errors.New("Could not find path to libc.so") + } + str := C.GoString(ret) + + return str, nil +} + // Iterates over all symbols in an elf file and returns the offset matching the provided symbol name. func lookupSymbol(elffile *elf.File, symbolName string) (uint64, error) { symbols, err := elffile.Symbols() @@ -50,10 +103,16 @@ func DnsListenerEbpf() error { // some how 0 must be replaced with the offset of getaddrinfo bcc does this using bcc_resolve_symname // Attaching to uprobe using perf open might be a better aproach requires https://github.com/iovisor/gobpf/pull/277 + libcFile, err := findLibc() + + if err != nil { + log.Error("EBPF-DNS: Failed to find libc.so: %v", err) + return err + } libc_elf, err := elf.Open(libcFile) if err != nil { - log.Error("EBPF-DNS: Failed to find libc.so.6: %v", err) + log.Error("EBPF-DNS: Failed to open %s: %v", libcFile, err) return err }