`wfCashERC4626`
The wfCash vault mints shares based on the deposit amount without accounting for transfer fees, so for fee-on-transfer tokens it credits less prime cash than the shares it issues. This makes the vault insolvent, and the last depositors to redeem cannot withdraw their full share value.
Source: https://github.com/sherlock-audit/2023-12-notional-update-5-judging/issues/58
Found by
eol
Summary
The wfCash vault is credited less prime cash than the wfCash it mints to the depositor when its underlying asset is a fee-on-transfer token. This leads to the vault being insolvent because it has issued more shares than can be redeemed.
Vulnerability Detail
When minting wfCash shares more than the maxFCash available, the wfCashERC4626 vault lends the deposited assets at 0% interest. The assets are deposited by the vault into Notional and the depositor gets 1:1 wfCash in return. This works fine for assets that are not fee-on-transfer tokens.
https://github.com/sherlock-audit/2023-12-notional-update-5/blob/main/wrapped-fcash/contracts/wfCashLogic.sol#L60-L68
if (maxFCash < fCashAmount) {
// NOTE: lending at zero
uint256 fCashAmountExternal = fCashAmount * precision / uint256(Constants.INTERNAL_TOKEN_PRECISION);
require(fCashAmountExternal <= depositAmountExternal);
// NOTE: Residual (depositAmountExternal - fCashAmountExternal) will be transferred
// back to the account
NotionalV2.depositUnderlyingToken{value: msgValue}(address(this), currencyId, fCashAmountExternal);
} else if (isETH || hasTransferFee || getCashBalance() > 0) {
For fee-on-transfer tokens, the vault is credited prime cash based on the actual amount it received, which is deposit amount - transfer fee.
https://github.com/sherlock-audit/2023-12-notional-update-5/blob/main/contracts-v3/contracts/internal/balances/TokenHandler.sol#L204-L214
} else {
// In the case of deposits, we use a balance before and after check
// to ensure that we record the proper balance change.
actualTransferExternal = GenericToken.safeTransferIn(
underlying.tokenAddress, account, underlyingExternalDeposit
).toInt();
}
netPrimeSupplyChange = _postTransferPrimeCashUpdate(
account, currencyId, actualTransferExternal, underlying, primeRate
);
However, the vault mints fCashAmount of shares to the depositor.
https://github.com/sherlock-audit/2023-12-notional-update-5/blob/main/wrapped-fcash/contracts/wfCashLogic.sol#L84-L85
// Mints ERC20 tokens for the receiver
_mint(receiver, fCashAmount);
In the case of lending at 0% interest, fCashAmount is equal to depositAmount but at 1e8 precision.
To simplify the example, let us assume that there are no other depositors. When the sole depositor redeems all their wfCash shares at maturity, they will be unable to redeem all their shares because the wfCash vault does not hold enough prime cash.
Impact
Although the example used to display the vulnerability is for the case of lending at 0% interest, the issue exists for minting any amount of shares.
The wfCashERC4626 vault will become insolvent and unable to buy back all shares. The larger the total amount deposited, the larger the deficit. The deficit is equal to the transfer fee. Given a total deposit amount of 100M USDT and a transfer fee of 2% (assuming a transfer fee was set and enabled for USDT), 2M USDT will be the deficit.
The last depositors to redeem their shares will be shouldering the loss.
Code Snippet
https://github.com/sherlock-audit/2023-12-notional-update-5/blob/main/wrapped-fcash/contracts/wfCashLogic.sol#L60-L68 https://github.com/sherlock-audit/2023-12-notional-update-5/blob/main/contracts-v3/contracts/internal/balances/TokenHandler.sol#L204-L214 https://github.com/sherlock-audit/2023-12-notional-update-5/blob/main/wrapped-fcash/contracts/wfCashLogic.sol#L84-L85
Tool used
Manual Review
Recommendation
Consider adding the following:
- A flag in
wfCashERC4626that signals that the vault's asset is a fee-on-transfer token. - In
wfCashERC4626._previewMint()andwfCashERC46262._previewDeposit, all calculations related toassetsshould account for the transfer fee of the token.
Discussion
sherlock-admin2
1 comment(s) were left on this issue during the judging contest.
takarez commented:
vlaid because { This is valid and a duplicate of 016 due to the same underlying cause of zero lending }
jeffywu
I think the issue is valid, although the contention that the vault becomes insolvent is not completely true. When lending at 0% interest the vault also accrues variable rate interest, which may or may not accrue sufficient value to recover the loss from the transfer fee.
Agree that some sort of flag here would be appropriate to ensure that these tokens do not trigger an immediate loss is appropriate.
jeffywu
Fix will be that we do not allow tokens with fee on transfer to perform lend at zero.
gjaldon
Escalate
I think the issue is valid, although the contention that the vault becomes insolvent is not completely true. When lending at 0% interest the vault also accrues variable rate interest, which may or may not accrue sufficient value to recover the loss from the transfer fee.
I think this is a High because the vault still stays insolvent even when the vault accrues variable rate interest. This is because any variable rate interest accrual is only applied to the prime cash's exchange rate (like a cToken.)
The issue here is that the total wfCash minted is supposed to be 1:1 with the vault's primeCash balance in Notional. However, there is more wfCash minted than the vault's prime cash balance in Notional because the Vault's wfCash minting does not subtract the transfer fees when computing for the amount to mint, which leads to the discrepancy.
Any variable interest rate accrual will not increase the prime cash balance of the Vault because it is non-rebasing. That means that the Vault's prime cash balance in Notional will not increase due to accrual and it will remain less than the Vault's total wfCash minted and the vault will remain insolvent.
For example:
Vault wfCash supply - 1000
Vault's prime cash balance in Notional - 975 (1000 - 2.5% transfer fee)
Variable interest rate accrual - increases the exchange rate from, for example, 110% to 115%
Total wfCash that can be redeemed is still capped at 975 even with the variable interest rate accrual. Vault remains insolvent because not all shares can be redeemed.
sherlock-admin2
Escalate
I think the issue is valid, although the contention that the vault becomes insolvent is not completely true. When lending at 0% interest the vault also accrues variable rate interest, which may or may not accrue sufficient value to recover the loss from the transfer fee.
I think this is a High because the vault still stays insolvent even when the vault accrues variable rate interest. This is because any variable rate interest accrual is only applied to the prime cash's exchange rate (like a cToken.)
The issue here is that the total
wfCashminted is supposed to be 1:1 with the vault's primeCash balance in Notional. However, there is morewfCashminted than the vault's prime cash balance in Notional because the Vault'swfCashminting does not subtract the transfer fees when computing for the amount to mint, which leads to the discrepancy.Any variable interest rate accrual will not increase the prime cash balance of the Vault because it is non-rebasing. That means that the Vault's prime cash balance in Notional will not increase due to accrual and it will remain less than the Vault's total
wfCashminted and the vault will remain insolvent.For example: Vault
wfCashsupply - 1000 Vault's prime cash balance in Notional - 975 (1000 - 2.5% transfer fee) Variable interest rate accrual - increases the exchange rate from, for example, 110% to 115%Total
wfCashthat can be redeemed is still capped at 975 even with the variable interest rate accrual. Vault remains insolvent because not all shares can be redeemed.
The escalation could not be created because you are not exceeding the escalation threshold.
You can view the required number of additional valid issues/judging contest payouts in your Profile page, in the Sherlock webapp.
JeffCX
Escalate
I think the issue is valid, although the contention that the vault becomes insolvent is not completely true. When lending at 0% interest the vault also accrues variable rate interest, which may or may not accrue sufficient value to recover the loss from the transfer fee.
I think this is a High because the vault still stays insolvent even when the vault accrues variable rate interest. This is because any variable rate interest accrual is only applied to the prime cash's exchange rate (like a cToken.)
The issue here is that the total wfCash minted is supposed to be 1:1 with the vault's primeCash balance in Notional. However, there is more wfCash minted than the vault's prime cash balance in Notional because the Vault's wfCash minting does not subtract the transfer fees when computing for the amount to mint, which leads to the discrepancy.
Any variable interest rate accrual will not increase the prime cash balance of the Vault because it is non-rebasing. That means that the Vault's prime cash balance in Notional will not increase due to accrual and it will remain less than the Vault's total wfCash minted and the vault will remain insolvent.
For example: Vault wfCash supply - 1000 Vault's prime cash balance in Notional - 975 (1000 - 2.5% transfer fee) Variable interest rate accrual - increases the exchange rate from, for example, 110% to 115%
Total wfCash that can be redeemed is still capped at 975 even with the variable interest rate accrual. Vault remains insolvent because not all shares can be redeemed.
sherlock-admin2
Escalate
I think the issue is valid, although the contention that the vault becomes insolvent is not completely true. When lending at 0% interest the vault also accrues variable rate interest, which may or may not accrue sufficient value to recover the loss from the transfer fee.
I think this is a High because the vault still stays insolvent even when the vault accrues variable rate interest. This is because any variable rate interest accrual is only applied to the prime cash's exchange rate (like a cToken.)
The issue here is that the total wfCash minted is supposed to be 1:1 with the vault's primeCash balance in Notional. However, there is more wfCash minted than the vault's prime cash balance in Notional because the Vault's wfCash minting does not subtract the transfer fees when computing for the amount to mint, which leads to the discrepancy.
Any variable interest rate accrual will not increase the prime cash balance of the Vault because it is non-rebasing. That means that the Vault's prime cash balance in Notional will not increase due to accrual and it will remain less than the Vault's total wfCash minted and the vault will remain insolvent.
For example: Vault wfCash supply - 1000 Vault's prime cash balance in Notional - 975 (1000 - 2.5% transfer fee) Variable interest rate accrual - increases the exchange rate from, for example, 110% to 115%
Total wfCash that can be redeemed is still capped at 975 even with the variable interest rate accrual. Vault remains insolvent because not all shares can be redeemed.
You've created a valid escalation!
To remove the escalation from consideration: Delete your comment.
You may delete or edit your escalation comment anytime before the 48-hour escalation window closes. After that, the escalation becomes final.
jeffywu
I won't opine the severity of the issue but I will say the understanding of the prime cash balance is not entirely correct in the escalation comment:
fCash is denominated in underlying (i.e. 1 fETH = 1 ETH at maturity) while prime cash is a non-rebasing token with a monotonically increasing exchange rate to ETH. If we mint 1 fETH "share" while lending at zero interest but only deposit 0.975 ETH worth of pCash prior to maturity...it would be appropriate to say that the vault is insolvent at this moment in time.
However, if the 0.975 ETH worth of pCash accrues interest over X days and is now worth 1 ETH and no other deposits / withdraws occur, the vault is no longer insolvent. The rebasing / non-rebasing nature of pCash does not affect the solvency issue, the vault has full access to the underlying value of the pCash to accommodate withdraws.
gjaldon
@jeffywu isn't it that a positive cash balance is non-rebasing and any interest accruals do not change the account's cash balance? If I'm not mistaken, any interest accruals only change the prime cash factors: ref: https://github.com/sherlock-audit/2023-12-notional-update-5/blob/main/contracts-v3/contracts/internal/pCash/PrimeCashExchangeRate.sol#L589-L613
function getPrimeCashRateStateful(
uint16 currencyId,
uint256 blockTime
) internal returns (PrimeRate memory rate) {
PrimeCashFactors memory factors = getPrimeCashFactors(currencyId);
// Only accrue if the block time has increased
if (factors.lastAccrueTime < blockTime) {
uint256 primeSupplyToReserve;
uint256 currentUnderlyingValue = getTotalUnderlyingStateful(currencyId);
(factors, primeSupplyToReserve) = _updatePrimeCashScalars(
currencyId, factors, currentUnderlyingValue, blockTime
);
_setPrimeCashFactorsOnAccrue(currencyId, primeSupplyToReserve, factors);
} else {
require(factors.lastAccrueTime == blockTime); // dev: revert invalid blocktime
}
rate = PrimeRate({
supplyFactor: factors.supplyScalar.mul(factors.underlyingScalar).toInt(),
debtFactor: factors.debtScalar.mul(factors.underlyingScalar).toInt(),
oracleSupplyRate: factors.oracleSupplyRate
});
}
_updatePrimeCashScalars() computes the interest accrual and updates the factors. PrimeRate.supplyFactor is the exchange rate that's used to compute for the total ETH that can be withdrawn.
Given a Vault that had deposited 1000 tokens into pCash and the following are true:
Vault has 1000 wfCash total supply Vault has pCash balance of 975 in Notional (1000 - 2.5% transfer fee) PR.supplyFactor is 100%
Let's say the PR.supplyFactor has increased to 120% due to interest accrual. When the Vault withdraws, it calls AccountAction.withdraw() which also ends up calling BalanceHandler._finalize().
transferAmountExternal = TokenHandler.withdrawPrimeCash(
account,
receiver,
balanceState.currencyId,
balanceState.primeCashWithdraw,
balanceState.primeRate,
withdrawWrapped // if true, withdraws ETH as WETH
);
{
// No changes to total cash after this point
int256 totalCashChange = balanceState.netCashChange.add(balanceState.primeCashWithdraw);
if (
checkAllowPrimeBorrow &&
totalCashChange < 0 &&
balanceState.storedCashBalance.add(totalCashChange) < 0
) {
// ... snip ...
require(accountContext.allowPrimeBorrow, "No Prime Borrow");
checkDebtCap = true;
}
if (totalCashChange != 0) {
balanceState.storedCashBalance = balanceState.storedCashBalance.add(totalCashChange);
mustUpdate = true;
}
}
In BalanceHandler.finalize(), balanceState.primeCashWithdraw is the amount being withdrawn, which is -1000 (all the wfCash supply.) It is negative since withdrawal amounts are turned negative to represent decrease in cash balance. totalCashChange will be equal to balanceState.primeCashWithdraw, which is -1000. balanceState.storedCashBalance will be the Vault's cash balance of 975. Adding both will result in -25 and will lead to the "No Prime Borrow" revert since there is not enough cash balance for the withdrawal and prime borrowing is not enabled for the vault.
In TokenHandler.withdrawPrimeCash() is where the exchange rate is applied so that we get the principal plus the interest accrual.
// TokenHandler.withdrawPrimeCash()
Token memory underlying = getUnderlyingToken(currencyId);
netTransferExternal = convertToExternal(
underlying,
primeRate.convertToUnderlying(primeCashToWithdraw)
);
primeRate.convertToUnderlying converts the prime cash value (principal) into its accrued value (principal + interest accrual.)
function convertToUnderlying(
PrimeRate memory pr,
int256 primeCashBalance
) internal pure returns (int256) {
int256 result = primeCashBalance.mul(pr.supplyFactor).div(Constants.DOUBLE_SCALAR_PRECISION);
return primeCashBalance < 0 ? SafeInt256.min(result, -1) : result;
}
We see above how primeRate.supplyFactor, which is an exchange rate that represents interest accrual for lending, is multiplied to the prime cash balance.
I think the details above show that the prime cash balance (balanceState.storedCashBalance) does not increase due to interest accrual. With the example above, it will stay at 975 even when interest accrues. This is because interest accruals are only applied to the Prime Cash factors and do not modify every account's cash balances, which would be expensive.
Am I missing anything @jeffywu?
jeffywu
Yeah, but 0.975 pETH units can be worth 1 ETH which is what is required for the vault to be solvent. If you call withdraw and expect to get 1 ETH, the vault will be able to satisfy the request and therefore it would be solvent.
gjaldon
I see. I think the user would expect interest accrual to make their 1 ETH larger than 1 ETH. Also, even though 0.975 pETH can become 1 ETH eventually due to variable interest accrual, there is still the 0.025 pETH that can not be redeemed. A Vault is insolvent when all of its shares can not be redeemed. When there are multiple depositors, this can lead to situations where the last depositor to withdraw will be unable to withdraw any assets.
jeffywu
Well the user could only expect up to 1 ETH because they have a claim on 1 fETH. They are not lending variable they are lending fixed
On Mon, Feb 5, 2024 at 4:06 PM G @.***> wrote:
I see. I think the user would expect interest accrual to make their 1 ETH larger than 1 ETH. Also, even though 0.975 pETH can become 1 ETH eventually due to variable interest accrual, there is still the 0.025 pETH that can not be redeemed. A Vault is insolvent when all of its shares can not be redeemed. When there are multiple depositors, this can lead to situations where the last depositor to withdraw will be unable to withdraw any assets.
— Reply to this email directly, view it on GitHub https://github.com/sherlock-audit/2023-12-notional-update-5-judging/issues/58#issuecomment-1928538045, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAHOUGSOW62TRY3ZX3MLMDTYSFXX5AVCNFSM6AAAAABCAQ65JCVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTSMRYGUZTQMBUGU . You are receiving this because you were mentioned.Message ID: <sherlock-audit/2023-12-notional-update-5-judging/issues/58/1928538045@ github.com>
gjaldon
Fair point. I think you are looking at it from the perspective of calling Vault.withdraw() where the user sets the amount of assets to be withdrawn. When using Vault.redeem(), the user sets the amount of shares to be redeemed. In this case, the amount to be redeemed would be the total shares of 1000. This is what I mean by insolvency, since only up to 975 shares can be redeemed and not all the shares are redeemable.
Thank you for the time and the discussion @jeffywu. I appreciate it and will do my best to carry over all my learnings to future Notional contests.
Evert0x
Planning to reject escalation and keep issue state as is @gjaldon
Evert0x
Result: Medium Unique
sherlock-admin2
Escalations have been resolved successfully!
Escalation status:
- JEFFCX: rejected
sherlock-admin
The protocol team fixed this issue in PR/commit https://github.com/notional-finance/wrapped-fcash/pull/17.
sherlock-admin
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
Likely the vault calculates the number of shares to mint from the nominal deposit amount rather than the actual amount received after the token's transfer fee.
Impact
The vault becomes insolvent and cannot buy back all shares; the last depositors to redeem bear the loss, though the protocol team notes variable interest may eventually recover the deficit.
What the attacker needed
Underlying asset is a fee-on-transfer token; vault lends at zero interest
The fix
The protocol team fixed this issue by not allowing tokens with a fee on transfer to perform lend at zero.
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 fee-on-transfer token takes a small cut each time it is moved. The vault gave the depositor a receipt for the full amount but only actually received the amount minus that cut. Later, when people try to cash in their receipts, there is not enough money to pay everyone.
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.