ARM: 8502/1: mm: mark section-aligned portion of rodata NX
authorKees Cook <keescook@chromium.org>
Tue, 26 Jan 2016 00:20:21 +0000 (01:20 +0100)
committerRussell King <rmk+kernel@arm.linux.org.uk>
Thu, 11 Feb 2016 15:44:10 +0000 (15:44 +0000)
When rodata is large enough that it crosses a section boundary after the
kernel text, mark the rest NX. This is as close to full NX of rodata as
we can get without splitting page tables or doing section alignment via
CONFIG_DEBUG_ALIGN_RODATA.

When the config is:

 CONFIG_DEBUG_RODATA=y
 # CONFIG_DEBUG_ALIGN_RODATA is not set

Before:

---[ Kernel Mapping ]---
0x80000000-0x80100000           1M     RW NX SHD
0x80100000-0x80a00000           9M     ro x  SHD
0x80a00000-0xa0000000         502M     RW NX SHD

After:

---[ Kernel Mapping ]---
0x80000000-0x80100000           1M     RW NX SHD
0x80100000-0x80700000           6M     ro x  SHD
0x80700000-0x80a00000           3M     ro NX SHD
0x80a00000-0xa0000000         502M     RW NX SHD

Signed-off-by: Kees Cook <keescook@chromium.org>
Reviewed-by: Ard Biesheuvel <ard.biesheuvel@linaro.org>
Signed-off-by: Russell King <rmk+kernel@arm.linux.org.uk>
arch/arm/kernel/vmlinux.lds.S
arch/arm/mm/init.c

index e087a2ed112bb8e272443f08262d29ae9f8ca889..fdca231e150a463bb299a6a7296d7bd6442494b3 100644 (file)
@@ -12,9 +12,7 @@
 #include <asm/thread_info.h>
 #include <asm/memory.h>
 #include <asm/page.h>
-#ifdef CONFIG_DEBUG_RODATA
 #include <asm/pgtable.h>
-#endif
 
 #define PROC_INFO                                                      \
        . = ALIGN(4);                                                   \
@@ -319,6 +317,13 @@ SECTIONS
        STABS_DEBUG
 }
 
+/*
+ * Without CONFIG_DEBUG_ALIGN_RODATA, __start_rodata_section_aligned will
+ * be the first section-aligned location after __start_rodata. Otherwise,
+ * it will be equal to __start_rodata.
+ */
+__start_rodata_section_aligned = ALIGN(__start_rodata, 1 << SECTION_SHIFT);
+
 /*
  * These must never be empty
  * If you have to comment these two assert statements out, your
index 53f42508025bf63495ed9ded82925f79d8925029..370581aeb87104273df695409af37876956c639f 100644 (file)
@@ -582,6 +582,9 @@ struct section_perm {
        pmdval_t clear;
 };
 
+/* First section-aligned location at or after __start_rodata. */
+extern char __start_rodata_section_aligned[];
+
 static struct section_perm nx_perms[] = {
        /* Make pages tables, etc before _stext RW (set NX). */
        {
@@ -599,16 +602,14 @@ static struct section_perm nx_perms[] = {
                .mask   = ~PMD_SECT_XN,
                .prot   = PMD_SECT_XN,
        },
-#ifdef CONFIG_DEBUG_ALIGN_RODATA
        /* Make rodata NX (set RO in ro_perms below). */
        {
                .name   = "rodata NX",
-               .start  = (unsigned long)__start_rodata,
+               .start  = (unsigned long)__start_rodata_section_aligned,
                .end    = (unsigned long)__init_begin,
                .mask   = ~PMD_SECT_XN,
                .prot   = PMD_SECT_XN,
        },
-#endif
 };
 
 static struct section_perm ro_perms[] = {