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 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. struct asmsh_env_s
  30. {
  31. ///! Path on wich the child will execve
  32. char *childpath;
  33. ///! Child process pid
  34. pid_t pid;
  35. ///! Last status fetched from child using waitpid
  36. int status;
  37. ///! Child process registers value
  38. struct user_regs_struct regs;
  39. ///! Child's memory map
  40. child_mmap_l mmap;
  41. /**Pointer, in child memory, of the mmap with PROT_EXEC
  42. * perm allowing use to write code to be executed by the
  43. * child
  44. */
  45. unsigned char *txt_map_addr;
  46. /** child's map size */
  47. size_t txt_map_sz;
  48. /** Pointer on current write addr in child's map */
  49. unsigned char *txt_map_ptr;
  50. /** List of breakpoints addresses in child's memory */
  51. asmsh_brk_t brks;
  52. /** @todo check & delete useless properties */
  53. /** Pointer on the next addr where we should write some
  54. * compiled bytecode */
  55. unsigned char *code_write_ptr;
  56. /** Pointer, in child memory, of the stack */
  57. unsigned char *stack_addr;
  58. size_t stack_sz;
  59. };
  60. /** Run a new asm child and return the environment
  61. * @param const char* the path of the executable to execve in child
  62. * @return NULL if error or a pointer on a newly allocated env
  63. */
  64. asmsh_env_t* asmsh_env(const char *childpath);
  65. /** Free an env returned by @ref asmsh_env() */
  66. void asmsh_env_free(asmsh_env_t *asmenv);
  67. /** Write a buffer in tracee memory
  68. *
  69. * Handle un-alligned addr by reading tracee memory before writing if
  70. * needed.
  71. * @param asmsh_env_t*
  72. * @param void* The address in tracee memory
  73. * @param const unsigned char* The buffer to write
  74. * @param size_t number of bytes to write
  75. * @return 0 if no errors else -1 with errno set
  76. */
  77. int asmsh_env_write_mem(asmsh_env_t *env, void *addr, const unsigned char *buf, size_t buf_sz);
  78. /** Write bytecode at the next location available in the text map
  79. * @param asmsh_env_t* The run environment
  80. * @param asmsh_bytecode_t* The bytecode to copy
  81. * @return 0 if no error occured 1 if child finished in unexpected state or -1
  82. * on other errror
  83. */
  84. int asmsh_env_write_code(asmsh_env_t *env, asmsh_bytecode_t *bcode);
  85. /** Run the next instruction
  86. * @param asmsh_env_t* The run environment
  87. * @param int* If not null will be set to the status of the child PID (error
  88. * cases)
  89. * @return 0 if no error else -1, if the child is not trapped as expected, 1 is
  90. * returned and wstatus is set to the child status (see man 2 waitpid)
  91. */
  92. int asmsh_env_step(asmsh_env_t *env, int *status);
  93. /** Update both registers and memory maps */
  94. int asmsh_env_update(asmsh_env_t *asmenv);
  95. /** Update env registers from child */
  96. int asmsh_env_update_regs(asmsh_env_t *asmenv);
  97. /** Update child's memory map in env */
  98. int asmsh_env_update_maps(asmsh_env_t *asmenv);
  99. /** Convert a child's memory address into a "txt" addr
  100. *
  101. * The local address is calculated as an offset from the start of the child's
  102. * "txt" map (the one with PROT_READ | PROT_EXEC perm)
  103. *
  104. * @param asmsh_env_t*
  105. * @param void* the address to convert
  106. * @param void** the converted txt offset
  107. * @return 0 if no error else -1
  108. */
  109. int asmsh_env_txt_addr(asmsh_env_t *asmenv, void *addr, void **loc);
  110. #endif