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 
16 #include <stddef.h>
17 #include <stdint.h>
18 #include <sanitizer/common_interface_defs.h>
19 
20 #ifdef __cplusplus
21 extern "C" {
22 #endif
23 
24 typedef uint8_t dfsan_label;
25 typedef uint32_t dfsan_origin;
26 
27 /// Signature of the callback argument to dfsan_set_write_callback().
28 typedef void (*dfsan_write_callback_t)(int fd, const void *buf, size_t count);
29 
30 /// Computes the union of \c l1 and \c l2, resulting in a union label.
31 dfsan_label dfsan_union(dfsan_label l1, dfsan_label l2);
32 
33 /// Sets the label for each address in [addr,addr+size) to \c label.
34 void dfsan_set_label(dfsan_label label, void *addr, size_t size);
35 
36 /// Sets the label for each address in [addr,addr+size) to the union of the
37 /// current label for that address and \c label.
38 void dfsan_add_label(dfsan_label label, void *addr, size_t size);
39 
40 /// Retrieves the label associated with the given data.
41 ///
42 /// The type of 'data' is arbitrary.  The function accepts a value of any type,
43 /// which can be truncated or extended (implicitly or explicitly) as necessary.
44 /// The truncation/extension operations will preserve the label of the original
45 /// value.
46 dfsan_label dfsan_get_label(long data);
47 
48 /// Retrieves the immediate origin associated with the given data. The returned
49 /// origin may point to another origin.
50 ///
51 /// The type of 'data' is arbitrary.
52 dfsan_origin dfsan_get_origin(long data);
53 
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 
57 /// Returns whether the given label label contains the label elem.
58 int dfsan_has_label(dfsan_label label, dfsan_label elem);
59 
60 /// Flushes the DFSan shadow, i.e. forgets about all labels currently associated
61 /// with the application memory.  Use this call to start over the taint tracking
62 /// within the same process.
63 ///
64 /// Note: If another thread is working with tainted data during the flush, that
65 /// taint could still be written to shadow after the flush.
66 void dfsan_flush(void);
67 
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 
73 /// Interceptor hooks.
74 /// Whenever a dfsan's custom function is called the corresponding
75 /// hook is called it non-zero. The hooks should be defined by the user.
76 /// The primary use case is taint-guided fuzzing, where the fuzzer
77 /// needs to see the parameters of the function and the labels.
78 /// FIXME: implement more hooks.
79 void dfsan_weak_hook_memcmp(void *caller_pc, const void *s1, const void *s2,
80                             size_t n, dfsan_label s1_label,
81                             dfsan_label s2_label, dfsan_label n_label);
82 void dfsan_weak_hook_strncmp(void *caller_pc, const char *s1, const char *s2,
83                              size_t n, dfsan_label s1_label,
84                              dfsan_label s2_label, dfsan_label n_label);
85 
86 /// Prints the origin trace of the label at the address addr to stderr. It also
87 /// prints description at the beginning of the trace. If origin tracking is not
88 /// on, or the address is not labeled, it prints nothing.
89 void dfsan_print_origin_trace(const void *addr, const char *description);
90 
91 /// Prints the origin trace of the label at the address \p addr to a
92 /// pre-allocated output buffer. If origin tracking is not on, or the address is
93 /// not labeled, it prints nothing.
94 ///
95 /// Typical usage:
96 /// \code
97 ///   char kDescription[] = "...";
98 ///   char buf[1024];
99 ///   dfsan_sprint_origin_trace(&tainted_var, kDescription, buf, sizeof(buf));
100 /// \endcode
101 ///
102 /// Typical usage that handles truncation:
103 /// \code
104 ///   char buf[1024];
105 ///   int len = dfsan_sprint_origin_trace(&var, nullptr, buf, sizeof(buf));
106 ///
107 ///   if (len < sizeof(buf)) {
108 ///     ProcessOriginTrace(buf);
109 ///   } else {
110 ///     char *tmpbuf = new char[len + 1];
111 ///     dfsan_sprint_origin_trace(&var, nullptr, tmpbuf, len + 1);
112 ///     ProcessOriginTrace(tmpbuf);
113 ///     delete[] tmpbuf;
114 ///   }
115 /// \endcode
116 ///
117 /// \param addr The tainted memory address whose origin we are printing.
118 /// \param description A description printed at the beginning of the trace.
119 /// \param [out] out_buf The output buffer to write the results to.
120 /// \param out_buf_size The size of \p out_buf.
121 ///
122 /// \returns The number of symbols that should have been written to \p out_buf
123 /// (not including trailing null byte '\0'). Thus, the string is truncated iff
124 /// return value is not less than \p out_buf_size.
125 size_t dfsan_sprint_origin_trace(const void *addr, const char *description,
126                                  char *out_buf, size_t out_buf_size);
127 
128 /// Prints the stack trace leading to this call to a pre-allocated output
129 /// buffer.
130 ///
131 /// For usage examples, see dfsan_sprint_origin_trace.
132 ///
133 /// \param [out] out_buf The output buffer to write the results to.
134 /// \param out_buf_size The size of \p out_buf.
135 ///
136 /// \returns The number of symbols that should have been written to \p out_buf
137 /// (not including trailing null byte '\0'). Thus, the string is truncated iff
138 /// return value is not less than \p out_buf_size.
139 size_t dfsan_sprint_stack_trace(char *out_buf, size_t out_buf_size);
140 
141 /// Retrieves the very first origin associated with the data at the given
142 /// address.
143 dfsan_origin dfsan_get_init_origin(const void *addr);
144 
145 /// Returns the value of -dfsan-track-origins.
146 /// * 0: do not track origins.
147 /// * 1: track origins at memory store operations.
148 /// * 2: track origins at memory load and store operations.
149 int dfsan_get_track_origins(void);
150 #ifdef __cplusplus
151 }  // extern "C"
152 
153 template <typename T>
154 void dfsan_set_label(dfsan_label label, T &data) { // NOLINT
155   dfsan_set_label(label, (void *)&data, sizeof(T));
156 }
157 
158 #endif
159 
160 #endif  // DFSAN_INTERFACE_H
161