No slippage protection for bonders
The bond function calculates the cost of a bond using a discount that depends on the fluctuating rDPX reserve, but the user cannot specify a maximum amount they are willing to pay. An attacker could manipulate the reserve or simply exploit normal fluctuations to make a bonder pay more WETH or rDPX than expected.
Submitted by dirk_y, also found by ladboy233, Evo, Brenzee, and T1MOH
https://github.com/code-423n4/2023-08-dopex/blob/main/contracts/core/RdpxV2Core.sol#L894-L913
https://github.com/code-423n4/2023-08-dopex/blob/main/contracts/core/RdpxV2Core.sol#L1156-L1165
When a user calls bond or bondWithDelegate they specify the amount of DpxEth they want to bond for. As part of this call the cost of the bond (in WETH and rDPX) is calculated. The discount provided for a bond depends on the bond discount factor and the amount of rDPX in the reserve. The bond discount factor should remain relatively static since it is modified only by the admin, but the amount of rDPX in the reserve is constantly fluctuating.
This is therefore a problem, because the discount at the time where the users tries to bond might be different from the actual discount when the transaction is included in a block since the rDPX in the reserve may have changed. Therefore the bonder will end up paying more WETH and/or rDPX for the given amount of bonds than expected.
Since the bonders are still receiving a discount I have lowered the severity of this report from “high” to “medium”, however I still consider this a bug because a bonder might expect/require a certain discount in order to make their whole trading strategy profitable.
Proof of Concept
For the sake of this report, let’s focus on the normal bond path:
function bond(
uint256 _amount,
uint256 rdpxBondId,
address _to
) public returns (uint256 receiptTokenAmount) {
_whenNotPaused();
// Validate amount
_validate(_amount > 0, 4);
// Compute the bond cost
(uint256 rdpxRequired, uint256 wethRequired) = calculateBondCost(
_amount,
rdpxBondId
);
IERC20WithBurn(weth).safeTransferFrom(
msg.sender,
address(this),
wethRequired
);
When calling bond, the amount of bond to mint is specified, and the cost of those bonds is then calculated with a call to calculateBondCost and the WETH transferred from the user (the rDPX is also transferred later). As you can see, there is no way for the user to specify the maximum amount of rDPX or WETH that they want to spend to purchase the bonds.
Now, let’s have a look at the first part of the discount calculation logic:
function calculateBondCost(
uint256 _amount,
uint256 _rdpxBondId
) public view returns (uint256 rdpxRequired, uint256 wethRequired) {
uint256 rdpxPrice = getRdpxPrice();
if (_rdpxBondId == 0) {
uint256 bondDiscount = (bondDiscountFactor *
Math.sqrt(IRdpxReserve(addresses.rdpxReserve).rdpxReserve()) *
1e2) / (Math.sqrt(1e18)); // 1e8 precision
As you can see, bondDiscount varies with bondDiscountFactor and the amount of rDPX in the reserve. Since the user has no control over the rDPX in the reserve, it is possible that this can change from block to block, thereby changing the discount and therefore the WETH & rDPX spent by the user.
This is now a classic “lack of slippage protection” bug that should be resolved as discussed below.
Recommended Mitigation Steps
The bond and bondWithDelegate should include an additional slippage argument to cap the amount of either WETH or rDPX that they want to spend. Since the ratio of rDPX to WETH is fixed you technically need only a maxAmount for one of these; I would suggest maxRDPX.
witherblock (Dopex) disagreed with severity and commented:
Avoided via adding slippage protection via token approvals, demoting to QA.
Alex the Entreprenerd (Judge) commented:
While the mitigation suggested by the Sponsor is technically correct, I don’t believe we can reasonably expect end users to use their allowance as a way to express pricing. This is due to the fact that such an operation would not be atomical.
With the information I have available, I believe it is reasonable to say that the execution price is not guaranteed, and since there seems to be a reasonable expectation that no router would be used, the code can cause a leak of value to the caller.
Leading me to believe that Medium Severity is most appropriate.
As mentioned before this can be mitigated via approval checks and will be added to the UI to allow users to know the max cost they would pay for a bond.
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 bond function lacks a slippage parameter to cap the cost, and the discount depends on a fluctuating reserve balance.
Impact
A bonder could pay more WETH and/or rDPX than expected, potentially making their trading strategy unprofitable.
What the attacker needed
None
The fix
The bond and bondWithDelegate functions should include a slippage argument, such as maxRDPX, to cap the amount the user is willing to spend.
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.
When you buy a bond, the price you pay depends on how much rDPX is in a reserve, which can change between when you send the transaction and when it is processed. The code does not let you set a maximum price, so you might end up paying more than you intended. This is like buying something online without a price guarantee.
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.