No way to revoke Approval in DelegateToken.approve leads to unauthorized calling of DelegateToken.transferFrom
The DelegateToken contract allows an operator to approve themselves for a specific token ID, and this approval persists even after the token holder revokes the operator's approval-for-all. An attacker who was previously an operator can still transfer the token via transferFrom.
Submitted by d4r3d3v1l
https://github.com/code-423n4/2023-09-delegate/blob/main/src/DelegateToken.sol#L134 https://github.com/code-423n4/2023-09-delegate/blob/main/src/DelegateToken.sol#L168 https://github.com/code-423n4/2023-09-delegate/blob/main/src/libraries/DelegateTokenStorageHelpers.sol#L149
There is no way to revoke the approval which given via DelegateToken.approve(address,delegateTokenId). They can able call the DelegateToken.transferFrom even the tokenHolder revoke the permission using the DelegateToken.setApprovalForAll.
If the spender address is approved by the PT token, we can call the DelegateToken.withdraw.
Proof of Concept
Alice is the token Holder.
Alice approves Bob via DelegateToken.setApprovalForAll(Bob,true).
Bob approves himself using DelegateToken.approve(Bob,delegateTokenId)
Alice revokes the Bob approval by calling DelegateToken.setApprovalForAll(Bob,false);
Now Bob can still calls the DelegateToken.transferFrom(Alice,to,delegateTokenId) which is subjected to call only by approved address.
The transfer will be successful.
Code details
function revertNotApprovedOrOperator(
mapping(address account => mapping(address operator => bool enabled)) storage accountOperator,
mapping(uint256 delegateTokenId => uint256[3] info) storage delegateTokenInfo,
address account,
uint256 delegateTokenId
) internal view {
if (msg.sender == account || accountOperator[account][msg.sender] || msg.sender == readApproved(delegateTokenInfo, delegateTokenId)) return;
revert Errors.NotApproved(msg.sender, delegateTokenId);
}
Even after revoking the approval for operator using setApprovalAll this msg.sender == readApproved(delegateTokenInfo, delegateTokenId) will be true and able to call transferFrom function.
Test function
function testFuzzingTransfer721(
address from,
address to,
uint256 underlyingTokenId,
bool expiryTypeRelative,
uint256 time
) public {
vm.assume(from != address(0));
vm.assume(from != address(dt));
(
,
/* ExpiryType */
uint256 expiry, /* ExpiryValue */
) = prepareValidExpiry(expiryTypeRelative, time);
mockERC721.mint(address(from), 33);
vm.startPrank(from);
mockERC721.setApprovalForAll(address(dt), true);
vm.stopPrank();
vm.prank(from);
dt.setApprovalForAll(address(dt), true);
vm.prank(from);
uint256 delegateId1 = dt.create(
DelegateTokenStructs.DelegateInfo(
from,
IDelegateRegistry.DelegationType.ERC721,
from,
0,
address(mockERC721),
33,
"",
expiry
),
SALT + 1
);
vm.prank(address(dt));
dt.approve(address(dt), delegateId1);
vm.prank(from);
dt.setApprovalForAll(address(dt), false);
address tmp = dt.getApproved(delegateId1);
console.log(tmp);
vm.prank(address(dt));
dt.transferFrom(from, address(0x320), delegateId1);
}
Recommended Mitigation Steps
If token Holder revokes the approval for a operator using DelegateToken.setApprovalForAll, revoke the all the approvals(DelegateToken.approve) which is done by the operator.
Assessed type
Access Control
0xfoobar (Delegate) confirmed and commented:
Need to double-check, but looks plausible.
Alex the Entreprenerd (judge) commented:
In contrast to ERC721, which only allows the owner to change
approveERC721.sol#L448-L454
DT.approveallows the operator to set themselves as approved, which can technically be undone via a multicall but may not be performed under normal usage.
Alex the Entreprenerd (judge) decreased severity to Medium and commented:
Leaning towards Medium Severity as the finding requires:
- Alice
approvesForAllBob- Bob approves self
- Alice revokes Bobs
approvalForAll- Alice doesn’t revoke the
_approvethat Bob gave to selfNotice that any transfer would reset the approval as well.
0xfoobar (Delegate) commented:
A note about how ERC721 works: there are two different types of approvals.
approve()lets an account move a single tokenId, whilesetApprovalForAll()marks an address as an operator to move all tokenIds. We can note the difference in the core OpenZeppelin ERC721 base contract, two separate mappings: https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/token/ERC721/ERC721.sol#L32-L34So plausible that an operator could be permitted to add new tokenid-specific approvals, but see your point that OZ ERC721 doesn’t allow this by default and so could lead to confusing states that are possible but not fun to get out of.
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 contract allows an operator to call approve for a token ID, and revoking operator status does not clear those individual approvals.
Impact
A previously approved operator could still transfer a specific token after their operator rights are revoked, potentially leading to unauthorised token transfers.
What the attacker needed
Victim must have approved attacker as operator; attacker must call approve for a token ID; victim must revoke operator status
The fix
When an operator's approval-for-all is revoked, all individual approvals that operator granted should also be revoked.
How it was found
- Alice approves Bob as an operator using setApprovalForAll.
- Bob calls approve to set himself as approved for a specific delegateTokenId.
- Alice revokes Bob's operator status using setApprovalForAll with false.
- Bob calls transferFrom and the transfer succeeds because the individual approval remains.
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.
In many token contracts, the owner can give another address permission to move their tokens. There are two kinds: approval for a single token, and approval for all tokens (called an operator). This report shows that if someone is made an operator, they can give themselves approval for a single token, and that approval stays even after the owner removes them as operator. So the person can still move that token, which the owner did not intend.
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.