ARM: mm: mark section-aligned portion of rodata NX
PATCH v2 1/1 ARM: mm: mark section-aligned portion of rodata NX
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
---
v2:
- static declaration, ard
--- arch/arm/kernel/vmlinux.lds.S | 9 +++++++-- arch/arm/mm/init.c | 7 ++++--- 2 files changed, 11 insertions(+), 5 deletions(-)
arch/arm/kernel/vmlinux.lds.S +7 -2
@@ -8,9 +8,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); \
@@ -337,6 +335,13 @@ SECTIONS } /* + * 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 * binutils is too old (for other reasons as well)
arch/arm/mm/init.c +4 -3
@@ -579,6 +579,9 @@ struct section_perm {pmdval_t clear; }; +/* First section-aligned location@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). */ {
@@ -596,16 +599,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[] = {
Signature · 6 lines
1.9.1 -- Kees Cook Chrome OS & Brillo Security
Quoted 77 lines · Kees Cook
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_RODATAis not setBefore:
---[ Kernel Mapping ]---
0x80000000-0x80100000 1M RW NX SHD 0x80100000-0x80a00000 9M ro x SHD 0x80a00000-0xa0000000 502M RW NX SHDAfter:
---[ 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 SHDSigned-off-by Kees Cook <[email protected]>---
v2:
- static declaration, ard--- arch/arm/kernel/vmlinux.lds.S | 9 +++++++-- arch/arm/mm/init.c | 7 ++++--- 2 files changed, 11 insertions(+), 5 deletions(-)arch/arm/kernel/vmlinux.lds.S +7
-2@@ -8,9 +8,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); \@@ -337,6 +335,13 @@ SECTIONS } /* + * 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 * binutils is too old (for other reasons as well)arch/arm/mm/init.c +3
-0@@ -579,6 +579,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). */ {
arch/arm/mm/init.c static struct section_perm nx_perms[] = { · line 606
Quoted 10 lines
arch/arm/mm/init.c +1
-2@@ -596,16 +599,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,
What happens if start > end ?
Quoted 14 lines
arch/arm/mm/init.c +0
-2.mask = ~PMD_SECT_XN, .prot = PMD_SECT_XN, }, -#endif }; static struct section_perm ro_perms[] = { --1.9.1
--
Kees Cook
Chrome OS & Brillo Security
Quoted 82 lines · Ard Biesheuvel
Quoted 81 lines · Kees Cook
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_RODATAis not setBefore:
---[ Kernel Mapping ]---
0x80000000-0x80100000 1M RW NX SHD 0x80100000-0x80a00000 9M ro x SHD 0x80a00000-0xa0000000 502M RW NX SHDAfter:
---[ 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 SHDSigned-off-by Kees Cook <[email protected]>---
v2:
- static declaration, ard--- arch/arm/kernel/vmlinux.lds.S | 9 +++++++-- arch/arm/mm/init.c | 7 ++++--- 2 files changed, 11 insertions(+), 5 deletions(-)arch/arm/kernel/vmlinux.lds.S +7
-2@@ -8,9 +8,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); \@@ -337,6 +335,13 @@ SECTIONS } /* + * 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 * binutils is too old (for other reasons as well)arch/arm/mm/init.c +3
-1@@ -579,6 +579,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). */ {@@ -596,16 +599,14 @@ static struct section_perm nx_perms[] = { .mask = ~PMD_SECT_XN, .prot = PMD_SECT_XN, }, -#ifdef CONFIG_DEBUG_ALIGN_RODATA
Quoted 8 lines
arch/arm/mm/init.c static struct section_perm nx_perms[] = { · line 606
Quoted 6 lines
arch/arm/mm/init.c +1
-1/* 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,What happens if start > end ?
It isn't possible, since rodata will be after text and before init_begin, so it must either less than or equal to init_begin. The equal case is quite possible, and that's fine, since both cases are (correctly) silently ignored by set_section_perms:
for (addr = perms[i].start; addr < perms[i].end; addr
+= SECTION_SIZE)
section_update(....
-Kees
Quoted 15 lines
Quoted 15 lines
arch/arm/mm/init.c +0
-2.mask = ~PMD_SECT_XN, .prot = PMD_SECT_XN, }, -#endif }; static struct section_perm ro_perms[] = { --1.9.1
--
Kees Cook
Chrome OS & Brillo Security
Signature · 2 lines
Kees Cook Chrome OS & Brillo Security
Quoted 98 lines · Kees Cook
Quoted 82 lines · Ard Biesheuvel
Quoted 81 lines · Kees Cook
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_RODATAis not setBefore:
---[ Kernel Mapping ]---
0x80000000-0x80100000 1M RW NX SHD 0x80100000-0x80a00000 9M ro x SHD 0x80a00000-0xa0000000 502M RW NX SHDAfter:
---[ 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 SHDSigned-off-by Kees Cook <[email protected]>---
v2:
- static declaration, ard--- arch/arm/kernel/vmlinux.lds.S | 9 +++++++-- arch/arm/mm/init.c | 7 ++++--- 2 files changed, 11 insertions(+), 5 deletions(-)arch/arm/kernel/vmlinux.lds.S +7
-2@@ -8,9 +8,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); \@@ -337,6 +335,13 @@ SECTIONS } /* + * 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 * binutils is too old (for other reasons as well)arch/arm/mm/init.c +3
-1@@ -579,6 +579,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). */ {@@ -596,16 +599,14 @@ static struct section_perm nx_perms[] = { .mask = ~PMD_SECT_XN, .prot = PMD_SECT_XN, }, -#ifdef CONFIG_DEBUG_ALIGN_RODATA Quoted 8 lines
arch/arm/mm/init.c static struct section_perm nx_perms[] = { · line 606
Quoted 6 lines
arch/arm/mm/init.c +1
-1/* 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,What happens if start > end ?
It isn't possible, since rodata will be after text and before
init_begin, so it must either less than or equal toinit_begin. The equal case is quite possible, and that's fine, since both cases are (correctly) silently ignored byset_section_perms:
Quoted 5 lines
for (addr = perms[i].start; addr < perms[i].end; addr
+=SECTION_SIZE)section_update(....-Kees
Hi Ard,
Is this v2 ok? I'd like to have your Ack before sending it to the patch tracker.
Thanks!
-Kees
Quoted 22 lines
Quoted 8 lines
Quoted 8 lines
arch/arm/mm/init.c +0
-1.mask = ~PMD_SECT_XN, .prot = PMD_SECT_XN, }, -#endif };Quoted 8 lines
Quoted 8 lines
arch/arm/mm/init.c +0
-1static struct section_perm ro_perms[] = { --1.9.1
--
Kees Cook
Chrome OS & Brillo Security--
Kees Cook
Chrome OS & Brillo Security
Signature · 2 lines
Kees Cook Chrome OS & Brillo Security
Ard Biesheuvel
Reviewed-byQuoted 103 lines · Kees Cook
Quoted 102 lines · Kees Cook
Quoted 82 lines · Ard Biesheuvel
Quoted 81 lines · Kees Cook
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_RODATAis not setBefore:
---[ Kernel Mapping ]---
0x80000000-0x80100000 1M RW NX SHD 0x80100000-0x80a00000 9M ro x SHD 0x80a00000-0xa0000000 502M RW NX SHDAfter:
---[ 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 SHDSigned-off-by Kees Cook <[email protected]>---
v2:
- static declaration, ard--- arch/arm/kernel/vmlinux.lds.S | 9 +++++++-- arch/arm/mm/init.c | 7 ++++--- 2 files changed, 11 insertions(+), 5 deletions(-)arch/arm/kernel/vmlinux.lds.S +7
-2@@ -8,9 +8,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); \@@ -337,6 +335,13 @@ SECTIONS } /* + * 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 * binutils is too old (for other reasons as well)arch/arm/mm/init.c +3
-1@@ -579,6 +579,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). */ {@@ -596,16 +599,14 @@ static struct section_perm nx_perms[] = { .mask = ~PMD_SECT_XN, .prot = PMD_SECT_XN, }, -#ifdef CONFIG_DEBUG_ALIGN_RODATA Quoted 8 lines
arch/arm/mm/init.c static struct section_perm nx_perms[] = { · line 606
Quoted 6 lines
arch/arm/mm/init.c +1
-1/* 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,What happens if start > end ?
It isn't possible, since rodata will be after text and before
init_begin, so it must either less than or equal toinit_begin. The equal case is quite possible, and that's fine, since both cases are (correctly) silently ignored byset_section_perms:for (addr = perms[i].start; addr < perms[i].end; addr
+=SECTION_SIZE)section_update(....
Quoted 6 lines
Quoted 1 line
-Kees
Hi Ard,
Is this v2 ok? I'd like to have your Ack before sending it to the patch tracker.
Looks fine to me
Thanks,
Ard.
Quoted 10 lines · Kees Cook
Quoted 9 lines · Kees Cook
Quoted 7 lines · Ard Biesheuvel
Quoted 6 lines · Kees Cook
/*
+ * WithoutCONFIG_DEBUG_ALIGN_RODATA,__start_rodata_section_alignedwill
+ * 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);
...
I'm afraid this causes build errors on two configurations:
./arch/arm/kernel/vmlinux.lds:701: undefined symbol `SECTION_SHIFT' referenced in expression
which I think is down to the new __start_rodata_section_aligned line.
Olof's builder errored out with this for both allnoconfig and lpc18xx_defconfig builds. Both probably due to CONFIG_MMU being unset, and hence no definition for SECTION_SHIFT.
I think you need to make this conditional on CONFIG_MMU.
Signature · 3 lines
RMK's Patch system: http://www.arm.linux.org.uk/developer/patches/ FTTC broadband for 0.8mile line: [email protected] down 400kbps up according to speedtest.net.
Quoted 17 lines · On Thursday 11 February 2016 18:00:32 Russell King - ARM Linux wrote:
Quoted 10 lines · Kees Cook
Quoted 9 lines · Kees Cook
Quoted 7 lines · Ard Biesheuvel
Quoted 6 lines · Kees Cook
/*
+ * WithoutCONFIG_DEBUG_ALIGN_RODATA,__start_rodata_section_alignedwill
+ * 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);...
I'm afraid this causes build errors on two configurations:
./arch/arm/kernel/vmlinux.lds:701: undefined symbol `SECTION_SHIFT' referenced in expression
Quoted 7 lines
which I think is down to the new
__start_rodata_section_alignedline.Olof's builder errored out with this for both allnoconfig and
lpc18xx_defconfigbuilds. Both probably due toCONFIG_MMUbeing unset, and hence no definition forSECTION_SHIFT.I think you need to make this conditional on
CONFIG_MMU.
I've hit another problem with this patch, when using XIP_KERNEL, I now see
arch/arm/mm/built-in.o:(.data+0x4bc): undefined reference to `__start_rodata_section_aligned'
in linux-next, apparently because of the combination with "ARM: 8513/1: xip: Move XIP linking to a separate file".
Arnd
Quoted 24 lines · Arnd Bergmann
Quoted 23 lines · On Thursday 11 February 2016 18:00:32 Russell King - ARM Linux wrote:
Quoted 10 lines · Kees Cook
Quoted 9 lines · Kees Cook
Quoted 7 lines · Ard Biesheuvel
Quoted 6 lines · Kees Cook
/*
+ * WithoutCONFIG_DEBUG_ALIGN_RODATA,__start_rodata_section_alignedwill
+ * 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);...
I'm afraid this causes build errors on two configurations:
./arch/arm/kernel/vmlinux.lds:701: undefined symbol `SECTION_SHIFT' referenced in expressionwhich I think is down to the new
__start_rodata_section_alignedline.Olof's builder errored out with this for both allnoconfig and
lpc18xx_defconfigbuilds. Both probably due toCONFIG_MMUbeing unset, and hence no definition forSECTION_SHIFT.
Quoted 8 lines
Quoted 1 line
I think you need to make this conditional on
CONFIG_MMU.I've hit another problem with this patch, when using
XIP_KERNEL, I now seearch/arm/mm/built-in.o:(.data+0x4bc): undefined reference to `__start_rodata_section_aligned'
in linux-next, apparently because of the combination with "ARM: 8513/1: xip: Move XIP linking to a separate file".
Is this due to the copy not taking into account the recent changes to vmlinux.lds.S?
"git show 538bf4694898b19e" with copy-rename detection shows:
copy from arch/arm/kernel/vmlinux.lds.S
copy to arch/arm/kernel/vmlinux-xip.lds.S
index a6e395c53a48e045..6f59ef290289e078 100644
--- a/arch/arm/kernel/vmlinux.lds.S
+++ b/arch/arm/kernel/vmlinux-xip.lds.S
[...]
@@ -117,7 +114,7 @@ SECTIONS
ARM_CPU_KEEP(PROC_INFO)
}-#ifdef CONFIG_DEBUG_ALIGN_RODATA
+#ifdef CONFIG_DEBUG_RODATA
. = ALIGN(1<<SECTION_SHIFT);
#endif
RO_DATA(PAGE_SIZE)
Gr{oetje,eeting}s,
Geert
--
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]
In personal conversations with technical people, I call myself a hacker. But when I'm talking to journalists I just say "programmer" or something like that.
-- Linus Torvalds