Underflow Vulnerability in `Market::_allowedBorrow` Function: Oversight with Pearlmit Allowance Handling
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.
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
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).
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
Tool used
Manual Review
Recommendation
After ensuring that the user has got the approval, return when permission from Pearlmit is used:
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.
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.