The removeBeneficiary
function in the InheritanceManager contract contains a critical vulnerability where removing a beneficiary leaves a "hole" (zero address) in the beneficiaries array. This impacts the inheritance distribution logic, potentially causing funds to be sent to invalid addresses or resulting in incorrect distribution amounts to remaining beneficiaries.
The removeBeneficiary
function uses the Solidity delete
keyword to remove an element from the beneficiaries array. However, this operation doesn't actually remove the element from the array - it only resets the value at that index to the default value (address(0)). The array length remains unchanged, and the array structure is compromised with a zero address in place of the removed beneficiary.
This creates several issues:
The contract calculates divisions based on beneficiaries.length
, which now includes invalid zero addresses
The withdrawInheritedFunds
function attempts to send funds to all addresses in the array, including the zero address
The contract's inheritance logic is based on the number of beneficiaries, but this count is incorrect after removals
This vulnerability has significant impacts:
Locked Funds: When withdrawInheritedFunds
attempts to distribute assets to address(0), the transaction may revert, potentially locking all funds in the contract.
Incorrect Distribution: If transfers to address(0) don't revert, remaining beneficiaries will receive less than their fair share because the divisor used includes removed beneficiaries.
Inheritance Logic Errors: The inherit
function behaves differently based on the number of beneficiaries; including removed addresses leads to incorrect control flow.
Gas Wastage: Iterating through arrays with deleted elements wastes gas and could potentially cause functions to hit the gas limit.
The impact is especially severe because this contract manages inheritance assets, where correctness of fund distribution is critical.
To properly test this, you would need to add a simple view function to the InheritanceManager contract that allows you to check the array contents. Without seeing inside the array, it's hard to prove the bug exists definitively.
The minimum function you need to add to InheritanceManager
is:
Paste these lines of code in the test folder: InheritanceManagerTest.t.sol
Manual code review
Foundry for testing and verification
Replace the vulnerable implementation with a secure array management pattern that maintains array integrity. Here's a corrected version:
This approach:
Replaces the removed element with the last element in the array
Reduces the array length by removing the last element
Preserves array integrity with no gaps or zero addresses
Maintains O(1) complexity for removal operations
Additionally, consider adding events to track beneficiary changes and implementing additional validations to ensure the integrity of the beneficiaries array.
The contest is live. Earn rewards by submitting a finding.
This is your time to appeal against judgements on your submissions.
Appeals are being carefully reviewed by our judges.