Skip to content

Commit 399f8dd

Browse files
committed
signal: Prevent sigqueue caching after task got released
syzbot reported a memory leak related to sigqueue caching. The assumption that a task cannot cache a sigqueue after the signal handler has been dropped and exit_task_sigqueue_cache() has been invoked turns out to be wrong. Such a task can still invoke release_task(other_task), which cleans up the signals of 'other_task' and ends up in sigqueue_cache_or_free(), which in turn will cache the signal because task->sigqueue_cache is NULL. That's obviously bogus because nothing will free the cached signal of that task anymore, so the cached item is leaked. This happens when e.g. the last non-leader thread exits and reaps the zombie leader. Prevent this by setting tsk::sigqueue_cache to an error pointer value in exit_task_sigqueue_cache() which forces any subsequent invocation of sigqueue_cache_or_free() from that task to hand the sigqueue back to the kmemcache. Add comments to all relevant places. Fixes: 4bad58e ("signal: Allow tasks to cache one sigqueue struct") Reported-by: [email protected] Signed-off-by: Thomas Gleixner <[email protected]> Reviewed-by: Oleg Nesterov <[email protected]> Acked-by: Christian Brauner <[email protected]> Link: https://lore.kernel.org/r/[email protected]
1 parent 13311e7 commit 399f8dd

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

kernel/signal.c

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -435,6 +435,12 @@ __sigqueue_alloc(int sig, struct task_struct *t, gfp_t gfp_flags,
435435
* Preallocation does not hold sighand::siglock so it can't
436436
* use the cache. The lockless caching requires that only
437437
* one consumer and only one producer run at a time.
438+
*
439+
* For the regular allocation case it is sufficient to
440+
* check @q for NULL because this code can only be called
441+
* if the target task @t has not been reaped yet; which
442+
* means this code can never observe the error pointer which is
443+
* written to @t->sigqueue_cache in exit_task_sigqueue_cache().
438444
*/
439445
q = READ_ONCE(t->sigqueue_cache);
440446
if (!q || sigqueue_flags)
@@ -463,13 +469,18 @@ void exit_task_sigqueue_cache(struct task_struct *tsk)
463469
struct sigqueue *q = tsk->sigqueue_cache;
464470

465471
if (q) {
466-
tsk->sigqueue_cache = NULL;
467472
/*
468473
* Hand it back to the cache as the task might
469474
* be self reaping which would leak the object.
470475
*/
471476
kmem_cache_free(sigqueue_cachep, q);
472477
}
478+
479+
/*
480+
* Set an error pointer to ensure that @tsk will not cache a
481+
* sigqueue when it is reaping it's child tasks
482+
*/
483+
tsk->sigqueue_cache = ERR_PTR(-1);
473484
}
474485

475486
static void sigqueue_cache_or_free(struct sigqueue *q)
@@ -481,6 +492,10 @@ static void sigqueue_cache_or_free(struct sigqueue *q)
481492
* is intentional when run without holding current->sighand->siglock,
482493
* which is fine as current obviously cannot run __sigqueue_free()
483494
* concurrently.
495+
*
496+
* The NULL check is safe even if current has been reaped already,
497+
* in which case exit_task_sigqueue_cache() wrote an error pointer
498+
* into current->sigqueue_cache.
484499
*/
485500
if (!READ_ONCE(current->sigqueue_cache))
486501
WRITE_ONCE(current->sigqueue_cache, q);

0 commit comments

Comments
 (0)