dfsan_interface.h 9.0 KB

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