11package it .unipr .crosschain .checker ;
22
3+ import it .unipr .analysis .contract .Signature ;
34import it .unipr .analysis .taint .TaintAbstractDomain ;
45import it .unipr .analysis .taint .TaintElement ;
56import it .unipr .cfg .*;
1516import it .unive .lisa .checks .semantic .SemanticCheck ;
1617import it .unive .lisa .program .cfg .CFG ;
1718import it .unive .lisa .program .cfg .statement .Statement ;
19+ import java .util .HashSet ;
20+ import java .util .Set ;
1821import org .apache .logging .log4j .LogManager ;
1922import org .apache .logging .log4j .Logger ;
2023
2124public class VulnerableLOGsComputer implements
2225 SemanticCheck <SimpleAbstractState <MonolithicHeap , TaintAbstractDomain , TypeEnvironment <InferredTypes >>> {
2326
2427 private static final Logger log = LogManager .getLogger (VulnerableLOGsComputer .class );
28+ private Set <Statement > eventsExitpoints ;
29+
30+ public VulnerableLOGsComputer (Set <Signature > events ) {
31+ this .eventsExitpoints = new HashSet <>();
32+ for (Signature event : events )
33+ eventsExitpoints .addAll (event .getExitPoints ());
34+ }
2535
2636 @ Override
2737 public boolean visit (
2838 CheckToolWithAnalysisResults <
2939 SimpleAbstractState <MonolithicHeap , TaintAbstractDomain , TypeEnvironment <InferredTypes >>> tool ,
3040 CFG graph , Statement node ) {
3141
32- if (node instanceof Log ) {
33-
42+ if (eventsExitpoints .contains (node )) {
3443 EVMCFG cfg = ((EVMCFG ) graph );
3544
3645 for (AnalyzedCFG <SimpleAbstractState <MonolithicHeap , TaintAbstractDomain ,
@@ -47,43 +56,45 @@ public boolean visit(
4756 // Retrieve the symbolic stack from the analysis result
4857 TaintAbstractDomain taintedStack = analysisResult .getState ().getValueState ();
4958
50- if (taintedStack .isBottom ())
59+ if (taintedStack .isBottom () || taintedStack . isTop () )
5160 // Nothing to do
5261 continue ;
53- else {
54- if (node instanceof Log1 )
55- if (TaintElement .isAtLeastOneTainted (taintedStack .getElementAtPosition (1 ),
56- taintedStack .getElementAtPosition (2 ),
57- taintedStack .getElementAtPosition (3 )))
58- addVulnerableLOG (node );
59- if (node instanceof Log2 )
60- if (TaintElement .isAtLeastOneTainted (taintedStack .getElementAtPosition (1 ),
61- taintedStack .getElementAtPosition (2 ),
62- taintedStack .getElementAtPosition (3 ),
63- taintedStack .getElementAtPosition (4 )))
64- addVulnerableLOG (node );
65- if (node instanceof Log3 )
66- if (TaintElement .isAtLeastOneTainted (taintedStack .getElementAtPosition (1 ),
67- taintedStack .getElementAtPosition (2 ),
68- taintedStack .getElementAtPosition (3 ),
69- taintedStack .getElementAtPosition (4 ),
70- taintedStack .getElementAtPosition (5 )))
71- addVulnerableLOG (node );
72- if (node instanceof Log4 )
73- if (TaintElement .isAtLeastOneTainted (taintedStack .getElementAtPosition (1 ),
74- taintedStack .getElementAtPosition (2 ),
75- taintedStack .getElementAtPosition (3 ),
76- taintedStack .getElementAtPosition (4 ),
77- taintedStack .getElementAtPosition (5 ),
78- taintedStack .getElementAtPosition (6 )))
79- addVulnerableLOG (node );
80- }
62+
63+ int numArgs = getNumberOfArgs (node );
64+ boolean isAtLeastOneTainted = false ;
65+
66+ for (int argIndex = 1 ; argIndex <= numArgs ; argIndex ++)
67+ isAtLeastOneTainted |= TaintElement .isAtLeastOneTainted (
68+ taintedStack .getElementAtPosition (argIndex ));
69+
70+ if (isAtLeastOneTainted )
71+ addVulnerableLOG (node );
8172 }
8273 }
8374
8475 return true ;
8576 }
8677
78+ /**
79+ * Computes the number of arguments consumed from the stack by the provided
80+ * EVM instruction.
81+ *
82+ * @param node the statement to inspect
83+ *
84+ * @return the amount of stack elements consumed by {@code node}
85+ */
86+ private int getNumberOfArgs (Statement node ) {
87+ if (node instanceof Log1 )
88+ return 3 ;
89+ if (node instanceof Log2 )
90+ return 4 ;
91+ if (node instanceof Log3 )
92+ return 5 ;
93+ if (node instanceof Log4 )
94+ return 6 ;
95+ return 0 ;
96+ }
97+
8798 /**
8899 * Adds a vulnerable log statement to the time synchronization checker and
89100 * logs a warning message indicating the potential vulnerability.
@@ -95,7 +106,7 @@ private void addVulnerableLOG(Statement node) {
95106 MyCache .getInstance ().addVulnerableLogStatementForLocalDependencyChecker (node );
96107
97108 ProgramCounterLocation nodeLocation = (ProgramCounterLocation ) node .getLocation ();
98- log .warn ("(Time Synchronization vulnerability) LOG possibly vulnerable at pc {} (line {}) (cfg={})." ,
109+ log .warn ("(Local Dependency Checker) Event possibly vulnerable at pc {} (line {}) (cfg={})." ,
99110 nodeLocation .getPc (),
100111 nodeLocation .getSourceCodeLine (),
101112 node .getCFG ().hashCode ());
0 commit comments