Skip to content

Commit 405c075

Browse files
amlutoIngo Molnar
authored andcommitted
fork: Add task stack refcounting sanity check and prevent premature task stack freeing
If something goes wrong with task stack refcounting and a stack refcount hits zero too early, warn and leak it rather than potentially freeing it early (and silently). Signed-off-by: Andy Lutomirski <luto@kernel.org> Cc: Borislav Petkov <bp@alien8.de> Cc: Brian Gerst <brgerst@gmail.com> Cc: Denys Vlasenko <dvlasenk@redhat.com> Cc: H. Peter Anvin <hpa@zytor.com> Cc: Josh Poimboeuf <jpoimboe@redhat.com> Cc: Linus Torvalds <torvalds@linux-foundation.org> Cc: Oleg Nesterov <oleg@redhat.com> Cc: Peter Zijlstra <peterz@infradead.org> Cc: Thomas Gleixner <tglx@linutronix.de> Link: http://lkml.kernel.org/r/f29119c783a9680a4b4656e751b6123917ace94b.1477926663.git.luto@kernel.org Signed-off-by: Ingo Molnar <mingo@kernel.org>
1 parent 0c183d9 commit 405c075

File tree

1 file changed

+4
-0
lines changed

1 file changed

+4
-0
lines changed

kernel/fork.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,9 @@ static void account_kernel_stack(struct task_struct *tsk, int account)
315315

316316
static void release_task_stack(struct task_struct *tsk)
317317
{
318+
if (WARN_ON(tsk->state != TASK_DEAD))
319+
return; /* Better to leak the stack than to free prematurely */
320+
318321
account_kernel_stack(tsk, -1);
319322
arch_release_thread_stack(tsk->stack);
320323
free_thread_stack(tsk);
@@ -1862,6 +1865,7 @@ static __latent_entropy struct task_struct *copy_process(
18621865
atomic_dec(&p->cred->user->processes);
18631866
exit_creds(p);
18641867
bad_fork_free:
1868+
p->state = TASK_DEAD;
18651869
put_task_stack(p);
18661870
free_task(p);
18671871
fork_out:

0 commit comments

Comments
 (0)