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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. ///! Initial size of the child's memory map with PROT_EXEC permission
  26. #define ASMSH_CHILD_TEXT_MAP_SZ 0x1000 // defined in child.s
  27. #define ASMSH_CHILD_PATH_DEFAULT "./child"
  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. void *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. void *txt_map_ptr;
  50. /** Pointer on the next addr where we should write some
  51. * compiled bytecode */
  52. void *code_write_ptr;
  53. /** Pointer, in child memory, of the stack */
  54. void *stack_addr;
  55. size_t stack_sz;
  56. };
  57. /** Run a new asm child and return the environment
  58. * @param const char* the path of the executable to execve in child
  59. * @return NULL if error or a pointer on a newly allocated env
  60. */
  61. asmsh_env_t* asmsh_env(const char *childpath);
  62. /** Free an env returned by @ref asmsh_env() */
  63. void asmsh_env_free(asmsh_env_t *asmenv);
  64. /** Write a buffer in tracee memory
  65. *
  66. * Handle un-alligned addr by reading tracee memory before writing if
  67. * needed.
  68. * @param asmsh_env_t*
  69. * @param void* The address in tracee memory
  70. * @param const unsigned char* The buffer to write
  71. * @param size_t number of bytes to write
  72. * @return 0 if no errors else -1 with errno set
  73. */
  74. int asmsh_env_write_mem(asmsh_env_t *env, void *addr, const unsigned char *buf, size_t buf_sz);
  75. /** Write bytecode at the next location available in the text map
  76. * @param asmsh_env_t* The run environment
  77. * @param asmsh_bytecode_t* The bytecode to copy
  78. * @return 0 if no error occured 1 if child finished in unexpected state or -1
  79. * on other errror
  80. */
  81. int asmsh_env_write_code(asmsh_env_t *env, asmsh_bytecode_t *bcode);
  82. /** Run the next instruction
  83. * @param asmsh_env_t* The run environment
  84. * @param int* If not null will be set to the status of the child PID (error cases)
  85. * @return 0 if no error from the child pid else -1 and wstatus is set to the child status
  86. */
  87. int asmsh_env_step(asmsh_env_t *env, int *status);
  88. /** Update both registers and memory maps */
  89. int asmsh_env_update(asmsh_env_t *asmenv);
  90. /** Update env registers from child */
  91. int asmsh_env_update_regs(asmsh_env_t *asmenv);
  92. /** Update child's memory map in env */
  93. int asmsh_env_update_maps(asmsh_env_t *asmenv);
  94. /** Convert a child's memory address into a "txt" addr
  95. *
  96. * The local address is calculated as an offset from the start of the child's
  97. * "txt" map (the one with PROT_READ | PROT_EXEC perm)
  98. *
  99. * @param asmsh_env_t*
  100. * @param void* the address to convert
  101. * @param void** the converted txt offset
  102. * @return 0 if no error else -1
  103. */
  104. int asmsh_env_txt_addr(asmsh_env_t *asmenv, void *addr, void **loc);
  105. #endif