Skip to contentSkip to conversation

x86, cpa: fix kernel text RO checks in static_protection()

PATCH In review Suresh Siddha 2 participants 10 messages Oct 29, 2009 – Nov 2, 2009

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/3 x86, cpa: fix kernel text RO checks in static_protection()

Steven Rostedt reported that we are unconditionally making the kernel text mapping as read-only. i.e., if someone does cpa() to the kernel text area for setting/clearing any page table attribute, we unconditionally clear the read-write attribute for the kernel text mapping that is set at compile time.

We should delay (to forbid the write attribute) and enforce only after the kernel has mapped the text as read-only.

---
 arch/x86/include/asm/cacheflush.h |    1 +
 arch/x86/mm/init_32.c             |    2 +-
 arch/x86/mm/init_64.c             |    2 +-
 arch/x86/mm/pageattr.c            |   10 ++++++----
 4 files changed, 9 insertions(+), 6 deletions(-)
arch/x86/include/asm/cacheflush.h +1 -0
Index: tip/arch/x86/include/asm/cacheflush.h
===================================================================
@@ -176,6 +176,7 @@ void clflush_cache_range(void *addr, uns
 #ifdef CONFIG_DEBUG_RODATA
 void mark_rodata_ro(void);
 extern const int rodata_test_data;
+extern int kernel_set_to_readonly;
 void set_kernel_text_rw(void);
 void set_kernel_text_ro(void);
 #else
arch/x86/mm/init_32.c +1 -1
Index: tip/arch/x86/mm/init_32.c
===================================================================
@@ -997,7 +997,7 @@ static noinline int do_test_wp_bit(void)
 const int rodata_test_data = 0xC3;
 EXPORT_SYMBOL_GPL(rodata_test_data);
 
-static int kernel_set_to_readonly;
+int kernel_set_to_readonly;
 
 void set_kernel_text_rw(void)
 {
arch/x86/mm/init_64.c +1 -1
Index: tip/arch/x86/mm/init_64.c
===================================================================
@@ -695,7 +695,7 @@ void __init mem_init(void)
 const int rodata_test_data = 0xC3;
 EXPORT_SYMBOL_GPL(rodata_test_data);
 
-static int kernel_set_to_readonly;
+int kernel_set_to_readonly;
 
 void set_kernel_text_rw(void)
 {
arch/x86/mm/pageattr.c +6 -4
Index: tip/arch/x86/mm/pageattr.c
===================================================================
@@ -282,14 +282,16 @@ static inline pgprot_t static_protection
 #if defined(CONFIG_X86_64) && defined(CONFIG_DEBUG_RODATA) && \
 	!defined(CONFIG_DYNAMIC_FTRACE)
 	/*
-	 * Kernel text mappings for the large page aligned .rodata section
-	 * will be read-only. For the kernel identity mappings covering
-	 * the holes caused by this alignment can be anything.
+	 * Once the kernel maps the text as RO (kernel_set_to_readonly is set),
+	 * kernel text mappings for the large page aligned text, rodata sections
+	 * will be always read-only. For the kernel identity mappings covering
+	 * the holes caused by this alignment can be anything that user asks.
 	 *
 	 * This will preserve the large page mappings for kernel text/data
 	 * at no extra cost.
 	 */
-	if (within(address, (unsigned long)_text,
+	if (kernel_set_to_readonly &&
+	    within(address, (unsigned long)_text,
 		   (unsigned long)__end_rodata_hpage_align))
 		pgprot_val(forbidden) |= _PAGE_RW;
 #endif
#1 lore raw

PATCH 2/3 x86_64, ftrace: make ftrace use kernel identity mapping to modify code

On x86_64, kernel text mappings are mapped read-only with CONFIG_DEBUG_RODATA. So use the kernel identity mapping instead of the kernel text mapping to modify the kernel text.

---
 arch/x86/kernel/ftrace.c |   17 +++++++++++++++++
 arch/x86/mm/pageattr.c   |    3 +--
 2 files changed, 18 insertions(+), 2 deletions(-)
arch/x86/kernel/ftrace.c +17 -0
Index: tip/arch/x86/kernel/ftrace.c
===================================================================
@@ -189,9 +189,26 @@ static void wait_for_nmi(void)
 	nmi_wait_count++;
 }
 
+static inline int
+within(unsigned long addr, unsigned long start, unsigned long end)
+{
+	return addr >= start && addr < end;
+}
+
 static int
 do_ftrace_mod_code(unsigned long ip, void *new_code)
 {
+	/*
+	 * On x86_64, kernel text mappings are mapped read-only with
+	 * CONFIG_DEBUG_RODATA. So we use the kernel identity mapping instead
+	 * of the kernel text mapping to modify the kernel text.
+	 *
+	 * For 32bit kernels, these mappings are same and we can use
+	 * kernel identity mapping to modify code.
+	 */
+	if (within(ip, (unsigned long)_text, (unsigned long)_etext))
+		ip = (unsigned long)__va(__pa(ip));
+
 	mod_code_ip = (void *)ip;
 	mod_code_newcode = new_code;
 
arch/x86/mm/pageattr.c +1 -2
Index: tip/arch/x86/mm/pageattr.c
===================================================================
@@ -279,8 +279,7 @@ static inline pgprot_t static_protection
 		   __pa((unsigned long)__end_rodata) >> PAGE_SHIFT))
 		pgprot_val(forbidden) |= _PAGE_RW;
 
-#if defined(CONFIG_X86_64) && defined(CONFIG_DEBUG_RODATA) && \
-	!defined(CONFIG_DYNAMIC_FTRACE)
+#if defined(CONFIG_X86_64) && defined(CONFIG_DEBUG_RODATA)
 	/*
 	 * Once the kernel maps the text as RO (kernel_set_to_readonly is set),
 	 * kernel text mappings for the large page aligned text, rodata sections
#2 lore raw

[tip:x86/mm] x86_64, ftrace: Make ftrace use kernel identity mapping to modify code

Commit-ID:  55ca3cc1746335bb6ef1d3894ddb6d0c729b3518
Gitweb:     http://git.kernel.org/tip/55ca3cc1746335bb6ef1d3894ddb6d0c729b3518
Author:     Suresh Siddha <[email protected]>
AuthorDate: Wed, 28 Oct 2009 18:46:57 -0800
Committer:  Ingo Molnar <[email protected]>
CommitDate: Mon, 2 Nov 2009 17:16:36 +0100

x86_64, ftrace: Make ftrace use kernel identity mapping to modify code

On x86_64, kernel text mappings are mapped read-only with CONFIG_DEBUG_RODATA. So use the kernel identity mapping instead of the kernel text mapping to modify the kernel text.

LKML-Reference: <[email protected]>

---
 arch/x86/kernel/ftrace.c |   17 +++++++++++++++++
 arch/x86/mm/pageattr.c   |    3 +--
 2 files changed, 18 insertions(+), 2 deletions(-)
arch/x86/kernel/ftrace.c +17 -0
index 9dbb527..944e982 100644
@@ -187,9 +187,26 @@ static void wait_for_nmi(void)
 	nmi_wait_count++;
 }
 
+static inline int
+within(unsigned long addr, unsigned long start, unsigned long end)
+{
+	return addr >= start && addr < end;
+}
+
 static int
 do_ftrace_mod_code(unsigned long ip, void *new_code)
 {
+	/*
+	 * On x86_64, kernel text mappings are mapped read-only with
+	 * CONFIG_DEBUG_RODATA. So we use the kernel identity mapping instead
+	 * of the kernel text mapping to modify the kernel text.
+	 *
+	 * For 32bit kernels, these mappings are same and we can use
+	 * kernel identity mapping to modify code.
+	 */
+	if (within(ip, (unsigned long)_text, (unsigned long)_etext))
+		ip = (unsigned long)__va(__pa(ip));
+
 	mod_code_ip = (void *)ip;
 	mod_code_newcode = new_code;
 
arch/x86/mm/pageattr.c +1 -2
index 8d1e8d9..09a140c 100644
@@ -279,8 +279,7 @@ static inline pgprot_t static_protections(pgprot_t prot, unsigned long address,
 		   __pa((unsigned long)__end_rodata) >> PAGE_SHIFT))
 		pgprot_val(forbidden) |= _PAGE_RW;
 
-#if defined(CONFIG_X86_64) && defined(CONFIG_DEBUG_RODATA) && \
-	!defined(CONFIG_DYNAMIC_FTRACE)
+#if defined(CONFIG_X86_64) && defined(CONFIG_DEBUG_RODATA)
 	/*
 	 * Once the kernel maps the text as RO (kernel_set_to_readonly is set),
 	 * kernel text mappings for the large page aligned text, rodata sections
#3 lore raw

PATCH 3/3 x86_64, cpa: use only text section in set_kernel_text_rw/ro

In reply to Suresh Siddha

set_kernel_text_rw()/set_kernel_text_ro() are marking pages starting from _text to __start_rodata as RW or RO.

With CONFIG_DEBUG_RODATA, there might be free pages (associated with padding the sections to 2MB large page boundary) between text and rodata sections that are given back to page allocator. So we should use only use the start (__text) and end (__stop___ex_table) of the text section in set_kernel_text_rw()/set_kernel_text_ro().

---
 arch/x86/mm/init_64.c |   12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)
arch/x86/mm/init_64.c +10 -2
Index: tip/arch/x86/mm/init_64.c
===================================================================
@@ -700,7 +700,7 @@ int kernel_set_to_readonly;
 void set_kernel_text_rw(void)
 {
 	unsigned long start = PFN_ALIGN(_text);
-	unsigned long end = PFN_ALIGN(__start_rodata);
+	unsigned long end = PFN_ALIGN(__stop___ex_table);
 
 	if (!kernel_set_to_readonly)
 		return;
@@ -708,13 +708,18 @@ void set_kernel_text_rw(void)
 	pr_debug("Set kernel text: %lx - %lx for read write\n",
 		 start, end);
 
+	/*
+	 * Make the kernel identity mapping for text RW. Kernel text
+	 * mapping will always be RO. Refer to the comment in
+	 * static_protections() in pageattr.c
+	 */
 	set_memory_rw(start, (end - start) >> PAGE_SHIFT);
 }
 
 void set_kernel_text_ro(void)
 {
 	unsigned long start = PFN_ALIGN(_text);
-	unsigned long end = PFN_ALIGN(__start_rodata);
+	unsigned long end = PFN_ALIGN(__stop___ex_table);
 
 	if (!kernel_set_to_readonly)
 		return;
@@ -722,6 +727,9 @@ void set_kernel_text_ro(void)
 	pr_debug("Set kernel text: %lx - %lx for read only\n",
 		 start, end);
 
+	/*
+ 	 * Set the kernel identity mapping for text RO.
+ 	 */
 	set_memory_ro(start, (end - start) >> PAGE_SHIFT);
 }
 
maintainer
#4 lore raw

Re: [patch 3/3] x86_64, cpa: use only text section in set_kernel_text_rw/ro

Quoted 10 lines · Suresh Siddha

plain text document attachment (fix_kernel_text_ro_rw.patch) set_kernel_text_rw()/set_kernel_text_ro() are marking pages starting from _text to __start_rodata as RW or RO.

With CONFIG_DEBUG_RODATA, there might be free pages (associated with padding the sections to 2MB large page boundary) between text and rodata sections that are given back to page allocator. So we should use only use the start (__text) and end (__stop___ex_table) of the text section in set_kernel_text_rw()/set_kernel_text_ro().

Quoted 4 lines

---

I'll go ahead and test this to make sure it does not bust ftrace with an allyesconfig.

Thanks,

-- Steve

#5 lore raw

Re: [patch 3/3] x86_64, cpa: use only text section in set_kernel_text_rw/ro

Quoted 11 lines · Steven Rostedt
Quoted 10 lines · Suresh Siddha

plain text document attachment (fix_kernel_text_ro_rw.patch) set_kernel_text_rw()/set_kernel_text_ro() are marking pages starting from _text to __start_rodata as RW or RO.

With CONFIG_DEBUG_RODATA, there might be free pages (associated with padding the sections to 2MB large page boundary) between text and rodata sections that are given back to page allocator. So we should use only use the start (__text) and end (__stop___ex_table) of the text section in set_kernel_text_rw()/set_kernel_text_ro().

Quoted 7 lines
Quoted 4 lines

---

I'll go ahead and test this to make sure it does not bust ftrace with an allyesconfig.

Steve, Good to go?

If so, Ingo can you please consider this patchset?

thanks,
suresh

maintainer Tested-by
#6 lore raw

Re: [patch 3/3] x86_64, cpa: use only text section in set_kernel_text_rw/ro

Quoted 17 lines · Suresh Siddha
Quoted 16 lines · Steven Rostedt
Quoted 14 lines · Suresh Siddha

plain text document attachment (fix_kernel_text_ro_rw.patch) set_kernel_text_rw()/set_kernel_text_ro() are marking pages starting from _text to __start_rodata as RW or RO.

With CONFIG_DEBUG_RODATA, there might be free pages (associated with padding the sections to 2MB large page boundary) between text and rodata sections that are given back to page allocator. So we should use only use the start (__text) and end (__stop___ex_table) of the text section in set_kernel_text_rw()/set_kernel_text_ro().

---

Quoted 4 lines
Quoted 2 lines

I'll go ahead and test this to make sure it does not bust ftrace with an allyesconfig.

Steve, Good to go?

Yep. Sorry, during the testing I hit the jprobe vs function graph bug and got distracted. But as for this patch set ...

Thanks,

-- Steve

Quoted 2 lines

If so, Ingo can you please consider this patchset?

#7 lore raw

[tip:x86/mm] x86_64, cpa: Use only text section in set_kernel_text_rw/ro

In reply to Suresh Siddha
Commit-ID:  e7d23dde9b7ebb575e2bcee2abefc9ec1e4adde9
Gitweb:     http://git.kernel.org/tip/e7d23dde9b7ebb575e2bcee2abefc9ec1e4adde9
Author:     Suresh Siddha <[email protected]>
AuthorDate: Wed, 28 Oct 2009 18:46:58 -0800
Committer:  Ingo Molnar <[email protected]>
CommitDate: Mon, 2 Nov 2009 17:17:24 +0100

x86_64, cpa: Use only text section in set_kernel_text_rw/ro

set_kernel_text_rw()/set_kernel_text_ro() are marking pages starting from _text to __start_rodata as RW or RO.

With CONFIG_DEBUG_RODATA, there might be free pages (associated with padding the sections to 2MB large page boundary) between text and rodata sections that are given back to page allocator. So we should use only use the start (__text) and end (__stop___ex_table) of the text section in set_kernel_text_rw()/set_kernel_text_ro().

LKML-Reference: <[email protected]>

---
 arch/x86/mm/init_64.c |   12 ++++++++++--
 1 files changed, 10 insertions(+), 2 deletions(-)
arch/x86/mm/init_64.c +10 -2
index 4b507c0..5198b9b 100644
@@ -700,7 +700,7 @@ int kernel_set_to_readonly;
 void set_kernel_text_rw(void)
 {
 	unsigned long start = PFN_ALIGN(_text);
-	unsigned long end = PFN_ALIGN(__start_rodata);
+	unsigned long end = PFN_ALIGN(__stop___ex_table);
 
 	if (!kernel_set_to_readonly)
 		return;
@@ -708,13 +708,18 @@ void set_kernel_text_rw(void)
 	pr_debug("Set kernel text: %lx - %lx for read write\n",
 		 start, end);
 
+	/*
+	 * Make the kernel identity mapping for text RW. Kernel text
+	 * mapping will always be RO. Refer to the comment in
+	 * static_protections() in pageattr.c
+	 */
 	set_memory_rw(start, (end - start) >> PAGE_SHIFT);
 }
 
 void set_kernel_text_ro(void)
 {
 	unsigned long start = PFN_ALIGN(_text);
-	unsigned long end = PFN_ALIGN(__start_rodata);
+	unsigned long end = PFN_ALIGN(__stop___ex_table);
 
 	if (!kernel_set_to_readonly)
 		return;
@@ -722,6 +727,9 @@ void set_kernel_text_ro(void)
 	pr_debug("Set kernel text: %lx - %lx for read only\n",
 		 start, end);
 
+	/*
+	 * Set the kernel identity mapping for text RO.
+	 */
 	set_memory_ro(start, (end - start) >> PAGE_SHIFT);
 }
 
maintainer Acked-by on v1 1/3
#8 lore raw
In reply to Suresh Siddha
Quoted 7 lines · Suresh Siddha

plain text document attachment (fix_cpa_check.patch)
Steven Rostedt reported that we are unconditionally making the kernel text mapping as read-only. i.e., if someone does cpa() to the kernel text area for setting/clearing any page table attribute, we unconditionally clear the read-write attribute for the kernel text mapping that is set at compile time.

Quoted 5 lines

We should delay (to forbid the write attribute) and enforce only after the kernel has mapped the text as read-only.

Heh, I wrote an almost identical patch myself. It would have been exact, but I did not make the comment change ;-)

-- Steve

#9 lore raw

[tip:x86/mm] x86, cpa: Fix kernel text RO checks in static_protection()

In reply to Suresh Siddha
Commit-ID:  502f660466ba7a66711ffdf414b1f7f1131dcbf7
Gitweb:     http://git.kernel.org/tip/502f660466ba7a66711ffdf414b1f7f1131dcbf7
Author:     Suresh Siddha <[email protected]>
AuthorDate: Wed, 28 Oct 2009 18:46:56 -0800
Committer:  Ingo Molnar <[email protected]>
CommitDate: Mon, 2 Nov 2009 17:16:35 +0100

x86, cpa: Fix kernel text RO checks in static_protection()

Steven Rostedt reported that we are unconditionally making the kernel text mapping as read-only. i.e., if someone does cpa() to the kernel text area for setting/clearing any page table attribute, we unconditionally clear the read-write attribute for the kernel text mapping that is set at compile time.

We should delay (to forbid the write attribute) and enforce only after the kernel has mapped the text as read-only.

LKML-Reference: <[email protected]>

[ marked kernel_set_to_readonly as __read_mostly ]
---
 arch/x86/include/asm/cacheflush.h |    1 +
 arch/x86/mm/init_32.c             |    2 +-
 arch/x86/mm/init_64.c             |    2 +-
 arch/x86/mm/pageattr.c            |   10 ++++++----
 4 files changed, 9 insertions(+), 6 deletions(-)
arch/x86/include/asm/cacheflush.h +1 -0
index b54f6af..eebb2cd 100644
@@ -176,6 +176,7 @@ void clflush_cache_range(void *addr, unsigned int size);
 #ifdef CONFIG_DEBUG_RODATA
 void mark_rodata_ro(void);
 extern const int rodata_test_data;
+extern int kernel_set_to_readonly;
 void set_kernel_text_rw(void);
 void set_kernel_text_ro(void);
 #else
arch/x86/mm/init_32.c +1 -1
index f64d0d5..c973f8e 100644
@@ -997,7 +997,7 @@ static noinline int do_test_wp_bit(void)
 const int rodata_test_data = 0xC3;
 EXPORT_SYMBOL_GPL(rodata_test_data);
 
-static int kernel_set_to_readonly;
+int kernel_set_to_readonly __read_mostly;
 
 void set_kernel_text_rw(void)
 {
arch/x86/mm/init_64.c +1 -1
index 0ed09fa..4b507c0 100644
@@ -695,7 +695,7 @@ void __init mem_init(void)
 const int rodata_test_data = 0xC3;
 EXPORT_SYMBOL_GPL(rodata_test_data);
 
-static int kernel_set_to_readonly;
+int kernel_set_to_readonly;
 
 void set_kernel_text_rw(void)
 {
arch/x86/mm/pageattr.c +6 -4
index 78d3168..8d1e8d9 100644
@@ -282,14 +282,16 @@ static inline pgprot_t static_protections(pgprot_t prot, unsigned long address,
 #if defined(CONFIG_X86_64) && defined(CONFIG_DEBUG_RODATA) && \
 	!defined(CONFIG_DYNAMIC_FTRACE)
 	/*
-	 * Kernel text mappings for the large page aligned .rodata section
-	 * will be read-only. For the kernel identity mappings covering
-	 * the holes caused by this alignment can be anything.
+	 * Once the kernel maps the text as RO (kernel_set_to_readonly is set),
+	 * kernel text mappings for the large page aligned text, rodata sections
+	 * will be always read-only. For the kernel identity mappings covering
+	 * the holes caused by this alignment can be anything that user asks.
 	 *
 	 * This will preserve the large page mappings for kernel text/data
 	 * at no extra cost.
 	 */
-	if (within(address, (unsigned long)_text,
+	if (kernel_set_to_readonly &&
+	    within(address, (unsigned long)_text,
 		   (unsigned long)__end_rodata_hpage_align))
 		pgprot_val(forbidden) |= _PAGE_RW;
 #endif