Unsafe downcasting in issue(...) can be exploited to cause permanent DoS
The issue function unsafely downcasts a user-supplied amount to uint192, so an attacker can pass a multiple of 2^192 to make the calculated basket amount zero and set the vesting end time to an extreme value, permanently preventing new issuances from vesting.
I first found this bug in issue(...), but unsafe downcasting appears in many other areas of the codebase, and seem to also be exploitable but no PoC is provided due to time constraints. Either way, using some form of safe casting library to replace all occurences of unsafe downcasting will prevent all the issues. I also do not list the individual instances of unsafe downcasting as all occurences should be replaced with safe cast.
Details
The amtRToken is a user supplied parameter in the issue(uint256 amtRToken) function
The calculated amount is unsafely downcasted into uint192.
This means that if the resulting calculation is a multiple of $2^{192}$, amtBaskets = 0
The code proceeds to the following line, where erc20s and deposits arrays will be empty since we are asking for a quote for 0. (see quote(...) in BasketHandler.sol where amounts are multiplied by zero)
This means an attacker can call issue(...) with a very high amtRToken amount that is a multiple of $2^{192}$, without depositing any amount of collateral.
The DoS issues arises because whenFinished(uint256 amtRToken) is dependent on amtRToken. With such a high value, allVestAt will be set so far in the future that it causes a permanent DoS. i.e. Issuances will never vest.
This PoC demonstrates that an attacker can call issue(...) without collateral tokens to modify allVestAt variable to an extreme value, such that all further issuances cannot be vested for all users.
Do note that the PoC is done with totalSupply() == 0 case, so we supply amtRToken as a multiple of $2^{192}$. Even if there is an existing totalSupply(), we just need to calculate a value for amtRToken >= 2^192 such that $\frac{\text{basketsNeeded} \times \text{amtRToken}}{totalSupply()} = 0$. This attack does not require totalSupply() be zero.
Permanent DoS would be High risk considering RToken is an asset-backed currency.
A currency that is unable to issue new currency does not work as a currency
Also, I believe existing collateral cannot be redeemed due to the extreme values also used in redeem(...) function. No PoC written due to time constriant for this case… but above should be enough impact.
Many other downcasting issues for this project. But using a safe casting library would prevent all the issues… not going to write multiple reports for same underlying issue.
Recommendations
Use some safe casting library. OpenZeppelin’s library does not have safe casting for uint192 type. May have to find another or write your own.
Will leave open for sponsor review, I think Medium severity is correct if the finding turns out to be fully valid. If no more issuance can occur, redemption is still possible. Warden would have needed to demonstrate a loss of funds for this to qualify as H severity.
I don’t see how documentation prevents this issue.
The issue exists because downcasting values above 2^192 does not revert. Maybe the sponsor misunderstood the issue thinking that it would require the attacker to deposit 2^192 of the collateral in order for the attack to succeed which is an extremely unlikely scenario.
Updated the PoC to clearly show that the attacker can permanently disable the issue(...) function for the protocol, without owning any amount of the basket token. - addr1 is the attacker with 0 basket tokens, addr2 represents all future users who will not be able to issue new RTokens.
it('Audit: DoS by downcasting', async function () {
const issueAmount: BigNumber = BigNumber.from(2n ** 192n)
await token0.burn(addr1.address, bn('6.3e57'))
await token0.burn(addr2.address, bn('6.3e57'))
// await token0.mint(addr1.address, bn('10e18'))
await token0.mint(addr2.address, bn('10e18'))
expect(await token0.balanceOf(addr1.address)).to.eq(0)
expect(await token0.balanceOf(addr2.address)).to.eq(bn('10e18'))
// Set basket
await basketHandler.connect(owner).setPrimeBasket([token0.address], [fp('1')])
await basketHandler.connect(owner).refreshBasket()
// Attacker issues 2 ** 192, or a multiple of 2 ** 192 RTokens
// This will cause allVestAt to be very high, permanent DoS
const tx = await rToken.connect(addr1)['issue(uint256)'](issueAmount)
const receipt = await tx.wait()
console.log(receipt.events[0].args)
await token0.connect(addr2).approve(rToken.address, bn('10e18'))
const tx2 = await rToken.connect(addr2)['issue(uint256)'](bn('10e18'))
const receipt2 = await tx2.wait()
console.log(receipt2.events[0].args)
// one eternity later...
await advanceTime('123456789123456789')
// and still not ready
await expect(rToken.connect(addr2).vest(addr2.address, 1))
.to.be.revertedWith("issuance not ready")
})
Additionally, I still believe this issue should be considered High risk as:
Disabling of critical function of the protocol
Attack is very simple to exploit, with no cost to the attacker - Low complexity with High likelihood
Permanent disabling of RToken issuance means that the RToken can no longer be used so all funds must be moved out, this will entail:
Redeeming all existing RTokens, which will take a reasonable amount of time depending on redemption battery parameters
Unstaking all stRSR which will take a reasonable amount of time depending on unstaking delay
Gas costs for all the above redeeming and unstaking will be in the thousands for a RToken with reasonable market cap.
RToken is a stable currency which means that it would be used in DeFi protocols. In the case of Lending/Borrowing, it would take even longer for RToken to be redeemed. There may also be loss of funds as a long wait time to redeem RTokens means that the RToken will trade at a discount in secondary markets - this can cause RToken-collateralized loans to be underwater.
There is no direct loss of funds but I’d argue the impact is vast due to RToken being used as a currency.
I agree with Medium if following C4 criteria in the docs exactly word for word.
It is just that there are many High findings in previous contests where High findings did not need to cause direct loss of funds, but break an important functionality in the protocol.
To be clear, this issue does lead to loss of funds. It is just that it may not be considered direct.
It is indeed my opinion that the finding should be High, but the points listed below are all facts. I will respect your decision regardless. Thanks!
Apologies, I misunderstood the issue the first time I read through it…indeed this can be used to mint large amounts of RToken to yourself while putting down very little in collateral, while pushing allVestAt extremely far into the future.
Since issuanceRate cannot be disabled, and cannot be above 100%, there is no way for the absurdly high RToken mint to finish vesting. In the event of the attack, RToken issuance would be bricked but redemption would remain enabled, and since no RToken is minted until vesting the redemptions would still function. I think this is a Medium.
The original report as published on Code4rena, by Soosh. Images and videos are our own copies; raw HTML is shown as text. View it on Code4rena.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
Generating the AI write-up for this report.
This usually takes about 30 seconds. You can read the original meanwhile; this page updates itself.
Writing the technical write-up
Writing the version for beginners
Checking every passage against the original
How it worked
Root cause
Likely the code casts a user-controlled value to uint192 without checking that it fits, so values at or above 2^192 wrap to zero and corrupt the vesting schedule.
Impact
New RToken issuance would be permanently bricked, though redemption remains possible; the judge rated this Medium because there is no direct loss of funds.
What the attacker needed
Attacker needs no collateral; no special privileges
The fix
The project mitigated the issue by making all dangerous uint192 downcasts truncation-safe.
How it was found
Set the basket and refresh it.
Call issue with an amount that is a multiple of 2^192.
Observe that the vesting end time is set to an extremely large value.
Have another user issue a normal amount and try to vest after a long time; vesting fails with 'issuance not ready'.
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.
Generating the AI write-up for this report.
This usually takes about 30 seconds. You can read the original meanwhile; this page updates itself.
Writing the technical write-up
Writing the version for beginners
Checking every passage against the original
A smart contract stores numbers in fixed-size slots. If a larger number is squeezed into a smaller slot, the extra digits are silently dropped. Here, a huge user-supplied number was squeezed into a smaller type, became zero, and that zero was used to schedule a vesting time so far in the future that no new tokens could ever finish vesting.
What is DoS?
A request that exhausts resources or crashes a component. Many programs forbid testing it; reports usually show it from code, not by attacking.