Skip to content
Access control Audit finding Advanced

Approvals not cleared after key transfer

The transferFrom function in the Unlock Protocol's lock contract does not always clear single-token approvals when a key is transferred to a recipient whose previous key had expired. An attacker could sell a key and then reclaim it using a leftover approval.

cmichel Follow

Originally disclosed on Code4rena

Submitted by cmichel

The locks implement three different approval types, see onlyKeyManagerOrApproved for an overview:

  • key manager (map keyManagerOf)
  • single-person approvals (map approved). Cleared by _clearApproval or _setKeyManagerOf
  • operator approvals (map managerToOperatorApproved)

The MixinTransfer.transferFrom requires any of the three approval types in the onlyKeyManagerOrApproved modifier on the tokenId to authenticate transfers from from.

Notice that if the to address previously had a key but it expired only the _setKeyManagerOf call is performed, which does not clear approved if the key manager was already set to 0:

function transferFrom(
  address _from,
  address _recipient,
  uint _tokenId
)
  public
  onlyIfAlive
  hasValidKey(_from)
  onlyKeyManagerOrApproved(_tokenId)
{
  // @audit this is skipped if user had a key that expired
  if (toKey.tokenId == 0) {
    toKey.tokenId = _tokenId;
    _recordOwner(_recipient, _tokenId);
    // Clear any previous approvals
    _clearApproval(_tokenId);
  }

  if (previousExpiration <= block.timestamp) {
    // The recipient did not have a key, or had a key but it expired. The new expiration is the sender's key expiration
    // An expired key is no longer a valid key, so the new tokenID is the sender's tokenID
    toKey.expirationTimestamp = fromKey.expirationTimestamp;
    toKey.tokenId = _tokenId;

    // Reset the key Manager to the key owner
    // @audit  doesn't clear approval if key manager already was 0
    _setKeyManagerOf(_tokenId, address(0));

    _recordOwner(_recipient, _tokenId);
  }
  // ...
}

//
function _setKeyManagerOf(
  uint _tokenId,
  address _keyManager
) internal
{
  // @audit-ok only clears approved if key manager updated
  if(keyManagerOf[_tokenId] != _keyManager) {
    keyManagerOf[_tokenId] = _keyManager;
    _clearApproval(_tokenId);
    emit KeyManagerChanged(_tokenId, address(0));
  }
}

Impact

It’s possible to sell someone a key and then claim it back as the approvals are not always cleared.

Proof Of Concept

  • Attacker A has a valuable key (tokenId = 42) with an expiry date far in the future.
  • A sets approvals for their second attacker controlled account A’ by calling MixinKeys.setApprovalForAll(A', true), which sets managerToOperatorApproved[A][A'] = true.
  • A clears the key manager by setting it to zero, for example, by transferring it to a second account that does not have a key yet, this calls the above _setKeyManagerOf(42, address(0)); in transferFrom
  • A sets single-token approval to A’ by calling MixinKeys.approve(A', 42), setting approved[42] = A'.
  • A sells the token to a victim V for a discount (compared to purchasing it from the Lock). The victim needs to have owned a key before which already expired. The transferFrom(A, V, 42) call sets the owner of token 42 to V, but does not clear the approved[42] == A' field as described above. (_setKeyManagerOf(_tokenId, address(0)); is called but the key manager was already zero, which then does not clear approvals.)
  • A’ can claim back the token by calling transferFrom(V, A', 42) and the onlyKeyManagerOrApproved(42) modifier will pass as approved[42] == A' is still set.

Recommended Mitigation Steps

The _setKeyManagerOf function should not handle clearing approvals of single-token approvals (approved) as these are two separate approval types. The transferFrom function should always call _clearApproval in the (previousExpiration <= block.timestamp) case.

julien51 (Unlock Protocol) confirmed and commented:

Thanks for reporting this. This is valid and we will fix it.

The original report as published on Code4rena, by cmichel. 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 _setKeyManagerOf function only clears approvals when the key manager changes, so if the key manager is already zero, approvals are not cleared during transferFrom.

What the attacker needed

Attacker owns a valuable key; Victim previously owned an expired key; Attacker can set approvals and transfer the key

The fix

The transferFrom function should always call _clearApproval when the recipient's previous key has expired, and _setKeyManagerOf should not handle clearing single-token approvals.

How it was found

  1. Attacker A owns a valuable key (tokenId 42) with a far-future expiry.
  2. A sets operator approval for a second account A' via setApprovalForAll.
  3. A clears the key manager by transferring the key to an account without a key, which calls _setKeyManagerOf(42, address(0)).
  4. A sets single-token approval for A' via approve(A', 42).
  5. A sells the key to victim V, who must have previously owned a key that expired; transferFrom(A, V, 42) does not clear approved[42].
  6. A' calls transferFrom(V, A', 42) and the onlyKeyManagerOrApproved modifier passes because approved[42] is still set.

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 transfer a digital key, the contract is supposed to remove any permissions that let someone else control it. Here, if the person receiving the key had an expired key before, the permission removal step is skipped, so the previous owner's helper can still take the key back after selling it.

What is Access control?

A user can do something their role should not allow: reach an admin function, act on a feature they have not paid for, or skip a permission check. IDOR is the special case where the object belongs to someone else.

More by cmichel

View all

196 more disclosed reports, best first.

More from Unlock Protocol contest

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Broken access control

The lesson behind this report.

A user can do something their role should not allow: reach an admin function, act on a feature they have not paid for, or skip a permission check. IDOR is the special case where the object belongs to someone else.