11package it .unipr .checker ;
22
3+ import it .unipr .analysis .AbstractStack ;
34import it .unipr .analysis .EVMAbstractState ;
45import it .unipr .analysis .MyCache ;
6+ import it .unipr .analysis .StackElement ;
7+ import it .unipr .analysis .taint .TaintAbstractStack ;
8+ import it .unipr .analysis .taint .TaintElement ;
59import it .unipr .cfg .EVMCFG ;
610import it .unipr .cfg .Origin ;
711import it .unipr .cfg .ProgramCounterLocation ;
12+ import it .unive .lisa .analysis .AnalysisState ;
13+ import it .unive .lisa .analysis .AnalyzedCFG ;
14+ import it .unive .lisa .analysis .SemanticException ;
815import it .unive .lisa .analysis .SimpleAbstractState ;
916import it .unive .lisa .analysis .heap .MonolithicHeap ;
1017import it .unive .lisa .analysis .nonrelational .value .TypeEnvironment ;
1825import org .apache .logging .log4j .Logger ;
1926
2027public class TxOriginChecker implements
21- SemanticCheck <SimpleAbstractState <MonolithicHeap , EVMAbstractState , TypeEnvironment <InferredTypes >>> {
28+ SemanticCheck <SimpleAbstractState <MonolithicHeap , TaintAbstractStack , TypeEnvironment <InferredTypes >>> {
2229
2330 private static final Logger log = LogManager .getLogger (TxOriginChecker .class );
2431
2532 @ Override
2633 public boolean visit (
2734 CheckToolWithAnalysisResults <
28- SimpleAbstractState <MonolithicHeap , EVMAbstractState , TypeEnvironment <InferredTypes >>> tool ,
35+ SimpleAbstractState <MonolithicHeap , TaintAbstractStack , TypeEnvironment <InferredTypes >>> tool ,
2936 CFG graph , Statement node ) {
3037
3138 if (node instanceof Origin ) {
3239 EVMCFG cfg = ((EVMCFG ) graph );
33- Set <Statement > jumps = cfg .getAllJumps ();
40+ Set <Statement > jumps = cfg .getAllJumpI ();
3441 Statement origin = node ;
3542
36- for (Statement jump : jumps ) {
43+ for (AnalyzedCFG <SimpleAbstractState <MonolithicHeap , TaintAbstractStack ,
44+ TypeEnvironment <InferredTypes >>> result : tool .getResultOf (cfg )) {
45+ AnalysisState <SimpleAbstractState <MonolithicHeap , TaintAbstractStack ,
46+ TypeEnvironment <InferredTypes >>> analysisResult = null ;
47+
48+ for (Statement jump : jumps ) {
49+ try {
50+ analysisResult = result .getAnalysisStateBefore (jump );
51+ } catch (SemanticException e1 ) {
52+ log .error ("(TxOriginChecker): {}" , e1 .getMessage ());
53+ }
54+
55+ // Retrieve the symbolic stack from the analysis result
56+ TaintAbstractStack taintedStack = analysisResult .getState ().getValueState ();
57+
58+ // If the stack is bottom, the jump is definitely
59+ // unreachable
60+ if (taintedStack .isBottom ())
61+ // Nothing to do
62+ continue ;
63+ else {
64+ TaintElement firstStackElement = taintedStack .getFirstElement ();
65+ TaintElement secondStackElement = taintedStack .getSecondElement ();
66+ if (secondStackElement .isBottom ())
67+ // Nothing to do
68+ continue ;
69+ else {
70+ // Checks if either first or second element in the stack is tainted
71+ if (firstStackElement .isTaint () || secondStackElement .isTaint ()) {
72+ checkForTxOrigin (origin , jump , tool , cfg );
73+ }
74+ }
75+ }
76+ }
77+
78+
79+
80+
81+
82+
83+ }
84+ /*for (Statement jump : jumps) {
3785 if (cfg.reachableFrom(origin, jump)) {
3886 ProgramCounterLocation jumploc = (ProgramCounterLocation) jump.getLocation();
3987
@@ -54,10 +102,26 @@ public boolean visit(
54102 // contaminated value from the stack by origin opcode
55103 // (GENERICS)
56104 }
57- }
105+ }*/
58106
59107 }
60108
61109 return true ;
62110 }
111+
112+ private void checkForTxOrigin (Statement origin , Statement jump , CheckToolWithAnalysisResults <
113+ SimpleAbstractState <MonolithicHeap , TaintAbstractStack , TypeEnvironment <InferredTypes >>> tool , EVMCFG cfg ) {
114+ if (cfg .reachableFrom (origin , jump )) {
115+ ProgramCounterLocation jumploc = (ProgramCounterLocation ) jump .getLocation ();
116+
117+ log .debug ("Tx. Origin attack at {} at line no. {} coming from line {}" , jumploc .getPc (),
118+ jumploc .getSourceCodeLine (),
119+ ((ProgramCounterLocation ) origin .getLocation ()).getSourceCodeLine ());
120+
121+ String warn = "TxOrigin attack at " + jumploc .getPc ();
122+ tool .warn (warn );
123+ MyCache .getInstance ().addTxOriginWarning (cfg .hashCode (), warn );
124+
125+ }
126+ }
63127}
0 commit comments