CVE-2025-27104
Executive Summary
CVE-2025-27104 is a critical vulnerability in the Vyper smart contract language (Pythonic language for the EVM) that allows multiple evaluations of a single expression in the iterator target of a for loop. This flaw can lead to unexpected program behavior, including the interleaving of reads and writes within loop bodies, potentially enabling attackers to manipulate contract state or extract sensitive information. The vulnerability affects all versions prior to 0.4.1, and no official patch or workaround is currently available. The EPSS score is 0.0%, indicating low exploitation activity at this time, but the risk remains significant for deployed contracts.
This record — and 170,000+ others — are available via the ValtersIT API. One endpoint replaces five: NVD + GitHub CVE + CISA KEV + EPSS + Nuclei + Exploit-DB, unified.
No signup required
Supply Chain & Package Security
Vyper is distributed as a Python package via PyPI. Developers using Vyper should monitor for the patched release (0.4.1) and update their development environments immediately. The vulnerability is in the compiler itself, meaning any contract compiled with an affected version is potentially vulnerable. Supply chain risks include:
- Compromised Compiler: An attacker could backdoor the Vyper compiler to inject malicious code into compiled contracts.
- Dependency Confusion: Malicious packages mimicking Vyper could be uploaded to PyPI.
- Version Pinning: Teams pinning Vyper versions without monitoring for security updates are at risk.
CVSS Breakdown & Threat Level
CVSS Score: 0.0 (Unavailable) CVSS Vector: UNKNOWN
While no official CVSS score is provided, the vulnerability is assessed as High based on:
- Attack Vector: Network (via malicious contract deployment or interaction)
- Attack Complexity: Low (requires understanding of Vyper loop semantics)
- Privileges Required: None (anyone can deploy a contract)
- User Interaction: None
- Scope: Unchanged
- Confidentiality Impact: High (potential data leakage)
- Integrity Impact: High (state manipulation)
- Availability Impact: Low (no direct DoS, but logic errors can cause contract failure)
Threat Level: High – Exploitation could lead to loss of funds or contract compromise.
Deep Dive & Mechanics
The vulnerability lies in the Vyper compiler's handling of for loop iterators. In Vyper, for loops can iterate over range() or iterable types like static arrays (SArray) and dynamic arrays (DArray). The compiler attempts to enforce that iterator expressions are side-effect-free by checking is_constant during code generation. However, this check is insufficient.
Root Cause: For SArray iterators, the iter_list is instantiated inside the body of a repeat IR (intermediate representation) node. This means the iterator expression can be evaluated multiple times during loop execution, even though it appears only once in the source code. This allows the iterator to consume side effects produced by the loop body, such as reading a storage variable that is updated within the loop.
Example Scenario:
# Vulnerable contract
storage_var: uint256
@external
def vulnerable_loop() -> uint256:
for s: uint256 in ([self.storage_var, self.storage_var] if True else []):
self.storage_var = s + 1
return self.storage_var
In this example, the iterator [self.storage_var, self.storage_var] is evaluated multiple times. The first iteration reads storage_var, then the loop body updates it. The second iteration reads the updated value, leading to unexpected behavior.
Impact:
- State Manipulation: Attackers can craft loops that read and write storage variables in unintended orders.
- Data Leakage: Sensitive data stored in contract state could be exposed through repeated reads.
- Logic Errors: Contracts relying on deterministic loop behavior may fail.
Vulnerable vs. Patched Code
Vulnerable Code (Pre-0.4.1):
# Example of vulnerable iterator
for s: uint256 in ([read(), read()] if True else []):
# loop body with side effects
write(s)
Patched Code (Expected in 0.4.1): The patch will likely enforce that iterator expressions are evaluated only once, either by:
- Caching the iterator result before the loop begins.
- Moving iterator evaluation outside the
repeatIR node. - Adding stricter compile-time checks to prevent side-effect consumption.
Hypothetical Patched Example:
# Patched behavior: iterator evaluated once
let iter = [read(), read()] if True else []
for s: uint256 in iter:
write(s)
Exploitation & Lab Setup
Red Team (Attacker)
Objective: Exploit the vulnerability to manipulate contract state or extract data.
Lab Setup:
- Install vulnerable Vyper version (e.g., 0.3.10):
pip install vyper==0.3.10 - Create a vulnerable contract:
# vulnerable.vy @external def exploit() -> uint256: secret: uint256 = 42 for s: uint256 in ([secret, secret] if True else []): secret = s + 1 return secret - Compile and deploy to a testnet (e.g., Ganache):
vyper vulnerable.vy > vulnerable_bytecode.txt - Interact with the contract to observe unexpected behavior.
Exploit Code (Conceptual):
from web3 import Web3
w3 = Web3(Web3.HTTPProvider('http://localhost:8545'))
contract = w3.eth.contract(address='0x...', abi=abi)
result = contract.functions.exploit().call()
print(f"Unexpected result: {result}") # Should be 43, but may be different
Blue Team (Defender)
Detection:
- Monitor for contracts using
forloops with complex iterator expressions (e.g., containingifexpor function calls). - Analyze bytecode for patterns indicating multiple iterator evaluations.
- Use static analysis tools to flag suspicious loop constructs.
Mitigation:
- Upgrade to Vyper 0.4.1 once released.
- Avoid using side-effect-producing expressions in loop iterators.
- Implement runtime checks to detect unexpected state changes.
Indicators of Compromise (IOCs)
- Contract Bytecode: Presence of
repeatIR nodes with iterator expressions that include storage reads or function calls. - Transaction Patterns: Repeated calls to a contract that produce non-deterministic loop behavior.
- Logs: Unexpected state changes in storage variables during loop execution.
- Source Code: Vyper contracts containing
forloops with iterators like[func(), func()]or[storage_var, storage_var].
Detection Engineering
YARA Rule
rule Detect_Vyper_For_Loop_Vulnerability {
meta:
description = "Detects Vyper contracts with vulnerable for loop iterators"
author = "Valters IT"
date = "2025-03-01"
cve = "CVE-2025-27104"
strings:
$for_loop = "for" ascii
$iterator_pattern = /\[.*,.*\]\s+if\s+/ ascii
$storage_read = /self\.\w+/ ascii
condition:
$for_loop and $iterator_pattern and $storage_read
}
Sigma Rule
title: Vyper For Loop Iterator Vulnerability
id: 7a8b9c0d-1e2f-3a4b-5c6d-7e8f9a0b1c2d
status: experimental
description: Detects Vyper contracts with vulnerable for loop iterators
author: Valters IT
date: 2025-03-01
logsource:
category: application
product: vyper
detection:
selection:
SourceCode|contains:
- 'for '
- 'if True'
- 'self.'
condition: selection
falsepositives:
- Legitimate use of conditional iterators
level: high
Suricata Rule
alert tcp any any -> any any (msg:"CVE-2025-27104 Vyper For Loop Iterator Vulnerability"; flow:to_server; content:"for"; content:"if True"; content:"self."; classtype:attempted-admin; sid:1000001; rev:1;)
Remediation & Mitigation
Immediate Actions:
- Upgrade Vyper: Update to version 0.4.1 as soon as it is released.
- Audit Contracts: Review all deployed contracts for vulnerable
forloop patterns. - Static Analysis: Use tools like Slither or Mythril to detect suspicious iterator expressions.
Long-Term Mitigation:
- Compiler Hardening: Ensure iterator expressions are evaluated only once at compile time.
- Runtime Guards: Add checks to prevent side-effect consumption in loops.
- Developer Training: Educate Vyper developers about safe loop patterns.
Workarounds:
- No known workarounds exist. Avoid using complex iterators in
forloops until patched.
Valters IT
Securing the Future of Smart Contracts