| 1 |
#define _GNU_SOURCE |
| 2 |
#include "gotcha_dl.h" |
| 3 |
|
| 4 |
#include <dlfcn.h> |
| 5 |
|
| 6 |
#include "elf_ops.h" |
| 7 |
#include "libc_wrappers.h" |
| 8 |
#include "tool.h" |
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
struct Addrs { |
| 14 |
ElfW(Addr) lookup_addr; |
| 15 |
struct link_map *lmap; |
| 16 |
int found; |
| 17 |
}; |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
int lib_header_callback(struct dl_phdr_info *info, size_t size, void *data) { |
| 30 |
struct Addrs *addrs = data; |
| 31 |
const char *name = NULL; |
| 32 |
ElfW(Addr) load_address; |
| 33 |
for (int i = 0; i < info->dlpi_phnum; ++i) { |
| 34 |
if (info->dlpi_phdr[i].p_type == PT_LOAD) { |
| 35 |
ElfW(Addr) base_addr = info->dlpi_addr; |
| 36 |
ElfW(Addr) start_addr = base_addr + info->dlpi_phdr[i].p_vaddr; |
| 37 |
ElfW(Addr) end_addr = start_addr + info->dlpi_phdr[i].p_memsz; |
| 38 |
if (addrs->lookup_addr >= start_addr && addrs->lookup_addr < end_addr) { |
| 39 |
name = info->dlpi_name; |
| 40 |
load_address = info->dlpi_addr; |
| 41 |
break; |
| 42 |
} |
| 43 |
} |
| 44 |
} |
| 45 |
if (name) { |
| 46 |
struct link_map *current = addrs->lmap; |
| 47 |
while (current) { |
| 48 |
if (strcmp(current->l_name, name) == 0 && |
| 49 |
load_address == current->l_addr) { |
| 50 |
addrs->lmap = current; |
| 51 |
addrs->found = 1; |
| 52 |
return 1; |
| 53 |
} |
| 54 |
current = current->l_next; |
| 55 |
} |
| 56 |
} |
| 57 |
return 0; |
| 58 |
} |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
static struct link_map *gotchas_dlsym_rtld_next_lookup(const char *name, |
| 71 |
void *who) { |
| 72 |
ElfW(Addr) caller = (ElfW(Addr))who; |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
struct Addrs addrs; |
| 77 |
addrs.lookup_addr = caller; |
| 78 |
addrs.lmap = _r_debug.r_map; |
| 79 |
addrs.found = 0; |
| 80 |
void *symbol; |
| 81 |
dl_iterate_phdr(lib_header_callback, &addrs); |
| 82 |
if (!addrs.found) { |
| 83 |
error_printf("RTLD_NEXT used in code not dynamically loaded"); |
| 84 |
exit(127); |
| 85 |
} |
| 86 |
struct link_map *handle = addrs.lmap->l_next; |
| 87 |
while (handle) { |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
long result = lookup_exported_symbol(name, handle, &symbol); |
| 92 |
if (result != -1) { |
| 93 |
return handle; |
| 94 |
} else { |
| 95 |
debug_printf(3, "Symbol %s not found in the library %s\n", name, |
| 96 |
LIB_NAME(handle)); |
| 97 |
} |
| 98 |
handle = handle->l_next; |
| 99 |
} |
| 100 |
debug_printf(3, "Symbol %s not found in the libraries after caller\n", name); |
| 101 |
return NULL; |
| 102 |
} |
| 103 |
|
| 104 |
gotcha_wrappee_handle_t orig_dlopen_handle; |
| 105 |
gotcha_wrappee_handle_t orig_dlsym_handle; |
| 106 |
|
| 107 |
static int per_binding(hash_key_t key, hash_data_t data, |
| 108 |
void *opaque KNOWN_UNUSED) { |
| 109 |
int result; |
| 110 |
struct internal_binding_t *binding = (struct internal_binding_t *)data; |
| 111 |
|
| 112 |
debug_printf(3, "Trying to re-bind %s from tool %s after dlopen\n", |
| 113 |
binding->user_binding->name, |
| 114 |
binding->associated_binding_table->tool->tool_name); |
| 115 |
|
| 116 |
if (!binding->user_binding->name) return 0; |
| 117 |
while (binding->next_binding) { |
| 118 |
binding = binding->next_binding; |
| 119 |
debug_printf(3, |
| 120 |
"Selecting new innermost version of binding %s from tool " |
| 121 |
"%s.\n", |
| 122 |
binding->user_binding->name, |
| 123 |
binding->associated_binding_table->tool->tool_name); |
| 124 |
} |
| 125 |
|
| 126 |
result = prepare_symbol(binding); |
| 127 |
if (result == -1) { |
| 128 |
debug_printf(3, "Still could not prepare binding %s after dlopen\n", |
| 129 |
binding->user_binding->name); |
| 130 |
return 0; |
| 131 |
} |
| 132 |
|
| 133 |
removefrom_hashtable(¬found_binding_table, key); |
| 134 |
return 0; |
| 135 |
} |
| 136 |
|
| 137 |
static void *dlopen_wrapper(const char *filename, int flags) { |
| 138 |
typeof(&dlopen_wrapper) orig_dlopen = gotcha_get_wrappee(orig_dlopen_handle); |
| 139 |
void *handle; |
| 140 |
debug_printf(1, "User called dlopen(%s, 0x%x)\n", filename, |
| 141 |
(unsigned int)flags); |
| 142 |
handle = orig_dlopen(filename, flags); |
| 143 |
|
| 144 |
debug_printf( |
| 145 |
2, "Searching new dlopened libraries for previously-not-found exports\n"); |
| 146 |
foreach_hash_entry(¬found_binding_table, NULL, per_binding); |
| 147 |
|
| 148 |
debug_printf(2, "Updating GOT entries for new dlopened libraries\n"); |
| 149 |
update_all_library_gots(&function_hash_table); |
| 150 |
|
| 151 |
return handle; |
| 152 |
} |
| 153 |
|
| 154 |
static void *dlsym_wrapper(void *handle, const char *symbol_name) { |
| 155 |
typeof(&dlopen_wrapper) orig_dlopen = gotcha_get_wrappee(orig_dlopen_handle); |
| 156 |
typeof(&dlsym_wrapper) orig_dlsym = gotcha_get_wrappee(orig_dlsym_handle); |
| 157 |
struct internal_binding_t *binding; |
| 158 |
debug_printf(1, "User called dlsym(%p, %s)\n", handle, symbol_name); |
| 159 |
int result = lookup_hashtable(&function_hash_table, (hash_key_t)symbol_name, |
| 160 |
(hash_data_t *)&binding); |
| 161 |
void *val = orig_dlsym(handle, symbol_name); |
| 162 |
if (result != -1) { |
| 163 |
void **wrappee_ptr = getInternalBindingAddressPointer( |
| 164 |
(struct internal_binding_t **)binding->user_binding->function_handle); |
| 165 |
if (val == NULL || *wrappee_ptr == val) { |
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
return binding->user_binding->wrapper_pointer; |
| 171 |
} |
| 172 |
} |
| 173 |
if (handle == RTLD_NEXT) { |
| 174 |
struct link_map *lib = gotchas_dlsym_rtld_next_lookup( |
| 175 |
symbol_name, __builtin_return_address(0)); |
| 176 |
if (lib) { |
| 177 |
void *handle = orig_dlopen(lib->l_name, RTLD_NOW); |
| 178 |
void *symbol = orig_dlsym(handle, symbol_name); |
| 179 |
return symbol; |
| 180 |
} |
| 181 |
return NULL; |
| 182 |
} else { |
| 183 |
return val; |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
struct gotcha_binding_t dl_binds[] = { |
| 188 |
{"dlopen", dlopen_wrapper, &orig_dlopen_handle}, |
| 189 |
{"dlsym", dlsym_wrapper, &orig_dlsym_handle}}; |
| 190 |
void handle_libdl() { gotcha_wrap(dl_binds, 2, "gotcha"); } |