Skip to content

Commit

Permalink
Add generic way to retreive libc path
Browse files Browse the repository at this point in the history
  • Loading branch information
calesanz committed Feb 8, 2022
1 parent b7fb3ee commit 8f89f98
Showing 1 changed file with 61 additions and 2 deletions.
63 changes: 61 additions & 2 deletions daemon/dns/ebpfhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,67 @@ import (
bpf "github.com/iovisor/gobpf/elf"
)

var libcFile = "/lib/libc.so.6"
/*
#cgo LDFLAGS: -ldl
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <link.h>
#include <dlfcn.h>
#include <string.h>
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
Ip [16]uint8
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()
Expand Down Expand Up @@ -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
}

Expand Down

0 comments on commit 8f89f98

Please sign in to comment.