dfsan_interface.h 7.9 KB

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