vtv_fail.cc 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /* Copyright (C) 2012-2013
  2. Free Software Foundation
  3. This file is part of GCC.
  4. GCC is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3, or (at your option)
  7. any later version.
  8. GCC is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. Under Section 7 of GPL version 3, you are granted additional
  13. permissions described in the GCC Runtime Library Exception, version
  14. 3.1, as published by the Free Software Foundation.
  15. You should have received a copy of the GNU General Public License and
  16. a copy of the GCC Runtime Library Exception along with this program;
  17. see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  18. <http://www.gnu.org/licenses/>. */
  19. /* This file is part of the vtable security feature implementation.
  20. The vtable security feature is designed to detect when a virtual
  21. call is about to be made through an invalid vtable pointer
  22. (possibly due to data corruption or malicious attacks).
  23. This file also contains the failure functions that get called when
  24. a vtable pointer is not found in the data set. Two particularly
  25. important functions are __vtv_verify_fail and __vtv_really_fail.
  26. They are both externally visible. __vtv_verify_fail is defined in
  27. such a way that it can be replaced by a programmer, if desired. It
  28. is the function that __VLTVerifyVtablePointer calls if it can't
  29. find the pointer in the data set. Allowing the programmer to
  30. overwrite this function means that he/she can do some alternate
  31. verification, including NOT failing in certain specific cases, if
  32. desired. This may be the case if the programmer has to deal wtih
  33. unverified third party software, for example. __vtv_really_fail is
  34. available for the programmer to call from his version of
  35. __vtv_verify_fail, if he decides the failure is real.
  36. */
  37. #include <stdlib.h>
  38. #include <stdio.h>
  39. #include <string.h>
  40. #if !defined (__CYGWIN__) && !defined (__MINGW32__)
  41. #include <execinfo.h>
  42. #endif
  43. #include <unistd.h>
  44. #include "vtv_utils.h"
  45. #include "vtv_fail.h"
  46. /* This is used to disable aborts for debugging purposes. */
  47. bool vtv_no_abort = false;
  48. extern "C" {
  49. /* __fortify_fail is a function in glibc that calls __libc_message,
  50. causing it to print out a program termination error message
  51. (including the name of the binary being terminated), a stack
  52. trace where the error occurred, and a memory map dump. Ideally
  53. we would have called __libc_message directly, but that function
  54. does not appear to be accessible to functions outside glibc,
  55. whereas __fortify_fail is. We call __fortify_fail from
  56. __vtv_really_fail. We looked at calling __libc_fatal, which is
  57. externally accessible, but it does not do the back trace and
  58. memory dump. */
  59. extern void __fortify_fail (const char *) __attribute__((noreturn));
  60. } /* extern "C" */
  61. const unsigned long SET_HANDLE_HANDLE_BIT = 0x2;
  62. /* Instantiate the template classes (in vtv_set.h) for our particular
  63. hash table needs. */
  64. typedef void * vtv_set_handle;
  65. typedef vtv_set_handle * vtv_set_handle_handle;
  66. static int vtv_failures_log_fd = -1;
  67. /* Open error logging file, if not already open, and write vtable
  68. verification failure messages (LOG_MSG) to the log file. Also
  69. generate a backtrace in the log file, if GENERATE_BACKTRACE is
  70. set. */
  71. static void
  72. log_error_message (const char *log_msg, bool generate_backtrace)
  73. {
  74. if (vtv_failures_log_fd == -1)
  75. vtv_failures_log_fd = vtv_open_log ("vtable_verification_failures.log");
  76. if (vtv_failures_log_fd == -1)
  77. return;
  78. vtv_add_to_log (vtv_failures_log_fd, "%s", log_msg);
  79. if (generate_backtrace)
  80. {
  81. #define STACK_DEPTH 20
  82. void *callers[STACK_DEPTH];
  83. #if !defined (__CYGWIN__) && !defined (__MINGW32__)
  84. int actual_depth = backtrace (callers, STACK_DEPTH);
  85. backtrace_symbols_fd (callers, actual_depth, vtv_failures_log_fd);
  86. #endif
  87. }
  88. }
  89. /* In the case where a vtable map variable is the only instance of the
  90. variable we have seen, it points directly to the set of valid
  91. vtable pointers. All subsequent instances of the 'same' vtable map
  92. variable point to the first vtable map variable. This function,
  93. given a vtable map variable PTR, checks a bit to see whether it's
  94. pointing directly to the data set or to the first vtable map
  95. variable. */
  96. static inline bool
  97. is_set_handle_handle (void * ptr)
  98. {
  99. return ((unsigned long) ptr & SET_HANDLE_HANDLE_BIT)
  100. == SET_HANDLE_HANDLE_BIT;
  101. }
  102. /* Returns the actual pointer value of a vtable map variable, PTR (see
  103. comments for is_set_handle_handle for more details). */
  104. static inline vtv_set_handle *
  105. ptr_from_set_handle_handle (void * ptr)
  106. {
  107. return (vtv_set_handle *) ((unsigned long) ptr & ~SET_HANDLE_HANDLE_BIT);
  108. }
  109. /* Given a vtable map variable, PTR, this function sets the bit that
  110. says this is the second (or later) instance of a vtable map
  111. variable. */
  112. static inline vtv_set_handle_handle
  113. set_handle_handle (vtv_set_handle * ptr)
  114. {
  115. return (vtv_set_handle_handle) ((unsigned long) ptr | SET_HANDLE_HANDLE_BIT);
  116. }
  117. /* This function is called from __VLTVerifyVtablePointerDebug; it
  118. sends as much debugging information as it can to the error log
  119. file, then calls __vtv_verify_fail. SET_HANDLE_PTR is the pointer
  120. to the set of valid vtable pointers, VTBL_PTR is the pointer that
  121. was not found in the set, and DEBUG_MSG is the message to be
  122. written to the log file before failing. n */
  123. void
  124. __vtv_verify_fail_debug (void **set_handle_ptr, const void *vtbl_ptr,
  125. const char *debug_msg)
  126. {
  127. log_error_message (debug_msg, false);
  128. /* Call the public interface in case it has been overwritten by
  129. user. */
  130. __vtv_verify_fail (set_handle_ptr, vtbl_ptr);
  131. log_error_message ("Returned from __vtv_verify_fail."
  132. " Secondary verification succeeded.\n", false);
  133. }
  134. /* This function calls __fortify_fail with a FAILURE_MSG and then
  135. calls abort. */
  136. void
  137. __vtv_really_fail (const char *failure_msg)
  138. {
  139. __fortify_fail (failure_msg);
  140. /* We should never get this far; __fortify_fail calls __libc_message
  141. which prints out a back trace and a memory dump and then is
  142. supposed to call abort, but let's play it safe anyway and call abort
  143. ourselves. */
  144. abort ();
  145. }
  146. /* This function takes an error MSG, a vtable map variable
  147. (DATA_SET_PTR) and a vtable pointer (VTBL_PTR). It is called when
  148. an attempt to verify VTBL_PTR with the set pointed to by
  149. DATA_SET_PTR failed. It outputs a failure message with the
  150. addresses involved, and calls __vtv_really_fail. */
  151. static void
  152. vtv_fail (const char *msg, void **data_set_ptr, const void *vtbl_ptr)
  153. {
  154. char buffer[128];
  155. int buf_len;
  156. const char *format_str =
  157. "*** Unable to verify vtable pointer (%p) in set (%p) *** \n";
  158. snprintf (buffer, sizeof (buffer), format_str, vtbl_ptr,
  159. is_set_handle_handle(*data_set_ptr) ?
  160. ptr_from_set_handle_handle (*data_set_ptr) :
  161. *data_set_ptr);
  162. buf_len = strlen (buffer);
  163. /* Send this to to stderr. */
  164. write (2, buffer, buf_len);
  165. if (!vtv_no_abort)
  166. __vtv_really_fail (msg);
  167. }
  168. /* Send information about what we were trying to do when verification
  169. failed to the error log, then call vtv_fail. This function can be
  170. overwritten/replaced by the user, to implement a secondary
  171. verification function instead. DATA_SET_PTR is the vtable map
  172. variable used for the failed verification, and VTBL_PTR is the
  173. vtable pointer that was not found in the set. */
  174. void
  175. __vtv_verify_fail (void **data_set_ptr, const void *vtbl_ptr)
  176. {
  177. char log_msg[256];
  178. snprintf (log_msg, sizeof (log_msg), "Looking for vtable %p in set %p.\n",
  179. vtbl_ptr,
  180. is_set_handle_handle (*data_set_ptr) ?
  181. ptr_from_set_handle_handle (*data_set_ptr) :
  182. *data_set_ptr);
  183. log_error_message (log_msg, false);
  184. const char *format_str =
  185. "*** Unable to verify vtable pointer (%p) in set (%p) *** \n";
  186. snprintf (log_msg, sizeof (log_msg), format_str, vtbl_ptr, *data_set_ptr);
  187. log_error_message (log_msg, false);
  188. log_error_message (" Backtrace: \n", true);
  189. const char *fail_msg = "Potential vtable pointer corruption detected!!\n";
  190. vtv_fail (fail_msg, data_set_ptr, vtbl_ptr);
  191. }