Re: [patch V2 01/17] posix-timers: Initialise timer before adding it to the hash table

From: Frederic Weisbecker
Date: Fri Mar 07 2025 - 08:47:52 EST


Le Sun, Mar 02, 2025 at 08:36:44PM +0100, Thomas Gleixner a écrit :
> From: Eric Dumazet <edumazet@xxxxxxxxxx>
>
> A timer is only valid in the hashtable when both timer::it_signal and
> timer::it_id are set to their final values, but timers are added without
> those values being set.
>
> The timer ID is allocated when the timer is added to the hash in invalid
> state. The ID is taken from a monotonically increasing per process counter
> which wraps around after reaching INT_MAX. The hash insertion validates
> that there is no timer with the allocated ID in the hash table which
> belongs to the same process. That opens a mostly theoretical race condition:
>
> If other threads of the same process manage to create/delete timers in
> rapid succession before the newly created timer is fully initialized and
> wrap around to the timer ID which was handed out, then a duplicate timer ID
> will be inserted into the hash table.
>
> Prevent this by:
>
> 1) Setting timer::it_id before inserting the timer into the hashtable.
>
> 2) Storing the signal pointer in timer::it_signal with bit 0 set before
> inserting it into the hashtable.
>
> Bit 0 acts as a invalid bit, which means that the regular lookup for
> sys_timer_*() will fail the comparison with the signal pointer.
>
> But the lookup on insertion masks out bit 0 and can therefore detect a
> timer which is not yet valid, but allocated in the hash table. Bit 0
> in the pointer is cleared once the initialization of the timer
> completed.
>
> [ tglx: Fold ID and signal iniitializaion into one patch and massage change
> log and comments. ]
>
> Signed-off-by: Eric Dumazet <edumazet@xxxxxxxxxx>
> Signed-off-by: Thomas Gleixner <tglx@xxxxxxxxxxxxx>
> Link: https://lore.kernel.org/all/20250219125522.2535263-3-edumazet@xxxxxxxxxx

Reviewed-by: Frederic Weisbecker <frederic@xxxxxxxxxx>