public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH v2] posix-timers: Fix potential memory leak in do_timer_create()
@ 2025-11-14 12:27 Eslam Khafagy
  2025-11-14 13:52 ` Frederic Weisbecker
  2025-11-14 16:02 ` [tip: timers/urgent] posix-timers: Plug " tip-bot2 for Eslam Khafagy
  0 siblings, 2 replies; 3+ messages in thread
From: Eslam Khafagy @ 2025-11-14 12:27 UTC (permalink / raw)
  To: anna-maria, frederic, tglx, gorcunov
  Cc: eslam.medhat1993, syzkaller-bugs, linux-kernel,
	syzbot+9c47ad18f978d4394986

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: syzbot+9c47ad18f978d4394986@syzkaller.appspotmail.com
Closes: https://lore.kernel.org/all/69155df4.a70a0220.3124cb.0017.GAE@google.com/T/
Fixes: ec2d0c04624b3 ("posix-timers: Provide a mechanism to allocate a given timer ID")
Suggested-by: Cyrill Gorcunov <gorcunov@gmail.com>
Signed-off-by: Eslam Khafagy <eslam.medhat1993@gmail.com>
---
v2:
* Move parameters check before new timer allocation, no need for new
  code.
v1: https://lore.kernel.org/all/20251114050621.875131-1-eslam.medhat1993@gmail.com/T/
---
 kernel/time/posix-timers.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)

diff --git a/kernel/time/posix-timers.c b/kernel/time/posix-timers.c
index aa3120104a51..56e17b625c72 100644
--- a/kernel/time/posix-timers.c
+++ b/kernel/time/posix-timers.c
@@ -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.
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH v2] posix-timers: Fix potential memory leak in do_timer_create()
  2025-11-14 12:27 [PATCH v2] posix-timers: Fix potential memory leak in do_timer_create() Eslam Khafagy
@ 2025-11-14 13:52 ` Frederic Weisbecker
  2025-11-14 16:02 ` [tip: timers/urgent] posix-timers: Plug " tip-bot2 for Eslam Khafagy
  1 sibling, 0 replies; 3+ messages in thread
From: Frederic Weisbecker @ 2025-11-14 13:52 UTC (permalink / raw)
  To: Eslam Khafagy
  Cc: anna-maria, tglx, gorcunov, syzkaller-bugs, linux-kernel,
	syzbot+9c47ad18f978d4394986

Le Fri, Nov 14, 2025 at 02:27:39PM +0200, Eslam Khafagy a écrit :
> 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: syzbot+9c47ad18f978d4394986@syzkaller.appspotmail.com
> Closes: https://lore.kernel.org/all/69155df4.a70a0220.3124cb.0017.GAE@google.com/T/
> Fixes: ec2d0c04624b3 ("posix-timers: Provide a mechanism to allocate a given timer ID")
> Suggested-by: Cyrill Gorcunov <gorcunov@gmail.com>
> Signed-off-by: Eslam Khafagy <eslam.medhat1993@gmail.com>

Reviewed-by: Frederic Weisbecker <frederic@kernel.org>

-- 
Frederic Weisbecker
SUSE Labs

^ permalink raw reply	[flat|nested] 3+ messages in thread

* [tip: timers/urgent] posix-timers: Plug potential memory leak in do_timer_create()
  2025-11-14 12:27 [PATCH v2] posix-timers: Fix potential memory leak in do_timer_create() Eslam Khafagy
  2025-11-14 13:52 ` Frederic Weisbecker
@ 2025-11-14 16:02 ` tip-bot2 for Eslam Khafagy
  1 sibling, 0 replies; 3+ messages in thread
From: tip-bot2 for Eslam Khafagy @ 2025-11-14 16:02 UTC (permalink / raw)
  To: linux-tip-commits
  Cc: syzbot+9c47ad18f978d4394986, Cyrill Gorcunov, Eslam Khafagy,
	Thomas Gleixner, Frederic Weisbecker, x86, linux-kernel

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 <eslam.medhat1993@gmail.com>
AuthorDate:    Fri, 14 Nov 2025 14:27:39 +02:00
Committer:     Thomas Gleixner <tglx@linutronix.de>
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 ]

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/
---
 kernel/time/posix-timers.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)

diff --git a/kernel/time/posix-timers.c b/kernel/time/posix-timers.c
index aa31201..56e17b6 100644
--- a/kernel/time/posix-timers.c
+++ b/kernel/time/posix-timers.c
@@ -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.

^ permalink raw reply related	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2025-11-14 16:02 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-11-14 12:27 [PATCH v2] posix-timers: Fix potential memory leak in do_timer_create() Eslam Khafagy
2025-11-14 13:52 ` Frederic Weisbecker
2025-11-14 16:02 ` [tip: timers/urgent] posix-timers: Plug " tip-bot2 for Eslam Khafagy

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox