Skip to content
Smart contracts Audit finding Intermediate

Bypass zap timelock

The timelock mint function overwrites an existing longer timelock with a shorter one because it always sets the finish time to the current block timestamp plus the new timelock length. An attacker could reduce a seven-day timelock to two milliseconds by calling deposit in NFTXInventoryStaking.

gzeon Follow

Originally disclosed on Code4rena

Submitted by gzeon

The default value of inventoryLockTime in NFTXStakingZap is 7 days while DEFAULT_LOCKTIME in NFTXInventoryStaking is 2 ms. These timelock value are used in NFTXInventoryStaking to eventually call _timelockMint in XTokenUpgradeable.

https://github.com/code-423n4/2021-12-nftx/blob/194073f750b7e2c9a886ece34b6382b4f1355f36/nftx-protocol-v2/contracts/solidity/token/XTokenUpgradeable.sol#L74

function _timelockMint(address account, uint256 amount, uint256 timelockLength) internal virtual {
  uint256 timelockFinish = block.timestamp + timelockLength;
  timelock[account] = timelockFinish;
  emit Timelocked(account, timelockFinish);
  _mint(account, amount);
}

The applicable timelock is calculated by block.timestamp + timelockLength, even when the existing timelock is further in the future. Therefore, one can reduce their long (e.g. 7 days) timelock to 2 ms calling deposit in NFTXInventoryStaking

Proof of Concept

https://github.com/code-423n4/2021-12-nftx/blob/194073f750b7e2c9a886ece34b6382b4f1355f36/nftx-protocol-v2/contracts/solidity/NFTXStakingZap.sol#L160 https://github.com/code-423n4/2021-12-nftx/blob/194073f750b7e2c9a886ece34b6382b4f1355f36/nftx-protocol-v2/contracts/solidity/NFTXInventoryStaking.sol#L30

Recommended Mitigation Steps

function _timelockMint(address account, uint256 amount, uint256 timelockLength) internal virtual {
  uint256 timelockFinish = block.timestamp + timelockLength;
  if(timelockFinish > timelock[account]){
    timelock[account] = timelockFinish;
    emit Timelocked(account, timelockFinish);
  }
  _mint(account, amount);
}

0xKiwi (NFTX) disputed

0xKiwi (NFTX) confirmed and commented:

After taking another look, this is definitely accurate. Thank you!

0xKiwi (NFTX) resolved


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

The _timelockMint function unconditionally sets the timelock to block.timestamp + timelockLength, overwriting any existing longer timelock instead of keeping the later of the two.

Impact

An attacker could bypass the intended long timelock and reduce it to 2 ms, allowing immediate withdrawal or transfer of tokens that should have been locked.

What the attacker needed

A position with a long timelock; ability to call deposit

The fix

The _timelockMint function should only update the timelock if the new finish time is later than the existing one, as shown in the recommended mitigation.

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 timelock is a waiting period before you can withdraw or transfer tokens. The code that sets the waiting period always replaces the old end time with a new one, even if the new one is sooner. So someone can make their own waiting period much shorter by making a small deposit.

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 gzeon

View all

46 more disclosed reports, best first.

More from NFTX 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.