The addMember function allowed adding members to a community that did not yet exist because the community owner address was zero and the signature check could be bypassed. An attacker could add as many members as they wanted before the community was created.
There is a addMember function in the Community. The function accepts _data that should be signed by the _community.owner and _newMemberAddr.
// Compute hash from bytes
bytes32 _hash = keccak256(_data);
// Decode params from _data
(
uint256 _communityID,
address _newMemberAddr,
bytes memory _messageHash
) = abi.decode(_data, (uint256, address, bytes));
CommunityStruct storage _community = _communities[_communityID];
// check signatures
checkSignatureValidity(_community.owner, _hash, _signature, 0); // must be community owner
checkSignatureValidity(_newMemberAddr, _hash, _signature, 1); // must be new member
The code above shows exactly what the contract logic looks like.
_communityID is taken from the data provided by user, so it can arbitrarily. Specifically, community with selected _communityID can be not yet created. For instance, it can be equal to the communityCount + 1, thus the next created community will have this _communityID.
_communities[_communityID] will store null values for all fields, for a selected _communityID. That means, _community.owner == address(0)
checkSignatureValidity with a parameters address(0), _hash, _signature, 0 will not revert a call if an attacker provide incorrect _signature.
Let’s see the implementation of checkSignatureValidity:
// Decode signer
address _recoveredSignature = SignatureDecoder.recoverKey(
_hash,
_signature,
_signatureIndex
);
// Revert if decoded signer does not match expected address
// Or if hash is not approved by the expected address.
require(
_recoveredSignature == _address || approvedHashes[_address][_hash],
"Community::invalid signature"
);
// Delete from approvedHash. So that signature cannot be reused.
delete approvedHashes[_address][_hash];
No restrictions on _recoveredSignature or _address. Moreover, if SignatureDecoder.recoverKey can return zero value, then there will be no revert.
if (messageSignatures.length % 65 != 0) {
return (address(0));
}
uint8 v;
bytes32 r;
bytes32 s;
(v, r, s) = signatureSplit(messageSignatures, pos);
// If the version is correct return the signer address
if (v != 27 && v != 28) {
return (address(0));
} else {
// solium-disable-next-line arg-overflow
return ecrecover(toEthSignedMessageHash(messageHash), v, r, s);
}
As we can see below, recoverKey function can return zero value, if an ecrecover return zero value or if v != 27 || v != 28. Both cases are completely dependent on the input parameters to the function, namely from signature that is provided by attacker.
checkSignatureValidity(_newMemberAddr, _hash, _signature, 1) will not revert the call if an attacker provide correct signature in the function. It is obviously possible.
All in all, an attacker can add as many members as they want, BEFORE the community will be created.
Recommended Mitigation Steps
checkSignatureValidity/recoverKey should revert the call if an address == 0.
addMember should have a require(_communityId <= communityCount)
The original report as published on Code4rena, by vlad_bochok. Images and videos are our own copies; raw HTML is shown as text. View it on Code4rena.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
Generating the AI write-up for this report.
This usually takes about 30 seconds. You can read the original meanwhile; this page updates itself.
Writing the technical write-up
Writing the version for beginners
Checking every passage against the original
How it worked
Root cause
Likely the signature verification did not reject a zero address and the addMember function did not check that the community ID already existed.
What the attacker needed
Ability to call addMember; a chosen community ID that is not yet created
The fix
The signature check should revert when the expected address is zero, and addMember should require that the community ID is not greater than the community count.
How it was found
Choose a community ID that has not been created yet, such as communityCount + 1.
Provide data with that community ID and a new member address, signed by the attacker as the new member.
Call addMember; the owner signature check passes because the owner is address(0) and the signature recovery can return zero.
The new member is added to the not-yet-created community.
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.
Generating the AI write-up for this report.
This usually takes about 30 seconds. You can read the original meanwhile; this page updates itself.
Writing the technical write-up
Writing the version for beginners
Checking every passage against the original
A smart contract lets people create communities and add members. To add a member, two signatures are needed: one from the community owner and one from the new member. The bug was that you could pick a community ID that had not been created yet, so the owner address was empty (zero). The signature check did not reject an empty owner, so an attacker could add members without the owner's permission.
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.
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.