Skip to contentSkip to conversation

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

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

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 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 attemps to fix that.

Reported-and-tested-by [email protected]
Fixes ec2d0c04624b3c8a7eb1682e006717fa20cfbe24 ("posix-timers: Provide a mechanism to allocate a given timer ID")
---
 kernel/time/posix-timers.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
kernel/time/posix-timers.c +6 -2
index aa3120104a51..46ee10551c63 100644
@@ -483,11 +483,15 @@ static int do_timer_create(clockid_t which_clock, struct sigevent *event,
 
 	/* 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)))
+		if (copy_from_user(&req_id, created_timer_id, sizeof(req_id))) {
+			posixtimer_free_timer(new_timer);
 			return -EFAULT;
+		}
 		/* Valid IDs are 0..INT_MAX */
-		if ((unsigned int)req_id > INT_MAX)
+		if ((unsigned int)req_id > INT_MAX) {
+			posixtimer_free_timer(new_timer);
 			return -EINVAL;
+		}
 	}
 
 	/*
Signature · 1 line
2.43.0
#1 lore raw
Quoted 11 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 attemps to fix that.

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

Simply move parameters check _before_ new timer allocation please, this way you won't need new code at all :)

#2 lore raw
Quoted 8 lines · Cyrill Gorcunov
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 attemps to fix that.

Quoted 6 lines
Quoted 4 lines
Reported-and-tested-by [email protected]
Fixes ec2d0c04624b3c8a7eb1682e006717fa20cfbe24 ("posix-timers: Provide a mechanism to allocate a given timer ID")

Simply move parameters check _before_ new timer allocation please, this way you won't need new code at all :)

:)
v2 coming right up.