Skip to contentSkip to conversation

provide check for ro_after_init memory sections

RFC PATCH Superseded Eddie Kovsky 2 participants 8 messages Feb 12, 2017 – Feb 15, 2017 Module support

v1 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

cover provide check for ro_after_init memory sections

Provide a mechansim for other functions to verify that their arguments are read-only. This implements the first half of a suggestion made by Kees Cook for the Kernel Self Protection Project:

* provide mechanism to check for ro_after_init memory areas, and reject structures not marked ro_after_init in vmbus_register()

http://www.openwall.com/lists/kernel-hardening/2017/02/04/1

I have succesfully compiled this series on next-20170206 for x86. I am not sure how to go about testing these changes (perhpas with LKDTM?).

Eddie Kovsky (2):
[RFC] [PATCH 1/2] module: verify address is read-only [RFC] [PATCH 2/2] extable: verify address is read-only

 include/linux/kernel.h |  2 ++
 include/linux/module.h |  2 ++
 kernel/extable.c       | 31 +++++++++++++++++++++++++++++++
 kernel/module.c        | 44 ++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 79 insertions(+)

--
2.11.1

#1 lore raw

RFC PATCH 1/2 module: verify address is read-only

Implement a mechanism to check if a module's address is in the rodata or ro_after_init sections. It mimics the exsiting functions that test if an address is inside a module's text section.

---
 include/linux/module.h |  2 ++
 kernel/module.c        | 44 ++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 46 insertions(+)
include/linux/module.h +2 -0
index 0297c5cd7cdf..2848416f7715 100644
@@ -492,7 +492,9 @@ static inline int module_is_live(struct module *mod)
 
 struct module *__module_text_address(unsigned long addr);
 struct module *__module_address(unsigned long addr);
+struct module *__module_ro_address(unsigned long addr);
 bool is_module_address(unsigned long addr);
+bool is_module_ro_address(unsigned long addr);
 bool is_module_percpu_address(unsigned long addr);
 bool is_module_text_address(unsigned long addr);
 
kernel/module.c +44 -0

50 lines not on this page · raw message

--
2.11.1

#2 lore raw

[kernel-hardening] Re: [RFC] [PATCH 1/2] module: verify address is read-only

Quoted 15 lines · Eddie Kovsky

Implement a mechanism to check if a module's address is in the rodata or ro_after_init sections. It mimics the exsiting functions that test if an address is inside a module's text section.

---
 include/linux/module.h |  2 ++
 kernel/module.c        | 44 ++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 46 insertions(+)
include/linux/module.h +0 -0
index 0297c5cd7cdf..2848416f7715 100644

include/linux/module.h static inline int module_is_live(struct module *mod) · line 499

Quoted 8 lines
include/linux/module.h +2 -0
@@ -492,7 +492,9 @@ static inline int module_is_live(struct module *mod)
 
 struct module *__module_text_address(unsigned long addr);
 struct module *__module_address(unsigned long addr);
+struct module *__module_ro_address(unsigned long addr);
 bool is_module_address(unsigned long addr);
+bool is_module_ro_address(unsigned long addr);
 bool is_module_percpu_address(unsigned long addr);
 bool is_module_text_address(unsigned long addr);

This will need the !CONFIG_MODULES stubs added as well (see __module_address()). e.g.:

...
#else /* !CONFIG_MODULES... */
...
static incline struct module *__module_ro_address(unsigned long addr)

{
       return NULL;
}

...
#endif

Quoted 42 lines
kernel/module.c +33 -0
index 8168eb738f4d..e7eb329c585d 100644
@@ -4270,6 +4270,50 @@ struct module *__module_text_address(unsigned long addr)
 }
 EXPORT_SYMBOL_GPL(__module_text_address);
 
+/**
+ * is_module_text_ro_address - is this address inside read-only module code?
+ * @addr: the address to check.
+ *
+ */
+bool is_module_ro_address(unsigned long addr)
+{
+       bool ret;
+
+       preempt_disable();
+       ret = __module_ro_address(addr) != NULL;
+       preempt_enable();
+
+       return ret;
+}
+
+/*
+ * __module_ro_address - get the module whose code contains a read-only address.
+ * @addr: the address.
+ *
+ * Must be called with preempt disabled or module mutex held so that
+ * module doesn't get freed during this.
+ */
+struct module *__module_ro_address(unsigned long addr)
+{
+       struct module *mod = __module_address(addr);
+
+       if (mod) {
+               /* Make sure it's within the read-only section. */
+               if (!within(addr, mod->init_layout.base,
+                           mod->init_layout.ro_size)
+                   && !within(addr, mod->core_layout.base,
+                              mod->core_layout.ro_size))

kernel/module.c struct module *__module_text_address(unsigned long addr) · line 4315

Quoted 10 lines
kernel/module.c +10 -0
+                       mod = NULL;
+               if (!within(addr, mod->init_layout.base,
+                           mod->init_layout.ro_after_init_size)
+                   && !within(addr, mod->core_layout.base,
+                              mod->core_layout.ro_after_init_size))
+                       mod = NULL;
+       }
+       return mod;
+}
+EXPORT_SYMBOL_GPL(__module_ro_address);

This follows what is_module_address() does, so that seems sensible to me. I defer to Jessica and Rusty, though. :)

-Kees

Signature · 2 lines
Kees Cook
Pixel Security
#3 lore raw

[kernel-hardening] Re: [RFC] [PATCH 1/2] module: verify address is read-only

Quoted 94 lines · Kees Cook
Quoted 15 lines · Eddie Kovsky

Implement a mechanism to check if a module's address is in the rodata or ro_after_init sections. It mimics the exsiting functions that test if an address is inside a module's text section.

---
 include/linux/module.h |  2 ++
 kernel/module.c        | 44 ++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 46 insertions(+)
include/linux/module.h +0 -0
index 0297c5cd7cdf..2848416f7715 100644

include/linux/module.h static inline int module_is_live(struct module *mod) · line 499

Quoted 8 lines
include/linux/module.h +2 -0
@@ -492,7 +492,9 @@ static inline int module_is_live(struct module *mod)
 
 struct module *__module_text_address(unsigned long addr);
 struct module *__module_address(unsigned long addr);
+struct module *__module_ro_address(unsigned long addr);
 bool is_module_address(unsigned long addr);
+bool is_module_ro_address(unsigned long addr);
 bool is_module_percpu_address(unsigned long addr);
 bool is_module_text_address(unsigned long addr);

This will need the !CONFIG_MODULES stubs added as well (see __module_address()). e.g.:

...
#else /* !CONFIG_MODULES... */
...
static incline struct module *__module_ro_address(unsigned long addr)

{
       return NULL;
}

...
#endif

Quoted 42 lines
kernel/module.c +33 -0
index 8168eb738f4d..e7eb329c585d 100644
@@ -4270,6 +4270,50 @@ struct module *__module_text_address(unsigned long addr)
 }
 EXPORT_SYMBOL_GPL(__module_text_address);
 
+/**
+ * is_module_text_ro_address - is this address inside read-only module code?
+ * @addr: the address to check.
+ *
+ */
+bool is_module_ro_address(unsigned long addr)
+{
+       bool ret;
+
+       preempt_disable();
+       ret = __module_ro_address(addr) != NULL;
+       preempt_enable();
+
+       return ret;
+}
+
+/*
+ * __module_ro_address - get the module whose code contains a read-only address.
+ * @addr: the address.
+ *
+ * Must be called with preempt disabled or module mutex held so that
+ * module doesn't get freed during this.
+ */
+struct module *__module_ro_address(unsigned long addr)
+{
+       struct module *mod = __module_address(addr);
+
+       if (mod) {
+               /* Make sure it's within the read-only section. */
+               if (!within(addr, mod->init_layout.base,
+                           mod->init_layout.ro_size)
+                   && !within(addr, mod->core_layout.base,
+                              mod->core_layout.ro_size))

kernel/module.c struct module *__module_text_address(unsigned long addr) · line 4315

Quoted 10 lines
kernel/module.c +10 -0
+                       mod = NULL;
+               if (!within(addr, mod->init_layout.base,
+                           mod->init_layout.ro_after_init_size)
+                   && !within(addr, mod->core_layout.base,
+                              mod->core_layout.ro_after_init_size))
+                       mod = NULL;
+       }
+       return mod;
+}
+EXPORT_SYMBOL_GPL(__module_ro_address);

This follows what is_module_address() does, so that seems sensible to me. I defer to Jessica and Rusty, though. :)

Quoted 5 lines

-Kees

--
Kees Cook
Pixel Security

I'll fix this up and add the vmbus_register() patch for the next series.

Thanks

Eddie

#4 lore raw

RFC PATCH 2/2 extable: verify address is read-only

In reply to Eddie Kovsky

Provide a mechanism to check if the address of a variable is const or ro_after_init. It mimics the existing functions that test if an address is inside the kernel's text section.

---
 include/linux/kernel.h |  2 ++
 kernel/extable.c       | 31 +++++++++++++++++++++++++++++++
 2 files changed, 33 insertions(+)
include/linux/kernel.h +2 -0
index 4c26dc3a8295..51beea39e6c4 100644
@@ -444,6 +444,8 @@ extern int core_kernel_data(unsigned long addr);
 extern int __kernel_text_address(unsigned long addr);
 extern int kernel_text_address(unsigned long addr);
 extern int func_ptr_is_kernel_text(void *ptr);
+extern int core_kernel_ro_data(unsigned long addr);
+extern int kernel_ro_address(unsigned long addr);
 
 unsigned long int_sqrt(unsigned long);
 
kernel/extable.c +31 -0
index 6b0d09051efb..f5a29c4ae391 100644
@@ -149,3 +149,34 @@ int func_ptr_is_kernel_text(void *ptr)
 		return 1;
 	return is_module_text_address(addr);
 }
+
+/**
+ * core_kernel_ro_data - Verify address points to read-only section
+ * @addr: address to test
+ *
+ */
+int core_kernel_ro_data(unsigned long addr)
+{
+	if (addr >= (unsigned long)__start_rodata &&
+	    addr < (unsigned long)__end_rodata)
+		return 1;
+
+	if (addr >= (unsigned long)__start_data_ro_after_init &&
+	    addr < (unsigned long)__end_data_ro_after_init)
+		return 1;
+
+	return 0;
+}
+
+/* Verify that address is const or ro_after_init. */
+int kernel_ro_address(unsigned long addr)
+{
+	if (core_kernel_ro_data(addr))
+		return 1;
+	if (is_module_ro_address(addr))
+		return 1;
+	if (is_ftrace_trampoline(addr))
+		return 1;
+
+	return 0;
+}

--
2.11.1

#5 lore raw

[kernel-hardening] Re: [RFC] [PATCH 2/2] extable: verify address is read-only

Quoted 49 lines · Eddie Kovsky

Provide a mechanism to check if the address of a variable is const or ro_after_init. It mimics the existing functions that test if an address is inside the kernel's text section.

---
 include/linux/kernel.h |  2 ++
 kernel/extable.c       | 31 +++++++++++++++++++++++++++++++
 2 files changed, 33 insertions(+)
include/linux/kernel.h +2 -0
index 4c26dc3a8295..51beea39e6c4 100644
@@ -444,6 +444,8 @@ extern int core_kernel_data(unsigned long addr);
 extern int __kernel_text_address(unsigned long addr);
 extern int kernel_text_address(unsigned long addr);
 extern int func_ptr_is_kernel_text(void *ptr);
+extern int core_kernel_ro_data(unsigned long addr);
+extern int kernel_ro_address(unsigned long addr);
 
 unsigned long int_sqrt(unsigned long);
 
kernel/extable.c +18 -0
index 6b0d09051efb..f5a29c4ae391 100644
@@ -149,3 +149,34 @@ int func_ptr_is_kernel_text(void *ptr)
                return 1;
        return is_module_text_address(addr);
 }
+
+/**
+ * core_kernel_ro_data - Verify address points to read-only section
+ * @addr: address to test
+ *
+ */
+int core_kernel_ro_data(unsigned long addr)
+{
+       if (addr >= (unsigned long)__start_rodata &&
+           addr < (unsigned long)__end_rodata)
+               return 1;
+
+       if (addr >= (unsigned long)__start_data_ro_after_init &&
+           addr < (unsigned long)__end_data_ro_after_init)
+               return 1;
+
+       return 0;
+}

kernel/extable.c int func_ptr_is_kernel_text(void *ptr) · line 179

Quoted 10 lines
kernel/extable.c +10 -0
+
+/* Verify that address is const or ro_after_init. */
+int kernel_ro_address(unsigned long addr)
+{
+       if (core_kernel_ro_data(addr))
+               return 1;
+       if (is_module_ro_address(addr))
+               return 1;
+       if (is_ftrace_trampoline(addr))
+               return 1;

Why the trampoline test here?

Quoted 5 lines
kernel/extable.c +3 -1
+
+       return 0;
+}
--

2.11.1

Otherwise looks exactly like what I had in mind; nice!

-Kees

Signature · 2 lines
Kees Cook
Pixel Security
#6 lore raw

[kernel-hardening] Re: [RFC] [PATCH 2/2] extable: verify address is read-only

Quoted 54 lines · Kees Cook
Quoted 53 lines · Eddie Kovsky

Provide a mechanism to check if the address of a variable is const or ro_after_init. It mimics the existing functions that test if an address is inside the kernel's text section.

---
 include/linux/kernel.h |  2 ++
 kernel/extable.c       | 31 +++++++++++++++++++++++++++++++
 2 files changed, 33 insertions(+)
include/linux/kernel.h +2 -0
index 4c26dc3a8295..51beea39e6c4 100644
@@ -444,6 +444,8 @@ extern int core_kernel_data(unsigned long addr);
 extern int __kernel_text_address(unsigned long addr);
 extern int kernel_text_address(unsigned long addr);
 extern int func_ptr_is_kernel_text(void *ptr);
+extern int core_kernel_ro_data(unsigned long addr);
+extern int kernel_ro_address(unsigned long addr);
 
 unsigned long int_sqrt(unsigned long);
 
kernel/extable.c +22 -0
index 6b0d09051efb..f5a29c4ae391 100644
@@ -149,3 +149,34 @@ int func_ptr_is_kernel_text(void *ptr)
                return 1;
        return is_module_text_address(addr);
 }
+
+/**
+ * core_kernel_ro_data - Verify address points to read-only section
+ * @addr: address to test
+ *
+ */
+int core_kernel_ro_data(unsigned long addr)
+{
+       if (addr >= (unsigned long)__start_rodata &&
+           addr < (unsigned long)__end_rodata)
+               return 1;
+
+       if (addr >= (unsigned long)__start_data_ro_after_init &&
+           addr < (unsigned long)__end_data_ro_after_init)
+               return 1;
+
+       return 0;
+}
+
+/* Verify that address is const or ro_after_init. */
+int kernel_ro_address(unsigned long addr)
+{
Quoted 9 lines

kernel/extable.c int func_ptr_is_kernel_text(void *ptr) · line 179

Quoted 6 lines
kernel/extable.c +6 -0
+       if (core_kernel_ro_data(addr))
+               return 1;
+       if (is_module_ro_address(addr))
+               return 1;
+       if (is_ftrace_trampoline(addr))
+               return 1;

Why the trampoline test here?

I wasn't certain if we needed to keep that as a fall through. I'll take it out when I send version 2.

Quoted 13 lines
Quoted 5 lines
kernel/extable.c +3 -1
+
+       return 0;
+}
--

2.11.1

Otherwise looks exactly like what I had in mind; nice!

-Kees

Signature · 2 lines
Kees Cook
Pixel Security
#7 lore raw
In reply to Eddie Kovsky
Quoted 8 lines · Eddie Kovsky

Provide a mechansim for other functions to verify that their arguments are read-only. This implements the first half of a suggestion made by Kees Cook for the Kernel Self Protection Project:

* provide mechanism to check for ro_after_init memory areas, and reject structures not marked ro_after_init in vmbus_register()

http://www.openwall.com/lists/kernel-hardening/2017/02/04/1

Awesome! I'd add the third patch too (to have vmbus_register() do the check), just to have an example in the series.

Quoted 2 lines

I have succesfully compiled this series on next-20170206 for x86. I am not sure how to go about testing these changes (perhpas with LKDTM?).

Hmm. LKDTM doesn't seem quite right since it's expecting to trip a kernel self-protection check of some kind (and already has an ro_after_init check). I think just adding users of this is a sufficient test (i.e. vmbus_register()), since the ro_after_init infrastructure is already being tested.

-Kees

Signature · 2 lines
Kees Cook
Pixel Security