{"id":1216,"cve":"CVE-2024-32649","cve_url":"https:\/\/www.cvedetails.com\/cve\/CVE-2024-32649\/","cwe":"CWE-95 Improper Neutralization of Directives in Dynamically Evaluated Code ('Eval Injection')","cwe_number":95,"cwe_url":"https:\/\/cwe.mitre.org\/data\/definitions\/95","cvss_score":5.3,"published":"2024-04-25","owasp":"A09 Security Logging and Monitoring Failures","odc":"Algorithm","cdc":"Incorrect Algorithm","category":"Numeric Errors","subcategory":"Arithmetic Errors","scope":"Remote","impact":"Unauthorized Access","summary":"Vyper's `sqrt` builtin may cause double evaluation of arguments with side-effects.","permalink":"https:\/\/netpack.pt\/vaitp\/vulnerability\/CVE-2024-32649\/","description":"Vyper is a pythonic Smart Contract Language for the Ethereum virtual machine. In versions 0.3.10 and prior, using the `sqrt` builtin can result in double eval vulnerability when the argument has side-effects. It can be seen that the `build_IR` function of the `sqrt` builtin doesn't cache the argument to the stack. As such, it can be evaluated multiple times (instead of retrieving the value from the stack). No vulnerable production contracts were found. Additionally, double evaluation of side-effects should be easily discoverable in client tests. As such, the impact is low. As of time of publication, no fixed versions are available. ","solution":"Upgrade to Vyper version 0.3.11 or later when available.","affected_component":"Vyper","fixed_by_upgrading":true,"vulnerable_code":"# Example of a vulnerable Vyper smart contract\n# This contract demonstrates the double evaluation vulnerability with sqrt\n\n# @version ^0.3.10\ncontract VulnerableContract:\nnum: public(uint256)\n\n@public\n@pure\ndef calculate_sqrt_with_side_effects() -> uint256:\n    self.num += 1  # Side effect\n    return sqrt(self.num)  # Vulnerable to double evaluation","patched_code":"# Example of a vulnerable Vyper smart contract\n# This contract demonstrates the double evaluation vulnerability with sqrt\n\n# @version ^0.3.10\ncontract VulnerableContract:\nnum: public(uint256)\n\n@public\n@pure\ndef calculate_sqrt_with_side_effects() -> uint256:\n    self.num += 1  # Side effect\n    return sqrt(self.num)  # Vulnerable to double evaluation\n\n# Example of the fixed version\n# @version ^0.3.11\n    contract FixedContract:\n    num: public(uint256)\n\n    @public\n    @pure\n    def calculate_sqrt_with_side_effects() -> uint256:\n        temp_num: uint256 = self.num + 1  # Cache the result of the side effect\n        return sqrt(temp_num)  # No double evaluation","payload_needed":true,"payload":"```python\n# Example payload to exploit the vulnerability\n# This payload calls the vulnerable contract method that uses sqrt with side effects\n\ncontract = VulnerableContract()\ncontract.calculate_sqrt_with_side_effects()  # This would cause self.num to be incremented twice\n```","payload_status":"available","published_fix":[{"repository":"vyperlang\/vyper","commit_url":"https:\/\/github.com\/vyperlang\/vyper\/pull\/2914","commit_subject":"optimizer fixes and guardrails","units":[{"file":"vyper\/builtin_functions\/functions.py","function":null},{"file":"vyper\/builtin_functions\/functions.py","function":"SelfDestruct.build_IR"},{"file":"vyper\/builtin_functions\/functions.py","function":"_create_ir"},{"file":"vyper\/codegen\/core.py","function":null},{"file":"vyper\/codegen\/core.py","function":"STORE"},{"file":"vyper\/codegen\/external_call.py","function":null},{"file":"vyper\/codegen\/external_call.py","function":"_external_call_helper"},{"file":"vyper\/codegen\/ir_node.py","function":null},{"file":"vyper\/codegen\/ir_node.py","function":"IRnode.__init__"},{"file":"vyper\/codegen\/self_call.py","function":null},{"file":"vyper\/codegen\/self_call.py","function":"ir_for_self_call"},{"file":"vyper\/compiler\/output.py","function":"_build_asm"},{"file":"vyper\/compiler\/__init__.py","function":null},{"file":"vyper\/compiler\/__init__.py","function":"compile_codes"},{"file":"vyper\/ir\/compile_ir.py","function":null},{"file":"vyper\/ir\/compile_ir.py","function":"_compile_to_assembly"},{"file":"vyper\/ir\/compile_ir.py","function":"_merge_iszero"},{"file":"vyper\/ir\/optimizer.py","function":null},{"file":"vyper\/ir\/optimizer.py","function":"optimize"},{"file":"vyper\/ir\/optimizer.py","function":"_flip_comparison_op"},{"file":"vyper\/ir\/optimizer.py","function":"_merge_calldataload"},{"file":"vyper\/ir\/optimizer.py","function":"_merge_memzero"}]}],"references":null}