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

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