Issue Bug Closed

#107337Session Garbage collection probability is calculated wrongly (off by one and non-uniform) and inefficient

S P 08/27/25, 09:35 updated 12/02/25, 13:58

Description

(relevant for all verson since “forever”, selected 13 because it is the current LTS, but also present in main branch)

https://github.com/TYPO3/typo3/blob/main/typo3/sysext/core/Classes/Session/UserSessionManager.php#L284

if (random_int(0, mt_getrandmax()) % 100 <= $garbageCollectionProbability)

Modulo 100 results in a maximum of 99, so comparing to less or equal will make a garbageCollectionProbability of 99 succeed always. Which is not how probabilities work (only 100 should be always ).

Also, why such complicated way of getting a number between 0 and 99 in the first place? Why not just random_int(0, 99) (without modulo)? The method random_int is guaranteed to be based on a uniform distribution. What the current implementation does is actually “deforming” away from the uniform distribution, because due to the modulo against a probably non-multiple maximum of the divisor, some numbers are a tiny bit more likely than others (namely all the remainders modulo 100 above the last multiple of 100 of mt_getrandmax() which are statistically more likely to appear because of this)

I suggest simplifying this to

if (random_int(0, 99) < $garbageCollectionProbability)

which would behave like it seemed to be intended originally and by the naming of the variables.

And since this is not cryptographically relevant, maybe even consider using just rand() instead of random_int(), because it is way faster:

if (rand(0, 99) < $garbageCollectionProbability)

Reviews 2

History 917

  1. Garvin Hicking commented and changed category status New Needs Feedback <span data-processor="relativetime" data-value="2025-08-27T10:34:11+00:00" > 08/27/25, 10:34 </span>

    That’s not correct. Modulo 100 works with any number to report the rest of the division operation. So the max number is INT_MAX here.l, not 99.

    The probability is approximately 2% (2 out of 100 possible values: 0, 1, 2, 3, …, 99).
    More precisely:

    Value 0 appears 21,474,837 times
    Value 1 appears 21,474,837 times
    Total occurrences: 43,949,674
    Total possible outcomes: 2,147,483,648

    Exact probability: 43,949,674 / 2,147,483,648 ≈ 2.0465%

    Due to the <= operation its not 1%.

    Having said that: I do agree that we could simplify the operation. Not sure about the cryptographic relevance for security, I would say we do need this to prevent session fixation attacks.

  2. Markus Klein commented <span data-processor="relativetime" data-value="2025-08-27T14:08:02+00:00" > 08/27/25, 14:08 </span>

    I disagree with both of you ;-)

    `$garbageCollectionProbability` has a default value of 1.

    The modulo operation yields a number between 0 and 99.

    So GC is triggered only if that operation results in the values [0,1].
    That means 2 out of 100 possible outcomes.

    This will probably not happen 99% of the time.

    —-

    Assuming by name, the `$garbageCollectionProbability` should have a valid value range of [0,100], which is nowhere specified.
    The comparison should of course be adjusted to this fact, so that a value of 100 really means always and 0 means never.

  3. S P commented <span data-processor="relativetime" data-value="2025-08-27T14:27:24+00:00" > 08/27/25, 14:27 </span>

    Yes, for the default case of $garbageCollectionProbability = 1 the current probability is 2% (which is already strange, but can be tolerated probably). But If you call this method with argument 99 (which you can) then the probability is already 100% (which is not just unexpected but plain wrong, it should be 99 – what if you rely on the missing 1%?). So the current behaviour is off by one.

    To the modulo statistics: mt_getrandmax() always yields 2147483647. It is 47 more than a multiple of 100. Picking any number uniformely out of 2147483600 (00 at the end) and then doing “mod 100” yields 0-99 uniformley. But when adding a slight amount of 0-47 (but not 48-99) additionally to the possible input values for the mod operation, makes the probably shift a little bit to these lower numbers in in terms of statistical measures. Of course this is statistically insignificant here (no cryptography involved), just wanted to point out that this whole calculation is over-complicated just to get a non-security-relevant number between 0 and 99.

  4. Markus Klein commented and changed status Needs Feedback Accepted Complexity medium <span data-processor="relativetime" data-value="2025-08-27T14:44:29+00:00" > 08/27/25, 14:44 </span>

    @stefanp Wanna push a patch?

  5. Gerrit Code Review pushed patch set 2 for main to change 90495 status Accepted Under Review <span data-processor="relativetime" data-value="2025-08-27T20:42:53+00:00" > 08/27/25, 20:42 </span>

  6. Anonymous commented and changed status Under Review Resolved done 0 % 100 % <span data-processor="relativetime" data-value="2025-10-27T16:25:10+00:00" > 10/27/25, 16:25 </span>

    Applied in changeset commit:6501c64b4b356b2ecb81dbe78accb9e8563d7b0e.