Skip to content
Business logic Audit finding Intermediate

Underflow Vulnerability in `Market::_allowedBorrow` Function: Oversight with Pearlmit Allowance Handling

Tendency Follow

Originally disclosed on Sherlock

Source: https://github.com/sherlock-audit/2024-02-tapioca-judging/issues/29

Found by

Tendency

Summary

The protocol permits users to authorize spenders using the MarketERC20::approveBorrow function, and also includes support for allowances granted through the Pearlmit contract. However, an oversight in the _allowedBorrow function leads to an underflow issue when spenders utilize Pearlmit allowances, rendering them unable to execute borrowing actions despite having the necessary permission.

Vulnerability Detail

Protocol users can approve a spender via MarketERC20::approveBorrow function, to perform certain actions like borrow, repay or adding of collateral on their behalf. Whenever the spender calls any of these functionalities, down the execution _allowedBorrow is invoked to check if the caller is allowed to borrow share from from, and then decrease the spender's allowance by the share amount.

Solidity
    function _allowedBorrow(address from, uint256 share) internal virtual override {
        if (from != msg.sender) {
            // TODO review risk of using this
            (uint256 pearlmitAllowed,) = penrose.pearlmit().allowance(from, msg.sender, address(yieldBox), collateralId);
            require(allowanceBorrow[from][msg.sender] >= share || pearlmitAllowed >= share, "Market: not approved");
            if (allowanceBorrow[from][msg.sender] != type(uint256).max) {
                allowanceBorrow[from][msg.sender] -= share;
            }
        }
    }

The problem here is, _allowedBorrow will always revert due to an underflow whenever the spender is given an allowance in the Pearlmit contract.

  • To Illustrate

Assuming we have two users, Bob and Alice, since Pearlmit allowance is also accepted, Alice grants Bob a borrowing allowance of 100 tokens for the collateral id using Pearlmit. Note that Bob's allowance in the Market contract for Alice will be zero(0) and 100 in Pearlmit.

When Bob tries to borrow an amount equal to his Pearlmit allowance, down the borrow logic _allowedBorrow is called, in _allowedBorrow function, the below requirement passes, since the returned pearlmitAllowed for Bob will equal 100 shares

Solidity
 require(allowanceBorrow[from][msg.sender] >= share || pearlmitAllowed >= share, "Market: not approved");

Remember Bob's allowance in the Market contract for Alice is 0, but 100 in Pearlmit, but _allowedBorrow function erroneously attempts to deduct the share from Bob's Market allowance, which will thus result in an underflow revert(0 - 100).

Solidity
            if (allowanceBorrow[from][msg.sender] != type(uint256).max) {
                allowanceBorrow[from][msg.sender] -= share;
            }

Impact

Although giving a spender allowance via Pearlmit will appear to be supported, the spender cannot carry out any borrowing action in the Market.

Code Snippet

https://github.com/sherlock-audit/2024-02-tapioca/blob/dc2464f420927409a67763de6ec60fe5c028ab0e/Tapioca-bar/contracts/markets/Market.sol#L416-L425

Tool used

Manual Review

Recommendation

After ensuring that the user has got the approval, return when permission from Pearlmit is used:

Solidity
    function _allowedBorrow(address from, uint256 share) internal virtual override {
        if (from != msg.sender) {
            // TODO review risk of using this
            (uint256 pearlmitAllowed,) = penrose.pearlmit().allowance(from, msg.sender, address(yieldBox), collateralId);
            require(allowanceBorrow[from][msg.sender] >= share || pearlmitAllowed >= share, "Market: not approved");
+            if (pearlmitAllowed != 0) return;
            if (allowanceBorrow[from][msg.sender] != type(uint256).max) {
                allowanceBorrow[from][msg.sender] -= share;
            }
        }
    }

Or remove support for Pearlmit allowance

Discussion

sherlock-admin2

1 comment(s) were left on this issue during the judging contest.

takarez commented:

this seem valid, the pearlmit allowance should be deducted instead of the market one; medium(7)

cryptotechmaker

Fixed in https://github.com/Tapioca-DAO/Tapioca-bar/pull/349

sherlock-admin4

The protocol team fixed this issue in PR/commit https://github.com/Tapioca-DAO/Tapioca-bar/pull/349.

The original report as published on Sherlock, by Tendency. Images and videos are our own copies; raw HTML is shown as text. View it on Sherlock.

Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.

What is known

A medium-severity business logic bug in Tapioca. Sherlock made it public on 1 April 2024. Found in the 170 audit contest (finding M-3).

About Business logic

The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.

Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.

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 Tendency

View all

2 more disclosed reports, best first.

More from Tapioca

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.