|
| 1 | +"""This module contains the detection code for transaction order dependence.""" |
| 2 | + |
| 3 | +from mythril.analysis import solver |
| 4 | +from mythril.analysis.potential_issues import ( |
| 5 | + PotentialIssue, |
| 6 | + get_potential_issues_annotation, |
| 7 | +) |
| 8 | +from mythril.analysis.swc_data import TX_ORDER_DEPENDENCE |
| 9 | +from mythril.laser.ethereum.transaction.symbolic import ACTORS |
| 10 | +from mythril.analysis.module.base import DetectionModule |
| 11 | +from mythril.laser.smt import Or, Bool |
| 12 | +from mythril.laser.ethereum.state.global_state import GlobalState |
| 13 | +from mythril.exceptions import UnsatError |
| 14 | +import logging |
| 15 | + |
| 16 | +log = logging.getLogger(__name__) |
| 17 | + |
| 18 | +DESCRIPTION = """ |
| 19 | +
|
| 20 | +Search for calls whose value depends on balance or storage. |
| 21 | +
|
| 22 | +""" |
| 23 | + |
| 24 | + |
| 25 | +class BalanceAnnotation: |
| 26 | + def __init__(self, caller): |
| 27 | + self.caller = caller |
| 28 | + |
| 29 | + |
| 30 | +class StorageAnnotation: |
| 31 | + def __init__(self, caller): |
| 32 | + self.caller = caller |
| 33 | + |
| 34 | + |
| 35 | +class TransactionOrderDependence(DetectionModule): |
| 36 | + """This module detects transaction order dependence.""" |
| 37 | + |
| 38 | + name = "Transaction Order Dependence" |
| 39 | + swc_id = TX_ORDER_DEPENDENCE |
| 40 | + description = DESCRIPTION |
| 41 | + entrypoint = "callback" |
| 42 | + pre_hooks = ["CALL"] |
| 43 | + post_hooks = ["BALANCE", "SLOAD"] |
| 44 | + plugin_default_enabled = True |
| 45 | + |
| 46 | + def _execute(self, state: GlobalState) -> None: |
| 47 | + """ |
| 48 | +
|
| 49 | + :param state: |
| 50 | + :return: |
| 51 | + """ |
| 52 | + potential_issues = self._analyze_state(state) |
| 53 | + |
| 54 | + annotation = get_potential_issues_annotation(state) |
| 55 | + annotation.potential_issues.extend(potential_issues) |
| 56 | + |
| 57 | + @staticmethod |
| 58 | + def annotate_balance_storage_vals(state, opcode): |
| 59 | + val = state.mstate.stack[-1] |
| 60 | + if opcode == "BALANCE": |
| 61 | + annotation = BalanceAnnotation |
| 62 | + else: |
| 63 | + annotation = StorageAnnotation |
| 64 | + if len(val.get_annotations(annotation)) == 0: |
| 65 | + state.mstate.stack[-1].annotate(annotation(state.environment.sender)) |
| 66 | + return [] |
| 67 | + |
| 68 | + def _analyze_state(self, state: GlobalState): |
| 69 | + """ |
| 70 | +
|
| 71 | + :param state: |
| 72 | + :return: |
| 73 | + """ |
| 74 | + opcode = state.get_current_instruction()["opcode"] |
| 75 | + if opcode != "CALL": |
| 76 | + opcode = state.environment.code.instruction_list[state.mstate.pc - 1][ |
| 77 | + "opcode" |
| 78 | + ] |
| 79 | + if opcode in ("BALANCE", "SLOAD"): |
| 80 | + self.annotate_balance_storage_vals(state, opcode) |
| 81 | + return [] |
| 82 | + |
| 83 | + value = state.mstate.stack[-3] |
| 84 | + if ( |
| 85 | + len(value.get_annotations(StorageAnnotation)) |
| 86 | + + len(value.get_annotations(BalanceAnnotation)) |
| 87 | + == 0 |
| 88 | + ): |
| 89 | + return [] |
| 90 | + callers = [] |
| 91 | + |
| 92 | + storage_annotations = value.get_annotations(StorageAnnotation) |
| 93 | + if len(storage_annotations) == 1: |
| 94 | + callers.append(storage_annotations[0].caller) |
| 95 | + balance_annotations = value.get_annotations(BalanceAnnotation) |
| 96 | + if len(balance_annotations) == 1: |
| 97 | + callers.append(balance_annotations[0].caller) |
| 98 | + |
| 99 | + address = state.get_current_instruction()["address"] |
| 100 | + call_constraint = Bool(False) |
| 101 | + for caller in callers: |
| 102 | + call_constraint = Or(call_constraint, ACTORS.attacker == caller) |
| 103 | + |
| 104 | + try: |
| 105 | + constraints = state.world_state.constraints + [call_constraint] |
| 106 | + |
| 107 | + solver.get_transaction_sequence(state, constraints) |
| 108 | + |
| 109 | + description_head = ( |
| 110 | + "The value of the call is dependent on balance or storage write" |
| 111 | + ) |
| 112 | + description_tail = ( |
| 113 | + "This can lead to race conditions. An attacker may be able to run a transaction after our transaction " |
| 114 | + "which can change the value of the call" |
| 115 | + ) |
| 116 | + |
| 117 | + issue = PotentialIssue( |
| 118 | + contract=state.environment.active_account.contract_name, |
| 119 | + function_name=state.environment.active_function_name, |
| 120 | + address=address, |
| 121 | + swc_id=TX_ORDER_DEPENDENCE, |
| 122 | + title="Transaction Order Dependence", |
| 123 | + bytecode=state.environment.code.bytecode, |
| 124 | + severity="Medium", |
| 125 | + description_head=description_head, |
| 126 | + description_tail=description_tail, |
| 127 | + constraints=constraints, |
| 128 | + detector=self, |
| 129 | + ) |
| 130 | + |
| 131 | + except UnsatError: |
| 132 | + log.debug("[Transaction Order Dependence] No model found.") |
| 133 | + return [] |
| 134 | + |
| 135 | + return [issue] |
| 136 | + |
| 137 | + |
| 138 | +detector = TransactionOrderDependence() |
0 commit comments