Skip to content
Smart contracts Audit finding Advanced

Past state query results are susceptible to manipulation due to multiple states with same block number

The protocol stores multiple history entries with the same block number for some state types, so functions that query past states by block number can return an imprecise or attacker-chosen entry. A malicious user could manipulate the number of entries to make a binary search return a desired past state.

rayn Follow

Originally disclosed on Code4rena

Submitted by rayn, also found by IllIllI and Czar102

HolyPaladinToken.sol#L466 HolyPaladinToken.sol#L492 HolyPaladinToken.sol#L644 HolyPaladinToken.sol#L663 HolyPaladinToken.sol#L917 HolyPaladinToken.sol#L961 HolyPaladinToken.sol#L993 HolyPaladinToken.sol#L1148 HolyPaladinToken.sol#L1164 HolyPaladinToken.sol#L1184 HolyPaladinToken.sol#L1199 HolyPaladinToken.sol#L1225 HolyPaladinToken.sol#L1250 HolyPaladinToken.sol#L1260 HolyPaladinToken.sol#L1287 HolyPaladinToken.sol#L1293 HolyPaladinToken.sol#L1324 HolyPaladinToken.sol#L1352 HolyPaladinToken.sol#L1357

4 kinds of states (UserLock, TotalLock, Checkpoint, DelegateCheckpoint) are maintained in the protocol to keep record of history. For functions that query history states, target block number is used as an index to search for the corresponding state.

However, 3 (DelegateCheckpoint, TotalLock, UserLocks) out of the 4 states are allowed to have multiple entries with same fromBlock, resulting in a one-to-many mapping between block number and history entry. This makes queried results at best imprecise, and at worst manipulatable by malicious users to present an incorrect history.

Proof of Concept

Functions that query history states including _getPastLock, getPastTotalLock, _getPastDelegate perform a binary search through the array of history states to find entry matching queried block number. However, the searched arrays can contain multiple entries with the same fromBlock.

For example the _lock function pushes a new UserLock to userLocks[user] regardless of previous lock block number.

    function _lock(address user, uint256 amount, uint256 duration, LockAction action) internal {
        require(user != address(0)); //Never supposed to happen, but security check
        require(amount != 0, "hPAL: Null amount");
        uint256 userBalance = balanceOf(user);
        require(amount <= userBalance, "hPAL: Amount over balance");
        require(duration >= MIN_LOCK_DURATION, "hPAL: Lock duration under min");
        require(duration <= MAX_LOCK_DURATION, "hPAL: Lock duration over max");

        if(userLocks[user].length == 0){
            ...
        }
        else {
            // Get the current user Lock
            uint256 currentUserLockIndex = userLocks[user].length - 1;
            UserLock storage currentUserLock = userLocks[user][currentUserLockIndex];
            // Calculate the end of the user current lock
            uint256 userCurrentLockEnd = currentUserLock.startTimestamp + currentUserLock.duration;

            uint256 startTimestamp = block.timestamp;

            if(currentUserLock.amount == 0 || userCurrentLockEnd < block.timestamp) {
                // User locked, and then unlocked
                // or user lock expired

                userLocks[user].push(UserLock(
                    safe128(amount),
                    safe48(startTimestamp),
                    safe48(duration),
                    safe32(block.number)
                ));
            }
            else {
                // Update of the current Lock : increase amount or increase duration
                // or renew with the same parameters, but starting at the current timestamp
                require(amount >=  currentUserLock.amount,"hPAL: smaller amount");
                require(duration >=  currentUserLock.duration,"hPAL: smaller duration");

                // If the method is called with INCREASE_AMOUNT, then we don't change the startTimestamp of the Lock

                userLocks[user].push(UserLock(
                    safe128(amount),
                    action == LockAction.INCREASE_AMOUNT ? currentUserLock.startTimestamp : safe48(startTimestamp),
                    safe48(duration),
                    safe32(block.number)
                ));
                ...
            }
        ...
    }

This makes the history searches imprecise at best. Additionally, if a user intends to shadow his past states from queries through public search functions, it is possible to control the number of entries precisely such that binsearch returns the entry he wants to show.

Tools Used

vim, ganache-cli

Recommended Mitigation Steps

Adopt the same strategy as checkpoint, and modify last entry in array instead of pushing new one if it fromBlock == block.number.

Kogaroshi (Paladin) confirmed, resolved, and commented:

Changes made in the PR: PaladinFinance/Paladin-Tokenomics#7.

0xean (judge) commented:

Agree with severity assigned in this issue. Assets are not directly compromised by this vulnerability, which would be required for 3.


The original report as published on Code4rena, by rayn. Images and videos are our own copies; raw HTML is shown as text. View it on Code4rena.

Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.

How it worked

Root cause

Likely the code pushes a new history entry even when an entry for the same block number already exists, creating duplicate block numbers that break the assumption of a one-to-one mapping used by binary search.

Impact

Past state queries could return incorrect or attacker-chosen history, but assets are not directly compromised.

What the attacker needed

Ability to create multiple locks in one block

The fix

The issue was fixed by updating the last entry instead of pushing a new one when the block number matches, as done for checkpoints.

Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.

Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.

A smart contract keeps a history of changes, each tagged with the block number when it happened. If several changes happen in the same block, the history can have several entries with the same block number. When someone asks 'what was the state at block X?', the contract may pick the wrong entry, and a user can arrange entries so it picks the one they want.

What is Smart contracts?

Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.

More by rayn

View all

12 more disclosed reports, best first.

More from Paladin - Warden Pledges contest

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Smart contract vulnerabilities

The lesson behind this report.

Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.