A shell that runs x86_64 assembly
c
x86-64
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

asm_env.h 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /* Copyright Yann Weber <asmsh@yannweb.net>
  2. This file is part of asmsh.
  3. asmsh is free software: you can redistribute it and/or modify it under the
  4. terms of the GNU General Public License as published by the Free Software
  5. Foundation, either version 3 of the License, or any later version.
  6. asmsh is distributed in the hope that it will be useful, but WITHOUT ANY
  7. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  8. FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  9. details.
  10. You should have received a copy of the GNU General Public License along
  11. with asmsh. If not, see <https://www.gnu.org/licenses/>.
  12. */
  13. #ifndef ASMSH_ASM_ENV_H
  14. #define ASMSH_ASM_ENV_H
  15. #include "config.h"
  16. #include <errno.h>
  17. #include <stdlib.h>
  18. #include <stdio.h>
  19. #include <sys/ptrace.h>
  20. #include <sys/types.h>
  21. #include <sys/user.h>
  22. #include <sys/wait.h>
  23. #include "mmap_parse.h"
  24. #include "compile.h"
  25. #include "breakpoints.h"
  26. ///! Initial size of the child's memory map with PROT_EXEC permission
  27. #define ASMSH_CHILD_TEXT_MAP_SZ 0x1000 // defined in child.s
  28. typedef struct asmsh_env_s asmsh_env_t;
  29. #include "shell_sym.h"
  30. struct asmsh_env_s
  31. {
  32. ///! Path on wich the child will execve
  33. char *childpath;
  34. ///! Child process pid
  35. pid_t pid;
  36. ///! Last status fetched from child using waitpid
  37. int status;
  38. ///! Child process registers value
  39. struct user_regs_struct regs;
  40. ///! Child's memory map
  41. child_mmap_l mmap;
  42. /**Pointer, in child memory, of the mmap with PROT_EXEC
  43. * perm allowing use to write code to be executed by the
  44. * child
  45. */
  46. unsigned char *txt_map_addr;
  47. /** child's map size */
  48. size_t txt_map_sz;
  49. /** Pointer on current write addr in child's memory (write mode) */
  50. unsigned char *write_ptr;
  51. /** List of breakpoints addresses in child's memory */
  52. asmsh_brk_t brks;
  53. /** List of labels addresses in child's memory */
  54. asmsh_symtable_t labels;
  55. /** @todo check & delete useless properties */
  56. /** Pointer on the next addr where we should write some
  57. * compiled bytecode */
  58. unsigned char *code_write_ptr;
  59. /** Pointer, in child memory, of the stack (useless) */
  60. //unsigned char *stack_addr;
  61. };
  62. /** Run a new asm child and return the environment
  63. * @param const char* the path of the executable to execve in child
  64. * @return NULL if error or a pointer on a newly allocated env
  65. */
  66. asmsh_env_t* asmsh_env(const char *childpath);
  67. /** Free an env returned by @ref asmsh_env() */
  68. void asmsh_env_free(asmsh_env_t *asmenv);
  69. /** Write a buffer in tracee memory
  70. *
  71. * Handle un-alligned addr by reading tracee memory before writing if
  72. * needed.
  73. * @param asmsh_env_t*
  74. * @param void* pointer on address in tracee memory
  75. * @param const unsigned char* The buffer to write
  76. * @param size_t number of bytes to write
  77. * @return 0 if no errors else -1 with errno set
  78. */
  79. int asmsh_env_write_mem(asmsh_env_t *env, uintptr_t *addr, const unsigned char *buf, size_t buf_sz);
  80. /** Write bytecode at the next location available in the text map
  81. * @param asmsh_env_t* The run environment
  82. * @param asmsh_bytecode_t* The bytecode to copy
  83. * @return 0 if no errors else -1 with errno set
  84. */
  85. int asmsh_env_write_code(asmsh_env_t *env, asmsh_bytecode_t *bcode);
  86. /** Run the next instruction
  87. * @param asmsh_env_t* The run environment
  88. * @param int* Will be set to the status of the child PID (error
  89. * cases)
  90. * @return 0 if no error else -1, if the child is not trapped as expected, 1 is
  91. * returned and wstatus is set to the child status (see man 2 waitpid)
  92. */
  93. int asmsh_env_step(asmsh_env_t *env, int *status);
  94. /** Run until next breakpoint
  95. * @param asmsh_env_t* The run environment
  96. * @param int* Will be set to the status of the child PID (error
  97. * cases)
  98. * @return 0 if no error else -1, if the child is not trapped as expected, 1 is
  99. * returned and wstatus is set to the child status (see man 2 waitpid)
  100. */
  101. int asmsh_env_run(asmsh_env_t *env, int *status);
  102. /** Update both registers and memory maps */
  103. int asmsh_env_update(asmsh_env_t *asmenv);
  104. /** Update env registers from child */
  105. int asmsh_env_update_regs(asmsh_env_t *asmenv);
  106. /** Update child's memory map in env */
  107. int asmsh_env_update_maps(asmsh_env_t *asmenv);
  108. /** Read data from tracee's memory
  109. * @param asmsh_env_t* Shell environment
  110. * @param void** Pointer on tracee's memory address
  111. * @param uint8_t Read buffer
  112. * @param size_t Read buffer size
  113. * @return 0 if no error else -1
  114. */
  115. int asmsh_env_read_mem(asmsh_env_t *env, uintptr_t *addr, uint8_t *buf, size_t buf_sz);
  116. /** Set a register value
  117. * @param asmsh_env_t*
  118. * @param const char* The register name
  119. * @param uint64_t The value to set
  120. * @return 0 or -1 on error
  121. */
  122. int asmsh_env_set_reg(asmsh_env_t *asmenv, const char *regname, uint64_t value);
  123. /** Return a pointer on a register given its name
  124. * @param struct user_regs_struct*
  125. * @param const char * register name
  126. * @return NULL if no register with given name else a pointer on the value
  127. */
  128. void *asmsh_env_reg_by_name(struct user_regs_struct *regs, const char *regname);
  129. /** Convert a child's memory address into a "txt" addr
  130. *
  131. * The local address is calculated as an offset from the start of the child's
  132. * "txt" map (the one with PROT_READ | PROT_EXEC perm)
  133. *
  134. * @param asmsh_env_t*
  135. * @param void* the address to convert
  136. * @param void** the converted txt offset
  137. * @return 0 if no error else -1
  138. */
  139. int asmsh_env_txt_addr(asmsh_env_t *asmenv, void *addr, void **loc);
  140. #endif