Skip to content

Commit 6ecf701

Browse files
author
Marek Surovič
authored
Merge pull request #6486 from trailofbits/Henrik/taint-argv
2 parents 0a45a60 + 250c9f7 commit 6ecf701

File tree

19 files changed

+209
-9
lines changed

19 files changed

+209
-9
lines changed

CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ if(NOT EXISTS "${PROJECT_SOURCE_DIR}/third_party/indicators/CMakeLists.txt")
2626
message(FATAL_ERROR "git submodule update --init --recursive must be run first to checkout submodules")
2727
endif()
2828

29+
set(CMAKE_CXX_STANDARD 20)
30+
2931
add_subdirectory(third_party/Catch2)
3032
list(APPEND CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/third_party/Catch2/contrib")
3133
add_subdirectory(third_party/indicators)

Dockerfile

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,17 @@ RUN update-ca-certificates
2020
RUN update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.8 10
2121
RUN python3 -m pip install pip && python3 -m pip install pytest
2222

23+
WORKDIR /blight
24+
RUN git clone https://github.com/trailofbits/blight.git .
25+
RUN pip3 install .
26+
2327
COPY . /polytracker
2428

2529
RUN mkdir /polytracker/build
2630
WORKDIR /polytracker/build
2731
RUN cmake -GNinja -DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++ -DCMAKE_VERBOSE_MAKEFILE=TRUE -DCXX_LIB_PATH=/cxx_libs ..
2832
RUN ninja install
2933

30-
WORKDIR /blight
31-
RUN git clone https://github.com/trailofbits/blight.git .
32-
RUN pip3 install .
3334

3435
WORKDIR /polytracker
3536
RUN pip3 install .

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,8 @@ variables PolyTracker supports is:
206206
POLYDB: A path to which to save the output database (default is polytracker.tdag)
207207
208208
WLLVM_ARTIFACT_STORE: Provides a path to an existing directory to store artifact/manifest for all build targets
209+
210+
POLYTRACKER_TAINT_ARGV: Set to '1' to use argv as a taint source.
209211
```
210212

211213
Polytracker will set its configuration parameters in the following order:

polytracker/custom_abi/dfsan_abilist.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ fun:__polytracker_store_blob=uninstrumented
6262
fun:__polytracker_store_blob=discard
6363
fun:__polytracker_preserve_map=uninstrumented
6464
fun:__polytracker_preserve_map=discard
65+
fun:__polytracker_taint_argv=uninstrumented
66+
fun:__polytracker_taint_argv=discard
6567

6668
fun:__dfsan_update_label_count=uninstrumented
6769
fun:__dfsan_update_label_count=discard

polytracker/custom_abi/polytracker_abilist.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ fun:__polytracker_store_blob=uninstrumented
6262
fun:__polytracker_store_blob=discard
6363
fun:__polytracker_preserve_map=uninstrumented
6464
fun:__polytracker_preserve_map=discard
65+
fun:__polytracker_taint_argv=uninstrumented
66+
fun:__polytracker_taint_argv=discard
6567

6668
fun:__remill_jump=uninstrumented
6769
fun:__remill_jump=discard

polytracker/include/polytracker/polytracker.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,6 @@ extern uint64_t func_mapping_count;
4646

4747
extern const block_mapping *block_mappings;
4848
extern uint64_t block_mapping_count;
49+
50+
// Controls argv being a taint source
51+
extern bool polytracker_taint_argv;

polytracker/include/polytracker/taint_sources.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,8 @@
88
#define EXT_C_FUNC extern "C" __attribute__((visibility("default")))
99
#define EXT_CXX_FUNC extern __attribute__((visibility("default")))
1010

11+
namespace polytracker {
12+
void taint_argv(int argc, char *argv[]);
13+
}
14+
1115
#endif

polytracker/include/taintdag/polytracker.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#define POLYTRACKER_TAINTDAG_POLYTRACKER_H
33

44
#include <filesystem>
5+
#include <span>
56

67
#include "taintdag/fdmapping.hpp"
78
#include "taintdag/output.hpp"
@@ -32,6 +33,13 @@ class PolyTracker {
3233
std::optional<taint_range_t> source_taint(int fd, source_offset_t offset,
3334
size_t length);
3435

36+
// Create a new taint source (not a file) and assigns taint labels
37+
// A new taint source named 'name' is created
38+
// Memory in 'dst' is assigned source taint labels referring to source 'name'
39+
// and in increasing offset.
40+
std::optional<taint_range_t> create_taint_source(std::string_view name,
41+
std::span<uint8_t> dst);
42+
3543
// Update the label, it affects control flow
3644
void affects_control_flow(label_t taint_label);
3745

polytracker/src/CMakeLists.txt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
set(CMAKE_CXX_STANDARD 17)
2-
31
find_package(LLVM 13 CONFIG)
42

53
if(LLVM_FOUND)
@@ -50,7 +48,8 @@ set(CMAKE_EXE_LINKER_FLAGS
5048
set(POLY_SOURCES ${POLY_DIR}/main.cpp ${POLY_DIR}/polytracker.cpp)
5149

5250
set(TAINT_SOURCES ${TAINT_DIR}/taint_sources.cpp ${TAINT_DIR}/string_taints.cpp
53-
${TAINT_DIR}/memory_taints.cpp ${TAINT_DIR}/write_taints.cpp)
51+
${TAINT_DIR}/memory_taints.cpp ${TAINT_DIR}/write_taints.cpp
52+
${TAINT_DIR}/argv.cpp)
5453

5554
set(TAINTDAG_SOURCES ${TAINTDAG_DIR}/encoding.cpp ${TAINTDAG_DIR}/fdmapping.cpp
5655
${TAINTDAG_DIR}/output.cpp ${TAINTDAG_DIR}/polytracker.cpp)

polytracker/src/passes/polytracker_pass.cpp

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
// #include "polytracker/thread_pool.h"
66
#include "spdlog/cfg/env.h"
77
#include "spdlog/spdlog.h"
8+
#include "llvm/IR/Argument.h"
89
#include "llvm/IR/BasicBlock.h"
910
#include "llvm/IR/Dominators.h"
1011
#include "llvm/IR/Function.h"
@@ -289,12 +290,54 @@ bool PolytrackerPass::analyzeBlock(llvm::Function *func,
289290
return true;
290291
}
291292

293+
// Inserts a function call to polytracker::taint_argv(argc, argv)
294+
// Assumes main is actually the main function of the program and
295+
// interprets first arg as argc and second as argv.
296+
static void emitTaintArgvCall(llvm::Function &main) {
297+
// Get the parameters of the main function, argc, argv
298+
auto argc = main.getArg(0);
299+
if (!argc) {
300+
spdlog::error("Failed to instrument argv. No argc available.");
301+
return;
302+
}
303+
auto argc_ty = argc->getType();
304+
305+
auto argv = main.getArg(1);
306+
if (!argv) {
307+
spdlog::error("Failed to instrument argv. No argv available.");
308+
return;
309+
}
310+
auto argv_ty = argv->getType();
311+
312+
// IRBuilder for emitting a call to __polytracker_taint_argv. Need to
313+
// specify insertion point first, to ensure that no instruction can
314+
// use argv before it is tainted.
315+
llvm::IRBuilder<> irb(&*(main.getEntryBlock().getFirstInsertionPt()));
316+
317+
// Define the target function type and make it available in the module
318+
auto taint_argv_ty =
319+
llvm::FunctionType::get(irb.getVoidTy(), {argc_ty, argv_ty}, false);
320+
llvm::FunctionCallee taint_argv = main.getParent()->getOrInsertFunction(
321+
"__polytracker_taint_argv", taint_argv_ty);
322+
if (!taint_argv) {
323+
spdlog::error("Failed to declare __polytracker_taint_argv.");
324+
return;
325+
}
326+
327+
// Emit the call using parameters from main.
328+
auto ci = irb.CreateCall(taint_argv, {argc, argv});
329+
if (!ci) {
330+
spdlog::error("Failed to insert call to taint_argv.");
331+
}
332+
}
333+
292334
/*
293335
We should instrument everything we have bitcode for, right?
294336
If instructions have __polytracker, or they have __dfsan, ignore!
295337
*/
296338
bool PolytrackerPass::analyzeFunction(llvm::Function *f,
297339
const func_index_t &func_index) {
340+
298341
// Add Function entry
299342
polytracker::BBSplittingPass bbSplitter;
300343
// llvm::removeUnreachableBlocks(*f);
@@ -340,6 +383,11 @@ bool PolytrackerPass::analyzeFunction(llvm::Function *f,
340383
visit(inst);
341384
}
342385

386+
// If this is the main function, insert a taint-argv call
387+
if (f && f->getName() == "main") {
388+
emitTaintArgvCall(*f);
389+
}
390+
343391
return true;
344392
}
345393

0 commit comments

Comments
 (0)