Open c4-submissions opened 1 year ago
minhquanym marked the issue as duplicate of #227
MarioPoneder marked the issue as satisfactory
Selected for report due to overall discussion, PoC and mitigation
MarioPoneder marked the issue as selected for report
Lines of code
https://github.com/code-423n4/2023-11-canto/blob/335930cd53cf9a137504a57f1215be52c6d67cb3/asD/src/asD.sol#L72-L90
Vulnerability details
Impact
The current
withdrawCarry
function in the contract underestimates the accrued interest due to a miscalculation. This error prevents the rightful owner from withdrawing their accrued interest, effectively locking the assets. The primary issue lies in the calculation ofmaximumWithdrawable
withinwithdrawCarry
.Flawed Calculation:
The following code segment is used to determine
maximumWithdrawable
:The problem arises with the scaling of
exchangeRate
, which is assumed to be scaled by10^28
. However, forCNOTE
in the Canto network, theexchangeRate
is actually scaled by10^18
. This discrepancy causes the owner to withdraw only 10^(-10) times the actual interest amount. Consequently, when a non-zero_amount
is specified,withdrawCarry
often fails due to therequire(_amount <= maximumWithdrawable, "Too many tokens requested");
condition.Proof of Concept
CNOTE Scaling Verification
An essential aspect of this audit involves verifying the scaling factor of the
CNOTE
exchange rate. TheexchangeRate
scale forCNOTE
can be verified in the Canto Network's GitHub repository. Evidence confirming that the exponent of theCNOTE
exchange rate is indeed18
can be found through this link to the token tracker. The data provided there shows the current value of the stored exchange rate (exchangeRateStored) as approximately1004161485744613000
. This value corresponds to1.00416 * 1e18
, reaffirming the 10^18 scaling factor.This information is critical for accurately understanding the mechanics of CNOTE and ensuring that the smart contract's calculations align with the actual scaling used in the token's implementation. The verification of this scaling factor supports the recommendations for adjusting the main contract's calculations and the associated test cases, as previously outlined.
Testing with Solidity Codes
Testing with the following Solidity code illustrates the actual
CNOTE
values:The corresponding TypeScript logs show a clear discrepancy between the expected and calculated underlying balances,
with the logs
Tools Used
Recommended Mitigation Steps
Using
balanceOfUnderlying
FunctionReplace the flawed calculation with the
balanceOfUnderlying
function. This function accurately calculates the underlyingNOTE
balance and is defined inCToken.sol
(source).Proposed Code Modifications: Two alternative implementations are suggested:
Without
balanceOfUnderlying
: Modify the scaling factor in the existing calculation from1e28
to1e18
.With
balanceOfUnderlying
(Recommended): Utilize thebalanceOfUnderlying
function for a simpler calculation ofmaximumWithdrawable
.The second option is highly recommended for its accuracy and simplicity.
Modification of Related Test Codes
Post-modifications to the main contract code, it's essential to update the associated test cases. In the
MockCNOTE
test contract, all scaling is currently set to10^28
. To align with the main contract changes, the following modifications are recommended forMockCNOTE
:This revised
MockCNOTE
contract reflects the updated scale factor and aligns with the main contract's logic.Scenario Testing with Mainnet Forking
For comprehensive validation, scenario testing using a fork of the mainnet is highly recommended. This approach allows for real-world testing conditions by simulating interactions with existing contracts on the mainnet. It provides a robust environment to verify the correctness and reliability of the contract modifications in real-world scenarios, ensuring that the contract behaves as expected when interfacing with other mainnet contracts.
This step is crucial to identify potential issues that might not be apparent in isolated or simulated environments, enhancing the overall reliability of the contract before deployment.
Assessed type
Math