Skip to content
DoS Audit finding Intermediate

YearnTokenAdapter’s wrap can become stuck as it uses one step approval for an arbitrary underlying

The YearnTokenAdapter's wrap function uses a single-step approval for an arbitrary underlying token. If the token requires the allowance to be zero before setting a new positive value, and a leftover approval exists, the wrap function becomes permanently stuck.

hyh Follow

Originally disclosed on Code4rena

Submitted by hyh

Some tokens do not allow for approval of positive amount when allowance is positive already (to handle approval race condition, most known example is USDT).

This can cause the function to stuck whenever a combination of such a token and leftover approval be met. The latter can be possible if, for example, yearn vault is becoming full on a particular wrap() call and accepts only a part of amount, not utilizing the approval fully.

Then each next safeApprove will revert and wrap becomes permanently unavailable. Setting the severity to medium as depositing (wrapping) is core functionality for the contract and its availability is affected.

Proof of Concept

wrap use one step approve:

YearnTokenAdapter.sol#L30-L32

    function wrap(uint256 amount, address recipient) external override returns (uint256) {
        TokenUtils.safeTransferFrom(underlyingToken, msg.sender, address(this), amount);
        TokenUtils.safeApprove(underlyingToken, token, amount);

Some ERC20 forbid the approval of positive amount when the allowance is positive:

https://github.com/d-xo/weird-erc20#approval-race-protections

For example, USDT is supported by Yearn and can be the underlying asset:

https://yearn.finance/#/vault/0x7Da96a3891Add058AdA2E826306D812C638D87a7

Recommended Mitigation Steps

As the most general approach consider approving zero before doing so for the amount:

    function wrap(uint256 amount, address recipient) external override returns (uint256) {
        TokenUtils.safeTransferFrom(underlyingToken, msg.sender, address(this), amount);
+      TokenUtils.safeApprove(underlyingToken, token, 0);
        TokenUtils.safeApprove(underlyingToken, token, amount);

0xfoobar (Alchemix) confirmed

0xleastwood (judge) commented:

It seems like approve() will fail to execute on non-standard tokens which require the approval amount to start from zero. This is valid and should be updated to handle such tokens.


The original report as published on Code4rena, by hyh. 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

The wrap function calls safeApprove with a positive amount without first resetting the allowance to zero, which fails for tokens that enforce the approval race condition protection.

Impact

The wrap function becomes permanently unavailable for affected tokens, disrupting core deposit functionality.

What the attacker needed

Underlying token enforces zero-first approval; Leftover approval exists from a partial wrap

The fix

The wrap function should reset the approval to zero before setting the new amount.

How it was found

  1. Call wrap with an amount that is only partially used by the Yearn vault, leaving a positive allowance.
  2. Attempt to call wrap again with the same token.
  3. The safeApprove call reverts because the token requires the allowance to be zero before setting a new positive value.

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.

Some tokens, like USDT, do not allow you to change an existing approval directly to a new amount; you must first set it to zero. The wrap function tries to set a new approval without resetting to zero, so if a previous approval is left over, the function will always fail and users cannot wrap their tokens.

What is DoS?

A request that exhausts resources or crashes a component. Many programs forbid testing it; reports usually show it from code, not by attacking.

More by hyh

View all

137 more disclosed reports, best first.

More from Alchemix contest

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Denial of service

The lesson behind this report.

A request that exhausts resources or crashes a component. Many programs forbid testing it; reports usually show it from code, not by attacking.