<!-- canonical: https://0xsimao.com/findings/keyring-ii-credentials-manipulated -->

# Credentials can be manipulated

Crit/High · Three Sigma · Zero-knowledge compliance · 10th July, 2024

Finding 3S-Keyring-C01 of the Keyring Credentials security review.

- Protocol: https://www.keyring.network/
- Report: /reports/keyring-ii
- Source: https://cdn.sanity.io/files/qoqld077/production/75b68b74f4b0dc6fbcd94892d934547d8259b57a.pdf

---

### Description

KeyringCoreV2::createCredential() downcasts several variables, uint24(policyId),
uint32(epoch), uint32(epochExp), uint168(cost) when sending them to
RsaVerifyOptimized::verifyAuthMessage(), where they are encoded to a signature.
Downcasting does not revert so if a key signs a message with a certain variable smaller than
the casting that is being made, attackers can send this variable but with higher upper bits
set to 1, such that the encoded message is still the same but the variable that is sent to
KeyringCoreV2Base::_createCredential() is actually much bigger.
One example of an abuse is an attacker setting a much bigger epochExp, which is
downcasted to the much lower value to sign, but sets the expiry of its
_entityData[policyId][tradingAddress].exp to a value very far away in the future (up
to uint64).
Another example is an attacker setting epoch to a much larger value such that it can set the
credentials forever.

### Recommendation

All the downcastings in the codebase should use the safe casting lib from [OpenZeppelin](https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/utils/math/SafeCast.sol).
Additionally, signatures could implement a nonce system to complement the deadline
epoch argument.

### Status

Addressed in [#af37b1d](https://github.com/Keyring-Network/core-v2/commit/af37b1dea5e16b35386b2175228e1974b90adf3f).
