`burnSharesToWithdrawEarnings` burns before math, causing the share value to increase
The burnSharesToWithdrawEarnings function burns shares before calculating the share price, which increases the share value and causes users to be overpaid. An attacker could withdraw more principal than their fair share, potentially leading to insolvency.
0x3b Follow with 0x73696d616f, 0xadrii and 7 others
Originally disclosed on Sherlock
Source: https://github.com/sherlock-audit/2024-04-teller-finance-judging/issues/19
Found by
0x3b, 0x73696d616f, 0xadrii, Bauer, EgisSecurity, IvanFitro, eeshenggoh, jovi, no, pkqs90
Summary
The burnSharesToWithdrawEarnings function burns shares before calculating the share price, resulting in an increase in share value and causing users to be overpaid.
Vulnerability Detail
When burnSharesToWithdrawEarnings burns shares and subsequently calculates the principal per share, the share amount is reduced but the principal remains the same, leading to a decrease in the sharesExchangeRateInverse, which increases the principal paid.
Principal is calculated using _valueOfUnderlying, where the formula is:
uint256 principalTokenValueToWithdraw = (amount * 1e36) / sharesExchangeRateInverse();
The problem arises in sharesExchangeRateInverse which uses the reduced share count:
return 1e72 / sharesExchangeRate();
Where sharesExchangeRate is defined as:
rate_ = (poolTotalEstimatedValue * 1e36) / poolSharesToken.totalSupply();
Example:
- 1k shares and 10k principal.
- Alice withdraws 20% of the shares.
- 200 shares are burned, reducing the supply to 800.
- Calculation for principal:
rate_ = (10_000e18 * 1e36) / 800e18 = 1.25e37
sharesExchangeRateInverse = 1e72 / 1.25e37 = 8e34
principalTokenValueToWithdraw = 200e18 * 1e36 / 8e34 = 2500e18
- Alice withdraws 25% of the pool's principal, despite owning only 20% of the shares.
Impact
This flaw results in incorrect calculations, allowing some users to withdraw more than their fair share, potentially leading to insolvency.
Code Snippet
poolSharesToken.burn(msg.sender, _amountPoolSharesTokens);
uint256 principalTokenValueToWithdraw = _valueOfUnderlying(
_amountPoolSharesTokens,
sharesExchangeRateInverse()
);
Tool used
Manual Review
Recommendation
To ensure accurate withdrawal amounts, calculate principalTokenValueToWithdraw before burning the shares.
Discussion
sherlock-admin2
The protocol team fixed this issue in the following PRs/commits: https://github.com/teller-protocol/teller-protocol-v2-audit-2024/pull/11
sherlock-admin2
The Lead Senior Watson signed off on the fix.
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
Root cause
The contract burns the user's shares before computing the exchange rate used to determine the withdrawal amount, so the reduced total supply inflates the per-share value.
Impact
Some users can withdraw more than their fair share, potentially leading to insolvency.
What the attacker needed
A user holds pool shares; the user initiates a withdrawal
The fix
The issue was fixed by calculating the principal to withdraw before burning the shares.
How it was found
- Alice initiates a withdrawal of 20% of the shares.
- The contract burns 200 shares, reducing total supply from 1000 to 800.
- The contract calculates the principal to withdraw using the reduced share count.
- Alice receives 25% of the pool's principal despite owning only 20% of the shares.
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.
In a shared pool, each share represents a portion of the total money. When someone withdraws, the contract first destroys their shares and then calculates how much money those shares are worth. Because the total number of shares is now smaller but the total money is the same, each remaining share appears more valuable, so the person gets more money than they should.
What is Business logic?
The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.