Skip to contentSkip to conversation

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

PATCH Applied Eslam Khafagy 3 participants 3 messages Nov 14, 2025 Posix clocks and timers

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 posix-timers: Fix potential memory leak in do_timer_create()

potential memory leak may happen if user space pointer created_timer_id is invallid. or the value it points to is invalid. the call will prematurely return.
However it doesn't free the memory it allocates with alloc_posix_timer(). This patch attempts to fix that by moving parameter check before alloc_posix_timer().

Reported-by [email protected]
Fixes ec2d0c04624b3 ("posix-timers: Provide a mechanism to allocate a given timer ID")

---
v2:
* Move parameters check before new timer allocation, no need for new code.
v1: https://lore.kernel.org/all/[email protected]/T/

---
 kernel/time/posix-timers.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)
kernel/time/posix-timers.c +6 -6
index aa3120104a51..56e17b625c72 100644
@@ -475,12 +475,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)))
@@ -490,6 +484,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.
Signature · 1 line
2.43.0
maintainer Reviewed-by on v2 1/1
#1 lore raw
Quoted 7 lines · Eslam Khafagy

potential memory leak may happen if user space pointer created_timer_id is invallid. or the value it points to is invalid. the call will prematurely return.
However it doesn't free the memory it allocates with alloc_posix_timer(). This patch attempts to fix that by moving parameter check before alloc_posix_timer().

Quoted 5 lines
Reported-by [email protected]
Fixes ec2d0c04624b3 ("posix-timers: Provide a mechanism to allocate a given timer ID")
Signature · 2 lines
Frederic Weisbecker
SUSE Labs
merge bot Applied
#2 lore raw

[tip: timers/urgent] posix-timers: Plug potential memory leak in do_timer_create()

In reply to Eslam Khafagy
The following commit has been merged into the timers/urgent branch of tip:

The following commit has been merged into the timers/urgent branch of tip:

Commit-ID:     e0fd4d42e27f761e9cc82801b3f183e658dc749d
Gitweb:        https://git.kernel.org/tip/e0fd4d42e27f761e9cc82801b3f183e658dc749d
Author:        Eslam Khafagy <[email protected]>
AuthorDate:    Fri, 14 Nov 2025 14:27:39 +02:00
Committer:     Thomas Gleixner <[email protected]>
CommitterDate: Fri, 14 Nov 2025 16:58:31 +01:00

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 ]

---
 kernel/time/posix-timers.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)
kernel/time/posix-timers.c +6 -6
index aa31201..56e17b6 100644
@@ -475,12 +475,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)))
@@ -490,6 +484,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.