Skip to contentSkip to conversation

ARM: mm: mark section-aligned portion of rodata NX

PATCH Superseded Kees Cook 2 participants 5 messages Dec 7, 2015 Arm (32-bit)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Kees Cook

author
#0 lore raw

PATCH 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.

---
I am baffled why I can't put the ALIGN in the ".start = " initializer. GCC seems to think it's non-static, but only because of the "&" operator. Does anyone see a way to do this that doesn't require the runtime ALIGN?

---
 arch/arm/mm/init.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)
arch/arm/mm/init.c +6 -3
index 321d3683dc7c..b40a0536ef60 100644
@@ -579,6 +579,8 @@ struct section_perm {
 	pmdval_t clear;
 };
 
+#define RODATA_IDX	0
+
 static struct section_perm nx_perms[] = {
 	/* Make pages tables, etc before _stext RW (set NX). */
 	{
@@ -596,16 +598,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). */
-	{
+	[RODATA_IDX] = {
 		.name	= "rodata NX",
 		.start  = (unsigned long)__start_rodata,
 		.end    = (unsigned long)__init_begin,
 		.mask   = ~PMD_SECT_XN,
 		.prot   = PMD_SECT_XN,
 	},
-#endif
 };
 
 static struct section_perm ro_perms[] = {
@@ -709,6 +709,9 @@ int __fix_kernmem_perms(void *unused)
 
 void fix_kernmem_perms(void)
 {
+	/* rodata may not be section aligned, so cover as much as we can. */
+	nx_perms[RODATA_IDX].start = ALIGN(nx_perms[RODATA_IDX].start,
+					   SECTION_SIZE);
 	stop_machine(__fix_kernmem_perms, NULL, NULL);
 }
 
Signature · 6 lines
1.9.1


-- 
Kees Cook
Chrome OS & Brillo Security
#1 lore raw
Quoted 10 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.

---
I am baffled why I can't put the ALIGN in the ".start = " initializer. GCC seems to think it's non-static, but only because of the "&" operator. Does anyone see a way to do this that doesn't require the runtime ALIGN?

Yes, but you need to move the ALIGN() expression to the linker script as the value of a new symbol

Quoted 52 lines
---
 arch/arm/mm/init.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)
arch/arm/mm/init.c +6 -4
index 321d3683dc7c..b40a0536ef60 100644
@@ -579,6 +579,8 @@ struct section_perm {
        pmdval_t clear;
 };
 
+#define RODATA_IDX     0
+
 static struct section_perm nx_perms[] = {
        /* Make pages tables, etc before _stext RW (set NX). */
        {
@@ -596,16 +598,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). */
-       {
+       [RODATA_IDX] = {
                .name   = "rodata NX",
                .start  = (unsigned long)__start_rodata,
                .end    = (unsigned long)__init_begin,
                .mask   = ~PMD_SECT_XN,
                .prot   = PMD_SECT_XN,
        },
-#endif
 };
 
 static struct section_perm ro_perms[] = {
@@ -709,6 +709,9 @@ int __fix_kernmem_perms(void *unused)
 
 void fix_kernmem_perms(void)
 {
+       /* rodata may not be section aligned, so cover as much as we can. */
+       nx_perms[RODATA_IDX].start = ALIGN(nx_perms[RODATA_IDX].start,
+                                          SECTION_SIZE);
        stop_machine(__fix_kernmem_perms, NULL, NULL);
 }
 
--

1.9.1

--
Kees Cook
Chrome OS & Brillo Security

author
#2 lore raw
Quoted 6 lines · Ard Biesheuvel
Quoted 5 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.

Quoted 8 lines
Quoted 5 lines

---
I am baffled why I can't put the ALIGN in the ".start = " initializer. GCC seems to think it's non-static, but only because of the "&" operator. Does anyone see a way to do this that doesn't require the runtime ALIGN?

Yes, but you need to move the ALIGN() expression to the linker script as the value of a new symbol

I didn't see a way to do this without actually bumping the alignment (CONFIG_DEBUG_ALIGN_RODATA already does that, I want the other case). Do you have an example anywhere I could look at?

-Kees

Signature · 2 lines
Kees Cook
Chrome OS & Brillo Security
#3 lore raw
Quoted 14 lines · Kees Cook
Quoted 12 lines · Ard Biesheuvel
Quoted 10 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.

---
I am baffled why I can't put the ALIGN in the ".start = " initializer. GCC seems to think it's non-static, but only because of the "&" operator. Does anyone see a way to do this that doesn't require the runtime ALIGN?

Quoted 7 lines
Quoted 2 lines

Yes, but you need to move the ALIGN() expression to the linker script as the value of a new symbol

I didn't see a way to do this without actually bumping the alignment (CONFIG_DEBUG_ALIGN_RODATA already does that, I want the other case). Do you have an example anywhere I could look at?

Something like this (untested)

arch/arm/kernel/vmlinux.lds.S +2 -0
index 8b60fde5ce48..9fb98c5dd35b 100644
@@ -336,6 +336,8 @@ SECTIONS
  STABS_DEBUG
 }
 
+__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
arch/arm/mm/init.c +1 -3
index 8a63b4cdc0f2..ddacd3bd8f8b 100644
@@ -592,15 +592,13 @@ static struct section_perm nx_perms[] = {
  .mask = ~PMD_SECT_XN,
  .prot = PMD_SECT_XN,
  },
-#ifdef CONFIG_DEBUG_RODATA
  /* Make rodata NX (set RO in ro_perms below). */
  {
- .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
 };
 
 #ifdef CONFIG_DEBUG_RODATA
#4 lore raw
Quoted 49 lines · Ard Biesheuvel
Quoted 14 lines · Kees Cook
Quoted 12 lines · Ard Biesheuvel
Quoted 10 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.

---
I am baffled why I can't put the ALIGN in the ".start = " initializer. GCC seems to think it's non-static, but only because of the "&" operator. Does anyone see a way to do this that doesn't require the runtime ALIGN?

Quoted 7 lines
Quoted 2 lines

Yes, but you need to move the ALIGN() expression to the linker script as the value of a new symbol

I didn't see a way to do this without actually bumping the alignment (CONFIG_DEBUG_ALIGN_RODATA already does that, I want the other case). Do you have an example anywhere I could look at?

Something like this (untested)

arch/arm/kernel/vmlinux.lds.S +2 -0
index 8b60fde5ce48..9fb98c5dd35b 100644
@@ -336,6 +336,8 @@ SECTIONS
  STABS_DEBUG
 }
 
+__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
arch/arm/mm/init.c +0 -1
index 8a63b4cdc0f2..ddacd3bd8f8b 100644
@@ -592,15 +592,13 @@ static struct section_perm nx_perms[] = {
  .mask = ~PMD_SECT_XN,
  .prot = PMD_SECT_XN,
  },
-#ifdef CONFIG_DEBUG_RODATA
  /* Make rodata NX (set RO in ro_perms below). */
  {

arch/arm/mm/init.c static struct section_perm nx_perms[] = { · line 604

Quoted 10 lines
arch/arm/mm/init.c +1 -2
- .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
 };
 
 #ifdef CONFIG_DEBUG_RODATA

... and you also need a

extern char __start_rodata_section_aligned[];

somewhere;