Skip to content
Business logic Audit finding Intermediate

Potential economic attack on UDT grants to the referrer

The Unlock protocol rewards referrers with UDT tokens based on a hardcoded estimated gas cost, which can be exploited by using gas-saving tokens to make the actual gas cost much lower than the estimate, allowing an attacker to mint excess UDT. An attacker could profit by creating a lock with a gas-saving token and calling purchase many times.

WatchPug Follow

Originally disclosed on Code4rena

Submitted by WatchPug

In the current implementation, Unlock.sol#recordKeyPurchase() will send estimatedGasForPurchase * tx.gasprice worth of UDT to the referrer.

https://github.com/code-423n4/2021-11-unlock/blob/ec41eada1dd116bcccc5603ce342257584bec783/smart-contracts/contracts/Unlock.sol#L325-L325

uint tokensToDistribute = (estimatedGasForPurchase * tx.gasprice) * (125 * 10 ** 18) / 100 / udtPrice;

We believe there are multiple potential economic attack vectors to exploit this.

If estimatedGasForPurchase is misconfigured to a higher amount than the actual avg gas cost for a purchase call, or future network upgrades make the actual gas cost become lower than the configured estimatedGasForPurchase, it can be exploited simply by creating a lock and call purchase() many times to mint UDT.

Even if estimatedGasForPurchase is configured to an amount similar to the actual gas cost, a more sophisticated attack is still possible:

Proof of Concept

Given:

  • estimatedGasForPurchase is configured as 200,000;
  • The gas cost of a regular purchase call is about 200,000.

The attacker can create a lock contract and set the token address to a special gas saving token, which will SELFDESTRUCT to get a gas refund on transfer.

The attacker can:

  1. Mint gas saving token with gas price: 1 gwei;
  2. Call purchase() and use 48 contract slots with gas price: 1000 gwei;

Total gas saved will be ~0.8 ETH (or other native tokens, eg. BNB, MATIC). Therefore, the attacker will profit ~0.8 ETH worth of UDT.

See: https://gastoken.io/

Recommendation

Consider setting a global daily upper limit of total UDT grants to referrers, plus, an upper limit for UDT minted per purchase.

julien51 (Unlock Protocol) acknowledged, but disagreed with severity and commented:

If estimatedGasForPurchase is misconfigured to a higher amount than the actual avg gas cost for a purchase call, or future network upgrades make the actual gas cost become lower than the configured estimatedGasForPurchase, it can be exploited simply by creating a lock and call purchase() many times to mint UDT.

Absolutely but considering the security model, the admin indeed have full control over the protocol. We are thinking about finding a mechanism to not hardcode gas spent but use the actual number eventually. When we do that we should consider the impact of things like gas-token (even though EIP1559 has probably made them mostly impractical?).

At this point given that the gas spent is hardcoded, there is a de-facto cap on how much UDT they could earn (based on the token price).

0xleastwood (judge) commented:

While I agree with the warden, there is potential for value extraction, however, it does require the admin to be unaware about upcoming network upgrades.

As the sponsor has noted, they will be moving towards a dynamic estimatedGasForPurchase value, however, from the perspective of the c4 contest, this doesn’t change the outcome of my decision.

As the protocol may leak value based on certain network assumptions, I’ll mark this as medium severity.

2 — Med (M): vulns have a risk of 2 and are considered “Medium” severity when assets are not at direct risk, but the function of the protocol or its availability could be impacted, or leak value with a hypothetical attack path with stated assumptions, but external requirements.

Please note: the following additional discussions and re-assessment took place approximately 2 months after judging and awarding were finalized. As such, this report will leave this finding in its originally assessed risk category as it simply reflects a snapshot in time.

julien51 (Unlock Protocol) commented:

I don’t think the protocl can “leak” value based on that. The tokens that are used to compute GDP and distribute tokens have to be approved by the DAO (right now only USDC, DAI and BAT have been approved on mainnet, and only USDC on Polygon). I don’t think the DAO would approve gas tokens givem that indeed they could result in leakage of UDT, so I think it is minor.

0xleastwood (judge) decreased severity to Low and commented:

Considering the sponsor’s comments, I actually agree that this is less likely than initially stated. Similar to the SafeERC20 issue, it isn’t expected that gas saving tokens will be approved to compute and distribute UDT tokens. I’ll downgrade this to low.

The original report as published on Code4rena, by WatchPug. Images and videos are our own copies; raw HTML is shown as text. View it on Code4rena.

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 UDT grant to referrers is calculated using a hardcoded estimated gas cost rather than the actual gas used, so any discrepancy can be exploited.

Impact

The protocol could leak value by granting more UDT than the actual gas cost, but this requires the admin to approve a gas-saving token and may be limited by the hardcoded estimate.

What the attacker needed

Admin approves a gas-saving token; Attacker can create a lock; Attacker can call purchase()

The fix

The protocol should set a global daily upper limit on total UDT grants to referrers and an upper limit per purchase, and eventually use the actual gas spent instead of a hardcoded estimate.

How it was found

  1. Mint a gas-saving token with a low gas price.
  2. Create a lock contract using that token.
  3. Call purchase() many times using high gas price and multiple contract slots to maximize gas refunds.
  4. Profit from the difference between the UDT grant and the actual gas cost.

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.

The protocol pays referrers an amount of its own token (UDT) that is supposed to cover the gas cost of a purchase. Gas is the fee paid to process a transaction on the blockchain. If the actual gas cost is lower than the fixed estimate, the referrer gets more UDT than they spent, which can be repeated to make a profit.

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.

More by WatchPug

View all

197 more disclosed reports, best first.

More from Unlock Protocol contest

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Business logic flaws

The lesson behind this report.

The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.