]> git.ipfire.org Git - thirdparty/kernel/stable.git/commitdiff
posix-timers: Plug potential memory leak in do_timer_create()
authorEslam Khafagy <eslam.medhat1993@gmail.com>
Fri, 14 Nov 2025 12:27:39 +0000 (14:27 +0200)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Mon, 24 Nov 2025 09:37:36 +0000 (10:37 +0100)
[ Upstream commit e0fd4d42e27f761e9cc82801b3f183e658dc749d ]

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/
Signed-off-by: Sasha Levin <sashal@kernel.org>
kernel/time/posix-timers.c

index 8b582174b1f9498a548fc1c214185577f994a647..42db8396f1999a20c0e6d301f3fae3375653e9c2 100644 (file)
@@ -476,12 +476,6 @@ static int do_timer_create(clockid_t which_clock, struct sigevent *event,
        if (!kc->timer_create)
                return -EOPNOTSUPP;
 
-       new_timer = alloc_posix_timer();
-       if (unlikely(!new_timer))
-               return -EAGAIN;
-
-       spin_lock_init(&new_timer->it_lock);
-
        /* Special case for CRIU to restore timers with a given timer ID. */
        if (unlikely(current->signal->timer_create_restore_ids)) {
                if (copy_from_user(&req_id, created_timer_id, sizeof(req_id)))
@@ -491,6 +485,12 @@ static int do_timer_create(clockid_t which_clock, struct sigevent *event,
                        return -EINVAL;
        }
 
+       new_timer = alloc_posix_timer();
+       if (unlikely(!new_timer))
+               return -EAGAIN;
+
+       spin_lock_init(&new_timer->it_lock);
+
        /*
         * Add the timer to the hash table. The timer is not yet valid
         * after insertion, but has a unique ID allocated.