Rounding Errors: Minor but Major Hacks

Extropy.IO
5 min readMar 17, 2024

Rounding errors, once minor quirks in the realm of software development, have ascended to prominent vulnerabilities within the Decentralized Finance (DeFi) ecosystem. Traditionally manageable within fields like scientific computing, these computational discrepancies have taken on a new gravity in the precision-driven world of blockchain technology. The inherent limitations of floating-point arithmetic, which approximates real numbers, collide with the blockchain’s demands for absolute precision, turning rounding errors into significant threats.

This blog looks into the emergence of rounding errors as a key exploitation vector in DeFi — now the second most exploited vulnerability, trailing only behind stolen private keys. Highlighting recent sophisticated attacks, we explore the nature of these vulnerabilities, their impact on DeFi, and the urgent need for robust mitigation strategies to enhance the ecosystem’s resilience against digital threats.

The Impact of Rounding Errors in DeFi

The unique challenges of DeFi stem significantly from Solidity’s lack of native support for floating-point arithmetic, leading to reliance on integer division. This limitation is critical because smart contracts, the backbone of DeFi services, demand precise execution to maintain the blockchain’s integrity — immutability, transparency, and security. Even slight miscalculations from rounding errors can result in significant financial discrepancies. Once an exploit occurs, the immutable nature of blockchain transactions means it cannot be easily rectified, often leading to irreversible financial loss.

The Hope Lending Hack: A Prelude to a Trend

The Hope Lending hack in October 2023, which led to an $851k loss, underscored the significant threat posed by rounding errors within the blockchain community. This incident was far from an isolated event; it heralded a worrying series of sophisticated attacks targeting similar vulnerabilities across various DeFi platforms. Subsequent breaches, affecting entities like Channels, Radiant, Wise Lending, and Abracadabra, have collectively resulted in millions of dollars in losses. These incidents have highlighted a concerning trend: attackers are exploiting the lack of mathematical precision in smart contracts to orchestrate large-scale thefts.

In the case of Hope Lending, the attacker utilized a flash loan to manipulate the lending pool of Hope.money, a comprehensive suite of decentralized financial services encompassing lending, swapping, and governance functionalities. The crux of the exploit involved a vulnerability within the HopeLend protocol’s lending pool, particularly during the process of burning deposit certificates. An incorrect integer division led to a truncation error, causing fewer certificates to be destroyed than required. This created a discrepancy that the attacker exploited to drain multiple lending pools in a single block transaction, significantly amplifying the discount rate within the hETHwBTC lending pool, which notably had been inactive with no funds until the attack.

Interestingly, the original attacker did not profit directly from this exploit. A front-runner, detecting the attacker’s transaction within the memory pool, replicated the attack method and successfully executed it first, profiting 527 ETH. This twist adds an additional layer of complexity to the incident, demonstrating not just the technical savvy required to identify and exploit such vulnerabilities, but also the competitive and opportunistic nature of blockchain exploits.

Exploiting Cauldrons: A Case of Misaligned Debt Calculation

Cauldrons, a DeFi dapp facilitating lending and borrowing of crypto assets, fell victim to an exploit that capitalized on a rounding bug within its “cauldron v4” contracts. The attacker manipulated the debt calculation mechanism, specifically targeting the synchronization process between the elastic (actual borrowed amount) and base (stable debt representation) values. The flaw allowed the exploitation of a rounding error when the elastic value was zero but the base was not, leading to the theft of $6.5 million in MIM tokens on Ethereum. This attack not only highlighted the criticality of accurate debt representation but also the catastrophic potential of minor rounding discrepancies.

The Hundred Finance Hack: Manipulation and Rounding Error

Similarly, Hundred Finance experienced an attack where the perpetrator manipulated the exchange rate in the hWBTC contract, compounded by a rounding error within the redeemUnderlying function. By donating a significant amount of WBTC, the attacker was able to influence the exchange rate, exploiting the Solidity version’s vulnerabilities and the absence of borrowers for hWBTC assets. This manipulation enabled the redemption of a disproportionate amount of WBTC with minimal hWBTC, underscoring the nuanced risks associated with rounding errors and exchange rate manipulations.

As you can see by looking at these exploits, it becomes evident that rounding errors are not mere computational oversights but significant vulnerabilities that can lead to substantial financial losses in the DeFi ecosystem. These incidents illuminate the complex interplay between smart contract logic, the precision of mathematical operations, and the opportunistic nature of attackers waiting to exploit any vulnerabilities. Each exploit provides critical lessons on the importance of accuracy, vigilance, and comprehensive security measures in smart contract design and implementation.

Mitigation and Prevention Strategies

The recurring theme across these incidents highlights an urgent need for DeFi projects to adopt a proactive stance toward identifying and mitigating rounding errors and other vulnerabilities. Let’s explore actionable strategies that can prevent similar exploits.

  • Code Audits and Formal Verification: Comprehensive reviews by experienced auditors can uncover hidden bugs, including those related to rounding errors, before they can be exploited.
  • Implementing Safe Math Libraries: While Safe Math libraries are essential for preventing overflow and underflow, addressing rounding errors in Solidity requires more sophisticated solutions. Projects should consider advanced libraries, like the PRB Maths Library, which provides fixed-point math operations, helping to approximate floating-point arithmetic safely within Solidity’s constraints. These tools are crucial for performing accurate mathematical computations and mitigating the risk of rounding errors.
  • Continuous Monitoring and Analysis: Deploying tools that monitor for unusual activity and analyzing new vulnerabilities discovered across the ecosystem can provide early warnings.
  • Educating the Community: Increasing awareness about security best practices and common vulnerabilities among developers and users alike fosters a more secure environment.

By learning from past incidents and implementing these robust security measures, the DeFi community can safeguard against not only rounding errors but a broad spectrum of vulnerabilities, ensuring the future growth and stability of decentralized finance.

Conclusion

The sophisticated nature of recent attacks on DeFi protocols underscores the critical need for vigilance, rigorous security practices, and ongoing education within the blockchain community. By learning from these incidents and implementing robust security measures, developers can better protect their projects against similar exploits, safeguarding the future of DeFi.

Take the Next Step in Blockchain Security

Interested in diving deeper into the world of smart contract security? Our latest course on smart contract auditing is designed for developers, security professionals, and blockchain enthusiasts eager to fortify their understanding of securing DeFi protocols against vulnerabilities.

--

--

Extropy.IO

Oxford-based blockchain and zero knowledge consultancy and auditing firm