Submitted by unforgiven, also found by unforgiven, unforgiven, ustas, and hihen
https://github.com/reserve-protocol/protocol/blob/df7ecadc2bae74244ace5e8b39e94bc992903158/contracts/p1/RToken.sol#L439-L514
https://github.com/reserve-protocol/protocol/blob/df7ecadc2bae74244ace5e8b39e94bc992903158/contracts/p1/BackingManager.sol#L105-L150
Function redeem() redeems RToken for basket collateral and it updated basketsNeeded and transfers users basket ERC20 from BackingManager to user address. it loops through tokens and transfer them to caller and if one of tokens were ERC777 or any other 3rd party protocol token with hook, attacker can perform reentrancy attack during token transfers. Attacker can cause multiple impacts by choosing the reentrancy function:
This is redeem() code:
As you can see code calculates withdrawal amount of each basket erc20 tokens by calling basketHandler.quote() and then bounds each withdrawal by the prorata share of token balance, in case protocol is under-collateralized. and then code updates basketsNeeded and in the end transfers the tokens.  if one of those tokens were ERC777 then that token would call receiver hook function in token transfer. there may be other 3rd party protocol tokens that calls registered hook functions during the token transfer. as reserve protocol is permission less and tries to work with all tokens so the external call in the token transfer can call hook functions. attacker can use this hook and perform reentrancy attack.
This is fullyCollateralized() code in BasketHandler:
As you can see it calculates baskets that can be held by backingManager tokens balance and needed baskets by RToken contract and by comparing them determines that if RToken is fully collateralized or not. If RToken is fully collateralized then BackingManager.manageTokens() would call handoutExcessAssets() and would distribute extra funds between RToken holders and RSR stakers.
The root cause of the issue is that during tokens transfers in redeem() not all the basket tokens balance of the BackingManager updates once and if one has hook function which calls attacker contract then attacker can use this updated token balance of the contract and perform his reentrancy attack. attacker can call different functions for reentrancy. these are two scenarios:
Scenario #1: attacker call redeem() again and bypass prorata share bound check when protocol is under-collaterialized:
Scenario #2: attacker can call BackingManager.manageTokens() for reentrancy call:
VIM
Prevent reading reentrancy attack by central reentrancy guard or by one main proxy interface contract that has reentrancy guard.
Or create contract state (similar to basket nonce) which changes after each interaction and check for contracts states change during the call. (start and end of the call)
0xean (judge) commented:
tmattimore (Reserve) confirmed and commented:
0xean (judge) decreased severity to Medium and commented:
tbrent (Reserve) commented:
