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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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 map */
  50. unsigned char *txt_map_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 */
  60. unsigned char *stack_addr;
  61. size_t stack_sz;
  62. };
  63. /** Run a new asm child and return the environment
  64. * @param const char* the path of the executable to execve in child
  65. * @return NULL if error or a pointer on a newly allocated env
  66. */
  67. asmsh_env_t* asmsh_env(const char *childpath);
  68. /** Free an env returned by @ref asmsh_env() */
  69. void asmsh_env_free(asmsh_env_t *asmenv);
  70. /** Write a buffer in tracee memory
  71. *
  72. * Handle un-alligned addr by reading tracee memory before writing if
  73. * needed.
  74. * @param asmsh_env_t*
  75. * @param void* The address in tracee memory
  76. * @param const unsigned char* The buffer to write
  77. * @param size_t number of bytes to write
  78. * @return 0 if no errors else -1 with errno set
  79. */
  80. int asmsh_env_write_mem(asmsh_env_t *env, void *addr, const unsigned char *buf, size_t buf_sz);
  81. /** Write bytecode at the next location available in the text map
  82. * @param asmsh_env_t* The run environment
  83. * @param asmsh_bytecode_t* The bytecode to copy
  84. * @return 0 if no errors else -1 with errno set
  85. */
  86. int asmsh_env_write_code(asmsh_env_t *env, asmsh_bytecode_t *bcode);
  87. /** Run the next instruction
  88. * @param asmsh_env_t* The run environment
  89. * @param int* Will be set to the status of the child PID (error
  90. * cases)
  91. * @return 0 if no error else -1, if the child is not trapped as expected, 1 is
  92. * returned and wstatus is set to the child status (see man 2 waitpid)
  93. */
  94. int asmsh_env_step(asmsh_env_t *env, int *status);
  95. /** Run until next breakpoint
  96. * @param asmsh_env_t* The run environment
  97. * @param int* Will be set to the status of the child PID (error
  98. * cases)
  99. * @return 0 if no error else -1, if the child is not trapped as expected, 1 is
  100. * returned and wstatus is set to the child status (see man 2 waitpid)
  101. */
  102. int asmsh_env_run(asmsh_env_t *env, int *status);
  103. /** Update both registers and memory maps */
  104. int asmsh_env_update(asmsh_env_t *asmenv);
  105. /** Update env registers from child */
  106. int asmsh_env_update_regs(asmsh_env_t *asmenv);
  107. /** Update child's memory map in env */
  108. int asmsh_env_update_maps(asmsh_env_t *asmenv);
  109. /** Convert a child's memory address into a "txt" addr
  110. *
  111. * The local address is calculated as an offset from the start of the child's
  112. * "txt" map (the one with PROT_READ | PROT_EXEC perm)
  113. *
  114. * @param asmsh_env_t*
  115. * @param void* the address to convert
  116. * @param void** the converted txt offset
  117. * @return 0 if no error else -1
  118. */
  119. int asmsh_env_txt_addr(asmsh_env_t *asmenv, void *addr, void **loc);
  120. #endif