2013-08-08 06:47:26 +08:00
|
|
|
//===-- dfsan_interface.h -------------------------------------------------===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2013-08-08 06:47:26 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file is a part of DataFlowSanitizer.
|
|
|
|
//
|
|
|
|
// Public interface header.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#ifndef DFSAN_INTERFACE_H
|
|
|
|
#define DFSAN_INTERFACE_H
|
|
|
|
|
|
|
|
#include <stddef.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <sanitizer/common_interface_defs.h>
|
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
2021-06-05 06:34:02 +08:00
|
|
|
typedef uint8_t dfsan_label;
|
2021-02-12 05:50:17 +08:00
|
|
|
typedef uint32_t dfsan_origin;
|
2013-08-08 06:47:26 +08:00
|
|
|
|
2014-04-25 01:42:16 +08:00
|
|
|
/// Signature of the callback argument to dfsan_set_write_callback().
|
|
|
|
typedef void (*dfsan_write_callback_t)(int fd, const void *buf, size_t count);
|
|
|
|
|
2021-12-23 15:54:26 +08:00
|
|
|
/// Signature of the callback argument to dfsan_set_conditional_callback().
|
|
|
|
typedef void (*dfsan_conditional_callback_t)(dfsan_label label,
|
|
|
|
dfsan_origin origin);
|
|
|
|
|
2021-06-05 06:34:02 +08:00
|
|
|
/// Computes the union of \c l1 and \c l2, resulting in a union label.
|
2013-08-13 07:47:37 +08:00
|
|
|
dfsan_label dfsan_union(dfsan_label l1, dfsan_label l2);
|
|
|
|
|
2013-08-08 06:47:26 +08:00
|
|
|
/// Sets the label for each address in [addr,addr+size) to \c label.
|
|
|
|
void dfsan_set_label(dfsan_label label, void *addr, size_t size);
|
|
|
|
|
|
|
|
/// Sets the label for each address in [addr,addr+size) to the union of the
|
|
|
|
/// current label for that address and \c label.
|
|
|
|
void dfsan_add_label(dfsan_label label, void *addr, size_t size);
|
|
|
|
|
|
|
|
/// Retrieves the label associated with the given data.
|
|
|
|
///
|
|
|
|
/// The type of 'data' is arbitrary. The function accepts a value of any type,
|
|
|
|
/// which can be truncated or extended (implicitly or explicitly) as necessary.
|
|
|
|
/// The truncation/extension operations will preserve the label of the original
|
|
|
|
/// value.
|
|
|
|
dfsan_label dfsan_get_label(long data);
|
|
|
|
|
2021-03-04 10:35:13 +08:00
|
|
|
/// Retrieves the immediate origin associated with the given data. The returned
|
|
|
|
/// origin may point to another origin.
|
|
|
|
///
|
|
|
|
/// The type of 'data' is arbitrary.
|
|
|
|
dfsan_origin dfsan_get_origin(long data);
|
|
|
|
|
2013-08-14 06:15:40 +08:00
|
|
|
/// Retrieves the label associated with the data at the given address.
|
|
|
|
dfsan_label dfsan_read_label(const void *addr, size_t size);
|
|
|
|
|
2021-12-23 08:56:37 +08:00
|
|
|
/// Return the origin associated with the first taint byte in the size bytes
|
|
|
|
/// from the address addr.
|
|
|
|
dfsan_origin dfsan_read_origin_of_first_taint(const void *addr, size_t size);
|
|
|
|
|
2013-08-08 06:47:26 +08:00
|
|
|
/// Returns whether the given label label contains the label elem.
|
|
|
|
int dfsan_has_label(dfsan_label label, dfsan_label elem);
|
|
|
|
|
2019-06-14 04:11:06 +08:00
|
|
|
/// Flushes the DFSan shadow, i.e. forgets about all labels currently associated
|
2020-08-15 02:43:33 +08:00
|
|
|
/// with the application memory. Use this call to start over the taint tracking
|
|
|
|
/// within the same process.
|
|
|
|
///
|
|
|
|
/// Note: If another thread is working with tainted data during the flush, that
|
|
|
|
/// taint could still be written to shadow after the flush.
|
2019-06-14 04:11:06 +08:00
|
|
|
void dfsan_flush(void);
|
|
|
|
|
2014-04-25 01:42:16 +08:00
|
|
|
/// Sets a callback to be invoked on calls to write(). The callback is invoked
|
|
|
|
/// before the write is done. The write is not guaranteed to succeed when the
|
|
|
|
/// callback executes. Pass in NULL to remove any callback.
|
|
|
|
void dfsan_set_write_callback(dfsan_write_callback_t labeled_write_callback);
|
|
|
|
|
2021-12-23 15:54:26 +08:00
|
|
|
/// Sets a callback to be invoked on any conditional expressions which have a
|
|
|
|
/// taint label set. This can be used to find where tainted data influences
|
|
|
|
/// the behavior of the program.
|
|
|
|
/// These callbacks will only be added when -dfsan-conditional-callbacks=true.
|
|
|
|
void dfsan_set_conditional_callback(dfsan_conditional_callback_t callback);
|
|
|
|
|
|
|
|
/// Conditional expressions occur during signal handlers.
|
|
|
|
/// Making callbacks that handle signals well is tricky, so when
|
|
|
|
/// -dfsan-conditional-callbacks=true, conditional expressions used in signal
|
|
|
|
/// handlers will add the labels they see into a global (bitwise-or together).
|
|
|
|
/// This function returns all label bits seen in signal handler conditions.
|
|
|
|
dfsan_label dfsan_get_labels_in_signal_conditional();
|
|
|
|
|
2015-07-30 10:32:51 +08:00
|
|
|
/// Interceptor hooks.
|
2015-05-07 08:04:39 +08:00
|
|
|
/// Whenever a dfsan's custom function is called the corresponding
|
|
|
|
/// hook is called it non-zero. The hooks should be defined by the user.
|
|
|
|
/// The primary use case is taint-guided fuzzing, where the fuzzer
|
|
|
|
/// needs to see the parameters of the function and the labels.
|
|
|
|
/// FIXME: implement more hooks.
|
|
|
|
void dfsan_weak_hook_memcmp(void *caller_pc, const void *s1, const void *s2,
|
|
|
|
size_t n, dfsan_label s1_label,
|
|
|
|
dfsan_label s2_label, dfsan_label n_label);
|
2015-07-30 10:32:51 +08:00
|
|
|
void dfsan_weak_hook_strncmp(void *caller_pc, const char *s1, const char *s2,
|
|
|
|
size_t n, dfsan_label s1_label,
|
|
|
|
dfsan_label s2_label, dfsan_label n_label);
|
2021-03-04 10:35:13 +08:00
|
|
|
|
|
|
|
/// Prints the origin trace of the label at the address addr to stderr. It also
|
|
|
|
/// prints description at the beginning of the trace. If origin tracking is not
|
|
|
|
/// on, or the address is not labeled, it prints nothing.
|
|
|
|
void dfsan_print_origin_trace(const void *addr, const char *description);
|
2021-12-23 04:35:42 +08:00
|
|
|
/// As above, but use an origin id from dfsan_get_origin() instead of address.
|
|
|
|
/// Does not include header line with taint label and address information.
|
|
|
|
void dfsan_print_origin_id_trace(dfsan_origin origin);
|
2021-03-04 10:35:13 +08:00
|
|
|
|
2021-05-22 01:56:45 +08:00
|
|
|
/// Prints the origin trace of the label at the address \p addr to a
|
|
|
|
/// pre-allocated output buffer. If origin tracking is not on, or the address is
|
|
|
|
/// not labeled, it prints nothing.
|
|
|
|
///
|
|
|
|
/// Typical usage:
|
|
|
|
/// \code
|
|
|
|
/// char kDescription[] = "...";
|
|
|
|
/// char buf[1024];
|
|
|
|
/// dfsan_sprint_origin_trace(&tainted_var, kDescription, buf, sizeof(buf));
|
|
|
|
/// \endcode
|
|
|
|
///
|
|
|
|
/// Typical usage that handles truncation:
|
|
|
|
/// \code
|
|
|
|
/// char buf[1024];
|
|
|
|
/// int len = dfsan_sprint_origin_trace(&var, nullptr, buf, sizeof(buf));
|
|
|
|
///
|
|
|
|
/// if (len < sizeof(buf)) {
|
2021-06-12 08:54:19 +08:00
|
|
|
/// ProcessOriginTrace(buf);
|
|
|
|
/// } else {
|
2021-05-22 01:56:45 +08:00
|
|
|
/// char *tmpbuf = new char[len + 1];
|
|
|
|
/// dfsan_sprint_origin_trace(&var, nullptr, tmpbuf, len + 1);
|
|
|
|
/// ProcessOriginTrace(tmpbuf);
|
|
|
|
/// delete[] tmpbuf;
|
|
|
|
/// }
|
|
|
|
/// \endcode
|
|
|
|
///
|
|
|
|
/// \param addr The tainted memory address whose origin we are printing.
|
|
|
|
/// \param description A description printed at the beginning of the trace.
|
|
|
|
/// \param [out] out_buf The output buffer to write the results to.
|
|
|
|
/// \param out_buf_size The size of \p out_buf.
|
|
|
|
///
|
|
|
|
/// \returns The number of symbols that should have been written to \p out_buf
|
|
|
|
/// (not including trailing null byte '\0'). Thus, the string is truncated iff
|
|
|
|
/// return value is not less than \p out_buf_size.
|
|
|
|
size_t dfsan_sprint_origin_trace(const void *addr, const char *description,
|
|
|
|
char *out_buf, size_t out_buf_size);
|
2021-12-23 04:35:42 +08:00
|
|
|
/// As above, but use an origin id from dfsan_get_origin() instead of address.
|
|
|
|
/// Does not include header line with taint label and address information.
|
|
|
|
size_t dfsan_sprint_origin_id_trace(dfsan_origin origin, char *out_buf,
|
|
|
|
size_t out_buf_size);
|
2021-05-22 01:56:45 +08:00
|
|
|
|
2021-06-12 08:54:19 +08:00
|
|
|
/// Prints the stack trace leading to this call to a pre-allocated output
|
|
|
|
/// buffer.
|
|
|
|
///
|
|
|
|
/// For usage examples, see dfsan_sprint_origin_trace.
|
|
|
|
///
|
|
|
|
/// \param [out] out_buf The output buffer to write the results to.
|
|
|
|
/// \param out_buf_size The size of \p out_buf.
|
|
|
|
///
|
|
|
|
/// \returns The number of symbols that should have been written to \p out_buf
|
|
|
|
/// (not including trailing null byte '\0'). Thus, the string is truncated iff
|
|
|
|
/// return value is not less than \p out_buf_size.
|
|
|
|
size_t dfsan_sprint_stack_trace(char *out_buf, size_t out_buf_size);
|
|
|
|
|
2021-03-04 10:35:13 +08:00
|
|
|
/// Retrieves the very first origin associated with the data at the given
|
|
|
|
/// address.
|
|
|
|
dfsan_origin dfsan_get_init_origin(const void *addr);
|
2021-06-29 14:17:00 +08:00
|
|
|
|
|
|
|
/// Returns the value of -dfsan-track-origins.
|
|
|
|
/// * 0: do not track origins.
|
|
|
|
/// * 1: track origins at memory store operations.
|
|
|
|
/// * 2: track origins at memory load and store operations.
|
|
|
|
int dfsan_get_track_origins(void);
|
2013-08-08 06:47:26 +08:00
|
|
|
#ifdef __cplusplus
|
|
|
|
} // extern "C"
|
|
|
|
|
2021-07-31 06:24:59 +08:00
|
|
|
template <typename T> void dfsan_set_label(dfsan_label label, T &data) {
|
2019-09-12 07:43:23 +08:00
|
|
|
dfsan_set_label(label, (void *)&data, sizeof(T));
|
|
|
|
}
|
|
|
|
|
2013-08-08 06:47:26 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#endif // DFSAN_INTERFACE_H
|