Wrong calculation for yVault price per share if decimals != 18
The yVault contract's getPricePerFullShare function hardcodes 1e18 for decimal scaling, assuming the underlying token always has 18 decimals. For tokens with different decimals, such as USDC with 6, the price per share is calculated incorrectly, potentially causing users or the protocol to lose funds.
Originally disclosed on Code4rena
Submitted by berndartmueller
The yVault.getPricePerFullShare() function calculates the price per share by multiplying with 1e18 token decimals with the assumption that the underlying token always has 18 decimals. yVault has the same amount of decimals as it’s underlying token see (yVault.decimals())
But tokens don’t always have 1e18 decimals (e.g. USDC).
Impact
The price per share calculation does not return the correct price for underlying tokens that do not have 18 decimals. This could lead to paying out too little or too much and therefore to a loss for either the protocol or the user.
Proof of Concept
Following test will fail with the current implementation when the underlying vault token has 6 decimals:
NOTE: units() helper function was adapted to accept the desired decimals.
it.only("should mint the correct amount of tokens for tokens with 6 decimals", async () => {
const DECIMALS = 6;
await token.setDecimals(DECIMALS);
expect(await yVault.decimals()).to.equal(DECIMALS);
expect(await yVault.getPricePerFullShare()).to.equal(0);
await token.mint(user1.address, units(1000, DECIMALS));
await token.connect(user1).approve(yVault.address, units(1000, DECIMALS));
await yVault.connect(user1).deposit(units(500, DECIMALS));
expect(await yVault.balanceOf(user1.address)).to.equal(units(500, DECIMALS));
await token.mint(strategy.address, units(500, DECIMALS));
expect(await yVault.getPricePerFullShare()).to.equal(units(2, DECIMALS));
});
Fails with following error: AssertionError: Expected "2000000000000000000" to be equal 2000000
Recommended mitigation steps
Use vault decimals() instead of hardcoded 1e18 decimals.
function getPricePerFullShare() external view returns (uint256) {
uint256 supply = totalSupply();
if (supply == 0) return 0;
return (balance() * (10**decimals())) / supply; // @audit-info use `decimals()` instead of hardcoded `1e18`
}
spaghettieth (JPEG’d) disputed and commented:
The
yVaultcontract has been designed to work with Curve LPs, which have 18 decimals
LSDan (judge) decreased severity to Medium and commented:
I’m downgrading this to a medium risk but leaving it as valid. Any number of external factors could conspire to result in a non-18 decimal token being used in the future, at which point this code may have been forgotten. A better choice would be to do a decimal check.
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 code hardcodes 1e18 instead of using the vault's actual decimals when scaling the price per share.
Impact
The incorrect price per share could lead to paying out too little or too much, causing a loss for either the protocol or the user.
What the attacker needed
Underlying token has non-18 decimals
The fix
The function should use the vault's decimals() instead of a hardcoded 1e18.
How it was found
- Set the token decimals to 6.
- Mint and approve tokens for the user.
- Deposit tokens into the vault.
- Mint additional tokens to the strategy address.
- Check the price per share and observe it is 2e18 instead of 2e6.
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.
A vault contract calculates how much one share is worth by multiplying the total balance by a fixed number that assumes the token has 18 decimal places. If the token actually has fewer decimals, like 6, the result is wrong by a huge factor. This could mean users get too little or too much when they withdraw.