|
| 1 | +from typing import List |
| 2 | + |
| 3 | +from slither.detectors.abstract_detector import ( |
| 4 | + AbstractDetector, |
| 5 | + DetectorClassification, |
| 6 | + DETECTOR_INFO, |
| 7 | +) |
| 8 | +from slither.utils.output import Output |
| 9 | +from slither.slithir.operations import Binary, Assignment, Unpack, SolidityCall |
| 10 | +from slither.core.variables import Variable |
| 11 | +from slither.core.declarations.solidity_variables import SolidityFunction |
| 12 | +from slither.core.cfg.node import Node |
| 13 | + |
| 14 | + |
| 15 | +class ChronicleUncheckedPrice(AbstractDetector): |
| 16 | + """ |
| 17 | + Documentation: This detector finds calls to Chronicle oracle where the returned price is not checked |
| 18 | + https://docs.chroniclelabs.org/Resources/FAQ/Oracles#how-do-i-check-if-an-oracle-becomes-inactive-gets-deprecated |
| 19 | + """ |
| 20 | + |
| 21 | + ARGUMENT = "chronicle-unchecked-price" |
| 22 | + HELP = "Detect when Chronicle price is not checked." |
| 23 | + IMPACT = DetectorClassification.MEDIUM |
| 24 | + CONFIDENCE = DetectorClassification.MEDIUM |
| 25 | + |
| 26 | + WIKI = "https://github.com/crytic/slither/wiki/Detector-Documentation#chronicle-unchecked-price" |
| 27 | + |
| 28 | + WIKI_TITLE = "Chronicle unchecked price" |
| 29 | + WIKI_DESCRIPTION = "Chronicle oracle is used and the price returned is not checked to be valid. For more information https://docs.chroniclelabs.org/Resources/FAQ/Oracles#how-do-i-check-if-an-oracle-becomes-inactive-gets-deprecated." |
| 30 | + |
| 31 | + # region wiki_exploit_scenario |
| 32 | + WIKI_EXPLOIT_SCENARIO = """ |
| 33 | +```solidity |
| 34 | +contract C { |
| 35 | + IChronicle chronicle; |
| 36 | +
|
| 37 | + constructor(address a) { |
| 38 | + chronicle = IChronicle(a); |
| 39 | + } |
| 40 | +
|
| 41 | + function bad() public { |
| 42 | + uint256 price = chronicle.read(); |
| 43 | + } |
| 44 | +``` |
| 45 | +The `bad` function gets the price from Chronicle by calling the read function however it does not check if the price is valid.""" |
| 46 | + # endregion wiki_exploit_scenario |
| 47 | + |
| 48 | + WIKI_RECOMMENDATION = "Validate that the price returned by the oracle is valid." |
| 49 | + |
| 50 | + def _var_is_checked(self, nodes: List[Node], var_to_check: Variable) -> bool: |
| 51 | + visited = set() |
| 52 | + checked = False |
| 53 | + |
| 54 | + while nodes: |
| 55 | + if checked: |
| 56 | + break |
| 57 | + next_node = nodes[0] |
| 58 | + nodes = nodes[1:] |
| 59 | + |
| 60 | + for node_ir in next_node.all_slithir_operations(): |
| 61 | + if isinstance(node_ir, Binary) and var_to_check in node_ir.read: |
| 62 | + checked = True |
| 63 | + break |
| 64 | + # This case is for tryRead and tryReadWithAge |
| 65 | + # if the isValid boolean is checked inside a require(isValid) |
| 66 | + if ( |
| 67 | + isinstance(node_ir, SolidityCall) |
| 68 | + and node_ir.function |
| 69 | + in ( |
| 70 | + SolidityFunction("require(bool)"), |
| 71 | + SolidityFunction("require(bool,string)"), |
| 72 | + SolidityFunction("require(bool,error)"), |
| 73 | + ) |
| 74 | + and var_to_check in node_ir.read |
| 75 | + ): |
| 76 | + checked = True |
| 77 | + break |
| 78 | + |
| 79 | + if next_node not in visited: |
| 80 | + visited.add(next_node) |
| 81 | + for son in next_node.sons: |
| 82 | + if son not in visited: |
| 83 | + nodes.append(son) |
| 84 | + return checked |
| 85 | + |
| 86 | + # pylint: disable=too-many-nested-blocks,too-many-branches |
| 87 | + def _detect(self) -> List[Output]: |
| 88 | + results: List[Output] = [] |
| 89 | + |
| 90 | + for contract in self.compilation_unit.contracts_derived: |
| 91 | + for target_contract, ir in sorted( |
| 92 | + contract.all_high_level_calls, |
| 93 | + key=lambda x: (x[1].node.node_id, x[1].node.function.full_name), |
| 94 | + ): |
| 95 | + if target_contract.name in ("IScribe", "IChronicle") and ir.function_name in ( |
| 96 | + "read", |
| 97 | + "tryRead", |
| 98 | + "readWithAge", |
| 99 | + "tryReadWithAge", |
| 100 | + "latestAnswer", |
| 101 | + "latestRoundData", |
| 102 | + ): |
| 103 | + found = False |
| 104 | + if ir.function_name in ("read", "latestAnswer"): |
| 105 | + # We need to iterate the IRs as we are not always sure that the following IR is the assignment |
| 106 | + # for example in case of type conversion it isn't |
| 107 | + for node_ir in ir.node.irs: |
| 108 | + if isinstance(node_ir, Assignment): |
| 109 | + possible_unchecked_variable_ir = node_ir.lvalue |
| 110 | + found = True |
| 111 | + break |
| 112 | + elif ir.function_name in ("readWithAge", "tryRead", "tryReadWithAge"): |
| 113 | + # We are interested in the first item of the tuple |
| 114 | + # readWithAge : value |
| 115 | + # tryRead/tryReadWithAge : isValid |
| 116 | + for node_ir in ir.node.irs: |
| 117 | + if isinstance(node_ir, Unpack) and node_ir.index == 0: |
| 118 | + possible_unchecked_variable_ir = node_ir.lvalue |
| 119 | + found = True |
| 120 | + break |
| 121 | + elif ir.function_name == "latestRoundData": |
| 122 | + found = False |
| 123 | + for node_ir in ir.node.irs: |
| 124 | + if isinstance(node_ir, Unpack) and node_ir.index == 1: |
| 125 | + possible_unchecked_variable_ir = node_ir.lvalue |
| 126 | + found = True |
| 127 | + break |
| 128 | + |
| 129 | + # If we did not find the variable assignment we know it's not checked |
| 130 | + checked = ( |
| 131 | + self._var_is_checked(ir.node.sons, possible_unchecked_variable_ir) |
| 132 | + if found |
| 133 | + else False |
| 134 | + ) |
| 135 | + |
| 136 | + if not checked: |
| 137 | + info: DETECTOR_INFO = [ |
| 138 | + "Chronicle price is not checked to be valid in ", |
| 139 | + ir.node.function, |
| 140 | + "\n\t- ", |
| 141 | + ir.node, |
| 142 | + "\n", |
| 143 | + ] |
| 144 | + res = self.generate_result(info) |
| 145 | + results.append(res) |
| 146 | + |
| 147 | + return results |
0 commit comments