Skip to content
Business logic Audit finding Intermediate

The Protocol breaks the Allowance Mechanism of the NFTs

The protocol transfers NFT positions to itself and back to the owner to execute actions, which clears any existing approvals for those tokens. As a result, users lose approvals they had granted to other entities, potentially leaving positions unattended.

Dup1337 Follow

Originally disclosed on Code4rena

Submitted by Dup1337

https://github.com/code-423n4/2024-06-krystal-defi/blob/f65b381b258290653fa638019a5a134c4ef90ba8/src/Common.sol#L392

https://github.com/code-423n4/2024-06-krystal-defi/blob/f65b381b258290653fa638019a5a134c4ef90ba8/src/V3Utils.sol#L76-L85

https://github.com/code-423n4/2024-06-krystal-defi/blob/f65b381b258290653fa638019a5a134c4ef90ba8/src/V3Utils.sol#L171

https://github.com/code-423n4/2024-06-krystal-defi/blob/f65b381b258290653fa638019a5a134c4ef90ba8/src/V3Automation.sol#L92

Impact

The user loses their approved entities

Proof of Concept

The protocol transfers the Position NFT from the position owner to the protocol and back to the position owner to execute the actions.

List of occurrences

Contract: Common.sol

402:         params.nfpm.transferFrom(address(this), params.recipient, result.tokenId);
Contract: V3Utils.sol

171:         nfpm.transferFrom(address(this), from, tokenId);
Contract: V3Utils.sol

76:     function execute(INonfungiblePositionManager _nfpm, uint256 tokenId, Instructions calldata instructions)  whenNotPaused() external
77:     {
78:         // must be approved beforehand
79:         _nfpm.safeTransferFrom(
80:             msg.sender,
81:             address(this),
82:             tokenId,
83:             abi.encode(instructions)
84:         );
85:     }
Contract: V3Automation.sol

92:         params.nfpm.transferFrom(positionOwner, address(this), params.tokenId);
Contract: V3Automation.sol

167:         params.nfpm.transferFrom(address(this), positionOwner, params.tokenId);

This breaks the allowance mechanism of ERC721 for every action taken on the platform and it omits the allowances given from the token owners to the approved entities.

For every NFT transfer, the allowances are zeroed out:

Contract: ERC721.sol

333:     function _transfer(address from, address to, uint256 tokenId) internal virtual {
334:         require(ERC721.ownerOf(tokenId) == from, "ERC721: transfer from incorrect owner");
335:         require(to != address(0), "ERC721: transfer to the zero address");
336:
337:         _beforeTokenTransfer(from, to, tokenId, 1);
338:
339:         // Check that tokenId was not transferred by `_beforeTokenTransfer` hook
340:         require(ERC721.ownerOf(tokenId) == from, "ERC721: transfer from incorrect owner");
341:
342:         // Clear approvals from the previous owner
343:   >>    delete _tokenApprovals[tokenId];
344:
345:         unchecked {
346:             // `_balances[from]` cannot overflow for the same reason as described in `_burn`:
347:             // `from`'s balance is the number of token held, which is at least one before the current
348:             // transfer.
349:             // `_balances[to]` could overflow in the conditions described in `_mint`. That would require
350:             // all 2**256 token ids to be minted, which in practice is impossible.
351:             _balances[from] -= 1;
352:             _balances[to] += 1;
353:         }
354:         _owners[tokenId] = to;
355:
356:         emit Transfer(from, to, tokenId);
357:
358:         _afterTokenTransfer(from, to, tokenId, 1);
359:     }

E.g.:

  1. Alice is on her flight to attend DSS Bangkok
  2. She already approved Bob to take care of her positions until she arrives during the long flight
  3. Bob interacts with Krystal to collect the LP fees.
  4. Bob loses his approval
  5. Alice´s position remains unattended and one of the pool tokens takes a nose dive before Bob can withdraw the liquidity and sell it.

Recommended Mitigation Steps

NFPM uses isAuthorizedForToken modifier as below:

Contract: NonfungiblePositionManager.sol

183:     modifier isAuthorizedForToken(uint256 tokenId) {
184:         require(_isApprovedOrOwner(msg.sender, tokenId), 'Not approved');
185:         _;
186:     }

Take approval of token owners rather than using onERC721Received hook.

Haupc (Krystal DeFi) acknowledged and commented:

There are 2 ways to take approval from user

  1. via approve function => this function also discards previous approval. So we can not keep other’s approval.
  2. via setApprovalForAll function => this function seems too risky for user to use

quanghuy219 (Krystal DeFi) commented:

ERC721 implementation allows only one spender on a token at a time, therefore taking user’s approval for our contract will also clear other approval on the same token

mapping(uint256 tokenId => address) private _tokenApprovals;

Another approach is to use setApprovalForAll function in ERC721, which poses another concern of allowing our contract to use all user’s positions.

With that in mind, we think that using onERC721Received hook is still the safest option for our users and Krystal is able to provide convenient user experience.


The original report as published on Code4rena, by Dup1337. 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 protocol transfers the NFT to itself and back, which triggers the ERC721 standard behaviour of clearing approvals on each transfer.

Impact

Users lose approvals they granted to other entities, which could leave their positions unattended and exposed to market risks.

What the attacker needed

User has approved another entity for the NFT; user interacts with the protocol

The fix

The protocol should take approval from token owners rather than using the onERC721Received hook, though the team notes that any approval method will clear other approvals.

How it was found

  1. Alice approves Bob to manage her position NFT.
  2. Bob interacts with Krystal to collect LP fees.
  3. The protocol transfers the NFT from Alice to itself and back, clearing Bob's approval.
  4. Bob loses his approval and can no longer manage the position.

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.

When you own an NFT, you can approve someone else to manage it for you. This report says that when the protocol performs an action on your NFT, it moves the NFT to itself and then back, and this movement automatically cancels any approvals you had given to others. So other people you trusted to manage your NFT lose access.

What is Business logic?

The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.

More by Dup1337

View all

3 more disclosed reports, best first.

More from Krystal DeFi Invitational

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Business logic flaws

The lesson behind this report.

The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.