Swivel: implementation for initiateZcTokenFillingZcTokenExit is incorrect
The function initiateZcTokenFillingZcTokenExit incorrectly transfers the fee to the maker instead of to Swivel, and also transfers the wrong amount of underlying tokens. An attacker could exploit this to avoid paying fees or cause loss of funds.
*Submitted by itsmeSTYJ*.
#### Impact
In `initiateZcTokenFillingZcTokenExit()` , this comment `// transfer underlying tokens - the premium paid + fee in underlying to swivel (from sender)` is incorrect because you are actually transferring the underlying tokens - premium paid to the maker (from sender) AND you have to pay fee separately to swivel.
`initiateZCTokenFillingZcTokenExit` means I want to sell my nTokens so that means `a` is the amount of principal I want to fill. Let’s use a hypothetical example where I (taker) wants to fill 10 units of ZcTokenExit for maker.
1. I transfer 10 units of underlying to Swivel. The net balances are: me (-a), swivel (+a)
2. I transfer fee (in underlying) to Swivel. The net balances are: me (-a-fee), swivel (+a+fee)
3. Swivel initiates my position, sends me the ZcToken and sends Maker the nTokens
4. Maker pays me premiumFilled for the nTokens. The net balances are: me (-a-fee+premiumsFilled), swivel (+a+fee), maker (-premiumsFilled)
5. Maker closes position. The net balances are: me (-a-fee+premiumsFilled), swivel (+fee), maker (-premiumsFilled+a)
So effectively, I (taker) should be paying a-premium to maker and fee to swivel.
#### Recommended Mitigation Steps
```
function initiateZcTokenFillingZcTokenExit(Hash.Order calldata o, uint256 a, Sig.Components calldata c) internal {
bytes32 hash = validOrderHash(o, c);
require(a <= o.principal - filled[hash]), 'taker amount > available volume'); // Note: you don't need to wrap these in brackets because if you look at the https://docs.soliditylang.org/en/latest/cheatsheet.html#order-of-precedence-of-operators, subtraction will always go before comparison
filled[hash] += a;
uint256 premiumFilled = (((a * 1e18) / o.principal) * o.premium) / 1e18;
uint256 fee = ((premiumFilled * 1e18) / fenominator[0]) / 1e18;
// transfer underlying tokens - the premium paid in underlying to maker (from sender)
Erc20(o.underlying).transferFrom(msg.sender, o.maker, a - premiumFilled);
Erc20(o.underlying).transferFrom(msg.sender, swivel, fee);
// transfer <a> zcTokens between users in marketplace
require(MarketPlace(marketPlace).p2pZcTokenExchange(o.underlying, o.maturity, o.maker, msg.sender, a), 'zcToken exchange failed');
emit Initiate(o.key, hash, o.maker, o.vault, o.exit, msg.sender, a, premiumFilled);
}
```
**[JTraversa (Swivel) confirmed](https://github.com/code-423n4/2021-09-swivel-findings/issues/38#issuecomment-937980208):**
> Really good eye.
>
>
>
> We had `o.maker` send the fee after receiving it (same # of transfers as suggested mitigation) and actually accidentally lost that in a larger organizational commit �
.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 transfer logic incorrectly sends the fee to the maker instead of Swivel and uses the wrong amount (a - premiumFilled instead of a - premiumFilled for maker and fee separately).
Impact
The taker could avoid paying the fee to Swivel, and the maker could receive more than intended, leading to loss of funds for the protocol.
What the attacker needed
Taker has underlying tokens; maker has an order
The fix
The function should transfer a - premiumFilled to the maker and the fee separately to Swivel, as shown in the recommended mitigation.
How it was found
- Taker transfers 10 units of underlying to Swivel.
- Taker transfers fee (in underlying) to Swivel.
- Swivel initiates the position, sends ZcToken to taker and nTokens to maker.
- Maker pays taker premiumFilled for the nTokens.
- Maker closes position.
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.
This is a smart contract bug where a function that handles token transfers sends the fee to the wrong place. Instead of sending the fee to the platform (Swivel), it sends it to the maker, and the amount transferred is also wrong. This could let someone avoid paying fees or cause funds to 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.