Skip to content
Smart contracts Audit finding Intermediate

Index values selected in `_woundRequestFulfilled()` are not uniformly distributed

The wound-selection loop in the smart contract increments the random word by one when it lands on an already-wounded agent, so the next index is usually the adjacent one. This makes agents next to a wounded agent more likely to be wounded, which unfairly clusters wounds on addresses that own sequential agents.

0xGoodess Follow with Tricko, cergyk and 1 other

Originally disclosed on Sherlock

Source: https://github.com/sherlock-audit/2023-10-looksrare-judging/issues/84

Found by

0xGoodess, Tricko, cergyk, detectiveking Index values selected in _woundRequestFulfilled() are not uniformly distributed. Indexes right next to wounded agents are more likely to be selected in the subsequent iterations, leading to bias in the distribution of wounded agents.

Vulnerability Detail

At the end of each round, the function _woundRequestFulfilled() is called, which uses uses the randomWord obtained from the VRF to select which agents should be marked as wounded. This selection process is carried out by performing a modulo operation on the randomWord with respect to the number of agents currently alive in the round, and then adding 1 to the result. The resulting value corresponds to the index of the agent to be designated as wounded, as illustrated in the code snippet section.

However, if the resulting index corresponds to an agent who is already wounded, the else branch is executed, where 1 is added to the randomWord for the next iteration of the loop. This is where the bias is introduced, because in the next iteration, the woundedAgentIndex will be the current woundedAgentIndex plus 1. As can be seen below:

$$ (A + 1) \bmod M $$

$$ ((A \bmod M) + (1 \bmod M)) \bmod M $$

As M > 1, we can simplify to

$$ ((A \bmod M) + 1) \bmod M $$

For $(A \bmod M) + 1$ less than $M$, we have

$$ (A + 1) \bmod M = (A \bmod M) + 1 $$

So with the exception of when randomWord overflows or (randomWord % currentRoundAgentsAlive + 1) >= currentRoundAgentsAlive, (randomWord + 1) % currentRoundAgentsAlive will be equal to (randomWord % currentRoundAgentsAlive) + 1.

Consequently, when the else branch is triggered, the next woundedAgentIndex will be ( previous woundedAgentIndex+ 1) from the last loop iteration (besides the two exceptions specified above). Therefore the agent at the next index will also be marked as wounded. As a result of this pattern, agents whose indexes are immediately next to an already wounded agent are more likely to be wounded than the remaining agents.

Consider the representative example below, albeit on a smaller scale (8 agents) to facilitate explanation. The initial state is represented in the table below:

Index 1 2 3 4 5 6 7 8
Agents Active Active Active Active Active Active Active Active
Probabilities 0.125 0.125 0.125 0.125 0.125 0.125 0.125 0.125

In the initial iteration of _woundRequestFulfilled(), assume that index 2 is selected. As expected from function logic, for the next iteration a new randomWord will be generated, resulting in a new index within the range of 1 to 8, all with equal probabilities. However now not all agents have an equal likelihood of being wounded. This disparity arises because both 3 and 2 (due to the else branch in the code above) will lead to the agent at index 3 being wounded.

Index 1 2 3 4 5 6 7 8
Agents Active Wounded Active Active Active Active Active Active
Probabilities 0.125 - 0.25 0.125 0.125 0.125 0.125 0.125

Now suppose that agents at index 2 and 3 are wounded. Following from the explanations above, index 4 has three times the chance of being wounded.

Index 1 2 3 4 5 6 7 8
Agents Active Wounded Wounded Active Active Active Active Active
Probabilities 0.125 - - 0.375 0.125 0.125 0.125 0.125

Impact

The distribution of wounded status among indexes is not uniform. Indexes subsequent to agents already wounded are more likely to be selected, introducing unfairness in the distribution of wounded agents.

This is particularly problematic because indexes that are close to each other are more likely to owned by the same address, due to the fact that when minting multiple agents, they are created in sequential indexes. Consequently, if an address has one of its agents marked as wounded, it becomes more probable that additional agents owned by the same address will also be marked as wounded. This creates an unfair situation to users.

Code Snippet

https://github.com/sherlock-audit/2023-10-looksrare/blob/main/contracts-infiltration/contracts/Infiltration.sol#L1404-L1468

Tool used

Manual Review

Recommendation

Consider eliminating the else and calling _nextRandomWord(randomWord) at the end of the loop iteration, so a new randomWord is generated each time. As shown in the diff below:

Diff
diff --git a/Infiltration.sol b/Infiltration.mod.sol
index 31af961..1c43c31 100644
--- a/Infiltration.sol
+++ b/Infiltration.mod.sol
@@ -1451,15 +1451,9 @@ contract Infiltration is
                     ++i;
                     currentRoundWoundedAgentIds[i] = uint16(woundedAgentId);
                 }
-
-                randomWord = _nextRandomWord(randomWord);
-            } else {
-                // If no agent is wounded using the current random word, increment by 1 and retry.
-                // If overflow, it will wrap around to 0.
-                unchecked {
-                    ++randomWord;
-                }
             }
+
+            randomWord = _nextRandomWord(randomWord);
         }

         currentRoundWoundedAgentIds[0] = uint16(woundedAgentsCount);

Discussion

0xhiroshi

https://github.com/LooksRare/contracts-infiltration/pull/152

SergeKireev

Fix LGTM

The original report as published on Sherlock, by 0xGoodess. Images and videos are our own copies; raw HTML is shown as text. View it on Sherlock.

Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.

How it worked

Root cause

Likely the retry path adds one to the random word instead of drawing a fresh random word, so the next index is almost always the previous index plus one.

Impact

Wounded status is not uniformly distributed, and because sequential indexes are often owned by the same address, one address can be unfairly hit with multiple wounds.

What the attacker needed

Agents minted in sequential indexes; randomness from a VRF

The fix

The retry branch should be removed and a new random word drawn at the end of every loop iteration; the text links a pull request and a reviewer says the fix looks good.

Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.

Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.

A smart contract picks which game characters get wounded using a random number. When the random pick lands on a character that is already wounded, the code adds one to the number and tries again, which usually selects the character sitting right next to it. That makes neighbouring characters much more likely to be picked, so the randomness is not fair.

What is Smart contracts?

Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.

More by 0xGoodess

View all

13 more disclosed reports, best first.

More from LooksRare YOLO

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Smart contract vulnerabilities

The lesson behind this report.

Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.