DataFlowSanitizer.rst 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. =================
  2. DataFlowSanitizer
  3. =================
  4. .. toctree::
  5. :hidden:
  6. DataFlowSanitizerDesign
  7. .. contents::
  8. :local:
  9. Introduction
  10. ============
  11. DataFlowSanitizer is a generalised dynamic data flow analysis.
  12. Unlike other Sanitizer tools, this tool is not designed to detect a
  13. specific class of bugs on its own. Instead, it provides a generic
  14. dynamic data flow analysis framework to be used by clients to help
  15. detect application-specific issues within their own code.
  16. Usage
  17. =====
  18. With no program changes, applying DataFlowSanitizer to a program
  19. will not alter its behavior. To use DataFlowSanitizer, the program
  20. uses API functions to apply tags to data to cause it to be tracked, and to
  21. check the tag of a specific data item. DataFlowSanitizer manages
  22. the propagation of tags through the program according to its data flow.
  23. The APIs are defined in the header file ``sanitizer/dfsan_interface.h``.
  24. For further information about each function, please refer to the header
  25. file.
  26. ABI List
  27. --------
  28. DataFlowSanitizer uses a list of functions known as an ABI list to decide
  29. whether a call to a specific function should use the operating system's native
  30. ABI or whether it should use a variant of this ABI that also propagates labels
  31. through function parameters and return values. The ABI list file also controls
  32. how labels are propagated in the former case. DataFlowSanitizer comes with a
  33. default ABI list which is intended to eventually cover the glibc library on
  34. Linux but it may become necessary for users to extend the ABI list in cases
  35. where a particular library or function cannot be instrumented (e.g. because
  36. it is implemented in assembly or another language which DataFlowSanitizer does
  37. not support) or a function is called from a library or function which cannot
  38. be instrumented.
  39. DataFlowSanitizer's ABI list file is a :doc:`SanitizerSpecialCaseList`.
  40. The pass treats every function in the ``uninstrumented`` category in the
  41. ABI list file as conforming to the native ABI. Unless the ABI list contains
  42. additional categories for those functions, a call to one of those functions
  43. will produce a warning message, as the labelling behavior of the function
  44. is unknown. The other supported categories are ``discard``, ``functional``
  45. and ``custom``.
  46. * ``discard`` -- To the extent that this function writes to (user-accessible)
  47. memory, it also updates labels in shadow memory (this condition is trivially
  48. satisfied for functions which do not write to user-accessible memory). Its
  49. return value is unlabelled.
  50. * ``functional`` -- Like ``discard``, except that the label of its return value
  51. is the union of the label of its arguments.
  52. * ``custom`` -- Instead of calling the function, a custom wrapper ``__dfsw_F``
  53. is called, where ``F`` is the name of the function. This function may wrap
  54. the original function or provide its own implementation. This category is
  55. generally used for uninstrumentable functions which write to user-accessible
  56. memory or which have more complex label propagation behavior. The signature
  57. of ``__dfsw_F`` is based on that of ``F`` with each argument having a
  58. label of type ``dfsan_label`` appended to the argument list. If ``F``
  59. is of non-void return type a final argument of type ``dfsan_label *``
  60. is appended to which the custom function can store the label for the
  61. return value. For example:
  62. .. code-block:: c++
  63. void f(int x);
  64. void __dfsw_f(int x, dfsan_label x_label);
  65. void *memcpy(void *dest, const void *src, size_t n);
  66. void *__dfsw_memcpy(void *dest, const void *src, size_t n,
  67. dfsan_label dest_label, dfsan_label src_label,
  68. dfsan_label n_label, dfsan_label *ret_label);
  69. If a function defined in the translation unit being compiled belongs to the
  70. ``uninstrumented`` category, it will be compiled so as to conform to the
  71. native ABI. Its arguments will be assumed to be unlabelled, but it will
  72. propagate labels in shadow memory.
  73. For example:
  74. .. code-block:: none
  75. # main is called by the C runtime using the native ABI.
  76. fun:main=uninstrumented
  77. fun:main=discard
  78. # malloc only writes to its internal data structures, not user-accessible memory.
  79. fun:malloc=uninstrumented
  80. fun:malloc=discard
  81. # tolower is a pure function.
  82. fun:tolower=uninstrumented
  83. fun:tolower=functional
  84. # memcpy needs to copy the shadow from the source to the destination region.
  85. # This is done in a custom function.
  86. fun:memcpy=uninstrumented
  87. fun:memcpy=custom
  88. Example
  89. =======
  90. The following program demonstrates label propagation by checking that
  91. the correct labels are propagated.
  92. .. code-block:: c++
  93. #include <sanitizer/dfsan_interface.h>
  94. #include <assert.h>
  95. int main(void) {
  96. int i = 1;
  97. dfsan_label i_label = dfsan_create_label("i", 0);
  98. dfsan_set_label(i_label, &i, sizeof(i));
  99. int j = 2;
  100. dfsan_label j_label = dfsan_create_label("j", 0);
  101. dfsan_set_label(j_label, &j, sizeof(j));
  102. int k = 3;
  103. dfsan_label k_label = dfsan_create_label("k", 0);
  104. dfsan_set_label(k_label, &k, sizeof(k));
  105. dfsan_label ij_label = dfsan_get_label(i + j);
  106. assert(dfsan_has_label(ij_label, i_label));
  107. assert(dfsan_has_label(ij_label, j_label));
  108. assert(!dfsan_has_label(ij_label, k_label));
  109. dfsan_label ijk_label = dfsan_get_label(i + j + k);
  110. assert(dfsan_has_label(ijk_label, i_label));
  111. assert(dfsan_has_label(ijk_label, j_label));
  112. assert(dfsan_has_label(ijk_label, k_label));
  113. return 0;
  114. }
  115. Current status
  116. ==============
  117. DataFlowSanitizer is a work in progress, currently under development for
  118. x86\_64 Linux.
  119. Design
  120. ======
  121. Please refer to the :doc:`design document<DataFlowSanitizerDesign>`.