vtv_fail.cc 8.5 KB

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