Path: blob/main/contrib/llvm-project/compiler-rt/include/sanitizer/dfsan_interface.h
35235 views
//===-- dfsan_interface.h -------------------------------------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7//8// This file is a part of DataFlowSanitizer.9//10// Public interface header.11//===----------------------------------------------------------------------===//12#ifndef DFSAN_INTERFACE_H13#define DFSAN_INTERFACE_H1415#include <sanitizer/common_interface_defs.h>16#include <stddef.h>17#include <stdint.h>1819#ifdef __cplusplus20extern "C" {21#endif2223typedef uint8_t dfsan_label;24typedef uint32_t dfsan_origin;2526/// Signature of the callback argument to dfsan_set_write_callback().27typedef void(SANITIZER_CDECL *dfsan_write_callback_t)(int fd, const void *buf,28size_t count);2930/// Signature of the callback argument to dfsan_set_conditional_callback().31typedef void(SANITIZER_CDECL *dfsan_conditional_callback_t)(32dfsan_label label, dfsan_origin origin);3334/// Signature of the callback argument to dfsan_set_reaches_function_callback().35/// The description is intended to hold the name of the variable.36typedef void(SANITIZER_CDECL *dfsan_reaches_function_callback_t)(37dfsan_label label, dfsan_origin origin, const char *file, unsigned int line,38const char *function);3940/// Computes the union of \c l1 and \c l2, resulting in a union label.41dfsan_label SANITIZER_CDECL dfsan_union(dfsan_label l1, dfsan_label l2);4243/// Sets the label for each address in [addr,addr+size) to \c label.44void SANITIZER_CDECL dfsan_set_label(dfsan_label label, void *addr,45size_t size);4647/// Sets the label for each address in [addr,addr+size) to the union of the48/// current label for that address and \c label.49void SANITIZER_CDECL dfsan_add_label(dfsan_label label, void *addr,50size_t size);5152/// Retrieves the label associated with the given data.53///54/// The type of 'data' is arbitrary. The function accepts a value of any type,55/// which can be truncated or extended (implicitly or explicitly) as necessary.56/// The truncation/extension operations will preserve the label of the original57/// value.58dfsan_label SANITIZER_CDECL dfsan_get_label(long data);5960/// Retrieves the immediate origin associated with the given data. The returned61/// origin may point to another origin.62///63/// The type of 'data' is arbitrary.64dfsan_origin SANITIZER_CDECL dfsan_get_origin(long data);6566/// Retrieves the label associated with the data at the given address.67dfsan_label SANITIZER_CDECL dfsan_read_label(const void *addr, size_t size);6869/// Return the origin associated with the first taint byte in the size bytes70/// from the address addr.71dfsan_origin SANITIZER_CDECL dfsan_read_origin_of_first_taint(const void *addr,72size_t size);7374/// Returns whether the given label contains the label elem.75int SANITIZER_CDECL dfsan_has_label(dfsan_label label, dfsan_label elem);7677/// Flushes the DFSan shadow, i.e. forgets about all labels currently associated78/// with the application memory. Use this call to start over the taint tracking79/// within the same process.80///81/// Note: If another thread is working with tainted data during the flush, that82/// taint could still be written to shadow after the flush.83void SANITIZER_CDECL dfsan_flush(void);8485/// Sets a callback to be invoked on calls to write(). The callback is invoked86/// before the write is done. The write is not guaranteed to succeed when the87/// callback executes. Pass in NULL to remove any callback.88void SANITIZER_CDECL89dfsan_set_write_callback(dfsan_write_callback_t labeled_write_callback);9091/// Sets a callback to be invoked on any conditional expressions which have a92/// taint label set. This can be used to find where tainted data influences93/// the behavior of the program.94/// These callbacks will only be added when -dfsan-conditional-callbacks=true.95void SANITIZER_CDECL96dfsan_set_conditional_callback(dfsan_conditional_callback_t callback);9798/// Conditional expressions occur during signal handlers.99/// Making callbacks that handle signals well is tricky, so when100/// -dfsan-conditional-callbacks=true, conditional expressions used in signal101/// handlers will add the labels they see into a global (bitwise-or together).102/// This function returns all label bits seen in signal handler conditions.103dfsan_label SANITIZER_CDECL dfsan_get_labels_in_signal_conditional();104105/// Sets a callback to be invoked when tainted data reaches a function.106/// This could occur at function entry, or at a load instruction.107/// These callbacks will only be added if -dfsan-reaches-function-callbacks=1.108void SANITIZER_CDECL109dfsan_set_reaches_function_callback(dfsan_reaches_function_callback_t callback);110111/// Making callbacks that handle signals well is tricky, so when112/// -dfsan-reaches-function-callbacks=true, functions reached in signal113/// handlers will add the labels they see into a global (bitwise-or together).114/// This function returns all label bits seen during signal handlers.115dfsan_label SANITIZER_CDECL dfsan_get_labels_in_signal_reaches_function();116117/// Interceptor hooks.118/// Whenever a dfsan's custom function is called the corresponding119/// hook is called it non-zero. The hooks should be defined by the user.120/// The primary use case is taint-guided fuzzing, where the fuzzer121/// needs to see the parameters of the function and the labels.122/// FIXME: implement more hooks.123void SANITIZER_CDECL dfsan_weak_hook_memcmp(void *caller_pc, const void *s1,124const void *s2, size_t n,125dfsan_label s1_label,126dfsan_label s2_label,127dfsan_label n_label);128void SANITIZER_CDECL dfsan_weak_hook_strncmp(void *caller_pc, const char *s1,129const char *s2, size_t n,130dfsan_label s1_label,131dfsan_label s2_label,132dfsan_label n_label);133134/// Prints the origin trace of the label at the address addr to stderr. It also135/// prints description at the beginning of the trace. If origin tracking is not136/// on, or the address is not labeled, it prints nothing.137void SANITIZER_CDECL dfsan_print_origin_trace(const void *addr,138const char *description);139/// As above, but use an origin id from dfsan_get_origin() instead of address.140/// Does not include header line with taint label and address information.141void SANITIZER_CDECL dfsan_print_origin_id_trace(dfsan_origin origin);142143/// Prints the origin trace of the label at the address \p addr to a144/// pre-allocated output buffer. If origin tracking is not on, or the address is145/// not labeled, it prints nothing.146///147/// Typical usage:148/// \code149/// char kDescription[] = "...";150/// char buf[1024];151/// dfsan_sprint_origin_trace(&tainted_var, kDescription, buf, sizeof(buf));152/// \endcode153///154/// Typical usage that handles truncation:155/// \code156/// char buf[1024];157/// int len = dfsan_sprint_origin_trace(&var, nullptr, buf, sizeof(buf));158///159/// if (len < sizeof(buf)) {160/// ProcessOriginTrace(buf);161/// } else {162/// char *tmpbuf = new char[len + 1];163/// dfsan_sprint_origin_trace(&var, nullptr, tmpbuf, len + 1);164/// ProcessOriginTrace(tmpbuf);165/// delete[] tmpbuf;166/// }167/// \endcode168///169/// \param addr The tainted memory address whose origin we are printing.170/// \param description A description printed at the beginning of the trace.171/// \param [out] out_buf The output buffer to write the results to.172/// \param out_buf_size The size of \p out_buf.173///174/// \returns The number of symbols that should have been written to \p out_buf175/// (not including trailing null byte '\0'). Thus, the string is truncated iff176/// return value is not less than \p out_buf_size.177size_t SANITIZER_CDECL dfsan_sprint_origin_trace(const void *addr,178const char *description,179char *out_buf,180size_t out_buf_size);181/// As above, but use an origin id from dfsan_get_origin() instead of address.182/// Does not include header line with taint label and address information.183size_t SANITIZER_CDECL dfsan_sprint_origin_id_trace(dfsan_origin origin,184char *out_buf,185size_t out_buf_size);186187/// Prints the stack trace leading to this call to a pre-allocated output188/// buffer.189///190/// For usage examples, see dfsan_sprint_origin_trace.191///192/// \param [out] out_buf The output buffer to write the results to.193/// \param out_buf_size The size of \p out_buf.194///195/// \returns The number of symbols that should have been written to \p out_buf196/// (not including trailing null byte '\0'). Thus, the string is truncated iff197/// return value is not less than \p out_buf_size.198size_t SANITIZER_CDECL dfsan_sprint_stack_trace(char *out_buf,199size_t out_buf_size);200201/// Retrieves the very first origin associated with the data at the given202/// address.203dfsan_origin SANITIZER_CDECL dfsan_get_init_origin(const void *addr);204205/// Returns the value of -dfsan-track-origins.206/// * 0: do not track origins.207/// * 1: track origins at memory store operations.208/// * 2: track origins at memory load and store operations.209int SANITIZER_CDECL dfsan_get_track_origins(void);210#ifdef __cplusplus211} // extern "C"212213template <typename T> void dfsan_set_label(dfsan_label label, T &data) {214dfsan_set_label(label, (void *)&data, sizeof(T));215}216217#endif218219#endif // DFSAN_INTERFACE_H220221222