diff --git a/src/disasm_wrapper.c b/src/disasm_wrapper.c index 36d50b02..ead1e1eb 100644 --- a/src/disasm_wrapper.c +++ b/src/disasm_wrapper.c @@ -91,7 +91,7 @@ intercept_disasm_init(const unsigned char *begin, const unsigned char *end) * The handle here must be passed to capstone each time it is used. */ if (cs_open(CS_ARCH_X86, CS_MODE_64, &context->handle) != CS_ERR_OK) - xabort(); + xabort("cs_open"); /* * Kindly ask capstone to return some details about the instruction. @@ -99,7 +99,7 @@ intercept_disasm_init(const unsigned char *begin, const unsigned char *end) * to parse the resulting string. */ if (cs_option(context->handle, CS_OPT_DETAIL, CS_OPT_ON) != 0) - xabort(); + xabort("cs_option - CS_OPT_DETAIL"); /* * Overriding the printing routine used by capstone, @@ -112,10 +112,10 @@ intercept_disasm_init(const unsigned char *begin, const unsigned char *end) .realloc = realloc, .vsnprintf = nop_vsnprintf}; if (cs_option(context->handle, CS_OPT_MEM, (size_t)&x) != 0) - xabort(); + xabort("cs_option - CS_OPT_MEM"); if ((context->insn = cs_malloc(context->handle)) == NULL) - xabort(); + xabort("cs_malloc"); return context; } diff --git a/src/intercept.c b/src/intercept.c index cbdb81d2..2e180b30 100644 --- a/src/intercept.c +++ b/src/intercept.c @@ -442,11 +442,13 @@ log_header(void) * by libc. */ void -xabort(void) +xabort(const char *msg) { - static const char msg[] = "libsyscall_intercept error\n"; + static const char main_msg[] = " libsyscall_intercept error\n"; - syscall_no_intercept(SYS_write, 2, msg, sizeof(msg)); + if (msg != NULL) + syscall_no_intercept(SYS_write, 2, msg, strlen(msg)); + syscall_no_intercept(SYS_write, 2, main_msg, sizeof(main_msg)); syscall_no_intercept(SYS_exit_group, 1); __builtin_trap(); diff --git a/src/intercept.h b/src/intercept.h index f85ca6c1..aaaf39c6 100644 --- a/src/intercept.h +++ b/src/intercept.h @@ -62,7 +62,7 @@ void intercept_patch_with_postfix(unsigned char *syscall_addr, #define INTERCEPTOR_EXIT_CODE 111 -__attribute__((noreturn)) void xabort(void); +__attribute__((noreturn)) void xabort(const char *); struct range { unsigned char *address; diff --git a/src/intercept_desc.c b/src/intercept_desc.c index 8ba43734..e736074f 100644 --- a/src/intercept_desc.c +++ b/src/intercept_desc.c @@ -64,8 +64,11 @@ open_orig_file(const struct intercept_desc *desc) fd = syscall_no_intercept(SYS_open, desc->path, O_RDONLY); - if (fd < 0) - xabort(); + if (fd < 0) { + syscall_no_intercept(SYS_write, 2, + desc->path, strlen(desc->path)); + xabort(" open_orig_file"); + } return fd; } @@ -143,7 +146,7 @@ find_sections(struct intercept_desc *desc, long fd) } if (!text_section_found) - xabort(); + xabort("text section not found"); } /* @@ -715,14 +718,14 @@ allocate_trampoline_table(struct intercept_desc *desc) size = 64 * 0x1000; /* XXX: don't just guess */ if ((maps = fopen("/proc/self/maps", "r")) == NULL) - xabort(); + xabort("fopen /proc/self/maps"); while ((fgets(line, sizeof(line), maps)) != NULL) { unsigned char *start; unsigned char *end; if (sscanf(line, "%p-%p", (void **)&start, (void **)&end) != 2) - xabort(); + xabort("sscanf from /proc/self/maps"); /* * Let's see if an existing mapping overlaps @@ -744,7 +747,7 @@ allocate_trampoline_table(struct intercept_desc *desc) if (guess + size >= desc->text_start + INT32_MAX) { /* Too far away */ - xabort(); + xabort("unable to find place for trampoline table"); } } @@ -756,7 +759,7 @@ allocate_trampoline_table(struct intercept_desc *desc) -1, 0); if (desc->trampoline_table == MAP_FAILED) - xabort(); + xabort("unable to allocate space for trampoline table"); desc->trampoline_table_size = size; diff --git a/src/intercept_util.c b/src/intercept_util.c index 121ff68a..8fdde811 100644 --- a/src/intercept_util.c +++ b/src/intercept_util.c @@ -59,7 +59,7 @@ xmmap_anon(size_t size) MAP_PRIVATE | MAP_ANON, -1, 0); if (addr == MAP_FAILED) - xabort(); + xabort("xmmap_anon"); return addr; } @@ -71,7 +71,7 @@ xmremap(void *addr, size_t old, size_t new) old, new, MREMAP_MAYMOVE); if (addr == MAP_FAILED) - xabort(); + xabort("xmremap"); return addr; } @@ -80,7 +80,7 @@ void xmunmap(void *addr, size_t len) { if (syscall_no_intercept(SYS_munmap, addr, len) != 0) - xabort(); + xabort("xmunmap"); } long @@ -89,7 +89,7 @@ xlseek(long fd, unsigned long off, int whence) long result = syscall_no_intercept(SYS_lseek, fd, off, whence); if (result < 0) - xabort(); + xabort("xlseek"); return result; } @@ -99,7 +99,7 @@ xread(long fd, void *buffer, size_t size) { if (syscall_no_intercept(SYS_read, fd, (long)buffer, (long)size) != (long)size) - xabort(); + xabort("xread"); } /* @@ -133,7 +133,7 @@ intercept_setup_log(const char *path_base, const char *trunc) log_fd = syscall_no_intercept(SYS_open, path, flags, 0700); if (log_fd < 0) - xabort(); + xabort("setup_log"); } /* diff --git a/src/patcher.c b/src/patcher.c index 0b2fcbd0..7bbcad9d 100644 --- a/src/patcher.c +++ b/src/patcher.c @@ -148,7 +148,7 @@ create_jump(unsigned char opcode, unsigned char *from, void *to) ptrdiff_t delta = ((unsigned char *)to) - (from + JUMP_INS_SIZE); if (delta > ((ptrdiff_t)INT32_MAX) || delta < ((ptrdiff_t)INT32_MIN)) - xabort(); + xabort("create_jump distance check"); int32_t delta32 = (int32_t)delta; unsigned char *d = (unsigned char *)&delta32; @@ -181,7 +181,7 @@ check_trampoline_usage(const struct intercept_desc *desc) size_t used = (size_t)(desc->next_trampoline - desc->trampoline_table); if (used + TRAMPOLINE_SIZE >= desc->trampoline_table_size) - xabort(); + xabort("trampoline space not enough"); } /* @@ -490,7 +490,8 @@ create_patch_wrappers(struct intercept_desc *desc) patch->syscall_offset); intercept_log(buffer, (size_t)l); - xabort(); + xabort("not enough space for patching" + " around syscal"); } } @@ -719,7 +720,8 @@ create_wrapper(struct patch_desc *patch, } if (patch->syscall_offset > UINT32_MAX) - xabort(); /* libc larger than 2 gigabytes? wow */ + xabort("patch->syscall_offset > UINT32_MAX"); + /* libc larger than 2 gigabytes? wow */ /* the instruction pushing the syscall's address to the stack */ create_push_imm(begin + o_push_origin, (uint32_t)patch->syscall_offset); @@ -790,7 +792,7 @@ create_short_jump(unsigned char *from, unsigned char *to) ptrdiff_t d = to - (from + 2); if (d < - 128 || d > 127) - xabort(); + xabort("create_short_jump distance check"); from[0] = SHORT_JMP_OPCODE; from[1] = (unsigned char)((char)d); @@ -824,14 +826,14 @@ activate_patches(struct intercept_desc *desc) if (syscall_no_intercept(SYS_mprotect, first_page, size, PROT_READ | PROT_WRITE | PROT_EXEC) != 0) - xabort(); + xabort("mprotect PROT_READ | PROT_WRITE | PROT_EXEC"); for (unsigned i = 0; i < desc->count; ++i) { const struct patch_desc *patch = desc->items + i; if (patch->dst_jmp_patch < desc->text_start || patch->dst_jmp_patch > desc->text_end) - xabort(); + xabort("dst_jmp_patch outside text"); /* * The dst_jmp_patch pointer contains the address where @@ -902,7 +904,7 @@ activate_patches(struct intercept_desc *desc) if (syscall_no_intercept(SYS_mprotect, first_page, size, PROT_READ | PROT_EXEC) != 0) - xabort(); + xabort("mprotect PROT_READ | PROT_EXEC"); } /* @@ -920,7 +922,7 @@ next_asm_wrapper_space(void) unsigned char *result; if (next + tmpl_size + PAGE_SIZE > sizeof(asm_wrapper_space)) - xabort(); + xabort("not enough space in asm_wrapper_space"); result = asm_wrapper_space + next; @@ -943,5 +945,5 @@ mprotect_asm_wrappers(void) round_down_address(asm_wrapper_space + PAGE_SIZE), sizeof(asm_wrapper_space) - PAGE_SIZE, PROT_READ | PROT_EXEC) != 0) - xabort(); + xabort("mprotect_asm_wrappers PROT_READ | PROT_EXEC"); }