# Exposed _removeCredIdPerAddress & _addCredIdPerAddress allows anyone to cause issues to current holders as well as upcoming ones

Public _addCredIdPerAddress/_removeCredIdPerAddress let anyone corrupt or bloat any user's credId array, reverting their cred share buy/sell transactions.

- Payout: Audit contest
- Severity: high
- Program: Phi
- Found by: 0xrex
- Disclosed: 2024-10-07
- Write-up quality: 6/10
- Original: https://github.com/code-423n4/2024-08-phi-findings/issues/51
- Library page: https://bugbountylibrary.com/r/c4%3A2024-08-phi%3AH-05
- Tags: area:web3 domain:web3 impact:dos kind:contest platform:code4rena sev:high tech:evm tech:solidity technique:source-review vuln:access-control vuln:dos

**Target:** Phi cred contract credId-per-address bookkeeping functions
**Root cause:** _addCredIdPerAddress and _removeCredIdPerAddress are declared public instead of internal, so any caller can push arbitrary credIds into or reorder/remove entries from any address's _credIdsPerAddress array and index mapping.
**Steps:**
1. Call `_addCredIdPerAddress(credId_, victim)` with arbitrary/non-existent credIds to bloat `_credIdsPerAddress[victim]` and desync `_credIdsPerAddressCredIdIndex`.
2. Repeat to grow the array until loops hit block gas limits on cheap-gas chains (Base, Avax C-Chain).
3. Call `_removeCredIdPerAddress` to reorder indices so the victim's later removal hits `WrongCredId()` or `IndexOutofBounds()` reverts.
4. Victim's attempts to buy or sell cred shares revert, blocking their transactions.
**How found:** Code review of the credId bookkeeping functions revealed they were marked public rather than internal, exposing state mutation to arbitrary callers.
**Exploitation:** Attacker directly invokes the public functions against a victim address, inflating the array with junk credIds and manipulating the index mapping so the victim's legitimate add/remove paths revert or run out of gas.
**Insight:** When auditing array+index-mapping bookkeeping helpers, check visibility: helpers meant to be internal but declared public let anyone mutate another user's state and desync index mappings, producing cheap DoS.

## Proof of concept (copied exactly from the original report)

**Public _addCredIdPerAddress and _removeCredIdPerAddress functions**

```
function _addCredIdPerAddress(uint256 credId_, address sender_) public {
        // Add the new credId to the array
        _credIdsPerAddress[sender_].push(credId_);
        // Store the index of the new credId
        _credIdsPerAddressCredIdIndex[sender_][credId_] = _credIdsPerAddressArrLength[sender_];
        // Increment the array length counter
        _credIdsPerAddressArrLength[sender_]++;
    }

    // Function to remove a credId from the address's list
    function _removeCredIdPerAddress(uint256 credId_, address sender_) public {
        // Check if the array is empty
        if (_credIdsPerAddress[sender_].length == 0) revert EmptyArray();

        // Get the index of the credId to remove
        uint256 indexToRemove = _credIdsPerAddressCredIdIndex[sender_][credId_];
        // Check if the index is valid
        if (indexToRemove >= _credIdsPerAddress[sender_].length) revert IndexOutofBounds();

        // Verify that the credId at the index matches the one we want to remove
        uint256 credIdToRemove = _credIdsPerAddress[sender_][indexToRemove];
        if (credId_ != credIdToRemove) revert WrongCredId();

        // Get the last element in the array
        uint256 lastIndex = _credIdsPerAddress[sender_].length - 1;
        uint256 lastCredId = _credIdsPerAddress[sender_][lastIndex];
        // Move the last element to the position of the element we're removing
        _credIdsPerAddress[sender_][indexToRemove] = lastCredId;

        // Update the index of the moved element, if it's not the one we're removing
        if (indexToRemove < lastIndex) {
            _credIdsPerAddressCredIdIndex[sender_][lastCredId] = indexToRemove;
        }

        // Remove the last element (which is now a duplicate)
        _credIdsPerAddress[sender_].pop();

        // Remove the index mapping for the removed credId
        delete _credIdsPerAddressCredIdIndex[sender_][credIdToRemove];

        // Decrement the array length counter, if it's greater than 0
        if (_credIdsPerAddressArrLength[sender_] > 0) {
            _credIdsPerAddressArrLength[sender_]--;
        }
    }
```

