code-423n4 / 2022-04-jpegd-findings

1 stars 1 forks source link

Early Depositor can steal funds from subsequent depositors #208

Closed code423n4 closed 2 years ago

code423n4 commented 2 years ago

Lines of code

https://github.com/code-423n4/2022-04-jpegd/blob/e72861a9ccb707ced9015166fbded5c97c6991b6/contracts/vaults/yVault/yVault.sol#L142-L157

Vulnerability details

Impact

An early depositor to yVault.sol, preferably the first to deposit, will have the ability to steal funds from subsequent user deposits. The malicious user is able to do this by directly transferring tokens to either the yVault or Controller contracts following their deposit. If subsequent user deposits are less than the amount of token sent directly to the contracts, they will lose funds because their tokens will successfully transfer to the contract while they will be minted 0 shares

Proof of Concept

Steps to exploit:

        if (supply == 0) {
            shares = _amount;
        } else {
            shares = (_amount * supply) / balanceBefore;
        }

Where balanceBefore is equal to balance() which is equal to token.balanceOf(address(this)) + controller.balanceOf(address(token)) The formula for the example looks like:

shares = (5,000 ether * 1 share) / 10,000 ether = 0 shares

Tools Used

Manual review.

Recommended Mitigation Steps

Instead of relying on the balance of tokens of the two contracts, use internal accounting variables to represent the current balance of tokens that have been properly deposited and withdrawn from the vault. This will remove the ability to directly transfer tokens to the contracts to alter the share distribution logic.

spaghettieth commented 2 years ago

Duplicate of #12