sherlock-audit / 2024-09-predict-fun-judging

0 stars 0 forks source link

Wild Cinnamon Crocodile - Incorrect Values Emitted in NoncesIncremented Event #329

Closed sherlock-admin3 closed 2 days ago

sherlock-admin3 commented 2 days ago

Wild Cinnamon Crocodile

Low/Info

Incorrect Values Emitted in NoncesIncremented Event

Summary

Root Cause

Vulnerable Function: predict-dot-loan/contracts/PredictDotLoan.sol#L687

function incrementNonces(bool lending, bool borrowing) external {
    if (!lending && !borrowing) {
        revert NotIncrementing();
    }

    uint128 lendingNonce = nonces[msg.sender].lending;
    uint128 borrowingNonce = nonces[msg.sender].borrowing;

    if (lending) {
        unchecked {
            nonces[msg.sender].lending = ++lendingNonce;
        }
    }

    if (borrowing) {
        unchecked {
            nonces[msg.sender].borrowing = ++borrowingNonce;
        }
    }

    emit NoncesIncremented(lendingNonce, borrowingNonce);  // Bug: Emitting old values
}

Impact:

PoC

  1. Call the incrementNonces function with either lending or borrowing set to true.
  2. Observe that the emitted values in the NoncesIncremented event correspond to the old values of the nonces (before increment), but the contract's storage reflects the updated values.

Mitigation

Proposed Fix:

Modify the emit statement to use the updated nonce values from storage rather than the local lendingNonce and borrowingNonce variables. The correct version is as follows:

function incrementNonces(bool lending, bool borrowing) external {
    if (!lending && !borrowing) {
        revert NotIncrementing();
    }

    uint128 lendingNonce = nonces[msg.sender].lending;
    uint128 borrowingNonce = nonces[msg.sender].borrowing;

    if (lending) {
        unchecked {
            nonces[msg.sender].lending = ++lendingNonce;
        }
    }

    if (borrowing) {
        unchecked {
            nonces[msg.sender].borrowing = ++borrowingNonce;
        }
    }

    // Emit the updated values from storage
    emit NoncesIncremented(nonces[msg.sender].lending, nonces[msg.sender].borrowing);
}