`JUSDBankStorage::getTRate()`,`JUSDBankStorage::accrueRate()` are calculated differently, and the data calculation is biased, Causes the `JUSDBank` contract funciton result to be incorrect
The functions getTRate() and accrueRate() in JUSDBankStorage calculate the interest rate differently, leading to inconsistent results. This causes functions like _isAccountSafe(), flashLoan(), and _handleBadDebt to produce biased outcomes.
Source: https://github.com/sherlock-audit/2023-12-jojo-exchange-update-judging/issues/1
Found by
FastTiger, T1MOH, bitsurfer, dany.armstrong90, detectiveking, joicygiore, rvierdiiev
Summary
function accrueRate() public {
uint256 currentTimestamp = block.timestamp;
if (currentTimestamp == lastUpdateTimestamp) {
return;
}
uint256 timeDifference = block.timestamp - uint256(lastUpdateTimestamp);
@> tRate = tRate.decimalMul((timeDifference * borrowFeeRate) / Types.SECONDS_PER_YEAR + 1e18);
lastUpdateTimestamp = currentTimestamp;
}
function getTRate() public view returns (uint256) {
uint256 timeDifference = block.timestamp - uint256(lastUpdateTimestamp);
@> return tRate + (borrowFeeRate * timeDifference) / Types.SECONDS_PER_YEAR;
}
JUSDBankStorage::getTRate(),JUSDBankStorage::accrueRate() are calculated differently, and the data calculation is biased, resulting in the JUSDBank contract not being executed correctly
Vulnerability Detail
The wrong result causes the funciton calculation results of JUSDBank::_isAccountSafe(), JUSDBank::flashLoan(), JUSDBank::_handleBadDebt, etc. to be biased,and all functions that call the relevant function will be biased
Impact
Causes the JUSDBank contract funciton result to be incorrect
Code Snippet
Tool used
Manual Review
POC
Please add the test code to JUSDViewTest.t.sol for execution
function testTRateDeviation() public {
console.log(block.timestamp);
console.log(jusdBank.lastUpdateTimestamp());
vm.warp(block.timestamp + 18_356 days);
jusdBank.accrueRate();
console.log("tRate value than 2e18:", jusdBank.tRate());
// block.timestamp for every 1 increment
vm.warp(block.timestamp + 1);
uint256 getTRateNum = jusdBank.getTRate();
jusdBank.accrueRate();
uint256 tRateNum = jusdBank.tRate();
console.log("block.timestamp for every 1 increment, deviation:", tRateNum - getTRateNum);
// block.timestamp for every 1 days increment
vm.warp(block.timestamp + 1 days);
getTRateNum = jusdBank.getTRate();
jusdBank.accrueRate();
tRateNum = jusdBank.tRate();
console.log("block.timestamp for every 1 days increment, deviation:", tRateNum - getTRateNum);
}
Recommendation
Use the same calculation formula:
function accrueRate() public {
uint256 currentTimestamp = block.timestamp;
if (currentTimestamp == lastUpdateTimestamp) {
return;
}
uint256 timeDifference = block.timestamp - uint256(lastUpdateTimestamp);
tRate = tRate.decimalMul((timeDifference * borrowFeeRate) / Types.SECONDS_PER_YEAR + 1e18);
lastUpdateTimestamp = currentTimestamp;
}
function getTRate() public view returns (uint256) {
uint256 timeDifference = block.timestamp - uint256(lastUpdateTimestamp);
- return tRate + (borrowFeeRate * timeDifference) / Types.SECONDS_PER_YEAR;
+ return tRate.decimalMul((timeDifference * borrowFeeRate) / Types.SECONDS_PER_YEAR + 1e18);
}
Discussion
sherlock-admin2
1 comment(s) were left on this issue during the judging contest.
takarez commented:
valid because { This is also valid and a dupp of 016}
JoscelynFarr
After internal discussion, we decide to accrue rate in the view which is getTRate() function
JoscelynFarr
Fixed PR: https://github.com/JOJOexchange/smart-contract-EVM/commit/4b591c9fb0a232f784919905752c8e68d32b39ff
IAm0x52
Fix looks good. Math has been updated to use full precision
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
Impact
The incorrect rate calculation could cause the contract to misjudge account safety, leading to incorrect liquidation decisions or flash loan outcomes.
What the attacker needed
None
The fix
The fix was to update getTRate() to use the same decimalMul formula as accrueRate(), ensuring consistent calculation.
How it was found
- Deploy the JUSDBank contract and note the initial lastUpdateTimestamp.
- Advance the block timestamp by a large amount (e.g., 18,356 days) and call accrueRate().
- Advance the timestamp by 1 second, then compare getTRate() with the result of accrueRate().
- Advance the timestamp by 1 day, then compare getTRate() with the result of accrueRate().
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 lending contract, the interest rate must be calculated the same way whether you are just viewing it or actually updating it. Here, two functions used different formulas, so the displayed rate and the stored rate could disagree. This could make the contract think an account is safe when it is not, or vice versa.
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.