Submitted by kfx, also found by bin2chen, minhquanym, and TrungOre
Lets assume two yangs in the system, yang A and yang B, and two users:
If the user U1 can force redistribution of their position, then they can steal from the shrine due to a bug in the code. The function redistribute_helper loops through all yangs in order, including those not in the trove #1. Since trove_yang_amt.is_zero() returns true for yang A, the updated_trove_yang_balances array is updated early and then continue statement is executed.
However, since the new_yang_totals array is not updated in the iteration of the loop, some values of updated_trove_yang_balances end up never being used.
Lets assume 100% redistribution. After the all loop is fully executed, the two arrays contain:
updated_trove_yang_balances = [(A, 0), (B, 0)];
new_yang_totals = [(B, 1000)];
The final loop of the function is executed just once. Its first and only iteration writes the new total B value. However, it does not update the amount of B in the trove #1, since (B, 0) is the second element of the first array.
The final state is that trove #1 still has 1000 units of B, but no more debt. The user U1 can now withdraw all 1000 units from the trove #1.
This bug violates the shrine invariant The total amount of a yang is equal to the sum of all troves deposits of that yang (this includes any exceptionally redistributed yangs and their accompanying errors) and the initial amount seeded at the time of add_yang.
Output:
Expected output:
One question is: how difficult is it to force the redistribution? It looks like its a realistic option in some cases. For instance, the attacker could first drain the stability pool (absorber) by forcing absorbtions until it is empty. A liquidation can be forced by borrowing max amount and then waiting for ltv > threshold to happen due to small price fluctuations, potentially even manipulating the price slightly (as only a small change is required to cross the threshold). For a collateral asset with assets threshold > ABSORPTION_THRESHOLD its not required that the troves penalty == max_possible_penalty.
Do not update the array updated_trove_yang_balances before the continue statement.
Loop
tserg (Opus) confirmed and commented via duplicate issue #199:
0xsomeone (judge) commented:
