DataFlowSanitizerDesign.rst 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. DataFlowSanitizer Design Document
  2. =================================
  3. This document sets out the design for DataFlowSanitizer, a general
  4. dynamic data flow analysis. Unlike other Sanitizer tools, this tool is
  5. not designed to detect a specific class of bugs on its own. Instead,
  6. it provides a generic dynamic data flow analysis framework to be used
  7. by clients to help detect application-specific issues within their
  8. own code.
  9. DataFlowSanitizer is a program instrumentation which can associate
  10. a number of taint labels with any data stored in any memory region
  11. accessible by the program. The analysis is dynamic, which means that
  12. it operates on a running program, and tracks how the labels propagate
  13. through that program. The tool shall support a large (>100) number
  14. of labels, such that programs which operate on large numbers of data
  15. items may be analysed with each data item being tracked separately.
  16. Use Cases
  17. ---------
  18. This instrumentation can be used as a tool to help monitor how data
  19. flows from a program's inputs (sources) to its outputs (sinks).
  20. This has applications from a privacy/security perspective in that
  21. one can audit how a sensitive data item is used within a program and
  22. ensure it isn't exiting the program anywhere it shouldn't be.
  23. Interface
  24. ---------
  25. A number of functions are provided which will create taint labels,
  26. attach labels to memory regions and extract the set of labels
  27. associated with a specific memory region. These functions are declared
  28. in the header file ``sanitizer/dfsan_interface.h``.
  29. .. code-block:: c
  30. /// Creates and returns a base label with the given description and user data.
  31. dfsan_label dfsan_create_label(const char *desc, void *userdata);
  32. /// Sets the label for each address in [addr,addr+size) to \c label.
  33. void dfsan_set_label(dfsan_label label, void *addr, size_t size);
  34. /// Sets the label for each address in [addr,addr+size) to the union of the
  35. /// current label for that address and \c label.
  36. void dfsan_add_label(dfsan_label label, void *addr, size_t size);
  37. /// Retrieves the label associated with the given data.
  38. ///
  39. /// The type of 'data' is arbitrary. The function accepts a value of any type,
  40. /// which can be truncated or extended (implicitly or explicitly) as necessary.
  41. /// The truncation/extension operations will preserve the label of the original
  42. /// value.
  43. dfsan_label dfsan_get_label(long data);
  44. /// Retrieves a pointer to the dfsan_label_info struct for the given label.
  45. const struct dfsan_label_info *dfsan_get_label_info(dfsan_label label);
  46. /// Returns whether the given label label contains the label elem.
  47. int dfsan_has_label(dfsan_label label, dfsan_label elem);
  48. /// If the given label label contains a label with the description desc, returns
  49. /// that label, else returns 0.
  50. dfsan_label dfsan_has_label_with_desc(dfsan_label label, const char *desc);
  51. Taint label representation
  52. --------------------------
  53. As stated above, the tool must track a large number of taint
  54. labels. This poses an implementation challenge, as most multiple-label
  55. tainting systems assign one label per bit to shadow storage, and
  56. union taint labels using a bitwise or operation. This will not scale
  57. to clients which use hundreds or thousands of taint labels, as the
  58. label union operation becomes O(n) in the number of supported labels,
  59. and data associated with it will quickly dominate the live variable
  60. set, causing register spills and hampering performance.
  61. Instead, a low overhead approach is proposed which is best-case O(log\
  62. :sub:`2` n) during execution. The underlying assumption is that
  63. the required space of label unions is sparse, which is a reasonable
  64. assumption to make given that we are optimizing for the case where
  65. applications mostly copy data from one place to another, without often
  66. invoking the need for an actual union operation. The representation
  67. of a taint label is a 16-bit integer, and new labels are allocated
  68. sequentially from a pool. The label identifier 0 is special, and means
  69. that the data item is unlabelled.
  70. When a label union operation is requested at a join point (any
  71. arithmetic or logical operation with two or more operands, such as
  72. addition), the code checks whether a union is required, whether the
  73. same union has been requested before, and whether one union label
  74. subsumes the other. If so, it returns the previously allocated union
  75. label. If not, it allocates a new union label from the same pool used
  76. for new labels.
  77. Specifically, the instrumentation pass will insert code like this
  78. to decide the union label ``lu`` for a pair of labels ``l1``
  79. and ``l2``:
  80. .. code-block:: c
  81. if (l1 == l2)
  82. lu = l1;
  83. else
  84. lu = __dfsan_union(l1, l2);
  85. The equality comparison is outlined, to provide an early exit in
  86. the common cases where the program is processing unlabelled data, or
  87. where the two data items have the same label. ``__dfsan_union`` is
  88. a runtime library function which performs all other union computation.
  89. Further optimizations are possible, for example if ``l1`` is known
  90. at compile time to be zero (e.g. it is derived from a constant),
  91. ``l2`` can be used for ``lu``, and vice versa.
  92. Memory layout and label management
  93. ----------------------------------
  94. The following is the current memory layout for Linux/x86\_64:
  95. +---------------+---------------+--------------------+
  96. | Start | End | Use |
  97. +===============+===============+====================+
  98. | 0x700000008000|0x800000000000 | application memory |
  99. +---------------+---------------+--------------------+
  100. | 0x200200000000|0x700000008000 | unused |
  101. +---------------+---------------+--------------------+
  102. | 0x200000000000|0x200200000000 | union table |
  103. +---------------+---------------+--------------------+
  104. | 0x000000010000|0x200000000000 | shadow memory |
  105. +---------------+---------------+--------------------+
  106. | 0x000000000000|0x000000010000 | reserved by kernel |
  107. +---------------+---------------+--------------------+
  108. Each byte of application memory corresponds to two bytes of shadow
  109. memory, which are used to store its taint label. As for LLVM SSA
  110. registers, we have not found it necessary to associate a label with
  111. each byte or bit of data, as some other tools do. Instead, labels are
  112. associated directly with registers. Loads will result in a union of
  113. all shadow labels corresponding to bytes loaded (which most of the
  114. time will be short circuited by the initial comparison) and stores will
  115. result in a copy of the label to the shadow of all bytes stored to.
  116. Propagating labels through arguments
  117. ------------------------------------
  118. In order to propagate labels through function arguments and return values,
  119. DataFlowSanitizer changes the ABI of each function in the translation unit.
  120. There are currently two supported ABIs:
  121. * Args -- Argument and return value labels are passed through additional
  122. arguments and by modifying the return type.
  123. * TLS -- Argument and return value labels are passed through TLS variables
  124. ``__dfsan_arg_tls`` and ``__dfsan_retval_tls``.
  125. The main advantage of the TLS ABI is that it is more tolerant of ABI mismatches
  126. (TLS storage is not shared with any other form of storage, whereas extra
  127. arguments may be stored in registers which under the native ABI are not used
  128. for parameter passing and thus could contain arbitrary values). On the other
  129. hand the args ABI is more efficient and allows ABI mismatches to be more easily
  130. identified by checking for nonzero labels in nominally unlabelled programs.
  131. Implementing the ABI list
  132. -------------------------
  133. The `ABI list <DataFlowSanitizer.html#abi-list>`_ provides a list of functions
  134. which conform to the native ABI, each of which is callable from an instrumented
  135. program. This is implemented by replacing each reference to a native ABI
  136. function with a reference to a function which uses the instrumented ABI.
  137. Such functions are automatically-generated wrappers for the native functions.
  138. For example, given the ABI list example provided in the user manual, the
  139. following wrappers will be generated under the args ABI:
  140. .. code-block:: llvm
  141. define linkonce_odr { i8*, i16 } @"dfsw$malloc"(i64 %0, i16 %1) {
  142. entry:
  143. %2 = call i8* @malloc(i64 %0)
  144. %3 = insertvalue { i8*, i16 } undef, i8* %2, 0
  145. %4 = insertvalue { i8*, i16 } %3, i16 0, 1
  146. ret { i8*, i16 } %4
  147. }
  148. define linkonce_odr { i32, i16 } @"dfsw$tolower"(i32 %0, i16 %1) {
  149. entry:
  150. %2 = call i32 @tolower(i32 %0)
  151. %3 = insertvalue { i32, i16 } undef, i32 %2, 0
  152. %4 = insertvalue { i32, i16 } %3, i16 %1, 1
  153. ret { i32, i16 } %4
  154. }
  155. define linkonce_odr { i8*, i16 } @"dfsw$memcpy"(i8* %0, i8* %1, i64 %2, i16 %3, i16 %4, i16 %5) {
  156. entry:
  157. %labelreturn = alloca i16
  158. %6 = call i8* @__dfsw_memcpy(i8* %0, i8* %1, i64 %2, i16 %3, i16 %4, i16 %5, i16* %labelreturn)
  159. %7 = load i16* %labelreturn
  160. %8 = insertvalue { i8*, i16 } undef, i8* %6, 0
  161. %9 = insertvalue { i8*, i16 } %8, i16 %7, 1
  162. ret { i8*, i16 } %9
  163. }
  164. As an optimization, direct calls to native ABI functions will call the
  165. native ABI function directly and the pass will compute the appropriate label
  166. internally. This has the advantage of reducing the number of union operations
  167. required when the return value label is known to be zero (i.e. ``discard``
  168. functions, or ``functional`` functions with known unlabelled arguments).
  169. Checking ABI Consistency
  170. ------------------------
  171. DFSan changes the ABI of each function in the module. This makes it possible
  172. for a function with the native ABI to be called with the instrumented ABI,
  173. or vice versa, thus possibly invoking undefined behavior. A simple way
  174. of statically detecting instances of this problem is to prepend the prefix
  175. "dfs$" to the name of each instrumented-ABI function.
  176. This will not catch every such problem; in particular function pointers passed
  177. across the instrumented-native barrier cannot be used on the other side.
  178. These problems could potentially be caught dynamically.