Skip to content

Latest commit

 

History

History
75 lines (62 loc) · 3.77 KB

File metadata and controls

75 lines (62 loc) · 3.77 KB

Clever Mocha Pheasant

Medium

Incomplete Validation State in Collateral Balance Checks

Summary

The CollateralManager contract contains a significant vulnerability in its collateral validation mechanism where the validation state becomes unreliable due to premature returns during short-circuit operations. The issue manifests in the _checkBalances function which attempts to optimize gas usage by allowing early termination of validation checks, but in doing so creates undefined states that could compromise the integrity of the collateral management system.

When performing balance validations with _shortCircuit enabled, the function returns upon encountering the first failed check without properly initializing the remaining validation states. This breaks the contract's ability to provide accurate validation information to dependent systems and could lead to incorrect collateral management decisions.

https://github.com/sherlock-audit/2024-11-teller-finance-update/blob/main/teller-protocol-v2-audit-2024/packages/contracts/contracts/CollateralManager.sol#L541

function _checkBalances(
    address _borrowerAddress,
    Collateral[] memory _collateralInfo,
    bool _shortCircuit
) internal virtual view returns (bool validated_, bool[] memory checks_) {
    checks_ = new bool[](_collateralInfo.length);
    validated_ = true;
    for (uint256 i; i < _collateralInfo.length; i++) {
        bool isValidated = _checkBalance(
            _borrowerAddress,
            _collateralInfo[i]
        );
        checks_[i] = isValidated;
        if (!isValidated) {
            validated_ = false;
            //if short circuit is true, return on the first invalid balance to save execution cycles. 
            //Values of checks[] will be invalid/undetermined if shortcircuit is true.
            if (_shortCircuit) {
                return (validated_, checks_);
            }
        }
    }
}

The implications of this vulnerability extend beyond simple data integrity. Contracts or interfaces relying on the validation results may misinterpret undefined states as successful validations, potentially allowing invalid collateral to be processed. This creates a systemic risk where the optimization for gas efficiency compromises the core security guarantees of the collateral validation system.

Recommended mitigation steps

To maintain both gas efficiency and data integrity, the validation mechanism should be modified to ensure complete and accurate state information even during short-circuit operations. The following implementation addresses these concerns while preserving the gas optimization benefits:

function _checkBalances(
    address _borrowerAddress,
    Collateral[] memory _collateralInfo,
    bool _shortCircuit
) internal virtual view returns (bool validated_, bool[] memory checks_) {
    checks_ = new bool[](_collateralInfo.length);
    validated_ = true;
    
    for (uint256 i; i < _collateralInfo.length; i++) {
        bool isValidated = _checkBalance(
            _borrowerAddress,
            _collateralInfo[i]
        );
        checks_[i] = isValidated;
        
        if (!isValidated) {
            validated_ = false;
            if (_shortCircuit) {
                for (uint256 j = i + 1; j < _collateralInfo.length; j++) {
                    checks_[j] = false;
                }
                return (validated_, checks_);
            }
        }
    }
}

This solution implements a deterministic approach to validation state management. When short-circuiting occurs, the function explicitly marks remaining validations as failed rather than leaving them in an undefined state. This maintains the gas efficiency of early termination while ensuring that all consumers of the validation data receive complete and accurate information about the validation state.