Skip to content
Smart contracts Audit finding Intermediate

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.

kirk-baird Follow

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


The original report as published on Code4rena, by kirk-baird. 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.

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.

More by kirk-baird

View all

32 more disclosed reports, best first.

More from Illuminate contest

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Smart contract vulnerabilities

The lesson behind this report.

Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.