Lybra Finance
The report describes two bugs in the Lybra Finance smart contract: one where liquidation logic uses the wrong variable, allowing more than the intended 50% of debt to be liquidated, and another where the contract does not enforce a 7-day waiting period before a redemption provider can cancel. An attacker could liquidate more collateral than expected or cancel redemption provider status immediately, contrary to the documentation.
medium.com
Published on medium.com
Smart Contract Security
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
Lybra Finance is a DeFi protocol whose smart contract implements a lending and liquidation model. Two behaviours are defined in its documentation and enforced (or not) by the contract:
- Liquidation limit. When a borrower's collateral ratio falls between 125% and 150%, the liquidation mechanism should allow liquidating up to 50% of the borrower's remaining debt.
- Redemption provider commitment. A redemption provider must offer their services for a minimum of 7 days before being allowed to cancel that status.
The write-up examines both behaviours against the contract logic and finds that the code does not match the documentation in either case.
Setup
The report does not describe any accounts, roles, tooling, versions or deployment details. The work is a source and documentation review: the expected behaviour is taken from the Lybra Finance documentation, and the actual behaviour from the contract code.
Finding it
The starting point in both cases is the documentation. Reading it carefully gives a specification to compare the code against, and in both places the code diverges from that specification.
Bug 1: liquidation beyond the expected limit
The documentation says liquidation should be capped at 50% of a borrower's remaining debt when the collateral ratio is between 125% and 150%. In the code, the calculation of the maximum liquidation amount uses the depositedEther variable instead of the borrowed variable. Because the cap is derived from the wrong quantity, more than the intended 50% of the debt can be liquidated.
Bug 2: cancellation without the minimum waiting period
The documentation states that a redemption provider must serve for at least 7 days before being allowed to cancel. The contract logic does not enforce this waiting period at all, so a user can cancel redemption provider status immediately, contradicting the intended behaviour of the protocol.
Exploitation
The report does not include a step-by-step exploitation walkthrough, requests, payloads or transaction traces. It describes the two defects at the level of code logic:
- Liquidation cap. A liquidator acting on a position whose collateral ratio sits between 125% and 150% can liquidate more than 50% of the remaining debt, because the maximum liquidation amount is computed from
depositedEtherrather thanborrowed. - Redemption provider cancellation. A user who has become a redemption provider can cancel that status straight away, without waiting the documented 7 days, because no check on elapsed time exists in the contract.
Root cause
- Bug 1. The maximum liquidation amount is calculated from the wrong state variable. The intended basis is the borrower's debt (
borrowed); the code uses the deposited collateral (depositedEther). The report does not describe any other check that would have constrained the result, so the incorrect variable is the root cause. - Bug 2. The minimum service period exists only in the documentation. The contract logic contains no enforcement of the 7 day waiting period, so nothing prevents immediate cancellation.
In both cases the divergence is between the written specification and the implementation, not a failure of a defence that the report describes.
Impact
- Bug 1. More than the intended 50% of a borrower's debt can be liquidated when the collateral ratio is between 125% and 150%. The report frames this as affecting borrowers' expectations and the protocol's risk management.
- Bug 2. Users can cancel redemption provider status instantly instead of committing for the documented 7 days, contradicting the intended behaviour of the protocol.
The report does not assign a severity or CVSS score to either finding.
Fix
For Bug 1, the recommendation was to change the code logic so that the borrowed variable, rather than depositedEther, is used when determining the maximum liquidation amount. This aligns the protocol with the intended behaviour and gives borrowers the expected level of protection.
For Bug 2, the recommendation was to add a waiting period check in the becomeRedemptionProvider function. The suggested approach is a mapping variable, redemptionProviderStart, storing the time at which the user became a redemption provider, so that the contract can verify whether 7 days have elapsed before allowing cancellation.
The Lybra Finance team responded that the issues were documentation mistakes. Their reply, quoted in the report:
Thank you for the reminder. It’s a documentation error. We have actually waived the time restriction for that feature, but failed to update the documentation in a timely manner.
However, we appreciate the hard work you’ve spent on this submission and would like to reward you with $800 LBR as a token of appreciation.
The team awarded $800 for each finding. The report's closing lesson is that an auditor should read the documentation carefully, because it describes the expected behaviour, intended functionality and specific requirements of the protocol being audited.
Variants to test
- Specification versus code on liquidation maths. For any lending protocol, read the documented liquidation cap and trace which state variable feeds the cap calculation. A positive result is a cap computed from collateral or another quantity where the specification says it should be based on debt, allowing more than the documented share to be liquidated.
- Time-locked roles and statuses. List every role or status the documentation says is time-bound (minimum service periods, cooldowns, lock-ups) and check whether the corresponding function enforces the elapsed time. A positive result is a cancellation or exit path with no timestamp check, or a check against a timestamp that is never set.
- Documentation drift after feature changes. Where a team says a restriction was waived, confirm whether the contract still contains the old check or the documentation still describes it. A positive result is a mismatch in either direction, which is worth reporting even when the code is the intended behaviour.
- Start-time bookkeeping. For any suggested fix that stores a start time, check that the variable is written on entry, read on exit, and cannot be reset to bypass the waiting period. A positive result is a path that overwrites or skips the stored start time.
What happened, in plain words
Lybra Finance is a piece of software that runs on a blockchain. It lets people borrow digital money by putting up something valuable as a guarantee, a bit like a pawn shop: you leave a valuable item, you get cash, and if you do not pay back, the shop keeps the item. In this kind of software the guarantee is called collateral and the cash you receive is a loan. The rules of the system are written down in two places: in a set of documents (the documentation) that describes how it is supposed to behave, and in the actual computer code that runs it. When those two disagree, something is wrong.
A security researcher read the documentation of Lybra Finance, then read the code, and found two places where the code does not do what the documentation promises. In the first, a rule that was supposed to protect borrowers from losing too much at once was calculated from the wrong number, so more of a borrower's debt could be taken than intended. In the second, a rule that said a certain role had to be held for at least 7 days before it could be given up was simply not enforced by the code at all.
The researcher reported both. The Lybra Finance team replied that the issues were mistakes in the documentation rather than in the code, and paid $800 for each finding. The lesson the researcher drew is simple: read the documentation carefully, because it tells you what the code is supposed to do.
The background you need
Before the two bugs make sense, a few ideas need unpacking.
What a smart contract is
A smart contract is a program that lives on a blockchain. Once it is deployed, anyone can send it instructions, and it runs exactly as written, with no person in the middle deciding whether to approve. Think of a vending machine: you press a button, it follows its internal rules, and nobody behind a counter can bend those rules for you. The catch is that if the rules written into the machine are wrong, the machine still follows them faithfully. There is no shop assistant to notice the mistake.
What a DeFi protocol is
DeFi stands for decentralised finance: financial services built out of smart contracts instead of banks. A protocol here just means the collection of contracts and rules that together make up one such service. Lybra Finance is a DeFi protocol that implements a lending and liquidation model.
Borrowing, collateral and the collateral ratio
When you borrow in a system like this, you first deposit something of value as security. The collateral ratio compares the value of what you deposited with the value of what you borrowed. If you deposit something worth 150 and borrow 100, your ratio is 150%. A higher ratio means more cushion. If the value of your deposit falls, the ratio drops, and the system becomes exposed to the risk that you simply walk away and leave the debt unpaid.
What liquidation is
Liquidation is the system's way of protecting itself when a borrower's cushion gets too thin. Someone else, called a liquidator, is allowed to repay part of the borrower's debt and in exchange take some of the borrower's collateral, usually at a favourable price. This is meant to be a controlled, limited action, not a free-for-all, because the borrower still deserves some protection. That is why the documentation sets a cap on how much can be taken at once.
What a redemption provider is
A redemption provider is a role inside the protocol. The documentation describes it as a commitment: someone who takes on the role must offer their services for a minimum of 7 days before they are allowed to cancel it. The idea is stability. If people could sign up and drop out at will, the service would be unreliable.
Why documentation matters in an audit
An audit, in this context, is a careful review of a codebase looking for mistakes and weaknesses. The documentation is the closest thing an auditor has to a statement of intent: it says what the code is supposed to do. Comparing the two is one of the most productive ways to find bugs, because every disagreement is either a bug in the code or a stale document, and both are worth reporting.
Setting the scene
The report does not describe any accounts, roles, tooling, versions or deployment details. The work is a source and documentation review: the expected behaviour is taken from the Lybra Finance documentation, and the actual behaviour from the contract code. In other words, no special software was needed beyond the ability to read the documents and the code side by side.
Finding it
The starting point in both cases is the documentation. Reading it carefully gives a specification to compare the code against, and in both places the code diverges from that specification. A specification is simply a written description of what something must do; here it is the documentation's description of the protocol's rules.
Bug 1: liquidation beyond the expected limit
The documentation says liquidation should be capped at 50% of a borrower's remaining debt when the collateral ratio is between 125% and 150%. In plain terms: if your cushion has slipped into that band, a liquidator may take at most half of what you still owe, no more.
In the code, the calculation of the maximum liquidation amount uses the depositedEther variable instead of the borrowed variable. A variable is a named storage slot in a program, a labelled box holding a value. depositedEther holds the amount of collateral the borrower put in; borrowed holds the amount of debt the borrower took out. The cap is supposed to be a share of the debt, so it should be computed from borrowed. Because the cap is derived from the wrong quantity, more than the intended 50% of the debt can be liquidated.
Why does this matter? Because the cap is supposed to be a share of the debt, and it is computed from a different quantity instead. The borrower expected to lose at most half of their debt in that band; in practice more can be taken.
Bug 2: cancellation without the minimum waiting period
The documentation states that a redemption provider must serve for at least 7 days before being allowed to cancel. The contract logic does not enforce this waiting period at all, so a user can cancel redemption provider status immediately, contradicting the intended behaviour of the protocol.
To enforce a waiting period, a program needs to remember when the person started and compare that with the current time. The report says no such check exists in the contract, so there is nothing to compare and nothing to stop an immediate exit.
Exploitation
The report does not include a step-by-step exploitation walkthrough, requests, payloads or transaction traces. It describes the two defects at the level of code logic:
- Liquidation cap. A liquidator acting on a position whose collateral ratio sits between 125% and 150% can liquidate more than 50% of the remaining debt, because the maximum liquidation amount is computed from
depositedEtherrather thanborrowed. - Redemption provider cancellation. A user who has become a redemption provider can cancel that status straight away, without waiting the documented 7 days, because no check on elapsed time exists in the contract.
In both cases the person taking the action is simply using the contract as it is written. No trickery, no forged messages, no stolen credentials: the contract itself permits the behaviour. That is what makes this class of bug interesting. The weakness is in the rules, not in the lock on the door.
Root cause
- Bug 1. The maximum liquidation amount is calculated from the wrong state variable. A state variable is a value the contract keeps stored between actions, part of its permanent record. The intended basis is the borrower's debt (
borrowed); the code uses the deposited collateral (depositedEther). The report does not describe any other check that would have constrained the result, so the incorrect variable is the root cause. - Bug 2. The minimum service period exists only in the documentation. The contract logic contains no enforcement of the 7 day waiting period, so nothing prevents immediate cancellation.
In both cases the divergence is between the written specification and the implementation, not a failure of a defence that the report describes.
Impact
- Bug 1. More than the intended 50% of a borrower's debt can be liquidated when the collateral ratio is between 125% and 150%. The report frames this as affecting borrowers' expectations and the protocol's risk management.
- Bug 2. Users can cancel redemption provider status instantly instead of committing for the documented 7 days, contradicting the intended behaviour of the protocol.
The report does not assign a severity or CVSS score to either finding. Severity is a label for how serious a flaw is, and CVSS is a standard scoring system for it; neither is given here.
Fix
For Bug 1, the recommendation was to change the code logic so that the borrowed variable, rather than depositedEther, is used when determining the maximum liquidation amount. This aligns the protocol with the intended behaviour and gives borrowers the expected level of protection. The change is small on paper: swap one box for another in the calculation. The effect is that the cap once again measures a share of the debt, which is what the documentation promised.
For Bug 2, the recommendation was to add a waiting period check in the becomeRedemptionProvider function. A function is a named block of code that does one job when called; this one is the code path that makes someone a redemption provider. The suggested approach is a mapping variable, redemptionProviderStart, storing the time at which the user became a redemption provider, so that the contract can verify whether 7 days have elapsed before allowing cancellation. A mapping is a lookup table inside a contract: you give it a key (here, presumably the user) and it returns a stored value (here, the start time). With that start time recorded, the cancellation path can subtract it from the current time and refuse the request if fewer than 7 days have passed.
The Lybra Finance team responded that the issues were documentation mistakes. Their reply, quoted in the report:
Thank you for the reminder. It’s a documentation error. We have actually waived the time restriction for that feature, but failed to update the documentation in a timely manner.
However, we appreciate the hard work you’ve spent on this submission and would like to reward you with $800 LBR as a token of appreciation.
The team awarded $800 for each finding. LBR is the token in which the reward was paid. The report's closing lesson is that an auditor should read the documentation carefully, because it describes the expected behaviour, intended functionality and specific requirements of the protocol being audited.
Note what the reply implies: for the time restriction, the team says the code was doing what they wanted and the document was out of date. That is still a real finding, because anyone reading the documentation, including users and other developers, would be misled about how the protocol behaves. A mismatch is worth reporting whichever side is wrong.
Variants to test
These are patterns to look for in other codebases, not steps performed in this report.
- Specification versus code on liquidation maths. For any lending protocol, read the documented liquidation cap and trace which state variable feeds the cap calculation. A positive result is a cap computed from collateral or another quantity where the specification says it should be based on debt, allowing more than the documented share to be liquidated.
- Time-locked roles and statuses. List every role or status the documentation says is time-bound (minimum service periods, cooldowns, lock-ups) and check whether the corresponding function enforces the elapsed time. A positive result is a cancellation or exit path with no timestamp check, or a check against a timestamp that is never set.
- Documentation drift after feature changes. Where a team says a restriction was waived, confirm whether the contract still contains the old check or the documentation still describes it. A positive result is a mismatch in either direction, which is worth reporting even when the code is the intended behaviour.
- Start-time bookkeeping. For any suggested fix that stores a start time, check that the variable is written on entry, read on exit, and cannot be reset to bypass the waiting period. A positive result is a path that overwrites or skips the stored start time.
Glossary
- Audit: a careful review of a codebase looking for mistakes and weaknesses.
- Blockchain: a shared record of transactions that many computers keep in sync, so no single party can quietly rewrite it.
- Borrowed: the variable holding the amount of debt a borrower took out.
- Collateral: something of value deposited as security against a loan.
- Collateral ratio: the value of a borrower's collateral compared with the value of their debt, expressed as a percentage.
- Contract logic: the rules actually written into a smart contract's code.
- CVSS: a standard scoring system for rating the severity of a security flaw.
- DeFi: decentralised finance, financial services built from smart contracts rather than run by a bank.
- DepositedEther: the variable holding the amount of collateral a borrower deposited.
- Documentation: the written description of what a system is supposed to do.
- Function: a named block of code that performs one job when called.
- LBR: the token in which the reward in this report was paid.
- Liquidation: the process by which a liquidator repays part of a risky loan and takes some of the borrower's collateral in exchange.
- Liquidator: the party who performs a liquidation.
- Mapping: a lookup table inside a contract that returns a stored value for a given key.
- Protocol: the collection of contracts and rules that together make up one service.
- Redemption provider: a role in the protocol that the documentation says must be held for at least 7 days before it can be cancelled.
- RedemptionProviderStart: the suggested mapping that would store when a user became a redemption provider.
- Severity: a label for how serious a flaw is.
- Smart contract: a program that lives on a blockchain and runs exactly as written, with no intermediary.
- Specification: a written description of what something must do, used as the yardstick for judging the code.
- State variable: a value a contract keeps stored between actions, part of its permanent record.
- Variable: a named storage slot in a program holding a value.