updateCommitmentBorrowers does not delete all existing users
The updateCommitmentBorrowers function uses the delete keyword on a mapping that contains an EnumerableSet, which does not clear the underlying mapping. As a result, previously removed borrowers remain in the set and can still call acceptCommitment to obtain a loan.
cducrest-brainbot Follow with monrel and nobody2018
Originally disclosed on Sherlock
Source: https://github.com/sherlock-audit/2023-03-teller-judging/issues/88
Found by
cducrest-brainbot, monrel, nobody2018
Summary
delete a complex structure that includes mapping will cause problem. See [ethereum/solidity#11843](https://github.com/ethereum/solidity/pull/11843) for more info.
Vulnerability Detail
The lender can update the list of borrowers by calling LenderCommitmentForwarder.updateCommitmentBorrowers. The list of borrowers is EnumerableSetUpgradeable.AddressSet that is a complex structure containing mapping. Using the delete keyword to delete this structure will not erase the mapping inside it. Let's look at the code of this function.
mapping(uint256 => EnumerableSetUpgradeable.AddressSet)
internal commitmentBorrowersList;
function updateCommitmentBorrowers(
uint256 _commitmentId,
address[] calldata _borrowerAddressList
) public commitmentLender(_commitmentId) {
delete commitmentBorrowersList[_commitmentId];
_addBorrowersToCommitmentAllowlist(_commitmentId, _borrowerAddressList);
}
I wrote a similar function to prove the problem.
using EnumerableSet for EnumerableSet.AddressSet;
mapping(uint256 => EnumerableSet.AddressSet) internal users;
function test_deleteEnumerableSet() public {
uint256 id = 1;
address[] memory newUsers = new address[](2);
newUsers[0] = address(0x1);
newUsers[1] = address(0x2);
for (uint256 i = 0; i < newUsers.length; i++) {
users[id].add(newUsers[i]);
}
delete users[id];
newUsers[0] = address(0x3);
newUsers[1] = address(0x4);
for (uint256 i = 0; i < newUsers.length; i++) {
users[id].add(newUsers[i]);
}
bool exist = users[id].contains(address(0x1));
if(exist) {
emit log_string("address(0x1) exist");
}
exist = users[id].contains(address(0x2));
if(exist) {
emit log_string("address(0x2) exist");
}
}
/*
[PASS] test_deleteEnumerableSet() (gas: 174783)
Logs:
address(0x1) exist
address(0x2) exist
*/
Impact
The deleted Users can still successfully call LenderCommitmentForwarder.acceptCommitment to get a loan.
Code Snippet
Tool used
Manual Review
Recommendation
In order to clean an EnumerableSet, you can either remove all elements one by one or create a fresh instance using an array of EnumerableSet.
Discussion
ethereumdegen
Github PR : Issue 88 - Changing the way borrowers are updated for Lender Commitment
IAm0x52
Fix looks good. updateCommitmentBorrowers has been split into two separate functions:
addCommitmentBorrowers - Explicitly adds borrowers listed removeCommitmentBorrowers - Explicitly removes borrowers listed
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
Impact
Previously removed borrowers could still call acceptCommitment and obtain a loan, bypassing the lender's updated borrower list.
What the attacker needed
Lender role; existing commitment with borrowers
The fix
The function was fixed by splitting it into separate add and remove functions that explicitly manage borrowers.
How it was found
- Lender calls updateCommitmentBorrowers with a new list of borrowers.
- The function deletes the existing commitmentBorrowersList entry for the commitment.
- The function adds the new borrowers to the list.
- Previously deleted borrowers are still present in the set due to the mapping not being cleared.
- Those borrowers can still call acceptCommitment and get a loan.
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.
In Solidity, a smart contract language, a mapping is like a dictionary that stores data. When you use 'delete' on a structure that contains a mapping, the mapping itself is not cleared. So, old entries stay, and people who were supposed to be removed can still access the system.
What is Business logic?
The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.