DynamoRIO / DynamoRIO/dynamorio

SIGSEGV when the client loads the auxiliary library and the auxiliary library invokes printf.

Open
#6,500 7 comments 0 reactions 0 assignees View on GitHub
help wanted
Dominant language
C
Stars
3.2k
Forks
629
Avg merge
2d 15h
Merged PRs (30d)
31

Description

**Describe the bug**

Necessary Conditions:
1. `client.so` wraps the function `func_for_hook` in the target application.
2. Upon invocation of `func_to_hook` by the target application, `client.so` executes `pre_func_for_hook`.
3. Inside `pre_func_to_hook`, `client.so` uses `dr_load_aux_library` to load the function `libtest_main` from `libtest.so`.
4. `client.so` executes `libtest_main`.
5. Within `libtest_main`, the function simply calls `printf` and returns its return

Then, drrun crashes.

**To Reproduce**
My client.c is:
```
#define _GNU_SOURCE
#include "dr_api.h"
#include "drmgr.h"
#include "drsyms.h"
#include "drwrap.h"

#include
#include
#include
#include
#include
#include
#include
#include
#include

static void pre_func_for_hook(void *wrapcxt, OUT void **user_data) {
void *mod = dr_load_aux_library("output/libtest.so", NULL, NULL);

int (*libtest_main)() =
(void *)dr_lookup_aux_library_routine(mod, "libtest_main");

libtest_main();
}

static void event_module_load(
void *drcontext, const module_data_t *mod, bool loaded) {
size_t offset = -1;
drsym_error_t error = drsym_lookup_symbol(
mod->full_path, "func_for_hook", &offset, DRSYM_DEMANGLE);
if (offset != -1 && error == DRSYM_SUCCESS &&
drwrap_wrap(mod->start + offset, pre_func_for_hook, NULL)) {
dr_printf("Successfully wrapped 'func_for_hook' function.\n");
}
}

static void event_exit(void) { drmgr_exit(); }

DR_EXPORT void dr_client_main(client_id_t id, int argc, const char *argv[]) {
drwrap_init();
drsym_init(0);
drmgr_init();

drmgr_register_module_load_event(event_module_load);
dr_register_exit_event(event_exit);
}
```

My app's source is:
```
#include
#include

int func_for_hook() { return 0; }

int main() {
printf("[app]: starting...\n");
func_for_hook();
return 0;
}
```

My libtest is:
```
#include

// Uncommenting these two lines will expose crash earlier
// _Thread_local int bigarray[1024 + 128 + 16 + 8 + 1];
// _Thread_local int local = 4578;

int libtest_main() {
printf("Solely invoking printf is ok\n");
return printf("Obtaining return value is not ok\n");
}
```

My makefile is:
```
DYNAMORIO_DIR ?= /usr/lib/DynamoRIO

CC := gcc
LIB_SCRH_DIRS != gcc -print-search-dirs | grep libraries | sed 's/libraries: =/-L/' | sed 's/:/ -L/g'
CFLAGS := -DLINUX -DX86_64
CFLAGS += -fPIC -shared
CFLAGS += -I$(DYNAMORIO_DIR)/include
CFLAGS += -I$(DYNAMORIO_DIR)/ext/include
CFLAGS += -I$(DYNAMORIO_DIR)/drmemory/drmf/include
CFLAGS += -L$(DYNAMORIO_DIR)/lib64/release
CFLAGS += -L$(DYNAMORIO_DIR)/ext/lib64/release
CFLAGS += $(LIB_SCRH_DIRS)
CFLAGS += -ldynamorio
CFLAGS += -ldrwrap -ldrmgr -ldrsyms -ldrreg -ldrx

ODIR := output
SO_FILE := $(ODIR)/client.so
LIB_TEST := $(ODIR)/libtest.so

all: $(SO_FILE)

$(SO_FILE): client.c $(LIB_TEST) Makefile
@mkdir -p $(@D)
@$(CC) $< $(CFLAGS) -o $@

$(LIB_TEST): libtest.c
@mkdir -p $(@D)
@gcc $< -fPIC -shared -o $@

test: $(SO_FILE) $(LIB_TEST)
@gcc -L$(abspath $(ODIR)) -ltest app.c -o $(ODIR)/app
@$(DYNAMORIO_DIR)/bin64/drrun -c $< -- $(ODIR)/app

clean:
@rm -rf $(ODIR)
```

Let's assume that the `DYNAMORIO_DIR` variable in the Makefile has been correctly set to the relevant path.

To reproduce the crash, place all the files in the same directory and execute `make test`.

To simplify the reproduction process, I have packaged these files into an attachment [reproduce.zip](https://github.com/DynamoRIO/dynamorio/files/13614722/reproduce.zip). Please find the attached file for your convenience.

**Expected behavior**

The `libtest_main` is expected to normally return.

**Screenshots or Pasted Text**
Output from my pc:
```
$ make test
Successfully wrapped 'func_for_hook' function.
[app]: starting...
Solely invoking printf is ok

make: *** [Makefile:32: test] Error 255
```

**Versions**
- I am running version 10.0.0 (64-bit) on Ubuntu 22.04 (x86-64 architecture).

**Additional context**
Add any other context about the problem here.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.