Started from 08:13:06 PM UTC on May 7th, 2021, ValueDeFi’s multi-strategy WBNB vault was exploited due to an invalid share calculation exploit. In this post, we review the technical details of this issue step by step.
- At the time of the exploit, ValueDeFi’s multi-strategy WBNB vault had two live strategies: Alpaca Finance and CDO.Finance.
- An incorrect assumption in ValueDeFI’s regarding Alpaca’s Finance ibBNB price, led to the number of shares issued to be miscalculated, inflating the amount of VSafeVault BNB shares the attacker received when depositing BNB into the vault.
- The attacker was then able to redeem his inflated shares back for BNB, causing loss of funds in ValueDeFi’s BNB Vault.
- Only the funds in ValueDeFi were affected by this attack. Funds in Alpaca Finance were safe.
- A similar attacking vector happened on the Rari Capital exploit, by the same attacker on Ethereum (https://nipunp.medium.com/5-8-21-rari-capital-exploit-timeline-analysis-8beda31cbc1a).
2. Related BSC Addresses
- Attacker’s Wallet:
- Attacker’s Malicious Token:
- Malicious Token-WBNB PCS LP: 0xbfa618f7b7078be3b00f5ccfb85907083efab9f0
- Alpaca Vault:
- Alpaca PancakeswapV2Worker: 0x7Af938f0EFDD98Dc513109F6A7E85106D26E16c4
- Alpaca PancakeswapV2StrategyAddBaseTokenOnly:
- Value VSafeVaultWBNB:
- Value VSafeVaultController:
- Value StrategyAlpacaWBNB:
3. In-depth Technical Details
3.1. Abusing Alpaca Vault work() Function to Utilize Execution Flow
In Alpaca Finance, users can open a leveraged yield farming position using
work() function, in which the pool can be selected dynamically using the farming token.
- A token with a specially written
approve()function must be deployed
- The attacker called the
work()function of Alpaca’s WBNB Vault contract with malicious data encoded in the data variable, which was passed to the
work()function of the worker contract (
- In the worker contract (
PancakeswapV2Worker), the data was then decoded into two variables,
stratis the address of the strategy used, and the worker checked whether the address was an approved strategy contract, so this value could not be arbitrarily manipulated. While
extvariable was passed further to the
execute()function in the decoded strategy contract.
- In the strategy contract (
PancakeswapV2StrategyAddBaseTokenOnly), the data variable (
extvariable in the worker contract) had a malicious
farmingTokenaddress embedded. With the contract calling the malicious
approve()function of the malicious token, the attacker used the execution flow to interact with the
By using the steps above, the attacker utilized the execution flow of the
work() function in Alpaca’s WBNB Vault contract as shown in the following screenshot. The marked transactions with the red box were executed by the malicious contract.
In this case, the cross-contract execution happened during the phase where the balance of the token inside the vault contract had been transferred to the worker, but before the debt value was updated.
Alpaca Finance’s smart contracts have security mechanisms in place that prevent reentrancy attacks, so the tokens inside Alpaca Vault contracts are safe from these types of attacks. However, Value’s integration of this Vault contract had a loophole that the attacker was able to exploit.
To sum up, the attacker called Alpaca’s WBNB Vault with a malicious input, allowing the attacker to execute functions in the ValueDeFi platform during the period when not all variables were fully updated, so the calculations on ValueDeFi were done incorrectly, which was the attacker’s target.
3.2. Invalid Share Calculation in Value’s vSafe Vault
VSafeVaultWBNB contract is a multi-strategy vault that compounds users’ funds to multiple destinations by selecting the best strategy. However, with ValueDeFi’s incorrect assumption of the balance value in Alpaca Vault, the amount of shares were miscalculated, causing loss to the funds in Value’s vSafe Vault.
- The current best strategy must be any pool other than Alpaca Vault
- In ValueDeFi’s
VSafeVaultWBNBcontract, tokens can be deposited into the vault using the
depositFor()function. This function checks whether the deposit cap is exceeded, gets the balance of the pool using the
balance()function, then passes the pool balance to the
balance()function calculates the sum of the base token in the vault contract and the balance of the token in each strategy pool. The sum is then deducted with the pending compound.
As the WBNB token balance of Alpaca Vault had been transferred to the Alpaca Worker, but the debt value was not yet updated, the value of the pool balance calculated was less than the actual value.
_deposit()function then transferred the base token to be deposited to the contract, and calculated the amount of share to be minted from this deposit.
As the pool balance was improperly calculated to be lower than the actual amount, this resulted in a higher number of shares being minted.
In this case, with the miscalculated balance by ValueDeFi, the attacker was able to deposit WBNB and gained a larger amount of
vSafeWBNB tokens than usual. This can be seen in the screenshot below:
In the next transaction, the attacker burned the
vSafeWBNB tokens gained from the previous transaction and withdrew WBNB from
VSafeVaultWBNB as shown in the screenshot below:
In this attack, the attacker deposited 1,456.58 WBNB to gain 2,083.95
vSafeWBNB tokens. Then withdrew 2,144.41 WBNB tokens by burning the
vSafeWBNB tokens minted. From this transaction, the attacker gained 687.83 WBNB tokens from the
4. Attack Summary
The attacker repeated these steps 9 times. In total, the attacker gained 5345.314 WBNB from
VSafeVaultWBNB contract as shown in the table below.
4.1. Tx Links
Round #1: Tx Link
Round #2: Tx Link
Round #3: Tx Link
Round #4: Tx Link
Round #5: Tx Link
Round #6: Tx Link
Round #7: Tx Link
Round #8: Tx Link
Round #9: Tx Link
5. Actions taken by Alpaca Finance
Inspex worked with Alpaca Finance since we became aware of the attack on May 8th, 2021.
Alpaca Finance’s team communicated to us that the strategies were intentionally designed to act as a library that other protocols could utilize and integrate. This is a common open-source approach to dapp interoperability in DeFi. However, from this incident, we made a suggestion to Alpaca Finance to make changes to the contract permissions, in order to prevent similar attacks from occurring to other platforms that build off Alpaca’s protocol without authorization or proper oversight, and might make incorrect assumptions on certain parameters.
Together with Alpaca Finance, we worked out a solution and Alpaca Finance’s relevant contracts are now updated with the suggested adjustment applied. Strategy now only reads baseToken and
farmingToken from the Worker contract, and only whitelisted workers can interact with the strategy.
6. Recommendations for ValueDeFi
To prevent this attack in future integration with any platform, the last known price of any asset should be kept as a reference. In case of an abnormal change of the price due to malicious manipulation, the transaction must be reverted or consider using an oracle to prevent this attack.
Inspex is formed by a team of cybersecurity experts highly experienced in various fields of cybersecurity. We provide blockchain and smart contract professional services at the highest quality to enhance the security of our clients and the overall blockchain ecosystem.
For any business inquiries, please contact us via Twitter, Telegram, firstname.lastname@example.org