From 2f9e5485153928799e62744839cec799ed17fa16 Mon Sep 17 00:00:00 2001 From: jdh8d <jdh8d@git.zephyr-software.com> Date: Wed, 21 Oct 2015 12:56:20 +0000 Subject: [PATCH] update to allow controlling of random seed and canary-value that's used Former-commit-id: ad0f88e86e87a46ec42691e084ae869d8cdaa198 --- tools/transforms/PNMain.cpp | 20 +++++++++++++++++++- tools/transforms/PNTransformDriver.cpp | 16 ++++++++++++++-- tools/transforms/globals.h | 20 +++++++++++++++++++- 3 files changed, 52 insertions(+), 4 deletions(-) diff --git a/tools/transforms/PNMain.cpp b/tools/transforms/PNMain.cpp index 9b96d488c..3973f2d10 100644 --- a/tools/transforms/PNMain.cpp +++ b/tools/transforms/PNMain.cpp @@ -71,7 +71,9 @@ enum RECURSIVE_MIN_STACK_PAD_OPTION, RECURSIVE_MAX_STACK_PAD_OPTION, SHOULD_DOUBLE_FRAME_SIZE_OPTION, - SELECTIVE_CANARIES_OPTION + SELECTIVE_CANARIES_OPTION, + SET_RANDOM_SEED, + SET_CANARY_VALUE }; @@ -96,6 +98,8 @@ static struct option const long_options[] = {"recursive_max_stack_padding",required_argument, NULL, RECURSIVE_MAX_STACK_PAD_OPTION}, {"should_double_frame_size",required_argument, NULL, SHOULD_DOUBLE_FRAME_SIZE_OPTION}, {"selective_canaries",required_argument, NULL, SELECTIVE_CANARIES_OPTION}, + {"random_seed",required_argument, NULL, SET_RANDOM_SEED}, + {"canary_value",required_argument, NULL, SET_CANARY_VALUE}, {NULL, 0, NULL, 0} }; @@ -362,6 +366,20 @@ int main(int argc, char **argv) break; } + case SET_RANDOM_SEED: + { + int the_seed=atoi(optarg); + cout<<"Setting random seed to: "<<the_seed<<endl; + pn_options->setRandomSeed(the_seed); + break; + } + case SET_CANARY_VALUE: + { + int the_val=strtoul(optarg, NULL, 0); + cout<<"Setting canary value to: 0x"<<hex<<the_val<<endl; + pn_options->setCanaryValue(the_val); + break; + } case '?': { //error message already printed by getopt_long diff --git a/tools/transforms/PNTransformDriver.cpp b/tools/transforms/PNTransformDriver.cpp index c2d1a43e3..a48fe4d1d 100644 --- a/tools/transforms/PNTransformDriver.cpp +++ b/tools/transforms/PNTransformDriver.cpp @@ -101,7 +101,8 @@ PNTransformDriver::PNTransformDriver(VariantID_t *pidp,string BED_script, pqxxDB { //TODO: throw exception? assert(pidp != NULL); - srand(time(NULL)); + + srand(pn_options->getRandomSeed()); //TODO: throw exception? this->pidp = pidp; @@ -2162,17 +2163,28 @@ bool PNTransformDriver::Validate(FileIR_t *virp, string name) unsigned int PNTransformDriver::GetRandomCanary() { + /* get a canary value from the options. + * assume the options package is returning a full 32-bits of entropy. + */ + return pn_options->getCanaryValue(); + +#if 0 +/* note: this code is being careful to get a full 32-bits of entropy, and rand() is only promising 16-bits of entropy. + */ //TODO: check for bias. stringstream canary; canary.str(""); + + //canary<<hex<<pn_options->GetCanaryValue(); for(int i=0;i<8;i++) { - canary<<hex<<(rand()%16); + canary<<hex<< (rand()%16); } unsigned int ret_val; sscanf(canary.str().c_str(),"%x",&ret_val); return ret_val; +#endif } bool PNTransformDriver::Canary_Rewrite(PNStackLayout *orig_layout, Function_t *func) diff --git a/tools/transforms/globals.h b/tools/transforms/globals.h index c9b51c251..feaf87ce1 100644 --- a/tools/transforms/globals.h +++ b/tools/transforms/globals.h @@ -23,6 +23,8 @@ #include <set> #include <string> +#include <stdlib.h> + extern bool verbose_log; @@ -37,8 +39,11 @@ class PNOptions recursive_min_stack_padding = 32; recursive_max_stack_padding = 64; do_canaries = true; - do_selective_canaries = true; + do_selective_canaries = false; should_double_frame_size=true; + random_seed=time(0); + canary_value=0; + canary_value_inited=false; } void setMinStackPadding(int val) { min_stack_padding = val; } @@ -46,12 +51,22 @@ class PNOptions void setRecursiveMinStackPadding(int val) { recursive_min_stack_padding = val; } void setRecursiveMaxStackPadding(int val) { recursive_max_stack_padding = val; } void setShouldDoubleFrameSize(bool val) { should_double_frame_size = val; } + void setRandomSeed(bool val) { random_seed = val; } + void setCanaryValue(int val) { canary_value = val; canary_value_inited=true; } int getMinStackPadding() const { return min_stack_padding; } int getMaxStackPadding() const { return max_stack_padding; } int getRecursiveMinStackPadding() const { return recursive_min_stack_padding; } int getRecursiveMaxStackPadding() const { return recursive_max_stack_padding; } bool getShouldDoubleFrameSize() const { return should_double_frame_size; } + bool getRandomSeed() { return random_seed; } + int getCanaryValue() + { + if (canary_value_inited) + return canary_value; + else + return (rand()&0xffff) | (rand()<<16); + } void setDoCanaries(bool canaries) { do_canaries = canaries; } bool getDoCanaries() const { return do_canaries; } @@ -77,6 +92,9 @@ class PNOptions bool do_canaries; bool do_selective_canaries; bool should_double_frame_size; + bool random_seed; + int canary_value; + bool canary_value_inited; std::set<std::string> canary_functions; }; -- GitLab