Invariant Testing Engagement · 2024
Corn
Corn is a DeFi protocol built around a vault system for depositing, staking, and distributing rewards. Recon's invariant testing identified a critical insolvency vulnerability within hours of starting the campaign — the vault's accounting could enter a state where it owed more to depositors than it actually held. Continued testing after the fix validated the correction and ensured subsequent development changes did not reintroduce the vulnerability. The rapid turnaround from campaign start to critical finding demonstrated the effectiveness of targeted invariant testing.
The Challenge
The protocol's core complexity lay in the interaction between three operations: depositing assets into the vault, staking those deposits for yield, and distributing rewards across depositors. The share pricing mechanism needed to correctly account for rewards in the conversion between assets and shares. Any miscalculation in this conversion could allow depositors to extract more value than the vault contains, leading to insolvency for remaining depositors. The combination of these operations in arbitrary order created a state space too large for manual analysis.
Our Approach
Solvency invariants were defined requiring that the vault's total assets must always be greater than or equal to the value owed to all shareholders. The fuzzer generated randomized sequences of deposits with varying amounts, withdrawals of partial and full positions, and reward distributions at different intervals. Handler functions simulated multiple users interacting simultaneously to test share pricing under concurrent access. Properties were checked after every operation to catch the earliest point at which accounting diverged.
Findings
Insolvency due to Incorrect Accounting
Invariant testing revealed a critical accounting error in the share minting logic. When rewards had recently been distributed, the asset-to-share conversion rounded in favor of new depositors, granting them slightly more shares than their deposit warranted. Repeated deposit/withdraw cycles around reward distribution events would progressively drain the vault, causing the protocol's total liabilities to exceed its actual assets.
Results
The critical insolvency bug was identified in the share minting logic — new deposits received slightly more shares than they deserved when rewards had recently been distributed, because the conversion rounded in favor of the depositor rather than the protocol. Over time, this would drain the vault. The fix reversed the rounding direction, and continued fuzzing confirmed the fix held across millions of additional test sequences. The Corn cofounder praised Recon's work in making invariant testing accessible and practical for their development workflow.
“The recon team is continuously innovating to make invariant testing vastly more accessible to projects, which makes a meaningful difference in security outcomes. They bring their broad expertise in and passion for web3 security to any engagement above and beyond the specified scope.”
Dapp, Cofounder at Corn
Get the same level of protection
See how Recon's invariant testing can secure your protocol like it did for Corn.