Submitted by bytes032, also found by 0xPkhatri, Udsen, thekmj, anodaram and volodya.
https://github.com/code-423n4/2023-04-rubicon/blob/511636d889742296a54392875a35e4c0c4727bb7/contracts/utilities/poolsUtility/Position.sol#L526-L542 https://github.com/code-423n4/2023-04-rubicon/blob/511636d889742296a54392875a35e4c0c4727bb7/contracts/utilities/poolsUtility/Position.sol#L158-L159
The infinite loop vulnerability could cause the openPosition functionality to become unresponsive or fail, negatively affecting the performance and usability of the whole system.
The _borrowLimit function is an internal view function used by openPosition  that calculates the maximum number of borrowing loops allowed for a given asset and leverage, taking into account the collateral factor and any previously supplied collateral. It returns the calculated borrowing limit and the amount required to borrow in the last loop to avoid exceeding the desired amount.
The function works as follows:
The calculations for the desiredAmount and the _loopBorrowed are performed by using the wmul function.
wmul is designed to multiply two fixed-point numbers (with 18 decimals precision) and return the result as another fixed-point number. Heres a breakdown of how the function works:
Now that we know how most of the calculation works lets assume we call _borrowLimit with the variables used in this test suite:
Additionally, the collateral factor stays unchanged:
Calculating the desired amount, we get that its 58.3e18, which is the result of wmul(25e18, 23.32e18).
Now, lets examine what happens within the while loop:
The important thing to note here is that on the first iteration _loopBorrowed is the result of wmul(_assetAmount, _collateralFactor, but going forward it is a result of itself, e.g. wmul(_loopBorrowed, _collateralFactor).
So, it takes 3 iterations to reach the required result. However, the vulnerability here though is hiding in plain sight.
Given that _loopBorrowed is dividing from itself, what would happen if it goes to 1, or even 0, but the desired amount is big enough?
But how could that happen? What if the collateralFactor is 0.5e18 instead of 0.7e18?
Lets do the exercise again with a reduced collateral factor:
Now that _loopBorrowed < 0 I think its pretty clear where this is going. It will take approximately 60 more iterations until wmul(_loopBorrowed, 0.5e18) returns 1.
At this point _assetAmount will be around 50000000000000000020, which means it will take the 8299999999999999980 more iterations to get to the required desired amount.
You can verify these results by running the yarn hardhat test ./test/leverage-wrapper.ts --grep "should open long position" command from the existing test suite, which will produce the following output:

I have added some console logs for you if you want a visual representation:
In conclusion, when the collateral factor is lower (and the leverage high), the _borrowLimit function becomes highly inefficient and requires a large number of iterations to calculate the desired amount.
This is just one nuance of this vulnerability and it could be triggered in various ways, but I hope this single explanation is good enough to convince you that it should be addressed.
Thats probably one of my hardest mitigations steps to write, so Ill brain-dump almost everything Ive researched and that comes to my mind.
Limit the number of iterations:
Introduce a maximum number of iterations that the while loop can run, preventing the function from running indefinitely.
This approach helps prevent the infinite loop issue, but it means your system wont be able to work with specific collateral factors.
Instead, the maximum number of iterations can be chosen based on expected usage patterns and system requirements.
Restrict the minimum _loopBorrowed value:
Define a minimum acceptable value for _loopBorrowed and halt the loop if the value goes below this threshold.
This approach can help prevent the _loopBorrowed value from becoming too small and causing an infinite loop. However, it has the same drawbacks as the previous solution.
Use a more sophisticated algorithm:
Instead of a while loop, consider using a more sophisticated algorithm to calculate the borrowing limit and last borrow amount.
It might be possible to derive a closed-form solution that directly calculates the borrowing limit without needing a loop.
However, this approach requires a deep understanding of the mathematical relationship between the input parameters and the desired borrowing limit.
For instance, if the borrowing limit calculation follows a simple linear relationship, the closed-form solution could be derived as follows:
To apply the closed-form solution to the _borrowLimit function, we would need to adjust the smart contract to directly compute the required number of iterations n and the final borrowed amount.
Instead, we can use the closed-form solution formula to calculate n and then determine the final borrowed amount using the formula for the sum of a geometric series.
For example, consider the scenario where the initial asset amount is A0 = 10,000 tokens, the desired asset amount is Ad = 15,000 tokens (10,000 tokens + 5,000 tokens), and the collateral factor is c = 0.5. 
Using the closed-form solution formula: n = log_c((1 - ((Ad * (1 - c)) / A0)) - 1).
Substituting the values: n = log_0.5((1 - ((15,000 * (1 - 0.5)) / 10,000)) - 1).
After calculating, we get: n  1.58.
Since n should be an integer (as it represents the number of iterations), we round up to the nearest whole number: n = 2.
We can now calculate the final borrowed amount using the formula for the sum of a geometric series: Ad = A0 + A0 * c + A0 * c^2 + ... + A0 * c^n.
Substituting the values: 15,000 = 10,000 + 10,000 * 0.5 + 10,000 * 0.5^2.
Calculating the geometric series: 15,000 = 10,000 + 5,000 + 2,500.
Thus, the final borrowed amount is: 5,000 = 5,000 + 2,500.
Now that we know how it could  work, we would need to identify the geometric series that represents the problem and use the closed-form formula for a geometric series to calculate the borrow limit.
If the collateral factor is not equal to 1, we would need to calculate the number of terms using the closed-form formula for a geometric series and the last borrow amount using the geometric series formula.
The pseudocode for the updated _borrowLimit function using the closed-form solution looks like this:
Please note that the provided solution is incomplete and requires additional adjustments.
daoio (Rubicon) acknowledged and commented:
