dfsan_interface.h 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. //===-- dfsan_interface.h -------------------------------------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file is a part of DataFlowSanitizer.
  10. //
  11. // Public interface header.
  12. //===----------------------------------------------------------------------===//
  13. #ifndef DFSAN_INTERFACE_H
  14. #define DFSAN_INTERFACE_H
  15. #include <sanitizer/common_interface_defs.h>
  16. #include <stddef.h>
  17. #include <stdint.h>
  18. #ifdef __cplusplus
  19. extern "C" {
  20. #endif
  21. typedef uint8_t dfsan_label;
  22. typedef uint32_t dfsan_origin;
  23. /// Signature of the callback argument to dfsan_set_write_callback().
  24. typedef void(SANITIZER_CDECL *dfsan_write_callback_t)(int fd, const void *buf,
  25. size_t count);
  26. /// Signature of the callback argument to dfsan_set_conditional_callback().
  27. typedef void(SANITIZER_CDECL *dfsan_conditional_callback_t)(
  28. dfsan_label label, dfsan_origin origin);
  29. /// Signature of the callback argument to dfsan_set_reaches_function_callback().
  30. /// The description is intended to hold the name of the variable.
  31. typedef void(SANITIZER_CDECL *dfsan_reaches_function_callback_t)(
  32. dfsan_label label, dfsan_origin origin, const char *file, unsigned int line,
  33. const char *function);
  34. /// Computes the union of \c l1 and \c l2, resulting in a union label.
  35. dfsan_label SANITIZER_CDECL dfsan_union(dfsan_label l1, dfsan_label l2);
  36. /// Sets the label for each address in [addr,addr+size) to \c label.
  37. void SANITIZER_CDECL dfsan_set_label(dfsan_label label, void *addr,
  38. size_t size);
  39. /// Sets the label for each address in [addr,addr+size) to the union of the
  40. /// current label for that address and \c label.
  41. void SANITIZER_CDECL dfsan_add_label(dfsan_label label, void *addr,
  42. size_t size);
  43. /// Retrieves the label associated with the given data.
  44. ///
  45. /// The type of 'data' is arbitrary. The function accepts a value of any type,
  46. /// which can be truncated or extended (implicitly or explicitly) as necessary.
  47. /// The truncation/extension operations will preserve the label of the original
  48. /// value.
  49. dfsan_label SANITIZER_CDECL dfsan_get_label(long data);
  50. /// Retrieves the immediate origin associated with the given data. The returned
  51. /// origin may point to another origin.
  52. ///
  53. /// The type of 'data' is arbitrary.
  54. dfsan_origin SANITIZER_CDECL dfsan_get_origin(long data);
  55. /// Retrieves the label associated with the data at the given address.
  56. dfsan_label SANITIZER_CDECL dfsan_read_label(const void *addr, size_t size);
  57. /// Return the origin associated with the first taint byte in the size bytes
  58. /// from the address addr.
  59. dfsan_origin SANITIZER_CDECL dfsan_read_origin_of_first_taint(const void *addr,
  60. size_t size);
  61. /// Returns whether the given label contains the label elem.
  62. int SANITIZER_CDECL dfsan_has_label(dfsan_label label, dfsan_label elem);
  63. /// Flushes the DFSan shadow, i.e. forgets about all labels currently associated
  64. /// with the application memory. Use this call to start over the taint tracking
  65. /// within the same process.
  66. ///
  67. /// Note: If another thread is working with tainted data during the flush, that
  68. /// taint could still be written to shadow after the flush.
  69. void SANITIZER_CDECL dfsan_flush(void);
  70. /// Sets a callback to be invoked on calls to write(). The callback is invoked
  71. /// before the write is done. The write is not guaranteed to succeed when the
  72. /// callback executes. Pass in NULL to remove any callback.
  73. void SANITIZER_CDECL
  74. dfsan_set_write_callback(dfsan_write_callback_t labeled_write_callback);
  75. /// Sets a callback to be invoked on any conditional expressions which have a
  76. /// taint label set. This can be used to find where tainted data influences
  77. /// the behavior of the program.
  78. /// These callbacks will only be added when -dfsan-conditional-callbacks=true.
  79. void SANITIZER_CDECL
  80. dfsan_set_conditional_callback(dfsan_conditional_callback_t callback);
  81. /// Conditional expressions occur during signal handlers.
  82. /// Making callbacks that handle signals well is tricky, so when
  83. /// -dfsan-conditional-callbacks=true, conditional expressions used in signal
  84. /// handlers will add the labels they see into a global (bitwise-or together).
  85. /// This function returns all label bits seen in signal handler conditions.
  86. dfsan_label SANITIZER_CDECL dfsan_get_labels_in_signal_conditional();
  87. /// Sets a callback to be invoked when tainted data reaches a function.
  88. /// This could occur at function entry, or at a load instruction.
  89. /// These callbacks will only be added if -dfsan-reaches-function-callbacks=1.
  90. void SANITIZER_CDECL
  91. dfsan_set_reaches_function_callback(dfsan_reaches_function_callback_t callback);
  92. /// Making callbacks that handle signals well is tricky, so when
  93. /// -dfsan-reaches-function-callbacks=true, functions reached in signal
  94. /// handlers will add the labels they see into a global (bitwise-or together).
  95. /// This function returns all label bits seen during signal handlers.
  96. dfsan_label SANITIZER_CDECL dfsan_get_labels_in_signal_reaches_function();
  97. /// Interceptor hooks.
  98. /// Whenever a dfsan's custom function is called the corresponding
  99. /// hook is called it non-zero. The hooks should be defined by the user.
  100. /// The primary use case is taint-guided fuzzing, where the fuzzer
  101. /// needs to see the parameters of the function and the labels.
  102. /// FIXME: implement more hooks.
  103. void SANITIZER_CDECL dfsan_weak_hook_memcmp(void *caller_pc, const void *s1,
  104. const void *s2, size_t n,
  105. dfsan_label s1_label,
  106. dfsan_label s2_label,
  107. dfsan_label n_label);
  108. void SANITIZER_CDECL dfsan_weak_hook_strncmp(void *caller_pc, const char *s1,
  109. const char *s2, size_t n,
  110. dfsan_label s1_label,
  111. dfsan_label s2_label,
  112. dfsan_label n_label);
  113. /// Prints the origin trace of the label at the address addr to stderr. It also
  114. /// prints description at the beginning of the trace. If origin tracking is not
  115. /// on, or the address is not labeled, it prints nothing.
  116. void SANITIZER_CDECL dfsan_print_origin_trace(const void *addr,
  117. const char *description);
  118. /// As above, but use an origin id from dfsan_get_origin() instead of address.
  119. /// Does not include header line with taint label and address information.
  120. void SANITIZER_CDECL dfsan_print_origin_id_trace(dfsan_origin origin);
  121. /// Prints the origin trace of the label at the address \p addr to a
  122. /// pre-allocated output buffer. If origin tracking is not on, or the address is
  123. /// not labeled, it prints nothing.
  124. ///
  125. /// Typical usage:
  126. /// \code
  127. /// char kDescription[] = "...";
  128. /// char buf[1024];
  129. /// dfsan_sprint_origin_trace(&tainted_var, kDescription, buf, sizeof(buf));
  130. /// \endcode
  131. ///
  132. /// Typical usage that handles truncation:
  133. /// \code
  134. /// char buf[1024];
  135. /// int len = dfsan_sprint_origin_trace(&var, nullptr, buf, sizeof(buf));
  136. ///
  137. /// if (len < sizeof(buf)) {
  138. /// ProcessOriginTrace(buf);
  139. /// } else {
  140. /// char *tmpbuf = new char[len + 1];
  141. /// dfsan_sprint_origin_trace(&var, nullptr, tmpbuf, len + 1);
  142. /// ProcessOriginTrace(tmpbuf);
  143. /// delete[] tmpbuf;
  144. /// }
  145. /// \endcode
  146. ///
  147. /// \param addr The tainted memory address whose origin we are printing.
  148. /// \param description A description printed at the beginning of the trace.
  149. /// \param [out] out_buf The output buffer to write the results to.
  150. /// \param out_buf_size The size of \p out_buf.
  151. ///
  152. /// \returns The number of symbols that should have been written to \p out_buf
  153. /// (not including trailing null byte '\0'). Thus, the string is truncated iff
  154. /// return value is not less than \p out_buf_size.
  155. size_t SANITIZER_CDECL dfsan_sprint_origin_trace(const void *addr,
  156. const char *description,
  157. char *out_buf,
  158. size_t out_buf_size);
  159. /// As above, but use an origin id from dfsan_get_origin() instead of address.
  160. /// Does not include header line with taint label and address information.
  161. size_t SANITIZER_CDECL dfsan_sprint_origin_id_trace(dfsan_origin origin,
  162. char *out_buf,
  163. size_t out_buf_size);
  164. /// Prints the stack trace leading to this call to a pre-allocated output
  165. /// buffer.
  166. ///
  167. /// For usage examples, see dfsan_sprint_origin_trace.
  168. ///
  169. /// \param [out] out_buf The output buffer to write the results to.
  170. /// \param out_buf_size The size of \p out_buf.
  171. ///
  172. /// \returns The number of symbols that should have been written to \p out_buf
  173. /// (not including trailing null byte '\0'). Thus, the string is truncated iff
  174. /// return value is not less than \p out_buf_size.
  175. size_t SANITIZER_CDECL dfsan_sprint_stack_trace(char *out_buf,
  176. size_t out_buf_size);
  177. /// Retrieves the very first origin associated with the data at the given
  178. /// address.
  179. dfsan_origin SANITIZER_CDECL dfsan_get_init_origin(const void *addr);
  180. /// Returns the value of -dfsan-track-origins.
  181. /// * 0: do not track origins.
  182. /// * 1: track origins at memory store operations.
  183. /// * 2: track origins at memory load and store operations.
  184. int SANITIZER_CDECL dfsan_get_track_origins(void);
  185. #ifdef __cplusplus
  186. } // extern "C"
  187. template <typename T> void dfsan_set_label(dfsan_label label, T &data) {
  188. dfsan_set_label(label, (void *)&data, sizeof(T));
  189. }
  190. #endif
  191. #endif // DFSAN_INTERFACE_H