Skip to content
Business logic Audit finding Intermediate

ZcToken.withdraw will send user 0 tokens if called after maturity deadline but before market is set mature

The ZcToken contract's withdraw function calculates the amount to send using a maturityRate that can still be zero after the maturity deadline if no transactions have set it. An attacker or user calling withdraw at that time receives zero tokens despite the transaction succeeding.

panprog Follow

Originally disclosed on Code4rena

Submitted by panprog

https://github.com/code-423n4/2022-07-swivel/blob/fd36ce96b46943026cb2dfcb76dfa3f884f51c18/Tokens/ZcToken.sol#L99

https://github.com/code-423n4/2022-07-swivel/blob/fd36ce96b46943026cb2dfcb76dfa3f884f51c18/Tokens/ZcToken.sol#L92

Impact

If maturityRate is still 0 after maturity deadline (because no transactions setting maturityRate have been executed yet), then previewWithdraw calculated amount (used by ZcToken.withdraw function) is 0 and thus withdraw function will send 0 underlying tokens to user, which might be very confusing to user. Subsequent call to the same function will send him correct amount.

The same problem applies to all view functions in ZcToken contract - they use saved market maturityRate, which can be 0 even past deadline time and functions revert or return 0 in this case.

Incorrect withdrawal behaviour:

  1. Bob has some ZcTokens.
  2. Right at the time of maturity Bob tries to withdraw his underlying tokens by calling ZcToken.withdraw with some underlying amount.
  3. Instead of receiving corresponding amount, Bob receives nothing (but transaction still succeeds and he uses gas for it).

Proof of Concept

  1. withdraw: calculates previewAmount from previewWithdraw

https://github.com/code-423n4/2022-07-swivel/blob/fd36ce96b46943026cb2dfcb76dfa3f884f51c18/Tokens/ZcToken.sol#L99

  1. previewWithdraw: multiplication by maturityRate returns 0

https://github.com/code-423n4/2022-07-swivel/blob/fd36ce96b46943026cb2dfcb76dfa3f884f51c18/Tokens/ZcToken.sol#L92

Recommended Mitigation Steps

Add getMaturityRate function to ZcToken, which will return either market’s maturityRate or (if it’s 0) current market’s exchangeRate. Use this function instead of maturityRate everywhere across ZcToken.

JTraversa (Swivel) confirmed

robrobbins (Swivel) resolved:

Addressed: https://github.com/Swivel-Finance/gost/pull/421


The original report as published on Code4rena, by panprog. 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 contract uses a stored maturityRate that defaults to zero and is only set by certain transactions, without a fallback to the current exchange rate when it is zero.

Impact

A user withdrawing right after maturity could receive zero tokens instead of the correct amount, though a subsequent call would work.

What the attacker needed

User holds ZcTokens; Maturity deadline has passed; No prior transaction set maturityRate

The fix

The issue was fixed by adding a getMaturityRate function that returns the market's maturityRate or, if zero, the current exchangeRate, and using it everywhere in ZcToken.

How it was found

  1. Bob holds some ZcTokens.
  2. At the maturity time, Bob calls ZcToken.withdraw with an underlying amount.
  3. The withdraw function calls previewWithdraw, which multiplies by maturityRate (still zero) and returns zero.
  4. Bob receives zero tokens, but the transaction succeeds and he pays gas.

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 smart contract lets users withdraw tokens after a certain date. But if nobody has triggered a specific update before that date, the contract still thinks the exchange rate is zero, so it sends zero tokens even though the withdrawal appears to succeed. The user loses gas and gets nothing, though a second try would work.

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 panprog

View all

66 more disclosed reports, best first.

More from Swivel v3 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.