Repository._removeContract() removes the contract wrongly.
The _removeContract function in Repository.sol resets the contract's index to zero before using it to update the contracts array, so it always replaces the first element with the last and leaves the removed name in the array. This corrupts the list of contract names, causing valid contracts to be removed and the removed contract to remain.
hansfriese Follow with KingNFT
Originally disclosed on Code4rena
Submitted by hansfriese, also found by KingNFT
After removing the contract, the contracts array would contain the wrong contract names.
Proof of Concept
Repository._removeContract() removes the contract name from contracts array.
File: 2023-02-malt\contracts\Repository.sol
223: function _removeContract(string memory _name) internal {
224: bytes32 hashedName = keccak256(abi.encodePacked(_name));
225: Contract storage currentContract = globalContracts[hashedName];
226: currentContract.contractAddress = address(0);
227: currentContract.index = 0;
228:
229: uint256 index = currentContract.index; //@audit wrong index
230: string memory lastContract = contracts[contracts.length - 1];
231: contracts[index] = lastContract;
232: contracts.pop();
233: emit RemoveContract(hashedName);
234: }
But it uses the already changed index(= 0) and replaces the last name with 0 index all the time.
As a result, the contracts array will still contain the removed name and remove the valid name at index 0.
Recommended Mitigation Steps
We should use the original index like below.
function _removeContract(string memory _name) internal {
bytes32 hashedName = keccak256(abi.encodePacked(_name));
Contract storage currentContract = globalContracts[hashedName];
uint256 index = currentContract.index; //++++++++++++++
currentContract.contractAddress = address(0);
currentContract.index = 0;
string memory lastContract = contracts[contracts.length - 1];
contracts[index] = lastContract;
contracts.pop();
emit RemoveContract(hashedName);
}
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 code reads the contract's index after it has already been set to zero, so the array update uses the wrong position.
Impact
The contracts array becomes corrupted: the removed contract's name stays in the list and a valid contract at index zero is removed instead, which could break any logic that relies on the array.
What the attacker needed
Contract removal is triggered
The fix
The index should be read before resetting the contract's fields, so the correct position is used when updating the array.
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 keeps a list of contract names. When removing one, the code is supposed to swap the last name into the removed one's spot and then shrink the list. But it accidentally sets the position to zero before doing the swap, so it always overwrites the first name and never actually removes the intended one.
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.