mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Christophe Leroy <christophe.leroy@csgroup.eu>
To: Aaron Tomlin <atomlin@redhat.com>,
	"mcgrof@kernel.org" <mcgrof@kernel.org>
Cc: "cl@linux.com" <cl@linux.com>,
	"pmladek@suse.com" <pmladek@suse.com>,
	"mbenes@suse.cz" <mbenes@suse.cz>,
	"akpm@linux-foundation.org" <akpm@linux-foundation.org>,
	"jeyu@kernel.org" <jeyu@kernel.org>,
	"linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>,
	"linux-modules@vger.kernel.org" <linux-modules@vger.kernel.org>,
	"void@manifault.com" <void@manifault.com>,
	"atomlin@atomlin.com" <atomlin@atomlin.com>,
	"allen.lkml@gmail.com" <allen.lkml@gmail.com>,
	"joe@perches.com" <joe@perches.com>,
	"msuchanek@suse.de" <msuchanek@suse.de>,
	"oleksandr@natalenko.name" <oleksandr@natalenko.name>
Subject: Re: [PATCH v8 06/13] module: Move strict rwx support to a separate file
Date: Tue, 22 Feb 2022 17:59:06 +0000	[thread overview]
Message-ID: <4a740d5d-6130-62e1-85e2-e429e5f4525e@csgroup.eu> (raw)
In-Reply-To: <20220222141303.1392190-7-atomlin@redhat.com>



Le 22/02/2022 à 15:12, Aaron Tomlin a écrit :
> No functional change.
> 
> This patch migrates code that makes module text
> and rodata memory read-only and non-text memory
> non-executable from core module code into
> kernel/module/strict_rwx.c.
> 
> Signed-off-by: Aaron Tomlin <atomlin@redhat.com>

Reviewed-by: Christophe Leroy <christophe.leroy@csgroup.eu>

> ---
>   kernel/module/Makefile     |  1 +
>   kernel/module/internal.h   | 32 ++++++++++++
>   kernel/module/main.c       | 99 +-------------------------------------
>   kernel/module/strict_rwx.c | 85 ++++++++++++++++++++++++++++++++
>   4 files changed, 120 insertions(+), 97 deletions(-)
>   create mode 100644 kernel/module/strict_rwx.c
> 
> diff --git a/kernel/module/Makefile b/kernel/module/Makefile
> index 88774e386276..d313c8472cb3 100644
> --- a/kernel/module/Makefile
> +++ b/kernel/module/Makefile
> @@ -12,3 +12,4 @@ obj-$(CONFIG_MODULE_DECOMPRESS) += decompress.o
>   obj-$(CONFIG_MODULE_SIG) += signing.o
>   obj-$(CONFIG_LIVEPATCH) += livepatch.o
>   obj-$(CONFIG_MODULES_TREE_LOOKUP) += tree_lookup.o
> +obj-$(CONFIG_STRICT_MODULE_RWX) += strict_rwx.o
> diff --git a/kernel/module/internal.h b/kernel/module/internal.h
> index f1682e3677be..a6895bb5598a 100644
> --- a/kernel/module/internal.h
> +++ b/kernel/module/internal.h
> @@ -20,6 +20,17 @@
>   /* Maximum number of characters written by module_flags() */
>   #define MODULE_FLAGS_BUF_SIZE (TAINT_FLAGS_COUNT + 4)
>   
> +/*
> + * Modules' sections will be aligned on page boundaries
> + * to ensure complete separation of code and data, but
> + * only when CONFIG_ARCH_HAS_STRICT_MODULE_RWX=y
> + */
> +#ifdef CONFIG_ARCH_HAS_STRICT_MODULE_RWX
> +# define debug_align(X) PAGE_ALIGN(X)
> +#else
> +# define debug_align(X) (X)
> +#endif
> +
>   extern struct mutex module_mutex;
>   extern struct list_head modules;
>   
> @@ -126,3 +137,24 @@ static inline struct module *mod_find(unsigned long addr)
>   	return NULL;
>   }
>   #endif /* CONFIG_MODULES_TREE_LOOKUP */
> +
> +#ifdef CONFIG_ARCH_HAS_STRICT_MODULE_RWX
> +void frob_text(const struct module_layout *layout, int (*set_memory)(unsigned long start,
> +								     int num_pages));
> +#endif /* CONFIG_ARCH_HAS_STRICT_MODULE_RWX */
> +
> +#ifdef CONFIG_STRICT_MODULE_RWX
> +void module_enable_ro(const struct module *mod, bool after_init);
> +void module_enable_nx(const struct module *mod);
> +int module_enforce_rwx_sections(Elf_Ehdr *hdr, Elf_Shdr *sechdrs,
> +				char *secstrings, struct module *mod);
> +
> +#else /* !CONFIG_STRICT_MODULE_RWX */
> +static inline void module_enable_nx(const struct module *mod) { }
> +static inline void module_enable_ro(const struct module *mod, bool after_init) {}
> +static inline int module_enforce_rwx_sections(Elf_Ehdr *hdr, Elf_Shdr *sechdrs,
> +				       char *secstrings, struct module *mod)
> +{
> +	return 0;
> +}
> +#endif /* CONFIG_STRICT_MODULE_RWX */
> diff --git a/kernel/module/main.c b/kernel/module/main.c
> index 76b53880ad91..5cd63f14b1ef 100644
> --- a/kernel/module/main.c
> +++ b/kernel/module/main.c
> @@ -63,17 +63,6 @@
>   #define CREATE_TRACE_POINTS
>   #include <trace/events/module.h>
>   
> -/*
> - * Modules' sections will be aligned on page boundaries
> - * to ensure complete separation of code and data, but
> - * only when CONFIG_ARCH_HAS_STRICT_MODULE_RWX=y
> - */
> -#ifdef CONFIG_ARCH_HAS_STRICT_MODULE_RWX
> -# define debug_align(X) ALIGN(X, PAGE_SIZE)
> -#else
> -# define debug_align(X) (X)
> -#endif
> -
>   /*
>    * Mutex protects:
>    * 1) List of modules (also safely readable with preempt_disable),
> @@ -1819,8 +1808,8 @@ static void mod_sysfs_teardown(struct module *mod)
>    * whether we are strict.
>    */
>   #ifdef CONFIG_ARCH_HAS_STRICT_MODULE_RWX
> -static void frob_text(const struct module_layout *layout,
> -		      int (*set_memory)(unsigned long start, int num_pages))
> +void frob_text(const struct module_layout *layout,
> +	       int (*set_memory)(unsigned long start, int num_pages))
>   {
>   	BUG_ON((unsigned long)layout->base & (PAGE_SIZE-1));
>   	BUG_ON((unsigned long)layout->text_size & (PAGE_SIZE-1));
> @@ -1837,90 +1826,6 @@ static void module_enable_x(const struct module *mod)
>   static void module_enable_x(const struct module *mod) { }
>   #endif /* CONFIG_ARCH_HAS_STRICT_MODULE_RWX */
>   
> -#ifdef CONFIG_STRICT_MODULE_RWX
> -static void frob_rodata(const struct module_layout *layout,
> -			int (*set_memory)(unsigned long start, int num_pages))
> -{
> -	BUG_ON((unsigned long)layout->base & (PAGE_SIZE-1));
> -	BUG_ON((unsigned long)layout->text_size & (PAGE_SIZE-1));
> -	BUG_ON((unsigned long)layout->ro_size & (PAGE_SIZE-1));
> -	set_memory((unsigned long)layout->base + layout->text_size,
> -		   (layout->ro_size - layout->text_size) >> PAGE_SHIFT);
> -}
> -
> -static void frob_ro_after_init(const struct module_layout *layout,
> -				int (*set_memory)(unsigned long start, int num_pages))
> -{
> -	BUG_ON((unsigned long)layout->base & (PAGE_SIZE-1));
> -	BUG_ON((unsigned long)layout->ro_size & (PAGE_SIZE-1));
> -	BUG_ON((unsigned long)layout->ro_after_init_size & (PAGE_SIZE-1));
> -	set_memory((unsigned long)layout->base + layout->ro_size,
> -		   (layout->ro_after_init_size - layout->ro_size) >> PAGE_SHIFT);
> -}
> -
> -static void frob_writable_data(const struct module_layout *layout,
> -			       int (*set_memory)(unsigned long start, int num_pages))
> -{
> -	BUG_ON((unsigned long)layout->base & (PAGE_SIZE-1));
> -	BUG_ON((unsigned long)layout->ro_after_init_size & (PAGE_SIZE-1));
> -	BUG_ON((unsigned long)layout->size & (PAGE_SIZE-1));
> -	set_memory((unsigned long)layout->base + layout->ro_after_init_size,
> -		   (layout->size - layout->ro_after_init_size) >> PAGE_SHIFT);
> -}
> -
> -static void module_enable_ro(const struct module *mod, bool after_init)
> -{
> -	if (!rodata_enabled)
> -		return;
> -
> -	set_vm_flush_reset_perms(mod->core_layout.base);
> -	set_vm_flush_reset_perms(mod->init_layout.base);
> -	frob_text(&mod->core_layout, set_memory_ro);
> -
> -	frob_rodata(&mod->core_layout, set_memory_ro);
> -	frob_text(&mod->init_layout, set_memory_ro);
> -	frob_rodata(&mod->init_layout, set_memory_ro);
> -
> -	if (after_init)
> -		frob_ro_after_init(&mod->core_layout, set_memory_ro);
> -}
> -
> -static void module_enable_nx(const struct module *mod)
> -{
> -	frob_rodata(&mod->core_layout, set_memory_nx);
> -	frob_ro_after_init(&mod->core_layout, set_memory_nx);
> -	frob_writable_data(&mod->core_layout, set_memory_nx);
> -	frob_rodata(&mod->init_layout, set_memory_nx);
> -	frob_writable_data(&mod->init_layout, set_memory_nx);
> -}
> -
> -static int module_enforce_rwx_sections(Elf_Ehdr *hdr, Elf_Shdr *sechdrs,
> -				       char *secstrings, struct module *mod)
> -{
> -	const unsigned long shf_wx = SHF_WRITE|SHF_EXECINSTR;
> -	int i;
> -
> -	for (i = 0; i < hdr->e_shnum; i++) {
> -		if ((sechdrs[i].sh_flags & shf_wx) == shf_wx) {
> -			pr_err("%s: section %s (index %d) has invalid WRITE|EXEC flags\n",
> -				mod->name, secstrings + sechdrs[i].sh_name, i);
> -			return -ENOEXEC;
> -		}
> -	}
> -
> -	return 0;
> -}
> -
> -#else /* !CONFIG_STRICT_MODULE_RWX */
> -static void module_enable_nx(const struct module *mod) { }
> -static void module_enable_ro(const struct module *mod, bool after_init) {}
> -static int module_enforce_rwx_sections(Elf_Ehdr *hdr, Elf_Shdr *sechdrs,
> -				       char *secstrings, struct module *mod)
> -{
> -	return 0;
> -}
> -#endif /*  CONFIG_STRICT_MODULE_RWX */
> -
>   void __weak module_memfree(void *module_region)
>   {
>   	/*
> diff --git a/kernel/module/strict_rwx.c b/kernel/module/strict_rwx.c
> new file mode 100644
> index 000000000000..7949dfd449c2
> --- /dev/null
> +++ b/kernel/module/strict_rwx.c
> @@ -0,0 +1,85 @@
> +// SPDX-License-Identifier: GPL-2.0-or-later
> +/*
> + * Module strict rwx
> + *
> + * Copyright (C) 2015 Rusty Russell
> + */
> +
> +#include <linux/module.h>
> +#include <linux/mm.h>
> +#include <linux/vmalloc.h>
> +#include <linux/set_memory.h>
> +#include "internal.h"
> +
> +static void frob_rodata(const struct module_layout *layout,
> +		 int (*set_memory)(unsigned long start, int num_pages))
> +{
> +	BUG_ON(!PAGE_ALIGNED(layout->base));
> +	BUG_ON(!PAGE_ALIGNED(layout->text_size));
> +	BUG_ON(!PAGE_ALIGNED(layout->ro_size));
> +	set_memory((unsigned long)layout->base + layout->text_size,
> +		   (layout->ro_size - layout->text_size) >> PAGE_SHIFT);
> +}
> +
> +static void frob_ro_after_init(const struct module_layout *layout,
> +			int (*set_memory)(unsigned long start, int num_pages))
> +{
> +	BUG_ON(!PAGE_ALIGNED(layout->base));
> +	BUG_ON(!PAGE_ALIGNED(layout->ro_size));
> +	BUG_ON(!PAGE_ALIGNED(layout->ro_after_init_size));
> +	set_memory((unsigned long)layout->base + layout->ro_size,
> +		   (layout->ro_after_init_size - layout->ro_size) >> PAGE_SHIFT);
> +}
> +
> +static void frob_writable_data(const struct module_layout *layout,
> +			int (*set_memory)(unsigned long start, int num_pages))
> +{
> +	BUG_ON(!PAGE_ALIGNED(layout->base));
> +	BUG_ON(!PAGE_ALIGNED(layout->ro_after_init_size));
> +	BUG_ON(!PAGE_ALIGNED(layout->size));
> +	set_memory((unsigned long)layout->base + layout->ro_after_init_size,
> +		   (layout->size - layout->ro_after_init_size) >> PAGE_SHIFT);
> +}
> +
> +void module_enable_ro(const struct module *mod, bool after_init)
> +{
> +	if (!rodata_enabled)
> +		return;
> +
> +	set_vm_flush_reset_perms(mod->core_layout.base);
> +	set_vm_flush_reset_perms(mod->init_layout.base);
> +	frob_text(&mod->core_layout, set_memory_ro);
> +
> +	frob_rodata(&mod->core_layout, set_memory_ro);
> +	frob_text(&mod->init_layout, set_memory_ro);
> +	frob_rodata(&mod->init_layout, set_memory_ro);
> +
> +	if (after_init)
> +		frob_ro_after_init(&mod->core_layout, set_memory_ro);
> +}
> +
> +void module_enable_nx(const struct module *mod)
> +{
> +	frob_rodata(&mod->core_layout, set_memory_nx);
> +	frob_ro_after_init(&mod->core_layout, set_memory_nx);
> +	frob_writable_data(&mod->core_layout, set_memory_nx);
> +	frob_rodata(&mod->init_layout, set_memory_nx);
> +	frob_writable_data(&mod->init_layout, set_memory_nx);
> +}
> +
> +int module_enforce_rwx_sections(Elf_Ehdr *hdr, Elf_Shdr *sechdrs,
> +				char *secstrings, struct module *mod)
> +{
> +	const unsigned long shf_wx = SHF_WRITE | SHF_EXECINSTR;
> +	int i;
> +
> +	for (i = 0; i < hdr->e_shnum; i++) {
> +		if ((sechdrs[i].sh_flags & shf_wx) == shf_wx) {
> +			pr_err("%s: section %s (index %d) has invalid WRITE|EXEC flags\n",
> +			       mod->name, secstrings + sechdrs[i].sh_name, i);
> +			return -ENOEXEC;
> +		}
> +	}
> +
> +	return 0;
> +}

  reply	other threads:[~2022-02-22 17:59 UTC|newest]

Thread overview: 43+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-02-22 14:12 [PATCH v8 00/13] module: core code clean up Aaron Tomlin
2022-02-22 14:12 ` [PATCH v8 01/13] module: Move all into module/ Aaron Tomlin
2022-02-22 17:58   ` Christophe Leroy
2022-02-22 20:00   ` Allen
2022-02-22 14:12 ` [PATCH v8 02/13] module: Simple refactor in preparation for split Aaron Tomlin
2022-02-22 17:58   ` Christophe Leroy
2022-02-22 14:12 ` [PATCH v8 03/13] module: Make internal.h and decompress.c more compliant Aaron Tomlin
2022-02-22 14:12 ` [PATCH v8 04/13] module: Move livepatch support to a separate file Aaron Tomlin
2022-02-22 17:58   ` Christophe Leroy
2022-02-25  9:34   ` Petr Mladek
2022-02-25 10:33     ` Aaron Tomlin
2022-02-25 16:49     ` Christophe Leroy
2022-02-28 10:56       ` Petr Mladek
2022-02-28 11:46         ` Christophe Leroy
2022-02-28 13:05           ` Petr Mladek
2022-02-22 14:12 ` [PATCH v8 05/13] module: Move latched RB-tree " Aaron Tomlin
2022-02-22 17:58   ` Christophe Leroy
2022-02-22 14:12 ` [PATCH v8 06/13] module: Move strict rwx " Aaron Tomlin
2022-02-22 17:59   ` Christophe Leroy [this message]
2022-02-22 14:12 ` [PATCH v8 07/13] module: Move extra signature support out of core code Aaron Tomlin
2022-02-22 17:59   ` Christophe Leroy
2022-02-22 14:12 ` [PATCH v8 08/13] module: Move kmemleak support to a separate file Aaron Tomlin
2022-02-22 17:59   ` Christophe Leroy
2022-02-22 14:12 ` [PATCH v8 09/13] module: Move kallsyms support into " Aaron Tomlin
2022-02-22 17:59   ` Christophe Leroy
2022-02-25  9:15   ` Petr Mladek
2022-02-25  9:27     ` Christophe Leroy
2022-02-25 10:15       ` Petr Mladek
2022-02-25 10:27         ` Aaron Tomlin
2022-02-25 12:21           ` Aaron Tomlin
2022-02-25 12:57             ` Christophe Leroy
2022-02-26 20:27               ` Luis Chamberlain
2022-02-28  9:02                 ` Christophe Leroy
2022-02-28  9:31                   ` Aaron Tomlin
2022-02-28  9:33                     ` Christophe Leroy
2022-02-22 14:13 ` [PATCH v8 10/13] module: Move procfs " Aaron Tomlin
2022-02-22 17:59   ` Christophe Leroy
2022-02-22 14:13 ` [PATCH v8 11/13] module: Move sysfs " Aaron Tomlin
2022-02-22 17:59   ` Christophe Leroy
2022-02-22 14:13 ` [PATCH v8 12/13] module: Move kdb_modules list out of core code Aaron Tomlin
2022-02-22 18:05   ` Christophe Leroy
2022-02-22 14:13 ` [PATCH v8 13/13] module: Move version support into a separate file Aaron Tomlin
2022-02-22 18:06   ` Christophe Leroy

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=4a740d5d-6130-62e1-85e2-e429e5f4525e@csgroup.eu \
    --to=christophe.leroy@csgroup.eu \
    --cc=akpm@linux-foundation.org \
    --cc=allen.lkml@gmail.com \
    --cc=atomlin@atomlin.com \
    --cc=atomlin@redhat.com \
    --cc=cl@linux.com \
    --cc=jeyu@kernel.org \
    --cc=joe@perches.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-modules@vger.kernel.org \
    --cc=mbenes@suse.cz \
    --cc=mcgrof@kernel.org \
    --cc=msuchanek@suse.de \
    --cc=oleksandr@natalenko.name \
    --cc=pmladek@suse.com \
    --cc=void@manifault.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®