module: core code clean up
Subsystems (2)
v5 cover module: core code clean up
Hi Luis,
As per your suggestion [1], this is an attempt to refactor and split optional code out of core module support code into separate components. This version is based on Linus' commit e6251ab4551f ("Merge tag 'nfs-for-5.17-2' of git://git.linux-nfs.org/projects/anna/linux-nfs").
Please let me know your thoughts. So far, no feedback from 0-day; albeit, if I see something, I'll let you know.
Changes since v1 [2]:
- Moved module version support code into a new file
Changes since v2 [3]:
- Moved module decompress support to a separate file
- Made check_modinfo_livepatch() generic (Petr Mladek)
- Removed filename from each newly created file (Luis Chamberlain)
- Addressed some (i.e. --ignore=ASSIGN_IN_IF,AVOID_BUG was used) minor scripts/checkpatch.pl concerns e.g., use strscpy over strlcpy and missing a blank line after declarations (Allen)
Changes since v3 [4]:
- Refactored both is_livepatch_module() and set_livepatch_module(), respectively, to use IS_ENABLED(CONFIG_LIVEPATCH) (Joe Perches)
- Addressed various compiler warnings e.g., no previous prototype (0-day)
Changes since v4 [5]:
- Dropped RFC (Luis Chamberlain)
- Moved is_livepatch_module() and set_livepatch_module() to kernel/module/livepatch.c; set_livepatch_module() will remain for internal use only
- Addressed minor compiler warning concerning kernel/module/internal.h (0-day)
- Resolved style violations reported by scripts/checkpatch.pl
- Dropped patch 5 [6] so external patch [7] can be applied at a later date post merge into module-next (Christophe Leroy)
[1]: https://lore.kernel.org/lkml/[email protected]/
[2]: https://lore.kernel.org/lkml/[email protected]/
[3]: https://lore.kernel.org/lkml/[email protected]/
[4]: https://lore.kernel.org/lkml/[email protected]/
[5]: https://lore.kernel.org/lkml/[email protected]/
[6]: https://lore.kernel.org/lkml/[email protected]/
[7]: https://lore.kernel.org/lkml/203348805c9ac9851d8939d15cb9802ef047b5e2.1643919758.git.christophe.leroy@csgroup.eu/
Aaron Tomlin (13):
module: Move all into module/
module: Simple refactor in preparation for split
module: Make internal.h more compliant
module: Move livepatch support to a separate file
module: Move latched RB-tree support to a separate file
module: Move strict rwx support to a separate file
module: Move extra signature support out of core code
module: Move kmemleak support to a separate file
module: Move kallsyms support into a separate file
module: Move procfs support into a separate file
module: Move sysfs support into a separate file
module: Move kdb_modules list out of core code
module: Move version support into a separate file
MAINTAINERS | 2 +-
include/linux/module.h | 10 +-
kernel/Makefile | 5 +-
kernel/debug/kdb/kdb_main.c | 5 +
kernel/module-internal.h | 50 -
kernel/module/Makefile | 19 +
kernel/module/debug_kmemleak.c | 30 +
.../decompress.c} | 2 +-
kernel/module/internal.h | 283 +++
kernel/module/kallsyms.c | 502 +++++
kernel/module/livepatch.c | 80 +
kernel/{module.c => module/main.c} | 1862 +----------------
kernel/module/procfs.c | 142 ++
.../signature.c} | 0
kernel/module/signing.c | 120 ++
kernel/module/strict_rwx.c | 84 +
kernel/module/sysfs.c | 425 ++++
kernel/module/tree_lookup.c | 109 +
kernel/module/version.c | 110 +
kernel/module_signing.c | 45 -
20 files changed, 2009 insertions(+), 1876 deletions(-)
delete mode 100644 kernel/module-internal.h
create mode 100644 kernel/module/Makefile
create mode 100644 kernel/module/debug_kmemleak.c
rename kernel/{module_decompress.c => module/decompress.c} (99%)
create mode 100644 kernel/module/internal.h
create mode 100644 kernel/module/kallsyms.c
create mode 100644 kernel/module/livepatch.c
rename kernel/{module.c => module/main.c} (64%)
create mode 100644 kernel/module/procfs.c
rename kernel/{module_signature.c => module/signature.c} (100%)
create mode 100644 kernel/module/signing.c
create mode 100644 kernel/module/strict_rwx.c
create mode 100644 kernel/module/sysfs.c
create mode 100644 kernel/module/tree_lookup.c
create mode 100644 kernel/module/version.c
delete mode 100644 kernel/module_signing.cbase-commit: e6251ab4551f51fa4cee03523e08051898c3ce82
Signature · 1 line
2.34.1
PATCH v5 1/13 module: Move all into module/
No functional changes.
This patch moves all module related code into a separate directory, modifies each file name and creates a new Makefile. Note: this effort is in preparation to refactor core module code.
---
MAINTAINERS | 2 +-
kernel/Makefile | 5 +----
kernel/module/Makefile | 9 +++++++++
kernel/{module_decompress.c => module/decompress.c} | 2 +-
kernel/{module-internal.h => module/internal.h} | 1 +
kernel/{module.c => module/main.c} | 2 +-
kernel/{module_signature.c => module/signature.c} | 0
kernel/{module_signing.c => module/signing.c} | 2 +-
8 files changed, 15 insertions(+), 8 deletions(-)
create mode 100644 kernel/module/Makefile
rename kernel/{module_decompress.c => module/decompress.c} (99%)
rename kernel/{module-internal.h => module/internal.h} (99%)
rename kernel/{module.c => module/main.c} (99%)
rename kernel/{module_signature.c => module/signature.c} (100%)
rename kernel/{module_signing.c => module/signing.c} (97%)MAINTAINERS +1 -1
@@ -13001,7 +13001,7 @@ L: [email protected] S: Maintained T: git git://git.kernel.org/pub/scm/linux/kernel/git/mcgrof/linux.git modules-next F: include/linux/module.h -F: kernel/module.c +F: kernel/module/main.c MONOLITHIC POWER SYSTEM PMIC DRIVER M: Saravanan Sekar <[email protected]>
kernel/Makefile +1 -4
@@ -53,6 +53,7 @@ obj-y += rcu/ obj-y += livepatch/ obj-y += dma/ obj-y += entry/ +obj-y += module/ obj-$(CONFIG_KCMP) += kcmp.o obj-$(CONFIG_FREEZER) += freezer.o
@@ -66,10 +67,6 @@ ifneq ($(CONFIG_SMP),y) obj-y += up.o endif obj-$(CONFIG_UID16) += uid16.o -obj-$(CONFIG_MODULES) += module.o -obj-$(CONFIG_MODULE_DECOMPRESS) += module_decompress.o -obj-$(CONFIG_MODULE_SIG) += module_signing.o -obj-$(CONFIG_MODULE_SIG_FORMAT) += module_signature.o obj-$(CONFIG_KALLSYMS) += kallsyms.o obj-$(CONFIG_BSD_PROCESS_ACCT) += acct.o obj-$(CONFIG_CRASH_CORE) += crash_core.o
kernel/module/Makefile new file +9 -0
@@ -0,0 +1,9 @@ +# SPDX-License-Identifier: GPL-2.0-only +# +# Makefile for linux kernel module support +# + +obj-$(CONFIG_MODULES) += main.o +obj-$(CONFIG_MODULE_DECOMPRESS) += decompress.o +obj-$(CONFIG_MODULE_SIG) += signing.o +obj-$(CONFIG_MODULE_SIG_FORMAT) += signature.o
kernel/module/decompress.c renamed from kernel/module_decompress.c +1 -1
@@ -12,7 +12,7 @@#include <linux/sysfs.h> #include <linux/vmalloc.h> -#include "module-internal.h" +#include "internal.h" static int module_extend_max_pages (struct load_info *info,unsigned int extent) {
kernel/module/internal.h renamed from kernel/module-internal.h +1 -0
@@ -44,6 +44,7 @@ static inline int module_decompress(struct load_info *info, {return -EOPNOTSUPP ; } +static inline void module_decompress_cleanup (struct load_info *info) { }
kernel/module/main.c renamed from kernel/module.c +1 -1
@@ -58,7 +58,7 @@#include <linux/dynamic_debug.h> #include <linux/audit.h> #include <uapi/linux/module.h> -#include "module-internal.h" +#include "internal.h" #define CREATE_TRACE_POINTS #include <trace/events/module.h>
kernel/module/signature.c renamed from kernel/module_signature.c +0 -0
kernel/module/signing.c renamed from kernel/module_signing.c +1 -1
@@ -12,7 +12,7 @@#include <linux/string.h> #include <linux/verification.h> #include <crypto/public_key.h> -#include "module-internal.h" +#include "internal.h" /* * Verify the signature on a module.
Signature · 1 line
2.34.1
Re: [PATCH v5 01/13] module: Move all into module/
Le 09/02/2022 à 18:03, Aaron Tomlin a écrit :
Quoted 29 lines
No functional changes.
This patch moves all module related code into a separate directory, modifies each file name and creates a new
Makefile. Note: this effort is in preparation to refactor core module code.Signed-off-by Aaron Tomlin <[email protected]>--- MAINTAINERS | 2 +- kernel/Makefile | 5 +---- kernel/module/Makefile | 9 +++++++++ kernel/{module_decompress.c => module/decompress.c} | 2 +- kernel/{module-internal.h => module/internal.h} | 1 + kernel/{module.c => module/main.c} | 2 +- kernel/{module_signature.c => module/signature.c} | 0 kernel/{module_signing.c => module/signing.c} | 2 +- 8 files changed, 15 insertions(+), 8 deletions(-)create mode 100644
kernel/module/Makefile
renamekernel/{module_decompress.c=>module/decompress.c} (99%) renamekernel/{module-internal.h=>module/internal.h} (99%) renamekernel/{module.c=>module/main.c} (99%) renamekernel/{module_signature.c=>module/signature.c} (100%) renamekernel/{module_signing.c=>module/signing.c} (97%)MAINTAINERS +0
-0
MAINTAINERS L: [email protected] · line 13004
Quoted 5 lines
MAINTAINERS +1
-1@@ -13001,7 +13001,7 @@ L: [email protected] S: Maintained T: git git://git.kernel.org/pub/scm/linux/kernel/git/mcgrof/linux.git modules-next F: include/linux/module.h -F: kernel/module.c +F: kernel/module/main.c
Shouldn't it be the entire directory ?
Otherwise you have to add all new files (and I didn't do it).
Quoted 65 lines
MAINTAINERS +0
-0MONOLITHIC POWER SYSTEM PMIC DRIVER M: Saravanan Sekar <[email protected]>kernel/Makefile +1
-4@@ -53,6 +53,7 @@ obj-y += rcu/ obj-y += livepatch/ obj-y += dma/ obj-y += entry/ +obj-y += module/ obj-$(CONFIG_KCMP) += kcmp.o obj-$(CONFIG_FREEZER) += freezer.o@@ -66,10 +67,6 @@ ifneq ($(CONFIG_SMP),y) obj-y += up.o endif obj-$(CONFIG_UID16) += uid16.o -obj-$(CONFIG_MODULES) += module.o -obj-$(CONFIG_MODULE_DECOMPRESS) += module_decompress.o -obj-$(CONFIG_MODULE_SIG) += module_signing.o -obj-$(CONFIG_MODULE_SIG_FORMAT) += module_signature.o obj-$(CONFIG_KALLSYMS) += kallsyms.o obj-$(CONFIG_BSD_PROCESS_ACCT) += acct.o obj-$(CONFIG_CRASH_CORE) += crash_core.okernel/module/Makefile new file +9
-0@@ -0,0 +1,9 @@ +# SPDX-License-Identifier: GPL-2.0-only +# +# Makefile for linux kernel module support +# + +obj-$(CONFIG_MODULES) += main.o +obj-$(CONFIG_MODULE_DECOMPRESS) += decompress.o +obj-$(CONFIG_MODULE_SIG) += signing.o +obj-$(CONFIG_MODULE_SIG_FORMAT) += signature.okernel/module/decompress.c renamed from kernel/module_decompress.c +1
-1@@ -12,7 +12,7 @@#include <linux/sysfs.h> #include <linux/vmalloc.h> -#include "module-internal.h" +#include "internal.h" static int module_extend_max_pages (struct load_info *info,unsigned int extent) {kernel/module/internal.h renamed from kernel/module-internal.h +0
-0
kernel/module/internal.h static inline int module_decompress(struct load_info *info, · line 47
Quoted 4 lines
kernel/module/internal.h renamed from kernel/module-internal.h +1
-0@@ -44,6 +44,7 @@ static inline int module_decompress(struct load_info *info, {return -EOPNOTSUPP ; } +
This new line should be in patch 3 instead.
Quoted 39 lines
kernel/module/internal.h renamed from kernel/module-internal.h +0
-0static inline void module_decompress_cleanup (struct load_info *info) { }kernel/module/main.c renamed from kernel/module.c +1
-1@@ -58,7 +58,7 @@#include <linux/dynamic_debug.h> #include <linux/audit.h> #include <uapi/linux/module.h> -#include "module-internal.h" +#include "internal.h" #define CREATE_TRACE_POINTS #include <trace/events/module.h> kernel/module/signature.c renamed from kernel/module_signature.c +0
-0kernel/module/signing.c renamed from kernel/module_signing.c +1
-1@@ -12,7 +12,7 @@#include <linux/string.h> #include <linux/verification.h> #include <crypto/public_key.h> -#include "module-internal.h" +#include "internal.h" /* * Verify the signature on a module.
Re: [PATCH v5 01/13] module: Move all into module/
Quoted 4 lines · Christophe Leroy
Quoted 4 lines
MAINTAINERS +0
-0
Quoted 8 lines
MAINTAINERS L: [email protected] · line 13004
Quoted 6 lines
MAINTAINERS +1
-1@@ -13001,7 +13001,7 @@ L: [email protected] S: Maintained T: git git://git.kernel.org/pub/scm/linux/kernel/git/mcgrof/linux.git modules-next F: include/linux/module.h -F: kernel/module.c +F: kernel/module/main.cShouldn't it be the entire directory ?
Firstly, thank you for your feedback Christophe.
Indeed it should. Moving forward: kernel/module/*
Quoted 7 lines
MAINTAINERS static inline int module_decompress(struct load_info *info, · line 47
Quoted 5 lines
MAINTAINERS +1
-0@@ -44,6 +44,7 @@ static inline int module_decompress(struct load_info *info, { return -EOPNOTSUPP; } +This new line should be in patch 3 instead.
Fair enough. Given that the purpose of this particular patch is a simple migration, style violations e.g. "Please use a blank line after function/struct/union/enum declarations", can be resolved at a later stage.
Kind regards,
Signature · 1 line
Aaron Tomlin
Re: [PATCH v5 01/13] module: Move all into module/
Quoted 2 lines · Aaron Tomlin
Quoted 1 line · Christophe Leroy
MAINTAINERS
Quoted 1 line
MAINTAINERS +0
-0
[]
Quoted 10 lines
Quoted 8 lines
MAINTAINERS L: [email protected] · line 13004
Quoted 6 lines
MAINTAINERS +1
-1@@ -13001,7 +13001,7 @@ L: [email protected] S: Maintained T: git git://git.kernel.org/pub/scm/linux/kernel/git/mcgrof/linux.git modules-next F: include/linux/module.h -F: kernel/module.c +F: kernel/module/main.cShouldn't it be the entire directory ?
Indeed it should. Moving forward:
kernel/module/*
Better would be:
F: kernel/module/
in case it ever gets subdirectories too.
Re: [PATCH v5 01/13] module: Move all into module/
Quoted 6 lines · Joe Perches
On Thu, 2022-02-10 at 14:45 +0000, Aaron Tomlin wrote:
Better would be:F:
kernel/module/in case it ever gets subdirectories too.
Agreed.
Signature · 1 line
Aaron Tomlin
Luis Chamberlain
maintainerRe: [PATCH v5 01/13] module: Move all into module/
In reply to Joe PerchesQuoted 13 lines · Joe Perches
Quoted 2 lines · Aaron Tomlin
Quoted 1 line · Christophe Leroy
MAINTAINERS
Quoted 1 line
MAINTAINERS +0
-0[]
Quoted 9 lines
Quoted 8 lines
MAINTAINERS L: [email protected] · line 13004
Quoted 6 lines
MAINTAINERS +1
-1@@ -13001,7 +13001,7 @@ L: [email protected] S: Maintained T: git git://git.kernel.org/pub/scm/linux/kernel/git/mcgrof/linux.git modules-next F: include/linux/module.h -F: kernel/module.c +F: kernel/module/main.cShouldn't it be the entire directory ?
Quoted 7 lines
Quoted 1 line
Indeed it should. Moving forward:
kernel/module/*Better would be:
F:
kernel/module/in case it ever gets subdirectories too.
Yes, that also later allows us to add entries for things like say livepatching. Or we peg the livepatching file to the livepatching tag. Either way, one of the side goals of the split is to eventually help scale maintenance.
Luis
PATCH v5 2/13 module: Simple refactor in preparation for split
In reply to Aaron TomlinNo functional change.
This patch makes it possible to move non-essential code out of core module code.
--- kernel/module/internal.h | 22 ++++++++++++++++++++++ kernel/module/main.c | 23 ++--------------------- 2 files changed, 24 insertions(+), 21 deletions(-)
kernel/module/internal.h +22 -0
@@ -7,6 +7,28 @@#include <linux/elf.h> #include <asm/module.h> +#include <linux/mutex.h> + +#ifndef ARCH_SHF_SMALL +#define ARCH_SHF_SMALL 0 +#endif + +/* If this is set, the section belongs in the init part of the module */ +#define INIT_OFFSET_MASK (1UL << (BITS_PER_LONG-1)) +/* Maximum number of characters written by module_flags() */ +#define MODULE_FLAGS_BUF_SIZE (TAINT_FLAGS_COUNT + 4) +#define MODULE_SECT_READ_SIZE (3 /* "0x", "\n" */ + (BITS_PER_LONG / 4)) + +extern struct mutex module_mutex; +extern struct list_head modules; + +/* Provided by the linker */ +extern const struct kernel_symbol __start___ksymtab[]; +extern const struct kernel_symbol __stop___ksymtab[]; +extern const struct kernel_symbol __start___ksymtab_gpl[]; +extern const struct kernel_symbol __stop___ksymtab_gpl[]; +extern const s32 __start___kcrctab[]; +extern const s32 __start___kcrctab_gpl[];struct load_info {const char *name;
kernel/module/main.c +2 -21
@@ -63,10 +63,6 @@#define CREATE_TRACE_POINTS #include <trace/events/module.h> -#ifndef ARCH_SHF_SMALL -#define ARCH_SHF_SMALL 0 -#endif -/* * Modules' sections will be aligned on page boundaries * to ensure complete separation of code and data, but
@@ -78,9 +74,6 @@# define debug_align (X ) (X)#endif -/* If this is set, the section belongs in the init part of the module */ -#define INIT_OFFSET_MASK (1UL << (BITS_PER_LONG-1)) -/* * Mutex protects: * 1) List of modules (also safely readable with preempt_disable),
@@ -88,8 +81,8 @@* 3) module_addr_min/module_addr_max. * (delete and add uses RCU list operations). */ -static DEFINE_MUTEX (module_mutex); -static LIST_HEAD (modules); +DEFINE_MUTEX (module_mutex); +LIST_HEAD (modules);/* Work queue for freeing init sections in success case */ static void do_free_init (struct work_struct *w);
@@ -408,14 +401,6 @@ static __maybe_unused void *any_section_objs(const struct load_info *info,return (void *)info->sechdrs[sec].sh_addr; } -/* Provided by the linker */ -extern const struct kernel_symbol __start___ksymtab[]; -extern const struct kernel_symbol __stop___ksymtab[]; -extern const struct kernel_symbol __start___ksymtab_gpl[]; -extern const struct kernel_symbol __stop___ksymtab_gpl[]; -extern const s32 __start___kcrctab[]; -extern const s32 __start___kcrctab_gpl[]; -#ifndef CONFIG_MODVERSIONS #define symversion (base, idx) NULL#else
@@ -1490,7 +1475,6 @@ struct module_sect_attrs {struct module_sect_attr attrs[]; }; -#define MODULE_SECT_READ_SIZE (3 /* "0x", "\n" */ + (BITS_PER_LONG / 4))static ssize_t module_sect_read (struct file *file,struct kobject *kobj,struct bin_attribute *battr,char *buf,loff_t pos,size_t count)
@@ -4542,9 +4526,6 @@ static void cfi_cleanup(struct module *mod)#endif } -/* Maximum number of characters written by module_flags() */ -#define MODULE_FLAGS_BUF_SIZE (TAINT_FLAGS_COUNT + 4) -/* Keep in sync with MODULE_FLAGS_BUF_SIZE !!! */ static char *module_flags (struct module *mod,char *buf) {
Signature · 1 line
2.34.1
Re: [PATCH v5 02/13] module: Simple refactor in preparation for split
Le 09/02/2022 à 18:03, Aaron Tomlin a écrit :
Quoted 20 lines
No functional change.
This patch makes it possible to move non-essential code out of core module code.
Signed-off-by Aaron Tomlin <[email protected]>--- kernel/module/internal.h | 22 ++++++++++++++++++++++ kernel/module/main.c | 23 ++--------------------- 2 files changed, 24 insertions(+), 21 deletions(-)kernel/module/internal.h +1
-0@@ -7,6 +7,28 @@#include <linux/elf.h> #include <asm/module.h> +#include <linux/mutex.h>
kernel/module/internal.h line 20
Quoted 10 lines
kernel/module/internal.h +10
-0+ +#ifndef ARCH_SHF_SMALL +#define ARCH_SHF_SMALL 0 +#endif + +/* If this is set, the section belongs in the init part of the module */ +#define INIT_OFFSET_MASK (1UL << (BITS_PER_LONG-1)) +/* Maximum number of characters written by module_flags() */ +#define MODULE_FLAGS_BUF_SIZE (TAINT_FLAGS_COUNT + 4) +#define MODULE_SECT_READ_SIZE (3 /* "0x", "\n" */ + (BITS_PER_LONG / 4))
This is used only in sysfs.c, why move it to internal.h ?
Quoted 66 lines
kernel/module/internal.h +11
-0+ +extern struct mutex module_mutex; +extern struct list_head modules; + +/* Provided by the linker */ +extern const struct kernel_symbol __start___ksymtab[]; +extern const struct kernel_symbol __stop___ksymtab[]; +extern const struct kernel_symbol __start___ksymtab_gpl[]; +extern const struct kernel_symbol __stop___ksymtab_gpl[]; +extern const s32 __start___kcrctab[]; +extern const s32 __start___kcrctab_gpl[];struct load_info {const char *name;kernel/module/main.c +2
-17@@ -63,10 +63,6 @@#define CREATE_TRACE_POINTS #include <trace/events/module.h> -#ifndef ARCH_SHF_SMALL -#define ARCH_SHF_SMALL 0 -#endif -/* * Modules' sections will be aligned on page boundaries * to ensure complete separation of code and data, but @@ -78,9 +74,6 @@# define debug_align (X ) (X)#endif -/* If this is set, the section belongs in the init part of the module */ -#define INIT_OFFSET_MASK (1UL << (BITS_PER_LONG-1)) -/* * Mutex protects: * 1) List of modules (also safely readable with preempt_disable), @@ -88,8 +81,8 @@* 3) module_addr_min/module_addr_max. * (delete and add uses RCU list operations). */ -static DEFINE_MUTEX (module_mutex); -static LIST_HEAD (modules); +DEFINE_MUTEX (module_mutex); +LIST_HEAD (modules);/* Work queue for freeing init sections in success case */ static void do_free_init (struct work_struct *w);@@ -408,14 +401,6 @@ static __maybe_unused void *any_section_objs(const struct load_info *info,return (void *)info->sechdrs[sec].sh_addr; } -/* Provided by the linker */ -extern const struct kernel_symbol __start___ksymtab[]; -extern const struct kernel_symbol __stop___ksymtab[]; -extern const struct kernel_symbol __start___ksymtab_gpl[]; -extern const struct kernel_symbol __stop___ksymtab_gpl[]; -extern const s32 __start___kcrctab[]; -extern const s32 __start___kcrctab_gpl[]; -#ifndef CONFIG_MODVERSIONS #define symversion (base, idx) NULL#else
kernel/module/main.c struct module_sect_attrs { · line 1493
Quoted 4 lines
kernel/module/main.c +0
-1@@ -1490,7 +1475,6 @@ struct module_sect_attrs {struct module_sect_attr attrs[]; }; -#define MODULE_SECT_READ_SIZE (3 /* "0x", "\n" */ + (BITS_PER_LONG / 4))
This is used only in sysfs.c, why move it to internal.h ?
Quoted 13 lines
kernel/module/main.c +0
-3static ssize_t module_sect_read (struct file *file,struct kobject *kobj,struct bin_attribute *battr,char *buf,loff_t pos,size_t count)@@ -4542,9 +4526,6 @@ static void cfi_cleanup(struct module *mod)#endif } -/* Maximum number of characters written by module_flags() */ -#define MODULE_FLAGS_BUF_SIZE (TAINT_FLAGS_COUNT + 4) -/* Keep in sync with MODULE_FLAGS_BUF_SIZE !!! */ static char *module_flags (struct module *mod,char *buf) {
Re: [PATCH v5 02/13] module: Simple refactor in preparation for split
Quoted 3 lines · Christophe Leroy
Quoted 1 line
+#define
MODULE_SECT_READ_SIZE(3 /* "0x", "\n" */ + (BITS_PER_LONG/ 4))This is used only in
sysfs.c, why move it tointernal.h?
Agreed. Since use is exclusive to kernel/module/sysfs.c it should be moved.
Kind regards,
Signature · 1 line
Aaron Tomlin
PATCH v5 3/13 module: Make internal.h more compliant
In reply to Aaron TomlinThis patch will address the following warning and style violations generated by ./scripts/checkpatch.pl:
WARNING: Use #include <linux/module.h> instead of <asm/module.h> #10: FILE: kernel/module/internal.h:10:
+#include <asm/module.h>
CHECK: spaces preferred around that '-' (ctx:VxV) #18: FILE: kernel/module/internal.h:18:
+#define INIT_OFFSET_MASK (1UL << (BITS_PER_LONG-1))
^
CHECK: extern prototypes should be avoided in .h files #84: FILE: kernel/module/internal.h:84:
+extern int mod_verify_sig(const void *mod, struct load_info *info);
Note: Fortunately, the multiple-include optimisation found in include/linux/module.h will prevent duplication/or inclusion more than once.
--- kernel/module/internal.h | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)
kernel/module/internal.h +4 -3
@@ -6,7 +6,8 @@*/ #include <linux/elf.h> -#include <asm/module.h> +#include <linux/compiler.h> +#include <linux/module.h> #include <linux/mutex.h> #ifndef ARCH_SHF_SMALL
@@ -14,7 +15,7 @@#endif /* If this is set, the section belongs in the init part of the module */ -#define INIT_OFFSET_MASK (1UL << (BITS_PER_LONG-1)) +#define INIT_OFFSET_MASK (1UL << (BITS_PER_LONG - 1))/* Maximum number of characters written by module_flags() */ #define MODULE_FLAGS_BUF_SIZE (TAINT_FLAGS_COUNT + 4) #define MODULE_SECT_READ_SIZE (3 /* "0x", "\n" */ + (BITS_PER_LONG / 4))
@@ -55,7 +56,7 @@ struct load_info { } index; }; -extern int mod_verify_sig (const void *mod,struct load_info *info); +int mod_verify_sig (const void *mod,struct load_info *info);#ifdef CONFIG_MODULE_DECOMPRESS int module_decompress (struct load_info *info,const void *buf,size_t size);
Signature · 1 line
2.34.1
Re: [PATCH v5 03/13] module: Make internal.h more compliant
Le 09/02/2022 à 18:03, Aaron Tomlin a écrit :
Quoted 15 lines
This patch will address the following warning and style violations generated by
./scripts/checkpatch.pl:WARNING: Use #include <linux/module.h> instead of <asm/module.h> #10: FILE: kernel/module/internal.h:10: +#include <asm/module.h> CHECK: spaces preferred around that '-' (ctx:VxV) #18: FILE: kernel/module/internal.h:18: +#define INIT_OFFSET_MASK (1UL << (BITS_PER_LONG-1)) ^ CHECK: extern prototypes should be avoided in .h files #84: FILE: kernel/module/internal.h:84: +extern int mod_verify_sig(const void *mod, struct load_info *info);
Quoted 6 lines
Note: Fortunately, the multiple-include optimisation found in
include/linux/module.hwill prevent duplication/or inclusion more than once.Fixes f314dfea16a ("modsign: log module name in the event of an error")Signed-off-by Aaron Tomlin <[email protected]>
Quoted 36 lines
--- kernel/module/internal.h | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)kernel/module/internal.h +4
-3@@ -6,7 +6,8 @@*/ #include <linux/elf.h> -#include <asm/module.h> +#include <linux/compiler.h> +#include <linux/module.h> #include <linux/mutex.h> #ifndef ARCH_SHF_SMALL @@ -14,7 +15,7 @@#endif /* If this is set, the section belongs in the init part of the module */ -#define INIT_OFFSET_MASK (1UL << (BITS_PER_LONG-1)) +#define INIT_OFFSET_MASK (1UL << (BITS_PER_LONG - 1))/* Maximum number of characters written by module_flags() */ #define MODULE_FLAGS_BUF_SIZE (TAINT_FLAGS_COUNT + 4) #define MODULE_SECT_READ_SIZE (3 /* "0x", "\n" */ + (BITS_PER_LONG / 4))@@ -55,7 +56,7 @@ struct load_info { } index; }; -extern int mod_verify_sig (const void *mod,struct load_info *info); +int mod_verify_sig (const void *mod,struct load_info *info);#ifdef CONFIG_MODULE_DECOMPRESS int module_decompress (struct load_info *info,const void *buf,size_t size);
PATCH v5 4/13 module: Move livepatch support to a separate file
In reply to Aaron TomlinNo functional change.
This patch migrates livepatch support (i.e. used during module add/or load and remove/or deletion) from core module code into kernel/module/livepatch.c. At the moment it contains code to persist Elf information about a given livepatch module, only.
--- include/linux/module.h | 5 +- kernel/module/Makefile | 3 ++ kernel/module/internal.h | 18 +++++++ kernel/module/livepatch.c | 80 ++++++++++++++++++++++++++++++ kernel/module/main.c | 102 ++++---------------------------------- 5 files changed, 112 insertions(+), 96 deletions(-) create mode 100644 kernel/module/livepatch.c
include/linux/module.h +1 -4
@@ -664,10 +664,7 @@ static inline bool module_requested_async_probing(struct module *module) }#ifdef CONFIG_LIVEPATCH -static inline bool is_livepatch_module (struct module *mod) -{ -return mod->klp; -} +bool is_livepatch_module (struct module *mod);#else /* !CONFIG_LIVEPATCH */ static inline bool is_livepatch_module (struct module *mod) {
kernel/module/Makefile +3 -0
@@ -7,3 +7,6 @@ obj-$(CONFIG_MODULES) += main.o obj-$(CONFIG_MODULE_DECOMPRESS) += decompress.o obj-$(CONFIG_MODULE_SIG) += signing.o obj-$(CONFIG_MODULE_SIG_FORMAT) += signature.o +ifdef CONFIG_MODULES +obj-$(CONFIG_LIVEPATCH) += livepatch.o +endif
kernel/module/internal.h +18 -0
@@ -58,6 +58,24 @@ struct load_info {int mod_verify_sig (const void *mod,struct load_info *info); +#ifdef CONFIG_LIVEPATCH +int copy_module_elf (struct module *mod,struct load_info *info); +void free_module_elf (struct module *mod); +bool set_livepatch_module (struct module *mod); +#else /* !CONFIG_LIVEPATCH */ +static inline int copy_module_elf (struct module *mod,struct load_info *info) +{ +return 0 ; +} + +static inline bool set_livepatch_module (struct module *mod) +{ +return false; +} + +static inline void free_module_elf (struct module *mod) { } +#endif /* CONFIG_LIVEPATCH */ +#ifdef CONFIG_MODULE_DECOMPRESS int module_decompress (struct load_info *info,const void *buf,size_t size);void module_decompress_cleanup (struct load_info *info);
kernel/module/livepatch.c new file +80 -0
80 lines not on this page · raw message
kernel/module/main.c +10 -92
121 lines not on this page · raw message
Signature · 1 line
2.34.1
Re: [PATCH v5 04/13] module: Move livepatch support to a separate file
Le 09/02/2022 à 18:03, Aaron Tomlin a écrit :
Quoted 6 lines
No functional change.
This patch migrates livepatch support (i.e. used during module add/or load and remove/or deletion) from core module code into
kernel/module/livepatch.c. At the moment it contains code to persist Elf information about a given livepatch module, only.
LD .tmp_vmlinux.kallsyms1
powerpc64-linux-ld: kernel/livepatch/core.o: in function `klp_enable_patch':
(.text+0x1a0c): undefined reference to `is_livepatch_module'
powerpc64-linux-ld: kernel/module/main.o: in function `free_module':
main.c:(.text+0x50f4): undefined reference to `is_livepatch_module'
powerpc64-linux-ld: kernel/module/main.o: in function `load_module':
main.c:(.text+0x6f20): undefined reference to `is_livepatch_module'
powerpc64-linux-ld: main.c:(.text+0x8390): undefined reference to `is_livepatch_module'
powerpc64-linux-ld: main.c:(.text+0x8d6c): undefined reference to `is_livepatch_module'
make: *** [Makefile:1155 : vmlinux] Erreur 1
I don't understand why you are uninlining that so simple function. Such a function is likely a single instruction in assembly, it is definitely not worth inlining.
Christophe
Quoted 280 lines
Signed-off-by Aaron Tomlin <[email protected]>--- include/linux/module.h | 5 +- kernel/module/Makefile | 3 ++ kernel/module/internal.h | 18 +++++++ kernel/module/livepatch.c | 80 ++++++++++++++++++++++++++++++ kernel/module/main.c | 102 ++++---------------------------------- 5 files changed, 112 insertions(+), 96 deletions(-)create mode 100644
kernel/module/livepatch.cinclude/linux/module.h +1
-4@@ -664,10 +664,7 @@ static inline bool module_requested_async_probing(struct module *module) }#ifdef CONFIG_LIVEPATCH -static inline bool is_livepatch_module (struct module *mod) -{ -return mod->klp; -} +bool is_livepatch_module (struct module *mod);#else /* !CONFIG_LIVEPATCH */ static inline bool is_livepatch_module (struct module *mod) {kernel/module/Makefile +3
-0@@ -7,3 +7,6 @@ obj-$(CONFIG_MODULES) += main.o obj-$(CONFIG_MODULE_DECOMPRESS) += decompress.o obj-$(CONFIG_MODULE_SIG) += signing.o obj-$(CONFIG_MODULE_SIG_FORMAT) += signature.o +ifdef CONFIG_MODULES +obj-$(CONFIG_LIVEPATCH) += livepatch.o +endifkernel/module/internal.h +18
-0@@ -58,6 +58,24 @@ struct load_info {int mod_verify_sig (const void *mod,struct load_info *info); +#ifdef CONFIG_LIVEPATCH +int copy_module_elf (struct module *mod,struct load_info *info); +void free_module_elf (struct module *mod); +bool set_livepatch_module (struct module *mod); +#else /* !CONFIG_LIVEPATCH */ +static inline int copy_module_elf (struct module *mod,struct load_info *info) +{ +return 0 ; +} + +static inline bool set_livepatch_module (struct module *mod) +{ +return false; +} + +static inline void free_module_elf (struct module *mod) { } +#endif /* CONFIG_LIVEPATCH */ +#ifdef CONFIG_MODULE_DECOMPRESS int module_decompress (struct load_info *info,const void *buf,size_t size);void module_decompress_cleanup (struct load_info *info);kernel/module/livepatch.c new file +80
-0@@ -0,0 +1,80 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* +* Module livepatch support +* +* Copyright (C) 2016 Jessica Yu <[email protected]> +*/ + +#include <linux/module.h> +#include <linux/string.h> +#include <linux/slab.h> +#include "internal.h" + +/* +* Persist Elf information about a module. Copy the Elf header, +* section header table, section string table, and symtab section +* index from info to mod->klp_info. +*/ +int copy_module_elf (struct module *mod,struct load_info *info) +{ +unsigned int size, symndx; +int ret; + + size =sizeof (*mod->klp_info); + mod->klp_info =kmalloc (size,GFP_KERNEL ); +if (mod->klp_info ==NULL ) +return -ENOMEM ; + +/* Elf header */ + size =sizeof (mod->klp_info->hdr); +memcpy (&mod->klp_info->hdr, info->hdr, size); + +/* Elf section header table */ + size =sizeof (*info->sechdrs) * info->hdr->e_shnum; + mod->klp_info->sechdrs =kmemdup (info->sechdrs, size,GFP_KERNEL ); +if (mod->klp_info->sechdrs ==NULL ) { + ret = -ENOMEM ; + goto free_info; + } + +/* Elf section name string table */ + size = info->sechdrs[info->hdr->e_shstrndx].sh_size; + mod->klp_info->secstrings =kmemdup (info->secstrings, size,GFP_KERNEL ); +if (mod->klp_info->secstrings ==NULL ) { + ret = -ENOMEM ; + goto free_sechdrs; + } + +/* Elf symbol section index */ + symndx = info->index.sym; + mod->klp_info->symndx = symndx; + +/* +* For livepatch modules, core_kallsyms.symtab is a complete +* copy of the original symbol table. Adjust sh_addr to point +* to core_kallsyms.symtab since the copy of the symtab in module +* init memory is freed at the end of do_init_module(). +*/ + mod->klp_info->sechdrs[symndx].sh_addr = (unsigned long ) mod->core_kallsyms.symtab; + +return 0 ; + +free_sechdrs: +kfree (mod->klp_info->sechdrs); +free_info: +kfree (mod->klp_info); +return ret; +} + +void free_module_elf (struct module *mod) +{ +kfree (mod->klp_info->sechdrs); +kfree (mod->klp_info->secstrings); +kfree (mod->klp_info); +} + +inline bool set_livepatch_module (struct module *mod) +{ + mod->klp = true; +return true; +}kernel/module/main.c +10
-92@@ -2042,81 +2042,6 @@ static int module_enforce_rwx_sections(Elf_Ehdr *hdr, Elf_Shdr *sechdrs, }#endif /* CONFIG_STRICT_MODULE_RWX */ -#ifdef CONFIG_LIVEPATCH -/* -* Persist Elf information about a module. Copy the Elf header, -* section header table, section string table, and symtab section -* index from info to mod->klp_info. -*/ -static int copy_module_elf (struct module *mod,struct load_info *info) -{ -unsigned int size, symndx; -int ret; - - size =sizeof (*mod->klp_info); - mod->klp_info =kmalloc (size,GFP_KERNEL ); -if (mod->klp_info ==NULL ) -return -ENOMEM ; - -/* Elf header */ - size =sizeof (mod->klp_info->hdr); -memcpy (&mod->klp_info->hdr, info->hdr, size); - -/* Elf section header table */ - size =sizeof (*info->sechdrs) * info->hdr->e_shnum; - mod->klp_info->sechdrs =kmemdup (info->sechdrs, size,GFP_KERNEL ); -if (mod->klp_info->sechdrs ==NULL ) { - ret = -ENOMEM ; - goto free_info; - } - -/* Elf section name string table */ - size = info->sechdrs[info->hdr->e_shstrndx].sh_size; - mod->klp_info->secstrings =kmemdup (info->secstrings, size,GFP_KERNEL ); -if (mod->klp_info->secstrings ==NULL ) { - ret = -ENOMEM ; - goto free_sechdrs; - } - -/* Elf symbol section index */ - symndx = info->index.sym; - mod->klp_info->symndx = symndx; - -/* -* For livepatch modules, core_kallsyms.symtab is a complete -* copy of the original symbol table. Adjust sh_addr to point -* to core_kallsyms.symtab since the copy of the symtab in module -* init memory is freed at the end of do_init_module(). -*/ - mod->klp_info->sechdrs[symndx].sh_addr = \ - (unsigned long ) mod->core_kallsyms.symtab; - -return 0 ; - -free_sechdrs: -kfree (mod->klp_info->sechdrs); -free_info: -kfree (mod->klp_info); -return ret; -} - -static void free_module_elf (struct module *mod) -{ -kfree (mod->klp_info->sechdrs); -kfree (mod->klp_info->secstrings); -kfree (mod->klp_info); -} -#else /* !CONFIG_LIVEPATCH */ -static int copy_module_elf (struct module *mod,struct load_info *info) -{ -return 0 ; -} - -static void free_module_elf (struct module *mod) -{ -} -#endif /* CONFIG_LIVEPATCH */ -void __weak module_memfree (void *module_region) {/* @@ -3091,30 +3016,23 @@ static int copy_chunked_from_user(void *dst, const void __user *usrc, unsigned lreturn 0 ; } -#ifdef CONFIG_LIVEPATCH static int check_modinfo_livepatch (struct module *mod,struct load_info *info) { -if (get_modinfo (info,"livepatch" )) { - mod->klp = true; +if (!get_modinfo (info,"livepatch" )) +/* Nothing more to do */ +return 0 ; + +if (set_livepatch_module (mod)) {add_taint_module (mod,TAINT_LIVEPATCH ,LOCKDEP_STILL_OK );pr_notice_once ("%s: tainting kernel with TAINT_LIVEPATCH\n" , - mod->name); - } - -return 0 ; -} -#else /* !CONFIG_LIVEPATCH */ -static int check_modinfo_livepatch (struct module *mod,struct load_info *info) -{ -if (get_modinfo (info,"livepatch" )) { -pr_err ("%s: module is marked as livepatch module, but livepatch support is disabled" , - mod->name); -return -ENOEXEC ; + mod->name); +return 0 ; } -return 0 ; +pr_err ("%s: module is marked as livepatch module, but livepatch support is disabled" , + mod->name); +return -ENOEXEC ; } -#endif /* CONFIG_LIVEPATCH */ static void check_modinfo_retpoline (struct module *mod,struct load_info *info) {
Re: [PATCH v5 04/13] module: Move livepatch support to a separate file
In reply to Aaron TomlinLe 09/02/2022 à 18:03, Aaron Tomlin a écrit :
Quoted 22 lines
No functional change.
This patch migrates livepatch support (i.e. used during module add/or load and remove/or deletion) from core module code into
kernel/module/livepatch.c. At the moment it contains code to persist Elf information about a given livepatch module, only.Signed-off-by Aaron Tomlin <[email protected]>--- include/linux/module.h | 5 +- kernel/module/Makefile | 3 ++ kernel/module/internal.h | 18 +++++++ kernel/module/livepatch.c | 80 ++++++++++++++++++++++++++++++ kernel/module/main.c | 102 ++++---------------------------------- 5 files changed, 112 insertions(+), 96 deletions(-)create mode 100644
kernel/module/livepatch.cinclude/linux/module.h +0
-0
include/linux/module.h static inline bool module_requested_async_probing(struct module *module) · line 667
Quoted 8 lines
include/linux/module.h +1
-4@@ -664,10 +664,7 @@ static inline bool module_requested_async_probing(struct module *module) }#ifdef CONFIG_LIVEPATCH -static inline bool is_livepatch_module (struct module *mod) -{ -return mod->klp; -} +bool is_livepatch_module (struct module *mod);
This change is wrong, build fails with it because is_livepatch_module() is nowhere defined.
You could move is_livepatch_module() to include/linux/livepatch.h but as a separate patch.
kernel/module/Makefile obj-$(CONFIG_MODULES) += main.o · line 10
Quoted 12 lines
include/linux/module.h +0
-0#else /* !CONFIG_LIVEPATCH */ static inline bool is_livepatch_module (struct module *mod) {kernel/module/Makefile +1
-0@@ -7,3 +7,6 @@ obj-$(CONFIG_MODULES) += main.o obj-$(CONFIG_MODULE_DECOMPRESS) += decompress.o obj-$(CONFIG_MODULE_SIG) += signing.o obj-$(CONFIG_MODULE_SIG_FORMAT) += signature.o +ifdef CONFIG_MODULES
CONFIG_LIVEPATCH depends on CONFIG_MODULES so this ifdef is not needed (See kernel/livepatch/Kconfig)
kernel/module/livepatch.c
Quoted 36 lines
kernel/module/Makefile +2
-0+obj-$(CONFIG_LIVEPATCH) += livepatch.o +endifkernel/module/internal.h +18
-0@@ -58,6 +58,24 @@ struct load_info {int mod_verify_sig (const void *mod,struct load_info *info); +#ifdef CONFIG_LIVEPATCH +int copy_module_elf (struct module *mod,struct load_info *info); +void free_module_elf (struct module *mod); +bool set_livepatch_module (struct module *mod); +#else /* !CONFIG_LIVEPATCH */ +static inline int copy_module_elf (struct module *mod,struct load_info *info) +{ +return 0 ; +} + +static inline bool set_livepatch_module (struct module *mod) +{ +return false; +} + +static inline void free_module_elf (struct module *mod) { } +#endif /* CONFIG_LIVEPATCH */ +#ifdef CONFIG_MODULE_DECOMPRESS int module_decompress (struct load_info *info,const void *buf,size_t size);void module_decompress_cleanup (struct load_info *info);kernel/module/livepatch.c new file +0
-0
Checkpatch reports the following:
WARNING: added, moved or deleted file(s), does MAINTAINERS need updating? #80:
new file mode 100644
CHECK: Comparison to NULL could be written "!mod->klp_info" #109: FILE: kernel/module/livepatch.c:25:
+ if (mod->klp_info == NULL)
CHECK: Comparison to NULL could be written "!mod->klp_info->sechdrs" #119: FILE: kernel/module/livepatch.c:35:
+ if (mod->klp_info->sechdrs == NULL) {
CHECK: Comparison to NULL could be written "!mod->klp_info->secstrings" #127: FILE: kernel/module/livepatch.c:43:
+ if (mod->klp_info->secstrings == NULL) {
CHECK: No space is necessary after a cast
#142: FILE: kernel/module/livepatch.c:58:
+ mod->klp_info->sechdrs[symndx].sh_addr = (unsigned long)mod->core_kallsyms.symtab;
Quoted 1 line
+inline bool
set_livepatch_module(struct module *mod)
'inline' keyword is pointless here, as far as this function is in a .c and is not static, it won't be inlined.
Given how simple this function is, it should be a 'static inline' in internal.c
Quoted 31 lines
kernel/module/livepatch.c new file +4
-0+{ + mod->klp = true; +return true; +}kernel/module/main.c +5
-6@@ -3091,30 +3016,23 @@ static int copy_chunked_from_user(void *dst, const void __user *usrc, unsigned lreturn 0 ; } -#ifdef CONFIG_LIVEPATCH static int check_modinfo_livepatch (struct module *mod,struct load_info *info) { -if (get_modinfo (info,"livepatch" )) { - mod->klp = true; +if (!get_modinfo (info,"livepatch" )) +/* Nothing more to do */ +return 0 ; + +if (set_livepatch_module (mod)) {add_taint_module (mod,TAINT_LIVEPATCH ,LOCKDEP_STILL_OK ); pr_notice_once("%s: tainting kernel with TAINT_LIVEPATCH\n", - mod->name); - } -
kernel/module/main.c static int copy_chunked_from_user(void *dst, const void __user *usrc, unsigned l · line 3028
Quoted 10 lines
kernel/module/main.c +1
-9- return 0; -} -#else /* !CONFIG_LIVEPATCH */ -static int check_modinfo_livepatch (struct module *mod,struct load_info *info) -{ -if (get_modinfo (info,"livepatch" )) { -pr_err ("%s: module is marked as livepatch module, but livepatch support is disabled" , - mod->name); -return -ENOEXEC ; + mod->name);
This change seems wrong, mod->name must remain aligned to open parenthesis.
kernel/module/main.c static int copy_chunked_from_user(void *dst, const void __user *usrc, unsigned l · line 3033
Quoted 6 lines
kernel/module/main.c +3
-1+return 0 ; } -return 0 ; +pr_err ("%s: module is marked as livepatch module, but livepatch support is disabled" , + mod->name);
CHECK: Alignment should match open parenthesis
#285: FILE: kernel/module/main.c:3033:
+ pr_err("%s: module is marked as livepatch module, but livepatch
support is disabled",
+ mod->name);
Quoted 6 lines
kernel/module/main.c +1
-1+return -ENOEXEC ; } -#endif /* CONFIG_LIVEPATCH */ static void check_modinfo_retpoline (struct module *mod,struct load_info *info) {
Re: [PATCH v5 04/13] module: Move livepatch support to a separate file
Quoted 2 lines · Christophe Leroy
This change is wrong, build fails with it because
is_livepatch_module()is nowhere defined.
Yes, sorry about that. This was an omission/or oversight during the rebase attempt.
Quoted 2 lines
You could move
is_livepatch_module()toinclude/linux/livepatch.hbut as a separate patch.
Fair enough. Albeit, I'd prefer to revert and keep is_livepatch_module() in include/linux/module.h - this is likely the best solution.
Note: set_livepatch_module() will remain for internal use only.
Quoted 6 lines
Quoted 6 lines
#else /* !CONFIG_LIVEPATCH */ static inline bool is_livepatch_module(struct module *mod) {kernel/module/Makefile +0
-1--- a/kernel/module/Makefile
Quoted 8 lines
kernel/module/Makefile obj-$(CONFIG_MODULES) += main.o · line 10
Quoted 6 lines
+++ b/kernel/module/Makefile
kernel/module/Makefile +1
-0@@ -7,3 +7,6 @@ obj-$(CONFIG_MODULES) += main.o obj-$(CONFIG_MODULE_DECOMPRESS) += decompress.o obj-$(CONFIG_MODULE_SIG) += signing.o obj-$(CONFIG_MODULE_SIG_FORMAT) += signature.o +ifdef CONFIG_MODULES
CONFIG_LIVEPATCHdepends onCONFIG_MODULESso this ifdef is not needed
Agreed.
Quoted 14 lines
Checkpatch reports the following:
WARNING: added, moved or deleted file(s), does
MAINTAINERSneed updating? #80:
new file mode 100644CHECK: Comparison to NULL could be written "!mod->klp_info" #109: FILE:
kernel/module/livepatch.c:25:
+ if (mod->klp_info== NULL)CHECK: Comparison to NULL could be written "!mod->klp_info->sechdrs" #119: FILE:
kernel/module/livepatch.c:35:
+ if (mod->klp_info->sechdrs== NULL) {
Quoted 8 lines
CHECK: Comparison to NULL could be written "!mod->klp_info->secstrings" #127: FILE:
kernel/module/livepatch.c:43:
+ if (mod->klp_info->secstrings== NULL) {CHECK: No space is necessary after a cast
#142: FILE:kernel/module/livepatch.c:58:
+ mod->klp_info->sechdrs[symndx].sh_addr = (unsigned long)mod->core_kallsyms.symtab;
Ok.
Quoted 2 lines
Given how simple this function is, it should be a 'static inline' in
internal.c
Agreed.
Quoted 5 lines
CHECK: Alignment should match open parenthesis
#285: FILE:kernel/module/main.c:3033:
+ pr_err("%s: module is marked as livepatch module, but livepatch
support is disabled",
+mod->name);
Fair enough.
Kind regards,
Signature · 1 line
Aaron Tomlin
PATCH v5 5/13 module: Move latched RB-tree support to a separate file
In reply to Aaron TomlinNo functional change.
This patch migrates module latched RB-tree support (e.g. see __module_address()) from core module code into kernel/module/tree_lookup.c.
--- include/linux/module.h | 4 +- kernel/module/Makefile | 1 + kernel/module/internal.h | 34 ++++++++++ kernel/module/main.c | 129 +----------------------------------- kernel/module/tree_lookup.c | 109 ++++++++++++++++++++++++++++++ 5 files changed, 148 insertions(+), 129 deletions(-) create mode 100644 kernel/module/tree_lookup.c
include/linux/module.h +2 -2
@@ -342,9 +342,9 @@ struct module_layout {#ifdef CONFIG_MODULES_TREE_LOOKUP /* Only touch one cacheline for common rbtree-for-core-layout case. */ #define __module_layout_align ____cacheline_aligned -#else +#else /* !CONFIG_MODULES_TREE_LOOKUP */ #define __module_layout_align -#endif +#endif /* CONFIG_MODULES_TREE_LOOKUP */ struct mod_kallsyms {Elf_Sym *symtab;
kernel/module/Makefile +1 -0
@@ -9,4 +9,5 @@ obj-$(CONFIG_MODULE_SIG) += signing.o obj-$(CONFIG_MODULE_SIG_FORMAT) += signature.o ifdef CONFIG_MODULES obj-$(CONFIG_LIVEPATCH) += livepatch.o +obj-$(CONFIG_MODULES_TREE_LOOKUP) += tree_lookup.o endif
kernel/module/internal.h +34 -0
@@ -9,6 +9,7 @@#include <linux/compiler.h> #include <linux/module.h> #include <linux/mutex.h> +#include <linux/rculist.h> #ifndef ARCH_SHF_SMALL #define ARCH_SHF_SMALL 0
@@ -90,3 +91,36 @@ static inline void module_decompress_cleanup(struct load_info *info) { }#endif + +#ifdef CONFIG_MODULES_TREE_LOOKUP +struct mod_tree_root { +struct latch_tree_root root; +unsigned long addr_min; +unsigned long addr_max; +}; + +extern struct mod_tree_root mod_tree; + +void mod_tree_insert (struct module *mod); +void mod_tree_remove_init (struct module *mod); +void mod_tree_remove (struct module *mod); +struct module *mod_find (unsigned long addr); +#else /* !CONFIG_MODULES_TREE_LOOKUP */ +static unsigned long module_addr_min =-1UL , module_addr_max; + +static void mod_tree_insert (struct module *mod) { } +static void mod_tree_remove_init (struct module *mod) { } +static void mod_tree_remove (struct module *mod) { } +static struct module *mod_find (unsigned long addr) +{ +struct module *mod; + +list_for_each_entry_rcu (mod, &modules, list, +lockdep_is_held (&module_mutex)) { +if (within_module (addr, mod)) +return mod; + } + +return NULL ; +} +#endif /* CONFIG_MODULES_TREE_LOOKUP */
kernel/module/main.c +2 -127
140 lines not on this page · raw message
kernel/module/tree_lookup.c new file +109 -0
109 lines not on this page · raw message
Signature · 1 line
2.34.1
Re: [PATCH v5 05/13] module: Move latched RB-tree support to a separate file
Le 09/02/2022 à 18:03, Aaron Tomlin a écrit :
Quoted 21 lines
No functional change.
This patch migrates module latched RB-tree support (e.g. see
__module_address()) from core module code intokernel/module/tree_lookup.c.Signed-off-by Aaron Tomlin <[email protected]>--- include/linux/module.h | 4 +- kernel/module/Makefile | 1 + kernel/module/internal.h | 34 ++++++++++ kernel/module/main.c | 129 +----------------------------------- kernel/module/tree_lookup.c | 109 ++++++++++++++++++++++++++++++ 5 files changed, 148 insertions(+), 129 deletions(-)create mode 100644
kernel/module/tree_lookup.cinclude/linux/module.h +0
-0
include/linux/module.h struct module_layout { · line 347
Quoted 8 lines
include/linux/module.h +2
-2@@ -342,9 +342,9 @@ struct module_layout {#ifdef CONFIG_MODULES_TREE_LOOKUP /* Only touch one cacheline for common rbtree-for-core-layout case. */ #define __module_layout_align ____cacheline_aligned -#else +#else /* !CONFIG_MODULES_TREE_LOOKUP */ #define __module_layout_align -#endif +#endif /* CONFIG_MODULES_TREE_LOOKUP */
What's the added value of those two changes ? That's a five lines #ifdef block without any other nested #ifdef.
Commenting an #else / #endif is only usefull when the block is more than one screen or when there are nested #ifdef inside the block.
Please keep changes at the minimum.
Quoted 35 lines
include/linux/module.h +0
-0struct mod_kallsyms {Elf_Sym *symtab;kernel/module/Makefile +1
-0@@ -9,4 +9,5 @@ obj-$(CONFIG_MODULE_SIG) += signing.o obj-$(CONFIG_MODULE_SIG_FORMAT) += signature.o ifdef CONFIG_MODULES obj-$(CONFIG_LIVEPATCH) += livepatch.o +obj-$(CONFIG_MODULES_TREE_LOOKUP) += tree_lookup.o endifkernel/module/internal.h +7
-0@@ -9,6 +9,7 @@#include <linux/compiler.h> #include <linux/module.h> #include <linux/mutex.h> +#include <linux/rculist.h> #ifndef ARCH_SHF_SMALL #define ARCH_SHF_SMALL 0@@ -90,3 +91,36 @@ static inline void module_decompress_cleanup(struct load_info *info) { }#endif + +#ifdef CONFIG_MODULES_TREE_LOOKUP +struct mod_tree_root { + struct latch_tree_root root; + unsigned long addr_min; + unsigned long addr_max;
kernel/module/internal.h static inline void module_decompress_cleanup(struct load_info *info) · line 109
Quoted 10 lines
kernel/module/internal.h +10
-0+}; + +extern struct mod_tree_root mod_tree; + +void mod_tree_insert (struct module *mod); +void mod_tree_remove_init (struct module *mod); +void mod_tree_remove (struct module *mod); +struct module *mod_find (unsigned long addr); +#else /* !CONFIG_MODULES_TREE_LOOKUP */ +static unsigned long module_addr_min =-1UL , module_addr_max;
This is wrong to put that in a .h.
By chance module_addr_min re used only in main.c but if they were used in another file you would get two independant versions of it.
So leave it in main.c, anyway it's going away with my series.
kernel/module/internal.h static inline void module_decompress_cleanup(struct load_info *info) · line 114
Quoted 5 lines
kernel/module/internal.h +5
-0+ +static void mod_tree_insert (struct module *mod) { } +static void mod_tree_remove_init (struct module *mod) { } +static void mod_tree_remove (struct module *mod) { } +static struct module *mod_find (unsigned long addr)
Also keep mod_find() in main.c, or make it a 'static inline'. Otherwise it will be duplicated in every file including internal.h
Quoted 175 lines
kernel/module/internal.h +12
-0+{ +struct module *mod; + +list_for_each_entry_rcu (mod, &modules, list, +lockdep_is_held (&module_mutex)) { +if (within_module (addr, mod)) +return mod; + } + +return NULL ; +} +#endif /* CONFIG_MODULES_TREE_LOOKUP */ kernel/module/main.c +2
-127@@ -90,138 +90,13 @@ static DECLARE_WORK(init_free_wq, do_free_init);static LLIST_HEAD (init_free_list);#ifdef CONFIG_MODULES_TREE_LOOKUP - -/* -* Use a latched RB-tree for __module_address(); this allows us to use -* RCU-sched lookups of the address from any context. -* -* This is conditional on PERF_EVENTS || TRACING because those can really hit -* __module_address() hard by doing a lot of stack unwinding; potentially from -* NMI context. -*/ - -static __always_inline unsigned long __mod_tree_val (struct latch_tree_node *n) -{ - struct module_layout *layout = container_of(n, struct module_layout, mtn.node); - -return (unsigned long )layout->base; -} - -static __always_inline unsigned long __mod_tree_size (struct latch_tree_node *n) -{ - struct module_layout *layout = container_of(n, struct module_layout, mtn.node); - -return (unsigned long )layout->size; -} - -static __always_inline bool -mod_tree_less (struct latch_tree_node *a,struct latch_tree_node *b) -{ -return __mod_tree_val (a) <__mod_tree_val (b); -} - -static __always_inline int -mod_tree_comp (void *key,struct latch_tree_node *n) -{ -unsigned long val = (unsigned long )key; -unsigned long start, end; - - start =__mod_tree_val (n); -if (val < start) -return -1 ; - - end = start +__mod_tree_size (n); -if (val >= end) -return 1 ; - -return 0 ; -} - -static const struct latch_tree_ops mod_tree_ops = { - .less = mod_tree_less, - .comp = mod_tree_comp, -}; - -static struct mod_tree_root { -struct latch_tree_root root; -unsigned long addr_min; -unsigned long addr_max; -} mod_tree __cacheline_aligned = { +struct mod_tree_root mod_tree __cacheline_aligned = { .addr_min =-1UL , };#define module_addr_min mod_tree.addr_min#define module_addr_max mod_tree.addr_max - -static noinline void __mod_tree_insert(struct mod_tree_node *node) -{ -latch_tree_insert (&node->node, &mod_tree.root, &mod_tree_ops); -} - -static void __mod_tree_remove (struct mod_tree_node *node) -{ -latch_tree_erase (&node->node, &mod_tree.root, &mod_tree_ops); -} - -/* -* These modifications: insert, remove_init and remove; are serialized by the -* module_mutex. -*/ -static void mod_tree_insert (struct module *mod) -{ - mod->core_layout.mtn.mod = mod; - mod->init_layout.mtn.mod = mod; - -__mod_tree_insert (&mod->core_layout.mtn); -if (mod->init_layout.size) -__mod_tree_insert (&mod->init_layout.mtn); -} - -static void mod_tree_remove_init (struct module *mod) -{ -if (mod->init_layout.size) -__mod_tree_remove (&mod->init_layout.mtn); -} - -static void mod_tree_remove (struct module *mod) -{ -__mod_tree_remove (&mod->core_layout.mtn); -mod_tree_remove_init (mod); -} - -static struct module *mod_find (unsigned long addr) -{ -struct latch_tree_node *ltn; - - ltn =latch_tree_find ((void *)addr, &mod_tree.root, &mod_tree_ops); -if (!ltn) -return NULL ; - - return container_of(ltn, struct mod_tree_node, node)->mod; -} - -#else /* MODULES_TREE_LOOKUP */ - -static unsigned long module_addr_min =-1UL , module_addr_max =0 ; - -static void mod_tree_insert (struct module *mod) { } -static void mod_tree_remove_init (struct module *mod) { } -static void mod_tree_remove (struct module *mod) { } - -static struct module *mod_find (unsigned long addr) -{ -struct module *mod; - -list_for_each_entry_rcu (mod, &modules, list, -lockdep_is_held (&module_mutex)) { -if (within_module (addr, mod)) -return mod; - } - -return NULL ; -} - -#endif /* MODULES_TREE_LOOKUP */ +#endif /* * Bounds of module text, for speeding up __module_address. kernel/module/tree_lookup.c new file +12
-0@@ -0,0 +1,109 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* +* Modules tree lookup +* +* Copyright (C) 2015 Peter Zijlstra +* Copyright (C) 2015 Rusty Russell +*/ + +#include <linux/module.h> +#include <linux/rbtree_latch.h> +#include "internal.h" +
kernel/module/tree_lookup.c line 22
Quoted 10 lines
kernel/module/tree_lookup.c new file +10
-0+/* +* Use a latched RB-tree for __module_address(); this allows us to use +* RCU-sched lookups of the address from any context. +* +* This is conditional on PERF_EVENTS || TRACING because those can really hit +* __module_address() hard by doing a lot of stack unwinding; potentially from +* NMI context. +*/ + +__always_inline unsigned long __mod_tree_val (struct latch_tree_node *n)
Should be static.
kernel/module/tree_lookup.c line 29
Quoted 7 lines
kernel/module/tree_lookup.c new file +7
-0+{ + struct module_layout *layout = container_of(n, struct module_layout, mtn.node); + +return (unsigned long )layout->base; +} + +__always_inline unsigned long __mod_tree_size (struct latch_tree_node *n)
Should be static.
kernel/module/tree_lookup.c line 37
Quoted 8 lines
kernel/module/tree_lookup.c new file +8
-0+{ + struct module_layout *layout = container_of(n, struct module_layout, mtn.node); + +return (unsigned long )layout->size; +} + +__always_inline bool +mod_tree_less (struct latch_tree_node *a,struct latch_tree_node *b)
Should be static.
kernel/module/tree_lookup.c line 43
Quoted 6 lines
kernel/module/tree_lookup.c new file +6
-0+{ +return __mod_tree_val (a) <__mod_tree_val (b); +} + +__always_inline int +mod_tree_comp (void *key,struct latch_tree_node *n)
Should be static.
Quoted 9 lines
kernel/module/tree_lookup.c new file +9
-0+{ +unsigned long val = (unsigned long )key; +unsigned long start, end; + + start =__mod_tree_val (n); +if (val < start) +return -1 ; + + end = start +__mod_tree_size (n);
kernel/module/tree_lookup.c line 62
Quoted 10 lines
kernel/module/tree_lookup.c new file +10
-0+if (val >= end) +return 1 ; + +return 0 ; +} + +const struct latch_tree_ops mod_tree_ops = { + .less = mod_tree_less, + .comp = mod_tree_comp, +};
Should be static.
Quoted 47 lines
kernel/module/tree_lookup.c new file +47
-0+ +static noinline void __mod_tree_insert(struct mod_tree_node *node) +{ +latch_tree_insert (&node->node, &mod_tree.root, &mod_tree_ops); +} + +static void __mod_tree_remove (struct mod_tree_node *node) +{ +latch_tree_erase (&node->node, &mod_tree.root, &mod_tree_ops); +} + +/* +* These modifications: insert, remove_init and remove; are serialized by the +* module_mutex. +*/ +void mod_tree_insert (struct module *mod) +{ + mod->core_layout.mtn.mod = mod; + mod->init_layout.mtn.mod = mod; + +__mod_tree_insert (&mod->core_layout.mtn); +if (mod->init_layout.size) +__mod_tree_insert (&mod->init_layout.mtn); +} + +void mod_tree_remove_init (struct module *mod) +{ +if (mod->init_layout.size) +__mod_tree_remove (&mod->init_layout.mtn); +} + +void mod_tree_remove (struct module *mod) +{ +__mod_tree_remove (&mod->core_layout.mtn); +mod_tree_remove_init (mod); +} + +struct module *mod_find (unsigned long addr) +{ +struct latch_tree_node *ltn; + + ltn =latch_tree_find ((void *)addr, &mod_tree.root, &mod_tree_ops); +if (!ltn) +return NULL ; + + return container_of(ltn, struct mod_tree_node, node)->mod; +}
Re: [PATCH v5 05/13] module: Move latched RB-tree support to a separate file
Quoted 7 lines · Christophe Leroy
Quoted 7 lines
include/linux/module.h +0
-0@@ -342,9 +342,9 @@ struct module_layout {#ifdef CONFIG_MODULES_TREE_LOOKUP /* Only touch one cacheline for common rbtree-for-core-layout case. */
Quoted 8 lines
include/linux/module.h struct module_layout { · line 347
Quoted 6 lines
include/linux/module.h +2
-2#define __module_layout_align ____cacheline_aligned -#else +#else /* !CONFIG_MODULES_TREE_LOOKUP */ #define __module_layout_align -#endif +#endif /* CONFIG_MODULES_TREE_LOOKUP */ Commenting an #else / #endif is only usefull when the block is more than one screen or when there are nested #ifdef inside the block.
For me, this is a personal style preference. That being said, fair enough.
Quoted 5 lines
include/linux/module.h struct module_layout { · line 349
Quoted 2 lines
include/linux/module.h +2
-0+#else /* !CONFIG_MODULES_TREE_LOOKUP */ +static unsigned long module_addr_min =-1UL , module_addr_max;This is wrong to put that in a .h.
I understand. This was an oversight. I'll move this to kernel/module/main.c in preparation for your work.
Quoted 7 lines
include/linux/module.h struct module_layout {
Quoted 4 lines
include/linux/module.h +4
-0+static void mod_tree_insert (struct module *mod) { } +static void mod_tree_remove_init (struct module *mod) { } +static void mod_tree_remove (struct module *mod) { } +static struct module *mod_find (unsigned long addr)Also keep
mod_find()inmain.c, or make it a 'static inline'. Otherwise it will be duplicated in every file includinginternal.h
Agreed. This too was an oversight. I'll use the 'inline' keyword here.
Quoted 44 lines
Quoted 18 lines
kernel/module/tree_lookup.c new file +12
-0@@ -0,0 +1,109 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* +* Modules tree lookup +* +* Copyright (C) 2015 Peter Zijlstra +* Copyright (C) 2015 Rusty Russell +*/ + +#include <linux/module.h> +#include <linux/rbtree_latch.h> +#include "internal.h" +kernel/module/tree_lookup.c line 22
Quoted 10 lines
kernel/module/tree_lookup.c new file +10
-0+/* +* Use a latched RB-tree for __module_address(); this allows us to use +* RCU-sched lookups of the address from any context. +* +* This is conditional on PERF_EVENTS || TRACING because those can really hit +* __module_address() hard by doing a lot of stack unwinding; potentially from +* NMI context. +*/ + +__always_inline unsigned long __mod_tree_val (struct latch_tree_node *n)Should be static.
kernel/module/tree_lookup.c line 29
Quoted 1 line
kernel/module/tree_lookup.c new file +1
-0+__always_inline unsigned long __mod_tree_size (struct latch_tree_node *n)Should be static.
kernel/module/tree_lookup.c line 37
Quoted 2 lines
kernel/module/tree_lookup.c new file +2
-0+__always_inline bool +mod_tree_less (struct latch_tree_node *a,struct latch_tree_node *b)Should be static.
kernel/module/tree_lookup.c line 43
Quoted 2 lines
kernel/module/tree_lookup.c new file +2
-0+__always_inline int +mod_tree_comp (void *key,struct latch_tree_node *n)Should be static.
Quoted 6 lines
kernel/module/tree_lookup.c line 62
Quoted 4 lines
kernel/module/tree_lookup.c new file +4
-0+const struct latch_tree_ops mod_tree_ops = { + .less = mod_tree_less, + .comp = mod_tree_comp, +};Should be static.
Agreed. Only used in kernel/module/tree_lookup.c.
Signature · 1 line
Aaron Tomlin
PATCH v5 6/13 module: Move strict rwx support to a separate file
In reply to Aaron TomlinNo 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 intokernel/module/strict_rwx.c.
--- kernel/module/Makefile | 1 + kernel/module/internal.h | 38 +++++++++++++++ kernel/module/main.c | 97 +------------------------------------- kernel/module/strict_rwx.c | 84 +++++++++++++++++++++++++++++++++ 4 files changed, 124 insertions(+), 96 deletions(-) create mode 100644 kernel/module/strict_rwx.c
kernel/module/Makefile +1 -0
@@ -10,4 +10,5 @@ obj-$(CONFIG_MODULE_SIG_FORMAT) += signature.o ifdef CONFIG_MODULES obj-$(CONFIG_LIVEPATCH) += livepatch.o obj-$(CONFIG_MODULES_TREE_LOOKUP) += tree_lookup.o +obj-$(CONFIG_STRICT_MODULE_RWX) += strict_rwx.o endif
kernel/module/internal.h +38 -0
@@ -21,6 +21,17 @@#define MODULE_FLAGS_BUF_SIZE (TAINT_FLAGS_COUNT + 4) #define MODULE_SECT_READ_SIZE (3 /* "0x", "\n" */ + (BITS_PER_LONG / 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 ) ALIGN(X, PAGE_SIZE) +#else +# define debug_align (X ) (X) +#endif +extern struct mutex module_mutex;extern struct list_head modules;
@@ -124,3 +135,30 @@ static 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 frob_rodata (const struct module_layout *layout, +int (*set_memory)(unsigned long start,int num_pages)); +void frob_ro_after_init (const struct module_layout *layout, +int (*set_memory)(unsigned long start,int num_pages)); +void frob_writable_data (const struct module_layout *layout, +int (*set_memory)(unsigned long start,int num_pages)); +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 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 */
kernel/module/main.c +1 -96
115 lines not on this page · raw message
kernel/module/strict_rwx.c new file +84 -0
84 lines not on this page · raw message
Signature · 1 line
2.34.1
Re: [PATCH v5 06/13] module: Move strict rwx support to a separate file
Le 09/02/2022 à 18:03, Aaron Tomlin a écrit :
Quoted 7 lines
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 intokernel/module/strict_rwx.c.
Quoted 6 lines
Signed-off-by Aaron Tomlin <[email protected]>--- kernel/module/Makefile | 1 + kernel/module/internal.h | 38 +++++++++++++++ kernel/module/main.c | 97 +------------------------------------- kernel/module/strict_rwx.c | 84 +++++++++++++++++++++++++++++++++
This file generates many checkpatch WARNINGs and CHECKs.
Don't worry too much about the ones telling you to use WARN_ON() instead of BUG_ON() for the time being, but others should be handled.
Quoted 18 lines
4 files changed, 124 insertions(+), 96 deletions(-)create mode 100644
kernel/module/strict_rwx.ckernel/module/Makefile +1
-0@@ -10,4 +10,5 @@ obj-$(CONFIG_MODULE_SIG_FORMAT) += signature.o ifdef CONFIG_MODULES obj-$(CONFIG_LIVEPATCH) += livepatch.o obj-$(CONFIG_MODULES_TREE_LOOKUP) += tree_lookup.o +obj-$(CONFIG_STRICT_MODULE_RWX) += strict_rwx.o endifkernel/module/internal.h +0
-0
kernel/module/internal.h line 30
Quoted 10 lines
kernel/module/internal.h +7
-0@@ -21,6 +21,17 @@#define MODULE_FLAGS_BUF_SIZE (TAINT_FLAGS_COUNT + 4) #define MODULE_SECT_READ_SIZE (3 /* "0x", "\n" */ + (BITS_PER_LONG / 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 ) ALIGN(X, PAGE_SIZE)
You can use PAGE_ALIGN() instead.
kernel/module/internal.h static struct module *mod_find(unsigned long addr) · line 139
Quoted 13 lines
kernel/module/internal.h +6
-0+#else +# define debug_align (X ) (X) +#endif +extern struct mutex module_mutex;extern struct list_head modules;@@ -124,3 +135,30 @@ static struct module *mod_find(unsigned long addr) return NULL; }#endif /* CONFIG_MODULES_TREE_LOOKUP */ + +#ifdef CONFIG_ARCH_HAS_STRICT_MODULE_RWX
This #ifdef is not needed, frob_text() always exists.
Quoted 48 lines
kernel/module/internal.h +25
-0+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 frob_rodata (const struct module_layout *layout, +int (*set_memory)(unsigned long start,int num_pages)); +void frob_ro_after_init (const struct module_layout *layout, +int (*set_memory)(unsigned long start,int num_pages)); +void frob_writable_data (const struct module_layout *layout, +int (*set_memory)(unsigned long start,int num_pages)); +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 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 */ kernel/module/main.c +0
-11@@ -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),
kernel/module/main.c static void mod_sysfs_teardown(struct module *mod) · line 1808
Quoted 6 lines
kernel/module/main.c +1
-1@@ -1815,7 +1804,7 @@ 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, +void frob_text (const struct module_layout *layout,int (*set_memory)(unsigned long start,int num_pages))
The alignment of the second line is not correct.
Quoted 105 lines
kernel/module/main.c +0
-84{BUG_ON ((unsigned long )layout->base & (PAGE_SIZE -1 ));@@ -1833,90 +1822,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) {/* kernel/module/strict_rwx.c new file +6
-0@@ -0,0 +1,84 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* +* Module strict rwx +* +* Copyright (C) 2015 Rusty Russell +*/
kernel/module/strict_rwx.c line 16
Quoted 10 lines
kernel/module/strict_rwx.c new file +10
-0+ +#include <linux/module.h> +#include <linux/vmalloc.h> +#include <linux/set_memory.h> +#include "internal.h" + +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 ));
Could be:
BUG_ON(!PAGE_ALIGNED(layout->base));
Same for all others.
Quoted 68 lines
kernel/module/strict_rwx.c new file +68
-0+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 ); +} + +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 ); +} + +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 ); +} + +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 ; +}
Re: [PATCH v5 06/13] module: Move strict rwx support to a separate file
Quoted 3 lines · Christophe Leroy
This file generates many checkpatch WARNINGs and CHECKs. Don't worry too much about the ones telling you to use
WARN_ON()instead ofBUG_ON()for the time being, but others should be handled.
Yes, with ./scripts/checkpatch.pl --strict'.
Please note: I have used '--ignore=ASSIGN_IN_IF,AVOID_BUG' previously on that file. Albeit, I will resolve the check violations e.g. "Alignment should match open parenthesis" etc.
Quoted 3 lines
Quoted 1 line
+# define
debug_align(X)ALIGN(X, PAGE_SIZE)You can use
PAGE_ALIGN()instead.
Agreed: PAGE_ALIGN(X) does expand to ALIGN(X, PAGE_SIZE)
Quoted 3 lines
Quoted 1 line
+#ifdef
CONFIG_ARCH_HAS_STRICT_MODULE_RWXThis #ifdef is not needed,
frob_text()always exists.
I will leave this for you to remove, in your patch [1].
Quoted 8 lines
Quoted 1 line
+
BUG_ON((unsigned long)layout->base & (PAGE_SIZE-1));Could be:
BUG_ON(!PAGE_ALIGNED(layout->base));Same for all others.
Agreed.
Kind regards,
Signature · 1 line
Aaron Tomlin