Skip to content

Commit d4311ff

Browse files
Aaron Tomliningomolnar
authored andcommitted
init/main.c: Give init_task a canary
Tasks get their end of stack set to STACK_END_MAGIC with the aim to catch stack overruns. Currently this feature does not apply to init_task. This patch removes this restriction. Note that a similar patch was posted by Prarit Bhargava some time ago but was never merged: http://marc.info/?l=linux-kernel&m=127144305403241&w=2 Signed-off-by: Aaron Tomlin <atomlin@redhat.com> Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org> Acked-by: Oleg Nesterov <oleg@redhat.com> Acked-by: Michael Ellerman <mpe@ellerman.id.au> Cc: aneesh.kumar@linux.vnet.ibm.com Cc: dzickus@redhat.com Cc: bmr@redhat.com Cc: jcastillo@redhat.com Cc: jgh@redhat.com Cc: minchan@kernel.org Cc: tglx@linutronix.de Cc: hannes@cmpxchg.org Cc: Alex Thorlton <athorlton@sgi.com> Cc: Andrew Morton <akpm@linux-foundation.org> Cc: Benjamin Herrenschmidt <benh@kernel.crashing.org> Cc: Daeseok Youn <daeseok.youn@gmail.com> Cc: David Rientjes <rientjes@google.com> Cc: Fabian Frederick <fabf@skynet.be> Cc: Geert Uytterhoeven <geert@linux-m68k.org> Cc: Jiri Olsa <jolsa@redhat.com> Cc: Kees Cook <keescook@chromium.org> Cc: Kirill A. Shutemov <kirill.shutemov@linux.intel.com> Cc: Linus Torvalds <torvalds@linux-foundation.org> Cc: Masami Hiramatsu <masami.hiramatsu.pt@hitachi.com> Cc: Michael Opdenacker <michael.opdenacker@free-electrons.com> Cc: Paul Mackerras <paulus@samba.org> Cc: Prarit Bhargava <prarit@redhat.com> Cc: Rik van Riel <riel@redhat.com> Cc: Rusty Russell <rusty@rustcorp.com.au> Cc: Seiji Aguchi <seiji.aguchi@hds.com> Cc: Steven Rostedt <rostedt@goodmis.org> Cc: Vladimir Davydov <vdavydov@parallels.com> Cc: Yasuaki Ishimatsu <isimatu.yasuaki@jp.fujitsu.com> Cc: linuxppc-dev@lists.ozlabs.org Link: http://lkml.kernel.org/r/1410527779-8133-2-git-send-email-atomlin@redhat.com Signed-off-by: Ingo Molnar <mingo@kernel.org>
1 parent a15b12a commit d4311ff

6 files changed

Lines changed: 15 additions & 10 deletions

File tree

‎arch/powerpc/mm/fault.c‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
#include <linux/kprobes.h>
3131
#include <linux/kdebug.h>
3232
#include <linux/perf_event.h>
33-
#include <linux/magic.h>
3433
#include <linux/ratelimit.h>
3534
#include <linux/context_tracking.h>
3635

@@ -538,7 +537,7 @@ void bad_page_fault(struct pt_regs *regs, unsigned long address, int sig)
538537
regs->nip);
539538

540539
stackend = end_of_stack(current);
541-
if (current != &init_task && *stackend != STACK_END_MAGIC)
540+
if (*stackend != STACK_END_MAGIC)
542541
printk(KERN_ALERT "Thread overran stack, or stack corrupted\n");
543542

544543
die("Kernel access of bad area", regs, sig);

‎arch/x86/mm/fault.c‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
* Copyright (C) 2001, 2002 Andi Kleen, SuSE Labs.
44
* Copyright (C) 2008-2009, Red Hat Inc., Ingo Molnar
55
*/
6-
#include <linux/magic.h> /* STACK_END_MAGIC */
76
#include <linux/sched.h> /* test_thread_flag(), ... */
87
#include <linux/kdebug.h> /* oops_begin/end, ... */
98
#include <linux/module.h> /* search_exception_table */
@@ -710,7 +709,7 @@ no_context(struct pt_regs *regs, unsigned long error_code,
710709
show_fault_oops(regs, error_code, address);
711710

712711
stackend = end_of_stack(tsk);
713-
if (tsk != &init_task && *stackend != STACK_END_MAGIC)
712+
if (*stackend != STACK_END_MAGIC)
714713
printk(KERN_EMERG "Thread overran stack, or stack corrupted\n");
715714

716715
tsk->thread.cr2 = address;

‎include/linux/sched.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ struct sched_param {
5757
#include <linux/llist.h>
5858
#include <linux/uidgid.h>
5959
#include <linux/gfp.h>
60+
#include <linux/magic.h>
6061

6162
#include <asm/processor.h>
6263

@@ -2638,6 +2639,7 @@ static inline unsigned long stack_not_used(struct task_struct *p)
26382639
return (unsigned long)n - (unsigned long)end_of_stack(p);
26392640
}
26402641
#endif
2642+
extern void set_task_stack_end_magic(struct task_struct *tsk);
26412643

26422644
/* set thread flags in other task's structures
26432645
* - see asm/thread_info.h for TIF_xxxx flags available

‎init/main.c‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -508,6 +508,7 @@ asmlinkage __visible void __init start_kernel(void)
508508
* lockdep hash:
509509
*/
510510
lockdep_init();
511+
set_task_stack_end_magic(&init_task);
511512
smp_setup_processor_id();
512513
debug_objects_early_init();
513514

‎kernel/fork.c‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -294,11 +294,18 @@ int __weak arch_dup_task_struct(struct task_struct *dst,
294294
return 0;
295295
}
296296

297+
void set_task_stack_end_magic(struct task_struct *tsk)
298+
{
299+
unsigned long *stackend;
300+
301+
stackend = end_of_stack(tsk);
302+
*stackend = STACK_END_MAGIC; /* for overflow detection */
303+
}
304+
297305
static struct task_struct *dup_task_struct(struct task_struct *orig)
298306
{
299307
struct task_struct *tsk;
300308
struct thread_info *ti;
301-
unsigned long *stackend;
302309
int node = tsk_fork_get_node(orig);
303310
int err;
304311

@@ -328,8 +335,7 @@ static struct task_struct *dup_task_struct(struct task_struct *orig)
328335
setup_thread_stack(tsk, orig);
329336
clear_user_return_notifier(tsk);
330337
clear_tsk_need_resched(tsk);
331-
stackend = end_of_stack(tsk);
332-
*stackend = STACK_END_MAGIC; /* for overflow detection */
338+
set_task_stack_end_magic(tsk);
333339

334340
#ifdef CONFIG_CC_STACKPROTECTOR
335341
tsk->stack_canary = get_random_int();

‎kernel/trace/trace_stack.c‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
#include <linux/sysctl.h>
1414
#include <linux/init.h>
1515
#include <linux/fs.h>
16-
#include <linux/magic.h>
1716

1817
#include <asm/setup.h>
1918

@@ -171,8 +170,7 @@ check_stack(unsigned long ip, unsigned long *stack)
171170
i++;
172171
}
173172

174-
if ((current != &init_task &&
175-
*(end_of_stack(current)) != STACK_END_MAGIC)) {
173+
if (*end_of_stack(current) != STACK_END_MAGIC) {
176174
print_max_stack();
177175
BUG();
178176
}

0 commit comments

Comments
 (0)