Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ExchangeHelpers: in setMaxAllowance, safeApprove shouldn't be used #50

Open
code423n4 opened this issue Nov 14, 2021 · 1 comment
Open
Assignees
Labels
1 (Low Risk) Assets are not at risk. State handling, function incorrect as to spec, issues with comments bug Something isn't working sponsor confirmed Sponsor agrees this is a problem and intends to fix it (OK to use w/ "disagree with severity")

Comments

@code423n4
Copy link
Contributor

Handle

PierrickGT

Vulnerability details

Impact

In setMaxAllowance, safeApprove is used to increase allowance. As stated in the following Pull Request, safeApprove has been deprecated in favor of safeIncreaseAllowance and safeDecreaseAllowance.

https://github.com/OpenZeppelin/openzeppelin-contracts/pull/2268/files

This is because safeApprove shouldn't check for allowance, as explained in the issue below:

OpenZeppelin/openzeppelin-contracts#2219

approve is actually vulnerable to a sandwich attack as explained in the following document and this check for allowance doesn't actually avoid it.

https://docs.google.com/document/d/1YLPtQxZu1UAvO9cZ1O2RPXBbT0mooh4DYKjA_jp-RLM/edit

Proof of Concept

safeIncreaseAllowance should be used to increase allowance and safeDecreaseAllowance to decrease allowance to 0. We can also gain in code clarity by refactoring the if else statement and calling _token.safeIncreaseAllowance(_spender, type(uint256).max); only once.

Recommended Mitigation Steps

The following changes are recommended.

function setMaxAllowance(IERC20 _token, address _spender) internal {
    uint256 _currentAllowance = _token.allowance(address(this), _spender);

    if (_currentAllowance != type(uint256).max) {
        // Decrease to 0 first for tokens mitigating the race condition
        _token.safeDecreaseAllowance(_spender, _currentAllowance);
    }

    _token.safeIncreaseAllowance(_spender, type(uint256).max);
}
@code423n4 code423n4 added 1 (Low Risk) Assets are not at risk. State handling, function incorrect as to spec, issues with comments bug Something isn't working labels Nov 14, 2021
code423n4 added a commit that referenced this issue Nov 14, 2021
@maximebrugel maximebrugel added the duplicate This issue or pull request already exists label Nov 18, 2021
@maximebrugel maximebrugel removed the duplicate This issue or pull request already exists label Nov 18, 2021
@maximebrugel
Copy link
Collaborator

Changed the main issue from 70 to 50

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
1 (Low Risk) Assets are not at risk. State handling, function incorrect as to spec, issues with comments bug Something isn't working sponsor confirmed Sponsor agrees this is a problem and intends to fix it (OK to use w/ "disagree with severity")
Projects
None yet
Development

No branches or pull requests

2 participants