Skip to content
Smart contracts Audit finding Advanced

An Optimizer Bug in PositionManager.getPositionIndexesFiltered

The contract uses Solidity 0.8.14, which contains a known Yul optimizer bug that can remove memory writes in inline assembly. This could cause the array length to be set incorrectly, returning a wrong number of indexes.

sces60107 Follow

Originally disclosed on Code4rena

Submitted by sces60107

https://github.com/code-423n4/2023-05-ajna/blob/main/ajna-core/src/PositionManager.sol#L484 https://github.com/code-423n4/2023-05-ajna/blob/main/ajna-core/src/PositionManager.sol#L3

There is an optimizer bug in PositionManager.getPositionIndexesFiltered. https://blog.soliditylang.org/2022/06/15/inline-assembly-memory-side-effects-bug/

The Yul optimizer considers all memory writes in the outermost Yul block that are never read from as unused and removes them. The bug is fixed in solidity 0.8.15. But PositionManager.sol uses solidity 0.8.14.

Proof of Concept

There is an inline assembly block at the end of PositionManager.getPositionIndexesFiltered. The written memory is never read from in the same assembly block. It would trigger the bug to remove the memory write. https://github.com/code-423n4/2023-05-ajna/blob/main/ajna-core/src/PositionManager.sol#L484

    function getPositionIndexesFiltered(
        uint256 tokenId_
    ) external view override returns (uint256[] memory filteredIndexes_) {
        …

        // resize array
        assembly { mstore(filteredIndexes_, filteredIndexesLength) }
    }

Unfortunately, PositionManager uses solidity 0.8.14 which would suffer from the bug. https://github.com/code-423n4/2023-05-ajna/blob/main/ajna-core/src/PositionManager.sol#L3

pragma solidity 0.8.14;

Recommended Mitigation Steps

Update the solidity version to 0.8.15

MikeHathaway (Ajna) confirmed


The original report as published on Code4rena, by sces60107. 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 use of Solidity 0.8.14, which has a known Yul optimizer bug that removes memory writes in inline assembly when the written memory is not read within the same block.

Impact

The function could return an array with an incorrect length, potentially causing incorrect results for callers.

What the attacker needed

None

The fix

The Solidity version should be updated to 0.8.15 or later.

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.

Solidity is a programming language for smart contracts. An optimizer is a tool that makes code smaller and cheaper to run, but sometimes it has bugs. Here, the optimizer might delete a line that sets the size of a list, so the list could come back with the wrong size.

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 sces60107

View all

3 more disclosed reports, best first.

More from Ajna Protocol

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.