MaltRepository._revokeRole may not work correctly
MaltRepository overrides hasRole to also require validRoles[role], so once a role is removed via removeRole, _revokeRole silently fails to revoke it from an account. An attacker holding a removed role could keep it, and _transferRole would grant the role to a new account without removing it from the old one.
Submitted by cccz
MaltRepository inherits from AccessControl and adds validation of validRoles to the hasRole function, which means that even if super.hasRole(role, account) == true, if validRoles[role] == false hasRole will return false, which will cause \_revokeRole to not work correctly.
function hasRole(bytes32 role, address account)
public
view
override
returns (bool)
{
// Timelock has all possible permissions
return
(super.hasRole(role, account) && validRoles[role]) ||
super.hasRole(TIMELOCK_ROLE, account);
}
Consider the case where Alice is granted ADMINROLE, then ADMINROLE is removed in the removeRole function, validRoles[ADMIN_ROLE] == false.
function removeRole(bytes32 role) external onlyRole(getRoleAdmin(role)) {
validRoles[role] = false;
emit RemoveRole(role);
}
Now if the revokeRole function is called on Alice, in the \_revokeRole, since hasRole returns false, Alice’s ADMIN_ROLE will not be revoked.
Since removeRole ends silently, this may actually cause the caller to incorrectly assume that Alice’s ADMIN_ROLE has been revoked:
function _revokeRole(bytes32 role, address account) internal virtual {
if (hasRole(role, account)) {
_roles[role].members[account] = false;
emit RoleRevoked(role, account, _msgSender());
}
}
In addition, the renounceRole and \_transferRole functions will also be affected.
In particular, the \_transferRole function, if you want to transfer Alice’s role to Bob, both Alice and Bob will have the role if validRoles[role]==false.
function _transferRole(
address newAccount,
address oldAccount,
bytes32 role
) internal {
_revokeRole(role, oldAccount);
_grantRole(role, newAccount);
}
...
function renounceRole(bytes32 role, address account) public virtual override {
require(account == _msgSender(), "AccessControl: can only renounce roles for self");
_revokeRole(role, account);
}
Proof of Concept
Recommended Mitigation Steps
Override renounceRole and removeRole in the MaltRepository and modify them as follows:
function renounceRole(bytes32 role, address account) public virtual override {
+ require(validRoles[role], "Unknown role");
require(account == _msgSender(), "AccessControl: can only renounce roles for self");
_revokeRole(role, account);
}
...
function revokeRole(bytes32 role, address account) public virtual override onlyRole(getRoleAdmin(role)) {
+ require(validRoles[role], "Unknown role");
_revokeRole(role, account);
}
...
function _transferRole(
address newAccount,
address oldAccount,
bytes32 role
) internal {
+ require(validRoles[role], "Unknown role");
_revokeRole(role, oldAccount);
_grantRole(role, newAccount);
}
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 overridden hasRole returns false for roles that are no longer valid, and _revokeRole only revokes when hasRole is true, so revocation is skipped.
Impact
A removed role may remain assigned to an account, and transferring a role could leave both the old and new account with it, so permissions may not be revoked as intended.
What the attacker needed
A role that has been removed via removeRole; an account still holding that role
The fix
Override renounceRole, revokeRole and _transferRole to require that validRoles[role] is true before proceeding.
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 smart contract has a list of roles (like admin) that give special permissions. The contract can mark a role as no longer valid, but because of how it checks permissions, it then cannot take that role away from someone who already has it. So a person could keep powers they were supposed to lose.
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.