The setRole() function grants role instead of removing
The setRole function always adds a role using a bitwise OR, ignoring the status flag, so revoking a role does not remove it. An admin who tries to revoke a role from a compromised account leaves that account with the privilege, allowing it to keep minting tickets.
Source: https://github.com/sherlock-audit/2024-08-winnables-raffles-judging/issues/53
Found by
0x0x0xw3, 0xAadi, 0xarno, 0xbrivan, 0xnolo, Afriaudit, DrasticWatermelon, Drynooo, Galturok, KaligoAudits, KingNFT, MaanVader, MrPotatoMagic, PTolev, Paradox, Penaldo, PeterSR, PratRed, Saurabh_Singh, Trooper, UAARRR, araj, azanux, boringslav, casper, chaduke, charles__cheerful, dany.armstrong90, denzi_, dimulski, dwaipayan01, iamnmt, ironside, joshuajee, ke1caM, korok, mahmud, matejdb, neko_nyaa, nilay27, petro1912, roguereggiant, sharonphiliplima, shikhar, thisvishalsingh, unnamed, utsav, ydlee
Summary
Access control in the Winnables Raffles protocol is handled with the Roles contract. It works similarly to OpenZeppelin's access control but uses bit flags to determine whether a user has a role. Each user has a bytes32 representing the bitfield of roles. Role 0 is an admin role, allowing its members to grant or deny(remove) roles to other users.
The setRole(address user, uint8 role, bool status) function, as it stands, always adds a role by performing a bitwise OR operation. However, it does not handle the removal of roles if the status parameter is false. This oversight results in incorrect role management within the contracts, potentially leading to accidental privilege grants or the inability to revoke privileges from compromised or revoked accounts.
Root Cause
In Roles.sol:L29 the _setRole() function always adds a role by performing a bitwise OR operation:
This internal function is used in the setRole() function:
Internal pre-conditions
The setRole() function can only be called by the Admin.
External pre-conditions
No response
Attack Path
Admindeploys theWinnablesTicketTestcontract.Admingrants role1toAliceby calling thesetRole()function of theWinnablesTicketTestcontract. The role is granted toAlice.Alicemints 10 tickets toBobusing the role.Adminrevokes role1fromAliceby calling thesetRole()function.- The role is not removed from
Alice. She can still mint tickets toBob.
Impact
The improper implementation results in incorrect role management within the contracts, potentially leading to accidental privilege grants or the inability to revoke privileges from compromised or revoked accounts.
PoC
describe('Ticket behaviour', () => {
...
it('Should not be able to mint tickets afer role deny', async () => {
await (await ticket.setRole(signers[2].address, 1, true)).wait();
const { events } = await (await ticket.connect(signers[2]).mint(signers[3].address, 1, 1)).wait();
expect(events).to.have.lengthOf(2);
expect(events[0].event).to.eq('NewTicket');
expect(events[1].event).to.eq('TransferSingle');
await (await ticket.setRole(signers[2].address, 1, false)).wait();
await expect(ticket.connect(signers[2]).mint(signers[2].address, 1, 1)).to.be.revertedWithCustomError(
ticket,
'MissingRole'
);
});
...
Mitigation
Modify the _setRole() function to handle both adding and removing roles based on the status parameter:
function _setRole(address user, uint8 role, bool status) internal virtual {
uint256 roles = uint256(_addressRoles[user]);
if (status) {
_addressRoles[user] = bytes32(roles | (1 << role));
} else {
_addressRoles[user] = bytes32(roles & ~(1 << role));
}
emit RoleUpdated(user, role, status);
}
Discussion
sherlock-admin2
The protocol team fixed this issue in the following PRs/commits: https://github.com/Winnables/public-contracts/pull/4
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 internal _setRole function always sets the role bit with a bitwise OR and never clears it when status is false.
Impact
The improper implementation results in incorrect role management within the contracts, potentially leading to accidental privilege grants or the inability to revoke privileges from compromised or revoked accounts.
What the attacker needed
Admin privileges to call setRole; A role previously granted to an account
The fix
The protocol team fixed this issue in a pull request.
How it was found
- Admin deploys the WinnablesTicketTest contract.
- Admin grants role 1 to Alice by calling setRole.
- Alice mints 10 tickets to Bob using the role.
- Admin revokes role 1 from Alice by calling setRole.
- The role is not removed from Alice, so she can still mint tickets to Bob.
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 stores each user's permissions as a set of bits, like switches that are either on or off. The function meant to turn a permission off only ever turns it on, so a user who should lose a permission keeps it. This means an admin cannot actually take away someone's ability to perform privileged actions.
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.