init/main.c: Give init_task a canary
authorAaron Tomlin <atomlin@redhat.com>
Fri, 12 Sep 2014 13:16:17 +0000 (14:16 +0100)
committerIngo Molnar <mingo@kernel.org>
Fri, 19 Sep 2014 10:35:22 +0000 (12:35 +0200)
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>
arch/powerpc/mm/fault.c
arch/x86/mm/fault.c
include/linux/sched.h
init/main.c
kernel/fork.c
kernel/trace/trace_stack.c

index 51ab9e7e6c391b9497a08730a7b5e4c625c96304..35d0760c3fa44f46db3e25dc6efb454af0d7bf24 100644 (file)
@@ -30,7 +30,6 @@
 #include <linux/kprobes.h>
 #include <linux/kdebug.h>
 #include <linux/perf_event.h>
-#include <linux/magic.h>
 #include <linux/ratelimit.h>
 #include <linux/context_tracking.h>
 
@@ -538,7 +537,7 @@ void bad_page_fault(struct pt_regs *regs, unsigned long address, int sig)
                regs->nip);
 
        stackend = end_of_stack(current);
-       if (current != &init_task && *stackend != STACK_END_MAGIC)
+       if (*stackend != STACK_END_MAGIC)
                printk(KERN_ALERT "Thread overran stack, or stack corrupted\n");
 
        die("Kernel access of bad area", regs, sig);
index a241946815131904498ae6ecd95d87c4e28bf2f9..bc23a7043c6599e9dcb209e65fe209fe46fc2f29 100644 (file)
@@ -3,7 +3,6 @@
  *  Copyright (C) 2001, 2002 Andi Kleen, SuSE Labs.
  *  Copyright (C) 2008-2009, Red Hat Inc., Ingo Molnar
  */
-#include <linux/magic.h>               /* STACK_END_MAGIC              */
 #include <linux/sched.h>               /* test_thread_flag(), ...      */
 #include <linux/kdebug.h>              /* oops_begin/end, ...          */
 #include <linux/module.h>              /* search_exception_table       */
@@ -710,7 +709,7 @@ no_context(struct pt_regs *regs, unsigned long error_code,
        show_fault_oops(regs, error_code, address);
 
        stackend = end_of_stack(tsk);
-       if (tsk != &init_task && *stackend != STACK_END_MAGIC)
+       if (*stackend != STACK_END_MAGIC)
                printk(KERN_EMERG "Thread overran stack, or stack corrupted\n");
 
        tsk->thread.cr2         = address;
index 82ff3d6efb1925b01ebf6dbab6bfe8c492399633..118dca7d5a28b9d2d25f589ef79cb4d962550a57 100644 (file)
@@ -57,6 +57,7 @@ struct sched_param {
 #include <linux/llist.h>
 #include <linux/uidgid.h>
 #include <linux/gfp.h>
+#include <linux/magic.h>
 
 #include <asm/processor.h>
 
@@ -2638,6 +2639,7 @@ static inline unsigned long stack_not_used(struct task_struct *p)
        return (unsigned long)n - (unsigned long)end_of_stack(p);
 }
 #endif
+extern void set_task_stack_end_magic(struct task_struct *tsk);
 
 /* set thread flags in other task's structures
  * - see asm/thread_info.h for TIF_xxxx flags available
index bb1aed928f21391b63493112c6bb1a1eae39077f..5fc3fc7bd4754122b8d51a788bcfe8eb63b00373 100644 (file)
@@ -508,6 +508,7 @@ asmlinkage __visible void __init start_kernel(void)
         * lockdep hash:
         */
        lockdep_init();
+       set_task_stack_end_magic(&init_task);
        smp_setup_processor_id();
        debug_objects_early_init();
 
index 9387ae8ab048114fda9fb505dbbf97fea1312ed9..ad64248c4b18fe2ddf61921f9d585fad4561e96c 100644 (file)
@@ -294,11 +294,18 @@ int __weak arch_dup_task_struct(struct task_struct *dst,
        return 0;
 }
 
+void set_task_stack_end_magic(struct task_struct *tsk)
+{
+       unsigned long *stackend;
+
+       stackend = end_of_stack(tsk);
+       *stackend = STACK_END_MAGIC;    /* for overflow detection */
+}
+
 static struct task_struct *dup_task_struct(struct task_struct *orig)
 {
        struct task_struct *tsk;
        struct thread_info *ti;
-       unsigned long *stackend;
        int node = tsk_fork_get_node(orig);
        int err;
 
@@ -328,8 +335,7 @@ static struct task_struct *dup_task_struct(struct task_struct *orig)
        setup_thread_stack(tsk, orig);
        clear_user_return_notifier(tsk);
        clear_tsk_need_resched(tsk);
-       stackend = end_of_stack(tsk);
-       *stackend = STACK_END_MAGIC;    /* for overflow detection */
+       set_task_stack_end_magic(tsk);
 
 #ifdef CONFIG_CC_STACKPROTECTOR
        tsk->stack_canary = get_random_int();
index 8a4e5cb66a4c4c1b9726f942e3e54b8e52a45ff5..1636e41828c241ad35f89b8d4629bb7a9272aea0 100644 (file)
@@ -13,7 +13,6 @@
 #include <linux/sysctl.h>
 #include <linux/init.h>
 #include <linux/fs.h>
-#include <linux/magic.h>
 
 #include <asm/setup.h>
 
@@ -171,8 +170,7 @@ check_stack(unsigned long ip, unsigned long *stack)
                        i++;
        }
 
-       if ((current != &init_task &&
-               *(end_of_stack(current)) != STACK_END_MAGIC)) {
+       if (*end_of_stack(current) != STACK_END_MAGIC) {
                print_max_stack();
                BUG();
        }