hotpatch.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. * hotpatch is a sofile injection strategy.
  3. * Copyright (c) 2010-2011, Vikas Naresh Kumar, Selective Intellect LLC
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are met:
  8. *
  9. * * Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. *
  12. * * Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. *
  16. * * Neither the name of Selective Intellect LLC nor the
  17. * names of its contributors may be used to endorse or promote products
  18. * derived from this software without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  21. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  22. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  23. * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
  24. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  25. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  26. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  27. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  29. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. */
  31. #ifndef __LIBHOTPATCH_H__
  32. #define __LIBHOTPATCH_H__
  33. #ifdef __cplusplus
  34. extern "C" {
  35. #endif /* __cplusplus */
  36. #include <sys/types.h>
  37. #include <stdint.h>
  38. #define HOTPATCH_MAJOR_VERSION 0
  39. #define HOTPATCH_MINOR_VERSION 2
  40. #ifndef HOTPATCH_LINUX_START
  41. #define HOTPATCH_LINUX_START "_start"
  42. #endif
  43. typedef struct hotpatch_is_opaque hotpatch_t;
  44. /* Create the hotpatch object for the running process whose PID is given as an
  45. * argument. Returns a pointer to an opaque object that must be freed by
  46. * hotpatch_delete() function later to conserve memory.
  47. */
  48. hotpatch_t *hotpatch_create(pid_t, int verbosity);
  49. /*
  50. * delete memory and close all open handles related to the hotpatch'ed process.
  51. * This can lead to the hotpatch'ed process to be unstable if not done in the same
  52. * thread as create function above.
  53. */
  54. void hotpatch_destroy(hotpatch_t *);
  55. /*
  56. * Inject a shared object into the process and invoke the given symbol without
  57. * arguments. No thread will be created by hotpatch.
  58. * If the symbol is NULL, then _init() is expected to be in the library.
  59. * If data is NULL, no data will be copied over to the other process for the
  60. * symbol that is being invoked. If the symbol being invoked is _init(), then
  61. * data will be ignored. This data and datalen will be provided as arguments to
  62. * the symbol when invoked.
  63. * The return address of the dlopen() call can be optionally returned in
  64. * the outaddr variable.
  65. * The return value from the invocation of the symbol in the process can be
  66. * optionally returned in the outres variable. If the symbol is NULL, or if the
  67. * symbol returns void, then the return value will be undefined.
  68. */
  69. int hotpatch_inject_library(hotpatch_t *, const char *sofile,
  70. const char *symbol,
  71. const unsigned char *data, size_t datalen,
  72. uintptr_t *outaddr, uintptr_t *outres);
  73. /* AUXILLIARY FUNCTIONS */
  74. void hotpatch_version(int *major, int *minor);
  75. /* finds the symbol in the symbol table of executable and returns the memory
  76. * location of it. On a 64-bit system the running process can be 32 or 64 bit,
  77. * and hence they both need to be handled correctly or even simultaneously.
  78. * Returns not only the location of the symbol but also the type and size
  79. */
  80. uintptr_t hotpatch_read_symbol(hotpatch_t *, const char *symbol, int *symtype,
  81. size_t *symsize);
  82. /*
  83. * Get the entry point of the executable in question
  84. */
  85. uintptr_t hotpatch_get_entry_point(hotpatch_t *);
  86. /*
  87. * Attach to the process that you wanted to hotpatch. Returns 0 on success and 1
  88. * on failure.
  89. */
  90. int hotpatch_attach(hotpatch_t *);
  91. /*
  92. * Detach from the process that you wanted to hotpatch. Returns -1 on failure
  93. * or if nothing was attached earlier. Returns 0 if detaching succeeded.
  94. */
  95. int hotpatch_detach(hotpatch_t *);
  96. /*
  97. * Sets the execution pointer to point to the address given by the user.
  98. * Returns 0 on success and -1 on failure.
  99. */
  100. int hotpatch_set_execution_pointer(hotpatch_t *, uintptr_t location);
  101. int cw_inject_library(int pid, int verbose, char *dll, char *rootfs);
  102. #ifdef __cplusplus
  103. } /* end of extern C */
  104. #endif /* __cplusplus */
  105. #endif /* __LIBHOTPATCH_H__ */