DataFlowSanitizer — Clang 21.0.0git documentation (original) (raw)

Introduction

DataFlowSanitizer is a generalised dynamic data flow analysis.

Unlike other Sanitizer tools, this tool is not designed to detect a specific class of bugs on its own. Instead, it provides a generic dynamic data flow analysis framework to be used by clients to help detect application-specific issues within their own code.

How to build libc++ with DFSan

DFSan requires either all of your code to be instrumented or for uninstrumented functions to be listed as uninstrumented in the ABI list.

If you’d like to have instrumented libc++ functions, then you need to build it with DFSan instrumentation from source. Here is an example of how to build libc++ and the libc++ ABI with data flow sanitizer instrumentation.

mkdir libcxx-build cd libcxx-build

An example using ninja

cmake -GNinja -S /runtimes
-DCMAKE_C_COMPILER=clang
-DCMAKE_CXX_COMPILER=clang++
-DLLVM_USE_SANITIZER="DataFlow"
-DLLVM_ENABLE_RUNTIMES="libcxx;libcxxabi"

ninja cxx cxxabi

Note: Ensure you are building with a sufficiently new version of Clang.

Usage

With no program changes, applying DataFlowSanitizer to a program will not alter its behavior. To use DataFlowSanitizer, the program uses API functions to apply tags to data to cause it to be tracked, and to check the tag of a specific data item. DataFlowSanitizer manages the propagation of tags through the program according to its data flow.

The APIs are defined in the header file sanitizer/dfsan_interface.h. For further information about each function, please refer to the header file.

ABI List

DataFlowSanitizer uses a list of functions known as an ABI list to decide whether a call to a specific function should use the operating system’s native ABI or whether it should use a variant of this ABI that also propagates labels through function parameters and return values. The ABI list file also controls how labels are propagated in the former case. DataFlowSanitizer comes with a default ABI list which is intended to eventually cover the glibc library on Linux but it may become necessary for users to extend the ABI list in cases where a particular library or function cannot be instrumented (e.g. because it is implemented in assembly or another language which DataFlowSanitizer does not support) or a function is called from a library or function which cannot be instrumented.

DataFlowSanitizer’s ABI list file is a Sanitizer special case list. The pass treats every function in the uninstrumented category in the ABI list file as conforming to the native ABI. Unless the ABI list contains additional categories for those functions, a call to one of those functions will produce a warning message, as the labelling behavior of the function is unknown. The other supported categories are discard, functionaland custom.

void f(int x); void __dfsw_f(int x, dfsan_label x_label);

void *memcpy(void *dest, const void *src, size_t n); void *__dfsw_memcpy(void *dest, const void *src, size_t n, dfsan_label dest_label, dfsan_label src_label, dfsan_label n_label, dfsan_label *ret_label);

If a function defined in the translation unit being compiled belongs to theuninstrumented category, it will be compiled so as to conform to the native ABI. Its arguments will be assumed to be unlabelled, but it will propagate labels in shadow memory.

For example:

main is called by the C runtime using the native ABI.

fun:main=uninstrumented fun:main=discard

malloc only writes to its internal data structures, not user-accessible memory.

fun:malloc=uninstrumented fun:malloc=discard

tolower is a pure function.

fun:tolower=uninstrumented fun:tolower=functional

memcpy needs to copy the shadow from the source to the destination region.

This is done in a custom function.

fun:memcpy=uninstrumented fun:memcpy=custom

For instrumented functions, the ABI list supports a force_zero_labelscategory, which will make all stores and return values set zero labels. Functions should never be labelled with both force_zero_labelsand uninstrumented or any of the uninstrumented wrapper kinds.

For example:

e.g. void writes_data(char* out_buf, int out_buf_len) {...}

Applying force_zero_labels will force out_buf shadow to zero.

fun:writes_data=force_zero_labels

Compilation Flags

If the flag is true, the label of v is the union of the label of p and the label of *p. If the flag is false, the label of v is the label of just *p.

If the flag is true, the label of *p is the union of the label of p and the label of v. If the flag is false, the label of *p is the label of just v.

If the flag is true, the label of p is the union of the label of p and the label of i. If the flag is false, the label of p is unchanged.

If the flag is true, the label of v is the union of the labels of b,v1 and v2. If the flag is false, the label of v is the union of the labels of just v1 and v2.

void __dfsan_load_callback(dfsan_label Label, void* Addr); void __dfsan_store_callback(dfsan_label Label, void* Addr); void __dfsan_mem_transfer_callback(dfsan_label *Start, size_t Len); void __dfsan_cmp_callback(dfsan_label CombinedLabel);

Environment Variables

Example

DataFlowSanitizer supports up to 8 labels, to achieve low CPU and code size overhead. Base labels are simply 8-bit unsigned integers that are powers of 2 (i.e. 1, 2, 4, 8, …, 128), and union labels are created by ORing base labels.

The following program demonstrates label propagation by checking that the correct labels are propagated.

#include <sanitizer/dfsan_interface.h> #include <assert.h>

int main(void) { int i = 100; int j = 200; int k = 300; dfsan_label i_label = 1; dfsan_label j_label = 2; dfsan_label k_label = 4; dfsan_set_label(i_label, &i, sizeof(i)); dfsan_set_label(j_label, &j, sizeof(j)); dfsan_set_label(k_label, &k, sizeof(k));

dfsan_label ij_label = dfsan_get_label(i + j);

assert(ij_label & i_label); // ij_label has i_label assert(ij_label & j_label); // ij_label has j_label assert(!(ij_label & k_label)); // ij_label doesn't have k_label assert(ij_label == 3); // Verifies all of the above

// Or, equivalently: assert(dfsan_has_label(ij_label, i_label)); assert(dfsan_has_label(ij_label, j_label)); assert(!dfsan_has_label(ij_label, k_label));

dfsan_label ijk_label = dfsan_get_label(i + j + k);

assert(ijk_label & i_label); // ijk_label has i_label assert(ijk_label & j_label); // ijk_label has j_label assert(ijk_label & k_label); // ijk_label has k_label assert(ijk_label == 7); // Verifies all of the above

// Or, equivalently: assert(dfsan_has_label(ijk_label, i_label)); assert(dfsan_has_label(ijk_label, j_label)); assert(dfsan_has_label(ijk_label, k_label));

return 0; }

Origin Tracking

DataFlowSanitizer can track origins of labeled values. This feature is enabled by-mllvm -dfsan-track-origins=1. For example,

% cat test.cc #include <sanitizer/dfsan_interface.h> #include <stdio.h>

int main(int argc, char** argv) { int i = 0; dfsan_set_label(i_label, &i, sizeof(i)); int j = i + 1; dfsan_print_origin_trace(&j, "A flow from i to j"); return 0; }

% clang++ -fsanitize=dataflow -mllvm -dfsan-track-origins=1 -fno-omit-frame-pointer -g -O2 test.cc % ./a.out Taint value 0x1 (at 0x7ffd42bf415c) origin tracking (A flow from i to j) Origin value: 0x13900001, Taint value was stored to memory at #0 0x55676db85a62 in main test.cc:7:7 #1 0x7f0083611bbc in __libc_start_main libc-start.c:285

Origin value: 0x9e00001, Taint value was created at #0 0x55676db85a08 in main test.cc:6:3 #1 0x7f0083611bbc in __libc_start_main libc-start.c:285

By -mllvm -dfsan-track-origins=1 DataFlowSanitizer collects only intermediate stores a labeled value went through. Origin tracking slows down program execution by a factor of 2x on top of the usual DataFlowSanitizer slowdown and increases memory overhead by 1x. By -mllvm -dfsan-track-origins=2DataFlowSanitizer also collects intermediate loads a labeled value went through. This mode slows down program execution by a factor of 4x.

Current status

DataFlowSanitizer is a work in progress, currently under development for x86_64 Linux.

Design

Please refer to the design document.