Skip to content
Business logic Audit finding Intermediate

One can become referral of hash 0x0 and because all users default referral hash is 0x0 so he would become all users referral by default and earn a lot of fees while users didn’t approve it

The referral system treated the zero hash as a valid referral code, and since all users default to a zero referral hash, the first person to register that hash became the referrer for every user who had not set a referral. That attacker then received referral fees from those users' trades without their approval.

unforgiven Follow

Originally disclosed on Code4rena

Submitted by unforgiven

https://github.com/code-423n4/2022-12-tigris/blob/588c84b7bb354d20cbca6034544c4faa46e6a80e/contracts/Referrals.sol#L20-L24

https://github.com/code-423n4/2022-12-tigris/blob/588c84b7bb354d20cbca6034544c4faa46e6a80e/contracts/TradingExtension.sol#L148-L152

Impact

By default the value of _referred[user] is 0x0 for all users and if one set 0x0 as his referral hash then he would become referral for all the users who didn’t set referral by default and he would earn a lot of referral funds that users didn’t approve it.

Proof of Concept

This is createReferralCode() code:

    function createReferralCode(bytes32 _hash) external {
        require(_referral[_hash] == address(0), "Referral code already exists");
        _referral[_hash] = _msgSender();
        emit ReferralCreated(_msgSender(), _hash);
    }

As you can see, attacker can become set 0x0 as his hash referral by calling createReferralCode(0x0) and code would set _referral[0x0] = attackerAddress (attacker needs to be the first one calling this).

Then in the getRef() code the logic would return attackerAddress as referral for all the users who didn’t set referral.

    function getRef(
        address _trader
    ) external view returns(address) {
        return referrals.getReferral(referrals.getReferred(_trader));
    }

In the code, getReferred(trader) would return 0x0 because trader didn’t set referred and getReferral(0x0) would return attackerAddress.

_handleOpenFees() and _handleCloseFees() function in the Trading contract would use getRef(trader) and they would transfer referral fee to attackerAddress and attacker would receive fee from a lot of users which didn’t set any referral, those users didn’t set any referral and didn’t approve attacker receiving referral fees from them and because most of the users wouldn’t know about this and referral codes so attacker would receive a lot of funds.

Tools Used

VIM

Recommended Mitigation Steps

Prevent someone from setting 0x0 hash for their referral code.

TriHaz (Tigris Trade) confirmed and commented:

It is valid but I’m not 100% sure it should be a High risk. Would like an opinion from a judge.

Alex the Entreprenerd (judge) decreased severity to Medium and commented:

The Warden has shown how, due to an incorrect assumption, the first claimer to the 0 hash will receive referral fees for all non-referred users.

Because the finding creates a negative externality and shows a way to extract value from what would be assumed to be the null value, I believe the finding to be of Medium Severity.

I’d recommend the Sponsor either mitigate or set themselves as the 0 hash recipient as a way to receive default fees.

GainsGoblin (Tigris Trade) resolved:

Mitigation: https://github.com/code-423n4/2022-12-tigris/pull/2#issuecomment-1419175772


The original report as published on Code4rena, by unforgiven. 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

The contract allowed the zero hash to be registered as a referral code even though zero is also the default 'no referrer' value for every user.

Impact

The attacker could earn referral fees from every user who had not chosen a referrer, without those users approving it; the judge rated this Medium severity.

What the attacker needed

Attacker is first to register the zero hash; victims trade without setting a referral

The fix

The project mitigated the issue, and the judge also suggested either blocking the zero hash or assigning it to the protocol itself so default fees go to the sponsor.

How it was found

  1. Call the referral creation function with the zero hash before anyone else does.
  2. The contract stores the attacker's address as the owner of the zero hash.
  3. When any user without a referral trades, the lookup resolves their empty referral to the attacker.
  4. The fee handling functions send the referral portion of the fee to the attacker.

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 referral code is a label that tells a trading platform who should get a cut of the fees. The platform used an empty value (zero) to mean 'no referrer', but it also let someone register that empty value as their own code. Because most users never pick a referrer, their trades pointed at the empty value, which now belonged to the attacker, so the attacker collected fees from them.

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 by unforgiven

View all

50 more disclosed reports, best first.

More from Tigris Trade contest

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.