Submitted by xiaoming90, also found by shenwilly, unforgiven, and WatchPug
https://github.com/code-423n4/2022-05-rubicon/blob/8c312a63a91193c6a192a9aab44ff980fbfd7741/contracts/rubiconPools/BathPair.sol#L213
https://github.com/code-423n4/2022-05-rubicon/blob/8c312a63a91193c6a192a9aab44ff980fbfd7741/contracts/rubiconPools/BathToken.sol#L294
In the BathToken Contract, outstandingAmount  means the amount of tokens that have been taken out of a pool, but have not been repaid back to the pool yet. Additionally, outstandingAmount is NOT inclusive of any non-underlyingToken assets sitting on the Bath Tokens that have filled to here and are awaiting rebalancing to the underlyingToken by strategists per code comment.
Placing new trade orders and scrubbing existing trade orders are essential for maintaining an effective market, thus they are the key operation of a Strategist and these activities are expected to be performed frequently.
It was observed that the outstandingAmount of the pool was reduced prematurely when a trade order is scrubbed by strategist, thus making the outstandingAmount inaccurate.
The following are the output of test script which will be used to demonstrate the issue.
Assume that Alice deposited 50 WETH and 50 DAI, and Bob deposited 50 WETH and 50 DAI to their respective BathToken pools.
At this point, as shown below, for both BathWETH and BathDAI pool: underlyingBalance = 100, balanceOf = 100, Outstanding Amount = 0.
These values are correct as the users have deposited 100 and none of the funds have been utilised by the Strategist yet. No asset is escrowed in the orderbook yet. The orderbook is empty.
Note: underlyingBalance = outstandingAmount + balanceOf
The Strategist placed a market trade order, thus an ask and bid order was sent to the orderbook and 40 WETH and 40 DAI were escrowed in the orderbook.  Since 40 WETH and 40 DAI were utilised by the Strategist, the pools balanceOf dropped from 100 to 60, and the outstanding amount increased from 0 to 40. The underlyingBalance remains at 100.
Charles decided to buy the ask order (OrderID = 1) in the orderbook, thus he paid 800 DAI and received 40 WETH. 40 WETH escrowed in the orderbook was released/transfered to Charles and the 800 DAI that Charles paid was redirected to BathWETH pool waiting to be rebalanced by Strategist. The ask order was removed from the orderbook since it had been fulfilled.
At this point, the outstanding amount of BathWETH was 40 and this was correct since the BathWETH pool was still missing the 40 WETH that was taken out by the Strategist earlier, and had not been repaid back to the pool yet.
Noted that there were 800 DAI sitting on the BathWETH pool, but understood that the outstandingAmount is NOT inclusive of any non-underlyingToken assets sitting on the Bath Tokens that have filled to here and are awaiting rebalancing to the underlyingToken by strategists.
Strategist decided to scrub his trade order by calling BathPair.scrubStrategistTrade(), so any outstanding orders in the orderbook were cancelled. Therefore, the 40 DAI  escrowed in the orderbook was released back to the bathDAI pool. Thus, the bathDAI pool had a balanceOf = 100, and no outstanding amount since all the DAI had been repaid back.
However, scrubbing a trade order has a unintended effect on the BathWETH pool. All the outstanding amount in BathWETH pool were cleared (changed from 40 to 0) although no WETH was repaid back to the BathWETH pool. The outstanding amount was inaccurate and did not reflect the actual amount not repaid back to the pool yet.
Per the design, the only time that the WETH will be repaid back to BathWETH pool is when a strategist performs a rebalance to swap (either with other pool or external AMM) the 800 DAI (non-underlying asset) sitting on BathWETH pool.
The inaccurate outstanding amount of a pool casues unintended impact on a number of key functions (e.g. deposit, withdraw) of the protocol as they rely on the proper accounting of the underlyingBalance of a pool.
Note: underlyingBalance = outstandingAmount + balanceOf (if outstandingAmount is wrong, the underlyingBalance would be wrong too)
The following shows an example of the impact to Alice due to this issue. Alice who has earlier deposited 50 WETH to the BathWETH pool decided to withdraw all her investment.
Note: Alice holds around 50% of the shares in the bathWETH pool. Fee is charged during pool withdrawal.
Alice lost 20 WETH In the second scenario.
Similar problem also affect the deposit function since it relies on the proper accounting of the underlying balance or outstanding amount too. The amount of BathToken (e.g. BathWETH) that depositer received might be affected.
The following aims to explain why this issue occurred.
When BathPair.scrubStrategistTrade is called, it will in turn call BairPair.handleStratOrderAtID. If an order has been filled, it will call BathToken.removeFilledTradeAmount
https://github.com/code-423n4/2022-05-rubicon/blob/8c312a63a91193c6a192a9aab44ff980fbfd7741/contracts/rubiconPools/BathPair.sol#L213
BathToken.removeFilledTradeAmount will reduce the outstanding amount of the pool.
https://github.com/code-423n4/2022-05-rubicon/blob/8c312a63a91193c6a192a9aab44ff980fbfd7741/contracts/rubiconPools/BathToken.sol#L294
Following test script is used to generate the output shown in the walkthrough
https://gist.github.com/xiaoming9090/f26e0d3ea365edc89b257d6c0aa54ac9
Loss of fund for the users. Key protocol functions (e.g. deposit and withdraw) that rely on proper accounting of the underlying balance or outstanding amount do not function as expected.
Update the systems to ensure that the outstandingAmount of the pool is reduced only when the outstanding amount of tokens have been repaid back.
For instance, in the above example, the reduction should happen during rebalancing when the 800 DAI sitting on the bathWETH pool have been swapped for WETH, and the swapped WETH are replenished/repaid back to the pool. In this case, the outstandingAmount can be reduced accordingly.
In the BathPair.handleStratOrderAtID(), remove the BathToken.removeFilledTradeAmount and only call BathToken.removeFilledTradeAmount in the BathPair.rebalancePair if tokens have been repaid back.
bghughes (Rubicon) acknowledged and commented:
HickupHH3 (judge) decreased severity to Medium and commented:
