[web3] APWine Incorrect Check of Delegations Bugfix Review. This is a postmortem of a vulnerability… | by Immunefi | Jul, 2022 | Medium
A missing check in the PT token burn logic allowed an attacker to bypass the delegation limit, inflating their delegated yield. By repeating deposits and withdrawals, the attacker could later mint and redeem tokens to steal yield from the protocol.
medium.com
Published on medium.com
Bug Bounty
The full write-up lives on its author's site
It belongs to its author, so we link to it rather than copy it. The other two tabs are our own rewrites, checked against it.
Context
APWine is a protocol for tokenising future yield. It stores Interest Bearing Tokens (IBTs), or any other yield-bearing asset, in a smart contract for a fixed period and issues Future Yield Tokens (FYTs) in exchange. The contract receives the yield generated by those assets directly, and only FYT holders can redeem that yield at the end of the period.
The core split is between Principal Tokens (PTs) and FYTs. PTs represent a user's deposit to the protocol. At the start of each period, APWine generates FYTs from PTs in a 1:1 ratio. Users who deposit PTs into the FutureVault receive FYT tokens on a regular basis in each new period.
A second feature is delegation of FYT generation. A depositor can delegate the FYTs generated by their PTs to another address, which gives that address access to the yield of the asset without giving it access to the principal. Delegation power is transferred in the beforeTokenTransfer() function of the PT token.
The PT token itself is a standard ERC20 with some additional logic. The interesting part is beforeTokenTransfer(), which contains a require check ensuring a user cannot delegate more yield than the PTs they hold plus what they have already delegated.

Setup
No special tooling is described beyond a working proof of concept. The exploit uses ordinary protocol calls and requires:
- A fresh account to make the initial deposit into the
FutureVaultcontract. - The ability to call
createFYTDelegationTowith the attacker address as the recipient. - The ability to call the withdraw function to redeem PTs for IBTs.
- Patience across several periods, since the inflated delegation only produces yield over time.
The report says the bug could lead to theft of the yield after two periods, i.e. 6 months.
Finding it
The bug is a missing condition during the burn of PT tokens. In beforeTokenTransfer(), the if block that updates delegation state is guarded by a check that the destination is not the zero address:
to != address(0)
As required by the ERC20 standard, burning tokens emits a transfer event to the zero address. The implementation calls the transfer hooks, but the balance of the zero address is never increased. The _burn function invokes the hook as:
_beforeTokenTransfer(account, address(0), amount);
Because to is address(0) during a burn, the to != address(0) condition fails and the code inside the if is never executed. That code is where the delegation accounting lives, so a burn skips the check that the amount being removed is consistent with the user's PT balance and their delegated amount.

The require check is meant to stop a user delegating more yield than the PTs they hold and have already delegated. The if condition is also meant to avoid updating state for the vault address and the null address. The last part, not allowing the transfer of a user's tokens to address zero, is what breaks the accounting during a burn.
Exploitation
Step 1: Deposit IBTs into the FutureVault
Make a deposit to the FutureVault contract using the deposit function. The deposit is standard: it takes the IBTs and calculates the PTs to be minted. The deposit must come from a fresh account, for the reason explained in the next step.

Step 2: Delegate FYT creation from the fresh account
Call createFYTDelegationTo and specify the attacker address as the recipient. This updates totalDelegationsReceived[_receiver], where _receiver is always the attacker address.

Inside the function, the contract first gets the total delegation amount of the depositor and compares the balance of PT tokens against the amount to be delegated plus the amount already delegated. Delegation therefore has to be made from fresh accounts to bypass this check: a fresh account holds PTs that have not yet been counted against a delegation.
Step 3: Withdraw, redeeming PTs for IBTs
Call the withdraw function. PT.burnFrom is called in the FutureVault contract during the withdrawal process.

The function withdraws funds corresponding to the PT holding of an address. It calculates how much is owed to the user, including all the interest from the periods they took part in, and sends back the IBTs. Because the _beforeTokenTransfer if condition does not pass during the burn, the contract fails to check whether the user's PT balance is greater than or equal to the amount ready to be withdrawn plus the delegated PT amount. The delegated amount from the previous step is not taken into consideration.
The attacker receives back all the PTs deposited in step 1 while keeping the delegation amount intact in totalDelegationsReceived[_receiver].
Step 4: Repeat
Repeat steps 1 to 3 multiple times. Each cycle leaves the delegation accounting inflated without a corresponding PT balance behind it.
Step 5: Wait
Wait a few periods for the inflated delegation to generate yield.
Step 6: Mint and redeem
Mint PTs and FYTs and call withdraw to redeem these tokens to IBTs. The inflated delegation has by then produced yield that the attacker can claim.
The full sequence, as given in the report:
- Call
createFYTDelegationTofrom a fresh account and specify the attacker address as the recipient.totalDelegationsReceived[_receiver]is updated. Delegation needs to happen from the fresh account to bypass the balance check. - Call the withdraw function, redeeming PTs for IBTs. Due to the logic error in
_beforeTokenTransfer()the check for the amount being redeemed against the redeem balance plus delegated tokens is not run. The attacker receives all PTs deposited in step 1 while keeping the delegation amount. - Repeat steps 1 to 3 multiple times.
- Mint PTs and FYTs and call withdraw to redeem these tokens to IBT.
Root cause
The delegation accounting is updated inside a block guarded by to != address(0). A burn passes address(0) as the destination, so the guard suppresses the accounting update exactly when tokens leave a user's balance through PT.burnFrom. The require that should compare the user's PT balance with the amount being withdrawn plus the delegated amount is therefore skipped during withdrawal.
The balance-versus-delegation require is skipped during withdrawal because the burn bypasses the block that contains it. The ERC20 burn convention of emitting a transfer to the zero address is what triggers the gap.
Impact
An attacker can inflate totalDelegationsReceived for their own address without holding matching PTs, then wait for the delegation to generate yield and redeem it. In effect the yield generated by the protocol can be stolen after two periods, i.e. 6 months. The attacker gets back the principal they deposited at each cycle while retaining the delegation, so the operation can be repeated to inflate the position further.
The whitehat who reported it, setuid0 of SSLab@Gatech, submitted a critical report with a working PoC on January 5th, and was rewarded $100,000. The APWine team paid the bounty 4 days after the report was submitted to Immunefi.
Fix
The fix was deployed to the APWine repository. It checks the delegated amount inside the _withdraw function.
Variants to test
- Other hooks guarded by a zero-address check. Look for
beforeTokenTransfer,_beforeTokenTransferor similar hooks where state updates sit behindto != address(0)orfrom != address(0). A positive result is a burn or mint path that skips accounting the protocol relies on. - Delegation and voting accounting on burn. Any contract that tracks delegated amounts, votes or shares per address and updates them in a transfer hook is a candidate. Burn tokens from an account that has delegated and check whether the delegated total stays unchanged while the balance drops.
- Withdrawal paths that rely on hook-side checks. Where a withdraw function assumes the transfer hook has already validated balances against delegated amounts, test whether a direct burn path reaches the same state. A positive result is a withdrawal that succeeds while the delegated amount remains counted.
- Repeatable inflation loops. Where a deposit, delegate, withdraw cycle returns the principal while leaving a delegation entry behind, check whether the cycle can be repeated to compound the inflated figure. A positive result is a monotonically growing delegation balance with no matching principal.
- Mint paths with the same guard. Check whether minting, which also commonly routes through the hook with
from == address(0), skips any accounting in the same way.
What happened, in plain words
APWine is a service that runs on a blockchain (a shared, public record of transactions that many computers keep in sync). You give it a digital asset that earns interest over time, and it gives you tokens in return. A token here is a small digital receipt recorded on the blockchain, not a password or a login code. APWine splits the value of your deposit into two kinds of receipt: one for the money you put in, and one for the interest it will earn in the future. The idea is that you can keep the principal and hand the future interest to someone else, or the other way round.
On January 5th, a security researcher using the name setuid0, from a group called SSLab@Gatech, sent APWine a bug report through the Immunefi bug bounty platform. A bug bounty programme is a public offer: a company says it will pay anyone who finds a flaw in its software and reports it privately instead of abusing it. The researcher included a working proof of concept, meaning a small demonstration that actually carries out the attack. The flaw was simple: when one particular kind of APWine token was destroyed (a "burn"), one check was skipped. That skipped check let someone build up a fake claim on future interest, and after two periods, about 6 months, that fake claim could be turned into real interest taken from the protocol.
APWine paid the researcher $100,000, four days after the report was submitted to Immunefi.

The rest of this article walks through how the protocol works, where the check was missing, how the attack was carried out step by step, and how it was fixed.
Background: what APWine does
Before the bug makes sense, you need to know what the product does.
APWine tokenises future yield. "Yield" is the interest an asset earns over time. "Tokenise" means turning something into a token on the blockchain so it can be held, transferred and redeemed by software. APWine takes an Interest Bearing Token (IBT), or any other asset that earns interest, and holds it in a smart contract for a fixed period. A smart contract is a program that lives on the blockchain and runs automatically when someone calls one of its functions. In exchange for the deposited asset, APWine issues Future Yield Tokens (FYTs). The contract collects the interest the asset produces, and only the people holding FYTs are allowed to redeem that interest at the end of the period.
The central feature is the split of a yield-bearing asset into two tokens:
- Principal Tokens (PTs) represent the user's deposit. They stand for the money itself, the principal.
- Future Yield Tokens (FYTs) represent the interest that deposit will earn.
At the start of each period, APWine creates FYTs from PTs in a 1:1 ratio: one PT produces one FYT. Users who deposit PTs into the FutureVault contract receive FYT tokens regularly, once per new period. The FutureVault is the contract that holds the deposited assets and keeps track of who owns what.
A second feature is delegation of FYT generation. Delegation means naming another address as the beneficiary of something. An address on a blockchain is like a bank account number: a string that can hold tokens and that someone controls with a private key. A depositor can delegate the FYTs generated by their PTs to another address. That gives the other address access to the yield of the asset without giving it access to the principal. A simple comparison: it is like putting a friend's name on the interest payments of your savings account while the savings themselves stay yours.
The delegation power is recorded in the beforeTokenTransfer() function of the PT token. A function is a named block of code inside a contract; calling it runs that code. beforeTokenTransfer() is a hook, meaning a function the token contract runs automatically just before any transfer of tokens happens, so the contract can update its records first.
The PT token itself is a standard ERC20 with some extra logic. ERC20 is the common standard for tokens on Ethereum-like blockchains: it defines a fixed set of functions such as transferring tokens, checking a balance and approving someone else to spend on your behalf. Because it is a standard, wallets and other contracts know how to interact with any ERC20 token. The interesting part for us is beforeTokenTransfer(), which contains a require check. require is a Solidity (smart contract language) instruction that stops execution and undoes everything if its condition is false. This particular require makes sure a user cannot delegate more yield than the PTs they hold plus what they have already delegated.
Finding the bug
The bug is a missing condition during the burn of PT tokens. Burning a token means destroying it: the total supply shrinks and the holder's balance goes down. In beforeTokenTransfer(), the if block that updates the delegation records is guarded by a check that the destination is not the zero address:
to != address(0)
Line by line: to is the destination address of the transfer, the account receiving the tokens. address(0) is the zero address, a special address made of all zeros that nobody controls. != means "is not equal to". So the whole line reads: "the destination is not the zero address". The if block only runs when this is true.
Why does the zero address matter? As required by the ERC20 standard, burning tokens emits a transfer event to the zero address. An event is a log entry the blockchain keeps so that outside software can see what happened. The usual convention is that a burn looks like a transfer to the zero address, because the tokens go nowhere. This implementation follows that convention: it calls the transfer hooks, but the balance of the zero address is never increased. The _burn function invokes the hook as:
_beforeTokenTransfer(account, address(0), amount);
Line by line: _burn is the internal function that destroys tokens. It calls _beforeTokenTransfer with three arguments. account is the address whose tokens are being destroyed. address(0) is the destination, the zero address, because the tokens are going nowhere. amount is how many tokens are being destroyed. So during a burn, the destination passed to the hook is always the zero address.
Because to is address(0) during a burn, the to != address(0) condition is false and the code inside the if is never executed. That code is where the delegation accounting lives. Accounting here means keeping the running totals: how much each address has delegated and how much it has received. So a burn skips the check that the amount being removed is consistent with the user's PT balance and their delegated amount.

The require check is meant to stop a user delegating more yield than the PTs they hold and have already delegated. The if condition is also meant to avoid updating records for the vault address and the null address (another name for the zero address). The last part, not allowing the transfer of a user's tokens to address zero, is what breaks the accounting during a burn.
A useful comparison: imagine a club that keeps a list of how many guest passes each member has handed out. The rule is that you cannot hand out more passes than the tickets you hold. The clerk updates the list every time a member gives a pass to someone. But the clerk's instruction says "only update the list when the pass goes to a real person". When a member's tickets are cancelled (burned), the clerk treats it as a pass going to nobody, so the list is not updated. The member's tickets disappear, but the record of passes handed out stays. That is exactly the shape of this bug.
Carrying out the attack
Step 1: Deposit IBTs into the FutureVault
Make a deposit to the FutureVault contract using the deposit function. The deposit is standard: it takes the IBTs and calculates the PTs to be minted. Minting means creating new tokens. The deposit must come from a fresh account, meaning a newly created address with no history, for the reason explained in the next step.

Why a fresh account? Because the delegation check in step 2 compares the amount being delegated against the PT balance the account already holds and has already delegated. A brand new account starts with a clean slate, so the numbers line up in the attacker's favour.
Step 2: Delegate FYT creation from the fresh account
Call createFYTDelegationTo and specify the attacker address as the recipient. This updates totalDelegationsReceived[_receiver], where _receiver is always the attacker address. In plain terms, the contract writes down that the attacker's address is entitled to a certain amount of future yield.

Inside the function, the contract first gets the total delegation amount of the depositor and compares the balance of PT tokens against the amount to be delegated plus the amount already delegated. Delegation therefore has to be made from fresh accounts to bypass this check: a fresh account holds PTs that have not yet been counted against a delegation, so the comparison passes.
Step 3: Withdraw, redeeming PTs for IBTs
Call the withdraw function. PT.burnFrom is called in the FutureVault contract during the withdrawal process. burnFrom is the function that destroys tokens taken from a given account.

The function withdraws funds corresponding to the PT holding of an address. It calculates how much is owed to the user, including all the interest from the periods they took part in, and sends back the IBTs. Because the _beforeTokenTransfer if condition does not pass during the burn, the contract fails to check whether the user's PT balance is greater than or equal to the amount ready to be withdrawn plus the delegated PT amount. The delegated amount from the previous step is not taken into consideration.
The result: the attacker receives back all the PTs deposited in step 1 while keeping the delegation amount intact in totalDelegationsReceived[_receiver]. In other words, the attacker gets their money back and still holds a claim on future yield that is no longer backed by any deposit.
Step 4: Repeat
Repeat steps 1 to 3 multiple times. Each cycle leaves the delegation accounting inflated without a corresponding PT balance behind it. Inflated here means the recorded entitlement is larger than what the attacker actually deposited.
Step 5: Wait
Wait a few periods for the inflated delegation to generate yield. Yield accrues over time, so the fake entitlement only turns into real value once enough periods have passed.
Step 6: Mint and redeem
Mint PTs and FYTs and call withdraw to redeem these tokens to IBTs. Redeeming means exchanging a token for the underlying asset it represents. The inflated delegation has by then produced yield that the attacker can claim.
The full sequence, as given in the report:
- Call
createFYTDelegationTofrom a fresh account and specify the attacker address as the recipient.totalDelegationsReceived[_receiver]is updated. Delegation needs to happen from the fresh account to bypass the balance check. - Call the withdraw function, redeeming PTs for IBTs. Due to the logic error in
_beforeTokenTransfer()the check for the amount being redeemed against the redeem balance plus delegated tokens is not run. The attacker receives all PTs deposited in step 1 while keeping the delegation amount. - Repeat steps 1 to 3 multiple times.
Root cause
The delegation accounting is updated inside a block guarded by to != address(0). A burn passes address(0) as the destination, so the guard suppresses the accounting update exactly when tokens leave a user's balance through PT.burnFrom. The require that should compare the user's PT balance with the amount being withdrawn plus the delegated amount is therefore skipped during withdrawal.
The balance-versus-delegation require is skipped during withdrawal because the burn bypasses the block that contains it. The ERC20 burn convention of emitting a transfer to the zero address is what triggers the gap.
Impact
An attacker can inflate totalDelegationsReceived for their own address without holding matching PTs, then wait for the delegation to generate yield and redeem it. In effect the yield generated by the protocol can be stolen after two periods, i.e. 6 months. The attacker gets back the principal they deposited at each cycle while retaining the delegation, so the operation can be repeated to inflate the position further.
The whitehat who reported it, setuid0 of SSLab@Gatech, submitted a critical report with a working PoC on January 5th, and was rewarded $100,000. The APWine team paid the bounty 4 days after the report was submitted to Immunefi.
Fix
The fix was deployed to the APWine repository. It checks the delegated amount inside the _withdraw function. In plain terms: instead of trusting the automatic hook that a burn skips, the withdrawal code now performs the check itself, at the moment the money leaves.
Variants to test
If you want to look for similar bugs yourself, these are the patterns worth checking:
- Other hooks guarded by a zero-address check. Look for
beforeTokenTransfer,_beforeTokenTransferor similar hooks where state updates sit behindto != address(0)orfrom != address(0). A positive result is a burn or mint path that skips accounting the protocol relies on. - Delegation and voting accounting on burn. Any contract that tracks delegated amounts, votes or shares per address and updates them in a transfer hook is a candidate. Burn tokens from an account that has delegated and check whether the delegated total stays unchanged while the balance drops.
- Withdrawal paths that rely on hook-side checks. Where a withdraw function assumes the transfer hook has already validated balances against delegated amounts, test whether a direct burn path reaches the same state. A positive result is a withdrawal that succeeds while the delegated amount remains counted.
- Repeatable inflation loops. Where a deposit, delegate, withdraw cycle returns the principal while leaving a delegation entry behind, check whether the cycle can be repeated to compound the inflated figure. A positive result is a monotonically growing delegation balance with no matching principal.
- Mint paths with the same guard. Check whether minting, which also commonly routes through the hook with
from == address(0), skips any accounting in the same way.
Glossary
- Address: a string on a blockchain that identifies an account and can hold tokens, controlled with a private key.
- Bug bounty programme: a public offer by a company to pay people who find and privately report flaws in its software.
- Burn: destroying tokens, reducing the total supply and the holder's balance.
- Delegation: naming another address as the beneficiary of something, such as future yield, without handing over the principal.
- ERC20: the common standard for tokens on Ethereum-like blockchains, defining functions such as transfer and balance checks.
- Event: a log entry the blockchain keeps so outside software can see what happened.
- Fresh account: a newly created address with no transaction history.
- Function: a named block of code inside a contract that runs when called.
- Future Yield Token (FYT): a token representing the interest a deposit will earn in a future period.
- Hook: a function a contract runs automatically at a set point, such as just before a token transfer.
- Immunefi: the bug bounty platform where the report was submitted.
- Inflated: larger than it should be, in this case a recorded entitlement bigger than the actual deposit.
- Interest Bearing Token (IBT): an asset that earns interest over time.
- Mint: creating new tokens.
- Period: a fixed stretch of time in APWine during which yield accrues; two periods are about 6 months.
- Principal: the money originally deposited, as opposed to the interest it earns.
- Principal Token (PT): a token representing a user's deposit to the protocol.
- Proof of concept (PoC): a small working demonstration that an attack is possible.
- Protocol: in this context, a set of smart contracts that together provide a service.
- Redeem: exchange a token for the underlying asset it represents.
- require: a Solidity instruction that stops execution and undoes everything if its condition is false.
- Smart contract: a program that lives on a blockchain and runs automatically when called.
- Solidity: the programming language used to write smart contracts on Ethereum-like blockchains.
- Token: a digital receipt recorded on a blockchain.
- Whitehat: a security researcher who finds flaws and reports them responsibly instead of abusing them.
- Yield: the interest an asset earns over time.
- Zero address: the special address made of all zeros, used as the destination when tokens are burned.