|
| 1 | +package it.unipr.crosschain.checker; |
| 2 | + |
| 3 | +import it.unipr.analysis.contract.SmartContract; |
| 4 | +import it.unipr.analysis.taint.TaintAbstractDomain; |
| 5 | +import it.unipr.analysis.taint.TaintElement; |
| 6 | +import it.unipr.cfg.*; |
| 7 | +import it.unipr.utils.MyCache; |
| 8 | +import it.unive.lisa.analysis.AnalysisState; |
| 9 | +import it.unive.lisa.analysis.AnalyzedCFG; |
| 10 | +import it.unive.lisa.analysis.SemanticException; |
| 11 | +import it.unive.lisa.analysis.SimpleAbstractState; |
| 12 | +import it.unive.lisa.analysis.heap.MonolithicHeap; |
| 13 | +import it.unive.lisa.analysis.nonrelational.value.TypeEnvironment; |
| 14 | +import it.unive.lisa.analysis.types.InferredTypes; |
| 15 | +import it.unive.lisa.checks.semantic.CheckToolWithAnalysisResults; |
| 16 | +import it.unive.lisa.checks.semantic.SemanticCheck; |
| 17 | +import it.unive.lisa.program.cfg.CFG; |
| 18 | +import it.unive.lisa.program.cfg.statement.Statement; |
| 19 | +import java.util.HashSet; |
| 20 | +import java.util.Set; |
| 21 | +import org.apache.logging.log4j.LogManager; |
| 22 | +import org.apache.logging.log4j.Logger; |
| 23 | + |
| 24 | +/** |
| 25 | + * Semantic checker that detects missing access control guards by combining a |
| 26 | + * taint analysis with control-flow reachability information. The checker looks |
| 27 | + * for sensitive sink instructions that can be reached from untrusted sources |
| 28 | + * without passing through any guarded conditional jumps. |
| 29 | + */ |
| 30 | +public class AccessControlIncompletenessChecker implements |
| 31 | + SemanticCheck<SimpleAbstractState<MonolithicHeap, TaintAbstractDomain, TypeEnvironment<InferredTypes>>> { |
| 32 | + |
| 33 | + private static final Logger log = LogManager.getLogger(AccessControlIncompletenessChecker.class); |
| 34 | + |
| 35 | + private Set<Statement> taintedJumpi = new HashSet<>(); |
| 36 | + private SmartContract contract; |
| 37 | + |
| 38 | + /** |
| 39 | + * Builds the checker for the given contract. |
| 40 | + * |
| 41 | + * @param contract the contract under analysis |
| 42 | + */ |
| 43 | + public AccessControlIncompletenessChecker(SmartContract contract) { |
| 44 | + this.contract = contract; |
| 45 | + } |
| 46 | + |
| 47 | + @Override |
| 48 | + public boolean visit( |
| 49 | + CheckToolWithAnalysisResults< |
| 50 | + SimpleAbstractState<MonolithicHeap, TaintAbstractDomain, TypeEnvironment<InferredTypes>>> tool, |
| 51 | + CFG graph) { |
| 52 | + |
| 53 | + EVMCFG cfg = ((EVMCFG) graph); |
| 54 | + |
| 55 | + for (Statement node : cfg.getNodes()) |
| 56 | + if (node instanceof Jumpi) |
| 57 | + for (AnalyzedCFG<SimpleAbstractState<MonolithicHeap, TaintAbstractDomain, |
| 58 | + TypeEnvironment<InferredTypes>>> result : tool.getResultOf(cfg)) { |
| 59 | + |
| 60 | + AnalysisState<SimpleAbstractState<MonolithicHeap, TaintAbstractDomain, |
| 61 | + TypeEnvironment<InferredTypes>>> analysisResult = null; |
| 62 | + |
| 63 | + try { |
| 64 | + analysisResult = result.getAnalysisStateBefore(node); |
| 65 | + } catch (SemanticException e1) { |
| 66 | + log.error("(AccessControlIncompletenessChecker): {}", e1.getMessage()); |
| 67 | + } |
| 68 | + |
| 69 | + TaintAbstractDomain taintedStack = analysisResult.getState().getValueState(); |
| 70 | + |
| 71 | + if (taintedStack.isBottom() || taintedStack.isTop()) |
| 72 | + continue; |
| 73 | + |
| 74 | + if (TaintElement.isAtLeastOneTainted( |
| 75 | + taintedStack.getElementAtPosition(1), |
| 76 | + taintedStack.getElementAtPosition(2))) |
| 77 | + taintedJumpi.add(node); |
| 78 | + } |
| 79 | + return true; |
| 80 | + } |
| 81 | + |
| 82 | + @Override |
| 83 | + public boolean visit( |
| 84 | + CheckToolWithAnalysisResults< |
| 85 | + SimpleAbstractState<MonolithicHeap, TaintAbstractDomain, TypeEnvironment<InferredTypes>>> tool, |
| 86 | + CFG graph, Statement node) { |
| 87 | + |
| 88 | + if (node instanceof Call |
| 89 | + || node instanceof Callcode |
| 90 | + || node instanceof Staticcall |
| 91 | + || node instanceof Delegatecall |
| 92 | + || node instanceof Sstore |
| 93 | + || node instanceof Balance |
| 94 | + || node instanceof Address) { |
| 95 | + EVMCFG cfg = ((EVMCFG) graph); |
| 96 | + |
| 97 | + for (AnalyzedCFG<SimpleAbstractState<MonolithicHeap, TaintAbstractDomain, |
| 98 | + TypeEnvironment<InferredTypes>>> result : tool.getResultOf(cfg)) { |
| 99 | + AnalysisState<SimpleAbstractState<MonolithicHeap, TaintAbstractDomain, |
| 100 | + TypeEnvironment<InferredTypes>>> analysisResult = null; |
| 101 | + |
| 102 | + try { |
| 103 | + analysisResult = result.getAnalysisStateBefore(node); |
| 104 | + } catch (SemanticException e1) { |
| 105 | + log.error("(AccessControlIncompletenessChecker): {}", e1.getMessage()); |
| 106 | + } |
| 107 | + |
| 108 | + // Retrieve the symbolic stack from the analysis result |
| 109 | + TaintAbstractDomain taintedStack = analysisResult.getState().getValueState(); |
| 110 | + |
| 111 | + if (taintedStack.isBottom() || taintedStack.isTop()) |
| 112 | + // Nothing to do |
| 113 | + continue; |
| 114 | + |
| 115 | + int numArgs = getNumberOfArgs(node); |
| 116 | + boolean isAtLeastOneTainted = false; |
| 117 | + |
| 118 | + for (int argIndex = 1; argIndex <= numArgs; argIndex++) |
| 119 | + isAtLeastOneTainted |= TaintElement.isAtLeastOneTainted( |
| 120 | + taintedStack.getElementAtPosition(argIndex)); |
| 121 | + |
| 122 | + if (isAtLeastOneTainted) |
| 123 | + checkForAccessControlIncompleteness(tool, cfg, node); |
| 124 | + } |
| 125 | + } |
| 126 | + return true; |
| 127 | + } |
| 128 | + |
| 129 | + /** |
| 130 | + * Computes the number of arguments consumed from the stack by the provided |
| 131 | + * EVM instruction. |
| 132 | + * |
| 133 | + * @param node the statement to inspect |
| 134 | + * |
| 135 | + * @return the amount of stack elements consumed by {@code node} |
| 136 | + */ |
| 137 | + private int getNumberOfArgs(Statement node) { |
| 138 | + if (node instanceof Call || node instanceof Callcode) |
| 139 | + return 7; |
| 140 | + if (node instanceof Staticcall || node instanceof Delegatecall) |
| 141 | + return 6; |
| 142 | + if (node instanceof Sstore) |
| 143 | + return 2; |
| 144 | + if (node instanceof Balance) |
| 145 | + return 1; |
| 146 | + return 0; |
| 147 | + } |
| 148 | + |
| 149 | + /** |
| 150 | + * Verifies whether a tainted source can reach an unguarded sink without |
| 151 | + * being protected by a conditional jump and, if so, records the |
| 152 | + * vulnerability. |
| 153 | + * |
| 154 | + * @param tool the analysis tool to report warnings on |
| 155 | + * @param cfg the CFG under inspection |
| 156 | + * @param sink the sink statement that manipulates sensitive state |
| 157 | + */ |
| 158 | + private void checkForAccessControlIncompleteness(CheckToolWithAnalysisResults< |
| 159 | + SimpleAbstractState<MonolithicHeap, TaintAbstractDomain, TypeEnvironment<InferredTypes>>> tool, EVMCFG cfg, |
| 160 | + Statement sink) { |
| 161 | + |
| 162 | + Set<Statement> sources = new HashSet<>(); |
| 163 | + sources.addAll(cfg.getAllCalldataload()); |
| 164 | + sources.addAll(cfg.getAllCalldatacopy()); |
| 165 | + sources.addAll(cfg.getAllCalldatacopy()); |
| 166 | + sources.addAll(cfg.getAllCaller()); |
| 167 | + sources.addAll(cfg.getAllOrigin()); |
| 168 | + sources.addAll(cfg.getAllCallvalue()); |
| 169 | + |
| 170 | + for (Statement source : sources) { |
| 171 | + if (cfg.reachableFromWithoutStatements(source, sink, taintedJumpi)) { |
| 172 | + String functionSignatureByStatement = contract.getFunctionSignatureByStatement(source); |
| 173 | + |
| 174 | + // It means that this vulnerability is inside a private function |
| 175 | + if (functionSignatureByStatement.equals("no-function-found")) |
| 176 | + continue; |
| 177 | + |
| 178 | + ProgramCounterLocation sinkLocation = (ProgramCounterLocation) sink.getLocation(); |
| 179 | + |
| 180 | + log.warn( |
| 181 | + "[DEFINITE] Access Control Incompleteness vulnerability at pc {} (line {}) coming from pc {} (line {}).", |
| 182 | + sinkLocation.getPc(), |
| 183 | + sinkLocation.getSourceCodeLine(), |
| 184 | + ((ProgramCounterLocation) sink.getLocation()).getPc(), |
| 185 | + ((ProgramCounterLocation) sink.getLocation()).getSourceCodeLine()); |
| 186 | + |
| 187 | + String warn = "[DEFINITE] Access Control Incompleteness vulnerability at " |
| 188 | + + ((ProgramCounterLocation) sink.getLocation()).getSourceCodeLine(); |
| 189 | + tool.warn(warn); |
| 190 | + MyCache.getInstance().addUncheckedExternalCallWarning(cfg.hashCode(), warn); |
| 191 | + |
| 192 | + warn = "[DEFINITE] Access Control Incompleteness vulnerability in " + contract.getName() + " at " |
| 193 | + + functionSignatureByStatement |
| 194 | + + " (pc: " + ((ProgramCounterLocation) sink.getLocation()).getPc() + ", " |
| 195 | + + "line: " + ((ProgramCounterLocation) sink.getLocation()).getSourceCodeLine() + ")"; |
| 196 | + MyCache.getInstance().addVulnerabilityPerFunction(cfg.hashCode(), warn); |
| 197 | + } |
| 198 | + } |
| 199 | + } |
| 200 | + |
| 201 | +} |
0 commit comments