From: Christophe Leroy <christophe.leroy@csgroup.eu>
To: Aaron Tomlin <atomlin@redhat.com>,
Luis Chamberlain <mcgrof@kernel.org>,
linux-modules@vger.kernel.org
Cc: Christophe Leroy <christophe.leroy@csgroup.eu>,
linux-kernel@vger.kernel.org
Subject: [PATCH v1 5/6] module: Rework layout alignment to avoid BUG_ON()s
Date: Tue, 22 Feb 2022 11:38:59 +0100 [thread overview]
Message-ID: <dfd4aeb85da37632c0ecabdc1d978c6cd236b89c.1645526008.git.christophe.leroy@csgroup.eu> (raw)
In-Reply-To: <cover.1645526008.git.christophe.leroy@csgroup.eu>
Perform layout alignment verification up front and WARN_ON()
and fail module loading instead of crashing the machine.
Signed-off-by: Christophe Leroy <christophe.leroy@csgroup.eu>
---
kernel/module/internal.h | 1 +
kernel/module/main.c | 5 +++++
kernel/module/strict_rwx.c | 28 +++++++++++++++++++---------
3 files changed, 25 insertions(+), 9 deletions(-)
diff --git a/kernel/module/internal.h b/kernel/module/internal.h
index b80a746b2e5a..32add08e7895 100644
--- a/kernel/module/internal.h
+++ b/kernel/module/internal.h
@@ -187,6 +187,7 @@ void module_enable_nx(const struct module *mod);
void module_enable_x(const struct module *mod);
int module_enforce_rwx_sections(Elf_Ehdr *hdr, Elf_Shdr *sechdrs,
char *secstrings, struct module *mod);
+bool module_check_misalignment(const struct module *mod);
#ifdef CONFIG_MODULE_SIG
int module_sig_check(struct load_info *info, int flags);
diff --git a/kernel/module/main.c b/kernel/module/main.c
index 47addf849ef7..4876e2beb5b6 100644
--- a/kernel/module/main.c
+++ b/kernel/module/main.c
@@ -2596,6 +2596,9 @@ static int complete_formation(struct module *mod, struct load_info *info)
/* This relies on module_mutex for list integrity. */
module_bug_finalize(info->hdr, info->sechdrs, mod);
+ if (module_check_misalignment(mod))
+ goto out_misaligned;
+
module_enable_ro(mod, false);
module_enable_nx(mod);
module_enable_x(mod);
@@ -2609,6 +2612,8 @@ static int complete_formation(struct module *mod, struct load_info *info)
return 0;
+out_misaligned:
+ err = -EINVAL;
out:
mutex_unlock(&module_mutex);
return err;
diff --git a/kernel/module/strict_rwx.c b/kernel/module/strict_rwx.c
index b023cea06af0..3966198a4fc0 100644
--- a/kernel/module/strict_rwx.c
+++ b/kernel/module/strict_rwx.c
@@ -42,9 +42,6 @@ static void frob_text(const struct module_layout *layout,
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);
}
@@ -52,9 +49,6 @@ static void frob_rodata(const struct module_layout *layout,
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);
}
@@ -62,13 +56,29 @@ static void frob_ro_after_init(const struct module_layout *layout,
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);
}
+static bool layout_check_misalignment(const struct module_layout *layout)
+{
+ return WARN_ON(!PAGE_ALIGNED(layout->base)) ||
+ WARN_ON(!PAGE_ALIGNED(layout->text_size)) ||
+ WARN_ON(!PAGE_ALIGNED(layout->ro_size)) ||
+ WARN_ON(!PAGE_ALIGNED(layout->ro_after_init_size)) ||
+ WARN_ON(!PAGE_ALIGNED(layout->size));
+}
+
+bool module_check_misalignment(const struct module *mod)
+{
+ if (!IS_ENABLED(CONFIG_STRICT_MODULE_RWX))
+ return false;
+
+ return layout_check_misalignment(&mod->core_layout) ||
+ layout_check_misalignment(&mod->data_layout) ||
+ layout_check_misalignment(&mod->init_layout);
+}
+
void module_enable_x(const struct module *mod)
{
if (!PAGE_ALIGNED(mod->core_layout.base) ||
--
2.34.1
next prev parent reply other threads:[~2022-02-22 10:39 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-02-22 10:38 [PATCH v1 0/6] Miscellaneous cleanups Christophe Leroy
2022-02-22 10:38 ` [PATCH v1 1/6] module: Have kernel/module/ dedicated to CONFIG_MODULES Christophe Leroy
2022-02-22 10:38 ` [PATCH v1 2/6] module: Move module's Kconfig item in kernel/module/ Christophe Leroy
2022-02-22 10:38 ` [PATCH v1 3/6] module: Make module_enable_x() independent of CONFIG_ARCH_HAS_STRICT_MODULE_RWX Christophe Leroy
2022-02-22 10:38 ` [PATCH v1 4/6] module: Move module_enable_x() and frob_text() in strict_rwx.c Christophe Leroy
2022-02-22 10:38 ` Christophe Leroy [this message]
2022-02-22 10:39 ` [PATCH v1 6/6] module: Rename debug_align() as strict_align() 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=dfd4aeb85da37632c0ecabdc1d978c6cd236b89c.1645526008.git.christophe.leroy@csgroup.eu \
--to=christophe.leroy@csgroup.eu \
--cc=atomlin@redhat.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-modules@vger.kernel.org \
--cc=mcgrof@kernel.org \
/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®