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.
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
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.