Skip to content
Business logic Audit finding Intermediate

NibblVault: In the buy function, users can avoid paying fees

In the NibblVault contract's buy function, when a purchase spans both the secondary and primary curves, the fee for the secondary curve portion is not charged. An attacker could avoid paying fees on that part of the purchase, reducing revenue for the curator and admin.

cccz Follow

Originally disclosed on Code4rena

Submitted by cccz, also found by kenzo, Lambda, WatchPug, xiaoming90, and zzzitron

In the buy function of the NibblVault contract, when msg.value > _lowerCurveDiff, the fee for SecondaryCurve part is not charged.

            if (_lowerCurveDiff >= msg.value) {
                _purchaseReturn = _buySecondaryCurve(msg.value, _totalSupply);
            } else {
                //Gas Optimization
                _purchaseReturn = _initialTokenSupply - _totalSupply;
                secondaryReserveBalance += _lowerCurveDiff;
                // _purchaseReturn = _buySecondaryCurve(_to, _lowerCurveDiff);
                _purchaseReturn += _buyPrimaryCurve(msg.value - _lowerCurveDiff, _totalSupply + _purchaseReturn);
            }

Proof of Concept

https://github.com/code-423n4/2022-06-nibbl/blob/8c3dbd6adf350f35c58b31723d42117765644110/contracts/NibblVault.sol#L314-L323

Recommended Mitigation Steps

The fee for the SecondaryCurve part is complex to charge. I implemented the _caculateFeeSecondaryCurve and _reverseFeeSecondaryCurve functions to do the relevant calculations.

The _caculateFeeSecondaryCurve function is used to calculate the value after the fee is charged, but not to actually charge the fee. The _reverseFeeSecondaryCurve function is used to calculate the value before the fee is charged.

            if (_lowerCurveDiff >= _caculateFeeSecondaryCurve(msg.value)) {
                _purchaseReturn = _buySecondaryCurve(msg.value, _totalSupply);
            } else {
                //Gas Optimization
                _purchaseReturn = _initialTokenSupply - _totalSupply;
                secondaryReserveBalance += _lowerCurveDiff;
                uint256 _amount = _reverseFeeSecondaryCurve(_lowerCurveDiff);
                _chargeFeeSecondaryCurve(_amount);
                // _purchaseReturn = _buySecondaryCurve(_to, _lowerCurveDiff);
                _purchaseReturn += _buyPrimaryCurve(msg.value - _amount, _totalSupply + _purchaseReturn);
            }
    function _caculateFeeSecondaryCurve(uint256 _amount) private returns(uint256) {
       address payable _factory = factory;
        uint256 _adminFeeAmt = NibblVaultFactory(_factory).feeAdmin();
        uint256 _feeAdmin = (_amount * _adminFeeAmt) / SCALE ;
        uint256 _feeCurator = (_amount * curatorFee) / SCALE ;
        return _amount - (_feeAdmin + _feeCurator);
    }
    function _reverseFeeSecondaryCurve(uint256 _amount) private returns(uint256) {
       address payable _factory = factory;
        uint256 _adminFeeAmt = NibblVaultFactory(_factory).feeAdmin();
        return _amount * SCALE / (SCALE - (_adminFeeAmt + curatorFee));
    }

mundhrakeshav (Nibbl) acknowledged, but disagreed with severity

HardlyDifficult (judge) commented:

Failing to collect fees is a form of leaking value for the curator and therefore falls into the Medium risk definition — keeping the severity as reported.


The original report as published on Code4rena, by cccz. 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 branch that handles purchases crossing the curve boundary updates the reserve and token supply directly without applying the secondary curve fee.

Impact

The curator and admin could lose fee income on the secondary curve portion of cross-curve purchases.

What the attacker needed

None

The fix

The fee for the secondary curve portion should be calculated and charged before moving to the primary curve, as suggested with helper functions.

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 for buying tokens along a price curve. When a purchase crosses from one pricing curve to another, the code forgets to take the usual fee on the first part. So a buyer pays less than they should, and the fee collector loses out.

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 cccz

View all

104 more disclosed reports, best first.

More from Nibbl 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.