xref: /freebsd/contrib/llvm-project/compiler-rt/include/sanitizer/dfsan_interface.h (revision 04eeddc0aa8e0a417a16eaf9d7d095207f4a8623)
10b57cec5SDimitry Andric //===-- dfsan_interface.h -------------------------------------------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric // This file is a part of DataFlowSanitizer.
100b57cec5SDimitry Andric //
110b57cec5SDimitry Andric // Public interface header.
120b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
130b57cec5SDimitry Andric #ifndef DFSAN_INTERFACE_H
140b57cec5SDimitry Andric #define DFSAN_INTERFACE_H
150b57cec5SDimitry Andric 
160b57cec5SDimitry Andric #include <stddef.h>
170b57cec5SDimitry Andric #include <stdint.h>
180b57cec5SDimitry Andric #include <sanitizer/common_interface_defs.h>
190b57cec5SDimitry Andric 
200b57cec5SDimitry Andric #ifdef __cplusplus
210b57cec5SDimitry Andric extern "C" {
220b57cec5SDimitry Andric #endif
230b57cec5SDimitry Andric 
24fe6060f1SDimitry Andric typedef uint8_t dfsan_label;
25fe6060f1SDimitry Andric typedef uint32_t dfsan_origin;
260b57cec5SDimitry Andric 
270b57cec5SDimitry Andric /// Signature of the callback argument to dfsan_set_write_callback().
280b57cec5SDimitry Andric typedef void (*dfsan_write_callback_t)(int fd, const void *buf, size_t count);
290b57cec5SDimitry Andric 
30*04eeddc0SDimitry Andric /// Signature of the callback argument to dfsan_set_conditional_callback().
31*04eeddc0SDimitry Andric typedef void (*dfsan_conditional_callback_t)(dfsan_label label,
32*04eeddc0SDimitry Andric                                              dfsan_origin origin);
33*04eeddc0SDimitry Andric 
34fe6060f1SDimitry Andric /// Computes the union of \c l1 and \c l2, resulting in a union label.
350b57cec5SDimitry Andric dfsan_label dfsan_union(dfsan_label l1, dfsan_label l2);
360b57cec5SDimitry Andric 
370b57cec5SDimitry Andric /// Sets the label for each address in [addr,addr+size) to \c label.
380b57cec5SDimitry Andric void dfsan_set_label(dfsan_label label, void *addr, size_t size);
390b57cec5SDimitry Andric 
400b57cec5SDimitry Andric /// Sets the label for each address in [addr,addr+size) to the union of the
410b57cec5SDimitry Andric /// current label for that address and \c label.
420b57cec5SDimitry Andric void dfsan_add_label(dfsan_label label, void *addr, size_t size);
430b57cec5SDimitry Andric 
440b57cec5SDimitry Andric /// Retrieves the label associated with the given data.
450b57cec5SDimitry Andric ///
460b57cec5SDimitry Andric /// The type of 'data' is arbitrary.  The function accepts a value of any type,
470b57cec5SDimitry Andric /// which can be truncated or extended (implicitly or explicitly) as necessary.
480b57cec5SDimitry Andric /// The truncation/extension operations will preserve the label of the original
490b57cec5SDimitry Andric /// value.
500b57cec5SDimitry Andric dfsan_label dfsan_get_label(long data);
510b57cec5SDimitry Andric 
52fe6060f1SDimitry Andric /// Retrieves the immediate origin associated with the given data. The returned
53fe6060f1SDimitry Andric /// origin may point to another origin.
54fe6060f1SDimitry Andric ///
55fe6060f1SDimitry Andric /// The type of 'data' is arbitrary.
56fe6060f1SDimitry Andric dfsan_origin dfsan_get_origin(long data);
57fe6060f1SDimitry Andric 
580b57cec5SDimitry Andric /// Retrieves the label associated with the data at the given address.
590b57cec5SDimitry Andric dfsan_label dfsan_read_label(const void *addr, size_t size);
600b57cec5SDimitry Andric 
610eae32dcSDimitry Andric /// Return the origin associated with the first taint byte in the size bytes
620eae32dcSDimitry Andric /// from the address addr.
630eae32dcSDimitry Andric dfsan_origin dfsan_read_origin_of_first_taint(const void *addr, size_t size);
640eae32dcSDimitry Andric 
650b57cec5SDimitry Andric /// Returns whether the given label label contains the label elem.
660b57cec5SDimitry Andric int dfsan_has_label(dfsan_label label, dfsan_label elem);
670b57cec5SDimitry Andric 
680b57cec5SDimitry Andric /// Flushes the DFSan shadow, i.e. forgets about all labels currently associated
69e8d8bef9SDimitry Andric /// with the application memory.  Use this call to start over the taint tracking
70e8d8bef9SDimitry Andric /// within the same process.
71e8d8bef9SDimitry Andric ///
72e8d8bef9SDimitry Andric /// Note: If another thread is working with tainted data during the flush, that
73e8d8bef9SDimitry Andric /// taint could still be written to shadow after the flush.
740b57cec5SDimitry Andric void dfsan_flush(void);
750b57cec5SDimitry Andric 
760b57cec5SDimitry Andric /// Sets a callback to be invoked on calls to write().  The callback is invoked
770b57cec5SDimitry Andric /// before the write is done.  The write is not guaranteed to succeed when the
780b57cec5SDimitry Andric /// callback executes.  Pass in NULL to remove any callback.
790b57cec5SDimitry Andric void dfsan_set_write_callback(dfsan_write_callback_t labeled_write_callback);
800b57cec5SDimitry Andric 
81*04eeddc0SDimitry Andric /// Sets a callback to be invoked on any conditional expressions which have a
82*04eeddc0SDimitry Andric /// taint label set. This can be used to find where tainted data influences
83*04eeddc0SDimitry Andric /// the behavior of the program.
84*04eeddc0SDimitry Andric /// These callbacks will only be added when -dfsan-conditional-callbacks=true.
85*04eeddc0SDimitry Andric void dfsan_set_conditional_callback(dfsan_conditional_callback_t callback);
86*04eeddc0SDimitry Andric 
87*04eeddc0SDimitry Andric /// Conditional expressions occur during signal handlers.
88*04eeddc0SDimitry Andric /// Making callbacks that handle signals well is tricky, so when
89*04eeddc0SDimitry Andric /// -dfsan-conditional-callbacks=true, conditional expressions used in signal
90*04eeddc0SDimitry Andric /// handlers will add the labels they see into a global (bitwise-or together).
91*04eeddc0SDimitry Andric /// This function returns all label bits seen in signal handler conditions.
92*04eeddc0SDimitry Andric dfsan_label dfsan_get_labels_in_signal_conditional();
93*04eeddc0SDimitry Andric 
940b57cec5SDimitry Andric /// Interceptor hooks.
950b57cec5SDimitry Andric /// Whenever a dfsan's custom function is called the corresponding
960b57cec5SDimitry Andric /// hook is called it non-zero. The hooks should be defined by the user.
970b57cec5SDimitry Andric /// The primary use case is taint-guided fuzzing, where the fuzzer
980b57cec5SDimitry Andric /// needs to see the parameters of the function and the labels.
990b57cec5SDimitry Andric /// FIXME: implement more hooks.
1000b57cec5SDimitry Andric void dfsan_weak_hook_memcmp(void *caller_pc, const void *s1, const void *s2,
1010b57cec5SDimitry Andric                             size_t n, dfsan_label s1_label,
1020b57cec5SDimitry Andric                             dfsan_label s2_label, dfsan_label n_label);
1030b57cec5SDimitry Andric void dfsan_weak_hook_strncmp(void *caller_pc, const char *s1, const char *s2,
1040b57cec5SDimitry Andric                              size_t n, dfsan_label s1_label,
1050b57cec5SDimitry Andric                              dfsan_label s2_label, dfsan_label n_label);
106fe6060f1SDimitry Andric 
107fe6060f1SDimitry Andric /// Prints the origin trace of the label at the address addr to stderr. It also
108fe6060f1SDimitry Andric /// prints description at the beginning of the trace. If origin tracking is not
109fe6060f1SDimitry Andric /// on, or the address is not labeled, it prints nothing.
110fe6060f1SDimitry Andric void dfsan_print_origin_trace(const void *addr, const char *description);
1110eae32dcSDimitry Andric /// As above, but use an origin id from dfsan_get_origin() instead of address.
1120eae32dcSDimitry Andric /// Does not include header line with taint label and address information.
1130eae32dcSDimitry Andric void dfsan_print_origin_id_trace(dfsan_origin origin);
114fe6060f1SDimitry Andric 
115fe6060f1SDimitry Andric /// Prints the origin trace of the label at the address \p addr to a
116fe6060f1SDimitry Andric /// pre-allocated output buffer. If origin tracking is not on, or the address is
117fe6060f1SDimitry Andric /// not labeled, it prints nothing.
118fe6060f1SDimitry Andric ///
119fe6060f1SDimitry Andric /// Typical usage:
120fe6060f1SDimitry Andric /// \code
121fe6060f1SDimitry Andric ///   char kDescription[] = "...";
122fe6060f1SDimitry Andric ///   char buf[1024];
123fe6060f1SDimitry Andric ///   dfsan_sprint_origin_trace(&tainted_var, kDescription, buf, sizeof(buf));
124fe6060f1SDimitry Andric /// \endcode
125fe6060f1SDimitry Andric ///
126fe6060f1SDimitry Andric /// Typical usage that handles truncation:
127fe6060f1SDimitry Andric /// \code
128fe6060f1SDimitry Andric ///   char buf[1024];
129fe6060f1SDimitry Andric ///   int len = dfsan_sprint_origin_trace(&var, nullptr, buf, sizeof(buf));
130fe6060f1SDimitry Andric ///
131fe6060f1SDimitry Andric ///   if (len < sizeof(buf)) {
132fe6060f1SDimitry Andric ///     ProcessOriginTrace(buf);
133fe6060f1SDimitry Andric ///   } else {
134fe6060f1SDimitry Andric ///     char *tmpbuf = new char[len + 1];
135fe6060f1SDimitry Andric ///     dfsan_sprint_origin_trace(&var, nullptr, tmpbuf, len + 1);
136fe6060f1SDimitry Andric ///     ProcessOriginTrace(tmpbuf);
137fe6060f1SDimitry Andric ///     delete[] tmpbuf;
138fe6060f1SDimitry Andric ///   }
139fe6060f1SDimitry Andric /// \endcode
140fe6060f1SDimitry Andric ///
141fe6060f1SDimitry Andric /// \param addr The tainted memory address whose origin we are printing.
142fe6060f1SDimitry Andric /// \param description A description printed at the beginning of the trace.
143fe6060f1SDimitry Andric /// \param [out] out_buf The output buffer to write the results to.
144fe6060f1SDimitry Andric /// \param out_buf_size The size of \p out_buf.
145fe6060f1SDimitry Andric ///
146fe6060f1SDimitry Andric /// \returns The number of symbols that should have been written to \p out_buf
147fe6060f1SDimitry Andric /// (not including trailing null byte '\0'). Thus, the string is truncated iff
148fe6060f1SDimitry Andric /// return value is not less than \p out_buf_size.
149fe6060f1SDimitry Andric size_t dfsan_sprint_origin_trace(const void *addr, const char *description,
150fe6060f1SDimitry Andric                                  char *out_buf, size_t out_buf_size);
1510eae32dcSDimitry Andric /// As above, but use an origin id from dfsan_get_origin() instead of address.
1520eae32dcSDimitry Andric /// Does not include header line with taint label and address information.
1530eae32dcSDimitry Andric size_t dfsan_sprint_origin_id_trace(dfsan_origin origin, char *out_buf,
1540eae32dcSDimitry Andric                                     size_t out_buf_size);
155fe6060f1SDimitry Andric 
156fe6060f1SDimitry Andric /// Prints the stack trace leading to this call to a pre-allocated output
157fe6060f1SDimitry Andric /// buffer.
158fe6060f1SDimitry Andric ///
159fe6060f1SDimitry Andric /// For usage examples, see dfsan_sprint_origin_trace.
160fe6060f1SDimitry Andric ///
161fe6060f1SDimitry Andric /// \param [out] out_buf The output buffer to write the results to.
162fe6060f1SDimitry Andric /// \param out_buf_size The size of \p out_buf.
163fe6060f1SDimitry Andric ///
164fe6060f1SDimitry Andric /// \returns The number of symbols that should have been written to \p out_buf
165fe6060f1SDimitry Andric /// (not including trailing null byte '\0'). Thus, the string is truncated iff
166fe6060f1SDimitry Andric /// return value is not less than \p out_buf_size.
167fe6060f1SDimitry Andric size_t dfsan_sprint_stack_trace(char *out_buf, size_t out_buf_size);
168fe6060f1SDimitry Andric 
169fe6060f1SDimitry Andric /// Retrieves the very first origin associated with the data at the given
170fe6060f1SDimitry Andric /// address.
171fe6060f1SDimitry Andric dfsan_origin dfsan_get_init_origin(const void *addr);
172fe6060f1SDimitry Andric 
173fe6060f1SDimitry Andric /// Returns the value of -dfsan-track-origins.
174fe6060f1SDimitry Andric /// * 0: do not track origins.
175fe6060f1SDimitry Andric /// * 1: track origins at memory store operations.
176fe6060f1SDimitry Andric /// * 2: track origins at memory load and store operations.
177fe6060f1SDimitry Andric int dfsan_get_track_origins(void);
1780b57cec5SDimitry Andric #ifdef __cplusplus
1790b57cec5SDimitry Andric }  // extern "C"
1800b57cec5SDimitry Andric 
181349cc55cSDimitry Andric template <typename T> void dfsan_set_label(dfsan_label label, T &data) {
1820b57cec5SDimitry Andric   dfsan_set_label(label, (void *)&data, sizeof(T));
1830b57cec5SDimitry Andric }
1840b57cec5SDimitry Andric 
1850b57cec5SDimitry Andric #endif
1860b57cec5SDimitry Andric 
1870b57cec5SDimitry Andric #endif  // DFSAN_INTERFACE_H
188