Staking tokens can be stolen
The staking contract calculates shares to burn when withdrawing tokens by rounding down, allowing an attacker to withdraw more tokens than the shares they burn. An attacker can repeatedly exploit this rounding error to make a small profit.
Submitted by cmichel
The staking contract keeps track of shares of each user.
When withdrawing from the staking contract the amount parameter is converted to shares and this value is decreased (shares = amount / total_balance * total_share).
This shares calculation rounds down which allows stealing tokens.
The current code seems to try to protect against this by doing a maximum std::cmp::max(v.multiply_ratio(total_share, total_balance).u128(), 1u128) but this only works for the case where the shares would round down to zero. It’s still exploitable.
let withdraw_share = amount
// @audit only rounds up when it'd be zero, but should always round up
.map(|v| std::cmp::max(v.multiply_ratio(total_share, total_balance).u128(), 1u128))
// @audit sets it to the `amount` parameter if not `None`
let withdraw_amount = amount
.map(|v| v.u128())
Proof of Concept
Assume total_balance = 2e18 and total_share=1e18. Then withdrawing/burning one share should allow withdrawing total_balance / total_share = 2 amount.
However, imagine someone owns 1 shares (bought for 2 amount). Then withdraw_voting_tokens(amount=3). And withdraw_share = std::cmp::max(v.multiply_ratio(total_share, total_balance).u128(), 1u128) = max(amount=3 * total_share=1e18 / total_balance=2e18, 1) = max("3/2", 1) = 1
The attacker made a profit.
The attacker can make a profit by staking the least amount to receive one share, and then immediately withdrawing this one share by specifying the largest amount such that the integer rounding still rounds to this one share. They make a small profit. They can repeat this many times in a single transaction with many messages.
Depending on the token price, this can be profitable as similar attacks show.
Recommended Mitigation Steps
The protocol tries to protect against this attack but the max(., 1) is not enough mitigation. There are several ways to fix this:
- Always round up in
withdraw_share - Always recompute the
withdraw_amountwith the newwithdraw_shareeven if theamountparameter was set. - Use a
shareparameter (instead of theamountparameter) and use it aswithdraw_share. Rounding down on the resultingwithdraw_amountis bad for the attacker as burning a share leads to fewer tokens.
bitn8 (Anchor) disagreed with severity
Albert Chon (judge) decreased severity to Medium and commented:
The amount would be miniscule due to the initial share amount scale being equal to the staked tokens scale (1e6), but indeed, the attack could be applied infinite times, though it’s likely then that the gas costs incurred would exceed the profits gained.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
How it worked
Impact
An attacker could steal tokens through repeated rounding errors, though the profit per withdrawal is minuscule and gas costs may exceed gains.
What the attacker needed
Ability to stake tokens; ability to withdraw tokens
The fix
The contract should always round up when calculating shares to burn, or recompute the withdrawal amount based on the actual shares burned.
How it was found
- Stake the minimum amount to receive one share.
- Immediately withdraw by specifying the largest amount that still rounds to one share.
- Repeat many times in a single transaction to accumulate profit.
Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
When you stake tokens, you get shares representing your portion. The contract calculates how many shares to burn when you withdraw, but it rounds down, so you might burn fewer shares than you should. This means you can withdraw more tokens than you're entitled to, and by repeating this, you can steal a small amount each time.
What is Smart contracts?
Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.