Skip to content

Commit 4cb5570

Browse files
committedJan 14, 2015
Insert random noops to increase security against ROP attacks (clang)
A pass that adds random noops to X86 binaries to introduce diversity with the goal of increasing security against most return-oriented programming attacks. Command line options: -noop-insertion // Enable noop insertion. -noop-insertion-percentage=X // X% of assembly instructions will have a noop prepended (default: 50%, requires -noop-insertion) -max-noops-per-instruction=X // Randomly generate X noops per instruction. ie. roll the dice X times with probability set above (default: 1). This doesn't guarantee X noop instructions. In addition, the following 'quick switch' in clang enables basic diversity using default settings (currently: noop insertion and schedule randomization; it is intended to be extended in the future). -fdiversify This is the clang part of the patch. llvm part: D3392 http://reviews.llvm.org/D3393 Patch by Stephen Crane (@rinon) llvm-svn: 225910
1 parent 934361a commit 4cb5570

File tree

6 files changed

+19
-1
lines changed

6 files changed

+19
-1
lines changed
 

Diff for: ‎clang/include/clang/Driver/CC1Options.td

+2
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,8 @@ def relaxed_aliasing : Flag<["-"], "relaxed-aliasing">,
194194
HelpText<"Turn off Type Based Alias Analysis">;
195195
def no_struct_path_tbaa : Flag<["-"], "no-struct-path-tbaa">,
196196
HelpText<"Turn off struct-path aware Type Based Alias Analysis">;
197+
def noop_insertion : Flag<["-"], "noop-insertion">,
198+
HelpText<"Randomly insert NOOPs">;
197199
def masm_verbose : Flag<["-"], "masm-verbose">,
198200
HelpText<"Generate verbose assembly output">;
199201
def mcode_model : Separate<["-"], "mcode-model">,

Diff for: ‎clang/include/clang/Driver/Options.td

+2-1
Original file line numberDiff line numberDiff line change
@@ -469,6 +469,7 @@ def fdiagnostics_show_category_EQ : Joined<["-"], "fdiagnostics-show-category=">
469469
def fdiagnostics_show_template_tree : Flag<["-"], "fdiagnostics-show-template-tree">,
470470
Group<f_Group>, Flags<[CC1Option]>,
471471
HelpText<"Print a template comparison tree for differing templates">;
472+
def fdiversify : Flag<["-"], "fdiversify">, Group<f_clang_Group>;
472473
def fdollars_in_identifiers : Flag<["-"], "fdollars-in-identifiers">, Group<f_Group>,
473474
HelpText<"Allow '$' in identifiers">, Flags<[CC1Option]>;
474475
def fdwarf2_cfi_asm : Flag<["-"], "fdwarf2-cfi-asm">, Group<clang_ignored_f_Group>;
@@ -857,7 +858,7 @@ def fprofile_arcs : Flag<["-"], "fprofile-arcs">, Group<f_Group>;
857858
def fno_profile_arcs : Flag<["-"], "fno-profile-arcs">, Group<f_Group>;
858859
def fprofile_generate : Flag<["-"], "fprofile-generate">, Group<f_Group>;
859860
def framework : Separate<["-"], "framework">, Flags<[LinkerInput]>;
860-
def frandom_seed_EQ : Joined<["-"], "frandom-seed=">, Group<clang_ignored_f_Group>;
861+
def frandom_seed_EQ : Joined<["-"], "frandom-seed=">, Group<f_Group>, Flags<[CC1Option]>;
861862
def freg_struct_return : Flag<["-"], "freg-struct-return">, Group<f_Group>, Flags<[CC1Option]>,
862863
HelpText<"Override the default ABI to return small structs in registers">;
863864
def frtti : Flag<["-"], "frtti">, Group<f_Group>;

Diff for: ‎clang/include/clang/Frontend/CodeGenOptions.def

+2
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,8 @@ ENUM_CODEGENOPT(Inlining, InliningMethod, 2, NoInlining)
160160
/// The default TLS model to use.
161161
ENUM_CODEGENOPT(DefaultTLSModel, TLSModel, 2, GeneralDynamicTLSModel)
162162

163+
CODEGENOPT(NoopInsertion, 1, 0) ///< Randomly add NOPs
164+
163165
#undef CODEGENOPT
164166
#undef ENUM_CODEGENOPT
165167
#undef VALUE_CODEGENOPT

Diff for: ‎clang/lib/CodeGen/BackendUtil.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -519,6 +519,7 @@ TargetMachine *EmitAssemblyHelper::CreateTargetMachine(bool MustCreateTM) {
519519
Options.PositionIndependentExecutable = LangOpts.PIELevel != 0;
520520
Options.FunctionSections = CodeGenOpts.FunctionSections;
521521
Options.DataSections = CodeGenOpts.DataSections;
522+
Options.NoopInsertion = CodeGenOpts.NoopInsertion;
522523

523524
Options.MCOptions.MCRelaxAll = CodeGenOpts.RelaxAll;
524525
Options.MCOptions.MCSaveTempLabels = CodeGenOpts.SaveTempLabels;

Diff for: ‎clang/lib/Driver/Tools.cpp

+11
Original file line numberDiff line numberDiff line change
@@ -3737,6 +3737,17 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,
37373737
getToolChain().getTriple().getArch() == llvm::Triple::aarch64_be)
37383738
CmdArgs.push_back("-fallow-half-arguments-and-returns");
37393739

3740+
// Translate -frandom-seed to seed the LLVM RNG
3741+
if (Args.hasArg(options::OPT_frandom_seed_EQ)) {
3742+
StringRef seed = Args.getLastArgValue(options::OPT_frandom_seed_EQ);
3743+
CmdArgs.push_back("-backend-option");
3744+
CmdArgs.push_back(Args.MakeArgString("-rng-seed=" + seed));
3745+
}
3746+
3747+
if (Args.hasArg(options::OPT_fdiversify)) {
3748+
CmdArgs.push_back("-noop-insertion");
3749+
}
3750+
37403751
if (Arg *A = Args.getLastArg(options::OPT_mrestrict_it,
37413752
options::OPT_mno_restrict_it)) {
37423753
if (A->getOption().matches(options::OPT_mrestrict_it)) {

Diff for: ‎clang/lib/Frontend/CompilerInvocation.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -516,6 +516,7 @@ static bool ParseCodeGenArgs(CodeGenOptions &Opts, ArgList &Args, InputKind IK,
516516
Opts.SSPBufferSize =
517517
getLastArgIntValue(Args, OPT_stack_protector_buffer_size, 8, Diags);
518518
Opts.StackRealignment = Args.hasArg(OPT_mstackrealign);
519+
Opts.NoopInsertion = Args.hasArg(OPT_noop_insertion);
519520
if (Arg *A = Args.getLastArg(OPT_mstack_alignment)) {
520521
StringRef Val = A->getValue();
521522
unsigned StackAlignment = Opts.StackAlignment;

0 commit comments

Comments
 (0)
Please sign in to comment.