Attackers will steal the reserve from the `Vault` by receiving `ra` in `FlashSwapRouter::__swapDsforRa()`
The FlashSwapRouter contract incorrectly sends the Ra tokens generated from selling reserve to the caller instead of using them to provide liquidity to the Vault. An attacker can repeatedly call swapRaforDs to drain the Vault's Ra reserves.
0x73696d616f Follow with KupiaSec, Smacaud and 1 other
Originally disclosed on Sherlock
Source: https://github.com/sherlock-audit/2024-08-cork-protocol-judging/issues/161
Found by
0x73696d616f, KupiaSec, Smacaud, oxelmiguel
Summary
FlashSwapRouter::__swapDsforRa() is called as part of FlashSwapRouter::_swapRaforDs() whenever the reserve is sold and the resulting Ra is used to provide liquidity to the Vault by calling Vault::provideLiquidityWithFlashSwapFee().
However, if we follow the code, FlashSwapRouter::__swapDsforRa() calls FlashSwapRouter::__flashSwap(), which calls UniswapV2Pair::swap(). Then, the Uniswap pair calls uniswapV2Call(), which calls FlashSwapRouter::__afterFlashswapSell() and sends the Ra to the caller.
Thus, the vault is providing a fee, but does not use the acquired funds to fund it, but the already existing Ra in the contract. The Ra meant for the liquidity fee is sent to the user calling FlashSwapRouter::_swapRaforDs().
Root Cause
In FlashSwapRouter.sol:124, FlashSwapRouter::__swapDsforRa() is called, which ultimately sends the Ra to the msg.sender.
Internal pre-conditions
None.
External pre-conditions
None.
Attack Path
- User calls
swapRaforDs()and ends up receivingRawhich was intended for theVault.
Impact
Users can steal all reserve from the Vault.
PoC
The following code snippets show how the Ra ends up in the caller, that is, the user that calls swapRaforDs().
function __flashSwap(...) internal {
...
bytes memory data = abi.encode(reserveId, dsId, buyDs, msg.sender, extraData);
univ2Pair.swap(amount0out, amount1out, address(this), data);
}
function uniswapV2Call(address sender, uint256 amount0, uint256 amount1, bytes calldata data) external {
(Id reserveId, uint256 dsId, bool buyDs, address caller, uint256 extraData) =
abi.decode(data, (Id, uint256, bool, address, uint256));
...
if (buyDs) {
...
} else {
uint256 amount = amount0 == 0 ? amount1 : amount0;
__afterFlashswapSell(self, amount, reserveId, dsId, caller, extraData);
}
}
function __afterFlashswapSell(...) internal {
...
IERC20(ra).safeTransfer(caller, raAttributed);
...
}
Mitigation
The FlashSwapRouter::__flashSwap() has to be modified to accept a caller argument, where it accepts either the msg.sender or owner(). In the flow of selling the reserve, it should send the Ra to the owner, which is the Vault.
Discussion
ziankork
This is a valid issue, though already fixed this prior to our trading competition. will provide links later
SakshamGuruji3
Escalate
According to README this should be a low ->
Any potential misbehaviour regarding DS/CT/FlashSwap mechanism that is not an issue right now but could pose risk in future integrations should be counted, but reported as Low issues.
sherlock-admin3
Escalate
According to README this should be a low ->
Any potential misbehaviour regarding DS/CT/FlashSwap mechanism that is not an issue right now but could pose risk in future integrations should be counted, but reported as Low issues.
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.
0xsimao
that is not an issue right now
This is an issue right now, it does not apply to this.
SakshamGuruji3
I think there is a confusion due to english grammar , pretty sure the sponsor meant that it is not an issue right now , let the sponsor clear on his intent by what he meant
0xsimao
There is nothing to clear, the statement could not be more obvious, they are talking about future issues, as in, 'that is not an issue right now'. Present issues are fine to report.
SakshamGuruji3
@0xsimao If you see my original escalation comment you can see the sponsor has given a thumbs - up reaction , I think it is indeed what he meant and escalation should be valid , athough it will be ideal if he can confirm with a comment
0xsimao
Does not matter, the readme is the source of truth. It's clear only future issues are out of scope. This is in the rules.
cvetanovv
@0xsimao is right here. Readme is the source of truth. There is no way to take into account that someone got the grammar wrong long after the contest is over.
Pheonix244001
@cvetanovv I think there are two ways to interpret what’s written in the readme and @SakshamGuruji3 might be right here because the meaning seems to be that issues regarding flashswaprouter will only be issues in future integrations hence everything regarding those is a low, its not justified to incline over one side without confirming what the sponsor actually meant (the words in the readme can be twisted both ways)
0xsimao
@Pheonix244001 firstly, that is incorrect, the readme is very clear. It means issues that could go wrong in the future, but are correct now.
Secondly, look at the question itself:
Should potential issues, like broken assumptions about function behavior, be reported if they could pose risks in future integrations, even if they might not be an issue in the context of the scope? If yes, can you elaborate on properties/invariants that should hold?
It is asking about issues that are not a problem right now, but could be in the future. The sponsor responds that these issues can be reported, but they are low.
The issue here is a present issue, so it does not apply here.
If the sponsor wanted to exclude the FlashSwapRouter as out of scope, that was not the section to do so.
sherlock-admin2
The protocol team fixed this issue in the following PRs/commits: https://github.com/Cork-Technology/Depeg-swap/pull/63
WangSecurity
I agree with @cvetanovv and @0xsimao and the README is clear that the issues related to DS/CT/FlashSwap mechanism should be low only if they pose issues in the future integration. Here, the issue poses the losses right now, without the Future integrations. Planning to reject the escalation and leave the issue as it is.
WangSecurity
Result: High Has duplicates
sherlock-admin4
Escalations have been resolved successfully!
Escalation status:
- SakshamGuruji3: rejected
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 function __swapDsforRa passes the original caller's address through the flash swap and transfers the Ra output to that caller instead of to the Vault owner.
Impact
Users can steal all reserve from the Vault.
What the attacker needed
None
The fix
The FlashSwapRouter::__flashSwap() should be modified to accept a caller argument, allowing it to send the Ra to the owner (the Vault) when selling the reserve.
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.
When you swap tokens, the system is supposed to use the proceeds to add liquidity to a pool. Instead, it gives those proceeds directly to you, while still taking the fee from the pool. This means you can keep swapping and collect the pool's tokens without the pool getting the intended funds, eventually emptying it.
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.