Division Before Multiplication Can Lead To Zero Rounding Of Return Amount
In the Swivel branch of the lend function, the return amount is computed by dividing the premium by the principal before multiplying by the lent amount, so integer rounding can make the result zero. An attacker could then have their underlying tokens transferred in while nothing is passed on to yield, effectively losing the user's funds.
Originally disclosed on Code4rena
Submitted by kirk-baird, also found by csanuragjain, datapunk, and ladboy233
There is a division before multiplication bug that exists in lend() for the Swivel case.
If order.premium is less than order.principal then returned will round to zero due to the integer rounding.
When this occurs the user’s funds are essentially lost. That is because they transfer in the underlying tokens but the amount sent to yield(u, y, returned, address(this)) will be zero.
Proof of Concept
function lend(
uint8 p,
address u,
uint256 m,
uint256[] calldata a,
address y,
Swivel.Order[] calldata o,
Swivel.Components[] calldata s
) public unpaused(p) returns (uint256) {
// lent represents the number of underlying tokens lent
uint256 lent;
// returned represents the number of underlying tokens to lend to yield
uint256 returned;
{
uint256 totalFee;
// iterate through each order a calculate the total lent and returned
for (uint256 i = 0; i < o.length; ) {
Swivel.Order memory order = o[i];
// Require the Swivel order provided matches the underlying and maturity market provided
if (order.underlying != u) {
revert NotEqual('underlying');
} else if (order.maturity > m) {
revert NotEqual('maturity');
}
// Determine the fee
uint256 fee = calculateFee(a[i]);
// Track accumulated fees
totalFee += fee;
// Sum the total amount lent to Swivel (amount of ERC5095 tokens to mint) minus fees
lent += a[i] - fee;
// Sum the total amount of premium paid from Swivel (amount of underlying to lend to yield)
returned += (a[i] - fee) * (order.premium / order.principal);
unchecked {
i++;
}
}
// Track accumulated fee
fees[u] += totalFee;
// transfer underlying tokens from user to illuminate
Safe.transferFrom(IERC20(u), msg.sender, address(this), lent);
// fill the orders on swivel protocol
ISwivel(swivelAddr).initiate(o, a, s);
yield(u, y, returned, address(this));
}
emit Lend(p, u, m, lent);
return lent;
}
Specifically the function returned += (a[i] - fee) * (order.premium / order.principal);
Recommended Mitigation Steps
The multiplication should occur before division, that is ((a[i] - fee) * order.premium) / order.principal);.
JTraversa (Illuminate) confirmed
Alex the Entreprenerd (warden) commented:
Also see how Swivel Calculates it
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 arithmetic order is wrong: the premium is divided by the principal before being multiplied by the lent amount, so integer truncation can zero out the result.
Impact
When the premium is smaller than the principal, the returned amount rounds to zero, so the user's underlying tokens are transferred in but nothing is sent to yield, meaning the funds are effectively lost.
What the attacker needed
Premium smaller than principal; user calls lend with Swivel orders
The fix
The multiplication should be performed before the division so the intermediate value is not truncated to zero.
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.
Smart contracts handle money using whole numbers only, so any division throws away the remainder. If you divide a small number by a bigger one, the result becomes zero, and multiplying zero by anything stays zero. Here the code divided first and multiplied later, so the amount forwarded to the yield step could silently become nothing even though the user's tokens were already taken.
What is Smart contracts?
Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.