Skip to content
Business logic Audit finding Advanced

UserManager's cancelVouchInternal breaks up voucher accounting on voucher removal

The _cancelVouchInternal function incorrectly updates the vouchees array using an index from voucherIndexes, and fails to update voucherIndexes when moving the last voucher. This can corrupt vouch accounting, potentially freezing a staker's funds or allowing a borrower to exploit unremovable trust.

seyni Follow with hyh

Originally disclosed on Sherlock

Source: https://github.com/sherlock-audit/2023-02-union-judging/issues/41

Found by

seyni, hyh

Summary

_cancelVouchInternal() doesn't update voucherIndices array and incorrectly updates vouchees array (using voucherIndexes for vouchees, which are unrelated index-wise), messing up the vouchers accounting as a result.

Vulnerability Detail

Currently _cancelVouchInternal() gets voucher array index from voucherIndexes, name it voucheeIdx and apply to vouchees, which isn't correct as those are different arrays, their indices are independent, so such addressing messes up the accounting data.

Also, proper update isn't carried out for voucher indices themselves, although it is required for the future referencing which is used in all voucher related activities of the protocol.

Impact

Immediate impact is unavailability of vouch accounting logic for the borrower-staker combination that was this last element cancelVouch() moved.

Furthermore, if a new entry is placed, which is a high probability event being a part of normal activity, then old entry will point to incorrect staked/locked amount, and a various violations of the related constrains become possible.

Some examples are: trust can be updated via updateTrust() to be less then real locked amount; vouch cancellation can become blocked (say new voucher borrower gains a long-term lock and old lock with misplaced index cannot be cancelled until new lock be fully cleared, i.e. potentially for a long time).

The total impact is up to freezing the corresponding staker's funds as this opens up a way for various exploitations, for example a old entry's borrower can use the situation of unremovable trust and utilize this trust that staker would remove in a normal course of operations, locking extra funds of the staker this way.

The whole issue is a violation of the core UNION vouch accounting logic, with misplaced indices potentially piling up. Placing overall severity to be high.

Code Snippet

_cancelVouchInternal() correctly treats vouchee entry removal update, but fails to do the same for vouchers case (first in the code):

https://github.com/sherlock-audit/2023-02-union/blob/main/union-v2-contracts/contracts/user/UserManager.sol#L583-L620

Solidity
    function _cancelVouchInternal(address staker, address borrower) internal {
        Index memory removeVoucherIndex = voucherIndexes[borrower][staker];
        if (!removeVoucherIndex.isSet) revert VoucherNotFound();

        // Check that the locked amount for this vouch is 0
        Vouch memory vouch = vouchers[borrower][removeVoucherIndex.idx];
        if (vouch.locked > 0) revert LockedStakeNonZero();

        // Remove borrower from vouchers array by moving the last item into the position
        // of the index being removed and then poping the last item off the array
        {
            // Cache the last voucher
            Vouch memory lastVoucher = vouchers[borrower][vouchers[borrower].length - 1];
            // Move the lastVoucher to the index of the voucher we are removing
            vouchers[borrower][removeVoucherIndex.idx] = lastVoucher;
            // Pop the last vouch off the end of the vouchers array
            vouchers[borrower].pop();
            // Delete the voucher index for this borrower => staker pair
            delete voucherIndexes[borrower][staker];
            // Update the last vouchers coresponsing Vouchee item
            uint128 voucheeIdx = voucherIndexes[borrower][lastVoucher.staker].idx;
            vouchees[staker][voucheeIdx].voucherIndex = removeVoucherIndex.idx.toUint96();
        }

        // Update the vouchee entry for this borrower => staker pair
        {
            Index memory removeVoucheeIndex = voucheeIndexes[borrower][staker];
            // Cache the last vouchee
            Vouchee memory lastVouchee = vouchees[staker][vouchees[staker].length - 1];
            // Move the last vouchee to the index of the removed vouchee
            vouchees[staker][removeVoucheeIndex.idx] = lastVouchee;
            // Pop the last vouchee off the end of the vouchees array
            vouchees[staker].pop();
            // Delete the vouchee index for this borrower => staker pair
            delete voucheeIndexes[borrower][staker];
            // Update the vouchee indexes to the new vouchee index
            voucheeIndexes[lastVouchee.borrower][staker].idx = removeVoucheeIndex.idx;
        }

Namely, voucherIndexes to be updated as lastVoucher has been moved and entries for its old position to be corrected on the spot. vouchees to be updated as well, but using correct voucheeIndexes index reference.

Tool used

Manual Review

Recommendation

Consider setting both voucherIndexes and vouchees items for the last staker:

https://github.com/sherlock-audit/2023-02-union/blob/main/union-v2-contracts/contracts/user/UserManager.sol#L591-L605

Solidity
        // Remove borrower from vouchers array by moving the last item into the position
        // of the index being removed and then poping the last item off the array
        {
            // Cache the last voucher
            Vouch memory lastVoucher = vouchers[borrower][vouchers[borrower].length - 1];
            // Move the lastVoucher to the index of the voucher we are removing
            vouchers[borrower][removeVoucherIndex.idx] = lastVoucher;
            // Pop the last vouch off the end of the vouchers array
            vouchers[borrower].pop();
            // Delete the voucher index for this borrower => staker pair
            delete voucherIndexes[borrower][staker];
-           // Update the last vouchers coresponsing Vouchee item
-           uint128 voucheeIdx = voucherIndexes[borrower][lastVoucher.staker].idx;
-           vouchees[staker][voucheeIdx].voucherIndex = removeVoucherIndex.idx.toUint96();
+           // Update the last vouchers coresponsing Vouchee item
+           vouchees[lastVoucher.staker][voucheeIndexes[borrower][lastVoucher.staker]].voucherIndex = removeVoucherIndex.idx.toUint96();
+           // Update the voucher indexes of the moved pair to the new voucher index
+           voucherIndexes[borrower][lastVoucher.staker].idx = removeVoucherIndex.idx;
        }

Discussion

kingjacob

Is this not also a dupe of #4 ?

dmitriia

Is this not also a dupe of #4 ?

#4 and #39 look to be partial reports of the #41/#31 issue, which basically has 2 parts.

#4 says 'vouchees[staker][voucheeIdx].voucherIndex = removeVoucherIndex.idx.toUint96() doesn't look to be correct', not seeing the original voucherIndexes issue and not suggesting the proper fix for the vouchees one.

#39, on the other hand, only observes and suggests the fix for the voucherIndexes part, failing to notice the vouchees issue.

The most correct way to reflect this, as I see it, is to mark #4 and #39 as Mediums and non-dups as they are pointing to the not really related issues.

#41/#31 looks to be full dups and High as both investigate the situation fully (as I see it at the moment), providing fixes for the both parts.

hrishibhat

@kingjacob Agree with the @dmitriia . Considering #4 & #39 as separate mediums.

kingjacob

https://github.com/unioncredit/union-v2-contracts/pull/105 PR also fixes the related 4,31,41,39

dmitriia

Looks ok

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

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 confuses two separate index arrays (voucherIndexes and voucheeIndexes) and fails to update voucherIndexes when moving the last voucher.

Impact

This could freeze a staker's funds or allow a borrower to exploit unremovable trust, as the accounting becomes inconsistent and constraints can be violated.

What the attacker needed

A staker has multiple vouchers; a voucher is cancelled

The fix

The code should be fixed by updating both voucherIndexes and vouchees correctly for the last staker, as shown in the recommendation.

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.

This is a smart contract bug where removing a voucher (a record of trust) from a list messes up the bookkeeping. The code uses the wrong list's index to update another list, so the records no longer match reality. This can lock funds or let someone keep trust they shouldn't have.

What is Business logic?

The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.

More from Union Finance Update

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Business logic flaws

The lesson behind this report.

The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.