The [forwardRevenue function](https://github.com/code-423n4/2024-07-reserve/blob/3f133997e186465f4904553b0f8e86ecb7bbacbf/contracts/p1/BackingManager.sol#L179-L266) in the BackingManager contract is responsible for distributing surplus assets to the rsrTrader and rTokenTrader. The function calculates the number of tokens to distribute per share using integer division, which can result in rounding down and potentially skipping the distribution of small surpluses. This issue arises because the calculation
tokensPerShare = delta / (totals.rTokenTotal + totals.rsrTotal) uses integer division, which truncates any remainder. If the surplus (delta) is smaller than the sum of totals.rTokenTotal and totals.rsrTotal, tokensPerShare will be 0, and the function will skip the distribution for that token.
Detailed Description
The forwardRevenue function is designed to forward surplus assets to the rsrTrader and rTokenTrader. The function first calculates the number of tokens to distribute per share using the following code:
This calculation uses integer division, which truncates any remainder. If the surplus (delta) is smaller than the sum of totals.rTokenTotal and totals.rsrTotal, tokensPerShare will be 0, and the function will skip the distribution for that token. This can lead to an accumulation of undistributed assets in the BackingManager, which is not the intended behavior.
For example, consider the following scenarios:
Surplus of 100 tokens:
delta = 100
totals.rTokenTotal = 60
totals.rsrTotal = 40
tokensPerShare = 100 / (60 + 40) = 100 / 100 = 1
Distribution:
rsrTrader receives: 1 * 40 = 40 tokens
rTokenTrader receives: 1 * 60 = 60 tokens
Total distributed: 100 tokens
Surplus of 99 tokens:
delta = 99
totals.rTokenTotal = 60
totals.rsrTotal = 40
tokensPerShare = 99 / (60 + 40) = 99 / 100 = 0
Since tokensPerShare is 0, the function will continue to the next token without distributing anything.
Total distributed: 0 tokens
Impact
The integer division rounding issue in the forwardRevenue function can lead to a significant loss of tokens, especially when dealing with small amounts of surplus assets or large total shares. This can result in an accumulation of undistributed assets in the BackingManager, leading to a loss of value for token holders and stakers. Over time, this can lead to a significant accumulation of undistributed assets, which is not the intended behavior and can affect the fairness and efficiency of the distribution mechanism.
Proof of Concept
User has a surplus of 99 tokens.
totals.rTokenTotal is 60 and totals.rsrTotal is 40.
The function calculates tokensPerShare = 99 / (60 + 40) = 99 / 100 = 0.
Since tokensPerShare is 0, the function skips the distribution for that token.
The 99 tokens remain undistributed, leading to a loss of value.
Tools Used
Manual review
Recommended Mitigation Steps
To address this issue, the calculation should use a more precise approach. The following code provides a fix by calculating the exact share for each trader and ensuring that all surplus tokens are distributed proportionally:
This approach ensures that all surplus tokens are distributed proportionally, even when dealing with small amounts. It eliminates the potential for large amounts of value to be left undistributed due to rounding issues.
Lines of code
https://github.com/code-423n4/2024-07-reserve/blob/3f133997e186465f4904553b0f8e86ecb7bbacbf/contracts/p1/BackingManager.sol#L179-L266
Vulnerability details
Description
The
[forwardRevenue
function](https://github.com/code-423n4/2024-07-reserve/blob/3f133997e186465f4904553b0f8e86ecb7bbacbf/contracts/p1/BackingManager.sol#L179-L266) in theBackingManager
contract is responsible for distributing surplus assets to thersrTrader
andrTokenTrader
. The function calculates the number of tokens to distribute per share using integer division, which can result in rounding down and potentially skipping the distribution of small surpluses. This issue arises because the calculationtokensPerShare = delta / (totals.rTokenTotal + totals.rsrTotal)
uses integer division, which truncates any remainder. If the surplus (delta
) is smaller than the sum oftotals.rTokenTotal
andtotals.rsrTotal
,tokensPerShare
will be0
, and the function will skip the distribution for that token.Detailed Description
The
forwardRevenue
function is designed to forward surplus assets to thersrTrader
andrTokenTrader
. The function first calculates the number of tokens to distribute per share using the following code:This calculation uses integer division, which truncates any remainder. If the surplus (
delta
) is smaller than the sum oftotals.rTokenTotal
andtotals.rsrTotal
,tokensPerShare
will be0
, and the function will skip the distribution for that token. This can lead to an accumulation of undistributed assets in theBackingManager
, which is not the intended behavior.For example, consider the following scenarios:
Surplus of 100 tokens:
delta = 100
totals.rTokenTotal = 60
totals.rsrTotal = 40
tokensPerShare = 100 / (60 + 40) = 100 / 100 = 1
rsrTrader
receives:1 * 40 = 40
tokensrTokenTrader
receives:1 * 60 = 60
tokens100
tokensSurplus of 99 tokens:
delta = 99
totals.rTokenTotal = 60
totals.rsrTotal = 40
tokensPerShare = 99 / (60 + 40) = 99 / 100 = 0
tokensPerShare
is0
, the function will continue to the next token without distributing anything.0
tokensImpact
The integer division rounding issue in the
forwardRevenue
function can lead to a significant loss of tokens, especially when dealing with small amounts of surplus assets or large total shares. This can result in an accumulation of undistributed assets in theBackingManager
, leading to a loss of value for token holders and stakers. Over time, this can lead to a significant accumulation of undistributed assets, which is not the intended behavior and can affect the fairness and efficiency of the distribution mechanism.Proof of Concept
totals.rTokenTotal
is 60 andtotals.rsrTotal
is 40.tokensPerShare = 99 / (60 + 40) = 99 / 100 = 0
.tokensPerShare
is0
, the function skips the distribution for that token.Tools Used
Manual review
Recommended Mitigation Steps
To address this issue, the calculation should use a more precise approach. The following code provides a fix by calculating the exact share for each trader and ensuring that all surplus tokens are distributed proportionally:
This approach ensures that all surplus tokens are distributed proportionally, even when dealing with small amounts. It eliminates the potential for large amounts of value to be left undistributed due to rounding issues.
Assessed type
Other