2020/08/13 Yam Finance Smart Contract Bug Analysis & Future Prevention

CertiK | Aug 13, 2020

Article's Poster

What Happened?

On August 12th, Yam Finance officially announced that a smart contract bug was found that would mint more YAM tokens than actually intended during its rebase event, which is a planned mint/burn event meant to keep the price stable over time. Yam Finance uses an elastic supply of YAM tokens to stabilize the price, similar to Ampleforth, a CertiK client. Normally, if the price of YAM is higher than the intended price, the rebase event would print more YAM tokens to dilute the price; inversely, if the YAM price is lower than the intended price, the rebase event would burn YAM tokens to use scarcity to increase the price.

In the case of this bug, an excess of YAM tokens would be printed, causing a larger supply to dilute the intended price, meaning that the value of each YAM token would decrease--hurting investors. Additionally, this larger supply of YAM tokens would make it more difficult for the community to contribute to governance, as more YAM tokens would be needed to make changes. This would make the code unable to be modified, so bugs such as these would persist.

Where Is The Smart Contract Bug?

The bug is in the rebase function of smart contract YAM.sol in the YAM project, as shown in the following screenshot:

REF: https://github.com/yam-finance/yam-protocol/blob/767e3a4a6918b6fb6100ad6bb356164408f5d82f/contracts/token/YAM.sol#L340

The rebase function shown above was built to keep the token at a stable price. However, the highlighted line in the code mistakenly calculates the totalSupply wrong , which would reserve too many minted tokens. The correct code/calculation equation for the highlighted line of code should be:

totalSupply = initSupply.mul(yamsScalingFactor).div(BASE);

Can Governance Fix This Bug?

YAM Finance publicly announced they need around 160,000 YAM by 3am EDT to be able to submit a proposal that would allow users to transfer or deposit tokens back into the staking pool if the delegated voting power above 400,000 YAM. This proposal needed to be submitted by 3am EDT since the next rebase was set to happen at 4am EST on August 13th.

YAM did receive a large amount of support from the community, and the proposal was successfully submitted. Unfortunately, the new submitted proposal failed to work in the smart contract and YAM Finance became an ungovernable DeFi project.

Current State and Future Plans

Due to this smart contract bug, YAM Finance lost the ability to govern. Additionally, 75% of the YAM/yCRV uniswap pool has been liquidated and moved out of the pool, while the rest has been completely removed.

YAM Finance plans to launch YAM 2.0 after the success of the community-funded audit of their contracts.

How Can We Prevent this Moving Forward?

[A pop-up from the Yam Finance home page.]

In the case of Yam Finance, the website was very explicit in mentioning that they did not undergo a security audit, and we can only speculate why. This could have been due to the speed, the cost, or underestimation of the importance of audits. But events such as these point to why the CertiK security team strongly recommends that all blockchain projects conduct rigorous security audits before code deployment.

What was an overnight DeFi success, which attracted $400M in less than a day, quickly crashed due to a simple, avoidable bug. Third-party audits with professional security experts allow more eyes and innovative techniques to attempt to find and fix vulnerabilities before it’s too late. CertiK is an expert in this field, securing over $6B in assets to date.

Audits can be costly, and in the fully decentralized, bootstrapped approach of recent DeFi projects like yam.finance and yearn.finance, a single person may not be willing to invest thousands of dollars to receive full-scale audits. For that reason, CertiK has created a decentralized security oracle system on the CertiK Chain, which allows for multiple security professionals to bid against each other to fulfill security information requests from users of a specific smart contract.

This decentralized security mechanism complements the decentralized spirit of development and governance of DeFi, allowing for transparent pricing, competition, and clear results for users to obtain. The burden of payment would be transferred from a centralized responsibility for the single individual who made a contract, but rather, shared among the users who are using the contract.

While the audit and analysis is crucial, CertiK believes that the audit is not the end, but actually just the beginning. After the code is deployed, it interacts with the rest of the ever-changing blockchain world, possibly opening the doors for novel vulnerabilities. For that reason, CertiK has created a safety-net mechanism called CertiKShield, which allows for flexible reimbursements for any scenarios in which assets are lost, stolen, or frozen.

CertiK’s Dynamic Approach to Smart Contract Bugs

Attacks on smart contracts are dynamic. So their defenses need to be too.

After a smart contract is deployed, it is not always transparent as to whether projects have undergone audits (Yam Finance was very clear that they had not, but other projects are more opaque). The CertiK Decentralized Security Oracles use the CertiK Chain to fetch security information in real-time, allowing users to retrieve evidence and security analysis; in turn, they can make more educated decisions about whether to actually send their funds a smart contract or not.

These security oracles can be deployed on any blockchain protocol, so that security diligence can be more widespread across the entire ecosystem. DeFi smart contracts can simply call a security oracle to check the risk of a given transaction/function/ address.

[This is a high level diagram of the Security Oracle architecture.]

For dynamic protection of any lost assets, CertiK created a reimbursement mechanism called CertiKShield, which is a fully flexible, decentralized pool of funds that use CertiK Chain’s on-chain voting to reimburse lost, stolen, or frozen assets. CertiKShields are able to be created for any blockchain project across any protocol (however, to avoid situations like Yam’s, these projects must be audited), allowing the benefits of security and reliability to extend across the blockchain world. The CertiKShield Pools are initiated directly by blockchain projects (for instance, Yam Finance), which makes sure that all parties have the best intentions in mind for the entire community.

[This is a theoretical example of the CertiKShield Pool participants.]

As DeFi continues to learn from these setbacks and find solid ground, CertiK’s new innovations aim to drive the mission of securing the cyber world for all.

Writing References: