The calculation of takeFees in Vault contract is incorrect
The Vault contract mints fee shares using a conversion that divides by the pre-fee asset total, so the fee recipient receives fewer shares than the fee is worth. An attacker (or any user) benefits because the fee charged is less than expected, diluting the fee recipient's entitlement.
Submitted by KingNFT, also found by immeas and bin2chen
The calculation of takeFees in Vault contract is incorrect, which will cause fee charged less than expected.
Proof of Concept
To simplify the problem, let’s given the fee parameters are as follows:
// Fees are set in 1e18 for 100% (1 BPS = 1e14)
struct VaultFees {
uint64 deposit; // 0
uint64 withdrawal; // 0
uint64 management; // 0.5e18 = 50%
uint64 performance; // 0
}
And the initial asset token and share tokens in the vault are:
totalAsset = 100 $AST
totalShare = 100 $SHR
yieldEarnings = 0 $AST
The yield earnings is also set to 0.
As the yearly management fee is 50%, so the expected fee for one year is:
feeInAsset = 100 $AST * 0.5 = 50 $AST
Now let’s calculate the actual fee. The implementation of accruedManagementFee() is
function accruedManagementFee() public view returns (uint256) {
uint256 managementFee = fees.management;
return
managementFee > 0
? managementFee.mulDiv(
totalAssets() * (block.timestamp - feesUpdatedAt),
SECONDS_PER_YEAR,
Math.Rounding.Down
) / 1e18
: 0;
}
So in this case, one year later, the calculation of first step for managementFee will be
managementFee = 0.5e18 * 100 $AST * SECONDS_PER_YEAR / SECONDS_PER_YEAR / 1e18 = 50 $AST
The implementation of takeFees() is
modifier takeFees() {
uint256 managementFee = accruedManagementFee();
uint256 totalFee = managementFee + accruedPerformanceFee();
uint256 currentAssets = totalAssets();
uint256 shareValue = convertToAssets(1e18);
if (shareValue > highWaterMark) highWaterMark = shareValue;
if (managementFee > 0) feesUpdatedAt = block.timestamp;
if (totalFee > 0 && currentAssets > 0)
_mint(feeRecipient, convertToShares(totalFee));
_;
}
So, variables before L491 of takeFees() will be
managementFee = 50 $AST
totalFee = 50 $AST + 0 = 50 $AST
currentAssets = 100 $AST
As the implementation of convertToShares() is
function convertToShares(uint256 assets) public view returns (uint256) {
uint256 supply = totalSupply();
return
supply == 0
? assets
: assets.mulDiv(supply, totalAssets(), Math.Rounding.Down);
}
So the second parameter for _mint() call at L491 is
feeInShare = convertToShares(totalFee) = convertToShares(50 $AST) = 50 $AST * 100 $SHR / 100 $AST = 50 $SHR
After _mint() at L491, the variables will be
shareOfUser = 100 $SHR
shareOfFeeRecipient = 50 $SHR
totalShare = 100 + 50 = 150 $SHR
totalAsset = 100 $AST
The implementation of convertToAssets() is
function convertToAssets(uint256 shares) public view returns (uint256) {
uint256 supply = totalSupply();
return
supply == 0
? shares
: shares.mulDiv(totalAssets(), supply, Math.Rounding.Down);
}
Now we can get actual fee by calling convertToAsset(), which is
actualFeeInAsset = convertToAsset(feeInShare) = convertToAsset(50 $SHR) = 50 $SHR * 100 $AST / 150 $SHR = 33.3 $AST
We can see the actual fee is less than expected, the realistic parameters will be less than the give 0.5e18, but it will be always true that the actual fee charged is not enough.
Tools Used
VS Code
Recommended Mitigation Steps
Use the correct formula such as:
modifier takeFees() {
uint256 managementFee = accruedManagementFee();
uint256 totalFee = managementFee + accruedPerformanceFee();
uint256 currentAssets = totalAssets();
uint256 shareValue = convertToAssets(1e18);
if (shareValue > highWaterMark) highWaterMark = shareValue;
if (managementFee > 0) feesUpdatedAt = block.timestamp;
- if (totalFee > 0 && currentAssets > 0)
- _mint(feeRecipient, convertToShares(totalFee));
+ if (totalFee > 0 && currentAssets > 0) {
+ uint256 supply = totalSupply();
+ uint256 feeInShare = supply == 0
+ ? totalFee
+ : totalFee.mulDiv(supply, currentAssets - totalFee, Math.Rounding.Down);
+ _mint(feeRecipient, feeInShare);
+ }
_;
}
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
Likely the fee shares are minted with convertToShares(totalFee), which divides by totalAssets() before the fee is added, rather than by the remaining assets after the fee.
Impact
The fee recipient receives less than the intended fee, so the vault undercharges; the text notes realistic parameters are lower but the shortfall always occurs.
What the attacker needed
A vault with a non-zero management fee; time passes so fees accrue
The fix
The fee share should be computed by dividing totalFee by the assets remaining after the fee (currentAssets - totalFee) rather than by currentAssets.
How it was found
- Set the management fee to a non-zero value and note the initial total assets and total shares.
- Wait for the fee period to pass so accruedManagementFee becomes non-zero.
- Trigger takeFees, which mints fee shares via convertToShares(totalFee).
- Convert the minted fee shares back to assets and observe the value is below the expected fee.
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 holds money for many people and charges a fee by giving the fee collector new shares. The code worked out how many shares to give using the old total, so the collector ended up with shares worth less than the fee. This means the vault undercharged its own fee.
What is Smart contracts?
Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.