11package it .unipr .checker ;
22
3+ import org .apache .logging .log4j .LogManager ;
4+ import org .apache .logging .log4j .Logger ;
5+
36import it .unipr .analysis .MyCache ;
47import it .unipr .analysis .taint .TaintAbstractDomain ;
58import it .unipr .analysis .taint .TaintElement ;
69import it .unipr .cfg .EVMCFG ;
7- import it .unipr .cfg .Origin ;
10+ import it .unipr .cfg .Jumpi ;
811import it .unipr .cfg .ProgramCounterLocation ;
912import it .unive .lisa .analysis .AnalysisState ;
1013import it .unive .lisa .analysis .AnalyzedCFG ;
1720import it .unive .lisa .checks .semantic .SemanticCheck ;
1821import it .unive .lisa .program .cfg .CFG ;
1922import it .unive .lisa .program .cfg .statement .Statement ;
20- import java .util .Set ;
21- import org .apache .logging .log4j .LogManager ;
22- import org .apache .logging .log4j .Logger ;
2323
2424public class TxOriginChecker implements
25- SemanticCheck <SimpleAbstractState <MonolithicHeap , TaintAbstractDomain , TypeEnvironment <InferredTypes >>> {
25+ SemanticCheck <SimpleAbstractState <MonolithicHeap , TaintAbstractDomain , TypeEnvironment <InferredTypes >>> {
2626
2727 private static final Logger log = LogManager .getLogger (TxOriginChecker .class );
2828
2929 @ Override
3030 public boolean visit (
3131 CheckToolWithAnalysisResults <
32- SimpleAbstractState <MonolithicHeap , TaintAbstractDomain , TypeEnvironment <InferredTypes >>> tool ,
32+ SimpleAbstractState <MonolithicHeap , TaintAbstractDomain , TypeEnvironment <InferredTypes >>> tool ,
3333 CFG graph , Statement node ) {
3434
35- if (node instanceof Origin ) {
36- EVMCFG cfg = ((EVMCFG ) graph );
37- Set <Statement > jumps = cfg .getAllJumpI ();
38- Statement origin = node ;
35+ if (node instanceof Jumpi ) {
36+ EVMCFG cfg = (EVMCFG ) graph ;
3937
4038 for (AnalyzedCFG <SimpleAbstractState <MonolithicHeap , TaintAbstractDomain ,
4139 TypeEnvironment <InferredTypes >>> result : tool .getResultOf (cfg )) {
4240 AnalysisState <SimpleAbstractState <MonolithicHeap , TaintAbstractDomain ,
43- TypeEnvironment <InferredTypes >>> analysisResult = null ;
41+ TypeEnvironment <InferredTypes >>> analysisResult = null ;
4442
45- for (Statement jump : jumps ) {
46- try {
47- analysisResult = result .getAnalysisStateBefore (jump );
48- } catch (SemanticException e1 ) {
49- log .error ("(TxOriginChecker): {}" , e1 .getMessage ());
50- }
43+ try {
44+ analysisResult = result .getAnalysisStateBefore (node );
45+ } catch (SemanticException e1 ) {
46+ log .error ("(TxOriginChecker): {}" , e1 .getMessage ());
47+ }
5148
52- // Retrieve the symbolic stack from the analysis result
53- TaintAbstractDomain taintedStack = analysisResult .getState ().getValueState ();
49+ // Retrieve the symbolic stack from the analysis result
50+ TaintAbstractDomain stack = analysisResult .getState ().getValueState ();
5451
55- // If the stack is bottom, the jump is definitely
56- // unreachable
57- if (taintedStack .isBottom ())
52+ // If the stack is bottom, the node is definitely
53+ // unreachable
54+ if (stack .isBottom ())
55+ // Nothing to do
56+ continue ;
57+ else {
58+ TaintElement firstElem = stack .getFirstElement ();
59+ TaintElement secondElem = stack .getSecondElement ();
60+ if (firstElem .isBottom () || secondElem .isBottom ())
5861 // Nothing to do
5962 continue ;
6063 else {
61- TaintElement firstStackElement = taintedStack . getFirstElement ();
62- TaintElement secondStackElement = taintedStack . getSecondElement ();
63- if (secondStackElement . isBottom ())
64- // Nothing to do
65- continue ;
66- else {
67- // Checks if either first or second element in the
68- // stack is tainted
69- if ( firstStackElement . isTaint () || secondStackElement . isTaint ()) {
70- checkForTxOrigin ( origin , jump , tool , cfg );
71- }
64+ // Checks if either first or second element in the
65+ // stack is tainted
66+ if (firstElem . isTaint () || secondElem . isTaint ()) {
67+ ProgramCounterLocation jumploc = ( ProgramCounterLocation ) node . getLocation ();
68+
69+ log . debug ( "Tx. Origin attack at {} at line no. {}" , jumploc . getPc (),
70+ jumploc . getSourceCodeLine ());
71+
72+ String warn = "TxOrigin attack at " + (( ProgramCounterLocation ) node . getLocation ()). getSourceCodeLine ();
73+ tool . warn ( warn );
74+ MyCache . getInstance (). addTxOriginWarning ( cfg . hashCode (), warn );
7275 }
7376 }
7477 }
7578 }
76-
7779 }
7880
7981 return true ;
8082 }
81-
82- private void checkForTxOrigin (Statement origin , Statement jump , CheckToolWithAnalysisResults <
83- SimpleAbstractState <MonolithicHeap , TaintAbstractDomain , TypeEnvironment <InferredTypes >>> tool ,
84- EVMCFG cfg ) {
85- if (cfg .reachableFrom (origin , jump )) {
86- ProgramCounterLocation jumploc = (ProgramCounterLocation ) jump .getLocation ();
87-
88- log .debug ("Tx. Origin attack at {} at line no. {} coming from line {}" , jumploc .getPc (),
89- jumploc .getSourceCodeLine (),
90- ((ProgramCounterLocation ) origin .getLocation ()).getSourceCodeLine ());
91-
92- String warn = "TxOrigin attack at " + ((ProgramCounterLocation ) origin .getLocation ()).getSourceCodeLine ();
93- tool .warn (warn );
94- MyCache .getInstance ().addTxOriginWarning (cfg .hashCode (), warn );
95- }
96- }
9783}
0 commit comments