posix-timers: Plug potential memory leak in do_timer_create()

When posix timer creation is set to allocate a given timer ID and the
access to the user space value faults, the function terminates without
freeing the already allocated posix timer structure.

Move the allocation after the user space access to cure that.

[ tglx: Massaged change log ]

Fixes: ec2d0c04624b3 ("posix-timers: Provide a mechanism to allocate a given timer ID")
Reported-by: syzbot+9c47ad18f978d4394986@syzkaller.appspotmail.com
Suggested-by: Cyrill Gorcunov <gorcunov@gmail.com>
Signed-off-by: Eslam Khafagy <eslam.medhat1993@gmail.com>
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
Reviewed-by: Frederic Weisbecker <frederic@kernel.org>
Link: https://patch.msgid.link/20251114122739.994326-1-eslam.medhat1993@gmail.com
Closes: https://lore.kernel.org/all/69155df4.a70a0220.3124cb.0017.GAE@google.com/T/

authored by Eslam Khafagy and committed by Thomas Gleixner e0fd4d42 e9a6fb0b

+6 -6
+6 -6
kernel/time/posix-timers.c
··· 475 475 if (!kc->timer_create) 476 476 return -EOPNOTSUPP; 477 477 478 - new_timer = alloc_posix_timer(); 479 - if (unlikely(!new_timer)) 480 - return -EAGAIN; 481 - 482 - spin_lock_init(&new_timer->it_lock); 483 - 484 478 /* Special case for CRIU to restore timers with a given timer ID. */ 485 479 if (unlikely(current->signal->timer_create_restore_ids)) { 486 480 if (copy_from_user(&req_id, created_timer_id, sizeof(req_id))) ··· 483 489 if ((unsigned int)req_id > INT_MAX) 484 490 return -EINVAL; 485 491 } 492 + 493 + new_timer = alloc_posix_timer(); 494 + if (unlikely(!new_timer)) 495 + return -EAGAIN; 496 + 497 + spin_lock_init(&new_timer->it_lock); 486 498 487 499 /* 488 500 * Add the timer to the hash table. The timer is not yet valid