123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130 |
- /* Copyright Yann Weber <asmsh@yannweb.net>
- This file is part of asmsh.
-
- asmsh is free software: you can redistribute it and/or modify it under the
- terms of the GNU General Public License as published by the Free Software
- Foundation, either version 3 of the License, or any later version.
-
- asmsh is distributed in the hope that it will be useful, but WITHOUT ANY
- WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
- FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
- details.
-
- You should have received a copy of the GNU General Public License along
- with asmsh. If not, see <https://www.gnu.org/licenses/>.
- */
- #ifndef ASMSH_ASM_ENV_H
- #define ASMSH_ASM_ENV_H
- #include "config.h"
-
- #include <errno.h>
- #include <stdlib.h>
- #include <stdio.h>
- #include <sys/ptrace.h>
- #include <sys/types.h>
- #include <sys/user.h>
- #include <sys/wait.h>
-
- #include "mmap_parse.h"
- #include "compile.h"
- #include "breakpoints.h"
-
- ///! Initial size of the child's memory map with PROT_EXEC permission
- #define ASMSH_CHILD_TEXT_MAP_SZ 0x1000 // defined in child.s
-
- typedef struct asmsh_env_s asmsh_env_t;
-
- struct asmsh_env_s
- {
- ///! Path on wich the child will execve
- char *childpath;
- ///! Child process pid
- pid_t pid;
- ///! Last status fetched from child using waitpid
- int status;
- ///! Child process registers value
- struct user_regs_struct regs;
- ///! Child's memory map
- child_mmap_l mmap;
-
- /**Pointer, in child memory, of the mmap with PROT_EXEC
- * perm allowing use to write code to be executed by the
- * child
- */
- unsigned char *txt_map_addr;
- /** child's map size */
- size_t txt_map_sz;
- /** Pointer on current write addr in child's map */
- unsigned char *txt_map_ptr;
-
- /** List of breakpoints addresses in child's memory */
- asmsh_brk_t brks;
-
- /** @todo check & delete useless properties */
- /** Pointer on the next addr where we should write some
- * compiled bytecode */
- unsigned char *code_write_ptr;
-
- /** Pointer, in child memory, of the stack */
- unsigned char *stack_addr;
- size_t stack_sz;
-
- };
-
- /** Run a new asm child and return the environment
- * @param const char* the path of the executable to execve in child
- * @return NULL if error or a pointer on a newly allocated env
- */
- asmsh_env_t* asmsh_env(const char *childpath);
- /** Free an env returned by @ref asmsh_env() */
- void asmsh_env_free(asmsh_env_t *asmenv);
-
- /** Write a buffer in tracee memory
- *
- * Handle un-alligned addr by reading tracee memory before writing if
- * needed.
- * @param asmsh_env_t*
- * @param void* The address in tracee memory
- * @param const unsigned char* The buffer to write
- * @param size_t number of bytes to write
- * @return 0 if no errors else -1 with errno set
- */
- int asmsh_env_write_mem(asmsh_env_t *env, void *addr, const unsigned char *buf, size_t buf_sz);
-
- /** Write bytecode at the next location available in the text map
- * @param asmsh_env_t* The run environment
- * @param asmsh_bytecode_t* The bytecode to copy
- * @return 0 if no error occured 1 if child finished in unexpected state or -1
- * on other errror
- */
- int asmsh_env_write_code(asmsh_env_t *env, asmsh_bytecode_t *bcode);
-
- /** Run the next instruction
- * @param asmsh_env_t* The run environment
- * @param int* If not null will be set to the status of the child PID (error
- * cases)
- * @return 0 if no error else -1, if the child is not trapped as expected, 1 is
- * returned and wstatus is set to the child status (see man 2 waitpid)
- */
- int asmsh_env_step(asmsh_env_t *env, int *status);
-
- /** Update both registers and memory maps */
- int asmsh_env_update(asmsh_env_t *asmenv);
- /** Update env registers from child */
- int asmsh_env_update_regs(asmsh_env_t *asmenv);
- /** Update child's memory map in env */
- int asmsh_env_update_maps(asmsh_env_t *asmenv);
-
- /** Convert a child's memory address into a "txt" addr
- *
- * The local address is calculated as an offset from the start of the child's
- * "txt" map (the one with PROT_READ | PROT_EXEC perm)
- *
- * @param asmsh_env_t*
- * @param void* the address to convert
- * @param void** the converted txt offset
- * @return 0 if no error else -1
- */
- int asmsh_env_txt_addr(asmsh_env_t *asmenv, void *addr, void **loc);
-
- #endif
|