D3UserQuote#getUserQuote queries incorrect token for exchangeRate leading to inaccurate quota calculations
A typo in the quota calculation loop of D3UserQuote#getUserQuote uses the wrong variable when fetching the exchange rate, leading to an incorrect quota being returned. This can dramatically underestimate the quota, allowing oversized and overly risky positions that could cause loss to the system.
0x4db5362c Follow with 0x52 and dirk_y
Originally disclosed on Sherlock
Source: https://github.com/sherlock-audit/2023-06-dodo-judging/issues/193
Found by
0x4db5362c, 0x52, dirk_y
Summary
A small typo in the valuation loop of D3UserQuote#getUserQuote uses the wrong variable leading to and incorrect quota being returned. The purpose of a quota is to mitigate risk of positions being too large. This incorrect assumption can dramatically underestimate the quota leading to oversized (and overrisk) positions.
Vulnerability Detail
for (uint256 i = 0; i < tokenList.length; i++) {
address _token = tokenList[i];
(address assetDToken,,,,,,,,,,) = d3Vault.getAssetInfo(_token);
uint256 tokenBalance = IERC20(assetDToken).balanceOf(user);
if (tokenBalance > 0) {
tokenBalance = tokenBalance.mul(d3Vault.getExchangeRate(token)); <- @audit-issue queries token instead of _token
(uint256 tokenPrice, uint8 priceDecimal) = ID3Oracle(d3Vault._ORACLE_()).getOriginalPrice(_token);
usedQuota = usedQuota + tokenBalance * tokenPrice / 10 ** (priceDecimal+tokenDecimals);
}
}
D3UserQuota.sol#L80 incorrectly uses token rather than _token as it should. This returns the wrong exchange rate which can dramatically alter the perceived token balance as well as the calculated quota.
Impact
Quota is calculated incorrectly leading to overly risky positions, which in turn can cause loss to the system
Code Snippet
Tool used
Manual Review
Recommendation
Change variable from token to _token:
- tokenBalance = tokenBalance.mul(d3Vault.getExchangeRate(token));
+ tokenBalance = tokenBalance.mul(d3Vault.getExchangeRate(_token));
Discussion
traceurl
We redesigned D3UserQuota.
In the old version:
- used quota is calculated based on the USD value of the deposited token
- global quota is shared by all tokens
In this new version:
- used quota is the amount of the deposited token, so price change won't affect quota
- each token has its own global quota
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 a typographical error where the variable 'token' is used instead of '_token' when calling getExchangeRate inside the loop.
Impact
Quota is calculated incorrectly leading to overly risky positions, which in turn can cause loss to the system.
What the attacker needed
None
The fix
The variable should be changed from token to _token in the getExchangeRate call.
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 smart contract calculates how much of a token a user has deposited and converts it to a common value using an exchange rate. Due to a typo, it looks up the exchange rate for the wrong token, so the final quota (a limit on position size) is wrong. This could let someone take a much larger position than intended, putting the system at risk.
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.