Multiple functions may leave excess funds in the contract that should be returned
Several periphery functions move the full specified amount of a sell token into the contract but do not guarantee the swap quote consumes all of it, so leftover tokens can remain stuck in the contract. An attacker or unlucky user could lose those excess funds instead of having them returned to the receiver.
0x52 Follow with Bauer and spyrosonic10
Originally disclosed on Sherlock
Source: https://github.com/sherlock-audit/2023-03-sense-judging/issues/29
Found by
0x52, Bauer, spyrosonic10
Summary
Periphery#combine may leave excess underlying in the contract due to _fromTarget unwrapping to underlying and the quote may not swap them all.
When using arbitrary tokens to swap to underlying the contract always moves in the full amount specified. There is no guarantee that the quote will consume all tokens. As a result the contract may leave excess sell tokens in the contract but it should return then to the receiver.
These functions include:
RollerPeriphery
- deposit
Periphery
- swapForPTs
- addLiquidity
- issue
RollerPeriphery#RollermintFromUnderlying uses adapter.scale and previewMint to determine the amount of underlying to transfer. The roller code will mean that previewMint will always perfectly reflect the exact exchange rate into the roller. However adapter.scale varies by adapter and isn't guaranteed to be exact. The result is that _transferFrom may take too much underlying. Since this underlying is wrapped to target the contract should return all excess target to receiver.
Vulnerability Detail
See summary.
Impact
Token may be left in the contract and lost
Code Snippet
https://github.com/sherlock-audit/2023-03-sense/blob/main/auto-roller/src/RollerPeriphery.sol#L196
https://github.com/sherlock-audit/2023-03-sense/blob/main/sense-v1/pkg/core/src/Periphery.sol#L178
https://github.com/sherlock-audit/2023-03-sense/blob/main/sense-v1/pkg/core/src/Periphery.sol#L325
https://github.com/sherlock-audit/2023-03-sense/blob/main/sense-v1/pkg/core/src/Periphery.sol#L409
Tool used
Manual Review
Recommendation
Return excess tokens at the end of the function
Discussion
jparklev
We have this feature, for example, on _swapSenseToken, but not on the cases mentioned.
Our fix will be: Transfer non-used tokens back to the user.
jparklev
Fixed here for sense-v1: https://github.com/sense-finance/sense-v1/pull/346 And here for the auto-roller: https://github.com/sense-finance/auto-roller/pull/32
We returned excess sellTokens funds in the from and to target functions
IAm0x52
Fixes look good. Excess sell tokens are now returned inside _toTarget and _fromTarget for both periphery and rollerPeriphery
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 functions transfer the full requested amount of sell tokens but never return the unswapped remainder to the receiver.
Impact
Excess tokens may be left in the contract and lost, though the text does not show an attacker actively triggering it.
What the attacker needed
User calls an affected function; swap quote uses less than sent
The fix
The project fixed it by returning unused sell tokens to the user inside the from and to target functions.
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 through a contract, you usually send in an amount and get something back. If the swap uses less than you sent, the leftover should come back to you. Here the contract kept the leftover, so those tokens could be lost.
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.