/** * Get the parent object of a breakpoint. * Thats needed for the mprotect stuff inside the breakpoint handler * @param addr * @return */ elfshobj_t *e2dbg_get_parent_object(eresi_Addr addr) { elfsh_Phdr *cur; elfshobj_t *curfile; elfshsect_t *cursect; char **keys; int index; int keynbr; PROFILER_IN(__FILE__, __FUNCTION__, __LINE__); keys = hash_get_keys(&world.curjob->loaded, &keynbr); for (index = 0; index < keynbr; index++) { curfile = hash_get(&world.curjob->loaded, keys[index]); cursect = elfsh_get_parent_section(curfile, addr, NULL); if (cursect) { cur = elfsh_get_parent_segment(curfile, cursect); if (cur) PROFILER_ROUT(__FILE__, __FUNCTION__, __LINE__, curfile); } } /* Parent object not found */ PROFILER_ERR(__FILE__, __FUNCTION__, __LINE__, "Unable to get parent object addr", (NULL)); }
/** * @brief Put write capability on the zone * @param file * @param addr * @param sz * @return */ int elfsh_munprotect_userland(elfshobj_t *file, eresi_Addr addr, uint32_t sz) { elfshsect_t *sect; elfsh_Phdr *phdr; int retval; int prot; PROFILER_IN(__FILE__, __FUNCTION__, __LINE__); if (!elfsh_is_runtime_mode()) PROFILER_ROUT(__FILE__, __FUNCTION__, __LINE__, 0); sect = elfsh_get_parent_section(file, addr, NULL); if (!sect) PROFILER_ERR(__FILE__, __FUNCTION__, __LINE__, "Cannot find parent section", -1); phdr = elfsh_get_parent_segment(file, sect); prot = 0; if (elfsh_segment_is_readable(phdr)) prot |= PROT_READ; if (elfsh_segment_is_writable(phdr)) prot |= PROT_WRITE; if (elfsh_segment_is_executable(phdr)) prot |= PROT_EXEC; retval = mprotect((void *) (addr - addr % getpagesize()), getpagesize(), PROT_READ | PROT_WRITE | PROT_EXEC); if (retval != 0) { perror("munprotect"); PROFILER_ERR(__FILE__, __FUNCTION__, __LINE__, "Failed mprotect", -1); } /* Return the original rights */ PROFILER_ROUT(__FILE__, __FUNCTION__, __LINE__, prot); }