Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Out-of-Bounds Error in getPositionsForCurator Function #215

Open
howlbot-integration bot opened this issue Sep 6, 2024 · 4 comments
Open

Out-of-Bounds Error in getPositionsForCurator Function #215

howlbot-integration bot opened this issue Sep 6, 2024 · 4 comments
Labels
bug Something isn't working downgraded by judge Judge downgraded the risk level of this issue duplicate-72 edited-by-warden grade-a Q-30 QA (Quality Assurance) Assets are not at risk. State handling, function incorrect as to spec, issues with clarity, syntax 🤖_59_group AI based duplicate group recommendation satisfactory satisfies C4 submission criteria; eligible for awards sufficient quality report This report is of sufficient quality

Comments

@howlbot-integration
Copy link

Lines of code

https://github.com/code-423n4/2024-08-phi/blob/main/src/Cred.sol#L480-L523

Vulnerability details

Impact

The current implementation of the getPositionsForCurator function has a potential out-of-bounds error. Specifically, the credIds and amounts arrays are populated using the i variable, which starts from start_, causing an incorrect index that can lead to an out-of-bounds error and unexpected behavior.

If the provided start_ value is greater than zero, the index will start incorrectly, leading to potential array bounds errors resulting in corruption or exceptions during execution. This can disrupt the proper functionality of the getPositionsForCurator function, leading to unexpected results and breaking the logic of the contract.

Proof of Concept

Below is the problematic code in the getPositionsForCurator function:

for (uint256 i = start_; i < stopIndex; i++) {
    uint256 credId = userCredIds[i];
    if (_credIdExistsPerAddress[curator_][credId]) {
        uint256 amount = shareBalance[credId].get(curator_);
513:        credIds[i] = credId;
514:        amounts[i] = amount;
        index++;
    }
}
// Resize the result array to remove unused slots
assembly {
    mstore(credIds, index)
    mstore(amounts, index)
}

Tools Used

Manual code review

Recommended Mitigation Steps

Use the index variable instead of i to populate the credIds and amounts arrays, ensuring they are populated correctly even when start_ is greater than zero.

Here is the final corrected implementation for clarity:

function getPositionsForCurator(
    address curator_,
    uint256 start_,
    uint256 stop_
)
    external
    view
    returns (uint256[] memory credIds, uint256[] memory amounts)
{
    uint256[] storage userCredIds = _credIdsPerAddress[curator_];

    uint256 stopIndex;
    if (userCredIds.length == 0) {
        return (credIds, amounts);
    }
    if (stop_ == 0 || stop_ > userCredIds.length) {
        stopIndex = userCredIds.length;
    } else {
        stopIndex = stop_;
    }

    if (start_ >= stopIndex) {
        revert InvalidPaginationParameters();
    }

    credIds = new uint256[](stopIndex - start_);
    amounts = new uint256[](stopIndex - start_);

    uint256 index = 0;
    for (uint256 i = start_; i < stopIndex; i++) {
        uint256 credId = userCredIds[i];
        if (_credIdExistsPerAddress[curator_][credId]) {
            uint256 amount = shareBalance[credId].get(curator_);
-            credIds[i] = credId;
+            credIds[index] = credId;
-            amounts[i] = amount;
+            amounts[index] = amount;
            index++;
        }
    }
    // Resize the result array to remove unused slots
    assembly {
        mstore(credIds, index)
        mstore(amounts, index)
    }
}

Assessed type

Invalid Validation

@howlbot-integration howlbot-integration bot added 3 (High Risk) Assets can be stolen/lost/compromised directly 🤖_59_group AI based duplicate group recommendation bug Something isn't working duplicate-72 edited-by-warden sufficient quality report This report is of sufficient quality labels Sep 6, 2024
howlbot-integration bot added a commit that referenced this issue Sep 6, 2024
@c4-judge
Copy link
Contributor

fatherGoose1 changed the severity to 2 (Med Risk)

@c4-judge c4-judge added 2 (Med Risk) Assets not at direct risk, but function/availability of the protocol could be impacted or leak value downgraded by judge Judge downgraded the risk level of this issue satisfactory satisfies C4 submission criteria; eligible for awards and removed 3 (High Risk) Assets can be stolen/lost/compromised directly labels Sep 13, 2024
@c4-judge
Copy link
Contributor

fatherGoose1 marked the issue as satisfactory

@c4-judge c4-judge added QA (Quality Assurance) Assets are not at risk. State handling, function incorrect as to spec, issues with clarity, syntax and removed 2 (Med Risk) Assets not at direct risk, but function/availability of the protocol could be impacted or leak value labels Sep 18, 2024
@c4-judge
Copy link
Contributor

fatherGoose1 changed the severity to QA (Quality Assurance)

@c4-judge
Copy link
Contributor

fatherGoose1 marked the issue as grade-a

@C4-Staff C4-Staff reopened this Sep 24, 2024
@C4-Staff C4-Staff added the Q-30 label Sep 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working downgraded by judge Judge downgraded the risk level of this issue duplicate-72 edited-by-warden grade-a Q-30 QA (Quality Assurance) Assets are not at risk. State handling, function incorrect as to spec, issues with clarity, syntax 🤖_59_group AI based duplicate group recommendation satisfactory satisfies C4 submission criteria; eligible for awards sufficient quality report This report is of sufficient quality
Projects
None yet
Development

No branches or pull requests

2 participants