Beginner FriendlySolidity
100 EXP
View results
Submission Details
Severity: high
Invalid

Array Out-of-Bounds Access in `onlyBeneficiaryWithIsInherited` Modifier

Description

The InheritanceManager contract contains a critical vulnerability in the onlyBeneficiaryWithIsInherited modifier. The modifier implements a while loop that can access an index beyond the boundaries of the beneficiaries array, leading to consistent out-of-bounds access that will revert all transactions using this modifier.

Vulnerability Analysis

The problematic code is in the onlyBeneficiaryWithIsInherited modifier:

modifier onlyBeneficiaryWithIsInherited() {
uint256 i = 0;
while (i < beneficiaries.length + 1) { // Vulnerability here
if (msg.sender == beneficiaries[i] && isInherited) {
break;
}
i++;
}
_;
}

The vulnerability exists because:

  1. The loop condition allows i to reach the value of beneficiaries.length

  2. When i == beneficiaries.length, the code attempts to access beneficiaries[i]

  3. This access is out-of-bounds as arrays in Solidity are 0-indexed (valid indices are 0 to length-1)

  4. This will cause a runtime error and revert the transaction

What makes this issue particularly severe is that it will always occur for non-beneficiary addresses or when isInherited is false, rendering the modifier and all functions using it completely unusable.

Impact

The impact of this vulnerability is severe:

  1. Complete Function Failure: All functions using this modifier will always revert for non-beneficiary callers or when isInherited is false

  2. Denial of Service: Beneficiaries cannot access their inheritance even after the inheritance condition is met

  3. Locked Funds: Assets controlled by functions protected by this modifier become permanently inaccessible

  4. Broken Access Control: The actual access control logic never executes correctly

  5. Failed Inheritance Process: The core purpose of the contract - managing inheritance - is compromised

Proof of Concept

The issue can be demonstrated with the following execution path:

  1. Initialize i = 0

  2. Check loop condition: 0 < beneficiaries.length + 1 is true

  3. If the caller is not a beneficiary or isInherited is false, the if condition fails

  4. Increment i to 1 and loop continues

  5. This repeats until i == beneficiaries.length

  6. At this point, the code attempts to access beneficiaries[beneficiaries.length]

  7. This access is out-of-bounds and causes a revert

  8. The function never executes its body

This means the modifier will revert for all non-beneficiary addresses and will not correctly enforce the access control it was designed for.

Remediation

The fix is straightforward - correct the loop condition to prevent out-of-bounds access:

modifier onlyBeneficiaryWithIsInherited() {
bool isAuthorized = false;
for (uint256 i = 0; i < beneficiaries.length; i++) {
if (msg.sender == beneficiaries[i] && isInherited) {
isAuthorized = true;
break;
}
}
require(isAuthorized, "Caller is not a beneficiary or inheritance not activated");
_;
}

This implementation:

  1. Uses a correctly bounded for loop to iterate through beneficiaries

  2. Sets a flag when an authorized caller is found

  3. Explicitly reverts with an informative message if no match is found

  4. Prevents out-of-bounds access entirely

References

  1. Solidity documentation on arrays: Arrays in Solidity are 0-indexed

  2. Common Smart Contract Vulnerabilities: Out-of-bounds array access

  3. The Solidity compiler does not prevent out-of-bounds access at compile time, it's a runtime error

  4. OpenZeppelin's AccessControl pattern demonstrates safer approaches to access control

Recommendations

  1. Immediate Fix: Implement the remediation above to prevent out-of-bounds access

  2. Refactor for Gas Efficiency: Consider refactoring to use a mapping for beneficiary lookups instead of array iteration

  3. Input Validation: Add explicit checks that prevent operations with invalid array indices

  4. Testing: Add comprehensive tests for edge cases in access control logic

  5. Access Control Patterns: Consider using established access control libraries like OpenZeppelin's AccessControl

  6. Error Messages: Add informative error messages to aid in debugging and improve user experience

  7. Formal Verification: Consider using formal verification tools to mathematically prove the absence of such issues

The vulnerability represents a complete modifier failure that renders protected functions unusable and potentially locks funds in the contract. It should be addressed immediately before deployment.

Updates

Lead Judging Commences

0xtimefliez Lead Judge 10 months ago
Submission Judgement Published
Invalidated
Reason: Incorrect statement

Support

FAQs

Can't find an answer? Chat with us on Discord, Twitter or Linkedin.

Give us feedback!