Approved gscApprove allowance to an address may not able to be decreased
The gscApprove function decreases the internal gscAllowance by the full approved amount each time, so if a spender already has an allowance, calling gscApprove again to reduce that allowance causes an underflow and reverts. An attacker could not directly exploit this, but it prevents the GSC_CORE_VOTING_ROLE from decreasing an approved allowance.
Juntao Follow with Juntao, UniversalCrypto and 5 others
Originally disclosed on Code4rena
Submitted by Juntao, also found by Juntao, UniversalCrypto, vangrim, Jiamin (1, 2), crunch (1, 2), zaevlad, auditsea, circlelooper, and lanrebayode77
Proof of Concept
GSC_CORE_VOTING_ROLE calls the gscApprove(...) function to approve tokens to be pulled from the treasury:
function gscApprove(
address token,
address spender,
uint256 amount
) external onlyRole(GSC_CORE_VOTING_ROLE) nonReentrant {
if (spender == address(0)) revert T_ZeroAddress("spender");
if (amount == 0) revert T_ZeroAmount();
// Will underflow if amount is greater than remaining allowance
gscAllowance[token] -= amount;
_approve(token, spender, amount, spendThresholds[token].small);
}
Each approval will decrease the gscAllowance[token] by the approved allowance amount, this is problematic as GSC_CORE_VOTING_ROLE may not able to decrease the approved allowance.
Consider the following scenario:
gscAllowance[token]is 100.GSC_CORE_VOTING_ROLEcallsgscApprove(...)to give 60 allowance to a third party.gscAllowance[token]is 40 now;- Later,
GSC_CORE_VOTING_ROLEfinds the approved allowance is a bit too high and wants to decrease the allowance to 50. gscApprove(...)is called again, but the transaction reverts due to an underflow error (gscAllowance[token]is less than 50).
Please see the tests:
contract ArcadeTreasuryTest is Test {
address timelock = address(1);
address coreVoting = address(2);
address gscCoreVoting = address(3);
address other = address(4);
ArcadeTreasury treasury;
MockToken mockToken;
function setUp() public {
treasury = new ArcadeTreasury(timelock);
vm.startPrank(timelock);
treasury.grantRole(treasury.CORE_VOTING_ROLE(), coreVoting);
treasury.grantRole(treasury.GSC_CORE_VOTING_ROLE(), gscCoreVoting);
mockToken = new MockToken("Mock Token", "MT");
IArcadeTreasury.SpendThreshold memory threshold = IArcadeTreasury.SpendThreshold(100 ether, 200 ether, 300 ether);
treasury.setThreshold(address(mockToken), threshold);
vm.stopPrank();
}
function testGscApprove() public {
vm.warp(7 days);
vm.prank(timelock);
treasury.setGSCAllowance(address(mockToken), 100 ether);
vm.startPrank(gscCoreVoting);
treasury.gscApprove(address(mockToken), address(4), 60 ether);
vm.expectRevert(stdError.arithmeticError);
treasury.gscApprove(address(mockToken), address(4), 50 ether);
vm.stopPrank();
}
}
contract MockToken is ERC20 {
constructor(string memory name_, string memory symbol_) ERC20(name_, symbol_) {}
function mint(address account, uint256 amount) public {
_mint(account, amount);
}
}
Recommended Mitigation Steps
When approving a particular address, compare the new allowance with the current allowance (IERC20.allowance(…)):
- If the current allowance is equal to the new allowance, do nothing.
- If the current allowance is less than the new allowance,
gscAllowance[token] -= (new allowance - current allowance). - If the current allowance is larger than the new allowance,
gscAllowance[token] += (current allowance - new allowance).
Assessed type
Access Control
An already approved allowance can not be decreased. Might be an expected behavior.
Because in this report, the focus is inner accounting
gscAllowance, not the external ERC20 allowance Issue #85. Here, the approved allowance seems different from the “ERC20 approved allowance”. Here, it can be seen as an account payable, the amount is spent, but the spender has not taken it yet.Or, if the designed behavior is to also provide a way to revoke the allowance, this inner accounting needs some improvement.
To my understanding, the issue is rooted in the un-sync of internal and external allowance accounting.
Issue #85 also talks about the allowance issue. It also focuses more on ERC20 approved allowance override, which seems better pointing out the potential impact of with this mechanism. Some way to revoke the allowance, if the allowance is overwritten.
PowVT (Arcade.xyz) disagreed with severity and confirmed
0xean (judge) decreased severity to Medium and commented:
Based on my current understanding, I think Medium is the correct severity here.
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 function subtracts the new approval amount from the internal allowance without accounting for the existing allowance already granted to the spender.
Impact
The GSC_CORE_VOTING_ROLE cannot decrease an approved allowance, which may leave excessive spending permissions in place.
What the attacker needed
GSC_CORE_VOTING_ROLE privileges; existing allowance granted to a spender
The fix
The function should compare the new allowance with the current allowance and adjust gscAllowance by the difference, rather than subtracting the full new amount.
How it was found
- Set gscAllowance for a token to 100.
- Call gscApprove to give 60 allowance to a third party; gscAllowance becomes 40.
- Later, call gscApprove again to reduce the allowance to 50; the transaction reverts due to underflow.
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 a function that approves spending also reduces an internal budget. If you try to lower an existing approval, the budget is already reduced, so the maths goes negative and the transaction fails. This means the budget manager cannot correct a too-high approval.
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.