MixinTransfer.sol#transferFrom Wrong implementation can potentially allows attackers to reverse transfer and cause fund loss to the users
The transferFrom function fails to reset the key manager when the recipient has no existing key, allowing the sender to retain key manager rights after transferring the key. An attacker could then transfer the key back or to another recipient, potentially causing financial loss to the buyer.
Submitted by WatchPug
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
_setKeyManagerOf(_tokenId, address(0));
_recordOwner(_recipient, _tokenId);
} else {
// The recipient has a non expired key. We just add them the corresponding remaining time
// SafeSub is not required since the if confirms `previousExpiration - block.timestamp` cannot underflow
toKey.expirationTimestamp = fromKey.expirationTimestamp + previousExpiration - block.timestamp;
}
Based on the context, L131-136 seems to be the logic of handling the case of the recipient with no key, and L138-148 is handing the case of the recipient’s key expired.
However, in L131-136, the key manager is not being reset.
This allows attackers to keep the role of key manager after the transfer, and transfer the key back or to another recipient.
Proof of Concept
Given:
- Alice owns a key that is valid until 1 year later.
- Alice calls
setKeyManagerOf(), making herself the keyManager; - Alice calls
transferFrom(), transferring the key to Bob; Bob might have paid a certain amount of money to Alice upon receive of the key; - Alice calls
transferFrom()again, transferring the key back from Bob.
Recommendation
Consider resetting the key manager regardless of the status of the recipient’s key.
julien51 (Unlock Protocol) confirmed:
I think you are onto something here. We will need to investigate further and reproduce to fix!
0xleastwood (judge) commented:
@julien51 Just following up if you were able to double-check this?
julien51 (Unlock Protocol) confirmed:
This is indeed valid and I think we will need to “patch” this. We’re still unsure how but we’re exploring multiple ways.
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 code path for a recipient without an existing key does not clear the key manager, unlike the path for an expired key.
What the attacker needed
Attacker owns a key; attacker sets themselves as key manager; victim buys the key
The fix
The key manager should be reset regardless of the recipient's key status.
How it was found
- Alice owns a key valid for one year and sets herself as key manager.
- Alice transfers the key to Bob, who pays her.
- Alice transfers the key back from Bob because she is still key manager.
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 key manager is someone with special permission to move a digital key (like a ticket) between people. The code forgets to remove that permission when the key is given to someone new who didn't have one before. So the original owner can still move the key again, even after selling it, and take it back.
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.