# SGL and BB repay do not round up both on allowance spending and elastic amount

Tapioca SGL/BB repay rounds shares-to-amount down instead of up, letting an attacker repay dust parts repeatedly to erase debt at a discount.

- Payout: Contest reward
- Severity: medium
- Program: Tapioca
- Credited: hyh
- Disclosed: 2024-02-14
- Write-up quality: 6/10
- Original: https://github.com/sherlock-audit/2024-02-tapioca-judging/issues/150
- Library page: https://bugbountylibrary.com/r/sherlock%3A2024-02-tapioca%3AM-36
- Tags: domain:web3 impact:funds-loss kind:contest platform:sherlock sev:medium tech:evm tech:solidity technique:regression technique:source-review vuln:precision-loss

**Target:** SGL/BB lending markets repay function (Solidity)
**Root cause:** _repay calls totalBorrow.sub(part, false) for both allowance spending and elastic amount, rounding the asset amount down in the user's favor instead of up in the protocol's favor.
**Steps:**
1. Call repay with a dust `part` (e.g. `part = 1 wei`) where `_totalBorrow.sub(part, false)` yields `amount = 1 wei` though true value is ~1.9 wei.
2. Ensure allowanceShare != 0 to bypass `if (allowanceShare == 0) revert AllowanceNotValid()`.
3. Repeat many times so 1.9x worth of asset debt is repaid with 1x worth of asset paid.
**How found:** Reviewer compared repay rounding direction against Abracadabra Cauldron/degenBox implementations (which round up on repay) and traced the mitigation commit that generalized rounding changes, spotting that `false` was left in the repay amount calculation.
**Exploitation:** Attacker repeatedly repays minimal parts, each time paying a rounded-down asset amount while a larger debt share is written off, accumulating a discount until debt is effectively closed for free; viable on L2 where tx costs are low relative to asset value.
**Insight:** When auditing share/amount rebase conversions, check every `sub(part, roundUp)` call site individually: a prior fix that generalized rounding can leave repay paths rounding down, and comparing against sibling protocols (Abracadabra Cauldrons) reveals the intended direction.
